tsdbFS.c 31.6 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

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

H
Hongze Cheng 已提交
18
// =================================================================================================
H
Hongze Cheng 已提交
19
static int32_t tsdbFSToBinary(uint8_t *p, STsdbFS *pFS) {
H
Hongze Cheng 已提交
20
  int32_t  n = 0;
H
Hongze Cheng 已提交
21 22
  int8_t   hasDel = pFS->pDelFile ? 1 : 0;
  uint32_t nSet = taosArrayGetSize(pFS->aDFileSet);
H
Hongze Cheng 已提交
23

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

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

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

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

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

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

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

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

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

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

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

  ASSERT(n + sizeof(TSCKSUM) == nData);

_exit:
  return code;
}

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

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

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

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

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

  taosCloseFile(&pFD);

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

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

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

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

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

H
Hongze Cheng 已提交
154 155 156 157 158
  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 已提交
159 160
    for (int32_t iStt = 0; iStt < pSet->nSttF; iStt++) {
      taosMemoryFree(pSet->aSttF[iStt]);
H
Hongze Cheng 已提交
161
    }
H
Hongze Cheng 已提交
162
  }
H
Hongze Cheng 已提交
163

164
  taosArrayDestroy(pFS->aDFileSet);
H
Hongze Cheng 已提交
165 166 167 168
}

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

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

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

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

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

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

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

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

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

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

H
Hongze Cheng 已提交
260 261 262 263 264 265 266 267 268
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 已提交
269

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

H
Hongze Cheng 已提交
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
static int32_t tsdbLoadFSFromFile(const char *fname, STsdbFS *pFS) {
  int32_t  code = 0;
  int32_t  lino = 0;
  uint8_t *pData = NULL;

  // load binary
  TdFilePtr pFD = taosOpenFile(fname, TD_FILE_READ);
  if (pFD == NULL) {
    code = terrno;
    TSDB_CHECK_CODE(code, lino, _exit);
  }

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

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

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

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

  taosCloseFile(&pFD);

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

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

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

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

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

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

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

_exit:
  return code;
}

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

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

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

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

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

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

H
Hongze Cheng 已提交
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
    pSetTo->nSttF++;
    *pSetTo->aSttF[iStt] = *pSetFrom->aSttF[iStt];
    pSetTo->aSttF[iStt]->nRef = 1;
  }

_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
  }
  return code;
}

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

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

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

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

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

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

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

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

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

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

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

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

H
Hongze Cheng 已提交
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 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
      pSetOld->nSttF++;
    }
  }

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

_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
  }
  return code;
}

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

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

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

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

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

      if (pDelFile) {
        nRef = atomic_sub_fetch_32(&pDelFile->nRef, 1);
        if (nRef == 0) {
          tsdbDelFileName(pTsdb, pDelFile, fname);
          (void)taosRemoveFile(fname);
          taosMemoryFree(pDelFile);
        }
      }
    }
  } else {
    ASSERT(pTsdb->fs.pDelFile == NULL);
  }

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

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

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

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

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

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

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

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

      iOld++;
      iNew++;
H
Hongze Cheng 已提交
690
    }
H
Hongze Cheng 已提交
691 692 693 694 695 696 697 698 699
  }

_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
  }
  return code;
}

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

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

H
Hongze Cheng 已提交
710 711
  if (!taosCheckExistFile(current_t)) goto _exit;

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

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

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

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

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

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

  char current_t[TSDB_FILENAME_LEN] = {0};
  tsdbGetCurrentFName(pTsdb, NULL, current_t);
H
Hongze Cheng 已提交
743
  (void)taosRemoveFile(current_t);
H
Hongze Cheng 已提交
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778

_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(errno));
  }
  return code;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

H
Hongze Cheng 已提交
830 831 832 833 834
  return code;
}

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

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

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

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

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

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

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

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

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

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

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

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

H
Hongze Cheng 已提交
912 913 914 915 916
  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 已提交
917 918
    }
  }
H
Hongze Cheng 已提交
919 920 921 922
  *pFS->pDelFile = *pDelFile;

_exit:
  return code;
H
Hongze Cheng 已提交
923 924
}

H
Hongze Cheng 已提交
925 926 927 928 929 930 931 932 933 934 935 936 937
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 已提交
938 939 940
      // stt
      if (pSet->nSttF > pDFileSet->nSttF) {
        ASSERT(pSet->nSttF == pDFileSet->nSttF + 1);
H
Hongze Cheng 已提交
941

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

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

H
Hongze Cheng 已提交
963 964 965 966
      goto _exit;
    }
  }

H
Hongze Cheng 已提交
967 968
  ASSERT(pSet->nSttF == 1);
  SDFileSet fSet = {.diskId = pSet->diskId, .fid = pSet->fid, .nSttF = 1};
H
Hongze Cheng 已提交
969 970 971 972

  // head
  fSet.pHeadF = (SHeadFile *)taosMemoryMalloc(sizeof(SHeadFile));
  if (fSet.pHeadF == NULL) {
H
Hongze Cheng 已提交
973
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
974
    goto _exit;
H
Hongze Cheng 已提交
975
  }
H
Hongze Cheng 已提交
976
  *fSet.pHeadF = *pSet->pHeadF;
H
Hongze Cheng 已提交
977

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

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

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

H
Hongze Cheng 已提交
1002
  if (taosArrayInsert(pFS->aDFileSet, idx, &fSet) == NULL) {
H
Hongze Cheng 已提交
1003
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1004 1005 1006 1007 1008 1009
    goto _exit;
  }

_exit:
  return code;
}
1010

H
Hongze Cheng 已提交
1011
int32_t tsdbFSPrepareCommit(STsdb *pTsdb, STsdbFS *pFSNew) {
1012
  int32_t code = 0;
H
Hongze Cheng 已提交
1013
  int32_t lino = 0;
1014 1015
  char    tfname[TSDB_FILENAME_LEN];

H
Hongze Cheng 已提交
1016
  tsdbGetCurrentFName(pTsdb, NULL, tfname);
1017 1018

  // gnrt CURRENT.t
H
Hongze Cheng 已提交
1019
  code = tsdbSaveFSToFile(pFSNew, tfname);
H
Hongze Cheng 已提交
1020
  TSDB_CHECK_CODE(code, lino, _exit);
1021

H
Hongze Cheng 已提交
1022
_exit:
1023
  if (code) {
H
Hongze Cheng 已提交
1024
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
1025 1026 1027 1028
  }
  return code;
}

C
Cary Xu 已提交
1029
int32_t tsdbFSRef(STsdb *pTsdb, STsdbFS *pFS) {
H
Hongze Cheng 已提交
1030
  int32_t code = 0;
H
Hongze Cheng 已提交
1031
  int32_t nRef;
H
Hongze Cheng 已提交
1032

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

H
Hongze Cheng 已提交
1039 1040 1041 1042
  pFS->pDelFile = pTsdb->fs.pDelFile;
  if (pFS->pDelFile) {
    nRef = atomic_fetch_add_32(&pFS->pDelFile->nRef, 1);
    ASSERT(nRef > 0);
H
Hongze Cheng 已提交
1043 1044
  }

H
Hongze Cheng 已提交
1045 1046 1047 1048
  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 已提交
1049

H
Hongze Cheng 已提交
1050 1051
    nRef = atomic_fetch_add_32(&pSet->pHeadF->nRef, 1);
    ASSERT(nRef > 0);
H
Hongze Cheng 已提交
1052

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

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

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

H
Hongze Cheng 已提交
1064
    if (taosArrayPush(pFS->aDFileSet, &fSet) == NULL) {
H
Hongze Cheng 已提交
1065
      code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
1066
      goto _exit;
H
Hongze Cheng 已提交
1067 1068 1069
    }
  }

H
Hongze Cheng 已提交
1070
_exit:
H
Hongze Cheng 已提交
1071 1072 1073
  return code;
}

H
Hongze Cheng 已提交
1074 1075 1076
void tsdbFSUnref(STsdb *pTsdb, STsdbFS *pFS) {
  int32_t nRef;
  char    fname[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
1077

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

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

H
Hongze Cheng 已提交
1091 1092 1093 1094 1095
    // 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);
H
Hongze Cheng 已提交
1096
      (void)taosRemoveFile(fname);
H
Hongze Cheng 已提交
1097 1098
      taosMemoryFree(pSet->pHeadF);
    }
H
Hongze Cheng 已提交
1099

H
Hongze Cheng 已提交
1100 1101 1102 1103 1104
    // 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);
H
Hongze Cheng 已提交
1105
      (void)taosRemoveFile(fname);
H
Hongze Cheng 已提交
1106 1107
      taosMemoryFree(pSet->pDataF);
    }
H
Hongze Cheng 已提交
1108

H
Hongze Cheng 已提交
1109 1110 1111 1112 1113
    // 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);
H
Hongze Cheng 已提交
1114
      (void)taosRemoveFile(fname);
H
Hongze Cheng 已提交
1115
      taosMemoryFree(pSet->pSmaF);
H
Hongze Cheng 已提交
1116
    }
H
Hongze Cheng 已提交
1117

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

H
Hongze Cheng 已提交
1131 1132
  taosArrayDestroy(pFS->aDFileSet);
}