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

93 lines
2.8 KiB
C++
Raw Permalink Normal View History

2022-11-10 04:43:23 +00:00
#include "syncTest.h"
2022-03-09 02:58:22 +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 ports[] = {7010, 7110, 7210, 7310, 7410};
int32_t replicaNum = 3;
int32_t myIndex = 0;
SRaftId ids[TSDB_MAX_REPLICA];
SSyncNode* pSyncNode;
SSyncNode* syncNodeInit() {
2022-06-07 08:33:37 +00:00
pSyncNode = (SSyncNode*)taosMemoryMalloc(sizeof(SSyncNode));
memset(pSyncNode, 0, sizeof(SSyncNode));
pSyncNode->replicaNum = replicaNum;
2022-03-09 02:58:22 +00:00
for (int i = 0; i < replicaNum; ++i) {
2022-06-07 08:33:37 +00:00
pSyncNode->replicasId[i].addr = syncUtilAddr2U64("127.0.0.1", ports[i]);
pSyncNode->replicasId[i].vgId = 1234;
2022-03-09 02:58:22 +00:00
2022-06-07 08:33:37 +00:00
ids[i].addr = pSyncNode->replicasId[i].addr;
ids[i].vgId = pSyncNode->replicasId[i].vgId;
}
2022-03-09 02:58:22 +00:00
return pSyncNode;
}
int main(int argc, char** argv) {
tsAsyncLog = 0;
2022-04-18 13:50:56 +00:00
sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE;
2022-03-09 02:58:22 +00:00
myIndex = 0;
if (argc >= 2) {
myIndex = atoi(argv[1]);
}
2022-06-07 08:33:37 +00:00
SSyncNode* pSyncNode = syncNodeInit();
TD_ALWAYS_ASSERT(pSyncNode != NULL);
2022-03-09 02:58:22 +00:00
2022-06-07 08:33:37 +00:00
printf("---------------------------------------\n");
2022-03-09 03:27:22 +00:00
SSyncIndexMgr* pSyncIndexMgr = syncIndexMgrCreate(pSyncNode);
TD_ALWAYS_ASSERT(pSyncIndexMgr != NULL);
2022-03-09 02:58:22 +00:00
{
2022-03-09 03:27:22 +00:00
char* serialized = syncIndexMgr2Str(pSyncIndexMgr);
TD_ALWAYS_ASSERT(serialized != NULL);
2022-03-09 02:58:22 +00:00
printf("%s\n", serialized);
2022-03-25 16:29:53 +00:00
taosMemoryFree(serialized);
2022-03-09 02:58:22 +00:00
}
2022-06-07 08:33:37 +00:00
printf("---------------------------------------\n");
2022-03-09 02:58:22 +00:00
2022-06-07 08:33:37 +00:00
printf("---------------------------------------\n");
2022-03-09 03:27:22 +00:00
syncIndexMgrSetIndex(pSyncIndexMgr, &ids[0], 100);
syncIndexMgrSetIndex(pSyncIndexMgr, &ids[1], 200);
syncIndexMgrSetIndex(pSyncIndexMgr, &ids[2], 300);
2022-06-08 03:25:24 +00:00
// syncIndexMgrSetTerm(pSyncIndexMgr, &ids[0], 700);
// syncIndexMgrSetTerm(pSyncIndexMgr, &ids[1], 800);
// syncIndexMgrSetTerm(pSyncIndexMgr, &ids[2], 900);
2022-03-09 02:58:22 +00:00
{
2022-03-09 03:27:22 +00:00
char* serialized = syncIndexMgr2Str(pSyncIndexMgr);
TD_ALWAYS_ASSERT(serialized != NULL);
2022-03-09 02:58:22 +00:00
printf("%s\n", serialized);
2022-03-25 16:29:53 +00:00
taosMemoryFree(serialized);
2022-03-09 02:58:22 +00:00
}
2022-06-07 08:33:37 +00:00
printf("---------------------------------------\n");
2022-03-09 02:58:22 +00:00
2022-03-09 03:27:22 +00:00
printf("---------------------------------------\n");
for (int i = 0; i < pSyncIndexMgr->replicaNum; ++i) {
SyncIndex idx = syncIndexMgrGetIndex(pSyncIndexMgr, &ids[i]);
2022-06-08 03:25:24 +00:00
// SyncTerm term = syncIndexMgrGetTerm(pSyncIndexMgr, &ids[i]);
2022-07-08 10:00:03 +00:00
// printf("%d: index:%" PRId64 " term:%" PRIu64 " \n", i, idx, term);
2022-03-09 02:58:22 +00:00
}
2022-06-07 08:33:37 +00:00
printf("---------------------------------------\n");
2022-03-09 02:58:22 +00:00
printf("---------------------------------------\n");
2022-06-07 08:33:37 +00:00
syncIndexMgrClear(pSyncIndexMgr);
2022-03-09 02:58:22 +00:00
{
2022-03-09 03:27:22 +00:00
char* serialized = syncIndexMgr2Str(pSyncIndexMgr);
TD_ALWAYS_ASSERT(serialized != NULL);
2022-03-09 02:58:22 +00:00
printf("%s\n", serialized);
2022-03-25 16:29:53 +00:00
taosMemoryFree(serialized);
2022-03-09 02:58:22 +00:00
}
2022-06-07 08:33:37 +00:00
printf("---------------------------------------\n");
2022-03-09 02:58:22 +00:00
2022-03-09 03:27:22 +00:00
syncIndexMgrDestroy(pSyncIndexMgr);
2022-03-09 02:58:22 +00:00
return 0;
}