vnodeMgr.c 3.4 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
Shengliang Guan 已提交
28
  vnodeMgr.putReqToVQueryQFp = pOption->putReqToVQueryQFp;
H
Hongze Cheng 已提交
29

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

    pthread_mutex_init(&(vnodeMgr.mutex), NULL);
    pthread_cond_init(&(vnodeMgr.hasTask), NULL);
H
Hongze Cheng 已提交
40
    TD_DLIST_INIT(&(vnodeMgr.queue));
H
Hongze Cheng 已提交
41

S
Shengliang Guan 已提交
42
    for (uint16_t i = 0; i < pOption->nthreads; i++) {
H
Hongze Cheng 已提交
43
      pthread_create(&(vnodeMgr.threads[i]), NULL, loop, NULL);
H
Hongze Cheng 已提交
44
      // pthread_setname_np(vnodeMgr.threads[i], "VND Commit Thread");
H
Hongze Cheng 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58
    }
  } 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 已提交
59
void vnodeCleanup() {
H
Hongze Cheng 已提交
60
  if (TD_CHECK_AND_SET_MOD_CLEAR(&(vnodeMgr.vnodeInitFlag)) == TD_MOD_UNINITIALIZED) {
H
Hongze Cheng 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    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 已提交
79 80 81
int vnodeScheduleTask(SVnodeTask* pTask) {
  pthread_mutex_lock(&(vnodeMgr.mutex));

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

  pthread_cond_signal(&(vnodeMgr.hasTask));

  pthread_mutex_unlock(&(vnodeMgr.mutex));

  return 0;
}

S
Shengliang Guan 已提交
91 92 93 94 95 96
int32_t vnodePutReqToVQueryQ(SVnode* pVnode, struct SRpcMsg* pReq) {
  if (pVnode == NULL || pVnode->pDnode == NULL || vnodeMgr.putReqToVQueryQFp == NULL) {
    terrno = TSDB_CODE_VND_APP_ERROR;
    return -1;
  }
  return (*vnodeMgr.putReqToVQueryQFp)(pVnode->pDnode, pReq);
S
Shengliang Guan 已提交
97 98
}

H
Hongze Cheng 已提交
99
/* ------------------------ STATIC METHODS ------------------------ */
H
Hongze Cheng 已提交
100
static void* loop(void* arg) {
H
Haojun Liao 已提交
101 102
  setThreadName("vnode-commit");

H
Hongze Cheng 已提交
103 104 105 106 107 108 109 110 111 112 113 114 115
  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 已提交
116
        TD_DLIST_POP(&(vnodeMgr.queue), pTask);
H
Hongze Cheng 已提交
117 118 119 120 121 122 123
        break;
      }
    }

    pthread_mutex_unlock(&(vnodeMgr.mutex));

    (*(pTask->execute))(pTask->arg);
H
Hongze Cheng 已提交
124
    free(pTask);
H
Hongze Cheng 已提交
125 126 127 128
  }

  return NULL;
}