walMetaTest.cpp 9.4 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 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
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;
    cfg.rollPeriod = -1,
    cfg.segSize = -1,
    cfg.retentionPeriod = -1,
    cfg.retentionSize = 0,
    cfg.rollPeriod = 0,
    cfg.vgId = 0,
    cfg.level = TAOS_WAL_FSYNC;
    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);
L
Liu Jicong 已提交
175
  free(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 219 220 221
    EXPECT_EQ(oldss[i], newss[i]);
  }
  free(oldss);
  free(newss);
}
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
  code = walRollback(pWal, 5);
  ASSERT_EQ(code, 0);
L
Liu Jicong 已提交
246
  ASSERT_EQ(pWal->vers.lastVer, 4);
L
Liu Jicong 已提交
247 248
  code = walRollback(pWal, 3);
  ASSERT_EQ(code, 0);
L
Liu Jicong 已提交
249
  ASSERT_EQ(pWal->vers.lastVer, 2);
L
Liu Jicong 已提交
250
  code = walSaveMeta(pWal);
L
Liu Jicong 已提交
251 252 253 254 255 256
  ASSERT_EQ(code, 0);
}

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

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

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

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

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

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

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

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

    ASSERT_EQ(pRead->pHead->head.version, ver);
L
Liu Jicong 已提交
313
    ASSERT_EQ(pRead->curVersion, ver + 1);
L
Liu Jicong 已提交
314 315 316 317
    char newStr[100];
    sprintf(newStr, "%s-%d", ranStr, ver);
    int len = strlen(newStr);
    ASSERT_EQ(pRead->pHead->head.len, len);
L
Liu Jicong 已提交
318
    for (int j = 0; j < len; j++) {
L
Liu Jicong 已提交
319 320 321 322
      EXPECT_EQ(newStr[j], pRead->pHead->head.body[j]);
    }
  }
}
L
Liu Jicong 已提交
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341

TEST_F(WalRetentionEnv, repairMeta1) {
  walResetEnv();
  int code;
  
  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();

  //getchar();
  char buf[100];
  sprintf(buf, "%s/meta-ver%d", pathName, 0);
342
  taosRemoveFile(buf);
L
Liu Jicong 已提交
343
  sprintf(buf, "%s/meta-ver%d", pathName, 1);
344
  taosRemoveFile(buf);
L
Liu Jicong 已提交
345
  SetUp();
L
Liu Jicong 已提交
346
  //getchar();
L
Liu Jicong 已提交
347 348 349 350 351 352 353 354 355 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

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

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

  for (int i = 0; i < 1000; i++) {
    int ver = rand() % 100;
    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 已提交
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
  for (int i = 0; i < 1000; i++) {
    int ver = rand() % 200;
    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
}