syncConfigChangeTest.cpp 8.3 KB
Newer Older
M
Minghao Li 已提交
1
#include <gtest/gtest.h>
2
#include "syncTest.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

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    gPorts[] = {7010, 7110, 7210, 7310, 7410};
const char* gDir = "./syncReplicateTest";
int32_t     gVgId = 1234;
SyncIndex   gSnapshotLastApplyIndex;

void init() {
  int code = walInit();
  assert(code == 0);

  code = syncInit();
  assert(code == 0);
M
Minghao Li 已提交
24 25

  sprintf(tsTempDir, "%s", ".");
M
Minghao Li 已提交
26 27 28 29
}

void cleanup() { walCleanUp(); }

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

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

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

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

M
Minghao Li 已提交
66
int32_t GetSnapshotCb(const struct SSyncFSM* pFsm, SSnapshot* pSnapshot) {
M
Minghao Li 已提交
67 68 69 70 71 72
  pSnapshot->data = NULL;
  pSnapshot->lastApplyIndex = gSnapshotLastApplyIndex;
  pSnapshot->lastApplyTerm = 100;
  return 0;
}

73
void RestoreFinishCb(struct SSyncFSM* pFsm) { sTrace("==callback== ==RestoreFinishCb=="); }
74

H
Hongze Cheng 已提交
75
void ReConfigCb(struct SSyncFSM* pFsm, const SRpcMsg* pMsg, SReConfigCbMeta* cbMeta) {
76 77
  sTrace("==callback== ==ReConfigCb== flag:%" PRIx64 ", index:%" PRId64 ", code:%d, currentTerm:%" PRIu64
         ", term:%" PRIu64,
S
Shengliang Guan 已提交
78
         cbMeta->flag, cbMeta->index, cbMeta->code, cbMeta->currentTerm, cbMeta->term);
M
Minghao Li 已提交
79 80
}

M
Minghao Li 已提交
81 82
SSyncFSM* createFsm() {
  SSyncFSM* pFsm = (SSyncFSM*)taosMemoryMalloc(sizeof(SSyncFSM));
83 84
  memset(pFsm, 0, sizeof(*pFsm));

M
Minghao Li 已提交
85
#if 0
M
Minghao Li 已提交
86 87 88
  pFsm->FpCommitCb = CommitCb;
  pFsm->FpPreCommitCb = PreCommitCb;
  pFsm->FpRollBackCb = RollBackCb;
M
Minghao Li 已提交
89

90
  pFsm->FpGetSnapshotInfo = GetSnapshotCb;
91
  pFsm->FpRestoreFinishCb = RestoreFinishCb;
92

M
Minghao Li 已提交
93
  pFsm->FpReConfigCb = ReConfigCb;
M
Minghao Li 已提交
94
#endif
M
Minghao Li 已提交
95

M
Minghao Li 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
  return pFsm;
}

SWal* createWal(char* path, int32_t vgId) {
  SWalCfg walCfg;
  memset(&walCfg, 0, sizeof(SWalCfg));
  walCfg.vgId = vgId;
  walCfg.fsyncPeriod = 1000;
  walCfg.retentionPeriod = 1000;
  walCfg.rollPeriod = 1000;
  walCfg.retentionSize = 1000;
  walCfg.segSize = 1000;
  walCfg.level = TAOS_WAL_FSYNC;
  SWal* pWal = walOpen(path, &walCfg);
  assert(pWal != NULL);
  return pWal;
}

M
Minghao Li 已提交
114
int64_t createSyncNode(int32_t replicaNum, int32_t myIndex, int32_t vgId, SWal* pWal, char* path, bool isStandBy) {
M
Minghao Li 已提交
115 116
  SSyncInfo syncInfo;
  syncInfo.vgId = vgId;
S
Shengliang Guan 已提交
117
  syncInfo.msgcb = &gSyncIO->msgcb;
S
Shengliang Guan 已提交
118 119
  syncInfo.syncSendMSg = syncIOSendMsg;
  syncInfo.syncEqMsg = syncIOEqMsg;
M
Minghao Li 已提交
120 121 122
  syncInfo.pFsm = createFsm();
  snprintf(syncInfo.path, sizeof(syncInfo.path), "%s_sync_replica%d_index%d", path, replicaNum, myIndex);
  syncInfo.pWal = pWal;
123
  syncInfo.isStandBy = isStandBy;
M
Minghao Li 已提交
124 125 126

  SSyncCfg* pCfg = &syncInfo.syncCfg;

M
Minghao Li 已提交
127 128 129 130
  if (isStandBy) {
    pCfg->myIndex = 0;
    pCfg->replicaNum = 1;
    pCfg->nodeInfo[0].nodePort = gPorts[myIndex];
M
Minghao Li 已提交
131
    taosGetFqdn(pCfg->nodeInfo[0].nodeFqdn);
M
Minghao Li 已提交
132 133 134 135 136 137 138 139 140 141

  } else {
    pCfg->myIndex = myIndex;
    pCfg->replicaNum = replicaNum;

    for (int i = 0; i < replicaNum; ++i) {
      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 已提交
142 143 144 145 146 147 148
  }

  int64_t rid = syncOpen(&syncInfo);
  assert(rid > 0);

  SSyncNode* pSyncNode = (SSyncNode*)syncNodeAcquire(rid);
  assert(pSyncNode != NULL);
149 150 151 152 153 154 155 156 157 158
  // 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->FpOnSyncClientRequest = pSyncNode->FpOnClientRequest;
M
Minghao Li 已提交
159 160 161 162 163 164
  gSyncIO->pSyncNode = pSyncNode;
  syncNodeRelease(pSyncNode);

  return rid;
}

M
Minghao Li 已提交
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
void configChange(int64_t rid, int32_t replicaNum, int32_t myIndex) {
  SSyncCfg syncCfg;

  syncCfg.myIndex = myIndex;
  syncCfg.replicaNum = replicaNum;

  for (int i = 0; i < replicaNum; ++i) {
    syncCfg.nodeInfo[i].nodePort = gPorts[i];
    taosGetFqdn(syncCfg.nodeInfo[i].nodeFqdn);
  }

  syncReconfig(rid, &syncCfg);
}

void usage(char* exe) {
  printf("usage: %s replicaNum myIndex lastApplyIndex writeRecordNum isStandBy isConfigChange \n", exe);
}
M
Minghao Li 已提交
182 183 184 185 186 187 188

SRpcMsg* createRpcMsg(int i, int count, int myIndex) {
  SRpcMsg* pMsg = (SRpcMsg*)taosMemoryMalloc(sizeof(SRpcMsg));
  memset(pMsg, 0, sizeof(SRpcMsg));
  pMsg->msgType = 9999;
  pMsg->contLen = 256;
  pMsg->pCont = rpcMallocCont(pMsg->contLen);
M
Minghao Li 已提交
189
  snprintf((char*)(pMsg->pCont), pMsg->contLen, "value-myIndex:%u-%d-%d-%" PRId64, myIndex, i, count,
190
           taosGetTimestampMs());
M
Minghao Li 已提交
191 192 193 194 195
  return pMsg;
}

int main(int argc, char** argv) {
  tsAsyncLog = 0;
M
Minghao Li 已提交
196
  sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE + DEBUG_INFO;
M
Minghao Li 已提交
197
  if (argc != 7) {
M
Minghao Li 已提交
198 199 200
    usage(argv[0]);
    exit(-1);
  }
M
Minghao Li 已提交
201

M
Minghao Li 已提交
202 203 204 205
  int32_t replicaNum = atoi(argv[1]);
  int32_t myIndex = atoi(argv[2]);
  int32_t lastApplyIndex = atoi(argv[3]);
  int32_t writeRecordNum = atoi(argv[4]);
M
Minghao Li 已提交
206
  bool    isStandBy = atoi(argv[5]);
M
Minghao Li 已提交
207
  bool    isConfigChange = atoi(argv[6]);
M
Minghao Li 已提交
208 209
  gSnapshotLastApplyIndex = lastApplyIndex;

M
Minghao Li 已提交
210 211 212 213 214 215
  if (!isStandBy) {
    assert(replicaNum >= 1 && replicaNum <= 5);
    assert(myIndex >= 0 && myIndex < replicaNum);
    assert(lastApplyIndex >= -1);
    assert(writeRecordNum >= 0);
  }
M
Minghao Li 已提交
216 217 218 219 220 221 222 223 224

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

  char walPath[128];
  snprintf(walPath, sizeof(walPath), "%s_wal_replica%d_index%d", gDir, replicaNum, myIndex);
  SWal* pWal = createWal(walPath, gVgId);

M
Minghao Li 已提交
225
  int64_t rid = createSyncNode(replicaNum, myIndex, gVgId, pWal, (char*)gDir, isStandBy);
M
Minghao Li 已提交
226
  assert(rid > 0);
M
Minghao Li 已提交
227

228 229 230 231 232 233 234 235 236
  syncStart(rid);

  /*
    if (isStandBy) {
      syncStartStandBy(rid);
    } else {
      syncStart(rid);
    }
  */
M
Minghao Li 已提交
237 238 239 240

  SSyncNode* pSyncNode = (SSyncNode*)syncNodeAcquire(rid);
  assert(pSyncNode != NULL);

M
Minghao Li 已提交
241
  if (isConfigChange) {
M
Minghao Li 已提交
242
    configChange(rid, 2, myIndex);
M
Minghao Li 已提交
243 244
  }

M
Minghao Li 已提交
245 246 247 248 249 250 251 252
  //---------------------------
  int32_t alreadySend = 0;
  while (1) {
    char* s = syncNode2SimpleStr(pSyncNode);

    if (alreadySend < writeRecordNum) {
      SRpcMsg* pRpcMsg = createRpcMsg(alreadySend, writeRecordNum, myIndex);
      int32_t  ret = syncPropose(rid, pRpcMsg, false);
M
Minghao Li 已提交
253
      if (ret == -1 && terrno == TSDB_CODE_SYN_NOT_LEADER) {
M
Minghao Li 已提交
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
        sTrace("%s value%d write not leader", s, alreadySend);
      } else {
        assert(ret == 0);
        sTrace("%s value%d write ok", s, alreadySend);
      }
      alreadySend++;

      rpcFreeCont(pRpcMsg->pCont);
      taosMemoryFree(pRpcMsg);
    } else {
      sTrace("%s", s);
    }

    taosMsleep(1000);
    taosMemoryFree(s);
    taosMsleep(1000);
  }

  syncNodeRelease(pSyncNode);
  syncStop(rid);
  walClose(pWal);
  syncIOStop();
  cleanup();
  return 0;
}