walMetaTest.cpp 9.6 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 22 23 24 25 26 27 28 29 30 31 32
  static void TearDownTestCase() { walCleanUp(); }

  void SetUp() override {
    taosRemoveDir(pathName);
    SWalCfg* pCfg = (SWalCfg*)malloc(sizeof(SWalCfg));
    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);
    free(pCfg);
    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 54 55 56 57 58 59 60 61 62
  static void TearDownTestCase() { walCleanUp(); }

  void SetUp() override {
    taosRemoveDir(pathName);
    SWalCfg* pCfg = (SWalCfg*)malloc(sizeof(SWalCfg));
    memset(pCfg, 0, sizeof(SWalCfg));
    pCfg->retentionPeriod = 0;
    pCfg->retentionSize = 0;
    pCfg->level = TAOS_WAL_FSYNC;
    pWal = walOpen(pathName, pCfg);
    free(pCfg);
    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 89 90 91 92 93 94 95 96 97 98 99
  void SetUp() override {
    SWalCfg* pCfg = (SWalCfg*)malloc(sizeof(SWalCfg));
    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);
    free(pCfg);
    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
    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 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141
    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 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
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 已提交
168
  char* ss = walMetaSerialize(pWal);
L
Liu Jicong 已提交
169
  printf("%s\n", ss);
L
Liu Jicong 已提交
170
  free(ss);
L
Liu Jicong 已提交
171
  code = walSaveMeta(pWal);
L
Liu Jicong 已提交
172 173 174 175 176 177 178
  ASSERT(code == 0);
}

TEST_F(WalCleanEnv, removeOldMeta) {
  int code = walRollFileInfo(pWal);
  ASSERT(code == 0);
  ASSERT(pWal->fileInfoSet != NULL);
L
Liu Jicong 已提交
179
  code = walSaveMeta(pWal);
L
Liu Jicong 已提交
180 181 182
  ASSERT(code == 0);
  code = walRollFileInfo(pWal);
  ASSERT(code == 0);
L
Liu Jicong 已提交
183
  code = walSaveMeta(pWal);
L
Liu Jicong 已提交
184 185 186
  ASSERT(code == 0);
}

L
Liu Jicong 已提交
187 188 189 190
TEST_F(WalKeepEnv, readOldMeta) {
  walResetEnv();
  int code;

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

  TearDown();
  SetUp();

L
Liu Jicong 已提交
204 205
  ASSERT_EQ(pWal->vers.firstVer, 0);
  ASSERT_EQ(pWal->vers.lastVer, 9);
L
Liu Jicong 已提交
206 207 208

  char* newss = walMetaSerialize(pWal);

L
Liu Jicong 已提交
209
  int len = strlen(oldss);
L
Liu Jicong 已提交
210
  ASSERT_EQ(len, strlen(newss));
L
Liu Jicong 已提交
211
  for (int i = 0; i < len; i++) {
L
Liu Jicong 已提交
212 213 214 215 216
    EXPECT_EQ(oldss[i], newss[i]);
  }
  free(oldss);
  free(newss);
}
L
Liu Jicong 已提交
217 218 219

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

L
Liu Jicong 已提交
232
TEST_F(WalCleanEnv, rollback) {
L
Liu Jicong 已提交
233
  int code;
L
Liu Jicong 已提交
234 235
  for (int i = 0; i < 10; i++) {
    code = walWrite(pWal, i, i + 1, (void*)ranStr, ranStrLen);
L
Liu Jicong 已提交
236
    ASSERT_EQ(code, 0);
L
Liu Jicong 已提交
237
    ASSERT_EQ(pWal->vers.lastVer, i);
L
Liu Jicong 已提交
238
  }
L
Liu Jicong 已提交
239 240 241 242 243 244
  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 已提交
245 246
  code = walRollback(pWal, 5);
  ASSERT_EQ(code, 0);
L
Liu Jicong 已提交
247
  ASSERT_EQ(pWal->vers.lastVer, 4);
L
Liu Jicong 已提交
248 249
  code = walRollback(pWal, 3);
  ASSERT_EQ(code, 0);
L
Liu Jicong 已提交
250
  ASSERT_EQ(pWal->vers.lastVer, 2);
L
Liu Jicong 已提交
251
  code = walSaveMeta(pWal);
L
Liu Jicong 已提交
252 253 254 255 256 257
  ASSERT_EQ(code, 0);
}

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

L
Liu Jicong 已提交
266 267
  walBeginSnapshot(pWal, i - 1);
  ASSERT_EQ(pWal->vers.verInSnapshotting, i - 1);
L
Liu Jicong 已提交
268
  walEndSnapshot(pWal);
L
Liu Jicong 已提交
269
  ASSERT_EQ(pWal->vers.snapshotVer, i - 1);
L
Liu Jicong 已提交
270
  ASSERT_EQ(pWal->vers.verInSnapshotting, -1);
L
Liu Jicong 已提交
271

L
Liu Jicong 已提交
272
  code = walWrite(pWal, 5, 0, (void*)ranStr, ranStrLen);
L
Liu Jicong 已提交
273 274
  ASSERT_NE(code, 0);

L
Liu Jicong 已提交
275
  for (; i < 200; i++) {
L
Liu Jicong 已提交
276
    code = walWrite(pWal, i, 0, (void*)ranStr, ranStrLen);
L
Liu Jicong 已提交
277 278
    ASSERT_EQ(code, 0);
    code = walCommit(pWal, i);
L
Liu Jicong 已提交
279
    ASSERT_EQ(pWal->vers.commitVer, i);
L
Liu Jicong 已提交
280 281
  }

L
Liu Jicong 已提交
282
  code = walBeginSnapshot(pWal, i - 1);
L
Liu Jicong 已提交
283
  ASSERT_EQ(code, 0);
L
Liu Jicong 已提交
284
  code = walEndSnapshot(pWal);
L
Liu Jicong 已提交
285 286
  ASSERT_EQ(code, 0);
}
L
Liu Jicong 已提交
287 288 289

TEST_F(WalKeepEnv, readHandleRead) {
  walResetEnv();
L
Liu Jicong 已提交
290
  int             code;
L
Liu Jicong 已提交
291 292 293
  SWalReadHandle* pRead = walOpenReadHandle(pWal);
  ASSERT(pRead != NULL);

L
Liu Jicong 已提交
294 295
  int i;
  for (i = 0; i < 100; i++) {
L
Liu Jicong 已提交
296 297 298 299 300 301
    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 已提交
302
  for (int i = 0; i < 1000; i++) {
wafwerar's avatar
wafwerar 已提交
303
    int ver = taosRand() % 100;
L
Liu Jicong 已提交
304 305 306
    code = walReadWithHandle(pRead, ver);
    ASSERT_EQ(code, 0);

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

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

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

L
Liu Jicong 已提交
329 330 331 332 333 334 335 336 337 338 339
  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 已提交
340
  // getchar();
L
Liu Jicong 已提交
341 342
  char buf[100];
  sprintf(buf, "%s/meta-ver%d", pathName, 0);
343
  taosRemoveFile(buf);
L
Liu Jicong 已提交
344
  sprintf(buf, "%s/meta-ver%d", pathName, 1);
345
  taosRemoveFile(buf);
L
Liu Jicong 已提交
346
  SetUp();
L
Liu Jicong 已提交
347
  // getchar();
L
Liu Jicong 已提交
348 349 350 351 352 353 354

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

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

  for (int i = 0; i < 1000; i++) {
wafwerar's avatar
wafwerar 已提交
355
    int ver = taosRand() % 100;
L
Liu Jicong 已提交
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
    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 已提交
384
  for (int i = 0; i < 1000; i++) {
wafwerar's avatar
wafwerar 已提交
385
    int ver = taosRand() % 200;
L
Liu Jicong 已提交
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
    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 已提交
405
}