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
SSyncNode* createSyncNode(int32_t replicaNum, int32_t myIndex, int32_t vgId, SWal* pWal, char* path) {
  SSyncInfo syncInfo;
  syncInfo.vgId = vgId;
S
Shengliang Guan 已提交
47
  syncInfo.msgcb = &gSyncIO->msgcb;
48 49 50 51
  syncInfo.FpSendMsg = syncIOSendMsg;
  syncInfo.FpEqMsg = syncIOEqMsg;
  syncInfo.pFsm = NULL;
  snprintf(syncInfo.path, sizeof(syncInfo.path), "%s_sync_replica%d_index%d", path, replicaNum, myIndex);
M
Minghao Li 已提交
52 53 54 55 56 57 58
  syncInfo.pWal = pWal;

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

  for (int i = 0; i < replicaNum; ++i) {
59 60 61
    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 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
  }

  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 已提交
78 79
  syncNodeStart(pSyncNode);

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

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

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

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

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

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

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

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

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