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

taosdata / TDengine / #4822

27 Oct 2025 05:42AM UTC coverage: 59.732% (+1.0%) from 58.728%
#4822

push

travis-ci

web-flow
Merge pull request #33377 from taosdata/fix/main/rename-udf-path

fix: update UDF example links to correct file paths

121214 of 258518 branches covered (46.89%)

Branch coverage included in aggregate %.

193636 of 268583 relevant lines covered (72.1%)

4002399.5 hits per line

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

46.21
/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
static int32_t mndMCfgGetValInt32(SMCfgDnodeReq *pInMCfgReq, int32_t optLen, int32_t *pOutValue);
39
static int32_t mndProcessShowVariablesReq(SRpcMsg *pReq);
40
static int32_t mndProcessConfigDnodeReq(SRpcMsg *pReq);
41
static int32_t mndProcessConfigDnodeRsp(SRpcMsg *pRsp);
42
static int32_t mndProcessConfigReq(SRpcMsg *pReq);
43
static int32_t mndInitWriteCfg(SMnode *pMnode);
44
static int32_t mndSendRebuildReq(SMnode *pMnode);
45
static int32_t mndTryRebuildConfigSdbRsp(SRpcMsg *pRsp);
46
static int32_t initConfigArrayFromSdb(SMnode *pMnode, SArray *array);
47
static int32_t mndTryRebuildConfigSdb(SRpcMsg *pReq);
48
static void    cfgArrayCleanUp(SArray *array);
49
static void    cfgObjArrayCleanUp(SArray *array);
50
int32_t        compareSConfigItemArrays(SMnode *pMnode, const SArray *dArray, SArray *diffArray);
51

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

60
int32_t mndSetCreateConfigCommitLogs(STrans *pTrans, SConfigObj *obj);
61
int32_t mndSetDeleteConfigCommitLogs(STrans *pTrans, SConfigObj *item);
62

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

75
  mndSetMsgHandle(pMnode, TDMT_MND_CONFIG, mndProcessConfigReq);
1,306✔
76
  mndSetMsgHandle(pMnode, TDMT_MND_CONFIG_DNODE, mndProcessConfigDnodeReq);
1,306✔
77
  mndSetMsgHandle(pMnode, TDMT_DND_CONFIG_DNODE_RSP, mndProcessConfigDnodeRsp);
1,306✔
78
  mndSetMsgHandle(pMnode, TDMT_MND_SHOW_VARIABLES, mndProcessShowVariablesReq);
1,306✔
79
  mndSetMsgHandle(pMnode, TDMT_MND_CONFIG_SDB, mndTryRebuildConfigSdb);
1,306✔
80
  mndSetMsgHandle(pMnode, TDMT_MND_CONFIG_SDB_RSP, mndTryRebuildConfigSdbRsp);
1,306✔
81

82
  return sdbSetTable(pMnode->pSdb, table);
1,306✔
83
}
84

85
SSdbRaw *mnCfgActionEncode(SConfigObj *obj) {
323,791✔
86
  int32_t  code = 0;
323,791✔
87
  int32_t  lino = 0;
323,791✔
88
  void    *buf = NULL;
323,791✔
89
  SSdbRaw *pRaw = NULL;
323,791✔
90

91
  SEncoder encoder;
92
  tEncoderInit(&encoder, NULL, 0);
323,791✔
93
  if ((code = tEncodeSConfigObj(&encoder, obj)) < 0) {
323,791!
94
    tEncoderClear(&encoder);
×
95
    TSDB_CHECK_CODE(code, lino, _over);
×
96
  }
97

98
  int32_t tlen = encoder.pos;
323,791✔
99
  tEncoderClear(&encoder);
323,791✔
100

101
  int32_t size = sizeof(int32_t) + tlen;
323,791✔
102
  pRaw = sdbAllocRaw(SDB_CFG, CFG_VER_NUMBER, size);
323,791✔
103
  TSDB_CHECK_NULL(pRaw, code, lino, _over, terrno);
323,791!
104

105
  buf = taosMemoryMalloc(tlen);
323,791!
106
  TSDB_CHECK_NULL(buf, code, lino, _over, terrno);
323,791!
107

108
  tEncoderInit(&encoder, buf, tlen);
323,791✔
109
  if ((code = tEncodeSConfigObj(&encoder, obj)) < 0) {
323,791!
110
    tEncoderClear(&encoder);
×
111
    TSDB_CHECK_CODE(code, lino, _over);
×
112
  }
113

114
  tEncoderClear(&encoder);
323,791✔
115

116
  int32_t dataPos = 0;
323,791✔
117
  SDB_SET_INT32(pRaw, dataPos, tlen, _over);
323,791!
118
  SDB_SET_BINARY(pRaw, dataPos, buf, tlen, _over);
323,791!
119
  SDB_SET_DATALEN(pRaw, dataPos, _over);
323,791!
120

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

130
  terrno = 0;
323,791✔
131
  mTrace("cfg:%s, encode to raw:%p, row:%p", obj->name, pRaw, obj);
323,791✔
132
  return pRaw;
323,791✔
133
}
134

135
SSdbRow *mndCfgActionDecode(SSdbRaw *pRaw) {
132,070✔
136
  int32_t     code = 0;
132,070✔
137
  int32_t     lino = 0;
132,070✔
138
  SSdbRow    *pRow = NULL;
132,070✔
139
  SConfigObj *pObj = NULL;
132,070✔
140
  void       *buf = NULL;
132,070✔
141
  int8_t      sver = 0;
132,070✔
142
  int32_t     tlen;
143
  int32_t     dataPos = 0;
132,070✔
144

145
  code = sdbGetRawSoftVer(pRaw, &sver);
132,070✔
146
  TSDB_CHECK_CODE(code, lino, _over);
132,070!
147

148
  if (sver != CFG_VER_NUMBER) {
132,070!
149
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
×
150
    goto _over;
×
151
  }
152

153
  pRow = sdbAllocRow(sizeof(SConfigObj));
132,070✔
154
  TSDB_CHECK_NULL(pRow, code, lino, _over, terrno);
132,070!
155

156
  pObj = sdbGetRowObj(pRow);
132,070✔
157
  TSDB_CHECK_NULL(pObj, code, lino, _over, terrno);
132,070!
158

159
  SDB_GET_INT32(pRaw, dataPos, &tlen, _over);
132,070!
160

161
  buf = taosMemoryMalloc(tlen + 1);
132,070!
162
  TSDB_CHECK_NULL(buf, code, lino, _over, terrno);
132,070!
163

164
  SDB_GET_BINARY(pRaw, dataPos, buf, tlen, _over);
132,070!
165

166
  SDecoder decoder;
167
  tDecoderInit(&decoder, buf, tlen + 1);
132,070✔
168
  code = tDecodeSConfigObj(&decoder, pObj);
132,070✔
169
  tDecoderClear(&decoder);
132,070✔
170

171
  if (code < 0) {
132,070!
172
    tFreeSConfigObj(pObj);
×
173
  }
174

175
_over:
132,070✔
176
  taosMemoryFreeClear(buf);
132,070!
177

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

190
static int32_t mndCfgActionInsert(SSdb *pSdb, SConfigObj *obj) {
132,070✔
191
  mTrace("cfg:%s, perform insert action, row:%p", obj->name, obj);
132,070✔
192
  return 0;
132,070✔
193
}
194

195
static int32_t mndCfgActionDelete(SSdb *pSdb, SConfigObj *obj) {
132,070✔
196
  mTrace("cfg:%s, perform delete action, row:%p", obj->name, obj);
132,070✔
197
  tFreeSConfigObj(obj);
132,070✔
198
  return 0;
132,070✔
199
}
200

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

234
static int32_t mndCfgActionDeploy(SMnode *pMnode) { return mndInitWriteCfg(pMnode); }
852✔
235

236
static int32_t mndCfgActionAfterRestored(SMnode *pMnode) { return mndSendRebuildReq(pMnode); }
691✔
237

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

250
  SConfigObj *vObj = sdbAcquire(pMnode->pSdb, SDB_CFG, "tsmmConfigVersion");
1,742✔
251
  if (vObj == NULL) {
1,742!
252
    mInfo("failed to acquire mnd config version, since %s", terrstr());
×
253
    goto _OVER;
×
254
  }
255

256
  array = taosArrayInit(16, sizeof(SConfigItem));
1,742✔
257
  if (array == NULL) {
1,742!
258
    code = TSDB_CODE_OUT_OF_MEMORY;
×
259
    goto _OVER;
×
260
  }
261
  SConfigRsp configRsp = {0};
1,742✔
262
  configRsp.cver = vObj->i32;
1,742✔
263

264
  if (configReq.cver == vObj->i32) {
1,742✔
265
    configRsp.isVersionVerified = 1;
399✔
266
  } else {
267
    code = initConfigArrayFromSdb(pMnode, array);
1,343✔
268
    if (code != 0) {
1,343!
269
      mError("failed to init config array from sdb, since %s", tstrerror(code));
×
270
      goto _OVER;
×
271
    }
272
    configRsp.array = array;
1,343✔
273
  }
274

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

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

304
int32_t mndInitWriteCfg(SMnode *pMnode) {
852✔
305
  int    code = 0;
852✔
306
  size_t sz = 0;
852✔
307

308
  mInfo("init write cfg to sdb");
852!
309
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_NOTHING, NULL, "init-write-config");
852✔
310
  if (pTrans == NULL) {
852!
311
    mError("failed to init write cfg in create trans, since %s", terrstr());
×
312
    goto _OVER;
×
313
  }
314

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

325
  for (int i = 0; i < sz; ++i) {
85,308✔
326
    SConfigItem *item = taosArrayGet(taosGetGlobalCfg(tsCfg), i);
84,456✔
327
    SConfigObj   obj;
328
    if ((code = mndInitConfigObj(item, &obj)) != 0) {
84,456!
329
      goto _OVER;
×
330
    }
331
    if ((code = mndSetCreateConfigCommitLogs(pTrans, &obj)) != 0) {
84,456!
332
      mError("failed to init mnd config:%s, since %s", item->name, tstrerror(code));
×
333
      tFreeSConfigObj(&obj);
×
334
      goto _OVER;
×
335
    }
336
    tFreeSConfigObj(&obj);
84,456✔
337
  }
338
  if ((code = mndTransPrepare(pMnode, pTrans)) != 0) goto _OVER;
852!
339

340
_OVER:
852✔
341
  if (code != 0) {
852!
342
    mError("failed to init write cfg, since %s", tstrerror(code));
×
343
  }
344
  mndTransDrop(pTrans);
852✔
345
  return code;
852✔
346
}
347

348
int32_t mndSendRebuildReq(SMnode *pMnode) {
691✔
349
  int32_t code = 0;
691✔
350

351
  SRpcMsg rpcMsg = {.pCont = NULL,
691✔
352
                    .contLen = 0,
353
                    .msgType = TDMT_MND_CONFIG_SDB,
354
                    .info.ahandle = 0,
355
                    .info.notFreeAhandle = 1,
356
                    .info.refId = 0,
357
                    .info.noResp = 0,
358
                    .info.handle = 0};
359
  SEpSet  epSet = {0};
691✔
360

361
  mndGetMnodeEpSet(pMnode, &epSet);
691✔
362

363
  code = tmsgSendReq(&epSet, &rpcMsg);
691✔
364
  if (code != 0) {
691!
365
    mError("failed to send rebuild config req, since %s", tstrerror(code));
×
366
  }
367
  return code;
691✔
368
}
369

370
static int32_t mndTryRebuildConfigSdb(SRpcMsg *pReq) {
687✔
371
  SMnode *pMnode = pReq->info.node;
687✔
372
  if (!mndIsLeader(pMnode)) {
687!
373
    return TSDB_CODE_SUCCESS;
×
374
  }
375

376
  int32_t     code = 0;
687✔
377
  SConfigObj *vObj = NULL;
687✔
378
  SArray     *addArray = NULL;
687✔
379
  SArray     *deleteArray = NULL;
687✔
380

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

392
  addArray = taosArrayInit(4, sizeof(SConfigObj));
687✔
393
  deleteArray = taosArrayInit(4, sizeof(SConfigObj));
687✔
394
  if (addArray == NULL || deleteArray == NULL) {
687!
395
    code = TSDB_CODE_OUT_OF_MEMORY;
×
396
    goto _exit;
×
397
  }
398

399
  // Find configs to add and delete
400
  if ((code = mndFindConfigsToAdd(pMnode, addArray)) != 0) {
687!
401
    mError("failed to find configs to add, since %s", tstrerror(code));
×
402
    goto _exit;
×
403
  }
404

405
  if ((code = mndFindConfigsToDelete(pMnode, deleteArray)) != 0) {
687!
406
    mError("failed to find configs to delete, since %s", tstrerror(code));
×
407
    goto _exit;
×
408
  }
409

410
  // Execute the sync transaction
411
  if ((code = mndExecuteConfigSyncTrans(pMnode, addArray, deleteArray)) != 0) {
687!
412
    mError("failed to execute config sync transaction, since %s", tstrerror(code));
×
413
    goto _exit;
×
414
  }
415

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

426
int32_t mndSetCreateConfigCommitLogs(STrans *pTrans, SConfigObj *item) {
85,404✔
427
  int32_t  code = 0;
85,404✔
428
  SSdbRaw *pCommitRaw = mnCfgActionEncode(item);
85,404✔
429
  if (pCommitRaw == NULL) {
85,404!
430
    code = terrno;
×
431
    TAOS_RETURN(code);
×
432
  }
433
  if ((code = mndTransAppendCommitlog(pTrans, pCommitRaw) != 0)) TAOS_RETURN(code);
85,404!
434
  if ((code = sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY)) != 0) TAOS_RETURN(code);
85,404!
435
  return TSDB_CODE_SUCCESS;
85,404✔
436
}
437

438
int32_t mndSetDeleteConfigCommitLogs(STrans *pTrans, SConfigObj *item) {
×
439
  int32_t  code = 0;
×
440
  SSdbRaw *pCommitRaw = mnCfgActionEncode(item);
×
441
  if (pCommitRaw == NULL) {
×
442
    code = terrno;
×
443
    TAOS_RETURN(code);
×
444
  }
445
  if ((code = mndTransAppendCommitlog(pTrans, pCommitRaw) != 0)) TAOS_RETURN(code);
×
446
  if ((code = sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED)) != 0) TAOS_RETURN(code);
×
447
  return TSDB_CODE_SUCCESS;
×
448
}
449

450
static int32_t mndFindConfigsToAdd(SMnode *pMnode, SArray *addArray) {
687✔
451
  int32_t code = 0;
687✔
452
  int32_t sz = taosArrayGetSize(taosGetGlobalCfg(tsCfg));
687✔
453

454
  for (int i = 0; i < sz; ++i) {
70,761✔
455
    SConfigItem *item = taosArrayGet(taosGetGlobalCfg(tsCfg), i);
70,074✔
456
    SConfigObj  *obj = sdbAcquire(pMnode->pSdb, SDB_CFG, item->name);
70,074✔
457
    if (obj == NULL) {
70,074!
458
      mInfo("config:%s, not exist in sdb, will add it", item->name);
×
459
      SConfigObj newObj;
460
      if ((code = mndInitConfigObj(item, &newObj)) != 0) {
×
461
        TAOS_RETURN(code);
×
462
      }
463
      if (NULL == taosArrayPush(addArray, &newObj)) {
×
464
        tFreeSConfigObj(&newObj);
×
465
        TAOS_RETURN(terrno);
×
466
      }
467
    } else {
468
      sdbRelease(pMnode->pSdb, obj);
70,074✔
469
    }
470
  }
471

472
  TAOS_RETURN(TSDB_CODE_SUCCESS);
687✔
473
}
474

475
static int32_t mndFindConfigsToDelete(SMnode *pMnode, SArray *deleteArray) {
687✔
476
  int32_t     code = 0;
687✔
477
  int32_t     sz = taosArrayGetSize(taosGetGlobalCfg(tsCfg));
687✔
478
  SSdb       *pSdb = pMnode->pSdb;
687✔
479
  void       *pIter = NULL;
687✔
480
  SConfigObj *obj = NULL;
687✔
481

482
  while (1) {
70,761✔
483
    pIter = sdbFetch(pSdb, SDB_CFG, pIter, (void **)&obj);
71,448✔
484
    if (pIter == NULL) break;
71,448✔
485
    if (obj == NULL) {
70,761!
486
      code = TSDB_CODE_OUT_OF_MEMORY;
×
487
      sdbCancelFetch(pSdb, pIter);
×
488
      TAOS_RETURN(code);
×
489
    }
490

491
    // Skip the version config
492
    if (strcasecmp(obj->name, "tsmmConfigVersion") == 0) {
70,761✔
493
      sdbRelease(pSdb, obj);
687✔
494
      continue;
687✔
495
    }
496

497
    // Check if this config exists in global config
498
    bool existsInGlobal = false;
70,074✔
499
    for (int i = 0; i < sz; ++i) {
3,608,811!
500
      SConfigItem *item = taosArrayGet(taosGetGlobalCfg(tsCfg), i);
3,608,811✔
501
      if (strcasecmp(obj->name, item->name) == 0) {
3,608,811✔
502
        existsInGlobal = true;
70,074✔
503
        break;
70,074✔
504
      }
505
    }
506

507
    if (!existsInGlobal) {
70,074!
508
      mInfo("config:%s, not exist in global config, will delete it from sdb", obj->name);
×
509
      SConfigObj deleteObj = {0};
×
510
      tstrncpy(deleteObj.name, obj->name, CFG_NAME_MAX_LEN);
×
511
      deleteObj.dtype = obj->dtype;
×
512

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

544
      if (NULL == taosArrayPush(deleteArray, &deleteObj)) {
×
545
        tFreeSConfigObj(&deleteObj);
×
546
        sdbCancelFetch(pSdb, pIter);
×
547
        sdbRelease(pSdb, obj);
×
548
        TAOS_RETURN(terrno);
×
549
      }
550
    }
551

552
    sdbRelease(pSdb, obj);
70,074✔
553
  }
554

555
  TAOS_RETURN(TSDB_CODE_SUCCESS);
687✔
556
}
557

558
static int32_t mndExecuteConfigSyncTrans(SMnode *pMnode, SArray *addArray, SArray *deleteArray) {
687✔
559
  int32_t addSize = taosArrayGetSize(addArray);
687✔
560
  int32_t deleteSize = taosArrayGetSize(deleteArray);
687✔
561

562
  if (addSize == 0 && deleteSize == 0) {
687!
563
    return TSDB_CODE_SUCCESS;
687✔
564
  }
565

566
  const char *transName = "sync-config";
×
567
  if (addSize > 0 && deleteSize > 0) {
×
568
    transName = "sync-config";
×
569
  } else if (addSize > 0) {
×
570
    transName = "add-config";
×
571
  } else {
572
    transName = "delete-config";
×
573
  }
574

575
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_NOTHING, NULL, transName);
×
576
  if (pTrans == NULL) {
×
577
    TAOS_RETURN(terrno);
×
578
  }
579

580
  int32_t code = 0;
×
581

582
  // Add new configs
583
  for (int i = 0; i < addSize; ++i) {
×
584
    SConfigObj *AddObj = taosArrayGet(addArray, i);
×
585
    if ((code = mndSetCreateConfigCommitLogs(pTrans, AddObj)) != 0) {
×
586
      mndTransDrop(pTrans);
×
587
      TAOS_RETURN(code);
×
588
    }
589
  }
590

591
  // Delete obsolete configs
592
  for (int i = 0; i < deleteSize; ++i) {
×
593
    SConfigObj *DelObj = taosArrayGet(deleteArray, i);
×
594
    if ((code = mndSetDeleteConfigCommitLogs(pTrans, DelObj)) != 0) {
×
595
      mndTransDrop(pTrans);
×
596
      TAOS_RETURN(code);
×
597
    }
598
  }
599

600
  if ((code = mndTransPrepare(pMnode, pTrans)) != 0) {
×
601
    mndTransDrop(pTrans);
×
602
    TAOS_RETURN(code);
×
603
  }
604

605
  mInfo("sync config to sdb, add nums:%d, delete nums:%d", addSize, deleteSize);
×
606
  mndTransDrop(pTrans);
×
607
  TAOS_RETURN(TSDB_CODE_SUCCESS);
×
608
}
609

610
static int32_t mndMCfg2DCfg(SMCfgDnodeReq *pMCfgReq, SDCfgDnodeReq *pDCfgReq) {
296✔
611
  int32_t code = 0;
296✔
612
  char   *p = pMCfgReq->config;
296✔
613
  while (*p) {
4,407✔
614
    if (*p == ' ') {
4,286✔
615
      break;
175✔
616
    }
617
    p++;
4,111✔
618
  }
619

620
  size_t optLen = p - pMCfgReq->config;
296✔
621
  tstrncpy(pDCfgReq->config, pMCfgReq->config, sizeof(pDCfgReq->config));
296✔
622
  pDCfgReq->config[optLen] = 0;
296✔
623

624
  if (' ' == pMCfgReq->config[optLen]) {
296✔
625
    // 'key value'
626
    if (strlen(pMCfgReq->value) != 0) goto _err;
175!
627
    tstrncpy(pDCfgReq->value, p + 1, sizeof(pDCfgReq->value));
175✔
628
  } else {
629
    // 'key' 'value'
630
    if (strlen(pMCfgReq->value) == 0) goto _err;
121✔
631
    tstrncpy(pDCfgReq->value, pMCfgReq->value, sizeof(pDCfgReq->value));
117✔
632
  }
633

634
  TAOS_RETURN(code);
292✔
635

636
_err:
4✔
637
  mError("dnode:%d, failed to config since invalid conf:%s", pMCfgReq->dnodeId, pMCfgReq->config);
4!
638
  code = TSDB_CODE_INVALID_CFG;
4✔
639
  TAOS_RETURN(code);
4✔
640
}
641

642
static int32_t mndBuildCfgDnodeRedoAction(STrans *pTrans, SDnodeObj *pDnode, SDCfgDnodeReq *pDcfgReq) {
54✔
643
  int32_t code = 0;
54✔
644
  SEpSet  epSet = mndGetDnodeEpset(pDnode);
54✔
645
  int32_t bufLen = tSerializeSDCfgDnodeReq(NULL, 0, pDcfgReq);
54✔
646
  void   *pBuf = taosMemoryMalloc(bufLen);
54!
647

648
  if (pBuf == NULL) {
54!
649
    code = terrno;
×
650
    return code;
×
651
  }
652

653
  if ((bufLen = tSerializeSDCfgDnodeReq(pBuf, bufLen, pDcfgReq)) <= 0) {
54!
654
    code = bufLen;
×
655
    taosMemoryFree(pBuf);
×
656
    return code;
×
657
  }
658

659
  STransAction action = {
54✔
660
      .epSet = epSet,
661
      .pCont = pBuf,
662
      .contLen = bufLen,
663
      .msgType = TDMT_DND_CONFIG_DNODE,
664
      .acceptableCode = 0,
665
      .groupId = -1,
666
  };
667

668
  mInfo("dnode:%d, append redo action to trans, config:%s value:%s", pDnode->id, pDcfgReq->config, pDcfgReq->value);
54!
669

670
  if ((code = mndTransAppendRedoAction(pTrans, &action)) != 0) {
54!
671
    taosMemoryFree(pBuf);
×
672
    return code;
×
673
  }
674

675
  return code;
54✔
676
}
677

678
static int32_t mndSendCfgDnodeReq(SMnode *pMnode, int32_t dnodeId, SDCfgDnodeReq *pDcfgReq) {
147✔
679
  int32_t code = -1;
147✔
680
  SSdb   *pSdb = pMnode->pSdb;
147✔
681
  void   *pIter = NULL;
147✔
682

683
  int64_t curMs = taosGetTimestampMs();
147✔
684

685
  while (1) {
328✔
686
    SDnodeObj *pDnode = NULL;
475✔
687
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
475✔
688
    if (pIter == NULL) break;
475✔
689

690
    if (pDnode->id == dnodeId || dnodeId == -1 || dnodeId == 0) {
331!
691
      bool online = mndIsDnodeOnline(pDnode, curMs);
155✔
692
      if (!online) {
155!
693
        mWarn("dnode:%d, is offline, skip to send config req", pDnode->id);
×
694
        continue;
×
695
      }
696
      SEpSet  epSet = mndGetDnodeEpset(pDnode);
155✔
697
      int32_t bufLen = tSerializeSDCfgDnodeReq(NULL, 0, pDcfgReq);
155✔
698
      void   *pBuf = rpcMallocCont(bufLen);
155✔
699

700
      if (pBuf == NULL) {
155!
701
        sdbCancelFetch(pMnode->pSdb, pIter);
×
702
        sdbRelease(pMnode->pSdb, pDnode);
×
703
        code = TSDB_CODE_OUT_OF_MEMORY;
×
704
        return code;
3✔
705
      }
706

707
      if ((bufLen = tSerializeSDCfgDnodeReq(pBuf, bufLen, pDcfgReq)) <= 0) {
155!
708
        sdbCancelFetch(pMnode->pSdb, pIter);
×
709
        sdbRelease(pMnode->pSdb, pDnode);
×
710
        code = bufLen;
×
711
        rpcFreeCont(pBuf);
×
712
        return code;
×
713
      }
714

715
      mInfo("dnode:%d, send config req to dnode, config:%s value:%s", pDnode->id, pDcfgReq->config, pDcfgReq->value);
155!
716
      SRpcMsg rpcMsg = {.msgType = TDMT_DND_CONFIG_DNODE, .pCont = pBuf, .contLen = bufLen};
155✔
717
      SRpcMsg rpcRsp = {0};
155✔
718

719
      code = rpcSendRecvWithTimeout(pMnode->msgCb.statusRpc, &epSet, &rpcMsg, &rpcRsp, NULL, CFG_ALTER_TIMEOUT);
155✔
720
      if (code != 0) {
155!
721
        mError("failed to send config req to dnode:%d, since %s", pDnode->id, tstrerror(code));
×
722
        sdbCancelFetch(pMnode->pSdb, pIter);
×
723
        sdbRelease(pMnode->pSdb, pDnode);
×
724
        return code;
×
725
      }
726

727
      code = rpcRsp.code;
155✔
728
      if (code != 0) {
155✔
729
        mError("failed to alter config %s,on dnode:%d, since %s", pDcfgReq->config, pDnode->id, tstrerror(code));
3!
730
        sdbCancelFetch(pMnode->pSdb, pIter);
3✔
731
        sdbRelease(pMnode->pSdb, pDnode);
3✔
732
        return code;
3✔
733
      }
734
      rpcFreeCont(rpcRsp.pCont);
152✔
735
    }
736
    sdbRelease(pSdb, pDnode);
328✔
737
  }
738

739
  if (code == -1) {
144✔
740
    code = TSDB_CODE_MND_DNODE_NOT_EXIST;
4✔
741
  }
742
  TAOS_RETURN(code);
144✔
743
}
744

745
static int32_t mndProcessConfigDnodeReq(SRpcMsg *pReq) {
305✔
746
  int32_t       code = 0;
305✔
747
  int32_t       lino = -1;
305✔
748
  SMnode       *pMnode = pReq->info.node;
305✔
749
  SMCfgDnodeReq cfgReq = {0};
305✔
750
  SConfigObj   *vObj = sdbAcquire(pMnode->pSdb, SDB_CFG, "tsmmConfigVersion");
305✔
751
  if (vObj == NULL) {
305!
752
    code = TSDB_CODE_SDB_OBJ_NOT_THERE;
×
753
    mInfo("failed to acquire mnd config version, since %s", tstrerror(code));
×
754
    goto _err_out;
×
755
  }
756

757
  TAOS_CHECK_RETURN(tDeserializeSMCfgDnodeReq(pReq->pCont, pReq->contLen, &cfgReq));
305!
758
  int8_t updateIpWhiteList = 0;
305✔
759
  mInfo("dnode:%d, start to config, option:%s, value:%s", cfgReq.dnodeId, cfgReq.config, cfgReq.value);
305!
760
  if ((code = mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_CONFIG_DNODE)) != 0) {
305✔
761
    goto _err_out;
7✔
762
  }
763

764
  SDCfgDnodeReq dcfgReq = {0};
298✔
765
  if (strcasecmp(cfgReq.config, "resetlog") == 0) {
298✔
766
    tstrncpy(dcfgReq.config, "resetlog", 9);
2✔
767
    goto _send_req;
2✔
768
#ifdef TD_ENTERPRISE
769
  } else if (strncasecmp(cfgReq.config, "ssblocksize", 12) == 0) {
296!
770
    int32_t optLen = strlen("ssblocksize");
×
771
    int32_t flag = -1;
×
772
    int32_t code = mndMCfgGetValInt32(&cfgReq, optLen, &flag);
×
773
    if (code < 0) {
×
774
      goto _err_out;
×
775
    }
776

777
    if (flag > 1024 * 1024 || (flag > -1 && flag < 1024) || flag < -1) {
×
778
      mError("dnode:%d, failed to config ssblocksize since value:%d. Valid range: -1 or [1024, 1024 * 1024]",
×
779
             cfgReq.dnodeId, flag);
780
      code = TSDB_CODE_INVALID_CFG;
×
781
      goto _err_out;
×
782
    }
783

784
    tstrncpy(dcfgReq.config, "ssblocksize", 12);
×
785
    snprintf(dcfgReq.value, TSDB_DNODE_VALUE_LEN, "%d", flag);
×
786
#endif
787
  } else {
788
    TAOS_CHECK_GOTO(mndMCfg2DCfg(&cfgReq, &dcfgReq), &lino, _err_out);
296✔
789
    if (strlen(dcfgReq.config) > TSDB_DNODE_CONFIG_LEN) {
292!
790
      mError("dnode:%d, failed to config since config is too long", cfgReq.dnodeId);
×
791
      code = TSDB_CODE_INVALID_CFG;
×
792
      goto _err_out;
×
793
    }
794
    if (strncasecmp(dcfgReq.config, "enableWhiteList", strlen("enableWhiteList")) == 0) {
292✔
795
      updateIpWhiteList = 1;
4✔
796
    }
797

798
    CfgAlterType alterType = (cfgReq.dnodeId == 0 || cfgReq.dnodeId == -1) ? CFG_ALTER_ALL_DNODES : CFG_ALTER_DNODE;
292!
799
    TAOS_CHECK_GOTO(cfgCheckRangeForDynUpdate(taosGetCfg(), dcfgReq.config, dcfgReq.value, true, alterType), &lino,
292✔
800
                    _err_out);
801
  }
802
  SConfigItem *pItem = cfgGetItem(taosGetCfg(), dcfgReq.config);
193✔
803
  // Update config in sdb.
804
  if (pItem == NULL) {
193!
805
    mError("failed to find config:%s while process config dnode req", cfgReq.config);
×
806
    code = TSDB_CODE_CFG_NOT_FOUND;
×
807
    goto _err_out;
×
808
  }
809

810
  // Audit log
811
  {
812
    char obj[50] = {0};
193✔
813
    (void)tsnprintf(obj, sizeof(obj), "%d", cfgReq.dnodeId);
193✔
814
    auditRecord(pReq, pMnode->clusterId, "alterDnode", obj, "", cfgReq.sql, cfgReq.sqlLen);
193✔
815
  }
816

817
  dcfgReq.version = vObj->i32 + 1;
193✔
818

819
  if (pItem->category == CFG_CATEGORY_GLOBAL) {
193✔
820
    // Use transaction to update SDB and send to dnode atomically
821
    TAOS_CHECK_GOTO(mndConfigUpdateTransWithDnode(pMnode, dcfgReq.config, dcfgReq.value, pItem->dtype, dcfgReq.version,
48!
822
                                                  cfgReq.dnodeId, &dcfgReq),
823
                    &lino, _err_out);
824
  } else {
825
    // For local config, still use the old method (only send to dnode)
826
    goto _send_req;
145✔
827
  }
828

829
  // For global config, transaction has handled everything, go to success
830
  goto _success;
48✔
831

832
_send_req:
147✔
833
  dcfgReq.version = vObj->i32;
147✔
834
  code = mndSendCfgDnodeReq(pMnode, cfgReq.dnodeId, &dcfgReq);
147✔
835
  if (code != 0) {
147✔
836
    mError("failed to send config req to dnode:%d, since %s", cfgReq.dnodeId, tstrerror(code));
7!
837
    goto _err_out;
7✔
838
  }
839

840
_success:
140✔
841
  // dont care suss or succ;
842
  if (updateIpWhiteList) mndRefreshUserIpWhiteList(pMnode);
188✔
843
  tFreeSMCfgDnodeReq(&cfgReq);
188✔
844
  sdbRelease(pMnode->pSdb, vObj);
188✔
845
  TAOS_RETURN(code);
188✔
846

847
_err_out:
117✔
848
  mError("failed to process config dnode req, since %s", tstrerror(code));
117!
849
  tFreeSMCfgDnodeReq(&cfgReq);
117✔
850
  sdbRelease(pMnode->pSdb, vObj);
117✔
851
  TAOS_RETURN(code);
117✔
852
}
853

854
static int32_t mndProcessConfigDnodeRsp(SRpcMsg *pRsp) {
246✔
855
  mInfo("config rsp from dnode");
246!
856
  return 0;
246✔
857
}
858

859
static int32_t mndTryRebuildConfigSdbRsp(SRpcMsg *pRsp) {
689✔
860
  mInfo("rebuild config sdb rsp");
689!
861
  return 0;
689✔
862
}
863

864
// Helper function to create and commit a config object
865
static int32_t mndCreateAndCommitConfigObj(STrans *pTrans, const char *srcName, const char *cfgName, char *value,
×
866
                                           int32_t *lino) {
867
  int32_t     code = 0;
×
868
  SConfigObj *pTmp = taosMemoryMalloc(sizeof(SConfigObj));
×
869
  if (pTmp == NULL) {
×
870
    code = terrno;
×
871
    return code;
×
872
  }
873

874
  pTmp->dtype = CFG_DTYPE_INT32;
×
875
  tstrncpy(pTmp->name, cfgName, CFG_NAME_MAX_LEN);
×
876
  code = mndUpdateObj(pTmp, srcName, value);
×
877
  if (code != 0) {
×
878
    tFreeSConfigObj(pTmp);
×
879
    taosMemoryFree(pTmp);
×
880
    return code;
×
881
  }
882

883
  code = mndSetCreateConfigCommitLogs(pTrans, pTmp);
×
884
  tFreeSConfigObj(pTmp);
×
885
  taosMemoryFree(pTmp);
×
886
  return code;
×
887
}
888

889
// Helper function to handle syncTimeout related config updates
890
static int32_t mndHandleSyncTimeoutConfigs(STrans *pTrans, const char *srcName, const char *pValue, int32_t *lino) {
×
891
  int32_t code = 0;
×
892
  int32_t syncTimeout = 0;
×
893
  char    tmp[10] = {0};
×
894

895
  if (sscanf(pValue, "%d", &syncTimeout) != 1) {
×
896
    syncTimeout = 0;
×
897
  }
898

899
  int32_t baseTimeout = syncTimeout - syncTimeout / SYNC_TIMEOUT_DIVISOR;
×
900

901
  // arbSetAssignedTimeoutMs = syncTimeout
902
  sprintf(tmp, "%d", syncTimeout);
×
903
  TAOS_CHECK_GOTO(mndCreateAndCommitConfigObj(pTrans, srcName, "arbSetAssignedTimeoutMs", tmp, lino), lino, _OVER);
×
904

905
  // arbHeartBeatIntervalMs = syncTimeout / 4
906
  sprintf(tmp, "%d", syncTimeout / SYNC_TIMEOUT_DIVISOR);
×
907
  TAOS_CHECK_GOTO(mndCreateAndCommitConfigObj(pTrans, srcName, "arbHeartBeatIntervalMs", tmp, lino), lino, _OVER);
×
908

909
  // arbCheckSyncIntervalMs = syncTimeout / 4
910
  TAOS_CHECK_GOTO(mndCreateAndCommitConfigObj(pTrans, srcName, "arbCheckSyncIntervalMs", tmp, lino), lino, _OVER);
×
911

912
  // syncVnodeElectIntervalMs = (syncTimeout - syncTimeout / 4) / 2
913
  sprintf(tmp, "%d", baseTimeout / SYNC_TIMEOUT_ELECT_DIVISOR);
×
914
  TAOS_CHECK_GOTO(mndCreateAndCommitConfigObj(pTrans, srcName, "syncVnodeElectIntervalMs", tmp, lino), lino, _OVER);
×
915

916
  // syncMnodeElectIntervalMs = (syncTimeout - syncTimeout / 4) / 2
917
  TAOS_CHECK_GOTO(mndCreateAndCommitConfigObj(pTrans, srcName, "syncMnodeElectIntervalMs", tmp, lino), lino, _OVER);
×
918

919
  // statusTimeoutMs = (syncTimeout - syncTimeout / 4) / 2
920
  TAOS_CHECK_GOTO(mndCreateAndCommitConfigObj(pTrans, srcName, "statusTimeoutMs", tmp, lino), lino, _OVER);
×
921

922
  // statusSRTimeoutMs = (syncTimeout - syncTimeout / 4) / 4
923
  sprintf(tmp, "%d", baseTimeout / SYNC_TIMEOUT_SR_DIVISOR);
×
924
  TAOS_CHECK_GOTO(mndCreateAndCommitConfigObj(pTrans, srcName, "statusSRTimeoutMs", tmp, lino), lino, _OVER);
×
925

926
  // syncVnodeHeartbeatIntervalMs = (syncTimeout - syncTimeout / 4) / 8
927
  sprintf(tmp, "%d", baseTimeout / SYNC_TIMEOUT_HB_DIVISOR);
×
928
  TAOS_CHECK_GOTO(mndCreateAndCommitConfigObj(pTrans, srcName, "syncVnodeHeartbeatIntervalMs", tmp, lino), lino, _OVER);
×
929

930
  // syncMnodeHeartbeatIntervalMs = (syncTimeout - syncTimeout / 4) / 8
931
  TAOS_CHECK_GOTO(mndCreateAndCommitConfigObj(pTrans, srcName, "syncMnodeHeartbeatIntervalMs", tmp, lino), lino, _OVER);
×
932

933
  // statusIntervalMs = (syncTimeout - syncTimeout / 4) / 8
934
  TAOS_CHECK_GOTO(mndCreateAndCommitConfigObj(pTrans, srcName, "statusIntervalMs", tmp, lino), lino, _OVER);
×
935

936
_OVER:
×
937
  return code;
×
938
}
939

940
// get int32_t value from 'SMCfgDnodeReq'
941
static int32_t mndMCfgGetValInt32(SMCfgDnodeReq *pMCfgReq, int32_t optLen, int32_t *pOutValue) {
×
942
  int32_t code = 0;
×
943
  if (' ' != pMCfgReq->config[optLen] && 0 != pMCfgReq->config[optLen]) {
×
944
    goto _err;
×
945
  }
946

947
  if (' ' == pMCfgReq->config[optLen]) {
×
948
    // 'key value'
949
    if (strlen(pMCfgReq->value) != 0) goto _err;
×
950
    *pOutValue = taosStr2Int32(pMCfgReq->config + optLen + 1, NULL, 10);
×
951
  } else {
952
    // 'key' 'value'
953
    if (strlen(pMCfgReq->value) == 0) goto _err;
×
954
    *pOutValue = taosStr2Int32(pMCfgReq->value, NULL, 10);
×
955
  }
956

957
  TAOS_RETURN(code);
×
958

959
_err:
×
960
  mError(" failed to set config since:%s", tstrerror(code));
×
961
  TAOS_RETURN(code);
×
962
}
963

964
static int32_t mndConfigUpdateTrans(SMnode *pMnode, const char *name, char *pValue, ECfgDataType dtype,
×
965
                                    int32_t tsmmConfigVersion) {
966
  int32_t     code = -1;
×
967
  int32_t     lino = -1;
×
968
  SConfigObj *pVersion = taosMemoryMalloc(sizeof(SConfigObj)), *pObj = taosMemoryMalloc(sizeof(SConfigObj));
×
969
  if (pVersion == NULL || pObj == NULL) {
×
970
    code = terrno;
×
971
    goto _OVER;
×
972
  }
973
  tstrncpy(pVersion->name, "tsmmConfigVersion", CFG_NAME_MAX_LEN);
×
974
  pVersion->dtype = CFG_DTYPE_INT32;
×
975
  pVersion->i32 = tsmmConfigVersion;
×
976

977
  pObj->dtype = dtype;
×
978
  tstrncpy(pObj->name, name, CFG_NAME_MAX_LEN);
×
979

980
  TAOS_CHECK_GOTO(mndUpdateObj(pObj, name, pValue), &lino, _OVER);
×
981
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, NULL, "update-config");
×
982
  if (pTrans == NULL) {
×
983
    code = terrno;
×
984
    goto _OVER;
×
985
  }
986
  mInfo("trans:%d, used to update config:%s to value:%s", pTrans->id, name, pValue);
×
987
  TAOS_CHECK_GOTO(mndSetCreateConfigCommitLogs(pTrans, pVersion), &lino, _OVER);
×
988
  TAOS_CHECK_GOTO(mndSetCreateConfigCommitLogs(pTrans, pObj), &lino, _OVER);
×
989

990
  if (taosStrncasecmp(name, "syncTimeout", CFG_NAME_MAX_LEN) == 0) {
×
991
    TAOS_CHECK_GOTO(mndHandleSyncTimeoutConfigs(pTrans, name, pValue, &lino), &lino, _OVER);
×
992
  }
993
  if ((code = mndTransPrepare(pMnode, pTrans)) != 0) goto _OVER;
×
994
  code = 0;
×
995
_OVER:
×
996
  if (code != 0) {
×
997
    mError("failed to update config:%s to value:%s, since %s", name, pValue, tstrerror(code));
×
998
  }
999
  mndTransDrop(pTrans);
×
1000
  tFreeSConfigObj(pVersion);
×
1001
  taosMemoryFree(pVersion);
×
1002
  tFreeSConfigObj(pObj);
×
1003
  taosMemoryFree(pObj);
×
1004
  return code;
×
1005
}
1006

1007
static int32_t mndConfigUpdateTransWithDnode(SMnode *pMnode, const char *name, char *pValue, ECfgDataType dtype,
48✔
1008
                                             int32_t tsmmConfigVersion, int32_t dnodeId, SDCfgDnodeReq *pDcfgReq) {
1009
  int32_t     code = -1;
48✔
1010
  int32_t     lino = -1;
48✔
1011
  SConfigObj *pVersion = taosMemoryMalloc(sizeof(SConfigObj)), *pObj = taosMemoryMalloc(sizeof(SConfigObj));
48!
1012
  if (pVersion == NULL || pObj == NULL) {
48!
1013
    code = terrno;
×
1014
    goto _OVER;
×
1015
  }
1016
  tstrncpy(pVersion->name, "tsmmConfigVersion", CFG_NAME_MAX_LEN);
48✔
1017
  pVersion->dtype = CFG_DTYPE_INT32;
48✔
1018
  pVersion->i32 = tsmmConfigVersion;
48✔
1019

1020
  pObj->dtype = dtype;
48✔
1021
  tstrncpy(pObj->name, name, CFG_NAME_MAX_LEN);
48✔
1022

1023
  TAOS_CHECK_GOTO(mndUpdateObj(pObj, name, pValue), &lino, _OVER);
48!
1024
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_NOTHING, NULL, "update-config-with-dnode");
48✔
1025
  if (pTrans == NULL) {
48!
1026
    code = terrno;
×
1027
    goto _OVER;
×
1028
  }
1029
  mInfo("trans:%d, used to update config:%s to value:%s and send to dnode", pTrans->id, name, pValue);
48!
1030

1031
  // Add commit logs for SDB config updates
1032
  TAOS_CHECK_GOTO(mndSetCreateConfigCommitLogs(pTrans, pVersion), &lino, _OVER);
48!
1033
  TAOS_CHECK_GOTO(mndSetCreateConfigCommitLogs(pTrans, pObj), &lino, _OVER);
48!
1034

1035
  if (taosStrncasecmp(name, "syncTimeout", CFG_NAME_MAX_LEN) == 0) {
48!
1036
    TAOS_CHECK_GOTO(mndHandleSyncTimeoutConfigs(pTrans, name, pValue, &lino), &lino, _OVER);
×
1037
  }
1038

1039
  // Add redo actions to send config to dnodes
1040
  SSdb   *pSdb = pMnode->pSdb;
48✔
1041
  void   *pIter = NULL;
48✔
1042
  int64_t curMs = taosGetTimestampMs();
48✔
1043

1044
  while (1) {
54✔
1045
    SDnodeObj *pDnode = NULL;
102✔
1046
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
102✔
1047
    if (pIter == NULL) break;
102✔
1048

1049
    if (pDnode->id == dnodeId || dnodeId == -1 || dnodeId == 0) {
54!
1050
      bool online = mndIsDnodeOnline(pDnode, curMs);
54✔
1051
      if (!online) {
54!
1052
        mWarn("dnode:%d, is offline, still add to trans for retry", pDnode->id);
×
1053
      }
1054

1055
      code = mndBuildCfgDnodeRedoAction(pTrans, pDnode, pDcfgReq);
54✔
1056
      if (code != 0) {
54!
1057
        mError("failed to build config redo action for dnode:%d, since %s", pDnode->id, tstrerror(code));
×
1058
        sdbCancelFetch(pMnode->pSdb, pIter);
×
1059
        sdbRelease(pMnode->pSdb, pDnode);
×
1060
        goto _OVER;
×
1061
      }
1062
    }
1063
    sdbRelease(pSdb, pDnode);
54✔
1064
  }
1065

1066
  if ((code = mndTransPrepare(pMnode, pTrans)) != 0) goto _OVER;
48!
1067
  code = 0;
48✔
1068

1069
_OVER:
48✔
1070
  if (code != 0) {
48!
1071
    mError("failed to update config:%s to value:%s and send to dnode, since %s", name, pValue, tstrerror(code));
×
1072
  }
1073
  mndTransDrop(pTrans);
48✔
1074
  tFreeSConfigObj(pVersion);
48✔
1075
  taosMemoryFree(pVersion);
48!
1076
  tFreeSConfigObj(pObj);
48✔
1077
  taosMemoryFree(pObj);
48!
1078
  return code;
48✔
1079
}
1080

1081
static int32_t initConfigArrayFromSdb(SMnode *pMnode, SArray *array) {
1,343✔
1082
  int32_t     code = 0;
1,343✔
1083
  SSdb       *pSdb = pMnode->pSdb;
1,343✔
1084
  void       *pIter = NULL;
1,343✔
1085
  SConfigObj *obj = NULL;
1,343✔
1086

1087
  while (1) {
135,269✔
1088
    pIter = sdbFetch(pSdb, SDB_CFG, pIter, (void **)&obj);
136,612✔
1089
    if (pIter == NULL) break;
136,612✔
1090
    if (obj == NULL) {
135,269!
1091
      code = TSDB_CODE_OUT_OF_MEMORY;
×
1092
      goto _exit;
×
1093
    }
1094
    if (strcasecmp(obj->name, "tsmmConfigVersion") == 0) {
135,269✔
1095
      sdbRelease(pSdb, obj);
1,343✔
1096
      continue;
1,343✔
1097
    }
1098
    SConfigItem item = {0};
133,926✔
1099
    item.dtype = obj->dtype;
133,926✔
1100
    item.name = taosStrdup(obj->name);
133,926!
1101
    if (item.name == NULL) {
133,926!
1102
      code = terrno;
×
1103
      sdbCancelFetch(pSdb, pIter);
×
1104
      sdbRelease(pSdb, obj);
×
1105
      goto _exit;
×
1106
    }
1107
    switch (obj->dtype) {
133,926!
1108
      case CFG_DTYPE_NONE:
×
1109
        break;
×
1110
      case CFG_DTYPE_BOOL:
24,947✔
1111
        item.bval = obj->bval;
24,947✔
1112
        break;
24,947✔
1113
      case CFG_DTYPE_INT32:
74,841✔
1114
        item.i32 = obj->i32;
74,841✔
1115
        break;
74,841✔
1116
      case CFG_DTYPE_INT64:
7,878✔
1117
        item.i64 = obj->i64;
7,878✔
1118
        break;
7,878✔
1119
      case CFG_DTYPE_FLOAT:
2,626✔
1120
      case CFG_DTYPE_DOUBLE:
1121
        item.fval = obj->fval;
2,626✔
1122
        break;
2,626✔
1123
      case CFG_DTYPE_STRING:
23,634✔
1124
      case CFG_DTYPE_DIR:
1125
      case CFG_DTYPE_LOCALE:
1126
      case CFG_DTYPE_CHARSET:
1127
      case CFG_DTYPE_TIMEZONE:
1128
        item.str = taosStrdup(obj->str);
23,634!
1129
        if (item.str == NULL) {
23,634!
1130
          sdbCancelFetch(pSdb, pIter);
×
1131
          sdbRelease(pSdb, obj);
×
1132
          code = terrno;
×
1133
          goto _exit;
×
1134
        }
1135
        break;
23,634✔
1136
    }
1137
    if (taosArrayPush(array, &item) == NULL) {
133,926!
1138
      sdbCancelFetch(pSdb, pIter);
×
1139
      sdbRelease(pSdb, obj);
×
1140
      code = TSDB_CODE_OUT_OF_MEMORY;
×
1141
      goto _exit;
×
1142
      break;
1143
    }
1144
    sdbRelease(pSdb, obj);
133,926✔
1145
  }
1146
_exit:
1,343✔
1147
  if (code != 0) {
1,343!
1148
    mError("failed to init config array from sdb, since %s", tstrerror(code));
×
1149
  }
1150
  return code;
1,343✔
1151
}
1152

1153
static void cfgArrayCleanUp(SArray *array) {
1,742✔
1154
  if (array == NULL) {
1,742!
1155
    return;
×
1156
  }
1157

1158
  int32_t sz = taosArrayGetSize(array);
1,742✔
1159
  for (int32_t i = 0; i < sz; ++i) {
135,668✔
1160
    SConfigItem *item = taosArrayGet(array, i);
133,926✔
1161
    if (item->dtype == CFG_DTYPE_STRING || item->dtype == CFG_DTYPE_DIR || item->dtype == CFG_DTYPE_LOCALE ||
133,926!
1162
        item->dtype == CFG_DTYPE_CHARSET || item->dtype == CFG_DTYPE_TIMEZONE) {
112,918✔
1163
      taosMemoryFreeClear(item->str);
23,634!
1164
    }
1165
    taosMemoryFreeClear(item->name);
133,926!
1166
  }
1167

1168
  taosArrayDestroy(array);
1,742✔
1169
}
1170

1171
static void cfgObjArrayCleanUp(SArray *array) {
1,374✔
1172
  if (array == NULL) {
1,374!
1173
    return;
×
1174
  }
1175
  int32_t sz = taosArrayGetSize(array);
1,374✔
1176
  for (int32_t i = 0; i < sz; ++i) {
1,374!
1177
    SConfigObj *obj = taosArrayGet(array, i);
×
1178
    tFreeSConfigObj(obj);
×
1179
  }
1180
  taosArrayDestroy(array);
1,374✔
1181
}
1182

1183
static SArray *initVariablesFromItems(SArray *pItems, const char* likePattern) {
129✔
1184
  if (pItems == NULL) {
129!
1185
    return NULL;
×
1186
  }
1187

1188
  int32_t sz = taosArrayGetSize(pItems);
129✔
1189

1190
  SArray *pInfos = taosArrayInit(sz, sizeof(SVariablesInfo));
129✔
1191
  if (pInfos == NULL) {
129!
1192
    mError("failed to init array while init variables from items, since %s", tstrerror(terrno));
×
1193
    return NULL;
×
1194
  }
1195
  for (int32_t i = 0; i < sz; ++i) {
13,287✔
1196
    SConfigItem   *pItem = taosArrayGet(pItems, i);
13,158✔
1197
    SVariablesInfo info = {0};
13,158✔
1198
    tstrncpy(info.name, pItem->name, sizeof(info.name));
13,158✔
1199
    if (likePattern != NULL && rawStrPatternMatch(pItem->name, likePattern) != TSDB_PATTERN_MATCH) {
13,158✔
1200
      continue;
1,095✔
1201
    }
1202

1203
    // init info value
1204
    switch (pItem->dtype) {
12,063!
1205
      case CFG_DTYPE_NONE:
×
1206
        break;
×
1207
      case CFG_DTYPE_BOOL:
2,244✔
1208
        tsnprintf(info.value, sizeof(info.value), "%d", pItem->bval);
2,244✔
1209
        break;
2,244✔
1210
      case CFG_DTYPE_INT32:
6,748✔
1211
        tsnprintf(info.value, sizeof(info.value), "%d", pItem->i32);
6,748✔
1212
        break;
6,748✔
1213
      case CFG_DTYPE_INT64:
708✔
1214
        tsnprintf(info.value, sizeof(info.value), "%" PRId64, pItem->i64);
708✔
1215
        break;
708✔
1216
      case CFG_DTYPE_FLOAT:
236✔
1217
      case CFG_DTYPE_DOUBLE:
1218
        tsnprintf(info.value, sizeof(info.value), "%f", pItem->fval);
236✔
1219
        break;
236✔
1220
      case CFG_DTYPE_STRING:
2,127✔
1221
      case CFG_DTYPE_DIR:
1222
      case CFG_DTYPE_LOCALE:
1223
      case CFG_DTYPE_CHARSET:
1224
      case CFG_DTYPE_TIMEZONE:
1225
        tsnprintf(info.value, sizeof(info.value), "%s", pItem->str);
2,127✔
1226
        break;
2,127✔
1227
    }
1228

1229
    // init info scope
1230
    switch (pItem->scope) {
12,063!
1231
      case CFG_SCOPE_SERVER:
9,584✔
1232
        tstrncpy(info.scope, "server", sizeof(info.scope));
9,584✔
1233
        break;
9,584✔
1234
      case CFG_SCOPE_CLIENT:
×
1235
        tstrncpy(info.scope, "client", sizeof(info.scope));
×
1236
        break;
×
1237
      case CFG_SCOPE_BOTH:
2,479✔
1238
        tstrncpy(info.scope, "both", sizeof(info.scope));
2,479✔
1239
        break;
2,479✔
1240
      default:
×
1241
        tstrncpy(info.scope, "unknown", sizeof(info.scope));
×
1242
        break;
×
1243
    }
1244
    // init info category
1245
    switch (pItem->category) {
12,063!
1246
      case CFG_CATEGORY_GLOBAL:
12,063✔
1247
        tstrncpy(info.category, "global", sizeof(info.category));
12,063✔
1248
        break;
12,063✔
1249
      case CFG_CATEGORY_LOCAL:
×
1250
        tstrncpy(info.category, "local", sizeof(info.category));
×
1251
        break;
×
1252
      default:
×
1253
        tstrncpy(info.category, "unknown", sizeof(info.category));
×
1254
        break;
×
1255
    }
1256
    if (NULL == taosArrayPush(pInfos, &info)) {
12,063!
1257
      mError("failed to push info to array while init variables from items,since %s", tstrerror(terrno));
×
1258
      taosArrayDestroy(pInfos);
×
1259
      return NULL;
×
1260
    }
1261
  }
1262

1263
  return pInfos;
129✔
1264
}
1265

1266
static int32_t mndProcessShowVariablesReq(SRpcMsg *pReq) {
129✔
1267
  SShowVariablesRsp rsp = {0};
129✔
1268
  int32_t           code = TSDB_CODE_SUCCESS;
129✔
1269
  SShowVariablesReq req = {0};
129✔
1270
  SArray           *array = NULL;
129✔
1271

1272
  code = tDeserializeSShowVariablesReq(pReq->pCont, pReq->contLen, &req);
129✔
1273
  if (code != 0) {
129!
1274
    mError("failed to deserialize config req, since %s", terrstr());
×
1275
    goto _OVER;
×
1276
  }
1277

1278
  if ((code = mndCheckOperPrivilege(pReq->info.node, pReq->info.conn.user, MND_OPER_SHOW_VARIABLES)) != 0) {
129!
1279
    goto _OVER;
×
1280
  }
1281

1282
  SVariablesInfo info = {0};
129✔
1283
  char          *likePattern = req.opType == OP_TYPE_LIKE ? req.val : NULL;
129✔
1284
  rsp.variables = initVariablesFromItems(taosGetGlobalCfg(tsCfg), likePattern);
129✔
1285
  if (rsp.variables == NULL) {
129!
1286
    code = terrno;
×
1287
    goto _OVER;
×
1288
  }
1289
  int32_t rspLen = tSerializeSShowVariablesRsp(NULL, 0, &rsp);
129✔
1290
  void   *pRsp = rpcMallocCont(rspLen);
129✔
1291
  if (pRsp == NULL) {
129!
1292
    code = terrno;
×
1293
    goto _OVER;
×
1294
  }
1295

1296
  if ((rspLen = tSerializeSShowVariablesRsp(pRsp, rspLen, &rsp)) <= 0) {
129!
1297
    rpcFreeCont(pRsp);
×
1298
    code = rspLen;
×
1299
    goto _OVER;
×
1300
  }
1301

1302
  pReq->info.rspLen = rspLen;
129✔
1303
  pReq->info.rsp = pRsp;
129✔
1304
  code = 0;
129✔
1305

1306
_OVER:
129✔
1307

1308
  if (code != 0) {
129!
1309
    mError("failed to get show variables info since %s", tstrerror(code));
×
1310
  }
1311
  tFreeSShowVariablesReq(&req);
129✔
1312
  tFreeSShowVariablesRsp(&rsp);
129✔
1313
  TAOS_RETURN(code);
129✔
1314
}
1315

1316
int32_t compareSConfigItem(const SConfigObj *item1, SConfigItem *item2, bool *compare) {
×
1317
  *compare = true;
×
1318
  switch (item1->dtype) {
×
1319
    case CFG_DTYPE_BOOL:
×
1320
      if (item1->bval != item2->bval) {
×
1321
        item2->bval = item1->bval;
×
1322
        *compare = false;
×
1323
      }
1324
      break;
×
1325
    case CFG_DTYPE_FLOAT:
×
1326
      if (item1->fval != item2->fval) {
×
1327
        item2->fval = item1->fval;
×
1328
        *compare = false;
×
1329
      }
1330
      break;
×
1331
    case CFG_DTYPE_INT32:
×
1332
      if (item1->i32 != item2->i32) {
×
1333
        item2->i32 = item1->i32;
×
1334
        *compare = false;
×
1335
      }
1336
      break;
×
1337
    case CFG_DTYPE_INT64:
×
1338
      if (item1->i64 != item2->i64) {
×
1339
        item2->i64 = item1->i64;
×
1340
        *compare = false;
×
1341
      }
1342
      break;
×
1343
    case CFG_DTYPE_STRING:
×
1344
    case CFG_DTYPE_DIR:
1345
    case CFG_DTYPE_LOCALE:
1346
    case CFG_DTYPE_CHARSET:
1347
    case CFG_DTYPE_TIMEZONE:
1348
      if (strcmp(item1->str, item2->str) != 0) {
×
1349
        item2->str = taosStrdup(item1->str);
×
1350
        if (item2->str == NULL) {
×
1351
          return TSDB_CODE_OUT_OF_MEMORY;
×
1352
        }
1353
        *compare = false;
×
1354
      }
1355
      break;
×
1356
    default:
×
1357
      *compare = false;
×
1358
      return TSDB_CODE_INVALID_CFG;
×
1359
  }
1360
  return TSDB_CODE_SUCCESS;
×
1361
}
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