vnodeMgr.c 4.0 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
  vnodeMgr.putToQueryQFp = pOption->putToQueryQFp;
S
shm  
Shengliang Guan 已提交
29
  vnodeMgr.putToFetchQFp = pOption->putToFetchQFp;
S
shm  
Shengliang Guan 已提交
30
  vnodeMgr.sendReqFp = pOption->sendReqFp;
S
shm  
Shengliang Guan 已提交
31 32
  vnodeMgr.sendMnodeReqFp = pOption->sendMnodeReqFp;
  vnodeMgr.sendRspFp = pOption->sendRspFp;
H
Hongze Cheng 已提交
33

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

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

S
Shengliang Guan 已提交
46
    for (uint16_t i = 0; i < pOption->nthreads; i++) {
wafwerar's avatar
wafwerar 已提交
47
      taosThreadCreate(&(vnodeMgr.threads[i]), NULL, loop, NULL);
H
Hongze Cheng 已提交
48
      // pthread_setname_np(vnodeMgr.threads[i], "VND Commit Thread");
H
Hongze Cheng 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61 62
    }
  } 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 已提交
63
void vnodeCleanup() {
H
Hongze Cheng 已提交
64
  if (TD_CHECK_AND_SET_MOD_CLEAR(&(vnodeMgr.vnodeInitFlag)) == TD_MOD_UNINITIALIZED) {
H
Hongze Cheng 已提交
65 66 67 68
    return;
  }

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

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

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

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

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

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

wafwerar's avatar
wafwerar 已提交
90
  taosThreadMutexUnlock(&(vnodeMgr.mutex));
H
Hongze Cheng 已提交
91 92 93 94

  return 0;
}

S
shm  
Shengliang Guan 已提交
95 96
int32_t vnodePutToVQueryQ(SVnode* pVnode, struct SRpcMsg* pReq) {
  return (*vnodeMgr.putToQueryQFp)(pVnode->pWrapper, pReq);
S
Shengliang Guan 已提交
97 98
}

S
shm  
Shengliang Guan 已提交
99 100 101 102 103 104 105 106 107 108 109 110 111 112
int32_t vnodePutToVFetchQ(SVnode* pVnode, struct SRpcMsg* pReq) {
  return (*vnodeMgr.putToFetchQFp)(pVnode->pWrapper, pReq);
}

int32_t vnodeSendReq(SVnode* pVnode, struct SEpSet* epSet, struct SRpcMsg* pReq) {
  return (*vnodeMgr.sendReqFp)(pVnode->pWrapper, epSet, pReq);
}

int32_t vnodeSendMnodeReq(SVnode* pVnode, struct SRpcMsg* pReq) {
  return (*vnodeMgr.sendMnodeReqFp)(pVnode->pWrapper, pReq);
}

void vnodeSendRsp(SVnode* pVnode, struct SEpSet* epSet, struct SRpcMsg* pRsp) {
  (*vnodeMgr.sendRspFp)(pVnode->pWrapper, pRsp);
S
Shengliang 已提交
113 114
}

H
Hongze Cheng 已提交
115
/* ------------------------ STATIC METHODS ------------------------ */
H
Hongze Cheng 已提交
116
static void* loop(void* arg) {
H
Haojun Liao 已提交
117 118
  setThreadName("vnode-commit");

H
Hongze Cheng 已提交
119 120
  SVnodeTask* pTask;
  for (;;) {
wafwerar's avatar
wafwerar 已提交
121
    taosThreadMutexLock(&(vnodeMgr.mutex));
H
Hongze Cheng 已提交
122 123 124 125
    for (;;) {
      pTask = TD_DLIST_HEAD(&(vnodeMgr.queue));
      if (pTask == NULL) {
        if (vnodeMgr.stop) {
wafwerar's avatar
wafwerar 已提交
126
          taosThreadMutexUnlock(&(vnodeMgr.mutex));
H
Hongze Cheng 已提交
127 128
          return NULL;
        } else {
wafwerar's avatar
wafwerar 已提交
129
          taosThreadCondWait(&(vnodeMgr.hasTask), &(vnodeMgr.mutex));
H
Hongze Cheng 已提交
130 131
        }
      } else {
H
Hongze Cheng 已提交
132
        TD_DLIST_POP(&(vnodeMgr.queue), pTask);
H
Hongze Cheng 已提交
133 134 135 136
        break;
      }
    }

wafwerar's avatar
wafwerar 已提交
137
    taosThreadMutexUnlock(&(vnodeMgr.mutex));
H
Hongze Cheng 已提交
138 139

    (*(pTask->execute))(pTask->arg);
H
Hongze Cheng 已提交
140
    free(pTask);
H
Hongze Cheng 已提交
141 142 143 144
  }

  return NULL;
}