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

taosdata / TDengine / #4720

08 Sep 2025 08:43AM UTC coverage: 58.139% (-0.6%) from 58.762%
#4720

push

travis-ci

web-flow
Merge pull request #32881 from taosdata/enh/add-new-windows-ci

fix(ci): update workflow reference to use new Windows CI YAML

133181 of 292179 branches covered (45.58%)

Branch coverage included in aggregate %.

201691 of 283811 relevant lines covered (71.07%)

5442780.71 hits per line

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

54.31
/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) {
1,929✔
56
  int32_t   code = 0;
1,929✔
57
  SSdbTable table = {.sdbType = SDB_CFG,
1,929✔
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);
1,929✔
68
  mndSetMsgHandle(pMnode, TDMT_MND_CONFIG_DNODE, mndProcessConfigDnodeReq);
1,929✔
69
  mndSetMsgHandle(pMnode, TDMT_DND_CONFIG_DNODE_RSP, mndProcessConfigDnodeRsp);
1,929✔
70
  mndSetMsgHandle(pMnode, TDMT_MND_SHOW_VARIABLES, mndProcessShowVariablesReq);
1,929✔
71
  mndSetMsgHandle(pMnode, TDMT_MND_CONFIG_SDB, mndTryRebuildConfigSdb);
1,929✔
72
  mndSetMsgHandle(pMnode, TDMT_MND_CONFIG_SDB_RSP, mndTryRebuildConfigSdbRsp);
1,929✔
73

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

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

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

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

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

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

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

106
  tEncoderClear(&encoder);
466,412✔
107

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

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

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

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

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

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

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

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

151
  SDB_GET_INT32(pRaw, dataPos, &tlen, _over);
181,579!
152

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

156
  SDB_GET_BINARY(pRaw, dataPos, buf, tlen, _over);
181,579!
157

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

163
  if (code < 0) {
181,579!
164
    tFreeSConfigObj(pObj);
×
165
  }
166

167
_over:
181,579✔
168
  taosMemoryFreeClear(buf);
181,579!
169

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

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

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

193
static int32_t mndCfgActionUpdate(SSdb *pSdb, SConfigObj *pOld, SConfigObj *pNew) {
580✔
194
  mTrace("cfg:%s, perform update action, old row:%p new row:%p", pOld->name, pOld, pNew);
580!
195
  switch (pNew->dtype) {
580!
196
    case CFG_DTYPE_NONE:
×
197
      break;
×
198
    case CFG_DTYPE_BOOL:
63✔
199
      pOld->bval = pNew->bval;
63✔
200
      break;
63✔
201
    case CFG_DTYPE_INT32:
471✔
202
      pOld->i32 = pNew->i32;
471✔
203
      break;
471✔
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:
13✔
212
    case CFG_DTYPE_DIR:
213
    case CFG_DTYPE_LOCALE:
214
    case CFG_DTYPE_CHARSET:
215
    case CFG_DTYPE_TIMEZONE:
216
      taosMemoryFree(pOld->str);
13!
217
      pOld->str = taosStrdup(pNew->str);
13!
218
      if (pOld->str == NULL) {
13!
219
        return terrno;
×
220
      }
221
      break;
13✔
222
  }
223
  return TSDB_CODE_SUCCESS;
580✔
224
}
225

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

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

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

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

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

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

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

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

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

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

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

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

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

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

343
  SRpcMsg rpcMsg = {.pCont = NULL,
785✔
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};
785✔
352

353
  mndGetMnodeEpSet(pMnode, &epSet);
785✔
354

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

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

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

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

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

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

397
  if ((code = mndFindConfigsToDelete(pMnode, deleteArray)) != 0) {
782!
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) {
782!
404
    mError("failed to execute config sync transaction, since %s", tstrerror(code));
×
405
    goto _exit;
×
406
  }
407

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

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

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

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

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

464
  TAOS_RETURN(TSDB_CODE_SUCCESS);
782✔
465
}
466

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

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

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

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

499
    if (!existsInGlobal) {
73,508!
500
      mInfo("config:%s, not exist in global config, will delete it from sdb", obj->name);
×
501
      SConfigObj deleteObj = {0};
×
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) {
×
507
        case CFG_DTYPE_BOOL:
×
508
          deleteObj.bval = obj->bval;
×
509
          break;
×
510
        case CFG_DTYPE_INT32:
×
511
          deleteObj.i32 = obj->i32;
×
512
          break;
×
513
        case CFG_DTYPE_INT64:
×
514
          deleteObj.i64 = obj->i64;
×
515
          break;
×
516
        case CFG_DTYPE_FLOAT:
×
517
        case CFG_DTYPE_DOUBLE:
518
          deleteObj.fval = obj->fval;
×
519
          break;
×
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:
525
          deleteObj.str = taosStrdup(obj->str);
×
526
          if (deleteObj.str == NULL) {
×
527
            sdbCancelFetch(pSdb, pIter);
×
528
            sdbRelease(pSdb, obj);
×
529
            TAOS_RETURN(terrno);
×
530
          }
531
          break;
×
532
        default:
×
533
          break;
×
534
      }
535

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

544
    sdbRelease(pSdb, obj);
73,508✔
545
  }
546

547
  TAOS_RETURN(TSDB_CODE_SUCCESS);
782✔
548
}
549

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

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

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

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

572
  int32_t code = 0;
×
573

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

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

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

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

602
static int32_t mndMCfg2DCfg(SMCfgDnodeReq *pMCfgReq, SDCfgDnodeReq *pDCfgReq) {
273✔
603
  int32_t code = 0;
273✔
604
  char   *p = pMCfgReq->config;
273✔
605
  while (*p) {
4,088✔
606
    if (*p == ' ') {
3,974✔
607
      break;
159✔
608
    }
609
    p++;
3,815✔
610
  }
611

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

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

626
  TAOS_RETURN(code);
269✔
627

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

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

639
  int64_t curMs = taosGetTimestampMs();
174✔
640

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

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

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

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

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

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

683
      code = rpcRsp.code;
182✔
684
      if (code != 0) {
182✔
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);
179✔
691
    }
692
    sdbRelease(pSdb, pDnode);
334✔
693
  }
694

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

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

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

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

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

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

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

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

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

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

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

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

813
  if (' ' == pMCfgReq->config[optLen]) {
×
814
    // 'key value'
815
    if (strlen(pMCfgReq->value) != 0) goto _err;
×
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

823
  TAOS_RETURN(code);
×
824

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

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

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

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

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

875
  while (1) {
179,868✔
876
    pIter = sdbFetch(pSdb, SDB_CFG, pIter, (void **)&obj);
181,794✔
877
    if (pIter == NULL) break;
181,794✔
878
    if (obj == NULL) {
179,868!
879
      code = TSDB_CODE_OUT_OF_MEMORY;
×
880
      goto _exit;
×
881
    }
882
    if (strcasecmp(obj->name, "tsmmConfigVersion") == 0) {
179,868✔
883
      sdbRelease(pSdb, obj);
1,926✔
884
      continue;
1,926✔
885
    }
886
    SConfigItem item = {0};
177,942✔
887
    item.dtype = obj->dtype;
177,942✔
888
    item.name = taosStrdup(obj->name);
177,942!
889
    if (item.name == NULL) {
177,942!
890
      code = terrno;
×
891
      sdbCancelFetch(pSdb, pIter);
×
892
      sdbRelease(pSdb, obj);
×
893
      goto _exit;
×
894
    }
895
    switch (obj->dtype) {
177,942!
896
      case CFG_DTYPE_NONE:
×
897
        break;
×
898
      case CFG_DTYPE_BOOL:
34,074✔
899
        item.bval = obj->bval;
34,074✔
900
        break;
34,074✔
901
      case CFG_DTYPE_INT32:
104,115✔
902
        item.i32 = obj->i32;
104,115✔
903
        break;
104,115✔
904
      case CFG_DTYPE_INT64:
11,358✔
905
        item.i64 = obj->i64;
11,358✔
906
        break;
11,358✔
907
      case CFG_DTYPE_FLOAT:
3,786✔
908
      case CFG_DTYPE_DOUBLE:
909
        item.fval = obj->fval;
3,786✔
910
        break;
3,786✔
911
      case CFG_DTYPE_STRING:
24,609✔
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);
24,609!
917
        if (item.str == NULL) {
24,609!
918
          sdbCancelFetch(pSdb, pIter);
×
919
          sdbRelease(pSdb, obj);
×
920
          code = terrno;
×
921
          goto _exit;
×
922
        }
923
        break;
24,609✔
924
    }
925
    if (taosArrayPush(array, &item) == NULL) {
177,942!
926
      sdbCancelFetch(pSdb, pIter);
×
927
      sdbRelease(pSdb, obj);
×
928
      code = TSDB_CODE_OUT_OF_MEMORY;
×
929
      goto _exit;
×
930
      break;
931
    }
932
    sdbRelease(pSdb, obj);
177,942✔
933
  }
934
_exit:
1,926✔
935
  if (code != 0) {
1,926!
936
    mError("failed to init config array from sdb, since %s", tstrerror(code));
×
937
  }
938
  return code;
1,926✔
939
}
940

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

946
  int32_t sz = taosArrayGetSize(array);
2,379✔
947
  for (int32_t i = 0; i < sz; ++i) {
180,321✔
948
    SConfigItem *item = taosArrayGet(array, i);
177,942✔
949
    if (item->dtype == CFG_DTYPE_STRING || item->dtype == CFG_DTYPE_DIR || item->dtype == CFG_DTYPE_LOCALE ||
177,942!
950
        item->dtype == CFG_DTYPE_CHARSET || item->dtype == CFG_DTYPE_TIMEZONE) {
157,119✔
951
      taosMemoryFreeClear(item->str);
24,609!
952
    }
953
    taosMemoryFreeClear(item->name);
177,942!
954
  }
955

956
  taosArrayDestroy(array);
2,379✔
957
}
958

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

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

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

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

991
    // init info value
992
    switch (pItem->dtype) {
11,119!
993
      case CFG_DTYPE_NONE:
×
994
        break;
×
995
      case CFG_DTYPE_BOOL:
2,126✔
996
        tsnprintf(info.value, sizeof(info.value), "%d", pItem->bval);
2,126✔
997
        break;
2,126✔
998
      case CFG_DTYPE_INT32:
6,512✔
999
        tsnprintf(info.value, sizeof(info.value), "%d", pItem->i32);
6,512✔
1000
        break;
6,512✔
1001
      case CFG_DTYPE_INT64:
708✔
1002
        tsnprintf(info.value, sizeof(info.value), "%" PRId64, pItem->i64);
708✔
1003
        break;
708✔
1004
      case CFG_DTYPE_FLOAT:
236✔
1005
      case CFG_DTYPE_DOUBLE:
1006
        tsnprintf(info.value, sizeof(info.value), "%f", pItem->fval);
236✔
1007
        break;
236✔
1008
      case CFG_DTYPE_STRING:
1,537✔
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);
1,537✔
1014
        break;
1,537✔
1015
    }
1016

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

1051
  return pInfos;
129✔
1052
}
1053

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

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

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

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

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

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

1094
_OVER:
129✔
1095

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

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