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 34
  if (pOption->nthreads > 0) {
    vnodeMgr.nthreads = pOption->nthreads;
    vnodeMgr.threads = (pthread_t*)calloc(pOption->nthreads, sizeof(pthread_t));
H
Hongze Cheng 已提交
35 36 37 38 39 40
    if (vnodeMgr.threads == NULL) {
      return -1;
    }

    pthread_mutex_init(&(vnodeMgr.mutex), NULL);
    pthread_cond_init(&(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++) {
H
Hongze Cheng 已提交
44
      pthread_create(&(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 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    return;
  }

  // Stop commit handler
  pthread_mutex_lock(&(vnodeMgr.mutex));
  vnodeMgr.stop = true;
  pthread_cond_broadcast(&(vnodeMgr.hasTask));
  pthread_mutex_unlock(&(vnodeMgr.mutex));

  for (uint16_t i = 0; i < vnodeMgr.nthreads; i++) {
    pthread_join(vnodeMgr.threads[i], NULL);
  }

  tfree(vnodeMgr.threads);
  pthread_cond_destroy(&(vnodeMgr.hasTask));
  pthread_mutex_destroy(&(vnodeMgr.mutex));
}

H
Hongze Cheng 已提交
80 81 82
int vnodeScheduleTask(SVnodeTask* pTask) {
  pthread_mutex_lock(&(vnodeMgr.mutex));

H
Hongze Cheng 已提交
83
  TD_DLIST_APPEND(&(vnodeMgr.queue), pTask);
H
Hongze Cheng 已提交
84 85 86 87 88 89 90 91

  pthread_cond_signal(&(vnodeMgr.hasTask));

  pthread_mutex_unlock(&(vnodeMgr.mutex));

  return 0;
}

S
Shengliang Guan 已提交
92
int32_t vnodePutReqToVQueryQ(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->pMgmt, pReq);
S
Shengliang Guan 已提交
98 99
}

S
Shengliang 已提交
100
void vnodeSendReqToDnode(SVnode* pVnode, struct SEpSet* epSet, struct SRpcMsg* pReq) {
S
shm  
Shengliang Guan 已提交
101
  (*vnodeMgr.sendReqFp)(pVnode->pMgmt, 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 110 111 112 113 114 115 116 117 118 119 120
  SVnodeTask* pTask;
  for (;;) {
    pthread_mutex_lock(&(vnodeMgr.mutex));
    for (;;) {
      pTask = TD_DLIST_HEAD(&(vnodeMgr.queue));
      if (pTask == NULL) {
        if (vnodeMgr.stop) {
          pthread_mutex_unlock(&(vnodeMgr.mutex));
          return NULL;
        } else {
          pthread_cond_wait(&(vnodeMgr.hasTask), &(vnodeMgr.mutex));
        }
      } else {
H
Hongze Cheng 已提交
121
        TD_DLIST_POP(&(vnodeMgr.queue), pTask);
H
Hongze Cheng 已提交
122 123 124 125 126 127 128
        break;
      }
    }

    pthread_mutex_unlock(&(vnodeMgr.mutex));

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

  return NULL;
}