tsdbFS.c 31.5 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 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
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);
    taosRemoveFile(fname);
    taosMemoryFree(pSet->pDataF);
  }

  nRef = atomic_sub_fetch_32(&pSet->pSmaF->nRef, 1);
  if (nRef == 0) {
    tsdbSmaFileName(pTsdb, pSet->diskId, pSet->fid, pSet->pSmaF, fname);
    taosRemoveFile(fname);
    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);
      taosRemoveFile(fname);
      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 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 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
  // 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);
          taosRemoveFile(fname);
          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);
            taosRemoveFile(fname);
            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 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721
  int32_t code = 0;
  int32_t lino = 0;

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

  // rename the file
  if (taosRenameFile(current_t, current) < 0) {
    code = TAOS_SYSTEM_ERROR(errno);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

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

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

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

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

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

  char current_t[TSDB_FILENAME_LEN] = {0};
  tsdbGetCurrentFName(pTsdb, NULL, current_t);
H
Hongze Cheng 已提交
741
  (void)taosRemoveFile(current_t);
H
Hongze Cheng 已提交
742 743 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

_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 已提交
777 778
  } else {
    // empty one
H
Hongze Cheng 已提交
779
    code = tsdbSaveFSToFile(&pTsdb->fs, current);
H
Hongze Cheng 已提交
780
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
781 782

    ASSERT(!rollback);
H
Hongze Cheng 已提交
783 784
  }

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

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

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

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

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

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

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

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

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

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

H
Hongze Cheng 已提交
828 829 830 831 832
  return code;
}

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

  pFS->pDelFile = NULL;
H
Hongze Cheng 已提交
836 837 838 839 840 841 842 843
  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 已提交
844 845
  }

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

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

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

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

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

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

H
Hongze Cheng 已提交
884 885 886 887
    // 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 已提交
888
        code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
889
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
890
      }
H
Hongze Cheng 已提交
891
      *fSet.aSttF[fSet.nSttF] = *pSet->aSttF[fSet.nSttF];
H
Hongze Cheng 已提交
892
    }
H
Hongze Cheng 已提交
893 894 895

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

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

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

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

_exit:
  return code;
H
Hongze Cheng 已提交
921 922
}

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

H
Hongze Cheng 已提交
940 941
        pDFileSet->aSttF[pDFileSet->nSttF] = (SSttFile *)taosMemoryMalloc(sizeof(SSttFile));
        if (pDFileSet->aSttF[pDFileSet->nSttF] == NULL) {
H
Hongze Cheng 已提交
942 943 944
          code = TSDB_CODE_OUT_OF_MEMORY;
          goto _exit;
        }
H
Hongze Cheng 已提交
945 946 947 948 949 950
        *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 已提交
951 952
        }

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

H
Hongze Cheng 已提交
961 962 963 964
      goto _exit;
    }
  }

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

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

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

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

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

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

_exit:
  return code;
}
1008

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

H
Hongze Cheng 已提交
1014
  tsdbGetCurrentFName(pTsdb, NULL, tfname);
1015 1016

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

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

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

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

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

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

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

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

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

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

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

H
Hongze Cheng 已提交
1068
_exit:
H
Hongze Cheng 已提交
1069 1070 1071
  return code;
}

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

H
Hongze Cheng 已提交
1076 1077 1078 1079 1080
  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 已提交
1081
      (void)taosRemoveFile(fname);
H
Hongze Cheng 已提交
1082 1083
      taosMemoryFree(pFS->pDelFile);
    }
H
Hongze Cheng 已提交
1084 1085
  }

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

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

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

H
Hongze Cheng 已提交
1107 1108 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);
      taosRemoveFile(fname);
      taosMemoryFree(pSet->pSmaF);
H
Hongze Cheng 已提交
1114
    }
H
Hongze Cheng 已提交
1115

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

H
Hongze Cheng 已提交
1129 1130
  taosArrayDestroy(pFS->aDFileSet);
}