dmWorker.c 8.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"
D
dapan1121 已提交
18
#include "thttp.h"
S
shm  
Shengliang Guan 已提交
19

S
Shengliang Guan 已提交
20
static void *dmStatusThreadFp(void *param) {
S
Shengliang Guan 已提交
21 22
  SDnodeMgmt *pMgmt = param;
  int64_t     lastTime = taosGetTimestampMs();
S
Shengliang Guan 已提交
23
  setThreadName("dnode-status");
dengyihao's avatar
dengyihao 已提交
24 25 26

  const static int16_t TRIM_FREQ = 30;
  int32_t              trimCount = 0;
K
kailixu 已提交
27 28 29 30
  int32_t              upTimeCount = 0;
  int64_t              upTime = 0;
  int64_t              thrdTime = 0;

S
Shengliang Guan 已提交
31
  while (1) {
S
shm  
Shengliang Guan 已提交
32
    taosMsleep(200);
S
Shengliang Guan 已提交
33
    if (pMgmt->pData->dropped || pMgmt->pData->stopped) break;
S
shm  
Shengliang Guan 已提交
34 35

    int64_t curTime = taosGetTimestampMs();
K
kailixu 已提交
36
    int64_t cost = 0;
S
Shengliang Guan 已提交
37 38
    float   interval = (curTime - lastTime) / 1000.0f;
    if (interval >= tsStatusInterval) {
S
Shengliang Guan 已提交
39
      dmSendStatusReq(pMgmt);
S
Shengliang Guan 已提交
40
      lastTime = curTime;
dengyihao's avatar
dengyihao 已提交
41 42 43 44 45

      trimCount = (trimCount + 1) % TRIM_FREQ;
      if (trimCount == 0) {
        taosMemoryTrim(0);
      }
K
kailixu 已提交
46 47 48 49

      if ((upTimeCount = (++upTimeCount & 7)) == 0) {  
        upTime = (taosGetOsUptime() - tsDndStartOsUptime) * 1000;
      }
S
Shengliang Guan 已提交
50
    }
K
kailixu 已提交
51 52 53 54 55 56 57

    thrdTime += 200;
    cost = taosGetTimestampMs() - curTime;
    if (cost > 0) thrdTime += cost;
    tsDndUpTime = upTime > thrdTime ? upTime : thrdTime;
    printf("upTime:%" PRIi64 " thrdTime:%" PRIi64 " tsDndUpTime:%" PRIi64 " delta:%" PRIi64 "\n", upTime, thrdTime,
           tsDndUpTime, upTime - thrdTime);
S
Shengliang Guan 已提交
58 59 60 61 62 63
  }

  return NULL;
}

static void *dmMonitorThreadFp(void *param) {
S
Shengliang Guan 已提交
64 65
  SDnodeMgmt *pMgmt = param;
  int64_t     lastTime = taosGetTimestampMs();
S
Shengliang Guan 已提交
66 67 68 69
  setThreadName("dnode-monitor");

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

S
Shengliang Guan 已提交
72 73 74
    int64_t curTime = taosGetTimestampMs();
    float   interval = (curTime - lastTime) / 1000.0f;
    if (interval >= tsMonitorInterval) {
S
Shengliang Guan 已提交
75
      (*pMgmt->sendMonitorReportFp)();
S
Shengliang Guan 已提交
76
      lastTime = curTime;
S
shm  
Shengliang Guan 已提交
77
    }
S
shm  
Shengliang Guan 已提交
78
  }
S
Shengliang Guan 已提交
79 80 81 82

  return NULL;
}

D
dapan1121 已提交
83 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 109
static void *dmCrashReportThreadFp(void *param) {
  SDnodeMgmt *pMgmt = param;
  int64_t     lastTime = taosGetTimestampMs();
  setThreadName("dnode-crashReport");
  char filepath[PATH_MAX] = {0};
  snprintf(filepath, sizeof(filepath), "%s%s.taosdCrashLog", tsLogDir, TD_DIRSEP);
  char *pMsg = NULL;
  int64_t msgLen = 0;
  TdFilePtr pFile = NULL;
  bool truncateFile = false;
  int32_t sleepTime = 200;
  int32_t reportPeriodNum = 3600 * 1000 / sleepTime;;
  int32_t loopTimes = reportPeriodNum;
  
  while (1) {
    if (pMgmt->pData->dropped || pMgmt->pData->stopped) break;
    if (loopTimes++ < reportPeriodNum) {
      taosMsleep(sleepTime);
      continue;
    }

    taosReadCrashInfo(filepath, &pMsg, &msgLen, &pFile);
    if (pMsg && msgLen > 0) {
      if (taosSendHttpReport(tsTelemServer, tsSvrCrashReportUri, tsTelemPort, pMsg, msgLen, HTTP_FLAT) != 0) {
        dError("failed to send crash report");
        if (pFile) {
          taosReleaseCrashLogFile(pFile, false);
D
dapan1121 已提交
110
          pFile = NULL;
D
dapan1121 已提交
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
          continue;
        }
      } else {
        dInfo("succeed to send crash report");
        truncateFile = true;
      }
    } else {
      dDebug("no crash info");
    }

    taosMemoryFree(pMsg);

    if (pMsg && msgLen > 0) {
      pMsg = NULL;
      continue;
    }
    
    if (pFile) {
      taosReleaseCrashLogFile(pFile, truncateFile);
D
dapan1121 已提交
130
      pFile = NULL;
D
dapan1121 已提交
131 132 133 134 135 136 137 138 139 140 141
      truncateFile = false;
    }
    
    taosMsleep(sleepTime);
    loopTimes = 0;
  }

  return NULL;
}


S
Shengliang Guan 已提交
142
int32_t dmStartStatusThread(SDnodeMgmt *pMgmt) {
S
Shengliang Guan 已提交
143 144 145 146 147
  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 已提交
148 149 150
    return -1;
  }

S
Shengliang Guan 已提交
151
  taosThreadAttrDestroy(&thAttr);
S
Shengliang Guan 已提交
152
  tmsgReportStartup("dnode-status", "initialized");
S
Shengliang Guan 已提交
153 154 155
  return 0;
}

S
Shengliang Guan 已提交
156
void dmStopStatusThread(SDnodeMgmt *pMgmt) {
S
Shengliang Guan 已提交
157 158
  if (taosCheckPthreadValid(pMgmt->statusThread)) {
    taosThreadJoin(pMgmt->statusThread, NULL);
159
    taosThreadClear(&pMgmt->statusThread);
S
Shengliang Guan 已提交
160
  }
S
shm  
Shengliang Guan 已提交
161 162
}

S
Shengliang Guan 已提交
163
int32_t dmStartMonitorThread(SDnodeMgmt *pMgmt) {
S
Shengliang Guan 已提交
164 165 166 167 168
  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 已提交
169 170 171
    return -1;
  }

S
Shengliang Guan 已提交
172
  taosThreadAttrDestroy(&thAttr);
S
Shengliang Guan 已提交
173
  tmsgReportStartup("dnode-monitor", "initialized");
S
Shengliang Guan 已提交
174 175 176
  return 0;
}

S
Shengliang Guan 已提交
177
void dmStopMonitorThread(SDnodeMgmt *pMgmt) {
S
Shengliang Guan 已提交
178 179
  if (taosCheckPthreadValid(pMgmt->monitorThread)) {
    taosThreadJoin(pMgmt->monitorThread, NULL);
180
    taosThreadClear(&pMgmt->monitorThread);
S
Shengliang Guan 已提交
181 182
  }
}
S
Shengliang Guan 已提交
183

D
dapan1121 已提交
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
int32_t dmStartCrashReportThread(SDnodeMgmt *pMgmt) {
  if (!tsEnableCrashReport) {
    return 0;
  }

  TdThreadAttr thAttr;
  taosThreadAttrInit(&thAttr);
  taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
  if (taosThreadCreate(&pMgmt->crashReportThread, &thAttr, dmCrashReportThreadFp, pMgmt) != 0) {
    dError("failed to create crashReport thread since %s", strerror(errno));
    return -1;
  }

  taosThreadAttrDestroy(&thAttr);
  tmsgReportStartup("dnode-crashReport", "initialized");
  return 0;
}

void dmStopCrashReportThread(SDnodeMgmt *pMgmt) {
  if (!tsEnableCrashReport) {
    return;
  }

  if (taosCheckPthreadValid(pMgmt->crashReportThread)) {
    taosThreadJoin(pMgmt->crashReportThread, NULL);
    taosThreadClear(&pMgmt->crashReportThread);
  }
}


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

S
Shengliang Guan 已提交
220
  switch (pMsg->msgType) {
S
shm  
Shengliang Guan 已提交
221
    case TDMT_DND_CONFIG_DNODE:
S
Shengliang Guan 已提交
222
      code = dmProcessConfigReq(pMgmt, pMsg);
S
shm  
Shengliang Guan 已提交
223 224
      break;
    case TDMT_MND_AUTH_RSP:
S
Shengliang Guan 已提交
225
      code = dmProcessAuthRsp(pMgmt, pMsg);
S
shm  
Shengliang Guan 已提交
226 227
      break;
    case TDMT_MND_GRANT_RSP:
S
Shengliang Guan 已提交
228
      code = dmProcessGrantRsp(pMgmt, pMsg);
S
Shengliang Guan 已提交
229 230
      break;
    case TDMT_DND_CREATE_MNODE:
231
      code = (*pMgmt->processCreateNodeFp)(MNODE, pMsg);
S
Shengliang Guan 已提交
232 233
      break;
    case TDMT_DND_DROP_MNODE:
234
      code = (*pMgmt->processDropNodeFp)(MNODE, pMsg);
S
Shengliang Guan 已提交
235 236
      break;
    case TDMT_DND_CREATE_QNODE:
237
      code = (*pMgmt->processCreateNodeFp)(QNODE, pMsg);
S
Shengliang Guan 已提交
238 239
      break;
    case TDMT_DND_DROP_QNODE:
240
      code = (*pMgmt->processDropNodeFp)(QNODE, pMsg);
S
Shengliang Guan 已提交
241 242
      break;
    case TDMT_DND_CREATE_SNODE:
243
      code = (*pMgmt->processCreateNodeFp)(SNODE, pMsg);
S
Shengliang Guan 已提交
244 245
      break;
    case TDMT_DND_DROP_SNODE:
246
      code = (*pMgmt->processDropNodeFp)(SNODE, pMsg);
S
Shengliang Guan 已提交
247
      break;
C
cadem 已提交
248 249 250
    case TDMT_DND_ALTER_MNODE_TYPE:
      code = (*pMgmt->processAlterNodeTypeFp)(MNODE, pMsg);
      break;
251 252 253
    case TDMT_DND_SERVER_STATUS:
      code = dmProcessServerRunStatus(pMgmt, pMsg);
      break;
D
dapan1121 已提交
254 255 256
    case TDMT_DND_SYSTABLE_RETRIEVE:
      code = dmProcessRetrieve(pMgmt, pMsg);
      break;
C
Cary Xu 已提交
257
    case TDMT_MND_GRANT:
K
kailixu 已提交
258
      code = dmProcessGrantReq(&pMgmt->pData->clusterId, pMsg);
C
Cary Xu 已提交
259
      break;
S
shm  
Shengliang Guan 已提交
260
    default:
261
      terrno = TSDB_CODE_MSG_NOT_PROCESSED;
262
      dGError("msg:%p, not processed in mgmt queue", pMsg);
S
Shengliang Guan 已提交
263
      break;
S
shm  
Shengliang Guan 已提交
264
  }
S
shm  
Shengliang Guan 已提交
265

S
Shengliang Guan 已提交
266
  if (IsReq(pMsg)) {
S
Shengliang Guan 已提交
267 268 269
    if (code != 0 && terrno != 0) code = terrno;
    SRpcMsg rsp = {
        .code = code,
S
Shengliang Guan 已提交
270 271
        .pCont = pMsg->info.rsp,
        .contLen = pMsg->info.rspLen,
272
        .info = pMsg->info,
S
Shengliang Guan 已提交
273
    };
S
shm  
Shengliang Guan 已提交
274 275 276
    rpcSendResponse(&rsp);
  }

S
Shengliang Guan 已提交
277
  dTrace("msg:%p, is freed, code:0x%x", pMsg, code);
S
Shengliang Guan 已提交
278
  rpcFreeCont(pMsg->pCont);
S
shm  
Shengliang Guan 已提交
279
  taosFreeQitem(pMsg);
S
shm  
Shengliang Guan 已提交
280 281
}

S
Shengliang Guan 已提交
282
int32_t dmStartWorker(SDnodeMgmt *pMgmt) {
S
Shengliang Guan 已提交
283 284 285 286 287
  SSingleWorkerCfg cfg = {
      .min = 1,
      .max = 1,
      .name = "dnode-mgmt",
      .fp = (FItem)dmProcessMgmtQueue,
S
Shengliang Guan 已提交
288
      .param = pMgmt,
S
Shengliang Guan 已提交
289
  };
S
Shengliang Guan 已提交
290
  if (tSingleWorkerInit(&pMgmt->mgmtWorker, &cfg) != 0) {
S
Shengliang Guan 已提交
291
    dError("failed to start dnode-mgmt worker since %s", terrstr());
S
shm  
Shengliang Guan 已提交
292 293 294
    return -1;
  }

S
Shengliang Guan 已提交
295
  dDebug("dnode workers are initialized");
S
shm  
Shengliang Guan 已提交
296 297 298
  return 0;
}

S
Shengliang Guan 已提交
299 300
void dmStopWorker(SDnodeMgmt *pMgmt) {
  tSingleWorkerCleanup(&pMgmt->mgmtWorker);
S
Shengliang Guan 已提交
301
  dDebug("dnode workers are closed");
S
shm  
Shengliang Guan 已提交
302
}
S
shm  
Shengliang Guan 已提交
303

S
Shengliang Guan 已提交
304
int32_t dmPutNodeMsgToMgmtQueue(SDnodeMgmt *pMgmt, SRpcMsg *pMsg) {
S
Shengliang Guan 已提交
305
  SSingleWorker *pWorker = &pMgmt->mgmtWorker;
S
Shengliang Guan 已提交
306
  dTrace("msg:%p, put into worker %s", pMsg, pWorker->name);
S
Shengliang Guan 已提交
307 308
  taosWriteQitem(pWorker->queue, pMsg);
  return 0;
S
shm  
Shengliang Guan 已提交
309
}