dmWorker.c 4.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 "dmImp.h"
S
shm  
Shengliang Guan 已提交
18

S
Shengliang Guan 已提交
19 20 21
static void *dmStatusThreadFp(void *param) {
  SDnode *pDnode = 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
Shengliang Guan 已提交
26
    taosThreadTestCancel();
S
shm  
Shengliang Guan 已提交
27
    taosMsleep(200);
S
Shengliang Guan 已提交
28 29

    if (pDnode->status != DND_STAT_RUNNING || pDnode->data.dropped) {
S
shm  
Shengliang Guan 已提交
30 31 32 33
      continue;
    }

    int64_t curTime = taosGetTimestampMs();
S
Shengliang Guan 已提交
34 35
    float   interval = (curTime - lastTime) / 1000.0f;
    if (interval >= tsStatusInterval) {
S
shm  
Shengliang Guan 已提交
36
      dmSendStatusReq(pMgmt);
S
Shengliang Guan 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
      lastTime = curTime;
    }
  }

  return NULL;
}

static void *dmMonitorThreadFp(void *param) {
  SDnode *pDnode = param;
  int64_t lastTime = taosGetTimestampMs();

  setThreadName("dnode-monitor");

  while (1) {
    taosThreadTestCancel();
    taosMsleep(200);

    if (pDnode->status != DND_STAT_RUNNING || pDnode->data.dropped) {
      continue;
S
shm  
Shengliang Guan 已提交
56 57
    }

S
Shengliang Guan 已提交
58 59 60
    int64_t curTime = taosGetTimestampMs();
    float   interval = (curTime - lastTime) / 1000.0f;
    if (interval >= tsMonitorInterval) {
S
Shengliang Guan 已提交
61
      dmSendMonitorReport(pDnode);
S
Shengliang Guan 已提交
62
      lastTime = curTime;
S
shm  
Shengliang Guan 已提交
63
    }
S
shm  
Shengliang Guan 已提交
64
  }
S
Shengliang Guan 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84

  return NULL;
}

int32_t dmStartStatusThread(SDnode *pDnode) {
  pDnode->statusThreadId = taosCreateThread(dmStatusThreadFp, pDnode);
  if (pDnode->statusThreadId == NULL) {
    dError("failed to init dnode status thread");
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  return 0;
}

void dmStopStatusThread(SDnode *pDnode) {
  if (pDnode->statusThreadId != NULL) {
    taosDestoryThread(pDnode->statusThreadId);
    pDnode->statusThreadId = NULL;
  }
S
shm  
Shengliang Guan 已提交
85 86
}

S
Shengliang Guan 已提交
87 88 89 90
int32_t dmStartMonitorThread(SDnode *pDnode) {
  pDnode->monitorThreadId = taosCreateThread(dmMonitorThreadFp, pDnode);
  if (pDnode->monitorThreadId == NULL) {
    dError("failed to init dnode monitor thread");
S
Shengliang Guan 已提交
91 92 93 94 95 96 97
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return -1;
  }

  return 0;
}

S
Shengliang Guan 已提交
98 99 100 101 102 103
void dmStopMonitorThread(SDnode *pDnode) {
  if (pMgmt->monitorThreadId != NULL) {
    taosDestoryThread(pMgmt->monitorThreadId);
    pMgmt->monitorThreadId = NULL;
  }
}
S
Shengliang Guan 已提交
104

S
Shengliang Guan 已提交
105 106
static void dmProcessMgmtQueue(SQueueInfo *pInfo, SNodeMsg *pMsg) {
  SDnode  *pDnode = pInfo->ahandle;
S
shm  
Shengliang Guan 已提交
107
  SRpcMsg *pRpc = &pMsg->rpcMsg;
S
Shengliang Guan 已提交
108
  int32_t  code = -1;
S
Shengliang Guan 已提交
109
  dTrace("msg:%p, will be processed in dnode-mgmt queue", pMsg);
S
shm  
Shengliang Guan 已提交
110

S
shm  
Shengliang Guan 已提交
111
  switch (pRpc->msgType) {
S
shm  
Shengliang Guan 已提交
112 113 114 115 116 117 118 119 120
    case TDMT_DND_CONFIG_DNODE:
      code = dmProcessConfigReq(pMgmt, pMsg);
      break;
    case TDMT_MND_AUTH_RSP:
      code = dmProcessAuthRsp(pMgmt, pMsg);
      break;
    case TDMT_MND_GRANT_RSP:
      code = dmProcessGrantRsp(pMgmt, pMsg);
      break;
S
shm  
Shengliang Guan 已提交
121
    default:
S
Shengliang Guan 已提交
122
      code = dmProcessCDnodeReq(pMgmt->pDnode, pMsg);
S
Shengliang Guan 已提交
123
      break;
S
shm  
Shengliang Guan 已提交
124
  }
S
shm  
Shengliang Guan 已提交
125

S
shm  
Shengliang Guan 已提交
126
  if (pRpc->msgType & 1u) {
S
shm  
Shengliang Guan 已提交
127
    if (code != 0) code = terrno;
S
shm  
Shengliang Guan 已提交
128
    SRpcMsg rsp = {.handle = pRpc->handle, .ahandle = pRpc->ahandle, .code = code};
S
shm  
Shengliang Guan 已提交
129 130 131
    rpcSendResponse(&rsp);
  }

S
shm  
Shengliang Guan 已提交
132
  dTrace("msg:%p, is freed, result:0x%04x:%s", pMsg, code & 0XFFFF, tstrerror(code));
S
shm  
Shengliang Guan 已提交
133 134
  rpcFreeCont(pMsg->rpcMsg.pCont);
  taosFreeQitem(pMsg);
S
shm  
Shengliang Guan 已提交
135 136
}

S
Shengliang Guan 已提交
137
int32_t dmStartWorker(SDnodeData *pMgmt) {
S
Shengliang Guan 已提交
138 139 140
  SSingleWorkerCfg cfg = {.min = 1, .max = 1, .name = "dnode-mgmt", .fp = (FItem)dmProcessMgmtQueue, .param = pMgmt};
  if (tSingleWorkerInit(&pMgmt->mgmtWorker, &cfg) != 0) {
    dError("failed to start dnode-mgmt worker since %s", terrstr());
S
shm  
Shengliang Guan 已提交
141 142 143
    return -1;
  }

S
Shengliang Guan 已提交
144
  dDebug("dnode workers are initialized");
S
shm  
Shengliang Guan 已提交
145 146 147
  return 0;
}

S
Shengliang Guan 已提交
148
void dmStopWorker(SDnodeData *pMgmt) {
S
Shengliang Guan 已提交
149
  tSingleWorkerCleanup(&pMgmt->mgmtWorker);
S
Shengliang Guan 已提交
150
  dDebug("dnode workers are closed");
S
shm  
Shengliang Guan 已提交
151
}
S
shm  
Shengliang Guan 已提交
152

S
Shengliang Guan 已提交
153
int32_t dmProcessMgmtMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) {
S
Shengliang Guan 已提交
154
  SDnodeData    *pMgmt = pWrapper->pMgmt;
S
Shengliang Guan 已提交
155
  SSingleWorker *pWorker = &pMgmt->mgmtWorker;
S
Shengliang Guan 已提交
156 157 158 159 160 161

  dTrace("msg:%p, put into worker %s", pMsg, pWorker->name);
  taosWriteQitem(pWorker->queue, pMsg);
  return 0;
}

S
Shengliang Guan 已提交
162
int32_t dmProcessStatusMsg(SMgmtWrapper *pWrapper, SNodeMsg *pMsg) {
S
Shengliang Guan 已提交
163
  SDnodeData    *pMgmt = pWrapper->pMgmt;
164
  SSingleWorker *pWorker = &pMgmt->monitorWorker;
S
shm  
Shengliang Guan 已提交
165

S
Shengliang Guan 已提交
166
  dTrace("msg:%p, put into worker %s", pMsg, pWorker->name);
S
Shengliang Guan 已提交
167 168
  taosWriteQitem(pWorker->queue, pMsg);
  return 0;
S
shm  
Shengliang Guan 已提交
169
}