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

99 lines
2.3 KiB
C++
Raw Normal View History

2022-03-10 03:21:04 +00:00
#include <gtest/gtest.h>
#include <stdio.h>
#include "syncEnv.h"
#include "syncIO.h"
#include "syncInt.h"
#include "syncRaftLog.h"
#include "syncRaftStore.h"
#include "syncUtil.h"
2022-04-22 09:11:56 +00:00
#include "wal.h"
2022-03-10 03:21:04 +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-04-18 13:50:56 +00:00
SSyncNode* pSyncNode;
SWal* pWal;
SSyncLogStore* pLogStore;
const char* pWalPath = "./syncLogStoreTest_wal";
void init() {
walInit();
taosRemoveDir(pWalPath);
2022-03-10 03:21:04 +00:00
SWalCfg walCfg;
memset(&walCfg, 0, sizeof(SWalCfg));
2022-04-18 13:50:56 +00:00
walCfg.vgId = 1000;
2022-03-10 03:21:04 +00:00
walCfg.fsyncPeriod = 1000;
walCfg.retentionPeriod = 1000;
walCfg.rollPeriod = 1000;
2022-03-10 08:18:16 +00:00
walCfg.retentionSize = 1000;
walCfg.segSize = 1000;
walCfg.level = TAOS_WAL_FSYNC;
2022-04-18 13:50:56 +00:00
pWal = walOpen(pWalPath, &walCfg);
2022-03-10 08:18:16 +00:00
assert(pWal != NULL);
2022-03-10 03:21:04 +00:00
2022-04-18 13:50:56 +00:00
pSyncNode = (SSyncNode*)taosMemoryMalloc(sizeof(SSyncNode));
memset(pSyncNode, 0, sizeof(SSyncNode));
pSyncNode->pWal = pWal;
2022-03-10 03:21:04 +00:00
}
2022-04-18 13:50:56 +00:00
void cleanup() {
walClose(pWal);
walCleanUp();
taosMemoryFree(pSyncNode);
}
2022-03-10 03:21:04 +00:00
void logStoreTest() {
2022-04-18 13:50:56 +00:00
pLogStore = logStoreCreate(pSyncNode);
assert(pLogStore);
assert(pLogStore->getLastIndex(pLogStore) == SYNC_INDEX_INVALID);
2022-03-14 06:05:40 +00:00
2022-04-18 13:50:56 +00:00
logStoreLog2((char*)"logStoreTest", pLogStore);
2022-03-14 06:05:40 +00:00
2022-03-10 03:21:04 +00:00
for (int i = 0; i < 5; ++i) {
2022-03-10 11:21:02 +00:00
int32_t dataLen = 10;
2022-03-10 08:18:16 +00:00
SSyncRaftEntry* pEntry = syncEntryBuild(dataLen);
assert(pEntry != NULL);
pEntry->msgType = 1;
pEntry->originalRpcType = 2;
pEntry->seqNum = 3;
pEntry->isWeak = true;
2022-04-18 13:50:56 +00:00
pEntry->term = 100 + i;
pEntry->index = pLogStore->getLastIndex(pLogStore) + 1;
2022-03-10 08:18:16 +00:00
snprintf(pEntry->data, dataLen, "value%d", i);
2022-04-18 13:50:56 +00:00
syncEntryLog2((char*)"==write entry== :", pEntry);
pLogStore->appendEntry(pLogStore, pEntry);
2022-03-10 08:18:16 +00:00
syncEntryDestory(pEntry);
2022-03-14 06:05:40 +00:00
if (i == 0) {
2022-04-18 13:50:56 +00:00
assert(pLogStore->getLastIndex(pLogStore) == SYNC_INDEX_BEGIN);
2022-03-14 06:05:40 +00:00
}
2022-03-10 03:21:04 +00:00
}
2022-04-18 13:50:56 +00:00
logStoreLog2((char*)"after appendEntry", pLogStore);
2022-03-10 03:21:04 +00:00
2022-04-18 13:50:56 +00:00
pLogStore->truncate(pLogStore, 3);
logStoreLog2((char*)"after truncate 3", pLogStore);
2022-03-10 03:21:04 +00:00
2022-04-18 13:50:56 +00:00
logStoreDestory(pLogStore);
2022-03-10 03:21:04 +00:00
}
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-10 03:21:04 +00:00
2022-04-18 13:50:56 +00:00
init();
2022-03-10 03:21:04 +00:00
logStoreTest();
2022-04-18 13:50:56 +00:00
taosMsleep(2000);
cleanup();
2022-03-10 03:21:04 +00:00
return 0;
}