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

taosdata / TDengine / #4740

18 Sep 2025 04:31AM UTC coverage: 58.139% (-0.9%) from 59.028%
#4740

push

travis-ci

web-flow
fix: clear parse csv error syntax error msg (#33000)

133663 of 293099 branches covered (45.6%)

Branch coverage included in aggregate %.

0 of 2 new or added lines in 1 file covered. (0.0%)

4143 existing lines in 175 files now uncovered.

202241 of 284660 relevant lines covered (71.05%)

5584206.0 hits per line

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

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

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

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

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

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

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

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

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

106
  tEncoderClear(&encoder);
471,034✔
107

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

113
_over:
471,034✔
114
  taosMemoryFreeClear(buf);
471,034!
115
  if (code != TSDB_CODE_SUCCESS) {
471,034!
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;
471,034✔
123
  mTrace("cfg:%s, encode to raw:%p, row:%p", obj->name, pRaw, obj);
471,034✔
124
  return pRaw;
471,034✔
125
}
126

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

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

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

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

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

151
  SDB_GET_INT32(pRaw, dataPos, &tlen, _over);
183,388!
152

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

156
  SDB_GET_BINARY(pRaw, dataPos, buf, tlen, _over);
183,388!
157

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

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

167
_over:
183,388✔
168
  taosMemoryFreeClear(buf);
183,388!
169

170
  if (code != TSDB_CODE_SUCCESS) {
183,388!
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);
183,388✔
177
    terrno = 0;
183,388✔
178
    return pRow;
183,388✔
179
  }
180
}
181

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

187
static int32_t mndCfgActionDelete(SSdb *pSdb, SConfigObj *obj) {
183,388✔
188
  mTrace("cfg:%s, perform delete action, row:%p", obj->name, obj);
183,388✔
189
  tFreeSConfigObj(obj);
183,388✔
190
  return 0;
183,388✔
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); }
789✔
229

230
static int32_t mndProcessConfigReq(SRpcMsg *pReq) {
2,375✔
231
  SMnode    *pMnode = pReq->info.node;
2,375✔
232
  SConfigReq configReq = {0};
2,375✔
233
  int32_t    code = TSDB_CODE_SUCCESS;
2,375✔
234
  SArray    *array = NULL;
2,375✔
235
  bool       needFree = false;
2,375✔
236
  code = tDeserializeSConfigReq(pReq->pCont, pReq->contLen, &configReq);
2,375✔
237
  if (code != 0) {
2,375!
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,375✔
243
  if (vObj == NULL) {
2,375!
244
    mInfo("failed to acquire mnd config version, since %s", terrstr());
×
245
    goto _OVER;
×
246
  }
247

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

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

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

286
_OVER:
2,375✔
287
  if (code != 0) {
2,375!
288
    mError("failed to process config req, since %s", tstrerror(code));
×
289
  }
290
  sdbRelease(pMnode->pSdb, vObj);
2,375✔
291
  cfgArrayCleanUp(array);
2,375✔
292
  tFreeSConfigReq(&configReq);
2,375✔
293
  return code;
2,375✔
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) {
134,616✔
318
    SConfigItem *item = taosArrayGet(taosGetGlobalCfg(tsCfg), i);
133,190✔
319
    SConfigObj   obj;
320
    if ((code = mndInitConfigObj(item, &obj)) != 0) {
133,190!
321
      goto _OVER;
×
322
    }
323
    if ((code = mndSetCreateConfigCommitLogs(pTrans, &obj)) != 0) {
133,190!
324
      mError("failed to init mnd config:%s, since %s", item->name, tstrerror(code));
×
325
      tFreeSConfigObj(&obj);
×
326
      goto _OVER;
×
327
    }
328
    tFreeSConfigObj(&obj);
133,190✔
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) {
789✔
341
  int32_t code = 0;
789✔
342

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

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

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

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

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

373
  vObj = sdbAcquire(pMnode->pSdb, SDB_CFG, "tsmmConfigVersion");
784✔
374
  if (vObj == NULL) {
784!
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));
784✔
385
  deleteArray = taosArrayInit(4, sizeof(SConfigObj));
784✔
386
  if (addArray == NULL || deleteArray == NULL) {
784!
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) {
784!
393
    mError("failed to find configs to add, since %s", tstrerror(code));
×
394
    goto _exit;
×
395
  }
396

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

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

418
int32_t mndSetCreateConfigCommitLogs(STrans *pTrans, SConfigObj *item) {
134,700✔
419
  int32_t  code = 0;
134,700✔
420
  SSdbRaw *pCommitRaw = mnCfgActionEncode(item);
134,700✔
421
  if (pCommitRaw == NULL) {
134,700!
422
    code = terrno;
×
423
    TAOS_RETURN(code);
×
424
  }
425
  if ((code = mndTransAppendCommitlog(pTrans, pCommitRaw) != 0)) TAOS_RETURN(code);
134,700!
426
  if ((code = sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY)) != 0) TAOS_RETURN(code);
134,700!
427
  return TSDB_CODE_SUCCESS;
134,700✔
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) {
784✔
443
  int32_t code = 0;
784✔
444
  int32_t sz = taosArrayGetSize(taosGetGlobalCfg(tsCfg));
784✔
445

446
  for (int i = 0; i < sz; ++i) {
75,264✔
447
    SConfigItem *item = taosArrayGet(taosGetGlobalCfg(tsCfg), i);
74,480✔
448
    SConfigObj  *obj = sdbAcquire(pMnode->pSdb, SDB_CFG, item->name);
74,480✔
449
    if (obj == NULL) {
74,480!
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);
74,480✔
461
    }
462
  }
463

464
  TAOS_RETURN(TSDB_CODE_SUCCESS);
784✔
465
}
466

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

474
  while (1) {
75,264✔
475
    pIter = sdbFetch(pSdb, SDB_CFG, pIter, (void **)&obj);
76,048✔
476
    if (pIter == NULL) break;
76,048✔
477
    if (obj == NULL) {
75,264!
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) {
75,264✔
485
      sdbRelease(pSdb, obj);
784✔
486
      continue;
784✔
487
    }
488

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

499
    if (!existsInGlobal) {
74,480!
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);
74,480✔
545
  }
546

547
  TAOS_RETURN(TSDB_CODE_SUCCESS);
784✔
548
}
549

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

554
  if (addSize == 0 && deleteSize == 0) {
784!
555
    return TSDB_CODE_SUCCESS;
784✔
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) {
272✔
603
  int32_t code = 0;
272✔
604
  char   *p = pMCfgReq->config;
272✔
605
  while (*p) {
4,076✔
606
    if (*p == ' ') {
3,962✔
607
      break;
158✔
608
    }
609
    p++;
3,804✔
610
  }
611

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

616
  if (' ' == pMCfgReq->config[optLen]) {
272✔
617
    // 'key value'
618
    if (strlen(pMCfgReq->value) != 0) goto _err;
158!
619
    tstrncpy(pDCfgReq->value, p + 1, sizeof(pDCfgReq->value));
158✔
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);
268✔
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) {
173✔
635
  int32_t code = -1;
173✔
636
  SSdb   *pSdb = pMnode->pSdb;
173✔
637
  void   *pIter = NULL;
173✔
638

639
  int64_t curMs = taosGetTimestampMs();
173✔
640

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

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

656
      if (pBuf == NULL) {
181!
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) {
181!
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);
181!
672
      SRpcMsg rpcMsg = {.msgType = TDMT_DND_CONFIG_DNODE, .pCont = pBuf, .contLen = bufLen};
181✔
673
      SRpcMsg rpcRsp = {0};
181✔
674

675
      code = rpcSendRecvWithTimeout(pMnode->msgCb.statusRpc, &epSet, &rpcMsg, &rpcRsp, NULL, CFG_ALTER_TIMEOUT);
181✔
676
      if (code != 0) {
181!
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;
181✔
684
      if (code != 0) {
181✔
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);
178✔
691
    }
692
    sdbRelease(pSdb, pDnode);
333✔
693
  }
694

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

701
static int32_t mndProcessConfigDnodeReq(SRpcMsg *pReq) {
281✔
702
  int32_t       code = 0;
281✔
703
  int32_t       lino = -1;
281✔
704
  SMnode       *pMnode = pReq->info.node;
281✔
705
  SMCfgDnodeReq cfgReq = {0};
281✔
706
  SConfigObj   *vObj = sdbAcquire(pMnode->pSdb, SDB_CFG, "tsmmConfigVersion");
281✔
707
  if (vObj == NULL) {
281!
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));
281!
714
  int8_t updateIpWhiteList = 0;
281✔
715
  mInfo("dnode:%d, start to config, option:%s, value:%s", cfgReq.dnodeId, cfgReq.config, cfgReq.value);
281!
716
  if ((code = mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_CONFIG_DNODE)) != 0) {
281✔
717
    goto _err_out;
7✔
718
  }
719

720
  SDCfgDnodeReq dcfgReq = {0};
274✔
721
  if (strcasecmp(cfgReq.config, "resetlog") == 0) {
274✔
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) {
272!
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);
272✔
745
    if (strlen(dcfgReq.config) > TSDB_DNODE_CONFIG_LEN) {
268!
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) {
268✔
751
      updateIpWhiteList = 1;
4✔
752
    }
753

754
    CfgAlterType alterType = (cfgReq.dnodeId == 0 || cfgReq.dnodeId == -1) ? CFG_ALTER_ALL_DNODES : CFG_ALTER_DNODE;
268!
755
    TAOS_CHECK_GOTO(cfgCheckRangeForDynUpdate(taosGetCfg(), dcfgReq.config, dcfgReq.value, true, alterType), &lino,
268✔
756
                    _err_out);
757
  }
758
  SConfigItem *pItem = cfgGetItem(taosGetCfg(), dcfgReq.config);
171✔
759
  // Update config in sdb.
760
  if (pItem == NULL) {
171!
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) {
171✔
766
    TAOS_CHECK_GOTO(mndConfigUpdateTrans(pMnode, dcfgReq.config, dcfgReq.value, pItem->dtype, ++vObj->i32), &lino,
42!
767
                    _err_out);
768
  }
769
_send_req :
171✔
770

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

775
  auditRecord(pReq, pMnode->clusterId, "alterDnode", obj, "", cfgReq.sql, cfgReq.sqlLen);
173✔
776
}
777
  dcfgReq.version = vObj->i32;
173✔
778
  code = mndSendCfgDnodeReq(pMnode, cfgReq.dnodeId, &dcfgReq);
173✔
779
  if (code != 0) {
173✔
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);
166✔
785
  tFreeSMCfgDnodeReq(&cfgReq);
166✔
786
  sdbRelease(pMnode->pSdb, vObj);
166✔
787
  TAOS_RETURN(code);
166✔
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) {
786✔
802
  mInfo("rebuild config sdb rsp");
786!
803
  return 0;
786✔
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

856
  if (taosStrncasecmp(name, "syncTimeout", CFG_NAME_MAX_LEN) == 0) {
42!
UNCOV
857
    SConfigObj *pTmp = NULL;
×
UNCOV
858
    int32_t     syncTimeout = 0;
×
859
    char        tmp[10] = {0};
×
UNCOV
860
    sscanf(pValue, "%d", &syncTimeout);
×
861

UNCOV
862
    sprintf(tmp, "%d", syncTimeout);
×
863

UNCOV
864
    pTmp = taosMemoryMalloc(sizeof(SConfigObj));
×
UNCOV
865
    pTmp->dtype = CFG_DTYPE_INT32;
×
UNCOV
866
    tstrncpy(pTmp->name, "arbSetAssignedTimeoutMs", CFG_NAME_MAX_LEN);
×
UNCOV
867
    TAOS_CHECK_GOTO(mndUpdateObj(pTmp, name, tmp), &lino, _OVER);
×
UNCOV
868
    TAOS_CHECK_GOTO(mndSetCreateConfigCommitLogs(pTrans, pTmp), &lino, _OVER);
×
UNCOV
869
    tFreeSConfigObj(pTmp);
×
UNCOV
870
    taosMemoryFree(pTmp);
×
871

UNCOV
872
    sprintf(tmp, "%d", syncTimeout / 4);
×
873

UNCOV
874
    pTmp = taosMemoryMalloc(sizeof(SConfigObj));
×
UNCOV
875
    pTmp->dtype = CFG_DTYPE_INT32;
×
UNCOV
876
    tstrncpy(pTmp->name, "arbHeartBeatIntervalMs", CFG_NAME_MAX_LEN);
×
UNCOV
877
    TAOS_CHECK_GOTO(mndUpdateObj(pTmp, name, tmp), &lino, _OVER);
×
UNCOV
878
    TAOS_CHECK_GOTO(mndSetCreateConfigCommitLogs(pTrans, pTmp), &lino, _OVER);
×
879
    tFreeSConfigObj(pTmp);
×
880
    taosMemoryFree(pTmp);
×
881

UNCOV
882
    pTmp = taosMemoryMalloc(sizeof(SConfigObj));
×
UNCOV
883
    pTmp->dtype = CFG_DTYPE_INT32;
×
UNCOV
884
    tstrncpy(pTmp->name, "arbCheckSyncIntervalMs", CFG_NAME_MAX_LEN);
×
UNCOV
885
    TAOS_CHECK_GOTO(mndUpdateObj(pTmp, name, tmp), &lino, _OVER);
×
UNCOV
886
    TAOS_CHECK_GOTO(mndSetCreateConfigCommitLogs(pTrans, pTmp), &lino, _OVER);
×
UNCOV
887
    tFreeSConfigObj(pTmp);
×
UNCOV
888
    taosMemoryFree(pTmp);
×
889

890
    sprintf(tmp, "%d", (syncTimeout - syncTimeout / 4) / 2);
×
891

892
    pTmp = taosMemoryMalloc(sizeof(SConfigObj));
×
893
    pTmp->dtype = CFG_DTYPE_INT32;
×
UNCOV
894
    tstrncpy(pTmp->name, "syncVnodeElectIntervalMs", CFG_NAME_MAX_LEN);
×
UNCOV
895
    TAOS_CHECK_GOTO(mndUpdateObj(pTmp, name, tmp), &lino, _OVER);
×
896
    TAOS_CHECK_GOTO(mndSetCreateConfigCommitLogs(pTrans, pTmp), &lino, _OVER);
×
897
    tFreeSConfigObj(pTmp);
×
UNCOV
898
    taosMemoryFree(pTmp);
×
899

UNCOV
900
    pTmp = taosMemoryMalloc(sizeof(SConfigObj));
×
UNCOV
901
    pTmp->dtype = CFG_DTYPE_INT32;
×
UNCOV
902
    tstrncpy(pTmp->name, "syncMnodeElectIntervalMs", CFG_NAME_MAX_LEN);
×
UNCOV
903
    TAOS_CHECK_GOTO(mndUpdateObj(pTmp, name, tmp), &lino, _OVER);
×
UNCOV
904
    TAOS_CHECK_GOTO(mndSetCreateConfigCommitLogs(pTrans, pTmp), &lino, _OVER);
×
UNCOV
905
    tFreeSConfigObj(pTmp);
×
UNCOV
906
    taosMemoryFree(pTmp);
×
907

UNCOV
908
    pTmp = taosMemoryMalloc(sizeof(SConfigObj));
×
UNCOV
909
    pTmp->dtype = CFG_DTYPE_INT32;
×
UNCOV
910
    tstrncpy(pTmp->name, "statusTimeoutMs", CFG_NAME_MAX_LEN);
×
UNCOV
911
    TAOS_CHECK_GOTO(mndUpdateObj(pTmp, name, tmp), &lino, _OVER);
×
UNCOV
912
    TAOS_CHECK_GOTO(mndSetCreateConfigCommitLogs(pTrans, pTmp), &lino, _OVER);
×
UNCOV
913
    tFreeSConfigObj(pTmp);
×
UNCOV
914
    taosMemoryFree(pTmp);
×
915

UNCOV
916
    sprintf(tmp, "%d", (syncTimeout - syncTimeout / 4) / 4);
×
917

918
    pTmp = taosMemoryMalloc(sizeof(SConfigObj));
×
919
    pTmp->dtype = CFG_DTYPE_INT32;
×
920
    tstrncpy(pTmp->name, "statusSRTimeoutMs", CFG_NAME_MAX_LEN);
×
921
    TAOS_CHECK_GOTO(mndUpdateObj(pTmp, name, tmp), &lino, _OVER);
×
UNCOV
922
    TAOS_CHECK_GOTO(mndSetCreateConfigCommitLogs(pTrans, pTmp), &lino, _OVER);
×
UNCOV
923
    tFreeSConfigObj(pTmp);
×
UNCOV
924
    taosMemoryFree(pTmp);
×
925

926
    sprintf(tmp, "%d", (syncTimeout - syncTimeout / 4) / 8);
×
927

928
    pTmp = taosMemoryMalloc(sizeof(SConfigObj));
×
929
    pTmp->dtype = CFG_DTYPE_INT32;
×
UNCOV
930
    tstrncpy(pTmp->name, "syncVnodeHeartbeatIntervalMs", CFG_NAME_MAX_LEN);
×
UNCOV
931
    TAOS_CHECK_GOTO(mndUpdateObj(pTmp, name, tmp), &lino, _OVER);
×
UNCOV
932
    TAOS_CHECK_GOTO(mndSetCreateConfigCommitLogs(pTrans, pTmp), &lino, _OVER);
×
UNCOV
933
    tFreeSConfigObj(pTmp);
×
UNCOV
934
    taosMemoryFree(pTmp);
×
935

936
    pTmp = taosMemoryMalloc(sizeof(SConfigObj));
×
UNCOV
937
    pTmp->dtype = CFG_DTYPE_INT32;
×
UNCOV
938
    tstrncpy(pTmp->name, "syncMnodeHeartbeatIntervalMs", CFG_NAME_MAX_LEN);
×
UNCOV
939
    TAOS_CHECK_GOTO(mndUpdateObj(pTmp, name, tmp), &lino, _OVER);
×
UNCOV
940
    TAOS_CHECK_GOTO(mndSetCreateConfigCommitLogs(pTrans, pTmp), &lino, _OVER);
×
UNCOV
941
    tFreeSConfigObj(pTmp);
×
UNCOV
942
    taosMemoryFree(pTmp);
×
943

UNCOV
944
    pTmp = taosMemoryMalloc(sizeof(SConfigObj));
×
UNCOV
945
    pTmp->dtype = CFG_DTYPE_INT32;
×
UNCOV
946
    tstrncpy(pTmp->name, "statusIntervalMs", CFG_NAME_MAX_LEN);
×
UNCOV
947
    TAOS_CHECK_GOTO(mndUpdateObj(pTmp, name, tmp), &lino, _OVER);
×
UNCOV
948
    TAOS_CHECK_GOTO(mndSetCreateConfigCommitLogs(pTrans, pTmp), &lino, _OVER);
×
UNCOV
949
    tFreeSConfigObj(pTmp);
×
UNCOV
950
    taosMemoryFree(pTmp);
×
951
  }
952
  if ((code = mndTransPrepare(pMnode, pTrans)) != 0) goto _OVER;
42!
953
  code = 0;
42✔
954
_OVER:
42✔
955
  if (code != 0) {
42!
UNCOV
956
    mError("failed to update config:%s to value:%s, since %s", name, pValue, tstrerror(code));
×
957
  }
958
  mndTransDrop(pTrans);
42✔
959
  tFreeSConfigObj(pVersion);
42✔
960
  taosMemoryFree(pVersion);
42!
961
  tFreeSConfigObj(pObj);
42✔
962
  taosMemoryFree(pObj);
42!
963
  return code;
42✔
964
}
965

966
static int32_t initConfigArrayFromSdb(SMnode *pMnode, SArray *array) {
1,923✔
967
  int32_t     code = 0;
1,923✔
968
  SSdb       *pSdb = pMnode->pSdb;
1,923✔
969
  void       *pIter = NULL;
1,923✔
970
  SConfigObj *obj = NULL;
1,923✔
971

972
  while (1) {
181,758✔
973
    pIter = sdbFetch(pSdb, SDB_CFG, pIter, (void **)&obj);
183,681✔
974
    if (pIter == NULL) break;
183,681✔
975
    if (obj == NULL) {
181,758!
UNCOV
976
      code = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
977
      goto _exit;
×
978
    }
979
    if (strcasecmp(obj->name, "tsmmConfigVersion") == 0) {
181,758✔
980
      sdbRelease(pSdb, obj);
1,923✔
981
      continue;
1,923✔
982
    }
983
    SConfigItem item = {0};
179,835✔
984
    item.dtype = obj->dtype;
179,835✔
985
    item.name = taosStrdup(obj->name);
179,835!
986
    if (item.name == NULL) {
179,835!
UNCOV
987
      code = terrno;
×
UNCOV
988
      sdbCancelFetch(pSdb, pIter);
×
UNCOV
989
      sdbRelease(pSdb, obj);
×
UNCOV
990
      goto _exit;
×
991
    }
992
    switch (obj->dtype) {
179,835!
993
      case CFG_DTYPE_NONE:
×
994
        break;
×
995
      case CFG_DTYPE_BOOL:
34,074✔
996
        item.bval = obj->bval;
34,074✔
997
        break;
34,074✔
998
      case CFG_DTYPE_INT32:
106,008✔
999
        item.i32 = obj->i32;
106,008✔
1000
        break;
106,008✔
1001
      case CFG_DTYPE_INT64:
11,358✔
1002
        item.i64 = obj->i64;
11,358✔
1003
        break;
11,358✔
1004
      case CFG_DTYPE_FLOAT:
3,786✔
1005
      case CFG_DTYPE_DOUBLE:
1006
        item.fval = obj->fval;
3,786✔
1007
        break;
3,786✔
1008
      case CFG_DTYPE_STRING:
24,609✔
1009
      case CFG_DTYPE_DIR:
1010
      case CFG_DTYPE_LOCALE:
1011
      case CFG_DTYPE_CHARSET:
1012
      case CFG_DTYPE_TIMEZONE:
1013
        item.str = taosStrdup(obj->str);
24,609!
1014
        if (item.str == NULL) {
24,609!
UNCOV
1015
          sdbCancelFetch(pSdb, pIter);
×
UNCOV
1016
          sdbRelease(pSdb, obj);
×
UNCOV
1017
          code = terrno;
×
UNCOV
1018
          goto _exit;
×
1019
        }
1020
        break;
24,609✔
1021
    }
1022
    if (taosArrayPush(array, &item) == NULL) {
179,835!
1023
      sdbCancelFetch(pSdb, pIter);
×
1024
      sdbRelease(pSdb, obj);
×
UNCOV
1025
      code = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
1026
      goto _exit;
×
1027
      break;
1028
    }
1029
    sdbRelease(pSdb, obj);
179,835✔
1030
  }
1031
_exit:
1,923✔
1032
  if (code != 0) {
1,923!
UNCOV
1033
    mError("failed to init config array from sdb, since %s", tstrerror(code));
×
1034
  }
1035
  return code;
1,923✔
1036
}
1037

1038
static void cfgArrayCleanUp(SArray *array) {
2,375✔
1039
  if (array == NULL) {
2,375!
1040
    return;
×
1041
  }
1042

1043
  int32_t sz = taosArrayGetSize(array);
2,375✔
1044
  for (int32_t i = 0; i < sz; ++i) {
182,210✔
1045
    SConfigItem *item = taosArrayGet(array, i);
179,835✔
1046
    if (item->dtype == CFG_DTYPE_STRING || item->dtype == CFG_DTYPE_DIR || item->dtype == CFG_DTYPE_LOCALE ||
179,835!
1047
        item->dtype == CFG_DTYPE_CHARSET || item->dtype == CFG_DTYPE_TIMEZONE) {
159,012✔
1048
      taosMemoryFreeClear(item->str);
24,609!
1049
    }
1050
    taosMemoryFreeClear(item->name);
179,835!
1051
  }
1052

1053
  taosArrayDestroy(array);
2,375✔
1054
}
1055

1056
static void cfgObjArrayCleanUp(SArray *array) {
1,568✔
1057
  if (array == NULL) {
1,568!
UNCOV
1058
    return;
×
1059
  }
1060
  int32_t sz = taosArrayGetSize(array);
1,568✔
1061
  for (int32_t i = 0; i < sz; ++i) {
1,568!
1062
    SConfigObj *obj = taosArrayGet(array, i);
×
1063
    tFreeSConfigObj(obj);
×
1064
  }
1065
  taosArrayDestroy(array);
1,568✔
1066
}
1067

1068
static SArray *initVariablesFromItems(SArray *pItems, const char* likePattern) {
129✔
1069
  if (pItems == NULL) {
129!
UNCOV
1070
    return NULL;
×
1071
  }
1072

1073
  int32_t sz = taosArrayGetSize(pItems);
129✔
1074

1075
  SArray *pInfos = taosArrayInit(sz, sizeof(SVariablesInfo));
129✔
1076
  if (pInfos == NULL) {
129!
UNCOV
1077
    mError("failed to init array while init variables from items, since %s", tstrerror(terrno));
×
UNCOV
1078
    return NULL;
×
1079
  }
1080
  for (int32_t i = 0; i < sz; ++i) {
12,384✔
1081
    SConfigItem   *pItem = taosArrayGet(pItems, i);
12,255✔
1082
    SVariablesInfo info = {0};
12,255✔
1083
    tstrncpy(info.name, pItem->name, sizeof(info.name));
12,255✔
1084
    if (likePattern != NULL && rawStrPatternMatch(pItem->name, likePattern) != TSDB_PATTERN_MATCH) {
12,255✔
1085
      continue;
1,018✔
1086
    }
1087

1088
    // init info value
1089
    switch (pItem->dtype) {
11,237!
UNCOV
1090
      case CFG_DTYPE_NONE:
×
UNCOV
1091
        break;
×
1092
      case CFG_DTYPE_BOOL:
2,126✔
1093
        tsnprintf(info.value, sizeof(info.value), "%d", pItem->bval);
2,126✔
1094
        break;
2,126✔
1095
      case CFG_DTYPE_INT32:
6,630✔
1096
        tsnprintf(info.value, sizeof(info.value), "%d", pItem->i32);
6,630✔
1097
        break;
6,630✔
1098
      case CFG_DTYPE_INT64:
708✔
1099
        tsnprintf(info.value, sizeof(info.value), "%" PRId64, pItem->i64);
708✔
1100
        break;
708✔
1101
      case CFG_DTYPE_FLOAT:
236✔
1102
      case CFG_DTYPE_DOUBLE:
1103
        tsnprintf(info.value, sizeof(info.value), "%f", pItem->fval);
236✔
1104
        break;
236✔
1105
      case CFG_DTYPE_STRING:
1,537✔
1106
      case CFG_DTYPE_DIR:
1107
      case CFG_DTYPE_LOCALE:
1108
      case CFG_DTYPE_CHARSET:
1109
      case CFG_DTYPE_TIMEZONE:
1110
        tsnprintf(info.value, sizeof(info.value), "%s", pItem->str);
1,537✔
1111
        break;
1,537✔
1112
    }
1113

1114
    // init info scope
1115
    switch (pItem->scope) {
11,237!
1116
      case CFG_SCOPE_SERVER:
9,466✔
1117
        tstrncpy(info.scope, "server", sizeof(info.scope));
9,466✔
1118
        break;
9,466✔
1119
      case CFG_SCOPE_CLIENT:
×
1120
        tstrncpy(info.scope, "client", sizeof(info.scope));
×
1121
        break;
×
1122
      case CFG_SCOPE_BOTH:
1,771✔
1123
        tstrncpy(info.scope, "both", sizeof(info.scope));
1,771✔
1124
        break;
1,771✔
1125
      default:
×
1126
        tstrncpy(info.scope, "unknown", sizeof(info.scope));
×
1127
        break;
×
1128
    }
1129
    // init info category
1130
    switch (pItem->category) {
11,237!
1131
      case CFG_CATEGORY_GLOBAL:
11,237✔
1132
        tstrncpy(info.category, "global", sizeof(info.category));
11,237✔
1133
        break;
11,237✔
UNCOV
1134
      case CFG_CATEGORY_LOCAL:
×
UNCOV
1135
        tstrncpy(info.category, "local", sizeof(info.category));
×
1136
        break;
×
1137
      default:
×
1138
        tstrncpy(info.category, "unknown", sizeof(info.category));
×
1139
        break;
×
1140
    }
1141
    if (NULL == taosArrayPush(pInfos, &info)) {
11,237!
UNCOV
1142
      mError("failed to push info to array while init variables from items,since %s", tstrerror(terrno));
×
1143
      taosArrayDestroy(pInfos);
×
1144
      return NULL;
×
1145
    }
1146
  }
1147

1148
  return pInfos;
129✔
1149
}
1150

1151
static int32_t mndProcessShowVariablesReq(SRpcMsg *pReq) {
129✔
1152
  SShowVariablesRsp rsp = {0};
129✔
1153
  int32_t           code = TSDB_CODE_SUCCESS;
129✔
1154
  SShowVariablesReq req = {0};
129✔
1155
  SArray           *array = NULL;
129✔
1156

1157
  code = tDeserializeSShowVariablesReq(pReq->pCont, pReq->contLen, &req);
129✔
1158
  if (code != 0) {
129!
UNCOV
1159
    mError("failed to deserialize config req, since %s", terrstr());
×
UNCOV
1160
    goto _OVER;
×
1161
  }
1162

1163
  if ((code = mndCheckOperPrivilege(pReq->info.node, pReq->info.conn.user, MND_OPER_SHOW_VARIABLES)) != 0) {
129!
UNCOV
1164
    goto _OVER;
×
1165
  }
1166

1167
  SVariablesInfo info = {0};
129✔
1168
  char          *likePattern = req.opType == OP_TYPE_LIKE ? req.val : NULL;
129✔
1169
  rsp.variables = initVariablesFromItems(taosGetGlobalCfg(tsCfg), likePattern);
129✔
1170
  if (rsp.variables == NULL) {
129!
UNCOV
1171
    code = terrno;
×
UNCOV
1172
    goto _OVER;
×
1173
  }
1174
  int32_t rspLen = tSerializeSShowVariablesRsp(NULL, 0, &rsp);
129✔
1175
  void   *pRsp = rpcMallocCont(rspLen);
129✔
1176
  if (pRsp == NULL) {
129!
UNCOV
1177
    code = terrno;
×
UNCOV
1178
    goto _OVER;
×
1179
  }
1180

1181
  if ((rspLen = tSerializeSShowVariablesRsp(pRsp, rspLen, &rsp)) <= 0) {
129!
UNCOV
1182
    rpcFreeCont(pRsp);
×
UNCOV
1183
    code = rspLen;
×
UNCOV
1184
    goto _OVER;
×
1185
  }
1186

1187
  pReq->info.rspLen = rspLen;
129✔
1188
  pReq->info.rsp = pRsp;
129✔
1189
  code = 0;
129✔
1190

1191
_OVER:
129✔
1192

1193
  if (code != 0) {
129!
UNCOV
1194
    mError("failed to get show variables info since %s", tstrerror(code));
×
1195
  }
1196
  tFreeSShowVariablesReq(&req);
129✔
1197
  tFreeSShowVariablesRsp(&rsp);
129✔
1198
  TAOS_RETURN(code);
129✔
1199
}
1200

UNCOV
1201
int32_t compareSConfigItem(const SConfigObj *item1, SConfigItem *item2, bool *compare) {
×
UNCOV
1202
  *compare = true;
×
UNCOV
1203
  switch (item1->dtype) {
×
UNCOV
1204
    case CFG_DTYPE_BOOL:
×
UNCOV
1205
      if (item1->bval != item2->bval) {
×
UNCOV
1206
        item2->bval = item1->bval;
×
UNCOV
1207
        *compare = false;
×
1208
      }
UNCOV
1209
      break;
×
UNCOV
1210
    case CFG_DTYPE_FLOAT:
×
UNCOV
1211
      if (item1->fval != item2->fval) {
×
UNCOV
1212
        item2->fval = item1->fval;
×
UNCOV
1213
        *compare = false;
×
1214
      }
UNCOV
1215
      break;
×
UNCOV
1216
    case CFG_DTYPE_INT32:
×
UNCOV
1217
      if (item1->i32 != item2->i32) {
×
UNCOV
1218
        item2->i32 = item1->i32;
×
UNCOV
1219
        *compare = false;
×
1220
      }
UNCOV
1221
      break;
×
UNCOV
1222
    case CFG_DTYPE_INT64:
×
UNCOV
1223
      if (item1->i64 != item2->i64) {
×
UNCOV
1224
        item2->i64 = item1->i64;
×
UNCOV
1225
        *compare = false;
×
1226
      }
UNCOV
1227
      break;
×
UNCOV
1228
    case CFG_DTYPE_STRING:
×
1229
    case CFG_DTYPE_DIR:
1230
    case CFG_DTYPE_LOCALE:
1231
    case CFG_DTYPE_CHARSET:
1232
    case CFG_DTYPE_TIMEZONE:
UNCOV
1233
      if (strcmp(item1->str, item2->str) != 0) {
×
UNCOV
1234
        item2->str = taosStrdup(item1->str);
×
UNCOV
1235
        if (item2->str == NULL) {
×
UNCOV
1236
          return TSDB_CODE_OUT_OF_MEMORY;
×
1237
        }
UNCOV
1238
        *compare = false;
×
1239
      }
UNCOV
1240
      break;
×
UNCOV
1241
    default:
×
UNCOV
1242
      *compare = false;
×
UNCOV
1243
      return TSDB_CODE_INVALID_CFG;
×
1244
  }
UNCOV
1245
  return TSDB_CODE_SUCCESS;
×
1246
}
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