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

taosdata / TDengine / #4940

27 Jan 2026 10:23AM UTC coverage: 66.832% (-0.1%) from 66.931%
#4940

push

travis-ci

web-flow
fix: asan invalid write issue (#34400)

7 of 8 new or added lines in 2 files covered. (87.5%)

822 existing lines in 141 files now uncovered.

204293 of 305680 relevant lines covered (66.83%)

124534723.83 hits per line

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

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

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

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

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

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

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

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

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

208
  if (pRsp->code != 0) {
51,185,598✔
209
    if (pRsp->code == TSDB_CODE_MND_DNODE_NOT_EXIST && !pMgmt->pData->dropped && pMgmt->pData->dnodeId > 0) {
405,767✔
UNCOV
210
      dGInfo("dnode:%d, set to dropped since not exist in mnode, statusSeq:%d", pMgmt->pData->dnodeId,
×
211
             pMgmt->statusSeq);
UNCOV
212
      pMgmt->pData->dropped = 1;
×
UNCOV
213
      if (dmWriteEps(pMgmt->pData) != 0) {
×
214
        dError("failed to write dnode file");
×
215
      }
UNCOV
216
      dInfo("dnode will exit since it is in the dropped state");
×
UNCOV
217
      (void)raise(SIGINT);
×
218
    }
219
  } else {
220
    SStatusRsp statusRsp = {0};
50,779,831✔
221
    if (pRsp->pCont != NULL && pRsp->contLen > 0 &&
52,868,366✔
222
        tDeserializeSStatusRsp(pRsp->pCont, pRsp->contLen, &statusRsp) == 0) {
2,088,535✔
223
      if (pMgmt->pData->dnodeVer != statusRsp.dnodeVer) {
2,088,535✔
224
        dGInfo("status rsp received from mnode, statusSeq:%d:%d dnodeVer:%" PRId64 ":%" PRId64, pMgmt->statusSeq,
1,964,272✔
225
               statusRsp.statusSeq, pMgmt->pData->dnodeVer, statusRsp.dnodeVer);
226
        pMgmt->pData->dnodeVer = statusRsp.dnodeVer;
1,964,272✔
227
        dmUpdateDnodeCfg(pMgmt, &statusRsp.dnodeCfg);
1,964,272✔
228
        dmUpdateEps(pMgmt->pData, statusRsp.pDnodeEps);
1,964,272✔
229
      }
230
      setAuditDbNameToken(statusRsp.auditDB, statusRsp.auditToken);
2,088,535✔
231
      dmMayShouldUpdateIpWhiteList(pMgmt, statusRsp.ipWhiteVer);
2,088,535✔
232
      dmMayShouldUpdateTimeWhiteList(pMgmt, statusRsp.timeWhiteVer);
2,088,535✔
233
      dmMayShouldUpdateAnalyticsFunc(pMgmt, statusRsp.analVer);
2,088,535✔
234
    }
235
    tFreeSStatusRsp(&statusRsp);
50,779,831✔
236
  }
237
  rpcFreeCont(pRsp->pCont);
51,185,598✔
238
}
51,185,598✔
239

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

246
  dTrace("send status req to mnode, begin to mgnt statusInfolock, statusSeq:%d", pMgmt->statusSeq);
51,265,736✔
247
  if (taosThreadMutexLock(&pMgmt->pData->statusInfolock) != 0) {
51,265,736✔
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);
51,265,736✔
253
  req.sver = tsVersion;
51,265,736✔
254
  req.dnodeVer = tsDnodeData.dnodeVer;
51,265,736✔
255
  req.dnodeId = tsDnodeData.dnodeId;
51,265,736✔
256
  req.clusterId = tsDnodeData.clusterId;
51,265,736✔
257
  if (req.clusterId == 0) req.dnodeId = 0;
51,265,736✔
258
  req.rebootTime = tsDnodeData.rebootTime;
51,265,736✔
259
  req.updateTime = tsDnodeData.updateTime;
51,265,736✔
260
  req.numOfCores = tsNumOfCores;
51,265,736✔
261
  req.numOfSupportVnodes = tsNumOfSupportVnodes;
51,265,736✔
262
  req.numOfDiskCfg = tsDiskCfgNum;
51,265,736✔
263
  req.memTotal = tsTotalMemoryKB * 1024;
51,265,736✔
264
  req.memAvail = req.memTotal - tsQueueMemoryAllowed - tsApplyMemoryAllowed - 16 * 1024 * 1024;
51,265,736✔
265
  tstrncpy(req.dnodeEp, tsLocalEp, TSDB_EP_LEN);
51,265,736✔
266
  tstrncpy(req.machineId, tsDnodeData.machineId, TSDB_MACHINE_ID_LEN + 1);
51,265,736✔
267

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

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

292
  req.pVloads = tsVinfo.pVloads;
51,265,736✔
293
  tsVinfo.pVloads = NULL;
51,265,736✔
294

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

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

306
  req.statusSeq = pMgmt->statusSeq;
51,265,736✔
307
  req.ipWhiteVer = pMgmt->pData->ipWhiteVer;
51,265,736✔
308
  req.analVer = taosAnalyGetVersion();
51,265,736✔
309
  req.timeWhiteVer = pMgmt->pData->timeWhiteVer;
51,265,736✔
310

311
  if (tsAuditUseToken) {
51,265,736✔
312
    getAuditDbNameToken(req.auditDB, req.auditToken);
51,265,736✔
313
  }
314

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

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

330
  SRpcMsg rpcMsg = {.pCont = pHead,
51,265,736✔
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};
51,265,736✔
339

340
  dDebug("send status req to mnode, dnodeVer:%" PRId64 " statusSeq:%d", req.dnodeVer, req.statusSeq);
51,265,736✔
341

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

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

364
  if (rpcRsp.code != 0) {
51,185,598✔
365
    dmRotateMnodeEpSet(pMgmt->pData);
405,767✔
366
    char tbuf[512];
405,767✔
367
    dmEpSetToStr(tbuf, sizeof(tbuf), &epSet);
405,767✔
368
    dInfo("Rotate mnode ep set since failed to SendRecv status req %s, epSet:%s, inUse:%d", tstrerror(rpcRsp.code),
405,767✔
369
          tbuf, epSet.inUse);
370
  } else {
371
    if (epUpdated == 1) {
50,779,831✔
372
      dmSetMnodeEpSet(pMgmt->pData, &epSet);
98,194✔
373
    }
374
  }
375
  dmProcessStatusRsp(pMgmt, &rpcRsp);
51,185,598✔
376
}
377

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

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

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

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

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

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

501
    // Encrypt existing plaintext config files
502
    code = taosEncryptExistingCfgFiles(tsDataDir);
1,424✔
503
    if (code != 0) {
1,424✔
UNCOV
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
UNCOV
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);
527,004✔
513
  }
514
  
515
  code = TSDB_CODE_SUCCESS;
528,428✔
516

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

665
  if (tsVinfo.pVloads == NULL) {
51,805,770✔
666
    tsVinfo.pVloads = vinfo.pVloads;
50,879,768✔
667
    vinfo.pVloads = NULL;
50,879,768✔
668
  } else {
669
    taosArrayDestroy(vinfo.pVloads);
926,002✔
670
    vinfo.pVloads = NULL;
926,002✔
671
  }
672

673
  tsMLoad = minfo.load;
51,805,770✔
674

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

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

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

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

749
  if (taosStrncasecmp(cfgReq.config, "syncTimeout", 128) == 0) {
68,536✔
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)) {
68,536✔
822
    TAOS_CHECK_RETURN(taosCfgDynamicOptions(pCfg, cfgReq.config, true));
67,954✔
823

824
    if (taosStrncasecmp(cfgReq.config, "syncTimeout", 128) == 0) {
67,954✔
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) {
68,536✔
844
    code = taosPersistGlobalConfig(taosGetGlobalCfg(pCfg), pMgmt->path, tsdmConfigVersion);
9,651✔
845
    if (code != TSDB_CODE_SUCCESS) {
9,651✔
846
      dError("failed to persist global config since %s", tstrerror(code));
×
847
    }
848
  } else {
849
    code = taosPersistLocalConfig(pMgmt->path);
58,885✔
850
    if (code != TSDB_CODE_SUCCESS) {
58,885✔
851
      dError("failed to persist local config since %s", tstrerror(code));
×
852
    }
853
  }
854

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

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

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

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

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

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

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

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

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

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

942
  char *ptr = buffer;
766✔
943

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

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

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

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

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

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

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

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

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

1211
  dInfo("encryption keys verified and initialized successfully");
766✔
1212
  return 0;
766✔
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) {
180✔
1507
  pStatus->statusCode = TSDB_SRV_STATUS_SERVICE_OK;
180✔
1508
  pStatus->details[0] = 0;
180✔
1509

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

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

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

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

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

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

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

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

1576
  size_t size = 0;
22,410✔
1577

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

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

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

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

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

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

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

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

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

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

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

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

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

1676
  for (int32_t i = 0; i < numOfCols; ++i) {
156,870✔
1677
    SSysTableSchema *pSchema = (SSysTableSchema *)pStart;
134,460✔
1678
    SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, i);
134,460✔
1679

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

1684
    pStart += sizeof(SSysTableSchema);
134,460✔
1685
  }
1686

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

1702
  blockDataDestroy(pBlock);
22,410✔
1703
  return TSDB_CODE_SUCCESS;
22,410✔
1704
}
1705

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

1714
  if (pMsg->code) {
17,728,689✔
1715
    return streamHbHandleRspErr(pMsg->code, currTs);
164,814✔
1716
  }
1717

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

1730
  return streamHbProcessRspMsg(&rsp);
17,563,875✔
1731
}
1732

1733

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

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

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

1765
  code = 0;
549,864✔
1766

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