init.cpp 7.9 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
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"
30
#include "session/session.h"
羽飞's avatar
羽飞 已提交
31 32 33 34 35 36 37 38 39
#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
羽飞 已提交
40 41
#include "storage/default/disk_buffer_pool.h"
#include "storage/default/default_handler.h"
羽飞's avatar
羽飞 已提交
42
#include "storage/trx/trx.h"
43
#include "global_context.h"
羽飞's avatar
羽飞 已提交
44 45 46

using namespace common;

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

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

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

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

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

  return;
}

75 76
int init_log(ProcessParam *process_cfg, Ini &properties)
{
羽飞's avatar
羽飞 已提交
77 78 79 80 81 82 83
  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;
    }

84 85
    auto log_context_getter = []() { return reinterpret_cast<intptr_t>(Session::current_session()); };

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

    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";
96
      std::cout << "Not set log file name, use default " << log_file_name << std::endl;
羽飞's avatar
羽飞 已提交
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
    } 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);
122
    g_log->set_context_getter(log_context_getter);
羽飞's avatar
羽飞 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135

    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) {
136
    std::cerr << "Failed to init log for " << proc_name << SYS_OUTPUT_FILE_POS << SYS_OUTPUT_ERROR << std::endl;
羽飞's avatar
羽飞 已提交
137 138 139 140 141 142
    return errno;
  }

  return 0;
}

143 144
void cleanup_log()
{
羽飞's avatar
羽飞 已提交
145 146 147 148 149 150 151 152

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

153 154 155 156 157
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
羽飞 已提交
158
  static StageFactory parse_stage_factory("ParseStage", &ParseStage::make_stage);
159 160
  static StageFactory plan_cache_factory("PlanCacheStage", &PlanCacheStage::make_stage);
  static StageFactory optimize_factory("OptimizeStage", &OptimizeStage::make_stage);
羽飞's avatar
羽飞 已提交
161
  static StageFactory execute_factory("ExecuteStage", &ExecuteStage::make_stage);
162 163
  static StageFactory default_storage_factory("DefaultStorageStage", &DefaultStorageStage::make_stage);
  static StageFactory mem_storage_factory("MemStorageStage", &MemStorageStage::make_stage);
羽飞's avatar
羽飞 已提交
164 165 166
  return 0;
}

167
int init_global_objects(ProcessParam *process_param, Ini &properties)
羽飞's avatar
羽飞 已提交
168
{
169 170
  GCTX.buffer_pool_manager_ = new BufferPoolManager();
  BufferPoolManager::set_instance(GCTX.buffer_pool_manager_);
羽飞's avatar
羽飞 已提交
171

172 173
  GCTX.handler_ = new DefaultHandler();
  DefaultHandler::set_default(GCTX.handler_);
羽飞's avatar
羽飞 已提交
174

羽飞's avatar
羽飞 已提交
175 176 177 178 179 180
  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;
  }
181
  GCTX.trx_kit_ = TrxKit::instance();
羽飞's avatar
羽飞 已提交
182
  return ret;
羽飞's avatar
羽飞 已提交
183 184 185 186
}

int uninit_global_objects()
{
187
  // TODO use global context
羽飞's avatar
羽飞 已提交
188 189 190 191 192 193 194 195 196 197 198 199 200 201
  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;
}

202 203
int init(ProcessParam *process_param)
{
羽飞's avatar
羽飞 已提交
204 205 206 207 208 209 210 211 212 213
  if (get_init()) {

    return 0;
  }

  set_init(true);

  // Run as daemon if daemonization requested
  int rc = STATUS_SUCCESS;
  if (process_param->is_demon()) {
214
    rc = daemonize_service(process_param->get_std_out().c_str(), process_param->get_std_err().c_str());
羽飞's avatar
羽飞 已提交
215
    if (rc != 0) {
216
      std::cerr << "Shutdown due to failed to daemon current process!" << std::endl;
羽飞's avatar
羽飞 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
      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());

245
  rc = init_global_objects(process_param, *get_properties());
羽飞's avatar
羽飞 已提交
246 247 248 249 250
  if (rc != 0) {
    LOG_ERROR("failed to init global objects");
    return rc;
  }

羽飞's avatar
羽飞 已提交
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
  // 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;
}

278 279
void cleanup_util()
{
羽飞's avatar
羽飞 已提交
280 281
  uninit_global_objects();
  
羽飞's avatar
羽飞 已提交
282 283 284 285 286 287 288 289 290 291 292 293 294 295
  if (nullptr != get_properties()) {
    delete get_properties();
    get_properties() = nullptr;
  }

  LOG_INFO("Shutdown Cleanly!");

  // Finalize tracer
  cleanup_log();

  set_init(false);
  return;
}

296
void cleanup()
羽飞's avatar
羽飞 已提交
297 298 299
{
  cleanup_util();
}