syncInitTest.cpp 2.5 KB
Newer Older
1
#include "syncTest.h"
M
Minghao Li 已提交
2 3 4 5 6 7 8 9 10 11

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");
}

M
Minghao Li 已提交
12 13 14
uint16_t ports[] = {7010, 7110, 7210, 7310, 7410};
int32_t  replicaNum = 5;
int32_t  myIndex = 0;
M
Minghao Li 已提交
15

M
Minghao Li 已提交
16 17 18
SRaftId   ids[TSDB_MAX_REPLICA];
SSyncInfo syncInfo;
SSyncFSM* pFsm;
M
Minghao Li 已提交
19

M
Minghao Li 已提交
20 21
SSyncNode* syncNodeInit() {
  syncInfo.vgId = 1234;
S
Shengliang Guan 已提交
22
  syncInfo.msgcb = &gSyncIO->msgcb;
S
Shengliang Guan 已提交
23 24
  syncInfo.syncSendMSg = syncIOSendMsg;
  syncInfo.syncEqMsg = syncIOEqMsg;
M
Minghao Li 已提交
25
  syncInfo.pFsm = pFsm;
M
Minghao Li 已提交
26
  snprintf(syncInfo.path, sizeof(syncInfo.path), "%s", "./sync_init_test");
M
Minghao Li 已提交
27 28

  SSyncCfg* pCfg = &syncInfo.syncCfg;
M
Minghao Li 已提交
29 30
  pCfg->myIndex = myIndex;
  pCfg->replicaNum = replicaNum;
M
Minghao Li 已提交
31

M
Minghao Li 已提交
32 33 34 35 36
  for (int i = 0; i < replicaNum; ++i) {
    pCfg->nodeInfo[i].nodePort = ports[i];
    snprintf(pCfg->nodeInfo[i].nodeFqdn, sizeof(pCfg->nodeInfo[i].nodeFqdn), "%s", "127.0.0.1");
    // taosGetFqdn(pCfg->nodeInfo[0].nodeFqdn);
  }
M
Minghao Li 已提交
37 38 39 40 41

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

  gSyncIO->FpOnSyncPing = pSyncNode->FpOnPing;
M
Minghao Li 已提交
42
  gSyncIO->FpOnSyncPingReply = pSyncNode->FpOnPingReply;
M
Minghao Li 已提交
43
  gSyncIO->FpOnSyncClientRequest = pSyncNode->FpOnClientRequest;
M
Minghao Li 已提交
44 45 46 47
  gSyncIO->FpOnSyncRequestVote = pSyncNode->FpOnRequestVote;
  gSyncIO->FpOnSyncRequestVoteReply = pSyncNode->FpOnRequestVoteReply;
  gSyncIO->FpOnSyncAppendEntries = pSyncNode->FpOnAppendEntries;
  gSyncIO->FpOnSyncAppendEntriesReply = pSyncNode->FpOnAppendEntriesReply;
M
Minghao Li 已提交
48
  gSyncIO->FpOnSyncTimeout = pSyncNode->FpOnTimeout;
M
Minghao Li 已提交
49 50 51 52 53
  gSyncIO->pSyncNode = pSyncNode;

  return pSyncNode;
}

M
Minghao Li 已提交
54 55 56 57 58 59
SSyncNode* syncInitTest() { return syncNodeInit(); }

void initRaftId(SSyncNode* pSyncNode) {
  for (int i = 0; i < replicaNum; ++i) {
    ids[i] = pSyncNode->replicasId[i];
    char* s = syncUtilRaftId2Str(&ids[i]);
M
Minghao Li 已提交
60
    sTrace("raftId[%d] : %s\n", i, s);
wafwerar's avatar
wafwerar 已提交
61
    taosMemoryFree(s);
M
Minghao Li 已提交
62 63 64 65
  }
}

int main(int argc, char** argv) {
M
Minghao Li 已提交
66
  // taosInitLog((char *)"tmp/syncInitTest.log", 100);
M
Minghao Li 已提交
67
  tsAsyncLog = 0;
M
Minghao Li 已提交
68
  sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE;
M
Minghao Li 已提交
69

M
Minghao Li 已提交
70 71 72 73 74 75 76 77
  myIndex = 0;
  if (argc >= 2) {
    myIndex = atoi(argv[1]);
  }

  int32_t ret = syncIOStart((char*)"127.0.0.1", ports[myIndex]);
  assert(ret == 0);

S
Shengliang Guan 已提交
78
  ret = syncInit();
M
Minghao Li 已提交
79 80 81 82 83
  assert(ret == 0);

  SSyncNode* pSyncNode = syncInitTest();
  assert(pSyncNode != NULL);

S
Shengliang Guan 已提交
84
  sNTrace(pSyncNode, "syncInitTest");
M
Minghao Li 已提交
85
  initRaftId(pSyncNode);
M
Minghao Li 已提交
86

M
Minghao Li 已提交
87
  syncNodeClose(pSyncNode);
S
Shengliang Guan 已提交
88
  syncCleanUp();
M
Minghao Li 已提交
89
  // syncIOStop();
M
Minghao Li 已提交
90

M
Minghao Li 已提交
91
  // taosCloseLog();
M
Minghao Li 已提交
92
  return 0;
M
Minghao Li 已提交
93
}