walMeta.c 14.4 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 37
int64_t FORCE_INLINE walGetAppliedVer(SWal* pWal) { return pWal->vers.appliedVer; }

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

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

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

L
Liu Jicong 已提交
55 56
  int64_t fileSize = 0;
  taosStatFile(fnameStr, &fileSize, NULL);
57
  int32_t readSize = TMIN(WAL_SCAN_BUF_SIZE, fileSize);
L
Liu Jicong 已提交
58
  pLastFileInfo->fileSize = fileSize;
L
Liu Jicong 已提交
59

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

  uint64_t magic = WAL_MAGIC;

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

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

L
Liu Jicong 已提交
84
  char* found = NULL;
85 86 87 88 89 90 91 92 93 94
  while (1) {
    char* haystack = buf;
    char* candidate;
    while ((candidate = tmemmem(haystack, readSize - (haystack - buf), (char*)&magic, sizeof(uint64_t))) != NULL) {
      // read and validate
      SWalCkHead* logContent = (SWalCkHead*)candidate;
      if (walValidHeadCksum(logContent) == 0 && walValidBodyCksum(logContent) == 0) {
        found = candidate;
      }
      haystack = candidate + 1;
L
Liu Jicong 已提交
95
    }
96
    if (found || offset == 0) break;
L
Liu Jicong 已提交
97
    offset = TMIN(0, offset - readSize + sizeof(uint64_t));
98 99 100
    int64_t offset2 = taosLSeekFile(pFile, offset, SEEK_SET);
    ASSERT(offset == offset2);
    if (readSize != taosReadFile(pFile, buf, readSize)) {
wafwerar's avatar
wafwerar 已提交
101
      taosMemoryFree(buf);
102
      taosCloseFile(&pFile);
103
      terrno = TAOS_SYSTEM_ERROR(errno);
L
Liu Jicong 已提交
104
      return -1;
L
Liu Jicong 已提交
105
    }
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
#if 0
    if (found == buf) {
      SWalCkHead* logContent = (SWalCkHead*)found;
      if (walValidHeadCksum(logContent) != 0 || walValidBodyCksum(logContent) != 0) {
        // file has to be deleted
        taosMemoryFree(buf);
        taosCloseFile(&pFile);
        terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
        return -1;
      }
    }
#endif
  }
  // TODO truncate file

  if (found == NULL) {
    // file corrupted, no complete log
    // TODO delete and search in previous files
    ASSERT(0);
    terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
    return -1;
L
Liu Jicong 已提交
127
  }
L
Liu Jicong 已提交
128
  SWalCkHead* lastEntry = (SWalCkHead*)found;
L
Liu Jicong 已提交
129
  int64_t     retVer = lastEntry->head.version;
130 131
  taosCloseFile(&pFile);
  taosMemoryFree(buf);
L
Liu Jicong 已提交
132

L
Liu Jicong 已提交
133
  return retVer;
L
Liu Jicong 已提交
134 135
}

L
Liu Jicong 已提交
136 137 138 139
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 已提交
140 141
  regex_t     logRegPattern;
  regex_t     idxRegPattern;
142
  SArray*     actualLog = taosArrayInit(8, sizeof(SWalFileInfo));
L
Liu Jicong 已提交
143 144 145

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

wafwerar's avatar
wafwerar 已提交
147 148
  TdDirPtr pDir = taosOpenDir(pWal->path);
  if (pDir == NULL) {
L
Liu Jicong 已提交
149 150 151 152
    wError("vgId:%d, path:%s, failed to open since %s", pWal->cfg.vgId, pWal->path, strerror(errno));
    return -1;
  }

L
Liu Jicong 已提交
153
  // scan log files and build new meta
wafwerar's avatar
wafwerar 已提交
154 155 156
  TdDirEntryPtr pDirEntry;
  while ((pDirEntry = taosReadDir(pDir)) != NULL) {
    char* name = taosDirEntryBaseName(taosGetDirEntryName(pDirEntry));
L
Liu Jicong 已提交
157 158
    int   code = regexec(&logRegPattern, name, 0, NULL, 0);
    if (code == 0) {
L
Liu Jicong 已提交
159 160 161
      SWalFileInfo fileInfo;
      memset(&fileInfo, -1, sizeof(SWalFileInfo));
      sscanf(name, "%" PRId64 ".log", &fileInfo.firstVer);
162
      taosArrayPush(actualLog, &fileInfo);
L
Liu Jicong 已提交
163 164 165
    }
  }

wafwerar's avatar
wafwerar 已提交
166
  taosCloseDir(&pDir);
L
Liu Jicong 已提交
167 168 169
  regfree(&logRegPattern);
  regfree(&idxRegPattern);

170
  taosArraySort(actualLog, compareWalFileInfo);
L
Liu Jicong 已提交
171

L
Liu Jicong 已提交
172
  int metaFileNum = taosArrayGetSize(pWal->fileInfoSet);
173
  int actualFileNum = taosArrayGetSize(actualLog);
L
Liu Jicong 已提交
174

L
Liu Jicong 已提交
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
#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 已提交
195 196 197 198
  if (metaFileNum > actualFileNum) {
    taosArrayPopFrontBatch(pWal->fileInfoSet, metaFileNum - actualFileNum);
  } else if (metaFileNum < actualFileNum) {
    for (int i = metaFileNum; i < actualFileNum; i++) {
199
      SWalFileInfo* pFileInfo = taosArrayGet(actualLog, i);
L
Liu Jicong 已提交
200 201
      taosArrayPush(pWal->fileInfoSet, pFileInfo);
    }
L
Liu Jicong 已提交
202
  }
203
  taosArrayDestroy(actualLog);
L
Liu Jicong 已提交
204

L
Liu Jicong 已提交
205 206
  pWal->writeCur = actualFileNum - 1;
  if (actualFileNum > 0) {
L
Liu Jicong 已提交
207 208
    pWal->vers.firstVer = ((SWalFileInfo*)taosArrayGet(pWal->fileInfoSet, 0))->firstVer;

L
Liu Jicong 已提交
209
    SWalFileInfo* pLastFileInfo = taosArrayGet(pWal->fileInfoSet, actualFileNum - 1);
L
Liu Jicong 已提交
210
    char          fnameStr[WAL_FILE_LEN];
L
Liu Jicong 已提交
211
    walBuildLogName(pWal, pLastFileInfo->firstVer, fnameStr);
L
Liu Jicong 已提交
212 213
    int64_t fileSize = 0;
    taosStatFile(fnameStr, &fileSize, NULL);
L
Liu Jicong 已提交
214
    /*ASSERT(fileSize != 0);*/
L
Liu Jicong 已提交
215

L
Liu Jicong 已提交
216 217
    if (metaFileNum != actualFileNum || pLastFileInfo->fileSize != fileSize) {
      pLastFileInfo->fileSize = fileSize;
L
Liu Jicong 已提交
218 219 220 221 222 223
      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) {
224
        taosArrayDestroy(actualLog);
L
Liu Jicong 已提交
225
        return -1;
L
Liu Jicong 已提交
226 227 228 229
      }
    }
  }

L
Liu Jicong 已提交
230 231
  // TODO: set fileSize and lastVer if necessary

L
Liu Jicong 已提交
232 233 234 235
  return 0;
}

int walCheckAndRepairIdx(SWal* pWal) {
L
Liu Jicong 已提交
236 237 238 239
  // 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 已提交
240 241 242
  return 0;
}

L
Liu Jicong 已提交
243 244 245 246
int walRollFileInfo(SWal* pWal) {
  int64_t ts = taosGetTimestampSec();

  SArray* pArray = pWal->fileInfoSet;
L
Liu Jicong 已提交
247
  if (taosArrayGetSize(pArray) != 0) {
L
Liu Jicong 已提交
248
    SWalFileInfo* pInfo = taosArrayGetLast(pArray);
L
Liu Jicong 已提交
249
    pInfo->lastVer = pWal->vers.lastVer;
L
Liu Jicong 已提交
250 251 252
    pInfo->closeTs = ts;
  }

L
Liu Jicong 已提交
253
  // TODO: change to emplace back
wafwerar's avatar
wafwerar 已提交
254
  SWalFileInfo* pNewInfo = taosMemoryMalloc(sizeof(SWalFileInfo));
L
Liu Jicong 已提交
255
  if (pNewInfo == NULL) {
L
Liu Jicong 已提交
256
    terrno = TSDB_CODE_WAL_OUT_OF_MEMORY;
L
Liu Jicong 已提交
257 258
    return -1;
  }
L
Liu Jicong 已提交
259
  pNewInfo->firstVer = pWal->vers.lastVer + 1;
L
Liu Jicong 已提交
260 261 262 263
  pNewInfo->lastVer = -1;
  pNewInfo->createTs = ts;
  pNewInfo->closeTs = -1;
  pNewInfo->fileSize = 0;
L
Liu Jicong 已提交
264
  taosArrayPush(pArray, pNewInfo);
wafwerar's avatar
wafwerar 已提交
265
  taosMemoryFree(pNewInfo);
L
Liu Jicong 已提交
266 267 268
  return 0;
}

269
char* walMetaSerialize(SWal* pWal) {
L
Liu Jicong 已提交
270
  char buf[30];
L
Liu Jicong 已提交
271
  ASSERT(pWal->fileInfoSet);
L
Liu Jicong 已提交
272
  int    sz = pWal->fileInfoSet->size;
273 274 275 276
  cJSON* pRoot = cJSON_CreateObject();
  cJSON* pMeta = cJSON_CreateObject();
  cJSON* pFiles = cJSON_CreateArray();
  cJSON* pField;
L
Liu Jicong 已提交
277
  if (pRoot == NULL || pMeta == NULL || pFiles == NULL) {
L
Liu Jicong 已提交
278
    if (pRoot) {
L
Liu Jicong 已提交
279 280
      cJSON_Delete(pRoot);
    }
L
Liu Jicong 已提交
281
    if (pMeta) {
L
Liu Jicong 已提交
282 283
      cJSON_Delete(pMeta);
    }
L
Liu Jicong 已提交
284
    if (pFiles) {
L
Liu Jicong 已提交
285 286 287
      cJSON_Delete(pFiles);
    }
    terrno = TSDB_CODE_WAL_OUT_OF_MEMORY;
L
Liu Jicong 已提交
288 289
    return NULL;
  }
290
  cJSON_AddItemToObject(pRoot, "meta", pMeta);
L
Liu Jicong 已提交
291
  sprintf(buf, "%" PRId64, pWal->vers.firstVer);
292
  cJSON_AddStringToObject(pMeta, "firstVer", buf);
L
Liu Jicong 已提交
293
  sprintf(buf, "%" PRId64, pWal->vers.snapshotVer);
294
  cJSON_AddStringToObject(pMeta, "snapshotVer", buf);
L
Liu Jicong 已提交
295
  sprintf(buf, "%" PRId64, pWal->vers.commitVer);
296
  cJSON_AddStringToObject(pMeta, "commitVer", buf);
L
Liu Jicong 已提交
297
  sprintf(buf, "%" PRId64, pWal->vers.lastVer);
298 299 300
  cJSON_AddStringToObject(pMeta, "lastVer", buf);

  cJSON_AddItemToObject(pRoot, "files", pFiles);
L
Liu Jicong 已提交
301
  SWalFileInfo* pData = pWal->fileInfoSet->pData;
L
Liu Jicong 已提交
302
  for (int i = 0; i < sz; i++) {
L
Liu Jicong 已提交
303
    SWalFileInfo* pInfo = &pData[i];
304
    cJSON_AddItemToArray(pFiles, pField = cJSON_CreateObject());
L
Liu Jicong 已提交
305
    if (pField == NULL) {
306
      cJSON_Delete(pRoot);
L
Liu Jicong 已提交
307 308
      return NULL;
    }
L
Liu Jicong 已提交
309 310
    // cjson only support int32_t or double
    // string are used to prohibit the loss of precision
311 312 313 314 315 316 317 318 319 320
    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 已提交
321
  }
L
Liu Jicong 已提交
322 323 324
  char* serialized = cJSON_Print(pRoot);
  cJSON_Delete(pRoot);
  return serialized;
L
Liu Jicong 已提交
325 326
}

327 328 329 330 331 332
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 已提交
333
  pWal->vers.firstVer = atoll(cJSON_GetStringValue(pField));
334
  pField = cJSON_GetObjectItem(pMeta, "snapshotVer");
L
Liu Jicong 已提交
335
  pWal->vers.snapshotVer = atoll(cJSON_GetStringValue(pField));
336
  pField = cJSON_GetObjectItem(pMeta, "commitVer");
L
Liu Jicong 已提交
337
  pWal->vers.commitVer = atoll(cJSON_GetStringValue(pField));
338
  pField = cJSON_GetObjectItem(pMeta, "lastVer");
L
Liu Jicong 已提交
339
  pWal->vers.lastVer = atoll(cJSON_GetStringValue(pField));
340 341 342

  pFiles = cJSON_GetObjectItem(pRoot, "files");
  int sz = cJSON_GetArraySize(pFiles);
L
Liu Jicong 已提交
343
  // deserialize
L
Liu Jicong 已提交
344 345
  SArray* pArray = pWal->fileInfoSet;
  taosArrayEnsureCap(pArray, sz);
L
Liu Jicong 已提交
346
  SWalFileInfo* pData = pArray->pData;
L
Liu Jicong 已提交
347
  for (int i = 0; i < sz; i++) {
L
Liu Jicong 已提交
348 349
    cJSON*        pInfoJson = cJSON_GetArrayItem(pFiles, i);
    SWalFileInfo* pInfo = &pData[i];
L
Liu Jicong 已提交
350 351 352 353 354 355 356 357 358 359 360 361
    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);
362
  pWal->fileInfoSet = pArray;
L
Liu Jicong 已提交
363
  pWal->writeCur = sz - 1;
L
Liu Jicong 已提交
364
  cJSON_Delete(pRoot);
365
  return 0;
L
Liu Jicong 已提交
366 367 368
}

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

wafwerar's avatar
wafwerar 已提交
373 374
  TdDirPtr pDir = taosOpenDir(pWal->path);
  if (pDir == NULL) {
L
Liu Jicong 已提交
375
    wError("vgId:%d, path:%s, failed to open since %s", pWal->cfg.vgId, pWal->path, strerror(errno));
L
Liu Jicong 已提交
376 377 378
    return -1;
  }

wafwerar's avatar
wafwerar 已提交
379
  TdDirEntryPtr pDirEntry;
L
Liu Jicong 已提交
380

L
Liu Jicong 已提交
381
  // find existing meta-ver[x].json
L
Liu Jicong 已提交
382
  int metaVer = -1;
wafwerar's avatar
wafwerar 已提交
383 384
  while ((pDirEntry = taosReadDir(pDir)) != NULL) {
    char* name = taosDirEntryBaseName(taosGetDirEntryName(pDirEntry));
L
Liu Jicong 已提交
385 386
    int   code = regexec(&walMetaRegexPattern, name, 0, NULL, 0);
    if (code == 0) {
L
Liu Jicong 已提交
387 388 389 390
      sscanf(name, "meta-ver%d", &metaVer);
      break;
    }
  }
wafwerar's avatar
wafwerar 已提交
391
  taosCloseDir(&pDir);
L
Liu Jicong 已提交
392
  regfree(&walMetaRegexPattern);
L
Liu Jicong 已提交
393 394 395
  return metaVer;
}

L
Liu Jicong 已提交
396
int walSaveMeta(SWal* pWal) {
L
Liu Jicong 已提交
397
  int  metaVer = walFindCurMetaVer(pWal);
L
Liu Jicong 已提交
398
  char fnameStr[WAL_FILE_LEN];
L
Liu Jicong 已提交
399
  walBuildMetaName(pWal, metaVer + 1, fnameStr);
400
  TdFilePtr pMataFile = taosOpenFile(fnameStr, TD_FILE_CREATE | TD_FILE_WRITE);
401
  if (pMataFile == NULL) {
L
Liu Jicong 已提交
402 403
    return -1;
  }
404
  char* serialized = walMetaSerialize(pWal);
L
Liu Jicong 已提交
405
  int   len = strlen(serialized);
406
  if (len != taosWriteFile(pMataFile, serialized, len)) {
L
Liu Jicong 已提交
407
    // TODO:clean file
L
Liu Jicong 已提交
408 409
    return -1;
  }
L
Liu Jicong 已提交
410

411
  taosCloseFile(&pMataFile);
L
Liu Jicong 已提交
412 413
  // delete old file
  if (metaVer > -1) {
L
Liu Jicong 已提交
414
    walBuildMetaName(pWal, metaVer, fnameStr);
415
    taosRemoveFile(fnameStr);
L
Liu Jicong 已提交
416
  }
wafwerar's avatar
wafwerar 已提交
417
  taosMemoryFree(serialized);
L
Liu Jicong 已提交
418 419 420
  return 0;
}

L
Liu Jicong 已提交
421
int walLoadMeta(SWal* pWal) {
L
Liu Jicong 已提交
422
  ASSERT(pWal->fileInfoSet->size == 0);
L
Liu Jicong 已提交
423
  // find existing meta file
L
Liu Jicong 已提交
424
  int metaVer = walFindCurMetaVer(pWal);
L
Liu Jicong 已提交
425
  if (metaVer == -1) {
L
Liu Jicong 已提交
426
    return -1;
L
Liu Jicong 已提交
427 428 429
  }
  char fnameStr[WAL_FILE_LEN];
  walBuildMetaName(pWal, metaVer, fnameStr);
L
Liu Jicong 已提交
430
  // read metafile
L
Liu Jicong 已提交
431 432 433
  int64_t fileSize = 0;
  taosStatFile(fnameStr, &fileSize, NULL);
  int   size = (int)fileSize;
wafwerar's avatar
wafwerar 已提交
434
  char* buf = taosMemoryMalloc(size + 5);
L
Liu Jicong 已提交
435
  if (buf == NULL) {
L
Liu Jicong 已提交
436
    terrno = TSDB_CODE_WAL_OUT_OF_MEMORY;
L
Liu Jicong 已提交
437 438
    return -1;
  }
L
Liu Jicong 已提交
439
  memset(buf, 0, size + 5);
440 441
  TdFilePtr pFile = taosOpenFile(fnameStr, TD_FILE_READ);
  if (pFile == NULL) {
L
Liu Jicong 已提交
442 443 444
    terrno = TSDB_CODE_WAL_FILE_CORRUPTED;
    return -1;
  }
445
  if (taosReadFile(pFile, buf, size) != size) {
L
Liu Jicong 已提交
446
    terrno = TAOS_SYSTEM_ERROR(errno);
447
    taosCloseFile(&pFile);
wafwerar's avatar
wafwerar 已提交
448
    taosMemoryFree(buf);
L
Liu Jicong 已提交
449 450
    return -1;
  }
L
Liu Jicong 已提交
451
  // load into fileInfoSet
452
  int code = walMetaDeserialize(pWal, buf);
453
  taosCloseFile(&pFile);
wafwerar's avatar
wafwerar 已提交
454
  taosMemoryFree(buf);
L
Liu Jicong 已提交
455
  return code;
L
Liu Jicong 已提交
456
}
457 458 459 460 461 462 463 464 465

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