walMeta.c 13.2 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 "tref.h"
L
Liu Jicong 已提交
20 21
#include "walInt.h"

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

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

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

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

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

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

L
Liu Jicong 已提交
36 37 38 39
void* tmemmem(char* haystack, int hlen, char* needle, int nlen) {
  char* limit;

  if (nlen == 0 || hlen < nlen) {
L
Liu Jicong 已提交
40
    return NULL;
L
Liu Jicong 已提交
41 42 43
  }

  limit = haystack + hlen - nlen + 1;
L
Liu Jicong 已提交
44
  while ((haystack = (char*)memchr(haystack, needle[0], limit - haystack)) != NULL) {
L
Liu Jicong 已提交
45 46 47 48 49 50 51 52
    if (memcmp(haystack, needle, nlen) == 0) {
      return haystack;
    }
    haystack++;
  }
  return NULL;
}

L
Liu Jicong 已提交
53
static FORCE_INLINE int64_t walScanLogGetLastVer(SWal* pWal) {
L
Liu Jicong 已提交
54 55 56
  ASSERT(pWal->fileInfoSet != NULL);
  int sz = taosArrayGetSize(pWal->fileInfoSet);
  ASSERT(sz > 0);
L
Liu Jicong 已提交
57
#if 0
L
Liu Jicong 已提交
58 59 60
  for (int i = 0; i < sz; i++) {
    SWalFileInfo* pFileInfo = taosArrayGet(pWal->fileInfoSet, i);
  }
L
Liu Jicong 已提交
61 62
#endif

L
Liu Jicong 已提交
63 64
  SWalFileInfo* pLastFileInfo = taosArrayGet(pWal->fileInfoSet, sz - 1);
  char          fnameStr[WAL_FILE_LEN];
L
Liu Jicong 已提交
65 66
  walBuildLogName(pWal, pLastFileInfo->firstVer, fnameStr);

67 68 69 70
  int64_t file_size = 0;
  taosStatFile(fnameStr, &file_size, NULL);
  int readSize = TMIN(WAL_MAX_SIZE + 2, file_size);
  pLastFileInfo->fileSize = file_size;
L
Liu Jicong 已提交
71

72 73
  TdFilePtr pFile = taosOpenFile(fnameStr, TD_FILE_READ);
  if (pFile == NULL) {
L
Liu Jicong 已提交
74 75 76 77 78 79
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }

  uint64_t magic = WAL_MAGIC;

wafwerar's avatar
wafwerar 已提交
80
  char* buf = taosMemoryMalloc(readSize + 5);
L
Liu Jicong 已提交
81
  if (buf == NULL) {
82
    taosCloseFile(&pFile);
L
Liu Jicong 已提交
83 84 85 86
    terrno = TSDB_CODE_WAL_OUT_OF_MEMORY;
    return -1;
  }

87 88
  taosLSeekFile(pFile, -readSize, SEEK_END);
  if (readSize != taosReadFile(pFile, buf, readSize)) {
wafwerar's avatar
wafwerar 已提交
89
    taosMemoryFree(buf);
90
    taosCloseFile(&pFile);
L
Liu Jicong 已提交
91 92 93
    terrno = TAOS_SYSTEM_ERROR(errno);
    return -1;
  }
L
Liu Jicong 已提交
94

L
Liu Jicong 已提交
95 96
  char* haystack = buf;
  char* found = NULL;
L
Liu Jicong 已提交
97 98
  char* candidate;
  while ((candidate = tmemmem(haystack, readSize - (haystack - buf), (char*)&magic, sizeof(uint64_t))) != NULL) {
L
Liu Jicong 已提交
99
    // read and validate
L
Liu Jicong 已提交
100
    SWalHead* logContent = (SWalHead*)candidate;
L
Liu Jicong 已提交
101
    if (walValidHeadCksum(logContent) == 0 && walValidBodyCksum(logContent) == 0) {
L
Liu Jicong 已提交
102 103 104 105 106
      found = candidate;
    }
    haystack = candidate + 1;
  }
  if (found == buf) {
L
Liu Jicong 已提交
107
    SWalHead* logContent = (SWalHead*)found;
L
Liu Jicong 已提交
108 109
    if (walValidHeadCksum(logContent) != 0 || walValidBodyCksum(logContent) != 0) {
      // file has to be deleted
wafwerar's avatar
wafwerar 已提交
110
      taosMemoryFree(buf);
111
      taosCloseFile(&pFile);
L
Liu Jicong 已提交
112 113
      terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
      return -1;
L
Liu Jicong 已提交
114 115
    }
  }
116
  taosCloseFile(&pFile);
L
Liu Jicong 已提交
117
  SWalHead* lastEntry = (SWalHead*)found;
L
Liu Jicong 已提交
118 119 120 121

  return lastEntry->head.version;
}

L
Liu Jicong 已提交
122 123 124 125
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 已提交
126 127
  regex_t     logRegPattern;
  regex_t     idxRegPattern;
L
Liu Jicong 已提交
128
  SArray*     pLogInfoArray = taosArrayInit(8, sizeof(SWalFileInfo));
L
Liu Jicong 已提交
129 130 131

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

wafwerar's avatar
wafwerar 已提交
133 134
  TdDirPtr pDir = taosOpenDir(pWal->path);
  if (pDir == NULL) {
L
Liu Jicong 已提交
135 136 137 138
    wError("vgId:%d, path:%s, failed to open since %s", pWal->cfg.vgId, pWal->path, strerror(errno));
    return -1;
  }

L
Liu Jicong 已提交
139
  // scan log files and build new meta
wafwerar's avatar
wafwerar 已提交
140 141 142
  TdDirEntryPtr pDirEntry;
  while ((pDirEntry = taosReadDir(pDir)) != NULL) {
    char* name = taosDirEntryBaseName(taosGetDirEntryName(pDirEntry));
L
Liu Jicong 已提交
143 144
    int   code = regexec(&logRegPattern, name, 0, NULL, 0);
    if (code == 0) {
L
Liu Jicong 已提交
145 146 147 148
      SWalFileInfo fileInfo;
      memset(&fileInfo, -1, sizeof(SWalFileInfo));
      sscanf(name, "%" PRId64 ".log", &fileInfo.firstVer);
      taosArrayPush(pLogInfoArray, &fileInfo);
L
Liu Jicong 已提交
149 150 151
    }
  }

wafwerar's avatar
wafwerar 已提交
152
  taosCloseDir(&pDir);
L
Liu Jicong 已提交
153 154 155 156 157 158 159 160 161
  regfree(&logRegPattern);
  regfree(&idxRegPattern);

  taosArraySort(pLogInfoArray, compareWalFileInfo);
  int oldSz = 0;
  if (pWal->fileInfoSet) {
    oldSz = taosArrayGetSize(pWal->fileInfoSet);
  }
  int newSz = taosArrayGetSize(pLogInfoArray);
L
Liu Jicong 已提交
162 163

  if (oldSz > newSz) {
L
Liu Jicong 已提交
164
    taosArrayPopFrontBatch(pWal->fileInfoSet, oldSz - newSz);
L
Liu Jicong 已提交
165
  } else if (oldSz < newSz) {
L
Liu Jicong 已提交
166
    for (int i = oldSz; i < newSz; i++) {
L
Liu Jicong 已提交
167
      SWalFileInfo* pFileInfo = taosArrayGet(pLogInfoArray, i);
L
Liu Jicong 已提交
168 169
      taosArrayPush(pWal->fileInfoSet, pFileInfo);
    }
L
Liu Jicong 已提交
170
  }
L
Liu Jicong 已提交
171
  taosArrayDestroy(pLogInfoArray);
L
Liu Jicong 已提交
172

L
Liu Jicong 已提交
173 174 175 176
  pWal->writeCur = newSz - 1;
  if (newSz > 0) {
    pWal->vers.firstVer = ((SWalFileInfo*)taosArrayGet(pWal->fileInfoSet, 0))->firstVer;

L
Liu Jicong 已提交
177 178
    SWalFileInfo* pLastFileInfo = taosArrayGet(pWal->fileInfoSet, newSz - 1);
    char          fnameStr[WAL_FILE_LEN];
L
Liu Jicong 已提交
179
    walBuildLogName(pWal, pLastFileInfo->firstVer, fnameStr);
180 181
    int64_t file_size = 0;
    taosStatFile(fnameStr, &file_size, NULL);
L
Liu Jicong 已提交
182

183 184
    if (oldSz != newSz || pLastFileInfo->fileSize != file_size) {
      pLastFileInfo->fileSize = file_size;
L
Liu Jicong 已提交
185 186 187 188 189 190 191 192
      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 已提交
193 194 195 196
      }
    }
  }

L
Liu Jicong 已提交
197 198
  // TODO: set fileSize and lastVer if necessary

L
Liu Jicong 已提交
199 200 201 202
  return 0;
}

int walCheckAndRepairIdx(SWal* pWal) {
L
Liu Jicong 已提交
203 204 205 206
  // 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 已提交
207 208 209
  return 0;
}

L
Liu Jicong 已提交
210 211 212 213
int walRollFileInfo(SWal* pWal) {
  int64_t ts = taosGetTimestampSec();

  SArray* pArray = pWal->fileInfoSet;
L
Liu Jicong 已提交
214
  if (taosArrayGetSize(pArray) != 0) {
L
Liu Jicong 已提交
215
    SWalFileInfo* pInfo = taosArrayGetLast(pArray);
L
Liu Jicong 已提交
216
    pInfo->lastVer = pWal->vers.lastVer;
L
Liu Jicong 已提交
217 218 219
    pInfo->closeTs = ts;
  }

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

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

  cJSON_AddItemToObject(pRoot, "files", pFiles);
L
Liu Jicong 已提交
268
  SWalFileInfo* pData = pWal->fileInfoSet->pData;
L
Liu Jicong 已提交
269
  for (int i = 0; i < sz; i++) {
L
Liu Jicong 已提交
270
    SWalFileInfo* pInfo = &pData[i];
271
    cJSON_AddItemToArray(pFiles, pField = cJSON_CreateObject());
L
Liu Jicong 已提交
272
    if (pField == NULL) {
273
      cJSON_Delete(pRoot);
L
Liu Jicong 已提交
274 275
      return NULL;
    }
L
Liu Jicong 已提交
276 277
    // cjson only support int32_t or double
    // string are used to prohibit the loss of precision
278 279 280 281 282 283 284 285 286 287
    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 已提交
288
  }
L
Liu Jicong 已提交
289 290 291
  char* serialized = cJSON_Print(pRoot);
  cJSON_Delete(pRoot);
  return serialized;
L
Liu Jicong 已提交
292 293
}

294 295 296 297 298 299
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 已提交
300
  pWal->vers.firstVer = atoll(cJSON_GetStringValue(pField));
301
  pField = cJSON_GetObjectItem(pMeta, "snapshotVer");
L
Liu Jicong 已提交
302
  pWal->vers.snapshotVer = atoll(cJSON_GetStringValue(pField));
303
  pField = cJSON_GetObjectItem(pMeta, "commitVer");
L
Liu Jicong 已提交
304
  pWal->vers.commitVer = atoll(cJSON_GetStringValue(pField));
305
  pField = cJSON_GetObjectItem(pMeta, "lastVer");
L
Liu Jicong 已提交
306
  pWal->vers.lastVer = atoll(cJSON_GetStringValue(pField));
307 308 309

  pFiles = cJSON_GetObjectItem(pRoot, "files");
  int sz = cJSON_GetArraySize(pFiles);
L
Liu Jicong 已提交
310
  // deserialize
L
Liu Jicong 已提交
311 312
  SArray* pArray = pWal->fileInfoSet;
  taosArrayEnsureCap(pArray, sz);
L
Liu Jicong 已提交
313
  SWalFileInfo* pData = pArray->pData;
L
Liu Jicong 已提交
314
  for (int i = 0; i < sz; i++) {
L
Liu Jicong 已提交
315 316
    cJSON*        pInfoJson = cJSON_GetArrayItem(pFiles, i);
    SWalFileInfo* pInfo = &pData[i];
L
Liu Jicong 已提交
317 318 319 320 321 322 323 324 325 326 327 328
    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);
329
  pWal->fileInfoSet = pArray;
L
Liu Jicong 已提交
330
  pWal->writeCur = sz - 1;
L
Liu Jicong 已提交
331
  cJSON_Delete(pRoot);
332
  return 0;
L
Liu Jicong 已提交
333 334 335
}

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

wafwerar's avatar
wafwerar 已提交
340 341
  TdDirPtr pDir = taosOpenDir(pWal->path);
  if (pDir == NULL) {
L
Liu Jicong 已提交
342
    wError("vgId:%d, path:%s, failed to open since %s", pWal->cfg.vgId, pWal->path, strerror(errno));
L
Liu Jicong 已提交
343 344 345
    return -1;
  }

wafwerar's avatar
wafwerar 已提交
346
  TdDirEntryPtr pDirEntry;
L
Liu Jicong 已提交
347

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

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

378
  taosCloseFile(&pMataFile);
L
Liu Jicong 已提交
379 380
  // delete old file
  if (metaVer > -1) {
L
Liu Jicong 已提交
381
    walBuildMetaName(pWal, metaVer, fnameStr);
382
    taosRemoveFile(fnameStr);
L
Liu Jicong 已提交
383
  }
wafwerar's avatar
wafwerar 已提交
384
  taosMemoryFree(serialized);
L
Liu Jicong 已提交
385 386 387
  return 0;
}

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

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;
}