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

taosdata / TDengine / #3910

23 Apr 2025 02:47AM UTC coverage: 62.362% (-0.7%) from 63.063%
#3910

push

travis-ci

web-flow
docs(datain): add missing health status types (#30828)

155061 of 317305 branches covered (48.87%)

Branch coverage included in aggregate %.

240172 of 316469 relevant lines covered (75.89%)

6269478.46 hits per line

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

64.79
/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) {
8,473✔
32
  int32_t code = 0;
8,473✔
33
  if (pMgmt->pData->dnodeId == 0 || pMgmt->pData->clusterId == 0) {
8,473✔
34
    dInfo("set local info, dnodeId:%d clusterId:%" PRId64, pCfg->dnodeId, pCfg->clusterId);
1,934!
35
    (void)taosThreadRwlockWrlock(&pMgmt->pData->lock);
1,934✔
36
    pMgmt->pData->dnodeId = pCfg->dnodeId;
1,934✔
37
    pMgmt->pData->clusterId = pCfg->clusterId;
1,934✔
38
    monSetDnodeId(pCfg->dnodeId);
1,934✔
39
    auditSetDnodeId(pCfg->dnodeId);
1,934✔
40
    code = dmWriteEps(pMgmt->pData);
1,934✔
41
    if (code != 0) {
1,934!
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);
1,934✔
46
  }
47
}
8,473✔
48

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

63
  SRetrieveIpWhiteReq req = {.ipWhiteVer = oldVer};
6✔
64
  int32_t             contLen = tSerializeRetrieveIpWhite(NULL, 0, &req);
6✔
65
  if (contLen < 0) {
6!
66
    dError("failed to serialize ip white list request since: %s", tstrerror(contLen));
×
67
    return;
×
68
  }
69
  void *pHead = rpcMallocCont(contLen);
6✔
70
  contLen = tSerializeRetrieveIpWhite(pHead, contLen, &req);
6✔
71
  if (contLen < 0) {
6!
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,
6✔
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};
6✔
86

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

89
  code = rpcSendRequest(pMgmt->msgCb.clientRpc, &epset, &rpcMsg, NULL);
6✔
90
  if (code != 0) {
6!
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) {
8,773✔
96
  int32_t code = 0;
8,773✔
97
  int64_t oldVer = taosAnalyGetVersion();
8,773✔
98
  if (oldVer == newVer) return;
8,773✔
99
  dDebug("analysis on dnode ver:%" PRId64 ", status ver:%" PRId64, oldVer, newVer);
2!
100

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

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

116
  SRpcMsg rpcMsg = {
2✔
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};
2✔
126

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

129
  code = rpcSendRequest(pMgmt->msgCb.clientRpc, &epset, &rpcMsg, NULL);
2✔
130
  if (code != 0) {
2!
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) {
76,279✔
136
  const STraceId *trace = &pRsp->info.traceId;
76,279✔
137
  dGTrace("status rsp received from mnode, statusSeq:%d code:0x%x", pMgmt->statusSeq, pRsp->code);
76,279!
138

139
  if (pRsp->code != 0) {
76,279!
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};
76,279✔
152
    if (pRsp->pCont != NULL && pRsp->contLen > 0 &&
85,052!
153
        tDeserializeSStatusRsp(pRsp->pCont, pRsp->contLen, &statusRsp) == 0) {
8,773✔
154
      if (pMgmt->pData->dnodeVer != statusRsp.dnodeVer) {
8,773✔
155
        dGInfo("status rsp received from mnode, statusSeq:%d:%d dnodeVer:%" PRId64 ":%" PRId64, pMgmt->statusSeq,
8,473!
156
               statusRsp.statusSeq, pMgmt->pData->dnodeVer, statusRsp.dnodeVer);
157
        pMgmt->pData->dnodeVer = statusRsp.dnodeVer;
8,473✔
158
        dmUpdateDnodeCfg(pMgmt, &statusRsp.dnodeCfg);
8,473✔
159
        dmUpdateEps(pMgmt->pData, statusRsp.pDnodeEps);
8,473✔
160
      }
161
      dmMayShouldUpdateIpWhiteList(pMgmt, statusRsp.ipWhiteVer);
8,773✔
162
      dmMayShouldUpdateAnalFunc(pMgmt, statusRsp.analVer);
8,773✔
163
    }
164
    tFreeSStatusRsp(&statusRsp);
76,279✔
165
  }
166
  rpcFreeCont(pRsp->pCont);
76,279✔
167
}
76,279✔
168

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

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

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

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

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

220
  req.pVloads = tsVinfo.pVloads;
76,764✔
221
  tsVinfo.pVloads = NULL;
76,764✔
222

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

226
  if (taosThreadMutexUnlock(&pMgmt->pData->statusInfolock) != 0) {
76,764!
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);
76,764✔
232
  (*pMgmt->getQnodeLoadsFp)(&req.qload);
76,764✔
233

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

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

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

253
  SRpcMsg rpcMsg = {.pCont = pHead,
76,764✔
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};
76,764✔
262

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

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

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

277
  if (rpcRsp.code != 0) {
76,279!
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) {
76,279✔
285
      dmSetMnodeEpSet(pMgmt->pData, &epSet);
695✔
286
    }
287
  }
288
  dmProcessStatusRsp(pMgmt, &rpcRsp);
76,279✔
289
}
290

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

297
  if (pRsp->code != 0) {
2,328!
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;
2,328✔
309
    if (pRsp->pCont != NULL && pRsp->contLen > 0 &&
4,656!
310
        tDeserializeSConfigRsp(pRsp->pCont, pRsp->contLen, &configRsp) == 0) {
2,328✔
311
      // Try to use cfg from mnode sdb.
312
      if (!configRsp.isVersionVerified) {
2,328✔
313
        uInfo("config version not verified, update config");
1,946!
314
        needUpdate = true;
1,946✔
315
        code = taosPersistGlobalConfig(configRsp.array, pMgmt->path, configRsp.cver);
1,946✔
316
        if (code != TSDB_CODE_SUCCESS) {
1,946✔
317
          dError("failed to persist global config since %s", tstrerror(code));
11!
318
          goto _exit;
11✔
319
        }
320
      }
321
    }
322
    if (needUpdate) {
2,317✔
323
      code = cfgUpdateFromArray(tsCfg, configRsp.array);
1,935✔
324
      if (code != TSDB_CODE_SUCCESS) {
1,935!
325
        dError("failed to update config since %s", tstrerror(code));
×
326
        goto _exit;
×
327
      }
328
      code = setAllConfigs(tsCfg);
1,935✔
329
      if (code != TSDB_CODE_SUCCESS) {
1,935✔
330
        dError("failed to set all configs since %s", tstrerror(code));
8!
331
        goto _exit;
8✔
332
      }
333
    }
334
    code = taosPersistLocalConfig(pMgmt->path);
2,309✔
335
    if (code != TSDB_CODE_SUCCESS) {
2,309!
336
      dError("failed to persist local config since %s", tstrerror(code));
×
337
    }
338
    tsConfigInited = 1;
2,309✔
339
  }
340
_exit:
2,328✔
341
  tFreeSConfigRsp(&configRsp);
2,328✔
342
  rpcFreeCont(pRsp->pCont);
2,328✔
343
  if (needStop) {
2,328!
344
    dmStop();
×
345
  }
346
}
2,328✔
347

348
void dmSendConfigReq(SDnodeMgmt *pMgmt) {
2,378✔
349
  int32_t    code = 0;
2,378✔
350
  SConfigReq req = {0};
2,378✔
351

352
  req.cver = tsdmConfigVersion;
2,378✔
353
  req.forceReadConfig = tsForceReadConfig;
2,378✔
354
  req.array = taosGetGlobalCfg(tsCfg);
2,378✔
355
  dDebug("send config req to mnode, configVersion:%d", req.cver);
2,378✔
356

357
  int32_t contLen = tSerializeSConfigReq(NULL, 0, &req);
2,378✔
358
  if (contLen < 0) {
2,378!
359
    dError("failed to serialize status req since %s", tstrerror(contLen));
×
360
    return;
50✔
361
  }
362

363
  void *pHead = rpcMallocCont(contLen);
2,378✔
364
  if (pHead == NULL) {
2,378!
365
    dError("failed to malloc cont since %s", tstrerror(contLen));
×
366
    return;
×
367
  }
368
  contLen = tSerializeSConfigReq(pHead, contLen, &req);
2,378✔
369
  if (contLen < 0) {
2,378!
370
    rpcFreeCont(pHead);
×
371
    dError("failed to serialize status req since %s", tstrerror(contLen));
×
372
    return;
×
373
  }
374

375
  SRpcMsg rpcMsg = {.pCont = pHead,
2,378✔
376
                    .contLen = contLen,
377
                    .msgType = TDMT_MND_CONFIG,
378
                    .info.ahandle = 0,
379
                    .info.notFreeAhandle = 1,
380
                    .info.refId = 0,
381
                    .info.noResp = 0,
382
                    .info.handle = 0};
383
  SRpcMsg rpcRsp = {0};
2,378✔
384

385
  SEpSet epSet = {0};
2,378✔
386
  int8_t epUpdated = 0;
2,378✔
387
  (void)dmGetMnodeEpSet(pMgmt->pData, &epSet);
2,378✔
388

389
  dDebug("send status req to mnode, statusSeq:%d, begin to send rpc msg", pMgmt->statusSeq);
2,378✔
390
  code =
391
      rpcSendRecvWithTimeout(pMgmt->msgCb.statusRpc, &epSet, &rpcMsg, &rpcRsp, &epUpdated, tsStatusInterval * 5 * 1000);
2,378✔
392
  if (code != 0) {
2,378✔
393
    dError("failed to SendRecv config req with timeout %d since %s", tsStatusInterval * 5 * 1000, tstrerror(code));
50!
394
    return;
50✔
395
  }
396
  if (rpcRsp.code != 0) {
2,328!
397
    dError("failed to send config req since %s", tstrerror(rpcRsp.code));
×
398
    return;
×
399
  }
400
  dmProcessConfigRsp(pMgmt, &rpcRsp);
2,328✔
401
}
402

403
void dmUpdateStatusInfo(SDnodeMgmt *pMgmt) {
79,227✔
404
  dDebug("begin to get dnode info");
79,227✔
405
  SDnodeData dnodeData = {0};
79,227✔
406
  (void)taosThreadRwlockRdlock(&pMgmt->pData->lock);
79,227✔
407
  dnodeData.dnodeVer = pMgmt->pData->dnodeVer;
79,227✔
408
  dnodeData.dnodeId = pMgmt->pData->dnodeId;
79,227✔
409
  dnodeData.clusterId = pMgmt->pData->clusterId;
79,227✔
410
  dnodeData.rebootTime = pMgmt->pData->rebootTime;
79,227✔
411
  dnodeData.updateTime = pMgmt->pData->updateTime;
79,227✔
412
  tstrncpy(dnodeData.machineId, pMgmt->pData->machineId, TSDB_MACHINE_ID_LEN + 1);
79,227✔
413
  (void)taosThreadRwlockUnlock(&pMgmt->pData->lock);
79,227✔
414

415
  dDebug("begin to get vnode loads");
79,227✔
416
  SMonVloadInfo vinfo = {0};
79,227✔
417
  (*pMgmt->getVnodeLoadsFp)(&vinfo);  // dmGetVnodeLoads
79,227✔
418

419
  dDebug("begin to get mnode loads");
79,227✔
420
  SMonMloadInfo minfo = {0};
79,227✔
421
  (*pMgmt->getMnodeLoadsFp)(&minfo);  // dmGetMnodeLoads
79,227✔
422

423
  dDebug("begin to lock status info");
79,227✔
424
  if (taosThreadMutexLock(&pMgmt->pData->statusInfolock) != 0) {
79,227!
425
    dError("failed to lock status info lock");
×
426
    return;
×
427
  }
428
  tsDnodeData.dnodeVer = dnodeData.dnodeVer;
79,227✔
429
  tsDnodeData.dnodeId = dnodeData.dnodeId;
79,227✔
430
  tsDnodeData.clusterId = dnodeData.clusterId;
79,227✔
431
  tsDnodeData.rebootTime = dnodeData.rebootTime;
79,227✔
432
  tsDnodeData.updateTime = dnodeData.updateTime;
79,227✔
433
  tstrncpy(tsDnodeData.machineId, dnodeData.machineId, TSDB_MACHINE_ID_LEN + 1);
79,227✔
434

435
  if (tsVinfo.pVloads == NULL) {
79,227✔
436
    tsVinfo.pVloads = vinfo.pVloads;
74,863✔
437
    vinfo.pVloads = NULL;
74,863✔
438
  } else {
439
    taosArrayDestroy(vinfo.pVloads);
4,364✔
440
    vinfo.pVloads = NULL;
4,364✔
441
  }
442

443
  tsMLoad = minfo.load;
79,227✔
444

445
  if (taosThreadMutexUnlock(&pMgmt->pData->statusInfolock) != 0) {
79,227!
446
    dError("failed to unlock status info lock");
×
447
    return;
×
448
  }
449
}
450

451
void dmSendNotifyReq(SDnodeMgmt *pMgmt, SNotifyReq *pReq) {
5✔
452
  int32_t contLen = tSerializeSNotifyReq(NULL, 0, pReq);
5✔
453
  if (contLen < 0) {
5!
454
    dError("failed to serialize notify req since %s", tstrerror(contLen));
×
455
    return;
×
456
  }
457
  void *pHead = rpcMallocCont(contLen);
5✔
458
  contLen = tSerializeSNotifyReq(pHead, contLen, pReq);
5✔
459
  if (contLen < 0) {
5!
460
    rpcFreeCont(pHead);
×
461
    dError("failed to serialize notify req since %s", tstrerror(contLen));
×
462
    return;
×
463
  }
464

465
  SRpcMsg rpcMsg = {.pCont = pHead,
5✔
466
                    .contLen = contLen,
467
                    .msgType = TDMT_MND_NOTIFY,
468
                    .info.ahandle = 0,
469
                    .info.notFreeAhandle = 1,
470
                    .info.refId = 0,
471
                    .info.noResp = 1,
472
                    .info.handle = 0};
473

474
  SEpSet epSet = {0};
5✔
475
  dmGetMnodeEpSet(pMgmt->pData, &epSet);
5✔
476
  if (rpcSendRequest(pMgmt->msgCb.clientRpc, &epSet, &rpcMsg, NULL) != 0) {
5!
477
    dError("failed to send notify req");
×
478
  }
479
}
480

481
int32_t dmProcessAuthRsp(SDnodeMgmt *pMgmt, SRpcMsg *pMsg) {
×
482
  dError("auth rsp is received, but not supported yet");
×
483
  return 0;
×
484
}
485

486
int32_t dmProcessGrantRsp(SDnodeMgmt *pMgmt, SRpcMsg *pMsg) {
×
487
  dError("grant rsp is received, but not supported yet");
×
488
  return 0;
×
489
}
490

491
int32_t dmProcessConfigReq(SDnodeMgmt *pMgmt, SRpcMsg *pMsg) {
518✔
492
  int32_t       code = 0;
518✔
493
  SDCfgDnodeReq cfgReq = {0};
518✔
494
  SConfig      *pCfg = taosGetCfg();
518✔
495
  SConfigItem  *pItem = NULL;
518✔
496

497
  if (tDeserializeSDCfgDnodeReq(pMsg->pCont, pMsg->contLen, &cfgReq) != 0) {
518!
498
    return TSDB_CODE_INVALID_MSG;
×
499
  }
500
  if (strcasecmp(cfgReq.config, "dataDir") == 0) {
518!
501
    return taosUpdateTfsItemDisable(pCfg, cfgReq.value, pMgmt->pTfs);
×
502
  }
503

504
  dInfo("start to config, option:%s, value:%s", cfgReq.config, cfgReq.value);
518!
505

506
  code = cfgGetAndSetItem(pCfg, &pItem, cfgReq.config, cfgReq.value, CFG_STYPE_ALTER_SERVER_CMD, true);
518✔
507
  if (code != 0) {
518✔
508
    if (strncasecmp(cfgReq.config, "resetlog", strlen("resetlog")) == 0) {
1!
509
      TAOS_CHECK_RETURN(taosCfgDynamicOptions(pCfg, cfgReq.config, true));
1!
510
      return TSDB_CODE_SUCCESS;
1✔
511
    } else {
512
      return code;
×
513
    }
514
  }
515
  if (pItem == NULL) {
517!
516
    return TSDB_CODE_CFG_NOT_FOUND;
×
517
  }
518
  if (!isConifgItemLazyMode(pItem)) {
517✔
519
    TAOS_CHECK_RETURN(taosCfgDynamicOptions(pCfg, cfgReq.config, true));
403!
520
  }
521

522
  if (pItem->category == CFG_CATEGORY_GLOBAL) {
517✔
523
    code = taosPersistGlobalConfig(taosGetGlobalCfg(pCfg), pMgmt->path, tsdmConfigVersion);
404✔
524
    if (code != TSDB_CODE_SUCCESS) {
404!
525
      dError("failed to persist global config since %s", tstrerror(code));
×
526
    }
527
  } else {
528
    code = taosPersistLocalConfig(pMgmt->path);
113✔
529
    if (code != TSDB_CODE_SUCCESS) {
113!
530
      dError("failed to persist local config since %s", tstrerror(code));
×
531
    }
532
  }
533
  if (cfgReq.version > 0) {
517✔
534
    tsdmConfigVersion = cfgReq.version;
508✔
535
  }
536
  return code;
517✔
537
}
538

539
int32_t dmProcessCreateEncryptKeyReq(SDnodeMgmt *pMgmt, SRpcMsg *pMsg) {
1✔
540
#ifdef TD_ENTERPRISE
541
  int32_t       code = 0;
1✔
542
  SDCfgDnodeReq cfgReq = {0};
1✔
543
  if (tDeserializeSDCfgDnodeReq(pMsg->pCont, pMsg->contLen, &cfgReq) != 0) {
1!
544
    code = TSDB_CODE_INVALID_MSG;
×
545
    goto _exit;
×
546
  }
547

548
  code = dmUpdateEncryptKey(cfgReq.value, true);
1✔
549
  if (code == 0) {
1!
550
    tsEncryptionKeyChksum = taosCalcChecksum(0, cfgReq.value, strlen(cfgReq.value));
1✔
551
    tsEncryptionKeyStat = ENCRYPT_KEY_STAT_LOADED;
1✔
552
    tstrncpy(tsEncryptKey, cfgReq.value, ENCRYPT_KEY_LEN + 1);
1✔
553
  }
554

555
_exit:
×
556
  pMsg->code = code;
1✔
557
  pMsg->info.rsp = NULL;
1✔
558
  pMsg->info.rspLen = 0;
1✔
559
  return code;
1✔
560
#else
561
  return 0;
562
#endif
563
}
564

565
static void dmGetServerRunStatus(SDnodeMgmt *pMgmt, SServerStatusRsp *pStatus) {
3✔
566
  pStatus->statusCode = TSDB_SRV_STATUS_SERVICE_OK;
3✔
567
  pStatus->details[0] = 0;
3✔
568

569
  SMonMloadInfo minfo = {0};
3✔
570
  (*pMgmt->getMnodeLoadsFp)(&minfo);
3✔
571
  if (minfo.isMnode &&
3!
572
      (minfo.load.syncState == TAOS_SYNC_STATE_ERROR || minfo.load.syncState == TAOS_SYNC_STATE_OFFLINE)) {
3!
573
    pStatus->statusCode = TSDB_SRV_STATUS_SERVICE_DEGRADED;
×
574
    snprintf(pStatus->details, sizeof(pStatus->details), "mnode sync state is %s", syncStr(minfo.load.syncState));
×
575
    return;
×
576
  }
577

578
  SMonVloadInfo vinfo = {0};
3✔
579
  (*pMgmt->getVnodeLoadsFp)(&vinfo);
3✔
580
  for (int32_t i = 0; i < taosArrayGetSize(vinfo.pVloads); ++i) {
9✔
581
    SVnodeLoad *pLoad = taosArrayGet(vinfo.pVloads, i);
6✔
582
    if (pLoad->syncState == TAOS_SYNC_STATE_ERROR || pLoad->syncState == TAOS_SYNC_STATE_OFFLINE) {
6!
583
      pStatus->statusCode = TSDB_SRV_STATUS_SERVICE_DEGRADED;
×
584
      snprintf(pStatus->details, sizeof(pStatus->details), "vnode:%d sync state is %s", pLoad->vgId,
×
585
               syncStr(pLoad->syncState));
×
586
      break;
×
587
    }
588
  }
589

590
  taosArrayDestroy(vinfo.pVloads);
3✔
591
}
592

593
int32_t dmProcessServerRunStatus(SDnodeMgmt *pMgmt, SRpcMsg *pMsg) {
3✔
594
  int32_t code = 0;
3✔
595
  dDebug("server run status req is received");
3!
596
  SServerStatusRsp statusRsp = {0};
3✔
597
  dmGetServerRunStatus(pMgmt, &statusRsp);
3✔
598

599
  pMsg->info.rsp = NULL;
3✔
600
  pMsg->info.rspLen = 0;
3✔
601

602
  SRpcMsg rspMsg = {.info = pMsg->info};
3✔
603
  int32_t rspLen = tSerializeSServerStatusRsp(NULL, 0, &statusRsp);
3✔
604
  if (rspLen < 0) {
3!
605
    return TSDB_CODE_OUT_OF_MEMORY;
×
606
    // rspMsg.code = TSDB_CODE_OUT_OF_MEMORY;
607
    // return rspMsg.code;
608
  }
609

610
  void *pRsp = rpcMallocCont(rspLen);
3✔
611
  if (pRsp == NULL) {
3!
612
    return terrno;
×
613
    // rspMsg.code = TSDB_CODE_OUT_OF_MEMORY;
614
    // return rspMsg.code;
615
  }
616

617
  rspLen = tSerializeSServerStatusRsp(pRsp, rspLen, &statusRsp);
3✔
618
  if (rspLen < 0) {
3!
619
    return TSDB_CODE_INVALID_MSG;
×
620
  }
621

622
  pMsg->info.rsp = pRsp;
3✔
623
  pMsg->info.rspLen = rspLen;
3✔
624
  return 0;
3✔
625
}
626

627
int32_t dmBuildVariablesBlock(SSDataBlock **ppBlock) {
196✔
628
  int32_t code = 0;
196✔
629

630
  SSDataBlock *pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
196!
631
  if (pBlock == NULL) {
196!
632
    return terrno;
×
633
  }
634

635
  size_t size = 0;
196✔
636

637
  const SSysTableMeta *pMeta = NULL;
196✔
638
  getInfosDbMeta(&pMeta, &size);
196✔
639

640
  int32_t index = 0;
196✔
641
  for (int32_t i = 0; i < size; ++i) {
3,528!
642
    if (strcmp(pMeta[i].name, TSDB_INS_TABLE_DNODE_VARIABLES) == 0) {
3,528✔
643
      index = i;
196✔
644
      break;
196✔
645
    }
646
  }
647

648
  pBlock->pDataBlock = taosArrayInit(pMeta[index].colNum, sizeof(SColumnInfoData));
196✔
649
  if (pBlock->pDataBlock == NULL) {
196!
650
    code = terrno;
×
651
    goto _exit;
×
652
  }
653

654
  for (int32_t i = 0; i < pMeta[index].colNum; ++i) {
1,372✔
655
    SColumnInfoData colInfoData = {0};
1,176✔
656
    colInfoData.info.colId = i + 1;
1,176✔
657
    colInfoData.info.type = pMeta[index].schema[i].type;
1,176✔
658
    colInfoData.info.bytes = pMeta[index].schema[i].bytes;
1,176✔
659
    if (taosArrayPush(pBlock->pDataBlock, &colInfoData) == NULL) {
2,352!
660
      code = terrno;
×
661
      goto _exit;
×
662
    }
663
  }
664

665
  pBlock->info.hasVarCol = true;
196✔
666
_exit:
196✔
667
  if (code != 0) {
196!
668
    blockDataDestroy(pBlock);
×
669
  } else {
670
    *ppBlock = pBlock;
196✔
671
  }
672
  return code;
196✔
673
}
674

675
int32_t dmAppendVariablesToBlock(SSDataBlock *pBlock, int32_t dnodeId) {
196✔
676
  int32_t code = dumpConfToDataBlock(pBlock, 1, NULL);
196✔
677
  if (code != 0) {
196!
678
    return code;
×
679
  }
680

681
  SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, 0);
196✔
682
  if (pColInfo == NULL) {
196!
683
    return TSDB_CODE_OUT_OF_RANGE;
×
684
  }
685

686
  return colDataSetNItems(pColInfo, 0, (const char *)&dnodeId, pBlock->info.rows, false);
196✔
687
}
688

689
int32_t dmProcessRetrieve(SDnodeMgmt *pMgmt, SRpcMsg *pMsg) {
196✔
690
  int32_t           size = 0;
196✔
691
  int32_t           rowsRead = 0;
196✔
692
  int32_t           code = 0;
196✔
693
  SRetrieveTableReq retrieveReq = {0};
196✔
694
  if (tDeserializeSRetrieveTableReq(pMsg->pCont, pMsg->contLen, &retrieveReq) != 0) {
196!
695
    return TSDB_CODE_INVALID_MSG;
×
696
  }
697
  dInfo("retrieve table:%s, user:%s, compactId:%" PRId64, retrieveReq.tb, retrieveReq.user, retrieveReq.compactId);
196!
698
#if 0
699
  if (strcmp(retrieveReq.user, TSDB_DEFAULT_USER) != 0) {
700
    code = TSDB_CODE_MND_NO_RIGHTS;
701
    return code;
702
  }
703
#endif
704
  if (strcasecmp(retrieveReq.tb, TSDB_INS_TABLE_DNODE_VARIABLES)) {
196!
705
    return TSDB_CODE_INVALID_MSG;
×
706
  }
707

708
  SSDataBlock *pBlock = NULL;
196✔
709
  if ((code = dmBuildVariablesBlock(&pBlock)) != 0) {
196!
710
    return code;
×
711
  }
712

713
  code = dmAppendVariablesToBlock(pBlock, pMgmt->pData->dnodeId);
196✔
714
  if (code != 0) {
196!
715
    blockDataDestroy(pBlock);
×
716
    return code;
×
717
  }
718

719
  size_t numOfCols = taosArrayGetSize(pBlock->pDataBlock);
196✔
720
  size_t dataEncodeBufSize = blockGetEncodeSize(pBlock);
196✔
721
  size = sizeof(SRetrieveMetaTableRsp) + sizeof(int32_t) + sizeof(SSysTableSchema) * numOfCols + dataEncodeBufSize;
196✔
722

723
  SRetrieveMetaTableRsp *pRsp = rpcMallocCont(size);
196✔
724
  if (pRsp == NULL) {
196!
725
    code = terrno;
×
726
    dError("failed to retrieve data since %s", tstrerror(code));
×
727
    blockDataDestroy(pBlock);
×
728
    return code;
×
729
  }
730

731
  char *pStart = pRsp->data;
196✔
732
  *(int32_t *)pStart = htonl(numOfCols);
196✔
733
  pStart += sizeof(int32_t);  // number of columns
196✔
734

735
  for (int32_t i = 0; i < numOfCols; ++i) {
1,372✔
736
    SSysTableSchema *pSchema = (SSysTableSchema *)pStart;
1,176✔
737
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, i);
1,176✔
738

739
    pSchema->bytes = htonl(pColInfo->info.bytes);
1,176✔
740
    pSchema->colId = htons(pColInfo->info.colId);
1,176✔
741
    pSchema->type = pColInfo->info.type;
1,176✔
742

743
    pStart += sizeof(SSysTableSchema);
1,176✔
744
  }
745

746
  int32_t len = blockEncode(pBlock, pStart, dataEncodeBufSize, numOfCols);
196✔
747
  if (len < 0) {
196!
748
    dError("failed to retrieve data since %s", tstrerror(code));
×
749
    blockDataDestroy(pBlock);
×
750
    rpcFreeCont(pRsp);
×
751
    return terrno;
×
752
  }
753

754
  pRsp->numOfRows = htonl(pBlock->info.rows);
196✔
755
  pRsp->precision = TSDB_TIME_PRECISION_MILLI;  // millisecond time precision
196✔
756
  pRsp->completed = 1;
196✔
757
  pMsg->info.rsp = pRsp;
196✔
758
  pMsg->info.rspLen = size;
196✔
759
  dDebug("dnode variables retrieve completed");
196✔
760

761
  blockDataDestroy(pBlock);
196✔
762
  return TSDB_CODE_SUCCESS;
196✔
763
}
764

765
SArray *dmGetMsgHandles() {
2,386✔
766
  int32_t code = -1;
2,386✔
767
  SArray *pArray = taosArrayInit(16, sizeof(SMgmtHandle));
2,386✔
768
  if (pArray == NULL) {
2,386!
769
    return NULL;
×
770
  }
771

772
  // Requests handled by DNODE
773
  if (dmSetMgmtHandle(pArray, TDMT_DND_CREATE_MNODE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
2,386!
774
  if (dmSetMgmtHandle(pArray, TDMT_DND_DROP_MNODE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
2,386!
775
  if (dmSetMgmtHandle(pArray, TDMT_DND_CREATE_QNODE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
2,386!
776
  if (dmSetMgmtHandle(pArray, TDMT_DND_DROP_QNODE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
2,386!
777
  if (dmSetMgmtHandle(pArray, TDMT_DND_CREATE_SNODE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
2,386!
778
  if (dmSetMgmtHandle(pArray, TDMT_DND_DROP_SNODE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
2,386!
779
  if (dmSetMgmtHandle(pArray, TDMT_DND_CONFIG_DNODE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
2,386!
780
  if (dmSetMgmtHandle(pArray, TDMT_DND_SERVER_STATUS, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
2,386!
781
  if (dmSetMgmtHandle(pArray, TDMT_DND_SYSTABLE_RETRIEVE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
2,386!
782
  if (dmSetMgmtHandle(pArray, TDMT_DND_ALTER_MNODE_TYPE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
2,386!
783
  if (dmSetMgmtHandle(pArray, TDMT_DND_CREATE_ENCRYPT_KEY, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
2,386!
784

785
  // Requests handled by MNODE
786
  if (dmSetMgmtHandle(pArray, TDMT_MND_GRANT, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
2,386!
787
  if (dmSetMgmtHandle(pArray, TDMT_MND_GRANT_NOTIFY, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
2,386!
788
  if (dmSetMgmtHandle(pArray, TDMT_MND_AUTH_RSP, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
2,386!
789

790
  code = 0;
2,386✔
791

792
_OVER:
2,386✔
793
  if (code != 0) {
2,386!
794
    taosArrayDestroy(pArray);
×
795
    return NULL;
×
796
  } else {
797
    return pArray;
2,386✔
798
  }
799
}
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