“068bfbb817611c856acd8c535de2b33a6126786c”上不存在“paddle/legacy/api/SequenceGenerator.cpp”
dmWorker.c 5.4 KB
Newer Older
S
shm  
Shengliang Guan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * 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/>.
 */

#define _DEFAULT_SOURCE
S
Shengliang Guan 已提交
17
#include "dmInt.h"
S
shm  
Shengliang Guan 已提交
18

S
Shengliang Guan 已提交
19
static void *dmStatusThreadFp(void *param) {
S
Shengliang Guan 已提交
20 21
  SDnodeMgmt *pMgmt = param;
  int64_t     lastTime = taosGetTimestampMs();
S
Shengliang Guan 已提交
22
  setThreadName("dnode-status");
S
shm  
Shengliang Guan 已提交
23

S
Shengliang Guan 已提交
24
  while (1) {
S
shm  
Shengliang Guan 已提交
25
    taosMsleep(200);
S
Shengliang Guan 已提交
26
    if (pMgmt->pData->dropped || pMgmt->pData->stopped) break;
S
shm  
Shengliang Guan 已提交
27 28

    int64_t curTime = taosGetTimestampMs();
S
Shengliang Guan 已提交
29 30
    float   interval = (curTime - lastTime) / 1000.0f;
    if (interval >= tsStatusInterval) {
S
Shengliang Guan 已提交
31
      dmSendStatusReq(pMgmt);
S
Shengliang Guan 已提交
32 33 34 35 36 37 38 39
      lastTime = curTime;
    }
  }

  return NULL;
}

static void *dmMonitorThreadFp(void *param) {
S
Shengliang Guan 已提交
40 41
  SDnodeMgmt *pMgmt = param;
  int64_t     lastTime = taosGetTimestampMs();
S
Shengliang Guan 已提交
42 43 44 45
  setThreadName("dnode-monitor");

  while (1) {
    taosMsleep(200);
S
Shengliang Guan 已提交
46
    if (pMgmt->pData->dropped || pMgmt->pData->stopped) break;
S
shm  
Shengliang Guan 已提交
47

S
Shengliang Guan 已提交
48 49 50
    int64_t curTime = taosGetTimestampMs();
    float   interval = (curTime - lastTime) / 1000.0f;
    if (interval >= tsMonitorInterval) {
S
Shengliang Guan 已提交
51
      (*pMgmt->sendMonitorReportFp)();
S
Shengliang Guan 已提交
52
      lastTime = curTime;
S
shm  
Shengliang Guan 已提交
53
    }
S
shm  
Shengliang Guan 已提交
54
  }
S
Shengliang Guan 已提交
55 56 57 58

  return NULL;
}

S
Shengliang Guan 已提交
59
int32_t dmStartStatusThread(SDnodeMgmt *pMgmt) {
S
Shengliang Guan 已提交
60 61 62 63 64
  TdThreadAttr thAttr;
  taosThreadAttrInit(&thAttr);
  taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
  if (taosThreadCreate(&pMgmt->statusThread, &thAttr, dmStatusThreadFp, pMgmt) != 0) {
    dError("failed to create status thread since %s", strerror(errno));
S
Shengliang Guan 已提交
65 66 67
    return -1;
  }

S
Shengliang Guan 已提交
68
  taosThreadAttrDestroy(&thAttr);
S
Shengliang Guan 已提交
69
  tmsgReportStartup("dnode-status", "initialized");
S
Shengliang Guan 已提交
70 71 72
  return 0;
}

S
Shengliang Guan 已提交
73
void dmStopStatusThread(SDnodeMgmt *pMgmt) {
S
Shengliang Guan 已提交
74 75
  if (taosCheckPthreadValid(pMgmt->statusThread)) {
    taosThreadJoin(pMgmt->statusThread, NULL);
76
    taosThreadClear(&pMgmt->statusThread);
S
Shengliang Guan 已提交
77
  }
S
shm  
Shengliang Guan 已提交
78 79
}

S
Shengliang Guan 已提交
80
int32_t dmStartMonitorThread(SDnodeMgmt *pMgmt) {
S
Shengliang Guan 已提交
81 82 83 84 85
  TdThreadAttr thAttr;
  taosThreadAttrInit(&thAttr);
  taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
  if (taosThreadCreate(&pMgmt->monitorThread, &thAttr, dmMonitorThreadFp, pMgmt) != 0) {
    dError("failed to create monitor thread since %s", strerror(errno));
S
Shengliang Guan 已提交
86 87 88
    return -1;
  }

S
Shengliang Guan 已提交
89
  taosThreadAttrDestroy(&thAttr);
S
Shengliang Guan 已提交
90
  tmsgReportStartup("dnode-monitor", "initialized");
S
Shengliang Guan 已提交
91 92 93
  return 0;
}

S
Shengliang Guan 已提交
94
void dmStopMonitorThread(SDnodeMgmt *pMgmt) {
S
Shengliang Guan 已提交
95 96
  if (taosCheckPthreadValid(pMgmt->monitorThread)) {
    taosThreadJoin(pMgmt->monitorThread, NULL);
97
    taosThreadClear(&pMgmt->monitorThread);
S
Shengliang Guan 已提交
98 99
  }
}
S
Shengliang Guan 已提交
100

S
Shengliang Guan 已提交
101
static void dmProcessMgmtQueue(SQueueInfo *pInfo, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
102
  SDnodeMgmt *pMgmt = pInfo->ahandle;
103
  int32_t     code = -1;
H
Hongze Cheng 已提交
104
  STraceId   *trace = &pMsg->info.traceId;
dengyihao's avatar
dengyihao 已提交
105
  dGTrace("msg:%p, will be processed in dnode queue, type:%s", pMsg, TMSG_INFO(pMsg->msgType));
S
shm  
Shengliang Guan 已提交
106

S
Shengliang Guan 已提交
107
  switch (pMsg->msgType) {
S
shm  
Shengliang Guan 已提交
108
    case TDMT_DND_CONFIG_DNODE:
S
Shengliang Guan 已提交
109
      code = dmProcessConfigReq(pMgmt, pMsg);
S
shm  
Shengliang Guan 已提交
110 111
      break;
    case TDMT_MND_AUTH_RSP:
S
Shengliang Guan 已提交
112
      code = dmProcessAuthRsp(pMgmt, pMsg);
S
shm  
Shengliang Guan 已提交
113 114
      break;
    case TDMT_MND_GRANT_RSP:
S
Shengliang Guan 已提交
115
      code = dmProcessGrantRsp(pMgmt, pMsg);
S
Shengliang Guan 已提交
116 117
      break;
    case TDMT_DND_CREATE_MNODE:
118
      code = (*pMgmt->processCreateNodeFp)(MNODE, pMsg);
S
Shengliang Guan 已提交
119 120
      break;
    case TDMT_DND_DROP_MNODE:
121
      code = (*pMgmt->processDropNodeFp)(MNODE, pMsg);
S
Shengliang Guan 已提交
122 123
      break;
    case TDMT_DND_CREATE_QNODE:
124
      code = (*pMgmt->processCreateNodeFp)(QNODE, pMsg);
S
Shengliang Guan 已提交
125 126
      break;
    case TDMT_DND_DROP_QNODE:
127
      code = (*pMgmt->processDropNodeFp)(QNODE, pMsg);
S
Shengliang Guan 已提交
128 129
      break;
    case TDMT_DND_CREATE_SNODE:
130
      code = (*pMgmt->processCreateNodeFp)(SNODE, pMsg);
S
Shengliang Guan 已提交
131 132
      break;
    case TDMT_DND_DROP_SNODE:
133
      code = (*pMgmt->processDropNodeFp)(SNODE, pMsg);
S
Shengliang Guan 已提交
134
      break;
135 136 137
    case TDMT_DND_SERVER_STATUS:
      code = dmProcessServerRunStatus(pMgmt, pMsg);
      break;
D
dapan1121 已提交
138 139 140
    case TDMT_DND_SYSTABLE_RETRIEVE:
      code = dmProcessRetrieve(pMgmt, pMsg);
      break;
C
Cary Xu 已提交
141 142 143
    case TDMT_MND_GRANT:
      code = dmProcessGrantReq(pMsg);
      break;
S
shm  
Shengliang Guan 已提交
144
    default:
145
      terrno = TSDB_CODE_MSG_NOT_PROCESSED;
S
Shengliang Guan 已提交
146
      break;
S
shm  
Shengliang Guan 已提交
147
  }
S
shm  
Shengliang Guan 已提交
148

S
Shengliang Guan 已提交
149
  if (IsReq(pMsg)) {
S
Shengliang Guan 已提交
150 151 152
    if (code != 0 && terrno != 0) code = terrno;
    SRpcMsg rsp = {
        .code = code,
S
Shengliang Guan 已提交
153 154
        .pCont = pMsg->info.rsp,
        .contLen = pMsg->info.rspLen,
155
        .info = pMsg->info,
S
Shengliang Guan 已提交
156
    };
S
shm  
Shengliang Guan 已提交
157 158 159
    rpcSendResponse(&rsp);
  }

S
Shengliang Guan 已提交
160
  dTrace("msg:%p, is freed, code:0x%x", pMsg, code);
S
Shengliang Guan 已提交
161
  rpcFreeCont(pMsg->pCont);
S
shm  
Shengliang Guan 已提交
162
  taosFreeQitem(pMsg);
S
shm  
Shengliang Guan 已提交
163 164
}

S
Shengliang Guan 已提交
165
int32_t dmStartWorker(SDnodeMgmt *pMgmt) {
S
Shengliang Guan 已提交
166 167 168 169 170
  SSingleWorkerCfg cfg = {
      .min = 1,
      .max = 1,
      .name = "dnode-mgmt",
      .fp = (FItem)dmProcessMgmtQueue,
S
Shengliang Guan 已提交
171
      .param = pMgmt,
S
Shengliang Guan 已提交
172
  };
S
Shengliang Guan 已提交
173
  if (tSingleWorkerInit(&pMgmt->mgmtWorker, &cfg) != 0) {
S
Shengliang Guan 已提交
174
    dError("failed to start dnode-mgmt worker since %s", terrstr());
S
shm  
Shengliang Guan 已提交
175 176 177
    return -1;
  }

S
Shengliang Guan 已提交
178
  dDebug("dnode workers are initialized");
S
shm  
Shengliang Guan 已提交
179 180 181
  return 0;
}

S
Shengliang Guan 已提交
182 183
void dmStopWorker(SDnodeMgmt *pMgmt) {
  tSingleWorkerCleanup(&pMgmt->mgmtWorker);
S
Shengliang Guan 已提交
184
  dDebug("dnode workers are closed");
S
shm  
Shengliang Guan 已提交
185
}
S
shm  
Shengliang Guan 已提交
186

S
Shengliang Guan 已提交
187
int32_t dmPutNodeMsgToMgmtQueue(SDnodeMgmt *pMgmt, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
188
  SSingleWorker *pWorker = &pMgmt->mgmtWorker;
S
Shengliang Guan 已提交
189
  dTrace("msg:%p, put into worker %s", pMsg, pWorker->name);
S
Shengliang Guan 已提交
190 191
  taosWriteQitem(pWorker->queue, pMsg);
  return 0;
S
shm  
Shengliang Guan 已提交
192
}