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

131 lines
3.3 KiB
C++
Raw Normal View History

2022-03-09 10:33:41 +00:00
#include <gtest/gtest.h>
2022-02-26 18:24:50 +00:00
#include <stdio.h>
#include "syncEnv.h"
#include "syncIO.h"
#include "syncInt.h"
#include "syncRaftStore.h"
2022-03-09 10:33:41 +00:00
#include "syncUtil.h"
2022-02-26 18:24:50 +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");
}
2022-03-09 10:33:41 +00:00
uint16_t ports[] = {7010, 7110, 7210, 7310, 7410};
int32_t replicaNum = 3;
int32_t myIndex = 0;
2022-03-03 03:37:19 +00:00
2022-03-09 10:33:41 +00:00
SRaftId ids[TSDB_MAX_REPLICA];
SSyncInfo syncInfo;
SSyncFSM* pFsm;
2022-02-27 02:22:15 +00:00
2022-03-09 10:33:41 +00:00
SSyncNode* syncNodeInit() {
syncInfo.vgId = 1234;
2022-02-28 09:47:47 +00:00
syncInfo.rpcClient = gSyncIO->clientRpc;
2022-02-27 02:22:15 +00:00
syncInfo.FpSendMsg = syncIOSendMsg;
2022-03-04 07:48:09 +00:00
syncInfo.queue = gSyncIO->pMsgQ;
syncInfo.FpEqMsg = syncIOEqMsg;
2022-02-27 02:22:15 +00:00
syncInfo.pFsm = pFsm;
2022-03-09 10:33:41 +00:00
snprintf(syncInfo.path, sizeof(syncInfo.path), "%s", "./");
2022-02-26 18:24:50 +00:00
SSyncCfg* pCfg = &syncInfo.syncCfg;
2022-03-03 03:37:19 +00:00
pCfg->myIndex = myIndex;
2022-03-09 10:33:41 +00:00
pCfg->replicaNum = replicaNum;
2022-02-26 18:24:50 +00:00
2022-03-09 10:33:41 +00:00
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);
}
2022-02-26 18:24:50 +00:00
SSyncNode* pSyncNode = syncNodeOpen(&syncInfo);
assert(pSyncNode != NULL);
2022-02-27 02:22:15 +00:00
2022-03-02 07:11:54 +00:00
gSyncIO->FpOnSyncPing = pSyncNode->FpOnPing;
2022-03-09 10:33:41 +00:00
gSyncIO->FpOnSyncPingReply = pSyncNode->FpOnPingReply;
gSyncIO->FpOnSyncRequestVote = pSyncNode->FpOnRequestVote;
gSyncIO->FpOnSyncRequestVoteReply = pSyncNode->FpOnRequestVoteReply;
gSyncIO->FpOnSyncAppendEntries = pSyncNode->FpOnAppendEntries;
gSyncIO->FpOnSyncAppendEntriesReply = pSyncNode->FpOnAppendEntriesReply;
gSyncIO->FpOnSyncTimeout = pSyncNode->FpOnTimeout;
2022-02-27 02:22:15 +00:00
gSyncIO->pSyncNode = pSyncNode;
2022-02-28 06:10:34 +00:00
return pSyncNode;
}
2022-03-09 10:33:41 +00:00
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]);
printf("raftId[%d] : %s\n", i, s);
free(s);
}
2022-02-26 18:24:50 +00:00
}
2022-03-03 03:37:19 +00:00
int main(int argc, char** argv) {
2022-03-09 10:33:41 +00:00
// taosInitLog((char *)"syncTest.log", 100000, 10);
2022-02-26 18:24:50 +00:00
tsAsyncLog = 0;
sDebugFlag = 143 + 64;
2022-03-09 10:33:41 +00:00
myIndex = 0;
2022-03-03 03:37:19 +00:00
if (argc >= 2) {
myIndex = atoi(argv[1]);
}
int32_t ret = syncIOStart((char*)"127.0.0.1", ports[myIndex]);
2022-02-26 18:24:50 +00:00
assert(ret == 0);
ret = syncEnvStart();
assert(ret == 0);
2022-03-09 10:33:41 +00:00
SSyncNode* pSyncNode = syncInitTest();
assert(pSyncNode != NULL);
syncNodePrint((char*)"----1", pSyncNode);
2022-02-28 06:10:34 +00:00
2022-03-09 10:33:41 +00:00
initRaftId(pSyncNode);
//---------------------------
sTrace("syncNodeStartPingTimer ...");
2022-03-02 07:20:49 +00:00
ret = syncNodeStartPingTimer(pSyncNode);
assert(ret == 0);
2022-03-09 10:33:41 +00:00
syncNodePrint((char*)"----2", pSyncNode);
2022-02-28 08:36:57 +00:00
2022-03-09 10:33:41 +00:00
sTrace("sleep ...");
2022-03-04 07:48:09 +00:00
taosMsleep(10000);
2022-03-06 09:59:24 +00:00
2022-03-09 10:33:41 +00:00
sTrace("syncNodeStopPingTimer ...");
2022-03-06 09:59:24 +00:00
ret = syncNodeStopPingTimer(pSyncNode);
assert(ret == 0);
2022-03-09 10:33:41 +00:00
syncNodePrint((char*)"----3", pSyncNode);
2022-03-06 09:59:24 +00:00
2022-03-09 10:33:41 +00:00
sTrace("sleep ...");
taosMsleep(5000);
2022-03-06 09:59:24 +00:00
2022-03-09 10:33:41 +00:00
sTrace("syncNodeStartPingTimer ...");
2022-03-06 09:59:24 +00:00
ret = syncNodeStartPingTimer(pSyncNode);
assert(ret == 0);
2022-03-09 10:33:41 +00:00
syncNodePrint((char*)"----4", pSyncNode);
2022-03-06 09:59:24 +00:00
2022-03-09 10:33:41 +00:00
sTrace("sleep ...");
2022-03-06 09:59:24 +00:00
taosMsleep(10000);
2022-03-09 10:33:41 +00:00
sTrace("syncNodeStopPingTimer ...");
2022-03-04 07:48:09 +00:00
ret = syncNodeStopPingTimer(pSyncNode);
assert(ret == 0);
2022-03-09 10:33:41 +00:00
syncNodePrint((char*)"----5", pSyncNode);
2022-02-26 18:24:50 +00:00
while (1) {
2022-03-09 10:33:41 +00:00
sTrace("while 1 sleep ...");
2022-02-26 18:24:50 +00:00
taosMsleep(1000);
}
return 0;
}