vnodeModule.c 4.2 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 "vnd.h"
H
Hongze Cheng 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

typedef struct SVnodeTask SVnodeTask;
struct SVnodeTask {
  SVnodeTask* next;
  SVnodeTask* prev;
  int (*execute)(void*);
  void* arg;
};

struct SVnodeGlobal {
  int8_t        init;
  int8_t        stop;
  int           nthreads;
  TdThread*     threads;
  TdThreadMutex mutex;
  TdThreadCond  hasTask;
  SVnodeTask    queue;
};

struct SVnodeGlobal vnodeGlobal;

static void* loop(void* arg);

H
Hongze Cheng 已提交
40 41 42 43 44 45
static sem_t canCommit = {0};

static void vnodeInitCommit() { tsem_init(&canCommit, 0, 4); };
void        vnode_wait_commit() { tsem_wait(&canCommit); }
void        vnode_done_commit() { tsem_wait(&canCommit); }

H
Hongze Cheng 已提交
46 47 48 49 50 51 52 53 54
int vnodeInit(int nthreads) {
  int8_t init;
  int    ret;

  init = atomic_val_compare_exchange_8(&(vnodeGlobal.init), 0, 1);
  if (init) {
    return 0;
  }

H
Hongze Cheng 已提交
55 56
  taosThreadMutexInit(&vnodeGlobal.mutex, NULL);
  taosThreadCondInit(&vnodeGlobal.hasTask, NULL);
H
Hongze Cheng 已提交
57

H
Hongze Cheng 已提交
58 59 60
  taosThreadMutexLock(&vnodeGlobal.mutex);

  vnodeGlobal.stop = 0;
H
Hongze Cheng 已提交
61 62 63
  vnodeGlobal.queue.next = &vnodeGlobal.queue;
  vnodeGlobal.queue.prev = &vnodeGlobal.queue;

H
Hongze Cheng 已提交
64 65
  taosThreadMutexUnlock(&(vnodeGlobal.mutex));

H
Hongze Cheng 已提交
66 67 68 69
  vnodeGlobal.nthreads = nthreads;
  vnodeGlobal.threads = taosMemoryCalloc(nthreads, sizeof(TdThread));
  if (vnodeGlobal.threads == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
S
Shengliang Guan 已提交
70
    vError("failed to init vnode module since:%s", tstrerror(terrno));
H
Hongze Cheng 已提交
71 72 73 74 75 76 77 78 79 80
    return -1;
  }

  for (int i = 0; i < nthreads; i++) {
    taosThreadCreate(&(vnodeGlobal.threads[i]), NULL, loop, NULL);
  }

  if (walInit() < 0) {
    return -1;
  }
L
Liu Jicong 已提交
81 82 83
  if (tqInit() < 0) {
    return -1;
  }
H
Hongze Cheng 已提交
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108

  return 0;
}

void vnodeCleanup() {
  int8_t init;

  init = atomic_val_compare_exchange_8(&(vnodeGlobal.init), 1, 0);
  if (init == 0) return;

  // set stop
  taosThreadMutexLock(&(vnodeGlobal.mutex));
  vnodeGlobal.stop = 1;
  taosThreadCondBroadcast(&(vnodeGlobal.hasTask));
  taosThreadMutexUnlock(&(vnodeGlobal.mutex));

  // wait for threads
  for (int i = 0; i < vnodeGlobal.nthreads; i++) {
    taosThreadJoin(vnodeGlobal.threads[i], NULL);
  }

  // clear source
  taosMemoryFreeClear(vnodeGlobal.threads);
  taosThreadCondDestroy(&(vnodeGlobal.hasTask));
  taosThreadMutexDestroy(&(vnodeGlobal.mutex));
L
Liu Jicong 已提交
109 110 111

  walCleanUp();
  tqCleanUp();
C
Cary Xu 已提交
112
  smaCleanUp();
H
Hongze Cheng 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
}

int vnodeScheduleTask(int (*execute)(void*), void* arg) {
  SVnodeTask* pTask;

  ASSERT(!vnodeGlobal.stop);

  pTask = taosMemoryMalloc(sizeof(*pTask));
  if (pTask == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  pTask->execute = execute;
  pTask->arg = arg;

  taosThreadMutexLock(&(vnodeGlobal.mutex));
  pTask->next = &vnodeGlobal.queue;
  pTask->prev = vnodeGlobal.queue.prev;
  vnodeGlobal.queue.prev->next = pTask;
  vnodeGlobal.queue.prev = pTask;
  taosThreadCondSignal(&(vnodeGlobal.hasTask));
  taosThreadMutexUnlock(&(vnodeGlobal.mutex));

  return 0;
}

/* ------------------------ STATIC METHODS ------------------------ */
static void* loop(void* arg) {
  SVnodeTask* pTask;
  int         ret;

  setThreadName("vnode-commit");

  for (;;) {
    taosThreadMutexLock(&(vnodeGlobal.mutex));
    for (;;) {
      pTask = vnodeGlobal.queue.next;
      if (pTask == &vnodeGlobal.queue) {
        // no task
        if (vnodeGlobal.stop) {
          taosThreadMutexUnlock(&(vnodeGlobal.mutex));
          return NULL;
        } else {
          taosThreadCondWait(&(vnodeGlobal.hasTask), &(vnodeGlobal.mutex));
        }
      } else {
        // has task
        pTask->prev->next = pTask->next;
        pTask->next->prev = pTask->prev;
        break;
      }
    }

    taosThreadMutexUnlock(&(vnodeGlobal.mutex));

    pTask->execute(pTask->arg);
    taosMemoryFree(pTask);
  }

  return NULL;
L
Liu Jicong 已提交
174
}