walMetaTest.cpp 9.7 KB
Newer Older
L
Liu Jicong 已提交
1 2 3 4 5 6 7
#include <gtest/gtest.h>
#include <cstring>
#include <iostream>
#include <queue>

#include "walInt.h"

L
Liu Jicong 已提交
8
const char* ranStr = "tvapq02tcp";
L
Liu Jicong 已提交
9
const int   ranStrLen = strlen(ranStr);
L
Liu Jicong 已提交
10

L
Liu Jicong 已提交
11
class WalCleanEnv : public ::testing::Test {
L
Liu Jicong 已提交
12 13 14 15 16
 protected:
  static void SetUpTestCase() {
    int code = walInit();
    ASSERT(code == 0);
  }
L
Liu Jicong 已提交
17

L
Liu Jicong 已提交
18 19 20 21
  static void TearDownTestCase() { walCleanUp(); }

  void SetUp() override {
    taosRemoveDir(pathName);
wafwerar's avatar
wafwerar 已提交
22
    SWalCfg* pCfg = (SWalCfg*)taosMemoryMalloc(sizeof(SWalCfg));
L
Liu Jicong 已提交
23 24 25 26 27 28 29
    memset(pCfg, 0, sizeof(SWalCfg));
    pCfg->rollPeriod = -1;
    pCfg->segSize = -1;
    pCfg->retentionPeriod = 0;
    pCfg->retentionSize = 0;
    pCfg->level = TAOS_WAL_FSYNC;
    pWal = walOpen(pathName, pCfg);
wafwerar's avatar
wafwerar 已提交
30
    taosMemoryFree(pCfg);
L
Liu Jicong 已提交
31 32
    ASSERT(pWal != NULL);
  }
L
Liu Jicong 已提交
33

L
Liu Jicong 已提交
34 35 36 37
  void TearDown() override {
    walClose(pWal);
    pWal = NULL;
  }
L
Liu Jicong 已提交
38

L
Liu Jicong 已提交
39 40
  SWal*       pWal = NULL;
  const char* pathName = "/tmp/wal_test";
L
Liu Jicong 已提交
41 42
};

L
Liu Jicong 已提交
43
class WalCleanDeleteEnv : public ::testing::Test {
L
Liu Jicong 已提交
44 45 46 47 48
 protected:
  static void SetUpTestCase() {
    int code = walInit();
    ASSERT(code == 0);
  }
L
Liu Jicong 已提交
49

L
Liu Jicong 已提交
50 51 52 53
  static void TearDownTestCase() { walCleanUp(); }

  void SetUp() override {
    taosRemoveDir(pathName);
wafwerar's avatar
wafwerar 已提交
54
    SWalCfg* pCfg = (SWalCfg*)taosMemoryMalloc(sizeof(SWalCfg));
L
Liu Jicong 已提交
55 56 57 58 59
    memset(pCfg, 0, sizeof(SWalCfg));
    pCfg->retentionPeriod = 0;
    pCfg->retentionSize = 0;
    pCfg->level = TAOS_WAL_FSYNC;
    pWal = walOpen(pathName, pCfg);
wafwerar's avatar
wafwerar 已提交
60
    taosMemoryFree(pCfg);
L
Liu Jicong 已提交
61 62
    ASSERT(pWal != NULL);
  }
L
Liu Jicong 已提交
63

L
Liu Jicong 已提交
64 65 66 67
  void TearDown() override {
    walClose(pWal);
    pWal = NULL;
  }
L
Liu Jicong 已提交
68

L
Liu Jicong 已提交
69 70
  SWal*       pWal = NULL;
  const char* pathName = "/tmp/wal_test";
L
Liu Jicong 已提交
71 72
};

L
Liu Jicong 已提交
73
class WalKeepEnv : public ::testing::Test {
L
Liu Jicong 已提交
74 75 76 77 78
 protected:
  static void SetUpTestCase() {
    int code = walInit();
    ASSERT(code == 0);
  }
L
Liu Jicong 已提交
79

L
Liu Jicong 已提交
80
  static void TearDownTestCase() { walCleanUp(); }
L
Liu Jicong 已提交
81

L
Liu Jicong 已提交
82 83 84 85 86
  void walResetEnv() {
    TearDown();
    taosRemoveDir(pathName);
    SetUp();
  }
L
Liu Jicong 已提交
87

L
Liu Jicong 已提交
88
  void SetUp() override {
wafwerar's avatar
wafwerar 已提交
89
    SWalCfg* pCfg = (SWalCfg*)taosMemoryMalloc(sizeof(SWalCfg));
L
Liu Jicong 已提交
90 91 92 93 94 95 96
    memset(pCfg, 0, sizeof(SWalCfg));
    pCfg->rollPeriod = -1;
    pCfg->segSize = -1;
    pCfg->retentionPeriod = 0;
    pCfg->retentionSize = 0;
    pCfg->level = TAOS_WAL_FSYNC;
    pWal = walOpen(pathName, pCfg);
wafwerar's avatar
wafwerar 已提交
97
    taosMemoryFree(pCfg);
L
Liu Jicong 已提交
98 99
    ASSERT(pWal != NULL);
  }
L
Liu Jicong 已提交
100

L
Liu Jicong 已提交
101 102 103 104
  void TearDown() override {
    walClose(pWal);
    pWal = NULL;
  }
L
Liu Jicong 已提交
105

L
Liu Jicong 已提交
106 107
  SWal*       pWal = NULL;
  const char* pathName = "/tmp/wal_test";
L
Liu Jicong 已提交
108 109
};

L
Liu Jicong 已提交
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
class WalRetentionEnv : public ::testing::Test {
 protected:
  static void SetUpTestCase() {
    int code = walInit();
    ASSERT(code == 0);
  }

  static void TearDownTestCase() { walCleanUp(); }

  void walResetEnv() {
    TearDown();
    taosRemoveDir(pathName);
    SetUp();
  }

  void SetUp() override {
    SWalCfg cfg;
L
Liu Jicong 已提交
127 128 129 130 131 132 133
    cfg.rollPeriod = -1;
    cfg.segSize = -1;
    cfg.retentionPeriod = -1;
    cfg.retentionSize = 0;
    cfg.rollPeriod = 0;
    cfg.vgId = 0;
    cfg.level = TAOS_WAL_FSYNC;
L
Liu Jicong 已提交
134 135 136 137 138 139 140 141 142 143 144 145 146
    pWal = walOpen(pathName, &cfg);
    ASSERT(pWal != NULL);
  }

  void TearDown() override {
    walClose(pWal);
    pWal = NULL;
  }

  SWal*       pWal = NULL;
  const char* pathName = "/tmp/wal_test";
};

L
Liu Jicong 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
TEST_F(WalCleanEnv, createNew) {
  walRollFileInfo(pWal);
  ASSERT(pWal->fileInfoSet != NULL);
  ASSERT_EQ(pWal->fileInfoSet->size, 1);
  WalFileInfo* pInfo = (WalFileInfo*)taosArrayGetLast(pWal->fileInfoSet);
  ASSERT_EQ(pInfo->firstVer, 0);
  ASSERT_EQ(pInfo->lastVer, -1);
  ASSERT_EQ(pInfo->closeTs, -1);
  ASSERT_EQ(pInfo->fileSize, 0);
}

TEST_F(WalCleanEnv, serialize) {
  int code = walRollFileInfo(pWal);
  ASSERT(code == 0);
  ASSERT(pWal->fileInfoSet != NULL);

  code = walRollFileInfo(pWal);
  ASSERT(code == 0);
  code = walRollFileInfo(pWal);
  ASSERT(code == 0);
  code = walRollFileInfo(pWal);
  ASSERT(code == 0);
  code = walRollFileInfo(pWal);
  ASSERT(code == 0);
  code = walRollFileInfo(pWal);
  ASSERT(code == 0);
L
Liu Jicong 已提交
173
  char* ss = walMetaSerialize(pWal);
L
Liu Jicong 已提交
174
  printf("%s\n", ss);
wafwerar's avatar
wafwerar 已提交
175
  taosMemoryFree(ss);
L
Liu Jicong 已提交
176
  code = walSaveMeta(pWal);
L
Liu Jicong 已提交
177 178 179 180 181 182 183
  ASSERT(code == 0);
}

TEST_F(WalCleanEnv, removeOldMeta) {
  int code = walRollFileInfo(pWal);
  ASSERT(code == 0);
  ASSERT(pWal->fileInfoSet != NULL);
L
Liu Jicong 已提交
184
  code = walSaveMeta(pWal);
L
Liu Jicong 已提交
185 186 187
  ASSERT(code == 0);
  code = walRollFileInfo(pWal);
  ASSERT(code == 0);
L
Liu Jicong 已提交
188
  code = walSaveMeta(pWal);
L
Liu Jicong 已提交
189 190 191
  ASSERT(code == 0);
}

L
Liu Jicong 已提交
192 193 194 195
TEST_F(WalKeepEnv, readOldMeta) {
  walResetEnv();
  int code;

L
Liu Jicong 已提交
196 197
  for (int i = 0; i < 10; i++) {
    code = walWrite(pWal, i, i + 1, (void*)ranStr, ranStrLen);
L
Liu Jicong 已提交
198
    ASSERT_EQ(code, 0);
L
Liu Jicong 已提交
199
    ASSERT_EQ(pWal->vers.lastVer, i);
L
Liu Jicong 已提交
200
    code = walWrite(pWal, i + 2, i, (void*)ranStr, ranStrLen);
L
Liu Jicong 已提交
201
    ASSERT_EQ(code, -1);
L
Liu Jicong 已提交
202
    ASSERT_EQ(pWal->vers.lastVer, i);
L
Liu Jicong 已提交
203 204 205 206 207 208
  }
  char* oldss = walMetaSerialize(pWal);

  TearDown();
  SetUp();

L
Liu Jicong 已提交
209 210
  ASSERT_EQ(pWal->vers.firstVer, 0);
  ASSERT_EQ(pWal->vers.lastVer, 9);
L
Liu Jicong 已提交
211 212 213

  char* newss = walMetaSerialize(pWal);

L
Liu Jicong 已提交
214
  int len = strlen(oldss);
L
Liu Jicong 已提交
215
  ASSERT_EQ(len, strlen(newss));
L
Liu Jicong 已提交
216
  for (int i = 0; i < len; i++) {
L
Liu Jicong 已提交
217 218
    EXPECT_EQ(oldss[i], newss[i]);
  }
wafwerar's avatar
wafwerar 已提交
219 220
  taosMemoryFree(oldss);
  taosMemoryFree(newss);
L
Liu Jicong 已提交
221
}
L
Liu Jicong 已提交
222 223 224

TEST_F(WalCleanEnv, write) {
  int code;
L
Liu Jicong 已提交
225 226
  for (int i = 0; i < 10; i++) {
    code = walWrite(pWal, i, i + 1, (void*)ranStr, ranStrLen);
L
Liu Jicong 已提交
227
    ASSERT_EQ(code, 0);
L
Liu Jicong 已提交
228
    ASSERT_EQ(pWal->vers.lastVer, i);
L
Liu Jicong 已提交
229
    code = walWrite(pWal, i + 2, i, (void*)ranStr, ranStrLen);
L
Liu Jicong 已提交
230
    ASSERT_EQ(code, -1);
L
Liu Jicong 已提交
231
    ASSERT_EQ(pWal->vers.lastVer, i);
L
Liu Jicong 已提交
232
  }
L
Liu Jicong 已提交
233
  code = walSaveMeta(pWal);
L
Liu Jicong 已提交
234
  ASSERT_EQ(code, 0);
L
Liu Jicong 已提交
235 236
}

L
Liu Jicong 已提交
237
TEST_F(WalCleanEnv, rollback) {
L
Liu Jicong 已提交
238
  int code;
L
Liu Jicong 已提交
239 240
  for (int i = 0; i < 10; i++) {
    code = walWrite(pWal, i, i + 1, (void*)ranStr, ranStrLen);
L
Liu Jicong 已提交
241
    ASSERT_EQ(code, 0);
L
Liu Jicong 已提交
242
    ASSERT_EQ(pWal->vers.lastVer, i);
L
Liu Jicong 已提交
243
  }
L
Liu Jicong 已提交
244 245 246 247 248 249
  code = walRollback(pWal, 12);
  ASSERT_NE(code, 0);
  ASSERT_EQ(pWal->vers.lastVer, 9);
  code = walRollback(pWal, 9);
  ASSERT_EQ(code, 0);
  ASSERT_EQ(pWal->vers.lastVer, 8);
L
Liu Jicong 已提交
250 251
  code = walRollback(pWal, 5);
  ASSERT_EQ(code, 0);
L
Liu Jicong 已提交
252
  ASSERT_EQ(pWal->vers.lastVer, 4);
L
Liu Jicong 已提交
253 254
  code = walRollback(pWal, 3);
  ASSERT_EQ(code, 0);
L
Liu Jicong 已提交
255
  ASSERT_EQ(pWal->vers.lastVer, 2);
L
Liu Jicong 已提交
256
  code = walSaveMeta(pWal);
L
Liu Jicong 已提交
257 258 259 260 261 262
  ASSERT_EQ(code, 0);
}

TEST_F(WalCleanDeleteEnv, roll) {
  int code;
  int i;
L
Liu Jicong 已提交
263
  for (i = 0; i < 100; i++) {
L
Liu Jicong 已提交
264
    code = walWrite(pWal, i, 0, (void*)ranStr, ranStrLen);
L
Liu Jicong 已提交
265
    ASSERT_EQ(code, 0);
L
Liu Jicong 已提交
266
    ASSERT_EQ(pWal->vers.lastVer, i);
L
Liu Jicong 已提交
267
    code = walCommit(pWal, i);
L
Liu Jicong 已提交
268
    ASSERT_EQ(pWal->vers.commitVer, i);
L
Liu Jicong 已提交
269 270
  }

L
Liu Jicong 已提交
271 272
  walBeginSnapshot(pWal, i - 1);
  ASSERT_EQ(pWal->vers.verInSnapshotting, i - 1);
L
Liu Jicong 已提交
273
  walEndSnapshot(pWal);
L
Liu Jicong 已提交
274
  ASSERT_EQ(pWal->vers.snapshotVer, i - 1);
L
Liu Jicong 已提交
275
  ASSERT_EQ(pWal->vers.verInSnapshotting, -1);
L
Liu Jicong 已提交
276

L
Liu Jicong 已提交
277
  code = walWrite(pWal, 5, 0, (void*)ranStr, ranStrLen);
L
Liu Jicong 已提交
278 279
  ASSERT_NE(code, 0);

L
Liu Jicong 已提交
280
  for (; i < 200; i++) {
L
Liu Jicong 已提交
281
    code = walWrite(pWal, i, 0, (void*)ranStr, ranStrLen);
L
Liu Jicong 已提交
282 283
    ASSERT_EQ(code, 0);
    code = walCommit(pWal, i);
L
Liu Jicong 已提交
284
    ASSERT_EQ(pWal->vers.commitVer, i);
L
Liu Jicong 已提交
285 286
  }

L
Liu Jicong 已提交
287
  code = walBeginSnapshot(pWal, i - 1);
L
Liu Jicong 已提交
288
  ASSERT_EQ(code, 0);
L
Liu Jicong 已提交
289
  code = walEndSnapshot(pWal);
L
Liu Jicong 已提交
290 291
  ASSERT_EQ(code, 0);
}
L
Liu Jicong 已提交
292 293 294

TEST_F(WalKeepEnv, readHandleRead) {
  walResetEnv();
L
Liu Jicong 已提交
295
  int             code;
L
Liu Jicong 已提交
296 297 298
  SWalReadHandle* pRead = walOpenReadHandle(pWal);
  ASSERT(pRead != NULL);

L
Liu Jicong 已提交
299 300
  int i;
  for (i = 0; i < 100; i++) {
L
Liu Jicong 已提交
301 302 303 304 305 306
    char newStr[100];
    sprintf(newStr, "%s-%d", ranStr, i);
    int len = strlen(newStr);
    code = walWrite(pWal, i, 0, newStr, len);
    ASSERT_EQ(code, 0);
  }
L
Liu Jicong 已提交
307
  for (int i = 0; i < 1000; i++) {
wafwerar's avatar
wafwerar 已提交
308
    int ver = taosRand() % 100;
L
Liu Jicong 已提交
309 310 311
    code = walReadWithHandle(pRead, ver);
    ASSERT_EQ(code, 0);

L
Liu Jicong 已提交
312 313 314
    // printf("rrbody: \n");
    // for(int i = 0; i < pRead->pHead->head.len; i++) {
    // printf("%d ", pRead->pHead->head.body[i]);
L
Liu Jicong 已提交
315
    //}
L
Liu Jicong 已提交
316
    // printf("\n");
L
Liu Jicong 已提交
317 318

    ASSERT_EQ(pRead->pHead->head.version, ver);
L
Liu Jicong 已提交
319
    ASSERT_EQ(pRead->curVersion, ver + 1);
L
Liu Jicong 已提交
320 321 322 323
    char newStr[100];
    sprintf(newStr, "%s-%d", ranStr, ver);
    int len = strlen(newStr);
    ASSERT_EQ(pRead->pHead->head.len, len);
L
Liu Jicong 已提交
324
    for (int j = 0; j < len; j++) {
L
Liu Jicong 已提交
325 326 327 328
      EXPECT_EQ(newStr[j], pRead->pHead->head.body[j]);
    }
  }
}
L
Liu Jicong 已提交
329 330 331 332

TEST_F(WalRetentionEnv, repairMeta1) {
  walResetEnv();
  int code;
L
Liu Jicong 已提交
333

L
Liu Jicong 已提交
334 335 336 337 338 339 340 341 342 343 344
  int i;
  for (i = 0; i < 100; i++) {
    char newStr[100];
    sprintf(newStr, "%s-%d", ranStr, i);
    int len = strlen(newStr);
    code = walWrite(pWal, i, 0, newStr, len);
    ASSERT_EQ(code, 0);
  }

  TearDown();

L
Liu Jicong 已提交
345
  // getchar();
L
Liu Jicong 已提交
346 347
  char buf[100];
  sprintf(buf, "%s/meta-ver%d", pathName, 0);
348
  taosRemoveFile(buf);
L
Liu Jicong 已提交
349
  sprintf(buf, "%s/meta-ver%d", pathName, 1);
350
  taosRemoveFile(buf);
L
Liu Jicong 已提交
351
  SetUp();
L
Liu Jicong 已提交
352
  // getchar();
L
Liu Jicong 已提交
353 354 355 356 357 358 359

  ASSERT_EQ(pWal->vers.lastVer, 99);

  SWalReadHandle* pRead = walOpenReadHandle(pWal);
  ASSERT(pRead != NULL);

  for (int i = 0; i < 1000; i++) {
wafwerar's avatar
wafwerar 已提交
360
    int ver = taosRand() % 100;
L
Liu Jicong 已提交
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
    code = walReadWithHandle(pRead, ver);
    ASSERT_EQ(code, 0);

    // printf("rrbody: \n");
    // for(int i = 0; i < pRead->pHead->head.len; i++) {
    // printf("%d ", pRead->pHead->head.body[i]);
    //}
    // printf("\n");

    ASSERT_EQ(pRead->pHead->head.version, ver);
    ASSERT_EQ(pRead->curVersion, ver + 1);
    char newStr[100];
    sprintf(newStr, "%s-%d", ranStr, ver);
    int len = strlen(newStr);
    ASSERT_EQ(pRead->pHead->head.len, len);
    for (int j = 0; j < len; j++) {
      EXPECT_EQ(newStr[j], pRead->pHead->head.body[j]);
    }
  }

  for (i = 100; i < 200; i++) {
    char newStr[100];
    sprintf(newStr, "%s-%d", ranStr, i);
    int len = strlen(newStr);
    code = walWrite(pWal, i, 0, newStr, len);
    ASSERT_EQ(code, 0);
  }

L
Liu Jicong 已提交
389
  for (int i = 0; i < 1000; i++) {
wafwerar's avatar
wafwerar 已提交
390
    int ver = taosRand() % 200;
L
Liu Jicong 已提交
391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409
    code = walReadWithHandle(pRead, ver);
    ASSERT_EQ(code, 0);

    // printf("rrbody: \n");
    // for(int i = 0; i < pRead->pHead->head.len; i++) {
    // printf("%d ", pRead->pHead->head.body[i]);
    //}
    // printf("\n");

    ASSERT_EQ(pRead->pHead->head.version, ver);
    ASSERT_EQ(pRead->curVersion, ver + 1);
    char newStr[100];
    sprintf(newStr, "%s-%d", ranStr, ver);
    int len = strlen(newStr);
    ASSERT_EQ(pRead->pHead->head.len, len);
    for (int j = 0; j < len; j++) {
      EXPECT_EQ(newStr[j], pRead->pHead->head.body[j]);
    }
  }
L
Liu Jicong 已提交
410
}