vnodeMgr.c 2.9 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

H
Hongze Cheng 已提交
16
#include "vnodeInt.h"
S
Shengliang Guan 已提交
17
#include "tglobal.h"
H
Hongze Cheng 已提交
18

H
Hongze Cheng 已提交
19
SVnodeMgr vnodeMgr = {.vnodeInitFlag = TD_MOD_UNINITIALIZED};
H
Hongze Cheng 已提交
20 21 22

static void* loop(void* arg);

S
Shengliang Guan 已提交
23
int vnodeInit() {
H
Hongze Cheng 已提交
24 25 26 27
  if (TD_CHECK_AND_SET_MODE_INIT(&(vnodeMgr.vnodeInitFlag)) == TD_MOD_INITIALIZED) {
    return 0;
  }

H
Hongze Cheng 已提交
28 29
  vnodeMgr.stop = false;

H
Hongze Cheng 已提交
30
  // Start commit handers
S
Shengliang Guan 已提交
31
  vnodeMgr.nthreads = tsNumOfCommitThreads;
wafwerar's avatar
wafwerar 已提交
32
  vnodeMgr.threads = taosMemoryCalloc(vnodeMgr.nthreads, sizeof(TdThread));
S
Shengliang Guan 已提交
33 34 35
  if (vnodeMgr.threads == NULL) {
    return -1;
  }
H
Hongze Cheng 已提交
36

S
Shengliang Guan 已提交
37 38 39
  taosThreadMutexInit(&(vnodeMgr.mutex), NULL);
  taosThreadCondInit(&(vnodeMgr.hasTask), NULL);
  TD_DLIST_INIT(&(vnodeMgr.queue));
H
Hongze Cheng 已提交
40

S
Shengliang Guan 已提交
41 42 43
  for (uint16_t i = 0; i < vnodeMgr.nthreads; i++) {
    taosThreadCreate(&(vnodeMgr.threads[i]), NULL, loop, NULL);
    // pthread_setname_np(vnodeMgr.threads[i], "VND Commit Thread");
H
Hongze Cheng 已提交
44 45 46 47 48 49 50 51 52
  }

  if (walInit() < 0) {
    return -1;
  }

  return 0;
}

S
Shengliang Guan 已提交
53
void vnodeCleanup() {
H
Hongze Cheng 已提交
54
  if (TD_CHECK_AND_SET_MOD_CLEAR(&(vnodeMgr.vnodeInitFlag)) == TD_MOD_UNINITIALIZED) {
H
Hongze Cheng 已提交
55 56 57 58
    return;
  }

  // Stop commit handler
wafwerar's avatar
wafwerar 已提交
59
  taosThreadMutexLock(&(vnodeMgr.mutex));
H
Hongze Cheng 已提交
60
  vnodeMgr.stop = true;
wafwerar's avatar
wafwerar 已提交
61 62
  taosThreadCondBroadcast(&(vnodeMgr.hasTask));
  taosThreadMutexUnlock(&(vnodeMgr.mutex));
H
Hongze Cheng 已提交
63 64

  for (uint16_t i = 0; i < vnodeMgr.nthreads; i++) {
wafwerar's avatar
wafwerar 已提交
65
    taosThreadJoin(vnodeMgr.threads[i], NULL);
H
Hongze Cheng 已提交
66 67
  }

wafwerar's avatar
wafwerar 已提交
68
  taosMemoryFreeClear(vnodeMgr.threads);
wafwerar's avatar
wafwerar 已提交
69 70
  taosThreadCondDestroy(&(vnodeMgr.hasTask));
  taosThreadMutexDestroy(&(vnodeMgr.mutex));
H
Hongze Cheng 已提交
71 72
}

H
Hongze Cheng 已提交
73
int vnodeScheduleTask(SVnodeTask* pTask) {
wafwerar's avatar
wafwerar 已提交
74
  taosThreadMutexLock(&(vnodeMgr.mutex));
H
Hongze Cheng 已提交
75

H
Hongze Cheng 已提交
76
  TD_DLIST_APPEND(&(vnodeMgr.queue), pTask);
H
Hongze Cheng 已提交
77

wafwerar's avatar
wafwerar 已提交
78
  taosThreadCondSignal(&(vnodeMgr.hasTask));
H
Hongze Cheng 已提交
79

wafwerar's avatar
wafwerar 已提交
80
  taosThreadMutexUnlock(&(vnodeMgr.mutex));
H
Hongze Cheng 已提交
81 82 83 84 85

  return 0;
}

/* ------------------------ STATIC METHODS ------------------------ */
H
Hongze Cheng 已提交
86
static void* loop(void* arg) {
H
Haojun Liao 已提交
87 88
  setThreadName("vnode-commit");

H
Hongze Cheng 已提交
89 90
  SVnodeTask* pTask;
  for (;;) {
wafwerar's avatar
wafwerar 已提交
91
    taosThreadMutexLock(&(vnodeMgr.mutex));
H
Hongze Cheng 已提交
92 93 94 95
    for (;;) {
      pTask = TD_DLIST_HEAD(&(vnodeMgr.queue));
      if (pTask == NULL) {
        if (vnodeMgr.stop) {
wafwerar's avatar
wafwerar 已提交
96
          taosThreadMutexUnlock(&(vnodeMgr.mutex));
H
Hongze Cheng 已提交
97 98
          return NULL;
        } else {
wafwerar's avatar
wafwerar 已提交
99
          taosThreadCondWait(&(vnodeMgr.hasTask), &(vnodeMgr.mutex));
H
Hongze Cheng 已提交
100 101
        }
      } else {
H
Hongze Cheng 已提交
102
        TD_DLIST_POP(&(vnodeMgr.queue), pTask);
H
Hongze Cheng 已提交
103 104 105 106
        break;
      }
    }

wafwerar's avatar
wafwerar 已提交
107
    taosThreadMutexUnlock(&(vnodeMgr.mutex));
H
Hongze Cheng 已提交
108 109

    (*(pTask->execute))(pTask->arg);
wafwerar's avatar
wafwerar 已提交
110
    taosMemoryFree(pTask);
H
Hongze Cheng 已提交
111 112 113 114
  }

  return NULL;
}