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

98 lines
2.6 KiB
C++
Raw Normal View History

2022-03-08 05:43:54 +00:00
#include <gtest/gtest.h>
#include <stdio.h>
2022-03-08 12:22:31 +00:00
#include "syncEnv.h"
2022-03-08 05:43:54 +00:00
#include "syncIO.h"
#include "syncInt.h"
#include "syncRaftStore.h"
2022-03-08 12:22:31 +00:00
#include "syncUtil.h"
2022-03-08 05:43:54 +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-08 12:22:31 +00:00
uint16_t ports[] = {7010, 7110, 7210, 7310, 7410};
int32_t replicaNum = 5;
int32_t myIndex = 0;
2022-03-08 05:43:54 +00:00
2022-03-08 12:22:31 +00:00
SRaftId ids[TSDB_MAX_REPLICA];
SSyncInfo syncInfo;
SSyncFSM* pFsm;
2022-03-08 05:43:54 +00:00
2022-03-08 12:22:31 +00:00
SSyncNode* syncNodeInit() {
syncInfo.vgId = 1234;
2022-03-08 05:43:54 +00:00
syncInfo.rpcClient = gSyncIO->clientRpc;
syncInfo.FpSendMsg = syncIOSendMsg;
syncInfo.queue = gSyncIO->pMsgQ;
syncInfo.FpEqMsg = syncIOEqMsg;
syncInfo.pFsm = pFsm;
2022-03-09 10:33:41 +00:00
snprintf(syncInfo.path, sizeof(syncInfo.path), "%s", "./");
2022-03-08 05:43:54 +00:00
SSyncCfg* pCfg = &syncInfo.syncCfg;
2022-03-08 12:22:31 +00:00
pCfg->myIndex = myIndex;
pCfg->replicaNum = replicaNum;
2022-03-08 05:43:54 +00:00
2022-03-08 12:22:31 +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-03-08 05:43:54 +00:00
SSyncNode* pSyncNode = syncNodeOpen(&syncInfo);
assert(pSyncNode != NULL);
gSyncIO->FpOnSyncPing = pSyncNode->FpOnPing;
2022-03-08 12:22:31 +00:00
gSyncIO->FpOnSyncPingReply = pSyncNode->FpOnPingReply;
2022-03-16 02:54:06 +00:00
gSyncIO->FpOnSyncClientRequest = pSyncNode->FpOnClientRequest;
2022-03-08 12:22:31 +00:00
gSyncIO->FpOnSyncRequestVote = pSyncNode->FpOnRequestVote;
gSyncIO->FpOnSyncRequestVoteReply = pSyncNode->FpOnRequestVoteReply;
gSyncIO->FpOnSyncAppendEntries = pSyncNode->FpOnAppendEntries;
gSyncIO->FpOnSyncAppendEntriesReply = pSyncNode->FpOnAppendEntriesReply;
2022-03-10 03:21:04 +00:00
gSyncIO->FpOnSyncTimeout = pSyncNode->FpOnTimeout;
2022-03-08 05:43:54 +00:00
gSyncIO->pSyncNode = pSyncNode;
return pSyncNode;
}
2022-03-08 12:22:31 +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);
}
}
int main(int argc, char** argv) {
2022-03-08 05:43:54 +00:00
// taosInitLog((char *)"syncTest.log", 100000, 10);
tsAsyncLog = 0;
sDebugFlag = 143 + 64;
2022-03-08 12:22:31 +00:00
myIndex = 0;
if (argc >= 2) {
myIndex = atoi(argv[1]);
}
int32_t ret = syncIOStart((char*)"127.0.0.1", ports[myIndex]);
assert(ret == 0);
ret = syncEnvStart();
2022-03-08 05:43:54 +00:00
assert(ret == 0);
SSyncNode* pSyncNode = syncInitTest();
assert(pSyncNode != NULL);
2022-03-10 11:21:02 +00:00
syncNodePrint2((char*)"syncInitTest", pSyncNode);
2022-03-08 12:22:31 +00:00
initRaftId(pSyncNode);
2022-03-08 05:43:54 +00:00
2022-03-10 03:21:04 +00:00
//--------------------------------------------------------------
2022-03-08 05:43:54 +00:00
return 0;
2022-03-10 03:21:04 +00:00
}