“7f3d4ee108c184ab215036051087aaaaa8de7661”上不存在“include/git@gitcode.net:openeuler/kernel.git”
dmWorker.c 5.6 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
shm  
Shengliang Guan 已提交
22

S
Shengliang Guan 已提交
23
  setThreadName("dnode-status");
S
shm  
Shengliang Guan 已提交
24

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

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

  return NULL;
}

static void *dmMonitorThreadFp(void *param) {
S
Shengliang Guan 已提交
41 42
  SDnodeMgmt *pMgmt = param;
  int64_t     lastTime = taosGetTimestampMs();
S
Shengliang Guan 已提交
43 44 45 46 47

  setThreadName("dnode-monitor");

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

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

  return NULL;
}

S
Shengliang Guan 已提交
61
int32_t dmStartStatusThread(SDnodeMgmt *pMgmt) {
S
Shengliang Guan 已提交
62 63 64 65 66
  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 已提交
67 68 69
    return -1;
  }

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

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

S
Shengliang Guan 已提交
81
int32_t dmStartMonitorThread(SDnodeMgmt *pMgmt) {
S
Shengliang Guan 已提交
82 83 84 85 86
  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 已提交
87 88 89
    return -1;
  }

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

S
Shengliang Guan 已提交
95
void dmStopMonitorThread(SDnodeMgmt *pMgmt) {
S
Shengliang Guan 已提交
96 97
  if (taosCheckPthreadValid(pMgmt->monitorThread)) {
    taosThreadJoin(pMgmt->monitorThread, NULL);
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;
S
Shengliang Guan 已提交
104
  tmsg_t      msgType = pMsg->msgType;
105 106
  bool        isRequest = msgType & 1u;
  dTrace("msg:%p, will be processed in dnode-mgmt queue, type:%s", pMsg, TMSG_INFO(msgType));
S
shm  
Shengliang Guan 已提交
107

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

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

S
shm  
Shengliang Guan 已提交
163
  dTrace("msg:%p, is freed, result:0x%04x:%s", pMsg, code & 0XFFFF, tstrerror(code));
S
Shengliang Guan 已提交
164
  rpcFreeCont(pMsg->pCont);
S
shm  
Shengliang Guan 已提交
165
  taosFreeQitem(pMsg);
S
shm  
Shengliang Guan 已提交
166 167
}

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

S
Shengliang Guan 已提交
181
  dDebug("dnode workers are initialized");
S
shm  
Shengliang Guan 已提交
182 183 184
  return 0;
}

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

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