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

taosdata / TDengine / #4665

12 Aug 2025 07:34AM UTC coverage: 59.901% (-0.6%) from 60.536%
#4665

push

travis-ci

web-flow
Merge pull request #32547 from taosdata/refactor/wangxu/get-started-installer

refactor: get started for installer and docker

137370 of 291999 branches covered (47.04%)

Branch coverage included in aggregate %.

207872 of 284354 relevant lines covered (73.1%)

4846328.0 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) {
2,359✔
56
  int32_t   code = 0;
2,359✔
57
  SSdbTable table = {.sdbType = SDB_CFG,
2,359✔
58
                     .keyType = SDB_KEY_BINARY,
59
                     .encodeFp = (SdbEncodeFp)mnCfgActionEncode,
60
                     .decodeFp = (SdbDecodeFp)mndCfgActionDecode,
61
                     .insertFp = (SdbInsertFp)mndCfgActionInsert,
62
                     .updateFp = (SdbUpdateFp)mndCfgActionUpdate,
63
                     .deleteFp = (SdbDeleteFp)mndCfgActionDelete,
64
                     .deployFp = (SdbDeployFp)mndCfgActionDeploy,
65
                     .afterRestoredFp = (SdbAfterRestoredFp)mndCfgActionAfterRestored};
66

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

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

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

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

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

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

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

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

106
  tEncoderClear(&encoder);
505,619✔
107

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

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

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

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

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

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

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

151
  SDB_GET_INT32(pRaw, dataPos, &tlen, _over);
198,072!
152

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

156
  SDB_GET_BINARY(pRaw, dataPos, buf, tlen, _over);
198,072!
157

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

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

167
_over:
198,072✔
168
  taosMemoryFreeClear(buf);
198,072!
169

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

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

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

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

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

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

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

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

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

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

300
  mInfo("init write cfg to sdb");
1,737!
301
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_NOTHING, NULL, "init-write-config");
1,737✔
302
  if (pTrans == NULL) {
1,737!
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,737✔
309
  if ((code = mndSetCreateConfigCommitLogs(pTrans, &versionObj)) != 0) {
1,737!
310
    mError("failed to init mnd config version, since %s", tstrerror(code));
×
311
    tFreeSConfigObj(&versionObj);
×
312
    goto _OVER;
×
313
  }
314
  tFreeSConfigObj(&versionObj);
1,737✔
315
  sz = taosArrayGetSize(taosGetGlobalCfg(tsCfg));
1,737✔
316

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

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

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

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

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

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

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

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

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

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

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

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

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

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

464
  TAOS_RETURN(TSDB_CODE_SUCCESS);
991✔
465
}
466

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

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

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

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

547
  TAOS_RETURN(TSDB_CODE_SUCCESS);
991✔
548
}
549

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

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

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) {
271✔
603
  int32_t code = 0;
271✔
604
  char   *p = pMCfgReq->config;
271✔
605
  while (*p) {
4,067✔
606
    if (*p == ' ') {
3,954✔
607
      break;
158✔
608
    }
609
    p++;
3,796✔
610
  }
611

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

616
  if (' ' == pMCfgReq->config[optLen]) {
271✔
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;
113✔
623
    tstrncpy(pDCfgReq->value, pMCfgReq->value, sizeof(pDCfgReq->value));
109✔
624
  }
625

626
  TAOS_RETURN(code);
267✔
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) {
331✔
642
    SDnodeObj *pDnode = NULL;
505✔
643
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
505✔
644
    if (pIter == NULL) break;
505✔
645

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

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

754
    CfgAlterType alterType = (cfgReq.dnodeId == 0 || cfgReq.dnodeId == -1) ? CFG_ALTER_ALL_DNODES : CFG_ALTER_DNODE;
267!
755
    TAOS_CHECK_GOTO(cfgCheckRangeForDynUpdate(taosGetCfg(), dcfgReq.config, dcfgReq.value, true, alterType), &lino,
267✔
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:
130✔
790
  mError("failed to process config dnode req, since %s", tstrerror(code));
130!
791
  tFreeSMCfgDnodeReq(&cfgReq);
130✔
792
  sdbRelease(pMnode->pSdb, vObj);
130✔
793
  TAOS_RETURN(code);
130✔
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) {
991✔
802
  mInfo("rebuild config sdb rsp");
991!
803
  return 0;
991✔
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) {
2,271✔
870
  int32_t     code = 0;
2,271✔
871
  SSdb       *pSdb = pMnode->pSdb;
2,271✔
872
  void       *pIter = NULL;
2,271✔
873
  SConfigObj *obj = NULL;
2,271✔
874

875
  while (1) {
189,685✔
876
    pIter = sdbFetch(pSdb, SDB_CFG, pIter, (void **)&obj);
191,956✔
877
    if (pIter == NULL) break;
191,956✔
878
    if (obj == NULL) {
189,685!
879
      code = TSDB_CODE_OUT_OF_MEMORY;
×
880
      goto _exit;
×
881
    }
882
    if (strcasecmp(obj->name, "tsmmConfigVersion") == 0) {
189,685✔
883
      sdbRelease(pSdb, obj);
2,271✔
884
      continue;
2,271✔
885
    }
886
    SConfigItem item = {0};
187,414✔
887
    item.dtype = obj->dtype;
187,414✔
888
    item.name = taosStrdup(obj->name);
187,414!
889
    if (item.name == NULL) {
187,414!
890
      code = terrno;
×
891
      sdbCancelFetch(pSdb, pIter);
×
892
      sdbRelease(pSdb, obj);
×
893
      goto _exit;
×
894
    }
895
    switch (obj->dtype) {
187,414!
896
      case CFG_DTYPE_NONE:
×
897
        break;
×
898
      case CFG_DTYPE_BOOL:
40,644✔
899
        item.bval = obj->bval;
40,644✔
900
        break;
40,644✔
901
      case CFG_DTYPE_INT32:
101,610✔
902
        item.i32 = obj->i32;
101,610✔
903
        break;
101,610✔
904
      case CFG_DTYPE_INT64:
11,290✔
905
        item.i64 = obj->i64;
11,290✔
906
        break;
11,290✔
907
      case CFG_DTYPE_FLOAT:
4,516✔
908
      case CFG_DTYPE_DOUBLE:
909
        item.fval = obj->fval;
4,516✔
910
        break;
4,516✔
911
      case CFG_DTYPE_STRING:
29,354✔
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);
29,354!
917
        if (item.str == NULL) {
29,354!
918
          sdbCancelFetch(pSdb, pIter);
×
919
          sdbRelease(pSdb, obj);
×
920
          code = terrno;
×
921
          goto _exit;
×
922
        }
923
        break;
29,354✔
924
    }
925
    if (taosArrayPush(array, &item) == NULL) {
187,414!
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);
187,414✔
933
  }
934
_exit:
2,271✔
935
  if (code != 0) {
2,271!
936
    mError("failed to init config array from sdb, since %s", tstrerror(code));
×
937
  }
938
  return code;
2,271✔
939
}
940

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

946
  int32_t sz = taosArrayGetSize(array);
2,824✔
947
  for (int32_t i = 0; i < sz; ++i) {
190,238✔
948
    SConfigItem *item = taosArrayGet(array, i);
187,414✔
949
    if (item->dtype == CFG_DTYPE_STRING || item->dtype == CFG_DTYPE_DIR || item->dtype == CFG_DTYPE_LOCALE ||
187,414!
950
        item->dtype == CFG_DTYPE_CHARSET || item->dtype == CFG_DTYPE_TIMEZONE) {
162,576✔
951
      taosMemoryFreeClear(item->str);
29,354!
952
    }
953
    taosMemoryFreeClear(item->name);
187,414!
954
  }
955

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

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

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

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

978
  SArray *pInfos = taosArrayInit(sz, sizeof(SVariablesInfo));
133✔
979
  if (pInfos == NULL) {
133!
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) {
11,172✔
984
    SConfigItem   *pItem = taosArrayGet(pItems, i);
11,039✔
985
    SVariablesInfo info = {0};
11,039✔
986
    tstrncpy(info.name, pItem->name, sizeof(info.name));
11,039✔
987
    if (likePattern != NULL && rawStrPatternMatch(pItem->name, likePattern) != TSDB_PATTERN_MATCH) {
11,039✔
988
      continue;
1,690✔
989
    }
990

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

1017
    // init info scope
1018
    switch (pItem->scope) {
9,349!
1019
      case CFG_SCOPE_SERVER:
7,668✔
1020
        tstrncpy(info.scope, "server", sizeof(info.scope));
7,668✔
1021
        break;
7,668✔
1022
      case CFG_SCOPE_CLIENT:
×
1023
        tstrncpy(info.scope, "client", sizeof(info.scope));
×
1024
        break;
×
1025
      case CFG_SCOPE_BOTH:
1,681✔
1026
        tstrncpy(info.scope, "both", sizeof(info.scope));
1,681✔
1027
        break;
1,681✔
1028
      default:
×
1029
        tstrncpy(info.scope, "unknown", sizeof(info.scope));
×
1030
        break;
×
1031
    }
1032
    // init info category
1033
    switch (pItem->category) {
9,349!
1034
      case CFG_CATEGORY_GLOBAL:
9,349✔
1035
        tstrncpy(info.category, "global", sizeof(info.category));
9,349✔
1036
        break;
9,349✔
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)) {
9,349!
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;
133✔
1052
}
1053

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

1060
  code = tDeserializeSShowVariablesReq(pReq->pCont, pReq->contLen, &req);
133✔
1061
  if (code != 0) {
133!
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) {
133!
1067
    goto _OVER;
×
1068
  }
1069

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

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

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

1094
_OVER:
133✔
1095

1096
  if (code != 0) {
133!
1097
    mError("failed to get show variables info since %s", tstrerror(code));
×
1098
  }
1099
  tFreeSShowVariablesReq(&req);
133✔
1100
  tFreeSShowVariablesRsp(&rsp);
133✔
1101
  TAOS_RETURN(code);
133✔
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