ob_config_manager.cpp 15.6 KB
Newer Older
O
oceanbase-admin 已提交
1 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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 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 131 132 133 134 135 136 137 138 139 140 141 142 143
/**
 * Copyright (c) 2021 OceanBase
 * OceanBase CE is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * 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 PubL v2 for more details.
 */

#define USING_LOG_PREFIX SHARE

#include "share/config/ob_config_manager.h"

#include "lib/file/file_directory_utils.h"
#include "lib/profile/ob_trace_id.h"
#include "lib/thread/thread_mgr.h"
#include "share/ob_cluster_version.h"
#include "share/ob_worker.h"
#include "observer/ob_sql_client_decorator.h"
#include "observer/ob_server_struct.h"
#include "observer/omt/ob_tenant_config_mgr.h"
#include "lib/utility/ob_tracepoint.h"
#include "observer/ob_server.h"

namespace oceanbase {
namespace common {
ObConfigManager::~ObConfigManager()
{
  TG_STOP(lib::TGDefIDs::CONFIG_MGR);
  TG_WAIT(lib::TGDefIDs::CONFIG_MGR);
  TG_DESTROY(lib::TGDefIDs::CONFIG_MGR);
}

int ObConfigManager::base_init()
{
  int ret = OB_SUCCESS;
  if (OB_FAIL(system_config_.init())) {
    LOG_ERROR("init system config failed", K(ret));
  } else if (OB_FAIL(server_config_.init(system_config_))) {
    LOG_ERROR("init server config failed", K(ret));
  }
  update_task_.config_mgr_ = this;
  return ret;
}

int ObConfigManager::init(const ObAddr& server)
{
  int ret = OB_SUCCESS;
  self_ = server;
  if (OB_FAIL(TG_START(lib::TGDefIDs::CONFIG_MGR))) {
    LOG_WARN("init timer failed", K(ret));
  } else {
    inited_ = true;
  }
  return ret;
}

int ObConfigManager::init(ObMySQLProxy& sql_proxy, const ObAddr& server)
{
  int ret = OB_SUCCESS;
  sql_proxy_ = &sql_proxy;
  self_ = server;
  if (OB_FAIL(TG_START(lib::TGDefIDs::CONFIG_MGR))) {
    LOG_WARN("init timer failed", K(ret));
  } else {
    inited_ = true;
  }
  return ret;
}

int ObConfigManager::reload_config()
{
  int ret = OB_SUCCESS;
  if (OB_FAIL(server_config_.check_all())) {
    LOG_WARN("Check configuration failed, can't reload", K(ret));
  } else if (OB_FAIL(reload_config_func_())) {
    LOG_WARN("Reload configuration failed.", K(ret));
  } else if (OB_FAIL(OBSERVER.get_net_frame().reload_ssl_config())) {
    LOG_WARN("reload ssl config for net frame fail", K(ret));
  }
  return ret;
}

int ObConfigManager::load_config(const char* path)
{
  int ret = OB_SUCCESS;
  FILE* fp = NULL;
  bool compat_mode = false;
  if (OB_ISNULL(path) || OB_UNLIKELY(STRLEN(path) <= 0)) {
    path = dump_path_;
  }

  char* buf = NULL;
  if (OB_FAIL(ret)) {
  } else if (OB_ISNULL(buf = static_cast<char*>(ob_malloc(OB_MAX_PACKET_LENGTH, ObModIds::OB_BUFFER)))) {
    ret = OB_ALLOCATE_MEMORY_FAILED;
    LOG_ERROR("alloc buffer failed", LITERAL_K(OB_MAX_PACKET_LENGTH), K(ret));
  } else if (OB_ISNULL(fp = fopen(path, "rb"))) {
    if (ENOENT == errno) {
      ret = OB_FILE_NOT_EXIST;
      LOG_INFO("Config file doesn't exist, read from command line", K(path), K(ret));
    } else {
      ret = OB_IO_ERROR;
      LOG_ERROR("Can't open file", K(path), K(errno), K(ret));
    }
  } else {
    LOG_INFO("Using config file", K(path));
    MEMSET(buf, 0, OB_MAX_PACKET_LENGTH);
    int64_t len = fread(buf, 1, OB_MAX_PACKET_LENGTH, fp);
    int64_t pos = 0;

    if (OB_UNLIKELY(0 != ferror(fp))) {  // read with error
      ret = OB_IO_ERROR;
      LOG_ERROR("Read config file error", K(path), K(ret));
    } else if (OB_UNLIKELY(0 == feof(fp))) {  // not end of file
      ret = OB_BUF_NOT_ENOUGH;
      LOG_ERROR("Config file is too long", K(path), K(ret));
    } else {
      ret = server_config_.deserialize_with_compat(buf, len, pos);
    }
    if (OB_FAIL(ret)) {
      LOG_ERROR("Deserialize server config failed", K(path), K(ret));
    } else if (OB_UNLIKELY(pos != len)) {
      ret = OB_DESERIALIZE_ERROR;
      LOG_ERROR("Deserialize server config failed", K(path), K(ret));
    }
    if (OB_UNLIKELY(0 != fclose(fp))) {
      ret = OB_IO_ERROR;
      LOG_ERROR("Close config file failed", K(ret));
    }
  }
  if (OB_LIKELY(NULL != buf)) {
    ob_free(buf);
    buf = NULL;
  }

  init_config_load_ = true;
  return ret;
}

144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
int ObConfigManager::check_header_change(const char* path, const char* buf) const
{
  int ret = OB_SUCCESS;
  if (OB_ISNULL(path) || OB_ISNULL(buf)) {
    ret = OB_INVALID_ARGUMENT;
  } else {
    PageArena<> pa;
    char *cmp_buf = nullptr;
    FILE* fp = NULL;
    ObRecordHeader header;
    int64_t header_len = header.get_serialize_size();
    if (OB_ISNULL(cmp_buf = pa.alloc(header_len))) {
      ret = OB_ALLOCATE_MEMORY_FAILED;
      LOG_ERROR("alloc buffer failed", K(header_len), K(ret));
    } else if (OB_ISNULL(fp = fopen(path, "rb"))) {
      if (ENOENT != errno) {
        ret = OB_IO_ERROR;
        LOG_ERROR("Can't open file", K(path), K(errno), K(ret));
      }
    } else {
      MEMSET(cmp_buf, 0, header_len);
      int64_t len = fread(cmp_buf, 1, header_len, fp);
      int64_t pos = 0;
      if (OB_UNLIKELY(0 != ferror(fp))) {  // read with error
        ret = OB_IO_ERROR;
        LOG_ERROR("Read config file error", K(path), K(ret));
      } else if (MEMCMP(buf, cmp_buf, header_len) == 0) {
        ret = OB_EAGAIN;
      }
      if (OB_UNLIKELY(0 != fclose(fp))) {
        ret = OB_IO_ERROR;
        LOG_ERROR("Close config file failed", K(errno));
      }
    }
  }
  return ret;
}

O
oceanbase-admin 已提交
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
int ObConfigManager::dump2file(const char* path) const
{
  int ret = OB_SUCCESS;
  int fd = 0;
  ssize_t size = 0;
  if (OB_ISNULL(path)) {
    path = dump_path_;
  }

  PageArena<> pa;

  if (OB_ISNULL(path) || STRLEN(path) <= 0) {
    ret = OB_INVALID_ERROR;
    LOG_WARN("NO dump path specified!", K(ret));
  } else {
    // write server config
    char* buf = nullptr;
    char* tmp_path = nullptr;
    char* hist_path = nullptr;
    int64_t pos = 0;
    if (OB_ISNULL(buf = pa.alloc(OB_MAX_PACKET_LENGTH))) {
      ret = OB_ALLOCATE_MEMORY_FAILED;
      LOG_ERROR("ob tc malloc memory for buf failed", K(ret));
    }
    if (OB_ISNULL(tmp_path = pa.alloc(MAX_PATH_SIZE))) {
      ret = OB_ALLOCATE_MEMORY_FAILED;
      LOG_ERROR("ob tc malloc memory for tmp configure path failed", K(ret));
    } else {
      snprintf(tmp_path, MAX_PATH_SIZE, "%s.tmp", path);
    }
    if (OB_ISNULL(hist_path = pa.alloc(MAX_PATH_SIZE))) {
      ret = OB_ALLOCATE_MEMORY_FAILED;
      LOG_ERROR("ob tc malloc memory for history configure path fail", K(ret));
    } else {
      snprintf(hist_path, MAX_PATH_SIZE, "%s.history", path);
    }

#ifdef ERRSIM
    ret = E(EventTable::EN_WRITE_CONFIG_FILE_FAILED) OB_SUCCESS;
    ;
    if (OB_FAIL(ret)) {
      ret = OB_IO_ERROR;
      LOG_WARN("ERRSIM, write config file failed", K(ret));
    }
#endif
    if (OB_SUCC(ret)) {
      if (OB_FAIL(server_config_.serialize(buf, OB_MAX_PACKET_LENGTH, pos))) {
        LOG_WARN("Serialize server config fail!", K(ret));
230 231
      } else if (OB_FAIL(check_header_change(path, buf)) && OB_EAGAIN == ret) {
        LOG_INFO("Header not change, no need to write server config!");
O
oceanbase-admin 已提交
232 233 234
      } else if ((fd = ::open(tmp_path, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP)) < 0) {
        ret = OB_IO_ERROR;
        LOG_WARN("fail to create config file", K(tmp_path), KERRMSG, K(ret));
N
nroskill 已提交
235
      } else if (pos != (size = unintr_write(fd, buf, pos))) {
O
oceanbase-admin 已提交
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
        ret = OB_IO_ERROR;
        LOG_WARN("Write server config fail!", K(errno), KERRMSG, K(pos), K(size), K(ret));
        if (0 != close(fd)) {
          LOG_WARN("fail to close file fd", K(fd), K(errno), KERRMSG, K(ret));
        }
      } else if (::fsync(fd) != 0) {
        ret = OB_IO_ERROR;
        LOG_WARN("Sync server config fail!", K(errno), KERRMSG, K(pos), K(size), K(ret));
        if (0 != close(fd)) {
          LOG_WARN("fail to close file fd", K(fd), K(errno), KERRMSG, K(ret));
        }
      } else if (0 != close(fd)) {
        ret = OB_IO_ERROR;
        LOG_WARN("fail to close file fd", K(fd), KERRMSG, K(ret));
      } else {
        LOG_INFO("Write server config successfully!");
      }
    }
    if (OB_SUCC(ret)) {
      if (0 != ::rename(path, hist_path) && errno != ENOENT) {
        ret = OB_ERR_SYS;
        LOG_WARN("fail to backup history config file", KERRMSG, K(ret));
      }
      if (0 != ::rename(tmp_path, path)) {
        ret = OB_ERR_SYS;
        LOG_WARN("fail to move tmp config file", KERRMSG, K(ret));
      }
263 264
    } else if (OB_EAGAIN == ret) {
      ret = OB_SUCCESS;
O
oceanbase-admin 已提交
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
    }
  }
  return ret;
}

int ObConfigManager::config_backup()
{
  int ret = OB_SUCCESS;
  char path[MAX_PATH_SIZE] = {};
  static const char* CONF_COPY_NAME = "/observer.conf.bin";

  for (int64_t idx = 0; idx < server_config_.config_additional_dir.size(); ++idx) {
    if (OB_SUCC(server_config_.config_additional_dir.get(idx, path, MAX_PATH_SIZE))) {
      if (STRLEN(path) > 0) {
        if (OB_FAIL(common::FileDirectoryUtils::create_full_path(path))) {
          LOG_ERROR("create additional configure directory fail", K(path), K(ret));
        } else if (STRLEN(path) + STRLEN(CONF_COPY_NAME) < static_cast<uint64_t>(MAX_PATH_SIZE)) {
          strcat(path, CONF_COPY_NAME);
          if (OB_FAIL(dump2file(path))) {
            LOG_WARN("make additional configure file copy fail", K(path), K(ret));
            ret = OB_SUCCESS;  // ignore ret code.
          }
        } else {
          LOG_ERROR("additional configure directory path is too long", K(path), "len", STRLEN(path));
        }
      }
    }
  }  // for
  return ret;
}

int ObConfigManager::update_local(int64_t expected_version)
{
  int ret = OB_SUCCESS;
  if (OB_ISNULL(sql_proxy_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("sql proxy is null", K(ret));
  } else {
    bool use_weak_read = false;
    ObSQLClientRetryWeak sql_client_retry_weak(sql_proxy_, use_weak_read);
    SMART_VAR(ObMySQLProxy::MySQLResult, result)
    {
      int64_t start = ObTimeUtility::current_time();
      const char* sqlstr = "select config_version, zone, svr_type, svr_ip, svr_port, name, "
                           "data_type, value, info, section, scope, source, edit_level "
                           "from __all_sys_parameter";
      if (OB_FAIL(sql_client_retry_weak.read(result, sqlstr))) {
        LOG_WARN("read config from __all_sys_parameter failed", K(sqlstr), K(ret));
      } else if (OB_FAIL(system_config_.update(result))) {
        LOG_WARN("failed to load system config", K(ret));
      } else if (expected_version != ObSystemConfig::INIT_VERSION &&
                 (system_config_.get_version() < current_version_ || system_config_.get_version() < expected_version)) {
        ret = OB_EAGAIN;
        LOG_WARN("__all_sys_parameter is older than the expected version",
            K(ret),
            "read_version",
            system_config_.get_version(),
            "current_version",
            current_version_,
            "expected_version",
            expected_version);
      } else {
        LOG_INFO("read config from __all_sys_parameter succed", K(start));
      }
    }
  }

  if (OB_SUCC(ret)) {
    if ('\0' == dump_path_[0]) {
      ret = OB_NOT_INIT;
      LOG_ERROR("Dump path doesn't set, stop read config", K(ret));
    } else if (OB_FAIL(server_config_.read_config())) {
      LOG_ERROR("Read server config failed", K(ret));
    } else if (OB_FAIL(reload_config())) {
      LOG_WARN("Reload configuration failed", K(ret));
    } else if (OB_FAIL(dump2file())) {
      LOG_WARN("Dump to file failed", K_(dump_path), K(ret));
    } else {
      LOG_INFO("Reload server config successfully!");
      ret = config_backup();
    }
    server_config_.print();
  } else {
    LOG_WARN("Read system config from inner table error", K(ret));
  }
  return ret;
}

int ObConfigManager::got_version(int64_t version, const bool remove_repeat /* = false */)
{
  int ret = OB_SUCCESS;
  bool schedule_task = false;
  if (!init_config_load_) {
    schedule_task = false;
  } else if (version < 0) {
    // from rs_admin, do whatever
    update_task_.update_local_ = false;
    schedule_task = true;
  } else if (0 == version) {
    // do nothing
    LOG_DEBUG("root server restarting");
  } else if (current_version_ == version) {
    // no new version
  } else if (version < current_version_) {
    LOG_WARN("Local config is newer than rs, weird", K_(current_version), K(version));
  } else if (version > current_version_) {
371 372 373 374
    // local:current_version_, got:version
    LOG_INFO("Got new config version", K_(current_version), K(version));
    update_task_.update_local_ = true;
    schedule_task = true;
O
oceanbase-admin 已提交
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
  }

  if (schedule_task) {
    bool schedule = true;
    if (!inited_) {
      // if got a config version before manager init, ignore this version
      schedule = false;
      ret = OB_NOT_INIT;
      LOG_WARN("Couldn't update config because timer is NULL", K(ret));
    } else if (true == remove_repeat) {
      bool task_exist = false;
      ret = TG_TASK_EXIST(lib::TGDefIDs::CONFIG_MGR, update_task_, task_exist);
      if (OB_SUCC(ret)) {
        schedule = !task_exist;
      }
      LOG_INFO("no need repeat schedule the same task");
    } else {
      // do nothing
    }

    if (schedule) {
      bool task_exist = false;
      int tmp_ret = TG_TASK_EXIST(lib::TGDefIDs::CONFIG_MGR, update_task_, task_exist);
      if (task_exist) {
        TG_CANCEL(lib::TGDefIDs::CONFIG_MGR, update_task_);
400
        LOG_INFO("Cancel pending update task", K(tmp_ret), K_(current_version), K(version));
O
oceanbase-admin 已提交
401 402 403 404 405
      }
    }

    if (schedule) {
      update_task_.version_ = version;
L
ly0 已提交
406
      update_task_.scheduled_time_ = obsys::ObSysTimeUtil::getMonotonicTime();
O
oceanbase-admin 已提交
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431
      if (OB_FAIL(TG_SCHEDULE(lib::TGDefIDs::CONFIG_MGR, update_task_, 0, false))) {
        LOG_ERROR("Update local config failed", K(ret));
      } else {
        LOG_INFO("Schedule update config task successfully!");
      }
    } else {
      // do nothing
    }
  }
  return ret;
}

void ObConfigManager::UpdateTask::runTimerTask()
{
  int ret = OB_SUCCESS;
  if (OB_ISNULL(config_mgr_)) {
    ret = OB_NOT_INIT;
    LOG_WARN("invalid argument", K_(config_mgr), K(ret));
  } else {
    const int64_t old_current_version = config_mgr_->current_version_;
    const int64_t version = version_;
    ObCurTraceId::init(config_mgr_->self_);
    THIS_WORKER.set_timeout_ts(INT64_MAX);
    if (config_mgr_->current_version_ == version) {
      ret = OB_ALREADY_DONE;
432
    } else if (config_mgr_->current_version_ > version) {
O
oceanbase-admin 已提交
433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473
      ret = OB_CANCELED;
    } else if (update_local_) {
      config_mgr_->current_version_ = version;
      if (OB_FAIL(config_mgr_->system_config_.clear())) {
        // just print log, ignore ret
        LOG_WARN("Clear system config map failed", K(ret));
      } else {
        // do nothing
      }
      if (OB_FAIL(config_mgr_->update_local(version))) {
        LOG_WARN("Update local config failed", K(ret));
        // recovery current_version_
        config_mgr_->current_version_ = old_current_version;
        // retry update local config in 1s later
        if (OB_FAIL(TG_SCHEDULE(lib::TGDefIDs::CONFIG_MGR, *this, 1000 * 1000L, false))) {
          LOG_WARN("Reschedule update local config failed", K(ret));
        }
      } else {
        // do nothing
        const int64_t read_version = config_mgr_->system_config_.get_version();
        LOG_INFO("loaded new config",
            "read_version",
            read_version,
            "old_version",
            old_current_version,
            "current_version",
            config_mgr_->current_version_,
            "expected_version",
            version);
      }
    } else if (OB_FAIL(config_mgr_->reload_config())) {
      LOG_WARN("Reload configuration failed", K(ret));
    } else {
      config_mgr_->server_config_.print();
    }
    ObCurTraceId::reset();
  }
}

}  // namespace common
}  // namespace oceanbase