init.cpp 7.6 KB
Newer Older
羽飞's avatar
羽飞 已提交
1
/* Copyright (c) 2021 OceanBase and/or its affiliates. All rights reserved.
羽飞's avatar
羽飞 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
miniob is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:
         http://license.coscl.org.cn/MulanPSL2
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details. */

//
// Created by Longda on 2021/5/3.
//

#include "init.h"

#include "ini_setting.h"
#include "common/conf/ini.h"
#include "common/lang/string.h"
#include "common/log/log.h"
#include "common/os/path.h"
#include "common/os/pidfile.h"
#include "common/os/process.h"
#include "common/os/signal.h"
#include "common/seda/init.h"
#include "common/seda/stage_factory.h"

#include "common/metrics/log_reporter.h"
#include "common/metrics/metrics_registry.h"
#include "session/session_stage.h"
#include "sql/executor/execute_stage.h"
#include "sql/optimizer/optimize_stage.h"
#include "sql/parser/parse_stage.h"
#include "sql/parser/resolve_stage.h"
#include "sql/plan_cache/plan_cache_stage.h"
#include "sql/query_cache/query_cache_stage.h"
#include "storage/default/default_storage_stage.h"
#include "storage/mem/mem_storage_stage.h"
羽飞's avatar
羽飞 已提交
39 40
#include "storage/default/disk_buffer_pool.h"
#include "storage/default/default_handler.h"
羽飞's avatar
羽飞 已提交
41
#include "storage/trx/trx.h"
羽飞's avatar
羽飞 已提交
42 43 44

using namespace common;

45 46
bool *&_get_init()
{
羽飞's avatar
羽飞 已提交
47 48 49 50 51
  static bool util_init = false;
  static bool *util_init_p = &util_init;
  return util_init_p;
}

52 53 54 55
bool get_init()
{
  return *_get_init();
}
羽飞's avatar
羽飞 已提交
56

57 58
void set_init(bool value)
{
羽飞's avatar
羽飞 已提交
59 60 61 62
  *_get_init() = value;
  return;
}

63 64
void sig_handler(int sig)
{
羽飞's avatar
羽飞 已提交
65 66 67 68 69 70 71 72
  // Signal handler will be add in the next step.
  //  Add action to shutdown

  LOG_INFO("Receive one signal of %d.", sig);

  return;
}

73 74
int init_log(ProcessParam *process_cfg, Ini &properties)
{
羽飞's avatar
羽飞 已提交
75 76 77 78 79 80 81 82
  const std::string &proc_name = process_cfg->get_process_name();
  try {
    // we had better alloc one lock to do so, but simplify the logic
    if (g_log) {
      return 0;
    }

    const std::string log_section_name = "LOG";
83
    std::map<std::string, std::string> log_section = properties.get(log_section_name);
羽飞's avatar
羽飞 已提交
84 85 86 87 88 89 90 91

    std::string log_file_name;

    // get log file name
    std::string key = "LOG_FILE_NAME";
    std::map<std::string, std::string>::iterator it = log_section.find(key);
    if (it == log_section.end()) {
      log_file_name = proc_name + ".log";
92
      std::cout << "Not set log file name, use default " << log_file_name << std::endl;
羽飞's avatar
羽飞 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    } else {
      log_file_name = it->second;
    }

    log_file_name = getAboslutPath(log_file_name.c_str());

    LOG_LEVEL log_level = LOG_LEVEL_INFO;
    key = ("LOG_FILE_LEVEL");
    it = log_section.find(key);
    if (it != log_section.end()) {
      int log = (int)log_level;
      str_to_val(it->second, log);
      log_level = (LOG_LEVEL)log;
    }

    LOG_LEVEL console_level = LOG_LEVEL_INFO;
    key = ("LOG_CONSOLE_LEVEL");
    it = log_section.find(key);
    if (it != log_section.end()) {
      int log = (int)console_level;
      str_to_val(it->second, log);
      console_level = (LOG_LEVEL)log;
    }

    LoggerFactory::init_default(log_file_name, log_level, console_level);

    key = ("DefaultLogModules");
    it = log_section.find(key);
    if (it != log_section.end()) {
      g_log->set_default_module(it->second);
    }

    if (process_cfg->is_demon()) {
      sys_log_redirect(log_file_name.c_str(), log_file_name.c_str());
    }

    return 0;
  } catch (std::exception &e) {
131
    std::cerr << "Failed to init log for " << proc_name << SYS_OUTPUT_FILE_POS << SYS_OUTPUT_ERROR << std::endl;
羽飞's avatar
羽飞 已提交
132 133 134 135 136 137
    return errno;
  }

  return 0;
}

138 139
void cleanup_log()
{
羽飞's avatar
羽飞 已提交
140 141 142 143 144 145 146 147

  if (g_log) {
    delete g_log;
    g_log = nullptr;
  }
  return;
}

148 149 150 151 152
int prepare_init_seda()
{
  static StageFactory session_stage_factory("SessionStage", &SessionStage::make_stage);
  static StageFactory resolve_stage_factory("ResolveStage", &ResolveStage::make_stage);
  static StageFactory query_cache_stage_factory("QueryCacheStage", &QueryCacheStage::make_stage);
羽飞's avatar
羽飞 已提交
153
  static StageFactory parse_stage_factory("ParseStage", &ParseStage::make_stage);
154 155
  static StageFactory plan_cache_factory("PlanCacheStage", &PlanCacheStage::make_stage);
  static StageFactory optimize_factory("OptimizeStage", &OptimizeStage::make_stage);
羽飞's avatar
羽飞 已提交
156
  static StageFactory execute_factory("ExecuteStage", &ExecuteStage::make_stage);
157 158
  static StageFactory default_storage_factory("DefaultStorageStage", &DefaultStorageStage::make_stage);
  static StageFactory mem_storage_factory("MemStorageStage", &MemStorageStage::make_stage);
羽飞's avatar
羽飞 已提交
159 160 161
  return 0;
}

羽飞's avatar
羽飞 已提交
162
int init_global_objects(ProcessParam *process_param)
羽飞's avatar
羽飞 已提交
163 164 165 166 167 168 169
{
  BufferPoolManager *bpm = new BufferPoolManager();
  BufferPoolManager::set_instance(bpm);

  DefaultHandler *handler = new DefaultHandler();
  DefaultHandler::set_default(handler);

羽飞's avatar
羽飞 已提交
170 171 172 173 174 175 176
  int ret = 0;
  RC rc = TrxKit::init_global(process_param->trx_kit_name().c_str());
  if (rc != RC::SUCCESS) {
    LOG_ERROR("failed to init trx kit. rc=%s", strrc(rc));
    ret = -1;
  }
  return ret;
羽飞's avatar
羽飞 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
}

int uninit_global_objects()
{
  DefaultHandler *default_handler = &DefaultHandler::get_default();
  if (default_handler != nullptr) {
    DefaultHandler::set_default(nullptr);
    delete default_handler;
  }

  BufferPoolManager *bpm = &BufferPoolManager::instance();
  if (bpm != nullptr) {
    BufferPoolManager::set_instance(nullptr);
    delete bpm;
  }
  return 0;
}

195 196
int init(ProcessParam *process_param)
{
羽飞's avatar
羽飞 已提交
197 198 199 200 201 202 203 204 205 206 207

  if (get_init()) {

    return 0;
  }

  set_init(true);

  // Run as daemon if daemonization requested
  int rc = STATUS_SUCCESS;
  if (process_param->is_demon()) {
208
    rc = daemonize_service(process_param->get_std_out().c_str(), process_param->get_std_err().c_str());
羽飞's avatar
羽飞 已提交
209
    if (rc != 0) {
210
      std::cerr << "Shutdown due to failed to daemon current process!" << std::endl;
羽飞's avatar
羽飞 已提交
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
      return rc;
    }
  }

  writePidFile(process_param->get_process_name().c_str());

  // Initialize global variables before enter multi-thread mode
  // to avoid race condition
  theSwVersion();

  // Read Configuration files
  rc = get_properties()->load(process_param->get_conf());
  if (rc) {
    std::cerr << "Failed to load configuration files" << std::endl;
    return rc;
  }

  // Init tracer
  rc = init_log(process_param, *get_properties());
  if (rc) {
    std::cerr << "Failed to init Log" << std::endl;
    return rc;
  }

  std::string conf_data;
  get_properties()->to_string(conf_data);
  LOG_INFO("Output configuration \n%s", conf_data.c_str());

羽飞's avatar
羽飞 已提交
239
  rc = init_global_objects(process_param);
羽飞's avatar
羽飞 已提交
240 241 242 243 244
  if (rc != 0) {
    LOG_ERROR("failed to init global objects");
    return rc;
  }

羽飞's avatar
羽飞 已提交
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
  // seda is used for backend async event handler
  // the latency of seda is slow, it isn't used for critical latency
  // environment.
  prepare_init_seda();
  rc = init_seda(process_param);
  if (rc) {
    LOG_ERROR("Failed to init seda configuration!");
    return rc;
  }

  LogReporter *log_reporter = get_log_reporter();
  MetricsRegistry &metrics_registry = get_metrics_registry();

  metrics_registry.add_reporter(log_reporter);

  // Block interrupt signals before creating child threads.
  // setSignalHandler(sig_handler);
  // sigset_t newSigset, oset;
  // blockDefaultSignals(&newSigset, &oset);
  //  wait interrupt signals
  // startWaitForSignals(&newSigset);

  LOG_INFO("Successfully init utility");

  return STATUS_SUCCESS;
}

272 273
void cleanup_util()
{
羽飞's avatar
羽飞 已提交
274 275
  uninit_global_objects();
  
羽飞's avatar
羽飞 已提交
276 277 278 279 280 281 282 283 284 285 286 287 288 289
  if (nullptr != get_properties()) {
    delete get_properties();
    get_properties() = nullptr;
  }

  LOG_INFO("Shutdown Cleanly!");

  // Finalize tracer
  cleanup_log();

  set_init(false);
  return;
}

290
void cleanup()
羽飞's avatar
羽飞 已提交
291 292 293
{
  cleanup_util();
}