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