• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #3581

15 Jan 2025 01:12AM UTC coverage: 63.809% (+0.3%) from 63.556%
#3581

push

travis-ci

web-flow
Merge pull request #29561 from taosdata/fix/TD-33504

fix:[TD-33504]add test case

141492 of 284535 branches covered (49.73%)

Branch coverage included in aggregate %.

219814 of 281694 relevant lines covered (78.03%)

19173854.68 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

59.12
/source/dnode/mgmt/mgmt_dnode/src/dmHandle.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http:www.gnu.org/licenses/>.
14
 */
15

16
#define _DEFAULT_SOURCE
17
#include "audit.h"
18
#include "dmInt.h"
19
#include "monitor.h"
20
#include "systable.h"
21
#include "tanalytics.h"
22
#include "tchecksum.h"
23
#include "tutil.h"
24

25
extern SConfig *tsCfg;
26

27
SMonVloadInfo tsVinfo = {0};
28

29
static void dmUpdateDnodeCfg(SDnodeMgmt *pMgmt, SDnodeCfg *pCfg) {
3,683✔
30
  int32_t code = 0;
3,683✔
31
  if (pMgmt->pData->dnodeId == 0 || pMgmt->pData->clusterId == 0) {
3,683✔
32
    dInfo("set local info, dnodeId:%d clusterId:%" PRId64, pCfg->dnodeId, pCfg->clusterId);
1,815!
33
    (void)taosThreadRwlockWrlock(&pMgmt->pData->lock);
1,815✔
34
    pMgmt->pData->dnodeId = pCfg->dnodeId;
1,815✔
35
    pMgmt->pData->clusterId = pCfg->clusterId;
1,815✔
36
    monSetDnodeId(pCfg->dnodeId);
1,815✔
37
    auditSetDnodeId(pCfg->dnodeId);
1,815✔
38
    code = dmWriteEps(pMgmt->pData);
1,815✔
39
    if (code != 0) {
1,815!
40
      dInfo("failed to set local info, dnodeId:%d clusterId:%" PRId64 " reason:%s", pCfg->dnodeId, pCfg->clusterId,
×
41
            tstrerror(code));
42
    }
43
    (void)taosThreadRwlockUnlock(&pMgmt->pData->lock);
1,815✔
44
  }
45
}
3,683✔
46

47
static void dmMayShouldUpdateIpWhiteList(SDnodeMgmt *pMgmt, int64_t ver) {
3,860✔
48
  int32_t code = 0;
3,860✔
49
  dDebug("ip-white-list on dnode ver: %" PRId64 ", status ver: %" PRId64 "", pMgmt->pData->ipWhiteVer, ver);
3,860✔
50
  if (pMgmt->pData->ipWhiteVer == ver) {
3,860✔
51
    if (ver == 0) {
3,854✔
52
      dDebug("disable ip-white-list on dnode ver: %" PRId64 ", status ver: %" PRId64 "", pMgmt->pData->ipWhiteVer, ver);
3,850✔
53
      if (rpcSetIpWhite(pMgmt->msgCb.serverRpc, NULL) != 0) {
3,850!
54
        dError("failed to disable ip white list on dnode");
×
55
      }
56
    }
57
    return;
3,854✔
58
  }
59
  int64_t oldVer = pMgmt->pData->ipWhiteVer;
6✔
60

61
  SRetrieveIpWhiteReq req = {.ipWhiteVer = oldVer};
6✔
62
  int32_t             contLen = tSerializeRetrieveIpWhite(NULL, 0, &req);
6✔
63
  if (contLen < 0) {
6!
64
    dError("failed to serialize ip white list request since: %s", tstrerror(contLen));
×
65
    return;
×
66
  }
67
  void *pHead = rpcMallocCont(contLen);
6✔
68
  contLen = tSerializeRetrieveIpWhite(pHead, contLen, &req);
6✔
69
  if (contLen < 0) {
6!
70
    rpcFreeCont(pHead);
×
71
    dError("failed to serialize ip white list request since:%s", tstrerror(contLen));
×
72
    return;
×
73
  }
74

75
  SRpcMsg rpcMsg = {.pCont = pHead,
6✔
76
                    .contLen = contLen,
77
                    .msgType = TDMT_MND_RETRIEVE_IP_WHITE,
78
                    .info.ahandle = 0,
79
                    .info.notFreeAhandle = 1,
80
                    .info.refId = 0,
81
                    .info.noResp = 0,
82
                    .info.handle = 0};
83
  SEpSet  epset = {0};
6✔
84

85
  (void)dmGetMnodeEpSet(pMgmt->pData, &epset);
6✔
86

87
  code = rpcSendRequest(pMgmt->msgCb.clientRpc, &epset, &rpcMsg, NULL);
6✔
88
  if (code != 0) {
6!
89
    dError("failed to send retrieve ip white list request since:%s", tstrerror(code));
×
90
  }
91
}
92

93
static void dmMayShouldUpdateAnalFunc(SDnodeMgmt *pMgmt, int64_t newVer) {
3,860✔
94
  int32_t code = 0;
3,860✔
95
  int64_t oldVer = taosAnalGetVersion();
3,860✔
96
  if (oldVer == newVer) return;
3,860✔
97
  dDebug("analysis on dnode ver:%" PRId64 ", status ver:%" PRId64, oldVer, newVer);
2!
98

99
  SRetrieveAnalAlgoReq req = {.dnodeId = pMgmt->pData->dnodeId, .analVer = oldVer};
2✔
100
  int32_t              contLen = tSerializeRetrieveAnalAlgoReq(NULL, 0, &req);
2✔
101
  if (contLen < 0) {
2!
102
    dError("failed to serialize analysis function ver request since %s", tstrerror(contLen));
×
103
    return;
×
104
  }
105

106
  void *pHead = rpcMallocCont(contLen);
2✔
107
  contLen = tSerializeRetrieveAnalAlgoReq(pHead, contLen, &req);
2✔
108
  if (contLen < 0) {
2!
109
    rpcFreeCont(pHead);
×
110
    dError("failed to serialize analysis function ver request since %s", tstrerror(contLen));
×
111
    return;
×
112
  }
113

114
  SRpcMsg rpcMsg = {
2✔
115
      .pCont = pHead,
116
      .contLen = contLen,
117
      .msgType = TDMT_MND_RETRIEVE_ANAL_ALGO,
118
      .info.ahandle = 0,
119
      .info.refId = 0,
120
      .info.noResp = 0,
121
      .info.handle = 0,
122
  };
123
  SEpSet epset = {0};
2✔
124

125
  (void)dmGetMnodeEpSet(pMgmt->pData, &epset);
2✔
126

127
  code = rpcSendRequest(pMgmt->msgCb.clientRpc, &epset, &rpcMsg, NULL);
2✔
128
  if (code != 0) {
2!
129
    dError("failed to send retrieve analysis func ver request since %s", tstrerror(code));
×
130
  }
131
}
132

133
static void dmProcessStatusRsp(SDnodeMgmt *pMgmt, SRpcMsg *pRsp) {
104,788✔
134
  const STraceId *trace = &pRsp->info.traceId;
104,788✔
135
  dGTrace("status rsp received from mnode, statusSeq:%d code:0x%x", pMgmt->statusSeq, pRsp->code);
104,788!
136

137
  if (pRsp->code != 0) {
104,788!
138
    if (pRsp->code == TSDB_CODE_MND_DNODE_NOT_EXIST && !pMgmt->pData->dropped && pMgmt->pData->dnodeId > 0) {
×
139
      dGInfo("dnode:%d, set to dropped since not exist in mnode, statusSeq:%d", pMgmt->pData->dnodeId,
×
140
             pMgmt->statusSeq);
141
      pMgmt->pData->dropped = 1;
×
142
      if (dmWriteEps(pMgmt->pData) != 0) {
×
143
        dError("failed to write dnode file");
×
144
      }
145
      dInfo("dnode will exit since it is in the dropped state");
×
146
      (void)raise(SIGINT);
×
147
    }
148
  } else {
149
    SStatusRsp statusRsp = {0};
104,788✔
150
    if (pRsp->pCont != NULL && pRsp->contLen > 0 &&
108,648!
151
        tDeserializeSStatusRsp(pRsp->pCont, pRsp->contLen, &statusRsp) == 0) {
3,860✔
152
      if (pMgmt->pData->dnodeVer != statusRsp.dnodeVer) {
3,860✔
153
        dGInfo("status rsp received from mnode, statusSeq:%d:%d dnodeVer:%" PRId64 ":%" PRId64, pMgmt->statusSeq,
3,683!
154
               statusRsp.statusSeq, pMgmt->pData->dnodeVer, statusRsp.dnodeVer);
155
        pMgmt->pData->dnodeVer = statusRsp.dnodeVer;
3,683✔
156
        dmUpdateDnodeCfg(pMgmt, &statusRsp.dnodeCfg);
3,683✔
157
        dmUpdateEps(pMgmt->pData, statusRsp.pDnodeEps);
3,683✔
158
      }
159
      dmMayShouldUpdateIpWhiteList(pMgmt, statusRsp.ipWhiteVer);
3,860✔
160
      dmMayShouldUpdateAnalFunc(pMgmt, statusRsp.analVer);
3,860✔
161
    }
162
    tFreeSStatusRsp(&statusRsp);
104,788✔
163
  }
164
  rpcFreeCont(pRsp->pCont);
104,788✔
165
}
104,788✔
166

167
void dmSendStatusReq(SDnodeMgmt *pMgmt) {
105,274✔
168
  int32_t    code = 0;
105,274✔
169
  SStatusReq req = {0};
105,274✔
170

171
  dDebug("send status req to mnode, statusSeq:%d, begin to mgnt lock", pMgmt->statusSeq);
105,274✔
172
  (void)taosThreadRwlockRdlock(&pMgmt->pData->lock);
105,274✔
173
  req.sver = tsVersion;
105,274✔
174
  req.dnodeVer = pMgmt->pData->dnodeVer;
105,274✔
175
  req.dnodeId = pMgmt->pData->dnodeId;
105,274✔
176
  req.clusterId = pMgmt->pData->clusterId;
105,274✔
177
  if (req.clusterId == 0) req.dnodeId = 0;
105,274✔
178
  req.rebootTime = pMgmt->pData->rebootTime;
105,274✔
179
  req.updateTime = pMgmt->pData->updateTime;
105,274✔
180
  req.numOfCores = tsNumOfCores;
105,274✔
181
  req.numOfSupportVnodes = tsNumOfSupportVnodes;
105,274✔
182
  req.numOfDiskCfg = tsDiskCfgNum;
105,274✔
183
  req.memTotal = tsTotalMemoryKB * 1024;
105,274✔
184
  req.memAvail = req.memTotal - tsQueueMemoryAllowed - 16 * 1024 * 1024;
105,274✔
185
  tstrncpy(req.dnodeEp, tsLocalEp, TSDB_EP_LEN);
105,274✔
186
  tstrncpy(req.machineId, pMgmt->pData->machineId, TSDB_MACHINE_ID_LEN + 1);
105,274✔
187

188
  req.clusterCfg.statusInterval = tsStatusInterval;
105,274✔
189
  req.clusterCfg.checkTime = 0;
105,274✔
190
  req.clusterCfg.ttlChangeOnWrite = tsTtlChangeOnWrite;
105,274✔
191
  req.clusterCfg.enableWhiteList = tsEnableWhiteList ? 1 : 0;
105,274✔
192
  req.clusterCfg.encryptionKeyStat = tsEncryptionKeyStat;
105,274✔
193
  req.clusterCfg.encryptionKeyChksum = tsEncryptionKeyChksum;
105,274✔
194
  req.clusterCfg.monitorParas.tsEnableMonitor = tsEnableMonitor;
105,274✔
195
  req.clusterCfg.monitorParas.tsMonitorInterval = tsMonitorInterval;
105,274✔
196
  req.clusterCfg.monitorParas.tsSlowLogScope = tsSlowLogScope;
105,274✔
197
  req.clusterCfg.monitorParas.tsSlowLogMaxLen = tsSlowLogMaxLen;
105,274✔
198
  req.clusterCfg.monitorParas.tsSlowLogThreshold = tsSlowLogThreshold;
105,274✔
199
  tstrncpy(req.clusterCfg.monitorParas.tsSlowLogExceptDb, tsSlowLogExceptDb, TSDB_DB_NAME_LEN);
105,274✔
200
  char timestr[32] = "1970-01-01 00:00:00.00";
105,274✔
201
  if (taosParseTime(timestr, &req.clusterCfg.checkTime, (int32_t)strlen(timestr), TSDB_TIME_PRECISION_MILLI, NULL) !=
105,274!
202
      0) {
203
    dError("failed to parse time since %s", tstrerror(code));
×
204
  }
205
  memcpy(req.clusterCfg.timezone, tsTimezoneStr, TD_TIMEZONE_LEN);
105,274✔
206
  memcpy(req.clusterCfg.locale, tsLocale, TD_LOCALE_LEN);
105,274✔
207
  memcpy(req.clusterCfg.charset, tsCharset, TD_LOCALE_LEN);
105,274✔
208
  (void)taosThreadRwlockUnlock(&pMgmt->pData->lock);
105,274✔
209

210
  dDebug("send status req to mnode, statusSeq:%d, begin to get vnode loads", pMgmt->statusSeq);
105,274✔
211
  if (taosThreadMutexLock(&pMgmt->pData->statusInfolock) != 0) {
105,274!
212
    dError("failed to lock status info lock");
×
213
    return;
486✔
214
  }
215
  req.pVloads = tsVinfo.pVloads;
105,274✔
216
  tsVinfo.pVloads = NULL;
105,274✔
217
  if (taosThreadMutexUnlock(&pMgmt->pData->statusInfolock) != 0) {
105,274!
218
    dError("failed to unlock status info lock");
×
219
    return;
×
220
  }
221

222
  dDebug("send status req to mnode, statusSeq:%d, begin to get mnode loads", pMgmt->statusSeq);
105,274✔
223
  SMonMloadInfo minfo = {0};
105,274✔
224
  (*pMgmt->getMnodeLoadsFp)(&minfo);
105,274✔
225
  req.mload = minfo.load;
105,274✔
226

227
  dDebug("send status req to mnode, statusSeq:%d, begin to get qnode loads", pMgmt->statusSeq);
105,274✔
228
  (*pMgmt->getQnodeLoadsFp)(&req.qload);
105,274✔
229

230
  pMgmt->statusSeq++;
105,274✔
231
  req.statusSeq = pMgmt->statusSeq;
105,274✔
232
  req.ipWhiteVer = pMgmt->pData->ipWhiteVer;
105,274✔
233
  req.analVer = taosAnalGetVersion();
105,274✔
234

235
  int32_t contLen = tSerializeSStatusReq(NULL, 0, &req);
105,274✔
236
  if (contLen < 0) {
105,274!
237
    dError("failed to serialize status req since %s", tstrerror(contLen));
×
238
    return;
×
239
  }
240

241
  void *pHead = rpcMallocCont(contLen);
105,274✔
242
  contLen = tSerializeSStatusReq(pHead, contLen, &req);
105,274✔
243
  if (contLen < 0) {
105,274!
244
    rpcFreeCont(pHead);
×
245
    dError("failed to serialize status req since %s", tstrerror(contLen));
×
246
    return;
×
247
  }
248
  tFreeSStatusReq(&req);
105,274✔
249

250
  SRpcMsg rpcMsg = {.pCont = pHead,
105,274✔
251
                    .contLen = contLen,
252
                    .msgType = TDMT_MND_STATUS,
253
                    .info.ahandle = 0,
254
                    .info.notFreeAhandle = 1,
255
                    .info.refId = 0,
256
                    .info.noResp = 0,
257
                    .info.handle = 0};
258
  SRpcMsg rpcRsp = {0};
105,274✔
259

260
  dTrace("send status req to mnode, dnodeVer:%" PRId64 " statusSeq:%d", req.dnodeVer, req.statusSeq);
105,274✔
261

262
  SEpSet epSet = {0};
105,274✔
263
  int8_t epUpdated = 0;
105,274✔
264
  (void)dmGetMnodeEpSet(pMgmt->pData, &epSet);
105,274✔
265

266
  dDebug("send status req to mnode, statusSeq:%d, begin to send rpc msg", pMgmt->statusSeq);
105,274✔
267
  code =
268
      rpcSendRecvWithTimeout(pMgmt->msgCb.statusRpc, &epSet, &rpcMsg, &rpcRsp, &epUpdated, tsStatusInterval * 5 * 1000);
105,274✔
269
  if (code != 0) {
105,274✔
270
    dError("failed to send status req since %s", tstrerror(code));
486!
271
    return;
486✔
272
  }
273

274
  if (rpcRsp.code != 0) {
104,788!
275
    dmRotateMnodeEpSet(pMgmt->pData);
×
276
    char tbuf[512];
277
    dmEpSetToStr(tbuf, sizeof(tbuf), &epSet);
×
278
    dError("failed to send status req since %s, epSet:%s, inUse:%d", tstrerror(rpcRsp.code), tbuf, epSet.inUse);
×
279
  } else {
280
    if (epUpdated == 1) {
104,788✔
281
      dmSetMnodeEpSet(pMgmt->pData, &epSet);
274✔
282
    }
283
  }
284
  dmProcessStatusRsp(pMgmt, &rpcRsp);
104,788✔
285
}
286

287
static void dmProcessConfigRsp(SDnodeMgmt *pMgmt, SRpcMsg *pRsp) {
2,181✔
288
  const STraceId *trace = &pRsp->info.traceId;
2,181✔
289
  int32_t         code = 0;
2,181✔
290
  SConfigRsp      configRsp = {0};
2,181✔
291
  bool            needStop = false;
2,181✔
292

293
  if (pRsp->code != 0) {
2,181!
294
    if (pRsp->code == TSDB_CODE_MND_DNODE_NOT_EXIST && !pMgmt->pData->dropped && pMgmt->pData->dnodeId > 0) {
×
295
      dGInfo("dnode:%d, set to dropped since not exist in mnode", pMgmt->pData->dnodeId);
×
296
      pMgmt->pData->dropped = 1;
×
297
      if (dmWriteEps(pMgmt->pData) != 0) {
×
298
        dError("failed to write dnode file");
×
299
      }
300
      dInfo("dnode will exit since it is in the dropped state");
×
301
      (void)raise(SIGINT);
×
302
    }
303
  } else {
304
    bool needUpdate = false;
2,181✔
305
    if (pRsp->pCont != NULL && pRsp->contLen > 0 &&
4,362!
306
        tDeserializeSConfigRsp(pRsp->pCont, pRsp->contLen, &configRsp) == 0) {
2,181✔
307
      // Try to use cfg file in current dnode.
308
      if (configRsp.forceReadConfig) {
2,181!
309
        if (configRsp.isConifgVerified) {
×
310
          uInfo("force read config and check config verified");
×
311
          code = taosPersistGlobalConfig(taosGetGlobalCfg(tsCfg), pMgmt->path, configRsp.cver);
×
312
          if (code != TSDB_CODE_SUCCESS) {
×
313
            dError("failed to persist global config since %s", tstrerror(code));
×
314
            goto _exit;
×
315
          }
316
          needUpdate = true;
×
317
        } else {
318
          // log the difference configurations
319
          printConfigNotMatch(configRsp.array);
×
320
          needStop = true;
×
321
          goto _exit;
×
322
        }
323
      }
324
      // Try to use cfg from mnode sdb.
325
      if (!configRsp.isVersionVerified) {
2,181✔
326
        uInfo("config version not verified, update config");
1,824!
327
        needUpdate = true;
1,824✔
328
        code = taosPersistGlobalConfig(configRsp.array, pMgmt->path, configRsp.cver);
1,824✔
329
        if (code != TSDB_CODE_SUCCESS) {
1,824✔
330
          dError("failed to persist global config since %s", tstrerror(code));
8!
331
          goto _exit;
8✔
332
        }
333
      }
334
    }
335
    if (needUpdate) {
2,173✔
336
      code = cfgUpdateFromArray(tsCfg, configRsp.array);
1,816✔
337
      if (code != TSDB_CODE_SUCCESS) {
1,816!
338
        dError("failed to update config since %s", tstrerror(code));
×
339
        goto _exit;
×
340
      }
341
      code = setAllConfigs(tsCfg);
1,816✔
342
      if (code != TSDB_CODE_SUCCESS) {
1,816✔
343
        dError("failed to set all configs since %s", tstrerror(code));
8!
344
        goto _exit;
8✔
345
      }
346
    }
347
    code = taosPersistLocalConfig(pMgmt->path);
2,165✔
348
    if (code != TSDB_CODE_SUCCESS) {
2,165!
349
      dError("failed to persist local config since %s", tstrerror(code));
×
350
    }
351
    tsConfigInited = 1;
2,165✔
352
  }
353
_exit:
2,181✔
354
  tFreeSConfigRsp(&configRsp);
2,181✔
355
  rpcFreeCont(pRsp->pCont);
2,181✔
356
  if (needStop) {
2,181!
357
    dmStop();
×
358
  }
359
}
2,181✔
360

361
void dmSendConfigReq(SDnodeMgmt *pMgmt) {
2,240✔
362
  int32_t    code = 0;
2,240✔
363
  SConfigReq req = {0};
2,240✔
364

365
  req.cver = tsdmConfigVersion;
2,240✔
366
  req.forceReadConfig = tsForceReadConfig;
2,240✔
367
  req.array = taosGetGlobalCfg(tsCfg);
2,240✔
368
  dDebug("send config req to mnode, configVersion:%d", req.cver);
2,240✔
369

370
  int32_t contLen = tSerializeSConfigReq(NULL, 0, &req);
2,240✔
371
  if (contLen < 0) {
2,240!
372
    dError("failed to serialize status req since %s", tstrerror(contLen));
×
373
    return;
59✔
374
  }
375

376
  void *pHead = rpcMallocCont(contLen);
2,240✔
377
  if (pHead == NULL) {
2,240!
378
    dError("failed to malloc cont since %s", tstrerror(contLen));
×
379
    return;
×
380
  }
381
  contLen = tSerializeSConfigReq(pHead, contLen, &req);
2,240✔
382
  if (contLen < 0) {
2,240!
383
    rpcFreeCont(pHead);
×
384
    dError("failed to serialize status req since %s", tstrerror(contLen));
×
385
    return;
×
386
  }
387

388
  SRpcMsg rpcMsg = {.pCont = pHead,
2,240✔
389
                    .contLen = contLen,
390
                    .msgType = TDMT_MND_CONFIG,
391
                    .info.ahandle = 0,
392
                    .info.notFreeAhandle = 1,
393
                    .info.refId = 0,
394
                    .info.noResp = 0,
395
                    .info.handle = 0};
396
  SRpcMsg rpcRsp = {0};
2,240✔
397

398
  SEpSet epSet = {0};
2,240✔
399
  int8_t epUpdated = 0;
2,240✔
400
  (void)dmGetMnodeEpSet(pMgmt->pData, &epSet);
2,240✔
401

402
  dDebug("send status req to mnode, statusSeq:%d, begin to send rpc msg", pMgmt->statusSeq);
2,240✔
403
  code =
404
      rpcSendRecvWithTimeout(pMgmt->msgCb.statusRpc, &epSet, &rpcMsg, &rpcRsp, &epUpdated, tsStatusInterval * 5 * 1000);
2,240✔
405
  if (code != 0) {
2,240✔
406
    dError("failed to send status req since %s", tstrerror(code));
59!
407
    return;
59✔
408
  }
409
  if (rpcRsp.code != 0) {
2,181!
410
    dError("failed to send config req since %s", tstrerror(rpcRsp.code));
×
411
    return;
×
412
  }
413
  dmProcessConfigRsp(pMgmt, &rpcRsp);
2,181✔
414
}
415

416
void dmUpdateStatusInfo(SDnodeMgmt *pMgmt) {
107,712✔
417
  SMonVloadInfo vinfo = {0};
107,712✔
418
  dDebug("begin to get vnode loads");
107,712✔
419
  (*pMgmt->getVnodeLoadsFp)(&vinfo);
107,712✔
420
  dDebug("begin to lock status info");
107,712✔
421
  if (taosThreadMutexLock(&pMgmt->pData->statusInfolock) != 0) {
107,712!
422
    dError("failed to lock status info lock");
×
423
    return;
×
424
  }
425
  if (tsVinfo.pVloads == NULL) {
107,712✔
426
    tsVinfo.pVloads = vinfo.pVloads;
103,907✔
427
    vinfo.pVloads = NULL;
103,907✔
428
  } else {
429
    taosArrayDestroy(vinfo.pVloads);
3,805✔
430
    vinfo.pVloads = NULL;
3,805✔
431
  }
432
  if (taosThreadMutexUnlock(&pMgmt->pData->statusInfolock) != 0) {
107,712!
433
    dError("failed to unlock status info lock");
×
434
    return;
×
435
  }
436
}
437

438
void dmSendNotifyReq(SDnodeMgmt *pMgmt, SNotifyReq *pReq) {
×
439
  int32_t contLen = tSerializeSNotifyReq(NULL, 0, pReq);
×
440
  if (contLen < 0) {
×
441
    dError("failed to serialize notify req since %s", tstrerror(contLen));
×
442
    return;
×
443
  }
444
  void *pHead = rpcMallocCont(contLen);
×
445
  contLen = tSerializeSNotifyReq(pHead, contLen, pReq);
×
446
  if (contLen < 0) {
×
447
    rpcFreeCont(pHead);
×
448
    dError("failed to serialize notify req since %s", tstrerror(contLen));
×
449
    return;
×
450
  }
451

452
  SRpcMsg rpcMsg = {.pCont = pHead,
×
453
                    .contLen = contLen,
454
                    .msgType = TDMT_MND_NOTIFY,
455
                    .info.ahandle = 0,
456
                    .info.notFreeAhandle = 1,
457
                    .info.refId = 0,
458
                    .info.noResp = 1,
459
                    .info.handle = 0};
460

461
  SEpSet epSet = {0};
×
462
  dmGetMnodeEpSet(pMgmt->pData, &epSet);
×
463
  if (rpcSendRequest(pMgmt->msgCb.clientRpc, &epSet, &rpcMsg, NULL) != 0) {
×
464
    dError("failed to send notify req");
×
465
  }
466
}
467

468
int32_t dmProcessAuthRsp(SDnodeMgmt *pMgmt, SRpcMsg *pMsg) {
×
469
  dError("auth rsp is received, but not supported yet");
×
470
  return 0;
×
471
}
472

473
int32_t dmProcessGrantRsp(SDnodeMgmt *pMgmt, SRpcMsg *pMsg) {
×
474
  dError("grant rsp is received, but not supported yet");
×
475
  return 0;
×
476
}
477

478
int32_t dmProcessConfigReq(SDnodeMgmt *pMgmt, SRpcMsg *pMsg) {
1,409✔
479
  int32_t       code = 0;
1,409✔
480
  SDCfgDnodeReq cfgReq = {0};
1,409✔
481
  SConfig      *pCfg = taosGetCfg();
1,409✔
482
  SConfigItem  *pItem = NULL;
1,409✔
483

484
  if (tDeserializeSDCfgDnodeReq(pMsg->pCont, pMsg->contLen, &cfgReq) != 0) {
1,409!
485
    return TSDB_CODE_INVALID_MSG;
×
486
  }
487
  if (strcasecmp(cfgReq.config, "dataDir") == 0) {
1,409!
488
    return taosUpdateTfsItemDisable(pCfg, cfgReq.value, pMgmt->pTfs);
×
489
  }
490

491
  dInfo("start to config, option:%s, value:%s", cfgReq.config, cfgReq.value);
1,409!
492

493
  code = cfgGetAndSetItem(pCfg, &pItem, cfgReq.config, cfgReq.value, CFG_STYPE_ALTER_SERVER_CMD, true);
1,409✔
494
  if (code != 0) {
1,409✔
495
    if (strncasecmp(cfgReq.config, "resetlog", strlen("resetlog")) == 0) {
1!
496
      TAOS_CHECK_RETURN(taosCfgDynamicOptions(pCfg, cfgReq.config, true));
1!
497
      return TSDB_CODE_SUCCESS;
1✔
498
    } else {
499
      return code;
×
500
    }
501
  }
502
  if (pItem == NULL) {
1,408!
503
    return TSDB_CODE_CFG_NOT_FOUND;
×
504
  }
505
  if (!isConifgItemLazyMode(pItem)) {
1,408✔
506
    TAOS_CHECK_RETURN(taosCfgDynamicOptions(pCfg, cfgReq.config, true));
1,294!
507
  }
508

509
  if (pItem->category == CFG_CATEGORY_GLOBAL) {
1,408✔
510
    code = taosPersistGlobalConfig(taosGetGlobalCfg(pCfg), pMgmt->path, tsdmConfigVersion);
1,320✔
511
    if (code != TSDB_CODE_SUCCESS) {
1,320!
512
      dError("failed to persist global config since %s", tstrerror(code));
×
513
    }
514
  } else {
515
    code = taosPersistLocalConfig(pMgmt->path);
88✔
516
    if (code != TSDB_CODE_SUCCESS) {
88!
517
      dError("failed to persist local config since %s", tstrerror(code));
×
518
    }
519
  }
520
  if (cfgReq.version > 0) {
1,408✔
521
    tsdmConfigVersion = cfgReq.version;
1,400✔
522
  }
523
  return code;
1,408✔
524
}
525

526
int32_t dmProcessCreateEncryptKeyReq(SDnodeMgmt *pMgmt, SRpcMsg *pMsg) {
×
527
#ifdef TD_ENTERPRISE
528
  int32_t       code = 0;
×
529
  SDCfgDnodeReq cfgReq = {0};
×
530
  if (tDeserializeSDCfgDnodeReq(pMsg->pCont, pMsg->contLen, &cfgReq) != 0) {
×
531
    code = TSDB_CODE_INVALID_MSG;
×
532
    goto _exit;
×
533
  }
534

535
  code = dmUpdateEncryptKey(cfgReq.value, true);
×
536
  if (code == 0) {
×
537
    tsEncryptionKeyChksum = taosCalcChecksum(0, cfgReq.value, strlen(cfgReq.value));
×
538
    tsEncryptionKeyStat = ENCRYPT_KEY_STAT_LOADED;
×
539
    tstrncpy(tsEncryptKey, cfgReq.value, ENCRYPT_KEY_LEN + 1);
×
540
  }
541

542
_exit:
×
543
  pMsg->code = code;
×
544
  pMsg->info.rsp = NULL;
×
545
  pMsg->info.rspLen = 0;
×
546
  return code;
×
547
#else
548
  return 0;
549
#endif
550
}
551

552
static void dmGetServerRunStatus(SDnodeMgmt *pMgmt, SServerStatusRsp *pStatus) {
3✔
553
  pStatus->statusCode = TSDB_SRV_STATUS_SERVICE_OK;
3✔
554
  pStatus->details[0] = 0;
3✔
555

556
  SMonMloadInfo minfo = {0};
3✔
557
  (*pMgmt->getMnodeLoadsFp)(&minfo);
3✔
558
  if (minfo.isMnode &&
3!
559
      (minfo.load.syncState == TAOS_SYNC_STATE_ERROR || minfo.load.syncState == TAOS_SYNC_STATE_OFFLINE)) {
3!
560
    pStatus->statusCode = TSDB_SRV_STATUS_SERVICE_DEGRADED;
×
561
    snprintf(pStatus->details, sizeof(pStatus->details), "mnode sync state is %s", syncStr(minfo.load.syncState));
×
562
    return;
×
563
  }
564

565
  SMonVloadInfo vinfo = {0};
3✔
566
  (*pMgmt->getVnodeLoadsFp)(&vinfo);
3✔
567
  for (int32_t i = 0; i < taosArrayGetSize(vinfo.pVloads); ++i) {
9✔
568
    SVnodeLoad *pLoad = taosArrayGet(vinfo.pVloads, i);
6✔
569
    if (pLoad->syncState == TAOS_SYNC_STATE_ERROR || pLoad->syncState == TAOS_SYNC_STATE_OFFLINE) {
6!
570
      pStatus->statusCode = TSDB_SRV_STATUS_SERVICE_DEGRADED;
×
571
      snprintf(pStatus->details, sizeof(pStatus->details), "vnode:%d sync state is %s", pLoad->vgId,
×
572
               syncStr(pLoad->syncState));
×
573
      break;
×
574
    }
575
  }
576

577
  taosArrayDestroy(vinfo.pVloads);
3✔
578
}
579

580
int32_t dmProcessServerRunStatus(SDnodeMgmt *pMgmt, SRpcMsg *pMsg) {
3✔
581
  int32_t code = 0;
3✔
582
  dDebug("server run status req is received");
3!
583
  SServerStatusRsp statusRsp = {0};
3✔
584
  dmGetServerRunStatus(pMgmt, &statusRsp);
3✔
585

586
  pMsg->info.rsp = NULL;
3✔
587
  pMsg->info.rspLen = 0;
3✔
588

589
  SRpcMsg rspMsg = {.info = pMsg->info};
3✔
590
  int32_t rspLen = tSerializeSServerStatusRsp(NULL, 0, &statusRsp);
3✔
591
  if (rspLen < 0) {
3!
592
    return TSDB_CODE_OUT_OF_MEMORY;
×
593
    // rspMsg.code = TSDB_CODE_OUT_OF_MEMORY;
594
    // return rspMsg.code;
595
  }
596

597
  void *pRsp = rpcMallocCont(rspLen);
3✔
598
  if (pRsp == NULL) {
3!
599
    return terrno;
×
600
    // rspMsg.code = TSDB_CODE_OUT_OF_MEMORY;
601
    // return rspMsg.code;
602
  }
603

604
  rspLen = tSerializeSServerStatusRsp(pRsp, rspLen, &statusRsp);
3✔
605
  if (rspLen < 0) {
3!
606
    return TSDB_CODE_INVALID_MSG;
×
607
  }
608

609
  pMsg->info.rsp = pRsp;
3✔
610
  pMsg->info.rspLen = rspLen;
3✔
611
  return 0;
3✔
612
}
613

614
int32_t dmBuildVariablesBlock(SSDataBlock **ppBlock) {
4,814✔
615
  int32_t code = 0;
4,814✔
616

617
  SSDataBlock *pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
4,814!
618
  if (pBlock == NULL) {
4,814!
619
    return terrno;
×
620
  }
621

622
  size_t size = 0;
4,814✔
623

624
  const SSysTableMeta *pMeta = NULL;
4,814✔
625
  getInfosDbMeta(&pMeta, &size);
4,814✔
626

627
  int32_t index = 0;
4,814✔
628
  for (int32_t i = 0; i < size; ++i) {
86,652!
629
    if (strcmp(pMeta[i].name, TSDB_INS_TABLE_DNODE_VARIABLES) == 0) {
86,652✔
630
      index = i;
4,814✔
631
      break;
4,814✔
632
    }
633
  }
634

635
  pBlock->pDataBlock = taosArrayInit(pMeta[index].colNum, sizeof(SColumnInfoData));
4,814✔
636
  if (pBlock->pDataBlock == NULL) {
4,814!
637
    code = terrno;
×
638
    goto _exit;
×
639
  }
640

641
  for (int32_t i = 0; i < pMeta[index].colNum; ++i) {
33,698✔
642
    SColumnInfoData colInfoData = {0};
28,884✔
643
    colInfoData.info.colId = i + 1;
28,884✔
644
    colInfoData.info.type = pMeta[index].schema[i].type;
28,884✔
645
    colInfoData.info.bytes = pMeta[index].schema[i].bytes;
28,884✔
646
    if (taosArrayPush(pBlock->pDataBlock, &colInfoData) == NULL) {
57,768!
647
      code = terrno;
×
648
      goto _exit;
×
649
    }
650
  }
651

652
  pBlock->info.hasVarCol = true;
4,814✔
653
_exit:
4,814✔
654
  if (code != 0) {
4,814!
655
    blockDataDestroy(pBlock);
×
656
  } else {
657
    *ppBlock = pBlock;
4,814✔
658
  }
659
  return code;
4,814✔
660
}
661

662
int32_t dmAppendVariablesToBlock(SSDataBlock *pBlock, int32_t dnodeId) {
4,814✔
663
  int32_t code = dumpConfToDataBlock(pBlock, 1);
4,814✔
664
  if (code != 0) {
4,814!
665
    return code;
×
666
  }
667

668
  SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, 0);
4,814✔
669
  if (pColInfo == NULL) {
4,814!
670
    return TSDB_CODE_OUT_OF_RANGE;
×
671
  }
672

673
  return colDataSetNItems(pColInfo, 0, (const char *)&dnodeId, pBlock->info.rows, false);
4,814✔
674
}
675

676
int32_t dmProcessRetrieve(SDnodeMgmt *pMgmt, SRpcMsg *pMsg) {
4,814✔
677
  int32_t           size = 0;
4,814✔
678
  int32_t           rowsRead = 0;
4,814✔
679
  int32_t           code = 0;
4,814✔
680
  SRetrieveTableReq retrieveReq = {0};
4,814✔
681
  if (tDeserializeSRetrieveTableReq(pMsg->pCont, pMsg->contLen, &retrieveReq) != 0) {
4,814!
682
    return TSDB_CODE_INVALID_MSG;
×
683
  }
684
  dInfo("retrieve table:%s, user:%s, compactId:%" PRId64, retrieveReq.tb, retrieveReq.user, retrieveReq.compactId);
4,814!
685
#if 0
686
  if (strcmp(retrieveReq.user, TSDB_DEFAULT_USER) != 0) {
687
    code = TSDB_CODE_MND_NO_RIGHTS;
688
    return code;
689
  }
690
#endif
691
  if (strcasecmp(retrieveReq.tb, TSDB_INS_TABLE_DNODE_VARIABLES)) {
4,814!
692
    return TSDB_CODE_INVALID_MSG;
×
693
  }
694

695
  SSDataBlock *pBlock = NULL;
4,814✔
696
  if ((code = dmBuildVariablesBlock(&pBlock)) != 0) {
4,814!
697
    return code;
×
698
  }
699

700
  code = dmAppendVariablesToBlock(pBlock, pMgmt->pData->dnodeId);
4,814✔
701
  if (code != 0) {
4,814!
702
    blockDataDestroy(pBlock);
×
703
    return code;
×
704
  }
705

706
  size_t numOfCols = taosArrayGetSize(pBlock->pDataBlock);
4,814✔
707
  size_t dataEncodeBufSize = blockGetEncodeSize(pBlock);
4,814✔
708
  size = sizeof(SRetrieveMetaTableRsp) + sizeof(int32_t) + sizeof(SSysTableSchema) * numOfCols + dataEncodeBufSize;
4,814✔
709

710
  SRetrieveMetaTableRsp *pRsp = rpcMallocCont(size);
4,814✔
711
  if (pRsp == NULL) {
4,814!
712
    code = terrno;
×
713
    dError("failed to retrieve data since %s", tstrerror(code));
×
714
    blockDataDestroy(pBlock);
×
715
    return code;
×
716
  }
717

718
  char *pStart = pRsp->data;
4,814✔
719
  *(int32_t *)pStart = htonl(numOfCols);
4,814✔
720
  pStart += sizeof(int32_t);  // number of columns
4,814✔
721

722
  for (int32_t i = 0; i < numOfCols; ++i) {
33,698✔
723
    SSysTableSchema *pSchema = (SSysTableSchema *)pStart;
28,884✔
724
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, i);
28,884✔
725

726
    pSchema->bytes = htonl(pColInfo->info.bytes);
28,884✔
727
    pSchema->colId = htons(pColInfo->info.colId);
28,884✔
728
    pSchema->type = pColInfo->info.type;
28,884✔
729

730
    pStart += sizeof(SSysTableSchema);
28,884✔
731
  }
732

733
  int32_t len = blockEncode(pBlock, pStart, dataEncodeBufSize, numOfCols);
4,814✔
734
  if (len < 0) {
4,814!
735
    dError("failed to retrieve data since %s", tstrerror(code));
×
736
    blockDataDestroy(pBlock);
×
737
    rpcFreeCont(pRsp);
×
738
    return terrno;
×
739
  }
740

741
  pRsp->numOfRows = htonl(pBlock->info.rows);
4,814✔
742
  pRsp->precision = TSDB_TIME_PRECISION_MILLI;  // millisecond time precision
4,814✔
743
  pRsp->completed = 1;
4,814✔
744
  pMsg->info.rsp = pRsp;
4,814✔
745
  pMsg->info.rspLen = size;
4,814✔
746
  dDebug("dnode variables retrieve completed");
4,814!
747

748
  blockDataDestroy(pBlock);
4,814✔
749
  return TSDB_CODE_SUCCESS;
4,814✔
750
}
751

752
SArray *dmGetMsgHandles() {
2,271✔
753
  int32_t code = -1;
2,271✔
754
  SArray *pArray = taosArrayInit(16, sizeof(SMgmtHandle));
2,271✔
755
  if (pArray == NULL) {
2,271!
756
    return NULL;
×
757
  }
758

759
  // Requests handled by DNODE
760
  if (dmSetMgmtHandle(pArray, TDMT_DND_CREATE_MNODE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
2,271!
761
  if (dmSetMgmtHandle(pArray, TDMT_DND_DROP_MNODE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
2,271!
762
  if (dmSetMgmtHandle(pArray, TDMT_DND_CREATE_QNODE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
2,271!
763
  if (dmSetMgmtHandle(pArray, TDMT_DND_DROP_QNODE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
2,271!
764
  if (dmSetMgmtHandle(pArray, TDMT_DND_CREATE_SNODE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
2,271!
765
  if (dmSetMgmtHandle(pArray, TDMT_DND_DROP_SNODE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
2,271!
766
  if (dmSetMgmtHandle(pArray, TDMT_DND_CONFIG_DNODE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
2,271!
767
  if (dmSetMgmtHandle(pArray, TDMT_DND_SERVER_STATUS, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
2,271!
768
  if (dmSetMgmtHandle(pArray, TDMT_DND_SYSTABLE_RETRIEVE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
2,271!
769
  if (dmSetMgmtHandle(pArray, TDMT_DND_ALTER_MNODE_TYPE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
2,271!
770
  if (dmSetMgmtHandle(pArray, TDMT_DND_CREATE_ENCRYPT_KEY, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
2,271!
771

772
  // Requests handled by MNODE
773
  if (dmSetMgmtHandle(pArray, TDMT_MND_GRANT, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
2,271!
774
  if (dmSetMgmtHandle(pArray, TDMT_MND_GRANT_NOTIFY, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
2,271!
775
  if (dmSetMgmtHandle(pArray, TDMT_MND_AUTH_RSP, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
2,271!
776

777
  code = 0;
2,271✔
778

779
_OVER:
2,271✔
780
  if (code != 0) {
2,271!
781
    taosArrayDestroy(pArray);
×
782
    return NULL;
×
783
  } else {
784
    return pArray;
2,271✔
785
  }
786
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc