waltest.c 3.6 KB
Newer Older
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

//#define _DEFAULT_SOURCE
S
slguan 已提交
17
#include "os.h"
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
18
#include "tutil.h"
S
slguan 已提交
19
#include "tglobal.h"
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
20 21 22 23 24 25
#include "tlog.h"
#include "twal.h"

int64_t  ver = 0;
void    *pWal = NULL;

S
TD-1918  
Shengliang Guan 已提交
26
int writeToQueue(void *pVnode, void *data, int type, void *pMsg) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
27
  // do nothing
J
Jeff Tao 已提交
28 29
  SWalHead *pHead = data;

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43
  if (pHead->version > ver)
    ver = pHead->version;

  walWrite(pWal, pHead);

  return 0;
}

int main(int argc, char *argv[]) {
  char path[128] = "/home/jhtao/test/wal";
  int  level = 2;
  int  total = 5;
  int  rows = 10000;
  int  size = 128;
J
Jeff Tao 已提交
44
  int  keep = 0;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
45 46 47

  for (int i=1; i<argc; ++i) {
    if (strcmp(argv[i], "-p")==0 && i < argc-1) {
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
48
      tstrncpy(path, argv[++i], sizeof(path));
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
49 50 51 52
    } else if (strcmp(argv[i], "-l")==0 && i < argc-1) {
      level = atoi(argv[++i]);
    } else if (strcmp(argv[i], "-r")==0 && i < argc-1) {
      rows = atoi(argv[++i]);
J
Jeff Tao 已提交
53 54
    } else if (strcmp(argv[i], "-k")==0 && i < argc-1) {
      keep = atoi(argv[++i]);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
55 56 57 58 59 60 61
    } else if (strcmp(argv[i], "-t")==0 && i < argc-1) {
      total = atoi(argv[++i]);
    } else if (strcmp(argv[i], "-s")==0 && i < argc-1) {
      size = atoi(argv[++i]);
    } else if (strcmp(argv[i], "-v")==0 && i < argc-1) {
      ver = atoll(argv[++i]);
    } else if (strcmp(argv[i], "-d")==0 && i < argc-1) {
62
      dDebugFlag = atoi(argv[++i]);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
63 64 65 66 67 68
    } else {
      printf("\nusage: %s [options] \n", argv[0]);
      printf("  [-p path]: wal file path default is:%s\n", path);
      printf("  [-l level]: log level, default is:%d\n", level);
      printf("  [-t total]: total wal files, default is:%d\n", total);
      printf("  [-r rows]: rows of records per wal file, default is:%d\n", rows);
J
Jeff Tao 已提交
69
      printf("  [-k keep]: keep the wal after closing, default is:%d\n", keep);
S
TD-1530  
Shengliang Guan 已提交
70
      printf("  [-v version]: initial version, default is:%" PRId64 "\n", ver);
71
      printf("  [-d debugFlag]: debug flag, default:%d\n", dDebugFlag);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
72 73 74 75 76 77 78
      printf("  [-h help]: print out this help\n\n");
      exit(0);
    }
  } 

  taosInitLog("wal.log", 100000, 10);

M
Minglei Jin 已提交
79
  SWalCfg walCfg = {0};
H
hjxilinx 已提交
80
  walCfg.walLevel = level;
J
Jeff Tao 已提交
81
  walCfg.keep = keep;
J
Jeff Tao 已提交
82 83

  pWal = walOpen(path, &walCfg);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
84 85 86 87 88 89 90 91 92 93 94
  if (pWal == NULL) {
    printf("failed to open wal\n");
    exit(-1);
  }

  int ret = walRestore(pWal, NULL, writeToQueue);
  if (ret <0) {
    printf("failed to restore wal\n");
    exit(-1);
  }

S
TD-1530  
Shengliang Guan 已提交
95
  printf("version starts from:%" PRId64 "\n", ver);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
96 97 98 99 100 101 102
  
  int contLen = sizeof(SWalHead) + size;
  SWalHead *pHead = (SWalHead *) malloc(contLen);

  for (int i=0; i<total; ++i) {
    for (int k=0; k<rows; ++k) {
      pHead->version = ++ver;
103
      pHead->len = size;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
104 105 106 107 108 109 110 111
      walWrite(pWal, pHead);
    }
       
    printf("renew a wal, i:%d\n", i);
    walRenew(pWal);
  }

  printf("%d wal files are written\n", total);
112

S
TD-1846  
Shengliang Guan 已提交
113
  int64_t index = 0;
S
TD-1856  
Shengliang Guan 已提交
114
  char    name[256];
115 116 117 118

  while (1) {
    int code = walGetWalFile(pWal, name, &index);
    if (code == -1) {
S
TD-1846  
Shengliang Guan 已提交
119
      printf("failed to get wal file, index:%" PRId64 "\n", index);
120 121 122
      break;
    }

S
TD-1846  
Shengliang Guan 已提交
123
    printf("index:%" PRId64 " wal:%s\n", index, name);
124 125 126 127 128
    if (code == 0) break;

    index++;
  }

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
129 130 131 132 133 134
  getchar();

  walClose(pWal);

  return 0;
}