TDengine/source/libs/sync/test/syncReplicateTest.cpp

219 lines
6.7 KiB
C++
Raw Normal View History

2022-03-18 09:46:40 +00:00
#include <gtest/gtest.h>
2022-11-10 04:43:23 +00:00
#include "syncTest.h"
2022-03-18 09:46:40 +00:00
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;
2022-03-18 09:46:40 +00:00
void init() {
int code = walInit();
assert(code == 0);
code = syncInit();
assert(code == 0);
}
void cleanup() { walCleanUp(); }
2022-03-18 09:46:40 +00:00
2022-04-21 07:24:50 +00:00
void CommitCb(struct SSyncFSM* pFsm, const SRpcMsg* pMsg, SFsmCbMeta cbMeta) {
SyncIndex beginIndex = SYNC_INDEX_INVALID;
if (pFsm->FpGetSnapshotInfo != NULL) {
SSnapshot snapshot;
pFsm->FpGetSnapshotInfo(pFsm, &snapshot);
beginIndex = snapshot.lastApplyIndex;
}
if (cbMeta.index > beginIndex) {
char logBuf[256];
snprintf(logBuf, sizeof(logBuf),
"==callback== ==CommitCb== pFsm:%p, index:%" PRId64 ", isWeak:%d, code:%d, state:%d %s \n", pFsm,
2022-11-02 02:24:55 +00:00
cbMeta.index, cbMeta.isWeak, cbMeta.code, cbMeta.state, syncStr(cbMeta.state));
2022-04-21 07:24:50 +00:00
syncRpcMsgLog2(logBuf, (SRpcMsg*)pMsg);
} else {
2022-07-08 10:00:03 +00:00
sTrace("==callback== ==CommitCb== do not apply again %" PRId64, cbMeta.index);
}
2022-03-18 10:42:49 +00:00
}
void PreCommitCb(struct SSyncFSM* pFsm, const SRpcMsg* pMsg, SFsmCbMeta cbMeta) {
2022-03-22 08:43:30 +00:00
char logBuf[256];
snprintf(logBuf, sizeof(logBuf),
"==callback== ==PreCommitCb== pFsm:%p, index:%" PRId64 ", isWeak:%d, code:%d, state:%d %s \n", pFsm,
2022-11-02 02:24:55 +00:00
cbMeta.index, cbMeta.isWeak, cbMeta.code, cbMeta.state, syncStr(cbMeta.state));
syncRpcMsgLog2(logBuf, (SRpcMsg*)pMsg);
2022-03-18 10:42:49 +00:00
}
void RollBackCb(struct SSyncFSM* pFsm, const SRpcMsg* pMsg, SFsmCbMeta cbMeta) {
2022-03-22 08:43:30 +00:00
char logBuf[256];
snprintf(logBuf, sizeof(logBuf),
"==callback== ==RollBackCb== pFsm:%p, index:%" PRId64 ", isWeak:%d, code:%d, state:%d %s \n", pFsm,
2022-11-02 02:24:55 +00:00
cbMeta.index, cbMeta.isWeak, cbMeta.code, cbMeta.state, syncStr(cbMeta.state));
syncRpcMsgLog2(logBuf, (SRpcMsg*)pMsg);
}
int32_t GetSnapshotCb(struct SSyncFSM* pFsm, SSnapshot* pSnapshot) {
pSnapshot->data = NULL;
pSnapshot->lastApplyIndex = gSnapshotLastApplyIndex;
pSnapshot->lastApplyTerm = 100;
return 0;
2022-03-18 10:42:49 +00:00
}
SSyncFSM* createFsm() {
SSyncFSM* pFsm = (SSyncFSM*)taosMemoryMalloc(sizeof(SSyncFSM));
2022-11-02 07:38:30 +00:00
#if 0
2022-03-18 10:42:49 +00:00
pFsm->FpCommitCb = CommitCb;
pFsm->FpPreCommitCb = PreCommitCb;
pFsm->FpRollBackCb = RollBackCb;
pFsm->FpGetSnapshotInfo = GetSnapshotCb;
2022-11-02 07:38:30 +00:00
#endif
return pFsm;
2022-03-18 10:42:49 +00:00
}
SWal* createWal(char* path, int32_t vgId) {
2022-03-18 09:46:40 +00:00
SWalCfg walCfg;
memset(&walCfg, 0, sizeof(SWalCfg));
walCfg.vgId = vgId;
2022-03-18 09:46:40 +00:00
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);
2022-03-18 09:46:40 +00:00
assert(pWal != NULL);
return pWal;
}
2022-03-18 09:46:40 +00:00
int64_t createSyncNode(int32_t replicaNum, int32_t myIndex, int32_t vgId, SWal* pWal, char* path) {
SSyncInfo syncInfo;
syncInfo.vgId = vgId;
2022-05-19 11:44:01 +00:00
syncInfo.msgcb = &gSyncIO->msgcb;
2022-11-01 07:40:23 +00:00
syncInfo.syncSendMSg = syncIOSendMsg;
syncInfo.syncEqMsg = syncIOEqMsg;
syncInfo.pFsm = createFsm();
snprintf(syncInfo.path, sizeof(syncInfo.path), "%s_sync_replica%d_index%d", path, replicaNum, myIndex);
2022-03-18 09:46:40 +00:00
syncInfo.pWal = pWal;
SSyncCfg* pCfg = &syncInfo.syncCfg;
2022-03-18 09:46:40 +00:00
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");
2022-03-18 09:46:40 +00:00
}
int64_t rid = syncOpen(&syncInfo);
assert(rid > 0);
SSyncNode* pSyncNode = (SSyncNode*)syncNodeAcquire(rid);
2022-03-18 09:46:40 +00:00
assert(pSyncNode != NULL);
2022-11-11 09:47:48 +00:00
// 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;
2022-03-18 09:46:40 +00:00
gSyncIO->pSyncNode = pSyncNode;
2022-04-21 07:24:50 +00:00
syncNodeRelease(pSyncNode);
2022-03-18 09:46:40 +00:00
return rid;
2022-03-18 09:46:40 +00:00
}
void usage(char* exe) { printf("usage: %s replicaNum myIndex lastApplyIndex writeRecordNum \n", exe); }
2022-04-21 07:24:50 +00:00
SRpcMsg* createRpcMsg(int i, int count, int myIndex) {
SRpcMsg* pMsg = (SRpcMsg*)taosMemoryMalloc(sizeof(SRpcMsg));
2022-03-18 10:42:49 +00:00
memset(pMsg, 0, sizeof(SRpcMsg));
pMsg->msgType = 9999;
2022-04-21 07:24:50 +00:00
pMsg->contLen = 256;
pMsg->pCont = rpcMallocCont(pMsg->contLen);
2022-10-27 09:38:33 +00:00
snprintf((char*)(pMsg->pCont), pMsg->contLen, "value-myIndex:%u-%d-%d-%" PRId64, myIndex, i, count,
taosGetTimestampMs());
2022-03-18 10:42:49 +00:00
return pMsg;
}
int main(int argc, char** argv) {
2022-03-18 09:46:40 +00:00
tsAsyncLog = 0;
sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE;
if (argc != 5) {
usage(argv[0]);
exit(-1);
2022-03-18 09:46:40 +00:00
}
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]);
2022-03-18 09:46:40 +00:00
assert(ret == 0);
char walPath[128];
snprintf(walPath, sizeof(walPath), "%s_wal_replica%d_index%d", gDir, replicaNum, myIndex);
SWal* pWal = createWal(walPath, gVgId);
2022-03-18 09:46:40 +00:00
int64_t rid = createSyncNode(replicaNum, myIndex, gVgId, pWal, (char*)gDir);
assert(rid > 0);
syncStart(rid);
2022-03-18 10:42:49 +00:00
2022-04-21 07:24:50 +00:00
SSyncNode* pSyncNode = (SSyncNode*)syncNodeAcquire(rid);
assert(pSyncNode != NULL);
2022-03-18 10:42:49 +00:00
//---------------------------
int32_t alreadySend = 0;
while (1) {
char* s = syncNode2SimpleStr(pSyncNode);
if (alreadySend < writeRecordNum) {
2022-04-21 07:24:50 +00:00
SRpcMsg* pRpcMsg = createRpcMsg(alreadySend, writeRecordNum, myIndex);
int32_t ret = syncPropose(rid, pRpcMsg, false);
2022-06-15 08:14:17 +00:00
if (ret == -1 && terrno == TSDB_CODE_SYN_NOT_LEADER) {
2022-04-21 07:24:50 +00:00
sTrace("%s value%d write not leader", s, alreadySend);
} else {
assert(ret == 0);
sTrace("%s value%d write ok", s, alreadySend);
}
alreadySend++;
2022-04-21 07:24:50 +00:00
rpcFreeCont(pRpcMsg->pCont);
taosMemoryFree(pRpcMsg);
2022-04-21 07:24:50 +00:00
} else {
sTrace("%s", s);
}
2022-03-18 10:42:49 +00:00
taosMsleep(1000);
taosMemoryFree(s);
2022-03-18 09:46:40 +00:00
taosMsleep(1000);
}
syncNodeRelease(pSyncNode);
syncStop(rid);
walClose(pWal);
syncIOStop();
cleanup();
2022-03-18 09:46:40 +00:00
return 0;
}