tsdbFS.c 31.7 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
// =================================================================================================
H
Hongze Cheng 已提交
19
static int32_t tsdbFSToBinary(uint8_t *p, STsdbFS *pFS) {
H
Hongze Cheng 已提交
20
  int32_t  n = 0;
H
Hongze Cheng 已提交
21 22
  int8_t   hasDel = pFS->pDelFile ? 1 : 0;
  uint32_t nSet = taosArrayGetSize(pFS->aDFileSet);
H
Hongze Cheng 已提交
23

H
Hongze Cheng 已提交
24 25 26
  // version
  n += tPutI8(p ? p + n : p, 0);

H
Hongze Cheng 已提交
27 28 29
  // SDelFile
  n += tPutI8(p ? p + n : p, hasDel);
  if (hasDel) {
H
Hongze Cheng 已提交
30
    n += tPutDelFile(p ? p + n : p, pFS->pDelFile);
H
Hongze Cheng 已提交
31 32
  }

H
Hongze Cheng 已提交
33
  // SArray<SDFileSet>
H
Hongze Cheng 已提交
34 35 36
  n += tPutU32v(p ? p + n : p, nSet);
  for (uint32_t iSet = 0; iSet < nSet; iSet++) {
    n += tPutDFileSet(p ? p + n : p, (SDFileSet *)taosArrayGet(pFS->aDFileSet, iSet));
H
Hongze Cheng 已提交
37 38
  }

H
Hongze Cheng 已提交
39
  return n;
H
Hongze Cheng 已提交
40 41
}

H
Hongze Cheng 已提交
42 43 44
static int32_t tsdbBinaryToFS(uint8_t *pData, int64_t nData, STsdbFS *pFS) {
  int32_t code = 0;
  int32_t n = 0;
H
Hongze Cheng 已提交
45

H
Hongze Cheng 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
  // version
  n += tGetI8(pData + n, NULL);

  // SDelFile
  int8_t hasDel = 0;
  n += tGetI8(pData + n, &hasDel);
  if (hasDel) {
    pFS->pDelFile = (SDelFile *)taosMemoryCalloc(1, sizeof(SDelFile));
    if (pFS->pDelFile == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _exit;
    }

    n += tGetDelFile(pData + n, pFS->pDelFile);
    pFS->pDelFile->nRef = 1;
  } else {
    pFS->pDelFile = NULL;
  }

  // aDFileSet
  taosArrayClear(pFS->aDFileSet);
  uint32_t nSet = 0;
  n += tGetU32v(pData + n, &nSet);
  for (uint32_t iSet = 0; iSet < nSet; iSet++) {
    SDFileSet fSet = {0};

    int32_t nt = tGetDFileSet(pData + n, &fSet);
    if (nt < 0) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _exit;
    }

    n += nt;
    if (taosArrayPush(pFS->aDFileSet, &fSet) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _exit;
    }
  }

85
  ASSERT(n + sizeof(TSCKSUM) == nData);
H
Hongze Cheng 已提交
86 87 88 89 90 91 92 93 94 95 96 97

_exit:
  return code;
}

static int32_t tsdbSaveFSToFile(STsdbFS *pFS, const char *fname) {
  int32_t code = 0;
  int32_t lino = 0;

  // encode to binary
  int32_t  size = tsdbFSToBinary(NULL, pFS) + sizeof(TSCKSUM);
  uint8_t *pData = taosMemoryMalloc(size);
H
Hongze Cheng 已提交
98 99
  if (pData == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
100
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
101
  }
H
Hongze Cheng 已提交
102
  tsdbFSToBinary(pData, pFS);
H
Hongze Cheng 已提交
103
  taosCalcChecksumAppend(0, pData, size);
H
Hongze Cheng 已提交
104

H
Hongze Cheng 已提交
105 106
  // save to file
  TdFilePtr pFD = taosOpenFile(fname, TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC);
H
Hongze Cheng 已提交
107 108
  if (pFD == NULL) {
    code = TAOS_SYSTEM_ERROR(errno);
H
Hongze Cheng 已提交
109
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
110 111
  }

H
Hongze Cheng 已提交
112
  int64_t n = taosWriteFile(pFD, pData, size);
H
Hongze Cheng 已提交
113 114
  if (n < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
H
Hongze Cheng 已提交
115 116
    taosCloseFile(&pFD);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
117 118 119 120
  }

  if (taosFsyncFile(pFD) < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
H
Hongze Cheng 已提交
121 122
    taosCloseFile(&pFD);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
123 124 125 126
  }

  taosCloseFile(&pFD);

H
Hongze Cheng 已提交
127
_exit:
H
Hongze Cheng 已提交
128
  if (pData) taosMemoryFree(pData);
H
Hongze Cheng 已提交
129 130 131
  if (code) {
    tsdbError("%s failed at line %d since %s, fname:%s", __func__, lino, tstrerror(code), fname);
  }
H
Hongze Cheng 已提交
132 133 134
  return code;
}

H
Hongze Cheng 已提交
135 136 137 138 139 140 141 142 143 144 145 146 147
int32_t tsdbFSCreate(STsdbFS *pFS) {
  int32_t code = 0;

  pFS->pDelFile = NULL;
  pFS->aDFileSet = taosArrayInit(0, sizeof(SDFileSet));
  if (pFS->aDFileSet == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

_exit:
  return code;
}
H
Hongze Cheng 已提交
148 149 150

void tsdbFSDestroy(STsdbFS *pFS) {
  if (pFS->pDelFile) {
151
    taosMemoryFree(pFS->pDelFile);
H
Hongze Cheng 已提交
152
    pFS->pDelFile = NULL;
H
Hongze Cheng 已提交
153
  }
H
Hongze Cheng 已提交
154

H
Hongze Cheng 已提交
155 156 157 158 159
  for (int32_t iSet = 0; iSet < taosArrayGetSize(pFS->aDFileSet); iSet++) {
    SDFileSet *pSet = (SDFileSet *)taosArrayGet(pFS->aDFileSet, iSet);
    taosMemoryFree(pSet->pHeadF);
    taosMemoryFree(pSet->pDataF);
    taosMemoryFree(pSet->pSmaF);
H
Hongze Cheng 已提交
160 161
    for (int32_t iStt = 0; iStt < pSet->nSttF; iStt++) {
      taosMemoryFree(pSet->aSttF[iStt]);
H
Hongze Cheng 已提交
162
    }
H
Hongze Cheng 已提交
163
  }
H
Hongze Cheng 已提交
164

165
  taosArrayDestroy(pFS->aDFileSet);
H
Hongze Cheng 已提交
166
  pFS->aDFileSet = NULL;
H
Hongze Cheng 已提交
167 168 169 170
}

static int32_t tsdbScanAndTryFixFS(STsdb *pTsdb) {
  int32_t code = 0;
H
Hongze Cheng 已提交
171 172 173
  int32_t lino = 0;
  int64_t size = 0;
  char    fname[TSDB_FILENAME_LEN] = {0};
H
Hongze Cheng 已提交
174 175 176 177 178

  // SDelFile
  if (pTsdb->fs.pDelFile) {
    tsdbDelFileName(pTsdb, pTsdb->fs.pDelFile, fname);
    if (taosStatFile(fname, &size, NULL)) {
H
Hongze Cheng 已提交
179
      code = TAOS_SYSTEM_ERROR(errno);
H
Hongze Cheng 已提交
180
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
181 182
    }

183
    if (size != tsdbLogicToFileSize(pTsdb->fs.pDelFile->size, pTsdb->pVnode->config.tsdbPageSize)) {
H
Hongze Cheng 已提交
184
      code = TSDB_CODE_FILE_CORRUPTED;
H
Hongze Cheng 已提交
185
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
186
    }
H
Hongze Cheng 已提交
187
  }
H
Hongze Cheng 已提交
188

H
Hongze Cheng 已提交
189
  // SArray<SDFileSet>
H
Hongze Cheng 已提交
190
  int32_t fid = 0;
H
Hongze Cheng 已提交
191 192
  for (int32_t iSet = 0; iSet < taosArrayGetSize(pTsdb->fs.aDFileSet); iSet++) {
    SDFileSet *pSet = (SDFileSet *)taosArrayGet(pTsdb->fs.aDFileSet, iSet);
H
Hongze Cheng 已提交
193
    fid = pSet->fid;
H
Hongze Cheng 已提交
194 195 196 197 198

    // head =========
    tsdbHeadFileName(pTsdb, pSet->diskId, pSet->fid, pSet->pHeadF, fname);
    if (taosStatFile(fname, &size, NULL)) {
      code = TAOS_SYSTEM_ERROR(errno);
H
Hongze Cheng 已提交
199
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
200
    }
H
Hongze Cheng 已提交
201
    if (size != tsdbLogicToFileSize(pSet->pHeadF->size, pTsdb->pVnode->config.tsdbPageSize)) {
H
Hongze Cheng 已提交
202
      code = TSDB_CODE_FILE_CORRUPTED;
H
Hongze Cheng 已提交
203
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
204 205
    }

H
Hongze Cheng 已提交
206 207 208
    // data =========
    tsdbDataFileName(pTsdb, pSet->diskId, pSet->fid, pSet->pDataF, fname);
    if (taosStatFile(fname, &size, NULL)) {
H
Hongze Cheng 已提交
209
      code = TAOS_SYSTEM_ERROR(errno);
H
Hongze Cheng 已提交
210
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
211
    }
H
Hongze Cheng 已提交
212
    if (size < tsdbLogicToFileSize(pSet->pDataF->size, pTsdb->pVnode->config.tsdbPageSize)) {
H
Hongze Cheng 已提交
213
      code = TSDB_CODE_FILE_CORRUPTED;
H
Hongze Cheng 已提交
214
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
215
    }
H
Hongze Cheng 已提交
216 217 218 219
    // else if (size > tsdbLogicToFileSize(pSet->pDataF->size, pTsdb->pVnode->config.tsdbPageSize)) {
    //   code = tsdbDFileRollback(pTsdb, pSet, TSDB_DATA_FILE);
    //   TSDB_CHECK_CODE(code, lino, _exit);
    // }
H
Hongze Cheng 已提交
220

H
Hongze Cheng 已提交
221 222 223 224
    // sma =============
    tsdbSmaFileName(pTsdb, pSet->diskId, pSet->fid, pSet->pSmaF, fname);
    if (taosStatFile(fname, &size, NULL)) {
      code = TAOS_SYSTEM_ERROR(errno);
H
Hongze Cheng 已提交
225
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
226
    }
H
Hongze Cheng 已提交
227
    if (size < tsdbLogicToFileSize(pSet->pSmaF->size, pTsdb->pVnode->config.tsdbPageSize)) {
H
Hongze Cheng 已提交
228
      code = TSDB_CODE_FILE_CORRUPTED;
H
Hongze Cheng 已提交
229
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
230
    }
H
Hongze Cheng 已提交
231 232 233 234
    // else if (size > tsdbLogicToFileSize(pSet->pSmaF->size, pTsdb->pVnode->config.tsdbPageSize)) {
    //   code = tsdbDFileRollback(pTsdb, pSet, TSDB_SMA_FILE);
    //   TSDB_CHECK_CODE(code, lino, _exit);
    // }
H
Hongze Cheng 已提交
235

H
Hongze Cheng 已提交
236 237 238
    // stt ===========
    for (int32_t iStt = 0; iStt < pSet->nSttF; iStt++) {
      tsdbSttFileName(pTsdb, pSet->diskId, pSet->fid, pSet->aSttF[iStt], fname);
H
Hongze Cheng 已提交
239 240
      if (taosStatFile(fname, &size, NULL)) {
        code = TAOS_SYSTEM_ERROR(errno);
H
Hongze Cheng 已提交
241
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
242
      }
H
Hongze Cheng 已提交
243
      if (size != tsdbLogicToFileSize(pSet->aSttF[iStt]->size, pTsdb->pVnode->config.tsdbPageSize)) {
H
Hongze Cheng 已提交
244
        code = TSDB_CODE_FILE_CORRUPTED;
H
Hongze Cheng 已提交
245
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
246 247
      }
    }
H
Hongze Cheng 已提交
248
  }
H
Hongze Cheng 已提交
249

H
Hongze Cheng 已提交
250 251
  {
    // remove those invalid files (todo)
H
Hongze Cheng 已提交
252 253
  }

H
Hongze Cheng 已提交
254 255
_exit:
  if (code) {
S
Shengliang Guan 已提交
256
    tsdbError("vgId:%d, %s failed at line %d since %s, fid:%d", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code),
H
Hongze Cheng 已提交
257 258
              fid);
  }
H
Hongze Cheng 已提交
259 260 261
  return code;
}

H
Hongze Cheng 已提交
262 263 264 265 266 267 268 269 270
int32_t tDFileSetCmprFn(const void *p1, const void *p2) {
  if (((SDFileSet *)p1)->fid < ((SDFileSet *)p2)->fid) {
    return -1;
  } else if (((SDFileSet *)p1)->fid > ((SDFileSet *)p2)->fid) {
    return 1;
  }

  return 0;
}
H
Hongze Cheng 已提交
271

H
Hongze Cheng 已提交
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
static void tsdbGetCurrentFName(STsdb *pTsdb, char *current, char *current_t) {
  SVnode *pVnode = pTsdb->pVnode;
  if (pVnode->pTfs) {
    if (current) {
      snprintf(current, TSDB_FILENAME_LEN - 1, "%s%s%s%sCURRENT", tfsGetPrimaryPath(pTsdb->pVnode->pTfs), TD_DIRSEP,
               pTsdb->path, TD_DIRSEP);
    }
    if (current_t) {
      snprintf(current_t, TSDB_FILENAME_LEN - 1, "%s%s%s%sCURRENT.t", tfsGetPrimaryPath(pTsdb->pVnode->pTfs), TD_DIRSEP,
               pTsdb->path, TD_DIRSEP);
    }
  } else {
    if (current) {
      snprintf(current, TSDB_FILENAME_LEN - 1, "%s%sCURRENT", pTsdb->path, TD_DIRSEP);
    }
    if (current_t) {
      snprintf(current_t, TSDB_FILENAME_LEN - 1, "%s%sCURRENT.t", pTsdb->path, TD_DIRSEP);
    }
  }
}

H
Hongze Cheng 已提交
293 294 295 296 297 298 299 300
static int32_t tsdbLoadFSFromFile(const char *fname, STsdbFS *pFS) {
  int32_t  code = 0;
  int32_t  lino = 0;
  uint8_t *pData = NULL;

  // load binary
  TdFilePtr pFD = taosOpenFile(fname, TD_FILE_READ);
  if (pFD == NULL) {
H
Hongze Cheng 已提交
301
    code = TAOS_SYSTEM_ERROR(errno);
H
Hongze Cheng 已提交
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  int64_t size;
  if (taosFStatFile(pFD, &size, NULL) < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    taosCloseFile(&pFD);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  pData = taosMemoryMalloc(size);
  if (pData == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    taosCloseFile(&pFD);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  if (taosReadFile(pFD, pData, size) < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    taosCloseFile(&pFD);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  if (!taosCheckChecksumWhole(pData, size)) {
    code = TSDB_CODE_FILE_CORRUPTED;
    taosCloseFile(&pFD);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  taosCloseFile(&pFD);

  // decode binary
  code = tsdbBinaryToFS(pData, size, pFS);
  TSDB_CHECK_CODE(code, lino, _exit);

_exit:
  if (pData) taosMemoryFree(pData);
  if (code) {
    tsdbError("%s failed at line %d since %s, fname:%s", __func__, lino, tstrerror(code), fname);
  }
  return code;
}

static int32_t tsdbRemoveFileSet(STsdb *pTsdb, SDFileSet *pSet) {
  int32_t code = 0;
  char    fname[TSDB_FILENAME_LEN] = {0};

  int32_t nRef = atomic_sub_fetch_32(&pSet->pHeadF->nRef, 1);
  if (nRef == 0) {
    tsdbHeadFileName(pTsdb, pSet->diskId, pSet->fid, pSet->pHeadF, fname);
    (void)taosRemoveFile(fname);
    taosMemoryFree(pSet->pHeadF);
  }

  nRef = atomic_sub_fetch_32(&pSet->pDataF->nRef, 1);
  if (nRef == 0) {
    tsdbDataFileName(pTsdb, pSet->diskId, pSet->fid, pSet->pDataF, fname);
H
Hongze Cheng 已提交
359
    (void)taosRemoveFile(fname);
H
Hongze Cheng 已提交
360 361 362 363 364 365
    taosMemoryFree(pSet->pDataF);
  }

  nRef = atomic_sub_fetch_32(&pSet->pSmaF->nRef, 1);
  if (nRef == 0) {
    tsdbSmaFileName(pTsdb, pSet->diskId, pSet->fid, pSet->pSmaF, fname);
H
Hongze Cheng 已提交
366
    (void)taosRemoveFile(fname);
H
Hongze Cheng 已提交
367 368 369 370 371 372 373
    taosMemoryFree(pSet->pSmaF);
  }

  for (int8_t iStt = 0; iStt < pSet->nSttF; iStt++) {
    nRef = atomic_sub_fetch_32(&pSet->aSttF[iStt]->nRef, 1);
    if (nRef == 0) {
      tsdbSttFileName(pTsdb, pSet->diskId, pSet->fid, pSet->aSttF[iStt], fname);
H
Hongze Cheng 已提交
374
      (void)taosRemoveFile(fname);
H
Hongze Cheng 已提交
375 376 377 378 379 380 381 382 383
      taosMemoryFree(pSet->aSttF[iStt]);
    }
  }

_exit:
  return code;
}

static int32_t tsdbNewFileSet(STsdb *pTsdb, SDFileSet *pSetTo, SDFileSet *pSetFrom) {
H
Hongze Cheng 已提交
384
  int32_t code = 0;
H
Hongze Cheng 已提交
385
  int32_t lino = 0;
H
Hongze Cheng 已提交
386

H
Hongze Cheng 已提交
387
  *pSetTo = (SDFileSet){.diskId = pSetFrom->diskId, .fid = pSetFrom->fid, .nSttF = 0};
H
Hongze Cheng 已提交
388

H
Hongze Cheng 已提交
389 390 391 392 393 394 395 396
  // head
  pSetTo->pHeadF = (SHeadFile *)taosMemoryMalloc(sizeof(SHeadFile));
  if (pSetTo->pHeadF == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    TSDB_CHECK_CODE(code, lino, _exit);
  }
  *pSetTo->pHeadF = *pSetFrom->pHeadF;
  pSetTo->pHeadF->nRef = 1;
H
Hongze Cheng 已提交
397

H
Hongze Cheng 已提交
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
  // data
  pSetTo->pDataF = (SDataFile *)taosMemoryMalloc(sizeof(SDataFile));
  if (pSetTo->pDataF == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    TSDB_CHECK_CODE(code, lino, _exit);
  }
  *pSetTo->pDataF = *pSetFrom->pDataF;
  pSetTo->pDataF->nRef = 1;

  // sma
  pSetTo->pSmaF = (SSmaFile *)taosMemoryMalloc(sizeof(SSmaFile));
  if (pSetTo->pSmaF == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    TSDB_CHECK_CODE(code, lino, _exit);
  }
  *pSetTo->pSmaF = *pSetFrom->pSmaF;
  pSetTo->pSmaF->nRef = 1;

  // stt
  for (int32_t iStt = 0; iStt < pSetFrom->nSttF; iStt++) {
    pSetTo->aSttF[iStt] = (SSttFile *)taosMemoryMalloc(sizeof(SSttFile));
    if (pSetTo->aSttF[iStt] == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
421
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
422
    }
H
Hongze Cheng 已提交
423

H
Hongze Cheng 已提交
424 425 426 427 428 429 430
    pSetTo->nSttF++;
    *pSetTo->aSttF[iStt] = *pSetFrom->aSttF[iStt];
    pSetTo->aSttF[iStt]->nRef = 1;
  }

_exit:
  if (code) {
S
Shengliang Guan 已提交
431
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
  }
  return code;
}

static int32_t tsdbMergeFileSet(STsdb *pTsdb, SDFileSet *pSetOld, SDFileSet *pSetNew) {
  int32_t code = 0;
  int32_t lino = 0;
  int32_t nRef = 0;
  bool    sameDisk = ((pSetOld->diskId.level == pSetNew->diskId.level) && (pSetOld->diskId.id == pSetNew->diskId.id));
  char    fname[TSDB_FILENAME_LEN] = {0};

  // head
  SHeadFile *pHeadF = pSetOld->pHeadF;
  if ((!sameDisk) || (pHeadF->commitID != pSetNew->pHeadF->commitID)) {
    pSetOld->pHeadF = (SHeadFile *)taosMemoryMalloc(sizeof(SHeadFile));
    if (pSetOld->pHeadF == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
449
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
450
    }
H
Hongze Cheng 已提交
451 452 453 454 455 456 457 458 459 460 461 462 463 464
    *pSetOld->pHeadF = *pSetNew->pHeadF;
    pSetOld->pHeadF->nRef = 1;

    nRef = atomic_sub_fetch_32(&pHeadF->nRef, 1);
    if (nRef == 0) {
      tsdbHeadFileName(pTsdb, pSetOld->diskId, pSetOld->fid, pHeadF, fname);
      (void)taosRemoveFile(fname);
      taosMemoryFree(pHeadF);
    }
  } else {
    nRef = pHeadF->nRef;
    *pHeadF = *pSetNew->pHeadF;
    pHeadF->nRef = nRef;
  }
H
Hongze Cheng 已提交
465

H
Hongze Cheng 已提交
466 467 468 469 470
  // data
  SDataFile *pDataF = pSetOld->pDataF;
  if ((!sameDisk) || (pDataF->commitID != pSetNew->pDataF->commitID)) {
    pSetOld->pDataF = (SDataFile *)taosMemoryMalloc(sizeof(SDataFile));
    if (pSetOld->pDataF == NULL) {
H
Hongze Cheng 已提交
471
      code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
472
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
473
    }
H
Hongze Cheng 已提交
474 475 476 477 478 479 480 481 482 483 484 485 486 487
    *pSetOld->pDataF = *pSetNew->pDataF;
    pSetOld->pDataF->nRef = 1;

    nRef = atomic_sub_fetch_32(&pDataF->nRef, 1);
    if (nRef == 0) {
      tsdbDataFileName(pTsdb, pSetOld->diskId, pSetOld->fid, pDataF, fname);
      (void)taosRemoveFile(fname);
      taosMemoryFree(pDataF);
    }
  } else {
    nRef = pDataF->nRef;
    *pDataF = *pSetNew->pDataF;
    pDataF->nRef = nRef;
  }
H
Hongze Cheng 已提交
488

H
Hongze Cheng 已提交
489 490 491 492 493 494
  // sma
  SSmaFile *pSmaF = pSetOld->pSmaF;
  if ((!sameDisk) || (pSmaF->commitID != pSetNew->pSmaF->commitID)) {
    pSetOld->pSmaF = (SSmaFile *)taosMemoryMalloc(sizeof(SSmaFile));
    if (pSetOld->pSmaF == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
495
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
496
    }
H
Hongze Cheng 已提交
497 498 499 500 501 502 503 504 505 506 507 508 509 510
    *pSetOld->pSmaF = *pSetNew->pSmaF;
    pSetOld->pSmaF->nRef = 1;

    nRef = atomic_sub_fetch_32(&pSmaF->nRef, 1);
    if (nRef == 0) {
      tsdbSmaFileName(pTsdb, pSetOld->diskId, pSetOld->fid, pSmaF, fname);
      (void)taosRemoveFile(fname);
      taosMemoryFree(pSmaF);
    }
  } else {
    nRef = pSmaF->nRef;
    *pSmaF = *pSetNew->pSmaF;
    pSmaF->nRef = nRef;
  }
H
Hongze Cheng 已提交
511

H
Hongze Cheng 已提交
512 513 514
  // stt
  if (sameDisk) {
    if (pSetNew->nSttF > pSetOld->nSttF) {
515
      ASSERT(pSetNew->nSttF == pSetOld->nSttF + 1);
H
Hongze Cheng 已提交
516 517 518 519 520 521 522 523 524
      pSetOld->aSttF[pSetOld->nSttF] = (SSttFile *)taosMemoryMalloc(sizeof(SSttFile));
      if (pSetOld->aSttF[pSetOld->nSttF] == NULL) {
        code = TSDB_CODE_OUT_OF_MEMORY;
        TSDB_CHECK_CODE(code, lino, _exit);
      }
      *pSetOld->aSttF[pSetOld->nSttF] = *pSetNew->aSttF[pSetOld->nSttF];
      pSetOld->aSttF[pSetOld->nSttF]->nRef = 1;
      pSetOld->nSttF++;
    } else if (pSetNew->nSttF < pSetOld->nSttF) {
525
      ASSERT(pSetNew->nSttF == 1);
H
Hongze Cheng 已提交
526 527 528 529 530
      for (int32_t iStt = 0; iStt < pSetOld->nSttF; iStt++) {
        SSttFile *pSttFile = pSetOld->aSttF[iStt];
        nRef = atomic_sub_fetch_32(&pSttFile->nRef, 1);
        if (nRef == 0) {
          tsdbSttFileName(pTsdb, pSetOld->diskId, pSetOld->fid, pSttFile, fname);
H
Hongze Cheng 已提交
531
          (void)taosRemoveFile(fname);
H
Hongze Cheng 已提交
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
          taosMemoryFree(pSttFile);
        }
        pSetOld->aSttF[iStt] = NULL;
      }

      pSetOld->nSttF = 1;
      pSetOld->aSttF[0] = (SSttFile *)taosMemoryMalloc(sizeof(SSttFile));
      if (pSetOld->aSttF[0] == NULL) {
        code = TSDB_CODE_OUT_OF_MEMORY;
        TSDB_CHECK_CODE(code, lino, _exit);
      }
      *pSetOld->aSttF[0] = *pSetNew->aSttF[0];
      pSetOld->aSttF[0]->nRef = 1;
    } else {
      for (int32_t iStt = 0; iStt < pSetOld->nSttF; iStt++) {
        if (pSetOld->aSttF[iStt]->commitID != pSetNew->aSttF[iStt]->commitID) {
          SSttFile *pSttFile = pSetOld->aSttF[iStt];
          nRef = atomic_sub_fetch_32(&pSttFile->nRef, 1);
          if (nRef == 0) {
            tsdbSttFileName(pTsdb, pSetOld->diskId, pSetOld->fid, pSttFile, fname);
H
Hongze Cheng 已提交
552
            (void)taosRemoveFile(fname);
H
Hongze Cheng 已提交
553 554 555 556 557 558 559 560 561 562 563
            taosMemoryFree(pSttFile);
          }

          pSetOld->aSttF[iStt] = (SSttFile *)taosMemoryMalloc(sizeof(SSttFile));
          if (pSetOld->aSttF[iStt] == NULL) {
            code = TSDB_CODE_OUT_OF_MEMORY;
            TSDB_CHECK_CODE(code, lino, _exit);
          }
          *pSetOld->aSttF[iStt] = *pSetNew->aSttF[iStt];
          pSetOld->aSttF[iStt]->nRef = 1;
        } else {
564 565
          ASSERT(pSetOld->aSttF[iStt]->size == pSetOld->aSttF[iStt]->size);
          ASSERT(pSetOld->aSttF[iStt]->offset == pSetOld->aSttF[iStt]->offset);
H
Hongze Cheng 已提交
566 567 568 569 570 571 572 573 574 575 576 577
        }
      }
    }
  } else {
    for (int32_t iStt = 0; iStt < pSetOld->nSttF; iStt++) {
      SSttFile *pSttFile = pSetOld->aSttF[iStt];
      nRef = atomic_sub_fetch_32(&pSttFile->nRef, 1);
      if (nRef == 0) {
        tsdbSttFileName(pTsdb, pSetOld->diskId, pSetOld->fid, pSttFile, fname);
        (void)taosRemoveFile(fname);
        taosMemoryFree(pSttFile);
      }
H
Hongze Cheng 已提交
578 579
    }

H
Hongze Cheng 已提交
580 581 582 583 584 585 586 587 588 589
    pSetOld->nSttF = 0;
    for (int32_t iStt = 0; iStt < pSetNew->nSttF; iStt++) {
      pSetOld->aSttF[iStt] = (SSttFile *)taosMemoryMalloc(sizeof(SSttFile));
      if (pSetOld->aSttF[iStt] == NULL) {
        code = TSDB_CODE_OUT_OF_MEMORY;
        TSDB_CHECK_CODE(code, lino, _exit);
      }

      *pSetOld->aSttF[iStt] = *pSetNew->aSttF[iStt];
      pSetOld->aSttF[iStt]->nRef = 1;
H
Hongze Cheng 已提交
590

H
Hongze Cheng 已提交
591 592 593 594 595 596 597 598 599 600
      pSetOld->nSttF++;
    }
  }

  if (!sameDisk) {
    pSetOld->diskId = pSetNew->diskId;
  }

_exit:
  if (code) {
S
Shengliang Guan 已提交
601
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
  }
  return code;
}

static int32_t tsdbFSApplyChange(STsdb *pTsdb, STsdbFS *pFS) {
  int32_t code = 0;
  int32_t lino = 0;

  int32_t nRef = 0;
  char    fname[TSDB_FILENAME_LEN] = {0};

  // SDelFile
  if (pFS->pDelFile) {
    SDelFile *pDelFile = pTsdb->fs.pDelFile;

    if (pDelFile == NULL || (pDelFile->commitID != pFS->pDelFile->commitID)) {
      pTsdb->fs.pDelFile = (SDelFile *)taosMemoryMalloc(sizeof(SDelFile));
      if (pTsdb->fs.pDelFile == NULL) {
        code = TSDB_CODE_OUT_OF_MEMORY;
        TSDB_CHECK_CODE(code, lino, _exit);
      }

      *pTsdb->fs.pDelFile = *pFS->pDelFile;
      pTsdb->fs.pDelFile->nRef = 1;

      if (pDelFile) {
        nRef = atomic_sub_fetch_32(&pDelFile->nRef, 1);
        if (nRef == 0) {
          tsdbDelFileName(pTsdb, pDelFile, fname);
          (void)taosRemoveFile(fname);
          taosMemoryFree(pDelFile);
        }
      }
    }
  } else {
637
    ASSERT(pTsdb->fs.pDelFile == NULL);
H
Hongze Cheng 已提交
638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678
  }

  // aDFileSet
  int32_t iOld = 0;
  int32_t iNew = 0;
  while (true) {
    int32_t   nOld = taosArrayGetSize(pTsdb->fs.aDFileSet);
    int32_t   nNew = taosArrayGetSize(pFS->aDFileSet);
    SDFileSet fSet = {0};
    int8_t    sameDisk = 0;

    if (iOld >= nOld && iNew >= nNew) break;

    SDFileSet *pSetOld = (iOld < nOld) ? taosArrayGet(pTsdb->fs.aDFileSet, iOld) : NULL;
    SDFileSet *pSetNew = (iNew < nNew) ? taosArrayGet(pFS->aDFileSet, iNew) : NULL;

    if (pSetOld && pSetNew) {
      if (pSetOld->fid == pSetNew->fid) {
        code = tsdbMergeFileSet(pTsdb, pSetOld, pSetNew);
        TSDB_CHECK_CODE(code, lino, _exit);

        iOld++;
        iNew++;
      } else if (pSetOld->fid < pSetNew->fid) {
        code = tsdbRemoveFileSet(pTsdb, pSetOld);
        TSDB_CHECK_CODE(code, lino, _exit);
        taosArrayRemove(pTsdb->fs.aDFileSet, iOld);
      } else {
        code = tsdbNewFileSet(pTsdb, &fSet, pSetNew);
        TSDB_CHECK_CODE(code, lino, _exit)

        if (taosArrayInsert(pTsdb->fs.aDFileSet, iOld, &fSet) == NULL) {
          code = TSDB_CODE_OUT_OF_MEMORY;
          TSDB_CHECK_CODE(code, lino, _exit);
        }

        iOld++;
        iNew++;
      }
    } else if (pSetOld) {
      code = tsdbRemoveFileSet(pTsdb, pSetOld);
H
Hongze Cheng 已提交
679
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
680 681 682 683 684 685 686 687 688 689 690 691
      taosArrayRemove(pTsdb->fs.aDFileSet, iOld);
    } else {
      code = tsdbNewFileSet(pTsdb, &fSet, pSetNew);
      TSDB_CHECK_CODE(code, lino, _exit)

      if (taosArrayInsert(pTsdb->fs.aDFileSet, iOld, &fSet) == NULL) {
        code = TSDB_CODE_OUT_OF_MEMORY;
        TSDB_CHECK_CODE(code, lino, _exit);
      }

      iOld++;
      iNew++;
H
Hongze Cheng 已提交
692
    }
H
Hongze Cheng 已提交
693 694 695 696
  }

_exit:
  if (code) {
S
Shengliang Guan 已提交
697
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
698 699 700 701
  }
  return code;
}

H
Hongze Cheng 已提交
702 703
// EXPOSED APIS ====================================================================================
int32_t tsdbFSCommit(STsdb *pTsdb) {
H
Hongze Cheng 已提交
704 705
  int32_t code = 0;
  int32_t lino = 0;
H
Hongze Cheng 已提交
706
  STsdbFS fs = {0};
H
Hongze Cheng 已提交
707 708 709 710 711

  char current[TSDB_FILENAME_LEN] = {0};
  char current_t[TSDB_FILENAME_LEN] = {0};
  tsdbGetCurrentFName(pTsdb, current, current_t);

H
Hongze Cheng 已提交
712 713
  if (!taosCheckExistFile(current_t)) goto _exit;

H
Hongze Cheng 已提交
714 715 716 717 718 719 720 721 722 723
  // rename the file
  if (taosRenameFile(current_t, current) < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  // Load the new FS
  code = tsdbFSCreate(&fs);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
724
  code = tsdbLoadFSFromFile(current, &fs);
H
Hongze Cheng 已提交
725
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
726

H
Hongze Cheng 已提交
727 728 729 730 731 732 733
  // apply file change
  code = tsdbFSApplyChange(pTsdb, &fs);
  TSDB_CHECK_CODE(code, lino, _exit);

_exit:
  tsdbFSDestroy(&fs);
  if (code) {
S
Shengliang Guan 已提交
734
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
735 736 737 738
  }
  return code;
}

H
Hongze Cheng 已提交
739
int32_t tsdbFSRollback(STsdb *pTsdb) {
H
Hongze Cheng 已提交
740 741 742 743 744
  int32_t code = 0;
  int32_t lino = 0;

  char current_t[TSDB_FILENAME_LEN] = {0};
  tsdbGetCurrentFName(pTsdb, NULL, current_t);
H
Hongze Cheng 已提交
745
  (void)taosRemoveFile(current_t);
H
Hongze Cheng 已提交
746 747 748

_exit:
  if (code) {
S
Shengliang Guan 已提交
749
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(errno));
H
Hongze Cheng 已提交
750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
  }
  return code;
}

int32_t tsdbFSOpen(STsdb *pTsdb, int8_t rollback) {
  int32_t code = 0;
  int32_t lino = 0;
  SVnode *pVnode = pTsdb->pVnode;

  // open handle
  code = tsdbFSCreate(&pTsdb->fs);
  TSDB_CHECK_CODE(code, lino, _exit);

  // open impl
  char current[TSDB_FILENAME_LEN] = {0};
  char current_t[TSDB_FILENAME_LEN] = {0};
  tsdbGetCurrentFName(pTsdb, current, current_t);

  if (taosCheckExistFile(current)) {
    code = tsdbLoadFSFromFile(current, &pTsdb->fs);
    TSDB_CHECK_CODE(code, lino, _exit);

    if (taosCheckExistFile(current_t)) {
      if (rollback) {
        code = tsdbFSRollback(pTsdb);
        TSDB_CHECK_CODE(code, lino, _exit);
      } else {
        code = tsdbFSCommit(pTsdb);
        TSDB_CHECK_CODE(code, lino, _exit);
      }
    }
H
Hongze Cheng 已提交
781 782
  } else {
    // empty one
H
Hongze Cheng 已提交
783
    code = tsdbSaveFSToFile(&pTsdb->fs, current);
H
Hongze Cheng 已提交
784
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
785

786
    ASSERT(!rollback);
H
Hongze Cheng 已提交
787 788
  }

H
Hongze Cheng 已提交
789 790
  // scan and fix FS
  code = tsdbScanAndTryFixFS(pTsdb);
H
Hongze Cheng 已提交
791
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
792

H
Hongze Cheng 已提交
793 794
_exit:
  if (code) {
S
Shengliang Guan 已提交
795
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
796
  }
H
Hongze Cheng 已提交
797 798 799
  return code;
}

H
Hongze Cheng 已提交
800 801
int32_t tsdbFSClose(STsdb *pTsdb) {
  int32_t code = 0;
H
Hongze Cheng 已提交
802

H
Hongze Cheng 已提交
803
  if (pTsdb->fs.pDelFile) {
804
    ASSERT(pTsdb->fs.pDelFile->nRef == 1);
H
Hongze Cheng 已提交
805 806
    taosMemoryFree(pTsdb->fs.pDelFile);
  }
H
Hongze Cheng 已提交
807

H
Hongze Cheng 已提交
808 809
  for (int32_t iSet = 0; iSet < taosArrayGetSize(pTsdb->fs.aDFileSet); iSet++) {
    SDFileSet *pSet = (SDFileSet *)taosArrayGet(pTsdb->fs.aDFileSet, iSet);
H
Hongze Cheng 已提交
810

H
Hongze Cheng 已提交
811
    // head
812
    ASSERT(pSet->pHeadF->nRef == 1);
H
Hongze Cheng 已提交
813
    taosMemoryFree(pSet->pHeadF);
H
Hongze Cheng 已提交
814

H
Hongze Cheng 已提交
815
    // data
816
    ASSERT(pSet->pDataF->nRef == 1);
H
Hongze Cheng 已提交
817
    taosMemoryFree(pSet->pDataF);
H
Hongze Cheng 已提交
818

H
Hongze Cheng 已提交
819
    // sma
820
    ASSERT(pSet->pSmaF->nRef == 1);
H
Hongze Cheng 已提交
821
    taosMemoryFree(pSet->pSmaF);
H
Hongze Cheng 已提交
822

H
Hongze Cheng 已提交
823 824
    // stt
    for (int32_t iStt = 0; iStt < pSet->nSttF; iStt++) {
825
      ASSERT(pSet->aSttF[iStt]->nRef == 1);
H
Hongze Cheng 已提交
826
      taosMemoryFree(pSet->aSttF[iStt]);
H
Hongze Cheng 已提交
827
    }
H
Hongze Cheng 已提交
828 829
  }

H
Hongze Cheng 已提交
830
  taosArrayDestroy(pTsdb->fs.aDFileSet);
H
Hongze Cheng 已提交
831

H
Hongze Cheng 已提交
832 833 834 835 836
  return code;
}

int32_t tsdbFSCopy(STsdb *pTsdb, STsdbFS *pFS) {
  int32_t code = 0;
H
Hongze Cheng 已提交
837
  int32_t lino = 0;
H
Hongze Cheng 已提交
838 839

  pFS->pDelFile = NULL;
H
Hongze Cheng 已提交
840 841 842 843 844 845 846 847
  if (pFS->aDFileSet) {
    taosArrayClear(pFS->aDFileSet);
  } else {
    pFS->aDFileSet = taosArrayInit(taosArrayGetSize(pTsdb->fs.aDFileSet), sizeof(SDFileSet));
    if (pFS->aDFileSet == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      TSDB_CHECK_CODE(code, lino, _exit);
    }
H
Hongze Cheng 已提交
848 849
  }

H
Hongze Cheng 已提交
850 851 852 853
  if (pTsdb->fs.pDelFile) {
    pFS->pDelFile = (SDelFile *)taosMemoryMalloc(sizeof(SDelFile));
    if (pFS->pDelFile == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
854
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
855
    }
H
Hongze Cheng 已提交
856

H
Hongze Cheng 已提交
857
    *pFS->pDelFile = *pTsdb->fs.pDelFile;
H
Hongze Cheng 已提交
858
  }
H
Hongze Cheng 已提交
859 860 861

  for (int32_t iSet = 0; iSet < taosArrayGetSize(pTsdb->fs.aDFileSet); iSet++) {
    SDFileSet *pSet = (SDFileSet *)taosArrayGet(pTsdb->fs.aDFileSet, iSet);
H
Hongze Cheng 已提交
862
    SDFileSet  fSet = {.diskId = pSet->diskId, .fid = pSet->fid};
H
Hongze Cheng 已提交
863 864 865 866 867

    // head
    fSet.pHeadF = (SHeadFile *)taosMemoryMalloc(sizeof(SHeadFile));
    if (fSet.pHeadF == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
868
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
869
    }
H
Hongze Cheng 已提交
870
    *fSet.pHeadF = *pSet->pHeadF;
H
Hongze Cheng 已提交
871 872 873 874 875

    // data
    fSet.pDataF = (SDataFile *)taosMemoryMalloc(sizeof(SDataFile));
    if (fSet.pDataF == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
876
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
877
    }
H
Hongze Cheng 已提交
878
    *fSet.pDataF = *pSet->pDataF;
H
Hongze Cheng 已提交
879

H
Hongze Cheng 已提交
880
    // sma
H
Hongze Cheng 已提交
881 882 883
    fSet.pSmaF = (SSmaFile *)taosMemoryMalloc(sizeof(SSmaFile));
    if (fSet.pSmaF == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
884
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
885
    }
H
Hongze Cheng 已提交
886
    *fSet.pSmaF = *pSet->pSmaF;
H
Hongze Cheng 已提交
887

H
Hongze Cheng 已提交
888 889 890 891
    // stt
    for (fSet.nSttF = 0; fSet.nSttF < pSet->nSttF; fSet.nSttF++) {
      fSet.aSttF[fSet.nSttF] = (SSttFile *)taosMemoryMalloc(sizeof(SSttFile));
      if (fSet.aSttF[fSet.nSttF] == NULL) {
H
Hongze Cheng 已提交
892
        code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
893
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
894
      }
H
Hongze Cheng 已提交
895
      *fSet.aSttF[fSet.nSttF] = *pSet->aSttF[fSet.nSttF];
H
Hongze Cheng 已提交
896
    }
H
Hongze Cheng 已提交
897 898 899

    if (taosArrayPush(pFS->aDFileSet, &fSet) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
900
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
901
    }
H
Hongze Cheng 已提交
902 903 904
  }

_exit:
H
Hongze Cheng 已提交
905
  if (code) {
S
Shengliang Guan 已提交
906
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
907
  }
H
Hongze Cheng 已提交
908 909 910
  return code;
}

H
Hongze Cheng 已提交
911 912
int32_t tsdbFSUpsertDelFile(STsdbFS *pFS, SDelFile *pDelFile) {
  int32_t code = 0;
H
Hongze Cheng 已提交
913

H
Hongze Cheng 已提交
914 915 916 917 918
  if (pFS->pDelFile == NULL) {
    pFS->pDelFile = (SDelFile *)taosMemoryMalloc(sizeof(SDelFile));
    if (pFS->pDelFile == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _exit;
H
Hongze Cheng 已提交
919 920
    }
  }
H
Hongze Cheng 已提交
921 922 923 924
  *pFS->pDelFile = *pDelFile;

_exit:
  return code;
H
Hongze Cheng 已提交
925 926
}

H
Hongze Cheng 已提交
927 928 929 930 931 932 933 934 935 936 937 938 939
int32_t tsdbFSUpsertFSet(STsdbFS *pFS, SDFileSet *pSet) {
  int32_t code = 0;
  int32_t idx = taosArraySearchIdx(pFS->aDFileSet, pSet, tDFileSetCmprFn, TD_GE);

  if (idx < 0) {
    idx = taosArrayGetSize(pFS->aDFileSet);
  } else {
    SDFileSet *pDFileSet = (SDFileSet *)taosArrayGet(pFS->aDFileSet, idx);
    int32_t    c = tDFileSetCmprFn(pSet, pDFileSet);
    if (c == 0) {
      *pDFileSet->pHeadF = *pSet->pHeadF;
      *pDFileSet->pDataF = *pSet->pDataF;
      *pDFileSet->pSmaF = *pSet->pSmaF;
H
Hongze Cheng 已提交
940 941
      // stt
      if (pSet->nSttF > pDFileSet->nSttF) {
942
        ASSERT(pSet->nSttF == pDFileSet->nSttF + 1);
H
Hongze Cheng 已提交
943

H
Hongze Cheng 已提交
944 945
        pDFileSet->aSttF[pDFileSet->nSttF] = (SSttFile *)taosMemoryMalloc(sizeof(SSttFile));
        if (pDFileSet->aSttF[pDFileSet->nSttF] == NULL) {
H
Hongze Cheng 已提交
946 947 948
          code = TSDB_CODE_OUT_OF_MEMORY;
          goto _exit;
        }
H
Hongze Cheng 已提交
949 950 951
        *pDFileSet->aSttF[pDFileSet->nSttF] = *pSet->aSttF[pSet->nSttF - 1];
        pDFileSet->nSttF++;
      } else if (pSet->nSttF < pDFileSet->nSttF) {
952
        ASSERT(pSet->nSttF == 1);
H
Hongze Cheng 已提交
953 954
        for (int32_t iStt = 1; iStt < pDFileSet->nSttF; iStt++) {
          taosMemoryFree(pDFileSet->aSttF[iStt]);
H
Hongze Cheng 已提交
955 956
        }

H
Hongze Cheng 已提交
957 958
        *pDFileSet->aSttF[0] = *pSet->aSttF[0];
        pDFileSet->nSttF = 1;
H
Hongze Cheng 已提交
959
      } else {
H
Hongze Cheng 已提交
960 961
        for (int32_t iStt = 0; iStt < pSet->nSttF; iStt++) {
          *pDFileSet->aSttF[iStt] = *pSet->aSttF[iStt];
H
Hongze Cheng 已提交
962
        }
H
Hongze Cheng 已提交
963
      }
H
Hongze Cheng 已提交
964

K
kailixu 已提交
965 966
      pDFileSet->diskId = pSet->diskId;
      
H
Hongze Cheng 已提交
967 968 969 970
      goto _exit;
    }
  }

971
  ASSERT(pSet->nSttF == 1);
H
Hongze Cheng 已提交
972
  SDFileSet fSet = {.diskId = pSet->diskId, .fid = pSet->fid, .nSttF = 1};
H
Hongze Cheng 已提交
973 974 975 976

  // head
  fSet.pHeadF = (SHeadFile *)taosMemoryMalloc(sizeof(SHeadFile));
  if (fSet.pHeadF == NULL) {
H
Hongze Cheng 已提交
977
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
978
    goto _exit;
H
Hongze Cheng 已提交
979
  }
H
Hongze Cheng 已提交
980
  *fSet.pHeadF = *pSet->pHeadF;
H
Hongze Cheng 已提交
981

H
Hongze Cheng 已提交
982 983 984
  // data
  fSet.pDataF = (SDataFile *)taosMemoryMalloc(sizeof(SDataFile));
  if (fSet.pDataF == NULL) {
H
Hongze Cheng 已提交
985
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
986
    goto _exit;
H
Hongze Cheng 已提交
987
  }
H
Hongze Cheng 已提交
988 989
  *fSet.pDataF = *pSet->pDataF;

H
Hongze Cheng 已提交
990
  // sma
H
Hongze Cheng 已提交
991 992
  fSet.pSmaF = (SSmaFile *)taosMemoryMalloc(sizeof(SSmaFile));
  if (fSet.pSmaF == NULL) {
H
Hongze Cheng 已提交
993
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
994
    goto _exit;
H
Hongze Cheng 已提交
995
  }
H
Hongze Cheng 已提交
996 997
  *fSet.pSmaF = *pSet->pSmaF;

H
Hongze Cheng 已提交
998 999 1000
  // stt
  fSet.aSttF[0] = (SSttFile *)taosMemoryMalloc(sizeof(SSttFile));
  if (fSet.aSttF[0] == NULL) {
H
Hongze Cheng 已提交
1001 1002 1003
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }
H
Hongze Cheng 已提交
1004
  *fSet.aSttF[0] = *pSet->aSttF[0];
H
Hongze Cheng 已提交
1005

H
Hongze Cheng 已提交
1006
  if (taosArrayInsert(pFS->aDFileSet, idx, &fSet) == NULL) {
H
Hongze Cheng 已提交
1007
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1008 1009 1010 1011 1012 1013
    goto _exit;
  }

_exit:
  return code;
}
1014

H
Hongze Cheng 已提交
1015
int32_t tsdbFSPrepareCommit(STsdb *pTsdb, STsdbFS *pFSNew) {
1016
  int32_t code = 0;
H
Hongze Cheng 已提交
1017
  int32_t lino = 0;
1018 1019
  char    tfname[TSDB_FILENAME_LEN];

H
Hongze Cheng 已提交
1020
  tsdbGetCurrentFName(pTsdb, NULL, tfname);
1021 1022

  // gnrt CURRENT.t
H
Hongze Cheng 已提交
1023
  code = tsdbSaveFSToFile(pFSNew, tfname);
H
Hongze Cheng 已提交
1024
  TSDB_CHECK_CODE(code, lino, _exit);
1025

H
Hongze Cheng 已提交
1026
_exit:
1027
  if (code) {
S
Shengliang Guan 已提交
1028
    tsdbError("vgId:%d, %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
1029 1030 1031 1032
  }
  return code;
}

C
Cary Xu 已提交
1033
int32_t tsdbFSRef(STsdb *pTsdb, STsdbFS *pFS) {
H
Hongze Cheng 已提交
1034
  int32_t code = 0;
H
Hongze Cheng 已提交
1035
  int32_t nRef;
H
Hongze Cheng 已提交
1036

H
Hongze Cheng 已提交
1037 1038 1039 1040
  pFS->aDFileSet = taosArrayInit(taosArrayGetSize(pTsdb->fs.aDFileSet), sizeof(SDFileSet));
  if (pFS->aDFileSet == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
H
Hongze Cheng 已提交
1041 1042
  }

H
Hongze Cheng 已提交
1043 1044 1045
  pFS->pDelFile = pTsdb->fs.pDelFile;
  if (pFS->pDelFile) {
    nRef = atomic_fetch_add_32(&pFS->pDelFile->nRef, 1);
1046
    ASSERT(nRef > 0);
H
Hongze Cheng 已提交
1047 1048
  }

H
Hongze Cheng 已提交
1049 1050 1051 1052
  SDFileSet fSet;
  for (int32_t iSet = 0; iSet < taosArrayGetSize(pTsdb->fs.aDFileSet); iSet++) {
    SDFileSet *pSet = (SDFileSet *)taosArrayGet(pTsdb->fs.aDFileSet, iSet);
    fSet = *pSet;
H
Hongze Cheng 已提交
1053

H
Hongze Cheng 已提交
1054
    nRef = atomic_fetch_add_32(&pSet->pHeadF->nRef, 1);
1055
    ASSERT(nRef > 0);
H
Hongze Cheng 已提交
1056

H
Hongze Cheng 已提交
1057
    nRef = atomic_fetch_add_32(&pSet->pDataF->nRef, 1);
1058
    ASSERT(nRef > 0);
H
Hongze Cheng 已提交
1059

H
Hongze Cheng 已提交
1060
    nRef = atomic_fetch_add_32(&pSet->pSmaF->nRef, 1);
1061
    ASSERT(nRef > 0);
H
Hongze Cheng 已提交
1062

H
Hongze Cheng 已提交
1063 1064
    for (int32_t iStt = 0; iStt < pSet->nSttF; iStt++) {
      nRef = atomic_fetch_add_32(&pSet->aSttF[iStt]->nRef, 1);
1065
      ASSERT(nRef > 0);
H
Hongze Cheng 已提交
1066 1067
    }

H
Hongze Cheng 已提交
1068
    if (taosArrayPush(pFS->aDFileSet, &fSet) == NULL) {
H
Hongze Cheng 已提交
1069
      code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1070
      goto _exit;
H
Hongze Cheng 已提交
1071 1072 1073
    }
  }

H
Hongze Cheng 已提交
1074
_exit:
H
Hongze Cheng 已提交
1075 1076 1077
  return code;
}

H
Hongze Cheng 已提交
1078 1079 1080
void tsdbFSUnref(STsdb *pTsdb, STsdbFS *pFS) {
  int32_t nRef;
  char    fname[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
1081

H
Hongze Cheng 已提交
1082 1083
  if (pFS->pDelFile) {
    nRef = atomic_sub_fetch_32(&pFS->pDelFile->nRef, 1);
1084
    ASSERT(nRef >= 0);
H
Hongze Cheng 已提交
1085 1086
    if (nRef == 0) {
      tsdbDelFileName(pTsdb, pFS->pDelFile, fname);
H
Hongze Cheng 已提交
1087
      (void)taosRemoveFile(fname);
H
Hongze Cheng 已提交
1088 1089
      taosMemoryFree(pFS->pDelFile);
    }
H
Hongze Cheng 已提交
1090 1091
  }

H
Hongze Cheng 已提交
1092 1093
  for (int32_t iSet = 0; iSet < taosArrayGetSize(pFS->aDFileSet); iSet++) {
    SDFileSet *pSet = (SDFileSet *)taosArrayGet(pFS->aDFileSet, iSet);
H
Hongze Cheng 已提交
1094

H
Hongze Cheng 已提交
1095 1096
    // head
    nRef = atomic_sub_fetch_32(&pSet->pHeadF->nRef, 1);
1097
    ASSERT(nRef >= 0);
H
Hongze Cheng 已提交
1098 1099
    if (nRef == 0) {
      tsdbHeadFileName(pTsdb, pSet->diskId, pSet->fid, pSet->pHeadF, fname);
H
Hongze Cheng 已提交
1100
      (void)taosRemoveFile(fname);
H
Hongze Cheng 已提交
1101 1102
      taosMemoryFree(pSet->pHeadF);
    }
H
Hongze Cheng 已提交
1103

H
Hongze Cheng 已提交
1104 1105
    // data
    nRef = atomic_sub_fetch_32(&pSet->pDataF->nRef, 1);
1106
    ASSERT(nRef >= 0);
H
Hongze Cheng 已提交
1107 1108
    if (nRef == 0) {
      tsdbDataFileName(pTsdb, pSet->diskId, pSet->fid, pSet->pDataF, fname);
H
Hongze Cheng 已提交
1109
      (void)taosRemoveFile(fname);
H
Hongze Cheng 已提交
1110 1111
      taosMemoryFree(pSet->pDataF);
    }
H
Hongze Cheng 已提交
1112

H
Hongze Cheng 已提交
1113 1114
    // sma
    nRef = atomic_sub_fetch_32(&pSet->pSmaF->nRef, 1);
1115
    ASSERT(nRef >= 0);
H
Hongze Cheng 已提交
1116 1117
    if (nRef == 0) {
      tsdbSmaFileName(pTsdb, pSet->diskId, pSet->fid, pSet->pSmaF, fname);
H
Hongze Cheng 已提交
1118
      (void)taosRemoveFile(fname);
H
Hongze Cheng 已提交
1119
      taosMemoryFree(pSet->pSmaF);
H
Hongze Cheng 已提交
1120
    }
H
Hongze Cheng 已提交
1121

H
Hongze Cheng 已提交
1122 1123 1124
    // stt
    for (int32_t iStt = 0; iStt < pSet->nSttF; iStt++) {
      nRef = atomic_sub_fetch_32(&pSet->aSttF[iStt]->nRef, 1);
1125
      ASSERT(nRef >= 0);
H
Hongze Cheng 已提交
1126
      if (nRef == 0) {
H
Hongze Cheng 已提交
1127
        tsdbSttFileName(pTsdb, pSet->diskId, pSet->fid, pSet->aSttF[iStt], fname);
H
Hongze Cheng 已提交
1128
        (void)taosRemoveFile(fname);
H
Hongze Cheng 已提交
1129
        taosMemoryFree(pSet->aSttF[iStt]);
H
Hongze Cheng 已提交
1130 1131 1132
        /* code */
      }
    }
H
Hongze Cheng 已提交
1133 1134
  }

H
Hongze Cheng 已提交
1135 1136
  taosArrayDestroy(pFS->aDFileSet);
}