syncRaftLogTest2.cpp 12.9 KB
Newer Older
M
Minghao Li 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
#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"
#include "wal.h"

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");
}

SSyncNode*     pSyncNode;
SWal*          pWal;
SSyncLogStore* pLogStore;
const char*    pWalPath = "./syncLogStoreTest_wal";

M
Minghao Li 已提交
25 26 27 28 29 30 31 32 33 34
SyncIndex gSnapshotLastApplyIndex;
SyncIndex gSnapshotLastApplyTerm;

int32_t GetSnapshotCb(struct SSyncFSM* pFsm, SSnapshot* pSnapshot) {
  pSnapshot->data = NULL;
  pSnapshot->lastApplyIndex = gSnapshotLastApplyIndex;
  pSnapshot->lastApplyTerm = gSnapshotLastApplyTerm;
  return 0;
}

M
Minghao Li 已提交
35 36
bool gAssert = true;

M
Minghao Li 已提交
37 38
void init() {
  walInit();
M
Minghao Li 已提交
39

M
Minghao Li 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
  SWalCfg walCfg;
  memset(&walCfg, 0, sizeof(SWalCfg));
  walCfg.vgId = 1000;
  walCfg.fsyncPeriod = 1000;
  walCfg.retentionPeriod = 1000;
  walCfg.rollPeriod = 1000;
  walCfg.retentionSize = 1000;
  walCfg.segSize = 1000;
  walCfg.level = TAOS_WAL_FSYNC;
  pWal = walOpen(pWalPath, &walCfg);
  assert(pWal != NULL);

  pSyncNode = (SSyncNode*)taosMemoryMalloc(sizeof(SSyncNode));
  memset(pSyncNode, 0, sizeof(SSyncNode));
  pSyncNode->pWal = pWal;
M
Minghao Li 已提交
55 56 57

  pSyncNode->pFsm = (SSyncFSM*)taosMemoryMalloc(sizeof(SSyncFSM));
  pSyncNode->pFsm->FpGetSnapshot = GetSnapshotCb;
M
Minghao Li 已提交
58 59 60 61 62 63 64 65
}

void cleanup() {
  walClose(pWal);
  walCleanUp();
  taosMemoryFree(pSyncNode);
}

M
Minghao Li 已提交
66
void test1() {
M
Minghao Li 已提交
67
  taosRemoveDir(pWalPath);
M
Minghao Li 已提交
68

M
Minghao Li 已提交
69
  init();
M
Minghao Li 已提交
70 71 72
  pLogStore = logStoreCreate(pSyncNode);
  assert(pLogStore);
  logStoreLog2((char*)"\n\n\ntest1 ----- ", pLogStore);
M
Minghao Li 已提交
73 74 75 76 77 78

  if (gAssert) {
    assert(pLogStore->syncLogBeginIndex(pLogStore) == 0);
    assert(pLogStore->syncLogEndIndex(pLogStore) == -1);
    assert(pLogStore->syncLogEntryCount(pLogStore) == 0);
    assert(pLogStore->syncLogWriteIndex(pLogStore) == 0);
M
Minghao Li 已提交
79
    assert(pLogStore->syncLogIsEmpty(pLogStore) == 1);
M
Minghao Li 已提交
80 81 82 83
    assert(pLogStore->syncLogLastIndex(pLogStore) == -1);
    assert(pLogStore->syncLogLastTerm(pLogStore) == 0);
  }

M
Minghao Li 已提交
84
  logStoreDestory(pLogStore);
M
Minghao Li 已提交
85
  cleanup();
M
Minghao Li 已提交
86

M
Minghao Li 已提交
87 88 89 90 91
  // restart
  init();
  pLogStore = logStoreCreate(pSyncNode);
  assert(pLogStore);
  logStoreLog2((char*)"\n\n\ntest1 restart ----- ", pLogStore);
M
Minghao Li 已提交
92 93 94 95 96 97

  if (gAssert) {
    assert(pLogStore->syncLogBeginIndex(pLogStore) == 0);
    assert(pLogStore->syncLogEndIndex(pLogStore) == -1);
    assert(pLogStore->syncLogEntryCount(pLogStore) == 0);
    assert(pLogStore->syncLogWriteIndex(pLogStore) == 0);
M
Minghao Li 已提交
98
    assert(pLogStore->syncLogIsEmpty(pLogStore) == 1);
M
Minghao Li 已提交
99 100 101 102
    assert(pLogStore->syncLogLastIndex(pLogStore) == -1);
    assert(pLogStore->syncLogLastTerm(pLogStore) == 0);
  }

M
Minghao Li 已提交
103
  logStoreDestory(pLogStore);
M
Minghao Li 已提交
104 105 106 107
  cleanup();
}

void test2() {
M
Minghao Li 已提交
108
  taosRemoveDir(pWalPath);
M
Minghao Li 已提交
109

M
Minghao Li 已提交
110
  init();
M
Minghao Li 已提交
111 112
  pLogStore = logStoreCreate(pSyncNode);
  assert(pLogStore);
M
Minghao Li 已提交
113 114
  pLogStore->syncLogSetBeginIndex(pLogStore, 5);
  logStoreLog2((char*)"\n\n\ntest2 ----- ", pLogStore);
M
Minghao Li 已提交
115 116 117 118 119 120

  if (gAssert) {
    assert(pLogStore->syncLogBeginIndex(pLogStore) == 5);
    assert(pLogStore->syncLogEndIndex(pLogStore) == -1);
    assert(pLogStore->syncLogEntryCount(pLogStore) == 0);
    assert(pLogStore->syncLogWriteIndex(pLogStore) == 5);
M
Minghao Li 已提交
121
    assert(pLogStore->syncLogIsEmpty(pLogStore) == 1);
M
Minghao Li 已提交
122 123 124 125
    assert(pLogStore->syncLogLastIndex(pLogStore) == -1);
    assert(pLogStore->syncLogLastTerm(pLogStore) == 0);
  }

M
Minghao Li 已提交
126
  logStoreDestory(pLogStore);
M
Minghao Li 已提交
127
  cleanup();
M
Minghao Li 已提交
128

M
Minghao Li 已提交
129 130 131 132 133
  // restart
  init();
  pLogStore = logStoreCreate(pSyncNode);
  assert(pLogStore);
  logStoreLog2((char*)"\n\n\ntest2 restart ----- ", pLogStore);
M
Minghao Li 已提交
134 135 136 137 138 139

  if (gAssert) {
    assert(pLogStore->syncLogBeginIndex(pLogStore) == 5);
    assert(pLogStore->syncLogEndIndex(pLogStore) == -1);
    assert(pLogStore->syncLogEntryCount(pLogStore) == 0);
    assert(pLogStore->syncLogWriteIndex(pLogStore) == 5);
M
Minghao Li 已提交
140
    assert(pLogStore->syncLogIsEmpty(pLogStore) == 1);
M
Minghao Li 已提交
141 142 143 144
    assert(pLogStore->syncLogLastIndex(pLogStore) == -1);
    assert(pLogStore->syncLogLastTerm(pLogStore) == 0);
  }

M
Minghao Li 已提交
145
  logStoreDestory(pLogStore);
M
Minghao Li 已提交
146 147 148 149
  cleanup();
}

void test3() {
M
Minghao Li 已提交
150
  taosRemoveDir(pWalPath);
M
Minghao Li 已提交
151

M
Minghao Li 已提交
152
  init();
M
Minghao Li 已提交
153 154 155
  pLogStore = logStoreCreate(pSyncNode);
  assert(pLogStore);
  logStoreLog2((char*)"\n\n\ntest3 ----- ", pLogStore);
M
Minghao Li 已提交
156

M
Minghao Li 已提交
157 158 159 160 161
  if (gAssert) {
    assert(pLogStore->syncLogBeginIndex(pLogStore) == 0);
    assert(pLogStore->syncLogEndIndex(pLogStore) == -1);
    assert(pLogStore->syncLogEntryCount(pLogStore) == 0);
    assert(pLogStore->syncLogWriteIndex(pLogStore) == 0);
M
Minghao Li 已提交
162
    assert(pLogStore->syncLogIsEmpty(pLogStore) == 1);
M
Minghao Li 已提交
163 164 165 166
    assert(pLogStore->syncLogLastIndex(pLogStore) == -1);
    assert(pLogStore->syncLogLastTerm(pLogStore) == 0);
  }

M
Minghao Li 已提交
167
  for (int i = 0; i <= 4; ++i) {
M
Minghao Li 已提交
168 169 170 171 172 173 174 175
    int32_t         dataLen = 10;
    SSyncRaftEntry* pEntry = syncEntryBuild(dataLen);
    assert(pEntry != NULL);
    pEntry->msgType = 1;
    pEntry->originalRpcType = 2;
    pEntry->seqNum = 3;
    pEntry->isWeak = true;
    pEntry->term = 100 + i;
M
Minghao Li 已提交
176
    pEntry->index = pLogStore->syncLogWriteIndex(pLogStore);
M
Minghao Li 已提交
177 178
    snprintf(pEntry->data, dataLen, "value%d", i);

M
Minghao Li 已提交
179
    pLogStore->syncLogAppendEntry(pLogStore, pEntry);
M
Minghao Li 已提交
180 181
    syncEntryDestory(pEntry);
  }
M
Minghao Li 已提交
182
  logStoreLog2((char*)"test3 after appendEntry", pLogStore);
M
Minghao Li 已提交
183 184 185 186 187 188

  if (gAssert) {
    assert(pLogStore->syncLogBeginIndex(pLogStore) == 0);
    assert(pLogStore->syncLogEndIndex(pLogStore) == 4);
    assert(pLogStore->syncLogEntryCount(pLogStore) == 5);
    assert(pLogStore->syncLogWriteIndex(pLogStore) == 5);
M
Minghao Li 已提交
189
    assert(pLogStore->syncLogIsEmpty(pLogStore) == 0);
M
Minghao Li 已提交
190 191 192 193
    assert(pLogStore->syncLogLastIndex(pLogStore) == 4);
    assert(pLogStore->syncLogLastTerm(pLogStore) == 104);
  }

M
Minghao Li 已提交
194
  logStoreDestory(pLogStore);
M
Minghao Li 已提交
195
  cleanup();
M
Minghao Li 已提交
196

M
Minghao Li 已提交
197 198 199 200 201
  // restart
  init();
  pLogStore = logStoreCreate(pSyncNode);
  assert(pLogStore);
  logStoreLog2((char*)"\n\n\ntest3 restart ----- ", pLogStore);
M
Minghao Li 已提交
202 203 204 205 206 207

  if (gAssert) {
    assert(pLogStore->syncLogBeginIndex(pLogStore) == 0);
    assert(pLogStore->syncLogEndIndex(pLogStore) == 4);
    assert(pLogStore->syncLogEntryCount(pLogStore) == 5);
    assert(pLogStore->syncLogWriteIndex(pLogStore) == 5);
M
Minghao Li 已提交
208
    assert(pLogStore->syncLogIsEmpty(pLogStore) == 0);
M
Minghao Li 已提交
209 210 211 212
    assert(pLogStore->syncLogLastIndex(pLogStore) == 4);
    assert(pLogStore->syncLogLastTerm(pLogStore) == 104);
  }

M
Minghao Li 已提交
213
  logStoreDestory(pLogStore);
M
Minghao Li 已提交
214 215 216 217
  cleanup();
}

void test4() {
M
Minghao Li 已提交
218
  taosRemoveDir(pWalPath);
M
Minghao Li 已提交
219

M
Minghao Li 已提交
220
  init();
M
Minghao Li 已提交
221 222 223 224
  pLogStore = logStoreCreate(pSyncNode);
  assert(pLogStore);
  logStoreLog2((char*)"\n\n\ntest4 ----- ", pLogStore);
  pLogStore->syncLogSetBeginIndex(pLogStore, 5);
M
Minghao Li 已提交
225

M
Minghao Li 已提交
226 227 228 229 230 231 232 233 234 235 236
  for (int i = 5; i <= 9; ++i) {
    int32_t         dataLen = 10;
    SSyncRaftEntry* pEntry = syncEntryBuild(dataLen);
    assert(pEntry != NULL);
    pEntry->msgType = 1;
    pEntry->originalRpcType = 2;
    pEntry->seqNum = 3;
    pEntry->isWeak = true;
    pEntry->term = 100 + i;
    pEntry->index = pLogStore->syncLogWriteIndex(pLogStore);
    snprintf(pEntry->data, dataLen, "value%d", i);
M
Minghao Li 已提交
237

M
Minghao Li 已提交
238 239 240 241
    pLogStore->syncLogAppendEntry(pLogStore, pEntry);
    syncEntryDestory(pEntry);
  }
  logStoreLog2((char*)"test4 after appendEntry", pLogStore);
M
Minghao Li 已提交
242 243 244 245 246 247

  if (gAssert) {
    assert(pLogStore->syncLogBeginIndex(pLogStore) == 5);
    assert(pLogStore->syncLogEndIndex(pLogStore) == 9);
    assert(pLogStore->syncLogEntryCount(pLogStore) == 5);
    assert(pLogStore->syncLogWriteIndex(pLogStore) == 10);
M
Minghao Li 已提交
248
    assert(pLogStore->syncLogIsEmpty(pLogStore) == 0);
M
Minghao Li 已提交
249 250 251 252
    assert(pLogStore->syncLogLastIndex(pLogStore) == 9);
    assert(pLogStore->syncLogLastTerm(pLogStore) == 109);
  }

M
Minghao Li 已提交
253
  logStoreDestory(pLogStore);
M
Minghao Li 已提交
254
  cleanup();
M
Minghao Li 已提交
255

M
Minghao Li 已提交
256 257 258 259 260
  // restart
  init();
  pLogStore = logStoreCreate(pSyncNode);
  assert(pLogStore);
  logStoreLog2((char*)"\n\n\ntest4 restart ----- ", pLogStore);
M
Minghao Li 已提交
261 262 263 264 265 266

  if (gAssert) {
    assert(pLogStore->syncLogBeginIndex(pLogStore) == 5);
    assert(pLogStore->syncLogEndIndex(pLogStore) == 9);
    assert(pLogStore->syncLogEntryCount(pLogStore) == 5);
    assert(pLogStore->syncLogWriteIndex(pLogStore) == 10);
M
Minghao Li 已提交
267
    assert(pLogStore->syncLogIsEmpty(pLogStore) == 0);
M
Minghao Li 已提交
268 269 270 271
    assert(pLogStore->syncLogLastIndex(pLogStore) == 9);
    assert(pLogStore->syncLogLastTerm(pLogStore) == 109);
  }

M
Minghao Li 已提交
272
  logStoreDestory(pLogStore);
M
Minghao Li 已提交
273
  cleanup();
M
Minghao Li 已提交
274 275
}

M
Minghao Li 已提交
276
void test5() {
M
Minghao Li 已提交
277
  taosRemoveDir(pWalPath);
M
Minghao Li 已提交
278

M
Minghao Li 已提交
279
  init();
M
Minghao Li 已提交
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
  pLogStore = logStoreCreate(pSyncNode);
  assert(pLogStore);
  logStoreLog2((char*)"\n\n\ntest5 ----- ", pLogStore);
  pLogStore->syncLogSetBeginIndex(pLogStore, 5);

  for (int i = 5; i <= 9; ++i) {
    int32_t         dataLen = 10;
    SSyncRaftEntry* pEntry = syncEntryBuild(dataLen);
    assert(pEntry != NULL);
    pEntry->msgType = 1;
    pEntry->originalRpcType = 2;
    pEntry->seqNum = 3;
    pEntry->isWeak = true;
    pEntry->term = 100 + i;
    pEntry->index = pLogStore->syncLogWriteIndex(pLogStore);
    snprintf(pEntry->data, dataLen, "value%d", i);

    pLogStore->syncLogAppendEntry(pLogStore, pEntry);
    syncEntryDestory(pEntry);
  }
  logStoreLog2((char*)"test5 after appendEntry", pLogStore);

M
Minghao Li 已提交
302 303 304 305 306
  if (gAssert) {
    assert(pLogStore->syncLogBeginIndex(pLogStore) == 5);
    assert(pLogStore->syncLogEndIndex(pLogStore) == 9);
    assert(pLogStore->syncLogEntryCount(pLogStore) == 5);
    assert(pLogStore->syncLogWriteIndex(pLogStore) == 10);
M
Minghao Li 已提交
307
    assert(pLogStore->syncLogIsEmpty(pLogStore) == 0);
M
Minghao Li 已提交
308 309 310 311
    assert(pLogStore->syncLogLastIndex(pLogStore) == 9);
    assert(pLogStore->syncLogLastTerm(pLogStore) == 109);
  }

M
Minghao Li 已提交
312 313 314
  pLogStore->syncLogTruncate(pLogStore, 7);
  logStoreLog2((char*)"after truncate 7", pLogStore);

M
Minghao Li 已提交
315 316 317 318 319
  if (gAssert) {
    assert(pLogStore->syncLogBeginIndex(pLogStore) == 5);
    assert(pLogStore->syncLogEndIndex(pLogStore) == 6);
    assert(pLogStore->syncLogEntryCount(pLogStore) == 2);
    assert(pLogStore->syncLogWriteIndex(pLogStore) == 7);
M
Minghao Li 已提交
320
    assert(pLogStore->syncLogIsEmpty(pLogStore) == 0);
M
Minghao Li 已提交
321 322 323 324
    assert(pLogStore->syncLogLastIndex(pLogStore) == 6);
    assert(pLogStore->syncLogLastTerm(pLogStore) == 106);
  }

M
Minghao Li 已提交
325
  logStoreDestory(pLogStore);
M
Minghao Li 已提交
326
  cleanup();
M
Minghao Li 已提交
327

M
Minghao Li 已提交
328 329 330 331 332
  // restart
  init();
  pLogStore = logStoreCreate(pSyncNode);
  assert(pLogStore);
  logStoreLog2((char*)"\n\n\ntest5 restart ----- ", pLogStore);
M
Minghao Li 已提交
333 334 335 336 337 338

  if (gAssert) {
    assert(pLogStore->syncLogBeginIndex(pLogStore) == 5);
    assert(pLogStore->syncLogEndIndex(pLogStore) == 6);
    assert(pLogStore->syncLogEntryCount(pLogStore) == 2);
    assert(pLogStore->syncLogWriteIndex(pLogStore) == 7);
M
Minghao Li 已提交
339
    assert(pLogStore->syncLogIsEmpty(pLogStore) == 0);
M
Minghao Li 已提交
340 341 342 343
    assert(pLogStore->syncLogLastIndex(pLogStore) == 6);
    assert(pLogStore->syncLogLastTerm(pLogStore) == 106);
  }

M
Minghao Li 已提交
344
  logStoreDestory(pLogStore);
M
Minghao Li 已提交
345 346
  cleanup();
}
M
Minghao Li 已提交
347

M
Minghao Li 已提交
348
void test6() {
M
Minghao Li 已提交
349
  taosRemoveDir(pWalPath);
M
Minghao Li 已提交
350

M
Minghao Li 已提交
351
  init();
M
Minghao Li 已提交
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
  pLogStore = logStoreCreate(pSyncNode);
  assert(pLogStore);
  logStoreLog2((char*)"\n\n\ntest6 ----- ", pLogStore);
  pLogStore->syncLogSetBeginIndex(pLogStore, 5);

  for (int i = 5; i <= 9; ++i) {
    int32_t         dataLen = 10;
    SSyncRaftEntry* pEntry = syncEntryBuild(dataLen);
    assert(pEntry != NULL);
    pEntry->msgType = 1;
    pEntry->originalRpcType = 2;
    pEntry->seqNum = 3;
    pEntry->isWeak = true;
    pEntry->term = 100 + i;
    pEntry->index = pLogStore->syncLogWriteIndex(pLogStore);
    snprintf(pEntry->data, dataLen, "value%d", i);

    pLogStore->syncLogAppendEntry(pLogStore, pEntry);
    syncEntryDestory(pEntry);
  }
  logStoreLog2((char*)"test6 after appendEntry", pLogStore);

M
Minghao Li 已提交
374 375 376 377 378
  if (gAssert) {
    assert(pLogStore->syncLogBeginIndex(pLogStore) == 5);
    assert(pLogStore->syncLogEndIndex(pLogStore) == 9);
    assert(pLogStore->syncLogEntryCount(pLogStore) == 5);
    assert(pLogStore->syncLogWriteIndex(pLogStore) == 10);
M
Minghao Li 已提交
379
    assert(pLogStore->syncLogIsEmpty(pLogStore) == 0);
M
Minghao Li 已提交
380 381 382 383
    assert(pLogStore->syncLogLastIndex(pLogStore) == 9);
    assert(pLogStore->syncLogLastTerm(pLogStore) == 109);
  }

M
Minghao Li 已提交
384 385 386
  pLogStore->syncLogTruncate(pLogStore, 5);
  logStoreLog2((char*)"after truncate 5", pLogStore);

M
Minghao Li 已提交
387 388 389 390 391
  if (gAssert) {
    assert(pLogStore->syncLogBeginIndex(pLogStore) == 5);
    assert(pLogStore->syncLogEndIndex(pLogStore) == -1);
    assert(pLogStore->syncLogEntryCount(pLogStore) == 0);
    assert(pLogStore->syncLogWriteIndex(pLogStore) == 5);
M
Minghao Li 已提交
392
    assert(pLogStore->syncLogIsEmpty(pLogStore) == 1);
M
Minghao Li 已提交
393 394 395 396
    assert(pLogStore->syncLogLastIndex(pLogStore) == -1);
    assert(pLogStore->syncLogLastTerm(pLogStore) == 0);
  }

M
Minghao Li 已提交
397
  logStoreDestory(pLogStore);
M
Minghao Li 已提交
398
  cleanup();
M
Minghao Li 已提交
399

M
Minghao Li 已提交
400 401 402 403 404
  // restart
  init();
  pLogStore = logStoreCreate(pSyncNode);
  assert(pLogStore);
  logStoreLog2((char*)"\n\n\ntest6 restart ----- ", pLogStore);
M
Minghao Li 已提交
405 406 407 408 409 410

  if (gAssert) {
    assert(pLogStore->syncLogBeginIndex(pLogStore) == 5);
    assert(pLogStore->syncLogEndIndex(pLogStore) == -1);
    assert(pLogStore->syncLogEntryCount(pLogStore) == 0);
    assert(pLogStore->syncLogWriteIndex(pLogStore) == 5);
M
Minghao Li 已提交
411
    assert(pLogStore->syncLogIsEmpty(pLogStore) == 1);
M
Minghao Li 已提交
412 413 414 415
    assert(pLogStore->syncLogLastIndex(pLogStore) == -1);
    assert(pLogStore->syncLogLastTerm(pLogStore) == 0);
  }

M
Minghao Li 已提交
416
  logStoreDestory(pLogStore);
M
Minghao Li 已提交
417
  cleanup();
M
Minghao Li 已提交
418 419 420 421 422 423
}

int main(int argc, char** argv) {
  tsAsyncLog = 0;
  sDebugFlag = DEBUG_TRACE + DEBUG_INFO + DEBUG_SCREEN + DEBUG_FILE;

M
Minghao Li 已提交
424 425 426 427 428
  if (argc == 2) {
    gAssert = atoi(argv[1]);
  }
  sTrace("gAssert : %d", gAssert);

M
Minghao Li 已提交
429 430 431 432 433 434
  test1();
  test2();
  test3();
  test4();
  test5();
  test6();
M
Minghao Li 已提交
435 436 437

  return 0;
}