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

taosdata / TDengine / #3563

21 Dec 2024 05:37AM UTC coverage: 61.045% (+34.4%) from 26.655%
#3563

push

travis-ci

web-flow
Merge pull request #29256 from taosdata/merge/mainto3.0

merge: from main t 3.0

135730 of 287838 branches covered (47.15%)

Branch coverage included in aggregate %.

9 of 28 new or added lines in 5 files covered. (32.14%)

784 existing lines in 21 files now uncovered.

213302 of 283921 relevant lines covered (75.13%)

9176355.9 hits per line

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

58.21
/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) {
2,475✔
30
  int32_t code = 0;
2,475✔
31
  if (pMgmt->pData->dnodeId == 0 || pMgmt->pData->clusterId == 0) {
2,475✔
32
    dInfo("set local info, dnodeId:%d clusterId:%" PRId64, pCfg->dnodeId, pCfg->clusterId);
1,417!
33
    (void)taosThreadRwlockWrlock(&pMgmt->pData->lock);
1,417✔
34
    pMgmt->pData->dnodeId = pCfg->dnodeId;
1,417✔
35
    pMgmt->pData->clusterId = pCfg->clusterId;
1,417✔
36
    monSetDnodeId(pCfg->dnodeId);
1,417✔
37
    auditSetDnodeId(pCfg->dnodeId);
1,417✔
38
    code = dmWriteEps(pMgmt->pData);
1,417✔
39
    if (code != 0) {
1,417✔
40
      dInfo("failed to set local info, dnodeId:%d clusterId:%" PRId64 " reason:%s", pCfg->dnodeId, pCfg->clusterId,
1!
41
            tstrerror(code));
42
    }
43
    (void)taosThreadRwlockUnlock(&pMgmt->pData->lock);
1,417✔
44
  }
45
}
2,475✔
46

47
static void dmMayShouldUpdateIpWhiteList(SDnodeMgmt *pMgmt, int64_t ver) {
2,542✔
48
  int32_t code = 0;
2,542✔
49
  dDebug("ip-white-list on dnode ver: %" PRId64 ", status ver: %" PRId64 "", pMgmt->pData->ipWhiteVer, ver);
2,542✔
50
  if (pMgmt->pData->ipWhiteVer == ver) {
2,542✔
51
    if (ver == 0) {
2,536✔
52
      dDebug("disable ip-white-list on dnode ver: %" PRId64 ", status ver: %" PRId64 "", pMgmt->pData->ipWhiteVer, ver);
2,533✔
53
      if (rpcSetIpWhite(pMgmt->msgCb.serverRpc, NULL) != 0) {
2,533!
54
        dError("failed to disable ip white list on dnode");
×
55
      }
56
    }
57
    return;
2,536✔
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) {
2,542✔
94
  int32_t code = 0;
2,542✔
95
  int64_t oldVer = taosAnalGetVersion();
2,542✔
96
  if (oldVer == newVer) return;
2,542✔
97
  dDebug("analysis on dnode ver:%" PRId64 ", status ver:%" PRId64, oldVer, newVer);
1!
98

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

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

114
  SRpcMsg rpcMsg = {
1✔
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};
1✔
124

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

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

137
  if (pRsp->code != 0) {
76,089!
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};
76,089✔
150
    if (pRsp->pCont != NULL && pRsp->contLen > 0 &&
78,631!
151
        tDeserializeSStatusRsp(pRsp->pCont, pRsp->contLen, &statusRsp) == 0) {
2,542✔
152
      if (pMgmt->pData->dnodeVer != statusRsp.dnodeVer) {
2,542✔
153
        dGInfo("status rsp received from mnode, statusSeq:%d:%d dnodeVer:%" PRId64 ":%" PRId64, pMgmt->statusSeq,
2,475!
154
               statusRsp.statusSeq, pMgmt->pData->dnodeVer, statusRsp.dnodeVer);
155
        pMgmt->pData->dnodeVer = statusRsp.dnodeVer;
2,475✔
156
        dmUpdateDnodeCfg(pMgmt, &statusRsp.dnodeCfg);
2,475✔
157
        dmUpdateEps(pMgmt->pData, statusRsp.pDnodeEps);
2,475✔
158
      }
159
      dmMayShouldUpdateIpWhiteList(pMgmt, statusRsp.ipWhiteVer);
2,542✔
160
      dmMayShouldUpdateAnalFunc(pMgmt, statusRsp.analVer);
2,542✔
161
    }
162
    tFreeSStatusRsp(&statusRsp);
76,089✔
163
  }
164
  rpcFreeCont(pRsp->pCont);
76,089✔
165
}
76,089✔
166

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

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

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

210
  dDebug("send status req to mnode, statusSeq:%d, begin to get vnode loads", pMgmt->statusSeq);
76,389✔
211
  if (taosThreadMutexLock(&pMgmt->pData->statusInfolock) != 0) {
76,389!
212
    dError("failed to lock status info lock");
×
213
    return;
300✔
214
  }
215
  req.pVloads = tsVinfo.pVloads;
76,389✔
216
  tsVinfo.pVloads = NULL;
76,389✔
217
  if (taosThreadMutexUnlock(&pMgmt->pData->statusInfolock) != 0) {
76,389!
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);
76,389✔
223
  SMonMloadInfo minfo = {0};
76,389✔
224
  (*pMgmt->getMnodeLoadsFp)(&minfo);
76,389✔
225
  req.mload = minfo.load;
76,389✔
226

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

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

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

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

250
  SRpcMsg rpcMsg = {.pCont = pHead,
76,389✔
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};
76,389✔
259

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

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

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

274
  if (rpcRsp.code != 0) {
76,089!
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) {
76,089✔
281
      dmSetMnodeEpSet(pMgmt->pData, &epSet);
75✔
282
    }
283
  }
284
  dmProcessStatusRsp(pMgmt, &rpcRsp);
76,089✔
285
}
286

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

293
  if (pRsp->code != 0) {
1,648!
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;
1,648✔
305
    if (pRsp->pCont != NULL && pRsp->contLen > 0 &&
3,296!
306
        tDeserializeSConfigRsp(pRsp->pCont, pRsp->contLen, &configRsp) == 0) {
1,648✔
307
      // Try to use cfg file in current dnode.
308
      if (configRsp.forceReadConfig) {
1,648!
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) {
1,648✔
326
        uInfo("config version not verified, update config");
1,435!
327
        needUpdate = true;
1,435✔
328
        code = taosPersistGlobalConfig(configRsp.array, pMgmt->path, configRsp.cver);
1,435✔
329
        if (code != TSDB_CODE_SUCCESS) {
1,435✔
330
          dError("failed to persist global config since %s", tstrerror(code));
17!
331
          goto _exit;
17✔
332
        }
333
      }
334
    }
335
    if (needUpdate) {
1,631✔
336
      code = cfgUpdateFromArray(tsCfg, configRsp.array);
1,418✔
337
      if (code != TSDB_CODE_SUCCESS) {
1,418!
338
        dError("failed to update config since %s", tstrerror(code));
×
339
        goto _exit;
×
340
      }
341
      code = setAllConfigs(tsCfg);
1,418✔
342
      if (code != TSDB_CODE_SUCCESS) {
1,418✔
343
        dError("failed to set all configs since %s", tstrerror(code));
16!
344
        goto _exit;
16✔
345
      }
346
    }
347
    code = taosPersistLocalConfig(pMgmt->path);
1,615✔
348
    if (code != TSDB_CODE_SUCCESS) {
1,615!
349
      dError("failed to persist local config since %s", tstrerror(code));
×
350
    }
351
    tsConfigInited = 1;
1,615✔
352
  }
353
_exit:
1,648✔
354
  tFreeSConfigRsp(&configRsp);
1,648✔
355
  rpcFreeCont(pRsp->pCont);
1,648✔
356
  if (needStop) {
1,648!
357
    dmStop();
×
358
  }
359
}
1,648✔
360

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

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

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

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

388
  SRpcMsg rpcMsg = {.pCont = pHead,
1,651✔
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};
1,651✔
397

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

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

416
void dmUpdateStatusInfo(SDnodeMgmt *pMgmt) {
77,630✔
417
  SMonVloadInfo vinfo = {0};
77,630✔
418
  dDebug("begin to get vnode loads");
77,630✔
419
  (*pMgmt->getVnodeLoadsFp)(&vinfo);
77,630✔
420
  dDebug("begin to lock status info");
77,630✔
421
  if (taosThreadMutexLock(&pMgmt->pData->statusInfolock) != 0) {
77,630!
422
    dError("failed to lock status info lock");
×
423
    return;
×
424
  }
425
  if (tsVinfo.pVloads == NULL) {
77,630✔
426
    tsVinfo.pVloads = vinfo.pVloads;
75,394✔
427
    vinfo.pVloads = NULL;
75,394✔
428
  } else {
429
    taosArrayDestroy(vinfo.pVloads);
2,236✔
430
    vinfo.pVloads = NULL;
2,236✔
431
  }
432
  if (taosThreadMutexUnlock(&pMgmt->pData->statusInfolock) != 0) {
77,630!
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
extern void    tsdbAlterNumCompactThreads();
479
static int32_t dmAlterMaxCompactTask(const char *value) {
×
480
  int32_t max_compact_tasks;
481
  char   *endptr = NULL;
×
482

483
  max_compact_tasks = taosStr2Int32(value, &endptr, 10);
×
484
  if (endptr == value || endptr[0] != '\0') {
×
485
    return TSDB_CODE_INVALID_MSG;
×
486
  }
487

488
  if (max_compact_tasks != tsNumOfCompactThreads) {
×
489
    dInfo("alter max compact tasks from %d to %d", tsNumOfCompactThreads, max_compact_tasks);
×
490
    tsNumOfCompactThreads = max_compact_tasks;
×
491
#ifdef TD_ENTERPRISE
492
    (void)tsdbAlterNumCompactThreads();
×
493
#endif
494
  }
495

496
  return TSDB_CODE_SUCCESS;
×
497
}
498

499
int32_t dmProcessConfigReq(SDnodeMgmt *pMgmt, SRpcMsg *pMsg) {
521✔
500
  int32_t       code = 0;
521✔
501
  SDCfgDnodeReq cfgReq = {0};
521✔
502
  SConfig      *pCfg = taosGetCfg();
521✔
503
  SConfigItem  *pItem = NULL;
521✔
504

505
  if (tDeserializeSDCfgDnodeReq(pMsg->pCont, pMsg->contLen, &cfgReq) != 0) {
521!
506
    return TSDB_CODE_INVALID_MSG;
×
507
  }
508
  if (strcasecmp(cfgReq.config, "dataDir") == 0) {
521!
NEW
509
    return taosUpdateTfsItemDisable(pCfg, cfgReq.value, pMgmt->pTfs);
×
510
  }
511

512
  if (strncmp(cfgReq.config, tsAlterCompactTaskKeywords, strlen(tsAlterCompactTaskKeywords) + 1) == 0) {
521!
513
    return dmAlterMaxCompactTask(cfgReq.value);
×
514
  }
515

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

518
  code = cfgGetAndSetItem(pCfg, &pItem, cfgReq.config, cfgReq.value, CFG_STYPE_ALTER_SERVER_CMD, true);
521✔
519
  if (code != 0) {
521✔
520
    if (strncasecmp(cfgReq.config, "resetlog", strlen("resetlog")) == 0) {
1!
521
      TAOS_CHECK_RETURN(taosCfgDynamicOptions(pCfg, cfgReq.config, true));
1!
522
      return TSDB_CODE_SUCCESS;
1✔
523
    } else {
524
      return code;
×
525
    }
526
  }
527
  if (pItem == NULL) {
520!
528
    return TSDB_CODE_CFG_NOT_FOUND;
×
529
  }
530
  if (!isConifgItemLazyMode(pItem)) {
520✔
531
    TAOS_CHECK_RETURN(taosCfgDynamicOptions(pCfg, cfgReq.config, true));
406!
532
  }
533

534
  if (pItem->category == CFG_CATEGORY_GLOBAL) {
520✔
535
    code = taosPersistGlobalConfig(taosGetGlobalCfg(pCfg), pMgmt->path, tsdmConfigVersion);
432✔
536
    if (code != TSDB_CODE_SUCCESS) {
432!
537
      dError("failed to persist global config since %s", tstrerror(code));
×
538
    }
539
  } else {
540
    code = taosPersistLocalConfig(pMgmt->path);
88✔
541
    if (code != TSDB_CODE_SUCCESS) {
88!
542
      dError("failed to persist local config since %s", tstrerror(code));
×
543
    }
544
  }
545
  if (cfgReq.version > 0) {
520✔
546
    tsdmConfigVersion = cfgReq.version;
512✔
547
  }
548
  return code;
520✔
549
}
550

551
int32_t dmProcessCreateEncryptKeyReq(SDnodeMgmt *pMgmt, SRpcMsg *pMsg) {
×
552
#ifdef TD_ENTERPRISE
553
  int32_t       code = 0;
×
554
  SDCfgDnodeReq cfgReq = {0};
×
555
  if (tDeserializeSDCfgDnodeReq(pMsg->pCont, pMsg->contLen, &cfgReq) != 0) {
×
556
    code = TSDB_CODE_INVALID_MSG;
×
557
    goto _exit;
×
558
  }
559

560
  code = dmUpdateEncryptKey(cfgReq.value, true);
×
561
  if (code == 0) {
×
562
    tsEncryptionKeyChksum = taosCalcChecksum(0, cfgReq.value, strlen(cfgReq.value));
×
563
    tsEncryptionKeyStat = ENCRYPT_KEY_STAT_LOADED;
×
564
    tstrncpy(tsEncryptKey, cfgReq.value, ENCRYPT_KEY_LEN + 1);
×
565
  }
566

567
_exit:
×
568
  pMsg->code = code;
×
569
  pMsg->info.rsp = NULL;
×
570
  pMsg->info.rspLen = 0;
×
571
  return code;
×
572
#else
573
  return 0;
574
#endif
575
}
576

577
static void dmGetServerRunStatus(SDnodeMgmt *pMgmt, SServerStatusRsp *pStatus) {
3✔
578
  pStatus->statusCode = TSDB_SRV_STATUS_SERVICE_OK;
3✔
579
  pStatus->details[0] = 0;
3✔
580

581
  SMonMloadInfo minfo = {0};
3✔
582
  (*pMgmt->getMnodeLoadsFp)(&minfo);
3✔
583
  if (minfo.isMnode &&
3!
584
      (minfo.load.syncState == TAOS_SYNC_STATE_ERROR || minfo.load.syncState == TAOS_SYNC_STATE_OFFLINE)) {
3!
585
    pStatus->statusCode = TSDB_SRV_STATUS_SERVICE_DEGRADED;
×
586
    snprintf(pStatus->details, sizeof(pStatus->details), "mnode sync state is %s", syncStr(minfo.load.syncState));
×
587
    return;
×
588
  }
589

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

602
  taosArrayDestroy(vinfo.pVloads);
3✔
603
}
604

605
int32_t dmProcessServerRunStatus(SDnodeMgmt *pMgmt, SRpcMsg *pMsg) {
3✔
606
  int32_t code = 0;
3✔
607
  dDebug("server run status req is received");
3!
608
  SServerStatusRsp statusRsp = {0};
3✔
609
  dmGetServerRunStatus(pMgmt, &statusRsp);
3✔
610

611
  pMsg->info.rsp = NULL;
3✔
612
  pMsg->info.rspLen = 0;
3✔
613

614
  SRpcMsg rspMsg = {.info = pMsg->info};
3✔
615
  int32_t rspLen = tSerializeSServerStatusRsp(NULL, 0, &statusRsp);
3✔
616
  if (rspLen < 0) {
3!
617
    return TSDB_CODE_OUT_OF_MEMORY;
×
618
    // rspMsg.code = TSDB_CODE_OUT_OF_MEMORY;
619
    // return rspMsg.code;
620
  }
621

622
  void *pRsp = rpcMallocCont(rspLen);
3✔
623
  if (pRsp == NULL) {
3!
624
    return terrno;
×
625
    // rspMsg.code = TSDB_CODE_OUT_OF_MEMORY;
626
    // return rspMsg.code;
627
  }
628

629
  rspLen = tSerializeSServerStatusRsp(pRsp, rspLen, &statusRsp);
3✔
630
  if (rspLen < 0) {
3!
631
    return TSDB_CODE_INVALID_MSG;
×
632
  }
633

634
  pMsg->info.rsp = pRsp;
3✔
635
  pMsg->info.rspLen = rspLen;
3✔
636
  return 0;
3✔
637
}
638

639
int32_t dmBuildVariablesBlock(SSDataBlock **ppBlock) {
496✔
640
  int32_t code = 0;
496✔
641

642
  SSDataBlock *pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
496!
643
  if (pBlock == NULL) {
496!
644
    return terrno;
×
645
  }
646

647
  size_t size = 0;
496✔
648

649
  const SSysTableMeta *pMeta = NULL;
496✔
650
  getInfosDbMeta(&pMeta, &size);
496✔
651

652
  int32_t index = 0;
496✔
653
  for (int32_t i = 0; i < size; ++i) {
8,928!
654
    if (strcmp(pMeta[i].name, TSDB_INS_TABLE_DNODE_VARIABLES) == 0) {
8,928✔
655
      index = i;
496✔
656
      break;
496✔
657
    }
658
  }
659

660
  pBlock->pDataBlock = taosArrayInit(pMeta[index].colNum, sizeof(SColumnInfoData));
496✔
661
  if (pBlock->pDataBlock == NULL) {
496!
662
    code = terrno;
×
663
    goto _exit;
×
664
  }
665

666
  for (int32_t i = 0; i < pMeta[index].colNum; ++i) {
3,472✔
667
    SColumnInfoData colInfoData = {0};
2,976✔
668
    colInfoData.info.colId = i + 1;
2,976✔
669
    colInfoData.info.type = pMeta[index].schema[i].type;
2,976✔
670
    colInfoData.info.bytes = pMeta[index].schema[i].bytes;
2,976✔
671
    if (taosArrayPush(pBlock->pDataBlock, &colInfoData) == NULL) {
5,952!
672
      code = terrno;
×
673
      goto _exit;
×
674
    }
675
  }
676

677
  pBlock->info.hasVarCol = true;
496✔
678
_exit:
496✔
679
  if (code != 0) {
496!
680
    blockDataDestroy(pBlock);
×
681
  } else {
682
    *ppBlock = pBlock;
496✔
683
  }
684
  return code;
496✔
685
}
686

687
int32_t dmAppendVariablesToBlock(SSDataBlock *pBlock, int32_t dnodeId) {
496✔
688
  int32_t code = dumpConfToDataBlock(pBlock, 1);
496✔
689
  if (code != 0) {
496!
690
    return code;
×
691
  }
692

693
  SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, 0);
496✔
694
  if (pColInfo == NULL) {
496!
695
    return TSDB_CODE_OUT_OF_RANGE;
×
696
  }
697

698
  return colDataSetNItems(pColInfo, 0, (const char *)&dnodeId, pBlock->info.rows, false);
496✔
699
}
700

701
int32_t dmProcessRetrieve(SDnodeMgmt *pMgmt, SRpcMsg *pMsg) {
496✔
702
  int32_t           size = 0;
496✔
703
  int32_t           rowsRead = 0;
496✔
704
  int32_t           code = 0;
496✔
705
  SRetrieveTableReq retrieveReq = {0};
496✔
706
  if (tDeserializeSRetrieveTableReq(pMsg->pCont, pMsg->contLen, &retrieveReq) != 0) {
496!
707
    return TSDB_CODE_INVALID_MSG;
×
708
  }
709
#if 0
710
  if (strcmp(retrieveReq.user, TSDB_DEFAULT_USER) != 0) {
711
    code = TSDB_CODE_MND_NO_RIGHTS;
712
    return code;
713
  }
714
#endif
715
  if (strcasecmp(retrieveReq.tb, TSDB_INS_TABLE_DNODE_VARIABLES)) {
496!
716
    return TSDB_CODE_INVALID_MSG;
×
717
  }
718

719
  SSDataBlock *pBlock = NULL;
496✔
720
  if ((code = dmBuildVariablesBlock(&pBlock)) != 0) {
496!
721
    return code;
×
722
  }
723

724
  code = dmAppendVariablesToBlock(pBlock, pMgmt->pData->dnodeId);
496✔
725
  if (code != 0) {
496!
726
    blockDataDestroy(pBlock);
×
727
    return code;
×
728
  }
729

730
  size_t numOfCols = taosArrayGetSize(pBlock->pDataBlock);
496✔
731
  size_t dataEncodeBufSize = blockGetEncodeSize(pBlock);
496✔
732
  size = sizeof(SRetrieveMetaTableRsp) + sizeof(int32_t) + sizeof(SSysTableSchema) * numOfCols + dataEncodeBufSize;
496✔
733

734
  SRetrieveMetaTableRsp *pRsp = rpcMallocCont(size);
496✔
735
  if (pRsp == NULL) {
496!
736
    code = terrno;
×
737
    dError("failed to retrieve data since %s", tstrerror(code));
×
738
    blockDataDestroy(pBlock);
×
739
    return code;
×
740
  }
741

742
  char *pStart = pRsp->data;
496✔
743
  *(int32_t *)pStart = htonl(numOfCols);
496✔
744
  pStart += sizeof(int32_t);  // number of columns
496✔
745

746
  for (int32_t i = 0; i < numOfCols; ++i) {
3,472✔
747
    SSysTableSchema *pSchema = (SSysTableSchema *)pStart;
2,976✔
748
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, i);
2,976✔
749

750
    pSchema->bytes = htonl(pColInfo->info.bytes);
2,976✔
751
    pSchema->colId = htons(pColInfo->info.colId);
2,976✔
752
    pSchema->type = pColInfo->info.type;
2,976✔
753

754
    pStart += sizeof(SSysTableSchema);
2,976✔
755
  }
756

757
  int32_t len = blockEncode(pBlock, pStart, dataEncodeBufSize, numOfCols);
496✔
758
  if (len < 0) {
496!
759
    dError("failed to retrieve data since %s", tstrerror(code));
×
760
    blockDataDestroy(pBlock);
×
761
    rpcFreeCont(pRsp);
×
762
    return terrno;
×
763
  }
764

765
  pRsp->numOfRows = htonl(pBlock->info.rows);
496✔
766
  pRsp->precision = TSDB_TIME_PRECISION_MILLI;  // millisecond time precision
496✔
767
  pRsp->completed = 1;
496✔
768
  pMsg->info.rsp = pRsp;
496✔
769
  pMsg->info.rspLen = size;
496✔
770
  dDebug("dnode variables retrieve completed");
496!
771

772
  blockDataDestroy(pBlock);
496✔
773
  return TSDB_CODE_SUCCESS;
496✔
774
}
775

776
SArray *dmGetMsgHandles() {
1,717✔
777
  int32_t code = -1;
1,717✔
778
  SArray *pArray = taosArrayInit(16, sizeof(SMgmtHandle));
1,717✔
779
  if (pArray == NULL) {
1,717!
780
    return NULL;
×
781
  }
782

783
  // Requests handled by DNODE
784
  if (dmSetMgmtHandle(pArray, TDMT_DND_CREATE_MNODE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
1,717!
785
  if (dmSetMgmtHandle(pArray, TDMT_DND_DROP_MNODE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
1,717!
786
  if (dmSetMgmtHandle(pArray, TDMT_DND_CREATE_QNODE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
1,717!
787
  if (dmSetMgmtHandle(pArray, TDMT_DND_DROP_QNODE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
1,717!
788
  if (dmSetMgmtHandle(pArray, TDMT_DND_CREATE_SNODE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
1,717!
789
  if (dmSetMgmtHandle(pArray, TDMT_DND_DROP_SNODE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
1,717!
790
  if (dmSetMgmtHandle(pArray, TDMT_DND_CONFIG_DNODE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
1,717!
791
  if (dmSetMgmtHandle(pArray, TDMT_DND_SERVER_STATUS, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
1,717!
792
  if (dmSetMgmtHandle(pArray, TDMT_DND_SYSTABLE_RETRIEVE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
1,717!
793
  if (dmSetMgmtHandle(pArray, TDMT_DND_ALTER_MNODE_TYPE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
1,717!
794
  if (dmSetMgmtHandle(pArray, TDMT_DND_CREATE_ENCRYPT_KEY, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
1,717!
795

796
  // Requests handled by MNODE
797
  if (dmSetMgmtHandle(pArray, TDMT_MND_GRANT, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
1,717!
798
  if (dmSetMgmtHandle(pArray, TDMT_MND_GRANT_NOTIFY, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
1,717!
799
  if (dmSetMgmtHandle(pArray, TDMT_MND_AUTH_RSP, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
1,717!
800

801
  code = 0;
1,717✔
802

803
_OVER:
1,717✔
804
  if (code != 0) {
1,717!
805
    taosArrayDestroy(pArray);
×
806
    return NULL;
×
807
  } else {
808
    return pArray;
1,717✔
809
  }
810
}
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