walMeta.c 13.7 KB
Newer Older
L
Liu Jicong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

L
Liu Jicong 已提交
16
#include "cJSON.h"
L
Liu Jicong 已提交
17 18
#include "os.h"
#include "taoserror.h"
L
Liu Jicong 已提交
19
#include "tutil.h"
L
Liu Jicong 已提交
20 21
#include "walInt.h"

22 23 24 25
bool FORCE_INLINE walLogExist(SWal* pWal, int64_t ver) {
  return !walIsEmpty(pWal) && walGetFirstVer(pWal) <= ver && walGetLastVer(pWal) >= ver;
}

L
Liu Jicong 已提交
26 27
bool FORCE_INLINE walIsEmpty(SWal* pWal) { return pWal->vers.firstVer == -1; }

L
Liu Jicong 已提交
28
int64_t FORCE_INLINE walGetFirstVer(SWal* pWal) { return pWal->vers.firstVer; }
L
Liu Jicong 已提交
29

L
Liu Jicong 已提交
30
int64_t FORCE_INLINE walGetSnaphostVer(SWal* pWal) { return pWal->vers.snapshotVer; }
L
Liu Jicong 已提交
31

L
Liu Jicong 已提交
32
int64_t FORCE_INLINE walGetLastVer(SWal* pWal) { return pWal->vers.lastVer; }
L
Liu Jicong 已提交
33

L
Liu Jicong 已提交
34 35
int64_t FORCE_INLINE walGetCommittedVer(SWal* pWal) { return pWal->vers.commitVer; }

L
Liu Jicong 已提交
36
static FORCE_INLINE int walBuildMetaName(SWal* pWal, int metaVer, char* buf) {
L
Liu Jicong 已提交
37 38 39
  return sprintf(buf, "%s/meta-ver%d", pWal->path, metaVer);
}

L
Liu Jicong 已提交
40
static FORCE_INLINE int64_t walScanLogGetLastVer(SWal* pWal) {
L
Liu Jicong 已提交
41
  ASSERT(pWal->fileInfoSet != NULL);
L
Liu Jicong 已提交
42
  int32_t sz = taosArrayGetSize(pWal->fileInfoSet);
L
Liu Jicong 已提交
43
  ASSERT(sz > 0);
L
Liu Jicong 已提交
44
#if 0
L
Liu Jicong 已提交
45 46 47
  for (int i = 0; i < sz; i++) {
    SWalFileInfo* pFileInfo = taosArrayGet(pWal->fileInfoSet, i);
  }
L
Liu Jicong 已提交
48 49
#endif

L
Liu Jicong 已提交
50 51
  SWalFileInfo* pLastFileInfo = taosArrayGet(pWal->fileInfoSet, sz - 1);
  char          fnameStr[WAL_FILE_LEN];
L
Liu Jicong 已提交
52 53
  walBuildLogName(pWal, pLastFileInfo->firstVer, fnameStr);

L
Liu Jicong 已提交
54 55 56 57
  int64_t fileSize = 0;
  taosStatFile(fnameStr, &fileSize, NULL);
  int readSize = TMIN(WAL_MAX_SIZE + 2, fileSize);
  pLastFileInfo->fileSize = fileSize;
L
Liu Jicong 已提交
58

59 60
  TdFilePtr pFile = taosOpenFile(fnameStr, TD_FILE_READ);
  if (pFile == NULL) {
L
Liu Jicong 已提交
61 62 63 64 65 66
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

  uint64_t magic = WAL_MAGIC;

wafwerar's avatar
wafwerar 已提交
67
  char* buf = taosMemoryMalloc(readSize + 5);
L
Liu Jicong 已提交
68
  if (buf == NULL) {
69
    taosCloseFile(&pFile);
L
Liu Jicong 已提交
70 71 72 73
    terrno = TSDB_CODE_WAL_OUT_OF_MEMORY;
    return -1;
  }

74 75
  taosLSeekFile(pFile, -readSize, SEEK_END);
  if (readSize != taosReadFile(pFile, buf, readSize)) {
wafwerar's avatar
wafwerar 已提交
76
    taosMemoryFree(buf);
77
    taosCloseFile(&pFile);
L
Liu Jicong 已提交
78 79 80
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }
L
Liu Jicong 已提交
81

L
Liu Jicong 已提交
82 83
  char* haystack = buf;
  char* found = NULL;
L
Liu Jicong 已提交
84 85
  char* candidate;
  while ((candidate = tmemmem(haystack, readSize - (haystack - buf), (char*)&magic, sizeof(uint64_t))) != NULL) {
L
Liu Jicong 已提交
86
    // read and validate
L
Liu Jicong 已提交
87
    SWalCkHead* logContent = (SWalCkHead*)candidate;
L
Liu Jicong 已提交
88
    if (walValidHeadCksum(logContent) == 0 && walValidBodyCksum(logContent) == 0) {
L
Liu Jicong 已提交
89 90 91 92 93
      found = candidate;
    }
    haystack = candidate + 1;
  }
  if (found == buf) {
L
Liu Jicong 已提交
94
    SWalCkHead* logContent = (SWalCkHead*)found;
L
Liu Jicong 已提交
95 96
    if (walValidHeadCksum(logContent) != 0 || walValidBodyCksum(logContent) != 0) {
      // file has to be deleted
wafwerar's avatar
wafwerar 已提交
97
      taosMemoryFree(buf);
98
      taosCloseFile(&pFile);
L
Liu Jicong 已提交
99 100
      terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
      return -1;
L
Liu Jicong 已提交
101 102
    }
  }
103
  taosCloseFile(&pFile);
L
Liu Jicong 已提交
104
  SWalCkHead* lastEntry = (SWalCkHead*)found;
L
Liu Jicong 已提交
105 106 107 108

  return lastEntry->head.version;
}

L
Liu Jicong 已提交
109 110 111 112
int walCheckAndRepairMeta(SWal* pWal) {
  // load log files, get first/snapshot/last version info
  const char* logPattern = "^[0-9]+.log$";
  const char* idxPattern = "^[0-9]+.idx$";
L
Liu Jicong 已提交
113 114
  regex_t     logRegPattern;
  regex_t     idxRegPattern;
L
Liu Jicong 已提交
115
  SArray*     pLogInfoArray = taosArrayInit(8, sizeof(SWalFileInfo));
L
Liu Jicong 已提交
116 117 118

  regcomp(&logRegPattern, logPattern, REG_EXTENDED);
  regcomp(&idxRegPattern, idxPattern, REG_EXTENDED);
L
Liu Jicong 已提交
119

wafwerar's avatar
wafwerar 已提交
120 121
  TdDirPtr pDir = taosOpenDir(pWal->path);
  if (pDir == NULL) {
L
Liu Jicong 已提交
122 123 124 125
    wError("vgId:%d, path:%s, failed to open since %s", pWal->cfg.vgId, pWal->path, strerror(errno));
    return -1;
  }

L
Liu Jicong 已提交
126
  // scan log files and build new meta
wafwerar's avatar
wafwerar 已提交
127 128 129
  TdDirEntryPtr pDirEntry;
  while ((pDirEntry = taosReadDir(pDir)) != NULL) {
    char* name = taosDirEntryBaseName(taosGetDirEntryName(pDirEntry));
L
Liu Jicong 已提交
130 131
    int   code = regexec(&logRegPattern, name, 0, NULL, 0);
    if (code == 0) {
L
Liu Jicong 已提交
132 133 134 135
      SWalFileInfo fileInfo;
      memset(&fileInfo, -1, sizeof(SWalFileInfo));
      sscanf(name, "%" PRId64 ".log", &fileInfo.firstVer);
      taosArrayPush(pLogInfoArray, &fileInfo);
L
Liu Jicong 已提交
136 137 138
    }
  }

wafwerar's avatar
wafwerar 已提交
139
  taosCloseDir(&pDir);
L
Liu Jicong 已提交
140 141 142 143
  regfree(&logRegPattern);
  regfree(&idxRegPattern);

  taosArraySort(pLogInfoArray, compareWalFileInfo);
L
Liu Jicong 已提交
144

L
Liu Jicong 已提交
145 146 147
  int metaFileNum = taosArrayGetSize(pWal->fileInfoSet);
  int actualFileNum = taosArrayGetSize(pLogInfoArray);

L
Liu Jicong 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
#if 0
  for (int32_t fileNo = actualFileNum - 1; fileNo >= 0; fileNo--) {
    SWalFileInfo* pFileInfo = taosArrayGet(pLogInfoArray, fileNo);
    char          fnameStr[WAL_FILE_LEN];
    walBuildLogName(pWal, pFileInfo->firstVer, fnameStr);
    int64_t fileSize = 0;
    taosStatFile(fnameStr, &fileSize, NULL);
    if (fileSize == 0) {
      taosRemoveFile(fnameStr);
      walBuildIdxName(pWal, pFileInfo->firstVer, fnameStr);
      taosRemoveFile(fnameStr);
      taosArrayPop(pLogInfoArray);
    } else {
      break;
    }
  }

  actualFileNum = taosArrayGetSize(pLogInfoArray);
#endif

L
Liu Jicong 已提交
168 169 170 171
  if (metaFileNum > actualFileNum) {
    taosArrayPopFrontBatch(pWal->fileInfoSet, metaFileNum - actualFileNum);
  } else if (metaFileNum < actualFileNum) {
    for (int i = metaFileNum; i < actualFileNum; i++) {
L
Liu Jicong 已提交
172
      SWalFileInfo* pFileInfo = taosArrayGet(pLogInfoArray, i);
L
Liu Jicong 已提交
173 174
      taosArrayPush(pWal->fileInfoSet, pFileInfo);
    }
L
Liu Jicong 已提交
175
  }
L
Liu Jicong 已提交
176
  taosArrayDestroy(pLogInfoArray);
L
Liu Jicong 已提交
177

L
Liu Jicong 已提交
178 179
  pWal->writeCur = actualFileNum - 1;
  if (actualFileNum > 0) {
L
Liu Jicong 已提交
180 181
    pWal->vers.firstVer = ((SWalFileInfo*)taosArrayGet(pWal->fileInfoSet, 0))->firstVer;

L
Liu Jicong 已提交
182
    SWalFileInfo* pLastFileInfo = taosArrayGet(pWal->fileInfoSet, actualFileNum - 1);
L
Liu Jicong 已提交
183
    char          fnameStr[WAL_FILE_LEN];
L
Liu Jicong 已提交
184
    walBuildLogName(pWal, pLastFileInfo->firstVer, fnameStr);
L
Liu Jicong 已提交
185 186
    int64_t fileSize = 0;
    taosStatFile(fnameStr, &fileSize, NULL);
L
Liu Jicong 已提交
187
    /*ASSERT(fileSize != 0);*/
L
Liu Jicong 已提交
188

L
Liu Jicong 已提交
189 190
    if (metaFileNum != actualFileNum || pLastFileInfo->fileSize != fileSize) {
      pLastFileInfo->fileSize = fileSize;
L
Liu Jicong 已提交
191 192 193 194 195 196 197 198
      pWal->vers.lastVer = walScanLogGetLastVer(pWal);
      ((SWalFileInfo*)taosArrayGetLast(pWal->fileInfoSet))->lastVer = pWal->vers.lastVer;
      ASSERT(pWal->vers.lastVer != -1);

      int code = walSaveMeta(pWal);
      if (code < 0) {
        taosArrayDestroy(pLogInfoArray);
        return -1;
L
Liu Jicong 已提交
199 200 201 202
      }
    }
  }

L
Liu Jicong 已提交
203 204
  // TODO: set fileSize and lastVer if necessary

L
Liu Jicong 已提交
205 206 207 208
  return 0;
}

int walCheckAndRepairIdx(SWal* pWal) {
L
Liu Jicong 已提交
209 210 211 212
  // TODO: iterate all log files
  // if idx not found, scan log and write idx
  // if found, check complete by first and last entry of each idx file
  // if idx incomplete, binary search last valid entry, and then build other part
L
Liu Jicong 已提交
213 214 215
  return 0;
}

L
Liu Jicong 已提交
216 217 218 219
int walRollFileInfo(SWal* pWal) {
  int64_t ts = taosGetTimestampSec();

  SArray* pArray = pWal->fileInfoSet;
L
Liu Jicong 已提交
220
  if (taosArrayGetSize(pArray) != 0) {
L
Liu Jicong 已提交
221
    SWalFileInfo* pInfo = taosArrayGetLast(pArray);
L
Liu Jicong 已提交
222
    pInfo->lastVer = pWal->vers.lastVer;
L
Liu Jicong 已提交
223 224 225
    pInfo->closeTs = ts;
  }

L
Liu Jicong 已提交
226
  // TODO: change to emplace back
wafwerar's avatar
wafwerar 已提交
227
  SWalFileInfo* pNewInfo = taosMemoryMalloc(sizeof(SWalFileInfo));
L
Liu Jicong 已提交
228
  if (pNewInfo == NULL) {
L
Liu Jicong 已提交
229
    terrno = TSDB_CODE_WAL_OUT_OF_MEMORY;
L
Liu Jicong 已提交
230 231
    return -1;
  }
L
Liu Jicong 已提交
232
  pNewInfo->firstVer = pWal->vers.lastVer + 1;
L
Liu Jicong 已提交
233 234 235 236
  pNewInfo->lastVer = -1;
  pNewInfo->createTs = ts;
  pNewInfo->closeTs = -1;
  pNewInfo->fileSize = 0;
L
Liu Jicong 已提交
237
  taosArrayPush(pArray, pNewInfo);
wafwerar's avatar
wafwerar 已提交
238
  taosMemoryFree(pNewInfo);
L
Liu Jicong 已提交
239 240 241
  return 0;
}

242
char* walMetaSerialize(SWal* pWal) {
L
Liu Jicong 已提交
243
  char buf[30];
L
Liu Jicong 已提交
244
  ASSERT(pWal->fileInfoSet);
L
Liu Jicong 已提交
245
  int    sz = pWal->fileInfoSet->size;
246 247 248 249
  cJSON* pRoot = cJSON_CreateObject();
  cJSON* pMeta = cJSON_CreateObject();
  cJSON* pFiles = cJSON_CreateArray();
  cJSON* pField;
L
Liu Jicong 已提交
250
  if (pRoot == NULL || pMeta == NULL || pFiles == NULL) {
L
Liu Jicong 已提交
251
    if (pRoot) {
L
Liu Jicong 已提交
252 253
      cJSON_Delete(pRoot);
    }
L
Liu Jicong 已提交
254
    if (pMeta) {
L
Liu Jicong 已提交
255 256
      cJSON_Delete(pMeta);
    }
L
Liu Jicong 已提交
257
    if (pFiles) {
L
Liu Jicong 已提交
258 259 260
      cJSON_Delete(pFiles);
    }
    terrno = TSDB_CODE_WAL_OUT_OF_MEMORY;
L
Liu Jicong 已提交
261 262
    return NULL;
  }
263
  cJSON_AddItemToObject(pRoot, "meta", pMeta);
L
Liu Jicong 已提交
264
  sprintf(buf, "%" PRId64, pWal->vers.firstVer);
265
  cJSON_AddStringToObject(pMeta, "firstVer", buf);
L
Liu Jicong 已提交
266
  sprintf(buf, "%" PRId64, pWal->vers.snapshotVer);
267
  cJSON_AddStringToObject(pMeta, "snapshotVer", buf);
L
Liu Jicong 已提交
268
  sprintf(buf, "%" PRId64, pWal->vers.commitVer);
269
  cJSON_AddStringToObject(pMeta, "commitVer", buf);
L
Liu Jicong 已提交
270
  sprintf(buf, "%" PRId64, pWal->vers.lastVer);
271 272 273
  cJSON_AddStringToObject(pMeta, "lastVer", buf);

  cJSON_AddItemToObject(pRoot, "files", pFiles);
L
Liu Jicong 已提交
274
  SWalFileInfo* pData = pWal->fileInfoSet->pData;
L
Liu Jicong 已提交
275
  for (int i = 0; i < sz; i++) {
L
Liu Jicong 已提交
276
    SWalFileInfo* pInfo = &pData[i];
277
    cJSON_AddItemToArray(pFiles, pField = cJSON_CreateObject());
L
Liu Jicong 已提交
278
    if (pField == NULL) {
279
      cJSON_Delete(pRoot);
L
Liu Jicong 已提交
280 281
      return NULL;
    }
L
Liu Jicong 已提交
282 283
    // cjson only support int32_t or double
    // string are used to prohibit the loss of precision
284 285 286 287 288 289 290 291 292 293
    sprintf(buf, "%" PRId64, pInfo->firstVer);
    cJSON_AddStringToObject(pField, "firstVer", buf);
    sprintf(buf, "%" PRId64, pInfo->lastVer);
    cJSON_AddStringToObject(pField, "lastVer", buf);
    sprintf(buf, "%" PRId64, pInfo->createTs);
    cJSON_AddStringToObject(pField, "createTs", buf);
    sprintf(buf, "%" PRId64, pInfo->closeTs);
    cJSON_AddStringToObject(pField, "closeTs", buf);
    sprintf(buf, "%" PRId64, pInfo->fileSize);
    cJSON_AddStringToObject(pField, "fileSize", buf);
L
Liu Jicong 已提交
294
  }
L
Liu Jicong 已提交
295 296 297
  char* serialized = cJSON_Print(pRoot);
  cJSON_Delete(pRoot);
  return serialized;
L
Liu Jicong 已提交
298 299
}

300 301 302 303 304 305
int walMetaDeserialize(SWal* pWal, const char* bytes) {
  ASSERT(taosArrayGetSize(pWal->fileInfoSet) == 0);
  cJSON *pRoot, *pMeta, *pFiles, *pInfoJson, *pField;
  pRoot = cJSON_Parse(bytes);
  pMeta = cJSON_GetObjectItem(pRoot, "meta");
  pField = cJSON_GetObjectItem(pMeta, "firstVer");
L
Liu Jicong 已提交
306
  pWal->vers.firstVer = atoll(cJSON_GetStringValue(pField));
307
  pField = cJSON_GetObjectItem(pMeta, "snapshotVer");
L
Liu Jicong 已提交
308
  pWal->vers.snapshotVer = atoll(cJSON_GetStringValue(pField));
309
  pField = cJSON_GetObjectItem(pMeta, "commitVer");
L
Liu Jicong 已提交
310
  pWal->vers.commitVer = atoll(cJSON_GetStringValue(pField));
311
  pField = cJSON_GetObjectItem(pMeta, "lastVer");
L
Liu Jicong 已提交
312
  pWal->vers.lastVer = atoll(cJSON_GetStringValue(pField));
313 314 315

  pFiles = cJSON_GetObjectItem(pRoot, "files");
  int sz = cJSON_GetArraySize(pFiles);
L
Liu Jicong 已提交
316
  // deserialize
L
Liu Jicong 已提交
317 318
  SArray* pArray = pWal->fileInfoSet;
  taosArrayEnsureCap(pArray, sz);
L
Liu Jicong 已提交
319
  SWalFileInfo* pData = pArray->pData;
L
Liu Jicong 已提交
320
  for (int i = 0; i < sz; i++) {
L
Liu Jicong 已提交
321 322
    cJSON*        pInfoJson = cJSON_GetArrayItem(pFiles, i);
    SWalFileInfo* pInfo = &pData[i];
L
Liu Jicong 已提交
323 324 325 326 327 328 329 330 331 332 333 334
    pField = cJSON_GetObjectItem(pInfoJson, "firstVer");
    pInfo->firstVer = atoll(cJSON_GetStringValue(pField));
    pField = cJSON_GetObjectItem(pInfoJson, "lastVer");
    pInfo->lastVer = atoll(cJSON_GetStringValue(pField));
    pField = cJSON_GetObjectItem(pInfoJson, "createTs");
    pInfo->createTs = atoll(cJSON_GetStringValue(pField));
    pField = cJSON_GetObjectItem(pInfoJson, "closeTs");
    pInfo->closeTs = atoll(cJSON_GetStringValue(pField));
    pField = cJSON_GetObjectItem(pInfoJson, "fileSize");
    pInfo->fileSize = atoll(cJSON_GetStringValue(pField));
  }
  taosArraySetSize(pArray, sz);
335
  pWal->fileInfoSet = pArray;
L
Liu Jicong 已提交
336
  pWal->writeCur = sz - 1;
L
Liu Jicong 已提交
337
  cJSON_Delete(pRoot);
338
  return 0;
L
Liu Jicong 已提交
339 340 341
}

static int walFindCurMetaVer(SWal* pWal) {
L
Liu Jicong 已提交
342 343
  const char* pattern = "^meta-ver[0-9]+$";
  regex_t     walMetaRegexPattern;
L
Liu Jicong 已提交
344 345
  regcomp(&walMetaRegexPattern, pattern, REG_EXTENDED);

wafwerar's avatar
wafwerar 已提交
346 347
  TdDirPtr pDir = taosOpenDir(pWal->path);
  if (pDir == NULL) {
L
Liu Jicong 已提交
348
    wError("vgId:%d, path:%s, failed to open since %s", pWal->cfg.vgId, pWal->path, strerror(errno));
L
Liu Jicong 已提交
349 350 351
    return -1;
  }

wafwerar's avatar
wafwerar 已提交
352
  TdDirEntryPtr pDirEntry;
L
Liu Jicong 已提交
353

L
Liu Jicong 已提交
354
  // find existing meta-ver[x].json
L
Liu Jicong 已提交
355
  int metaVer = -1;
wafwerar's avatar
wafwerar 已提交
356 357
  while ((pDirEntry = taosReadDir(pDir)) != NULL) {
    char* name = taosDirEntryBaseName(taosGetDirEntryName(pDirEntry));
L
Liu Jicong 已提交
358 359
    int   code = regexec(&walMetaRegexPattern, name, 0, NULL, 0);
    if (code == 0) {
L
Liu Jicong 已提交
360 361 362 363
      sscanf(name, "meta-ver%d", &metaVer);
      break;
    }
  }
wafwerar's avatar
wafwerar 已提交
364
  taosCloseDir(&pDir);
L
Liu Jicong 已提交
365
  regfree(&walMetaRegexPattern);
L
Liu Jicong 已提交
366 367 368
  return metaVer;
}

L
Liu Jicong 已提交
369
int walSaveMeta(SWal* pWal) {
L
Liu Jicong 已提交
370
  int  metaVer = walFindCurMetaVer(pWal);
L
Liu Jicong 已提交
371
  char fnameStr[WAL_FILE_LEN];
L
Liu Jicong 已提交
372
  walBuildMetaName(pWal, metaVer + 1, fnameStr);
373
  TdFilePtr pMataFile = taosOpenFile(fnameStr, TD_FILE_CREATE | TD_FILE_WRITE);
374
  if (pMataFile == NULL) {
L
Liu Jicong 已提交
375 376
    return -1;
  }
377
  char* serialized = walMetaSerialize(pWal);
L
Liu Jicong 已提交
378
  int   len = strlen(serialized);
379
  if (len != taosWriteFile(pMataFile, serialized, len)) {
L
Liu Jicong 已提交
380
    // TODO:clean file
L
Liu Jicong 已提交
381 382
    return -1;
  }
L
Liu Jicong 已提交
383

384
  taosCloseFile(&pMataFile);
L
Liu Jicong 已提交
385 386
  // delete old file
  if (metaVer > -1) {
L
Liu Jicong 已提交
387
    walBuildMetaName(pWal, metaVer, fnameStr);
388
    taosRemoveFile(fnameStr);
L
Liu Jicong 已提交
389
  }
wafwerar's avatar
wafwerar 已提交
390
  taosMemoryFree(serialized);
L
Liu Jicong 已提交
391 392 393
  return 0;
}

L
Liu Jicong 已提交
394
int walLoadMeta(SWal* pWal) {
L
Liu Jicong 已提交
395
  ASSERT(pWal->fileInfoSet->size == 0);
L
Liu Jicong 已提交
396
  // find existing meta file
L
Liu Jicong 已提交
397
  int metaVer = walFindCurMetaVer(pWal);
L
Liu Jicong 已提交
398
  if (metaVer == -1) {
L
Liu Jicong 已提交
399
    return -1;
L
Liu Jicong 已提交
400 401 402
  }
  char fnameStr[WAL_FILE_LEN];
  walBuildMetaName(pWal, metaVer, fnameStr);
L
Liu Jicong 已提交
403
  // read metafile
L
Liu Jicong 已提交
404 405 406
  int64_t fileSize = 0;
  taosStatFile(fnameStr, &fileSize, NULL);
  int   size = (int)fileSize;
wafwerar's avatar
wafwerar 已提交
407
  char* buf = taosMemoryMalloc(size + 5);
L
Liu Jicong 已提交
408
  if (buf == NULL) {
L
Liu Jicong 已提交
409
    terrno = TSDB_CODE_WAL_OUT_OF_MEMORY;
L
Liu Jicong 已提交
410 411
    return -1;
  }
L
Liu Jicong 已提交
412
  memset(buf, 0, size + 5);
413 414
  TdFilePtr pFile = taosOpenFile(fnameStr, TD_FILE_READ);
  if (pFile == NULL) {
L
Liu Jicong 已提交
415 416 417
    terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
    return -1;
  }
418
  if (taosReadFile(pFile, buf, size) != size) {
L
Liu Jicong 已提交
419
    terrno = TAOS_SYSTEM_ERROR(errno);
420
    taosCloseFile(&pFile);
wafwerar's avatar
wafwerar 已提交
421
    taosMemoryFree(buf);
L
Liu Jicong 已提交
422 423
    return -1;
  }
L
Liu Jicong 已提交
424
  // load into fileInfoSet
425
  int code = walMetaDeserialize(pWal, buf);
426
  taosCloseFile(&pFile);
wafwerar's avatar
wafwerar 已提交
427
  taosMemoryFree(buf);
L
Liu Jicong 已提交
428
  return code;
L
Liu Jicong 已提交
429
}
430 431 432 433 434 435 436 437 438

int walRemoveMeta(SWal* pWal) {
  int metaVer = walFindCurMetaVer(pWal);
  if (metaVer == -1) return 0;
  char fnameStr[WAL_FILE_LEN];
  walBuildMetaName(pWal, metaVer, fnameStr);
  taosRemoveFile(fnameStr);
  return 0;
}