tsdbFS.c 31.3 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:
S
Shengliang Guan 已提交
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
// 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);
//     }

H
Hongze Cheng 已提交
113
//     // sst
H
Hongze Cheng 已提交
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
//     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);

H
Hongze Cheng 已提交
143
//     // sst
H
Hongze Cheng 已提交
144 145 146 147 148 149 150 151 152 153 154
//     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:
S
Shengliang Guan 已提交
155
//   tsdbError("vgId:%d, tsdb apply disk file set change failed since %s", TD_VID(pFS->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
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
//   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:
S
Shengliang Guan 已提交
184
//   tsdbError("vgId:%d, tsdb apply del file change failed since %s", TD_VID(pFS->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
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
//   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:
S
Shengliang Guan 已提交
244
//   tsdbError("vgId:%d, tsdb fs apply disk change failed sicne %s", TD_VID(pFS->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
245 246 247 248 249 250 251
//   return code;
// }

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

H
Hongze Cheng 已提交
253 254 255 256 257
  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 已提交
258 259
    for (int32_t iSst = 0; iSst < pSet->nSstF; iSst++) {
      taosMemoryFree(pSet->aSstF[iSst]);
H
Hongze Cheng 已提交
260
    }
H
Hongze Cheng 已提交
261
  }
H
Hongze Cheng 已提交
262

H
Hongze Cheng 已提交
263 264 265 266 267 268 269 270 271 272 273 274
  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 已提交
275 276 277 278
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }

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

H
Hongze Cheng 已提交
285 286 287 288 289 290 291 292 293 294 295 296
  // 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 已提交
297 298 299
      goto _err;
    }

H
Hongze Cheng 已提交
300 301 302
    // data =========
    tsdbDataFileName(pTsdb, pSet->diskId, pSet->fid, pSet->pDataF, fname);
    if (taosStatFile(fname, &size, NULL)) {
H
Hongze Cheng 已提交
303 304 305
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }
H
Hongze Cheng 已提交
306 307 308 309 310 311 312
    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 已提交
313

H
Hongze Cheng 已提交
314
    // sst ===========
H
Hongze Cheng 已提交
315
    tsdbSstFileName(pTsdb, pSet->diskId, pSet->fid, pSet->aSstF[0], fname);
H
Hongze Cheng 已提交
316 317 318 319
    if (taosStatFile(fname, &size, NULL)) {
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }
H
Hongze Cheng 已提交
320
    if (size != pSet->aSstF[0]->size) {
H
Hongze Cheng 已提交
321 322 323 324
      code = TSDB_CODE_FILE_CORRUPTED;
      goto _err;
    }

H
Hongze Cheng 已提交
325 326 327 328 329 330 331 332 333 334 335 336 337 338
    // 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 已提交
339

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

  return code;

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

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

H
Hongze Cheng 已提交
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 386
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++) {
H
Hongze Cheng 已提交
387
    SDFileSet fSet = {0};
H
Hongze Cheng 已提交
388

H
Hongze Cheng 已提交
389 390
    int32_t nt = tGetDFileSet(pData + n, &fSet);
    if (nt < 0) {
H
Hongze Cheng 已提交
391 392
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
H
Hongze Cheng 已提交
393 394
    }

H
Hongze Cheng 已提交
395
    n += nt;
H
Hongze Cheng 已提交
396

H
Hongze Cheng 已提交
397 398 399 400
    if (taosArrayPush(pTsdb->fs.aDFileSet, &fSet) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
H
Hongze Cheng 已提交
401 402
  }

H
Hongze Cheng 已提交
403
  ASSERT(n + sizeof(TSCKSUM) == nData);
H
Hongze Cheng 已提交
404 405 406
  return code;

_err:
H
Hongze Cheng 已提交
407 408 409
  return code;
}

H
Hongze Cheng 已提交
410 411
// EXPOSED APIS ====================================================================================
int32_t tsdbFSOpen(STsdb *pTsdb) {
H
Hongze Cheng 已提交
412 413
  int32_t code = 0;

H
Hongze Cheng 已提交
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
  // 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 已提交
438
    }
H
Hongze Cheng 已提交
439 440 441

    int64_t size;
    if (taosFStatFile(pFD, &size, NULL) < 0) {
H
Hongze Cheng 已提交
442
      code = TAOS_SYSTEM_ERROR(errno);
H
Hongze Cheng 已提交
443
      taosCloseFile(&pFD);
H
Hongze Cheng 已提交
444 445
      goto _err;
    }
H
Hongze Cheng 已提交
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478

    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 已提交
479 480
  }

H
Hongze Cheng 已提交
481 482 483 484
  // scan and fix FS
  code = tsdbScanAndTryFixFS(pTsdb);
  if (code) goto _err;

H
Hongze Cheng 已提交
485 486 487
  return code;

_err:
S
Shengliang Guan 已提交
488
  tsdbError("vgId:%d, tsdb fs open failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
489 490 491
  return code;
}

H
Hongze Cheng 已提交
492 493
int32_t tsdbFSClose(STsdb *pTsdb) {
  int32_t code = 0;
H
Hongze Cheng 已提交
494

H
Hongze Cheng 已提交
495 496 497 498
  if (pTsdb->fs.pDelFile) {
    ASSERT(pTsdb->fs.pDelFile->nRef == 1);
    taosMemoryFree(pTsdb->fs.pDelFile);
  }
H
Hongze Cheng 已提交
499

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

H
Hongze Cheng 已提交
503 504 505
    // head
    ASSERT(pSet->pHeadF->nRef == 1);
    taosMemoryFree(pSet->pHeadF);
H
Hongze Cheng 已提交
506

H
Hongze Cheng 已提交
507 508 509
    // data
    ASSERT(pSet->pDataF->nRef == 1);
    taosMemoryFree(pSet->pDataF);
H
Hongze Cheng 已提交
510

H
Hongze Cheng 已提交
511 512 513
    // sma
    ASSERT(pSet->pSmaF->nRef == 1);
    taosMemoryFree(pSet->pSmaF);
H
Hongze Cheng 已提交
514 515 516 517 518 519

    // sst
    for (int32_t iSst = 0; iSst < pSet->nSstF; iSst++) {
      ASSERT(pSet->aSstF[iSst]->nRef == 1);
      taosMemoryFree(pSet->aSstF[iSst]);
    }
H
Hongze Cheng 已提交
520 521
  }

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

H
Hongze Cheng 已提交
524 525 526 527 528 529 530 531 532 533 534
  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 已提交
535 536
  }

H
Hongze Cheng 已提交
537 538 539 540 541 542
  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 已提交
543

H
Hongze Cheng 已提交
544
    *pFS->pDelFile = *pTsdb->fs.pDelFile;
H
Hongze Cheng 已提交
545
  }
H
Hongze Cheng 已提交
546 547 548

  for (int32_t iSet = 0; iSet < taosArrayGetSize(pTsdb->fs.aDFileSet); iSet++) {
    SDFileSet *pSet = (SDFileSet *)taosArrayGet(pTsdb->fs.aDFileSet, iSet);
H
Hongze Cheng 已提交
549
    SDFileSet  fSet = {.diskId = pSet->diskId, .fid = pSet->fid};
H
Hongze Cheng 已提交
550 551 552 553 554 555 556

    // head
    fSet.pHeadF = (SHeadFile *)taosMemoryMalloc(sizeof(SHeadFile));
    if (fSet.pHeadF == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _exit;
    }
H
Hongze Cheng 已提交
557
    *fSet.pHeadF = *pSet->pHeadF;
H
Hongze Cheng 已提交
558 559 560 561 562 563 564

    // data
    fSet.pDataF = (SDataFile *)taosMemoryMalloc(sizeof(SDataFile));
    if (fSet.pDataF == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _exit;
    }
H
Hongze Cheng 已提交
565
    *fSet.pDataF = *pSet->pDataF;
H
Hongze Cheng 已提交
566

H
Hongze Cheng 已提交
567
    // sma
H
Hongze Cheng 已提交
568 569 570 571 572
    fSet.pSmaF = (SSmaFile *)taosMemoryMalloc(sizeof(SSmaFile));
    if (fSet.pSmaF == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _exit;
    }
H
Hongze Cheng 已提交
573
    *fSet.pSmaF = *pSet->pSmaF;
H
Hongze Cheng 已提交
574

H
Hongze Cheng 已提交
575
    // sst
H
Hongze Cheng 已提交
576 577 578
    for (fSet.nSstF = 0; fSet.nSstF < pSet->nSstF; fSet.nSstF++) {
      fSet.aSstF[fSet.nSstF] = (SSstFile *)taosMemoryMalloc(sizeof(SSstFile));
      if (fSet.aSstF[fSet.nSstF] == NULL) {
H
Hongze Cheng 已提交
579 580 581
        code = TSDB_CODE_OUT_OF_MEMORY;
        goto _exit;
      }
H
Hongze Cheng 已提交
582
      *fSet.aSstF[fSet.nSstF] = *pSet->aSstF[fSet.nSstF];
H
Hongze Cheng 已提交
583
    }
H
Hongze Cheng 已提交
584 585 586 587 588

    if (taosArrayPush(pFS->aDFileSet, &fSet) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _exit;
    }
H
Hongze Cheng 已提交
589 590 591 592 593 594 595 596 597 598
  }

_exit:
  return code;
}

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

  ASSERT(0);
H
Hongze Cheng 已提交
599 600 601 602 603 604 605

  return code;

_err:
  return code;
}

H
Hongze Cheng 已提交
606 607
int32_t tsdbFSUpsertDelFile(STsdbFS *pFS, SDelFile *pDelFile) {
  int32_t code = 0;
H
Hongze Cheng 已提交
608

H
Hongze Cheng 已提交
609 610 611 612 613
  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 已提交
614 615
    }
  }
H
Hongze Cheng 已提交
616 617 618 619
  *pFS->pDelFile = *pDelFile;

_exit:
  return code;
H
Hongze Cheng 已提交
620 621
}

H
Hongze Cheng 已提交
622 623 624 625 626 627 628 629 630 631 632 633 634
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 已提交
635
      // sst
H
Hongze Cheng 已提交
636 637
      if (pSet->nSstF > pDFileSet->nSstF) {
        ASSERT(pSet->nSstF == pDFileSet->nSstF + 1);
H
Hongze Cheng 已提交
638

H
Hongze Cheng 已提交
639 640
        pDFileSet->aSstF[pDFileSet->nSstF] = (SSstFile *)taosMemoryMalloc(sizeof(SSstFile));
        if (pDFileSet->aSstF[pDFileSet->nSstF] == NULL) {
H
Hongze Cheng 已提交
641 642 643
          code = TSDB_CODE_OUT_OF_MEMORY;
          goto _exit;
        }
H
Hongze Cheng 已提交
644 645 646 647
        *pDFileSet->aSstF[pDFileSet->nSstF] = *pSet->aSstF[pSet->nSstF - 1];
        pDFileSet->nSstF++;
      } else if (pSet->nSstF < pDFileSet->nSstF) {
        ASSERT(pSet->nSstF == 1);
H
Hongze Cheng 已提交
648 649
        for (int32_t iSst = 1; iSst < pDFileSet->nSstF; iSst++) {
          taosMemoryFree(pDFileSet->aSstF[iSst]);
H
Hongze Cheng 已提交
650 651
        }

H
Hongze Cheng 已提交
652 653
        *pDFileSet->aSstF[0] = *pSet->aSstF[0];
        pDFileSet->nSstF = 1;
H
Hongze Cheng 已提交
654
      } else {
H
Hongze Cheng 已提交
655 656
        for (int32_t iSst = 0; iSst < pSet->nSstF; iSst++) {
          *pDFileSet->aSstF[iSst] = *pSet->aSstF[iSst];
H
Hongze Cheng 已提交
657
        }
H
Hongze Cheng 已提交
658
      }
H
Hongze Cheng 已提交
659

H
Hongze Cheng 已提交
660 661 662 663
      goto _exit;
    }
  }

H
Hongze Cheng 已提交
664 665
  ASSERT(pSet->nSstF == 1);
  SDFileSet fSet = {.diskId = pSet->diskId, .fid = pSet->fid, .nSstF = 1};
H
Hongze Cheng 已提交
666 667 668 669

  // head
  fSet.pHeadF = (SHeadFile *)taosMemoryMalloc(sizeof(SHeadFile));
  if (fSet.pHeadF == NULL) {
H
Hongze Cheng 已提交
670
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
671
    goto _exit;
H
Hongze Cheng 已提交
672
  }
H
Hongze Cheng 已提交
673
  *fSet.pHeadF = *pSet->pHeadF;
H
Hongze Cheng 已提交
674

H
Hongze Cheng 已提交
675 676 677
  // data
  fSet.pDataF = (SDataFile *)taosMemoryMalloc(sizeof(SDataFile));
  if (fSet.pDataF == NULL) {
H
Hongze Cheng 已提交
678
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
679
    goto _exit;
H
Hongze Cheng 已提交
680
  }
H
Hongze Cheng 已提交
681 682
  *fSet.pDataF = *pSet->pDataF;

H
Hongze Cheng 已提交
683
  // sma
H
Hongze Cheng 已提交
684 685
  fSet.pSmaF = (SSmaFile *)taosMemoryMalloc(sizeof(SSmaFile));
  if (fSet.pSmaF == NULL) {
H
Hongze Cheng 已提交
686
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
687
    goto _exit;
H
Hongze Cheng 已提交
688
  }
H
Hongze Cheng 已提交
689 690
  *fSet.pSmaF = *pSet->pSmaF;

H
Hongze Cheng 已提交
691
  // sst
H
Hongze Cheng 已提交
692 693
  fSet.aSstF[0] = (SSstFile *)taosMemoryMalloc(sizeof(SSstFile));
  if (fSet.aSstF[0] == NULL) {
H
Hongze Cheng 已提交
694 695 696
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }
H
Hongze Cheng 已提交
697
  *fSet.aSstF[0] = *pSet->aSstF[0];
H
Hongze Cheng 已提交
698

H
Hongze Cheng 已提交
699
  if (taosArrayInsert(pFS->aDFileSet, idx, &fSet) == NULL) {
H
Hongze Cheng 已提交
700
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
    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 已提交
726 727 728 729 730 731
    goto _err;
  }

  return code;

_err:
S
Shengliang Guan 已提交
732
  tsdbError("vgId:%d, tsdb fs commit phase 1 failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
733 734 735
  return code;
}

H
Hongze Cheng 已提交
736 737 738 739
int32_t tsdbFSCommit2(STsdb *pTsdb, STsdbFS *pFSNew) {
  int32_t code = 0;
  int32_t nRef;
  char    fname[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
740

H
Hongze Cheng 已提交
741 742 743
  // del
  if (pFSNew->pDelFile) {
    SDelFile *pDelFile = pTsdb->fs.pDelFile;
H
Hongze Cheng 已提交
744

H
Hongze Cheng 已提交
745 746 747 748 749 750
    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 已提交
751

H
Hongze Cheng 已提交
752 753 754 755 756 757 758 759 760 761 762
      *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 已提交
763
    }
H
Hongze Cheng 已提交
764 765
  } else {
    ASSERT(pTsdb->fs.pDelFile == NULL);
H
Hongze Cheng 已提交
766 767
  }

H
Hongze Cheng 已提交
768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794
  // 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 已提交
795

H
Hongze Cheng 已提交
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818
  _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 已提交
819 820
    }

H
Hongze Cheng 已提交
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840
    // 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 已提交
841 842
    }

H
Hongze Cheng 已提交
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
    // 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 已提交
863 864
    }

H
Hongze Cheng 已提交
865
    // sst
H
Hongze Cheng 已提交
866
    if (sameDisk) {
H
Hongze Cheng 已提交
867 868 869 870
      if (pSetNew->nSstF > pSetOld->nSstF) {
        ASSERT(pSetNew->nSstF = pSetOld->nSstF + 1);
        pSetOld->aSstF[pSetOld->nSstF] = (SSstFile *)taosMemoryMalloc(sizeof(SSstFile));
        if (pSetOld->aSstF[pSetOld->nSstF] == NULL) {
H
Hongze Cheng 已提交
871 872 873
          code = TSDB_CODE_OUT_OF_MEMORY;
          goto _err;
        }
H
Hongze Cheng 已提交
874 875 876 877 878
        *pSetOld->aSstF[pSetOld->nSstF] = *pSetNew->aSstF[pSetOld->nSstF];
        pSetOld->aSstF[pSetOld->nSstF]->nRef = 1;
        pSetOld->nSstF++;
      } else if (pSetNew->nSstF < pSetOld->nSstF) {
        ASSERT(pSetNew->nSstF == 1);
H
Hongze Cheng 已提交
879 880
        for (int32_t iSst = 0; iSst < pSetOld->nSstF; iSst++) {
          SSstFile *pSstFile = pSetOld->aSstF[iSst];
H
Hongze Cheng 已提交
881
          nRef = atomic_sub_fetch_32(&pSstFile->nRef, 1);
H
Hongze Cheng 已提交
882
          if (nRef == 0) {
H
Hongze Cheng 已提交
883
            tsdbSstFileName(pTsdb, pSetOld->diskId, pSetOld->fid, pSstFile, fname);
H
Hongze Cheng 已提交
884
            taosRemoveFile(fname);
H
Hongze Cheng 已提交
885
            taosMemoryFree(pSstFile);
H
Hongze Cheng 已提交
886
          }
H
Hongze Cheng 已提交
887
          pSetOld->aSstF[iSst] = NULL;
H
Hongze Cheng 已提交
888
        }
H
Hongze Cheng 已提交
889

H
Hongze Cheng 已提交
890 891 892
        pSetOld->nSstF = 1;
        pSetOld->aSstF[0] = (SSstFile *)taosMemoryMalloc(sizeof(SSstFile));
        if (pSetOld->aSstF[0] == NULL) {
H
Hongze Cheng 已提交
893 894 895
          code = TSDB_CODE_OUT_OF_MEMORY;
          goto _err;
        }
H
Hongze Cheng 已提交
896 897
        *pSetOld->aSstF[0] = *pSetNew->aSstF[0];
        pSetOld->aSstF[0]->nRef = 1;
H
Hongze Cheng 已提交
898
      } else {
H
Hongze Cheng 已提交
899 900
        for (int32_t iSst = 0; iSst < pSetOld->nSstF; iSst++) {
          SSstFile *pSstFile = pSetOld->aSstF[iSst];
H
Hongze Cheng 已提交
901
          nRef = atomic_sub_fetch_32(&pSstFile->nRef, 1);
H
Hongze Cheng 已提交
902
          if (nRef == 0) {
H
Hongze Cheng 已提交
903
            tsdbSstFileName(pTsdb, pSetOld->diskId, pSetOld->fid, pSstFile, fname);
H
Hongze Cheng 已提交
904
            taosRemoveFile(fname);
H
Hongze Cheng 已提交
905
            taosMemoryFree(pSstFile);
H
Hongze Cheng 已提交
906 907
          }

H
Hongze Cheng 已提交
908 909
          pSetOld->aSstF[iSst] = (SSstFile *)taosMemoryMalloc(sizeof(SSstFile));
          if (pSetOld->aSstF[iSst] == NULL) {
H
Hongze Cheng 已提交
910 911 912
            code = TSDB_CODE_OUT_OF_MEMORY;
            goto _err;
          }
H
Hongze Cheng 已提交
913 914
          *pSetOld->aSstF[iSst] = *pSetNew->aSstF[iSst];
          pSetOld->aSstF[iSst]->nRef = 1;
H
Hongze Cheng 已提交
915
        }
H
Hongze Cheng 已提交
916 917
      }
    } else {
H
Hongze Cheng 已提交
918
      ASSERT(pSetOld->nSstF == pSetNew->nSstF);
H
Hongze Cheng 已提交
919 920
      for (int32_t iSst = 0; iSst < pSetOld->nSstF; iSst++) {
        SSstFile *pSstFile = pSetOld->aSstF[iSst];
H
Hongze Cheng 已提交
921
        nRef = atomic_sub_fetch_32(&pSstFile->nRef, 1);
H
Hongze Cheng 已提交
922
        if (nRef == 0) {
H
Hongze Cheng 已提交
923
          tsdbSstFileName(pTsdb, pSetOld->diskId, pSetOld->fid, pSstFile, fname);
H
Hongze Cheng 已提交
924
          taosRemoveFile(fname);
H
Hongze Cheng 已提交
925
          taosMemoryFree(pSstFile);
H
Hongze Cheng 已提交
926 927
        }

H
Hongze Cheng 已提交
928 929
        pSetOld->aSstF[iSst] = (SSstFile *)taosMemoryMalloc(sizeof(SSstFile));
        if (pSetOld->aSstF[iSst] == NULL) {
H
Hongze Cheng 已提交
930 931 932
          code = TSDB_CODE_OUT_OF_MEMORY;
          goto _err;
        }
H
Hongze Cheng 已提交
933 934
        *pSetOld->aSstF[iSst] = *pSetNew->aSstF[iSst];
        pSetOld->aSstF[iSst]->nRef = 1;
H
Hongze Cheng 已提交
935
      }
H
Hongze Cheng 已提交
936 937
    }

H
Hongze Cheng 已提交
938 939
    if (!sameDisk) {
      pSetOld->diskId = pSetNew->diskId;
H
Hongze Cheng 已提交
940 941
    }

H
Hongze Cheng 已提交
942 943 944 945 946 947 948 949 950 951
    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 已提交
952 953
    }

H
Hongze Cheng 已提交
954 955 956 957 958
    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 已提交
959 960
    }

H
Hongze Cheng 已提交
961 962 963 964 965
    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 已提交
966 967
    }

H
Hongze Cheng 已提交
968 969
    for (int8_t iSst = 0; iSst < pSetOld->nSstF; iSst++) {
      nRef = atomic_sub_fetch_32(&pSetOld->aSstF[iSst]->nRef, 1);
H
Hongze Cheng 已提交
970
      if (nRef == 0) {
H
Hongze Cheng 已提交
971
        tsdbSstFileName(pTsdb, pSetOld->diskId, pSetOld->fid, pSetOld->aSstF[iSst], fname);
H
Hongze Cheng 已提交
972
        taosRemoveFile(fname);
H
Hongze Cheng 已提交
973
        taosMemoryFree(pSetOld->aSstF[iSst]);
H
Hongze Cheng 已提交
974 975 976
      }
    }

H
Hongze Cheng 已提交
977 978 979 980
    taosArrayRemove(pTsdb->fs.aDFileSet, iOld);
    continue;

  _add_new:
H
Hongze Cheng 已提交
981
    fSet = (SDFileSet){.diskId = pSetNew->diskId, .fid = pSetNew->fid, .nSstF = 1};
H
Hongze Cheng 已提交
982 983 984 985 986

    // head
    fSet.pHeadF = (SHeadFile *)taosMemoryMalloc(sizeof(SHeadFile));
    if (fSet.pHeadF == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
987 988
      goto _err;
    }
H
Hongze Cheng 已提交
989 990
    *fSet.pHeadF = *pSetNew->pHeadF;
    fSet.pHeadF->nRef = 1;
H
Hongze Cheng 已提交
991

H
Hongze Cheng 已提交
992 993 994 995 996
    // data
    fSet.pDataF = (SDataFile *)taosMemoryMalloc(sizeof(SDataFile));
    if (fSet.pDataF == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
H
Hongze Cheng 已提交
997
    }
H
Hongze Cheng 已提交
998 999
    *fSet.pDataF = *pSetNew->pDataF;
    fSet.pDataF->nRef = 1;
H
Hongze Cheng 已提交
1000

H
Hongze Cheng 已提交
1001 1002 1003 1004 1005 1006 1007 1008
    // 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 已提交
1009

H
Hongze Cheng 已提交
1010
    // sst
H
Hongze Cheng 已提交
1011 1012 1013
    ASSERT(pSetNew->nSstF == 1);
    fSet.aSstF[0] = (SSstFile *)taosMemoryMalloc(sizeof(SSstFile));
    if (fSet.aSstF[0] == NULL) {
H
Hongze Cheng 已提交
1014 1015 1016
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
H
Hongze Cheng 已提交
1017 1018
    *fSet.aSstF[0] = *pSetNew->aSstF[0];
    fSet.aSstF[0]->nRef = 1;
H
Hongze Cheng 已提交
1019

H
Hongze Cheng 已提交
1020 1021 1022 1023 1024 1025 1026
    if (taosArrayInsert(pTsdb->fs.aDFileSet, iOld, &fSet) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
    iOld++;
    iNew++;
    continue;
H
Hongze Cheng 已提交
1027 1028 1029 1030 1031
  }

  return code;

_err:
S
Shengliang Guan 已提交
1032
  tsdbError("vgId:%d, tsdb fs commit phase 2 failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
1033 1034 1035
  return code;
}

H
Hongze Cheng 已提交
1036
int32_t tsdbFSRef(STsdb *pTsdb, STsdbFS *pFS) {
H
Hongze Cheng 已提交
1037
  int32_t code = 0;
H
Hongze Cheng 已提交
1038
  int32_t nRef;
H
Hongze Cheng 已提交
1039

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

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

H
Hongze Cheng 已提交
1052 1053 1054 1055
  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 已提交
1056

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

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

H
Hongze Cheng 已提交
1063 1064
    nRef = atomic_fetch_add_32(&pSet->pSmaF->nRef, 1);
    ASSERT(nRef > 0);
H
Hongze Cheng 已提交
1065

H
Hongze Cheng 已提交
1066 1067
    for (int32_t iSst = 0; iSst < pSet->nSstF; iSst++) {
      nRef = atomic_fetch_add_32(&pSet->aSstF[iSst]->nRef, 1);
H
Hongze Cheng 已提交
1068 1069 1070
      ASSERT(nRef > 0);
    }

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

H
Hongze Cheng 已提交
1077
_exit:
H
Hongze Cheng 已提交
1078 1079 1080
  return code;
}

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

H
Hongze Cheng 已提交
1085 1086 1087 1088 1089 1090 1091 1092
  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 已提交
1093 1094
  }

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

H
Hongze Cheng 已提交
1098 1099 1100 1101 1102 1103 1104 1105
    // 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 已提交
1106

H
Hongze Cheng 已提交
1107 1108 1109 1110 1111 1112 1113 1114
    // 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 已提交
1115

H
Hongze Cheng 已提交
1116 1117 1118 1119 1120 1121 1122
    // 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 已提交
1123
    }
H
Hongze Cheng 已提交
1124

H
Hongze Cheng 已提交
1125
    // sst
H
Hongze Cheng 已提交
1126 1127
    for (int32_t iSst = 0; iSst < pSet->nSstF; iSst++) {
      nRef = atomic_sub_fetch_32(&pSet->aSstF[iSst]->nRef, 1);
H
Hongze Cheng 已提交
1128 1129
      ASSERT(nRef >= 0);
      if (nRef == 0) {
H
Hongze Cheng 已提交
1130
        tsdbSstFileName(pTsdb, pSet->diskId, pSet->fid, pSet->aSstF[iSst], fname);
H
Hongze Cheng 已提交
1131
        taosRemoveFile(fname);
H
Hongze Cheng 已提交
1132
        taosMemoryFree(pSet->aSstF[iSst]);
H
Hongze Cheng 已提交
1133 1134 1135
        /* code */
      }
    }
H
Hongze Cheng 已提交
1136 1137
  }

H
Hongze Cheng 已提交
1138 1139
  taosArrayDestroy(pFS->aDFileSet);
}