tsdbFS.c 28.9 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 tsdbEncodeFS(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
  // SDelFile
  n += tPutI8(p ? p + n : p, hasDel);
  if (hasDel) {
H
Hongze Cheng 已提交
27
    n += tPutDelFile(p ? p + n : p, pFS->pDelFile);
H
Hongze Cheng 已提交
28 29
  }

H
Hongze Cheng 已提交
30
  // SArray<SDFileSet>
H
Hongze Cheng 已提交
31 32 33
  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 已提交
34 35
  }

H
Hongze Cheng 已提交
36
  return n;
H
Hongze Cheng 已提交
37 38
}

H
Hongze Cheng 已提交
39
static int32_t tsdbGnrtCurrent(STsdb *pTsdb, STsdbFS *pFS, char *fname) {
H
Hongze Cheng 已提交
40 41 42
  int32_t   code = 0;
  int64_t   n;
  int64_t   size;
H
Hongze Cheng 已提交
43
  uint8_t  *pData = NULL;
H
Hongze Cheng 已提交
44 45
  TdFilePtr pFD = NULL;

H
Hongze Cheng 已提交
46
  // to binary
H
Hongze Cheng 已提交
47
  size = tsdbEncodeFS(NULL, pFS) + sizeof(TSCKSUM);
H
Hongze Cheng 已提交
48 49 50 51 52
  pData = taosMemoryMalloc(size);
  if (pData == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
H
Hongze Cheng 已提交
53
  n = tsdbEncodeFS(pData, pFS);
H
Hongze Cheng 已提交
54 55
  ASSERT(n + sizeof(TSCKSUM) == size);
  taosCalcChecksumAppend(0, pData, size);
H
Hongze Cheng 已提交
56

H
Hongze Cheng 已提交
57 58
  // create and write
  pFD = taosOpenFile(fname, TD_FILE_WRITE | TD_FILE_CREATE);
H
Hongze Cheng 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
  if (pFD == NULL) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  n = taosWriteFile(pFD, pData, size);
  if (n < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  if (taosFsyncFile(pFD) < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  taosCloseFile(&pFD);

  if (pData) taosMemoryFree(pData);
  return code;

_err:
H
Hongze Cheng 已提交
81
  tsdbError("vgId:%d tsdb gnrt current failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
82 83 84 85
  if (pData) taosMemoryFree(pData);
  return code;
}

H
Hongze Cheng 已提交
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
// static int32_t tsdbApplyDFileSetChange(STsdbFS *pFS, SDFileSet *pFrom, SDFileSet *pTo) {
//   int32_t code = 0;
//   char    fname[TSDB_FILENAME_LEN];

//   if (pFrom && pTo) {
//     bool isSameDisk = (pFrom->diskId.level == pTo->diskId.level) && (pFrom->diskId.id == pTo->diskId.id);

//     // head
//     if (isSameDisk && pFrom->pHeadF->commitID == pTo->pHeadF->commitID) {
//       ASSERT(pFrom->pHeadF->size == pTo->pHeadF->size);
//       ASSERT(pFrom->pHeadF->offset == pTo->pHeadF->offset);
//     } else {
//       tsdbHeadFileName(pFS->pTsdb, pFrom->diskId, pFrom->fid, pFrom->pHeadF, fname);
//       taosRemoveFile(fname);
//     }

//     // data
//     if (isSameDisk && pFrom->pDataF->commitID == pTo->pDataF->commitID) {
//       if (pFrom->pDataF->size > pTo->pDataF->size) {
//         code = tsdbDFileRollback(pFS->pTsdb, pTo, TSDB_DATA_FILE);
//         if (code) goto _err;
//       }
//     } else {
//       tsdbDataFileName(pFS->pTsdb, pFrom->diskId, pFrom->fid, pFrom->pDataF, fname);
//       taosRemoveFile(fname);
//     }

//     // last
//     if (isSameDisk && pFrom->pLastF->commitID == pTo->pLastF->commitID) {
//       if (pFrom->pLastF->size > pTo->pLastF->size) {
//         code = tsdbDFileRollback(pFS->pTsdb, pTo, TSDB_LAST_FILE);
//         if (code) goto _err;
//       }
//     } else {
//       tsdbLastFileName(pFS->pTsdb, pFrom->diskId, pFrom->fid, pFrom->pLastF, fname);
//       taosRemoveFile(fname);
//     }

//     // sma
//     if (isSameDisk && pFrom->pSmaF->commitID == pTo->pSmaF->commitID) {
//       if (pFrom->pSmaF->size > pTo->pSmaF->size) {
//         code = tsdbDFileRollback(pFS->pTsdb, pTo, TSDB_SMA_FILE);
//         if (code) goto _err;
//       }
//     } else {
//       tsdbSmaFileName(pFS->pTsdb, pFrom->diskId, pFrom->fid, pFrom->pSmaF, fname);
//       taosRemoveFile(fname);
//     }
//   } else if (pFrom) {
//     // head
//     tsdbHeadFileName(pFS->pTsdb, pFrom->diskId, pFrom->fid, pFrom->pHeadF, fname);
//     taosRemoveFile(fname);

//     // data
//     tsdbDataFileName(pFS->pTsdb, pFrom->diskId, pFrom->fid, pFrom->pDataF, fname);
//     taosRemoveFile(fname);

//     // last
//     tsdbLastFileName(pFS->pTsdb, pFrom->diskId, pFrom->fid, pFrom->pLastF, fname);
//     taosRemoveFile(fname);

//     // fsm
//     tsdbSmaFileName(pFS->pTsdb, pFrom->diskId, pFrom->fid, pFrom->pSmaF, fname);
//     taosRemoveFile(fname);
//   }

//   return code;

// _err:
//   tsdbError("vgId:%d tsdb apply disk file set change failed since %s", TD_VID(pFS->pTsdb->pVnode), tstrerror(code));
//   return code;
// }

// static int32_t tsdbApplyDelFileChange(STsdbFS *pFS, SDelFile *pFrom, SDelFile *pTo) {
//   int32_t code = 0;
//   char    fname[TSDB_FILENAME_LEN];

//   if (pFrom && pTo) {
//     if (!tsdbDelFileIsSame(pFrom, pTo)) {
//       tsdbDelFileName(pFS->pTsdb, pFrom, fname);
//       if (taosRemoveFile(fname) < 0) {
//         code = TAOS_SYSTEM_ERROR(errno);
//         goto _err;
//       }
//     }
//   } else if (pFrom) {
//     tsdbDelFileName(pFS->pTsdb, pFrom, fname);
//     if (taosRemoveFile(fname) < 0) {
//       code = TAOS_SYSTEM_ERROR(errno);
//       goto _err;
//     }
//   } else {
//     // do nothing
//   }

//   return code;

// _err:
//   tsdbError("vgId:%d tsdb apply del file change failed since %s", TD_VID(pFS->pTsdb->pVnode), tstrerror(code));
//   return code;
// }

// static int32_t tsdbFSApplyDiskChange(STsdbFS *pFS, STsdbFSState *pFrom, STsdbFSState *pTo) {
//   int32_t    code = 0;
//   int32_t    iFrom = 0;
//   int32_t    nFrom = taosArrayGetSize(pFrom->aDFileSet);
//   int32_t    iTo = 0;
//   int32_t    nTo = taosArrayGetSize(pTo->aDFileSet);
//   SDFileSet *pDFileSetFrom;
//   SDFileSet *pDFileSetTo;

//   // SDelFile
//   code = tsdbApplyDelFileChange(pFS, pFrom->pDelFile, pTo->pDelFile);
//   if (code) goto _err;

//   // SDFileSet
//   while (iFrom < nFrom && iTo < nTo) {
//     pDFileSetFrom = (SDFileSet *)taosArrayGet(pFrom->aDFileSet, iFrom);
//     pDFileSetTo = (SDFileSet *)taosArrayGet(pTo->aDFileSet, iTo);

//     if (pDFileSetFrom->fid == pDFileSetTo->fid) {
//       code = tsdbApplyDFileSetChange(pFS, pDFileSetFrom, pDFileSetTo);
//       if (code) goto _err;

//       iFrom++;
//       iTo++;
//     } else if (pDFileSetFrom->fid < pDFileSetTo->fid) {
//       code = tsdbApplyDFileSetChange(pFS, pDFileSetFrom, NULL);
//       if (code) goto _err;

//       iFrom++;
//     } else {
//       iTo++;
//     }
//   }

//   while (iFrom < nFrom) {
//     pDFileSetFrom = (SDFileSet *)taosArrayGet(pFrom->aDFileSet, iFrom);
//     code = tsdbApplyDFileSetChange(pFS, pDFileSetFrom, NULL);
//     if (code) goto _err;

//     iFrom++;
//   }

// #if 0
//   // do noting
//   while (iTo < nTo) {
//     pDFileSetTo = (SDFileSet *)taosArrayGetP(pTo->aDFileSet, iTo);
//     code = tsdbApplyDFileSetChange(pFS, NULL, pDFileSetTo);
//     if (code) goto _err;

//     iTo++;
//   }
// #endif

//   return code;

// _err:
//   tsdbError("vgId:%d tsdb fs apply disk change failed sicne %s", TD_VID(pFS->pTsdb->pVnode), tstrerror(code));
//   return code;
// }

void tsdbFSDestroy(STsdbFS *pFS) {
  if (pFS->pDelFile) {
    taosMemoryFree(pFS->pDelFile);
  }
H
Hongze Cheng 已提交
252

H
Hongze Cheng 已提交
253 254 255 256 257 258 259
  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->pLastF);
    taosMemoryFree(pSet->pSmaF);
  }
H
Hongze Cheng 已提交
260

H
Hongze Cheng 已提交
261 262 263 264 265 266 267 268 269 270 271 272
  taosArrayDestroy(pFS->aDFileSet);
}

static int32_t tsdbScanAndTryFixFS(STsdb *pTsdb) {
  int32_t code = 0;
  int64_t size;
  char    fname[TSDB_FILENAME_LEN];

  // SDelFile
  if (pTsdb->fs.pDelFile) {
    tsdbDelFileName(pTsdb, pTsdb->fs.pDelFile, fname);
    if (taosStatFile(fname, &size, NULL)) {
H
Hongze Cheng 已提交
273 274 275 276
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }

H
Hongze Cheng 已提交
277 278
    if (size != pTsdb->fs.pDelFile->size) {
      code = TSDB_CODE_FILE_CORRUPTED;
H
Hongze Cheng 已提交
279 280
      goto _err;
    }
H
Hongze Cheng 已提交
281
  }
H
Hongze Cheng 已提交
282

H
Hongze Cheng 已提交
283 284 285 286 287 288 289 290 291 292 293 294
  // SArray<SDFileSet>
  for (int32_t iSet = 0; iSet < taosArrayGetSize(pTsdb->fs.aDFileSet); iSet++) {
    SDFileSet *pSet = (SDFileSet *)taosArrayGet(pTsdb->fs.aDFileSet, iSet);

    // head =========
    tsdbHeadFileName(pTsdb, pSet->diskId, pSet->fid, pSet->pHeadF, fname);
    if (taosStatFile(fname, &size, NULL)) {
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }
    if (size != pSet->pHeadF->size) {
      code = TSDB_CODE_FILE_CORRUPTED;
H
Hongze Cheng 已提交
295 296 297
      goto _err;
    }

H
Hongze Cheng 已提交
298 299 300
    // data =========
    tsdbDataFileName(pTsdb, pSet->diskId, pSet->fid, pSet->pDataF, fname);
    if (taosStatFile(fname, &size, NULL)) {
H
Hongze Cheng 已提交
301 302 303
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }
H
Hongze Cheng 已提交
304 305 306 307 308 309 310
    if (size < pSet->pDataF->size) {
      code = TSDB_CODE_FILE_CORRUPTED;
      goto _err;
    } else if (size > pSet->pDataF->size) {
      code = tsdbDFileRollback(pTsdb, pSet, TSDB_DATA_FILE);
      if (code) goto _err;
    }
H
Hongze Cheng 已提交
311

H
Hongze Cheng 已提交
312 313 314 315 316 317 318
    // last ===========
    tsdbLastFileName(pTsdb, pSet->diskId, pSet->fid, pSet->pLastF, fname);
    if (taosStatFile(fname, &size, NULL)) {
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }
    if (size != pSet->pLastF->size) {
H
Hongze Cheng 已提交
319 320 321 322
      code = TSDB_CODE_FILE_CORRUPTED;
      goto _err;
    }

H
Hongze Cheng 已提交
323 324 325 326 327 328 329 330 331 332 333 334 335 336
    // sma =============
    tsdbSmaFileName(pTsdb, pSet->diskId, pSet->fid, pSet->pSmaF, fname);
    if (taosStatFile(fname, &size, NULL)) {
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }
    if (size < pSet->pSmaF->size) {
      code = TSDB_CODE_FILE_CORRUPTED;
      goto _err;
    } else if (size > pSet->pSmaF->size) {
      code = tsdbDFileRollback(pTsdb, pSet, TSDB_SMA_FILE);
      if (code) goto _err;
    }
  }
H
Hongze Cheng 已提交
337

H
Hongze Cheng 已提交
338 339
  {
    // remove those invalid files (todo)
H
Hongze Cheng 已提交
340 341 342 343 344
  }

  return code;

_err:
H
Hongze Cheng 已提交
345
  tsdbError("vgId:%d tsdb scan and try fix fs failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
346 347 348
  return code;
}

H
Hongze Cheng 已提交
349 350 351 352 353 354 355 356 357
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 已提交
358

H
Hongze Cheng 已提交
359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
static int32_t tsdbRecoverFS(STsdb *pTsdb, uint8_t *pData, int64_t nData) {
  int32_t  code = 0;
  int8_t   hasDel;
  uint32_t nSet;
  int32_t  n;

  // SDelFile
  n = 0;
  n += tGetI8(pData + n, &hasDel);
  if (hasDel) {
    pTsdb->fs.pDelFile = (SDelFile *)taosMemoryMalloc(sizeof(SDelFile));
    if (pTsdb->fs.pDelFile == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }

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

  // SArray<SDFileSet>
  taosArrayClear(pTsdb->fs.aDFileSet);
  n += tGetU32v(pData + n, &nSet);
  for (uint32_t iSet = 0; iSet < nSet; iSet++) {
    SDFileSet fSet;
H
Hongze Cheng 已提交
386

H
Hongze Cheng 已提交
387
    // head
H
Hongze Cheng 已提交
388 389 390 391
    fSet.pHeadF = (SHeadFile *)taosMemoryCalloc(1, sizeof(SHeadFile));
    if (fSet.pHeadF == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
H
Hongze Cheng 已提交
392
    }
H
Hongze Cheng 已提交
393
    fSet.pHeadF->nRef = 1;
H
Hongze Cheng 已提交
394 395

    // data
H
Hongze Cheng 已提交
396 397 398 399
    fSet.pDataF = (SDataFile *)taosMemoryCalloc(1, sizeof(SDataFile));
    if (fSet.pDataF == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
H
Hongze Cheng 已提交
400
    }
H
Hongze Cheng 已提交
401
    fSet.pDataF->nRef = 1;
H
Hongze Cheng 已提交
402 403

    // last
H
Hongze Cheng 已提交
404 405 406 407
    fSet.pLastF = (SLastFile *)taosMemoryCalloc(1, sizeof(SLastFile));
    if (fSet.pLastF == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
H
Hongze Cheng 已提交
408
    }
H
Hongze Cheng 已提交
409
    fSet.pLastF->nRef = 1;
H
Hongze Cheng 已提交
410 411

    // sma
H
Hongze Cheng 已提交
412 413 414 415
    fSet.pSmaF = (SSmaFile *)taosMemoryCalloc(1, sizeof(SSmaFile));
    if (fSet.pSmaF == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
H
Hongze Cheng 已提交
416
    }
H
Hongze Cheng 已提交
417
    fSet.pSmaF->nRef = 1;
H
Hongze Cheng 已提交
418

H
Hongze Cheng 已提交
419
    n += tGetDFileSet(pData + n, &fSet);
H
Hongze Cheng 已提交
420

H
Hongze Cheng 已提交
421 422 423 424
    if (taosArrayPush(pTsdb->fs.aDFileSet, &fSet) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
H
Hongze Cheng 已提交
425 426
  }

H
Hongze Cheng 已提交
427
  ASSERT(n + sizeof(TSCKSUM) == nData);
H
Hongze Cheng 已提交
428 429 430
  return code;

_err:
H
Hongze Cheng 已提交
431 432 433
  return code;
}

H
Hongze Cheng 已提交
434 435
// EXPOSED APIS ====================================================================================
int32_t tsdbFSOpen(STsdb *pTsdb) {
H
Hongze Cheng 已提交
436 437
  int32_t code = 0;

H
Hongze Cheng 已提交
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
  // open handle
  pTsdb->fs.pDelFile = NULL;
  pTsdb->fs.aDFileSet = taosArrayInit(0, sizeof(SDFileSet));
  if (pTsdb->fs.aDFileSet == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }

  // load fs or keep empty
  char fname[TSDB_FILENAME_LEN];

  snprintf(fname, TSDB_FILENAME_LEN - 1, "%s%s%s%sCURRENT", tfsGetPrimaryPath(pTsdb->pVnode->pTfs), TD_DIRSEP,
           pTsdb->path, TD_DIRSEP);

  if (!taosCheckExistFile(fname)) {
    // empty one
    code = tsdbGnrtCurrent(pTsdb, &pTsdb->fs, fname);
    if (code) goto _err;
  } else {
    // read
    TdFilePtr pFD = taosOpenFile(fname, TD_FILE_READ);
    if (pFD == NULL) {
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
H
Hongze Cheng 已提交
462
    }
H
Hongze Cheng 已提交
463 464 465

    int64_t size;
    if (taosFStatFile(pFD, &size, NULL) < 0) {
H
Hongze Cheng 已提交
466
      code = TAOS_SYSTEM_ERROR(errno);
H
Hongze Cheng 已提交
467
      taosCloseFile(&pFD);
H
Hongze Cheng 已提交
468 469
      goto _err;
    }
H
Hongze Cheng 已提交
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502

    uint8_t *pData = taosMemoryMalloc(size);
    if (pData == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      taosCloseFile(&pFD);
      goto _err;
    }

    int64_t n = taosReadFile(pFD, pData, size);
    if (n < 0) {
      code = TAOS_SYSTEM_ERROR(errno);
      taosMemoryFree(pData);
      taosCloseFile(&pFD);
      goto _err;
    }

    if (!taosCheckChecksumWhole(pData, size)) {
      code = TSDB_CODE_FILE_CORRUPTED;
      taosMemoryFree(pData);
      taosCloseFile(&pFD);
      goto _err;
    }

    taosCloseFile(&pFD);

    // recover fs
    code = tsdbRecoverFS(pTsdb, pData, size);
    if (code) {
      taosMemoryFree(pData);
      goto _err;
    }

    taosMemoryFree(pData);
H
Hongze Cheng 已提交
503 504
  }

H
Hongze Cheng 已提交
505 506 507 508
  // scan and fix FS
  code = tsdbScanAndTryFixFS(pTsdb);
  if (code) goto _err;

H
Hongze Cheng 已提交
509 510 511
  return code;

_err:
H
Hongze Cheng 已提交
512
  tsdbError("vgId:%d tsdb fs open failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
513 514 515
  return code;
}

H
Hongze Cheng 已提交
516 517
int32_t tsdbFSClose(STsdb *pTsdb) {
  int32_t code = 0;
H
Hongze Cheng 已提交
518

H
Hongze Cheng 已提交
519 520 521 522
  if (pTsdb->fs.pDelFile) {
    ASSERT(pTsdb->fs.pDelFile->nRef == 1);
    taosMemoryFree(pTsdb->fs.pDelFile);
  }
H
Hongze Cheng 已提交
523

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

H
Hongze Cheng 已提交
527 528 529
    // head
    ASSERT(pSet->pHeadF->nRef == 1);
    taosMemoryFree(pSet->pHeadF);
H
Hongze Cheng 已提交
530

H
Hongze Cheng 已提交
531 532 533
    // data
    ASSERT(pSet->pDataF->nRef == 1);
    taosMemoryFree(pSet->pDataF);
H
Hongze Cheng 已提交
534

H
Hongze Cheng 已提交
535 536 537 538 539 540 541
    // last
    ASSERT(pSet->pLastF->nRef == 1);
    taosMemoryFree(pSet->pLastF);

    // sma
    ASSERT(pSet->pSmaF->nRef == 1);
    taosMemoryFree(pSet->pSmaF);
H
Hongze Cheng 已提交
542 543
  }

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

H
Hongze Cheng 已提交
546 547 548 549 550 551 552 553 554 555 556
  return code;
}

int32_t tsdbFSCopy(STsdb *pTsdb, STsdbFS *pFS) {
  int32_t code = 0;

  pFS->pDelFile = NULL;
  pFS->aDFileSet = taosArrayInit(taosArrayGetSize(pTsdb->fs.aDFileSet), sizeof(SDFileSet));
  if (pFS->aDFileSet == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
H
Hongze Cheng 已提交
557 558
  }

H
Hongze Cheng 已提交
559 560 561 562 563 564
  if (pTsdb->fs.pDelFile) {
    pFS->pDelFile = (SDelFile *)taosMemoryMalloc(sizeof(SDelFile));
    if (pFS->pDelFile == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _exit;
    }
H
Hongze Cheng 已提交
565

H
Hongze Cheng 已提交
566
    *pFS->pDelFile = *pTsdb->fs.pDelFile;
H
Hongze Cheng 已提交
567
  }
H
Hongze Cheng 已提交
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 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

  for (int32_t iSet = 0; iSet < taosArrayGetSize(pTsdb->fs.aDFileSet); iSet++) {
    SDFileSet *pSet = (SDFileSet *)taosArrayGet(pTsdb->fs.aDFileSet, iSet);
    SDFileSet  fSet = {.diskId = pSet->diskId, .fid = pSet->fid};

    // head
    fSet.pHeadF = (SHeadFile *)taosMemoryMalloc(sizeof(SHeadFile));
    if (fSet.pHeadF == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _exit;
    }
    fSet.pHeadF->nRef = 0;
    fSet.pHeadF->commitID = pSet->pHeadF->commitID;
    fSet.pHeadF->size = pSet->pHeadF->size;
    fSet.pHeadF->offset = pSet->pHeadF->offset;

    // data
    fSet.pDataF = (SDataFile *)taosMemoryMalloc(sizeof(SDataFile));
    if (fSet.pDataF == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _exit;
    }
    fSet.pDataF->nRef = 0;
    fSet.pDataF->commitID = pSet->pDataF->commitID;
    fSet.pDataF->size = pSet->pDataF->size;

    // data
    fSet.pLastF = (SLastFile *)taosMemoryMalloc(sizeof(SLastFile));
    if (fSet.pLastF == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _exit;
    }
    fSet.pLastF->nRef = 0;
    fSet.pLastF->commitID = pSet->pLastF->commitID;
    fSet.pLastF->size = pSet->pLastF->size;

    // last
    fSet.pSmaF = (SSmaFile *)taosMemoryMalloc(sizeof(SSmaFile));
    if (fSet.pSmaF == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _exit;
    }
    fSet.pSmaF->nRef = 0;
    fSet.pSmaF->commitID = pSet->pSmaF->commitID;
    fSet.pSmaF->size = pSet->pSmaF->size;

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

_exit:
  return code;
}

int32_t tsdbFSRollback(STsdbFS *pFS) {
  int32_t code = 0;

  ASSERT(0);
H
Hongze Cheng 已提交
628 629 630 631 632 633 634

  return code;

_err:
  return code;
}

H
Hongze Cheng 已提交
635 636
int32_t tsdbFSUpsertDelFile(STsdbFS *pFS, SDelFile *pDelFile) {
  int32_t code = 0;
H
Hongze Cheng 已提交
637

H
Hongze Cheng 已提交
638 639 640 641 642
  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 已提交
643 644
    }
  }
H
Hongze Cheng 已提交
645 646 647 648
  *pFS->pDelFile = *pDelFile;

_exit:
  return code;
H
Hongze Cheng 已提交
649 650
}

H
Hongze Cheng 已提交
651 652 653 654 655 656 657 658 659 660 661 662 663 664
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->pLastF = *pSet->pLastF;
      *pDFileSet->pSmaF = *pSet->pSmaF;
H
Hongze Cheng 已提交
665

H
Hongze Cheng 已提交
666 667 668 669 670 671 672 673 674
      goto _exit;
    }
  }

  SDFileSet fSet = {.diskId = pSet->diskId, .fid = pSet->fid};

  // head
  fSet.pHeadF = (SHeadFile *)taosMemoryMalloc(sizeof(SHeadFile));
  if (fSet.pHeadF == NULL) {
H
Hongze Cheng 已提交
675
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
676
    goto _exit;
H
Hongze Cheng 已提交
677
  }
H
Hongze Cheng 已提交
678
  *fSet.pHeadF = *pSet->pHeadF;
H
Hongze Cheng 已提交
679

H
Hongze Cheng 已提交
680 681 682
  // data
  fSet.pDataF = (SDataFile *)taosMemoryMalloc(sizeof(SDataFile));
  if (fSet.pDataF == NULL) {
H
Hongze Cheng 已提交
683
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
684
    goto _exit;
H
Hongze Cheng 已提交
685
  }
H
Hongze Cheng 已提交
686 687 688 689 690
  *fSet.pDataF = *pSet->pDataF;

  // data
  fSet.pLastF = (SLastFile *)taosMemoryMalloc(sizeof(SLastFile));
  if (fSet.pLastF == NULL) {
H
Hongze Cheng 已提交
691
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
692
    goto _exit;
H
Hongze Cheng 已提交
693
  }
H
Hongze Cheng 已提交
694
  *fSet.pLastF = *pSet->pLastF;
H
Hongze Cheng 已提交
695

H
Hongze Cheng 已提交
696 697 698
  // last
  fSet.pSmaF = (SSmaFile *)taosMemoryMalloc(sizeof(SSmaFile));
  if (fSet.pSmaF == NULL) {
H
Hongze Cheng 已提交
699
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
700
    goto _exit;
H
Hongze Cheng 已提交
701
  }
H
Hongze Cheng 已提交
702 703 704
  *fSet.pSmaF = *pSet->pSmaF;

  if (taosArrayInsert(pFS->aDFileSet, idx, &fSet) == NULL) {
H
Hongze Cheng 已提交
705
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
    goto _exit;
  }

_exit:
  return code;
}

int32_t tsdbFSCommit1(STsdb *pTsdb, STsdbFS *pFSNew) {
  int32_t code = 0;
  char    tfname[TSDB_FILENAME_LEN];
  char    fname[TSDB_FILENAME_LEN];

  snprintf(tfname, TSDB_FILENAME_LEN - 1, "%s%s%s%sCURRENT.t", tfsGetPrimaryPath(pTsdb->pVnode->pTfs), TD_DIRSEP,
           pTsdb->path, TD_DIRSEP);
  snprintf(fname, TSDB_FILENAME_LEN - 1, "%s%s%s%sCURRENT", tfsGetPrimaryPath(pTsdb->pVnode->pTfs), TD_DIRSEP,
           pTsdb->path, TD_DIRSEP);

  // gnrt CURRENT.t
  code = tsdbGnrtCurrent(pTsdb, pFSNew, tfname);
  if (code) goto _err;

  // rename
  code = taosRenameFile(tfname, fname);
  if (code) {
    code = TAOS_SYSTEM_ERROR(code);
H
Hongze Cheng 已提交
731 732 733 734 735 736
    goto _err;
  }

  return code;

_err:
H
Hongze Cheng 已提交
737
  tsdbError("vgId:%d tsdb fs commit phase 1 failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
738 739 740
  return code;
}

H
Hongze Cheng 已提交
741 742 743 744
int32_t tsdbFSCommit2(STsdb *pTsdb, STsdbFS *pFSNew) {
  int32_t code = 0;
  int32_t nRef;
  char    fname[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
745

H
Hongze Cheng 已提交
746 747 748
  // del
  if (pFSNew->pDelFile) {
    SDelFile *pDelFile = pTsdb->fs.pDelFile;
H
Hongze Cheng 已提交
749

H
Hongze Cheng 已提交
750 751 752 753 754 755
    if (pDelFile == NULL || (pDelFile->commitID != pFSNew->pDelFile->commitID)) {
      pTsdb->fs.pDelFile = (SDelFile *)taosMemoryMalloc(sizeof(SDelFile));
      if (pTsdb->fs.pDelFile == NULL) {
        code = TSDB_CODE_OUT_OF_MEMORY;
        goto _err;
      }
H
Hongze Cheng 已提交
756

H
Hongze Cheng 已提交
757 758 759 760 761 762 763 764 765 766 767
      *pTsdb->fs.pDelFile = *pFSNew->pDelFile;
      pTsdb->fs.pDelFile->nRef = 1;

      if (pDelFile) {
        nRef = atomic_sub_fetch_32(&pDelFile->nRef, 1);
        if (nRef == 0) {
          tsdbDelFileName(pTsdb, pDelFile, fname);
          taosRemoveFile(fname);
          taosMemoryFree(pDelFile);
        }
      }
H
Hongze Cheng 已提交
768
    }
H
Hongze Cheng 已提交
769 770
  } else {
    ASSERT(pTsdb->fs.pDelFile == NULL);
H
Hongze Cheng 已提交
771 772
  }

H
Hongze Cheng 已提交
773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
  // data
  int32_t iOld = 0;
  int32_t iNew = 0;
  while (true) {
    int32_t   nOld = taosArrayGetSize(pTsdb->fs.aDFileSet);
    int32_t   nNew = taosArrayGetSize(pFSNew->aDFileSet);
    SDFileSet fSet;
    int8_t    sameDisk;

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

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

    if (pSetOld && pSetNew) {
      if (pSetOld->fid == pSetNew->fid) {
        goto _merge_old_and_new;
      } else if (pSetOld->fid < pSetNew->fid) {
        goto _remove_old;
      } else {
        goto _add_new;
      }
    } else if (pSetOld) {
      goto _remove_old;
    } else {
      goto _add_new;
    }
H
Hongze Cheng 已提交
800

H
Hongze Cheng 已提交
801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823
  _merge_old_and_new:
    sameDisk = ((pSetOld->diskId.level == pSetNew->diskId.level) && (pSetOld->diskId.id == pSetNew->diskId.id));

    // head
    fSet.pHeadF = pSetOld->pHeadF;
    if ((!sameDisk) || (pSetOld->pHeadF->commitID != pSetNew->pHeadF->commitID)) {
      pSetOld->pHeadF = (SHeadFile *)taosMemoryMalloc(sizeof(SHeadFile));
      if (pSetOld->pHeadF == NULL) {
        code = TSDB_CODE_OUT_OF_MEMORY;
        goto _err;
      }
      *pSetOld->pHeadF = *pSetNew->pHeadF;
      pSetOld->pHeadF->nRef = 1;

      nRef = atomic_sub_fetch_32(&fSet.pHeadF->nRef, 1);
      if (nRef == 0) {
        tsdbHeadFileName(pTsdb, pSetOld->diskId, pSetOld->fid, fSet.pHeadF, fname);
        taosRemoveFile(fname);
        taosMemoryFree(fSet.pHeadF);
      }
    } else {
      ASSERT(fSet.pHeadF->size == pSetNew->pHeadF->size);
      ASSERT(fSet.pHeadF->offset == pSetNew->pHeadF->offset);
H
Hongze Cheng 已提交
824 825
    }

H
Hongze Cheng 已提交
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
    // data
    fSet.pDataF = pSetOld->pDataF;
    if ((!sameDisk) || (pSetOld->pDataF->commitID != pSetNew->pDataF->commitID)) {
      pSetOld->pDataF = (SDataFile *)taosMemoryMalloc(sizeof(SDataFile));
      if (pSetOld->pDataF == NULL) {
        code = TSDB_CODE_OUT_OF_MEMORY;
        goto _err;
      }
      *pSetOld->pDataF = *pSetNew->pDataF;
      pSetOld->pDataF->nRef = 1;

      nRef = atomic_sub_fetch_32(&fSet.pDataF->nRef, 1);
      if (nRef == 0) {
        tsdbDataFileName(pTsdb, pSetOld->diskId, pSetOld->fid, fSet.pDataF, fname);
        taosRemoveFile(fname);
        taosMemoryFree(fSet.pDataF);
      }
    } else {
      ASSERT(pSetOld->pDataF->size <= pSetNew->pDataF->size);
      pSetOld->pDataF->size = pSetNew->pDataF->size;
H
Hongze Cheng 已提交
846 847
    }

H
Hongze Cheng 已提交
848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866
    // last
    fSet.pLastF = pSetOld->pLastF;
    if ((!sameDisk) || (pSetOld->pLastF->commitID != pSetNew->pLastF->commitID)) {
      pSetOld->pLastF = (SLastFile *)taosMemoryMalloc(sizeof(SLastFile));
      if (pSetOld->pLastF == NULL) {
        code = TSDB_CODE_OUT_OF_MEMORY;
        goto _err;
      }
      *pSetOld->pLastF = *pSetNew->pLastF;
      pSetOld->pLastF->nRef = 1;

      nRef = atomic_sub_fetch_32(&fSet.pLastF->nRef, 1);
      if (nRef == 0) {
        tsdbLastFileName(pTsdb, pSetOld->diskId, pSetOld->fid, fSet.pLastF, fname);
        taosRemoveFile(fname);
        taosMemoryFree(fSet.pLastF);
      }
    } else {
      ASSERT(pSetOld->pLastF->size == pSetNew->pLastF->size);
H
Hongze Cheng 已提交
867 868
    }

H
Hongze Cheng 已提交
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
    // sma
    fSet.pSmaF = pSetOld->pSmaF;
    if ((!sameDisk) || (pSetOld->pSmaF->commitID != pSetNew->pSmaF->commitID)) {
      pSetOld->pSmaF = (SSmaFile *)taosMemoryMalloc(sizeof(SSmaFile));
      if (pSetOld->pSmaF == NULL) {
        code = TSDB_CODE_OUT_OF_MEMORY;
        goto _err;
      }
      *pSetOld->pSmaF = *pSetNew->pSmaF;
      pSetOld->pSmaF->nRef = 1;

      nRef = atomic_sub_fetch_32(&fSet.pSmaF->nRef, 1);
      if (nRef == 0) {
        tsdbSmaFileName(pTsdb, pSetOld->diskId, pSetOld->fid, fSet.pSmaF, fname);
        taosRemoveFile(fname);
        taosMemoryFree(fSet.pSmaF);
      }
    } else {
      ASSERT(pSetOld->pSmaF->size <= pSetNew->pSmaF->size);
      pSetOld->pSmaF->size = pSetNew->pSmaF->size;
H
Hongze Cheng 已提交
889 890
    }

H
Hongze Cheng 已提交
891 892
    if (!sameDisk) {
      pSetOld->diskId = pSetNew->diskId;
H
Hongze Cheng 已提交
893 894
    }

H
Hongze Cheng 已提交
895 896 897 898 899 900 901 902 903 904
    iOld++;
    iNew++;
    continue;

  _remove_old:
    nRef = atomic_sub_fetch_32(&pSetOld->pHeadF->nRef, 1);
    if (nRef == 0) {
      tsdbHeadFileName(pTsdb, pSetOld->diskId, pSetOld->fid, pSetOld->pHeadF, fname);
      taosRemoveFile(fname);
      taosMemoryFree(pSetOld->pHeadF);
H
Hongze Cheng 已提交
905 906
    }

H
Hongze Cheng 已提交
907 908 909 910 911
    nRef = atomic_sub_fetch_32(&pSetOld->pDataF->nRef, 1);
    if (nRef == 0) {
      tsdbDataFileName(pTsdb, pSetOld->diskId, pSetOld->fid, pSetOld->pDataF, fname);
      taosRemoveFile(fname);
      taosMemoryFree(pSetOld->pDataF);
H
Hongze Cheng 已提交
912 913
    }

H
Hongze Cheng 已提交
914 915 916 917 918
    nRef = atomic_sub_fetch_32(&pSetOld->pLastF->nRef, 1);
    if (nRef == 0) {
      tsdbLastFileName(pTsdb, pSetOld->diskId, pSetOld->fid, pSetOld->pLastF, fname);
      taosRemoveFile(fname);
      taosMemoryFree(pSetOld->pLastF);
H
Hongze Cheng 已提交
919 920
    }

H
Hongze Cheng 已提交
921 922 923 924 925
    nRef = atomic_sub_fetch_32(&pSetOld->pSmaF->nRef, 1);
    if (nRef == 0) {
      tsdbSmaFileName(pTsdb, pSetOld->diskId, pSetOld->fid, pSetOld->pSmaF, fname);
      taosRemoveFile(fname);
      taosMemoryFree(pSetOld->pSmaF);
H
Hongze Cheng 已提交
926 927
    }

H
Hongze Cheng 已提交
928 929 930 931 932 933 934 935 936 937 938
    taosArrayRemove(pTsdb->fs.aDFileSet, iOld);
    continue;

  _add_new:
    fSet.diskId = pSetNew->diskId;
    fSet.fid = pSetNew->fid;

    // head
    fSet.pHeadF = (SHeadFile *)taosMemoryMalloc(sizeof(SHeadFile));
    if (fSet.pHeadF == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
939 940
      goto _err;
    }
H
Hongze Cheng 已提交
941 942
    *fSet.pHeadF = *pSetNew->pHeadF;
    fSet.pHeadF->nRef = 1;
H
Hongze Cheng 已提交
943

H
Hongze Cheng 已提交
944 945 946 947 948
    // data
    fSet.pDataF = (SDataFile *)taosMemoryMalloc(sizeof(SDataFile));
    if (fSet.pDataF == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
H
Hongze Cheng 已提交
949
    }
H
Hongze Cheng 已提交
950 951
    *fSet.pDataF = *pSetNew->pDataF;
    fSet.pDataF->nRef = 1;
H
Hongze Cheng 已提交
952

H
Hongze Cheng 已提交
953 954 955 956 957 958 959 960
    // last
    fSet.pLastF = (SLastFile *)taosMemoryMalloc(sizeof(SLastFile));
    if (fSet.pLastF == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
    *fSet.pLastF = *pSetNew->pLastF;
    fSet.pLastF->nRef = 1;
H
Hongze Cheng 已提交
961

H
Hongze Cheng 已提交
962 963 964 965 966 967 968 969
    // sma
    fSet.pSmaF = (SSmaFile *)taosMemoryMalloc(sizeof(SSmaFile));
    if (fSet.pSmaF == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
    *fSet.pSmaF = *pSetNew->pSmaF;
    fSet.pSmaF->nRef = 1;
H
Hongze Cheng 已提交
970

H
Hongze Cheng 已提交
971 972 973 974 975 976 977
    if (taosArrayInsert(pTsdb->fs.aDFileSet, iOld, &fSet) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
    iOld++;
    iNew++;
    continue;
H
Hongze Cheng 已提交
978 979 980 981 982
  }

  return code;

_err:
H
Hongze Cheng 已提交
983
  tsdbError("vgId:%d tsdb fs commit phase 2 failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
984 985 986
  return code;
}

H
Hongze Cheng 已提交
987
int32_t tsdbFSRef(STsdb *pTsdb, STsdbFS *pFS) {
H
Hongze Cheng 已提交
988
  int32_t code = 0;
H
Hongze Cheng 已提交
989
  int32_t nRef;
H
Hongze Cheng 已提交
990

H
Hongze Cheng 已提交
991 992 993 994
  pFS->aDFileSet = taosArrayInit(taosArrayGetSize(pTsdb->fs.aDFileSet), sizeof(SDFileSet));
  if (pFS->aDFileSet == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
H
Hongze Cheng 已提交
995 996
  }

H
Hongze Cheng 已提交
997 998 999 1000
  pFS->pDelFile = pTsdb->fs.pDelFile;
  if (pFS->pDelFile) {
    nRef = atomic_fetch_add_32(&pFS->pDelFile->nRef, 1);
    ASSERT(nRef > 0);
H
Hongze Cheng 已提交
1001 1002
  }

H
Hongze Cheng 已提交
1003 1004 1005 1006
  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 已提交
1007

H
Hongze Cheng 已提交
1008 1009
    nRef = atomic_fetch_add_32(&pSet->pHeadF->nRef, 1);
    ASSERT(nRef > 0);
H
Hongze Cheng 已提交
1010

H
Hongze Cheng 已提交
1011 1012
    nRef = atomic_fetch_add_32(&pSet->pDataF->nRef, 1);
    ASSERT(nRef > 0);
H
Hongze Cheng 已提交
1013

H
Hongze Cheng 已提交
1014 1015
    nRef = atomic_fetch_add_32(&pSet->pLastF->nRef, 1);
    ASSERT(nRef > 0);
H
Hongze Cheng 已提交
1016

H
Hongze Cheng 已提交
1017 1018
    nRef = atomic_fetch_add_32(&pSet->pSmaF->nRef, 1);
    ASSERT(nRef > 0);
H
Hongze Cheng 已提交
1019

H
Hongze Cheng 已提交
1020
    if (taosArrayPush(pFS->aDFileSet, &fSet) == NULL) {
H
Hongze Cheng 已提交
1021
      code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1022
      goto _exit;
H
Hongze Cheng 已提交
1023 1024 1025
    }
  }

H
Hongze Cheng 已提交
1026
_exit:
H
Hongze Cheng 已提交
1027 1028 1029
  return code;
}

H
Hongze Cheng 已提交
1030 1031 1032
void tsdbFSUnref(STsdb *pTsdb, STsdbFS *pFS) {
  int32_t nRef;
  char    fname[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
1033

H
Hongze Cheng 已提交
1034 1035 1036 1037 1038 1039 1040 1041
  if (pFS->pDelFile) {
    nRef = atomic_sub_fetch_32(&pFS->pDelFile->nRef, 1);
    ASSERT(nRef >= 0);
    if (nRef == 0) {
      tsdbDelFileName(pTsdb, pFS->pDelFile, fname);
      taosRemoveFile(fname);
      taosMemoryFree(pFS->pDelFile);
    }
H
Hongze Cheng 已提交
1042 1043
  }

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

H
Hongze Cheng 已提交
1047 1048 1049 1050 1051 1052 1053 1054
    // head
    nRef = atomic_sub_fetch_32(&pSet->pHeadF->nRef, 1);
    ASSERT(nRef >= 0);
    if (nRef == 0) {
      tsdbHeadFileName(pTsdb, pSet->diskId, pSet->fid, pSet->pHeadF, fname);
      taosRemoveFile(fname);
      taosMemoryFree(pSet->pHeadF);
    }
H
Hongze Cheng 已提交
1055

H
Hongze Cheng 已提交
1056 1057 1058 1059 1060 1061 1062 1063
    // data
    nRef = atomic_sub_fetch_32(&pSet->pDataF->nRef, 1);
    ASSERT(nRef >= 0);
    if (nRef == 0) {
      tsdbDataFileName(pTsdb, pSet->diskId, pSet->fid, pSet->pDataF, fname);
      taosRemoveFile(fname);
      taosMemoryFree(pSet->pDataF);
    }
H
Hongze Cheng 已提交
1064

H
Hongze Cheng 已提交
1065 1066 1067 1068 1069 1070 1071
    // last
    nRef = atomic_sub_fetch_32(&pSet->pLastF->nRef, 1);
    ASSERT(nRef >= 0);
    if (nRef == 0) {
      tsdbLastFileName(pTsdb, pSet->diskId, pSet->fid, pSet->pLastF, fname);
      taosRemoveFile(fname);
      taosMemoryFree(pSet->pLastF);
H
Hongze Cheng 已提交
1072
    }
H
Hongze Cheng 已提交
1073 1074 1075 1076 1077 1078 1079 1080

    // sma
    nRef = atomic_sub_fetch_32(&pSet->pSmaF->nRef, 1);
    ASSERT(nRef >= 0);
    if (nRef == 0) {
      tsdbSmaFileName(pTsdb, pSet->diskId, pSet->fid, pSet->pSmaF, fname);
      taosRemoveFile(fname);
      taosMemoryFree(pSet->pSmaF);
H
Hongze Cheng 已提交
1081 1082 1083
    }
  }

H
Hongze Cheng 已提交
1084 1085
  taosArrayDestroy(pFS->aDFileSet);
}