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

taosdata / TDengine / #4944

30 Jan 2026 06:19AM UTC coverage: 66.849% (+0.1%) from 66.718%
#4944

push

travis-ci

web-flow
merge: from main to 3.0 #34453

1124 of 2018 new or added lines in 72 files covered. (55.7%)

13677 existing lines in 155 files now uncovered.

205211 of 306978 relevant lines covered (66.85%)

125657591.7 hits per line

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

58.16
/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
// Sync timeout ratio constants
33
#define SYNC_TIMEOUT_DIVISOR       4
34
#define SYNC_TIMEOUT_ELECT_DIVISOR 2
35
#define SYNC_TIMEOUT_SR_DIVISOR    4
36
#define SYNC_TIMEOUT_HB_DIVISOR    8
37

38
extern SConfig *tsCfg;
39

40
static int32_t mndMCfgGetValInt32(SMCfgDnodeReq *pInMCfgReq, int32_t optLen, int32_t *pOutValue);
41
static int32_t mndProcessShowVariablesReq(SRpcMsg *pReq);
42
static int32_t mndProcessConfigDnodeReq(SRpcMsg *pReq);
43
static int32_t mndProcessConfigDnodeRsp(SRpcMsg *pRsp);
44
static int32_t mndProcessConfigReq(SRpcMsg *pReq);
45
static int32_t mndInitWriteCfg(SMnode *pMnode);
46
static int32_t mndSendRebuildReq(SMnode *pMnode);
47
static int32_t mndTryRebuildConfigSdbRsp(SRpcMsg *pRsp);
48
static int32_t initConfigArrayFromSdb(SMnode *pMnode, SArray *array);
49
static int32_t mndTryRebuildConfigSdb(SRpcMsg *pReq);
50
static void    cfgArrayCleanUp(SArray *array);
51
static void    cfgObjArrayCleanUp(SArray *array);
52
int32_t        compareSConfigItemArrays(SMnode *pMnode, const SArray *dArray, SArray *diffArray);
53

54
static int32_t mndConfigUpdateTrans(SMnode *pMnode, const char *name, char *pValue, ECfgDataType dtype,
55
                                    int32_t tsmmConfigVersion);
56
static int32_t mndConfigUpdateTransWithDnode(SMnode *pMnode, const char *name, char *pValue, ECfgDataType dtype,
57
                                             int32_t tsmmConfigVersion, int32_t dnodeId, SDCfgDnodeReq *pDcfgReq);
58
static int32_t mndFindConfigsToAdd(SMnode *pMnode, SArray *addArray);
59
static int32_t mndFindConfigsToDelete(SMnode *pMnode, SArray *deleteArray);
60
static int32_t mndExecuteConfigSyncTrans(SMnode *pMnode, SArray *addArray, SArray *deleteArray);
61

62
int32_t mndSetCreateConfigCommitLogs(STrans *pTrans, SConfigObj *obj);
63
int32_t mndSetDeleteConfigCommitLogs(STrans *pTrans, SConfigObj *item);
64
int32_t mndSetCreateConfigPrepareLogs(STrans *pTrans, SConfigObj *obj);
65

66
int32_t mndInitConfig(SMnode *pMnode) {
415,413✔
67
  int32_t   code = 0;
415,413✔
68
  SSdbTable table = {.sdbType = SDB_CFG,
415,413✔
69
                     .keyType = SDB_KEY_BINARY,
70
                     .encodeFp = (SdbEncodeFp)mnCfgActionEncode,
71
                     .decodeFp = (SdbDecodeFp)mndCfgActionDecode,
72
                     .insertFp = (SdbInsertFp)mndCfgActionInsert,
73
                     .updateFp = (SdbUpdateFp)mndCfgActionUpdate,
74
                     .deleteFp = (SdbDeleteFp)mndCfgActionDelete,
75
                     .deployFp = (SdbDeployFp)mndCfgActionDeploy,
76
                     .afterRestoredFp = (SdbAfterRestoredFp)mndCfgActionAfterRestored};
77

78
  mndSetMsgHandle(pMnode, TDMT_MND_CONFIG, mndProcessConfigReq);
415,413✔
79
  mndSetMsgHandle(pMnode, TDMT_MND_CONFIG_DNODE, mndProcessConfigDnodeReq);
415,413✔
80
  mndSetMsgHandle(pMnode, TDMT_DND_CONFIG_DNODE_RSP, mndTransProcessRsp);
415,413✔
81
  mndSetMsgHandle(pMnode, TDMT_MND_SHOW_VARIABLES, mndProcessShowVariablesReq);
415,413✔
82
  mndSetMsgHandle(pMnode, TDMT_MND_CONFIG_SDB, mndTryRebuildConfigSdb);
415,413✔
83
  mndSetMsgHandle(pMnode, TDMT_MND_CONFIG_SDB_RSP, mndTryRebuildConfigSdbRsp);
415,413✔
84

85
  return sdbSetTable(pMnode->pSdb, table);
415,413✔
86
}
87

88
SSdbRaw *mnCfgActionEncode(SConfigObj *obj) {
123,959,473✔
89
  int32_t  code = 0;
123,959,473✔
90
  int32_t  lino = 0;
123,959,473✔
91
  void    *buf = NULL;
123,959,473✔
92
  SSdbRaw *pRaw = NULL;
123,959,473✔
93

94
  SEncoder encoder;
123,918,547✔
95
  tEncoderInit(&encoder, NULL, 0);
123,959,473✔
96
  if ((code = tEncodeSConfigObj(&encoder, obj)) < 0) {
123,959,473✔
UNCOV
97
    tEncoderClear(&encoder);
×
UNCOV
98
    TSDB_CHECK_CODE(code, lino, _over);
×
99
  }
100

101
  int32_t tlen = encoder.pos;
123,959,473✔
102
  tEncoderClear(&encoder);
123,959,473✔
103

104
  int32_t size = sizeof(int32_t) + tlen;
123,959,473✔
105
  pRaw = sdbAllocRaw(SDB_CFG, CFG_VER_NUMBER, size);
123,959,473✔
106
  TSDB_CHECK_NULL(pRaw, code, lino, _over, terrno);
123,959,473✔
107

108
  buf = taosMemoryMalloc(tlen);
123,959,473✔
109
  TSDB_CHECK_NULL(buf, code, lino, _over, terrno);
123,959,473✔
110

111
  tEncoderInit(&encoder, buf, tlen);
123,959,473✔
112
  if ((code = tEncodeSConfigObj(&encoder, obj)) < 0) {
123,959,473✔
UNCOV
113
    tEncoderClear(&encoder);
×
UNCOV
114
    TSDB_CHECK_CODE(code, lino, _over);
×
115
  }
116

117
  tEncoderClear(&encoder);
123,959,473✔
118

119
  int32_t dataPos = 0;
123,959,473✔
120
  SDB_SET_INT32(pRaw, dataPos, tlen, _over);
123,959,473✔
121
  SDB_SET_BINARY(pRaw, dataPos, buf, tlen, _over);
123,959,473✔
122
  SDB_SET_DATALEN(pRaw, dataPos, _over);
123,959,473✔
123

124
_over:
123,959,473✔
125
  taosMemoryFreeClear(buf);
123,959,473✔
126
  if (code != TSDB_CODE_SUCCESS) {
123,959,473✔
UNCOV
127
    mError("cfg:%s, failed to encode to raw:%p at line:%d since %s", obj->name, pRaw, lino, tstrerror(code));
×
UNCOV
128
    sdbFreeRaw(pRaw);
×
129
    terrno = code;
×
130
    return NULL;
×
131
  }
132

133
  terrno = 0;
123,959,473✔
134
  mTrace("cfg:%s, encode to raw:%p, row:%p", obj->name, pRaw, obj);
123,959,473✔
135
  return pRaw;
123,959,473✔
136
}
137

138
SSdbRow *mndCfgActionDecode(SSdbRaw *pRaw) {
47,476,416✔
139
  int32_t     code = 0;
47,476,416✔
140
  int32_t     lino = 0;
47,476,416✔
141
  SSdbRow    *pRow = NULL;
47,476,416✔
142
  SConfigObj *pObj = NULL;
47,476,416✔
143
  void       *buf = NULL;
47,476,416✔
144
  int8_t      sver = 0;
47,476,416✔
145
  int32_t     tlen;
47,455,440✔
146
  int32_t     dataPos = 0;
47,476,416✔
147

148
  code = sdbGetRawSoftVer(pRaw, &sver);
47,476,416✔
149
  TSDB_CHECK_CODE(code, lino, _over);
47,476,416✔
150

151
  if (sver != CFG_VER_NUMBER) {
47,476,416✔
UNCOV
152
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
×
UNCOV
153
    goto _over;
×
154
  }
155

156
  pRow = sdbAllocRow(sizeof(SConfigObj));
47,476,416✔
157
  TSDB_CHECK_NULL(pRow, code, lino, _over, terrno);
47,476,416✔
158

159
  pObj = sdbGetRowObj(pRow);
47,476,416✔
160
  TSDB_CHECK_NULL(pObj, code, lino, _over, terrno);
47,476,416✔
161

162
  SDB_GET_INT32(pRaw, dataPos, &tlen, _over);
47,476,416✔
163

164
  buf = taosMemoryMalloc(tlen + 1);
47,476,416✔
165
  TSDB_CHECK_NULL(buf, code, lino, _over, terrno);
47,476,416✔
166

167
  SDB_GET_BINARY(pRaw, dataPos, buf, tlen, _over);
47,476,416✔
168

169
  SDecoder decoder;
47,455,440✔
170
  tDecoderInit(&decoder, buf, tlen + 1);
47,476,416✔
171
  code = tDecodeSConfigObj(&decoder, pObj);
47,476,416✔
172
  tDecoderClear(&decoder);
47,476,416✔
173

174
  if (code < 0) {
47,476,416✔
UNCOV
175
    tFreeSConfigObj(pObj);
×
176
  }
177

178
_over:
47,476,416✔
179
  taosMemoryFreeClear(buf);
47,476,416✔
180

181
  if (code != TSDB_CODE_SUCCESS) {
47,476,416✔
UNCOV
182
    mError("cfg:%s, failed to decode from raw:%p since %s at:%d", pObj->name, pRaw, tstrerror(code), lino);
×
UNCOV
183
    taosMemoryFreeClear(pRow);
×
184
    terrno = code;
×
185
    return NULL;
×
186
  } else {
187
    mTrace("config:%s, decode from raw:%p, row:%p", pObj->name, pRaw, pObj);
47,476,416✔
188
    terrno = 0;
47,476,416✔
189
    return pRow;
47,476,416✔
190
  }
191
}
192

193
static int32_t mndCfgActionInsert(SSdb *pSdb, SConfigObj *obj) {
47,355,911✔
194
  mTrace("cfg:%s, perform insert action, row:%p", obj->name, obj);
47,355,911✔
195
  return 0;
47,355,911✔
196
}
197

198
static int32_t mndCfgActionDelete(SSdb *pSdb, SConfigObj *obj) {
47,471,286✔
199
  mTrace("cfg:%s, perform delete action, row:%p", obj->name, obj);
47,471,286✔
200
  tFreeSConfigObj(obj);
47,471,286✔
201
  return 0;
47,471,286✔
202
}
203

204
static int32_t mndCfgActionUpdate(SSdb *pSdb, SConfigObj *pOld, SConfigObj *pNew) {
120,505✔
205
  mTrace("cfg:%s, perform update action, old row:%p new row:%p", pOld->name, pOld, pNew);
120,505✔
206
  switch (pNew->dtype) {
120,505✔
UNCOV
207
    case CFG_DTYPE_NONE:
×
UNCOV
208
      break;
×
209
    case CFG_DTYPE_BOOL:
11,565✔
210
      pOld->bval = pNew->bval;
11,565✔
211
      break;
11,565✔
212
    case CFG_DTYPE_INT32:
101,098✔
213
      pOld->i32 = pNew->i32;
101,098✔
214
      break;
101,098✔
215
    case CFG_DTYPE_INT64:
4,388✔
216
      pOld->i64 = pNew->i64;
4,388✔
217
      break;
4,388✔
218
    case CFG_DTYPE_FLOAT:
1,316✔
219
    case CFG_DTYPE_DOUBLE:
220
      pOld->fval = pNew->fval;
1,316✔
221
      break;
1,316✔
222
    case CFG_DTYPE_STRING:
2,138✔
223
    case CFG_DTYPE_DIR:
224
    case CFG_DTYPE_LOCALE:
225
    case CFG_DTYPE_CHARSET:
226
    case CFG_DTYPE_TIMEZONE:
227
      taosMemoryFree(pOld->str);
2,138✔
228
      pOld->str = taosStrdup(pNew->str);
2,138✔
229
      if (pOld->str == NULL) {
2,138✔
UNCOV
230
        return terrno;
×
231
      }
232
      break;
2,138✔
233
  }
234
  return TSDB_CODE_SUCCESS;
120,505✔
235
}
236

237
static int32_t mndCfgActionDeploy(SMnode *pMnode) { return mndInitWriteCfg(pMnode); }
295,138✔
238

239
static int32_t mndCfgActionAfterRestored(SMnode *pMnode) { return mndSendRebuildReq(pMnode); }
197,683✔
240

241
static int32_t mndProcessConfigReq(SRpcMsg *pReq) {
605,006✔
242
  SMnode    *pMnode = pReq->info.node;
605,006✔
243
  SConfigReq configReq = {0};
605,006✔
244
  int32_t    code = TSDB_CODE_SUCCESS;
605,006✔
245
  SArray    *array = NULL;
605,006✔
246
  bool       needFree = false;
605,006✔
247
  code = tDeserializeSConfigReq(pReq->pCont, pReq->contLen, &configReq);
605,006✔
248
  if (code != 0) {
605,006✔
UNCOV
249
    mError("failed to deserialize config req, since %s", terrstr());
×
UNCOV
250
    goto _OVER;
×
251
  }
252

253
  SConfigObj *vObj = sdbAcquire(pMnode->pSdb, SDB_CFG, "tsmmConfigVersion");
605,006✔
254
  if (vObj == NULL) {
605,006✔
UNCOV
255
    mInfo("failed to acquire mnd config version, since %s", terrstr());
×
UNCOV
256
    goto _OVER;
×
257
  }
258

259
  array = taosArrayInit(16, sizeof(SConfigItem));
605,006✔
260
  if (array == NULL) {
605,006✔
UNCOV
261
    code = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
262
    goto _OVER;
×
263
  }
264
  SConfigRsp configRsp = {0};
605,006✔
265
  configRsp.cver = vObj->i32;
605,006✔
266

267
  if (configReq.cver == vObj->i32) {
605,006✔
268
    configRsp.isVersionVerified = 1;
132,814✔
269
  } else {
270
    code = initConfigArrayFromSdb(pMnode, array);
472,192✔
271
    if (code != 0) {
472,192✔
UNCOV
272
      mError("failed to init config array from sdb, since %s", tstrerror(code));
×
UNCOV
273
      goto _OVER;
×
274
    }
275
    configRsp.array = array;
472,192✔
276
  }
277

278
  int32_t contLen = tSerializeSConfigRsp(NULL, 0, &configRsp);
605,006✔
279
  if (contLen < 0) {
605,006✔
UNCOV
280
    code = contLen;
×
UNCOV
281
    goto _OVER;
×
282
  }
283
  void *pHead = rpcMallocCont(contLen);
605,006✔
284
  if (pHead == NULL) {
605,006✔
UNCOV
285
    code = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
286
    goto _OVER;
×
287
  }
288
  contLen = tSerializeSConfigRsp(pHead, contLen, &configRsp);
605,006✔
289
  if (contLen < 0) {
605,006✔
UNCOV
290
    rpcFreeCont(pHead);
×
UNCOV
291
    code = contLen;
×
292
    goto _OVER;
×
293
  }
294
  pReq->info.rspLen = contLen;
605,006✔
295
  pReq->info.rsp = pHead;
605,006✔
296

297
_OVER:
605,006✔
298
  if (code != 0) {
605,006✔
UNCOV
299
    mError("failed to process config req, since %s", tstrerror(code));
×
300
  }
301
  sdbRelease(pMnode->pSdb, vObj);
605,006✔
302
  cfgArrayCleanUp(array);
605,006✔
303
  tFreeSConfigReq(&configReq);
605,006✔
304
  return code;
605,006✔
305
}
306

307
int32_t mndInitWriteCfg(SMnode *pMnode) {
295,138✔
308
  int    code = 0;
295,138✔
309
  size_t sz = 0;
295,138✔
310

311
  mInfo("init write cfg to sdb");
295,138✔
312
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_NOTHING, NULL, "init-write-config");
295,138✔
313
  if (pTrans == NULL) {
295,138✔
UNCOV
314
    mError("failed to init write cfg in create trans, since %s", terrstr());
×
UNCOV
315
    goto _OVER;
×
316
  }
317

318
  // encode mnd config version
319
  SConfigObj versionObj = mndInitConfigVersion();
295,138✔
320
  if ((code = mndSetCreateConfigCommitLogs(pTrans, &versionObj)) != 0) {
295,138✔
UNCOV
321
    mError("failed to init mnd config version, since %s", tstrerror(code));
×
UNCOV
322
    tFreeSConfigObj(&versionObj);
×
323
    goto _OVER;
×
324
  }
325
  tFreeSConfigObj(&versionObj);
295,138✔
326
  sz = taosArrayGetSize(taosGetGlobalCfg(tsCfg));
295,138✔
327

328
  for (int i = 0; i < sz; ++i) {
33,645,732✔
329
    SConfigItem *item = taosArrayGet(taosGetGlobalCfg(tsCfg), i);
33,350,594✔
330
    SConfigObj   obj;
33,338,164✔
331
    if ((code = mndInitConfigObj(item, &obj)) != 0) {
33,350,594✔
UNCOV
332
      goto _OVER;
×
333
    }
334
    if ((code = mndSetCreateConfigCommitLogs(pTrans, &obj)) != 0) {
33,350,594✔
UNCOV
335
      mError("failed to init mnd config:%s, since %s", item->name, tstrerror(code));
×
UNCOV
336
      tFreeSConfigObj(&obj);
×
337
      goto _OVER;
×
338
    }
339
    tFreeSConfigObj(&obj);
33,350,594✔
340
  }
341
  if ((code = mndTransPrepare(pMnode, pTrans)) != 0) goto _OVER;
295,138✔
342

343
_OVER:
295,138✔
344
  if (code != 0) {
295,138✔
UNCOV
345
    mError("failed to init write cfg, since %s", tstrerror(code));
×
346
  }
347
  mndTransDrop(pTrans);
295,138✔
348
  return code;
295,138✔
349
}
350

351
int32_t mndSendRebuildReq(SMnode *pMnode) {
197,683✔
352
  int32_t code = 0;
197,683✔
353

354
  SRpcMsg rpcMsg = {.pCont = NULL,
197,683✔
355
                    .contLen = 0,
356
                    .msgType = TDMT_MND_CONFIG_SDB,
357
                    .info.ahandle = 0,
358
                    .info.notFreeAhandle = 1,
359
                    .info.refId = 0,
360
                    .info.noResp = 0,
361
                    .info.handle = 0};
362
  SEpSet  epSet = {0};
197,683✔
363

364
  mndGetMnodeEpSet(pMnode, &epSet);
197,683✔
365

366
  code = tmsgSendReq(&epSet, &rpcMsg);
197,683✔
367
  if (code != 0) {
197,683✔
UNCOV
368
    mError("failed to send rebuild config req, since %s", tstrerror(code));
×
369
  }
370
  return code;
197,683✔
371
}
372

373
static int32_t mndTryRebuildConfigSdb(SRpcMsg *pReq) {
198,063✔
374
  SMnode *pMnode = pReq->info.node;
198,063✔
375
  if (!mndIsLeader(pMnode)) {
198,063✔
UNCOV
376
    return TSDB_CODE_SUCCESS;
×
377
  }
378

379
  int32_t     code = 0;
198,063✔
380
  SConfigObj *vObj = NULL;
198,063✔
381
  SArray     *addArray = NULL;
198,063✔
382
  SArray     *deleteArray = NULL;
198,063✔
383

384
  vObj = sdbAcquire(pMnode->pSdb, SDB_CFG, "tsmmConfigVersion");
198,063✔
385
  if (vObj == NULL) {
198,063✔
UNCOV
386
    code = mndInitWriteCfg(pMnode);
×
UNCOV
387
    if (code < 0) {
×
388
      mError("failed to init write cfg, since %s", tstrerror(code));
×
389
    } else {
390
      mInfo("failed to acquire mnd config version, try to rebuild config in sdb.");
×
391
    }
392
    goto _exit;
×
393
  }
394

395
  addArray = taosArrayInit(4, sizeof(SConfigObj));
198,063✔
396
  deleteArray = taosArrayInit(4, sizeof(SConfigObj));
198,063✔
397
  if (addArray == NULL || deleteArray == NULL) {
198,063✔
UNCOV
398
    code = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
399
    goto _exit;
×
400
  }
401

402
  // Find configs to add and delete
403
  if ((code = mndFindConfigsToAdd(pMnode, addArray)) != 0) {
198,063✔
UNCOV
404
    mError("failed to find configs to add, since %s", tstrerror(code));
×
UNCOV
405
    goto _exit;
×
406
  }
407

408
  if ((code = mndFindConfigsToDelete(pMnode, deleteArray)) != 0) {
198,063✔
UNCOV
409
    mError("failed to find configs to delete, since %s", tstrerror(code));
×
UNCOV
410
    goto _exit;
×
411
  }
412

413
  // Execute the sync transaction
414
  if ((code = mndExecuteConfigSyncTrans(pMnode, addArray, deleteArray)) != 0) {
198,063✔
UNCOV
415
    mError("failed to execute config sync transaction, since %s", tstrerror(code));
×
UNCOV
416
    goto _exit;
×
417
  }
418

419
_exit:
198,063✔
420
  if (code != 0) {
198,063✔
UNCOV
421
    mError("failed to try rebuild config in sdb, since %s", tstrerror(code));
×
422
  }
423
  sdbRelease(pMnode->pSdb, vObj);
198,063✔
424
  cfgObjArrayCleanUp(addArray);
198,063✔
425
  cfgObjArrayCleanUp(deleteArray);
198,063✔
426
  TAOS_RETURN(code);
198,063✔
427
}
428

429
int32_t mndSetCreateConfigCommitLogs(STrans *pTrans, SConfigObj *item) {
33,665,458✔
430
  int32_t  code = 0;
33,665,458✔
431
  SSdbRaw *pCommitRaw = mnCfgActionEncode(item);
33,665,458✔
432
  if (pCommitRaw == NULL) {
33,665,458✔
UNCOV
433
    code = terrno;
×
UNCOV
434
    TAOS_RETURN(code);
×
435
  }
436
  if ((code = mndTransAppendCommitlog(pTrans, pCommitRaw)) != 0) {
33,665,458✔
UNCOV
437
    taosMemoryFree(pCommitRaw);
×
UNCOV
438
    TAOS_RETURN(code);
×
439
  }
440
  if ((code = sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY)) != 0) TAOS_RETURN(code);
33,665,458✔
441
  return TSDB_CODE_SUCCESS;
33,665,458✔
442
}
443

UNCOV
444
int32_t mndSetDeleteConfigCommitLogs(STrans *pTrans, SConfigObj *item) {
×
UNCOV
445
  int32_t  code = 0;
×
446
  SSdbRaw *pCommitRaw = mnCfgActionEncode(item);
×
447
  if (pCommitRaw == NULL) {
×
448
    code = terrno;
×
449
    TAOS_RETURN(code);
×
450
  }
451
  if ((code = mndTransAppendCommitlog(pTrans, pCommitRaw)) != 0) {
×
UNCOV
452
    taosMemoryFree(pCommitRaw);
×
453
    TAOS_RETURN(code);
×
454
  }
455
  if ((code = sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED)) != 0) TAOS_RETURN(code);
×
UNCOV
456
  return TSDB_CODE_SUCCESS;
×
457
}
458

459
int32_t mndSetCreateConfigPrepareLogs(STrans *pTrans, SConfigObj *item) {
19,726✔
460
  int32_t  code = 0;
19,726✔
461
  SSdbRaw *pPrepareRaw = mnCfgActionEncode(item);
19,726✔
462
  if (pPrepareRaw == NULL) {
19,726✔
UNCOV
463
    code = terrno;
×
UNCOV
464
    TAOS_RETURN(code);
×
465
  }
466
  if ((code = mndTransAppendPrepareLog(pTrans, pPrepareRaw)) != 0) {
19,726✔
UNCOV
467
    taosMemoryFree(pPrepareRaw);
×
UNCOV
468
    TAOS_RETURN(code);
×
469
  }
470
  if ((code = sdbSetRawStatus(pPrepareRaw, SDB_STATUS_READY)) != 0) TAOS_RETURN(code);
19,726✔
471
  return TSDB_CODE_SUCCESS;
19,726✔
472
}
473

474
static int32_t mndFindConfigsToAdd(SMnode *pMnode, SArray *addArray) {
198,063✔
475
  int32_t code = 0;
198,063✔
476
  int32_t sz = taosArrayGetSize(taosGetGlobalCfg(tsCfg));
198,063✔
477

478
  for (int i = 0; i < sz; ++i) {
22,579,182✔
479
    SConfigItem *item = taosArrayGet(taosGetGlobalCfg(tsCfg), i);
22,381,119✔
480
    SConfigObj  *obj = sdbAcquire(pMnode->pSdb, SDB_CFG, item->name);
22,381,119✔
481
    if (obj == NULL) {
22,381,119✔
UNCOV
482
      mInfo("config:%s, not exist in sdb, will add it", item->name);
×
UNCOV
483
      SConfigObj newObj;
×
484
      if ((code = mndInitConfigObj(item, &newObj)) != 0) {
×
485
        TAOS_RETURN(code);
×
486
      }
487
      if (NULL == taosArrayPush(addArray, &newObj)) {
×
UNCOV
488
        tFreeSConfigObj(&newObj);
×
489
        TAOS_RETURN(terrno);
×
490
      }
491
    } else {
492
      sdbRelease(pMnode->pSdb, obj);
22,381,119✔
493
    }
494
  }
495

496
  TAOS_RETURN(TSDB_CODE_SUCCESS);
198,063✔
497
}
498

499
static int32_t mndFindConfigsToDelete(SMnode *pMnode, SArray *deleteArray) {
198,063✔
500
  int32_t     code = 0;
198,063✔
501
  int32_t     sz = taosArrayGetSize(taosGetGlobalCfg(tsCfg));
198,063✔
502
  SSdb       *pSdb = pMnode->pSdb;
198,063✔
503
  void       *pIter = NULL;
198,063✔
504
  SConfigObj *obj = NULL;
198,063✔
505

506
  while (1) {
22,579,182✔
507
    pIter = sdbFetch(pSdb, SDB_CFG, pIter, (void **)&obj);
22,777,245✔
508
    if (pIter == NULL) break;
22,777,245✔
509
    if (obj == NULL) {
22,579,182✔
UNCOV
510
      code = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
511
      sdbCancelFetch(pSdb, pIter);
×
512
      TAOS_RETURN(code);
×
513
    }
514

515
    // Skip the version config
516
    if (strcasecmp(obj->name, "tsmmConfigVersion") == 0) {
22,579,182✔
517
      sdbRelease(pSdb, obj);
198,063✔
518
      continue;
198,063✔
519
    }
520

521
    // Check if this config exists in global config
522
    bool existsInGlobal = false;
22,381,119✔
523
    for (int i = 0; i < sz; ++i) {
1,275,723,783✔
524
      SConfigItem *item = taosArrayGet(taosGetGlobalCfg(tsCfg), i);
1,275,723,783✔
525
      if (strcasecmp(obj->name, item->name) == 0) {
1,275,723,783✔
526
        existsInGlobal = true;
22,381,119✔
527
        break;
22,381,119✔
528
      }
529
    }
530

531
    if (!existsInGlobal) {
22,381,119✔
UNCOV
532
      mInfo("config:%s, not exist in global config, will delete it from sdb", obj->name);
×
UNCOV
533
      SConfigObj deleteObj = {0};
×
534
      tstrncpy(deleteObj.name, obj->name, CFG_NAME_MAX_LEN);
×
535
      deleteObj.dtype = obj->dtype;
×
536

537
      // Copy the value based on type
UNCOV
538
      switch (obj->dtype) {
×
UNCOV
539
        case CFG_DTYPE_BOOL:
×
540
          deleteObj.bval = obj->bval;
×
541
          break;
×
542
        case CFG_DTYPE_INT32:
×
543
          deleteObj.i32 = obj->i32;
×
544
          break;
×
545
        case CFG_DTYPE_INT64:
×
546
          deleteObj.i64 = obj->i64;
×
547
          break;
×
548
        case CFG_DTYPE_FLOAT:
×
549
        case CFG_DTYPE_DOUBLE:
550
          deleteObj.fval = obj->fval;
×
UNCOV
551
          break;
×
552
        case CFG_DTYPE_STRING:
×
553
        case CFG_DTYPE_DIR:
554
        case CFG_DTYPE_LOCALE:
555
        case CFG_DTYPE_CHARSET:
556
        case CFG_DTYPE_TIMEZONE:
UNCOV
557
          deleteObj.str = taosStrdup(obj->str);
×
UNCOV
558
          if (deleteObj.str == NULL) {
×
559
            sdbCancelFetch(pSdb, pIter);
×
560
            sdbRelease(pSdb, obj);
×
561
            TAOS_RETURN(terrno);
×
562
          }
563
          break;
×
UNCOV
564
        default:
×
565
          break;
×
566
      }
567

UNCOV
568
      if (NULL == taosArrayPush(deleteArray, &deleteObj)) {
×
UNCOV
569
        tFreeSConfigObj(&deleteObj);
×
570
        sdbCancelFetch(pSdb, pIter);
×
571
        sdbRelease(pSdb, obj);
×
572
        TAOS_RETURN(terrno);
×
573
      }
574
    }
575

576
    sdbRelease(pSdb, obj);
22,381,119✔
577
  }
578

579
  TAOS_RETURN(TSDB_CODE_SUCCESS);
198,063✔
580
}
581

582
static int32_t mndExecuteConfigSyncTrans(SMnode *pMnode, SArray *addArray, SArray *deleteArray) {
198,063✔
583
  int32_t addSize = taosArrayGetSize(addArray);
198,063✔
584
  int32_t deleteSize = taosArrayGetSize(deleteArray);
198,063✔
585

586
  if (addSize == 0 && deleteSize == 0) {
198,063✔
587
    return TSDB_CODE_SUCCESS;
198,063✔
588
  }
589

UNCOV
590
  const char *transName = "sync-config";
×
UNCOV
591
  if (addSize > 0 && deleteSize > 0) {
×
592
    transName = "sync-config";
×
593
  } else if (addSize > 0) {
×
594
    transName = "add-config";
×
595
  } else {
596
    transName = "delete-config";
×
597
  }
598

UNCOV
599
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_NOTHING, NULL, transName);
×
UNCOV
600
  if (pTrans == NULL) {
×
601
    TAOS_RETURN(terrno);
×
602
  }
603

UNCOV
604
  int32_t code = 0;
×
605

606
  // Add new configs
UNCOV
607
  for (int i = 0; i < addSize; ++i) {
×
UNCOV
608
    SConfigObj *AddObj = taosArrayGet(addArray, i);
×
609
    if ((code = mndSetCreateConfigCommitLogs(pTrans, AddObj)) != 0) {
×
610
      mndTransDrop(pTrans);
×
611
      TAOS_RETURN(code);
×
612
    }
613
  }
614

615
  // Delete obsolete configs
UNCOV
616
  for (int i = 0; i < deleteSize; ++i) {
×
UNCOV
617
    SConfigObj *DelObj = taosArrayGet(deleteArray, i);
×
618
    if ((code = mndSetDeleteConfigCommitLogs(pTrans, DelObj)) != 0) {
×
619
      mndTransDrop(pTrans);
×
620
      TAOS_RETURN(code);
×
621
    }
622
  }
623

UNCOV
624
  if ((code = mndTransPrepare(pMnode, pTrans)) != 0) {
×
UNCOV
625
    mndTransDrop(pTrans);
×
626
    TAOS_RETURN(code);
×
627
  }
628

UNCOV
629
  mInfo("sync config to sdb, add nums:%d, delete nums:%d", addSize, deleteSize);
×
UNCOV
630
  mndTransDrop(pTrans);
×
631
  TAOS_RETURN(TSDB_CODE_SUCCESS);
×
632
}
633

634
static int32_t mndMCfg2DCfg(SMCfgDnodeReq *pMCfgReq, SDCfgDnodeReq *pDCfgReq) {
88,045✔
635
  int32_t code = 0;
88,045✔
636
  char   *p = pMCfgReq->config;
88,045✔
637
  while (*p) {
1,195,258✔
638
    if (*p == ' ') {
1,147,960✔
639
      break;
40,747✔
640
    }
641
    p++;
1,107,213✔
642
  }
643

644
  size_t optLen = p - pMCfgReq->config;
88,045✔
645
  tstrncpy(pDCfgReq->config, pMCfgReq->config, sizeof(pDCfgReq->config));
88,045✔
646
  pDCfgReq->config[optLen] = 0;
88,045✔
647

648
  if (' ' == pMCfgReq->config[optLen]) {
88,045✔
649
    // 'key value'
650
    if (strlen(pMCfgReq->value) != 0) goto _err;
40,747✔
651
    tstrncpy(pDCfgReq->value, p + 1, sizeof(pDCfgReq->value));
40,747✔
652
  } else {
653
    // 'key' 'value'
654
    if (strlen(pMCfgReq->value) == 0) goto _err;
47,298✔
655
    tstrncpy(pDCfgReq->value, pMCfgReq->value, sizeof(pDCfgReq->value));
46,664✔
656
  }
657

658
  TAOS_RETURN(code);
87,411✔
659

660
_err:
634✔
661
  mError("dnode:%d, failed to config since invalid conf:%s", pMCfgReq->dnodeId, pMCfgReq->config);
634✔
662
  code = TSDB_CODE_INVALID_CFG;
634✔
663
  TAOS_RETURN(code);
634✔
664
}
665

666
static int32_t mndBuildCfgDnodeRedoAction(STrans *pTrans, SDnodeObj *pDnode, SDCfgDnodeReq *pDcfgReq) {
10,711✔
667
  int32_t code = 0;
10,711✔
668
  SEpSet  epSet = mndGetDnodeEpset(pDnode);
10,711✔
669
  int32_t bufLen = tSerializeSDCfgDnodeReq(NULL, 0, pDcfgReq);
10,711✔
670
  void   *pBuf = taosMemoryMalloc(bufLen);
10,711✔
671

672
  if (pBuf == NULL) {
10,711✔
UNCOV
673
    code = terrno;
×
UNCOV
674
    return code;
×
675
  }
676

677
  if ((bufLen = tSerializeSDCfgDnodeReq(pBuf, bufLen, pDcfgReq)) <= 0) {
10,711✔
UNCOV
678
    code = bufLen;
×
UNCOV
679
    taosMemoryFree(pBuf);
×
680
    return code;
×
681
  }
682

683
  STransAction action = {
10,711✔
684
      .epSet = epSet,
685
      .pCont = pBuf,
686
      .contLen = bufLen,
687
      .msgType = TDMT_DND_CONFIG_DNODE,
688
      .acceptableCode = 0,
689
      .groupId = -1,
690
  };
691

692
  mInfo("dnode:%d, append redo action to trans, config:%s value:%s", pDnode->id, pDcfgReq->config, pDcfgReq->value);
10,711✔
693

694
  if ((code = mndTransAppendRedoAction(pTrans, &action)) != 0) {
10,711✔
UNCOV
695
    taosMemoryFree(pBuf);
×
UNCOV
696
    return code;
×
697
  }
698

699
  return code;
10,711✔
700
}
701

702
static int32_t mndSendCfgDnodeReq(SMnode *pMnode, int32_t dnodeId, SDCfgDnodeReq *pDcfgReq) {
66,061✔
703
  int32_t code = -1;
66,061✔
704
  SSdb   *pSdb = pMnode->pSdb;
66,061✔
705
  void   *pIter = NULL;
66,061✔
706

707
  int64_t curMs = taosGetTimestampMs();
66,061✔
708

709
  while (1) {
173,289✔
710
    SDnodeObj *pDnode = NULL;
239,350✔
711
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
239,350✔
712
    if (pIter == NULL) break;
239,350✔
713

714
    if (pDnode->id == dnodeId || dnodeId == -1 || dnodeId == 0) {
175,281✔
715
      bool online = mndIsDnodeOnline(pDnode, curMs);
66,503✔
716
      if (!online) {
66,503✔
UNCOV
717
        mWarn("dnode:%d, is offline, skip to send config req", pDnode->id);
×
UNCOV
718
        continue;
×
719
      }
720
      SEpSet  epSet = mndGetDnodeEpset(pDnode);
66,503✔
721
      int32_t bufLen = tSerializeSDCfgDnodeReq(NULL, 0, pDcfgReq);
66,503✔
722
      void   *pBuf = rpcMallocCont(bufLen);
66,503✔
723

724
      if (pBuf == NULL) {
66,503✔
UNCOV
725
        sdbCancelFetch(pMnode->pSdb, pIter);
×
UNCOV
726
        sdbRelease(pMnode->pSdb, pDnode);
×
727
        code = TSDB_CODE_OUT_OF_MEMORY;
×
728
        return code;
×
729
      }
730

731
      if ((bufLen = tSerializeSDCfgDnodeReq(pBuf, bufLen, pDcfgReq)) <= 0) {
66,503✔
UNCOV
732
        sdbCancelFetch(pMnode->pSdb, pIter);
×
UNCOV
733
        sdbRelease(pMnode->pSdb, pDnode);
×
734
        code = bufLen;
×
735
        rpcFreeCont(pBuf);
×
736
        return code;
×
737
      }
738

739
      mInfo("dnode:%d, send config req to dnode, config:%s value:%s", pDnode->id, pDcfgReq->config, pDcfgReq->value);
66,503✔
740
      SRpcMsg rpcMsg = {.msgType = TDMT_DND_CONFIG_DNODE, .pCont = pBuf, .contLen = bufLen};
66,503✔
741
      SRpcMsg rpcRsp = {0};
66,503✔
742

743
      code = rpcSendRecvWithTimeout(pMnode->msgCb.statusRpc, &epSet, &rpcMsg, &rpcRsp, NULL, CFG_ALTER_TIMEOUT);
66,503✔
744
      if (code != 0) {
66,503✔
UNCOV
745
        mError("failed to send config req to dnode:%d, since %s", pDnode->id, tstrerror(code));
×
UNCOV
746
        sdbCancelFetch(pMnode->pSdb, pIter);
×
747
        sdbRelease(pMnode->pSdb, pDnode);
×
748
        return code;
×
749
      }
750

751
      code = rpcRsp.code;
66,503✔
752
      if (code != 0) {
66,503✔
753
        mError("failed to alter config %s,on dnode:%d, since %s", pDcfgReq->config, pDnode->id, tstrerror(code));
1,992✔
754
        sdbCancelFetch(pMnode->pSdb, pIter);
1,992✔
755
        sdbRelease(pMnode->pSdb, pDnode);
1,992✔
756
        return code;
1,992✔
757
      }
758
      rpcFreeCont(rpcRsp.pCont);
64,511✔
759
    }
760
    sdbRelease(pSdb, pDnode);
173,289✔
761
  }
762

763
  if (code == -1) {
64,069✔
764
    code = TSDB_CODE_MND_DNODE_NOT_EXIST;
652✔
765
  }
766
  TAOS_RETURN(code);
64,069✔
767
}
768

769
static int32_t  mndProcessConfigDnodeReq(SRpcMsg *pReq) {
89,453✔
770
  int32_t       code = 0;
89,453✔
771
  int32_t       lino = -1;
89,453✔
772
  SMnode       *pMnode = pReq->info.node;
89,453✔
773
  SMCfgDnodeReq cfgReq = {0};
89,453✔
774
  SUserObj     *pOperUser = NULL;
89,453✔
775
  int64_t       tss = taosGetTimestampMs();
89,453✔
776
  SConfigObj   *vObj = sdbAcquire(pMnode->pSdb, SDB_CFG, "tsmmConfigVersion");
89,453✔
777
  if (vObj == NULL) {
89,453✔
UNCOV
778
    code = TSDB_CODE_SDB_OBJ_NOT_THERE;
×
UNCOV
779
    mInfo("failed to acquire mnd config version, since %s", tstrerror(code));
×
UNCOV
780
    goto _err_out;
×
781
  }
782

783
  TAOS_CHECK_RETURN(tDeserializeSMCfgDnodeReq(pReq->pCont, pReq->contLen, &cfgReq));
89,453✔
784
  int8_t updateWhiteList = 0;
89,453✔
785
  mInfo("dnode:%d, start to config, option:%s, value:%s", cfgReq.dnodeId, cfgReq.config, cfgReq.value);
89,453✔
786

787
  code = mndAcquireUser(pMnode, RPC_MSG_USER(pReq), &pOperUser);
89,453✔
788
  if (pOperUser == NULL) {
89,453✔
NEW
789
    code = TSDB_CODE_MND_NO_USER_FROM_CONN;
×
NEW
790
    goto _err_out;
×
791
  }
792

793
  char configName[TSDB_DNODE_CONFIG_LEN] = {0};
89,453✔
794
  strncpy(configName, cfgReq.config, sizeof(configName) - 1);
89,453✔
795
  const char *p = strstr(configName, " ");
89,453✔
796
  if (p) *(char *)p = 0;
89,453✔
797
  EPrivType privType = cfgGetPrivType(tsCfg, configName, 0);
89,453✔
798
  if ((code = mndCheckSysObjPrivilege(pMnode, pOperUser, RPC_MSG_TOKEN(pReq), privType, 0, 0, NULL, NULL))) {
89,453✔
799
    goto _err_out;
1,176✔
800
  }
801

802
  SDCfgDnodeReq dcfgReq = {0};
88,277✔
803
  if (strcasecmp(cfgReq.config, "resetlog") == 0) {
88,277✔
804
    tstrncpy(dcfgReq.config, "resetlog", 9);
232✔
805
    goto _send_req;
232✔
806
#ifdef TD_ENTERPRISE
807
  } else if (strncasecmp(cfgReq.config, "ssblocksize", 12) == 0) {
88,045✔
UNCOV
808
    int32_t optLen = strlen("ssblocksize");
×
UNCOV
809
    int32_t flag = -1;
×
UNCOV
810
    int32_t code = mndMCfgGetValInt32(&cfgReq, optLen, &flag);
×
UNCOV
811
    if (code < 0) {
×
UNCOV
812
      goto _err_out;
×
813
    }
814

UNCOV
815
    if (flag > 1024 * 1024 || (flag > -1 && flag < 1024) || flag < -1) {
×
UNCOV
816
      mError("dnode:%d, failed to config ssblocksize since value:%d. Valid range: -1 or [1024, 1024 * 1024]",
×
817
             cfgReq.dnodeId, flag);
UNCOV
818
      code = TSDB_CODE_INVALID_CFG;
×
UNCOV
819
      goto _err_out;
×
820
    }
821

UNCOV
822
    tstrncpy(dcfgReq.config, "ssblocksize", 12);
×
823
    snprintf(dcfgReq.value, TSDB_DNODE_VALUE_LEN, "%d", flag);
×
824
#endif
825
  } else {
826
    TAOS_CHECK_GOTO(mndMCfg2DCfg(&cfgReq, &dcfgReq), &lino, _err_out);
88,045✔
827
    if (strlen(dcfgReq.config) > TSDB_DNODE_CONFIG_LEN) {
87,411✔
UNCOV
828
      mError("dnode:%d, failed to config since config is too long", cfgReq.dnodeId);
×
UNCOV
829
      code = TSDB_CODE_INVALID_CFG;
×
830
      goto _err_out;
×
831
    }
832
    if (strncasecmp(dcfgReq.config, "enableWhiteList", strlen("enableWhiteList")) == 0) {
87,411✔
833
      updateWhiteList = 1;
276✔
834
    }
835

836
    CfgAlterType alterType = (cfgReq.dnodeId == 0 || cfgReq.dnodeId == -1) ? CFG_ALTER_ALL_DNODES : CFG_ALTER_DNODE;
87,411✔
837
    TAOS_CHECK_GOTO(cfgCheckRangeForDynUpdate(taosGetCfg(), dcfgReq.config, dcfgReq.value, true, alterType), &lino,
87,411✔
838
                    _err_out);
839
  }
840
  SConfigItem *pItem = cfgGetItem(taosGetCfg(), dcfgReq.config);
75,692✔
841
  // Update config in sdb.
842
  if (pItem == NULL) {
75,692✔
843
    mError("failed to find config:%s while process config dnode req", cfgReq.config);
×
844
    code = TSDB_CODE_CFG_NOT_FOUND;
×
845
    goto _err_out;
×
846
  }
847

848
  // Audit log
849
  if (tsAuditLevel >= AUDIT_LEVEL_SYSTEM) {
75,692✔
850
    char obj[50] = {0};
75,692✔
851
    (void)tsnprintf(obj, sizeof(obj), "%d", cfgReq.dnodeId);
75,692✔
852
    int64_t tse = taosGetTimestampMs();
75,692✔
853
    double  duration = (double)(tse - tss);
75,692✔
854
    duration = duration / 1000;
75,692✔
855
    auditRecord(pReq, pMnode->clusterId, "alterDnode", obj, "", cfgReq.sql, cfgReq.sqlLen, duration, 0);
75,692✔
856
  }
857

858
  dcfgReq.version = vObj->i32 + 1;
75,692✔
859

860
  if (pItem->category == CFG_CATEGORY_GLOBAL) {
75,692✔
861
    // Use transaction to update SDB and send to dnode atomically
862
    TAOS_CHECK_GOTO(mndConfigUpdateTransWithDnode(pMnode, dcfgReq.config, dcfgReq.value, pItem->dtype, dcfgReq.version,
9,863✔
863
                                                  cfgReq.dnodeId, &dcfgReq),
864
                    &lino, _err_out);
865
  } else {
866
    // For local config, still use the old method (only send to dnode)
867
    goto _send_req;
65,829✔
868
  }
869

870
  // For global config, transaction has handled everything, go to success
871
  goto _success;
9,863✔
872

873
_send_req:
66,061✔
874
  dcfgReq.version = vObj->i32;
66,061✔
875
  code = mndSendCfgDnodeReq(pMnode, cfgReq.dnodeId, &dcfgReq);
66,061✔
876
  if (code != 0) {
66,061✔
877
    mError("failed to send config req to dnode:%d, since %s", cfgReq.dnodeId, tstrerror(code));
2,644✔
878
    goto _err_out;
2,644✔
879
  }
880

881
_success:
73,280✔
882
  // dont care suss or succ;
883
  if (updateWhiteList) {
73,280✔
884
    int32_t dummy1 = mndRefreshUserIpWhiteList(pMnode);
138✔
885
    int32_t dummy2 = mndRefreshUserDateTimeWhiteList(pMnode);
138✔
886
    (void)dummy1;
887
    (void)dummy2;
888
  }
889
  tFreeSMCfgDnodeReq(&cfgReq);
73,280✔
890
  sdbRelease(pMnode->pSdb, vObj);
73,280✔
891
  mndReleaseUser(pMnode, pOperUser);
73,280✔
892
  TAOS_RETURN(code);
73,280✔
893

894
_err_out:
16,173✔
895
  mError("failed to process config dnode req, since %s", tstrerror(code));
16,173✔
896
  tFreeSMCfgDnodeReq(&cfgReq);
16,173✔
897
  sdbRelease(pMnode->pSdb, vObj);
16,173✔
898
  mndReleaseUser(pMnode, pOperUser);
16,173✔
899
  TAOS_RETURN(code);
16,173✔
900
}
901

UNCOV
902
static int32_t mndProcessConfigDnodeRsp(SRpcMsg *pRsp) {
×
UNCOV
903
  mInfo("config rsp from dnode");
×
UNCOV
904
  return 0;
×
905
}
906

907
static int32_t mndTryRebuildConfigSdbRsp(SRpcMsg *pRsp) {
197,451✔
908
  mInfo("rebuild config sdb rsp");
197,451✔
909
  return 0;
197,451✔
910
}
911

912
// Helper function to create and commit a config object
UNCOV
913
static int32_t mndCreateAndCommitConfigObj(STrans *pTrans, const char *srcName, const char *cfgName, char *value,
×
914
                                           int32_t *lino) {
UNCOV
915
  int32_t     code = 0;
×
UNCOV
916
  SConfigObj *pTmp = taosMemoryMalloc(sizeof(SConfigObj));
×
UNCOV
917
  if (pTmp == NULL) {
×
UNCOV
918
    code = terrno;
×
919
    return code;
×
920
  }
921

UNCOV
922
  pTmp->dtype = CFG_DTYPE_INT32;
×
UNCOV
923
  tstrncpy(pTmp->name, cfgName, CFG_NAME_MAX_LEN);
×
UNCOV
924
  code = mndUpdateObj(pTmp, srcName, value);
×
UNCOV
925
  if (code != 0) {
×
UNCOV
926
    tFreeSConfigObj(pTmp);
×
UNCOV
927
    taosMemoryFree(pTmp);
×
UNCOV
928
    return code;
×
929
  }
930

UNCOV
931
  code = mndSetCreateConfigCommitLogs(pTrans, pTmp);
×
932
  tFreeSConfigObj(pTmp);
×
933
  taosMemoryFree(pTmp);
×
934
  return code;
×
935
}
936

937
// Helper function to handle syncTimeout related config updates
UNCOV
938
static int32_t mndHandleSyncTimeoutConfigs(STrans *pTrans, const char *srcName, const char *pValue, int32_t *lino) {
×
939
  int32_t code = 0;
×
940
  int32_t syncTimeout = 0;
×
941
  char    tmp[10] = {0};
×
942

943
  if (sscanf(pValue, "%d", &syncTimeout) != 1) {
×
944
    syncTimeout = 0;
×
945
  }
946

UNCOV
947
  int32_t baseTimeout = syncTimeout - syncTimeout / SYNC_TIMEOUT_DIVISOR;
×
948

949
  // arbSetAssignedTimeoutMs = syncTimeout
950
  sprintf(tmp, "%d", syncTimeout);
×
951
  TAOS_CHECK_GOTO(mndCreateAndCommitConfigObj(pTrans, srcName, "arbSetAssignedTimeoutMs", tmp, lino), lino, _OVER);
×
952

953
  // arbHeartBeatIntervalMs = syncTimeout / 4
UNCOV
954
  sprintf(tmp, "%d", syncTimeout / SYNC_TIMEOUT_DIVISOR);
×
955
  TAOS_CHECK_GOTO(mndCreateAndCommitConfigObj(pTrans, srcName, "arbHeartBeatIntervalMs", tmp, lino), lino, _OVER);
×
956

957
  // arbCheckSyncIntervalMs = syncTimeout / 4
958
  TAOS_CHECK_GOTO(mndCreateAndCommitConfigObj(pTrans, srcName, "arbCheckSyncIntervalMs", tmp, lino), lino, _OVER);
×
959

960
  // syncVnodeElectIntervalMs = (syncTimeout - syncTimeout / 4) / 2
961
  sprintf(tmp, "%d", baseTimeout / SYNC_TIMEOUT_ELECT_DIVISOR);
×
UNCOV
962
  TAOS_CHECK_GOTO(mndCreateAndCommitConfigObj(pTrans, srcName, "syncVnodeElectIntervalMs", tmp, lino), lino, _OVER);
×
963

964
  // syncMnodeElectIntervalMs = (syncTimeout - syncTimeout / 4) / 2
UNCOV
965
  TAOS_CHECK_GOTO(mndCreateAndCommitConfigObj(pTrans, srcName, "syncMnodeElectIntervalMs", tmp, lino), lino, _OVER);
×
966

967
  // statusTimeoutMs = (syncTimeout - syncTimeout / 4) / 2
968
  TAOS_CHECK_GOTO(mndCreateAndCommitConfigObj(pTrans, srcName, "statusTimeoutMs", tmp, lino), lino, _OVER);
×
969

970
  // statusSRTimeoutMs = (syncTimeout - syncTimeout / 4) / 4
971
  sprintf(tmp, "%d", baseTimeout / SYNC_TIMEOUT_SR_DIVISOR);
×
972
  TAOS_CHECK_GOTO(mndCreateAndCommitConfigObj(pTrans, srcName, "statusSRTimeoutMs", tmp, lino), lino, _OVER);
×
973

974
  // syncVnodeHeartbeatIntervalMs = (syncTimeout - syncTimeout / 4) / 8
975
  sprintf(tmp, "%d", baseTimeout / SYNC_TIMEOUT_HB_DIVISOR);
×
UNCOV
976
  TAOS_CHECK_GOTO(mndCreateAndCommitConfigObj(pTrans, srcName, "syncVnodeHeartbeatIntervalMs", tmp, lino), lino, _OVER);
×
977

978
  // syncMnodeHeartbeatIntervalMs = (syncTimeout - syncTimeout / 4) / 8
979
  TAOS_CHECK_GOTO(mndCreateAndCommitConfigObj(pTrans, srcName, "syncMnodeHeartbeatIntervalMs", tmp, lino), lino, _OVER);
×
980

981
  // statusIntervalMs = (syncTimeout - syncTimeout / 4) / 8
982
  TAOS_CHECK_GOTO(mndCreateAndCommitConfigObj(pTrans, srcName, "statusIntervalMs", tmp, lino), lino, _OVER);
×
983

UNCOV
984
_OVER:
×
985
  return code;
×
986
}
987

988
// get int32_t value from 'SMCfgDnodeReq'
989
static int32_t mndMCfgGetValInt32(SMCfgDnodeReq *pMCfgReq, int32_t optLen, int32_t *pOutValue) {
×
UNCOV
990
  int32_t code = 0;
×
UNCOV
991
  if (' ' != pMCfgReq->config[optLen] && 0 != pMCfgReq->config[optLen]) {
×
992
    goto _err;
×
993
  }
994

UNCOV
995
  if (' ' == pMCfgReq->config[optLen]) {
×
996
    // 'key value'
UNCOV
997
    if (strlen(pMCfgReq->value) != 0) goto _err;
×
UNCOV
998
    *pOutValue = taosStr2Int32(pMCfgReq->config + optLen + 1, NULL, 10);
×
999
  } else {
1000
    // 'key' 'value'
1001
    if (strlen(pMCfgReq->value) == 0) goto _err;
×
1002
    *pOutValue = taosStr2Int32(pMCfgReq->value, NULL, 10);
×
1003
  }
1004

UNCOV
1005
  TAOS_RETURN(code);
×
1006

1007
_err:
×
1008
  mError(" failed to set config since:%s", tstrerror(code));
×
1009
  TAOS_RETURN(code);
×
1010
}
1011

1012
static int32_t mndConfigUpdateTrans(SMnode *pMnode, const char *name, char *pValue, ECfgDataType dtype,
×
1013
                                    int32_t tsmmConfigVersion) {
1014
  int32_t     code = -1;
×
1015
  int32_t     lino = -1;
×
UNCOV
1016
  SConfigObj *pVersion = taosMemoryMalloc(sizeof(SConfigObj)), *pObj = taosMemoryMalloc(sizeof(SConfigObj));
×
UNCOV
1017
  if (pVersion == NULL || pObj == NULL) {
×
1018
    code = terrno;
×
1019
    goto _OVER;
×
1020
  }
UNCOV
1021
  tstrncpy(pVersion->name, "tsmmConfigVersion", CFG_NAME_MAX_LEN);
×
1022
  pVersion->dtype = CFG_DTYPE_INT32;
×
UNCOV
1023
  pVersion->i32 = tsmmConfigVersion;
×
1024

1025
  pObj->dtype = dtype;
×
1026
  tstrncpy(pObj->name, name, CFG_NAME_MAX_LEN);
×
1027

UNCOV
1028
  TAOS_CHECK_GOTO(mndUpdateObj(pObj, name, pValue), &lino, _OVER);
×
1029
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, NULL, "update-config");
×
UNCOV
1030
  if (pTrans == NULL) {
×
1031
    code = terrno;
×
1032
    goto _OVER;
×
1033
  }
1034
  mInfo("trans:%d, used to update config:%s to value:%s", pTrans->id, name, pValue);
×
1035
  TAOS_CHECK_GOTO(mndSetCreateConfigCommitLogs(pTrans, pVersion), &lino, _OVER);
×
1036
  TAOS_CHECK_GOTO(mndSetCreateConfigCommitLogs(pTrans, pObj), &lino, _OVER);
×
1037

1038
  if (taosStrncasecmp(name, "syncTimeout", CFG_NAME_MAX_LEN) == 0) {
×
1039
    TAOS_CHECK_GOTO(mndHandleSyncTimeoutConfigs(pTrans, name, pValue, &lino), &lino, _OVER);
×
1040
  }
UNCOV
1041
  if ((code = mndTransPrepare(pMnode, pTrans)) != 0) goto _OVER;
×
1042
  code = 0;
×
1043
_OVER:
×
UNCOV
1044
  if (code != 0) {
×
1045
    mError("failed to update config:%s to value:%s, since %s", name, pValue, tstrerror(code));
×
1046
  }
1047
  mndTransDrop(pTrans);
×
1048
  tFreeSConfigObj(pVersion);
×
1049
  taosMemoryFree(pVersion);
×
UNCOV
1050
  tFreeSConfigObj(pObj);
×
1051
  taosMemoryFree(pObj);
×
1052
  return code;
×
1053
}
1054

1055
static int32_t mndConfigUpdateTransWithDnode(SMnode *pMnode, const char *name, char *pValue, ECfgDataType dtype,
9,863✔
1056
                                             int32_t tsmmConfigVersion, int32_t dnodeId, SDCfgDnodeReq *pDcfgReq) {
1057
  int32_t     code = -1;
9,863✔
1058
  int32_t     lino = -1;
9,863✔
1059
  SConfigObj *pVersion = taosMemoryMalloc(sizeof(SConfigObj)), *pObj = taosMemoryMalloc(sizeof(SConfigObj));
9,863✔
1060
  if (pVersion == NULL || pObj == NULL) {
9,863✔
1061
    code = terrno;
×
1062
    goto _OVER;
×
1063
  }
1064
  tstrncpy(pVersion->name, "tsmmConfigVersion", CFG_NAME_MAX_LEN);
9,863✔
1065
  pVersion->dtype = CFG_DTYPE_INT32;
9,863✔
1066
  pVersion->i32 = tsmmConfigVersion;
9,863✔
1067

1068
  pObj->dtype = dtype;
9,863✔
1069
  tstrncpy(pObj->name, name, CFG_NAME_MAX_LEN);
9,863✔
1070

1071
  TAOS_CHECK_GOTO(mndUpdateObj(pObj, name, pValue), &lino, _OVER);
9,863✔
1072
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_NOTHING, NULL, "update-config-with-dnode");
9,863✔
1073
  if (pTrans == NULL) {
9,863✔
UNCOV
1074
    code = terrno;
×
UNCOV
1075
    goto _OVER;
×
1076
  }
1077
  mInfo("trans:%d, used to update config:%s to value:%s and send to dnode", pTrans->id, name, pValue);
9,863✔
1078

1079
  // Add prepare logs for SDB config updates (execute in PREPARE stage, before redo actions)
1080
  TAOS_CHECK_GOTO(mndSetCreateConfigPrepareLogs(pTrans, pVersion), &lino, _OVER);
9,863✔
1081
  TAOS_CHECK_GOTO(mndSetCreateConfigPrepareLogs(pTrans, pObj), &lino, _OVER);
9,863✔
1082

1083
  // Add commit logs for transaction persistence
1084
  TAOS_CHECK_GOTO(mndSetCreateConfigCommitLogs(pTrans, pVersion), &lino, _OVER);
9,863✔
1085
  TAOS_CHECK_GOTO(mndSetCreateConfigCommitLogs(pTrans, pObj), &lino, _OVER);
9,863✔
1086

1087
  if (taosStrncasecmp(name, "syncTimeout", CFG_NAME_MAX_LEN) == 0) {
9,863✔
UNCOV
1088
    TAOS_CHECK_GOTO(mndHandleSyncTimeoutConfigs(pTrans, name, pValue, &lino), &lino, _OVER);
×
1089
  }
1090

1091
  // Add redo actions to send config to dnodes
1092
  SSdb   *pSdb = pMnode->pSdb;
9,863✔
1093
  void   *pIter = NULL;
9,863✔
1094
  int64_t curMs = taosGetTimestampMs();
9,863✔
1095

1096
  while (1) {
10,711✔
1097
    SDnodeObj *pDnode = NULL;
20,574✔
1098
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
20,574✔
1099
    if (pIter == NULL) break;
20,574✔
1100

1101
    if (pDnode->id == dnodeId || dnodeId == -1 || dnodeId == 0) {
10,711✔
1102
      bool online = mndIsDnodeOnline(pDnode, curMs);
10,711✔
1103
      if (!online) {
10,711✔
UNCOV
1104
        mWarn("dnode:%d, is offline, still add to trans for retry", pDnode->id);
×
1105
      }
1106

1107
      code = mndBuildCfgDnodeRedoAction(pTrans, pDnode, pDcfgReq);
10,711✔
1108
      if (code != 0) {
10,711✔
UNCOV
1109
        mError("failed to build config redo action for dnode:%d, since %s", pDnode->id, tstrerror(code));
×
UNCOV
1110
        sdbCancelFetch(pMnode->pSdb, pIter);
×
UNCOV
1111
        sdbRelease(pMnode->pSdb, pDnode);
×
UNCOV
1112
        goto _OVER;
×
1113
      }
1114
    }
1115
    sdbRelease(pSdb, pDnode);
10,711✔
1116
  }
1117

1118
  if ((code = mndTransPrepare(pMnode, pTrans)) != 0) goto _OVER;
9,863✔
1119
  code = 0;
9,863✔
1120

1121
_OVER:
9,863✔
1122
  if (code != 0) {
9,863✔
UNCOV
1123
    mError("failed to update config:%s to value:%s and send to dnode, since %s", name, pValue, tstrerror(code));
×
1124
  }
1125
  mndTransDrop(pTrans);
9,863✔
1126
  tFreeSConfigObj(pVersion);
9,863✔
1127
  taosMemoryFree(pVersion);
9,863✔
1128
  tFreeSConfigObj(pObj);
9,863✔
1129
  taosMemoryFree(pObj);
9,863✔
1130
  return code;
9,863✔
1131
}
1132

1133
static int32_t initConfigArrayFromSdb(SMnode *pMnode, SArray *array) {
472,192✔
1134
  int32_t     code = 0;
472,192✔
1135
  SSdb       *pSdb = pMnode->pSdb;
472,192✔
1136
  void       *pIter = NULL;
472,192✔
1137
  SConfigObj *obj = NULL;
472,192✔
1138

1139
  while (1) {
53,829,888✔
1140
    pIter = sdbFetch(pSdb, SDB_CFG, pIter, (void **)&obj);
54,302,080✔
1141
    if (pIter == NULL) break;
54,302,080✔
1142
    if (obj == NULL) {
53,829,888✔
UNCOV
1143
      code = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
1144
      goto _exit;
×
1145
    }
1146
    if (strcasecmp(obj->name, "tsmmConfigVersion") == 0) {
53,829,888✔
1147
      sdbRelease(pSdb, obj);
472,192✔
1148
      continue;
472,192✔
1149
    }
1150
    SConfigItem item = {0};
53,357,696✔
1151
    item.dtype = obj->dtype;
53,357,696✔
1152
    item.name = taosStrdup(obj->name);
53,357,696✔
1153
    if (item.name == NULL) {
53,357,696✔
UNCOV
1154
      code = terrno;
×
UNCOV
1155
      sdbCancelFetch(pSdb, pIter);
×
UNCOV
1156
      sdbRelease(pSdb, obj);
×
UNCOV
1157
      goto _exit;
×
1158
    }
1159
    switch (obj->dtype) {
53,357,696✔
1160
      case CFG_DTYPE_NONE:
×
1161
        break;
×
1162
      case CFG_DTYPE_BOOL:
12,749,184✔
1163
        item.bval = obj->bval;
12,749,184✔
1164
        break;
12,749,184✔
1165
      case CFG_DTYPE_INT32:
27,859,328✔
1166
        item.i32 = obj->i32;
27,859,328✔
1167
        break;
27,859,328✔
1168
      case CFG_DTYPE_INT64:
2,833,152✔
1169
        item.i64 = obj->i64;
2,833,152✔
1170
        break;
2,833,152✔
1171
      case CFG_DTYPE_FLOAT:
944,384✔
1172
      case CFG_DTYPE_DOUBLE:
1173
        item.fval = obj->fval;
944,384✔
1174
        break;
944,384✔
1175
      case CFG_DTYPE_STRING:
8,971,648✔
1176
      case CFG_DTYPE_DIR:
1177
      case CFG_DTYPE_LOCALE:
1178
      case CFG_DTYPE_CHARSET:
1179
      case CFG_DTYPE_TIMEZONE:
1180
        item.str = taosStrdup(obj->str);
8,971,648✔
1181
        if (item.str == NULL) {
8,971,648✔
UNCOV
1182
          sdbCancelFetch(pSdb, pIter);
×
UNCOV
1183
          sdbRelease(pSdb, obj);
×
UNCOV
1184
          code = terrno;
×
UNCOV
1185
          goto _exit;
×
1186
        }
1187
        break;
8,971,648✔
1188
    }
1189
    if (taosArrayPush(array, &item) == NULL) {
53,357,696✔
UNCOV
1190
      sdbCancelFetch(pSdb, pIter);
×
UNCOV
1191
      sdbRelease(pSdb, obj);
×
UNCOV
1192
      code = TSDB_CODE_OUT_OF_MEMORY;
×
UNCOV
1193
      goto _exit;
×
1194
      break;
1195
    }
1196
    sdbRelease(pSdb, obj);
53,357,696✔
1197
  }
1198
_exit:
472,192✔
1199
  if (code != 0) {
472,192✔
1200
    mError("failed to init config array from sdb, since %s", tstrerror(code));
×
1201
  }
1202
  return code;
472,192✔
1203
}
1204

1205
static void cfgArrayCleanUp(SArray *array) {
605,006✔
1206
  if (array == NULL) {
605,006✔
1207
    return;
×
1208
  }
1209

1210
  int32_t sz = taosArrayGetSize(array);
605,006✔
1211
  for (int32_t i = 0; i < sz; ++i) {
53,962,702✔
1212
    SConfigItem *item = taosArrayGet(array, i);
53,357,696✔
1213
    if (item->dtype == CFG_DTYPE_STRING || item->dtype == CFG_DTYPE_DIR || item->dtype == CFG_DTYPE_LOCALE ||
53,357,696✔
1214
        item->dtype == CFG_DTYPE_CHARSET || item->dtype == CFG_DTYPE_TIMEZONE) {
45,330,432✔
1215
      taosMemoryFreeClear(item->str);
8,971,648✔
1216
    }
1217
    taosMemoryFreeClear(item->name);
53,357,696✔
1218
  }
1219

1220
  taosArrayDestroy(array);
605,006✔
1221
}
1222

1223
static void cfgObjArrayCleanUp(SArray *array) {
396,126✔
1224
  if (array == NULL) {
396,126✔
UNCOV
1225
    return;
×
1226
  }
1227
  int32_t sz = taosArrayGetSize(array);
396,126✔
1228
  for (int32_t i = 0; i < sz; ++i) {
396,126✔
UNCOV
1229
    SConfigObj *obj = taosArrayGet(array, i);
×
UNCOV
1230
    tFreeSConfigObj(obj);
×
1231
  }
1232
  taosArrayDestroy(array);
396,126✔
1233
}
1234

1235
static SArray *initVariablesFromItems(SArray *pItems, const char* likePattern) {
10,402✔
1236
  if (pItems == NULL) {
10,402✔
UNCOV
1237
    return NULL;
×
1238
  }
1239

1240
  int32_t sz = taosArrayGetSize(pItems);
10,402✔
1241

1242
  SArray *pInfos = taosArrayInit(sz, sizeof(SVariablesInfo));
10,402✔
1243
  if (pInfos == NULL) {
10,402✔
UNCOV
1244
    mError("failed to init array while init variables from items, since %s", tstrerror(terrno));
×
UNCOV
1245
    return NULL;
×
1246
  }
1247
  for (int32_t i = 0; i < sz; ++i) {
1,185,828✔
1248
    SConfigItem   *pItem = taosArrayGet(pItems, i);
1,175,426✔
1249
    SVariablesInfo info = {0};
1,175,426✔
1250
    tstrncpy(info.name, pItem->name, sizeof(info.name));
1,175,426✔
1251
    if (likePattern != NULL && rawStrPatternMatch(pItem->name, likePattern) != TSDB_PATTERN_MATCH) {
1,175,426✔
1252
      continue;
291,808✔
1253
    }
1254

1255
    // init info value
1256
    switch (pItem->dtype) {
883,618✔
UNCOV
1257
      case CFG_DTYPE_NONE:
×
UNCOV
1258
        break;
×
1259
      case CFG_DTYPE_BOOL:
210,032✔
1260
        tsnprintf(info.value, sizeof(info.value), "%d", pItem->bval);
210,032✔
1261
        break;
210,032✔
1262
      case CFG_DTYPE_INT32:
463,472✔
1263
        tsnprintf(info.value, sizeof(info.value), "%d", pItem->i32);
463,472✔
1264
        break;
463,472✔
1265
      case CFG_DTYPE_INT64:
46,560✔
1266
        tsnprintf(info.value, sizeof(info.value), "%" PRId64, pItem->i64);
46,560✔
1267
        break;
46,560✔
1268
      case CFG_DTYPE_FLOAT:
15,520✔
1269
      case CFG_DTYPE_DOUBLE:
1270
        tsnprintf(info.value, sizeof(info.value), "%f", pItem->fval);
15,520✔
1271
        break;
15,520✔
1272
      case CFG_DTYPE_STRING:
148,034✔
1273
      case CFG_DTYPE_DIR:
1274
      case CFG_DTYPE_LOCALE:
1275
      case CFG_DTYPE_CHARSET:
1276
      case CFG_DTYPE_TIMEZONE:
1277
        tsnprintf(info.value, sizeof(info.value), "%s", pItem->str);
148,034✔
1278
        break;
148,034✔
1279
    }
1280

1281
    // init info scope
1282
    switch (pItem->scope) {
883,618✔
1283
      case CFG_SCOPE_SERVER:
712,816✔
1284
        tstrncpy(info.scope, "server", sizeof(info.scope));
712,816✔
1285
        break;
712,816✔
UNCOV
1286
      case CFG_SCOPE_CLIENT:
×
UNCOV
1287
        tstrncpy(info.scope, "client", sizeof(info.scope));
×
UNCOV
1288
        break;
×
1289
      case CFG_SCOPE_BOTH:
170,802✔
1290
        tstrncpy(info.scope, "both", sizeof(info.scope));
170,802✔
1291
        break;
170,802✔
UNCOV
1292
      default:
×
UNCOV
1293
        tstrncpy(info.scope, "unknown", sizeof(info.scope));
×
UNCOV
1294
        break;
×
1295
    }
1296
    // init info category
1297
    switch (pItem->category) {
883,618✔
1298
      case CFG_CATEGORY_GLOBAL:
883,618✔
1299
        tstrncpy(info.category, "global", sizeof(info.category));
883,618✔
1300
        break;
883,618✔
UNCOV
1301
      case CFG_CATEGORY_LOCAL:
×
UNCOV
1302
        tstrncpy(info.category, "local", sizeof(info.category));
×
1303
        break;
×
1304
      default:
×
1305
        tstrncpy(info.category, "unknown", sizeof(info.category));
×
UNCOV
1306
        break;
×
1307
    }
1308
    if (NULL == taosArrayPush(pInfos, &info)) {
883,618✔
1309
      mError("failed to push info to array while init variables from items,since %s", tstrerror(terrno));
×
1310
      taosArrayDestroy(pInfos);
×
1311
      return NULL;
×
1312
    }
1313
  }
1314

1315
  return pInfos;
10,402✔
1316
}
1317

1318
static int32_t mndProcessShowVariablesReq(SRpcMsg *pReq) {
10,402✔
1319
  SShowVariablesRsp rsp = {0};
10,402✔
1320
  int32_t           code = TSDB_CODE_SUCCESS;
10,402✔
1321
  SShowVariablesReq req = {0};
10,402✔
1322
  SArray           *array = NULL;
10,402✔
1323

1324
  code = tDeserializeSShowVariablesReq(pReq->pCont, pReq->contLen, &req);
10,402✔
1325
  if (code != 0) {
10,402✔
1326
    mError("failed to deserialize config req, since %s", terrstr());
×
1327
    goto _OVER;
×
1328
  }
1329

1330
  if ((code = mndCheckOperPrivilege(pReq->info.node, RPC_MSG_USER(pReq), RPC_MSG_TOKEN(pReq), MND_OPER_SHOW_VARIABLES)) != 0) {
10,402✔
UNCOV
1331
    goto _OVER;
×
1332
  }
1333

1334
  SVariablesInfo info = {0};
10,402✔
1335
  char          *likePattern = req.opType == OP_TYPE_LIKE ? req.val : NULL;
10,402✔
1336
  rsp.variables = initVariablesFromItems(taosGetGlobalCfg(tsCfg), likePattern);
10,402✔
1337
  if (rsp.variables == NULL) {
10,402✔
UNCOV
1338
    code = terrno;
×
UNCOV
1339
    goto _OVER;
×
1340
  }
1341
  int32_t rspLen = tSerializeSShowVariablesRsp(NULL, 0, &rsp);
10,402✔
1342
  void   *pRsp = rpcMallocCont(rspLen);
10,402✔
1343
  if (pRsp == NULL) {
10,402✔
1344
    code = terrno;
×
UNCOV
1345
    goto _OVER;
×
1346
  }
1347

1348
  if ((rspLen = tSerializeSShowVariablesRsp(pRsp, rspLen, &rsp)) <= 0) {
10,402✔
UNCOV
1349
    rpcFreeCont(pRsp);
×
UNCOV
1350
    code = rspLen;
×
UNCOV
1351
    goto _OVER;
×
1352
  }
1353

1354
  pReq->info.rspLen = rspLen;
10,402✔
1355
  pReq->info.rsp = pRsp;
10,402✔
1356
  code = 0;
10,402✔
1357

1358
_OVER:
10,402✔
1359

1360
  if (code != 0) {
10,402✔
1361
    mError("failed to get show variables info since %s", tstrerror(code));
×
1362
  }
1363
  tFreeSShowVariablesReq(&req);
10,402✔
1364
  tFreeSShowVariablesRsp(&rsp);
10,402✔
1365
  TAOS_RETURN(code);
10,402✔
1366
}
1367

1368
int32_t compareSConfigItem(const SConfigObj *item1, SConfigItem *item2, bool *compare) {
×
UNCOV
1369
  *compare = true;
×
UNCOV
1370
  switch (item1->dtype) {
×
UNCOV
1371
    case CFG_DTYPE_BOOL:
×
UNCOV
1372
      if (item1->bval != item2->bval) {
×
UNCOV
1373
        item2->bval = item1->bval;
×
UNCOV
1374
        *compare = false;
×
1375
      }
UNCOV
1376
      break;
×
UNCOV
1377
    case CFG_DTYPE_FLOAT:
×
1378
      if (item1->fval != item2->fval) {
×
UNCOV
1379
        item2->fval = item1->fval;
×
UNCOV
1380
        *compare = false;
×
1381
      }
UNCOV
1382
      break;
×
UNCOV
1383
    case CFG_DTYPE_INT32:
×
UNCOV
1384
      if (item1->i32 != item2->i32) {
×
1385
        item2->i32 = item1->i32;
×
1386
        *compare = false;
×
1387
      }
1388
      break;
×
1389
    case CFG_DTYPE_INT64:
×
1390
      if (item1->i64 != item2->i64) {
×
1391
        item2->i64 = item1->i64;
×
UNCOV
1392
        *compare = false;
×
1393
      }
1394
      break;
×
1395
    case CFG_DTYPE_STRING:
×
1396
    case CFG_DTYPE_DIR:
1397
    case CFG_DTYPE_LOCALE:
1398
    case CFG_DTYPE_CHARSET:
1399
    case CFG_DTYPE_TIMEZONE:
1400
      if (strcmp(item1->str, item2->str) != 0) {
×
1401
        item2->str = taosStrdup(item1->str);
×
1402
        if (item2->str == NULL) {
×
1403
          return TSDB_CODE_OUT_OF_MEMORY;
×
1404
        }
1405
        *compare = false;
×
1406
      }
1407
      break;
×
1408
    default:
×
1409
      *compare = false;
×
UNCOV
1410
      return TSDB_CODE_INVALID_CFG;
×
1411
  }
1412
  return TSDB_CODE_SUCCESS;
×
1413
}
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