syncElectTest.cpp 3.5 KB
Newer Older
M
Minghao Li 已提交
1 2 3 4 5 6
#include <gtest/gtest.h>
#include <stdio.h>
#include "syncEnv.h"
#include "syncIO.h"
#include "syncInt.h"
#include "syncUtil.h"
7
#include "wal.h"
M
Minghao Li 已提交
8 9 10 11 12 13 14 15 16 17

void logTest() {
  sTrace("--- sync log test: trace");
  sDebug("--- sync log test: debug");
  sInfo("--- sync log test: info");
  sWarn("--- sync log test: warn");
  sError("--- sync log test: error");
  sFatal("--- sync log test: fatal");
}

18 19 20
uint16_t    gPorts[] = {7010, 7110, 7210, 7310, 7410};
const char* gDir = "./syncElectTest";
int32_t     gVgId = 1234;
M
Minghao Li 已提交
21

22
void init() {
M
Minghao Li 已提交
23 24
  int code = walInit();
  assert(code == 0);
25 26 27 28 29
}

void cleanup() { walCleanUp(); }

SWal* createWal(char* path, int32_t vgId) {
M
Minghao Li 已提交
30 31
  SWalCfg walCfg;
  memset(&walCfg, 0, sizeof(SWalCfg));
32
  walCfg.vgId = vgId;
M
Minghao Li 已提交
33 34 35 36 37 38
  walCfg.fsyncPeriod = 1000;
  walCfg.retentionPeriod = 1000;
  walCfg.rollPeriod = 1000;
  walCfg.retentionSize = 1000;
  walCfg.segSize = 1000;
  walCfg.level = TAOS_WAL_FSYNC;
39
  SWal* pWal = walOpen(path, &walCfg);
M
Minghao Li 已提交
40
  assert(pWal != NULL);
41 42
  return pWal;
}
M
Minghao Li 已提交
43

44 45 46 47 48 49 50 51 52
SSyncNode* createSyncNode(int32_t replicaNum, int32_t myIndex, int32_t vgId, SWal* pWal, char* path) {
  SSyncInfo syncInfo;
  syncInfo.vgId = vgId;
  syncInfo.rpcClient = gSyncIO->clientRpc;
  syncInfo.FpSendMsg = syncIOSendMsg;
  syncInfo.queue = gSyncIO->pMsgQ;
  syncInfo.FpEqMsg = syncIOEqMsg;
  syncInfo.pFsm = NULL;
  snprintf(syncInfo.path, sizeof(syncInfo.path), "%s_sync_replica%d_index%d", path, replicaNum, myIndex);
M
Minghao Li 已提交
53 54 55 56 57 58 59
  syncInfo.pWal = pWal;

  SSyncCfg* pCfg = &syncInfo.syncCfg;
  pCfg->myIndex = myIndex;
  pCfg->replicaNum = replicaNum;

  for (int i = 0; i < replicaNum; ++i) {
60 61 62
    pCfg->nodeInfo[i].nodePort = gPorts[i];
    taosGetFqdn(pCfg->nodeInfo[i].nodeFqdn);
    // snprintf(pCfg->nodeInfo[i].nodeFqdn, sizeof(pCfg->nodeInfo[i].nodeFqdn), "%s", "127.0.0.1");
M
Minghao Li 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
  }

  SSyncNode* pSyncNode = syncNodeOpen(&syncInfo);
  assert(pSyncNode != NULL);

  gSyncIO->FpOnSyncPing = pSyncNode->FpOnPing;
  gSyncIO->FpOnSyncPingReply = pSyncNode->FpOnPingReply;
  gSyncIO->FpOnSyncRequestVote = pSyncNode->FpOnRequestVote;
  gSyncIO->FpOnSyncRequestVoteReply = pSyncNode->FpOnRequestVoteReply;
  gSyncIO->FpOnSyncAppendEntries = pSyncNode->FpOnAppendEntries;
  gSyncIO->FpOnSyncAppendEntriesReply = pSyncNode->FpOnAppendEntriesReply;
  gSyncIO->FpOnSyncPing = pSyncNode->FpOnPing;
  gSyncIO->FpOnSyncPingReply = pSyncNode->FpOnPingReply;
  gSyncIO->FpOnSyncTimeout = pSyncNode->FpOnTimeout;
  gSyncIO->pSyncNode = pSyncNode;

M
Minghao Li 已提交
79 80
  syncNodeStart(pSyncNode);

M
Minghao Li 已提交
81 82 83
  return pSyncNode;
}

84
void usage(char* exe) { printf("usage: %s replicaNum myIndex \n", exe); }
M
Minghao Li 已提交
85 86 87

int main(int argc, char** argv) {
  tsAsyncLog = 0;
88 89 90 91
  sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE;
  if (argc != 3) {
    usage(argv[0]);
    exit(-1);
M
Minghao Li 已提交
92
  }
93 94
  int32_t replicaNum = atoi(argv[1]);
  int32_t myIndex = atoi(argv[2]);
M
Minghao Li 已提交
95

96 97
  assert(replicaNum >= 1 && replicaNum <= 5);
  assert(myIndex >= 0 && myIndex < replicaNum);
M
Minghao Li 已提交
98

99 100 101
  init();
  int32_t ret = syncIOStart((char*)"127.0.0.1", gPorts[myIndex]);
  assert(ret == 0);
M
Minghao Li 已提交
102 103 104
  ret = syncEnvStart();
  assert(ret == 0);

105 106 107
  char walPath[128];
  snprintf(walPath, sizeof(walPath), "%s_wal_replica%d_index%d", gDir, replicaNum, myIndex);
  SWal* pWal = createWal(walPath, gVgId);
M
Minghao Li 已提交
108

109 110 111
  SSyncNode* pSyncNode = createSyncNode(replicaNum, myIndex, gVgId, pWal, (char*)gDir);
  assert(pSyncNode != NULL);
  syncNodeLog2((char*)"==syncElectTest==", pSyncNode);
M
Minghao Li 已提交
112 113 114

  //---------------------------
  while (1) {
115
    char* s = syncNode2SimpleStr(pSyncNode);
M
Minghao Li 已提交
116 117
    sTrace("%s", s);
    taosMemoryFree(s);
M
Minghao Li 已提交
118
    taosMsleep(1000);
M
Minghao Li 已提交
119 120
  }

121 122 123 124
  syncNodeClose(pSyncNode);
  walClose(pWal);
  syncIOStop();
  cleanup();
M
Minghao Li 已提交
125 126
  return 0;
}