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

taosdata / TDengine / #4938

23 Jan 2026 09:40AM UTC coverage: 66.8% (+0.006%) from 66.794%
#4938

push

travis-ci

web-flow
fix: case failuer caused by the modification of the error description (#34391)

204187 of 305671 relevant lines covered (66.8%)

124015580.65 hits per line

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

54.3
/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 "dmMgmt.h"
20
#include "crypt.h"
21
#include "monitor.h"
22
#include "stream.h"
23
#include "systable.h"
24
#include "tanalytics.h"
25
#include "tchecksum.h"
26
#include "tencrypt.h"
27
#include "tutil.h"
28

29
#if defined(TD_ENTERPRISE) && defined(TD_HAS_TAOSK)
30
#include "taoskInt.h"
31
#endif
32

33
extern SConfig *tsCfg;
34
extern void setAuditDbNameToken(char *pDb, char *pToken);
35

36
#ifndef TD_ENTERPRISE
37
void setAuditDbNameToken(char *pDb, char *pToken) {}
38
#endif
39

40
extern void getAuditDbNameToken(char *pDb, char *pToken);
41

42
#ifndef TD_ENTERPRISE
43
void getAuditDbNameToken(char *pDb, char *pToken) {}
44
#endif
45

46
SMonVloadInfo tsVinfo = {0};
47
SMnodeLoad    tsMLoad = {0};
48
SDnodeData    tsDnodeData = {0};
49

50
static void dmUpdateDnodeCfg(SDnodeMgmt *pMgmt, SDnodeCfg *pCfg) {
1,732,903✔
51
  int32_t code = 0;
1,732,903✔
52
  if (pMgmt->pData->dnodeId == 0 || pMgmt->pData->clusterId == 0) {
1,732,903✔
53
    dInfo("set local info, dnodeId:%d clusterId:%" PRId64, pCfg->dnodeId, pCfg->clusterId);
420,416✔
54
    (void)taosThreadRwlockWrlock(&pMgmt->pData->lock);
420,416✔
55
    pMgmt->pData->dnodeId = pCfg->dnodeId;
420,416✔
56
    pMgmt->pData->clusterId = pCfg->clusterId;
420,416✔
57
    monSetDnodeId(pCfg->dnodeId);
420,416✔
58
    auditSetDnodeId(pCfg->dnodeId);
420,416✔
59
    code = dmWriteEps(pMgmt->pData);
420,416✔
60
    if (code != 0) {
420,416✔
61
      dInfo("failed to set local info, dnodeId:%d clusterId:0x%" PRIx64 " reason:%s", pCfg->dnodeId, pCfg->clusterId,
202✔
62
            tstrerror(code));
63
    }
64
    (void)taosThreadRwlockUnlock(&pMgmt->pData->lock);
420,416✔
65
  }
66
}
1,732,903✔
67

68
static void dmMayShouldUpdateIpWhiteList(SDnodeMgmt *pMgmt, int64_t ver) {
1,866,015✔
69
  int32_t code = 0;
1,866,015✔
70
  dDebug("ip-white-list on dnode ver: %" PRId64 ", status ver: %" PRId64, pMgmt->pData->ipWhiteVer, ver);
1,866,015✔
71
  if (pMgmt->pData->ipWhiteVer == ver) {
1,866,015✔
72
    if (ver == 0) {
1,865,592✔
73
      dDebug("disable ip-white-list on dnode ver: %" PRId64 ", status ver: %" PRId64, pMgmt->pData->ipWhiteVer, ver);
1,864,782✔
74
      if (rpcSetIpWhite(pMgmt->msgCb.serverRpc, NULL) != 0) {
1,864,782✔
75
        dError("failed to disable ip white list on dnode");
×
76
      }
77
    }
78
    return;
1,865,592✔
79
  }
80
  int64_t oldVer = pMgmt->pData->ipWhiteVer;
423✔
81

82
  SRetrieveWhiteListReq req = {.ver = oldVer};
423✔
83
  int32_t             contLen = tSerializeRetrieveWhiteListReq(NULL, 0, &req);
423✔
84
  if (contLen < 0) {
423✔
85
    dError("failed to serialize ip white list request since: %s", tstrerror(contLen));
×
86
    return;
×
87
  }
88
  void *pHead = rpcMallocCont(contLen);
423✔
89
  contLen = tSerializeRetrieveWhiteListReq(pHead, contLen, &req);
423✔
90
  if (contLen < 0) {
423✔
91
    rpcFreeCont(pHead);
×
92
    dError("failed to serialize ip white list request since:%s", tstrerror(contLen));
×
93
    return;
×
94
  }
95

96
  SRpcMsg rpcMsg = {.pCont = pHead,
423✔
97
                    .contLen = contLen,
98
                    .msgType = TDMT_MND_RETRIEVE_IP_WHITELIST_DUAL,
99
                    .info.ahandle = 0,
100
                    .info.notFreeAhandle = 1,
101
                    .info.refId = 0,
102
                    .info.noResp = 0,
103
                    .info.handle = 0};
104
  SEpSet  epset = {0};
423✔
105

106
  (void)dmGetMnodeEpSet(pMgmt->pData, &epset);
423✔
107

108
  code = rpcSendRequest(pMgmt->msgCb.clientRpc, &epset, &rpcMsg, NULL);
423✔
109
  if (code != 0) {
423✔
110
    dError("failed to send retrieve ip white list request since:%s", tstrerror(code));
×
111
  }
112
}
113

114

115

116
static void dmMayShouldUpdateTimeWhiteList(SDnodeMgmt *pMgmt, int64_t ver) {
1,866,015✔
117
  int32_t code = 0;
1,866,015✔
118
  dDebug("time-white-list on dnode ver: %" PRId64 ", status ver: %" PRId64, pMgmt->pData->timeWhiteVer, ver);
1,866,015✔
119
  if (pMgmt->pData->timeWhiteVer == ver) {
1,866,015✔
120
    if (ver == 0) {
1,865,592✔
121
      dDebug("disable time-white-list on dnode ver: %" PRId64 ", status ver: %" PRId64, pMgmt->pData->timeWhiteVer, ver);
1,864,782✔
122
      if (rpcSetIpWhite(pMgmt->msgCb.serverRpc, NULL) != 0) {
1,864,782✔
123
        dError("failed to disable time white list on dnode");
×
124
      }
125
    }
126
    return;
1,865,592✔
127
  }
128
  int64_t oldVer = pMgmt->pData->timeWhiteVer;
423✔
129

130
  SRetrieveWhiteListReq req = {.ver = oldVer};
423✔
131
  int32_t             contLen = tSerializeRetrieveWhiteListReq(NULL, 0, &req);
423✔
132
  if (contLen < 0) {
423✔
133
    dError("failed to serialize datetime white list request since: %s", tstrerror(contLen));
×
134
    return;
×
135
  }
136
  void *pHead = rpcMallocCont(contLen);
423✔
137
  contLen = tSerializeRetrieveWhiteListReq(pHead, contLen, &req);
423✔
138
  if (contLen < 0) {
423✔
139
    rpcFreeCont(pHead);
×
140
    dError("failed to serialize datetime white list request since:%s", tstrerror(contLen));
×
141
    return;
×
142
  }
143

144
  SRpcMsg rpcMsg = {.pCont = pHead,
423✔
145
                    .contLen = contLen,
146
                    .msgType = TDMT_MND_RETRIEVE_DATETIME_WHITELIST,
147
                    .info.ahandle = 0,
148
                    .info.notFreeAhandle = 1,
149
                    .info.refId = 0,
150
                    .info.noResp = 0,
151
                    .info.handle = 0};
152
  SEpSet  epset = {0};
423✔
153

154
  (void)dmGetMnodeEpSet(pMgmt->pData, &epset);
423✔
155

156
  code = rpcSendRequest(pMgmt->msgCb.clientRpc, &epset, &rpcMsg, NULL);
423✔
157
  if (code != 0) {
423✔
158
    dError("failed to send retrieve datetime white list request since:%s", tstrerror(code));
×
159
  }
160
}
161

162

163

164
static void dmMayShouldUpdateAnalyticsFunc(SDnodeMgmt *pMgmt, int64_t newVer) {
1,866,015✔
165
  int32_t code = 0;
1,866,015✔
166
  int64_t oldVer = taosAnalyGetVersion();
1,866,015✔
167
  if (oldVer == newVer) return;
1,866,015✔
168
  dDebug("analysis on dnode ver:%" PRId64 ", status ver:%" PRId64, oldVer, newVer);
×
169

170
  SRetrieveAnalyticsAlgoReq req = {.dnodeId = pMgmt->pData->dnodeId, .analVer = oldVer};
×
171
  int32_t              contLen = tSerializeRetrieveAnalyticAlgoReq(NULL, 0, &req);
×
172
  if (contLen < 0) {
×
173
    dError("failed to serialize analysis function ver request since %s", tstrerror(contLen));
×
174
    return;
×
175
  }
176

177
  void *pHead = rpcMallocCont(contLen);
×
178
  contLen = tSerializeRetrieveAnalyticAlgoReq(pHead, contLen, &req);
×
179
  if (contLen < 0) {
×
180
    rpcFreeCont(pHead);
×
181
    dError("failed to serialize analysis function ver request since %s", tstrerror(contLen));
×
182
    return;
×
183
  }
184

185
  SRpcMsg rpcMsg = {
×
186
      .pCont = pHead,
187
      .contLen = contLen,
188
      .msgType = TDMT_MND_RETRIEVE_ANAL_ALGO,
189
      .info.ahandle = 0,
190
      .info.refId = 0,
191
      .info.noResp = 0,
192
      .info.handle = 0,
193
  };
194
  SEpSet epset = {0};
×
195

196
  (void)dmGetMnodeEpSet(pMgmt->pData, &epset);
×
197

198
  code = rpcSendRequest(pMgmt->msgCb.clientRpc, &epset, &rpcMsg, NULL);
×
199
  if (code != 0) {
×
200
    dError("failed to send retrieve analysis func ver request since %s", tstrerror(code));
×
201
  }
202
}
203

204
static void dmProcessStatusRsp(SDnodeMgmt *pMgmt, SRpcMsg *pRsp) {
40,144,389✔
205
  const STraceId *trace = &pRsp->info.traceId;
40,144,389✔
206
  dGTrace("status rsp received from mnode, statusSeq:%d code:0x%x", pMgmt->statusSeq, pRsp->code);
40,144,389✔
207

208
  if (pRsp->code != 0) {
40,144,389✔
209
    if (pRsp->code == TSDB_CODE_MND_DNODE_NOT_EXIST && !pMgmt->pData->dropped && pMgmt->pData->dnodeId > 0) {
386,928✔
210
      dGInfo("dnode:%d, set to dropped since not exist in mnode, statusSeq:%d", pMgmt->pData->dnodeId,
247✔
211
             pMgmt->statusSeq);
212
      pMgmt->pData->dropped = 1;
247✔
213
      if (dmWriteEps(pMgmt->pData) != 0) {
247✔
214
        dError("failed to write dnode file");
×
215
      }
216
      dInfo("dnode will exit since it is in the dropped state");
247✔
217
      (void)raise(SIGINT);
247✔
218
    }
219
  } else {
220
    SStatusRsp statusRsp = {0};
39,757,461✔
221
    if (pRsp->pCont != NULL && pRsp->contLen > 0 &&
41,623,476✔
222
        tDeserializeSStatusRsp(pRsp->pCont, pRsp->contLen, &statusRsp) == 0) {
1,866,015✔
223
      if (pMgmt->pData->dnodeVer != statusRsp.dnodeVer) {
1,866,015✔
224
        dGInfo("status rsp received from mnode, statusSeq:%d:%d dnodeVer:%" PRId64 ":%" PRId64, pMgmt->statusSeq,
1,732,903✔
225
               statusRsp.statusSeq, pMgmt->pData->dnodeVer, statusRsp.dnodeVer);
226
        pMgmt->pData->dnodeVer = statusRsp.dnodeVer;
1,732,903✔
227
        dmUpdateDnodeCfg(pMgmt, &statusRsp.dnodeCfg);
1,732,903✔
228
        dmUpdateEps(pMgmt->pData, statusRsp.pDnodeEps);
1,732,903✔
229
      }
230
      setAuditDbNameToken(statusRsp.auditDB, statusRsp.auditToken);
1,866,015✔
231
      dmMayShouldUpdateIpWhiteList(pMgmt, statusRsp.ipWhiteVer);
1,866,015✔
232
      dmMayShouldUpdateTimeWhiteList(pMgmt, statusRsp.timeWhiteVer);
1,866,015✔
233
      dmMayShouldUpdateAnalyticsFunc(pMgmt, statusRsp.analVer);
1,866,015✔
234
    }
235
    tFreeSStatusRsp(&statusRsp);
39,757,461✔
236
  }
237
  rpcFreeCont(pRsp->pCont);
40,144,389✔
238
}
40,144,389✔
239

240
void dmSendStatusReq(SDnodeMgmt *pMgmt) {
40,234,692✔
241
  int32_t    code = 0;
40,234,692✔
242
  SStatusReq req = {0};
40,234,692✔
243
  req.timestamp = taosGetTimestampMs();
40,234,692✔
244
  pMgmt->statusSeq++;
40,234,692✔
245

246
  dTrace("send status req to mnode, begin to mgnt statusInfolock, statusSeq:%d", pMgmt->statusSeq);
40,234,692✔
247
  if (taosThreadMutexLock(&pMgmt->pData->statusInfolock) != 0) {
40,234,692✔
248
    dError("failed to lock status info lock");
×
249
    return;
×
250
  }
251

252
  dTrace("send status req to mnode, begin to get dnode info, statusSeq:%d", pMgmt->statusSeq);
40,234,692✔
253
  req.sver = tsVersion;
40,234,692✔
254
  req.dnodeVer = tsDnodeData.dnodeVer;
40,234,692✔
255
  req.dnodeId = tsDnodeData.dnodeId;
40,234,692✔
256
  req.clusterId = tsDnodeData.clusterId;
40,234,692✔
257
  if (req.clusterId == 0) req.dnodeId = 0;
40,234,692✔
258
  req.rebootTime = tsDnodeData.rebootTime;
40,234,692✔
259
  req.updateTime = tsDnodeData.updateTime;
40,234,692✔
260
  req.numOfCores = tsNumOfCores;
40,234,692✔
261
  req.numOfSupportVnodes = tsNumOfSupportVnodes;
40,234,692✔
262
  req.numOfDiskCfg = tsDiskCfgNum;
40,234,692✔
263
  req.memTotal = tsTotalMemoryKB * 1024;
40,234,692✔
264
  req.memAvail = req.memTotal - tsQueueMemoryAllowed - tsApplyMemoryAllowed - 16 * 1024 * 1024;
40,234,692✔
265
  tstrncpy(req.dnodeEp, tsLocalEp, TSDB_EP_LEN);
40,234,692✔
266
  tstrncpy(req.machineId, tsDnodeData.machineId, TSDB_MACHINE_ID_LEN + 1);
40,234,692✔
267

268
  req.clusterCfg.statusInterval = tsStatusInterval;
40,234,692✔
269
  req.clusterCfg.statusIntervalMs = tsStatusIntervalMs;
40,234,692✔
270
  req.clusterCfg.checkTime = 0;
40,234,692✔
271
  req.clusterCfg.ttlChangeOnWrite = tsTtlChangeOnWrite;
40,234,692✔
272
  req.clusterCfg.enableWhiteList = tsEnableWhiteList ? 1 : 0;
40,234,692✔
273
  req.clusterCfg.encryptionKeyStat = tsEncryptionKeyStat;
40,234,692✔
274
  req.clusterCfg.encryptionKeyChksum = tsEncryptionKeyChksum;
40,234,692✔
275
  req.clusterCfg.monitorParas.tsEnableMonitor = tsEnableMonitor;
40,234,692✔
276
  req.clusterCfg.monitorParas.tsMonitorInterval = tsMonitorInterval;
40,234,692✔
277
  req.clusterCfg.monitorParas.tsSlowLogScope = tsSlowLogScope;
40,234,692✔
278
  req.clusterCfg.monitorParas.tsSlowLogMaxLen = tsSlowLogMaxLen;
40,234,692✔
279
  req.clusterCfg.monitorParas.tsSlowLogThreshold = tsSlowLogThreshold;
40,234,692✔
280
  tstrncpy(req.clusterCfg.monitorParas.tsSlowLogExceptDb, tsSlowLogExceptDb, TSDB_DB_NAME_LEN);
40,234,692✔
281
  char timestr[32] = "1970-01-01 00:00:00.00";
40,234,692✔
282
  if (taosParseTime(timestr, &req.clusterCfg.checkTime, (int32_t)strlen(timestr), TSDB_TIME_PRECISION_MILLI, NULL) !=
40,234,692✔
283
      0) {
284
    dError("failed to parse time since %s", tstrerror(code));
×
285
  }
286
  memcpy(req.clusterCfg.timezone, tsTimezoneStr, TD_TIMEZONE_LEN);
40,234,692✔
287
  memcpy(req.clusterCfg.locale, tsLocale, TD_LOCALE_LEN);
40,234,692✔
288
  memcpy(req.clusterCfg.charset, tsCharset, TD_LOCALE_LEN);
40,234,692✔
289

290
  dTrace("send status req to mnode, begin to get vnode loads, statusSeq:%d", pMgmt->statusSeq);
40,234,692✔
291

292
  req.pVloads = tsVinfo.pVloads;
40,234,692✔
293
  tsVinfo.pVloads = NULL;
40,234,692✔
294

295
  dTrace("send status req to mnode, begin to get mnode loads, statusSeq:%d", pMgmt->statusSeq);
40,234,692✔
296
  req.mload = tsMLoad;
40,234,692✔
297

298
  if (taosThreadMutexUnlock(&pMgmt->pData->statusInfolock) != 0) {
40,234,692✔
299
    dError("failed to unlock status info lock");
×
300
    return;
×
301
  }
302

303
  dTrace("send status req to mnode, begin to get qnode loads, statusSeq:%d", pMgmt->statusSeq);
40,234,692✔
304
  (*pMgmt->getQnodeLoadsFp)(&req.qload);
40,234,692✔
305

306
  req.statusSeq = pMgmt->statusSeq;
40,234,692✔
307
  req.ipWhiteVer = pMgmt->pData->ipWhiteVer;
40,234,692✔
308
  req.analVer = taosAnalyGetVersion();
40,234,692✔
309
  req.timeWhiteVer = pMgmt->pData->timeWhiteVer;
40,234,692✔
310

311
  if (tsAuditUseToken) {
40,234,692✔
312
    getAuditDbNameToken(req.auditDB, req.auditToken);
40,234,692✔
313
  }
314

315
  int32_t contLen = tSerializeSStatusReq(NULL, 0, &req);
40,234,692✔
316
  if (contLen < 0) {
40,234,692✔
317
    dError("failed to serialize status req since %s", tstrerror(contLen));
×
318
    return;
×
319
  }
320

321
  void *pHead = rpcMallocCont(contLen);
40,234,692✔
322
  contLen = tSerializeSStatusReq(pHead, contLen, &req);
40,234,692✔
323
  if (contLen < 0) {
40,234,692✔
324
    rpcFreeCont(pHead);
×
325
    dError("failed to serialize status req since %s", tstrerror(contLen));
×
326
    return;
×
327
  }
328
  tFreeSStatusReq(&req);
40,234,692✔
329

330
  SRpcMsg rpcMsg = {.pCont = pHead,
40,234,692✔
331
                    .contLen = contLen,
332
                    .msgType = TDMT_MND_STATUS,
333
                    .info.ahandle = 0,
334
                    .info.notFreeAhandle = 1,
335
                    .info.refId = 0,
336
                    .info.noResp = 0,
337
                    .info.handle = 0};
338
  SRpcMsg rpcRsp = {0};
40,234,692✔
339

340
  dDebug("send status req to mnode, dnodeVer:%" PRId64 " statusSeq:%d", req.dnodeVer, req.statusSeq);
40,234,692✔
341

342
  SEpSet epSet = {0};
40,234,692✔
343
  int8_t epUpdated = 0;
40,234,692✔
344
  (void)dmGetMnodeEpSet(pMgmt->pData, &epSet);
40,234,692✔
345

346
  if (dDebugFlag & DEBUG_TRACE) {
40,234,692✔
347
    char tbuf[512];
1,129,762✔
348
    dmEpSetToStr(tbuf, sizeof(tbuf), &epSet);
1,129,762✔
349
    dTrace("send status req to mnode, begin to send rpc msg, statusSeq:%d to %s", pMgmt->statusSeq, tbuf);
1,129,762✔
350
  }
351
  code = rpcSendRecvWithTimeout(pMgmt->msgCb.statusRpc, &epSet, &rpcMsg, &rpcRsp, &epUpdated, tsStatusSRTimeoutMs);
40,234,692✔
352
  if (code != 0) {
40,234,692✔
353
    dError("failed to SendRecv status req with timeout %d since %s", tsStatusSRTimeoutMs, tstrerror(code));
90,303✔
354
    if (code == TSDB_CODE_TIMEOUT_ERROR) {
90,303✔
355
      dmRotateMnodeEpSet(pMgmt->pData);
90,303✔
356
      char tbuf[512];
90,303✔
357
      dmEpSetToStr(tbuf, sizeof(tbuf), &epSet);
90,303✔
358
      dInfo("Rotate mnode ep set since failed to SendRecv status req %s, epSet:%s, inUse:%d", tstrerror(rpcRsp.code),
90,303✔
359
            tbuf, epSet.inUse);
360
    }
361
    return;
90,303✔
362
  }
363

364
  if (rpcRsp.code != 0) {
40,144,389✔
365
    dmRotateMnodeEpSet(pMgmt->pData);
386,928✔
366
    char tbuf[512];
386,928✔
367
    dmEpSetToStr(tbuf, sizeof(tbuf), &epSet);
386,928✔
368
    dInfo("Rotate mnode ep set since failed to SendRecv status req %s, epSet:%s, inUse:%d", tstrerror(rpcRsp.code),
386,928✔
369
          tbuf, epSet.inUse);
370
  } else {
371
    if (epUpdated == 1) {
39,757,461✔
372
      dmSetMnodeEpSet(pMgmt->pData, &epSet);
90,331✔
373
    }
374
  }
375
  dmProcessStatusRsp(pMgmt, &rpcRsp);
40,144,389✔
376
}
377

378
static void dmProcessConfigRsp(SDnodeMgmt *pMgmt, SRpcMsg *pRsp) {
544,812✔
379
  const STraceId *trace = &pRsp->info.traceId;
544,812✔
380
  int32_t         code = 0;
544,812✔
381
  SConfigRsp      configRsp = {0};
544,812✔
382
  bool            needStop = false;
544,812✔
383

384
  if (pRsp->code != 0) {
544,812✔
385
    if (pRsp->code == TSDB_CODE_MND_DNODE_NOT_EXIST && !pMgmt->pData->dropped && pMgmt->pData->dnodeId > 0) {
×
386
      dGInfo("dnode:%d, set to dropped since not exist in mnode", pMgmt->pData->dnodeId);
×
387
      pMgmt->pData->dropped = 1;
×
388
      if (dmWriteEps(pMgmt->pData) != 0) {
×
389
        dError("failed to write dnode file");
×
390
      }
391
      dInfo("dnode will exit since it is in the dropped state");
×
392
      (void)raise(SIGINT);
×
393
    }
394
  } else {
395
    bool needUpdate = false;
544,812✔
396
    if (pRsp->pCont != NULL && pRsp->contLen > 0 &&
1,089,624✔
397
        tDeserializeSConfigRsp(pRsp->pCont, pRsp->contLen, &configRsp) == 0) {
544,812✔
398
      // Try to use cfg from mnode sdb.
399
      if (!configRsp.isVersionVerified) {
544,812✔
400
        uInfo("config version not verified, update config");
421,208✔
401
        needUpdate = true;
421,208✔
402
        code = taosPersistGlobalConfig(configRsp.array, pMgmt->path, configRsp.cver);
421,208✔
403
        if (code != TSDB_CODE_SUCCESS) {
421,208✔
404
          dError("failed to persist global config since %s", tstrerror(code));
×
405
          goto _exit;
×
406
        }
407
      }
408
    }
409
    if (needUpdate) {
544,812✔
410
      code = cfgUpdateFromArray(tsCfg, configRsp.array);
421,208✔
411
      if (code != TSDB_CODE_SUCCESS) {
421,208✔
412
        dError("failed to update config since %s", tstrerror(code));
×
413
        goto _exit;
×
414
      }
415
      code = setAllConfigs(tsCfg);
421,208✔
416
      if (code != TSDB_CODE_SUCCESS) {
421,208✔
417
        dError("failed to set all configs since %s", tstrerror(code));
×
418
        goto _exit;
×
419
      }
420
    }
421
    code = taosPersistLocalConfig(pMgmt->path);
544,812✔
422
    if (code != TSDB_CODE_SUCCESS) {
544,812✔
423
      dError("failed to persist local config since %s", tstrerror(code));
×
424
    }
425
    tsConfigInited = 1;
544,812✔
426
  }
427
_exit:
544,812✔
428
  tFreeSConfigRsp(&configRsp);
544,812✔
429
  rpcFreeCont(pRsp->pCont);
544,812✔
430
  if (needStop) {
544,812✔
431
    dmStop();
×
432
  }
433
}
544,812✔
434

435
int32_t dmProcessKeySyncRsp(SDnodeMgmt *pMgmt, SRpcMsg *pRsp) {
523,102✔
436
  const STraceId *trace = &pRsp->info.traceId;
523,102✔
437
  int32_t         code = 0;
523,102✔
438
  SKeySyncRsp     keySyncRsp = {0};
523,102✔
439

440
  if (pRsp->code != 0) {
523,102✔
441
    dError("failed to sync keys from mnode since %s", tstrerror(pRsp->code));
×
442
    code = pRsp->code;
×
443
    goto _exit;
×
444
  }
445

446
  if (pRsp->pCont == NULL || pRsp->contLen <= 0) {
523,102✔
447
    dError("invalid key sync response, empty content");
×
448
    code = TSDB_CODE_INVALID_MSG;
×
449
    goto _exit;
×
450
  }
451

452
  code = tDeserializeSKeySyncRsp(pRsp->pCont, pRsp->contLen, &keySyncRsp);
523,102✔
453
  if (code != 0) {
523,102✔
454
    dError("failed to deserialize key sync response since %s", tstrerror(code));
×
455
    goto _exit;
×
456
  }
457

458
  dInfo("received key sync response, mnode keyVersion:%d, local keyVersion:%d, needUpdate:%d", keySyncRsp.keyVersion,
523,102✔
459
        tsLocalKeyVersion, keySyncRsp.needUpdate);
460
  tsEncryptKeysStatus = keySyncRsp.encryptionKeyStatus;
523,102✔
461
  if (keySyncRsp.needUpdate) {
523,102✔
462
#if defined(TD_ENTERPRISE) && defined(TD_HAS_TAOSK)
463
    // Get encrypt file path from tsDataDir
464
    char masterKeyFile[PATH_MAX] = {0};
1,452✔
465
    char derivedKeyFile[PATH_MAX] = {0};
1,452✔
466
    snprintf(masterKeyFile, sizeof(masterKeyFile), "%s%sdnode%sconfig%smaster.bin", tsDataDir, TD_DIRSEP, TD_DIRSEP,
1,452✔
467
             TD_DIRSEP);
468
    snprintf(derivedKeyFile, sizeof(derivedKeyFile), "%s%sdnode%sconfig%sderived.bin", tsDataDir, TD_DIRSEP, TD_DIRSEP,
1,452✔
469
             TD_DIRSEP);
470

471
    dInfo("updating local encryption keys from mnode, key file is saved in %s and %s, keyVersion:%d -> %d",
1,452✔
472
          masterKeyFile, derivedKeyFile, tsLocalKeyVersion, keySyncRsp.keyVersion);
473

474
    // Save keys to master.bin and derived.bin
475
    // Use the same algorithm for cfg and meta keys (backward compatible)
476
    code = taoskSaveEncryptKeys(masterKeyFile, derivedKeyFile, keySyncRsp.svrKey, keySyncRsp.dbKey, keySyncRsp.cfgKey, keySyncRsp.metaKey,
1,452✔
477
                                keySyncRsp.dataKey, keySyncRsp.algorithm, keySyncRsp.algorithm, keySyncRsp.algorithm,
478
                                keySyncRsp.keyVersion, keySyncRsp.createTime,
479
                                keySyncRsp.svrKeyUpdateTime, keySyncRsp.dbKeyUpdateTime);
480
    if (code != 0) {
1,452✔
481
      dError("failed to save encryption keys since %s", tstrerror(code));
×
482
      goto _exit;
×
483
    }
484

485
    // Update global variables with synced keys
486
    tstrncpy(tsSvrKey, keySyncRsp.svrKey, sizeof(tsSvrKey));
1,452✔
487
    tstrncpy(tsDbKey, keySyncRsp.dbKey, sizeof(tsDbKey));
1,452✔
488
    tstrncpy(tsCfgKey, keySyncRsp.cfgKey, sizeof(tsCfgKey));
1,452✔
489
    tstrncpy(tsMetaKey, keySyncRsp.metaKey, sizeof(tsMetaKey));
1,452✔
490
    tstrncpy(tsDataKey, keySyncRsp.dataKey, sizeof(tsDataKey));
1,452✔
491
    tsEncryptAlgorithmType = keySyncRsp.algorithm;
1,452✔
492
    tsEncryptKeyVersion = keySyncRsp.keyVersion;
1,452✔
493
    tsEncryptKeyCreateTime = keySyncRsp.createTime;
1,452✔
494
    tsSvrKeyUpdateTime = keySyncRsp.svrKeyUpdateTime;
1,452✔
495
    tsDbKeyUpdateTime = keySyncRsp.dbKeyUpdateTime;
1,452✔
496

497
    // Update local key version
498
    tsLocalKeyVersion = keySyncRsp.keyVersion;
1,452✔
499
    dInfo("successfully updated local encryption keys to version:%d", tsLocalKeyVersion);
1,452✔
500

501
    // Encrypt existing plaintext config files
502
    code = taosEncryptExistingCfgFiles(tsDataDir);
1,452✔
503
    if (code != 0) {
1,452✔
504
      dWarn("failed to encrypt existing config files since %s, will retry on next write", tstrerror(code));
726✔
505
      // Don't fail the key sync, files will be encrypted on next write
506
      code = 0;
726✔
507
    }
508
#else
509
    dWarn("enterprise features not enabled, skipping key sync");
510
#endif
511
  } else {
512
    dDebug("local keys are up to date, version:%d", tsLocalKeyVersion);
521,650✔
513
  }
514
  
515
  code = TSDB_CODE_SUCCESS;
523,102✔
516

517
_exit:
523,102✔
518
  rpcFreeCont(pRsp->pCont);
523,102✔
519
  return code;
523,102✔
520
}
521

522
void dmSendKeySyncReq(SDnodeMgmt *pMgmt) {
552,288✔
523
  int32_t     code = 0;
552,288✔
524
  SKeySyncReq req = {0};
552,288✔
525

526
  req.dnodeId = pMgmt->pData->dnodeId;
552,288✔
527
  req.keyVersion = tsLocalKeyVersion;
552,288✔
528
  dDebug("send key sync req to mnode, dnodeId:%d keyVersion:%d", req.dnodeId, req.keyVersion);
552,288✔
529

530
  int32_t contLen = tSerializeSKeySyncReq(NULL, 0, &req);
552,288✔
531
  if (contLen < 0) {
552,288✔
532
    dError("failed to serialize key sync req since %s", tstrerror(contLen));
×
533
    return;
×
534
  }
535

536
  void *pHead = rpcMallocCont(contLen);
552,288✔
537
  if (pHead == NULL) {
552,288✔
538
    dError("failed to malloc cont since %s", tstrerror(contLen));
×
539
    return;
×
540
  }
541
  contLen = tSerializeSKeySyncReq(pHead, contLen, &req);
552,288✔
542
  if (contLen < 0) {
552,288✔
543
    rpcFreeCont(pHead);
×
544
    dError("failed to serialize key sync req since %s", tstrerror(contLen));
×
545
    return;
×
546
  }
547

548
  SRpcMsg rpcMsg = {.pCont = pHead,
552,288✔
549
                    .contLen = contLen,
550
                    .msgType = TDMT_MND_KEY_SYNC,
551
                    .info.ahandle = 0,
552
                    .info.notFreeAhandle = 1,
553
                    .info.refId = 0,
554
                    .info.noResp = 0,
555
                    .info.handle = 0};
556
  SRpcMsg rpcRsp = {0};
552,288✔
557

558
  SEpSet epSet = {0};
552,288✔
559
  int8_t epUpdated = 0;
552,288✔
560
  (void)dmGetMnodeEpSet(pMgmt->pData, &epSet);
552,288✔
561

562
  dDebug("send key sync req to mnode, dnodeId:%d keyVersion:%d, begin to send rpc msg", req.dnodeId, req.keyVersion);
552,288✔
563
  code = rpcSendRecvWithTimeout(pMgmt->msgCb.statusRpc, &epSet, &rpcMsg, &rpcRsp, &epUpdated, tsStatusSRTimeoutMs);
552,288✔
564
  if (code != 0) {
552,288✔
565
    dError("failed to SendRecv key sync req with timeout %d since %s", tsStatusSRTimeoutMs, tstrerror(code));
29,186✔
566
    return;
29,186✔
567
  }
568
  if (rpcRsp.code != 0) {
523,102✔
569
    dError("failed to send key sync req since %s", tstrerror(rpcRsp.code));
×
570
    return;
×
571
  }
572
  code = dmProcessKeySyncRsp(pMgmt, &rpcRsp);
523,102✔
573
  if (code != 0) {
523,102✔
574
    dError("failed to process key sync rsp since %s", tstrerror(code));
×
575
    return;
×
576
  }
577
}
578

579
void dmSendConfigReq(SDnodeMgmt *pMgmt) {
573,779✔
580
  int32_t    code = 0;
573,779✔
581
  SConfigReq req = {0};
573,779✔
582

583
  req.cver = tsdmConfigVersion;
573,779✔
584
  req.forceReadConfig = tsForceReadConfig;
573,779✔
585
  req.array = taosGetGlobalCfg(tsCfg);
573,779✔
586
  dDebug("send config req to mnode, configVersion:%d", req.cver);
573,779✔
587

588
  int32_t contLen = tSerializeSConfigReq(NULL, 0, &req);
573,779✔
589
  if (contLen < 0) {
573,779✔
590
    dError("failed to serialize status req since %s", tstrerror(contLen));
×
591
    return;
×
592
  }
593

594
  void *pHead = rpcMallocCont(contLen);
573,779✔
595
  if (pHead == NULL) {
573,779✔
596
    dError("failed to malloc cont since %s", tstrerror(contLen));
×
597
    return;
×
598
  }
599
  contLen = tSerializeSConfigReq(pHead, contLen, &req);
573,779✔
600
  if (contLen < 0) {
573,779✔
601
    rpcFreeCont(pHead);
×
602
    dError("failed to serialize status req since %s", tstrerror(contLen));
×
603
    return;
×
604
  }
605

606
  SRpcMsg rpcMsg = {.pCont = pHead,
573,779✔
607
                    .contLen = contLen,
608
                    .msgType = TDMT_MND_CONFIG,
609
                    .info.ahandle = 0,
610
                    .info.notFreeAhandle = 1,
611
                    .info.refId = 0,
612
                    .info.noResp = 0,
613
                    .info.handle = 0};
614
  SRpcMsg rpcRsp = {0};
573,779✔
615

616
  SEpSet epSet = {0};
573,779✔
617
  int8_t epUpdated = 0;
573,779✔
618
  (void)dmGetMnodeEpSet(pMgmt->pData, &epSet);
573,779✔
619

620
  dDebug("send config req to mnode, configSeq:%d, begin to send rpc msg", pMgmt->statusSeq);
573,779✔
621
  code = rpcSendRecvWithTimeout(pMgmt->msgCb.statusRpc, &epSet, &rpcMsg, &rpcRsp, &epUpdated, tsStatusSRTimeoutMs);
573,779✔
622
  if (code != 0) {
573,779✔
623
    dError("failed to SendRecv config req with timeout %d since %s", tsStatusSRTimeoutMs, tstrerror(code));
28,967✔
624
    return;
28,967✔
625
  }
626
  if (rpcRsp.code != 0) {
544,812✔
627
    dError("failed to send config req since %s", tstrerror(rpcRsp.code));
×
628
    return;
×
629
  }
630
  dmProcessConfigRsp(pMgmt, &rpcRsp);
544,812✔
631
}
632

633
void dmUpdateStatusInfo(SDnodeMgmt *pMgmt) {
40,827,799✔
634
  dDebug("begin to get dnode info");
40,827,799✔
635
  SDnodeData dnodeData = {0};
40,827,799✔
636
  (void)taosThreadRwlockRdlock(&pMgmt->pData->lock);
40,827,799✔
637
  dnodeData.dnodeVer = pMgmt->pData->dnodeVer;
40,827,799✔
638
  dnodeData.dnodeId = pMgmt->pData->dnodeId;
40,827,799✔
639
  dnodeData.clusterId = pMgmt->pData->clusterId;
40,827,799✔
640
  dnodeData.rebootTime = pMgmt->pData->rebootTime;
40,827,799✔
641
  dnodeData.updateTime = pMgmt->pData->updateTime;
40,827,799✔
642
  tstrncpy(dnodeData.machineId, pMgmt->pData->machineId, TSDB_MACHINE_ID_LEN + 1);
40,827,799✔
643
  (void)taosThreadRwlockUnlock(&pMgmt->pData->lock);
40,827,799✔
644

645
  dDebug("begin to get vnode loads");
40,827,799✔
646
  SMonVloadInfo vinfo = {0};
40,827,799✔
647
  (*pMgmt->getVnodeLoadsFp)(&vinfo);  // dmGetVnodeLoads
40,827,799✔
648

649
  dDebug("begin to get mnode loads");
40,827,799✔
650
  SMonMloadInfo minfo = {0};
40,827,799✔
651
  (*pMgmt->getMnodeLoadsFp)(&minfo);  // dmGetMnodeLoads
40,827,799✔
652

653
  dDebug("begin to lock status info");
40,827,799✔
654
  if (taosThreadMutexLock(&pMgmt->pData->statusInfolock) != 0) {
40,827,799✔
655
    dError("failed to lock status info lock");
×
656
    return;
×
657
  }
658
  tsDnodeData.dnodeVer = dnodeData.dnodeVer;
40,827,799✔
659
  tsDnodeData.dnodeId = dnodeData.dnodeId;
40,827,799✔
660
  tsDnodeData.clusterId = dnodeData.clusterId;
40,827,799✔
661
  tsDnodeData.rebootTime = dnodeData.rebootTime;
40,827,799✔
662
  tsDnodeData.updateTime = dnodeData.updateTime;
40,827,799✔
663
  tstrncpy(tsDnodeData.machineId, dnodeData.machineId, TSDB_MACHINE_ID_LEN + 1);
40,827,799✔
664

665
  if (tsVinfo.pVloads == NULL) {
40,827,799✔
666
    tsVinfo.pVloads = vinfo.pVloads;
39,741,985✔
667
    vinfo.pVloads = NULL;
39,741,985✔
668
  } else {
669
    taosArrayDestroy(vinfo.pVloads);
1,085,814✔
670
    vinfo.pVloads = NULL;
1,085,814✔
671
  }
672

673
  tsMLoad = minfo.load;
40,827,799✔
674

675
  if (taosThreadMutexUnlock(&pMgmt->pData->statusInfolock) != 0) {
40,827,799✔
676
    dError("failed to unlock status info lock");
×
677
    return;
×
678
  }
679
}
680

681
void dmSendNotifyReq(SDnodeMgmt *pMgmt, SNotifyReq *pReq) {
×
682
  int32_t contLen = tSerializeSNotifyReq(NULL, 0, pReq);
×
683
  if (contLen < 0) {
×
684
    dError("failed to serialize notify req since %s", tstrerror(contLen));
×
685
    return;
×
686
  }
687
  void *pHead = rpcMallocCont(contLen);
×
688
  contLen = tSerializeSNotifyReq(pHead, contLen, pReq);
×
689
  if (contLen < 0) {
×
690
    rpcFreeCont(pHead);
×
691
    dError("failed to serialize notify req since %s", tstrerror(contLen));
×
692
    return;
×
693
  }
694

695
  SRpcMsg rpcMsg = {.pCont = pHead,
×
696
                    .contLen = contLen,
697
                    .msgType = TDMT_MND_NOTIFY,
698
                    .info.ahandle = 0,
699
                    .info.notFreeAhandle = 1,
700
                    .info.refId = 0,
701
                    .info.noResp = 1,
702
                    .info.handle = 0};
703

704
  SEpSet epSet = {0};
×
705
  dmGetMnodeEpSet(pMgmt->pData, &epSet);
×
706
  if (rpcSendRequest(pMgmt->msgCb.clientRpc, &epSet, &rpcMsg, NULL) != 0) {
×
707
    dError("failed to send notify req");
×
708
  }
709
}
710

711
int32_t dmProcessAuthRsp(SDnodeMgmt *pMgmt, SRpcMsg *pMsg) {
×
712
  dError("auth rsp is received, but not supported yet");
×
713
  return 0;
×
714
}
715

716
int32_t dmProcessGrantRsp(SDnodeMgmt *pMgmt, SRpcMsg *pMsg) {
×
717
  dError("grant rsp is received, but not supported yet");
×
718
  return 0;
×
719
}
720

721
int32_t dmProcessConfigReq(SDnodeMgmt *pMgmt, SRpcMsg *pMsg) {
70,871✔
722
  int32_t       code = 0;
70,871✔
723
  SDCfgDnodeReq cfgReq = {0};
70,871✔
724
  SConfig      *pCfg = taosGetCfg();
70,871✔
725
  SConfigItem  *pItem = NULL;
70,871✔
726

727
  if (tDeserializeSDCfgDnodeReq(pMsg->pCont, pMsg->contLen, &cfgReq) != 0) {
70,871✔
728
    return TSDB_CODE_INVALID_MSG;
×
729
  }
730
  if (strcasecmp(cfgReq.config, "dataDir") == 0) {
70,871✔
731
    return taosUpdateTfsItemDisable(pCfg, cfgReq.value, pMgmt->pTfs);
3,197✔
732
  }
733

734
  dInfo("start to config, option:%s, value:%s", cfgReq.config, cfgReq.value);
67,674✔
735

736
  code = cfgGetAndSetItem(pCfg, &pItem, cfgReq.config, cfgReq.value, CFG_STYPE_ALTER_SERVER_CMD, true);
67,674✔
737
  if (code != 0) {
67,674✔
738
    if (strncasecmp(cfgReq.config, "resetlog", strlen("resetlog")) == 0) {
193✔
739
      TAOS_CHECK_RETURN(taosCfgDynamicOptions(pCfg, cfgReq.config, true));
193✔
740
      return TSDB_CODE_SUCCESS;
193✔
741
    } else {
742
      return code;
×
743
    }
744
  }
745
  if (pItem == NULL) {
67,481✔
746
    return TSDB_CODE_CFG_NOT_FOUND;
×
747
  }
748

749
  if (taosStrncasecmp(cfgReq.config, "syncTimeout", 128) == 0) {
67,481✔
750
    char value[10] = {0};
×
751
    if (sscanf(cfgReq.value, "%d", &tsSyncTimeout) != 1) {
×
752
      tsSyncTimeout = 0;
×
753
    }
754

755
    if (tsSyncTimeout > 0) {
×
756
      SConfigItem *pItemTmp = NULL;
×
757
      char         tmp[10] = {0};
×
758

759
      sprintf(tmp, "%d", tsSyncTimeout);
×
760
      TAOS_CHECK_RETURN(
×
761
          cfgGetAndSetItem(pCfg, &pItemTmp, "arbSetAssignedTimeoutMs", tmp, CFG_STYPE_ALTER_SERVER_CMD, true));
762
      if (pItemTmp == NULL) {
×
763
        return TSDB_CODE_CFG_NOT_FOUND;
×
764
      }
765

766
      sprintf(tmp, "%d", tsSyncTimeout / 4);
×
767
      TAOS_CHECK_RETURN(
×
768
          cfgGetAndSetItem(pCfg, &pItemTmp, "arbHeartBeatIntervalMs", tmp, CFG_STYPE_ALTER_SERVER_CMD, true));
769
      if (pItemTmp == NULL) {
×
770
        return TSDB_CODE_CFG_NOT_FOUND;
×
771
      }
772
      TAOS_CHECK_RETURN(
×
773
          cfgGetAndSetItem(pCfg, &pItemTmp, "arbCheckSyncIntervalMs", tmp, CFG_STYPE_ALTER_SERVER_CMD, true));
774
      if (pItemTmp == NULL) {
×
775
        return TSDB_CODE_CFG_NOT_FOUND;
×
776
      }
777

778
      sprintf(tmp, "%d", (tsSyncTimeout - tsSyncTimeout / 4) / 2);
×
779
      TAOS_CHECK_RETURN(
×
780
          cfgGetAndSetItem(pCfg, &pItemTmp, "syncVnodeElectIntervalMs", tmp, CFG_STYPE_ALTER_SERVER_CMD, true));
781
      if (pItemTmp == NULL) {
×
782
        return TSDB_CODE_CFG_NOT_FOUND;
×
783
      }
784
      TAOS_CHECK_RETURN(
×
785
          cfgGetAndSetItem(pCfg, &pItemTmp, "syncMnodeElectIntervalMs", tmp, CFG_STYPE_ALTER_SERVER_CMD, true));
786
      if (pItemTmp == NULL) {
×
787
        return TSDB_CODE_CFG_NOT_FOUND;
×
788
      }
789
      TAOS_CHECK_RETURN(cfgGetAndSetItem(pCfg, &pItemTmp, "statusTimeoutMs", tmp, CFG_STYPE_ALTER_SERVER_CMD, true));
×
790
      if (pItemTmp == NULL) {
×
791
        return TSDB_CODE_CFG_NOT_FOUND;
×
792
      }
793

794
      sprintf(tmp, "%d", (tsSyncTimeout - tsSyncTimeout / 4) / 4);
×
795
      TAOS_CHECK_RETURN(cfgGetAndSetItem(pCfg, &pItemTmp, "statusSRTimeoutMs", tmp, CFG_STYPE_ALTER_SERVER_CMD, true));
×
796
      if (pItemTmp == NULL) {
×
797
        return TSDB_CODE_CFG_NOT_FOUND;
×
798
      }
799

800
      sprintf(tmp, "%d", (tsSyncTimeout - tsSyncTimeout / 4) / 8);
×
801
      TAOS_CHECK_RETURN(
×
802
          cfgGetAndSetItem(pCfg, &pItemTmp, "syncVnodeHeartbeatIntervalMs", tmp, CFG_STYPE_ALTER_SERVER_CMD, true));
803
      if (pItemTmp == NULL) {
×
804
        return TSDB_CODE_CFG_NOT_FOUND;
×
805
      }
806
      TAOS_CHECK_RETURN(
×
807
          cfgGetAndSetItem(pCfg, &pItemTmp, "syncMnodeHeartbeatIntervalMs", tmp, CFG_STYPE_ALTER_SERVER_CMD, true));
808
      if (pItemTmp == NULL) {
×
809
        return TSDB_CODE_CFG_NOT_FOUND;
×
810
      }
811
      TAOS_CHECK_RETURN(cfgGetAndSetItem(pCfg, &pItemTmp, "statusIntervalMs", tmp, CFG_STYPE_ALTER_SERVER_CMD, true));
×
812
      if (pItemTmp == NULL) {
×
813
        return TSDB_CODE_CFG_NOT_FOUND;
×
814
      }
815

816
      dInfo("change syncTimeout, GetAndSetItem, option:%s, value:%s, tsSyncTimeout:%d", cfgReq.config, cfgReq.value,
×
817
            tsSyncTimeout);
818
    }
819
  }
820

821
  if (!isConifgItemLazyMode(pItem)) {
67,481✔
822
    TAOS_CHECK_RETURN(taosCfgDynamicOptions(pCfg, cfgReq.config, true));
66,894✔
823

824
    if (taosStrncasecmp(cfgReq.config, "syncTimeout", 128) == 0) {
66,894✔
825
      TAOS_CHECK_RETURN(taosCfgDynamicOptions(pCfg, "arbSetAssignedTimeoutMs", true));
×
826
      TAOS_CHECK_RETURN(taosCfgDynamicOptions(pCfg, "arbHeartBeatIntervalMs", true));
×
827
      TAOS_CHECK_RETURN(taosCfgDynamicOptions(pCfg, "arbCheckSyncIntervalMs", true));
×
828

829
      TAOS_CHECK_RETURN(taosCfgDynamicOptions(pCfg, "syncVnodeElectIntervalMs", true));
×
830
      TAOS_CHECK_RETURN(taosCfgDynamicOptions(pCfg, "syncMnodeElectIntervalMs", true));
×
831
      TAOS_CHECK_RETURN(taosCfgDynamicOptions(pCfg, "syncVnodeHeartbeatIntervalMs", true));
×
832
      TAOS_CHECK_RETURN(taosCfgDynamicOptions(pCfg, "syncMnodeHeartbeatIntervalMs", true));
×
833

834
      TAOS_CHECK_RETURN(taosCfgDynamicOptions(pCfg, "statusTimeoutMs", true));
×
835
      TAOS_CHECK_RETURN(taosCfgDynamicOptions(pCfg, "statusSRTimeoutMs", true));
×
836
      TAOS_CHECK_RETURN(taosCfgDynamicOptions(pCfg, "statusIntervalMs", true));
×
837

838
      dInfo("change syncTimeout, DynamicOptions, option:%s, value:%s, tsSyncTimeout:%d", cfgReq.config, cfgReq.value,
×
839
            tsSyncTimeout);
840
    }
841
  }
842

843
  if (pItem->category == CFG_CATEGORY_GLOBAL) {
67,481✔
844
    code = taosPersistGlobalConfig(taosGetGlobalCfg(pCfg), pMgmt->path, tsdmConfigVersion);
9,595✔
845
    if (code != TSDB_CODE_SUCCESS) {
9,595✔
846
      dError("failed to persist global config since %s", tstrerror(code));
×
847
    }
848
  } else {
849
    code = taosPersistLocalConfig(pMgmt->path);
57,886✔
850
    if (code != TSDB_CODE_SUCCESS) {
57,886✔
851
      dError("failed to persist local config since %s", tstrerror(code));
×
852
    }
853
  }
854

855
  if (taosStrncasecmp(cfgReq.config, "syncTimeout", 128) == 0) {
67,481✔
856
    dInfo("finished change syncTimeout, option:%s, value:%s", cfgReq.config, cfgReq.value);
×
857

858
    (*pMgmt->setMnodeSyncTimeoutFp)();
×
859
    (*pMgmt->setVnodeSyncTimeoutFp)();
×
860
  }
861

862
  if (taosStrncasecmp(cfgReq.config, "syncVnodeElectIntervalMs", 128) == 0 ||
67,481✔
863
      taosStrncasecmp(cfgReq.config, "syncVnodeHeartbeatIntervalMs", 128) == 0) {
67,481✔
864
    (*pMgmt->setVnodeSyncTimeoutFp)();
×
865
  }
866

867
  if (taosStrncasecmp(cfgReq.config, "syncMnodeElectIntervalMs", 128) == 0 ||
67,481✔
868
      taosStrncasecmp(cfgReq.config, "syncMnodeHeartbeatIntervalMs", 128) == 0) {
67,481✔
869
    (*pMgmt->setMnodeSyncTimeoutFp)();
×
870
  }
871

872
  if (cfgReq.version > 0) {
67,481✔
873
    tsdmConfigVersion = cfgReq.version;
15,681✔
874
  }
875
  return code;
67,481✔
876
}
877

878
int32_t dmProcessCreateEncryptKeyReq(SDnodeMgmt *pMgmt, SRpcMsg *pMsg) {
163✔
879
#ifdef TD_ENTERPRISE
880
  int32_t       code = 0;
163✔
881
  SDCfgDnodeReq cfgReq = {0};
163✔
882
  if (tDeserializeSDCfgDnodeReq(pMsg->pCont, pMsg->contLen, &cfgReq) != 0) {
163✔
883
    code = TSDB_CODE_INVALID_MSG;
×
884
    goto _exit;
×
885
  }
886

887
  code = dmUpdateEncryptKey(cfgReq.value, true);
163✔
888
  if (code == 0) {
163✔
889
    tsEncryptionKeyChksum = taosCalcChecksum(0, cfgReq.value, strlen(cfgReq.value));
163✔
890
    tsEncryptionKeyStat = ENCRYPT_KEY_STAT_LOADED;
163✔
891
    tstrncpy(tsEncryptKey, cfgReq.value, ENCRYPT_KEY_LEN + 1);
163✔
892
  }
893

894
_exit:
163✔
895
  pMsg->code = code;
163✔
896
  pMsg->info.rsp = NULL;
163✔
897
  pMsg->info.rspLen = 0;
163✔
898
  return code;
163✔
899
#else
900
  return 0;
901
#endif
902
}
903

904
#if defined(TD_ENTERPRISE) && defined(TD_HAS_TAOSK)
905
// Verification plaintext used to validate encryption keys
906
#define KEY_VERIFY_PLAINTEXT "TDengine_Encryption_Key_Verification_v1.0"
907

908
// Save key verification file with encrypted plaintext for each key
909
static int32_t dmSaveKeyVerification(const char *svrKey, const char *dbKey, const char *cfgKey, const char *metaKey,
786✔
910
                                     const char *dataKey, int32_t algorithm, int32_t cfgAlgorithm,
911
                                     int32_t metaAlgorithm) {
912
  char    verifyFile[PATH_MAX] = {0};
786✔
913
  int32_t nBytes = snprintf(verifyFile, sizeof(verifyFile), "%s%sdnode%sconfig%skey_verify.dat", tsDataDir, TD_DIRSEP,
786✔
914
                            TD_DIRSEP, TD_DIRSEP);
915
  if (nBytes <= 0 || nBytes >= sizeof(verifyFile)) {
786✔
916
    dError("failed to build key verification file path");
×
917
    return TSDB_CODE_OUT_OF_BUFFER;
×
918
  }
919

920
  int32_t     code = 0;
786✔
921
  const char *plaintext = KEY_VERIFY_PLAINTEXT;
786✔
922
  int32_t     plaintextLen = strlen(plaintext);
786✔
923

924
  // Array of keys and their algorithms
925
  const char *keys[] = {svrKey, dbKey, cfgKey, metaKey, dataKey};
786✔
926
  int32_t     algorithms[] = {algorithm, algorithm, cfgAlgorithm, metaAlgorithm, algorithm};
786✔
927
  const char *keyNames[] = {"SVR_KEY", "DB_KEY", "CFG_KEY", "META_KEY", "DATA_KEY"};
786✔
928

929
  // Calculate total buffer size
930
  int32_t encryptedLen = ((plaintextLen + 15) / 16) * 16;                 // Padded length for CBC
786✔
931
  int32_t headerSize = sizeof(uint32_t) + sizeof(uint16_t);               // magic + version
786✔
932
  int32_t perKeySize = sizeof(int32_t) + sizeof(int32_t) + encryptedLen;  // algo + len + encrypted data
786✔
933
  int32_t totalSize = headerSize + perKeySize * 5;
786✔
934

935
  // Allocate buffer for all data
936
  char *buffer = taosMemoryMalloc(totalSize);
786✔
937
  if (buffer == NULL) {
786✔
938
    dError("failed to allocate memory for key verification buffer");
×
939
    return terrno;
×
940
  }
941

942
  char *ptr = buffer;
786✔
943

944
  // Write magic number and version to buffer
945
  uint32_t magic = 0x544B5659;  // "TKVY" in hex
786✔
946
  uint16_t version = 1;
786✔
947
  memcpy(ptr, &magic, sizeof(magic));
786✔
948
  ptr += sizeof(magic);
786✔
949
  memcpy(ptr, &version, sizeof(version));
786✔
950
  ptr += sizeof(version);
786✔
951

952
  // Encrypt all keys and write to buffer
953
  char paddedPlaintext[512] = {0};
786✔
954
  memcpy(paddedPlaintext, plaintext, plaintextLen);
786✔
955

956
  for (int i = 0; i < 5; i++) {
4,716✔
957
    char encrypted[512] = {0};
3,930✔
958

959
    // Encrypt the verification plaintext with this key using CBC
960
    SCryptOpts opts = {0};
3,930✔
961
    opts.len = encryptedLen;
3,930✔
962
    opts.source = paddedPlaintext;
3,930✔
963
    opts.result = encrypted;
3,930✔
964
    opts.unitLen = 16;
3,930✔
965
    opts.pOsslAlgrName =
3,930✔
966
        (algorithms[i] == TSDB_ENCRYPT_ALGO_SM4) ? TSDB_ENCRYPT_ALGO_SM4_STR : TSDB_ENCRYPT_ALGO_NONE_STR;
3,930✔
967
    tstrncpy(opts.key, keys[i], sizeof(opts.key));
3,930✔
968

969
    int32_t count = CBC_Encrypt(&opts);
3,930✔
970
    if (count != opts.len) {
3,930✔
971
      code = terrno ? terrno : TSDB_CODE_FAILED;
×
972
      dError("failed to encrypt verification for %s, count=%d, expected=%d, since %s", keyNames[i], count, opts.len,
×
973
             tstrerror(code));
974
      taosMemoryFree(buffer);
×
975
      return code;
×
976
    }
977

978
    // Write to buffer: algorithm + encrypted length + encrypted data
979
    memcpy(ptr, &algorithms[i], sizeof(int32_t));
3,930✔
980
    ptr += sizeof(int32_t);
3,930✔
981
    memcpy(ptr, &encryptedLen, sizeof(int32_t));
3,930✔
982
    ptr += sizeof(int32_t);
3,930✔
983
    memcpy(ptr, encrypted, encryptedLen);
3,930✔
984
    ptr += encryptedLen;
3,930✔
985

986
    dDebug("prepared verification for %s: algorithm=%d, encLen=%d", keyNames[i], algorithms[i], encryptedLen);
3,930✔
987
  }
988

989
  // Write all data to file in one operation
990
  TdFilePtr pFile = taosOpenFile(verifyFile, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC | TD_FILE_WRITE_THROUGH);
786✔
991
  if (pFile == NULL) {
786✔
992
    dError("failed to create key verification file:%s, errno:%d", verifyFile, errno);
×
993
    taosMemoryFree(buffer);
×
994
    return TSDB_CODE_FILE_CORRUPTED;
×
995
  }
996

997
  int64_t written = taosWriteFile(pFile, buffer, totalSize);
786✔
998
  (void)taosCloseFile(&pFile);
786✔
999
  taosMemoryFree(buffer);
786✔
1000

1001
  if (written != totalSize) {
786✔
1002
    dError("failed to write key verification file, written=%" PRId64 ", expected=%d", written, totalSize);
×
1003
    return TSDB_CODE_FILE_CORRUPTED;
×
1004
  }
1005

1006
  dInfo("successfully saved key verification file:%s, size=%d", verifyFile, totalSize);
786✔
1007
  return 0;
786✔
1008
}
1009

1010
// Verify all encryption keys by decrypting and comparing with original plaintext
1011
static int32_t dmVerifyEncryptionKeys(const char *svrKey, const char *dbKey, const char *cfgKey, const char *metaKey,
786✔
1012
                                      const char *dataKey, int32_t algorithm, int32_t cfgAlgorithm,
1013
                                      int32_t metaAlgorithm) {
1014
  char    verifyFile[PATH_MAX] = {0};
786✔
1015
  int32_t nBytes = snprintf(verifyFile, sizeof(verifyFile), "%s%sdnode%sconfig%skey_verify.dat", tsDataDir, TD_DIRSEP,
786✔
1016
                            TD_DIRSEP, TD_DIRSEP);
1017
  if (nBytes <= 0 || nBytes >= sizeof(verifyFile)) {
786✔
1018
    dError("failed to build key verification file path");
×
1019
    return TSDB_CODE_OUT_OF_BUFFER;
×
1020
  }
1021

1022
  // Get file size
1023
  int64_t fileSize = 0;
786✔
1024
  if (taosStatFile(verifyFile, &fileSize, NULL, NULL) < 0) {
786✔
1025
    // File doesn't exist, create it with current keys
1026
    dInfo("key verification file not found, creating new one");
786✔
1027
    return dmSaveKeyVerification(svrKey, dbKey, cfgKey, metaKey, dataKey, algorithm, cfgAlgorithm, metaAlgorithm);
786✔
1028
  }
1029

1030
  if (fileSize <= 0 || fileSize > 10240) {  // Max 10KB
×
1031
    dError("invalid key verification file size: %" PRId64, fileSize);
×
1032
    return TSDB_CODE_FILE_CORRUPTED;
×
1033
  }
1034

1035
  // Allocate buffer and read entire file
1036
  char *buffer = taosMemoryMalloc(fileSize);
×
1037
  if (buffer == NULL) {
×
1038
    dError("failed to allocate memory for key verification buffer");
×
1039
    return terrno;
×
1040
  }
1041

1042
  TdFilePtr pFile = taosOpenFile(verifyFile, TD_FILE_READ);
×
1043
  if (pFile == NULL) {
×
1044
    dError("failed to open key verification file:%s", verifyFile);
×
1045
    taosMemoryFree(buffer);
×
1046
    return TSDB_CODE_FILE_CORRUPTED;
×
1047
  }
1048

1049
  int64_t bytesRead = taosReadFile(pFile, buffer, fileSize);
×
1050
  (void)taosCloseFile(&pFile);
×
1051

1052
  if (bytesRead != fileSize) {
×
1053
    dError("failed to read key verification file, read=%" PRId64 ", expected=%" PRId64, bytesRead, fileSize);
×
1054
    taosMemoryFree(buffer);
×
1055
    return TSDB_CODE_FILE_CORRUPTED;
×
1056
  }
1057

1058
  int32_t     code = 0;
×
1059
  const char *plaintext = KEY_VERIFY_PLAINTEXT;
×
1060
  int32_t     plaintextLen = strlen(plaintext);
×
1061
  const char *ptr = buffer;
×
1062

1063
  // Parse and verify header
1064
  uint32_t magic = 0;
×
1065
  uint16_t version = 0;
×
1066
  memcpy(&magic, ptr, sizeof(magic));
×
1067
  ptr += sizeof(magic);
×
1068
  memcpy(&version, ptr, sizeof(version));
×
1069
  ptr += sizeof(version);
×
1070

1071
  if (magic != 0x544B5659) {
×
1072
    dError("invalid magic number in key verification file: 0x%x", magic);
×
1073
    taosMemoryFree(buffer);
×
1074
    return TSDB_CODE_FILE_CORRUPTED;
×
1075
  }
1076

1077
  // Array of keys and their algorithms
1078
  const char *keys[] = {svrKey, dbKey, cfgKey, metaKey, dataKey};
×
1079
  int32_t     expectedAlgos[] = {algorithm, algorithm, cfgAlgorithm, metaAlgorithm, algorithm};
×
1080
  const char *keyNames[] = {"SVR_KEY", "DB_KEY", "CFG_KEY", "META_KEY", "DATA_KEY"};
×
1081

1082
  // Verify each key from buffer
1083
  for (int i = 0; i < 5; i++) {
×
1084
    // Check if we have enough data remaining
1085
    if (ptr - buffer + sizeof(int32_t) * 2 > fileSize) {
×
1086
      dError("unexpected end of file while reading %s metadata", keyNames[i]);
×
1087
      taosMemoryFree(buffer);
×
1088
      return TSDB_CODE_FILE_CORRUPTED;
×
1089
    }
1090

1091
    int32_t savedAlgo = 0;
×
1092
    int32_t encryptedLen = 0;
×
1093

1094
    memcpy(&savedAlgo, ptr, sizeof(int32_t));
×
1095
    ptr += sizeof(int32_t);
×
1096
    memcpy(&encryptedLen, ptr, sizeof(int32_t));
×
1097
    ptr += sizeof(int32_t);
×
1098

1099
    if (encryptedLen <= 0 || encryptedLen > 512) {
×
1100
      dError("invalid encrypted length %d for %s", encryptedLen, keyNames[i]);
×
1101
      taosMemoryFree(buffer);
×
1102
      return TSDB_CODE_FILE_CORRUPTED;
×
1103
    }
1104

1105
    if (ptr - buffer + encryptedLen > fileSize) {
×
1106
      dError("unexpected end of file while reading %s encrypted data", keyNames[i]);
×
1107
      taosMemoryFree(buffer);
×
1108
      return TSDB_CODE_FILE_CORRUPTED;
×
1109
    }
1110

1111
    uint8_t encrypted[512] = {0};
×
1112
    memcpy(encrypted, ptr, encryptedLen);
×
1113
    ptr += encryptedLen;
×
1114

1115
    // Decrypt with current key using CBC
1116
    char decrypted[512] = {0};
×
1117

1118
    SCryptOpts opts = {0};
×
1119
    opts.len = encryptedLen;
×
1120
    opts.source = (char *)encrypted;
×
1121
    opts.result = decrypted;
×
1122
    opts.unitLen = 16;
×
1123
    opts.pOsslAlgrName = (savedAlgo == TSDB_ENCRYPT_ALGO_SM4) ? TSDB_ENCRYPT_ALGO_SM4_STR : TSDB_ENCRYPT_ALGO_NONE_STR;
×
1124
    tstrncpy(opts.key, keys[i], sizeof(opts.key));
×
1125

1126
    int32_t count = CBC_Decrypt(&opts);
×
1127
    if (count != opts.len) {
×
1128
      code = terrno ? terrno : TSDB_CODE_DNODE_INVALID_ENCRYPTKEY;
×
1129
      dError("failed to decrypt verification for %s, count=%d, expected=%d, since %s - KEY IS INCORRECT", keyNames[i],
×
1130
             count, opts.len, tstrerror(code));
1131
      taosMemoryFree(buffer);
×
1132
      return TSDB_CODE_DNODE_INVALID_ENCRYPTKEY;
×
1133
    }
1134

1135
    // Verify decrypted data matches original plaintext (compare only the plaintext length)
1136
    if (memcmp(decrypted, plaintext, plaintextLen) != 0) {
×
1137
      dError("%s verification FAILED: decrypted text does not match - KEY IS INCORRECT", keyNames[i]);
×
1138
      taosMemoryFree(buffer);
×
1139
      return TSDB_CODE_DNODE_INVALID_ENCRYPTKEY;
×
1140
    }
1141

1142
    dInfo("%s verification passed (algorithm=%d)", keyNames[i], savedAlgo);
×
1143
  }
1144

1145
  taosMemoryFree(buffer);
×
1146
  dInfo("all encryption keys verified successfully");
×
1147
  return 0;
×
1148
}
1149

1150
// Public API: Verify and initialize encryption keys at startup
1151
int32_t dmVerifyAndInitEncryptionKeys(void) {
546,910✔
1152
  // Skip verification in dump sdb mode (taosd -s)
1153
  if (tsSkipKeyCheckMode) {
546,910✔
1154
    dInfo("skip encryption key verification in some special check mode");
×
1155
    return 0;
×
1156
  }
1157

1158
  // Check if encryption keys are loaded
1159
  if (tsEncryptKeysStatus != TSDB_ENCRYPT_KEY_STAT_LOADED) {
546,910✔
1160
    dDebug("encryption keys not loaded, skipping verification");
546,124✔
1161
    return 0;
546,124✔
1162
  }
1163

1164
  // Get key file paths
1165
  char    masterKeyFile[PATH_MAX] = {0};
786✔
1166
  char    derivedKeyFile[PATH_MAX] = {0};
786✔
1167
  int32_t nBytes = snprintf(masterKeyFile, sizeof(masterKeyFile), "%s%sdnode%sconfig%smaster.bin", tsDataDir, TD_DIRSEP,
786✔
1168
                            TD_DIRSEP, TD_DIRSEP);
1169
  if (nBytes <= 0 || nBytes >= sizeof(masterKeyFile)) {
786✔
1170
    dError("failed to build master key file path");
×
1171
    return TSDB_CODE_OUT_OF_BUFFER;
×
1172
  }
1173

1174
  nBytes = snprintf(derivedKeyFile, sizeof(derivedKeyFile), "%s%sdnode%sconfig%sderived.bin", tsDataDir, TD_DIRSEP,
786✔
1175
                    TD_DIRSEP, TD_DIRSEP);
1176
  if (nBytes <= 0 || nBytes >= sizeof(derivedKeyFile)) {
786✔
1177
    dError("failed to build derived key file path");
×
1178
    return TSDB_CODE_OUT_OF_BUFFER;
×
1179
  }
1180

1181
  // Load encryption keys
1182
  char    svrKey[ENCRYPT_KEY_LEN + 1] = {0};
786✔
1183
  char    dbKey[ENCRYPT_KEY_LEN + 1] = {0};
786✔
1184
  char    cfgKey[ENCRYPT_KEY_LEN + 1] = {0};
786✔
1185
  char    metaKey[ENCRYPT_KEY_LEN + 1] = {0};
786✔
1186
  char    dataKey[ENCRYPT_KEY_LEN + 1] = {0};
786✔
1187
  int32_t algorithm = 0;
786✔
1188
  int32_t cfgAlgorithm = 0;
786✔
1189
  int32_t metaAlgorithm = 0;
786✔
1190
  int32_t fileVersion = 0;
786✔
1191
  int32_t keyVersion = 0;
786✔
1192
  int64_t createTime = 0;
786✔
1193
  int64_t svrKeyUpdateTime = 0;
786✔
1194
  int64_t dbKeyUpdateTime = 0;
786✔
1195

1196
  int32_t code = taoskLoadEncryptKeys(masterKeyFile, derivedKeyFile, svrKey, dbKey, cfgKey, metaKey, dataKey,
786✔
1197
                                      &algorithm, &cfgAlgorithm, &metaAlgorithm, &fileVersion, &keyVersion, &createTime,
1198
                                      &svrKeyUpdateTime, &dbKeyUpdateTime);
1199
  if (code != 0) {
786✔
1200
    dError("failed to load encryption keys, since %s", tstrerror(code));
×
1201
    return code;
×
1202
  }
1203

1204
  // Verify all keys
1205
  code = dmVerifyEncryptionKeys(svrKey, dbKey, cfgKey, metaKey, dataKey, algorithm, cfgAlgorithm, metaAlgorithm);
786✔
1206
  if (code != 0) {
786✔
1207
    dError("encryption key verification failed, since %s", tstrerror(code));
×
1208
    return code;
×
1209
  }
1210

1211
  dInfo("encryption keys verified and initialized successfully");
786✔
1212
  return 0;
786✔
1213
}
1214
#else
1215
int32_t dmVerifyAndInitEncryptionKeys(void) {
1216
  // Community edition or no TaosK support
1217
  return 0;
1218
}
1219
#endif
1220

1221
#if defined(TD_ENTERPRISE) && defined(TD_HAS_TAOSK)
1222
static int32_t dmUpdateSvrKey(const char *newKey) {
×
1223
  if (newKey == NULL || newKey[0] == '\0') {
×
1224
    dError("invalid new SVR_KEY, key is empty");
×
1225
    return TSDB_CODE_INVALID_PARA;
×
1226
  }
1227

1228
  char masterKeyFile[PATH_MAX] = {0};
×
1229
  char derivedKeyFile[PATH_MAX] = {0};
×
1230

1231
  // Build path to key files
1232
  int32_t nBytes = snprintf(masterKeyFile, sizeof(masterKeyFile), "%s%sdnode%sconfig%smaster.bin", tsDataDir, TD_DIRSEP,
×
1233
                            TD_DIRSEP, TD_DIRSEP);
1234
  if (nBytes <= 0 || nBytes >= sizeof(masterKeyFile)) {
×
1235
    dError("failed to build master key file path");
×
1236
    return TSDB_CODE_OUT_OF_BUFFER;
×
1237
  }
1238

1239
  nBytes = snprintf(derivedKeyFile, sizeof(derivedKeyFile), "%s%sdnode%sconfig%sderived.bin", tsDataDir, TD_DIRSEP,
×
1240
                    TD_DIRSEP, TD_DIRSEP);
1241
  if (nBytes <= 0 || nBytes >= sizeof(derivedKeyFile)) {
×
1242
    dError("failed to build derived key file path");
×
1243
    return TSDB_CODE_OUT_OF_BUFFER;
×
1244
  }
1245

1246
  // Load current keys
1247
  char    svrKey[ENCRYPT_KEY_LEN + 1] = {0};
×
1248
  char    dbKey[ENCRYPT_KEY_LEN + 1] = {0};
×
1249
  char    cfgKey[ENCRYPT_KEY_LEN + 1] = {0};
×
1250
  char    metaKey[ENCRYPT_KEY_LEN + 1] = {0};
×
1251
  char    dataKey[ENCRYPT_KEY_LEN + 1] = {0};
×
1252
  int32_t algorithm = 0;
×
1253
  int32_t cfgAlgorithm = 0;
×
1254
  int32_t metaAlgorithm = 0;
×
1255
  int32_t fileVersion = 0;
×
1256
  int32_t keyVersion = 0;
×
1257
  int64_t createTime = 0;
×
1258
  int64_t svrKeyUpdateTime = 0;
×
1259
  int64_t dbKeyUpdateTime = 0;
×
1260

1261
  int32_t code =
1262
      taoskLoadEncryptKeys(masterKeyFile, derivedKeyFile, svrKey, dbKey, cfgKey, metaKey, dataKey, &algorithm,
×
1263
                           &cfgAlgorithm, &metaAlgorithm, &fileVersion, &keyVersion, &createTime, 
1264
                           &svrKeyUpdateTime, &dbKeyUpdateTime);
1265
  if (code != 0) {
×
1266
    dError("failed to load encryption keys, since %s", tstrerror(code));
×
1267
    return code;
×
1268
  }
1269

1270
  // Update SVR_KEY
1271
  int64_t now = taosGetTimestampMs();
×
1272
  int32_t newKeyVersion = keyVersion + 1;
×
1273

1274
  dInfo("updating SVR_KEY, old version:%d, new version:%d", keyVersion, newKeyVersion);
×
1275
  tstrncpy(svrKey, newKey, sizeof(svrKey));
×
1276
  svrKeyUpdateTime = now;
×
1277

1278
  // Save updated keys (use algorithm for all keys for backward compatibility)
1279
  code = taoskSaveEncryptKeys(masterKeyFile, derivedKeyFile, svrKey, dbKey, cfgKey, metaKey, dataKey, algorithm,
×
1280
                              algorithm, algorithm, newKeyVersion, createTime, svrKeyUpdateTime, dbKeyUpdateTime);
1281
  if (code != 0) {
×
1282
    dError("failed to save updated encryption keys, since %s", tstrerror(code));
×
1283
    return code;
×
1284
  }
1285

1286
  // Update key verification file with new SVR_KEY
1287
  code = dmSaveKeyVerification(svrKey, dbKey, cfgKey, metaKey, dataKey, algorithm, algorithm, algorithm);
×
1288
  if (code != 0) {
×
1289
    dWarn("failed to update key verification file, since %s", tstrerror(code));
×
1290
    // Don't fail the operation if verification file update fails
1291
  }
1292

1293
  // Update global variables
1294
  tstrncpy(tsSvrKey, svrKey, sizeof(tsSvrKey));
×
1295
  tstrncpy(tsDbKey, dbKey, sizeof(tsDbKey));
×
1296
  tstrncpy(tsCfgKey, cfgKey, sizeof(tsCfgKey));
×
1297
  tstrncpy(tsMetaKey, metaKey, sizeof(tsMetaKey));
×
1298
  tstrncpy(tsDataKey, dataKey, sizeof(tsDataKey));
×
1299
  tsEncryptAlgorithmType = algorithm;
×
1300
  tsEncryptFileVersion = fileVersion;
×
1301
  tsEncryptKeyVersion = newKeyVersion;
×
1302
  tsEncryptKeyCreateTime = createTime;
×
1303
  tsSvrKeyUpdateTime = svrKeyUpdateTime;
×
1304
  tsDbKeyUpdateTime = dbKeyUpdateTime;
×
1305

1306
  // Update encryption key status for backward compatibility
1307
  int keyLen = strlen(tsDataKey);
×
1308
  if (keyLen > ENCRYPT_KEY_LEN) {
×
1309
    keyLen = ENCRYPT_KEY_LEN;
×
1310
  }
1311
  memset(tsEncryptKey, 0, ENCRYPT_KEY_LEN + 1);
×
1312
  memcpy(tsEncryptKey, tsDataKey, keyLen);
×
1313
  tsEncryptKey[ENCRYPT_KEY_LEN] = '\0';
×
1314
  tsEncryptionKeyChksum = taosCalcChecksum(0, (const uint8_t *)tsEncryptKey, strlen(tsEncryptKey));
×
1315

1316
  dInfo("successfully updated SVR_KEY to version:%d", newKeyVersion);
×
1317
  return 0;
×
1318
}
1319

1320
static int32_t dmUpdateDbKey(const char *newKey) {
×
1321
  if (newKey == NULL || newKey[0] == '\0') {
×
1322
    dError("invalid new DB_KEY, key is empty");
×
1323
    return TSDB_CODE_INVALID_PARA;
×
1324
  }
1325

1326
  char masterKeyFile[PATH_MAX] = {0};
×
1327
  char derivedKeyFile[PATH_MAX] = {0};
×
1328

1329
  // Build path to key files
1330
  int32_t nBytes = snprintf(masterKeyFile, sizeof(masterKeyFile), "%s%sdnode%sconfig%smaster.bin", tsDataDir, TD_DIRSEP,
×
1331
                            TD_DIRSEP, TD_DIRSEP);
1332
  if (nBytes <= 0 || nBytes >= sizeof(masterKeyFile)) {
×
1333
    dError("failed to build master key file path");
×
1334
    return TSDB_CODE_OUT_OF_BUFFER;
×
1335
  }
1336

1337
  nBytes = snprintf(derivedKeyFile, sizeof(derivedKeyFile), "%s%sdnode%sconfig%sderived.bin", tsDataDir, TD_DIRSEP,
×
1338
                    TD_DIRSEP, TD_DIRSEP);
1339
  if (nBytes <= 0 || nBytes >= sizeof(derivedKeyFile)) {
×
1340
    dError("failed to build derived key file path");
×
1341
    return TSDB_CODE_OUT_OF_BUFFER;
×
1342
  }
1343

1344
  // Load current keys
1345
  char    svrKey[ENCRYPT_KEY_LEN + 1] = {0};
×
1346
  char    dbKey[ENCRYPT_KEY_LEN + 1] = {0};
×
1347
  char    cfgKey[ENCRYPT_KEY_LEN + 1] = {0};
×
1348
  char    metaKey[ENCRYPT_KEY_LEN + 1] = {0};
×
1349
  char    dataKey[ENCRYPT_KEY_LEN + 1] = {0};
×
1350
  int32_t algorithm = 0;
×
1351
  int32_t cfgAlgorithm = 0;
×
1352
  int32_t metaAlgorithm = 0;
×
1353
  int32_t fileVersion = 0;
×
1354
  int32_t keyVersion = 0;
×
1355
  int64_t createTime = 0;
×
1356
  int64_t svrKeyUpdateTime = 0;
×
1357
  int64_t dbKeyUpdateTime = 0;
×
1358

1359
  int32_t code =
1360
      taoskLoadEncryptKeys(masterKeyFile, derivedKeyFile, svrKey, dbKey, cfgKey, metaKey, dataKey, &algorithm,
×
1361
                           &cfgAlgorithm, &metaAlgorithm, &fileVersion, &keyVersion, &createTime, 
1362
                           &svrKeyUpdateTime, &dbKeyUpdateTime);
1363
  if (code != 0) {
×
1364
    dError("failed to load encryption keys, since %s", tstrerror(code));
×
1365
    return code;
×
1366
  }
1367

1368
  // Update DB_KEY
1369
  int64_t now = taosGetTimestampMs();
×
1370
  int32_t newKeyVersion = keyVersion + 1;
×
1371

1372
  dInfo("updating DB_KEY, old version:%d, new version:%d", keyVersion, newKeyVersion);
×
1373
  tstrncpy(dbKey, newKey, sizeof(dbKey));
×
1374
  dbKeyUpdateTime = now;
×
1375

1376
  // Save updated keys (use algorithm for all keys for backward compatibility)
1377
  code = taoskSaveEncryptKeys(masterKeyFile, derivedKeyFile, svrKey, dbKey, cfgKey, metaKey, dataKey, algorithm,
×
1378
                              algorithm, algorithm, newKeyVersion, createTime, svrKeyUpdateTime, dbKeyUpdateTime);
1379
  if (code != 0) {
×
1380
    dError("failed to save updated encryption keys, since %s", tstrerror(code));
×
1381
    return code;
×
1382
  }
1383

1384
  // Update key verification file with new DB_KEY
1385
  code = dmSaveKeyVerification(svrKey, dbKey, cfgKey, metaKey, dataKey, algorithm, algorithm, algorithm);
×
1386
  if (code != 0) {
×
1387
    dWarn("failed to update key verification file, since %s", tstrerror(code));
×
1388
    // Don't fail the operation if verification file update fails
1389
  }
1390

1391
  // Update global variables
1392
  tstrncpy(tsSvrKey, svrKey, sizeof(tsSvrKey));
×
1393
  tstrncpy(tsDbKey, dbKey, sizeof(tsDbKey));
×
1394
  tstrncpy(tsCfgKey, cfgKey, sizeof(tsCfgKey));
×
1395
  tstrncpy(tsMetaKey, metaKey, sizeof(tsMetaKey));
×
1396
  tstrncpy(tsDataKey, dataKey, sizeof(tsDataKey));
×
1397
  tsEncryptAlgorithmType = algorithm;
×
1398
  tsEncryptFileVersion = fileVersion;
×
1399
  tsEncryptKeyVersion = newKeyVersion;
×
1400
  tsEncryptKeyCreateTime = createTime;
×
1401
  tsSvrKeyUpdateTime = svrKeyUpdateTime;
×
1402
  tsDbKeyUpdateTime = dbKeyUpdateTime;
×
1403

1404
  // Update encryption key status for backward compatibility
1405
  int keyLen = strlen(tsDataKey);
×
1406
  if (keyLen > ENCRYPT_KEY_LEN) {
×
1407
    keyLen = ENCRYPT_KEY_LEN;
×
1408
  }
1409
  memset(tsEncryptKey, 0, ENCRYPT_KEY_LEN + 1);
×
1410
  memcpy(tsEncryptKey, tsDataKey, keyLen);
×
1411
  tsEncryptKey[ENCRYPT_KEY_LEN] = '\0';
×
1412
  tsEncryptionKeyChksum = taosCalcChecksum(0, (const uint8_t *)tsEncryptKey, strlen(tsEncryptKey));
×
1413

1414
  dInfo("successfully updated DB_KEY to version:%d", newKeyVersion);
×
1415
  return 0;
×
1416
}
1417
#endif
1418

1419
int32_t dmProcessAlterEncryptKeyReq(SDnodeMgmt *pMgmt, SRpcMsg *pMsg) {
×
1420
#if defined(TD_ENTERPRISE) && defined(TD_HAS_TAOSK)
1421
  int32_t              code = 0;
×
1422
  SMAlterEncryptKeyReq alterKeyReq = {0};
×
1423
  if (tDeserializeSMAlterEncryptKeyReq(pMsg->pCont, pMsg->contLen, &alterKeyReq) != 0) {
×
1424
    code = TSDB_CODE_INVALID_MSG;
×
1425
    dError("failed to deserialize alter encrypt key req, since %s", tstrerror(code));
×
1426
    goto _exit;
×
1427
  }
1428

1429
  dInfo("received alter encrypt key req, keyType:%d", alterKeyReq.keyType);
×
1430

1431
  // Update the specified key (svr_key or db_key)
1432
  if (alterKeyReq.keyType == 0) {
×
1433
    // Update SVR_KEY
1434
    code = dmUpdateSvrKey(alterKeyReq.newKey);
×
1435
    if (code == 0) {
×
1436
      dInfo("successfully updated SVR_KEY");
×
1437
    } else {
1438
      dError("failed to update SVR_KEY, since %s", tstrerror(code));
×
1439
    }
1440
  } else if (alterKeyReq.keyType == 1) {
×
1441
    // Update DB_KEY
1442
    code = dmUpdateDbKey(alterKeyReq.newKey);
×
1443
    if (code == 0) {
×
1444
      dInfo("successfully updated DB_KEY");
×
1445
    } else {
1446
      dError("failed to update DB_KEY, since %s", tstrerror(code));
×
1447
    }
1448
  } else {
1449
    dError("invalid keyType:%d, must be 0 (SVR_KEY) or 1 (DB_KEY)", alterKeyReq.keyType);
×
1450
    code = TSDB_CODE_INVALID_PARA;
×
1451
  }
1452

1453
_exit:
×
1454
  tFreeSMAlterEncryptKeyReq(&alterKeyReq);
×
1455
  pMsg->code = code;
×
1456
  pMsg->info.rsp = NULL;
×
1457
  pMsg->info.rspLen = 0;
×
1458
  return code;
×
1459
#else
1460
  dError("encryption key management is only available in enterprise edition");
1461
  pMsg->code = TSDB_CODE_OPS_NOT_SUPPORT;
1462
  pMsg->info.rsp = NULL;
1463
  pMsg->info.rspLen = 0;
1464
  return TSDB_CODE_OPS_NOT_SUPPORT;
1465
#endif
1466
}
1467

1468
int32_t dmProcessReloadTlsConfig(SDnodeMgmt *pMgmt, SRpcMsg *pMsg) {
×
1469
  int32_t code = 0;
×
1470
  int32_t lino = 0;
×
1471
  SMsgCb *msgCb = &pMgmt->msgCb;
×
1472
  void *pTransCli = msgCb->clientRpc;
×
1473
  void *pTransStatus = msgCb->statusRpc;  
×
1474
  void *pTransSync = msgCb->syncRpc; 
×
1475
  void *pTransServer = msgCb->serverRpc;
×
1476

1477
  code = rpcReloadTlsConfig(pTransServer, TAOS_CONN_SERVER);
×
1478
  if (code != 0) {
×
1479
    dError("failed to reload tls config for transport %s since %s", "server", tstrerror(code));
×
1480
    goto _error;
×
1481
  }
1482

1483
  code = rpcReloadTlsConfig(pTransCli, TAOS_CONN_CLIENT);
×
1484
  if (code != 0) {
×
1485
    dError("failed to reload tls config for transport %s since %s", "cli", tstrerror(code));
×
1486
    goto _error;
×
1487
  }
1488

1489
  code = rpcReloadTlsConfig(pTransStatus, TAOS_CONN_CLIENT);
×
1490
  if (code != 0) {
×
1491
    dError("failed to reload tls config for transport %s since %s", "status-cli", tstrerror(code));
×
1492
    goto _error;
×
1493
  }
1494

1495
  code = rpcReloadTlsConfig(pTransSync, TAOS_CONN_CLIENT);
×
1496
  if (code != 0) {
×
1497
    dError("failed to reload tls config for transport %s since %s", "sync-cli", tstrerror(code));
×
1498
    goto _error;
×
1499
  }
1500

1501
_error:
×
1502
  
1503
  return code;
×
1504
}
1505

1506
static void dmGetServerRunStatus(SDnodeMgmt *pMgmt, SServerStatusRsp *pStatus) {
167✔
1507
  pStatus->statusCode = TSDB_SRV_STATUS_SERVICE_OK;
167✔
1508
  pStatus->details[0] = 0;
167✔
1509

1510
  SMonMloadInfo minfo = {0};
167✔
1511
  (*pMgmt->getMnodeLoadsFp)(&minfo);
167✔
1512
  if (minfo.isMnode &&
167✔
1513
      (minfo.load.syncState == TAOS_SYNC_STATE_ERROR || minfo.load.syncState == TAOS_SYNC_STATE_OFFLINE)) {
167✔
1514
    pStatus->statusCode = TSDB_SRV_STATUS_SERVICE_DEGRADED;
×
1515
    snprintf(pStatus->details, sizeof(pStatus->details), "mnode sync state is %s", syncStr(minfo.load.syncState));
×
1516
    return;
×
1517
  }
1518

1519
  SMonVloadInfo vinfo = {0};
167✔
1520
  (*pMgmt->getVnodeLoadsFp)(&vinfo);
167✔
1521
  for (int32_t i = 0; i < taosArrayGetSize(vinfo.pVloads); ++i) {
501✔
1522
    SVnodeLoad *pLoad = taosArrayGet(vinfo.pVloads, i);
334✔
1523
    if (pLoad->syncState == TAOS_SYNC_STATE_ERROR || pLoad->syncState == TAOS_SYNC_STATE_OFFLINE) {
334✔
1524
      pStatus->statusCode = TSDB_SRV_STATUS_SERVICE_DEGRADED;
×
1525
      snprintf(pStatus->details, sizeof(pStatus->details), "vnode:%d sync state is %s", pLoad->vgId,
×
1526
               syncStr(pLoad->syncState));
×
1527
      break;
×
1528
    }
1529
  }
1530

1531
  taosArrayDestroy(vinfo.pVloads);
167✔
1532
}
1533

1534
int32_t dmProcessServerRunStatus(SDnodeMgmt *pMgmt, SRpcMsg *pMsg) {
167✔
1535
  int32_t code = 0;
167✔
1536
  dDebug("server run status req is received");
167✔
1537
  SServerStatusRsp statusRsp = {0};
167✔
1538
  dmGetServerRunStatus(pMgmt, &statusRsp);
167✔
1539

1540
  pMsg->info.rsp = NULL;
167✔
1541
  pMsg->info.rspLen = 0;
167✔
1542

1543
  SRpcMsg rspMsg = {.info = pMsg->info};
167✔
1544
  int32_t rspLen = tSerializeSServerStatusRsp(NULL, 0, &statusRsp);
167✔
1545
  if (rspLen < 0) {
167✔
1546
    return TSDB_CODE_OUT_OF_MEMORY;
×
1547
    // rspMsg.code = TSDB_CODE_OUT_OF_MEMORY;
1548
    // return rspMsg.code;
1549
  }
1550

1551
  void *pRsp = rpcMallocCont(rspLen);
167✔
1552
  if (pRsp == NULL) {
167✔
1553
    return terrno;
×
1554
    // rspMsg.code = TSDB_CODE_OUT_OF_MEMORY;
1555
    // return rspMsg.code;
1556
  }
1557

1558
  rspLen = tSerializeSServerStatusRsp(pRsp, rspLen, &statusRsp);
167✔
1559
  if (rspLen < 0) {
167✔
1560
    return TSDB_CODE_INVALID_MSG;
×
1561
  }
1562

1563
  pMsg->info.rsp = pRsp;
167✔
1564
  pMsg->info.rspLen = rspLen;
167✔
1565
  return 0;
167✔
1566
}
1567

1568
int32_t dmBuildVariablesBlock(SSDataBlock **ppBlock) {
23,866✔
1569
  int32_t code = 0;
23,866✔
1570

1571
  SSDataBlock *pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
23,866✔
1572
  if (pBlock == NULL) {
23,866✔
1573
    return terrno;
×
1574
  }
1575

1576
  size_t size = 0;
23,866✔
1577

1578
  const SSysTableMeta *pMeta = NULL;
23,866✔
1579
  getInfosDbMeta(&pMeta, &size);
23,866✔
1580

1581
  int32_t index = 0;
23,866✔
1582
  for (int32_t i = 0; i < size; ++i) {
477,320✔
1583
    if (strcmp(pMeta[i].name, TSDB_INS_TABLE_DNODE_VARIABLES) == 0) {
477,320✔
1584
      index = i;
23,866✔
1585
      break;
23,866✔
1586
    }
1587
  }
1588

1589
  pBlock->pDataBlock = taosArrayInit(pMeta[index].colNum, sizeof(SColumnInfoData));
23,866✔
1590
  if (pBlock->pDataBlock == NULL) {
23,866✔
1591
    code = terrno;
×
1592
    goto _exit;
×
1593
  }
1594

1595
  for (int32_t i = 0; i < pMeta[index].colNum; ++i) {
167,062✔
1596
    SColumnInfoData colInfoData = {0};
143,196✔
1597
    colInfoData.info.colId = i + 1;
143,196✔
1598
    colInfoData.info.type = pMeta[index].schema[i].type;
143,196✔
1599
    colInfoData.info.bytes = pMeta[index].schema[i].bytes;
143,196✔
1600
    if (taosArrayPush(pBlock->pDataBlock, &colInfoData) == NULL) {
286,392✔
1601
      code = terrno;
×
1602
      goto _exit;
×
1603
    }
1604
  }
1605

1606
  pBlock->info.hasVarCol = true;
23,866✔
1607
_exit:
23,866✔
1608
  if (code != 0) {
23,866✔
1609
    blockDataDestroy(pBlock);
×
1610
  } else {
1611
    *ppBlock = pBlock;
23,866✔
1612
  }
1613
  return code;
23,866✔
1614
}
1615

1616
int32_t dmAppendVariablesToBlock(SSDataBlock *pBlock, int32_t dnodeId) {
23,866✔
1617
  int32_t code = dumpConfToDataBlock(pBlock, 1, NULL);
23,866✔
1618
  if (code != 0) {
23,866✔
1619
    return code;
×
1620
  }
1621

1622
  SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, 0);
23,866✔
1623
  if (pColInfo == NULL) {
23,866✔
1624
    return TSDB_CODE_OUT_OF_RANGE;
×
1625
  }
1626

1627
  return colDataSetNItems(pColInfo, 0, (const char *)&dnodeId, pBlock->info.rows, 1, false);
23,866✔
1628
}
1629

1630
int32_t dmProcessRetrieve(SDnodeMgmt *pMgmt, SRpcMsg *pMsg) {
23,866✔
1631
  int32_t           size = 0;
23,866✔
1632
  int32_t           rowsRead = 0;
23,866✔
1633
  int32_t           code = 0;
23,866✔
1634
  SRetrieveTableReq retrieveReq = {0};
23,866✔
1635
  if (tDeserializeSRetrieveTableReq(pMsg->pCont, pMsg->contLen, &retrieveReq) != 0) {
23,866✔
1636
    return TSDB_CODE_INVALID_MSG;
×
1637
  }
1638
  dInfo("retrieve table:%s, user:%s, compactId:%" PRId64, retrieveReq.tb, retrieveReq.user, retrieveReq.compactId);
23,866✔
1639
#if 0
1640
  if (strcmp(retrieveReq.user, TSDB_DEFAULT_USER) != 0) {
1641
    code = TSDB_CODE_MND_NO_RIGHTS;
1642
    return code;
1643
  }
1644
#endif
1645
  if (strcasecmp(retrieveReq.tb, TSDB_INS_TABLE_DNODE_VARIABLES)) {
23,866✔
1646
    return TSDB_CODE_INVALID_MSG;
×
1647
  }
1648

1649
  SSDataBlock *pBlock = NULL;
23,866✔
1650
  if ((code = dmBuildVariablesBlock(&pBlock)) != 0) {
23,866✔
1651
    return code;
×
1652
  }
1653

1654
  code = dmAppendVariablesToBlock(pBlock, pMgmt->pData->dnodeId);
23,866✔
1655
  if (code != 0) {
23,866✔
1656
    blockDataDestroy(pBlock);
×
1657
    return code;
×
1658
  }
1659

1660
  size_t numOfCols = taosArrayGetSize(pBlock->pDataBlock);
23,866✔
1661
  size_t dataEncodeBufSize = blockGetEncodeSize(pBlock);
23,866✔
1662
  size = sizeof(SRetrieveMetaTableRsp) + sizeof(int32_t) + sizeof(SSysTableSchema) * numOfCols + dataEncodeBufSize;
23,866✔
1663

1664
  SRetrieveMetaTableRsp *pRsp = rpcMallocCont(size);
23,866✔
1665
  if (pRsp == NULL) {
23,866✔
1666
    code = terrno;
×
1667
    dError("failed to retrieve data since %s", tstrerror(code));
×
1668
    blockDataDestroy(pBlock);
×
1669
    return code;
×
1670
  }
1671

1672
  char *pStart = pRsp->data;
23,866✔
1673
  *(int32_t *)pStart = htonl(numOfCols);
23,866✔
1674
  pStart += sizeof(int32_t);  // number of columns
23,866✔
1675

1676
  for (int32_t i = 0; i < numOfCols; ++i) {
167,062✔
1677
    SSysTableSchema *pSchema = (SSysTableSchema *)pStart;
143,196✔
1678
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, i);
143,196✔
1679

1680
    pSchema->bytes = htonl(pColInfo->info.bytes);
143,196✔
1681
    pSchema->colId = htons(pColInfo->info.colId);
143,196✔
1682
    pSchema->type = pColInfo->info.type;
143,196✔
1683

1684
    pStart += sizeof(SSysTableSchema);
143,196✔
1685
  }
1686

1687
  int32_t len = blockEncode(pBlock, pStart, dataEncodeBufSize, numOfCols);
23,866✔
1688
  if (len < 0) {
23,866✔
1689
    dError("failed to retrieve data since %s", tstrerror(code));
×
1690
    blockDataDestroy(pBlock);
×
1691
    rpcFreeCont(pRsp);
×
1692
    return terrno;
×
1693
  }
1694

1695
  pRsp->numOfRows = htonl(pBlock->info.rows);
23,866✔
1696
  pRsp->precision = TSDB_TIME_PRECISION_MILLI;  // millisecond time precision
23,866✔
1697
  pRsp->completed = 1;
23,866✔
1698
  pMsg->info.rsp = pRsp;
23,866✔
1699
  pMsg->info.rspLen = size;
23,866✔
1700
  dDebug("dnode variables retrieve completed");
23,866✔
1701

1702
  blockDataDestroy(pBlock);
23,866✔
1703
  return TSDB_CODE_SUCCESS;
23,866✔
1704
}
1705

1706
int32_t dmProcessStreamHbRsp(SDnodeMgmt *pMgmt, SRpcMsg *pMsg) {
13,885,230✔
1707
  SMStreamHbRspMsg rsp = {0};
13,885,230✔
1708
  int32_t          code = 0;
13,885,230✔
1709
  SDecoder         decoder;
13,765,862✔
1710
  char*            msg = POINTER_SHIFT(pMsg->pCont, sizeof(SStreamMsgGrpHeader));
13,885,230✔
1711
  int32_t          len = pMsg->contLen - sizeof(SStreamMsgGrpHeader);
13,885,230✔
1712
  int64_t          currTs = taosGetTimestampMs();
13,885,230✔
1713

1714
  if (pMsg->code) {
13,885,230✔
1715
    return streamHbHandleRspErr(pMsg->code, currTs);
187,128✔
1716
  }
1717

1718
  tDecoderInit(&decoder, (uint8_t*)msg, len);
13,698,102✔
1719
  code = tDecodeStreamHbRsp(&decoder, &rsp);
13,698,102✔
1720
  if (code < 0) {
13,698,102✔
1721
    code = TSDB_CODE_INVALID_MSG;
×
1722
    tDeepFreeSMStreamHbRspMsg(&rsp);
×
1723
    tDecoderClear(&decoder);
×
1724
    dError("fail to decode stream hb rsp msg, error:%s", tstrerror(code));
×
1725
    return streamHbHandleRspErr(code, currTs);
×
1726
  }
1727

1728
  tDecoderClear(&decoder);
13,698,102✔
1729

1730
  return streamHbProcessRspMsg(&rsp);
13,698,102✔
1731
}
1732

1733

1734
SArray *dmGetMsgHandles() {
546,936✔
1735
  int32_t code = -1;
546,936✔
1736
  SArray *pArray = taosArrayInit(16, sizeof(SMgmtHandle));
546,936✔
1737
  if (pArray == NULL) {
546,936✔
1738
    return NULL;
×
1739
  }
1740

1741
  // Requests handled by DNODE
1742
  if (dmSetMgmtHandle(pArray, TDMT_DND_CREATE_MNODE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
546,936✔
1743
  if (dmSetMgmtHandle(pArray, TDMT_DND_DROP_MNODE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
546,936✔
1744
  if (dmSetMgmtHandle(pArray, TDMT_DND_CREATE_QNODE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
546,936✔
1745
  if (dmSetMgmtHandle(pArray, TDMT_DND_DROP_QNODE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
546,936✔
1746
  if (dmSetMgmtHandle(pArray, TDMT_DND_CREATE_SNODE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
546,936✔
1747
  if (dmSetMgmtHandle(pArray, TDMT_DND_ALTER_SNODE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
546,936✔
1748
  if (dmSetMgmtHandle(pArray, TDMT_DND_DROP_SNODE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
546,936✔
1749
  if (dmSetMgmtHandle(pArray, TDMT_DND_CREATE_BNODE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
546,936✔
1750
  if (dmSetMgmtHandle(pArray, TDMT_DND_DROP_BNODE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
546,936✔
1751
  if (dmSetMgmtHandle(pArray, TDMT_DND_CONFIG_DNODE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
546,936✔
1752
  if (dmSetMgmtHandle(pArray, TDMT_DND_SERVER_STATUS, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
546,936✔
1753
  if (dmSetMgmtHandle(pArray, TDMT_DND_SYSTABLE_RETRIEVE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
546,936✔
1754
  if (dmSetMgmtHandle(pArray, TDMT_DND_ALTER_MNODE_TYPE, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
546,936✔
1755
  if (dmSetMgmtHandle(pArray, TDMT_DND_CREATE_ENCRYPT_KEY, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
546,936✔
1756
  if (dmSetMgmtHandle(pArray, TDMT_MND_ALTER_ENCRYPT_KEY, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
546,936✔
1757
  if (dmSetMgmtHandle(pArray, TDMT_MND_STREAM_HEARTBEAT_RSP, dmPutMsgToStreamMgmtQueue, 0) == NULL) goto _OVER;
546,936✔
1758
  if (dmSetMgmtHandle(pArray, TDMT_DND_RELOAD_DNODE_TLS, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
546,936✔
1759

1760
  // Requests handled by MNODE
1761
  if (dmSetMgmtHandle(pArray, TDMT_MND_GRANT, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
546,936✔
1762
  if (dmSetMgmtHandle(pArray, TDMT_MND_GRANT_NOTIFY, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
546,936✔
1763
  if (dmSetMgmtHandle(pArray, TDMT_MND_AUTH_RSP, dmPutNodeMsgToMgmtQueue, 0) == NULL) goto _OVER;
546,936✔
1764

1765
  code = 0;
546,936✔
1766

1767
_OVER:
546,936✔
1768
  if (code != 0) {
546,936✔
1769
    taosArrayDestroy(pArray);
×
1770
    return NULL;
×
1771
  } else {
1772
    return pArray;
546,936✔
1773
  }
1774
}
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