syncIndexMgrTest.cpp 2.9 KB
Newer Older
M
Minghao Li 已提交
1 2
#include "syncIndexMgr.h"
//#include <gtest/gtest.h>
M
Minghao Li 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#include <stdio.h>
#include "syncEnv.h"
#include "syncIO.h"
#include "syncInt.h"
#include "syncRaftStore.h"
#include "syncUtil.h"
#include "syncVoteMgr.h"

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

uint16_t ports[] = {7010, 7110, 7210, 7310, 7410};
int32_t  replicaNum = 3;
int32_t  myIndex = 0;

SRaftId    ids[TSDB_MAX_REPLICA];
SSyncNode* pSyncNode;

SSyncNode* syncNodeInit() {
M
Minghao Li 已提交
28 29 30
  pSyncNode = (SSyncNode*)taosMemoryMalloc(sizeof(SSyncNode));
  memset(pSyncNode, 0, sizeof(SSyncNode));
  pSyncNode->replicaNum = replicaNum;
M
Minghao Li 已提交
31
  for (int i = 0; i < replicaNum; ++i) {
M
Minghao Li 已提交
32 33
    pSyncNode->replicasId[i].addr = syncUtilAddr2U64("127.0.0.1", ports[i]);
    pSyncNode->replicasId[i].vgId = 1234;
M
Minghao Li 已提交
34

M
Minghao Li 已提交
35 36 37
    ids[i].addr = pSyncNode->replicasId[i].addr;
    ids[i].vgId = pSyncNode->replicasId[i].vgId;
  }
M
Minghao Li 已提交
38 39 40 41 42 43

  return pSyncNode;
}

int main(int argc, char** argv) {
  tsAsyncLog = 0;
M
Minghao Li 已提交
44
  sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE;
M
Minghao Li 已提交
45 46 47 48 49 50

  myIndex = 0;
  if (argc >= 2) {
    myIndex = atoi(argv[1]);
  }

M
Minghao Li 已提交
51
  SSyncNode* pSyncNode = syncNodeInit();
M
Minghao Li 已提交
52 53
  assert(pSyncNode != NULL);

M
Minghao Li 已提交
54
  printf("---------------------------------------\n");
M
Minghao Li 已提交
55 56
  SSyncIndexMgr* pSyncIndexMgr = syncIndexMgrCreate(pSyncNode);
  assert(pSyncIndexMgr != NULL);
M
Minghao Li 已提交
57
  {
M
Minghao Li 已提交
58
    char* serialized = syncIndexMgr2Str(pSyncIndexMgr);
M
Minghao Li 已提交
59 60
    assert(serialized != NULL);
    printf("%s\n", serialized);
wafwerar's avatar
wafwerar 已提交
61
    taosMemoryFree(serialized);
M
Minghao Li 已提交
62
  }
M
Minghao Li 已提交
63
  printf("---------------------------------------\n");
M
Minghao Li 已提交
64

M
Minghao Li 已提交
65
  printf("---------------------------------------\n");
M
Minghao Li 已提交
66 67 68
  syncIndexMgrSetIndex(pSyncIndexMgr, &ids[0], 100);
  syncIndexMgrSetIndex(pSyncIndexMgr, &ids[1], 200);
  syncIndexMgrSetIndex(pSyncIndexMgr, &ids[2], 300);
M
Minghao Li 已提交
69 70 71
  // syncIndexMgrSetTerm(pSyncIndexMgr, &ids[0], 700);
  // syncIndexMgrSetTerm(pSyncIndexMgr, &ids[1], 800);
  // syncIndexMgrSetTerm(pSyncIndexMgr, &ids[2], 900);
M
Minghao Li 已提交
72
  {
M
Minghao Li 已提交
73
    char* serialized = syncIndexMgr2Str(pSyncIndexMgr);
M
Minghao Li 已提交
74 75
    assert(serialized != NULL);
    printf("%s\n", serialized);
wafwerar's avatar
wafwerar 已提交
76
    taosMemoryFree(serialized);
M
Minghao Li 已提交
77
  }
M
Minghao Li 已提交
78
  printf("---------------------------------------\n");
M
Minghao Li 已提交
79

M
Minghao Li 已提交
80 81 82
  printf("---------------------------------------\n");
  for (int i = 0; i < pSyncIndexMgr->replicaNum; ++i) {
    SyncIndex idx = syncIndexMgrGetIndex(pSyncIndexMgr, &ids[i]);
M
Minghao Li 已提交
83
    // SyncTerm  term = syncIndexMgrGetTerm(pSyncIndexMgr, &ids[i]);
S
Shengliang Guan 已提交
84
    // printf("%d: index:%" PRId64 " term:%" PRIu64 " \n", i, idx, term);
M
Minghao Li 已提交
85
  }
M
Minghao Li 已提交
86
  printf("---------------------------------------\n");
M
Minghao Li 已提交
87 88

  printf("---------------------------------------\n");
M
Minghao Li 已提交
89
  syncIndexMgrClear(pSyncIndexMgr);
M
Minghao Li 已提交
90
  {
M
Minghao Li 已提交
91
    char* serialized = syncIndexMgr2Str(pSyncIndexMgr);
M
Minghao Li 已提交
92 93
    assert(serialized != NULL);
    printf("%s\n", serialized);
wafwerar's avatar
wafwerar 已提交
94
    taosMemoryFree(serialized);
M
Minghao Li 已提交
95
  }
M
Minghao Li 已提交
96
  printf("---------------------------------------\n");
M
Minghao Li 已提交
97

M
Minghao Li 已提交
98
  syncIndexMgrDestroy(pSyncIndexMgr);
M
Minghao Li 已提交
99 100
  return 0;
}