tsdbFS.c 28.5 KB
Newer Older
H
Hongze Cheng 已提交
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/>.
 */

H
Hongze Cheng 已提交
16
#include "tsdb.h"
H
Hongze Cheng 已提交
17

H
Hongze Cheng 已提交
18 19
typedef enum { TSDB_TXN_TEMP_FILE = 0, TSDB_TXN_CURR_FILE } TSDB_TXN_FILE_T;
static const char *tsdbTxnFname[] = {"current.t", "current"};
H
Hongze Cheng 已提交
20
#define TSDB_MAX_FSETS(keep, days) ((keep) / (days) + 3)
H
Hongze Cheng 已提交
21

H
Hongze Cheng 已提交
22 23
static int  tsdbComparFidFSet(const void *arg1, const void *arg2);
static void tsdbResetFSStatus(SFSStatus *pStatus);
S
Shengliang Guan 已提交
24
static int  tsdbSaveFSStatus(STsdb *pRepo, SFSStatus *pStatus);
H
Hongze Cheng 已提交
25
static void tsdbApplyFSTxnOnDisk(SFSStatus *pFrom, SFSStatus *pTo);
S
Shengliang Guan 已提交
26
static void tsdbGetTxnFname(STsdb *pRepo, TSDB_TXN_FILE_T ftype, char fname[]);
H
Hongze Cheng 已提交
27 28 29 30
static int  tsdbOpenFSFromCurrent(STsdb *pRepo);
static int  tsdbScanAndTryFixFS(STsdb *pRepo);
static int  tsdbScanRootDir(STsdb *pRepo);
static int  tsdbScanDataDir(STsdb *pRepo);
S
Shengliang Guan 已提交
31
static bool tsdbIsTFileInFS(STsdbFS *pfs, const STfsFile *pf);
H
Hongze Cheng 已提交
32
static int  tsdbRestoreCurrent(STsdb *pRepo);
H
Hongze Cheng 已提交
33
static int  tsdbComparTFILE(const void *arg1, const void *arg2);
H
Hongze Cheng 已提交
34
static void tsdbScanAndTryFixDFilesHeader(STsdb *pRepo, int32_t *nExpired);
H
Hongze Cheng 已提交
35 36 37 38 39 40 41 42 43 44
// static int  tsdbProcessExpiredFS(STsdb *pRepo);
// static int  tsdbCreateMeta(STsdb *pRepo);

static void tsdbGetRootDir(int repoid, char dirName[]) {
  snprintf(dirName, TSDB_FILENAME_LEN, "vnode/vnode%d/tsdb", repoid);
}

static void tsdbGetDataDir(int repoid, char dirName[]) {
  snprintf(dirName, TSDB_FILENAME_LEN, "vnode/vnode%d/tsdb/data", repoid);
}
H
Hongze Cheng 已提交
45

46
// For backward compatibility
H
Hongze Cheng 已提交
47 48 49 50 51 52 53 54 55 56
// ================== CURRENT file header info
static int tsdbEncodeFSHeader(void **buf, SFSHeader *pHeader) {
  int tlen = 0;

  tlen += taosEncodeFixedU32(buf, pHeader->version);
  tlen += taosEncodeFixedU32(buf, pHeader->len);

  return tlen;
}

H
Hongze Cheng 已提交
57
static void *tsdbDecodeFSHeader(void *buf, SFSHeader *pHeader) {
H
Hongze Cheng 已提交
58 59
  buf = taosDecodeFixedU32(buf, &(pHeader->version));
  buf = taosDecodeFixedU32(buf, &(pHeader->len));
H
Hongze Cheng 已提交
60 61 62 63 64 65 66 67

  return buf;
}

// ================== STsdbFSMeta
static int tsdbEncodeFSMeta(void **buf, STsdbFSMeta *pMeta) {
  int tlen = 0;

H
Hongze Cheng 已提交
68
  tlen += taosEncodeFixedU32(buf, pMeta->version);
H
Hongze Cheng 已提交
69 70 71 72 73 74
  tlen += taosEncodeFixedI64(buf, pMeta->totalPoints);
  tlen += taosEncodeFixedI64(buf, pMeta->totalStorage);

  return tlen;
}

H
Hongze Cheng 已提交
75
static void *tsdbDecodeFSMeta(void *buf, STsdbFSMeta *pMeta) {
H
Hongze Cheng 已提交
76
  buf = taosDecodeFixedU32(buf, &(pMeta->version));
H
Hongze Cheng 已提交
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
  buf = taosDecodeFixedI64(buf, &(pMeta->totalPoints));
  buf = taosDecodeFixedI64(buf, &(pMeta->totalStorage));

  return buf;
}

// ================== SFSStatus
static int tsdbEncodeDFileSetArray(void **buf, SArray *pArray) {
  int      tlen = 0;
  uint64_t nset = taosArrayGetSize(pArray);

  tlen += taosEncodeFixedU64(buf, nset);
  for (size_t i = 0; i < nset; i++) {
    SDFileSet *pSet = taosArrayGet(pArray, i);

    tlen += tsdbEncodeDFileSet(buf, pSet);
  }

  return tlen;
}

H
Hongze Cheng 已提交
98 99
static void *tsdbDecodeDFileSetArray(STsdb *pRepo, void *buf, SArray *pArray) {
  uint64_t nset = 0;
H
Hongze Cheng 已提交
100 101 102 103 104

  taosArrayClear(pArray);

  buf = taosDecodeFixedU64(buf, &nset);
  for (size_t i = 0; i < nset; i++) {
C
update  
Cary Xu 已提交
105
    SDFileSet dset = {0};
S
Shengliang Guan 已提交
106
    buf = tsdbDecodeDFileSet(pRepo, buf, &dset);
H
Hongze Cheng 已提交
107 108 109 110 111 112
    taosArrayPush(pArray, (void *)(&dset));
  }
  return buf;
}

static int tsdbEncodeFSStatus(void **buf, SFSStatus *pStatus) {
H
Hongze Cheng 已提交
113
  // ASSERT(pStatus->pmf);
H
Hongze Cheng 已提交
114

H
Hongze Cheng 已提交
115 116
  int tlen = 0;

H
Hongze Cheng 已提交
117
  // tlen += tsdbEncodeSMFile(buf, pStatus->pmf);
H
Hongze Cheng 已提交
118 119 120 121 122
  tlen += tsdbEncodeDFileSetArray(buf, pStatus->df);

  return tlen;
}

H
Hongze Cheng 已提交
123
static void *tsdbDecodeFSStatus(STsdb *pRepo, void *buf, SFSStatus *pStatus) {
H
Hongze Cheng 已提交
124 125
  tsdbResetFSStatus(pStatus);

H
Hongze Cheng 已提交
126
  // pStatus->pmf = &(pStatus->mf);
H
Hongze Cheng 已提交
127

H
Hongze Cheng 已提交
128
  // buf = tsdbDecodeSMFile(buf, pStatus->pmf);
S
Shengliang Guan 已提交
129
  buf = tsdbDecodeDFileSetArray(pRepo, buf, pStatus->df);
H
Hongze Cheng 已提交
130 131 132 133 134

  return buf;
}

static SFSStatus *tsdbNewFSStatus(int maxFSet) {
wafwerar's avatar
wafwerar 已提交
135
  SFSStatus *pStatus = (SFSStatus *)taosMemoryCalloc(1, sizeof(*pStatus));
H
Hongze Cheng 已提交
136 137 138 139 140
  if (pStatus == NULL) {
    terrno = TSDB_CODE_TDB_OUT_OF_MEMORY;
    return NULL;
  }

H
Hongze Cheng 已提交
141
  // TSDB_FILE_SET_CLOSED(&(pStatus->mf));
H
Hongze Cheng 已提交
142

H
Hongze Cheng 已提交
143
  pStatus->df = taosArrayInit(maxFSet, sizeof(SDFileSet));
H
Hongze Cheng 已提交
144
  if (pStatus->df == NULL) {
H
Hongze Cheng 已提交
145
    terrno = TSDB_CODE_TDB_OUT_OF_MEMORY;
wafwerar's avatar
wafwerar 已提交
146
    taosMemoryFree(pStatus);
H
Hongze Cheng 已提交
147 148 149 150 151 152 153 154 155
    return NULL;
  }

  return pStatus;
}

static SFSStatus *tsdbFreeFSStatus(SFSStatus *pStatus) {
  if (pStatus) {
    pStatus->df = taosArrayDestroy(pStatus->df);
wafwerar's avatar
wafwerar 已提交
156
    taosMemoryFree(pStatus);
H
Hongze Cheng 已提交
157 158 159 160 161 162 163 164 165 166
  }

  return NULL;
}

static void tsdbResetFSStatus(SFSStatus *pStatus) {
  if (pStatus == NULL) {
    return;
  }

H
Hongze Cheng 已提交
167
  // TSDB_FILE_SET_CLOSED(&(pStatus->mf));
H
Hongze Cheng 已提交
168

H
Hongze Cheng 已提交
169
  // pStatus->pmf = NULL;
H
Hongze Cheng 已提交
170 171 172
  taosArrayClear(pStatus->df);
}

H
Hongze Cheng 已提交
173 174
// static void tsdbSetStatusMFile(SFSStatus *pStatus, const SMFile *pMFile) {
//   ASSERT(pStatus->pmf == NULL);
H
Hongze Cheng 已提交
175

H
Hongze Cheng 已提交
176 177 178
//   pStatus->pmf = &(pStatus->mf);
//   tsdbInitMFileEx(pStatus->pmf, (SMFile *)pMFile);
// }
H
Hongze Cheng 已提交
179 180

static int tsdbAddDFileSetToStatus(SFSStatus *pStatus, const SDFileSet *pSet) {
H
Hongze Cheng 已提交
181
  if (taosArrayPush(pStatus->df, (void *)pSet) == NULL) {
H
Hongze Cheng 已提交
182 183 184 185
    terrno = TSDB_CODE_TDB_OUT_OF_MEMORY;
    return -1;
  }

H
refact  
Hongze Cheng 已提交
186 187
  TSDB_FSET_SET_CLOSED(((SDFileSet *)taosArrayGetLast(pStatus->df)));

H
Hongze Cheng 已提交
188 189 190
  return 0;
}

H
Hongze Cheng 已提交
191
// ================== STsdbFS
H
more  
Hongze Cheng 已提交
192
STsdbFS *tsdbNewFS(const STsdbCfg *pCfg) {
H
Hongze Cheng 已提交
193
  int      keep = pCfg->keep2;
H
refact  
Hongze Cheng 已提交
194
  int      days = pCfg->days;
H
Hongze Cheng 已提交
195 196 197
  int      maxFSet = TSDB_MAX_FSETS(keep, days);
  STsdbFS *pfs;

wafwerar's avatar
wafwerar 已提交
198
  pfs = (STsdbFS *)taosMemoryCalloc(1, sizeof(*pfs));
H
Hongze Cheng 已提交
199
  if (pfs == NULL) {
H
Hongze Cheng 已提交
200 201 202 203
    terrno = TSDB_CODE_TDB_OUT_OF_MEMORY;
    return NULL;
  }

wafwerar's avatar
wafwerar 已提交
204
  int code = taosThreadRwlockInit(&(pfs->lock), NULL);
H
Hongze Cheng 已提交
205 206
  if (code) {
    terrno = TAOS_SYSTEM_ERROR(code);
wafwerar's avatar
wafwerar 已提交
207
    taosMemoryFree(pfs);
H
Hongze Cheng 已提交
208 209 210
    return NULL;
  }

H
Hongze Cheng 已提交
211 212 213
  pfs->cstatus = tsdbNewFSStatus(maxFSet);
  if (pfs->cstatus == NULL) {
    tsdbFreeFS(pfs);
H
Hongze Cheng 已提交
214 215 216
    return NULL;
  }

H
Hongze Cheng 已提交
217 218
  pfs->metaCache = taosHashInit(4096, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
  if (pfs->metaCache == NULL) {
H
Hongze Cheng 已提交
219
    terrno = TSDB_CODE_TDB_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
220
    tsdbFreeFS(pfs);
H
Hongze Cheng 已提交
221 222 223
    return NULL;
  }

224
  pfs->intxn = false;
225
  pfs->metaCacheComp = NULL;
226

H
Hongze Cheng 已提交
227 228 229
  pfs->nstatus = tsdbNewFSStatus(maxFSet);
  if (pfs->nstatus == NULL) {
    tsdbFreeFS(pfs);
H
Hongze Cheng 已提交
230 231 232
    return NULL;
  }

H
Hongze Cheng 已提交
233
  return pfs;
H
Hongze Cheng 已提交
234 235
}

H
Hongze Cheng 已提交
236 237 238 239 240 241
void *tsdbFreeFS(STsdbFS *pfs) {
  if (pfs) {
    pfs->nstatus = tsdbFreeFSStatus(pfs->nstatus);
    taosHashCleanup(pfs->metaCache);
    pfs->metaCache = NULL;
    pfs->cstatus = tsdbFreeFSStatus(pfs->cstatus);
wafwerar's avatar
wafwerar 已提交
242
    taosThreadRwlockDestroy(&(pfs->lock));
wafwerar's avatar
wafwerar 已提交
243
    taosMemoryFree(pfs);
H
Hongze Cheng 已提交
244
  }
H
Hongze Cheng 已提交
245

H
Hongze Cheng 已提交
246 247 248
  return NULL;
}

H
Hongze Cheng 已提交
249
int tsdbOpenFS(STsdb *pRepo) {
H
refact  
Hongze Cheng 已提交
250 251
  STsdbFS *pfs = REPO_FS(pRepo);
  char     current[TSDB_FILENAME_LEN] = "\0";
252
  int      nExpired = 0;
H
Hongze Cheng 已提交
253 254 255

  ASSERT(pfs != NULL);

S
Shengliang Guan 已提交
256
  tsdbGetTxnFname(pRepo, TSDB_TXN_CURR_FILE, current);
H
Hongze Cheng 已提交
257

258
  tsdbGetRtnSnap(pRepo, &pRepo->rtn);
259
  if (taosCheckExistFile(current)) {
H
Hongze Cheng 已提交
260 261 262 263
    if (tsdbOpenFSFromCurrent(pRepo) < 0) {
      tsdbError("vgId:%d failed to open FS since %s", REPO_ID(pRepo), tstrerror(terrno));
      return -1;
    }
H
Hongze Cheng 已提交
264

265
    tsdbScanAndTryFixDFilesHeader(pRepo, &nExpired);
H
Hongze Cheng 已提交
266 267 268
    // if (nExpired > 0) {
    //   tsdbProcessExpiredFS(pRepo);
    // }
H
refact  
Hongze Cheng 已提交
269
  } else {
270
    // should skip expired fileset inside of the function
H
refact  
Hongze Cheng 已提交
271 272 273 274 275 276 277 278 279
    if (tsdbRestoreCurrent(pRepo) < 0) {
      tsdbError("vgId:%d failed to restore current file since %s", REPO_ID(pRepo), tstrerror(terrno));
      return -1;
    }
  }

  if (tsdbScanAndTryFixFS(pRepo) < 0) {
    tsdbError("vgId:%d failed to scan and fix FS since %s", REPO_ID(pRepo), tstrerror(terrno));
    return -1;
H
Hongze Cheng 已提交
280
  }
H
Hongze Cheng 已提交
281

H
Hongze Cheng 已提交
282 283 284 285 286
  // // Load meta cache if has meta file
  // if ((!(pRepo->state & TSDB_STATE_BAD_META)) && tsdbLoadMetaCache(pRepo, true) < 0) {
  //   tsdbError("vgId:%d failed to open FS while loading meta cache since %s", REPO_ID(pRepo), tstrerror(terrno));
  //   return -1;
  // }
H
Hongze Cheng 已提交
287

H
Hongze Cheng 已提交
288 289 290
  return 0;
}

H
Hongze Cheng 已提交
291
void tsdbCloseFS(STsdb *pRepo) {
H
refact  
Hongze Cheng 已提交
292
  // Do nothing
H
Hongze Cheng 已提交
293 294
}

H
Hongze Cheng 已提交
295
// Start a new transaction to modify the file system
H
Hongze Cheng 已提交
296
void tsdbStartFSTxn(STsdb *pRepo, int64_t pointsAdd, int64_t storageAdd) {
H
Hongze Cheng 已提交
297
  STsdbFS *pfs = REPO_FS(pRepo);
H
Hongze Cheng 已提交
298
  ASSERT(pfs->intxn == false);
H
Hongze Cheng 已提交
299

H
Hongze Cheng 已提交
300 301
  pfs->intxn = true;
  tsdbResetFSStatus(pfs->nstatus);
H
Hongze Cheng 已提交
302
  pfs->nstatus->meta = pfs->cstatus->meta;
H
Hongze Cheng 已提交
303
  // if (pfs->cstatus->pmf == NULL) {
H
Hongze Cheng 已提交
304
  pfs->nstatus->meta.version += 1;
H
Hongze Cheng 已提交
305 306 307
  // } else {
  //   pfs->nstatus->meta.version = pfs->cstatus->meta.version + 1;
  // }
H
Hongze Cheng 已提交
308
  pfs->nstatus->meta.totalPoints = pfs->cstatus->meta.totalPoints + pointsAdd;
H
Hongze Cheng 已提交
309
  pfs->nstatus->meta.totalStorage = pfs->cstatus->meta.totalStorage += storageAdd;
H
Hongze Cheng 已提交
310
}
H
Hongze Cheng 已提交
311

H
Hongze Cheng 已提交
312 313
void tsdbUpdateFSTxnMeta(STsdbFS *pfs, STsdbFSMeta *pMeta) { pfs->nstatus->meta = *pMeta; }

H
Hongze Cheng 已提交
314
int tsdbEndFSTxn(STsdb *pRepo) {
H
Hongze Cheng 已提交
315
  STsdbFS *pfs = REPO_FS(pRepo);
H
Hongze Cheng 已提交
316 317
  ASSERT(FS_IN_TXN(pfs));
  SFSStatus *pStatus;
H
Hongze Cheng 已提交
318

H
Hongze Cheng 已提交
319
  // Write current file system snapshot
S
Shengliang Guan 已提交
320
  if (tsdbSaveFSStatus(pRepo, pfs->nstatus) < 0) {
H
Hongze Cheng 已提交
321
    tsdbEndFSTxnWithError(pfs);
H
Hongze Cheng 已提交
322 323
    return -1;
  }
H
Hongze Cheng 已提交
324

H
Hongze Cheng 已提交
325
  // Make new
H
Hongze Cheng 已提交
326 327 328 329 330
  tsdbWLockFS(pfs);
  pStatus = pfs->cstatus;
  pfs->cstatus = pfs->nstatus;
  pfs->nstatus = pStatus;
  tsdbUnLockFS(pfs);
H
Hongze Cheng 已提交
331

H
Hongze Cheng 已提交
332
  // Apply actual change to each file and SDFileSet
H
Hongze Cheng 已提交
333
  tsdbApplyFSTxnOnDisk(pfs->nstatus, pfs->cstatus);
H
Hongze Cheng 已提交
334

H
Hongze Cheng 已提交
335
  pfs->intxn = false;
H
Hongze Cheng 已提交
336 337 338
  return 0;
}

H
Hongze Cheng 已提交
339
int tsdbEndFSTxnWithError(STsdbFS *pfs) {
H
Hongze Cheng 已提交
340 341
  tsdbApplyFSTxnOnDisk(pfs->nstatus, pfs->cstatus);
  // TODO: if mf change, reload pfs->metaCache
H
Hongze Cheng 已提交
342 343
  pfs->intxn = false;
  return 0;
H
Hongze Cheng 已提交
344 345
}

H
Hongze Cheng 已提交
346
// void tsdbUpdateMFile(STsdbFS *pfs, const SMFile *pMFile) { tsdbSetStatusMFile(pfs->nstatus, pMFile); }
H
Hongze Cheng 已提交
347

H
Hongze Cheng 已提交
348
int tsdbUpdateDFileSet(STsdbFS *pfs, const SDFileSet *pSet) { return tsdbAddDFileSetToStatus(pfs->nstatus, pSet); }
H
Hongze Cheng 已提交
349

S
Shengliang Guan 已提交
350
static int tsdbSaveFSStatus(STsdb *pRepo, SFSStatus *pStatus) {
H
Hongze Cheng 已提交
351
  SFSHeader fsheader;
H
Hongze Cheng 已提交
352 353
  void     *pBuf = NULL;
  void     *ptr;
H
Hongze Cheng 已提交
354
  char      hbuf[TSDB_FILE_HEAD_SIZE] = "\0";
H
Hongze Cheng 已提交
355 356
  char      tfname[TSDB_FILENAME_LEN] = "\0";
  char      cfname[TSDB_FILENAME_LEN] = "\0";
H
Hongze Cheng 已提交
357

S
Shengliang Guan 已提交
358 359
  tsdbGetTxnFname(pRepo, TSDB_TXN_TEMP_FILE, tfname);
  tsdbGetTxnFname(pRepo, TSDB_TXN_CURR_FILE, cfname);
H
Hongze Cheng 已提交
360

361
  TdFilePtr pFile = taosOpenFile(tfname, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
362
  if (pFile == NULL) {
H
Hongze Cheng 已提交
363
    terrno = TAOS_SYSTEM_ERROR(errno);
H
Hongze Cheng 已提交
364 365 366
    return -1;
  }

367
  fsheader.version = TSDB_LATEST_SFS_VER;
H
Hongze Cheng 已提交
368
  if (taosArrayGetSize(pStatus->df) == 0) {
H
Hongze Cheng 已提交
369 370
    fsheader.len = 0;
  } else {
H
Hongze Cheng 已提交
371
    fsheader.len = tsdbEncodeFSStatus(NULL, pStatus) + sizeof(TSCKSUM);
H
Hongze Cheng 已提交
372 373
  }

H
Hongze Cheng 已提交
374 375 376
  // Encode header part and write
  ptr = hbuf;
  tsdbEncodeFSHeader(&ptr, &fsheader);
H
Hongze Cheng 已提交
377
  tsdbEncodeFSMeta(&ptr, &(pStatus->meta));
H
Hongze Cheng 已提交
378

H
Hongze Cheng 已提交
379
  taosCalcChecksumAppend(0, (uint8_t *)hbuf, TSDB_FILE_HEAD_SIZE);
H
Hongze Cheng 已提交
380

381
  if (taosWriteFile(pFile, hbuf, TSDB_FILE_HEAD_SIZE) < TSDB_FILE_HEAD_SIZE) {
H
Hongze Cheng 已提交
382
    terrno = TAOS_SYSTEM_ERROR(errno);
383
    taosCloseFile(&pFile);
384
    taosRemoveFile(tfname);
H
Hongze Cheng 已提交
385 386 387
    return -1;
  }

H
Hongze Cheng 已提交
388 389 390
  // Encode file status and write to file
  if (fsheader.len > 0) {
    if (tsdbMakeRoom(&(pBuf), fsheader.len) < 0) {
391
      taosCloseFile(&pFile);
392
      taosRemoveFile(tfname);
H
Hongze Cheng 已提交
393
      return -1;
H
Hongze Cheng 已提交
394 395
    }

H
Hongze Cheng 已提交
396
    ptr = pBuf;
H
Hongze Cheng 已提交
397
    tsdbEncodeFSStatus(&ptr, pStatus);
H
Hongze Cheng 已提交
398
    taosCalcChecksumAppend(0, (uint8_t *)pBuf, fsheader.len);
H
Hongze Cheng 已提交
399

400
    if (taosWriteFile(pFile, pBuf, fsheader.len) < fsheader.len) {
H
Hongze Cheng 已提交
401
      terrno = TAOS_SYSTEM_ERROR(errno);
402
      taosCloseFile(&pFile);
403
      (void)taosRemoveFile(tfname);
H
Hongze Cheng 已提交
404
      taosTZfree(pBuf);
H
Hongze Cheng 已提交
405 406 407 408
      return -1;
    }
  }

H
Hongze Cheng 已提交
409
  // fsync, close and rename
410
  if (taosFsyncFile(pFile) < 0) {
H
Hongze Cheng 已提交
411
    terrno = TAOS_SYSTEM_ERROR(errno);
412
    taosCloseFile(&pFile);
413
    taosRemoveFile(tfname);
H
Hongze Cheng 已提交
414 415
    taosTZfree(pBuf);
    return -1;
H
Hongze Cheng 已提交
416
  }
H
Hongze Cheng 已提交
417

418
  (void)taosCloseFile(&pFile);
H
Hongze Cheng 已提交
419
  (void)taosRenameFile(tfname, cfname);
H
Hongze Cheng 已提交
420
  taosTZfree(pBuf);
H
Hongze Cheng 已提交
421

H
Hongze Cheng 已提交
422 423
  return 0;
}
H
Hongze Cheng 已提交
424

H
Hongze Cheng 已提交
425
static void tsdbApplyFSTxnOnDisk(SFSStatus *pFrom, SFSStatus *pTo) {
H
Hongze Cheng 已提交
426 427 428 429 430
  int        ifrom = 0;
  int        ito = 0;
  size_t     sizeFrom, sizeTo;
  SDFileSet *pSetFrom;
  SDFileSet *pSetTo;
H
Hongze Cheng 已提交
431

H
Hongze Cheng 已提交
432 433
  sizeFrom = taosArrayGetSize(pFrom->df);
  sizeTo = taosArrayGetSize(pTo->df);
H
Hongze Cheng 已提交
434

H
Hongze Cheng 已提交
435
  // Apply meta file change
H
Hongze Cheng 已提交
436
  // (void)tsdbApplyMFileChange(pFrom->pmf, pTo->pmf);
H
Hongze Cheng 已提交
437

H
Hongze Cheng 已提交
438 439 440
  // Apply SDFileSet change
  if (ifrom >= sizeFrom) {
    pSetFrom = NULL;
H
Hongze Cheng 已提交
441
  } else {
H
Hongze Cheng 已提交
442
    pSetFrom = taosArrayGet(pFrom->df, ifrom);
H
Hongze Cheng 已提交
443 444
  }

H
Hongze Cheng 已提交
445 446 447 448 449
  if (ito >= sizeTo) {
    pSetTo = NULL;
  } else {
    pSetTo = taosArrayGet(pTo->df, ito);
  }
H
Hongze Cheng 已提交
450

H
Hongze Cheng 已提交
451 452
  while (true) {
    if ((pSetTo == NULL) && (pSetFrom == NULL)) break;
H
Hongze Cheng 已提交
453

H
Hongze Cheng 已提交
454 455
    if (pSetTo == NULL || (pSetFrom && pSetFrom->fid < pSetTo->fid)) {
      tsdbApplyDFileSetChange(pSetFrom, NULL);
H
Hongze Cheng 已提交
456

H
Hongze Cheng 已提交
457 458 459 460 461 462 463 464
      ifrom++;
      if (ifrom >= sizeFrom) {
        pSetFrom = NULL;
      } else {
        pSetFrom = taosArrayGet(pFrom->df, ifrom);
      }
    } else if (pSetFrom == NULL || pSetFrom->fid > pSetTo->fid) {
      // Do nothing
H
Hongze Cheng 已提交
465 466 467 468 469
      ito++;
      if (ito >= sizeTo) {
        pSetTo = NULL;
      } else {
        pSetTo = taosArrayGet(pTo->df, ito);
H
Hongze Cheng 已提交
470 471 472
      }
    } else {
      tsdbApplyDFileSetChange(pSetFrom, pSetTo);
H
Hongze Cheng 已提交
473

H
Hongze Cheng 已提交
474 475 476 477 478 479
      ifrom++;
      if (ifrom >= sizeFrom) {
        pSetFrom = NULL;
      } else {
        pSetFrom = taosArrayGet(pFrom->df, ifrom);
      }
H
Hongze Cheng 已提交
480

H
Hongze Cheng 已提交
481 482 483 484 485 486 487 488
      ito++;
      if (ito >= sizeTo) {
        pSetTo = NULL;
      } else {
        pSetTo = taosArrayGet(pTo->df, ito);
      }
    }
  }
H
Hongze Cheng 已提交
489 490
}

H
Hongze Cheng 已提交
491 492 493 494 495
// ================== SFSIter
// ASSUMPTIONS: the FS Should be read locked when calling these functions
void tsdbFSIterInit(SFSIter *pIter, STsdbFS *pfs, int direction) {
  pIter->pfs = pfs;
  pIter->direction = direction;
H
Hongze Cheng 已提交
496

H
Hongze Cheng 已提交
497
  size_t size = taosArrayGetSize(pfs->cstatus->df);
H
Hongze Cheng 已提交
498

H
Hongze Cheng 已提交
499
  pIter->version = pfs->cstatus->meta.version;
H
Hongze Cheng 已提交
500

H
Hongze Cheng 已提交
501 502 503 504 505 506 507
  if (size == 0) {
    pIter->index = -1;
    pIter->fid = TSDB_IVLD_FID;
  } else {
    if (direction == TSDB_FS_ITER_FORWARD) {
      pIter->index = 0;
    } else {
S
TD-1207  
Shengliang Guan 已提交
508
      pIter->index = (int)(size - 1);
H
Hongze Cheng 已提交
509
    }
H
Hongze Cheng 已提交
510

H
Hongze Cheng 已提交
511
    pIter->fid = ((SDFileSet *)taosArrayGet(pfs->cstatus->df, pIter->index))->fid;
H
Hongze Cheng 已提交
512 513 514
  }
}

H
Hongze Cheng 已提交
515 516 517
void tsdbFSIterSeek(SFSIter *pIter, int fid) {
  STsdbFS *pfs = pIter->pfs;
  size_t   size = taosArrayGetSize(pfs->cstatus->df);
H
Hongze Cheng 已提交
518

H
Hongze Cheng 已提交
519 520 521 522 523
  int flags;
  if (pIter->direction == TSDB_FS_ITER_FORWARD) {
    flags = TD_GE;
  } else {
    flags = TD_LE;
H
Hongze Cheng 已提交
524 525
  }

H
Hongze Cheng 已提交
526
  void *ptr = taosbsearch(&fid, pfs->cstatus->df->pData, size, sizeof(SDFileSet), tsdbComparFidFSet, flags);
H
Hongze Cheng 已提交
527 528 529 530
  if (ptr == NULL) {
    pIter->index = -1;
    pIter->fid = TSDB_IVLD_FID;
  } else {
S
TD-1207  
Shengliang Guan 已提交
531
    pIter->index = (int)(TARRAY_ELEM_IDX(pfs->cstatus->df, ptr));
H
Hongze Cheng 已提交
532
    pIter->fid = ((SDFileSet *)ptr)->fid;
H
Hongze Cheng 已提交
533 534 535
  }
}

H
Hongze Cheng 已提交
536
SDFileSet *tsdbFSIterNext(SFSIter *pIter) {
H
Hongze Cheng 已提交
537
  STsdbFS   *pfs = pIter->pfs;
H
Hongze Cheng 已提交
538
  SDFileSet *pSet;
H
Hongze Cheng 已提交
539

H
Hongze Cheng 已提交
540 541
  if (pIter->index < 0) {
    ASSERT(pIter->fid == TSDB_IVLD_FID);
H
Hongze Cheng 已提交
542 543 544
    return NULL;
  }

H
Hongze Cheng 已提交
545
  ASSERT(pIter->fid != TSDB_IVLD_FID);
H
Hongze Cheng 已提交
546

H
Hongze Cheng 已提交
547
  if (pIter->version != pfs->cstatus->meta.version) {
548
    pIter->version = pfs->cstatus->meta.version;
H
Hongze Cheng 已提交
549
    tsdbFSIterSeek(pIter, pIter->fid);
H
Hongze Cheng 已提交
550 551
  }

H
Hongze Cheng 已提交
552 553
  if (pIter->index < 0) {
    return NULL;
H
Hongze Cheng 已提交
554 555
  }

H
Hongze Cheng 已提交
556 557
  pSet = (SDFileSet *)taosArrayGet(pfs->cstatus->df, pIter->index);
  ASSERT(pSet->fid == pIter->fid);
H
Hongze Cheng 已提交
558

H
Hongze Cheng 已提交
559 560 561 562
  if (pIter->direction == TSDB_FS_ITER_FORWARD) {
    pIter->index++;
    if (pIter->index >= taosArrayGetSize(pfs->cstatus->df)) {
      pIter->index = -1;
H
Hongze Cheng 已提交
563 564
    }
  } else {
H
Hongze Cheng 已提交
565
    pIter->index--;
H
Hongze Cheng 已提交
566 567
  }

H
Hongze Cheng 已提交
568
  if (pIter->index >= 0) {
H
Hongze Cheng 已提交
569
    pIter->fid = ((SDFileSet *)taosArrayGet(pfs->cstatus->df, pIter->index))->fid;
H
Hongze Cheng 已提交
570
  } else {
H
Hongze Cheng 已提交
571
    pIter->fid = TSDB_IVLD_FID;
H
Hongze Cheng 已提交
572 573
  }

H
Hongze Cheng 已提交
574
  return pSet;
H
Hongze Cheng 已提交
575 576 577 578 579 580 581 582 583 584 585 586 587
}

static int tsdbComparFidFSet(const void *arg1, const void *arg2) {
  int        fid = *(int *)arg1;
  SDFileSet *pSet = (SDFileSet *)arg2;

  if (fid < pSet->fid) {
    return -1;
  } else if (fid == pSet->fid) {
    return 0;
  } else {
    return 1;
  }
H
Hongze Cheng 已提交
588 589
}

S
Shengliang Guan 已提交
590
static void tsdbGetTxnFname(STsdb *pRepo, TSDB_TXN_FILE_T ftype, char fname[]) {
H
Hongze Cheng 已提交
591
  snprintf(fname, TSDB_FILENAME_LEN, "%s/vnode/vnode%d/tsdb/%s", tfsGetPrimaryPath(REPO_TFS(pRepo)), REPO_ID(pRepo),
S
Shengliang Guan 已提交
592
           tsdbTxnFname[ftype]);
H
Hongze Cheng 已提交
593 594
}

H
Hongze Cheng 已提交
595
static int tsdbOpenFSFromCurrent(STsdb *pRepo) {
H
Hongze Cheng 已提交
596
  STsdbFS  *pfs = REPO_FS(pRepo);
597
  TdFilePtr pFile = NULL;
H
Hongze Cheng 已提交
598
  void     *buffer = NULL;
H
Hongze Cheng 已提交
599 600
  SFSHeader fsheader;
  char      current[TSDB_FILENAME_LEN] = "\0";
H
Hongze Cheng 已提交
601
  void     *ptr;
H
Hongze Cheng 已提交
602

S
Shengliang Guan 已提交
603
  tsdbGetTxnFname(pRepo, TSDB_TXN_CURR_FILE, current);
H
Hongze Cheng 已提交
604 605

  // current file exists, try to recover
606 607
  pFile = taosOpenFile(current, TD_FILE_READ);
  if (pFile == NULL) {
H
Hongze Cheng 已提交
608 609 610 611 612 613 614 615 616
    tsdbError("vgId:%d failed to open file %s since %s", REPO_ID(pRepo), current, strerror(errno));
    terrno = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  if (tsdbMakeRoom(&buffer, TSDB_FILE_HEAD_SIZE) < 0) {
    goto _err;
  }

617
  int nread = (int)taosReadFile(pFile, buffer, TSDB_FILE_HEAD_SIZE);
H
Hongze Cheng 已提交
618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
  if (nread < 0) {
    tsdbError("vgId:%d failed to read %d bytes from file %s since %s", REPO_ID(pRepo), TSDB_FILENAME_LEN, current,
              strerror(errno));
    terrno = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  if (nread < TSDB_FILE_HEAD_SIZE) {
    tsdbError("vgId:%d failed to read header of file %s, read bytes:%d", REPO_ID(pRepo), current, nread);
    terrno = TSDB_CODE_TDB_FILE_CORRUPTED;
    goto _err;
  }

  if (!taosCheckChecksumWhole((uint8_t *)buffer, TSDB_FILE_HEAD_SIZE)) {
    tsdbError("vgId:%d header of file %s failed checksum check", REPO_ID(pRepo), current);
    terrno = TSDB_CODE_TDB_FILE_CORRUPTED;
    goto _err;
  }

H
Hongze Cheng 已提交
637
  SFSStatus *pStatus = pfs->cstatus;
H
Hongze Cheng 已提交
638 639
  ptr = buffer;
  ptr = tsdbDecodeFSHeader(ptr, &fsheader);
H
Hongze Cheng 已提交
640
  ptr = tsdbDecodeFSMeta(ptr, &(pStatus->meta));
H
Hongze Cheng 已提交
641

642
  if (fsheader.version != TSDB_LATEST_SFS_VER) {
H
Hongze Cheng 已提交
643 644 645 646 647 648 649 650
    // TODO: handle file version change
  }

  if (fsheader.len > 0) {
    if (tsdbMakeRoom(&buffer, fsheader.len) < 0) {
      goto _err;
    }

651
    nread = (int)taosReadFile(pFile, buffer, fsheader.len);
H
Hongze Cheng 已提交
652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670
    if (nread < 0) {
      tsdbError("vgId:%d failed to read file %s since %s", REPO_ID(pRepo), current, strerror(errno));
      terrno = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }

    if (nread < fsheader.len) {
      tsdbError("vgId:%d failed to read %d bytes from file %s", REPO_ID(pRepo), fsheader.len, current);
      terrno = TSDB_CODE_TDB_FILE_CORRUPTED;
      goto _err;
    }

    if (!taosCheckChecksumWhole((uint8_t *)buffer, fsheader.len)) {
      tsdbError("vgId:%d file %s is corrupted since wrong checksum", REPO_ID(pRepo), current);
      terrno = TSDB_CODE_TDB_FILE_CORRUPTED;
      goto _err;
    }

    ptr = buffer;
S
Shengliang Guan 已提交
671
    ptr = tsdbDecodeFSStatus(pRepo, ptr, pStatus);
H
Hongze Cheng 已提交
672 673 674 675 676
  } else {
    tsdbResetFSStatus(pStatus);
  }

  taosTZfree(buffer);
677
  taosCloseFile(&pFile);
H
Hongze Cheng 已提交
678 679 680 681

  return 0;

_err:
682 683
  if (pFile != NULL) {
    taosCloseFile(&pFile);
H
Hongze Cheng 已提交
684 685 686 687 688 689
  }
  taosTZfree(buffer);
  return -1;
}

// Scan and try to fix incorrect files
H
Hongze Cheng 已提交
690
static int tsdbScanAndTryFixFS(STsdb *pRepo) {
H
Hongze Cheng 已提交
691
  STsdbFS   *pfs = REPO_FS(pRepo);
H
Hongze Cheng 已提交
692 693
  SFSStatus *pStatus = pfs->cstatus;

H
Hongze Cheng 已提交
694 695 696 697
  // if (tsdbScanAndTryFixMFile(pRepo) < 0) {
  //   tsdbError("vgId:%d failed to fix MFile since %s", REPO_ID(pRepo), tstrerror(terrno));
  //   return -1;
  // }
H
Hongze Cheng 已提交
698 699 700 701 702 703

  size_t size = taosArrayGetSize(pStatus->df);

  for (size_t i = 0; i < size; i++) {
    SDFileSet *pSet = (SDFileSet *)taosArrayGet(pStatus->df, i);

H
Hongze Cheng 已提交
704
    if (tsdbScanAndTryFixDFileSet(pRepo, pSet) < 0) {
H
Hongze Cheng 已提交
705 706 707 708 709
      tsdbError("vgId:%d failed to fix MFile since %s", REPO_ID(pRepo), tstrerror(terrno));
      return -1;
    }
  }

H
Hongze Cheng 已提交
710
  // remove those unused files
711 712
  tsdbScanRootDir(pRepo);
  tsdbScanDataDir(pRepo);
H
Hongze Cheng 已提交
713 714 715
  return 0;
}

H
Hongze Cheng 已提交
716
static int tsdbScanRootDir(STsdb *pRepo) {
H
Hongze Cheng 已提交
717 718 719
  char            rootDir[TSDB_FILENAME_LEN];
  char            bname[TSDB_FILENAME_LEN];
  STsdbFS        *pfs = REPO_FS(pRepo);
S
Shengliang Guan 已提交
720
  const STfsFile *pf;
721 722

  tsdbGetRootDir(REPO_ID(pRepo), rootDir);
H
Hongze Cheng 已提交
723
  STfsDir *tdir = tfsOpendir(REPO_TFS(pRepo), rootDir);
724 725 726 727 728 729
  if (tdir == NULL) {
    tsdbError("vgId:%d failed to open directory %s since %s", REPO_ID(pRepo), rootDir, tstrerror(terrno));
    return -1;
  }

  while ((pf = tfsReaddir(tdir))) {
S
Shengliang Guan 已提交
730
    tfsBasename(pf, bname);
731 732 733 734 735 736

    if (strcmp(bname, tsdbTxnFname[TSDB_TXN_CURR_FILE]) == 0 || strcmp(bname, "data") == 0) {
      // Skip current file and data directory
      continue;
    }

H
Hongze Cheng 已提交
737 738 739
    // if (/*pfs->cstatus->pmf && */ tfsIsSameFile(pf, &(pfs->cstatus->pmf->f))) {
    //   continue;
    // }
740

S
Shengliang Guan 已提交
741 742
    (void)tfsRemoveFile(pf);
    tsdbDebug("vgId:%d invalid file %s is removed", REPO_ID(pRepo), pf->aname);
743 744 745 746 747 748 749
  }

  tfsClosedir(tdir);

  return 0;
}

H
Hongze Cheng 已提交
750
static int tsdbScanDataDir(STsdb *pRepo) {
H
Hongze Cheng 已提交
751 752 753
  char            dataDir[TSDB_FILENAME_LEN];
  char            bname[TSDB_FILENAME_LEN];
  STsdbFS        *pfs = REPO_FS(pRepo);
S
Shengliang Guan 已提交
754
  const STfsFile *pf;
755 756

  tsdbGetDataDir(REPO_ID(pRepo), dataDir);
H
Hongze Cheng 已提交
757
  STfsDir *tdir = tfsOpendir(REPO_TFS(pRepo), dataDir);
758 759 760 761 762 763
  if (tdir == NULL) {
    tsdbError("vgId:%d failed to open directory %s since %s", REPO_ID(pRepo), dataDir, tstrerror(terrno));
    return -1;
  }

  while ((pf = tfsReaddir(tdir))) {
S
Shengliang Guan 已提交
764
    tfsBasename(pf, bname);
765 766

    if (!tsdbIsTFileInFS(pfs, pf)) {
S
Shengliang Guan 已提交
767 768
      (void)tfsRemoveFile(pf);
      tsdbDebug("vgId:%d invalid file %s is removed", REPO_ID(pRepo), pf->aname);
769 770 771 772 773 774 775 776
    }
  }

  tfsClosedir(tdir);

  return 0;
}

S
Shengliang Guan 已提交
777
static bool tsdbIsTFileInFS(STsdbFS *pfs, const STfsFile *pf) {
778 779 780 781 782 783 784 785 786 787 788 789 790 791
  SFSIter fsiter;
  tsdbFSIterInit(&fsiter, pfs, TSDB_FS_ITER_FORWARD);
  SDFileSet *pSet;

  while ((pSet = tsdbFSIterNext(&fsiter))) {
    for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
      SDFile *pDFile = TSDB_DFILE_IN_SET(pSet, ftype);
      if (tfsIsSameFile(pf, TSDB_FILE_F(pDFile))) {
        return true;
      }
    }
  }

  return false;
H
Hongze Cheng 已提交
792 793
}

H
Hongze Cheng 已提交
794
static int tsdbRestoreDFileSet(STsdb *pRepo) {
H
Hongze Cheng 已提交
795 796 797
  char            dataDir[TSDB_FILENAME_LEN];
  char            bname[TSDB_FILENAME_LEN];
  STfsDir        *tdir = NULL;
S
Shengliang Guan 已提交
798
  const STfsFile *pf = NULL;
H
Hongze Cheng 已提交
799 800 801 802
  const char     *pattern = "^v[0-9]+f[0-9]+\\.(head|data|last|smad|smal)(-ver[0-9]+)?$";
  SArray         *fArray = NULL;
  regex_t         regex;
  STsdbFS        *pfs = REPO_FS(pRepo);
H
Hongze Cheng 已提交
803 804

  tsdbGetDataDir(REPO_ID(pRepo), dataDir);
H
Hongze Cheng 已提交
805 806 807 808

  // Resource allocation and init
  regcomp(&regex, pattern, REG_EXTENDED);

S
Shengliang Guan 已提交
809
  fArray = taosArrayInit(1024, sizeof(STfsFile));
H
Hongze Cheng 已提交
810 811 812 813 814 815 816 817
  if (fArray == NULL) {
    terrno = TSDB_CODE_TDB_OUT_OF_MEMORY;
    tsdbError("vgId:%d failed to restore DFileSet while open directory %s since %s", REPO_ID(pRepo), dataDir,
              tstrerror(terrno));
    regfree(&regex);
    return -1;
  }

H
Hongze Cheng 已提交
818
  tdir = tfsOpendir(REPO_TFS(pRepo), dataDir);
H
Hongze Cheng 已提交
819
  if (tdir == NULL) {
H
Hongze Cheng 已提交
820 821 822 823
    tsdbError("vgId:%d failed to restore DFileSet while open directory %s since %s", REPO_ID(pRepo), dataDir,
              tstrerror(terrno));
    taosArrayDestroy(fArray);
    regfree(&regex);
H
Hongze Cheng 已提交
824 825 826
    return -1;
  }

H
Hongze Cheng 已提交
827
  while ((pf = tfsReaddir(tdir))) {
S
Shengliang Guan 已提交
828
    tfsBasename(pf, bname);
H
Hongze Cheng 已提交
829 830 831

    int code = regexec(&regex, bname, 0, NULL, 0);
    if (code == 0) {
832
      if (taosArrayPush(fArray, (void *)pf) == NULL) {
H
Hongze Cheng 已提交
833 834 835 836 837 838 839 840
        terrno = TSDB_CODE_TDB_OUT_OF_MEMORY;
        tfsClosedir(tdir);
        taosArrayDestroy(fArray);
        regfree(&regex);
        return -1;
      }
    } else if (code == REG_NOMATCH) {
      // Not match
S
Shengliang Guan 已提交
841 842
      tsdbInfo("vgId:%d invalid file %s exists, remove it", REPO_ID(pRepo), pf->aname);
      (void)tfsRemoveFile(pf);
H
Hongze Cheng 已提交
843 844 845 846 847 848 849 850 851 852 853
      continue;
    } else {
      // Has other error
      tsdbError("vgId:%d failed to restore DFileSet Array while run regexec since %s", REPO_ID(pRepo), strerror(code));
      terrno = TAOS_SYSTEM_ERROR(code);
      tfsClosedir(tdir);
      taosArrayDestroy(fArray);
      regfree(&regex);
      return -1;
    }
  }
H
Hongze Cheng 已提交
854 855

  tfsClosedir(tdir);
H
Hongze Cheng 已提交
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
  regfree(&regex);

  // Sort the array according to file name
  taosArraySort(fArray, tsdbComparTFILE);

  size_t index = 0;
  // Loop to recover each file set
  for (;;) {
    if (index >= taosArrayGetSize(fArray)) {
      break;
    }

    SDFileSet fset = {0};

    TSDB_FSET_SET_CLOSED(&fset);

    // Loop to recover ONE fset
    for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
      SDFile *pDFile = TSDB_DFILE_IN_SET(&fset, ftype);

      if (index >= taosArrayGetSize(fArray)) {
        tsdbError("vgId:%d incomplete DFileSet, fid:%d", REPO_ID(pRepo), fset.fid);
        taosArrayDestroy(fArray);
        return -1;
      }

      pf = taosArrayGet(fArray, index);

      int         tvid, tfid;
      TSDB_FILE_T ttype;
      uint32_t    tversion;
887
      char        _bname[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
888

S
Shengliang Guan 已提交
889
      tfsBasename(pf, _bname);
890
      tsdbParseDFilename(_bname, &tvid, &tfid, &ttype, &tversion);
H
Hongze Cheng 已提交
891 892 893

      ASSERT(tvid == REPO_ID(pRepo));

894 895 896 897 898
      if (tfid < pRepo->rtn.minFid) {  // skip file expired
        ++index;
        continue;
      }

H
Hongze Cheng 已提交
899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
      if (ftype == 0) {
        fset.fid = tfid;
      } else {
        if (tfid != fset.fid) {
          tsdbError("vgId:%d incomplete dFileSet, fid:%d", REPO_ID(pRepo), fset.fid);
          taosArrayDestroy(fArray);
          return -1;
        }
      }

      if (ttype != ftype) {
        tsdbError("vgId:%d incomplete dFileSet, fid:%d", REPO_ID(pRepo), fset.fid);
        taosArrayDestroy(fArray);
        return -1;
      }

      pDFile->f = *pf;
H
Hongze Cheng 已提交
916

917 918
      // if (tsdbOpenDFile(pDFile, O_RDONLY) < 0) {
      if (tsdbOpenDFile(pDFile, TD_FILE_READ) < 0) {
H
Hongze Cheng 已提交
919 920
        tsdbError("vgId:%d failed to open DFile %s since %s", REPO_ID(pRepo), TSDB_FILE_FULL_NAME(pDFile),
                  tstrerror(terrno));
H
Hongze Cheng 已提交
921 922 923 924 925 926 927 928 929 930 931
        taosArrayDestroy(fArray);
        return -1;
      }

      if (tsdbLoadDFileHeader(pDFile, &(pDFile->info)) < 0) {
        tsdbError("vgId:%d failed to load DFile %s header since %s", REPO_ID(pRepo), TSDB_FILE_FULL_NAME(pDFile),
                  tstrerror(terrno));
        taosArrayDestroy(fArray);
        return -1;
      }

932
      if (tsdbForceKeepFile) {
933
        int64_t file_size;
934
        // Get real file size
935
        if (taosFStatFile(pDFile->pFile, &file_size, NULL) < 0) {
936 937 938 939 940
          terrno = TAOS_SYSTEM_ERROR(errno);
          taosArrayDestroy(fArray);
          return -1;
        }

941
        if (pDFile->info.size != file_size) {
942
          int64_t tfsize = pDFile->info.size;
943
          pDFile->info.size = file_size;
944 945 946 947 948
          tsdbInfo("vgId:%d file %s header size is changed from %" PRId64 " to %" PRId64, REPO_ID(pRepo),
                   TSDB_FILE_FULL_NAME(pDFile), tfsize, pDFile->info.size);
        }
      }

H
Hongze Cheng 已提交
949
      tsdbCloseDFile(pDFile);
H
Hongze Cheng 已提交
950
      index++;
H
Hongze Cheng 已提交
951 952
    }

H
Hongze Cheng 已提交
953
    tsdbInfo("vgId:%d FSET %d is restored", REPO_ID(pRepo), fset.fid);
H
Hongze Cheng 已提交
954 955 956 957 958
    taosArrayPush(pfs->cstatus->df, &fset);
  }

  // Resource release
  taosArrayDestroy(fArray);
H
Hongze Cheng 已提交
959 960

  return 0;
H
Hongze Cheng 已提交
961 962
}

H
Hongze Cheng 已提交
963
static int tsdbRestoreCurrent(STsdb *pRepo) {
H
Hongze Cheng 已提交
964 965 966 967 968
  // // Loop to recover mfile
  // if (tsdbRestoreMeta(pRepo) < 0) {
  //   tsdbError("vgId:%d failed to restore current since %s", REPO_ID(pRepo), tstrerror(terrno));
  //   return -1;
  // }
H
Hongze Cheng 已提交
969 970 971 972 973 974 975

  // Loop to recover dfile set
  if (tsdbRestoreDFileSet(pRepo) < 0) {
    tsdbError("vgId:%d failed to restore DFileSet since %s", REPO_ID(pRepo), tstrerror(terrno));
    return -1;
  }

S
Shengliang Guan 已提交
976
  if (tsdbSaveFSStatus(pRepo, pRepo->fs->cstatus) < 0) {
S
Shengliang Guan 已提交
977
    tsdbError("vgId:%d failed to restore current since %s", REPO_ID(pRepo), tstrerror(terrno));
H
Hongze Cheng 已提交
978 979 980 981 982 983 984
    return -1;
  }

  return 0;
}

static int tsdbComparTFILE(const void *arg1, const void *arg2) {
S
Shengliang Guan 已提交
985 986
  STfsFile *pf1 = (STfsFile *)arg1;
  STfsFile *pf2 = (STfsFile *)arg2;
H
Hongze Cheng 已提交
987 988 989 990

  int         vid1, fid1, vid2, fid2;
  TSDB_FILE_T ftype1, ftype2;
  uint32_t    version1, version2;
H
Hongze Cheng 已提交
991 992
  char        bname1[TSDB_FILENAME_LEN];
  char        bname2[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
993

S
Shengliang Guan 已提交
994 995
  tfsBasename(pf1, bname1);
  tfsBasename(pf2, bname2);
H
Hongze Cheng 已提交
996 997
  tsdbParseDFilename(bname1, &vid1, &fid1, &ftype1, &version1);
  tsdbParseDFilename(bname2, &vid2, &fid2, &ftype2, &version2);
H
Hongze Cheng 已提交
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011

  if (fid1 < fid2) {
    return -1;
  } else if (fid1 > fid2) {
    return 1;
  } else {
    if (ftype1 < ftype2) {
      return -1;
    } else if (ftype1 > ftype2) {
      return 1;
    } else {
      return 0;
    }
  }
H
Hongze Cheng 已提交
1012 1013
}

H
Hongze Cheng 已提交
1014
static void tsdbScanAndTryFixDFilesHeader(STsdb *pRepo, int32_t *nExpired) {
H
Hongze Cheng 已提交
1015
  STsdbFS   *pfs = REPO_FS(pRepo);
H
Hongze Cheng 已提交
1016 1017 1018 1019 1020 1021
  SFSStatus *pStatus = pfs->cstatus;
  SDFInfo    info;

  for (size_t i = 0; i < taosArrayGetSize(pStatus->df); i++) {
    SDFileSet fset;
    tsdbInitDFileSetEx(&fset, (SDFileSet *)taosArrayGet(pStatus->df, i));
1022 1023 1024
    if (fset.fid < pRepo->rtn.minFid) {
      ++*nExpired;
    }
H
Hongze Cheng 已提交
1025 1026
    tsdbDebug("vgId:%d scan DFileSet %d header", REPO_ID(pRepo), fset.fid);

1027 1028
    // if (tsdbOpenDFileSet(&fset, O_RDWR) < 0) {
    if (tsdbOpenDFileSet(&fset, TD_FILE_WRITE | TD_FILE_READ) < 0) {
H
Hongze Cheng 已提交
1029 1030 1031 1032 1033 1034 1035
      tsdbError("vgId:%d failed to open DFileSet %d since %s, continue", REPO_ID(pRepo), fset.fid, tstrerror(terrno));
      continue;
    }

    for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) {
      SDFile *pDFile = TSDB_DFILE_IN_SET(&fset, ftype);

H
Hongze Cheng 已提交
1036 1037
      if ((tsdbLoadDFileHeader(pDFile, &info) < 0) || pDFile->info.size != info.size ||
          pDFile->info.magic != info.magic) {
H
Hongze Cheng 已提交
1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
        if (tsdbUpdateDFileHeader(pDFile) < 0) {
          tsdbError("vgId:%d failed to update DFile header of %s since %s, continue", REPO_ID(pRepo),
                    TSDB_FILE_FULL_NAME(pDFile), tstrerror(terrno));
        } else {
          tsdbInfo("vgId:%d DFile header of %s is updated", REPO_ID(pRepo), TSDB_FILE_FULL_NAME(pDFile));
          TSDB_FILE_FSYNC(pDFile);
        }
      } else {
        tsdbDebug("vgId:%d DFile header of %s is correct", REPO_ID(pRepo), TSDB_FILE_FULL_NAME(pDFile));
      }
    }

    tsdbCloseDFileSet(&fset);
  }
L
Liu Jicong 已提交
1052
}