walMeta.c 12.9 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
int64_t FORCE_INLINE walGetFirstVer(SWal* pWal) { return pWal->vers.firstVer; }
L
Liu Jicong 已提交
23

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

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

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

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

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

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

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

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

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

65 66 67 68
  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 已提交
69

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

  uint64_t magic = WAL_MAGIC;

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

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

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

  return lastEntry->head.version;
}

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

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

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

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

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

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

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

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

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

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

L
Liu Jicong 已提交
195 196
  // TODO: set fileSize and lastVer if necessary

L
Liu Jicong 已提交
197 198 199 200
  return 0;
}

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

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

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

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

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

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

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

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

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

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

wafwerar's avatar
wafwerar 已提交
344
  TdDirEntryPtr pDirEntry;
L
Liu Jicong 已提交
345

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

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

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

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