vnodeMgr.c 3.6 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
refact  
Hongze Cheng 已提交
16
#include "vnd.h"
H
Hongze Cheng 已提交
17

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

static void* loop(void* arg);

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

H
Hongze Cheng 已提交
27
  vnodeMgr.stop = false;
S
shm  
Shengliang Guan 已提交
28 29
  vnodeMgr.putToQueryQFp = pOption->putToQueryQFp;
  vnodeMgr.sendReqFp = pOption->sendReqFp;
H
Hongze Cheng 已提交
30

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

wafwerar's avatar
wafwerar 已提交
39 40
    taosThreadMutexInit(&(vnodeMgr.mutex), NULL);
    taosThreadCondInit(&(vnodeMgr.hasTask), NULL);
H
Hongze Cheng 已提交
41
    TD_DLIST_INIT(&(vnodeMgr.queue));
H
Hongze Cheng 已提交
42

S
Shengliang Guan 已提交
43
    for (uint16_t i = 0; i < pOption->nthreads; i++) {
wafwerar's avatar
wafwerar 已提交
44
      taosThreadCreate(&(vnodeMgr.threads[i]), NULL, loop, NULL);
H
Hongze Cheng 已提交
45
      // pthread_setname_np(vnodeMgr.threads[i], "VND Commit Thread");
H
Hongze Cheng 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59
    }
  } else {
    // TODO: if no commit thread is set, then another mechanism should be
    // given. Otherwise, it is a false.
    ASSERT(0);
  }

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

  return 0;
}

S
Shengliang Guan 已提交
60
void vnodeCleanup() {
H
Hongze Cheng 已提交
61
  if (TD_CHECK_AND_SET_MOD_CLEAR(&(vnodeMgr.vnodeInitFlag)) == TD_MOD_UNINITIALIZED) {
H
Hongze Cheng 已提交
62 63 64 65
    return;
  }

  // Stop commit handler
wafwerar's avatar
wafwerar 已提交
66
  taosThreadMutexLock(&(vnodeMgr.mutex));
H
Hongze Cheng 已提交
67
  vnodeMgr.stop = true;
wafwerar's avatar
wafwerar 已提交
68 69
  taosThreadCondBroadcast(&(vnodeMgr.hasTask));
  taosThreadMutexUnlock(&(vnodeMgr.mutex));
H
Hongze Cheng 已提交
70 71

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

  tfree(vnodeMgr.threads);
wafwerar's avatar
wafwerar 已提交
76 77
  taosThreadCondDestroy(&(vnodeMgr.hasTask));
  taosThreadMutexDestroy(&(vnodeMgr.mutex));
H
Hongze Cheng 已提交
78 79
}

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

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

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

wafwerar's avatar
wafwerar 已提交
87
  taosThreadMutexUnlock(&(vnodeMgr.mutex));
H
Hongze Cheng 已提交
88 89 90 91

  return 0;
}

S
shm  
Shengliang Guan 已提交
92
int32_t vnodePutToVQueryQ(SVnode* pVnode, struct SRpcMsg* pReq) {
S
shm  
Shengliang Guan 已提交
93
  if (pVnode == NULL || pVnode->pMeta == NULL || vnodeMgr.putToQueryQFp == NULL) {
S
Shengliang Guan 已提交
94 95 96
    terrno = TSDB_CODE_VND_APP_ERROR;
    return -1;
  }
S
shm  
Shengliang Guan 已提交
97
  return (*vnodeMgr.putToQueryQFp)(pVnode->pWrapper, pReq);
S
Shengliang Guan 已提交
98 99
}

S
shm  
Shengliang Guan 已提交
100 101
void vnodeSendReq(SVnode* pVnode, struct SEpSet* epSet, struct SRpcMsg* pReq) {
  (*vnodeMgr.sendReqFp)(pVnode->pWrapper, epSet, pReq);
S
Shengliang 已提交
102 103
}

H
Hongze Cheng 已提交
104
/* ------------------------ STATIC METHODS ------------------------ */
H
Hongze Cheng 已提交
105
static void* loop(void* arg) {
H
Haojun Liao 已提交
106 107
  setThreadName("vnode-commit");

H
Hongze Cheng 已提交
108 109
  SVnodeTask* pTask;
  for (;;) {
wafwerar's avatar
wafwerar 已提交
110
    taosThreadMutexLock(&(vnodeMgr.mutex));
H
Hongze Cheng 已提交
111 112 113 114
    for (;;) {
      pTask = TD_DLIST_HEAD(&(vnodeMgr.queue));
      if (pTask == NULL) {
        if (vnodeMgr.stop) {
wafwerar's avatar
wafwerar 已提交
115
          taosThreadMutexUnlock(&(vnodeMgr.mutex));
H
Hongze Cheng 已提交
116 117
          return NULL;
        } else {
wafwerar's avatar
wafwerar 已提交
118
          taosThreadCondWait(&(vnodeMgr.hasTask), &(vnodeMgr.mutex));
H
Hongze Cheng 已提交
119 120
        }
      } else {
H
Hongze Cheng 已提交
121
        TD_DLIST_POP(&(vnodeMgr.queue), pTask);
H
Hongze Cheng 已提交
122 123 124 125
        break;
      }
    }

wafwerar's avatar
wafwerar 已提交
126
    taosThreadMutexUnlock(&(vnodeMgr.mutex));
H
Hongze Cheng 已提交
127 128

    (*(pTask->execute))(pTask->arg);
H
Hongze Cheng 已提交
129
    free(pTask);
H
Hongze Cheng 已提交
130 131 132 133
  }

  return NULL;
}