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

taosdata / TDengine / #3599

08 Feb 2025 11:23AM UTC coverage: 1.77% (-61.6%) from 63.396%
#3599

push

travis-ci

web-flow
Merge pull request #29712 from taosdata/fix/TD-33652-3.0

fix: reduce write rows from 30w to 3w

3776 of 278949 branches covered (1.35%)

Branch coverage included in aggregate %.

6012 of 274147 relevant lines covered (2.19%)

1642.73 hits per line

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

0.0
/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

27
#define CFG_VER_NUMBER    1
28
#define CFG_RESERVE_SIZE  63
29
#define CFG_ALTER_TIMEOUT 3 * 1000
30

31
static int32_t mndMCfgGetValInt32(SMCfgDnodeReq *pInMCfgReq, int32_t optLen, int32_t *pOutValue);
32
static int32_t mndProcessShowVariablesReq(SRpcMsg *pReq);
33
static int32_t mndProcessConfigDnodeReq(SRpcMsg *pReq);
34
static int32_t mndProcessConfigDnodeRsp(SRpcMsg *pRsp);
35
static int32_t mndProcessConfigReq(SRpcMsg *pReq);
36
static int32_t mndInitWriteCfg(SMnode *pMnode);
37
static int32_t mndSendRebuildReq(SMnode *pMnode);
38
static int32_t mndTryRebuildConfigSdbRsp(SRpcMsg *pRsp);
39
static int32_t initConfigArrayFromSdb(SMnode *pMnode, SArray *array);
40
static int32_t mndTryRebuildConfigSdb(SRpcMsg *pReq);
41
static void    cfgArrayCleanUp(SArray *array);
42
static void    cfgObjArrayCleanUp(SArray *array);
43

44
static int32_t mndConfigUpdateTrans(SMnode *pMnode, const char *name, char *pValue, ECfgDataType dtype,
45
                                    int32_t tsmmConfigVersion);
46

47
int32_t mndSetCreateConfigCommitLogs(STrans *pTrans, SConfigObj *obj);
48

49
int32_t mndInitConfig(SMnode *pMnode) {
×
50
  int32_t   code = 0;
×
51
  SSdbTable table = {.sdbType = SDB_CFG,
×
52
                     .keyType = SDB_KEY_BINARY,
53
                     .encodeFp = (SdbEncodeFp)mnCfgActionEncode,
54
                     .decodeFp = (SdbDecodeFp)mndCfgActionDecode,
55
                     .insertFp = (SdbInsertFp)mndCfgActionInsert,
56
                     .updateFp = (SdbUpdateFp)mndCfgActionUpdate,
57
                     .deleteFp = (SdbDeleteFp)mndCfgActionDelete,
58
                     .deployFp = (SdbDeployFp)mndCfgActionDeploy,
59
                     .afterRestoredFp = (SdbAfterRestoredFp)mndCfgActionAfterRestored};
60

61
  mndSetMsgHandle(pMnode, TDMT_MND_CONFIG, mndProcessConfigReq);
×
62
  mndSetMsgHandle(pMnode, TDMT_MND_CONFIG_DNODE, mndProcessConfigDnodeReq);
×
63
  mndSetMsgHandle(pMnode, TDMT_DND_CONFIG_DNODE_RSP, mndProcessConfigDnodeRsp);
×
64
  mndSetMsgHandle(pMnode, TDMT_MND_SHOW_VARIABLES, mndProcessShowVariablesReq);
×
65
  mndSetMsgHandle(pMnode, TDMT_MND_CONFIG_SDB, mndTryRebuildConfigSdb);
×
66
  mndSetMsgHandle(pMnode, TDMT_MND_CONFIG_SDB_RSP, mndTryRebuildConfigSdbRsp);
×
67

68
  return sdbSetTable(pMnode->pSdb, table);
×
69
}
70

71
SSdbRaw *mnCfgActionEncode(SConfigObj *obj) {
×
72
  int32_t  code = 0;
×
73
  int32_t  lino = 0;
×
74
  void    *buf = NULL;
×
75
  SSdbRaw *pRaw = NULL;
×
76

77
  SEncoder encoder;
78
  tEncoderInit(&encoder, NULL, 0);
×
79
  if ((code = tEncodeSConfigObj(&encoder, obj)) < 0) {
×
80
    tEncoderClear(&encoder);
×
81
    TSDB_CHECK_CODE(code, lino, _over);
×
82
  }
83

84
  int32_t tlen = encoder.pos;
×
85
  tEncoderClear(&encoder);
×
86

87
  int32_t size = sizeof(int32_t) + tlen;
×
88
  pRaw = sdbAllocRaw(SDB_CFG, CFG_VER_NUMBER, size);
×
89
  TSDB_CHECK_NULL(pRaw, code, lino, _over, terrno);
×
90

91
  buf = taosMemoryMalloc(tlen);
×
92
  TSDB_CHECK_NULL(buf, code, lino, _over, terrno);
×
93

94
  tEncoderInit(&encoder, buf, tlen);
×
95
  if ((code = tEncodeSConfigObj(&encoder, obj)) < 0) {
×
96
    tEncoderClear(&encoder);
×
97
    TSDB_CHECK_CODE(code, lino, _over);
×
98
  }
99

100
  tEncoderClear(&encoder);
×
101

102
  int32_t dataPos = 0;
×
103
  SDB_SET_INT32(pRaw, dataPos, tlen, _over);
×
104
  SDB_SET_BINARY(pRaw, dataPos, buf, tlen, _over);
×
105
  SDB_SET_DATALEN(pRaw, dataPos, _over);
×
106

107
_over:
×
108
  taosMemoryFreeClear(buf);
×
109
  if (code != TSDB_CODE_SUCCESS) {
×
110
    mError("cfg:%s, failed to encode to raw:%p at line:%d since %s", obj->name, pRaw, lino, tstrerror(code));
×
111
    sdbFreeRaw(pRaw);
×
112
    terrno = code;
×
113
    return NULL;
×
114
  }
115

116
  terrno = 0;
×
117
  mTrace("cfg:%s, encode to raw:%p, row:%p", obj->name, pRaw, obj);
×
118
  return pRaw;
×
119
}
120

121
SSdbRow *mndCfgActionDecode(SSdbRaw *pRaw) {
×
122
  int32_t     code = 0;
×
123
  int32_t     lino = 0;
×
124
  SSdbRow    *pRow = NULL;
×
125
  SConfigObj *pObj = NULL;
×
126
  void       *buf = NULL;
×
127
  int8_t      sver = 0;
×
128
  int32_t     tlen;
129
  int32_t     dataPos = 0;
×
130

131
  code = sdbGetRawSoftVer(pRaw, &sver);
×
132
  TSDB_CHECK_CODE(code, lino, _over);
×
133

134
  if (sver != CFG_VER_NUMBER) {
×
135
    terrno = TSDB_CODE_SDB_INVALID_DATA_VER;
×
136
    goto _over;
×
137
  }
138

139
  pRow = sdbAllocRow(sizeof(SConfigObj));
×
140
  TSDB_CHECK_NULL(pRow, code, lino, _over, terrno);
×
141

142
  pObj = sdbGetRowObj(pRow);
×
143
  TSDB_CHECK_NULL(pObj, code, lino, _over, terrno);
×
144

145
  SDB_GET_INT32(pRaw, dataPos, &tlen, _over);
×
146

147
  buf = taosMemoryMalloc(tlen + 1);
×
148
  TSDB_CHECK_NULL(buf, code, lino, _over, terrno);
×
149

150
  SDB_GET_BINARY(pRaw, dataPos, buf, tlen, _over);
×
151

152
  SDecoder decoder;
153
  tDecoderInit(&decoder, buf, tlen + 1);
×
154
  code = tDecodeSConfigObj(&decoder, pObj);
×
155
  tDecoderClear(&decoder);
×
156

157
  if (code < 0) {
×
158
    tFreeSConfigObj(pObj);
×
159
  }
160

161
_over:
×
162
  taosMemoryFreeClear(buf);
×
163

164
  if (code != TSDB_CODE_SUCCESS) {
×
165
    mError("cfg:%s, failed to decode from raw:%p since %s at:%d", pObj->name, pRaw, tstrerror(code), lino);
×
166
    taosMemoryFreeClear(pRow);
×
167
    terrno = code;
×
168
    return NULL;
×
169
  } else {
170
    mTrace("config:%s, decode from raw:%p, row:%p", pObj->name, pRaw, pObj);
×
171
    terrno = 0;
×
172
    return pRow;
×
173
  }
174
}
175

176
static int32_t mndCfgActionInsert(SSdb *pSdb, SConfigObj *obj) {
×
177
  mTrace("cfg:%s, perform insert action, row:%p", obj->name, obj);
×
178
  return 0;
×
179
}
180

181
static int32_t mndCfgActionDelete(SSdb *pSdb, SConfigObj *obj) {
×
182
  mTrace("cfg:%s, perform delete action, row:%p", obj->name, obj);
×
183
  tFreeSConfigObj(obj);
×
184
  return 0;
×
185
}
186

187
static int32_t mndCfgActionUpdate(SSdb *pSdb, SConfigObj *pOld, SConfigObj *pNew) {
×
188
  mTrace("cfg:%s, perform update action, old row:%p new row:%p", pOld->name, pOld, pNew);
×
189
  switch (pNew->dtype) {
×
190
    case CFG_DTYPE_NONE:
×
191
      break;
×
192
    case CFG_DTYPE_BOOL:
×
193
      pOld->bval = pNew->bval;
×
194
      break;
×
195
    case CFG_DTYPE_INT32:
×
196
      pOld->i32 = pNew->i32;
×
197
      break;
×
198
    case CFG_DTYPE_INT64:
×
199
      pOld->i64 = pNew->i64;
×
200
      break;
×
201
    case CFG_DTYPE_FLOAT:
×
202
    case CFG_DTYPE_DOUBLE:
203
      pOld->fval = pNew->fval;
×
204
      break;
×
205
    case CFG_DTYPE_STRING:
×
206
    case CFG_DTYPE_DIR:
207
    case CFG_DTYPE_LOCALE:
208
    case CFG_DTYPE_CHARSET:
209
    case CFG_DTYPE_TIMEZONE:
210
      taosMemoryFree(pOld->str);
×
211
      pOld->str = taosStrdup(pNew->str);
×
212
      if (pOld->str == NULL) {
×
213
        return terrno;
×
214
      }
215
      break;
×
216
  }
217
  return TSDB_CODE_SUCCESS;
×
218
}
219

220
static int32_t mndCfgActionDeploy(SMnode *pMnode) { return mndInitWriteCfg(pMnode); }
×
221

222
static int32_t mndCfgActionAfterRestored(SMnode *pMnode) { return mndSendRebuildReq(pMnode); }
×
223

224
static int32_t mndProcessConfigReq(SRpcMsg *pReq) {
×
225
  SMnode    *pMnode = pReq->info.node;
×
226
  SConfigReq configReq = {0};
×
227
  int32_t    code = TSDB_CODE_SUCCESS;
×
228
  SArray    *array = NULL;
×
229

230
  code = tDeserializeSConfigReq(pReq->pCont, pReq->contLen, &configReq);
×
231
  if (code != 0) {
×
232
    mError("failed to deserialize config req, since %s", terrstr());
×
233
    goto _OVER;
×
234
  }
235

236
  SConfigObj *vObj = sdbAcquire(pMnode->pSdb, SDB_CFG, "tsmmConfigVersion");
×
237
  if (vObj == NULL) {
×
238
    mInfo("failed to acquire mnd config version, since %s", terrstr());
×
239
    goto _OVER;
×
240
  }
241

242
  array = taosArrayInit(16, sizeof(SConfigItem));
×
243
  if (array == NULL) {
×
244
    code = TSDB_CODE_OUT_OF_MEMORY;
×
245
    goto _OVER;
×
246
  }
247
  SConfigRsp configRsp = {0};
×
248
  configRsp.forceReadConfig = configReq.forceReadConfig;
×
249

250
  configRsp.cver = vObj->i32;
×
251
  if (configRsp.forceReadConfig) {
×
252
    // compare config array from configReq with current config array
253
    if (compareSConfigItemArrays(taosGetGlobalCfg(tsCfg), configReq.array, array)) {
×
254
      configRsp.array = array;
×
255
    } else {
256
      configRsp.isConifgVerified = 1;
×
257
    }
258
  } else {
259
    if (configReq.cver == vObj->i32) {
×
260
      configRsp.isVersionVerified = 1;
×
261
    } else {
262
      code = initConfigArrayFromSdb(pMnode, array);
×
263
      if (code != 0) {
×
264
        mError("failed to init config array from sdb, since %s", terrstr());
×
265
        goto _OVER;
×
266
      }
267
      configRsp.array = array;
×
268
    }
269
  }
270

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

290
_OVER:
×
291
  if (code != 0) {
×
292
    mError("failed to process config req, since %s", tstrerror(code));
×
293
  }
294
  sdbRelease(pMnode->pSdb, vObj);
×
295
  cfgArrayCleanUp(array);
×
296
  return TSDB_CODE_SUCCESS;
×
297
}
298

299
int32_t mndInitWriteCfg(SMnode *pMnode) {
×
300
  int    code = 0;
×
301
  size_t sz = 0;
×
302

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

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

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

335
_OVER:
×
336
  if (code != 0) {
×
337
    mError("failed to init write cfg, since %s", tstrerror(code));
×
338
  }
339
  mndTransDrop(pTrans);
×
340
  return code;
×
341
}
342

343
int32_t mndSendRebuildReq(SMnode *pMnode) {
×
344
  int32_t code = 0;
×
345

346
  SRpcMsg rpcMsg = {.pCont = NULL,
×
347
                    .contLen = 0,
348
                    .msgType = TDMT_MND_CONFIG_SDB,
349
                    .info.ahandle = 0,
350
                    .info.notFreeAhandle = 1,
351
                    .info.refId = 0,
352
                    .info.noResp = 0,
353
                    .info.handle = 0};
354
  SEpSet  epSet = {0};
×
355

356
  mndGetMnodeEpSet(pMnode, &epSet);
×
357

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

365
static int32_t mndTryRebuildConfigSdb(SRpcMsg *pReq) {
×
366
  SMnode *pMnode = pReq->info.node;
×
367
  if (!mndIsLeader(pMnode)) {
×
368
    return TSDB_CODE_SUCCESS;
×
369
  }
370
  int32_t     code = 0;
×
371
  int32_t     sz = -1;
×
372
  STrans     *pTrans = NULL;
×
373
  SConfigObj *vObj = NULL;
×
374
  SArray     *addArray = NULL;
×
375

376
  vObj = sdbAcquire(pMnode->pSdb, SDB_CFG, "tsmmConfigVersion");
×
377
  if (vObj == NULL) {
×
378
    if ((code = mndInitWriteCfg(pMnode)) < 0) goto _exit;
×
379
    mInfo("failed to acquire mnd config version, try to rebuild config in sdb.");
×
380
  } else {
381
    sz = taosArrayGetSize(taosGetGlobalCfg(tsCfg));
×
382
    addArray = taosArrayInit(4, sizeof(SConfigObj));
×
383
    for (int i = 0; i < sz; ++i) {
×
384
      SConfigItem *item = taosArrayGet(taosGetGlobalCfg(tsCfg), i);
×
385
      SConfigObj  *obj = sdbAcquire(pMnode->pSdb, SDB_CFG, item->name);
×
386
      if (obj == NULL) {
×
387
        mInfo("config:%s, not exist in sdb, try to add it", item->name);
×
388
        SConfigObj newObj;
389
        if ((code = mndInitConfigObj(item, &newObj)) != 0) goto _exit;
×
390
        if (NULL == taosArrayPush(addArray, &newObj)) {
×
391
          code = terrno;
×
392
          goto _exit;
×
393
        }
394
      } else {
395
        sdbRelease(pMnode->pSdb, obj);
×
396
      }
397
    }
398
    int32_t addSize = taosArrayGetSize(addArray);
×
399
    if (addSize > 0) {
×
400
      pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_NOTHING, NULL, "add-config");
×
401
      if (pTrans == NULL) {
×
402
        code = terrno;
×
403
        goto _exit;
×
404
      }
405
      for (int i = 0; i < addSize; ++i) {
×
406
        SConfigObj *AddObj = taosArrayGet(addArray, i);
×
407
        if ((code = mndSetCreateConfigCommitLogs(pTrans, AddObj)) != 0) goto _exit;
×
408
      }
409
      if ((code = mndTransPrepare(pMnode, pTrans)) != 0) goto _exit;
×
410
      mInfo("add new config to sdb, nums:%d", addSize);
×
411
    }
412
  }
413
_exit:
×
414
  if (code != 0) {
×
415
    mError("failed to try rebuild config in sdb, since %s", tstrerror(code));
×
416
  }
417
  sdbRelease(pMnode->pSdb, vObj);
×
418
  cfgObjArrayCleanUp(addArray);
×
419
  mndTransDrop(pTrans);
×
420
  TAOS_RETURN(code);
×
421
}
422

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

435
static int32_t mndMCfg2DCfg(SMCfgDnodeReq *pMCfgReq, SDCfgDnodeReq *pDCfgReq) {
×
436
  int32_t code = 0;
×
437
  char   *p = pMCfgReq->config;
×
438
  while (*p) {
×
439
    if (*p == ' ') {
×
440
      break;
×
441
    }
442
    p++;
×
443
  }
444

445
  size_t optLen = p - pMCfgReq->config;
×
446
  tstrncpy(pDCfgReq->config, pMCfgReq->config, sizeof(pDCfgReq->config));
×
447
  pDCfgReq->config[optLen] = 0;
×
448

449
  if (' ' == pMCfgReq->config[optLen]) {
×
450
    // 'key value'
451
    if (strlen(pMCfgReq->value) != 0) goto _err;
×
452
    tstrncpy(pDCfgReq->value, p + 1, sizeof(pDCfgReq->value));
×
453
  } else {
454
    // 'key' 'value'
455
    if (strlen(pMCfgReq->value) == 0) goto _err;
×
456
    tstrncpy(pDCfgReq->value, pMCfgReq->value, sizeof(pDCfgReq->value));
×
457
  }
458

459
  TAOS_RETURN(code);
×
460

461
_err:
×
462
  mError("dnode:%d, failed to config since invalid conf:%s", pMCfgReq->dnodeId, pMCfgReq->config);
×
463
  code = TSDB_CODE_INVALID_CFG;
×
464
  TAOS_RETURN(code);
×
465
}
466

467
static int32_t mndSendCfgDnodeReq(SMnode *pMnode, int32_t dnodeId, SDCfgDnodeReq *pDcfgReq) {
×
468
  int32_t code = -1;
×
469
  SSdb   *pSdb = pMnode->pSdb;
×
470
  void   *pIter = NULL;
×
471

472
  int64_t curMs = taosGetTimestampMs();
×
473

474
  while (1) {
×
475
    SDnodeObj *pDnode = NULL;
×
476
    pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pDnode);
×
477
    if (pIter == NULL) break;
×
478

479
    if (pDnode->id == dnodeId || dnodeId == -1 || dnodeId == 0) {
×
480
      bool online = mndIsDnodeOnline(pDnode, curMs);
×
481
      if (!online) {
×
482
        mWarn("dnode:%d, is offline, skip to send config req", pDnode->id);
×
483
        continue;
×
484
      }
485
      SEpSet  epSet = mndGetDnodeEpset(pDnode);
×
486
      int32_t bufLen = tSerializeSDCfgDnodeReq(NULL, 0, pDcfgReq);
×
487
      void   *pBuf = rpcMallocCont(bufLen);
×
488

489
      if (pBuf == NULL) {
×
490
        sdbCancelFetch(pMnode->pSdb, pIter);
×
491
        sdbRelease(pMnode->pSdb, pDnode);
×
492
        code = TSDB_CODE_OUT_OF_MEMORY;
×
493
        return code;
×
494
      }
495

496
      if ((bufLen = tSerializeSDCfgDnodeReq(pBuf, bufLen, pDcfgReq)) <= 0) {
×
497
        sdbCancelFetch(pMnode->pSdb, pIter);
×
498
        sdbRelease(pMnode->pSdb, pDnode);
×
499
        code = bufLen;
×
500
        rpcFreeCont(pBuf);
×
501
        return code;
×
502
      }
503

504
      mInfo("dnode:%d, send config req to dnode, config:%s value:%s", pDnode->id, pDcfgReq->config, pDcfgReq->value);
×
505
      SRpcMsg rpcMsg = {.msgType = TDMT_DND_CONFIG_DNODE, .pCont = pBuf, .contLen = bufLen};
×
506
      SRpcMsg rpcRsp = {0};
×
507

508
      code = rpcSendRecvWithTimeout(pMnode->msgCb.statusRpc, &epSet, &rpcMsg, &rpcRsp, NULL, CFG_ALTER_TIMEOUT);
×
509
      if (code != 0) {
×
510
        mError("failed to send config req to dnode:%d, since %s", pDnode->id, tstrerror(code));
×
511
        sdbCancelFetch(pMnode->pSdb, pIter);
×
512
        sdbRelease(pMnode->pSdb, pDnode);
×
513
        return code;
×
514
      }
515

516
      code = rpcRsp.code;
×
517
      if (code != 0) {
×
518
        mError("failed to alter config %s,on dnode:%d, since %s", pDcfgReq->config, pDnode->id, tstrerror(code));
×
519
        sdbCancelFetch(pMnode->pSdb, pIter);
×
520
        sdbRelease(pMnode->pSdb, pDnode);
×
521
        return code;
×
522
      }
523
      rpcFreeCont(rpcRsp.pCont);
×
524
    }
525
    sdbRelease(pSdb, pDnode);
×
526
  }
527

528
  if (code == -1) {
×
529
    code = TSDB_CODE_MND_DNODE_NOT_EXIST;
×
530
  }
531
  TAOS_RETURN(code);
×
532
}
533

534
static int32_t mndProcessConfigDnodeReq(SRpcMsg *pReq) {
×
535
  int32_t       code = 0;
×
536
  int32_t       lino = -1;
×
537
  SMnode       *pMnode = pReq->info.node;
×
538
  SMCfgDnodeReq cfgReq = {0};
×
539
  SConfigObj   *vObj = sdbAcquire(pMnode->pSdb, SDB_CFG, "tsmmConfigVersion");
×
540
  if (vObj == NULL) {
×
541
    code = TSDB_CODE_SDB_OBJ_NOT_THERE;
×
542
    mInfo("failed to acquire mnd config version, since %s", tstrerror(code));
×
543
    goto _err_out;
×
544
  }
545

546
  TAOS_CHECK_RETURN(tDeserializeSMCfgDnodeReq(pReq->pCont, pReq->contLen, &cfgReq));
×
547
  int8_t updateIpWhiteList = 0;
×
548
  mInfo("dnode:%d, start to config, option:%s, value:%s", cfgReq.dnodeId, cfgReq.config, cfgReq.value);
×
549
  if ((code = mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_CONFIG_DNODE)) != 0) {
×
550
    goto _err_out;
×
551
  }
552

553
  SDCfgDnodeReq dcfgReq = {0};
×
554
  if (strcasecmp(cfgReq.config, "resetlog") == 0) {
×
555
    tstrncpy(dcfgReq.config, "resetlog", 9);
×
556
    goto _send_req;
×
557
#ifdef TD_ENTERPRISE
558
  } else if (strncasecmp(cfgReq.config, "s3blocksize", 12) == 0) {
×
559
    int32_t optLen = strlen("s3blocksize");
×
560
    int32_t flag = -1;
×
561
    int32_t code = mndMCfgGetValInt32(&cfgReq, optLen, &flag);
×
562
    if (code < 0) {
×
563
      goto _err_out;
×
564
    }
565

566
    if (flag > 1024 * 1024 || (flag > -1 && flag < 1024) || flag < -1) {
×
567
      mError("dnode:%d, failed to config s3blocksize since value:%d. Valid range: -1 or [1024, 1024 * 1024]",
×
568
             cfgReq.dnodeId, flag);
569
      code = TSDB_CODE_INVALID_CFG;
×
570
      goto _err_out;
×
571
    }
572

573
    tstrncpy(dcfgReq.config, "s3blocksize", 12);
×
574
    snprintf(dcfgReq.value, TSDB_DNODE_VALUE_LEN, "%d", flag);
×
575
#endif
576
  } else {
577
    TAOS_CHECK_GOTO(mndMCfg2DCfg(&cfgReq, &dcfgReq), &lino, _err_out);
×
578
    if (strlen(dcfgReq.config) > TSDB_DNODE_CONFIG_LEN) {
×
579
      mError("dnode:%d, failed to config since config is too long", cfgReq.dnodeId);
×
580
      code = TSDB_CODE_INVALID_CFG;
×
581
      goto _err_out;
×
582
    }
583
    if (strncasecmp(dcfgReq.config, "enableWhiteList", strlen("enableWhiteList")) == 0) {
×
584
      updateIpWhiteList = 1;
×
585
    }
586

587
    CfgAlterType alterType = (cfgReq.dnodeId == 0 || cfgReq.dnodeId == -1) ? CFG_ALTER_ALL_DNODES : CFG_ALTER_DNODE;
×
588
    TAOS_CHECK_GOTO(cfgCheckRangeForDynUpdate(taosGetCfg(), dcfgReq.config, dcfgReq.value, true, alterType), &lino,
×
589
                    _err_out);
590
  }
591
  SConfigItem *pItem = cfgGetItem(taosGetCfg(), dcfgReq.config);
×
592
  // Update config in sdb.
593
  if (pItem == NULL) {
×
594
    mError("failed to find config:%s while process config dnode req", cfgReq.config);
×
595
    code = TSDB_CODE_CFG_NOT_FOUND;
×
596
    goto _err_out;
×
597
  }
598
  if (pItem->category == CFG_CATEGORY_GLOBAL) {
×
599
    TAOS_CHECK_GOTO(mndConfigUpdateTrans(pMnode, dcfgReq.config, dcfgReq.value, pItem->dtype, ++vObj->i32), &lino,
×
600
                    _err_out);
601
  }
602
_send_req :
×
603

604
{  // audit
605
  char obj[50] = {0};
×
606
  (void)tsnprintf(obj, sizeof(obj), "%d", cfgReq.dnodeId);
×
607

608
  auditRecord(pReq, pMnode->clusterId, "alterDnode", obj, "", cfgReq.sql, cfgReq.sqlLen);
×
609
}
610
  dcfgReq.version = vObj->i32;
×
611
  code = mndSendCfgDnodeReq(pMnode, cfgReq.dnodeId, &dcfgReq);
×
612
  if (code != 0) {
×
613
    mError("failed to send config req to dnode:%d, since %s", cfgReq.dnodeId, tstrerror(code));
×
614
    goto _err_out;
×
615
  }
616
  // dont care suss or succ;
617
  if (updateIpWhiteList) mndRefreshUserIpWhiteList(pMnode);
×
618
  tFreeSMCfgDnodeReq(&cfgReq);
×
619
  sdbRelease(pMnode->pSdb, vObj);
×
620
  TAOS_RETURN(code);
×
621

622
_err_out:
×
623
  mError("failed to process config dnode req, since %s", tstrerror(code));
×
624
  tFreeSMCfgDnodeReq(&cfgReq);
×
625
  sdbRelease(pMnode->pSdb, vObj);
×
626
  TAOS_RETURN(code);
×
627
}
628

629
static int32_t mndProcessConfigDnodeRsp(SRpcMsg *pRsp) {
×
630
  mInfo("config rsp from dnode");
×
631
  return 0;
×
632
}
633

634
static int32_t mndTryRebuildConfigSdbRsp(SRpcMsg *pRsp) {
×
635
  mInfo("rebuild config sdb rsp");
×
636
  return 0;
×
637
}
638

639
// get int32_t value from 'SMCfgDnodeReq'
640
static int32_t mndMCfgGetValInt32(SMCfgDnodeReq *pMCfgReq, int32_t optLen, int32_t *pOutValue) {
×
641
  int32_t code = 0;
×
642
  if (' ' != pMCfgReq->config[optLen] && 0 != pMCfgReq->config[optLen]) {
×
643
    goto _err;
×
644
  }
645

646
  if (' ' == pMCfgReq->config[optLen]) {
×
647
    // 'key value'
648
    if (strlen(pMCfgReq->value) != 0) goto _err;
×
649
    *pOutValue = taosStr2Int32(pMCfgReq->config + optLen + 1, NULL, 10);
×
650
  } else {
651
    // 'key' 'value'
652
    if (strlen(pMCfgReq->value) == 0) goto _err;
×
653
    *pOutValue = taosStr2Int32(pMCfgReq->value, NULL, 10);
×
654
  }
655

656
  TAOS_RETURN(code);
×
657

658
_err:
×
659
  mError(" failed to set config since:%s", tstrerror(code));
×
660
  TAOS_RETURN(code);
×
661
}
662

663
static int32_t mndConfigUpdateTrans(SMnode *pMnode, const char *name, char *pValue, ECfgDataType dtype,
×
664
                                    int32_t tsmmConfigVersion) {
665
  int32_t     code = -1;
×
666
  int32_t     lino = -1;
×
667
  SConfigObj *pVersion = taosMemoryMalloc(sizeof(SConfigObj)), *pObj = taosMemoryMalloc(sizeof(SConfigObj));
×
668
  if (pVersion == NULL || pObj == NULL) {
×
669
    code = terrno;
×
670
    goto _OVER;
×
671
  }
672
  tstrncpy(pVersion->name, "tsmmConfigVersion", CFG_NAME_MAX_LEN);
×
673
  pVersion->dtype = CFG_DTYPE_INT32;
×
674
  pVersion->i32 = tsmmConfigVersion;
×
675

676
  pObj->dtype = dtype;
×
677
  tstrncpy(pObj->name, name, CFG_NAME_MAX_LEN);
×
678

679
  TAOS_CHECK_GOTO(mndUpdateObj(pObj, name, pValue), &lino, _OVER);
×
680
  STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_NOTHING, NULL, "update-config");
×
681
  if (pTrans == NULL) {
×
682
    code = terrno;
×
683
    goto _OVER;
×
684
  }
685
  mInfo("trans:%d, used to update config:%s to value:%s", pTrans->id, name, pValue);
×
686
  TAOS_CHECK_GOTO(mndSetCreateConfigCommitLogs(pTrans, pVersion), &lino, _OVER);
×
687
  TAOS_CHECK_GOTO(mndSetCreateConfigCommitLogs(pTrans, pObj), &lino, _OVER);
×
688
  if ((code = mndTransPrepare(pMnode, pTrans)) != 0) goto _OVER;
×
689
  code = 0;
×
690
_OVER:
×
691
  if (code != 0) {
×
692
    mError("failed to update config:%s to value:%s, since %s", name, pValue, tstrerror(code));
×
693
  }
694
  mndTransDrop(pTrans);
×
695
  tFreeSConfigObj(pVersion);
×
696
  taosMemoryFree(pVersion);
×
697
  tFreeSConfigObj(pObj);
×
698
  taosMemoryFree(pObj);
×
699
  return code;
×
700
}
701

702
static int32_t initConfigArrayFromSdb(SMnode *pMnode, SArray *array) {
×
703
  int32_t     code = 0;
×
704
  SSdb       *pSdb = pMnode->pSdb;
×
705
  void       *pIter = NULL;
×
706
  SConfigObj *obj = NULL;
×
707

708
  while (1) {
×
709
    pIter = sdbFetch(pSdb, SDB_CFG, pIter, (void **)&obj);
×
710
    if (pIter == NULL) break;
×
711
    if (obj == NULL) {
×
712
      code = TSDB_CODE_OUT_OF_MEMORY;
×
713
      goto _exit;
×
714
    }
715
    if (strcasecmp(obj->name, "tsmmConfigVersion") == 0) {
×
716
      sdbRelease(pSdb, obj);
×
717
      continue;
×
718
    }
719
    SConfigItem item = {0};
×
720
    item.dtype = obj->dtype;
×
721
    item.name = taosStrdup(obj->name);
×
722
    if (item.name == NULL) {
×
723
      code = terrno;
×
724
      sdbCancelFetch(pSdb, pIter);
×
725
      sdbRelease(pSdb, obj);
×
726
      goto _exit;
×
727
    }
728
    switch (obj->dtype) {
×
729
      case CFG_DTYPE_NONE:
×
730
        break;
×
731
      case CFG_DTYPE_BOOL:
×
732
        item.bval = obj->bval;
×
733
        break;
×
734
      case CFG_DTYPE_INT32:
×
735
        item.i32 = obj->i32;
×
736
        break;
×
737
      case CFG_DTYPE_INT64:
×
738
        item.i64 = obj->i64;
×
739
        break;
×
740
      case CFG_DTYPE_FLOAT:
×
741
      case CFG_DTYPE_DOUBLE:
742
        item.fval = obj->fval;
×
743
        break;
×
744
      case CFG_DTYPE_STRING:
×
745
      case CFG_DTYPE_DIR:
746
      case CFG_DTYPE_LOCALE:
747
      case CFG_DTYPE_CHARSET:
748
      case CFG_DTYPE_TIMEZONE:
749
        item.str = taosStrdup(obj->str);
×
750
        if (item.str == NULL) {
×
751
          sdbCancelFetch(pSdb, pIter);
×
752
          sdbRelease(pSdb, obj);
×
753
          code = terrno;
×
754
          goto _exit;
×
755
        }
756
        break;
×
757
    }
758
    if (taosArrayPush(array, &item) == NULL) {
×
759
      sdbCancelFetch(pSdb, pIter);
×
760
      sdbRelease(pSdb, obj);
×
761
      code = TSDB_CODE_OUT_OF_MEMORY;
×
762
      goto _exit;
×
763
      break;
764
    }
765
    sdbRelease(pSdb, obj);
×
766
  }
767
_exit:
×
768
  if (code != 0) {
×
769
    mError("failed to init config array from sdb, since %s", tstrerror(code));
×
770
  }
771
  return code;
×
772
}
773

774
static void cfgArrayCleanUp(SArray *array) {
×
775
  if (array == NULL) {
×
776
    return;
×
777
  }
778

779
  int32_t sz = taosArrayGetSize(array);
×
780
  for (int32_t i = 0; i < sz; ++i) {
×
781
    SConfigItem *item = taosArrayGet(array, i);
×
782
    if (item->dtype == CFG_DTYPE_STRING || item->dtype == CFG_DTYPE_DIR || item->dtype == CFG_DTYPE_LOCALE ||
×
783
        item->dtype == CFG_DTYPE_CHARSET || item->dtype == CFG_DTYPE_TIMEZONE) {
×
784
      taosMemoryFreeClear(item->str);
×
785
    }
786
    taosMemoryFreeClear(item->name);
×
787
  }
788

789
  taosArrayDestroy(array);
×
790
}
791

792
static void cfgObjArrayCleanUp(SArray *array) {
×
793
  if (array == NULL) {
×
794
    return;
×
795
  }
796
  int32_t sz = taosArrayGetSize(array);
×
797
  for (int32_t i = 0; i < sz; ++i) {
×
798
    SConfigObj *obj = taosArrayGet(array, i);
×
799
    tFreeSConfigObj(obj);
×
800
  }
801
  taosArrayDestroy(array);
×
802
}
803

804
SArray *initVariablesFromItems(SArray *pItems) {
×
805
  if (pItems == NULL) {
×
806
    return NULL;
×
807
  }
808

809
  int32_t sz = taosArrayGetSize(pItems);
×
810

811
  SArray *pInfos = taosArrayInit(sz, sizeof(SVariablesInfo));
×
812
  if (pInfos == NULL) {
×
813
    mError("failed to init array while init variables from items, since %s", tstrerror(terrno));
×
814
    return NULL;
×
815
  }
816
  for (int32_t i = 0; i < sz; ++i) {
×
817
    SConfigItem   *pItem = taosArrayGet(pItems, i);
×
818
    SVariablesInfo info = {0};
×
819
    tstrncpy(info.name, pItem->name, sizeof(info.name));
×
820

821
    // init info value
822
    switch (pItem->dtype) {
×
823
      case CFG_DTYPE_NONE:
×
824
        break;
×
825
      case CFG_DTYPE_BOOL:
×
826
        tsnprintf(info.value, sizeof(info.value), "%d", pItem->bval);
×
827
        break;
×
828
      case CFG_DTYPE_INT32:
×
829
        tsnprintf(info.value, sizeof(info.value), "%d", pItem->i32);
×
830
        break;
×
831
      case CFG_DTYPE_INT64:
×
832
        tsnprintf(info.value, sizeof(info.value), "%" PRId64, pItem->i64);
×
833
        break;
×
834
      case CFG_DTYPE_FLOAT:
×
835
      case CFG_DTYPE_DOUBLE:
836
        tsnprintf(info.value, sizeof(info.value), "%f", pItem->fval);
×
837
        break;
×
838
      case CFG_DTYPE_STRING:
×
839
      case CFG_DTYPE_DIR:
840
      case CFG_DTYPE_LOCALE:
841
      case CFG_DTYPE_CHARSET:
842
      case CFG_DTYPE_TIMEZONE:
843
        tsnprintf(info.value, sizeof(info.value), "%s", pItem->str);
×
844
        break;
×
845
    }
846

847
    // init info scope
848
    switch (pItem->scope) {
×
849
      case CFG_SCOPE_SERVER:
×
850
        tstrncpy(info.scope, "server", sizeof(info.scope));
×
851
        break;
×
852
      case CFG_SCOPE_CLIENT:
×
853
        tstrncpy(info.scope, "client", sizeof(info.scope));
×
854
        break;
×
855
      case CFG_SCOPE_BOTH:
×
856
        tstrncpy(info.scope, "both", sizeof(info.scope));
×
857
        break;
×
858
      default:
×
859
        tstrncpy(info.scope, "unknown", sizeof(info.scope));
×
860
        break;
×
861
    }
862
    // init info category
863
    switch (pItem->category) {
×
864
      case CFG_CATEGORY_GLOBAL:
×
865
        tstrncpy(info.category, "global", sizeof(info.category));
×
866
        break;
×
867
      case CFG_CATEGORY_LOCAL:
×
868
        tstrncpy(info.category, "local", sizeof(info.category));
×
869
        break;
×
870
      default:
×
871
        tstrncpy(info.category, "unknown", sizeof(info.category));
×
872
        break;
×
873
    }
874
    if (NULL == taosArrayPush(pInfos, &info)) {
×
875
      mError("failed to push info to array while init variables from items,since %s", tstrerror(terrno));
×
876
      taosArrayDestroy(pInfos);
×
877
      return NULL;
×
878
    }
879
  }
880

881
  return pInfos;
×
882
}
883

884
static int32_t mndProcessShowVariablesReq(SRpcMsg *pReq) {
×
885
  SShowVariablesRsp rsp = {0};
×
886
  int32_t           code = -1;
×
887

888
  if ((code = mndCheckOperPrivilege(pReq->info.node, pReq->info.conn.user, MND_OPER_SHOW_VARIABLES)) != 0) {
×
889
    goto _OVER;
×
890
  }
891

892
  SVariablesInfo info = {0};
×
893

894
  rsp.variables = initVariablesFromItems(taosGetGlobalCfg(tsCfg));
×
895
  if (rsp.variables == NULL) {
×
896
    code = terrno;
×
897
    goto _OVER;
×
898
  }
899
  int32_t rspLen = tSerializeSShowVariablesRsp(NULL, 0, &rsp);
×
900
  void   *pRsp = rpcMallocCont(rspLen);
×
901
  if (pRsp == NULL) {
×
902
    code = terrno;
×
903
    goto _OVER;
×
904
  }
905

906
  if ((rspLen = tSerializeSShowVariablesRsp(pRsp, rspLen, &rsp)) <= 0) {
×
907
    rpcFreeCont(pRsp);
×
908
    code = rspLen;
×
909
    goto _OVER;
×
910
  }
911

912
  pReq->info.rspLen = rspLen;
×
913
  pReq->info.rsp = pRsp;
×
914
  code = 0;
×
915

916
_OVER:
×
917

918
  if (code != 0) {
×
919
    mError("failed to get show variables info since %s", tstrerror(code));
×
920
  }
921

922
  tFreeSShowVariablesRsp(&rsp);
×
923
  TAOS_RETURN(code);
×
924
}
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