OmniSciDB  085a039ca4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CalciteServerCaller.java
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, 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.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31 
32 import java.io.BufferedReader;
33 import java.io.FileReader;
34 import java.io.IOException;
35 import java.io.StringReader;
36 import java.nio.file.FileSystemNotFoundException;
37 import java.nio.file.Path;
38 import java.nio.file.Paths;
39 import java.util.Properties;
40 public class CalciteServerCaller {
43  private final static Logger HEAVYDBLOGGER =
44  LoggerFactory.getLogger(CalciteServerCaller.class);
45  private CommandLine cmd = null;
46 
47  public static void main(String[] args) {
49  csc.doWork(args);
50  }
51 
52  private void doWork(String[] args) {
53  CalciteServerWrapper calciteServerWrapper = null;
54 
55  // create Options object
56  Options options = new Options();
57 
58  Option port =
59  Option.builder("p").hasArg().desc("port number").longOpt("port").build();
60 
61  Option ssl_trust_store = Option.builder("T")
62  .hasArg()
63  .desc("SSL_trust_store")
64  .longOpt("trust_store")
65  .build();
66 
67  Option ssl_trust_passwd = Option.builder("P")
68  .hasArg()
69  .desc("SSL_trust_password")
70  .longOpt("trust_store_pw")
71  .build();
72 
73  Option ssl_keystore =
74  Option.builder("Y").hasArg().desc("SSL keystore").longOpt("keystore").build();
75 
76  Option ssl_keystore_password = Option.builder("Z")
77  .hasArg()
78  .desc("SSL keystore password")
79  .longOpt("keystore_password")
80  .build();
81 
82  Option dbPort = Option.builder("m")
83  .hasArg()
84  .desc("HEAVY.AI port number")
85  .longOpt("db_port")
86  .build();
87 
88  Option data = Option.builder("d")
89  .hasArg()
90  .desc("data directory")
91  .required()
92  .longOpt("data")
93  .build();
94 
95  Option extensions = Option.builder("e")
96  .hasArg()
97  .desc("extension signatures directory")
98  .longOpt("extensions")
99  .build();
100 
101  Option udf_file = Option.builder("u")
102  .hasArg()
103  .desc("User Defined Functions file path")
104  .longOpt("udf")
105  .build();
106 
107  Option config_file = Option.builder("c")
108  .hasArg()
109  .desc("Configuration file")
110  .longOpt("config")
111  .build();
112  options.addOption(port);
113  options.addOption(data);
114  options.addOption(extensions);
115  options.addOption(dbPort);
116  options.addOption(ssl_trust_store);
117  options.addOption(ssl_trust_passwd);
118  options.addOption(ssl_keystore);
119  options.addOption(ssl_keystore_password);
120  options.addOption(udf_file);
121  options.addOption(config_file);
122 
123  CommandLineParser parser = new DefaultParser();
124 
125  try {
126  cmd = parser.parse(options, args);
127  } catch (ParseException ex) {
128  HEAVYDBLOGGER.error(ex.getLocalizedMessage());
129  help(options);
130  exit(0);
131  }
132 
133  int portNum = Integer.valueOf(cmd.getOptionValue("port", "6279"));
134  int dbPortNum = Integer.valueOf(cmd.getOptionValue("db_port", "6274"));
135  String dataDir = cmd.getOptionValue("data", "data");
136  String extensionsDir = cmd.getOptionValue("extensions", "build/QueryEngine");
137  String trust_store = cmd.getOptionValue("trust_store", "");
138  String trust_store_pw = cmd.getOptionValue("trust_store_pw", "");
139  String key_store = cmd.getOptionValue("keystore", "");
140  String key_store_pw = cmd.getOptionValue("keystore_password", "");
141  String udfName = cmd.getOptionValue("udf", "");
142  String configuration_file = cmd.getOptionValue("config", "");
143 
144  final Path extensionFunctionsAstFile =
145  Paths.get(extensionsDir, "ExtensionFunctions.ast");
146 
147  // Add logging to our log files directories
148  Properties p = new Properties();
149  try {
150  p.load(getClass().getResourceAsStream("/log4j2.properties"));
151  } catch (IOException ex) {
152  HEAVYDBLOGGER.error(
153  "Could not load log4j property file from resources " + ex.getMessage());
154  }
155  p.put("log.dir", dataDir); // overwrite "log.dir"
156 
157  try {
158  Properties properties = null;
159  if (!configuration_file.isEmpty()) {
160  properties = CalciteServerCaller.readPropertyFile(configuration_file);
161  }
162  if (trust_store == null || trust_store.isEmpty()) {
163  client_skT = SockTransportProperties.getUnencryptedClient();
164  // client_skT = new SockTransportProperties(load_trust_store);
165  } else {
166  if (properties != null && trust_store_pw.isEmpty()) {
167  // If a configuration file was supplied and trust password is empty read
168  // properties it to get the trust store password
169  trust_store_pw = properties.getProperty("ssl-trust-password");
170  if (trust_store_pw == null) {
171  HEAVYDBLOGGER.warn("Failed to load trust store password from config file ["
172  + configuration_file + "] for trust store [" + trust_store + "]");
173  }
174  }
175  try {
176  client_skT = SockTransportProperties.getEncryptedClientSpecifiedTrustStore(
177  trust_store, trust_store_pw);
178  } catch (Exception eX) {
179  String error =
180  "Loading encrypted client SockTransportProperties failed. Error - "
181  + eX.toString();
182  throw new RuntimeException(error);
183  }
184  }
185  if (key_store != null && !key_store.isEmpty()) {
186  // If suppled a key store load a server transport, otherwise server_skT can stay
187  // as null If a configuration file was supplied read and password is empty read
188  // properties for key store password
189  if (properties != null && key_store_pw.isEmpty()) {
190  key_store_pw = properties.getProperty("ssl-keystore-password");
191  if (key_store_pw == null) {
192  String err = "Failed to load key store password from config file ["
193  + configuration_file + "] for key store [" + key_store + "]";
194  throw new RuntimeException(err);
195  }
196  }
197  try {
198  server_skT =
199  SockTransportProperties.getEncryptedServer(key_store, key_store_pw);
200  } catch (Exception eX) {
201  String error =
202  "Loading encrypted Server SockTransportProperties failed. Error - "
203  + eX.toString();
204  throw eX;
205  }
206  } else {
207  server_skT = SockTransportProperties.getUnecryptedServer();
208  }
209  } catch (Exception ex) {
210  HEAVYDBLOGGER.error("Error opening SocketTransport. " + ex.getMessage());
211  exit(0);
212  }
213 
214  Path udfPath;
215 
216  try {
217  if (!udfName.isEmpty()) {
218  udfPath = Paths.get(udfName);
219  }
220  } catch (FileSystemNotFoundException ex1) {
221  HEAVYDBLOGGER.error("Could not load udf file " + ex1.getMessage());
222  }
223 
224  calciteServerWrapper = new CalciteServerWrapper(portNum,
225  dbPortNum,
226  dataDir,
227  extensionFunctionsAstFile.toString(),
228  client_skT,
229  server_skT,
230  udfName);
231 
232  while (true) {
233  try {
234  calciteServerWrapper.run();
235  if (calciteServerWrapper.shutdown()) {
236  break;
237  }
238  try {
239  // wait for 4 secs before retry
240  Thread.sleep(4000);
241  } catch (InterruptedException ex) {
242  // noop
243  }
244  } catch (Exception x) {
245  x.printStackTrace();
246  }
247  }
248  }
249 
250  private void help(Options options) {
251  // automatically generate the help statement
252  HelpFormatter formatter = new HelpFormatter();
253  formatter.printHelp("CalciteServerCaller", options);
254  }
255 
256  static private Properties readPropertyFile(String fileName) {
257  Properties properties = new Properties();
258  try {
259  BufferedReader bufferedReader = new BufferedReader(new FileReader(fileName));
260  StringBuffer sb = new StringBuffer();
261  for (String line = bufferedReader.readLine(); line != null;
262  line = bufferedReader.readLine()) {
263  if (line.toLowerCase().equals("[web]")) {
264  break;
265  }
266  line = line.replaceAll("[\",']", "");
267  sb.append(line + "\n");
268  }
269  properties.load(new StringReader(sb.toString()));
270  } catch (java.io.IOException iE) {
271  HEAVYDBLOGGER.warn("Could not load configuration file [" + fileName
272  + "] to get keystore/truststore password. Error - " + iE.toString());
273  }
274  return properties;
275  }
276 }
tuple line
Definition: parse_ast.py:10
static Properties readPropertyFile(String fileName)