tsdbFS.c 17.1 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 20 21 22
static int32_t tPutFSState(uint8_t *p, STsdbFSState *pState) {
  int32_t  n = 0;
  int8_t   hasDel = pState->pDelFile ? 1 : 0;
  uint32_t nDFileSet = taosArrayGetSize(pState->aDFileSet);
H
Hongze Cheng 已提交
23

H
Hongze Cheng 已提交
24 25 26 27
  // SDelFile
  n += tPutI8(p ? p + n : p, hasDel);
  if (hasDel) {
    n += tPutDelFile(p ? p + n : p, pState->pDelFile);
H
Hongze Cheng 已提交
28 29
  }

H
Hongze Cheng 已提交
30 31 32 33
  // SArray<SDFileSet>
  n += tPutU32v(p ? p + n : p, nDFileSet);
  for (uint32_t iDFileSet = 0; iDFileSet < nDFileSet; iDFileSet++) {
    n += tPutDFileSet(p ? p + n : p, (SDFileSet *)taosArrayGet(pState->aDFileSet, iDFileSet));
H
Hongze Cheng 已提交
34 35
  }

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

H
Hongze Cheng 已提交
39 40 41 42 43
static int32_t tGetFSState(uint8_t *p, STsdbFSState *pState) {
  int32_t    n = 0;
  int8_t     hasDel;
  uint32_t   nDFileSet;
  SDFileSet *pSet = &(SDFileSet){0};
H
Hongze Cheng 已提交
44

H
Hongze Cheng 已提交
45 46 47 48 49 50 51
  // SDelFile
  n += tGetI8(p + n, &hasDel);
  if (hasDel) {
    pState->pDelFile = &pState->delFile;
    n += tGetDelFile(p + n, pState->pDelFile);
  } else {
    pState->pDelFile = NULL;
H
Hongze Cheng 已提交
52 53
  }

H
Hongze Cheng 已提交
54 55 56 57 58 59
  // SArray<SDFileSet>
  taosArrayClear(pState->aDFileSet);
  n += tGetU32v(p + n, &nDFileSet);
  for (uint32_t iDFileSet = 0; iDFileSet < nDFileSet; iDFileSet++) {
    n += tGetDFileSet(p + n, pSet);
    taosArrayPush(pState->aDFileSet, pSet);
H
Hongze Cheng 已提交
60 61
  }

H
Hongze Cheng 已提交
62
  return n;
H
Hongze Cheng 已提交
63 64
}

H
Hongze Cheng 已提交
65
static int32_t tsdbGnrtCurrent(const char *fname, STsdbFSState *pState) {
H
Hongze Cheng 已提交
66 67 68
  int32_t   code = 0;
  int64_t   n;
  int64_t   size;
H
Hongze Cheng 已提交
69
  uint8_t  *pData;
H
Hongze Cheng 已提交
70 71
  TdFilePtr pFD = NULL;

H
Hongze Cheng 已提交
72 73 74 75 76 77 78 79 80 81
  // to binary
  size = tPutFSState(NULL, pState) + sizeof(TSCKSUM);
  pData = taosMemoryMalloc(size);
  if (pData == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
  n = tPutFSState(pData, pState);
  ASSERT(n + sizeof(TSCKSUM) == size);
  taosCalcChecksumAppend(0, pData, size);
H
Hongze Cheng 已提交
82

H
Hongze Cheng 已提交
83 84
  // create and write
  pFD = taosOpenFile(fname, TD_FILE_WRITE | TD_FILE_CREATE);
H
Hongze Cheng 已提交
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
  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 已提交
107
  tsdbError("tsdb gnrt current failed since %s", tstrerror(code));
H
Hongze Cheng 已提交
108 109 110 111
  if (pData) taosMemoryFree(pData);
  return code;
}

H
Hongze Cheng 已提交
112 113 114 115 116
static int32_t tsdbLoadCurrentState(STsdbFS *pFS, STsdbFSState *pState) {
  int32_t   code = 0;
  int64_t   size;
  int64_t   n;
  char      fname[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
117
  uint8_t  *pData = NULL;
H
Hongze Cheng 已提交
118 119
  TdFilePtr pFD;

H
Hongze Cheng 已提交
120 121
  snprintf(fname, TSDB_FILENAME_LEN - 1, "%s%s%s%sCURRENT", tfsGetPrimaryPath(pFS->pTsdb->pVnode->pTfs), TD_DIRSEP,
           pFS->pTsdb->path, TD_DIRSEP);
H
Hongze Cheng 已提交
122 123 124

  if (!taosCheckExistFile(fname)) {
    // create an empry CURRENT file if not exists
H
Hongze Cheng 已提交
125
    code = tsdbGnrtCurrent(fname, pState);
H
Hongze Cheng 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139
    if (code) goto _err;
  } else {
    // open the file and load
    pFD = taosOpenFile(fname, TD_FILE_READ);
    if (pFD == NULL) {
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }

    if (taosFStatFile(pFD, &size, NULL) < 0) {
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }

H
Hongze Cheng 已提交
140
    pData = taosMemoryMalloc(size);
H
Hongze Cheng 已提交
141 142 143 144 145 146 147 148 149 150 151
    if (pData == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }

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

H
Hongze Cheng 已提交
152 153 154 155 156
    if (!taosCheckChecksumWhole(pData, size)) {
      code = TSDB_CODE_FILE_CORRUPTED;
      goto _err;
    }

H
Hongze Cheng 已提交
157 158 159
    taosCloseFile(&pFD);

    // decode
H
Hongze Cheng 已提交
160
    tGetFSState(pData, pState);
H
Hongze Cheng 已提交
161 162 163 164 165 166 167 168 169 170 171
  }

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

_err:
  tsdbError("vgId:%d tsdb load current state failed since %s", TD_VID(pFS->pTsdb->pVnode), tstrerror(code));
  if (pData) taosMemoryFree(pData);
  return code;
}

H
Hongze Cheng 已提交
172
static int32_t tsdbApplyDFileSetChange(STsdbFS *pFS, SDFileSet *pFrom, SDFileSet *pTo) {
H
Hongze Cheng 已提交
173
  int32_t code = 0;
H
Hongze Cheng 已提交
174 175 176 177 178
  char    fname[TSDB_FILENAME_LEN];

  if (pFrom && pTo) {
    // head
    if (tsdbFileIsSame(pFrom, pTo, TSDB_HEAD_FILE)) {
H
Hongze Cheng 已提交
179 180
      ASSERT(pFrom->fHead.size == pTo->fHead.size);
      ASSERT(pFrom->fHead.offset == pTo->fHead.offset);
H
Hongze Cheng 已提交
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
    } else {
      tsdbDataFileName(pFS->pTsdb, pFrom, TSDB_HEAD_FILE, fname);
      taosRemoveFile(fname);
    }

    // data
    if (tsdbFileIsSame(pFrom, pTo, TSDB_DATA_FILE)) {
      if (pFrom->fData.size > pTo->fData.size) {
        code = tsdbDFileRollback(pFS->pTsdb, pTo, TSDB_DATA_FILE);
        if (code) goto _err;
      }
    } else {
      tsdbDataFileName(pFS->pTsdb, pFrom, TSDB_DATA_FILE, fname);
      taosRemoveFile(fname);
    }

    // last
    if (tsdbFileIsSame(pFrom, pTo, TSDB_LAST_FILE)) {
      if (pFrom->fLast.size > pTo->fLast.size) {
        code = tsdbDFileRollback(pFS->pTsdb, pTo, TSDB_LAST_FILE);
        if (code) goto _err;
      }
    } else {
      tsdbDataFileName(pFS->pTsdb, pFrom, TSDB_LAST_FILE, fname);
      taosRemoveFile(fname);
    }

    // sma
    if (tsdbFileIsSame(pFrom, pTo, TSDB_SMA_FILE)) {
      if (pFrom->fSma.size > pTo->fSma.size) {
        code = tsdbDFileRollback(pFS->pTsdb, pTo, TSDB_SMA_FILE);
        if (code) goto _err;
      }
    } else {
      tsdbDataFileName(pFS->pTsdb, pFrom, TSDB_SMA_FILE, fname);
      taosRemoveFile(fname);
    }
  } else if (pFrom) {
    // head
    tsdbDataFileName(pFS->pTsdb, pFrom, TSDB_HEAD_FILE, fname);
    taosRemoveFile(fname);

    // data
    tsdbDataFileName(pFS->pTsdb, pFrom, TSDB_DATA_FILE, fname);
    taosRemoveFile(fname);

    // last
    tsdbDataFileName(pFS->pTsdb, pFrom, TSDB_LAST_FILE, fname);
    taosRemoveFile(fname);

    // fsm
    tsdbDataFileName(pFS->pTsdb, pFrom, TSDB_SMA_FILE, 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));
H
Hongze Cheng 已提交
240 241 242
  return code;
}

H
Hongze Cheng 已提交
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
static int32_t tsdbApplyDelFileChange(STsdbFS *pFS, SDelFile *pFrom, SDelFile *pTo) {
  int32_t code = 0;
  char    fname[TSDB_FILENAME_LEN];

  if (pFrom && pTo) {
    if (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) {
H
Hongze Cheng 已提交
287 288
    pDFileSetFrom = (SDFileSet *)taosArrayGet(pFrom->aDFileSet, iFrom);
    pDFileSetTo = (SDFileSet *)taosArrayGet(pTo->aDFileSet, iTo);
H
Hongze Cheng 已提交
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306

    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) {
H
Hongze Cheng 已提交
307
    pDFileSetFrom = (SDFileSet *)taosArrayGet(pFrom->aDFileSet, iFrom);
H
Hongze Cheng 已提交
308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
    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;
}

H
Hongze Cheng 已提交
332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
static void tsdbFSDestroy(STsdbFS *pFS) {
  if (pFS) {
    if (pFS->nState) {
      taosArrayDestroy(pFS->nState->aDFileSet);
      taosMemoryFree(pFS->nState);
    }

    if (pFS->cState) {
      taosArrayDestroy(pFS->cState->aDFileSet);
      taosMemoryFree(pFS->cState);
    }

    taosThreadRwlockDestroy(&pFS->lock);
    taosMemoryFree(pFS);
  }
  // TODO
}

static int32_t tsdbFSCreate(STsdb *pTsdb, STsdbFS **ppFS) {
H
Hongze Cheng 已提交
351 352 353 354 355 356 357 358 359 360 361 362
  int32_t  code = 0;
  STsdbFS *pFS = NULL;

  pFS = (STsdbFS *)taosMemoryCalloc(1, sizeof(*pFS));
  if (pFS == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
  pFS->pTsdb = pTsdb;

  code = taosThreadRwlockInit(&pFS->lock, NULL);
  if (code) {
H
Hongze Cheng 已提交
363
    taosMemoryFree(pFS);
H
Hongze Cheng 已提交
364 365 366 367 368 369 370 371 372 373 374
    code = TAOS_SYSTEM_ERROR(code);
    goto _err;
  }

  pFS->inTxn = 0;

  pFS->cState = (STsdbFSState *)taosMemoryCalloc(1, sizeof(STsdbFSState));
  if (pFS->cState == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
H
Hongze Cheng 已提交
375
  pFS->cState->aDFileSet = taosArrayInit(0, sizeof(SDFileSet));
H
Hongze Cheng 已提交
376 377 378 379 380 381 382 383 384 385
  if (pFS->cState->aDFileSet == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }

  pFS->nState = (STsdbFSState *)taosMemoryCalloc(1, sizeof(STsdbFSState));
  if (pFS->nState == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }
H
Hongze Cheng 已提交
386
  pFS->nState->aDFileSet = taosArrayInit(0, sizeof(SDFileSet));
H
Hongze Cheng 已提交
387 388 389 390 391 392 393 394 395
  if (pFS->nState->aDFileSet == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }

  *ppFS = pFS;
  return code;

_err:
H
Hongze Cheng 已提交
396 397
  tsdbError("vgId:%d tsdb fs create failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
  tsdbFSDestroy(pFS);
H
Hongze Cheng 已提交
398 399 400 401
  *ppFS = NULL;
  return code;
}

H
Hongze Cheng 已提交
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 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 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
static int32_t tsdbScanAndTryFixFS(STsdbFS *pFS, int8_t deepScan) {
  int32_t   code = 0;
  STsdb    *pTsdb = pFS->pTsdb;
  STfs     *pTfs = pTsdb->pVnode->pTfs;
  int64_t   size;
  char      fname[TSDB_FILENAME_LEN];
  char      pHdr[TSDB_FHDR_SIZE];
  TdFilePtr pFD;

  // SDelFile
  if (pFS->cState->pDelFile) {
    tsdbDelFileName(pTsdb, pFS->cState->pDelFile, fname);
    if (taosStatFile(fname, &size, NULL)) {
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }

    if (size != pFS->cState->pDelFile->size) {
      code = TSDB_CODE_FILE_CORRUPTED;
      goto _err;
    }

    if (deepScan) {
      // TODO
    }
  }

  // SArray<SDFileSet>
  for (int32_t iSet = 0; iSet < taosArrayGetSize(pFS->cState->aDFileSet); iSet++) {
    SDFileSet *pDFileSet = (SDFileSet *)taosArrayGet(pFS->cState->aDFileSet, iSet);

    // head =========
    tsdbDataFileName(pTsdb, pDFileSet, TSDB_HEAD_FILE, fname);
    if (taosStatFile(fname, &size, NULL)) {
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }

    if (deepScan) {
      // TODO
    }

    // data =========
    tsdbDataFileName(pTsdb, pDFileSet, TSDB_DATA_FILE, fname);
    if (taosStatFile(fname, &size, NULL)) {
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }

    if (size < pDFileSet->fData.size) {
      code = TSDB_CODE_FILE_CORRUPTED;
      goto _err;
    } else if (size > pDFileSet->fData.size) {
      ASSERT(0);
      // need to rollback the file
    }

    if (deepScan) {
      // TODO
    }

    // last ===========
    tsdbDataFileName(pTsdb, pDFileSet, TSDB_LAST_FILE, fname);
    if (taosStatFile(fname, &size, NULL)) {
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }

    if (size < pDFileSet->fLast.size) {
      code = TSDB_CODE_FILE_CORRUPTED;
      goto _err;
    } else if (size > pDFileSet->fLast.size) {
      ASSERT(0);
      // need to rollback the file
    }

    if (deepScan) {
      // TODO
    }

    // sma =============
    tsdbDataFileName(pTsdb, pDFileSet, TSDB_SMA_FILE, fname);
    if (taosStatFile(fname, &size, NULL)) {
      code = TAOS_SYSTEM_ERROR(errno);
      goto _err;
    }

    if (size < pDFileSet->fSma.size) {
      code = TSDB_CODE_FILE_CORRUPTED;
      goto _err;
    } else if (size > pDFileSet->fSma.size) {
      ASSERT(0);
      // need to rollback the file
    }

    if (deepScan) {
      // TODO
    }
  }

  // remove those invalid files (todo)
#if 0
  STfsDir        *tdir;
  const STfsFile *pf;

  tdir = tfsOpendir(pTfs, pTsdb->path);
  if (tdir == NULL) {
    code = TAOS_SYSTEM_ERROR(errno);
    goto _err;
  }

  while ((pf = tfsReaddir(tdir))) {
    tfsBasename(pf, fname);
  }

  tfsClosedir(tdir);
#endif

  return code;

_err:
  tsdbError("vgId:%d tsdb can and try fix fs failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
  return code;
}

H
Hongze Cheng 已提交
527 528 529 530 531 532 533 534 535 536
static 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 已提交
537 538
// EXPOSED APIS ====================================================================================
int32_t tsdbFSOpen(STsdb *pTsdb, STsdbFS **ppFS) {
H
Hongze Cheng 已提交
539
  int32_t code = 0;
H
Hongze Cheng 已提交
540

H
Hongze Cheng 已提交
541 542 543
  // create handle
  code = tsdbFSCreate(pTsdb, ppFS);
  if (code) goto _err;
H
Hongze Cheng 已提交
544

H
Hongze Cheng 已提交
545 546 547 548 549 550 551 552 553 554 555 556
  // load current state
  code = tsdbLoadCurrentState(*ppFS, (*ppFS)->cState);
  if (code) {
    tsdbFSDestroy(*ppFS);
    goto _err;
  }

  // scan and fix FS
  code = tsdbScanAndTryFixFS(*ppFS, 0);
  if (code) {
    tsdbFSDestroy(*ppFS);
    goto _err;
H
Hongze Cheng 已提交
557 558 559 560 561
  }

  return code;

_err:
H
Hongze Cheng 已提交
562 563 564 565 566 567 568 569
  *ppFS = NULL;
  tsdbError("vgId:%d tsdb fs open failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
  return code;
}

int32_t tsdbFSClose(STsdbFS *pFS) {
  int32_t code = 0;
  tsdbFSDestroy(pFS);
H
Hongze Cheng 已提交
570 571 572
  return code;
}

H
Hongze Cheng 已提交
573
int32_t tsdbFSBegin(STsdbFS *pFS) {
H
Hongze Cheng 已提交
574
  int32_t code = 0;
H
Hongze Cheng 已提交
575 576 577

  ASSERT(!pFS->inTxn);

H
Hongze Cheng 已提交
578 579 580 581 582 583
  // SDelFile
  pFS->nState->pDelFile = NULL;
  if (pFS->cState->pDelFile) {
    pFS->nState->delFile = pFS->cState->delFile;
    pFS->nState->pDelFile = &pFS->nState->delFile;
  }
H
Hongze Cheng 已提交
584

H
Hongze Cheng 已提交
585
  // SArray<aDFileSet>
H
Hongze Cheng 已提交
586
  taosArrayClear(pFS->nState->aDFileSet);
H
Hongze Cheng 已提交
587 588
  for (int32_t iSet = 0; iSet < taosArrayGetSize(pFS->cState->aDFileSet); iSet++) {
    SDFileSet *pDFileSet = (SDFileSet *)taosArrayGet(pFS->cState->aDFileSet, iSet);
H
Hongze Cheng 已提交
589

H
Hongze Cheng 已提交
590
    if (taosArrayPush(pFS->nState->aDFileSet, pDFileSet) == NULL) {
H
Hongze Cheng 已提交
591 592 593 594 595
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _err;
    }
  }

H
Hongze Cheng 已提交
596
  pFS->inTxn = 1;
H
Hongze Cheng 已提交
597 598 599 600 601 602 603 604 605 606
  return code;

_err:
  tsdbError("vgId:%d tsdb fs begin failed since %s", TD_VID(pFS->pTsdb->pVnode), tstrerror(code));
  return code;
}

int32_t tsdbFSCommit(STsdbFS *pFS) {
  int32_t       code = 0;
  STsdbFSState *pState = pFS->nState;
H
Hongze Cheng 已提交
607 608
  char          tfname[TSDB_FILENAME_LEN];
  char          fname[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
609 610 611 612 613

  // need lock (todo)
  pFS->nState = pFS->cState;
  pFS->cState = pState;

H
Hongze Cheng 已提交
614 615 616 617 618 619 620
  snprintf(tfname, TSDB_FILENAME_LEN - 1, "%s%s%s%sCURRENT.t", tfsGetPrimaryPath(pFS->pTsdb->pVnode->pTfs), TD_DIRSEP,
           pFS->pTsdb->path, TD_DIRSEP);
  snprintf(fname, TSDB_FILENAME_LEN - 1, "%s%s%s%sCURRENT", tfsGetPrimaryPath(pFS->pTsdb->pVnode->pTfs), TD_DIRSEP,
           pFS->pTsdb->path, TD_DIRSEP);

  // gnrt CURRENT.t
  code = tsdbGnrtCurrent(tfname, pFS->cState);
H
Hongze Cheng 已提交
621 622
  if (code) goto _err;

H
Hongze Cheng 已提交
623 624 625 626 627 628 629
  // rename
  code = taosRenameFile(tfname, fname);
  if (code) {
    code = TAOS_SYSTEM_ERROR(code);
    goto _err;
  }

H
Hongze Cheng 已提交
630 631 632 633 634 635 636 637 638 639
  // apply commit on disk
  code = tsdbFSApplyDiskChange(pFS, pFS->nState, pFS->cState);
  if (code) goto _err;

  pFS->inTxn = 0;

  return code;

_err:
  tsdbError("vgId:%d tsdb fs commit failed since %s", TD_VID(pFS->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
640 641 642
  return code;
}

H
Hongze Cheng 已提交
643
int32_t tsdbFSRollback(STsdbFS *pFS) {
H
Hongze Cheng 已提交
644
  int32_t code = 0;
H
Hongze Cheng 已提交
645 646 647 648 649 650 651 652 653 654

  code = tsdbFSApplyDiskChange(pFS, pFS->nState, pFS->cState);
  if (code) goto _err;

  pFS->inTxn = 0;

  return code;

_err:
  tsdbError("vgId:%d tsdb fs rollback failed since %s", TD_VID(pFS->pTsdb->pVnode), tstrerror(code));
H
Hongze Cheng 已提交
655 656
  return code;
}
H
Hongze Cheng 已提交
657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694

int32_t tsdbFSStateUpsertDelFile(STsdbFSState *pState, SDelFile *pDelFile) {
  int32_t code = 0;
  pState->delFile = *pDelFile;
  pState->pDelFile = &pState->delFile;
  return code;
}

int32_t tsdbFSStateUpsertDFileSet(STsdbFSState *pState, SDFileSet *pSet) {
  int32_t code = 0;
  int32_t idx = taosArraySearchIdx(pState->aDFileSet, pSet, tDFileSetCmprFn, TD_GE);

  if (idx < 0) {
    if (taosArrayPush(pState->aDFileSet, pSet) == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      goto _exit;
    }
  } else {
    SDFileSet *tDFileSet = (SDFileSet *)taosArrayGet(pState->aDFileSet, idx);
    int32_t    c = tDFileSetCmprFn(pSet, tDFileSet);
    if (c == 0) {
      taosArraySet(pState->aDFileSet, idx, pSet);
    } else {
      if (taosArrayInsert(pState->aDFileSet, idx, pSet) == NULL) {
        code = TSDB_CODE_OUT_OF_MEMORY;
        goto _exit;
      }
    }
  }

_exit:
  return code;
}

SDelFile *tsdbFSStateGetDelFile(STsdbFSState *pState) { return pState->pDelFile; }

SDFileSet *tsdbFSStateGetDFileSet(STsdbFSState *pState, int32_t fid) {
  return (SDFileSet *)taosArraySearch(pState->aDFileSet, &(SDFileSet){.fid = fid}, tDFileSetCmprFn, TD_EQ);
695
}