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

taosdata / TDengine / #4440

04 Jul 2025 02:10AM UTC coverage: 63.29% (-0.4%) from 63.643%
#4440

push

travis-ci

web-flow
fix:(stmt2) heap buffer overflow (#31607)

159782 of 321690 branches covered (49.67%)

Branch coverage included in aggregate %.

19 of 22 new or added lines in 3 files covered. (86.36%)

5735 existing lines in 195 files now uncovered.

246739 of 320626 relevant lines covered (76.96%)

6757056.21 hits per line

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

54.62
/source/dnode/mnode/impl/src/mndConfig.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 "mndConfig.h"
19
#include "mndDnode.h"
20
#include "mndMnode.h"
21
#include "mndPrivilege.h"
22
#include "mndSync.h"
23
#include "mndTrans.h"
24
#include "mndUser.h"
25
#include "tutil.h"
26
#include "tcompare.h"
27

28
#define CFG_VER_NUMBER    1
29
#define CFG_RESERVE_SIZE  63
30
#define CFG_ALTER_TIMEOUT 3 * 1000
31

32
static int32_t mndMCfgGetValInt32(SMCfgDnodeReq *pInMCfgReq, int32_t optLen, int32_t *pOutValue);
33
static int32_t mndProcessShowVariablesReq(SRpcMsg *pReq);
34
static int32_t mndProcessConfigDnodeReq(SRpcMsg *pReq);
35
static int32_t mndProcessConfigDnodeRsp(SRpcMsg *pRsp);
36
static int32_t mndProcessConfigReq(SRpcMsg *pReq);
37
static int32_t mndInitWriteCfg(SMnode *pMnode);
38
static int32_t mndSendRebuildReq(SMnode *pMnode);
39
static int32_t mndTryRebuildConfigSdbRsp(SRpcMsg *pRsp);
40
static int32_t initConfigArrayFromSdb(SMnode *pMnode, SArray *array);
41
static int32_t mndTryRebuildConfigSdb(SRpcMsg *pReq);
42
static void    cfgArrayCleanUp(SArray *array);
43
static void    cfgObjArrayCleanUp(SArray *array);
44
int32_t        compareSConfigItemArrays(SMnode *pMnode, const SArray *dArray, SArray *diffArray);
45

46
static int32_t mndConfigUpdateTrans(SMnode *pMnode, const char *name, char *pValue, ECfgDataType dtype,
47
                                    int32_t tsmmConfigVersion);
48
static int32_t mndFindConfigsToAdd(SMnode *pMnode, SArray *addArray);
49
static int32_t mndFindConfigsToDelete(SMnode *pMnode, SArray *deleteArray);
50
static int32_t mndExecuteConfigSyncTrans(SMnode *pMnode, SArray *addArray, SArray *deleteArray);
51

52
int32_t mndSetCreateConfigCommitLogs(STrans *pTrans, SConfigObj *obj);
53
int32_t mndSetDeleteConfigCommitLogs(STrans *pTrans, SConfigObj *item);
54

55
int32_t mndInitConfig(SMnode *pMnode) {
2,560✔
56
  int32_t   code = 0;
2,560✔
57
  SSdbTable table = {.sdbType = SDB_CFG,
2,560✔
58
                     .keyType = SDB_KEY_BINARY,
59
                     .encodeFp = (SdbEncodeFp)mnCfgActionEncode,
60
                     .decodeFp = (SdbDecodeFp)mndCfgActionDecode,
61
                     .insertFp = (SdbInsertFp)mndCfgActionInsert,
62
                     .updateFp = (SdbUpdateFp)mndCfgActionUpdate,
63
                     .deleteFp = (SdbDeleteFp)mndCfgActionDelete,
64
                     .deployFp = (SdbDeployFp)mndCfgActionDeploy,
65
                     .afterRestoredFp = (SdbAfterRestoredFp)mndCfgActionAfterRestored};
66

67
  mndSetMsgHandle(pMnode, TDMT_MND_CONFIG, mndProcessConfigReq);
2,560✔
68
  mndSetMsgHandle(pMnode, TDMT_MND_CONFIG_DNODE, mndProcessConfigDnodeReq);
2,560✔
69
  mndSetMsgHandle(pMnode, TDMT_DND_CONFIG_DNODE_RSP, mndProcessConfigDnodeRsp);
2,560✔
70
  mndSetMsgHandle(pMnode, TDMT_MND_SHOW_VARIABLES, mndProcessShowVariablesReq);
2,560✔
71
  mndSetMsgHandle(pMnode, TDMT_MND_CONFIG_SDB, mndTryRebuildConfigSdb);
2,560✔
72
  mndSetMsgHandle(pMnode, TDMT_MND_CONFIG_SDB_RSP, mndTryRebuildConfigSdbRsp);
2,560✔
73

74
  return sdbSetTable(pMnode->pSdb, table);
2,560✔
75
}
76

77
SSdbRaw *mnCfgActionEncode(SConfigObj *obj) {
614,676✔
78
  int32_t  code = 0;
614,676✔
79
  int32_t  lino = 0;
614,676✔
80
  void    *buf = NULL;
614,676✔
81
  SSdbRaw *pRaw = NULL;
614,676✔
82

83
  SEncoder encoder;
84
  tEncoderInit(&encoder, NULL, 0);
614,676✔
85
  if ((code = tEncodeSConfigObj(&encoder, obj)) < 0) {
614,676!
UNCOV
86
    tEncoderClear(&encoder);
×
UNCOV
87
    TSDB_CHECK_CODE(code, lino, _over);
×
88
  }
89

90
  int32_t tlen = encoder.pos;
614,676✔
91
  tEncoderClear(&encoder);
614,676✔
92

93
  int32_t size = sizeof(int32_t) + tlen;
614,676✔
94
  pRaw = sdbAllocRaw(SDB_CFG, CFG_VER_NUMBER, size);
614,676✔
95
  TSDB_CHECK_NULL(pRaw, code, lino, _over, terrno);
614,676!
96

97
  buf = taosMemoryMalloc(tlen);
614,676!
98
  TSDB_CHECK_NULL(buf, code, lino, _over, terrno);
614,676!
99

100
  tEncoderInit(&encoder, buf, tlen);
614,676✔
101
  if ((code = tEncodeSConfigObj(&encoder, obj)) < 0) {
614,676!
UNCOV
102
    tEncoderClear(&encoder);
×
UNCOV
103
    TSDB_CHECK_CODE(code, lino, _over);
×
104
  }
105

106
  tEncoderClear(&encoder);
614,676✔
107

108
  int32_t dataPos = 0;
614,676✔
109
  SDB_SET_INT32(pRaw, dataPos, tlen, _over);
614,676!
110
  SDB_SET_BINARY(pRaw, dataPos, buf, tlen, _over);
614,676!
111
  SDB_SET_DATALEN(pRaw, dataPos, _over);
614,676!
112

113
_over:
614,676✔
114
  taosMemoryFreeClear(buf);
614,676!
115
  if (code != TSDB_CODE_SUCCESS) {
614,676!
UNCOV
116
    mError("cfg:%s, failed to encode to raw:%p at line:%d since %s", obj->name, pRaw, lino, tstrerror(code));
×
UNCOV
117
    sdbFreeRaw(pRaw);
×
UNCOV
118
    terrno = code;
×
UNCOV
119
    return NULL;
×
120
  }
121

122
  terrno = 0;
614,676✔
123
  mTrace("cfg:%s, encode to raw:%p, row:%p", obj->name, pRaw, obj);
614,676✔
124
  return pRaw;
614,676✔
125
}
126

127
SSdbRow *mndCfgActionDecode(SSdbRaw *pRaw) {
240,522✔
128
  int32_t     code = 0;
240,522✔
129
  int32_t     lino = 0;
240,522✔
130
  SSdbRow    *pRow = NULL;
240,522✔
131
  SConfigObj *pObj = NULL;
240,522✔
132
  void       *buf = NULL;
240,522✔
133
  int8_t      sver = 0;
240,522✔
134
  int32_t     tlen;
135
  int32_t     dataPos = 0;
240,522✔
136

137
  code = sdbGetRawSoftVer(pRaw, &sver);
240,522✔
138
  TSDB_CHECK_CODE(code, lino, _over);
240,522!
139

140
  if (sver != CFG_VER_NUMBER) {
240,522!
UNCOV
141
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
×
UNCOV
142
    goto _over;
×
143
  }
144

145
  pRow = sdbAllocRow(sizeof(SConfigObj));
240,522✔
146
  TSDB_CHECK_NULL(pRow, code, lino, _over, terrno);
240,522!
147

148
  pObj = sdbGetRowObj(pRow);
240,522✔
149
  TSDB_CHECK_NULL(pObj, code, lino, _over, terrno);
240,522!
150

151
  SDB_GET_INT32(pRaw, dataPos, &tlen, _over);
240,522!
152

153
  buf = taosMemoryMalloc(tlen + 1);
240,522!
154
  TSDB_CHECK_NULL(buf, code, lino, _over, terrno);
240,522!
155

156
  SDB_GET_BINARY(pRaw, dataPos, buf, tlen, _over);
240,522!
157

158
  SDecoder decoder;
159
  tDecoderInit(&decoder, buf, tlen + 1);
240,522✔
160
  code = tDecodeSConfigObj(&decoder, pObj);
240,522✔
161
  tDecoderClear(&decoder);
240,522✔
162

163
  if (code < 0) {
240,522!
UNCOV
164
    tFreeSConfigObj(pObj);
×
165
  }
166

167
_over:
240,522✔
168
  taosMemoryFreeClear(buf);
240,522!
169

170
  if (code != TSDB_CODE_SUCCESS) {
240,522!
UNCOV
171
    mError("cfg:%s, failed to decode from raw:%p since %s at:%d", pObj->name, pRaw, tstrerror(code), lino);
×
UNCOV
172
    taosMemoryFreeClear(pRow);
×
UNCOV
173
    terrno = code;
×
UNCOV
174
    return NULL;
×
175
  } else {
176
    mTrace("config:%s, decode from raw:%p, row:%p", pObj->name, pRaw, pObj);
240,522✔
177
    terrno = 0;
240,522✔
178
    return pRow;
240,522✔
179
  }
180
}
181

182
static int32_t mndCfgActionInsert(SSdb *pSdb, SConfigObj *obj) {
239,896✔
183
  mTrace("cfg:%s, perform insert action, row:%p", obj->name, obj);
239,896✔
184
  return 0;
239,896✔
185
}
186

187
static int32_t mndCfgActionDelete(SSdb *pSdb, SConfigObj *obj) {
240,240✔
188
  mTrace("cfg:%s, perform delete action, row:%p", obj->name, obj);
240,240✔
189
  tFreeSConfigObj(obj);
240,240✔
190
  return 0;
240,240✔
191
}
192

193
static int32_t mndCfgActionUpdate(SSdb *pSdb, SConfigObj *pOld, SConfigObj *pNew) {
626✔
194
  mTrace("cfg:%s, perform update action, old row:%p new row:%p", pOld->name, pOld, pNew);
626!
195
  switch (pNew->dtype) {
626!
UNCOV
196
    case CFG_DTYPE_NONE:
×
UNCOV
197
      break;
×
198
    case CFG_DTYPE_BOOL:
76✔
199
      pOld->bval = pNew->bval;
76✔
200
      break;
76✔
201
    case CFG_DTYPE_INT32:
505✔
202
      pOld->i32 = pNew->i32;
505✔
203
      break;
505✔
204
    case CFG_DTYPE_INT64:
25✔
205
      pOld->i64 = pNew->i64;
25✔
206
      break;
25✔
207
    case CFG_DTYPE_FLOAT:
8✔
208
    case CFG_DTYPE_DOUBLE:
209
      pOld->fval = pNew->fval;
8✔
210
      break;
8✔
211
    case CFG_DTYPE_STRING:
12✔
212
    case CFG_DTYPE_DIR:
213
    case CFG_DTYPE_LOCALE:
214
    case CFG_DTYPE_CHARSET:
215
    case CFG_DTYPE_TIMEZONE:
216
      taosMemoryFree(pOld->str);
12!
217
      pOld->str = taosStrdup(pNew->str);
12!
218
      if (pOld->str == NULL) {
12!
UNCOV
219
        return terrno;
×
220
      }
221
      break;
12✔
222
  }
223
  return TSDB_CODE_SUCCESS;
626✔
224
}
225

226
static int32_t mndCfgActionDeploy(SMnode *pMnode) { return mndInitWriteCfg(pMnode); }
1,917✔
227

228
static int32_t mndCfgActionAfterRestored(SMnode *pMnode) { return mndSendRebuildReq(pMnode); }
1,001✔
229

230
static int32_t mndProcessConfigReq(SRpcMsg *pReq) {
3,181✔
231
  SMnode    *pMnode = pReq->info.node;
3,181✔
232
  SConfigReq configReq = {0};
3,181✔
233
  int32_t    code = TSDB_CODE_SUCCESS;
3,181✔
234
  SArray    *array = NULL;
3,181✔
235
  bool       needFree = false;
3,181✔
236
  code = tDeserializeSConfigReq(pReq->pCont, pReq->contLen, &configReq);
3,181✔
237
  if (code != 0) {
3,181!
UNCOV
238
    mError("failed to deserialize config req, since %s", terrstr());
×
UNCOV
239
    goto _OVER;
×
240
  }
241

242
  SConfigObj *vObj = sdbAcquire(pMnode->pSdb, SDB_CFG, "tsmmConfigVersion");
3,181✔
243
  if (vObj == NULL) {
3,181!
UNCOV
244
    mInfo("failed to acquire mnd config version, since %s", terrstr());
×
UNCOV
245
    goto _OVER;
×
246
  }
247

248
  array = taosArrayInit(16, sizeof(SConfigItem));
3,181✔
249
  if (array == NULL) {
3,181!
UNCOV
250
    code = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
251
    goto _OVER;
×
252
  }
253
  SConfigRsp configRsp = {0};
3,181✔
254
  configRsp.cver = vObj->i32;
3,181✔
255

256
  if (configReq.cver == vObj->i32) {
3,181✔
257
    configRsp.isVersionVerified = 1;
642✔
258
  } else {
259
    code = initConfigArrayFromSdb(pMnode, array);
2,539✔
260
    if (code != 0) {
2,539!
UNCOV
261
      mError("failed to init config array from sdb, since %s", tstrerror(code));
×
UNCOV
262
      goto _OVER;
×
263
    }
264
    configRsp.array = array;
2,539✔
265
  }
266

267
  int32_t contLen = tSerializeSConfigRsp(NULL, 0, &configRsp);
3,181✔
268
  if (contLen < 0) {
3,181!
UNCOV
269
    code = contLen;
×
270
    goto _OVER;
×
271
  }
272
  void *pHead = rpcMallocCont(contLen);
3,181✔
273
  if (pHead == NULL) {
3,181!
UNCOV
274
    code = TSDB_CODE_OUT_OF_MEMORY;
×
275
    goto _OVER;
×
276
  }
277
  contLen = tSerializeSConfigRsp(pHead, contLen, &configRsp);
3,181✔
278
  if (contLen < 0) {
3,181!
UNCOV
279
    rpcFreeCont(pHead);
×
UNCOV
280
    code = contLen;
×
UNCOV
281
    goto _OVER;
×
282
  }
283
  pReq->info.rspLen = contLen;
3,181✔
284
  pReq->info.rsp = pHead;
3,181✔
285

286
_OVER:
3,181✔
287
  if (code != 0) {
3,181!
UNCOV
288
    mError("failed to process config req, since %s", tstrerror(code));
×
289
  }
290
  sdbRelease(pMnode->pSdb, vObj);
3,181✔
291
  cfgArrayCleanUp(array);
3,181✔
292
  tFreeSConfigReq(&configReq);
3,181✔
293
  return code;
3,181✔
294
}
295

296
int32_t mndInitWriteCfg(SMnode *pMnode) {
1,917✔
297
  int    code = 0;
1,917✔
298
  size_t sz = 0;
1,917✔
299

300
  mInfo("init write cfg to sdb");
1,917!
301
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_NOTHING, NULL, "init-write-config");
1,917✔
302
  if (pTrans == NULL) {
1,917!
UNCOV
303
    mError("failed to init write cfg in create trans, since %s", terrstr());
×
UNCOV
304
    goto _OVER;
×
305
  }
306

307
  // encode mnd config version
308
  SConfigObj versionObj = mndInitConfigVersion();
1,917✔
309
  if ((code = mndSetCreateConfigCommitLogs(pTrans, &versionObj)) != 0) {
1,917!
UNCOV
310
    mError("failed to init mnd config version, since %s", tstrerror(code));
×
UNCOV
311
    tFreeSConfigObj(&versionObj);
×
UNCOV
312
    goto _OVER;
×
313
  }
314
  tFreeSConfigObj(&versionObj);
1,917✔
315
  sz = taosArrayGetSize(taosGetGlobalCfg(tsCfg));
1,917✔
316

317
  for (int i = 0; i < sz; ++i) {
179,454✔
318
    SConfigItem *item = taosArrayGet(taosGetGlobalCfg(tsCfg), i);
177,537✔
319
    SConfigObj   obj;
320
    if ((code = mndInitConfigObj(item, &obj)) != 0) {
177,537!
321
      goto _OVER;
×
322
    }
323
    if ((code = mndSetCreateConfigCommitLogs(pTrans, &obj)) != 0) {
177,537!
UNCOV
324
      mError("failed to init mnd config:%s, since %s", item->name, tstrerror(code));
×
UNCOV
325
      tFreeSConfigObj(&obj);
×
UNCOV
326
      goto _OVER;
×
327
    }
328
    tFreeSConfigObj(&obj);
177,537✔
329
  }
330
  if ((code = mndTransPrepare(pMnode, pTrans)) != 0) goto _OVER;
1,917!
331

332
_OVER:
1,917✔
333
  if (code != 0) {
1,917!
UNCOV
334
    mError("failed to init write cfg, since %s", tstrerror(code));
×
335
  }
336
  mndTransDrop(pTrans);
1,917✔
337
  return code;
1,917✔
338
}
339

340
int32_t mndSendRebuildReq(SMnode *pMnode) {
1,001✔
341
  int32_t code = 0;
1,001✔
342

343
  SRpcMsg rpcMsg = {.pCont = NULL,
1,001✔
344
                    .contLen = 0,
345
                    .msgType = TDMT_MND_CONFIG_SDB,
346
                    .info.ahandle = 0,
347
                    .info.notFreeAhandle = 1,
348
                    .info.refId = 0,
349
                    .info.noResp = 0,
350
                    .info.handle = 0};
351
  SEpSet  epSet = {0};
1,001✔
352

353
  mndGetMnodeEpSet(pMnode, &epSet);
1,001✔
354

355
  code = tmsgSendReq(&epSet, &rpcMsg);
1,001✔
356
  if (code != 0) {
1,001!
UNCOV
357
    mError("failed to send rebuild config req, since %s", tstrerror(code));
×
358
  }
359
  return code;
1,001✔
360
}
361

362
static int32_t mndTryRebuildConfigSdb(SRpcMsg *pReq) {
991✔
363
  SMnode *pMnode = pReq->info.node;
991✔
364
  if (!mndIsLeader(pMnode)) {
991!
UNCOV
365
    return TSDB_CODE_SUCCESS;
×
366
  }
367

368
  int32_t     code = 0;
991✔
369
  SConfigObj *vObj = NULL;
991✔
370
  SArray     *addArray = NULL;
991✔
371
  SArray     *deleteArray = NULL;
991✔
372

373
  vObj = sdbAcquire(pMnode->pSdb, SDB_CFG, "tsmmConfigVersion");
991✔
374
  if (vObj == NULL) {
991!
UNCOV
375
    code = mndInitWriteCfg(pMnode);
×
UNCOV
376
    if (code < 0) {
×
UNCOV
377
      mError("failed to init write cfg, since %s", tstrerror(code));
×
378
    } else {
UNCOV
379
      mInfo("failed to acquire mnd config version, try to rebuild config in sdb.");
×
380
    }
UNCOV
381
    goto _exit;
×
382
  }
383

384
  addArray = taosArrayInit(4, sizeof(SConfigObj));
991✔
385
  deleteArray = taosArrayInit(4, sizeof(SConfigObj));
991✔
386
  if (addArray == NULL || deleteArray == NULL) {
991!
UNCOV
387
    code = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
388
    goto _exit;
×
389
  }
390

391
  // Find configs to add and delete
392
  if ((code = mndFindConfigsToAdd(pMnode, addArray)) != 0) {
991!
393
    mError("failed to find configs to add, since %s", tstrerror(code));
×
394
    goto _exit;
×
395
  }
396

397
  if ((code = mndFindConfigsToDelete(pMnode, deleteArray)) != 0) {
991!
398
    mError("failed to find configs to delete, since %s", tstrerror(code));
×
399
    goto _exit;
×
400
  }
401

402
  // Execute the sync transaction
403
  if ((code = mndExecuteConfigSyncTrans(pMnode, addArray, deleteArray)) != 0) {
991!
UNCOV
404
    mError("failed to execute config sync transaction, since %s", tstrerror(code));
×
UNCOV
405
    goto _exit;
×
406
  }
407

408
_exit:
991✔
409
  if (code != 0) {
991!
UNCOV
410
    mError("failed to try rebuild config in sdb, since %s", tstrerror(code));
×
411
  }
412
  sdbRelease(pMnode->pSdb, vObj);
991✔
413
  cfgObjArrayCleanUp(addArray);
991✔
414
  cfgObjArrayCleanUp(deleteArray);
991✔
415
  TAOS_RETURN(code);
991✔
416
}
417

418
int32_t mndSetCreateConfigCommitLogs(STrans *pTrans, SConfigObj *item) {
179,816✔
419
  int32_t  code = 0;
179,816✔
420
  SSdbRaw *pCommitRaw = mnCfgActionEncode(item);
179,816✔
421
  if (pCommitRaw == NULL) {
179,816!
UNCOV
422
    code = terrno;
×
UNCOV
423
    TAOS_RETURN(code);
×
424
  }
425
  if ((code = mndTransAppendCommitlog(pTrans, pCommitRaw) != 0)) TAOS_RETURN(code);
179,816!
426
  if ((code = sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY)) != 0) TAOS_RETURN(code);
179,816!
427
  return TSDB_CODE_SUCCESS;
179,816✔
428
}
429

UNCOV
430
int32_t mndSetDeleteConfigCommitLogs(STrans *pTrans, SConfigObj *item) {
×
UNCOV
431
  int32_t  code = 0;
×
UNCOV
432
  SSdbRaw *pCommitRaw = mnCfgActionEncode(item);
×
UNCOV
433
  if (pCommitRaw == NULL) {
×
UNCOV
434
    code = terrno;
×
UNCOV
435
    TAOS_RETURN(code);
×
436
  }
UNCOV
437
  if ((code = mndTransAppendCommitlog(pTrans, pCommitRaw) != 0)) TAOS_RETURN(code);
×
UNCOV
438
  if ((code = sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED)) != 0) TAOS_RETURN(code);
×
UNCOV
439
  return TSDB_CODE_SUCCESS;
×
440
}
441

442
static int32_t mndFindConfigsToAdd(SMnode *pMnode, SArray *addArray) {
991✔
443
  int32_t code = 0;
991✔
444
  int32_t sz = taosArrayGetSize(taosGetGlobalCfg(tsCfg));
991✔
445

446
  for (int i = 0; i < sz; ++i) {
93,154✔
447
    SConfigItem *item = taosArrayGet(taosGetGlobalCfg(tsCfg), i);
92,163✔
448
    SConfigObj  *obj = sdbAcquire(pMnode->pSdb, SDB_CFG, item->name);
92,163✔
449
    if (obj == NULL) {
92,163!
UNCOV
450
      mInfo("config:%s, not exist in sdb, will add it", item->name);
×
451
      SConfigObj newObj;
UNCOV
452
      if ((code = mndInitConfigObj(item, &newObj)) != 0) {
×
UNCOV
453
        TAOS_RETURN(code);
×
454
      }
UNCOV
455
      if (NULL == taosArrayPush(addArray, &newObj)) {
×
UNCOV
456
        tFreeSConfigObj(&newObj);
×
UNCOV
457
        TAOS_RETURN(terrno);
×
458
      }
459
    } else {
460
      sdbRelease(pMnode->pSdb, obj);
92,163✔
461
    }
462
  }
463

464
  TAOS_RETURN(TSDB_CODE_SUCCESS);
991✔
465
}
466

467
static int32_t mndFindConfigsToDelete(SMnode *pMnode, SArray *deleteArray) {
991✔
468
  int32_t     code = 0;
991✔
469
  int32_t     sz = taosArrayGetSize(taosGetGlobalCfg(tsCfg));
991✔
470
  SSdb       *pSdb = pMnode->pSdb;
991✔
471
  void       *pIter = NULL;
991✔
472
  SConfigObj *obj = NULL;
991✔
473

474
  while (1) {
93,154✔
475
    pIter = sdbFetch(pSdb, SDB_CFG, pIter, (void **)&obj);
94,145✔
476
    if (pIter == NULL) break;
94,145✔
477
    if (obj == NULL) {
93,154!
UNCOV
478
      code = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
479
      sdbCancelFetch(pSdb, pIter);
×
UNCOV
480
      TAOS_RETURN(code);
×
481
    }
482

483
    // Skip the version config
484
    if (strcasecmp(obj->name, "tsmmConfigVersion") == 0) {
93,154✔
485
      sdbRelease(pSdb, obj);
991✔
486
      continue;
991✔
487
    }
488

489
    // Check if this config exists in global config
490
    bool existsInGlobal = false;
92,163✔
491
    for (int i = 0; i < sz; ++i) {
4,331,661!
492
      SConfigItem *item = taosArrayGet(taosGetGlobalCfg(tsCfg), i);
4,331,661✔
493
      if (strcasecmp(obj->name, item->name) == 0) {
4,331,661✔
494
        existsInGlobal = true;
92,163✔
495
        break;
92,163✔
496
      }
497
    }
498

499
    if (!existsInGlobal) {
92,163!
UNCOV
500
      mInfo("config:%s, not exist in global config, will delete it from sdb", obj->name);
×
UNCOV
501
      SConfigObj deleteObj = {0};
×
UNCOV
502
      tstrncpy(deleteObj.name, obj->name, CFG_NAME_MAX_LEN);
×
503
      deleteObj.dtype = obj->dtype;
×
504

505
      // Copy the value based on type
506
      switch (obj->dtype) {
×
UNCOV
507
        case CFG_DTYPE_BOOL:
×
UNCOV
508
          deleteObj.bval = obj->bval;
×
UNCOV
509
          break;
×
UNCOV
510
        case CFG_DTYPE_INT32:
×
UNCOV
511
          deleteObj.i32 = obj->i32;
×
UNCOV
512
          break;
×
UNCOV
513
        case CFG_DTYPE_INT64:
×
UNCOV
514
          deleteObj.i64 = obj->i64;
×
UNCOV
515
          break;
×
UNCOV
516
        case CFG_DTYPE_FLOAT:
×
517
        case CFG_DTYPE_DOUBLE:
UNCOV
518
          deleteObj.fval = obj->fval;
×
UNCOV
519
          break;
×
UNCOV
520
        case CFG_DTYPE_STRING:
×
521
        case CFG_DTYPE_DIR:
522
        case CFG_DTYPE_LOCALE:
523
        case CFG_DTYPE_CHARSET:
524
        case CFG_DTYPE_TIMEZONE:
UNCOV
525
          deleteObj.str = taosStrdup(obj->str);
×
UNCOV
526
          if (deleteObj.str == NULL) {
×
UNCOV
527
            sdbCancelFetch(pSdb, pIter);
×
UNCOV
528
            sdbRelease(pSdb, obj);
×
UNCOV
529
            TAOS_RETURN(terrno);
×
530
          }
UNCOV
531
          break;
×
UNCOV
532
        default:
×
UNCOV
533
          break;
×
534
      }
535

536
      if (NULL == taosArrayPush(deleteArray, &deleteObj)) {
×
UNCOV
537
        tFreeSConfigObj(&deleteObj);
×
UNCOV
538
        sdbCancelFetch(pSdb, pIter);
×
UNCOV
539
        sdbRelease(pSdb, obj);
×
UNCOV
540
        TAOS_RETURN(terrno);
×
541
      }
542
    }
543

544
    sdbRelease(pSdb, obj);
92,163✔
545
  }
546

547
  TAOS_RETURN(TSDB_CODE_SUCCESS);
991✔
548
}
549

550
static int32_t mndExecuteConfigSyncTrans(SMnode *pMnode, SArray *addArray, SArray *deleteArray) {
991✔
551
  int32_t addSize = taosArrayGetSize(addArray);
991✔
552
  int32_t deleteSize = taosArrayGetSize(deleteArray);
991✔
553

554
  if (addSize == 0 && deleteSize == 0) {
991!
555
    return TSDB_CODE_SUCCESS;
991✔
556
  }
557

UNCOV
558
  const char *transName = "sync-config";
×
559
  if (addSize > 0 && deleteSize > 0) {
×
560
    transName = "sync-config";
×
UNCOV
561
  } else if (addSize > 0) {
×
562
    transName = "add-config";
×
563
  } else {
UNCOV
564
    transName = "delete-config";
×
565
  }
566

567
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_NOTHING, NULL, transName);
×
UNCOV
568
  if (pTrans == NULL) {
×
UNCOV
569
    TAOS_RETURN(terrno);
×
570
  }
571

572
  int32_t code = 0;
×
573

574
  // Add new configs
UNCOV
575
  for (int i = 0; i < addSize; ++i) {
×
UNCOV
576
    SConfigObj *AddObj = taosArrayGet(addArray, i);
×
UNCOV
577
    if ((code = mndSetCreateConfigCommitLogs(pTrans, AddObj)) != 0) {
×
UNCOV
578
      mndTransDrop(pTrans);
×
UNCOV
579
      TAOS_RETURN(code);
×
580
    }
581
  }
582

583
  // Delete obsolete configs
UNCOV
584
  for (int i = 0; i < deleteSize; ++i) {
×
UNCOV
585
    SConfigObj *DelObj = taosArrayGet(deleteArray, i);
×
UNCOV
586
    if ((code = mndSetDeleteConfigCommitLogs(pTrans, DelObj)) != 0) {
×
587
      mndTransDrop(pTrans);
×
588
      TAOS_RETURN(code);
×
589
    }
590
  }
591

UNCOV
592
  if ((code = mndTransPrepare(pMnode, pTrans)) != 0) {
×
UNCOV
593
    mndTransDrop(pTrans);
×
UNCOV
594
    TAOS_RETURN(code);
×
595
  }
596

UNCOV
597
  mInfo("sync config to sdb, add nums:%d, delete nums:%d", addSize, deleteSize);
×
UNCOV
598
  mndTransDrop(pTrans);
×
UNCOV
599
  TAOS_RETURN(TSDB_CODE_SUCCESS);
×
600
}
601

602
static int32_t mndMCfg2DCfg(SMCfgDnodeReq *pMCfgReq, SDCfgDnodeReq *pDCfgReq) {
466✔
603
  int32_t code = 0;
466✔
604
  char   *p = pMCfgReq->config;
466✔
605
  while (*p) {
6,869✔
606
    if (*p == ' ') {
6,717✔
607
      break;
314✔
608
    }
609
    p++;
6,403✔
610
  }
611

612
  size_t optLen = p - pMCfgReq->config;
466✔
613
  tstrncpy(pDCfgReq->config, pMCfgReq->config, sizeof(pDCfgReq->config));
466✔
614
  pDCfgReq->config[optLen] = 0;
466✔
615

616
  if (' ' == pMCfgReq->config[optLen]) {
466✔
617
    // 'key value'
618
    if (strlen(pMCfgReq->value) != 0) goto _err;
314!
619
    tstrncpy(pDCfgReq->value, p + 1, sizeof(pDCfgReq->value));
314✔
620
  } else {
621
    // 'key' 'value'
622
    if (strlen(pMCfgReq->value) == 0) goto _err;
152✔
623
    tstrncpy(pDCfgReq->value, pMCfgReq->value, sizeof(pDCfgReq->value));
147✔
624
  }
625

626
  TAOS_RETURN(code);
461✔
627

628
_err:
5✔
629
  mError("dnode:%d, failed to config since invalid conf:%s", pMCfgReq->dnodeId, pMCfgReq->config);
5!
630
  code = TSDB_CODE_INVALID_CFG;
5✔
631
  TAOS_RETURN(code);
5✔
632
}
633

634
static int32_t mndSendCfgDnodeReq(SMnode *pMnode, int32_t dnodeId, SDCfgDnodeReq *pDcfgReq) {
375✔
635
  int32_t code = -1;
375✔
636
  SSdb   *pSdb = pMnode->pSdb;
375✔
637
  void   *pIter = NULL;
375✔
638

639
  int64_t curMs = taosGetTimestampMs();
375✔
640

641
  while (1) {
846✔
642
    SDnodeObj *pDnode = NULL;
1,221✔
643
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
1,221✔
644
    if (pIter == NULL) break;
1,221✔
645

646
    if (pDnode->id == dnodeId || dnodeId == -1 || dnodeId == 0) {
849!
647
      bool online = mndIsDnodeOnline(pDnode, curMs);
671✔
648
      if (!online) {
671✔
649
        mWarn("dnode:%d, is offline, skip to send config req", pDnode->id);
2!
650
        continue;
2✔
651
      }
652
      SEpSet  epSet = mndGetDnodeEpset(pDnode);
669✔
653
      int32_t bufLen = tSerializeSDCfgDnodeReq(NULL, 0, pDcfgReq);
669✔
654
      void   *pBuf = rpcMallocCont(bufLen);
669✔
655

656
      if (pBuf == NULL) {
669!
UNCOV
657
        sdbCancelFetch(pMnode->pSdb, pIter);
×
UNCOV
658
        sdbRelease(pMnode->pSdb, pDnode);
×
UNCOV
659
        code = TSDB_CODE_OUT_OF_MEMORY;
×
660
        return code;
3✔
661
      }
662

663
      if ((bufLen = tSerializeSDCfgDnodeReq(pBuf, bufLen, pDcfgReq)) <= 0) {
669!
UNCOV
664
        sdbCancelFetch(pMnode->pSdb, pIter);
×
UNCOV
665
        sdbRelease(pMnode->pSdb, pDnode);
×
UNCOV
666
        code = bufLen;
×
UNCOV
667
        rpcFreeCont(pBuf);
×
UNCOV
668
        return code;
×
669
      }
670

671
      mInfo("dnode:%d, send config req to dnode, config:%s value:%s", pDnode->id, pDcfgReq->config, pDcfgReq->value);
669!
672
      SRpcMsg rpcMsg = {.msgType = TDMT_DND_CONFIG_DNODE, .pCont = pBuf, .contLen = bufLen};
669✔
673
      SRpcMsg rpcRsp = {0};
669✔
674

675
      code = rpcSendRecvWithTimeout(pMnode->msgCb.statusRpc, &epSet, &rpcMsg, &rpcRsp, NULL, CFG_ALTER_TIMEOUT);
669✔
676
      if (code != 0) {
669!
UNCOV
677
        mError("failed to send config req to dnode:%d, since %s", pDnode->id, tstrerror(code));
×
UNCOV
678
        sdbCancelFetch(pMnode->pSdb, pIter);
×
UNCOV
679
        sdbRelease(pMnode->pSdb, pDnode);
×
UNCOV
680
        return code;
×
681
      }
682

683
      code = rpcRsp.code;
669✔
684
      if (code != 0) {
669✔
685
        mError("failed to alter config %s,on dnode:%d, since %s", pDcfgReq->config, pDnode->id, tstrerror(code));
3!
686
        sdbCancelFetch(pMnode->pSdb, pIter);
3✔
687
        sdbRelease(pMnode->pSdb, pDnode);
3✔
688
        return code;
3✔
689
      }
690
      rpcFreeCont(rpcRsp.pCont);
666✔
691
    }
692
    sdbRelease(pSdb, pDnode);
844✔
693
  }
694

695
  if (code == -1) {
372✔
696
    code = TSDB_CODE_MND_DNODE_NOT_EXIST;
10✔
697
  }
698
  TAOS_RETURN(code);
372✔
699
}
700

701
static int32_t mndProcessConfigDnodeReq(SRpcMsg *pReq) {
477✔
702
  int32_t       code = 0;
477✔
703
  int32_t       lino = -1;
477✔
704
  SMnode       *pMnode = pReq->info.node;
477✔
705
  SMCfgDnodeReq cfgReq = {0};
477✔
706
  SConfigObj   *vObj = sdbAcquire(pMnode->pSdb, SDB_CFG, "tsmmConfigVersion");
477✔
707
  if (vObj == NULL) {
477!
UNCOV
708
    code = TSDB_CODE_SDB_OBJ_NOT_THERE;
×
UNCOV
709
    mInfo("failed to acquire mnd config version, since %s", tstrerror(code));
×
UNCOV
710
    goto _err_out;
×
711
  }
712

713
  TAOS_CHECK_RETURN(tDeserializeSMCfgDnodeReq(pReq->pCont, pReq->contLen, &cfgReq));
477!
714
  int8_t updateIpWhiteList = 0;
477✔
715
  mInfo("dnode:%d, start to config, option:%s, value:%s", cfgReq.dnodeId, cfgReq.config, cfgReq.value);
477!
716
  if ((code = mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_CONFIG_DNODE)) != 0) {
477✔
717
    goto _err_out;
7✔
718
  }
719

720
  SDCfgDnodeReq dcfgReq = {0};
470✔
721
  if (strcasecmp(cfgReq.config, "resetlog") == 0) {
470✔
722
    tstrncpy(dcfgReq.config, "resetlog", 9);
4✔
723
    goto _send_req;
4✔
724
#ifdef TD_ENTERPRISE
725
  } else if (strncasecmp(cfgReq.config, "s3blocksize", 12) == 0) {
466!
UNCOV
726
    int32_t optLen = strlen("s3blocksize");
×
UNCOV
727
    int32_t flag = -1;
×
UNCOV
728
    int32_t code = mndMCfgGetValInt32(&cfgReq, optLen, &flag);
×
UNCOV
729
    if (code < 0) {
×
UNCOV
730
      goto _err_out;
×
731
    }
732

UNCOV
733
    if (flag > 1024 * 1024 || (flag > -1 && flag < 1024) || flag < -1) {
×
UNCOV
734
      mError("dnode:%d, failed to config s3blocksize since value:%d. Valid range: -1 or [1024, 1024 * 1024]",
×
735
             cfgReq.dnodeId, flag);
UNCOV
736
      code = TSDB_CODE_INVALID_CFG;
×
UNCOV
737
      goto _err_out;
×
738
    }
739

UNCOV
740
    tstrncpy(dcfgReq.config, "s3blocksize", 12);
×
UNCOV
741
    snprintf(dcfgReq.value, TSDB_DNODE_VALUE_LEN, "%d", flag);
×
742
#endif
743
  } else {
744
    TAOS_CHECK_GOTO(mndMCfg2DCfg(&cfgReq, &dcfgReq), &lino, _err_out);
466✔
745
    if (strlen(dcfgReq.config) > TSDB_DNODE_CONFIG_LEN) {
461!
746
      mError("dnode:%d, failed to config since config is too long", cfgReq.dnodeId);
×
747
      code = TSDB_CODE_INVALID_CFG;
×
UNCOV
748
      goto _err_out;
×
749
    }
750
    if (strncasecmp(dcfgReq.config, "enableWhiteList", strlen("enableWhiteList")) == 0) {
461✔
751
      updateIpWhiteList = 1;
6✔
752
    }
753

754
    CfgAlterType alterType = (cfgReq.dnodeId == 0 || cfgReq.dnodeId == -1) ? CFG_ALTER_ALL_DNODES : CFG_ALTER_DNODE;
461!
755
    TAOS_CHECK_GOTO(cfgCheckRangeForDynUpdate(taosGetCfg(), dcfgReq.config, dcfgReq.value, true, alterType), &lino,
461✔
756
                    _err_out);
757
  }
758
  SConfigItem *pItem = cfgGetItem(taosGetCfg(), dcfgReq.config);
371✔
759
  // Update config in sdb.
760
  if (pItem == NULL) {
371!
UNCOV
761
    mError("failed to find config:%s while process config dnode req", cfgReq.config);
×
762
    code = TSDB_CODE_CFG_NOT_FOUND;
×
UNCOV
763
    goto _err_out;
×
764
  }
765
  if (pItem->category == CFG_CATEGORY_GLOBAL) {
371✔
766
    TAOS_CHECK_GOTO(mndConfigUpdateTrans(pMnode, dcfgReq.config, dcfgReq.value, pItem->dtype, ++vObj->i32), &lino,
181!
767
                    _err_out);
768
  }
769
_send_req :
371✔
770

771
{  // audit
772
  char obj[50] = {0};
375✔
773
  (void)tsnprintf(obj, sizeof(obj), "%d", cfgReq.dnodeId);
375✔
774

775
  auditRecord(pReq, pMnode->clusterId, "alterDnode", obj, "", cfgReq.sql, cfgReq.sqlLen);
375✔
776
}
777
  dcfgReq.version = vObj->i32;
375✔
778
  code = mndSendCfgDnodeReq(pMnode, cfgReq.dnodeId, &dcfgReq);
375✔
779
  if (code != 0) {
375✔
780
    mError("failed to send config req to dnode:%d, since %s", cfgReq.dnodeId, tstrerror(code));
13!
781
    goto _err_out;
13✔
782
  }
783
  // dont care suss or succ;
784
  if (updateIpWhiteList) mndRefreshUserIpWhiteList(pMnode);
362✔
785
  tFreeSMCfgDnodeReq(&cfgReq);
362✔
786
  sdbRelease(pMnode->pSdb, vObj);
362✔
787
  TAOS_RETURN(code);
362✔
788

789
_err_out:
115✔
790
  mError("failed to process config dnode req, since %s", tstrerror(code));
115!
791
  tFreeSMCfgDnodeReq(&cfgReq);
115✔
792
  sdbRelease(pMnode->pSdb, vObj);
115✔
793
  TAOS_RETURN(code);
115✔
794
}
795

UNCOV
796
static int32_t mndProcessConfigDnodeRsp(SRpcMsg *pRsp) {
×
UNCOV
797
  mInfo("config rsp from dnode");
×
UNCOV
798
  return 0;
×
799
}
800

801
static int32_t mndTryRebuildConfigSdbRsp(SRpcMsg *pRsp) {
1,001✔
802
  mInfo("rebuild config sdb rsp");
1,001!
803
  return 0;
1,001✔
804
}
805

806
// get int32_t value from 'SMCfgDnodeReq'
807
static int32_t mndMCfgGetValInt32(SMCfgDnodeReq *pMCfgReq, int32_t optLen, int32_t *pOutValue) {
×
UNCOV
808
  int32_t code = 0;
×
UNCOV
809
  if (' ' != pMCfgReq->config[optLen] && 0 != pMCfgReq->config[optLen]) {
×
UNCOV
810
    goto _err;
×
811
  }
812

UNCOV
813
  if (' ' == pMCfgReq->config[optLen]) {
×
814
    // 'key value'
UNCOV
815
    if (strlen(pMCfgReq->value) != 0) goto _err;
×
UNCOV
816
    *pOutValue = taosStr2Int32(pMCfgReq->config + optLen + 1, NULL, 10);
×
817
  } else {
818
    // 'key' 'value'
819
    if (strlen(pMCfgReq->value) == 0) goto _err;
×
820
    *pOutValue = taosStr2Int32(pMCfgReq->value, NULL, 10);
×
821
  }
822

UNCOV
823
  TAOS_RETURN(code);
×
824

UNCOV
825
_err:
×
UNCOV
826
  mError(" failed to set config since:%s", tstrerror(code));
×
UNCOV
827
  TAOS_RETURN(code);
×
828
}
829

830
static int32_t mndConfigUpdateTrans(SMnode *pMnode, const char *name, char *pValue, ECfgDataType dtype,
181✔
831
                                    int32_t tsmmConfigVersion) {
832
  int32_t     code = -1;
181✔
833
  int32_t     lino = -1;
181✔
834
  SConfigObj *pVersion = taosMemoryMalloc(sizeof(SConfigObj)), *pObj = taosMemoryMalloc(sizeof(SConfigObj));
181!
835
  if (pVersion == NULL || pObj == NULL) {
181!
UNCOV
836
    code = terrno;
×
UNCOV
837
    goto _OVER;
×
838
  }
839
  tstrncpy(pVersion->name, "tsmmConfigVersion", CFG_NAME_MAX_LEN);
181✔
840
  pVersion->dtype = CFG_DTYPE_INT32;
181✔
841
  pVersion->i32 = tsmmConfigVersion;
181✔
842

843
  pObj->dtype = dtype;
181✔
844
  tstrncpy(pObj->name, name, CFG_NAME_MAX_LEN);
181✔
845

846
  TAOS_CHECK_GOTO(mndUpdateObj(pObj, name, pValue), &lino, _OVER);
181!
847
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, NULL, "update-config");
181✔
848
  if (pTrans == NULL) {
181!
849
    code = terrno;
×
850
    goto _OVER;
×
851
  }
852
  mInfo("trans:%d, used to update config:%s to value:%s", pTrans->id, name, pValue);
181!
853
  TAOS_CHECK_GOTO(mndSetCreateConfigCommitLogs(pTrans, pVersion), &lino, _OVER);
181!
854
  TAOS_CHECK_GOTO(mndSetCreateConfigCommitLogs(pTrans, pObj), &lino, _OVER);
181!
855
  if ((code = mndTransPrepare(pMnode, pTrans)) != 0) goto _OVER;
181!
856
  code = 0;
181✔
857
_OVER:
181✔
858
  if (code != 0) {
181!
UNCOV
859
    mError("failed to update config:%s to value:%s, since %s", name, pValue, tstrerror(code));
×
860
  }
861
  mndTransDrop(pTrans);
181✔
862
  tFreeSConfigObj(pVersion);
181✔
863
  taosMemoryFree(pVersion);
181!
864
  tFreeSConfigObj(pObj);
181✔
865
  taosMemoryFree(pObj);
181!
866
  return code;
181✔
867
}
868

869
static int32_t initConfigArrayFromSdb(SMnode *pMnode, SArray *array) {
2,539✔
870
  int32_t     code = 0;
2,539✔
871
  SSdb       *pSdb = pMnode->pSdb;
2,539✔
872
  void       *pIter = NULL;
2,539✔
873
  SConfigObj *obj = NULL;
2,539✔
874

875
  while (1) {
236,806✔
876
    pIter = sdbFetch(pSdb, SDB_CFG, pIter, (void **)&obj);
239,345✔
877
    if (pIter == NULL) break;
239,345✔
878
    if (obj == NULL) {
236,806!
UNCOV
879
      code = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
880
      goto _exit;
×
881
    }
882
    if (strcasecmp(obj->name, "tsmmConfigVersion") == 0) {
236,806✔
883
      sdbRelease(pSdb, obj);
2,539✔
884
      continue;
2,539✔
885
    }
886
    SConfigItem item = {0};
234,267✔
887
    item.dtype = obj->dtype;
234,267✔
888
    item.name = taosStrdup(obj->name);
234,267!
889
    if (item.name == NULL) {
234,267!
UNCOV
890
      code = terrno;
×
UNCOV
891
      sdbCancelFetch(pSdb, pIter);
×
UNCOV
892
      sdbRelease(pSdb, obj);
×
893
      goto _exit;
×
894
    }
895
    switch (obj->dtype) {
234,267!
UNCOV
896
      case CFG_DTYPE_NONE:
×
UNCOV
897
        break;
×
898
      case CFG_DTYPE_BOOL:
50,380✔
899
        item.bval = obj->bval;
50,380✔
900
        break;
50,380✔
901
      case CFG_DTYPE_INT32:
120,912✔
902
        item.i32 = obj->i32;
120,912✔
903
        break;
120,912✔
904
      case CFG_DTYPE_INT64:
20,152✔
905
        item.i64 = obj->i64;
20,152✔
906
        break;
20,152✔
907
      case CFG_DTYPE_FLOAT:
7,557✔
908
      case CFG_DTYPE_DOUBLE:
909
        item.fval = obj->fval;
7,557✔
910
        break;
7,557✔
911
      case CFG_DTYPE_STRING:
35,266✔
912
      case CFG_DTYPE_DIR:
913
      case CFG_DTYPE_LOCALE:
914
      case CFG_DTYPE_CHARSET:
915
      case CFG_DTYPE_TIMEZONE:
916
        item.str = taosStrdup(obj->str);
35,266!
917
        if (item.str == NULL) {
35,266!
UNCOV
918
          sdbCancelFetch(pSdb, pIter);
×
UNCOV
919
          sdbRelease(pSdb, obj);
×
UNCOV
920
          code = terrno;
×
UNCOV
921
          goto _exit;
×
922
        }
923
        break;
35,266✔
924
    }
925
    if (taosArrayPush(array, &item) == NULL) {
234,267!
UNCOV
926
      sdbCancelFetch(pSdb, pIter);
×
UNCOV
927
      sdbRelease(pSdb, obj);
×
UNCOV
928
      code = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
929
      goto _exit;
×
930
      break;
931
    }
932
    sdbRelease(pSdb, obj);
234,267✔
933
  }
934
_exit:
2,539✔
935
  if (code != 0) {
2,539!
936
    mError("failed to init config array from sdb, since %s", tstrerror(code));
×
937
  }
938
  return code;
2,539✔
939
}
940

941
static void cfgArrayCleanUp(SArray *array) {
3,181✔
942
  if (array == NULL) {
3,181!
UNCOV
943
    return;
×
944
  }
945

946
  int32_t sz = taosArrayGetSize(array);
3,181✔
947
  for (int32_t i = 0; i < sz; ++i) {
237,448✔
948
    SConfigItem *item = taosArrayGet(array, i);
234,267✔
949
    if (item->dtype == CFG_DTYPE_STRING || item->dtype == CFG_DTYPE_DIR || item->dtype == CFG_DTYPE_LOCALE ||
234,267!
950
        item->dtype == CFG_DTYPE_CHARSET || item->dtype == CFG_DTYPE_TIMEZONE) {
204,039✔
951
      taosMemoryFreeClear(item->str);
35,266!
952
    }
953
    taosMemoryFreeClear(item->name);
234,267!
954
  }
955

956
  taosArrayDestroy(array);
3,181✔
957
}
958

959
static void cfgObjArrayCleanUp(SArray *array) {
1,982✔
960
  if (array == NULL) {
1,982!
UNCOV
961
    return;
×
962
  }
963
  int32_t sz = taosArrayGetSize(array);
1,982✔
964
  for (int32_t i = 0; i < sz; ++i) {
1,982!
965
    SConfigObj *obj = taosArrayGet(array, i);
×
UNCOV
966
    tFreeSConfigObj(obj);
×
967
  }
968
  taosArrayDestroy(array);
1,982✔
969
}
970

971
static SArray *initVariablesFromItems(SArray *pItems, const char* likePattern) {
36✔
972
  if (pItems == NULL) {
36!
UNCOV
973
    return NULL;
×
974
  }
975

976
  int32_t sz = taosArrayGetSize(pItems);
36✔
977

978
  SArray *pInfos = taosArrayInit(sz, sizeof(SVariablesInfo));
36✔
979
  if (pInfos == NULL) {
36!
UNCOV
980
    mError("failed to init array while init variables from items, since %s", tstrerror(terrno));
×
UNCOV
981
    return NULL;
×
982
  }
983
  for (int32_t i = 0; i < sz; ++i) {
3,384✔
984
    SConfigItem   *pItem = taosArrayGet(pItems, i);
3,348✔
985
    SVariablesInfo info = {0};
3,348✔
986
    tstrncpy(info.name, pItem->name, sizeof(info.name));
3,348✔
987
    if (likePattern != NULL && rawStrPatternMatch(pItem->name, likePattern) != TSDB_PATTERN_MATCH) {
3,348✔
988
      continue;
1,804✔
989
    }
990

991
    // init info value
992
    switch (pItem->dtype) {
1,544!
UNCOV
993
      case CFG_DTYPE_NONE:
×
UNCOV
994
        break;
×
995
      case CFG_DTYPE_BOOL:
328✔
996
        tsnprintf(info.value, sizeof(info.value), "%d", pItem->bval);
328✔
997
        break;
328✔
998
      case CFG_DTYPE_INT32:
808✔
999
        tsnprintf(info.value, sizeof(info.value), "%d", pItem->i32);
808✔
1000
        break;
808✔
1001
      case CFG_DTYPE_INT64:
128✔
1002
        tsnprintf(info.value, sizeof(info.value), "%" PRId64, pItem->i64);
128✔
1003
        break;
128✔
1004
      case CFG_DTYPE_FLOAT:
48✔
1005
      case CFG_DTYPE_DOUBLE:
1006
        tsnprintf(info.value, sizeof(info.value), "%f", pItem->fval);
48✔
1007
        break;
48✔
1008
      case CFG_DTYPE_STRING:
232✔
1009
      case CFG_DTYPE_DIR:
1010
      case CFG_DTYPE_LOCALE:
1011
      case CFG_DTYPE_CHARSET:
1012
      case CFG_DTYPE_TIMEZONE:
1013
        tsnprintf(info.value, sizeof(info.value), "%s", pItem->str);
232✔
1014
        break;
232✔
1015
    }
1016

1017
    // init info scope
1018
    switch (pItem->scope) {
1,544!
1019
      case CFG_SCOPE_SERVER:
1,304✔
1020
        tstrncpy(info.scope, "server", sizeof(info.scope));
1,304✔
1021
        break;
1,304✔
UNCOV
1022
      case CFG_SCOPE_CLIENT:
×
UNCOV
1023
        tstrncpy(info.scope, "client", sizeof(info.scope));
×
UNCOV
1024
        break;
×
1025
      case CFG_SCOPE_BOTH:
240✔
1026
        tstrncpy(info.scope, "both", sizeof(info.scope));
240✔
1027
        break;
240✔
UNCOV
1028
      default:
×
UNCOV
1029
        tstrncpy(info.scope, "unknown", sizeof(info.scope));
×
UNCOV
1030
        break;
×
1031
    }
1032
    // init info category
1033
    switch (pItem->category) {
1,544!
1034
      case CFG_CATEGORY_GLOBAL:
1,544✔
1035
        tstrncpy(info.category, "global", sizeof(info.category));
1,544✔
1036
        break;
1,544✔
UNCOV
1037
      case CFG_CATEGORY_LOCAL:
×
UNCOV
1038
        tstrncpy(info.category, "local", sizeof(info.category));
×
UNCOV
1039
        break;
×
UNCOV
1040
      default:
×
UNCOV
1041
        tstrncpy(info.category, "unknown", sizeof(info.category));
×
UNCOV
1042
        break;
×
1043
    }
1044
    if (NULL == taosArrayPush(pInfos, &info)) {
1,544!
UNCOV
1045
      mError("failed to push info to array while init variables from items,since %s", tstrerror(terrno));
×
UNCOV
1046
      taosArrayDestroy(pInfos);
×
UNCOV
1047
      return NULL;
×
1048
    }
1049
  }
1050

1051
  return pInfos;
36✔
1052
}
1053

1054
static int32_t mndProcessShowVariablesReq(SRpcMsg *pReq) {
36✔
1055
  SShowVariablesRsp rsp = {0};
36✔
1056
  int32_t           code = TSDB_CODE_SUCCESS;
36✔
1057
  SShowVariablesReq req = {0};
36✔
1058
  SArray           *array = NULL;
36✔
1059

1060
  code = tDeserializeSShowVariablesReq(pReq->pCont, pReq->contLen, &req);
36✔
1061
  if (code != 0) {
36!
UNCOV
1062
    mError("failed to deserialize config req, since %s", terrstr());
×
UNCOV
1063
    goto _OVER;
×
1064
  }
1065

1066
  if ((code = mndCheckOperPrivilege(pReq->info.node, pReq->info.conn.user, MND_OPER_SHOW_VARIABLES)) != 0) {
36!
UNCOV
1067
    goto _OVER;
×
1068
  }
1069

1070
  SVariablesInfo info = {0};
36✔
1071
  char          *likePattern = req.opType == OP_TYPE_LIKE ? req.val : NULL;
36✔
1072
  rsp.variables = initVariablesFromItems(taosGetGlobalCfg(tsCfg), likePattern);
36✔
1073
  if (rsp.variables == NULL) {
36!
UNCOV
1074
    code = terrno;
×
UNCOV
1075
    goto _OVER;
×
1076
  }
1077
  int32_t rspLen = tSerializeSShowVariablesRsp(NULL, 0, &rsp);
36✔
1078
  void   *pRsp = rpcMallocCont(rspLen);
36✔
1079
  if (pRsp == NULL) {
36!
UNCOV
1080
    code = terrno;
×
UNCOV
1081
    goto _OVER;
×
1082
  }
1083

1084
  if ((rspLen = tSerializeSShowVariablesRsp(pRsp, rspLen, &rsp)) <= 0) {
36!
UNCOV
1085
    rpcFreeCont(pRsp);
×
UNCOV
1086
    code = rspLen;
×
UNCOV
1087
    goto _OVER;
×
1088
  }
1089

1090
  pReq->info.rspLen = rspLen;
36✔
1091
  pReq->info.rsp = pRsp;
36✔
1092
  code = 0;
36✔
1093

1094
_OVER:
36✔
1095

1096
  if (code != 0) {
36!
UNCOV
1097
    mError("failed to get show variables info since %s", tstrerror(code));
×
1098
  }
1099
  tFreeSShowVariablesReq(&req);
36✔
1100
  tFreeSShowVariablesRsp(&rsp);
36✔
1101
  TAOS_RETURN(code);
36✔
1102
}
1103

UNCOV
1104
int32_t compareSConfigItem(const SConfigObj *item1, SConfigItem *item2, bool *compare) {
×
UNCOV
1105
  *compare = true;
×
UNCOV
1106
  switch (item1->dtype) {
×
UNCOV
1107
    case CFG_DTYPE_BOOL:
×
UNCOV
1108
      if (item1->bval != item2->bval) {
×
UNCOV
1109
        item2->bval = item1->bval;
×
UNCOV
1110
        *compare = false;
×
1111
      }
UNCOV
1112
      break;
×
UNCOV
1113
    case CFG_DTYPE_FLOAT:
×
UNCOV
1114
      if (item1->fval != item2->fval) {
×
UNCOV
1115
        item2->fval = item1->fval;
×
UNCOV
1116
        *compare = false;
×
1117
      }
UNCOV
1118
      break;
×
UNCOV
1119
    case CFG_DTYPE_INT32:
×
UNCOV
1120
      if (item1->i32 != item2->i32) {
×
UNCOV
1121
        item2->i32 = item1->i32;
×
UNCOV
1122
        *compare = false;
×
1123
      }
UNCOV
1124
      break;
×
UNCOV
1125
    case CFG_DTYPE_INT64:
×
UNCOV
1126
      if (item1->i64 != item2->i64) {
×
UNCOV
1127
        item2->i64 = item1->i64;
×
UNCOV
1128
        *compare = false;
×
1129
      }
UNCOV
1130
      break;
×
UNCOV
1131
    case CFG_DTYPE_STRING:
×
1132
    case CFG_DTYPE_DIR:
1133
    case CFG_DTYPE_LOCALE:
1134
    case CFG_DTYPE_CHARSET:
1135
    case CFG_DTYPE_TIMEZONE:
UNCOV
1136
      if (strcmp(item1->str, item2->str) != 0) {
×
UNCOV
1137
        item2->str = taosStrdup(item1->str);
×
UNCOV
1138
        if (item2->str == NULL) {
×
UNCOV
1139
          return TSDB_CODE_OUT_OF_MEMORY;
×
1140
        }
UNCOV
1141
        *compare = false;
×
1142
      }
UNCOV
1143
      break;
×
UNCOV
1144
    default:
×
UNCOV
1145
      *compare = false;
×
UNCOV
1146
      return TSDB_CODE_INVALID_CFG;
×
1147
  }
UNCOV
1148
  return TSDB_CODE_SUCCESS;
×
1149
}
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