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

taosdata / TDengine / #3831

02 Apr 2025 01:14AM UTC coverage: 34.081% (-0.02%) from 34.097%
#3831

push

travis-ci

happyguoxy
test:alter gcda dir

148596 of 599532 branches covered (24.79%)

Branch coverage included in aggregate %.

222550 of 489473 relevant lines covered (45.47%)

1589752.67 hits per line

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

54.5
/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
SMnodeLoad    tsMLoad = {0};
29
SDnodeData    tsDnodeData = {0};
30

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

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

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

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

87
  (void)dmGetMnodeEpSet(pMgmt->pData, &epset);
×
88

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

95
static void dmMayShouldUpdateAnalFunc(SDnodeMgmt *pMgmt, int64_t newVer) {
230✔
96
  int32_t code = 0;
230✔
97
  int64_t oldVer = taosAnalyGetVersion();
230✔
98
  if (oldVer == newVer) return;
230!
99
  dDebug("analysis on dnode ver:%" PRId64 ", status ver:%" PRId64, oldVer, newVer);
×
100

101
  SRetrieveAnalyticsAlgoReq req = {.dnodeId = pMgmt->pData->dnodeId, .analVer = oldVer};
×
102
  int32_t              contLen = tSerializeRetrieveAnalyticAlgoReq(NULL, 0, &req);
×
103
  if (contLen < 0) {
×
104
    dError("failed to serialize analysis function ver request since %s", tstrerror(contLen));
×
105
    return;
×
106
  }
107

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

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

127
  (void)dmGetMnodeEpSet(pMgmt->pData, &epset);
×
128

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

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

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

169
void dmSendStatusReq(SDnodeMgmt *pMgmt) {
3,903✔
170
  int32_t    code = 0;
3,903✔
171
  SStatusReq req = {0};
3,903✔
172
  req.timestamp = taosGetTimestampMs();
3,903✔
173
  pMgmt->statusSeq++;
3,903✔
174

175
  dDebug("send status req to mnode, statusSeq:%d, begin to mgnt statusInfolock", pMgmt->statusSeq);
3,903✔
176
  if (taosThreadMutexLock(&pMgmt->pData->statusInfolock) != 0) {
3,903!
177
    dError("failed to lock status info lock");
×
178
    return;
15✔
179
  }
180

181
  dDebug("send status req to mnode, statusSeq:%d, begin to get dnode info", pMgmt->statusSeq);
3,903✔
182
  req.sver = tsVersion;
3,903✔
183
  req.dnodeVer = tsDnodeData.dnodeVer;
3,903✔
184
  req.dnodeId = tsDnodeData.dnodeId;
3,903✔
185
  req.clusterId = tsDnodeData.clusterId;
3,903✔
186
  if (req.clusterId == 0) req.dnodeId = 0;
3,903✔
187
  req.rebootTime = tsDnodeData.rebootTime;
3,903✔
188
  req.updateTime = tsDnodeData.updateTime;
3,903✔
189
  req.numOfCores = tsNumOfCores;
3,903✔
190
  req.numOfSupportVnodes = tsNumOfSupportVnodes;
3,903✔
191
  req.numOfDiskCfg = tsDiskCfgNum;
3,903✔
192
  req.memTotal = tsTotalMemoryKB * 1024;
3,903✔
193
  req.memAvail = req.memTotal - tsQueueMemoryAllowed - tsApplyMemoryAllowed - 16 * 1024 * 1024;
3,903✔
194
  tstrncpy(req.dnodeEp, tsLocalEp, TSDB_EP_LEN);
3,903✔
195
  tstrncpy(req.machineId, tsDnodeData.machineId, TSDB_MACHINE_ID_LEN + 1);
3,903✔
196

197
  req.clusterCfg.statusInterval = tsStatusInterval;
3,903✔
198
  req.clusterCfg.checkTime = 0;
3,903✔
199
  req.clusterCfg.ttlChangeOnWrite = tsTtlChangeOnWrite;
3,903✔
200
  req.clusterCfg.enableWhiteList = tsEnableWhiteList ? 1 : 0;
3,903✔
201
  req.clusterCfg.encryptionKeyStat = tsEncryptionKeyStat;
3,903✔
202
  req.clusterCfg.encryptionKeyChksum = tsEncryptionKeyChksum;
3,903✔
203
  req.clusterCfg.monitorParas.tsEnableMonitor = tsEnableMonitor;
3,903✔
204
  req.clusterCfg.monitorParas.tsMonitorInterval = tsMonitorInterval;
3,903✔
205
  req.clusterCfg.monitorParas.tsSlowLogScope = tsSlowLogScope;
3,903✔
206
  req.clusterCfg.monitorParas.tsSlowLogMaxLen = tsSlowLogMaxLen;
3,903✔
207
  req.clusterCfg.monitorParas.tsSlowLogThreshold = tsSlowLogThreshold;
3,903✔
208
  tstrncpy(req.clusterCfg.monitorParas.tsSlowLogExceptDb, tsSlowLogExceptDb, TSDB_DB_NAME_LEN);
3,903✔
209
  char timestr[32] = "1970-01-01 00:00:00.00";
3,903✔
210
  if (taosParseTime(timestr, &req.clusterCfg.checkTime, (int32_t)strlen(timestr), TSDB_TIME_PRECISION_MILLI, NULL) !=
3,903!
211
      0) {
212
    dError("failed to parse time since %s", tstrerror(code));
×
213
  }
214
  memcpy(req.clusterCfg.timezone, tsTimezoneStr, TD_TIMEZONE_LEN);
3,903✔
215
  memcpy(req.clusterCfg.locale, tsLocale, TD_LOCALE_LEN);
3,903✔
216
  memcpy(req.clusterCfg.charset, tsCharset, TD_LOCALE_LEN);
3,903✔
217

218
  dDebug("send status req to mnode, statusSeq:%d, begin to get vnode loads", pMgmt->statusSeq);
3,903✔
219

220
  req.pVloads = tsVinfo.pVloads;
3,903✔
221
  tsVinfo.pVloads = NULL;
3,903✔
222

223
  dDebug("send status req to mnode, statusSeq:%d, begin to get mnode loads", pMgmt->statusSeq);
3,903✔
224
  req.mload = tsMLoad;
3,903✔
225

226
  if (taosThreadMutexUnlock(&pMgmt->pData->statusInfolock) != 0) {
3,903!
227
    dError("failed to unlock status info lock");
×
228
    return;
×
229
  }
230

231
  dDebug("send status req to mnode, statusSeq:%d, begin to get qnode loads", pMgmt->statusSeq);
3,903✔
232
  (*pMgmt->getQnodeLoadsFp)(&req.qload);
3,903✔
233

234
  req.statusSeq = pMgmt->statusSeq;
3,903✔
235
  req.ipWhiteVer = pMgmt->pData->ipWhiteVer;
3,903✔
236
  req.analVer = taosAnalyGetVersion();
3,903✔
237

238
  int32_t contLen = tSerializeSStatusReq(NULL, 0, &req);
3,903✔
239
  if (contLen < 0) {
3,903!
240
    dError("failed to serialize status req since %s", tstrerror(contLen));
×
241
    return;
×
242
  }
243

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

253
  SRpcMsg rpcMsg = {.pCont = pHead,
3,903✔
254
                    .contLen = contLen,
255
                    .msgType = TDMT_MND_STATUS,
256
                    .info.ahandle = 0,
257
                    .info.notFreeAhandle = 1,
258
                    .info.refId = 0,
259
                    .info.noResp = 0,
260
                    .info.handle = 0};
261
  SRpcMsg rpcRsp = {0};
3,903✔
262

263
  dTrace("send status req to mnode, dnodeVer:%" PRId64 " statusSeq:%d", req.dnodeVer, req.statusSeq);
3,903✔
264

265
  SEpSet epSet = {0};
3,903✔
266
  int8_t epUpdated = 0;
3,903✔
267
  (void)dmGetMnodeEpSet(pMgmt->pData, &epSet);
3,903✔
268

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

277
  if (rpcRsp.code != 0) {
3,888!
278
    dmRotateMnodeEpSet(pMgmt->pData);
×
279
    char tbuf[512];
280
    dmEpSetToStr(tbuf, sizeof(tbuf), &epSet);
×
281
    dInfo("Rotate mnode ep set since failed to SendRecv status req %s, epSet:%s, inUse:%d", tstrerror(rpcRsp.code),
×
282
          tbuf, epSet.inUse);
283
  } else {
284
    if (epUpdated == 1) {
3,888✔
285
      dmSetMnodeEpSet(pMgmt->pData, &epSet);
4✔
286
    }
287
  }
288
  dmProcessStatusRsp(pMgmt, &rpcRsp);
3,888✔
289
}
290

291
static void dmProcessConfigRsp(SDnodeMgmt *pMgmt, SRpcMsg *pRsp) {
80✔
292
  const STraceId *trace = &pRsp->info.traceId;
80✔
293
  int32_t         code = 0;
80✔
294
  SConfigRsp      configRsp = {0};
80✔
295
  bool            needStop = false;
80✔
296

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

365
void dmSendConfigReq(SDnodeMgmt *pMgmt) {
80✔
366
  int32_t    code = 0;
80✔
367
  SConfigReq req = {0};
80✔
368

369
  req.cver = tsdmConfigVersion;
80✔
370
  req.forceReadConfig = tsForceReadConfig;
80✔
371
  req.array = taosGetGlobalCfg(tsCfg);
80✔
372
  dDebug("send config req to mnode, configVersion:%d", req.cver);
80✔
373

374
  int32_t contLen = tSerializeSConfigReq(NULL, 0, &req);
80✔
375
  if (contLen < 0) {
80!
376
    dError("failed to serialize status req since %s", tstrerror(contLen));
×
377
    return;
×
378
  }
379

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

392
  SRpcMsg rpcMsg = {.pCont = pHead,
80✔
393
                    .contLen = contLen,
394
                    .msgType = TDMT_MND_CONFIG,
395
                    .info.ahandle = 0,
396
                    .info.notFreeAhandle = 1,
397
                    .info.refId = 0,
398
                    .info.noResp = 0,
399
                    .info.handle = 0};
400
  SRpcMsg rpcRsp = {0};
80✔
401

402
  SEpSet epSet = {0};
80✔
403
  int8_t epUpdated = 0;
80✔
404
  (void)dmGetMnodeEpSet(pMgmt->pData, &epSet);
80✔
405

406
  dDebug("send status req to mnode, statusSeq:%d, begin to send rpc msg", pMgmt->statusSeq);
80✔
407
  code =
408
      rpcSendRecvWithTimeout(pMgmt->msgCb.statusRpc, &epSet, &rpcMsg, &rpcRsp, &epUpdated, tsStatusInterval * 5 * 1000);
80✔
409
  if (code != 0) {
80!
410
    dError("failed to SendRecv config req with timeout %d since %s", tsStatusInterval * 5 * 1000, tstrerror(code));
×
411
    return;
×
412
  }
413
  if (rpcRsp.code != 0) {
80!
414
    dError("failed to send config req since %s", tstrerror(rpcRsp.code));
×
415
    return;
×
416
  }
417
  dmProcessConfigRsp(pMgmt, &rpcRsp);
80✔
418
}
419

420
void dmUpdateStatusInfo(SDnodeMgmt *pMgmt) {
3,999✔
421
  dDebug("begin to get dnode info");
3,999✔
422
  SDnodeData dnodeData = {0};
3,999✔
423
  (void)taosThreadRwlockRdlock(&pMgmt->pData->lock);
3,999✔
424
  dnodeData.dnodeVer = pMgmt->pData->dnodeVer;
3,999✔
425
  dnodeData.dnodeId = pMgmt->pData->dnodeId;
3,999✔
426
  dnodeData.clusterId = pMgmt->pData->clusterId;
3,999✔
427
  dnodeData.rebootTime = pMgmt->pData->rebootTime;
3,999✔
428
  dnodeData.updateTime = pMgmt->pData->updateTime;
3,999✔
429
  tstrncpy(dnodeData.machineId, pMgmt->pData->machineId, TSDB_MACHINE_ID_LEN + 1);
3,999✔
430
  (void)taosThreadRwlockUnlock(&pMgmt->pData->lock);
3,999✔
431

432
  dDebug("begin to get vnode loads");
3,999✔
433
  SMonVloadInfo vinfo = {0};
3,999✔
434
  (*pMgmt->getVnodeLoadsFp)(&vinfo);  // dmGetVnodeLoads
3,999✔
435

436
  dDebug("begin to get mnode loads");
3,999✔
437
  SMonMloadInfo minfo = {0};
3,999✔
438
  (*pMgmt->getMnodeLoadsFp)(&minfo);  // dmGetMnodeLoads
3,999✔
439

440
  dDebug("begin to lock status info");
3,999✔
441
  if (taosThreadMutexLock(&pMgmt->pData->statusInfolock) != 0) {
3,999!
442
    dError("failed to lock status info lock");
×
443
    return;
×
444
  }
445
  tsDnodeData.dnodeVer = dnodeData.dnodeVer;
3,999✔
446
  tsDnodeData.dnodeId = dnodeData.dnodeId;
3,999✔
447
  tsDnodeData.clusterId = dnodeData.clusterId;
3,999✔
448
  tsDnodeData.rebootTime = dnodeData.rebootTime;
3,999✔
449
  tsDnodeData.updateTime = dnodeData.updateTime;
3,999✔
450
  tstrncpy(tsDnodeData.machineId, dnodeData.machineId, TSDB_MACHINE_ID_LEN + 1);
3,999✔
451

452
  if (tsVinfo.pVloads == NULL) {
3,999✔
453
    tsVinfo.pVloads = vinfo.pVloads;
3,835✔
454
    vinfo.pVloads = NULL;
3,835✔
455
  } else {
456
    taosArrayDestroy(vinfo.pVloads);
164✔
457
    vinfo.pVloads = NULL;
164✔
458
  }
459

460
  tsMLoad = minfo.load;
3,999✔
461

462
  if (taosThreadMutexUnlock(&pMgmt->pData->statusInfolock) != 0) {
3,999!
463
    dError("failed to unlock status info lock");
×
464
    return;
×
465
  }
466
}
467

468
void dmSendNotifyReq(SDnodeMgmt *pMgmt, SNotifyReq *pReq) {
×
469
  int32_t contLen = tSerializeSNotifyReq(NULL, 0, pReq);
×
470
  if (contLen < 0) {
×
471
    dError("failed to serialize notify req since %s", tstrerror(contLen));
×
472
    return;
×
473
  }
474
  void *pHead = rpcMallocCont(contLen);
×
475
  contLen = tSerializeSNotifyReq(pHead, contLen, pReq);
×
476
  if (contLen < 0) {
×
477
    rpcFreeCont(pHead);
×
478
    dError("failed to serialize notify req since %s", tstrerror(contLen));
×
479
    return;
×
480
  }
481

482
  SRpcMsg rpcMsg = {.pCont = pHead,
×
483
                    .contLen = contLen,
484
                    .msgType = TDMT_MND_NOTIFY,
485
                    .info.ahandle = 0,
486
                    .info.notFreeAhandle = 1,
487
                    .info.refId = 0,
488
                    .info.noResp = 1,
489
                    .info.handle = 0};
490

491
  SEpSet epSet = {0};
×
492
  dmGetMnodeEpSet(pMgmt->pData, &epSet);
×
493
  if (rpcSendRequest(pMgmt->msgCb.clientRpc, &epSet, &rpcMsg, NULL) != 0) {
×
494
    dError("failed to send notify req");
×
495
  }
496
}
497

498
int32_t dmProcessAuthRsp(SDnodeMgmt *pMgmt, SRpcMsg *pMsg) {
×
499
  dError("auth rsp is received, but not supported yet");
×
500
  return 0;
×
501
}
502

503
int32_t dmProcessGrantRsp(SDnodeMgmt *pMgmt, SRpcMsg *pMsg) {
×
504
  dError("grant rsp is received, but not supported yet");
×
505
  return 0;
×
506
}
507

508
int32_t dmProcessConfigReq(SDnodeMgmt *pMgmt, SRpcMsg *pMsg) {
3✔
509
  int32_t       code = 0;
3✔
510
  SDCfgDnodeReq cfgReq = {0};
3✔
511
  SConfig      *pCfg = taosGetCfg();
3✔
512
  SConfigItem  *pItem = NULL;
3✔
513

514
  if (tDeserializeSDCfgDnodeReq(pMsg->pCont, pMsg->contLen, &cfgReq) != 0) {
3!
515
    return TSDB_CODE_INVALID_MSG;
×
516
  }
517
  if (strcasecmp(cfgReq.config, "dataDir") == 0) {
3!
518
    return taosUpdateTfsItemDisable(pCfg, cfgReq.value, pMgmt->pTfs);
×
519
  }
520

521
  dInfo("start to config, option:%s, value:%s", cfgReq.config, cfgReq.value);
3!
522

523
  code = cfgGetAndSetItem(pCfg, &pItem, cfgReq.config, cfgReq.value, CFG_STYPE_ALTER_SERVER_CMD, true);
3✔
524
  if (code != 0) {
3!
525
    if (strncasecmp(cfgReq.config, "resetlog", strlen("resetlog")) == 0) {
×
526
      TAOS_CHECK_RETURN(taosCfgDynamicOptions(pCfg, cfgReq.config, true));
×
527
      return TSDB_CODE_SUCCESS;
×
528
    } else {
529
      return code;
×
530
    }
531
  }
532
  if (pItem == NULL) {
3!
533
    return TSDB_CODE_CFG_NOT_FOUND;
×
534
  }
535
  if (!isConifgItemLazyMode(pItem)) {
3!
536
    TAOS_CHECK_RETURN(taosCfgDynamicOptions(pCfg, cfgReq.config, true));
3!
537
  }
538

539
  if (pItem->category == CFG_CATEGORY_GLOBAL) {
3!
540
    code = taosPersistGlobalConfig(taosGetGlobalCfg(pCfg), pMgmt->path, tsdmConfigVersion);
×
541
    if (code != TSDB_CODE_SUCCESS) {
×
542
      dError("failed to persist global config since %s", tstrerror(code));
×
543
    }
544
  } else {
545
    code = taosPersistLocalConfig(pMgmt->path);
3✔
546
    if (code != TSDB_CODE_SUCCESS) {
3!
547
      dError("failed to persist local config since %s", tstrerror(code));
×
548
    }
549
  }
550
  if (cfgReq.version > 0) {
3!
551
    tsdmConfigVersion = cfgReq.version;
×
552
  }
553
  return code;
3✔
554
}
555

556
int32_t dmProcessCreateEncryptKeyReq(SDnodeMgmt *pMgmt, SRpcMsg *pMsg) {
×
557
#ifdef TD_ENTERPRISE
558
  int32_t       code = 0;
×
559
  SDCfgDnodeReq cfgReq = {0};
×
560
  if (tDeserializeSDCfgDnodeReq(pMsg->pCont, pMsg->contLen, &cfgReq) != 0) {
×
561
    code = TSDB_CODE_INVALID_MSG;
×
562
    goto _exit;
×
563
  }
564

565
  code = dmUpdateEncryptKey(cfgReq.value, true);
×
566
  if (code == 0) {
×
567
    tsEncryptionKeyChksum = taosCalcChecksum(0, cfgReq.value, strlen(cfgReq.value));
×
568
    tsEncryptionKeyStat = ENCRYPT_KEY_STAT_LOADED;
×
569
    tstrncpy(tsEncryptKey, cfgReq.value, ENCRYPT_KEY_LEN + 1);
×
570
  }
571

572
_exit:
×
573
  pMsg->code = code;
×
574
  pMsg->info.rsp = NULL;
×
575
  pMsg->info.rspLen = 0;
×
576
  return code;
×
577
#else
578
  return 0;
579
#endif
580
}
581

582
static void dmGetServerRunStatus(SDnodeMgmt *pMgmt, SServerStatusRsp *pStatus) {
1✔
583
  pStatus->statusCode = TSDB_SRV_STATUS_SERVICE_OK;
1✔
584
  pStatus->details[0] = 0;
1✔
585

586
  SMonMloadInfo minfo = {0};
1✔
587
  (*pMgmt->getMnodeLoadsFp)(&minfo);
1✔
588
  if (minfo.isMnode &&
1!
589
      (minfo.load.syncState == TAOS_SYNC_STATE_ERROR || minfo.load.syncState == TAOS_SYNC_STATE_OFFLINE)) {
1!
590
    pStatus->statusCode = TSDB_SRV_STATUS_SERVICE_DEGRADED;
×
591
    snprintf(pStatus->details, sizeof(pStatus->details), "mnode sync state is %s", syncStr(minfo.load.syncState));
×
592
    return;
×
593
  }
594

595
  SMonVloadInfo vinfo = {0};
1✔
596
  (*pMgmt->getVnodeLoadsFp)(&vinfo);
1✔
597
  for (int32_t i = 0; i < taosArrayGetSize(vinfo.pVloads); ++i) {
3✔
598
    SVnodeLoad *pLoad = taosArrayGet(vinfo.pVloads, i);
2✔
599
    if (pLoad->syncState == TAOS_SYNC_STATE_ERROR || pLoad->syncState == TAOS_SYNC_STATE_OFFLINE) {
2!
600
      pStatus->statusCode = TSDB_SRV_STATUS_SERVICE_DEGRADED;
×
601
      snprintf(pStatus->details, sizeof(pStatus->details), "vnode:%d sync state is %s", pLoad->vgId,
×
602
               syncStr(pLoad->syncState));
×
603
      break;
×
604
    }
605
  }
606

607
  taosArrayDestroy(vinfo.pVloads);
1✔
608
}
609

610
int32_t dmProcessServerRunStatus(SDnodeMgmt *pMgmt, SRpcMsg *pMsg) {
1✔
611
  int32_t code = 0;
1✔
612
  dDebug("server run status req is received");
1!
613
  SServerStatusRsp statusRsp = {0};
1✔
614
  dmGetServerRunStatus(pMgmt, &statusRsp);
1✔
615

616
  pMsg->info.rsp = NULL;
1✔
617
  pMsg->info.rspLen = 0;
1✔
618

619
  SRpcMsg rspMsg = {.info = pMsg->info};
1✔
620
  int32_t rspLen = tSerializeSServerStatusRsp(NULL, 0, &statusRsp);
1✔
621
  if (rspLen < 0) {
1!
622
    return TSDB_CODE_OUT_OF_MEMORY;
×
623
    // rspMsg.code = TSDB_CODE_OUT_OF_MEMORY;
624
    // return rspMsg.code;
625
  }
626

627
  void *pRsp = rpcMallocCont(rspLen);
1✔
628
  if (pRsp == NULL) {
1!
629
    return terrno;
×
630
    // rspMsg.code = TSDB_CODE_OUT_OF_MEMORY;
631
    // return rspMsg.code;
632
  }
633

634
  rspLen = tSerializeSServerStatusRsp(pRsp, rspLen, &statusRsp);
1✔
635
  if (rspLen < 0) {
1!
636
    return TSDB_CODE_INVALID_MSG;
×
637
  }
638

639
  pMsg->info.rsp = pRsp;
1✔
640
  pMsg->info.rspLen = rspLen;
1✔
641
  return 0;
1✔
642
}
643

644
int32_t dmBuildVariablesBlock(SSDataBlock **ppBlock) {
2✔
645
  int32_t code = 0;
2✔
646

647
  SSDataBlock *pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
2!
648
  if (pBlock == NULL) {
2!
649
    return terrno;
×
650
  }
651

652
  size_t size = 0;
2✔
653

654
  const SSysTableMeta *pMeta = NULL;
2✔
655
  getInfosDbMeta(&pMeta, &size);
2✔
656

657
  int32_t index = 0;
2✔
658
  for (int32_t i = 0; i < size; ++i) {
36!
659
    if (strcmp(pMeta[i].name, TSDB_INS_TABLE_DNODE_VARIABLES) == 0) {
36✔
660
      index = i;
2✔
661
      break;
2✔
662
    }
663
  }
664

665
  pBlock->pDataBlock = taosArrayInit(pMeta[index].colNum, sizeof(SColumnInfoData));
2✔
666
  if (pBlock->pDataBlock == NULL) {
2!
667
    code = terrno;
×
668
    goto _exit;
×
669
  }
670

671
  for (int32_t i = 0; i < pMeta[index].colNum; ++i) {
14✔
672
    SColumnInfoData colInfoData = {0};
12✔
673
    colInfoData.info.colId = i + 1;
12✔
674
    colInfoData.info.type = pMeta[index].schema[i].type;
12✔
675
    colInfoData.info.bytes = pMeta[index].schema[i].bytes;
12✔
676
    if (taosArrayPush(pBlock->pDataBlock, &colInfoData) == NULL) {
24!
677
      code = terrno;
×
678
      goto _exit;
×
679
    }
680
  }
681

682
  pBlock->info.hasVarCol = true;
2✔
683
_exit:
2✔
684
  if (code != 0) {
2!
685
    blockDataDestroy(pBlock);
×
686
  } else {
687
    *ppBlock = pBlock;
2✔
688
  }
689
  return code;
2✔
690
}
691

692
int32_t dmAppendVariablesToBlock(SSDataBlock *pBlock, int32_t dnodeId) {
2✔
693
  int32_t code = dumpConfToDataBlock(pBlock, 1, NULL);
2✔
694
  if (code != 0) {
2!
695
    return code;
×
696
  }
697

698
  SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, 0);
2✔
699
  if (pColInfo == NULL) {
2!
700
    return TSDB_CODE_OUT_OF_RANGE;
×
701
  }
702

703
  return colDataSetNItems(pColInfo, 0, (const char *)&dnodeId, pBlock->info.rows, false);
2✔
704
}
705

706
int32_t dmProcessRetrieve(SDnodeMgmt *pMgmt, SRpcMsg *pMsg) {
2✔
707
  int32_t           size = 0;
2✔
708
  int32_t           rowsRead = 0;
2✔
709
  int32_t           code = 0;
2✔
710
  SRetrieveTableReq retrieveReq = {0};
2✔
711
  if (tDeserializeSRetrieveTableReq(pMsg->pCont, pMsg->contLen, &retrieveReq) != 0) {
2!
712
    return TSDB_CODE_INVALID_MSG;
×
713
  }
714
  dInfo("retrieve table:%s, user:%s, compactId:%" PRId64, retrieveReq.tb, retrieveReq.user, retrieveReq.compactId);
2!
715
#if 0
716
  if (strcmp(retrieveReq.user, TSDB_DEFAULT_USER) != 0) {
717
    code = TSDB_CODE_MND_NO_RIGHTS;
718
    return code;
719
  }
720
#endif
721
  if (strcasecmp(retrieveReq.tb, TSDB_INS_TABLE_DNODE_VARIABLES)) {
2!
722
    return TSDB_CODE_INVALID_MSG;
×
723
  }
724

725
  SSDataBlock *pBlock = NULL;
2✔
726
  if ((code = dmBuildVariablesBlock(&pBlock)) != 0) {
2!
727
    return code;
×
728
  }
729

730
  code = dmAppendVariablesToBlock(pBlock, pMgmt->pData->dnodeId);
2✔
731
  if (code != 0) {
2!
732
    blockDataDestroy(pBlock);
×
733
    return code;
×
734
  }
735

736
  size_t numOfCols = taosArrayGetSize(pBlock->pDataBlock);
2✔
737
  size_t dataEncodeBufSize = blockGetEncodeSize(pBlock);
2✔
738
  size = sizeof(SRetrieveMetaTableRsp) + sizeof(int32_t) + sizeof(SSysTableSchema) * numOfCols + dataEncodeBufSize;
2✔
739

740
  SRetrieveMetaTableRsp *pRsp = rpcMallocCont(size);
2✔
741
  if (pRsp == NULL) {
2!
742
    code = terrno;
×
743
    dError("failed to retrieve data since %s", tstrerror(code));
×
744
    blockDataDestroy(pBlock);
×
745
    return code;
×
746
  }
747

748
  char *pStart = pRsp->data;
2✔
749
  *(int32_t *)pStart = htonl(numOfCols);
2✔
750
  pStart += sizeof(int32_t);  // number of columns
2✔
751

752
  for (int32_t i = 0; i < numOfCols; ++i) {
14✔
753
    SSysTableSchema *pSchema = (SSysTableSchema *)pStart;
12✔
754
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, i);
12✔
755

756
    pSchema->bytes = htonl(pColInfo->info.bytes);
12✔
757
    pSchema->colId = htons(pColInfo->info.colId);
12✔
758
    pSchema->type = pColInfo->info.type;
12✔
759

760
    pStart += sizeof(SSysTableSchema);
12✔
761
  }
762

763
  int32_t len = blockEncode(pBlock, pStart, dataEncodeBufSize, numOfCols);
2✔
764
  if (len < 0) {
2!
765
    dError("failed to retrieve data since %s", tstrerror(code));
×
766
    blockDataDestroy(pBlock);
×
767
    rpcFreeCont(pRsp);
×
768
    return terrno;
×
769
  }
770

771
  pRsp->numOfRows = htonl(pBlock->info.rows);
2✔
772
  pRsp->precision = TSDB_TIME_PRECISION_MILLI;  // millisecond time precision
2✔
773
  pRsp->completed = 1;
2✔
774
  pMsg->info.rsp = pRsp;
2✔
775
  pMsg->info.rspLen = size;
2✔
776
  dDebug("dnode variables retrieve completed");
2!
777

778
  blockDataDestroy(pBlock);
2✔
779
  return TSDB_CODE_SUCCESS;
2✔
780
}
781

782
SArray *dmGetMsgHandles() {
137✔
783
  int32_t code = -1;
137✔
784
  SArray *pArray = taosArrayInit(16, sizeof(SMgmtHandle));
137✔
785
  if (pArray == NULL) {
137!
786
    return NULL;
×
787
  }
788

789
  // Requests handled by DNODE
790
  if (dmSetMgmtHandle(pArray, TDMT_DND_CREATE_MNODE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
137!
791
  if (dmSetMgmtHandle(pArray, TDMT_DND_DROP_MNODE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
137!
792
  if (dmSetMgmtHandle(pArray, TDMT_DND_CREATE_QNODE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
137!
793
  if (dmSetMgmtHandle(pArray, TDMT_DND_DROP_QNODE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
137!
794
  if (dmSetMgmtHandle(pArray, TDMT_DND_CREATE_SNODE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
137!
795
  if (dmSetMgmtHandle(pArray, TDMT_DND_DROP_SNODE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
137!
796
  if (dmSetMgmtHandle(pArray, TDMT_DND_CONFIG_DNODE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
137!
797
  if (dmSetMgmtHandle(pArray, TDMT_DND_SERVER_STATUS, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
137!
798
  if (dmSetMgmtHandle(pArray, TDMT_DND_SYSTABLE_RETRIEVE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
137!
799
  if (dmSetMgmtHandle(pArray, TDMT_DND_ALTER_MNODE_TYPE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
137!
800
  if (dmSetMgmtHandle(pArray, TDMT_DND_CREATE_ENCRYPT_KEY, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
137!
801

802
  // Requests handled by MNODE
803
  if (dmSetMgmtHandle(pArray, TDMT_MND_GRANT, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
137!
804
  if (dmSetMgmtHandle(pArray, TDMT_MND_GRANT_NOTIFY, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
137!
805
  if (dmSetMgmtHandle(pArray, TDMT_MND_AUTH_RSP, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
137!
806

807
  code = 0;
137✔
808

809
_OVER:
137✔
810
  if (code != 0) {
137!
811
    taosArrayDestroy(pArray);
×
812
    return NULL;
×
813
  } else {
814
    return pArray;
137✔
815
  }
816
}
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