ob_dag_scheduler.h 25.0 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
/**
 * 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.
 */

#ifndef SRC_SHARE_SCHEDULER_OB_DAG_SCHEDULER_H_
#define SRC_SHARE_SCHEDULER_OB_DAG_SCHEDULER_H_

#include "lib/ob_define.h"
#include "lib/allocator/ob_concurrent_fifo_allocator.h"
#include "lib/container/ob_se_array.h"
#include "lib/hash/ob_hashmap.h"
#include "lib/list/ob_dlink_node.h"
#include "lib/lock/ob_thread_cond.h"
#include "lib/lock/ob_mutex.h"
#include "lib/net/ob_addr.h"
#include "lib/profile/ob_trace_id.h"
#include "lib/thread/thread_pool.h"

namespace oceanbase {

namespace storage {
class ObDagWarningInfo;
}
namespace share {

typedef common::ObCurTraceId::TraceId ObDagId;
class ObIDag;
class ObDagScheduler;
class ObDagWorker;

class ObITask : public common::ObDLinkBase<ObITask> {
  static const int64_t DEFAULT_CHILDREN_NUM = 8;
  friend class ObIDag;
  friend class ObDagScheduler;

G
gm 已提交
44
public:
O
oceanbase-admin 已提交
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
  enum ObITaskType {
    TASK_TYPE_UT = 0,
    TASK_TYPE_MACROMERGE = 1,
    TASK_TYPE_INDEX_FINISH = 2,
    TASK_TYPE_MAIN_FINISH = 3,
    TASK_TYPE_MINOR_MERGE = 4,
    TASK_TYPE_INDEX_PERPARE = 5,
    TASK_TYPE_INDEX_LOCAL_SORT = 6,
    TASK_TYPE_INDEX_MERGE = 7,
    TASK_TYPE_NORMAL_MINOR_MERGE = 8,
    TASK_TYPE_BUILD_INDEX_NORMAL_MERGE = 9,
    TASK_TYPE_UNIQUE_INDEX_CHECKING = 10,
    TASK_TYPE_REPORT_INDEX_STATUS = 11,
    TASK_TYPE_MERGE_PREPARE_TASK = 12,
    TASK_TYPE_INDEX_MERGE_TO_LATEST_FINISH = 13,
    TASK_TYPE_COMPACT_TO_LASTEST = 14,
    TASK_TYPE_SSTABLE_MERGE_PREPARE = 15,
    TASK_TYPE_SSTABLE_MERGE_FINISH = 16,
    TASK_TYPE_SPLIT_PREPARE_TASK = 17,
    TASK_TYPE_SPLIT_TASK = 18,
    TASK_TYPE_SPLIT_FINISH_TASK = 19,
    TASK_TYPE_UNIQUE_CHECKING_PREPARE = 20,
    TASK_TYPE_SIMPLE_UNIQUE_CHECKING = 21,
    TASK_TYPE_MIGRATE_PREPARE = 22,
    TASK_TYPE_MIGRATE_COPY_LOGIC = 23,
    TASK_TYPE_MIGRATE_FINISH_LOGIC = 24,
    TASK_TYPE_MIGRATE_COPY_PHYSICAL = 25,
    TASK_TYPE_MIGRATE_FINISH_PHYSICAL = 26,
    TASK_TYPE_MIGRATE_FINISH = 27,
    TASK_TYPE_FAKE = 28,
    TASK_TYPE_MIGRATE_ENABLE_REPLAY = 29,
    TASK_TYPE_MAJOR_MERGE_FINISH = 30,
    TASK_TYPE_GROUP_MIGRATE = 31,
    TASK_TYPE_SQL_BUILD_INDEX = 32,  // build index by sql plan.
    TASK_TYPE_SERVER_PREPROCESS = 33,
    TASK_TYPE_FAST_RECOVERY = 34,
    TASK_TYPE_MIGRATE_POST_PREPARE = 35,
    TASK_TYPE_FAST_MIGRATE_ASYNC_TASK = 36,
    TASK_TYPE_VALIDATE_BACKUP = 37,
    TASK_TYPE_VALIDATE_FINISH = 38,
    TASK_TYPE_BUILD_CHANGE_REPLICA = 39,
    TASK_TYPE_RESTORE_TAILORED_PREPARE = 40,
    TASK_TYPE_RESTORE_TAILORED_PROCESS = 41,
    TASK_TYPE_RESTORE_TAILORED_FINISH = 42,
    TASK_TYPE_MAX,
  };

  enum ObITaskStatus {
    TASK_STATUS_INITING = 0,
    TASK_STATUS_WAITING = 1,
    TASK_STATUS_RUNNING = 2,
    TASK_STATUS_FINISHED = 3,
  };

  enum ObITaskColor {
    WHITE,  // not visited
    GRAY,   // visiting, on the stack
    BLACK,  // visited, all paths have walked
  };
  explicit ObITask(const ObITaskType type);
  virtual ~ObITask();
  int do_work();
  bool is_valid() const;
  int add_child(ObITask& child);
  ObIDag* get_dag() const
  {
    return dag_;
  }
  ObITaskStatus get_status() const
  {
    return status_;
  }
  ObITaskType get_type() const
  {
    return type_;
  }
  const common::ObIArray<ObITask*>& get_child_tasks() const
  {
    return children_;
  }
  VIRTUAL_TO_STRING_KV(K_(type), K_(status), K(dag_));

G
gm 已提交
127
private:
O
oceanbase-admin 已提交
128 129 130 131 132 133 134
  virtual int generate_next_task(ObITask*& next_task)
  {
    UNUSED(next_task);
    return common::OB_ITER_END;
  }
  virtual int process() = 0;

G
gm 已提交
135
private:
O
oceanbase-admin 已提交
136 137 138 139 140 141 142 143 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
  void reset();
  int copy_dep_to(ObITask& other_task) const;
  void set_status(const ObITaskStatus status)
  {
    status_ = status;
  }
  void set_dag(ObIDag& dag)
  {
    dag_ = &dag;
  }
  int64_t get_indegree() const
  {
    return indegree_;
  }
  void inc_indegree()
  {
    ATOMIC_INC(&indegree_);
  }
  int64_t dec_indegree()
  {
    return ATOMIC_SAF(&indegree_, 1);
  }
  void prepare_check_cycle();
  ObITaskColor get_color()
  {
    return color_;
  }
  void set_color(const ObITaskColor color)
  {
    color_ = color;
  }
  int64_t get_last_visit_child()
  {
    return last_visit_child_;
  }
  void set_last_visit_child(const int64_t idx)
  {
    last_visit_child_ = idx;
  }
  int generate_next_task();

G
gm 已提交
177
protected:
O
oceanbase-admin 已提交
178 179
  ObIDag* dag_;

G
gm 已提交
180
private:
O
oceanbase-admin 已提交
181 182 183 184 185 186 187 188 189
  ObITaskType type_;
  ObITaskStatus status_;
  int64_t indegree_;
  int64_t last_visit_child_;
  ObITaskColor color_;
  common::ObSEArray<ObITask*, DEFAULT_CHILDREN_NUM> children_;
};

class ObFakeTask : public ObITask {
G
gm 已提交
190
public:
O
oceanbase-admin 已提交
191 192 193 194 195 196 197 198
  ObFakeTask() : ObITask(TASK_TYPE_FAKE)
  {}
  virtual ~ObFakeTask()
  {}
  virtual int process() override;
};

class ObIDag : public common::ObDLinkBase<ObIDag> {
G
gm 已提交
199
public:
O
oceanbase-admin 已提交
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 230 231 232 233 234 235 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 263 264 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
  // priority from high to low, high priority tasks can preempt low priority tasks util their lower bound
  enum ObIDagPriority {
    DAG_PRIO_TRANS_TABLE_MERGE = 0,
    DAG_PRIO_SSTABLE_MINI_MERGE,
    DAG_PRIO_SSTABLE_MINOR_MERGE,
    DAG_PRIO_GROUP_MIGRATE,
    DAG_PRIO_MIGRATE_HIGH,
    DAG_PRIO_MIGRATE_MID,
    DAG_PRIO_SSTABLE_MAJOR_MERGE,
    DAG_PRIO_BACKUP,
    DAG_PRIO_MIGRATE_LOW,
    DAG_PRIO_CREATE_INDEX,
    DAG_PRIO_SSTABLE_SPLIT,
    DAG_PRIO_VALIDATE,
    DAG_PRIO_MAX,
  };
  const static common::ObString ObIDagPriorityStr[DAG_PRIO_MAX]; /* = {
     "DAG_PRIO_TRANS_TABLE_MERGE",
     "DAG_PRIO_SSTABLE_MINI_MERGE",
     "DAG_PRIO_SSTABLE_MINOR_MERGE",
     "DAG_PRIO_GROUP_MIGRATE",
     "DAG_PRIO_MIGRATE_HIGH",
     "DAG_PRIO_MIGRATE_MID",
     "DAG_PRIO_SSTABLE_MAJOR_MERGE",
     "DAG_PRIO_BACKUP",
     "DAG_PRIO_MIGRATE_LOW",
     "DAG_PRIO_CREATE_INDEX",
     "DAG_PRIO_SSTABLE_SPLIT",
     "DAG_PRIO_VALIDATE"
   };*/

  // We limit the max concurrency of tasks by UpLimitType (ult for short)
  // why not simply use Priority? since several priorities may share one UpLimitType
  // e.g. the total running task number of migrate tasks should be limited, but we want
  // each priority can use up to up limit when no other priorities have tasks. If we
  // limit max concurrency by priority, we may use as many as three times threads than
  // we expected.
  enum ObIDagUpLimitType {
    DAG_ULT_MINI_MERGE = 0,
    DAG_ULT_MINOR_MERGE = 1,
    DAG_ULT_GROUP_MIGRATE = 2,
    DAG_ULT_MIGRATE = 3,
    DAG_ULT_MAJOR_MERGE = 4,
    DAG_ULT_CREATE_INDEX = 5,
    DAG_ULT_SPLIT = 6,
    DAG_ULT_BACKUP = 7,
    DAG_ULT_MAX,
  };
  const static common::ObString ObIDagUpLimitTypeStr[DAG_ULT_MAX]; /* = {
     "DAG_ULT_MINI_MERGE",
     "DAG_ULT_MINOR_MERGE",
     "DAG_ULT_GROUP_MIGRATE",
     "DAG_ULT_MIGRATE",
     "DAG_ULT_MAJOR_MERGE",
     "DAG_ULT_CREATE_INDEX",
     "DAG_ULT_SPLIT",
     "DAG_ULT_BACKUP"
   };*/
  enum ObIDagType {
    DAG_TYPE_UT = 0,
    DAG_TYPE_SSTABLE_MINOR_MERGE = 1,
    DAG_TYPE_SSTABLE_MAJOR_MERGE = 2,
    DAG_TYPE_CREATE_INDEX = 3,
    DAG_TYPE_SSTABLE_SPLIT = 4,
    DAG_TYPE_UNIQUE_CHECKING = 5,
    DAG_TYPE_MIGRATE = 6,
    DAG_TYPE_MAJOR_MERGE_FINISH = 7,
    DAG_TYPE_GROUP_MIGRATE = 8,
    DAG_TYPE_SQL_BUILD_INDEX = 9,  // build index by sql plan.
    DAG_TYPE_SSTABLE_MINI_MERGE = 10,
    DAG_TYPE_TRANS_TABLE_MERGE = 11,
    DAG_TYPE_FAST_RECOVERY_SPLIT = 12,
    DAG_TYPE_FAST_RECOVERY_RECOVER = 13,
    DAG_TYPE_BACKUP = 14,
    DAG_TYPE_SERVER_PREPROCESS = 15,
    DAG_TYPE_FAST_RECOVERY = 16,
    DAG_TYPE_VALIDATE = 17,
    DAG_TYPE_MAX,
  };

  const static char* ObIDagTypeStr[DAG_TYPE_MAX];

  enum ObDagStatus {
    DAG_STATUS_INITING = 0,
    DAG_STATUS_READY = 1,         // the dag is ready
    DAG_STATUS_NODE_RUNNING = 2,  // the dag is running,the ready dag and the running dag are both in ready_list
    DAG_STATUS_FINISH = 3,
    DAG_STATUS_NODE_FAILED = 4,
    DAG_STATUS_ABORT = 5,  // not using status 5, 6, 7
    DAG_STATUS_RM = 6,
    DAG_STATUS_HALTED = 7,
  };

  explicit ObIDag(const ObIDagType type, const ObIDagPriority priority);
  virtual ~ObIDag();
  int add_task(ObITask& task);
  template <typename T>
  int alloc_task(T*& task);
  int get_dag_ret() const
  {
    return dag_ret_;
  };
  ObDagStatus get_dag_status() const
  {
    return dag_status_;
  }
  int64_t get_priority() const
  {
    return priority_;
  }
  const ObDagId& get_dag_id() const
  {
    return id_;
  }
  int set_dag_id(const ObDagId& dag_id);
  ObIDagType get_type() const
  {
    return type_;
  }
  const char* get_name() const
  {
    return ObIDagTypeStr[type_];
  }
  bool has_set_stop()
  {
    return is_stop_;
  }
  void set_stop()
  {
    is_stop_ = true;
  }
  void set_priority(ObIDag::ObIDagPriority prio)
  {
    priority_ = prio;
  }
  int64_t get_running_task_count() const
  {
    return running_task_cnt_;
  }
  virtual void gene_basic_warning_info(storage::ObDagWarningInfo& info);
  virtual void gene_warning_info(storage::ObDagWarningInfo& info);
  virtual bool ignore_warning()
  {
    return false;
  }
  DECLARE_VIRTUAL_TO_STRING;
  DISABLE_COPY_ASSIGN(ObIDag);

G
gm 已提交
348
public:
O
oceanbase-admin 已提交
349 350 351 352 353 354
  virtual bool operator==(const ObIDag& other) const = 0;
  virtual int64_t hash() const = 0;
  virtual int64_t get_tenant_id() const = 0;
  virtual int fill_comment(char* buf, const int64_t buf_len) const = 0;
  virtual int64_t get_compat_mode() const = 0;

G
gm 已提交
355
protected:
O
oceanbase-admin 已提交
356 357
  int dag_ret_;

G
gm 已提交
358
private:
O
oceanbase-admin 已提交
359 360 361 362 363 364 365 366 367 368 369
  typedef common::ObDList<ObITask> TaskList;
  typedef lib::ObLockGuard<ObIDag> ObDagGuard;
  static const int64_t DEFAULT_TASK_NUM = 32;
  static const int64_t TOTAL_LIMIT = 1024L * 1024L * 1024L;
  static const int64_t HOLD_LIMIT = 8 * 1024L * 1024L;
  static const int64_t PAGE_SIZE = common::OB_MALLOC_NORMAL_BLOCK_SIZE;
  friend class ObDagScheduler;
  friend class ObITask;
  friend class ObDagWorker;
  friend ObDagGuard;

G
gm 已提交
370
private:
O
oceanbase-admin 已提交
371 372 373 374 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 400 401 402 403 404 405
  void free_task(ObITask& task);
  int finish_task(ObITask& task, int64_t& available_cnt);
  bool is_valid();
  bool is_valid_type() const;
  int init(const int64_t total = TOTAL_LIMIT, const int64_t hold = HOLD_LIMIT, const int64_t page_size = PAGE_SIZE);
  void restart_task(ObITask& task);
  void reset();
  int check_cycle();
  int get_next_ready_task(ObITask*& task);
  bool has_finished() const;
  void set_dag_ret(const int ret)
  {
    ATOMIC_VCAS(&dag_ret_, common::OB_SUCCESS, ret);
  }
  void set_dag_status(const ObDagStatus status)
  {
    dag_status_ = status;
  }
  int lock()
  {
    return lock_.lock();
  }
  int unlock()
  {
    return lock_.unlock();
  }
  void inc_running_task_cnt()
  {
    ++running_task_cnt_;
  }
  void dec_running_task_cnt()
  {
    --running_task_cnt_;
  }

G
gm 已提交
406
private:
O
oceanbase-admin 已提交
407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
  common::ObConcurrentFIFOAllocator allocator_;
  bool is_inited_;
  ObIDagPriority priority_;
  ObIDagType type_;
  ObDagId id_;
  ObDagStatus dag_status_;
  int64_t running_task_cnt_;
  int64_t start_time_;
  lib::ObMutex lock_;
  TaskList task_list_;
  bool is_stop_;
};

template <typename T, int PRIOS>
class ObPriorityList {
G
gm 已提交
422
public:
O
oceanbase-admin 已提交
423 424 425 426 427 428 429 430 431 432 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 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546
  enum { PRIO_CNT = PRIOS };

  bool add_first(T* item, const int64_t prio)
  {
    bool bret = false;
    if (prio >= 0 && prio < PRIO_CNT) {
      bret = dlists_[prio].add_first(item);
    } else {
      COMMON_LOG(ERROR, "invalid priority", K(prio), K(PRIO_CNT));
    }
    return bret;
  }

  bool add_last(T* item, const int64_t prio)
  {
    bool bret = false;
    if (prio >= 0 && prio < PRIO_CNT) {
      bret = dlists_[prio].add_last(item);
    } else {
      COMMON_LOG(ERROR, "invalid priority", K(prio), K(PRIO_CNT));
    }
    return bret;
  }

  bool remove(T* item, const int64_t prio)
  {
    bool bret = false;
    if (prio >= 0 && prio < PRIO_CNT) {
      if (NULL != dlists_[prio].remove(item)) {
        bret = true;
      }
    } else {
      COMMON_LOG(ERROR, "invalid priority", K(prio), K(PRIO_CNT));
    }
    return bret;
  }

  T* get_head(const int64_t prio)
  {
    T* result = NULL;
    if (prio >= 0 && prio < PRIO_CNT) {
      result = dlists_[prio].get_header();
    } else {
      COMMON_LOG(ERROR, "invalid priority", K(prio), K(PRIO_CNT));
    }
    return result;
  }

  T* get_first(const int64_t prio)
  {
    T* result = NULL;
    if (prio >= 0 && prio < PRIO_CNT) {
      result = dlists_[prio].get_first();
    } else {
      COMMON_LOG(ERROR, "invalid priority", K(prio), K(PRIO_CNT));
    }
    return result;
  }

  T* remove_first(const int64_t prio)
  {
    T* result = NULL;
    if (prio >= 0 && prio < PRIO_CNT) {
      result = dlists_[prio].remove_first();
    } else {
      COMMON_LOG(ERROR, "invalid priority", K(prio), K(PRIO_CNT));
    }
    return result;
  }

  T* remove_last(const int64_t prio)
  {
    T* result = NULL;
    if (prio >= 0 && prio < PRIO_CNT) {
      result = dlists_[prio].remove_last();
    } else {
      COMMON_LOG(ERROR, "invalid priority", K(prio), K(PRIO_CNT));
    }
    return result;
  }

  bool is_empty(const int64_t prio) const
  {
    bool bret = false;
    if (prio >= 0 && prio < PRIO_CNT) {
      bret = dlists_[prio].is_empty();
    } else {
      COMMON_LOG(ERROR, "invalid priority", K(prio), K(PRIO_CNT));
    }
    return bret;
  }

  bool is_empty() const
  {
    bool bret = true;
    for (int64_t i = 0; i < PRIO_CNT; ++i) {
      bret &= dlists_[i].is_empty();
    }
    return bret;
  }

  void reset()
  {
    for (int64_t i = 0; i < PRIO_CNT; ++i) {
      dlists_[i].reset();
    }
  }

  common::ObDList<T>& get_list(const int64_t prio)
  {
    return dlists_[prio];
  }

  int64_t size(const int64_t prio)
  {
    int64_t ret = 0;
    if (prio >= 0 && prio < PRIO_CNT) {
      ret = dlists_[prio].get_size();
    } else {
      COMMON_LOG(ERROR, "invalid priority", K(prio), K(PRIO_CNT));
    }
    return ret;
  }

G
gm 已提交
547
private:
O
oceanbase-admin 已提交
548 549 550 551
  common::ObDList<T> dlists_[PRIOS];
};

class ObDagWorker : public lib::ThreadPool, public common::ObDLinkBase<ObDagWorker> {
G
gm 已提交
552
public:
O
oceanbase-admin 已提交
553 554 555 556 557 558 559 560 561 562
  typedef common::ObDLinkNode<ObDagWorker*> Node;
  typedef common::ObDList<Node> WorkerNodeList;
  enum DagWorkerStatus {
    DWS_FREE,
    DWS_RUNNABLE,
    DWS_WAITING,
    DWS_RUNNING,
    DWS_STOP,
  };

G
gm 已提交
563
public:
O
oceanbase-admin 已提交
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
  ObDagWorker();
  ~ObDagWorker();
  int init(const int64_t check_period);
  void destroy();
  void stop_worker();
  void resume();
  void run1() override;
  void yield();
  void set_task(ObITask* task)
  {
    task_ = task;
  }
  bool need_wake_up() const;
  ObITask* get_task() const
  {
    return task_;
  }
  static ObDagWorker* self()
  {
    return self_;
  }

G
gm 已提交
586
private:
O
oceanbase-admin 已提交
587 588
  void notify(DagWorkerStatus status);

G
gm 已提交
589
private:
O
oceanbase-admin 已提交
590 591
  static __thread ObDagWorker* self_;
  static const uint32_t SLEEP_TIME_MS = 100;  // 100ms
G
gm 已提交
592
private:
O
oceanbase-admin 已提交
593 594 595 596 597 598 599 600 601
  common::ObThreadCond cond_;
  ObITask* task_;
  DagWorkerStatus status_;
  int64_t check_period_;
  int64_t last_check_time_;
  bool is_inited_;
};

class ObLoadShedder {
G
gm 已提交
602
public:
O
oceanbase-admin 已提交
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
  enum ObLoadType {
    LOAD_PAST_ONE = 0,
    LOAD_PAST_FIVE = 1,
    LOAD_PAST_FIFTEEN = 2,
    LOAD_TYPE_MAX = 3,
  };
  ObLoadShedder();
  ~ObLoadShedder() = default;
  void refresh_stat();
  void reset();
  OB_INLINE int64_t get_shedding_factor() const
  {
    return load_shedding_factor_;
  }
  TO_STRING_KV(K_(cpu_cnt_online), K_(cpu_cnt_configure), K_(load_per_cpu_threshold), K_(load_shedding_factor),
      "load_past_one", load_avg_[LOAD_PAST_ONE], "load_past_five", load_avg_[LOAD_PAST_FIVE], "load_past_fifteen",
      load_avg_[LOAD_PAST_FIFTEEN]);

G
gm 已提交
621
private:
O
oceanbase-admin 已提交
622 623 624 625 626 627 628 629 630
  static const int64_t DEFAULT_LOAD_PER_CPU_THRESHOLD = 500;
  static const int64_t DEFAULT_LOAD_SHEDDING_FACTOR = 2;
  static constexpr int64_t LOAD_SHEDDING_FACTOR[LOAD_TYPE_MAX] = {
      4,  // load past one minute
      3,  // load past five minute
      2,  // load past fifteen minute
  };
  void refresh_load_shedding_factor();

G
gm 已提交
631
private:
O
oceanbase-admin 已提交
632 633 634 635 636 637 638 639 640 641
  int64_t cpu_cnt_online_;
  int64_t cpu_cnt_configure_;
  double load_avg_[LOAD_TYPE_MAX];
  int64_t load_per_cpu_threshold_;
  int64_t load_shedding_factor_;
};

class ObDagScheduler : public lib::ThreadPool {
  friend class ObDagWorker;

G
gm 已提交
642
public:
O
oceanbase-admin 已提交
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686
  static ObDagScheduler& get_instance();
  int init(const common::ObAddr& addr, const int64_t check_period = DEFAULT_CHECK_PERIOD,
      const int32_t work_thread_num = 0, const int64_t dag_limit = DEFAULT_MAX_DAG_NUM,
      const int64_t total_mem_limit = TOTAL_LIMIT, const int64_t hold_mem_limit = HOLD_LIMIT,
      const int64_t page_size = PAGE_SIZE);
  int add_dag(ObIDag* dag, const bool emergency = false);
  template <typename T>
  int alloc_dag(T*& dag);
  void free_dag(ObIDag& dag);
  void run1() final;
  void notify();
  void destroy();
  int set_mini_merge_concurrency(const int32_t mini_merge_concurrency);
  int set_minor_merge_concurrency(const int32_t minor_merge_concurrency);
  int set_major_merge_concurrency(const int32_t major_merge_concurrency);
  int set_create_index_concurrency(const int32_t create_index_concurrency);
  int set_migrate_concurrency(const int32_t migrate_concurrency);
  int set_group_migrate_concurrency(const int32_t migrate_concurrency);
  int set_backup_concurrency(const int32_t backup_concurrency);
  int32_t get_max_thread_cnt() const;
  int32_t get_default_work_thread_cnt(const int32_t proposed_thread_num = 0) const;
  int32_t get_work_thread_num() const
  {
    return work_thread_num_;
  }
  int32_t get_thread2reserve() const
  {
    return thread2reserve_;
  }
  bool is_empty() const
  {
    return ready_dag_list_.is_empty();
  }
  int64_t get_cur_dag_cnt() const
  {
    return dag_cnt_;
  }
  int sys_task_start(ObIDag* dag);
  int64_t get_dag_count(const ObIDag::ObIDagType type) const;
  int32_t get_running_task_cnt(const ObIDag::ObIDagPriority priority);
  int32_t get_up_limit(const int64_t up_limit_type, int32_t& up_limit);
  int check_dag_exist(const ObIDag* dag, bool& exist);
  int cancel_dag(const ObIDag* dag);

G
gm 已提交
687
private:
O
oceanbase-admin 已提交
688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757
  struct ThreadLimit {
    int32_t min_thread_;
    int32_t max_thread_;
    ThreadLimit() : min_thread_(0), max_thread_(0)
    {}
    TO_STRING_KV(K_(min_thread), K_(max_thread));
  };
  typedef common::ObDList<ObIDag> DagList;
  typedef ObPriorityList<ObIDag, ObIDag::DAG_PRIO_MAX> PriorityDagList;
  typedef ObPriorityList<ObDagWorker, ObIDag::DAG_PRIO_MAX> PriorityWorkerList;
  typedef common::ObDList<ObDagWorker> WorkerList;
  typedef common::hash::ObHashMap<const ObIDag*, ObIDag*, common::hash::NoPthreadDefendMode,
      common::hash::hash_func<const ObIDag*>, common::hash::equal_to<const ObIDag*> >
      DagMap;
  static const uint32_t DEFAULT_TIME_SLICE_US = 500 * 1000;
  static const int64_t SCHEDULER_WAIT_TIME_MS = 1000;    // 1s
  static const int64_t WORK_THREAD_WAIT_TIME_MS = 1000;  // 1s
  static const int64_t TOTAL_LIMIT = 1024L * 1024L * 1024L;
  static const int64_t HOLD_LIMIT = 8 * 1024L * 1024L;
  static const int64_t PAGE_SIZE = common::OB_MALLOC_NORMAL_BLOCK_SIZE;
  static const int64_t DAG_SIZE_LIMIT = 10 << 12;
  static const int64_t DEFAULT_MAX_DAG_NUM = 15000;
  static const int32_t DEFAULT_WORK_THREAD_NUM = 10;
  static const int32_t DEFAULT_MAX_CONCURRENCY = 10;
  static const int32_t DEFAULT_MINOR_MERGE_CONCURRENCY = 10;
  static const int32_t WORK_THREAD_CNT_PERCENT = 30;
  static const int32_t SPARE_THREAD_PERCENT = 10;
  static const int64_t DUMP_DAG_STATUS_INTERVAL = 10 * 1000LL * 1000LL;
  static const int64_t DEFAULT_CHECK_PERIOD = 3 * 1000 * 1000;  // 3s
  static const int32_t MAX_THREAD_LIMIT = 64;
  static const int32_t MAX_MIGRATE_THREAD_NUM = 64;
  static const int32_t MAX_GROUP_MIGRATE_THREAD_NUM = 20;
  static const int32_t MAX_VALIDATE_THREAD_NUM = 20;
  static constexpr int32_t DEFAULT_LOW_LIMIT[ObIDag::DAG_PRIO_MAX] = {1, /*trans table*/
      2,                                                                 /*Mini merge*/
      3,                                                                 /*Minor merge*/
      2,                                                                 /*Group migrate*/
      2,                                                                 /*Migrate Replica High Prio*/
      2,                                                                 /*Migrate Replica Mid Prio*/
      1,                                                                 /*Major merge*/
      5,                                                                 /*Backup task*/
      1,                                                                 /*Migrate Replica Low Prio*/
      2,                                                                 /*Create index*/
      1,                                                                 /*Split task*/
      5,
      /*Validate task*/};
  static constexpr int32_t DEFAULT_UP_LIMIT[ObIDag::DAG_ULT_MAX] = {6, /*Mini merge and trans table*/
      10,                                                              /*Minor merge*/
      2,                                                               /*Group migrate*/
      10,                                                              /*Migrate*/
      10,                                                              /*Major merge*/
      10,                                                              /*Create index*/
      10,                                                              /*Split*/
      10,
      /*Backup*/};
  // map from priority to UpLimitType
  static constexpr ObIDag::ObIDagUpLimitType UP_LIMIT_MAP[ObIDag::DAG_PRIO_MAX] = {
      ObIDag::DAG_ULT_MINI_MERGE, /*trans table*/
      ObIDag::DAG_ULT_MINI_MERGE, /*mini merge*/
      ObIDag::DAG_ULT_MINOR_MERGE,
      ObIDag::DAG_ULT_GROUP_MIGRATE,
      ObIDag::DAG_ULT_MIGRATE,
      ObIDag::DAG_ULT_MIGRATE,
      ObIDag::DAG_ULT_MAJOR_MERGE,
      ObIDag::DAG_ULT_BACKUP,
      ObIDag::DAG_ULT_MIGRATE,
      ObIDag::DAG_ULT_CREATE_INDEX,
      ObIDag::DAG_ULT_SPLIT,
      ObIDag::DAG_ULT_BACKUP};

G
gm 已提交
758
private:
O
oceanbase-admin 已提交
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777
  ObDagScheduler();
  virtual ~ObDagScheduler();
  int schedule();
  int pop_task(const int64_t priority, ObITask*& task);
  int finish_task(ObITask& task, ObDagWorker& worker);
  bool has_remain_task(const int64_t priority);
  int create_worker();
  int try_reclaim_threads();
  int schedule_one(const int64_t priority);
  int dispatch_task(ObITask& task, ObDagWorker*& ret_worker);
  void destroy_all_workers();
  int set_min_thread(const int64_t priority, const int32_t concurrency);
  int set_max_thread(const int64_t priority, const int32_t concurrency);
  bool try_switch(ObDagWorker& worker);
  int try_switch(ObDagWorker& worker, const int64_t src_prio, const int64_t dest_prio, bool& need_pause);
  void pause_worker(ObDagWorker& worker, const int64_t priority);
  void dump_dag_status();
  int check_need_load_shedding(const int64_t priority, const bool for_schedule, bool& need_shedding);

G
gm 已提交
778
private:
O
oceanbase-admin 已提交
779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
  bool is_inited_;
  common::ObAddr addr_;
  DagMap dag_map_;
  PriorityDagList ready_dag_list_;
  common::ObThreadCond scheduler_sync_;
  int64_t dag_cnt_;
  int64_t dag_limit_;
  int64_t check_period_;
  int32_t total_worker_cnt_;
  int32_t work_thread_num_;
  int32_t default_thread_num_;
  int32_t thread2reserve_;
  int32_t total_running_task_cnt_;
  int32_t running_task_cnts_[ObIDag::DAG_PRIO_MAX];
  int32_t running_task_cnts_per_ult_[ObIDag::DAG_ULT_MAX];  // count running task number for each UpLimitType
  int32_t low_limits_[ObIDag::DAG_PRIO_MAX];
  int32_t up_limits_[ObIDag::DAG_ULT_MAX];
  int64_t dag_cnts_[ObIDag::DAG_TYPE_MAX];
  common::ObConcurrentFIFOAllocator allocator_;
  PriorityWorkerList waiting_workers_;  // workers waiting for time slice to run
  PriorityWorkerList running_workers_;  // running workers
  WorkerList free_workers_;             // free workers who have not been assigned to any task
  ObLoadShedder load_shedder_;
};

template <typename T>
int ObIDag::alloc_task(T*& task)
{
  int ret = common::OB_SUCCESS;
  void* buf = NULL;
  task = NULL;

  if (IS_NOT_INIT) {
    ret = common::OB_NOT_INIT;
    COMMON_LOG(WARN, "dag is not inited", K(ret));
  } else if (NULL == (buf = allocator_.alloc(sizeof(T)))) {
    ret = common::OB_ALLOCATE_MEMORY_FAILED;
    task = NULL;
    COMMON_LOG(WARN, "failed to alloc task", K(ret));
  } else {
    T* ntask = new (buf) T();
    ntask->set_dag(*this);
    {
      lib::ObMutexGuard guard(lock_);
      if (!task_list_.add_last(ntask)) {
        ret = common::OB_ERR_UNEXPECTED;
        COMMON_LOG(WARN, "Failed to add task", K(task), K_(id));
      }
    }
    if (OB_SUCC(ret)) {
      task = ntask;
    }
  }
  return ret;
}

template <typename T>
int ObDagScheduler::alloc_dag(T*& dag)
{
  int ret = common::OB_SUCCESS;
  void* buf = NULL;
  dag = NULL;

  if (IS_NOT_INIT) {
    ret = common::OB_NOT_INIT;
    COMMON_LOG(WARN, "scheduler is not init", K(ret));
  } else if (DAG_SIZE_LIMIT < sizeof(T)) {
    ret = common::OB_ALLOCATE_MEMORY_FAILED;
    COMMON_LOG(WARN, "Dag Object is too large", K(ret), K(sizeof(T)));
  } else {
    if (NULL == (buf = allocator_.alloc(sizeof(T)))) {
      ret = common::OB_ALLOCATE_MEMORY_FAILED;
      COMMON_LOG(WARN, "failed to alloc dag", K(ret));
    } else {
      ObIDag* new_dag = new (buf) T();
      if (OB_FAIL(new_dag->init())) {
        COMMON_LOG(WARN, "failed to init allocator", K(ret));
      } else {
        dag = static_cast<T*>(new_dag);
      }
    }
  }
  return ret;
}

inline void dag_yield()
{
  ObDagWorker* worker = ObDagWorker::self();
  if (NULL != worker) {
    worker->yield();
  }
}

}  // namespace share
}  // namespace oceanbase

#endif /* SRC_SHARE_SCHEDULER_OB_DAG_SCHEDULER_H_ */