syncReplicateTest.cpp 6.8 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 21
uint16_t    gPorts[] = {7010, 7110, 7210, 7310, 7410};
const char* gDir = "./syncReplicateTest";
int32_t     gVgId = 1234;
SyncIndex   gSnapshotLastApplyIndex;
M
Minghao Li 已提交
22

23 24 25 26 27 28 29 30 31
void init() {
  int code = walInit();
  assert(code == 0);

  code = syncInit();
  assert(code == 0);
}

void cleanup() { walCleanUp(); }
M
Minghao Li 已提交
32

M
Minghao Li 已提交
33
void CommitCb(struct SSyncFSM* pFsm, const SRpcMsg* pMsg, SFsmCbMeta cbMeta) {
34
  SyncIndex beginIndex = SYNC_INDEX_INVALID;
35
  if (pFsm->FpGetSnapshotInfo != NULL) {
36
    SSnapshot snapshot;
37
    pFsm->FpGetSnapshotInfo(pFsm, &snapshot);
38 39 40 41 42
    beginIndex = snapshot.lastApplyIndex;
  }

  if (cbMeta.index > beginIndex) {
    char logBuf[256];
43 44 45
    snprintf(logBuf, sizeof(logBuf),
             "==callback== ==CommitCb== pFsm:%p, index:%" PRId64 ", isWeak:%d, code:%d, state:%d %s \n", pFsm,
             cbMeta.index, cbMeta.isWeak, cbMeta.code, cbMeta.state, syncUtilState2String(cbMeta.state));
M
Minghao Li 已提交
46
    syncRpcMsgLog2(logBuf, (SRpcMsg*)pMsg);
47
  } else {
S
Shengliang Guan 已提交
48
    sTrace("==callback== ==CommitCb== do not apply again %" PRId64, cbMeta.index);
49
  }
M
Minghao Li 已提交
50 51
}

52
void PreCommitCb(struct SSyncFSM* pFsm, const SRpcMsg* pMsg, SFsmCbMeta cbMeta) {
M
Minghao Li 已提交
53 54
  char logBuf[256];
  snprintf(logBuf, sizeof(logBuf),
55 56
           "==callback== ==PreCommitCb== pFsm:%p, index:%" PRId64 ", isWeak:%d, code:%d, state:%d %s \n", pFsm,
           cbMeta.index, cbMeta.isWeak, cbMeta.code, cbMeta.state, syncUtilState2String(cbMeta.state));
57
  syncRpcMsgLog2(logBuf, (SRpcMsg*)pMsg);
M
Minghao Li 已提交
58 59
}

60
void RollBackCb(struct SSyncFSM* pFsm, const SRpcMsg* pMsg, SFsmCbMeta cbMeta) {
M
Minghao Li 已提交
61
  char logBuf[256];
62 63 64
  snprintf(logBuf, sizeof(logBuf),
           "==callback== ==RollBackCb== pFsm:%p, index:%" PRId64 ", isWeak:%d, code:%d, state:%d %s \n", pFsm,
           cbMeta.index, cbMeta.isWeak, cbMeta.code, cbMeta.state, syncUtilState2String(cbMeta.state));
65 66 67 68 69 70 71 72
  syncRpcMsgLog2(logBuf, (SRpcMsg*)pMsg);
}

int32_t GetSnapshotCb(struct SSyncFSM* pFsm, SSnapshot* pSnapshot) {
  pSnapshot->data = NULL;
  pSnapshot->lastApplyIndex = gSnapshotLastApplyIndex;
  pSnapshot->lastApplyTerm = 100;
  return 0;
M
Minghao Li 已提交
73 74
}

75 76
SSyncFSM* createFsm() {
  SSyncFSM* pFsm = (SSyncFSM*)taosMemoryMalloc(sizeof(SSyncFSM));
M
Minghao Li 已提交
77 78 79
  pFsm->FpCommitCb = CommitCb;
  pFsm->FpPreCommitCb = PreCommitCb;
  pFsm->FpRollBackCb = RollBackCb;
80
  pFsm->FpGetSnapshotInfo = GetSnapshotCb;
81
  return pFsm;
M
Minghao Li 已提交
82 83
}

84
SWal* createWal(char* path, int32_t vgId) {
M
Minghao Li 已提交
85 86
  SWalCfg walCfg;
  memset(&walCfg, 0, sizeof(SWalCfg));
87
  walCfg.vgId = vgId;
M
Minghao Li 已提交
88 89 90 91 92 93
  walCfg.fsyncPeriod = 1000;
  walCfg.retentionPeriod = 1000;
  walCfg.rollPeriod = 1000;
  walCfg.retentionSize = 1000;
  walCfg.segSize = 1000;
  walCfg.level = TAOS_WAL_FSYNC;
94
  SWal* pWal = walOpen(path, &walCfg);
M
Minghao Li 已提交
95
  assert(pWal != NULL);
96 97
  return pWal;
}
M
Minghao Li 已提交
98

99 100 101
int64_t createSyncNode(int32_t replicaNum, int32_t myIndex, int32_t vgId, SWal* pWal, char* path) {
  SSyncInfo syncInfo;
  syncInfo.vgId = vgId;
S
Shengliang Guan 已提交
102
  syncInfo.msgcb = &gSyncIO->msgcb;
103 104 105 106
  syncInfo.FpSendMsg = syncIOSendMsg;
  syncInfo.FpEqMsg = syncIOEqMsg;
  syncInfo.pFsm = createFsm();
  snprintf(syncInfo.path, sizeof(syncInfo.path), "%s_sync_replica%d_index%d", path, replicaNum, myIndex);
M
Minghao Li 已提交
107 108
  syncInfo.pWal = pWal;

109
  SSyncCfg* pCfg = &syncInfo.syncCfg;
M
Minghao Li 已提交
110 111 112 113
  pCfg->myIndex = myIndex;
  pCfg->replicaNum = replicaNum;

  for (int i = 0; i < replicaNum; ++i) {
114 115 116
    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 已提交
117 118
  }

119 120 121 122
  int64_t rid = syncOpen(&syncInfo);
  assert(rid > 0);

  SSyncNode* pSyncNode = (SSyncNode*)syncNodeAcquire(rid);
M
Minghao Li 已提交
123 124 125 126 127 128 129
  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;
130 131
  gSyncIO->FpOnSyncPing = pSyncNode->FpOnPing;
  gSyncIO->FpOnSyncPingReply = pSyncNode->FpOnPingReply;
M
Minghao Li 已提交
132
  gSyncIO->FpOnSyncTimeout = pSyncNode->FpOnTimeout;
M
Minghao Li 已提交
133
  gSyncIO->FpOnSyncClientRequest = pSyncNode->FpOnClientRequest;
M
Minghao Li 已提交
134
  gSyncIO->pSyncNode = pSyncNode;
M
Minghao Li 已提交
135
  syncNodeRelease(pSyncNode);
M
Minghao Li 已提交
136

137
  return rid;
M
Minghao Li 已提交
138 139
}

140 141
void usage(char* exe) { printf("usage: %s replicaNum myIndex lastApplyIndex writeRecordNum \n", exe); }

M
Minghao Li 已提交
142 143
SRpcMsg* createRpcMsg(int i, int count, int myIndex) {
  SRpcMsg* pMsg = (SRpcMsg*)taosMemoryMalloc(sizeof(SRpcMsg));
M
Minghao Li 已提交
144 145
  memset(pMsg, 0, sizeof(SRpcMsg));
  pMsg->msgType = 9999;
M
Minghao Li 已提交
146 147
  pMsg->contLen = 256;
  pMsg->pCont = rpcMallocCont(pMsg->contLen);
M
Minghao Li 已提交
148
  snprintf((char*)(pMsg->pCont), pMsg->contLen, "value-myIndex:%u-%d-%d-%" PRId64, myIndex, i, count,
149
           taosGetTimestampMs());
M
Minghao Li 已提交
150 151 152
  return pMsg;
}

153
int main(int argc, char** argv) {
M
Minghao Li 已提交
154
  tsAsyncLog = 0;
155 156 157 158
  sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE;
  if (argc != 5) {
    usage(argv[0]);
    exit(-1);
M
Minghao Li 已提交
159
  }
160 161 162 163 164 165 166 167 168 169 170 171 172
  int32_t replicaNum = atoi(argv[1]);
  int32_t myIndex = atoi(argv[2]);
  int32_t lastApplyIndex = atoi(argv[3]);
  int32_t writeRecordNum = atoi(argv[4]);
  gSnapshotLastApplyIndex = lastApplyIndex;

  assert(replicaNum >= 1 && replicaNum <= 5);
  assert(myIndex >= 0 && myIndex < replicaNum);
  assert(lastApplyIndex >= -1);
  assert(writeRecordNum >= 0);

  init();
  int32_t ret = syncIOStart((char*)"127.0.0.1", gPorts[myIndex]);
M
Minghao Li 已提交
173 174
  assert(ret == 0);

175 176 177
  char walPath[128];
  snprintf(walPath, sizeof(walPath), "%s_wal_replica%d_index%d", gDir, replicaNum, myIndex);
  SWal* pWal = createWal(walPath, gVgId);
M
Minghao Li 已提交
178

179 180 181
  int64_t rid = createSyncNode(replicaNum, myIndex, gVgId, pWal, (char*)gDir);
  assert(rid > 0);
  syncStart(rid);
M
Minghao Li 已提交
182

M
Minghao Li 已提交
183
  SSyncNode* pSyncNode = (SSyncNode*)syncNodeAcquire(rid);
184
  assert(pSyncNode != NULL);
M
Minghao Li 已提交
185

186 187 188 189 190 191
  //---------------------------
  int32_t alreadySend = 0;
  while (1) {
    char* s = syncNode2SimpleStr(pSyncNode);

    if (alreadySend < writeRecordNum) {
M
Minghao Li 已提交
192 193
      SRpcMsg* pRpcMsg = createRpcMsg(alreadySend, writeRecordNum, myIndex);
      int32_t  ret = syncPropose(rid, pRpcMsg, false);
M
Minghao Li 已提交
194
      if (ret == -1 && terrno == TSDB_CODE_SYN_NOT_LEADER) {
M
Minghao Li 已提交
195
        sTrace("%s value%d write not leader", s, alreadySend);
196 197 198 199 200 201
      } else {
        assert(ret == 0);
        sTrace("%s value%d write ok", s, alreadySend);
      }
      alreadySend++;

M
Minghao Li 已提交
202
      rpcFreeCont(pRpcMsg->pCont);
203
      taosMemoryFree(pRpcMsg);
M
Minghao Li 已提交
204 205
    } else {
      sTrace("%s", s);
206
    }
M
Minghao Li 已提交
207 208

    taosMsleep(1000);
209
    taosMemoryFree(s);
M
Minghao Li 已提交
210 211 212
    taosMsleep(1000);
  }

213 214 215 216 217
  syncNodeRelease(pSyncNode);
  syncStop(rid);
  walClose(pWal);
  syncIOStop();
  cleanup();
M
Minghao Li 已提交
218 219
  return 0;
}