OmniSciDB  1dac507f6e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CalciteServerCaller.java
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.mapd.parser.server;
17 
18 import static java.lang.System.exit;
19 
21 
22 import org.apache.commons.cli.CommandLine;
23 import org.apache.commons.cli.CommandLineParser;
24 import org.apache.commons.cli.DefaultParser;
25 import org.apache.commons.cli.HelpFormatter;
26 import org.apache.commons.cli.Option;
27 import org.apache.commons.cli.Options;
28 import org.apache.commons.cli.ParseException;
29 import org.apache.log4j.PropertyConfigurator;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 
33 import java.io.IOException;
34 import java.nio.file.FileSystemNotFoundException;
35 import java.nio.file.Path;
36 import java.nio.file.Paths;
37 import java.util.Properties;
38 
39 public class CalciteServerCaller {
42  private final static Logger MAPDLOGGER =
43  LoggerFactory.getLogger(CalciteServerCaller.class);
44  private CommandLine cmd = null;
45 
46  public static void main(String[] args) {
48  csc.doWork(args);
49  }
50 
51  private void doWork(String[] args) {
52  CalciteServerWrapper calciteServerWrapper = null;
53 
54  // create Options object
55  Options options = new Options();
56 
57  Option port =
58  Option.builder("p").hasArg().desc("port number").longOpt("port").build();
59 
60  Option ssl_trust_store = Option.builder("T")
61  .hasArg()
62  .desc("SSL_trust_store")
63  .longOpt("trust_store")
64  .build();
65 
66  Option ssl_trust_passwd = Option.builder("P")
67  .hasArg()
68  .desc("SSL_trust_password")
69  .longOpt("trust_store_pw")
70  .build();
71 
72  Option ssl_keystore =
73  Option.builder("Y").hasArg().desc("SSL keystore").longOpt("keystore").build();
74 
75  Option ssl_keystore_password = Option.builder("Z")
76  .hasArg()
77  .desc("SSL keystore password")
78  .longOpt("keystore_password")
79  .build();
80 
81  Option mapdPort = Option.builder("m")
82  .hasArg()
83  .desc("mapd port number")
84  .longOpt("mapd_port")
85  .build();
86 
87  Option data = Option.builder("d")
88  .hasArg()
89  .desc("data directory")
90  .required()
91  .longOpt("data")
92  .build();
93 
94  Option extensions = Option.builder("e")
95  .hasArg()
96  .desc("extension signatures directory")
97  .longOpt("extensions")
98  .build();
99 
100  Option udf_file = Option.builder("u")
101  .hasArg()
102  .desc("User Defined Functions file path")
103  .longOpt("udf")
104  .build();
105 
106  options.addOption(port);
107  options.addOption(data);
108  options.addOption(extensions);
109  options.addOption(mapdPort);
110  options.addOption(ssl_trust_store);
111  options.addOption(ssl_trust_passwd);
112  options.addOption(ssl_keystore);
113  options.addOption(ssl_keystore_password);
114  options.addOption(udf_file);
115 
116  CommandLineParser parser = new DefaultParser();
117 
118  try {
119  cmd = parser.parse(options, args);
120  } catch (ParseException ex) {
121  MAPDLOGGER.error(ex.getLocalizedMessage());
122  help(options);
123  exit(0);
124  }
125 
126  int portNum = Integer.valueOf(cmd.getOptionValue("port", "6279"));
127  int mapdPortNum = Integer.valueOf(cmd.getOptionValue("mapd_port", "6274"));
128  String dataDir = cmd.getOptionValue("data", "data");
129  String extensionsDir = cmd.getOptionValue("extensions", "build/QueryEngine");
130  String trust_store = cmd.getOptionValue("trust_store", "");
131  String trust_store_pw = cmd.getOptionValue("trust_store_pw", "");
132  String key_store = cmd.getOptionValue("keystore", "");
133  String key_store_pw = cmd.getOptionValue("keystore_password", "");
134  String udfName = cmd.getOptionValue("udf", "");
135 
136  final Path extensionFunctionsAstFile =
137  Paths.get(extensionsDir, "ExtensionFunctions.ast");
138 
139  // Add logging to our log files directories
140  Properties p = new Properties();
141  try {
142  p.load(getClass().getResourceAsStream("/log4j.properties"));
143  } catch (IOException ex) {
144  MAPDLOGGER.error(
145  "Could not load log4j property file from resources " + ex.getMessage());
146  }
147  p.put("log.dir", dataDir); // overwrite "log.dir"
148  PropertyConfigurator.configure(p);
149 
150  try {
151  if (trust_store == null || trust_store.isEmpty()) {
152  client_skT = SockTransportProperties.getUnencryptedClient();
153  // client_skT = new SockTransportProperties(load_trust_store);
154  } else {
155  client_skT = SockTransportProperties.getEncryptedClientSpecifiedTrustStore(
156  trust_store, trust_store_pw);
157  }
158 
159  if (key_store != null && !key_store.isEmpty()) {
160  // If suppled a key store load a server transport, otherwise server_skT can stay
161  // as null
162  server_skT = SockTransportProperties.getEncryptedServer(key_store, key_store_pw);
163  } else {
164  server_skT = SockTransportProperties.getUnecryptedServer();
165  }
166  } catch (Exception ex) {
167  MAPDLOGGER.error(
168  "Supplied java trust stored could not be opened " + ex.getMessage());
169  exit(0);
170  }
171 
172  Path udfPath;
173 
174  try {
175  if (!udfName.isEmpty()) {
176  udfPath = Paths.get(udfName);
177  }
178  } catch (FileSystemNotFoundException ex1) {
179  MAPDLOGGER.error("Could not load udf file " + ex1.getMessage());
180  }
181 
182  calciteServerWrapper = new CalciteServerWrapper(portNum,
183  mapdPortNum,
184  dataDir,
185  extensionFunctionsAstFile.toString(),
186  client_skT,
187  server_skT,
188  udfName);
189 
190  while (true) {
191  try {
192  calciteServerWrapper.run();
193  if (calciteServerWrapper.shutdown()) {
194  break;
195  }
196  try {
197  // wait for 4 secs before retry
198  Thread.sleep(4000);
199  } catch (InterruptedException ex) {
200  // noop
201  }
202  } catch (Exception x) {
203  x.printStackTrace();
204  }
205  }
206  }
207 
208  private void help(Options options) {
209  // automatically generate the help statement
210  HelpFormatter formatter = new HelpFormatter();
211  formatter.printHelp("CalciteServerCaller", options);
212  }
213 }