OmniSciDB  3a86f6ec37
/home/jenkins-slave/workspace/core-os-doxygen/MapDServer.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2018 OmniSci, 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 
19 
20 #ifdef HAVE_THRIFT_THREADFACTORY
21 #include <thrift/concurrency/ThreadFactory.h>
22 #else
23 #include <thrift/concurrency/PlatformThreadFactory.h>
24 #endif
25 
26 #include <thrift/concurrency/ThreadManager.h>
27 #include <thrift/protocol/TBinaryProtocol.h>
28 #include <thrift/protocol/TJSONProtocol.h>
29 #include <thrift/server/TThreadedServer.h>
30 #include <thrift/transport/TBufferTransports.h>
31 #include <thrift/transport/THttpServer.h>
32 #include <thrift/transport/TSSLServerSocket.h>
33 #include <thrift/transport/TSSLSocket.h>
34 #include <thrift/transport/TServerSocket.h>
35 
36 #include "Logger/Logger.h"
38 #include "Shared/file_delete.h"
40 #include "Shared/mapd_shared_ptr.h"
41 #include "Shared/scope.h"
42 
43 #include <boost/algorithm/string.hpp>
44 #include <boost/algorithm/string/trim.hpp>
45 #include <boost/filesystem.hpp>
46 #include <boost/locale/generator.hpp>
47 #include <boost/make_shared.hpp>
48 #include <boost/program_options.hpp>
49 
50 #include <csignal>
51 #include <cstdlib>
52 #include <sstream>
53 #include <thread>
54 #include <vector>
55 
56 #ifdef HAVE_AWS_S3
57 #include "DataMgr/OmniSciAwsSdk.h"
58 #endif
60 #include "MapDRelease.h"
61 #include "Shared/Compressor.h"
63 #include "Shared/file_delete.h"
64 #include "Shared/mapd_shared_ptr.h"
65 #include "Shared/scope.h"
66 
67 using namespace ::apache::thrift;
68 using namespace ::apache::thrift::concurrency;
69 using namespace ::apache::thrift::protocol;
70 using namespace ::apache::thrift::server;
71 using namespace ::apache::thrift::transport;
72 
73 extern bool g_enable_thrift_logs;
74 
75 std::atomic<bool> g_running{true};
76 std::atomic<int> g_saw_signal{-1};
77 
79 TThreadedServer* g_thrift_http_server{nullptr};
80 TThreadedServer* g_thrift_buf_server{nullptr};
81 
82 mapd::shared_ptr<DBHandler> g_warmup_handler =
83  0; // global "g_warmup_handler" needed to avoid circular dependency
84 // between "DBHandler" & function "run_warmup_queries"
85 mapd::shared_ptr<DBHandler> g_mapd_handler = 0;
86 std::once_flag g_shutdown_once_flag;
87 
89  if (g_mapd_handler) {
90  std::call_once(g_shutdown_once_flag, []() { g_mapd_handler->shutdown(); });
91  }
92 }
93 
94 void register_signal_handler(int signum, void (*handler)(int)) {
95  struct sigaction act;
96  memset(&act, 0, sizeof(act));
97  if (handler != SIG_DFL && handler != SIG_IGN) {
98  // block all signal deliveries while inside the signal handler
99  sigfillset(&act.sa_mask);
100  }
101  act.sa_handler = handler;
102  sigaction(signum, &act, NULL);
103 }
104 
105 // Signal handler to set a global flag telling the server to exit.
106 // Do not call other functions inside this (or any) signal handler
107 // unless you really know what you are doing. See also:
108 // man 7 signal-safety
109 // man 7 signal
110 // https://en.wikipedia.org/wiki/Reentrancy_(computing)
111 void omnisci_signal_handler(int signum) {
112  // Record the signal number for logging during shutdown.
113  // Only records the first signal if called more than once.
114  int expected_signal{-1};
115  if (!g_saw_signal.compare_exchange_strong(expected_signal, signum)) {
116  return; // this wasn't the first signal
117  }
118 
119  // This point should never be reached more than once.
120 
121  // Tell heartbeat() to shutdown by unsetting the 'g_running' flag.
122  // If 'g_running' is already false, this has no effect and the
123  // shutdown is already in progress.
124  g_running = false;
125 
126  // Handle core dumps specially by pausing inside this signal handler
127  // because on some systems, some signals will execute their default
128  // action immediately when and if the signal handler returns.
129  // We would like to do some emergency cleanup before core dump.
130  if (signum == SIGQUIT || signum == SIGABRT || signum == SIGSEGV || signum == SIGFPE) {
131  // Wait briefly to give heartbeat() a chance to flush the logs and
132  // do any other emergency shutdown tasks.
133  sleep(2);
134 
135  // Explicitly trigger whatever default action this signal would
136  // have done, such as terminate the process or dump core.
137  // Signals are currently blocked so this new signal will be queued
138  // until this signal handler returns.
139  register_signal_handler(signum, SIG_DFL);
140  kill(getpid(), signum);
141  sleep(5);
142 
143 #ifndef __APPLE__
144  // as a last resort, abort
145  // primary used in Docker environments, where we can end up with PID 1 and fail to
146  // catch unix signals
147  quick_exit(signum);
148 #endif
149  }
150 }
151 
159  // Thrift secure socket can cause problems with SIGPIPE
160  register_signal_handler(SIGPIPE, SIG_IGN);
161 }
162 
163 void start_server(TThreadedServer& server, const int port) {
164  try {
165  server.serve();
166  if (errno != 0) {
167  throw std::runtime_error(std::string("Thrift server exited: ") +
168  std::strerror(errno));
169  }
170  } catch (std::exception& e) {
171  LOG(ERROR) << "Exception: " << e.what() << ": port " << port << std::endl;
172  }
173 }
174 
175 void releaseWarmupSession(TSessionId& sessionId, std::ifstream& query_file) {
176  query_file.close();
177  if (sessionId != g_warmup_handler->getInvalidSessionId()) {
178  g_warmup_handler->disconnect(sessionId);
179  }
180 }
181 
182 void run_warmup_queries(mapd::shared_ptr<DBHandler> handler,
183  std::string base_path,
184  std::string query_file_path) {
185  // run warmup queries to load cache if requested
186  if (query_file_path.empty()) {
187  return;
188  }
189  LOG(INFO) << "Running DB warmup with queries from " << query_file_path;
190  try {
191  g_warmup_handler = handler;
192  std::string db_info;
193  std::string user_keyword, user_name, db_name;
194  std::ifstream query_file;
197  TSessionId sessionId = g_warmup_handler->getInvalidSessionId();
198 
199  ScopeGuard session_guard = [&] { releaseWarmupSession(sessionId, query_file); };
200  query_file.open(query_file_path);
201  while (std::getline(query_file, db_info)) {
202  if (db_info.length() == 0) {
203  continue;
204  }
205  std::istringstream iss(db_info);
206  iss >> user_keyword >> user_name >> db_name;
207  if (user_keyword.compare(0, 4, "USER") == 0) {
208  // connect to DB for given user_name/db_name with super_user_rights (without
209  // password), & start session
210  g_warmup_handler->super_user_rights_ = true;
211  g_warmup_handler->connect(sessionId, user_name, "", db_name);
212  g_warmup_handler->super_user_rights_ = false;
213 
214  // read and run one query at a time for the DB with the setup connection
215  TQueryResult ret;
216  std::string single_query;
217  while (std::getline(query_file, single_query)) {
218  boost::algorithm::trim(single_query);
219  if (single_query.length() == 0 || single_query[0] == '-') {
220  continue;
221  }
222  if (single_query[0] == '}') {
223  single_query.clear();
224  break;
225  }
226  if (single_query.find(';') == single_query.npos) {
227  std::string multiline_query;
228  std::getline(query_file, multiline_query, ';');
229  single_query += multiline_query;
230  }
231 
232  try {
233  g_warmup_handler->sql_execute(ret, sessionId, single_query, true, "", -1, -1);
234  } catch (...) {
235  LOG(WARNING) << "Exception while executing '" << single_query
236  << "', ignoring";
237  }
238  single_query.clear();
239  }
240 
241  // stop session and disconnect from the DB
242  g_warmup_handler->disconnect(sessionId);
243  sessionId = g_warmup_handler->getInvalidSessionId();
244  } else {
245  LOG(WARNING) << "\nSyntax error in the file: " << query_file_path.c_str()
246  << " Missing expected keyword USER. Following line will be ignored: "
247  << db_info.c_str() << std::endl;
248  }
249  db_info.clear();
250  }
251  } catch (...) {
252  LOG(WARNING) << "Exception while executing warmup queries. "
253  << "Warmup may not be fully completed. Will proceed nevertheless."
254  << std::endl;
255  }
256 }
257 
258 void heartbeat() {
259  // Block all signals for this heartbeat thread, only.
260  sigset_t set;
261  sigfillset(&set);
262  int result = pthread_sigmask(SIG_BLOCK, &set, NULL);
263  if (result != 0) {
264  throw std::runtime_error("heartbeat() thread startup failed");
265  }
266 
267  // Sleep until omnisci_signal_handler or anything clears the g_running flag.
268  VLOG(1) << "heartbeat thread starting";
269  while (::g_running) {
270  using namespace std::chrono;
271  std::this_thread::sleep_for(1s);
272  }
273  VLOG(1) << "heartbeat thread exiting";
274 
275  // Get the signal number if there was a signal.
276  int signum = g_saw_signal;
277  if (signum >= 1 && signum != SIGTERM) {
278  LOG(INFO) << "Interrupt signal (" << signum << ") received.";
279  }
280 
281  // if dumping core, try to do some quick stuff
282  if (signum == SIGQUIT || signum == SIGABRT || signum == SIGSEGV || signum == SIGFPE) {
283  if (g_mapd_handler) {
284  std::call_once(g_shutdown_once_flag,
285  []() { g_mapd_handler->emergency_shutdown(); });
286  }
288  return;
289  // core dump should begin soon after this, see omnisci_signal_handler()
290  }
291 
292  // trigger an orderly shutdown by telling Thrift to stop serving
293  {
294  mapd_shared_lock<mapd_shared_mutex> read_lock(g_thrift_mutex);
295  auto httpserv = g_thrift_http_server;
296  if (httpserv) {
297  httpserv->stop();
298  }
299  auto bufserv = g_thrift_buf_server;
300  if (bufserv) {
301  bufserv->stop();
302  }
303  // main() should return soon after this
304  }
305 }
306 
307 int startMapdServer(CommandLineOptions& prog_config_opts, bool start_http_server = true) {
308  // try to enforce an orderly shutdown even after a signal
310 
311  // register shutdown procedures for when a normal shutdown happens
312  // be aware that atexit() functions run in reverse order
313  atexit(&logger::shutdown);
314  atexit(&shutdown_handler);
315 
316 #ifdef HAVE_AWS_S3
318  ScopeGuard aws_sdk_guard = [] { omnisci_aws_sdk::shutdown_sdk(); };
319 #endif
320 
321  // start background thread to clean up _DELETE_ME files
322  const unsigned int wait_interval =
323  3; // wait time in secs after looking for deleted file before looking again
324  std::thread file_delete_thread(file_delete,
325  std::ref(g_running),
326  wait_interval,
327  prog_config_opts.base_path + "/mapd_data");
328  std::thread heartbeat_thread(heartbeat);
329 
330  if (!g_enable_thrift_logs) {
331  apache::thrift::GlobalOutput.setOutputFunction([](const char* msg) {});
332  }
333 
335  // Use the locale setting of the server by default. The generate parameter can be
336  // updated appropriately if a locale override option is ever supported.
337  boost::locale::generator generator;
338  std::locale::global(generator.generate(""));
339  }
340 
341  try {
343  mapd::make_shared<DBHandler>(prog_config_opts.db_leaves,
344  prog_config_opts.string_leaves,
345  prog_config_opts.base_path,
346  prog_config_opts.cpu_only,
347  prog_config_opts.allow_multifrag,
348  prog_config_opts.jit_debug,
349  prog_config_opts.intel_jit_profile,
350  prog_config_opts.read_only,
351  prog_config_opts.allow_loop_joins,
352  prog_config_opts.enable_rendering,
353  prog_config_opts.renderer_use_vulkan_driver,
354  prog_config_opts.enable_auto_clear_render_mem,
355  prog_config_opts.render_oom_retry_threshold,
356  prog_config_opts.render_mem_bytes,
357  prog_config_opts.max_concurrent_render_sessions,
358  prog_config_opts.num_gpus,
359  prog_config_opts.start_gpu,
360  prog_config_opts.reserved_gpu_mem,
361  prog_config_opts.render_compositor_use_last_gpu,
362  prog_config_opts.num_reader_threads,
363  prog_config_opts.authMetadata,
364  prog_config_opts.system_parameters,
365  prog_config_opts.enable_legacy_syntax,
366  prog_config_opts.idle_session_duration,
367  prog_config_opts.max_session_duration,
368  prog_config_opts.enable_runtime_udf,
369  prog_config_opts.udf_file_name,
370  prog_config_opts.udf_compiler_path,
371  prog_config_opts.udf_compiler_options,
372 #ifdef ENABLE_GEOS
373  prog_config_opts.libgeos_so_filename,
374 #endif
375  prog_config_opts.disk_cache_config);
376  } catch (const std::exception& e) {
377  LOG(FATAL) << "Failed to initialize service handler: " << e.what();
378  }
379 
380  if (g_enable_fsi) {
382  }
383 
384  mapd::shared_ptr<TServerSocket> serverSocket;
385  mapd::shared_ptr<TServerSocket> httpServerSocket;
386  if (!prog_config_opts.system_parameters.ssl_cert_file.empty() &&
387  !prog_config_opts.system_parameters.ssl_key_file.empty()) {
388  mapd::shared_ptr<TSSLSocketFactory> sslSocketFactory;
389  sslSocketFactory =
390  mapd::shared_ptr<TSSLSocketFactory>(new TSSLSocketFactory(SSLProtocol::SSLTLS));
391  sslSocketFactory->loadCertificate(
392  prog_config_opts.system_parameters.ssl_cert_file.c_str());
393  sslSocketFactory->loadPrivateKey(
394  prog_config_opts.system_parameters.ssl_key_file.c_str());
395  if (prog_config_opts.system_parameters.ssl_transport_client_auth) {
396  sslSocketFactory->authenticate(true);
397  } else {
398  sslSocketFactory->authenticate(false);
399  }
400  sslSocketFactory->ciphers("ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
401  serverSocket = mapd::shared_ptr<TServerSocket>(new TSSLServerSocket(
402  prog_config_opts.system_parameters.omnisci_server_port, sslSocketFactory));
403  httpServerSocket = mapd::shared_ptr<TServerSocket>(
404  new TSSLServerSocket(prog_config_opts.http_port, sslSocketFactory));
405  LOG(INFO) << " OmniSci server using encrypted connection. Cert file ["
406  << prog_config_opts.system_parameters.ssl_cert_file << "], key file ["
407  << prog_config_opts.system_parameters.ssl_key_file << "]";
408  } else {
409  LOG(INFO) << " OmniSci server using unencrypted connection";
410  serverSocket = mapd::shared_ptr<TServerSocket>(
411  new TServerSocket(prog_config_opts.system_parameters.omnisci_server_port));
412  httpServerSocket =
413  mapd::shared_ptr<TServerSocket>(new TServerSocket(prog_config_opts.http_port));
414  }
415 
416  ScopeGuard pointer_to_thrift_guard = [] {
417  mapd_lock_guard<mapd_shared_mutex> write_lock(g_thrift_mutex);
419  };
420 
421  if (prog_config_opts.system_parameters.ha_group_id.empty()) {
422  mapd::shared_ptr<TProcessor> processor(
423  new TrackingProcessor(g_mapd_handler, prog_config_opts.log_user_origin));
424  mapd::shared_ptr<TTransportFactory> bufTransportFactory(
425  new TBufferedTransportFactory());
426  mapd::shared_ptr<TProtocolFactory> bufProtocolFactory(new TBinaryProtocolFactory());
427 
428  mapd::shared_ptr<TServerTransport> bufServerTransport(serverSocket);
429  TThreadedServer bufServer(
430  processor, bufServerTransport, bufTransportFactory, bufProtocolFactory);
431  {
432  mapd_lock_guard<mapd_shared_mutex> write_lock(g_thrift_mutex);
433  g_thrift_buf_server = &bufServer;
434  }
435 
436  std::thread bufThread(start_server,
437  std::ref(bufServer),
438  prog_config_opts.system_parameters.omnisci_server_port);
439 
440  // TEMPORARY
441  auto warmup_queries = [&prog_config_opts]() {
442  // run warm up queries if any exists
444  g_mapd_handler, prog_config_opts.base_path, prog_config_opts.db_query_file);
445  if (prog_config_opts.exit_after_warmup) {
446  g_running = false;
447  }
448  };
449 
450  mapd::shared_ptr<TServerTransport> httpServerTransport(httpServerSocket);
451  mapd::shared_ptr<TTransportFactory> httpTransportFactory(
452  new THttpServerTransportFactory());
453  mapd::shared_ptr<TProtocolFactory> httpProtocolFactory(new TJSONProtocolFactory());
454  TThreadedServer httpServer(
455  processor, httpServerTransport, httpTransportFactory, httpProtocolFactory);
456  if (start_http_server) {
457  {
458  mapd_lock_guard<mapd_shared_mutex> write_lock(g_thrift_mutex);
459  g_thrift_http_server = &httpServer;
460  }
461  std::thread httpThread(
462  start_server, std::ref(httpServer), prog_config_opts.http_port);
463 
464  warmup_queries();
465 
466  bufThread.join();
467  httpThread.join();
468  } else {
469  warmup_queries();
470  bufThread.join();
471  }
472  } else { // running ha server
473  LOG(FATAL) << "No High Availability module available, please contact OmniSci support";
474  }
475 
476  g_running = false;
477  file_delete_thread.join();
478  heartbeat_thread.join();
480 
481  if (g_enable_fsi) {
483  }
484 
485  int signum = g_saw_signal;
486  if (signum <= 0 || signum == SIGTERM) {
487  return 0;
488  } else {
489  return signum;
490  }
491 }
492 
493 int main(int argc, char** argv) {
494  bool has_clust_topo = false;
495 
496  CommandLineOptions prog_config_opts(argv[0], has_clust_topo);
497 
498  try {
499  if (auto return_code =
500  prog_config_opts.parse_command_line(argc, argv, !has_clust_topo)) {
501  return *return_code;
502  }
503 
504  if (!has_clust_topo) {
505  prog_config_opts.validate_base_path();
506  prog_config_opts.validate();
507  return (startMapdServer(prog_config_opts));
508  }
509  } catch (std::runtime_error& e) {
510  std::cerr << "Can't start: " << e.what() << std::endl;
511  return 1;
512  } catch (boost::program_options::error& e) {
513  std::cerr << "Usage Error: " << e.what() << std::endl;
514  return 1;
515  }
516 }
mapd_shared_mutex g_thrift_mutex
Definition: MapDServer.cpp:78
TThreadedServer * g_thrift_buf_server
Definition: MapDServer.cpp:80
mapd::shared_ptr< DBHandler > g_warmup_handler
Definition: MapDServer.cpp:82
std::vector< LeafHostInfo > string_leaves
std::string udf_compiler_path
std::once_flag g_shutdown_once_flag
Definition: MapDServer.cpp:86
#define LOG(tag)
Definition: Logger.h:188
DiskCacheConfig disk_cache_config
shared utility for the db server and string dictionary server to remove old files ...
void heartbeat()
Definition: MapDServer.cpp:258
boost::optional< int > parse_command_line(int argc, char const *const *argv, const bool should_init_logging=false)
void run_warmup_queries(mapd::shared_ptr< DBHandler > handler, std::string base_path, std::string query_file_path)
Definition: MapDServer.cpp:182
TThreadedServer * g_thrift_http_server
Definition: MapDServer.cpp:79
int main(int argc, char **argv)
Definition: MapDServer.cpp:493
void releaseWarmupSession(TSessionId &sessionId, std::ifstream &query_file)
Definition: MapDServer.cpp:175
size_t max_concurrent_render_sessions
singleton class to handle concurrancy and state for blosc library. A C++ wrapper over a pure C librar...
std::vector< LeafHostInfo > db_leaves
std::shared_timed_mutex mapd_shared_mutex
mapd::shared_ptr< DBHandler > g_mapd_handler
Definition: MapDServer.cpp:85
void start_server(TThreadedServer &server, const int port)
Definition: MapDServer.cpp:163
std::string ssl_key_file
AuthMetadata authMetadata
void shutdown_handler()
Definition: MapDServer.cpp:88
std::atomic< int > g_saw_signal
Definition: MapDServer.cpp:76
static void start(std::atomic< bool > &is_program_running)
void register_signal_handlers()
Definition: MapDServer.cpp:152
void shutdown()
Definition: Logger.cpp:314
bool g_enable_experimental_string_functions
std::atomic< bool > g_running
Definition: MapDServer.cpp:75
std::vector< std::string > udf_compiler_options
void register_signal_handler(int signum, void(*handler)(int))
Definition: MapDServer.cpp:94
mapd_shared_lock< mapd_shared_mutex > read_lock
mapd_unique_lock< mapd_shared_mutex > write_lock
bool g_enable_thrift_logs
Definition: initdb.cpp:42
void file_delete(std::atomic< bool > &program_is_running, const unsigned int wait_interval_seconds, const std::string base_path)
Definition: File.cpp:233
bool g_enable_fsi
Definition: Catalog.cpp:91
void omnisci_signal_handler(int signum)
Definition: MapDServer.cpp:111
int startMapdServer(CommandLineOptions &prog_config_opts, bool start_http_server=true)
Definition: MapDServer.cpp:307
std::string ha_group_id
#define VLOG(n)
Definition: Logger.h:291
SystemParameters system_parameters
std::string ssl_cert_file