syncRaftCfg.c 16.7 KB
Newer Older
M
Minghao Li 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "syncRaftCfg.h"
#include "cJSON.h"
#include "syncEnv.h"
#include "syncUtil.h"

21 22 23 24 25 26 27 28 29 30 31
// file must already exist!
SRaftCfgIndex *raftCfgIndexOpen(const char *path) {
  SRaftCfgIndex *pRaftCfgIndex = taosMemoryMalloc(sizeof(SRaftCfg));
  snprintf(pRaftCfgIndex->path, sizeof(pRaftCfgIndex->path), "%s", path);

  pRaftCfgIndex->pFile = taosOpenFile(pRaftCfgIndex->path, TD_FILE_READ | TD_FILE_WRITE);
  ASSERT(pRaftCfgIndex->pFile != NULL);

  taosLSeekFile(pRaftCfgIndex->pFile, 0, SEEK_SET);

  int32_t bufLen = MAX_CONFIG_INDEX_COUNT * 16;
S
Shengliang Guan 已提交
32
  char   *pBuf = taosMemoryMalloc(bufLen);
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
  memset(pBuf, 0, bufLen);
  int64_t len = taosReadFile(pRaftCfgIndex->pFile, pBuf, bufLen);
  ASSERT(len > 0);

  int32_t ret = raftCfgIndexFromStr(pBuf, pRaftCfgIndex);
  ASSERT(ret == 0);

  taosMemoryFree(pBuf);

  return pRaftCfgIndex;
}

int32_t raftCfgIndexClose(SRaftCfgIndex *pRaftCfgIndex) {
  if (pRaftCfgIndex != NULL) {
    int64_t ret = taosCloseFile(&(pRaftCfgIndex->pFile));
    ASSERT(ret == 0);
    taosMemoryFree(pRaftCfgIndex);
  }
  return 0;
}

int32_t raftCfgIndexPersist(SRaftCfgIndex *pRaftCfgIndex) {
  ASSERT(pRaftCfgIndex != NULL);

  char *s = raftCfgIndex2Str(pRaftCfgIndex);
  taosLSeekFile(pRaftCfgIndex->pFile, 0, SEEK_SET);

  int64_t ret = taosWriteFile(pRaftCfgIndex->pFile, s, strlen(s) + 1);
  ASSERT(ret == strlen(s) + 1);

  taosMemoryFree(s);
  taosFsyncFile(pRaftCfgIndex->pFile);
  return 0;
}

int32_t raftCfgIndexAddConfigIndex(SRaftCfgIndex *pRaftCfgIndex, SyncIndex configIndex) {
  ASSERT(pRaftCfgIndex->configIndexCount <= MAX_CONFIG_INDEX_COUNT);
  (pRaftCfgIndex->configIndexArr)[pRaftCfgIndex->configIndexCount] = configIndex;
  ++(pRaftCfgIndex->configIndexCount);
  return 0;
}

cJSON *raftCfgIndex2Json(SRaftCfgIndex *pRaftCfgIndex) {
  cJSON *pRoot = cJSON_CreateObject();

  cJSON_AddNumberToObject(pRoot, "configIndexCount", pRaftCfgIndex->configIndexCount);
  cJSON *pIndexArr = cJSON_CreateArray();
  cJSON_AddItemToObject(pRoot, "configIndexArr", pIndexArr);
  for (int i = 0; i < pRaftCfgIndex->configIndexCount; ++i) {
    char buf64[128];
    snprintf(buf64, sizeof(buf64), "%" PRId64, (pRaftCfgIndex->configIndexArr)[i]);
    cJSON *pIndexObj = cJSON_CreateObject();
    cJSON_AddStringToObject(pIndexObj, "index", buf64);
    cJSON_AddItemToArray(pIndexArr, pIndexObj);
  }

  cJSON *pJson = cJSON_CreateObject();
  cJSON_AddItemToObject(pJson, "SRaftCfgIndex", pRoot);
  return pJson;
}

char *raftCfgIndex2Str(SRaftCfgIndex *pRaftCfgIndex) {
  cJSON *pJson = raftCfgIndex2Json(pRaftCfgIndex);
S
Shengliang Guan 已提交
96
  char  *serialized = cJSON_Print(pJson);
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
  cJSON_Delete(pJson);
  return serialized;
}

int32_t raftCfgIndexFromJson(const cJSON *pRoot, SRaftCfgIndex *pRaftCfgIndex) {
  cJSON *pJson = cJSON_GetObjectItem(pRoot, "SRaftCfgIndex");

  cJSON *pJsonConfigIndexCount = cJSON_GetObjectItem(pJson, "configIndexCount");
  pRaftCfgIndex->configIndexCount = cJSON_GetNumberValue(pJsonConfigIndexCount);

  cJSON *pIndexArr = cJSON_GetObjectItem(pJson, "configIndexArr");
  int    arraySize = cJSON_GetArraySize(pIndexArr);
  ASSERT(arraySize == pRaftCfgIndex->configIndexCount);

  memset(pRaftCfgIndex->configIndexArr, 0, sizeof(pRaftCfgIndex->configIndexArr));
  for (int i = 0; i < arraySize; ++i) {
    cJSON *pIndexObj = cJSON_GetArrayItem(pIndexArr, i);
    ASSERT(pIndexObj != NULL);

    cJSON *pIndex = cJSON_GetObjectItem(pIndexObj, "index");
    ASSERT(cJSON_IsString(pIndex));
    (pRaftCfgIndex->configIndexArr)[i] = atoll(pIndex->valuestring);
  }

  return 0;
}

int32_t raftCfgIndexFromStr(const char *s, SRaftCfgIndex *pRaftCfgIndex) {
  cJSON *pRoot = cJSON_Parse(s);
  ASSERT(pRoot != NULL);

  int32_t ret = raftCfgIndexFromJson(pRoot, pRaftCfgIndex);
  ASSERT(ret == 0);

  cJSON_Delete(pRoot);
  return 0;
}

int32_t raftCfgIndexCreateFile(const char *path) {
  TdFilePtr pFile = taosOpenFile(path, TD_FILE_CREATE | TD_FILE_WRITE);
  if (pFile == NULL) {
    int32_t     err = terrno;
    const char *errStr = tstrerror(err);
    int32_t     sysErr = errno;
    const char *sysErrStr = strerror(errno);
    sError("create raft cfg index file error, err:%d %X, msg:%s, syserr:%d, sysmsg:%s", err, err, errStr, sysErr,
           sysErrStr);
    ASSERT(0);

    return -1;
  }

  SRaftCfgIndex raftCfgIndex;
  memset(raftCfgIndex.configIndexArr, 0, sizeof(raftCfgIndex.configIndexArr));
  raftCfgIndex.configIndexCount = 1;
  raftCfgIndex.configIndexArr[0] = -1;

S
Shengliang Guan 已提交
154
  char   *s = raftCfgIndex2Str(&raftCfgIndex);
155 156 157 158 159 160 161 162 163
  int64_t ret = taosWriteFile(pFile, s, strlen(s) + 1);
  ASSERT(ret == strlen(s) + 1);

  taosMemoryFree(s);
  taosCloseFile(&pFile);
  return 0;
}

// ---------------------------------------
M
Minghao Li 已提交
164 165 166 167 168 169
// file must already exist!
SRaftCfg *raftCfgOpen(const char *path) {
  SRaftCfg *pCfg = taosMemoryMalloc(sizeof(SRaftCfg));
  snprintf(pCfg->path, sizeof(pCfg->path), "%s", path);

  pCfg->pFile = taosOpenFile(pCfg->path, TD_FILE_READ | TD_FILE_WRITE);
M
Minghao Li 已提交
170
  ASSERT(pCfg->pFile != NULL);
M
Minghao Li 已提交
171 172 173

  taosLSeekFile(pCfg->pFile, 0, SEEK_SET);

174
  char buf[CONFIG_FILE_LEN] = {0};
M
Minghao Li 已提交
175
  int  len = taosReadFile(pCfg->pFile, buf, sizeof(buf));
M
Minghao Li 已提交
176
  ASSERT(len > 0);
M
Minghao Li 已提交
177

M
Minghao Li 已提交
178
  int32_t ret = raftCfgFromStr(buf, pCfg);
M
Minghao Li 已提交
179
  ASSERT(ret == 0);
M
Minghao Li 已提交
180 181 182 183 184 185

  return pCfg;
}

int32_t raftCfgClose(SRaftCfg *pRaftCfg) {
  int64_t ret = taosCloseFile(&(pRaftCfg->pFile));
M
Minghao Li 已提交
186
  ASSERT(ret == 0);
M
Minghao Li 已提交
187 188 189 190 191
  taosMemoryFree(pRaftCfg);
  return 0;
}

int32_t raftCfgPersist(SRaftCfg *pRaftCfg) {
M
Minghao Li 已提交
192
  ASSERT(pRaftCfg != NULL);
M
Minghao Li 已提交
193

M
Minghao Li 已提交
194
  char *s = raftCfg2Str(pRaftCfg);
M
Minghao Li 已提交
195 196
  taosLSeekFile(pRaftCfg->pFile, 0, SEEK_SET);

197
  char buf[CONFIG_FILE_LEN] = {0};
M
Minghao Li 已提交
198
  memset(buf, 0, sizeof(buf));
M
Minghao Li 已提交
199 200 201 202 203 204

  if (strlen(s) + 1 > CONFIG_FILE_LEN) {
    sError("too long config str:%s", s);
    ASSERT(0);
  }

M
Minghao Li 已提交
205 206
  snprintf(buf, sizeof(buf), "%s", s);
  int64_t ret = taosWriteFile(pRaftCfg->pFile, buf, sizeof(buf));
M
Minghao Li 已提交
207
  ASSERT(ret == sizeof(buf));
M
Minghao Li 已提交
208

209
  // int64_t ret = taosWriteFile(pRaftCfg->pFile, s, strlen(s) + 1);
M
Minghao Li 已提交
210
  // ASSERT(ret == strlen(s) + 1);
M
Minghao Li 已提交
211 212

  taosMemoryFree(s);
M
Minghao Li 已提交
213 214 215 216
  taosFsyncFile(pRaftCfg->pFile);
  return 0;
}

217 218 219 220 221 222 223
int32_t raftCfgAddConfigIndex(SRaftCfg *pRaftCfg, SyncIndex configIndex) {
  ASSERT(pRaftCfg->configIndexCount <= MAX_CONFIG_INDEX_COUNT);
  (pRaftCfg->configIndexArr)[pRaftCfg->configIndexCount] = configIndex;
  ++(pRaftCfg->configIndexCount);
  return 0;
}

M
Minghao Li 已提交
224
cJSON *syncCfg2Json(SSyncCfg *pSyncCfg) {
225
  char   u64buf[128] = {0};
M
Minghao Li 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
  cJSON *pRoot = cJSON_CreateObject();

  if (pSyncCfg != NULL) {
    cJSON_AddNumberToObject(pRoot, "replicaNum", pSyncCfg->replicaNum);
    cJSON_AddNumberToObject(pRoot, "myIndex", pSyncCfg->myIndex);

    cJSON *pNodeInfoArr = cJSON_CreateArray();
    cJSON_AddItemToObject(pRoot, "nodeInfo", pNodeInfoArr);
    for (int i = 0; i < pSyncCfg->replicaNum; ++i) {
      cJSON *pNodeInfo = cJSON_CreateObject();
      cJSON_AddNumberToObject(pNodeInfo, "nodePort", ((pSyncCfg->nodeInfo)[i]).nodePort);
      cJSON_AddStringToObject(pNodeInfo, "nodeFqdn", ((pSyncCfg->nodeInfo)[i]).nodeFqdn);
      cJSON_AddItemToArray(pNodeInfoArr, pNodeInfo);
    }
  }

M
Minghao Li 已提交
242
  return pRoot;
M
Minghao Li 已提交
243 244 245 246
}

char *syncCfg2Str(SSyncCfg *pSyncCfg) {
  cJSON *pJson = syncCfg2Json(pSyncCfg);
S
Shengliang Guan 已提交
247
  char  *serialized = cJSON_Print(pJson);
M
Minghao Li 已提交
248 249 250 251
  cJSON_Delete(pJson);
  return serialized;
}

M
Minghao Li 已提交
252
char *syncCfg2SimpleStr(SSyncCfg *pSyncCfg) {
253 254
  if (pSyncCfg != NULL) {
    int32_t len = 512;
S
Shengliang Guan 已提交
255
    char   *s = taosMemoryMalloc(len);
256 257
    memset(s, 0, len);

M
Minghao Li 已提交
258
    snprintf(s, len, "{r-num:%d, my:%d, ", pSyncCfg->replicaNum, pSyncCfg->myIndex);
259 260 261 262 263 264 265 266 267 268 269
    char *p = s + strlen(s);
    for (int i = 0; i < pSyncCfg->replicaNum; ++i) {
      /*
      if (p + 128 + 32 > s + len) {
        break;
      }
      */
      char buf[128 + 32];
      snprintf(buf, sizeof(buf), "%s:%d, ", pSyncCfg->nodeInfo[i].nodeFqdn, pSyncCfg->nodeInfo[i].nodePort);
      strncpy(p, buf, sizeof(buf));
      p = s + strlen(s);
M
Minghao Li 已提交
270
    }
271 272 273
    strcpy(p - 2, "}");

    return s;
M
Minghao Li 已提交
274 275
  }

276
  return NULL;
M
Minghao Li 已提交
277 278
}

M
Minghao Li 已提交
279 280
int32_t syncCfgFromJson(const cJSON *pRoot, SSyncCfg *pSyncCfg) {
  memset(pSyncCfg, 0, sizeof(SSyncCfg));
M
Minghao Li 已提交
281 282
  // cJSON *pJson = cJSON_GetObjectItem(pRoot, "SSyncCfg");
  const cJSON *pJson = pRoot;
M
Minghao Li 已提交
283 284

  cJSON *pReplicaNum = cJSON_GetObjectItem(pJson, "replicaNum");
M
Minghao Li 已提交
285
  ASSERT(cJSON_IsNumber(pReplicaNum));
M
Minghao Li 已提交
286 287 288
  pSyncCfg->replicaNum = cJSON_GetNumberValue(pReplicaNum);

  cJSON *pMyIndex = cJSON_GetObjectItem(pJson, "myIndex");
M
Minghao Li 已提交
289
  ASSERT(cJSON_IsNumber(pMyIndex));
M
Minghao Li 已提交
290 291 292 293
  pSyncCfg->myIndex = cJSON_GetNumberValue(pMyIndex);

  cJSON *pNodeInfoArr = cJSON_GetObjectItem(pJson, "nodeInfo");
  int    arraySize = cJSON_GetArraySize(pNodeInfoArr);
M
Minghao Li 已提交
294
  ASSERT(arraySize == pSyncCfg->replicaNum);
M
Minghao Li 已提交
295 296 297

  for (int i = 0; i < arraySize; ++i) {
    cJSON *pNodeInfo = cJSON_GetArrayItem(pNodeInfoArr, i);
M
Minghao Li 已提交
298
    ASSERT(pNodeInfo != NULL);
M
Minghao Li 已提交
299 300

    cJSON *pNodePort = cJSON_GetObjectItem(pNodeInfo, "nodePort");
M
Minghao Li 已提交
301
    ASSERT(cJSON_IsNumber(pNodePort));
M
Minghao Li 已提交
302 303 304
    ((pSyncCfg->nodeInfo)[i]).nodePort = cJSON_GetNumberValue(pNodePort);

    cJSON *pNodeFqdn = cJSON_GetObjectItem(pNodeInfo, "nodeFqdn");
M
Minghao Li 已提交
305
    ASSERT(cJSON_IsString(pNodeFqdn));
M
Minghao Li 已提交
306 307 308 309 310 311 312 313 314
    snprintf(((pSyncCfg->nodeInfo)[i]).nodeFqdn, sizeof(((pSyncCfg->nodeInfo)[i]).nodeFqdn), "%s",
             pNodeFqdn->valuestring);
  }

  return 0;
}

int32_t syncCfgFromStr(const char *s, SSyncCfg *pSyncCfg) {
  cJSON *pRoot = cJSON_Parse(s);
M
Minghao Li 已提交
315
  ASSERT(pRoot != NULL);
M
Minghao Li 已提交
316 317

  int32_t ret = syncCfgFromJson(pRoot, pSyncCfg);
M
Minghao Li 已提交
318
  ASSERT(ret == 0);
M
Minghao Li 已提交
319 320 321 322 323 324

  cJSON_Delete(pRoot);
  return 0;
}

cJSON *raftCfg2Json(SRaftCfg *pRaftCfg) {
M
Minghao Li 已提交
325 326 327
  cJSON *pRoot = cJSON_CreateObject();
  cJSON_AddItemToObject(pRoot, "SSyncCfg", syncCfg2Json(&(pRaftCfg->cfg)));
  cJSON_AddNumberToObject(pRoot, "isStandBy", pRaftCfg->isStandBy);
M
Minghao Li 已提交
328
  cJSON_AddNumberToObject(pRoot, "snapshotStrategy", pRaftCfg->snapshotStrategy);
M
Minghao Li 已提交
329
  cJSON_AddNumberToObject(pRoot, "batchSize", pRaftCfg->batchSize);
M
Minghao Li 已提交
330

331
  char buf64[128];
S
Shengliang Guan 已提交
332
  snprintf(buf64, sizeof(buf64), "%" PRId64, pRaftCfg->lastConfigIndex);
333 334
  cJSON_AddStringToObject(pRoot, "lastConfigIndex", buf64);

335 336 337 338
  cJSON_AddNumberToObject(pRoot, "configIndexCount", pRaftCfg->configIndexCount);
  cJSON *pIndexArr = cJSON_CreateArray();
  cJSON_AddItemToObject(pRoot, "configIndexArr", pIndexArr);
  for (int i = 0; i < pRaftCfg->configIndexCount; ++i) {
S
Shengliang Guan 已提交
339
    snprintf(buf64, sizeof(buf64), "%" PRId64, (pRaftCfg->configIndexArr)[i]);
340 341 342 343 344
    cJSON *pIndexObj = cJSON_CreateObject();
    cJSON_AddStringToObject(pIndexObj, "index", buf64);
    cJSON_AddItemToArray(pIndexArr, pIndexObj);
  }

M
Minghao Li 已提交
345 346
  cJSON *pJson = cJSON_CreateObject();
  cJSON_AddItemToObject(pJson, "RaftCfg", pRoot);
M
Minghao Li 已提交
347 348 349 350
  return pJson;
}

char *raftCfg2Str(SRaftCfg *pRaftCfg) {
M
Minghao Li 已提交
351
  cJSON *pJson = raftCfg2Json(pRaftCfg);
S
Shengliang Guan 已提交
352
  char  *serialized = cJSON_Print(pJson);
M
Minghao Li 已提交
353 354
  cJSON_Delete(pJson);
  return serialized;
M
Minghao Li 已提交
355 356
}

M
Minghao Li 已提交
357
int32_t raftCfgCreateFile(SSyncCfg *pCfg, SRaftCfgMeta meta, const char *path) {
M
Minghao Li 已提交
358
  TdFilePtr pFile = taosOpenFile(path, TD_FILE_CREATE | TD_FILE_WRITE);
M
Minghao Li 已提交
359 360 361 362 363 364 365 366
  if (pFile == NULL) {
    int32_t     err = terrno;
    const char *errStr = tstrerror(err);
    int32_t     sysErr = errno;
    const char *sysErrStr = strerror(errno);
    sError("create raft cfg file error, err:%d %X, msg:%s, syserr:%d, sysmsg:%s", err, err, errStr, sysErr, sysErrStr);
    return -1;
  }
M
Minghao Li 已提交
367

M
Minghao Li 已提交
368 369
  SRaftCfg raftCfg;
  raftCfg.cfg = *pCfg;
M
Minghao Li 已提交
370
  raftCfg.isStandBy = meta.isStandBy;
M
Minghao Li 已提交
371
  raftCfg.batchSize = meta.batchSize;
M
Minghao Li 已提交
372
  raftCfg.snapshotStrategy = meta.snapshotStrategy;
373
  raftCfg.lastConfigIndex = meta.lastConfigIndex;
374 375 376
  raftCfg.configIndexCount = 1;
  memset(raftCfg.configIndexArr, 0, sizeof(raftCfg.configIndexArr));
  raftCfg.configIndexArr[0] = -1;
377
  char *s = raftCfg2Str(&raftCfg);
M
Minghao Li 已提交
378

379
  char buf[CONFIG_FILE_LEN] = {0};
M
Minghao Li 已提交
380 381 382 383
  memset(buf, 0, sizeof(buf));
  ASSERT(strlen(s) + 1 <= CONFIG_FILE_LEN);
  snprintf(buf, sizeof(buf), "%s", s);
  int64_t ret = taosWriteFile(pFile, buf, sizeof(buf));
M
Minghao Li 已提交
384
  ASSERT(ret == sizeof(buf));
M
Minghao Li 已提交
385

386
  // int64_t ret = taosWriteFile(pFile, s, strlen(s) + 1);
M
Minghao Li 已提交
387
  // ASSERT(ret == strlen(s) + 1);
M
Minghao Li 已提交
388 389 390 391 392 393

  taosMemoryFree(s);
  taosCloseFile(&pFile);
  return 0;
}

M
Minghao Li 已提交
394 395 396 397 398 399 400
int32_t raftCfgFromJson(const cJSON *pRoot, SRaftCfg *pRaftCfg) {
  // memset(pRaftCfg, 0, sizeof(SRaftCfg));
  cJSON *pJson = cJSON_GetObjectItem(pRoot, "RaftCfg");

  cJSON *pJsonIsStandBy = cJSON_GetObjectItem(pJson, "isStandBy");
  pRaftCfg->isStandBy = cJSON_GetNumberValue(pJsonIsStandBy);

M
Minghao Li 已提交
401 402 403
  cJSON *pJsonBatchSize = cJSON_GetObjectItem(pJson, "batchSize");
  pRaftCfg->batchSize = cJSON_GetNumberValue(pJsonBatchSize);

M
Minghao Li 已提交
404 405
  cJSON *pJsonSnapshotStrategy = cJSON_GetObjectItem(pJson, "snapshotStrategy");
  pRaftCfg->snapshotStrategy = cJSON_GetNumberValue(pJsonSnapshotStrategy);
M
Minghao Li 已提交
406

407 408 409
  cJSON *pJsonLastConfigIndex = cJSON_GetObjectItem(pJson, "lastConfigIndex");
  pRaftCfg->lastConfigIndex = atoll(cJSON_GetStringValue(pJsonLastConfigIndex));

410 411 412 413 414
  cJSON *pJsonConfigIndexCount = cJSON_GetObjectItem(pJson, "configIndexCount");
  pRaftCfg->configIndexCount = cJSON_GetNumberValue(pJsonConfigIndexCount);

  cJSON *pIndexArr = cJSON_GetObjectItem(pJson, "configIndexArr");
  int    arraySize = cJSON_GetArraySize(pIndexArr);
M
Minghao Li 已提交
415
  ASSERT(arraySize == pRaftCfg->configIndexCount);
416 417 418 419

  memset(pRaftCfg->configIndexArr, 0, sizeof(pRaftCfg->configIndexArr));
  for (int i = 0; i < arraySize; ++i) {
    cJSON *pIndexObj = cJSON_GetArrayItem(pIndexArr, i);
M
Minghao Li 已提交
420
    ASSERT(pIndexObj != NULL);
421 422

    cJSON *pIndex = cJSON_GetObjectItem(pIndexObj, "index");
M
Minghao Li 已提交
423
    ASSERT(cJSON_IsString(pIndex));
424 425 426
    (pRaftCfg->configIndexArr)[i] = atoll(pIndex->valuestring);
  }

S
Shengliang Guan 已提交
427
  cJSON  *pJsonSyncCfg = cJSON_GetObjectItem(pJson, "SSyncCfg");
M
Minghao Li 已提交
428 429 430 431 432 433 434 435
  int32_t code = syncCfgFromJson(pJsonSyncCfg, &(pRaftCfg->cfg));
  ASSERT(code == 0);

  return code;
}

int32_t raftCfgFromStr(const char *s, SRaftCfg *pRaftCfg) {
  cJSON *pRoot = cJSON_Parse(s);
M
Minghao Li 已提交
436
  ASSERT(pRoot != NULL);
M
Minghao Li 已提交
437 438

  int32_t ret = raftCfgFromJson(pRoot, pRaftCfg);
M
Minghao Li 已提交
439
  ASSERT(ret == 0);
M
Minghao Li 已提交
440 441 442 443 444

  cJSON_Delete(pRoot);
  return 0;
}

M
Minghao Li 已提交
445 446 447
// for debug ----------------------
void syncCfgPrint(SSyncCfg *pCfg) {
  char *serialized = syncCfg2Str(pCfg);
S
Shengliang Guan 已提交
448
  printf("syncCfgPrint | len:%d | %s \n", (int32_t)strlen(serialized), serialized);
M
Minghao Li 已提交
449 450 451 452 453 454
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncCfgPrint2(char *s, SSyncCfg *pCfg) {
  char *serialized = syncCfg2Str(pCfg);
S
Shengliang Guan 已提交
455
  printf("syncCfgPrint2 | len:%d | %s | %s \n", (int32_t)strlen(serialized), s, serialized);
M
Minghao Li 已提交
456 457 458 459 460 461
  fflush(NULL);
  taosMemoryFree(serialized);
}

void syncCfgLog(SSyncCfg *pCfg) {
  char *serialized = syncCfg2Str(pCfg);
S
Shengliang Guan 已提交
462
  sTrace("syncCfgLog | len:%d | %s", (int32_t)strlen(serialized), serialized);
M
Minghao Li 已提交
463 464 465 466 467
  taosMemoryFree(serialized);
}

void syncCfgLog2(char *s, SSyncCfg *pCfg) {
  char *serialized = syncCfg2Str(pCfg);
S
Shengliang Guan 已提交
468
  sTrace("syncCfgLog2 | len:%d | %s | %s", (int32_t)strlen(serialized), s, serialized);
M
Minghao Li 已提交
469 470 471
  taosMemoryFree(serialized);
}

M
Minghao Li 已提交
472 473
void syncCfgLog3(char *s, SSyncCfg *pCfg) {
  char *serialized = syncCfg2SimpleStr(pCfg);
S
Shengliang Guan 已提交
474
  sTrace("syncCfgLog3 | len:%d | %s | %s", (int32_t)strlen(serialized), s, serialized);
M
Minghao Li 已提交
475 476 477
  taosMemoryFree(serialized);
}

M
Minghao Li 已提交
478 479
void raftCfgPrint(SRaftCfg *pCfg) {
  char *serialized = raftCfg2Str(pCfg);
S
Shengliang Guan 已提交
480
  printf("raftCfgPrint | len:%d | %s \n", (int32_t)strlen(serialized), serialized);
M
Minghao Li 已提交
481 482 483 484 485 486
  fflush(NULL);
  taosMemoryFree(serialized);
}

void raftCfgPrint2(char *s, SRaftCfg *pCfg) {
  char *serialized = raftCfg2Str(pCfg);
S
Shengliang Guan 已提交
487
  printf("raftCfgPrint2 | len:%d | %s | %s \n", (int32_t)strlen(serialized), s, serialized);
M
Minghao Li 已提交
488 489 490 491 492 493
  fflush(NULL);
  taosMemoryFree(serialized);
}

void raftCfgLog(SRaftCfg *pCfg) {
  char *serialized = raftCfg2Str(pCfg);
S
Shengliang Guan 已提交
494
  sTrace("raftCfgLog | len:%d | %s", (int32_t)strlen(serialized), serialized);
M
Minghao Li 已提交
495 496 497 498 499
  taosMemoryFree(serialized);
}

void raftCfgLog2(char *s, SRaftCfg *pCfg) {
  char *serialized = raftCfg2Str(pCfg);
S
Shengliang Guan 已提交
500
  sTrace("raftCfgLog2 | len:%d | %s | %s", (int32_t)strlen(serialized), s, serialized);
M
Minghao Li 已提交
501 502
  taosMemoryFree(serialized);
}
503 504 505 506

// ---------
void raftCfgIndexPrint(SRaftCfgIndex *pCfg) {
  char *serialized = raftCfgIndex2Str(pCfg);
S
Shengliang Guan 已提交
507
  printf("raftCfgIndexPrint | len:%d | %s \n", (int32_t)strlen(serialized), serialized);
508 509 510 511 512 513
  fflush(NULL);
  taosMemoryFree(serialized);
}

void raftCfgIndexPrint2(char *s, SRaftCfgIndex *pCfg) {
  char *serialized = raftCfgIndex2Str(pCfg);
S
Shengliang Guan 已提交
514
  printf("raftCfgIndexPrint2 | len:%d | %s | %s \n", (int32_t)strlen(serialized), s, serialized);
515 516 517 518 519 520
  fflush(NULL);
  taosMemoryFree(serialized);
}

void raftCfgIndexLog(SRaftCfgIndex *pCfg) {
  char *serialized = raftCfgIndex2Str(pCfg);
S
Shengliang Guan 已提交
521
  sTrace("raftCfgIndexLog | len:%d | %s", (int32_t)strlen(serialized), serialized);
522 523 524 525 526
  taosMemoryFree(serialized);
}

void raftCfgIndexLog2(char *s, SRaftCfgIndex *pCfg) {
  char *serialized = raftCfgIndex2Str(pCfg);
S
Shengliang Guan 已提交
527
  sTrace("raftCfgIndexLog2 | len:%d | %s | %s", (int32_t)strlen(serialized), s, serialized);
528 529
  taosMemoryFree(serialized);
}