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

taosdata / TDengine / #4930

16 Jan 2026 02:32AM UTC coverage: 66.716% (-0.007%) from 66.723%
#4930

push

travis-ci

web-flow
enh: interp supports using non-null prev/next values to fill (#34236)

281 of 327 new or added lines in 11 files covered. (85.93%)

1747 existing lines in 129 files now uncovered.

203203 of 304580 relevant lines covered (66.72%)

131619261.81 hits per line

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

53.75
/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,859,550✔
51
  int32_t code = 0;
1,859,550✔
52
  if (pMgmt->pData->dnodeId == 0 || pMgmt->pData->clusterId == 0) {
1,859,550✔
53
    dInfo("set local info, dnodeId:%d clusterId:%" PRId64, pCfg->dnodeId, pCfg->clusterId);
449,532✔
54
    (void)taosThreadRwlockWrlock(&pMgmt->pData->lock);
449,532✔
55
    pMgmt->pData->dnodeId = pCfg->dnodeId;
449,532✔
56
    pMgmt->pData->clusterId = pCfg->clusterId;
449,532✔
57
    monSetDnodeId(pCfg->dnodeId);
449,532✔
58
    auditSetDnodeId(pCfg->dnodeId);
449,532✔
59
    code = dmWriteEps(pMgmt->pData);
449,532✔
60
    if (code != 0) {
449,532✔
61
      dInfo("failed to set local info, dnodeId:%d clusterId:0x%" PRIx64 " reason:%s", pCfg->dnodeId, pCfg->clusterId,
1,800✔
62
            tstrerror(code));
63
    }
64
    (void)taosThreadRwlockUnlock(&pMgmt->pData->lock);
449,532✔
65
  }
66
}
1,859,550✔
67

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

82
  SRetrieveWhiteListReq req = {.ver = oldVer};
563✔
83
  int32_t             contLen = tSerializeRetrieveWhiteListReq(NULL, 0, &req);
563✔
84
  if (contLen < 0) {
563✔
85
    dError("failed to serialize ip white list request since: %s", tstrerror(contLen));
×
86
    return;
×
87
  }
88
  void *pHead = rpcMallocCont(contLen);
563✔
89
  contLen = tSerializeRetrieveWhiteListReq(pHead, contLen, &req);
563✔
90
  if (contLen < 0) {
563✔
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,
563✔
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};
563✔
105

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

108
  code = rpcSendRequest(pMgmt->msgCb.clientRpc, &epset, &rpcMsg, NULL);
563✔
109
  if (code != 0) {
563✔
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) {
2,010,396✔
117
  int32_t code = 0;
2,010,396✔
118
  dDebug("time-white-list on dnode ver: %" PRId64 ", status ver: %" PRId64, pMgmt->pData->timeWhiteVer, ver);
2,010,396✔
119
  if (pMgmt->pData->timeWhiteVer == ver) {
2,010,396✔
120
    if (ver == 0) {
2,009,833✔
121
      dDebug("disable time-white-list on dnode ver: %" PRId64 ", status ver: %" PRId64, pMgmt->pData->timeWhiteVer, ver);
2,009,590✔
122
      if (rpcSetIpWhite(pMgmt->msgCb.serverRpc, NULL) != 0) {
2,009,590✔
123
        dError("failed to disable time white list on dnode");
×
124
      }
125
    }
126
    return;
2,009,833✔
127
  }
128
  int64_t oldVer = pMgmt->pData->timeWhiteVer;
563✔
129

130
  SRetrieveWhiteListReq req = {.ver = oldVer};
563✔
131
  int32_t             contLen = tSerializeRetrieveWhiteListReq(NULL, 0, &req);
563✔
132
  if (contLen < 0) {
563✔
133
    dError("failed to serialize datetime white list request since: %s", tstrerror(contLen));
×
134
    return;
×
135
  }
136
  void *pHead = rpcMallocCont(contLen);
563✔
137
  contLen = tSerializeRetrieveWhiteListReq(pHead, contLen, &req);
563✔
138
  if (contLen < 0) {
563✔
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,
563✔
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};
563✔
153

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

156
  code = rpcSendRequest(pMgmt->msgCb.clientRpc, &epset, &rpcMsg, NULL);
563✔
157
  if (code != 0) {
563✔
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) {
2,010,396✔
165
  int32_t code = 0;
2,010,396✔
166
  int64_t oldVer = taosAnalyGetVersion();
2,010,396✔
167
  if (oldVer == newVer) return;
2,010,396✔
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) {
44,067,652✔
205
  const STraceId *trace = &pRsp->info.traceId;
44,067,652✔
206
  dGTrace("status rsp received from mnode, statusSeq:%d code:0x%x", pMgmt->statusSeq, pRsp->code);
44,067,652✔
207

208
  if (pRsp->code != 0) {
44,067,652✔
209
    if (pRsp->code == TSDB_CODE_MND_DNODE_NOT_EXIST && !pMgmt->pData->dropped && pMgmt->pData->dnodeId > 0) {
428,904✔
210
      dGInfo("dnode:%d, set to dropped since not exist in mnode, statusSeq:%d", pMgmt->pData->dnodeId,
×
211
             pMgmt->statusSeq);
212
      pMgmt->pData->dropped = 1;
×
213
      if (dmWriteEps(pMgmt->pData) != 0) {
×
214
        dError("failed to write dnode file");
×
215
      }
216
      dInfo("dnode will exit since it is in the dropped state");
×
217
      (void)raise(SIGINT);
×
218
    }
219
  } else {
220
    SStatusRsp statusRsp = {0};
43,638,748✔
221
    if (pRsp->pCont != NULL && pRsp->contLen > 0 &&
45,649,144✔
222
        tDeserializeSStatusRsp(pRsp->pCont, pRsp->contLen, &statusRsp) == 0) {
2,010,396✔
223
      if (pMgmt->pData->dnodeVer != statusRsp.dnodeVer) {
2,010,396✔
224
        dGInfo("status rsp received from mnode, statusSeq:%d:%d dnodeVer:%" PRId64 ":%" PRId64, pMgmt->statusSeq,
1,859,550✔
225
               statusRsp.statusSeq, pMgmt->pData->dnodeVer, statusRsp.dnodeVer);
226
        pMgmt->pData->dnodeVer = statusRsp.dnodeVer;
1,859,550✔
227
        dmUpdateDnodeCfg(pMgmt, &statusRsp.dnodeCfg);
1,859,550✔
228
        dmUpdateEps(pMgmt->pData, statusRsp.pDnodeEps);
1,859,550✔
229
      }
230
      setAuditDbNameToken(statusRsp.auditDB, statusRsp.auditToken);
2,010,396✔
231
      dmMayShouldUpdateIpWhiteList(pMgmt, statusRsp.ipWhiteVer);
2,010,396✔
232
      dmMayShouldUpdateTimeWhiteList(pMgmt, statusRsp.timeWhiteVer);
2,010,396✔
233
      dmMayShouldUpdateAnalyticsFunc(pMgmt, statusRsp.analVer);
2,010,396✔
234
    }
235
    tFreeSStatusRsp(&statusRsp);
43,638,748✔
236
  }
237
  rpcFreeCont(pRsp->pCont);
44,067,652✔
238
}
44,067,652✔
239

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

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

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

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

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

292
  req.pVloads = tsVinfo.pVloads;
44,165,504✔
293
  tsVinfo.pVloads = NULL;
44,165,504✔
294

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

298
  if (taosThreadMutexUnlock(&pMgmt->pData->statusInfolock) != 0) {
44,165,504✔
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);
44,165,504✔
304
  (*pMgmt->getQnodeLoadsFp)(&req.qload);
44,165,504✔
305

306
  req.statusSeq = pMgmt->statusSeq;
44,165,504✔
307
  req.ipWhiteVer = pMgmt->pData->ipWhiteVer;
44,165,504✔
308
  req.analVer = taosAnalyGetVersion();
44,165,504✔
309
  req.timeWhiteVer = pMgmt->pData->timeWhiteVer;
44,165,504✔
310

311
  if (tsAuditUseToken) {
44,165,504✔
312
    getAuditDbNameToken(req.auditDB, req.auditToken);
44,165,504✔
313
  }
314

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

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

330
  SRpcMsg rpcMsg = {.pCont = pHead,
44,165,504✔
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};
44,165,504✔
339

340
  dDebug("send status req to mnode, dnodeVer:%" PRId64 " statusSeq:%d", req.dnodeVer, req.statusSeq);
44,165,504✔
341

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

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

364
  if (rpcRsp.code != 0) {
44,067,652✔
365
    dmRotateMnodeEpSet(pMgmt->pData);
428,904✔
366
    char tbuf[512];
428,904✔
367
    dmEpSetToStr(tbuf, sizeof(tbuf), &epSet);
428,904✔
368
    dInfo("Rotate mnode ep set since failed to SendRecv status req %s, epSet:%s, inUse:%d", tstrerror(rpcRsp.code),
428,904✔
369
          tbuf, epSet.inUse);
370
  } else {
371
    if (epUpdated == 1) {
43,638,748✔
372
      dmSetMnodeEpSet(pMgmt->pData, &epSet);
104,547✔
373
    }
374
  }
375
  dmProcessStatusRsp(pMgmt, &rpcRsp);
44,067,652✔
376
}
377

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

384
  if (pRsp->code != 0) {
582,526✔
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;
582,526✔
396
    if (pRsp->pCont != NULL && pRsp->contLen > 0 &&
1,165,052✔
397
        tDeserializeSConfigRsp(pRsp->pCont, pRsp->contLen, &configRsp) == 0) {
582,526✔
398
      // Try to use cfg from mnode sdb.
399
      if (!configRsp.isVersionVerified) {
582,526✔
400
        uInfo("config version not verified, update config");
450,277✔
401
        needUpdate = true;
450,277✔
402
        code = taosPersistGlobalConfig(configRsp.array, pMgmt->path, configRsp.cver);
450,277✔
403
        if (code != TSDB_CODE_SUCCESS) {
450,277✔
404
          dError("failed to persist global config since %s", tstrerror(code));
×
405
          goto _exit;
×
406
        }
407
      }
408
    }
409
    if (needUpdate) {
582,526✔
410
      code = cfgUpdateFromArray(tsCfg, configRsp.array);
450,277✔
411
      if (code != TSDB_CODE_SUCCESS) {
450,277✔
412
        dError("failed to update config since %s", tstrerror(code));
×
413
        goto _exit;
×
414
      }
415
      code = setAllConfigs(tsCfg);
450,277✔
416
      if (code != TSDB_CODE_SUCCESS) {
450,277✔
417
        dError("failed to set all configs since %s", tstrerror(code));
×
418
        goto _exit;
×
419
      }
420
    }
421
    code = taosPersistLocalConfig(pMgmt->path);
582,526✔
422
    if (code != TSDB_CODE_SUCCESS) {
582,526✔
423
      dError("failed to persist local config since %s", tstrerror(code));
×
424
    }
425
    tsConfigInited = 1;
582,526✔
426
  }
427
_exit:
582,526✔
428
  tFreeSConfigRsp(&configRsp);
582,526✔
429
  rpcFreeCont(pRsp->pCont);
582,526✔
430
  if (needStop) {
582,526✔
431
    dmStop();
×
432
  }
433
}
582,526✔
434

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

440
  if (pRsp->code != 0) {
560,999✔
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) {
560,999✔
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);
560,999✔
453
  if (code != 0) {
560,999✔
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,
560,999✔
459
        tsLocalKeyVersion, keySyncRsp.needUpdate);
460
  tsEncryptKeysStatus = keySyncRsp.encryptionKeyStatus;
560,999✔
461
  if (keySyncRsp.needUpdate) {
560,999✔
462
#if defined(TD_ENTERPRISE) && defined(TD_HAS_TAOSK)
463
    // Get encrypt file path from tsDataDir
464
    char masterKeyFile[PATH_MAX] = {0};
1,488✔
465
    char derivedKeyFile[PATH_MAX] = {0};
1,488✔
466
    snprintf(masterKeyFile, sizeof(masterKeyFile), "%s%sdnode%sconfig%smaster.bin", tsDataDir, TD_DIRSEP, TD_DIRSEP,
1,488✔
467
             TD_DIRSEP);
468
    snprintf(derivedKeyFile, sizeof(derivedKeyFile), "%s%sdnode%sconfig%sderived.bin", tsDataDir, TD_DIRSEP, TD_DIRSEP,
1,488✔
469
             TD_DIRSEP);
470

471
    dInfo("updating local encryption keys from mnode, key file is saved in %s and %s, keyVersion:%d -> %d",
1,488✔
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,488✔
477
                                keySyncRsp.dataKey, keySyncRsp.algorithm, keySyncRsp.algorithm, keySyncRsp.algorithm,
478
                                keySyncRsp.keyVersion, keySyncRsp.createTime,
479
                                keySyncRsp.svrKeyUpdateTime, keySyncRsp.dbKeyUpdateTime);
480
    if (code != 0) {
1,488✔
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,488✔
487
    tstrncpy(tsDbKey, keySyncRsp.dbKey, sizeof(tsDbKey));
1,488✔
488
    tstrncpy(tsCfgKey, keySyncRsp.cfgKey, sizeof(tsCfgKey));
1,488✔
489
    tstrncpy(tsMetaKey, keySyncRsp.metaKey, sizeof(tsMetaKey));
1,488✔
490
    tstrncpy(tsDataKey, keySyncRsp.dataKey, sizeof(tsDataKey));
1,488✔
491
    tsEncryptAlgorithmType = keySyncRsp.algorithm;
1,488✔
492
    tsEncryptKeyVersion = keySyncRsp.keyVersion;
1,488✔
493
    tsEncryptKeyCreateTime = keySyncRsp.createTime;
1,488✔
494
    tsSvrKeyUpdateTime = keySyncRsp.svrKeyUpdateTime;
1,488✔
495
    tsDbKeyUpdateTime = keySyncRsp.dbKeyUpdateTime;
1,488✔
496

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

501
    // Encrypt existing plaintext config files
502
    code = taosEncryptExistingCfgFiles(tsDataDir);
1,488✔
503
    if (code != 0) {
1,488✔
504
      dWarn("failed to encrypt existing config files since %s, will retry on next write", tstrerror(code));
×
505
      // Don't fail the key sync, files will be encrypted on next write
506
      code = 0;
×
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);
559,511✔
513
  }
514
  
515
  code = TSDB_CODE_SUCCESS;
560,999✔
516

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

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

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

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

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

548
  SRpcMsg rpcMsg = {.pCont = pHead,
583,400✔
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};
583,400✔
557

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

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

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

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

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

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

606
  SRpcMsg rpcMsg = {.pCont = pHead,
604,940✔
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};
604,940✔
615

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

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

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

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

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

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

665
  if (tsVinfo.pVloads == NULL) {
44,796,871✔
666
    tsVinfo.pVloads = vinfo.pVloads;
43,573,362✔
667
    vinfo.pVloads = NULL;
43,573,362✔
668
  } else {
669
    taosArrayDestroy(vinfo.pVloads);
1,223,509✔
670
    vinfo.pVloads = NULL;
1,223,509✔
671
  }
672

673
  tsMLoad = minfo.load;
44,796,871✔
674

675
  if (taosThreadMutexUnlock(&pMgmt->pData->statusInfolock) != 0) {
44,796,871✔
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) {
80,104✔
722
  int32_t       code = 0;
80,104✔
723
  SDCfgDnodeReq cfgReq = {0};
80,104✔
724
  SConfig      *pCfg = taosGetCfg();
80,104✔
725
  SConfigItem  *pItem = NULL;
80,104✔
726

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

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

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

749
  if (taosStrncasecmp(cfgReq.config, "syncTimeout", 128) == 0) {
76,526✔
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)) {
76,526✔
822
    TAOS_CHECK_RETURN(taosCfgDynamicOptions(pCfg, cfgReq.config, true));
75,901✔
823

824
    if (taosStrncasecmp(cfgReq.config, "syncTimeout", 128) == 0) {
75,901✔
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) {
76,526✔
844
    code = taosPersistGlobalConfig(taosGetGlobalCfg(pCfg), pMgmt->path, tsdmConfigVersion);
10,878✔
845
    if (code != TSDB_CODE_SUCCESS) {
10,878✔
846
      dError("failed to persist global config since %s", tstrerror(code));
×
847
    }
848
  } else {
849
    code = taosPersistLocalConfig(pMgmt->path);
65,648✔
850
    if (code != TSDB_CODE_SUCCESS) {
65,648✔
851
      dError("failed to persist local config since %s", tstrerror(code));
×
852
    }
853
  }
854

855
  if (taosStrncasecmp(cfgReq.config, "syncTimeout", 128) == 0) {
76,526✔
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 ||
76,526✔
863
      taosStrncasecmp(cfgReq.config, "syncVnodeHeartbeatIntervalMs", 128) == 0) {
76,526✔
864
    (*pMgmt->setVnodeSyncTimeoutFp)();
×
865
  }
866

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

872
  if (cfgReq.version > 0) {
76,526✔
873
    tsdmConfigVersion = cfgReq.version;
18,024✔
874
  }
875
  return code;
76,526✔
876
}
877

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

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

894
_exit:
169✔
895
  pMsg->code = code;
169✔
896
  pMsg->info.rsp = NULL;
169✔
897
  pMsg->info.rspLen = 0;
169✔
898
  return code;
169✔
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,
825✔
910
                                     const char *dataKey, int32_t algorithm, int32_t cfgAlgorithm,
911
                                     int32_t metaAlgorithm) {
912
  char    verifyFile[PATH_MAX] = {0};
825✔
913
  int32_t nBytes = snprintf(verifyFile, sizeof(verifyFile), "%s%sdnode%sconfig%skey_verify.dat", tsDataDir, TD_DIRSEP,
825✔
914
                            TD_DIRSEP, TD_DIRSEP);
915
  if (nBytes <= 0 || nBytes >= sizeof(verifyFile)) {
825✔
916
    dError("failed to build key verification file path");
×
917
    return TSDB_CODE_OUT_OF_BUFFER;
×
918
  }
919

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

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

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

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

942
  char *ptr = buffer;
825✔
943

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

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

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

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

969
    int32_t count = CBC_Encrypt(&opts);
4,125✔
970
    if (count != opts.len) {
4,125✔
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));
4,125✔
980
    ptr += sizeof(int32_t);
4,125✔
981
    memcpy(ptr, &encryptedLen, sizeof(int32_t));
4,125✔
982
    ptr += sizeof(int32_t);
4,125✔
983
    memcpy(ptr, encrypted, encryptedLen);
4,125✔
984
    ptr += encryptedLen;
4,125✔
985

986
    dDebug("prepared verification for %s: algorithm=%d, encLen=%d", keyNames[i], algorithms[i], encryptedLen);
4,125✔
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);
825✔
991
  if (pFile == NULL) {
825✔
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);
825✔
998
  (void)taosCloseFile(&pFile);
825✔
999
  taosMemoryFree(buffer);
825✔
1000

1001
  if (written != totalSize) {
825✔
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);
825✔
1007
  return 0;
825✔
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,
825✔
1012
                                      const char *dataKey, int32_t algorithm, int32_t cfgAlgorithm,
1013
                                      int32_t metaAlgorithm) {
1014
  char    verifyFile[PATH_MAX] = {0};
825✔
1015
  int32_t nBytes = snprintf(verifyFile, sizeof(verifyFile), "%s%sdnode%sconfig%skey_verify.dat", tsDataDir, TD_DIRSEP,
825✔
1016
                            TD_DIRSEP, TD_DIRSEP);
1017
  if (nBytes <= 0 || nBytes >= sizeof(verifyFile)) {
825✔
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;
825✔
1024
  if (taosStatFile(verifyFile, &fileSize, NULL, NULL) < 0) {
825✔
1025
    // File doesn't exist, create it with current keys
1026
    dInfo("key verification file not found, creating new one");
825✔
1027
    return dmSaveKeyVerification(svrKey, dbKey, cfgKey, metaKey, dataKey, algorithm, cfgAlgorithm, metaAlgorithm);
825✔
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) {
585,497✔
1152
  // Skip verification in dump sdb mode (taosd -s)
1153
  if (tsSkipKeyCheckMode) {
585,497✔
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) {
585,497✔
1160
    dDebug("encryption keys not loaded, skipping verification");
584,672✔
1161
    return 0;
584,672✔
1162
  }
1163

1164
  // Get key file paths
1165
  char    masterKeyFile[PATH_MAX] = {0};
825✔
1166
  char    derivedKeyFile[PATH_MAX] = {0};
825✔
1167
  int32_t nBytes = snprintf(masterKeyFile, sizeof(masterKeyFile), "%s%sdnode%sconfig%smaster.bin", tsDataDir, TD_DIRSEP,
825✔
1168
                            TD_DIRSEP, TD_DIRSEP);
1169
  if (nBytes <= 0 || nBytes >= sizeof(masterKeyFile)) {
825✔
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,
825✔
1175
                    TD_DIRSEP, TD_DIRSEP);
1176
  if (nBytes <= 0 || nBytes >= sizeof(derivedKeyFile)) {
825✔
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[129] = {0};
825✔
1183
  char    dbKey[129] = {0};
825✔
1184
  char    cfgKey[129] = {0};
825✔
1185
  char    metaKey[129] = {0};
825✔
1186
  char    dataKey[129] = {0};
825✔
1187
  int32_t algorithm = 0;
825✔
1188
  int32_t cfgAlgorithm = 0;
825✔
1189
  int32_t metaAlgorithm = 0;
825✔
1190
  int32_t fileVersion = 0;
825✔
1191
  int32_t keyVersion = 0;
825✔
1192
  int64_t createTime = 0;
825✔
1193
  int64_t svrKeyUpdateTime = 0;
825✔
1194
  int64_t dbKeyUpdateTime = 0;
825✔
1195

1196
  int32_t code = taoskLoadEncryptKeys(masterKeyFile, derivedKeyFile, svrKey, dbKey, cfgKey, metaKey, dataKey,
825✔
1197
                                      &algorithm, &cfgAlgorithm, &metaAlgorithm, &fileVersion, &keyVersion, &createTime,
1198
                                      &svrKeyUpdateTime, &dbKeyUpdateTime);
1199
  if (code != 0) {
825✔
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);
825✔
1206
  if (code != 0) {
825✔
1207
    dError("encryption key verification failed, since %s", tstrerror(code));
×
1208
    return code;
×
1209
  }
1210

1211
  dInfo("encryption keys verified and initialized successfully");
825✔
1212
  return 0;
825✔
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[129] = {0};
×
1248
  char    dbKey[129] = {0};
×
1249
  char    cfgKey[129] = {0};
×
1250
  char    metaKey[129] = {0};
×
1251
  char    dataKey[129] = {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[129] = {0};
×
1346
  char    dbKey[129] = {0};
×
1347
  char    cfgKey[129] = {0};
×
1348
  char    metaKey[129] = {0};
×
1349
  char    dataKey[129] = {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) {
169✔
1507
  pStatus->statusCode = TSDB_SRV_STATUS_SERVICE_OK;
169✔
1508
  pStatus->details[0] = 0;
169✔
1509

1510
  SMonMloadInfo minfo = {0};
169✔
1511
  (*pMgmt->getMnodeLoadsFp)(&minfo);
169✔
1512
  if (minfo.isMnode &&
169✔
1513
      (minfo.load.syncState == TAOS_SYNC_STATE_ERROR || minfo.load.syncState == TAOS_SYNC_STATE_OFFLINE)) {
169✔
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};
169✔
1520
  (*pMgmt->getVnodeLoadsFp)(&vinfo);
169✔
1521
  for (int32_t i = 0; i < taosArrayGetSize(vinfo.pVloads); ++i) {
507✔
1522
    SVnodeLoad *pLoad = taosArrayGet(vinfo.pVloads, i);
338✔
1523
    if (pLoad->syncState == TAOS_SYNC_STATE_ERROR || pLoad->syncState == TAOS_SYNC_STATE_OFFLINE) {
338✔
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);
169✔
1532
}
1533

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

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

1543
  SRpcMsg rspMsg = {.info = pMsg->info};
169✔
1544
  int32_t rspLen = tSerializeSServerStatusRsp(NULL, 0, &statusRsp);
169✔
1545
  if (rspLen < 0) {
169✔
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);
169✔
1552
  if (pRsp == NULL) {
169✔
1553
    return terrno;
×
1554
    // rspMsg.code = TSDB_CODE_OUT_OF_MEMORY;
1555
    // return rspMsg.code;
1556
  }
1557

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

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

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

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

1576
  size_t size = 0;
29,006✔
1577

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

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

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

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

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

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

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

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

1630
int32_t dmProcessRetrieve(SDnodeMgmt *pMgmt, SRpcMsg *pMsg) {
29,006✔
1631
  int32_t           size = 0;
29,006✔
1632
  int32_t           rowsRead = 0;
29,006✔
1633
  int32_t           code = 0;
29,006✔
1634
  SRetrieveTableReq retrieveReq = {0};
29,006✔
1635
  if (tDeserializeSRetrieveTableReq(pMsg->pCont, pMsg->contLen, &retrieveReq) != 0) {
29,006✔
1636
    return TSDB_CODE_INVALID_MSG;
×
1637
  }
1638
  dInfo("retrieve table:%s, user:%s, compactId:%" PRId64, retrieveReq.tb, retrieveReq.user, retrieveReq.compactId);
29,006✔
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)) {
29,006✔
1646
    return TSDB_CODE_INVALID_MSG;
×
1647
  }
1648

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

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

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

1664
  SRetrieveMetaTableRsp *pRsp = rpcMallocCont(size);
29,006✔
1665
  if (pRsp == NULL) {
29,006✔
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;
29,006✔
1673
  *(int32_t *)pStart = htonl(numOfCols);
29,006✔
1674
  pStart += sizeof(int32_t);  // number of columns
29,006✔
1675

1676
  for (int32_t i = 0; i < numOfCols; ++i) {
203,042✔
1677
    SSysTableSchema *pSchema = (SSysTableSchema *)pStart;
174,036✔
1678
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, i);
174,036✔
1679

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

1684
    pStart += sizeof(SSysTableSchema);
174,036✔
1685
  }
1686

1687
  int32_t len = blockEncode(pBlock, pStart, dataEncodeBufSize, numOfCols);
29,006✔
1688
  if (len < 0) {
29,006✔
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);
29,006✔
1696
  pRsp->precision = TSDB_TIME_PRECISION_MILLI;  // millisecond time precision
29,006✔
1697
  pRsp->completed = 1;
29,006✔
1698
  pMsg->info.rsp = pRsp;
29,006✔
1699
  pMsg->info.rspLen = size;
29,006✔
1700
  dDebug("dnode variables retrieve completed");
29,006✔
1701

1702
  blockDataDestroy(pBlock);
29,006✔
1703
  return TSDB_CODE_SUCCESS;
29,006✔
1704
}
1705

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

1714
  if (pMsg->code) {
15,286,997✔
1715
    return streamHbHandleRspErr(pMsg->code, currTs);
197,452✔
1716
  }
1717

1718
  tDecoderInit(&decoder, (uint8_t*)msg, len);
15,089,545✔
1719
  code = tDecodeStreamHbRsp(&decoder, &rsp);
15,089,545✔
1720
  if (code < 0) {
15,089,545✔
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);
15,089,545✔
1729

1730
  return streamHbProcessRspMsg(&rsp);
15,089,545✔
1731
}
1732

1733

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

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

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

1765
  code = 0;
585,525✔
1766

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