syncServer.c 12.3 KB
Newer Older
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * 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/>.
 */

//#define _DEFAULT_SOURCE
#include <stdint.h>
#include "os.h"
#include "tulog.h"
#include "tglobal.h"
#include "tsocket.h"
#include "trpc.h"
#include "tqueue.h"
#include "twal.h"
#include "tsync.h"

S
TD-1617  
Shengliang Guan 已提交
27 28 29 30 31
int       msgSize = 128;
int       commit = 0;
int       dataFd = -1;
void *    qhandle = NULL;
int       walNum = 0;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
32
uint64_t  tversion = 0;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
33
int64_t   syncHandle;
S
TD-1617  
Shengliang Guan 已提交
34 35 36 37
int       role;
int       nodeId;
char      path[256];
int       numOfWrites;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
38 39 40
SSyncInfo syncInfo;
SSyncCfg *pCfg;

S
TD-1617  
Shengliang Guan 已提交
41
int writeIntoWal(SWalHead *pHead) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
42
  if (dataFd < 0) {
S
TD-1617  
Shengliang Guan 已提交
43
    char walName[280];
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
44
    snprintf(walName, sizeof(walName), "%s/wal/wal.%d", path, walNum);
S
TD-1617  
Shengliang Guan 已提交
45 46 47
    (void)remove(walName);
    dataFd = open(walName, O_CREAT | O_WRONLY, S_IRWXU | S_IRWXG | S_IRWXO);
    if (dataFd < 0) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
      uInfo("failed to open wal file:%s(%s)", walName, strerror(errno));
      return -1;
    } else {
      walNum++;
      uInfo("file:%s is opened to write, walNum:%d", walName, walNum);
    }
  }

  if (write(dataFd, pHead, sizeof(SWalHead) + pHead->len) < 0) {
    uError("ver:%" PRIu64 ", failed to write wal file(%s)", pHead->version, strerror(errno));
  } else {
    uDebug("ver:%" PRIu64 ", written to wal", pHead->version);
  }

  numOfWrites++;
  if (numOfWrites >= 10000) {
    uInfo("%d request have been written into disk", numOfWrites);
    close(dataFd);
    dataFd = -1;
    numOfWrites = 0;
  }
S
TD-1617  
Shengliang Guan 已提交
69

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
70 71 72
  return 0;
}

73
void confirmForward(int32_t vgId, void *mhandle, int32_t code) {
S
TD-1617  
Shengliang Guan 已提交
74
  SRpcMsg * pMsg = (SRpcMsg *)mhandle;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
75 76 77 78 79 80
  SWalHead *pHead = (SWalHead *)(((char *)pMsg->pCont) - sizeof(SWalHead));

  uDebug("ver:%" PRIu64 ", confirm is received", pHead->version);

  rpcFreeCont(pMsg->pCont);

S
TD-1617  
Shengliang Guan 已提交
81
  SRpcMsg rpcMsg = {0};
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
82 83 84 85 86 87
  rpcMsg.pCont = rpcMallocCont(msgSize);
  rpcMsg.contLen = msgSize;
  rpcMsg.handle = pMsg->handle;
  rpcMsg.code = code;
  rpcSendResponse(&rpcMsg);

S
TD-1617  
Shengliang Guan 已提交
88
  taosFreeQitem(mhandle);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
89 90 91
}

int processRpcMsg(void *item) {
S
TD-1617  
Shengliang Guan 已提交
92 93 94
  SRpcMsg * pMsg = (SRpcMsg *)item;
  SWalHead *pHead = (SWalHead *)(((char *)pMsg->pCont) - sizeof(SWalHead));
  int       code = -1;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
95 96 97 98 99 100 101 102

  if (role != TAOS_SYNC_ROLE_MASTER) {
    uError("not master, write failed, role:%s", syncRole[role]);
  } else {
    pHead->version = ++tversion;
    pHead->msgType = pMsg->msgType;
    pHead->len = pMsg->contLen;

S
TD-2428  
Shengliang Guan 已提交
103
    uDebug("ver:%" PRIu64 ", rsp from client processed", pHead->version);
S
TD-1617  
Shengliang Guan 已提交
104
    writeIntoWal(pHead);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
105 106 107 108 109
    syncForwardToPeer(syncHandle, pHead, item, TAOS_QTYPE_RPC);

    code = 0;
  }

S
TD-1617  
Shengliang Guan 已提交
110
  if (pCfg->quorum <= 1) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
111 112
    rpcFreeCont(pMsg->pCont);

S
TD-1617  
Shengliang Guan 已提交
113
    SRpcMsg rpcMsg = {0};
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
114 115 116 117 118
    rpcMsg.pCont = rpcMallocCont(msgSize);
    rpcMsg.contLen = msgSize;
    rpcMsg.handle = pMsg->handle;
    rpcMsg.code = code;
    rpcSendResponse(&rpcMsg);
S
TD-1617  
Shengliang Guan 已提交
119
    taosFreeQitem(item);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
  }

  return code;
}

int processFwdMsg(void *item) {
  SWalHead *pHead = (SWalHead *)item;

  if (pHead->version <= tversion) {
    uError("ver:%" PRIu64 ", forward is even lower than local:%" PRIu64, pHead->version, tversion);
    return -1;
  }

  uDebug("ver:%" PRIu64 ", forward from peer is received", pHead->version);
  writeIntoWal(pHead);
  tversion = pHead->version;

  if (pCfg->quorum > 1) syncConfirmForward(syncHandle, pHead->version, 0);

  // write into cache

S
TD-1617  
Shengliang Guan 已提交
141 142 143 144 145
  /*
    if (pHead->handle) {
      syncSendFwdAck(syncHandle, pHead->handle, 0);
    }
  */
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

  taosFreeQitem(item);

  return 0;
}

int processWalMsg(void *item) {
  SWalHead *pHead = (SWalHead *)item;

  if (pHead->version <= tversion) {
    uError("ver:%" PRIu64 ", wal is even lower than local:%" PRIu64, pHead->version, tversion);
    return -1;
  };

  uDebug("ver:%" PRIu64 ", wal from peer is received", pHead->version);
  writeIntoWal(pHead);
  tversion = pHead->version;

  // write into cache

S
TD-1617  
Shengliang Guan 已提交
166 167 168 169 170
  /*
    if (pHead->handle) {
      syncSendFwdAck(syncHandle, pHead->handle, 0);
    }
  */
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
171 172 173 174 175 176 177

  taosFreeQitem(item);

  return 0;
}

void *processWriteQueue(void *param) {
S
TD-1617  
Shengliang Guan 已提交
178 179
  int   type;
  void *item;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
180

181 182
  setThreadName("writeQ");

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
183 184 185 186 187
  while (1) {
    int ret = taosReadQitem(qhandle, &type, &item);
    if (ret <= 0) {
      usleep(1000);
      continue;
S
TD-1617  
Shengliang Guan 已提交
188
    }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
189 190 191 192 193 194 195

    if (type == TAOS_QTYPE_RPC) {
      processRpcMsg(item);
    } else if (type == TAOS_QTYPE_WAL) {
      processWalMsg(item);
    } else if (type == TAOS_QTYPE_FWD) {
      processFwdMsg(item);
S
TD-1617  
Shengliang Guan 已提交
196
    }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
  }

  return NULL;
}

int retrieveAuthInfo(char *meterId, char *spi, char *encrypt, char *secret, char *ckey) {
  // app shall retrieve the auth info based on meterID from DB or a data file
  // demo code here only for simple demo
  int ret = 0;

  if (strcmp(meterId, "michael") == 0) {
    *spi = 1;
    *encrypt = 0;
    strcpy(secret, "mypassword");
    strcpy(ckey, "key");
  } else if (strcmp(meterId, "jeff") == 0) {
    *spi = 0;
    *encrypt = 0;
  } else {
    ret = -1;  // user not there
  }

  return ret;
}

void processRequestMsg(SRpcMsg *pMsg, SRpcEpSet *pEpSet) {
  SRpcMsg *pTemp;

  pTemp = taosAllocateQitem(sizeof(SRpcMsg));
  memcpy(pTemp, pMsg, sizeof(SRpcMsg));
S
TD-1617  
Shengliang Guan 已提交
227

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
228
  uDebug("request is received, type:%d, len:%d", pMsg->msgType, pMsg->contLen);
S
TD-1617  
Shengliang Guan 已提交
229
  taosWriteQitem(qhandle, TAOS_QTYPE_RPC, pTemp);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
230 231
}

232
uint32_t getFileInfo(int32_t vgId, char *name, uint32_t *index, uint32_t eindex, int64_t *size, uint64_t *fversion) {
S
TD-1617  
Shengliang Guan 已提交
233 234 235
  uint32_t    magic;
  struct stat fstat;
  char        aname[280];
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
236 237 238 239 240 241 242

  if (*index == 2) {
    uInfo("wait for a while .....");
    sleep(3);
  }

  if (name[0] == 0) {
S
TD-1617  
Shengliang Guan 已提交
243
    // find the file
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
244
    snprintf(aname, sizeof(aname), "%s/data/data.%d", path, *index);
S
TD-1617  
Shengliang Guan 已提交
245
    sprintf(name, "data/data.%d", *index);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
246 247 248 249 250
  } else {
    snprintf(aname, sizeof(aname), "%s/%s", path, name);
  }

  uInfo("get file info:%s", aname);
S
TD-1617  
Shengliang Guan 已提交
251
  if (stat(aname, &fstat) < 0) return 0;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
252 253 254 255 256 257 258

  *size = fstat.st_size;
  magic = fstat.st_size;

  return magic;
}

259
int getWalInfo(int32_t vgId, char *name, int64_t *index) {
S
TD-1617  
Shengliang Guan 已提交
260 261
  struct stat fstat;
  char        aname[280];
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
262 263

  name[0] = 0;
S
TD-1617  
Shengliang Guan 已提交
264
  if (*index + 1 > walNum) return 0;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
265 266

  snprintf(aname, sizeof(aname), "%s/wal/wal.%d", path, *index);
S
TD-1617  
Shengliang Guan 已提交
267
  sprintf(name, "wal/wal.%d", *index);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
268 269
  uInfo("get wal info:%s", aname);

S
TD-1617  
Shengliang Guan 已提交
270
  if (stat(aname, &fstat) < 0) return -1;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
271

S
TD-1617  
Shengliang Guan 已提交
272
  if (*index >= walNum - 1) return 0;  // no more
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
273 274 275 276

  return 1;
}

277
int writeToCache(int32_t vgId, void *data, int type) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
278 279
  SWalHead *pHead = data;

S
TD-2428  
Shengliang Guan 已提交
280
  uDebug("rsp from peer is received, ver:%" PRIu64 " len:%d type:%d", pHead->version, pHead->len, type);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
281 282 283 284

  int   msgSize = pHead->len + sizeof(SWalHead);
  void *pMsg = taosAllocateQitem(msgSize);
  memcpy(pMsg, pHead, msgSize);
S
TD-1617  
Shengliang Guan 已提交
285
  taosWriteQitem(qhandle, type, pMsg);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
286 287 288 289

  return 0;
}

290
void confirmFwd(int32_t vgId, int64_t version) { return; }
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
291

292
void notifyRole(int32_t vgId, int8_t r) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
293 294 295 296 297 298 299 300
  role = r;
  printf("current role:%s\n", syncRole[role]);
}

void initSync() {
  pCfg->replica = 1;
  pCfg->quorum = 1;
  syncInfo.vgId = 1;
S
TD-2798  
Shengliang Guan 已提交
301 302
  syncInfo.getWalInfoFp = getWalInfo;
  syncInfo.writeToCacheFp = writeToCache;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
303
  syncInfo.confirmForward = confirmForward;
S
TD-2798  
Shengliang Guan 已提交
304
  syncInfo.notifyRoleFp = notifyRole;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326

  pCfg->nodeInfo[0].nodeId = 1;
  pCfg->nodeInfo[0].nodePort = 7010;
  taosGetFqdn(pCfg->nodeInfo[0].nodeFqdn);

  pCfg->nodeInfo[1].nodeId = 2;
  pCfg->nodeInfo[1].nodePort = 7110;
  taosGetFqdn(pCfg->nodeInfo[1].nodeFqdn);

  pCfg->nodeInfo[2].nodeId = 3;
  pCfg->nodeInfo[2].nodePort = 7210;
  taosGetFqdn(pCfg->nodeInfo[2].nodeFqdn);

  pCfg->nodeInfo[3].nodeId = 4;
  pCfg->nodeInfo[3].nodePort = 7310;
  taosGetFqdn(pCfg->nodeInfo[3].nodeFqdn);

  pCfg->nodeInfo[4].nodeId = 5;
  pCfg->nodeInfo[4].nodePort = 7410;
  taosGetFqdn(pCfg->nodeInfo[4].nodeFqdn);
}

S
TD-1617  
Shengliang Guan 已提交
327 328 329
void doSync() {
  for (int i = 0; i < 5; ++i) {
    if (tsSyncPort == pCfg->nodeInfo[i].nodePort) nodeId = pCfg->nodeInfo[i].nodeId;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
330 331 332
  }

  snprintf(path, sizeof(path), "/root/test/d%d", nodeId);
S
TD-1617  
Shengliang Guan 已提交
333
  tstrncpy(syncInfo.path, path, sizeof(syncInfo.path));
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
334

S
TD-1617  
Shengliang Guan 已提交
335 336
  if (syncHandle == NULL) {
    syncHandle = syncStart(&syncInfo);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
337
  } else {
S
TD-1617  
Shengliang Guan 已提交
338
    if (syncReconfig(syncHandle, pCfg) < 0) syncHandle = NULL;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
339 340 341 342 343 344 345 346
  }

  uInfo("nodeId:%d path:%s syncPort:%d", nodeId, path, tsSyncPort);
}

int main(int argc, char *argv[]) {
  SRpcInit rpcInit;
  char     dataName[20] = "server.data";
S
TD-1617  
Shengliang Guan 已提交
347
  pCfg = &syncInfo.syncCfg;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
348 349 350 351

  initSync();

  memset(&rpcInit, 0, sizeof(rpcInit));
S
TD-1617  
Shengliang Guan 已提交
352 353
  rpcInit.localPort = 7000;
  rpcInit.label = "SER";
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
354
  rpcInit.numOfThreads = 1;
S
TD-1617  
Shengliang Guan 已提交
355 356 357 358
  rpcInit.cfp = processRequestMsg;
  rpcInit.sessions = 1000;
  rpcInit.idleTime = tsShellActivityTimer * 1500;
  rpcInit.afp = retrieveAuthInfo;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
359

S
TD-1617  
Shengliang Guan 已提交
360 361
  for (int i = 1; i < argc; ++i) {
    if (strcmp(argv[i], "-p") == 0 && i < argc - 1) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
362
      rpcInit.localPort = atoi(argv[++i]);
S
TD-1617  
Shengliang Guan 已提交
363
    } else if (strcmp(argv[i], "-t") == 0 && i < argc - 1) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
364
      rpcInit.numOfThreads = atoi(argv[++i]);
S
TD-1617  
Shengliang Guan 已提交
365
    } else if (strcmp(argv[i], "-m") == 0 && i < argc - 1) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
366
      msgSize = atoi(argv[++i]);
S
TD-1617  
Shengliang Guan 已提交
367
    } else if (strcmp(argv[i], "-s") == 0 && i < argc - 1) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
368
      rpcInit.sessions = atoi(argv[++i]);
S
TD-1617  
Shengliang Guan 已提交
369
    } else if (strcmp(argv[i], "-o") == 0 && i < argc - 1) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
370
      tsCompressMsgSize = atoi(argv[++i]);
S
TD-1617  
Shengliang Guan 已提交
371
    } else if (strcmp(argv[i], "-w") == 0 && i < argc - 1) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
372
      commit = atoi(argv[++i]);
S
TD-1617  
Shengliang Guan 已提交
373
    } else if (strcmp(argv[i], "-v") == 0 && i < argc - 1) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
374
      syncInfo.version = atoi(argv[++i]);
S
TD-1617  
Shengliang Guan 已提交
375
    } else if (strcmp(argv[i], "-r") == 0 && i < argc - 1) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
376
      pCfg->replica = atoi(argv[++i]);
S
TD-1617  
Shengliang Guan 已提交
377
    } else if (strcmp(argv[i], "-q") == 0 && i < argc - 1) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
378
      pCfg->quorum = atoi(argv[++i]);
S
TD-1617  
Shengliang Guan 已提交
379
    } else if (strcmp(argv[i], "-d") == 0 && i < argc - 1) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
380 381 382 383 384 385 386 387 388
      rpcDebugFlag = atoi(argv[++i]);
    } else {
      printf("\nusage: %s [options] \n", argv[0]);
      printf("  [-p port]: server port number, default is:%d\n", rpcInit.localPort);
      printf("  [-t threads]: number of rpc threads, default is:%d\n", rpcInit.numOfThreads);
      printf("  [-s sessions]: number of sessions, default is:%d\n", rpcInit.sessions);
      printf("  [-m msgSize]: message body size, default is:%d\n", msgSize);
      printf("  [-o compSize]: compression message size, default is:%d\n", tsCompressMsgSize);
      printf("  [-w write]: write received data to file(0, 1, 2), default is:%d\n", commit);
S
TD-1530  
Shengliang Guan 已提交
389
      printf("  [-v version]: initial node version, default is:%" PRId64 "\n", syncInfo.version);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
390 391 392 393 394 395 396
      printf("  [-r replica]: replicacation number, default is:%d\n", pCfg->replica);
      printf("  [-q quorum]: quorum, default is:%d\n", pCfg->quorum);
      printf("  [-d debugFlag]: debug flag, default:%d\n", rpcDebugFlag);
      printf("  [-h help]: print out this help\n\n");
      exit(0);
    }
  }
S
TD-1617  
Shengliang Guan 已提交
397

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
398
  uDebugFlag = rpcDebugFlag;
S
TD-1617  
Shengliang Guan 已提交
399 400
  dDebugFlag = rpcDebugFlag;
  // tmrDebugFlag = rpcDebugFlag;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
  tsAsyncLog = 0;
  taosInitLog("server.log", 1000000, 10);

  rpcInit.connType = TAOS_CONN_SERVER;
  void *pRpc = rpcOpen(&rpcInit);
  if (pRpc == NULL) {
    uError("failed to start RPC server");
    return -1;
  }

  tsSyncPort = rpcInit.localPort + 10;
  qhandle = taosOpenQueue();

  doSync();

  pthread_attr_t thattr;
  pthread_t      thread;
  pthread_attr_init(&thattr);
  pthread_attr_setdetachstate(&thattr, PTHREAD_CREATE_JOINABLE);
  if (pthread_create(&thread, &thattr, processWriteQueue, NULL) != 0) {
    uError("failed to create thread, reason:%s", strerror(errno));
    return -1;
  }

  printf("server is running, localPort:%d\n", rpcInit.localPort);
  SNodesRole nroles;

  while (1) {
S
TD-1617  
Shengliang Guan 已提交
429
    int c = getchar();
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
430

S
TD-1617  
Shengliang Guan 已提交
431
    switch (c) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
432
      case '1':
S
TD-1617  
Shengliang Guan 已提交
433 434 435
        pCfg->replica = 1;
        doSync();
        break;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
436
      case '2':
S
TD-1617  
Shengliang Guan 已提交
437 438
        pCfg->replica = 2;
        doSync();
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
439 440
        break;
      case '3':
S
TD-1617  
Shengliang Guan 已提交
441 442
        pCfg->replica = 3;
        doSync();
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
443 444
        break;
      case '4':
S
TD-1617  
Shengliang Guan 已提交
445 446
        pCfg->replica = 4;
        doSync();
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
447 448
        break;
      case '5':
S
TD-1617  
Shengliang Guan 已提交
449 450
        pCfg->replica = 5;
        doSync();
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
451 452 453
        break;
      case 's':
        syncGetNodesRole(syncHandle, &nroles);
S
TD-1617  
Shengliang Guan 已提交
454
        for (int i = 0; i < pCfg->replica; ++i)
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
455 456 457 458 459 460
          printf("=== nodeId:%d role:%s\n", nroles.nodeId[i], syncRole[nroles.role[i]]);
        break;
      default:
        break;
    }

S
TD-1617  
Shengliang Guan 已提交
461
    if (c == 'q') break;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
462 463 464 465 466 467 468 469 470 471 472
  }

  syncStop(syncHandle);

  if (dataFd >= 0) {
    close(dataFd);
    remove(dataName);
  }

  return 0;
}