dmWorker.c 5.5 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);
78
    taosThreadClear(&pMgmt->statusThread);
S
Shengliang Guan 已提交
79
  }
S
shm  
Shengliang Guan 已提交
80 81
}

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

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

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

S
Shengliang Guan 已提交
103
static void dmProcessMgmtQueue(SQueueInfo *pInfo, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
104
  SDnodeMgmt *pMgmt = pInfo->ahandle;
105
  int32_t     code = -1;
S
Shengliang Guan 已提交
106
  tmsg_t      msgType = pMsg->msgType;
107 108
  bool        isRequest = msgType & 1u;
  dTrace("msg:%p, will be processed in dnode-mgmt queue, type:%s", pMsg, TMSG_INFO(msgType));
S
shm  
Shengliang Guan 已提交
109

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

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

S
Shengliang Guan 已提交
163
  dTrace("msg:%p, is freed, code:0x%x", pMsg, 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
}