tsdbFS.c 13.8 KB
Newer Older
H
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * 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 已提交
14 15
 */

H
Hongze Cheng 已提交
16
#include "inc/tsdbFS.h"
H
Hongze Cheng 已提交
17

H
Hongze Cheng 已提交
18 19
#define TSDB_FS_EDIT_MIN TSDB_FEDIT_COMMIT
#define TSDB_FS_EDIT_MAX (TSDB_FEDIT_MERGE + 1)
H
Hongze Cheng 已提交
20 21 22 23 24 25 26 27

enum {
  TSDB_FS_STATE_NONE = 0,
  TSDB_FS_STATE_OPEN,
  TSDB_FS_STATE_EDIT,
  TSDB_FS_STATE_CLOSE,
};

H
Hongze Cheng 已提交
28 29
typedef enum {
  TSDB_FCURRENT = 1,
H
Hongze Cheng 已提交
30 31
  TSDB_FCURRENT_C,  // for commit
  TSDB_FCURRENT_M,  // for merge
H
Hongze Cheng 已提交
32 33 34 35
} EFCurrentT;

static const char *gCurrentFname[] = {
    [TSDB_FCURRENT] = "current.json",
H
Hongze Cheng 已提交
36 37
    [TSDB_FCURRENT_C] = "current.c.json",
    [TSDB_FCURRENT_M] = "current.m.json",
H
Hongze Cheng 已提交
38 39
};

H
Hongze Cheng 已提交
40 41 42 43 44 45 46 47 48 49
static int32_t create_fs(STsdb *pTsdb, STFileSystem **fs) {
  fs[0] = taosMemoryCalloc(1, sizeof(*fs[0]));
  if (fs[0] == NULL) return TSDB_CODE_OUT_OF_MEMORY;

  fs[0]->cstate = taosArrayInit(16, sizeof(STFileSet));
  fs[0]->nstate = taosArrayInit(16, sizeof(STFileSet));
  if (fs[0]->cstate == NULL || fs[0]->nstate == NULL) {
    taosArrayDestroy(fs[0]->nstate);
    taosArrayDestroy(fs[0]->cstate);
    taosMemoryFree(fs[0]);
H
Hongze Cheng 已提交
50 51 52
    return TSDB_CODE_OUT_OF_MEMORY;
  }

H
Hongze Cheng 已提交
53 54 55 56
  fs[0]->pTsdb = pTsdb;
  fs[0]->state = TSDB_FS_STATE_NONE;
  tsem_init(&fs[0]->canEdit, 0, 1);
  fs[0]->neid = 0;
H
Hongze Cheng 已提交
57

H
Hongze Cheng 已提交
58 59 60
  return 0;
}

H
Hongze Cheng 已提交
61 62 63 64 65 66 67
static int32_t destroy_fs(STFileSystem **fs) {
  if (fs[0] == NULL) return 0;
  taosArrayDestroy(fs[0]->nstate);
  taosArrayDestroy(fs[0]->cstate);
  tsem_destroy(&fs[0]->canEdit);
  taosMemoryFree(fs[0]);
  fs[0] = NULL;
H
Hongze Cheng 已提交
68 69 70
  return 0;
}

H
Hongze Cheng 已提交
71
static int32_t current_fname(STsdb *pTsdb, char *fname, EFCurrentT ftype) {
H
Hongze Cheng 已提交
72 73 74 75 76 77 78 79
  if (pTsdb->pVnode->pTfs) {
    snprintf(fname,                                   //
             TSDB_FILENAME_LEN,                       //
             "%s%s%s%s%s",                            //
             tfsGetPrimaryPath(pTsdb->pVnode->pTfs),  //
             TD_DIRSEP,                               //
             pTsdb->path,                             //
             TD_DIRSEP,                               //
H
Hongze Cheng 已提交
80
             gCurrentFname[ftype]);
H
Hongze Cheng 已提交
81 82 83 84 85 86
  } else {
    snprintf(fname,              //
             TSDB_FILENAME_LEN,  //
             "%s%s%s",           //
             pTsdb->path,        //
             TD_DIRSEP,          //
H
Hongze Cheng 已提交
87
             gCurrentFname[ftype]);
H
Hongze Cheng 已提交
88 89 90 91
  }
  return 0;
}

H
Hongze Cheng 已提交
92
static int32_t fs_from_json_str(const char *pData, STFileSystem *pFS) {
H
Hongze Cheng 已提交
93 94 95
  int32_t code = 0;
  int32_t lino;

H
Hongze Cheng 已提交
96
  ASSERTS(0, "TODO: Not implemented yet");
H
Hongze Cheng 已提交
97

H
Hongze Cheng 已提交
98 99 100
_exit:
  return code;
}
H
Hongze Cheng 已提交
101

H
Hongze Cheng 已提交
102
static int32_t save_json(const cJSON *json, const char *fname) {
H
Hongze Cheng 已提交
103
  int32_t code = 0;
H
Hongze Cheng 已提交
104 105 106 107 108 109 110 111

  char *data = cJSON_Print(json);
  if (data == NULL) return TSDB_CODE_OUT_OF_MEMORY;

  TdFilePtr fp = taosOpenFile(fname, TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC);
  if (fp == NULL) {
    code = TAOS_SYSTEM_ERROR(code);
    goto _exit;
H
Hongze Cheng 已提交
112 113
  }

H
Hongze Cheng 已提交
114 115 116
  if (taosWriteFile(fp, data, strlen(data) + 1) < 0) {
    code = TAOS_SYSTEM_ERROR(code);
    goto _exit;
H
Hongze Cheng 已提交
117 118
  }

H
Hongze Cheng 已提交
119 120 121 122
  if (taosFsyncFile(fp) < 0) {
    code = TAOS_SYSTEM_ERROR(code);
    goto _exit;
  }
H
Hongze Cheng 已提交
123

H
Hongze Cheng 已提交
124
  taosCloseFile(&fp);
H
Hongze Cheng 已提交
125 126

_exit:
H
Hongze Cheng 已提交
127
  taosMemoryFree(data);
H
Hongze Cheng 已提交
128 129 130
  return code;
}

H
Hongze Cheng 已提交
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
static int32_t load_json(const char *fname, cJSON **json) {
  int32_t code = 0;
  void   *data = NULL;

  TdFilePtr fp = taosOpenFile(fname, TD_FILE_READ);
  if (fp == NULL) return TAOS_SYSTEM_ERROR(code);

  int64_t size;
  if (taosFStatFile(fp, &size, NULL) < 0) {
    code = TAOS_SYSTEM_ERROR(code);
    goto _exit;
  }

  data = taosMemoryMalloc(size);
  if (data == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

  if (taosReadFile(fp, data, size) < 0) {
    code = TAOS_SYSTEM_ERROR(code);
    goto _exit;
  }

  json[0] = cJSON_Parse(data);
  if (json[0] == NULL) {
    code = TSDB_CODE_FILE_CORRUPTED;
    goto _exit;
  }

_exit:
  taosCloseFile(&fp);
  if (data) taosMemoryFree(data);
  if (code) json[0] = NULL;
  return code;
}

H
Hongze Cheng 已提交
168
static int32_t save_fs(SArray *aTFileSet, const char *fname) {
H
Hongze Cheng 已提交
169
  int32_t code = 0;
H
Hongze Cheng 已提交
170
  int32_t lino = 0;
H
Hongze Cheng 已提交
171

H
Hongze Cheng 已提交
172 173
  cJSON *json = cJSON_CreateObject();
  if (json == NULL) return TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
174

H
Hongze Cheng 已提交
175 176 177
  // fmtv
  if (cJSON_AddNumberToObject(json, "fmtv", 1) == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
178
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
179 180
  }

H
Hongze Cheng 已提交
181 182 183
  // fset
  cJSON *ajson = cJSON_AddArrayToObject(json, "fset");
  if (ajson == NULL) {
H
Hongze Cheng 已提交
184
    TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
H
Hongze Cheng 已提交
185 186 187
  }
  for (int32_t i = 0; i < taosArrayGetSize(aTFileSet); i++) {
    STFileSet *pFileSet = (STFileSet *)taosArrayGet(aTFileSet, i);
H
Hongze Cheng 已提交
188
    cJSON     *item;
H
Hongze Cheng 已提交
189

H
Hongze Cheng 已提交
190 191 192
    item = cJSON_CreateObject();
    if (!item) TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
    cJSON_AddItemToArray(ajson, item);
H
Hongze Cheng 已提交
193

H
Hongze Cheng 已提交
194
    code = tsdbFileSetToJson(pFileSet, item);
H
Hongze Cheng 已提交
195 196 197
    TSDB_CHECK_CODE(code, lino, _exit);
  }

H
Hongze Cheng 已提交
198
  code = save_json(json, fname);
H
Hongze Cheng 已提交
199
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
200 201 202

_exit:
  if (code) {
H
Hongze Cheng 已提交
203
    tsdbError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
204
  }
H
Hongze Cheng 已提交
205
  cJSON_Delete(json);
H
Hongze Cheng 已提交
206
  return code;
H
Hongze Cheng 已提交
207 208
}

H
Hongze Cheng 已提交
209 210 211 212 213 214 215 216 217
static int32_t load_fs(const char *fname, SArray *aTFileSet, int64_t *eid) {
  int32_t code = 0;
  int32_t lino = 0;

  taosArrayClear(aTFileSet);

  // load json
  cJSON *json = NULL;
  code = load_json(fname, &json);
H
Hongze Cheng 已提交
218
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
219 220 221 222 223 224 225 226 227

  // parse json
  const cJSON *item;

  /* fmtv */
  item = cJSON_GetObjectItem(json, "fmtv");
  if (cJSON_IsNumber(item)) {
    ASSERT(item->valuedouble == 1);
  } else {
H
Hongze Cheng 已提交
228
    TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
H
Hongze Cheng 已提交
229 230 231 232 233 234 235
  }

  /* eid */
  item = cJSON_GetObjectItem(json, "eid");
  if (cJSON_IsNumber(item)) {
    eid[0] = item->valuedouble;
  } else {
H
Hongze Cheng 已提交
236
    TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
H
Hongze Cheng 已提交
237 238 239 240 241 242 243
  }

  /* fset */
  item = cJSON_GetObjectItem(json, "fset");
  if (cJSON_IsArray(item)) {
    const cJSON *titem;
    cJSON_ArrayForEach(titem, item) {
H
Hongze Cheng 已提交
244 245 246
      STFileSet *pFileSet;
      if ((pFileSet = taosArrayReserve(aTFileSet, 1)) == NULL) {
        TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
H
Hongze Cheng 已提交
247 248 249
      }

      code = tsdbFileSetFromJson(titem, pFileSet);
H
Hongze Cheng 已提交
250
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
251 252
    }
  } else {
H
Hongze Cheng 已提交
253
    TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
H
Hongze Cheng 已提交
254 255 256 257 258 259 260 261
  }

_exit:
  if (code) {
    tsdbError("%s failed at line %d since %s, fname:%s", __func__, lino, tstrerror(code), fname);
  }
  if (json) cJSON_Delete(json);
  return code;
H
Hongze Cheng 已提交
262 263
}

H
Hongze Cheng 已提交
264 265 266 267 268 269
static int32_t apply_commit(STFileSystem *fs) {
  // TODO
  return 0;
}

static int32_t commit_edit(STFileSystem *fs) {
H
Hongze Cheng 已提交
270 271
  char current[TSDB_FILENAME_LEN];
  char current_t[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
272

H
Hongze Cheng 已提交
273 274 275 276 277
  current_fname(fs->pTsdb, current, TSDB_FCURRENT);
  if (fs->etype == TSDB_FEDIT_COMMIT) {
    current_fname(fs->pTsdb, current, TSDB_FCURRENT_C);
  } else if (fs->etype == TSDB_FEDIT_MERGE) {
    current_fname(fs->pTsdb, current, TSDB_FCURRENT_M);
H
Hongze Cheng 已提交
278 279
  } else {
    ASSERT(0);
H
Hongze Cheng 已提交
280 281
  }

H
Hongze Cheng 已提交
282 283 284
  int32_t code;
  int32_t lino;
  if ((code = taosRenameFile(current_t, current))) {
H
Hongze Cheng 已提交
285
    TSDB_CHECK_CODE(code = TAOS_SYSTEM_ERROR(code), lino, _exit);
H
Hongze Cheng 已提交
286
  }
H
Hongze Cheng 已提交
287

H
Hongze Cheng 已提交
288
  code = apply_commit(fs);
H
Hongze Cheng 已提交
289
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
290

H
Hongze Cheng 已提交
291 292
_exit:
  if (code) {
H
Hongze Cheng 已提交
293 294 295
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(fs->pTsdb->pVnode), __func__, lino, tstrerror(code));
  } else {
    tsdbInfo("vgId:%d %s success, eid:%" PRId64 " etype:%d", TD_VID(fs->pTsdb->pVnode), __func__, fs->eid, fs->etype);
H
Hongze Cheng 已提交
296 297
  }
  return code;
H
Hongze Cheng 已提交
298 299
}

H
Hongze Cheng 已提交
300 301 302 303 304 305 306
// static int32_t
static int32_t apply_abort(STFileSystem *fs) {
  // TODO
  return 0;
}

static int32_t abort_edit(STFileSystem *fs) {
H
Hongze Cheng 已提交
307
  char fname[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
308

H
Hongze Cheng 已提交
309 310 311 312
  if (fs->etype == TSDB_FEDIT_COMMIT) {
    current_fname(fs->pTsdb, fname, TSDB_FCURRENT_C);
  } else if (fs->etype == TSDB_FEDIT_MERGE) {
    current_fname(fs->pTsdb, fname, TSDB_FCURRENT_M);
H
Hongze Cheng 已提交
313 314 315
  } else {
    ASSERT(0);
  }
H
Hongze Cheng 已提交
316

H
Hongze Cheng 已提交
317 318 319
  int32_t code;
  int32_t lino;
  if ((code = taosRemoveFile(fname))) {
H
Hongze Cheng 已提交
320
    TSDB_CHECK_CODE(code = TAOS_SYSTEM_ERROR(code), lino, _exit);
H
Hongze Cheng 已提交
321
  }
H
Hongze Cheng 已提交
322

H
Hongze Cheng 已提交
323
  code = apply_abort(fs);
H
Hongze Cheng 已提交
324
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
325 326 327 328 329 330 331 332

_exit:
  if (code) {
    tsdbError("vgId:%d %s failed since %s", TD_VID(fs->pTsdb->pVnode), __func__, tstrerror(code));
  } else {
    tsdbInfo("vgId:%d %s success, eid:%" PRId64 " etype:%d", TD_VID(fs->pTsdb->pVnode), __func__, fs->eid, fs->etype);
  }
  return code;
H
Hongze Cheng 已提交
333 334
}

H
Hongze Cheng 已提交
335
static int32_t scan_and_fix_fs(STFileSystem *pFS) {
H
Hongze Cheng 已提交
336
  // TODO
H
Hongze Cheng 已提交
337 338 339
  return 0;
}

H
Hongze Cheng 已提交
340 341 342 343 344
static int32_t update_fs_if_needed(STFileSystem *pFS) {
  // TODO
  return 0;
}

H
Hongze Cheng 已提交
345
static int32_t open_fs(STFileSystem *fs, int8_t rollback) {
H
Hongze Cheng 已提交
346
  int32_t code = 0;
H
Hongze Cheng 已提交
347
  int32_t lino = 0;
H
Hongze Cheng 已提交
348
  STsdb  *pTsdb = fs->pTsdb;
H
Hongze Cheng 已提交
349

H
Hongze Cheng 已提交
350
  code = update_fs_if_needed(fs);
H
Hongze Cheng 已提交
351
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
352

H
Hongze Cheng 已提交
353 354 355
  char fCurrent[TSDB_FILENAME_LEN];
  char cCurrent[TSDB_FILENAME_LEN];
  char mCurrent[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
356

H
Hongze Cheng 已提交
357 358
  current_fname(pTsdb, fCurrent, TSDB_FCURRENT);
  current_fname(pTsdb, cCurrent, TSDB_FCURRENT_C);
H
Hongze Cheng 已提交
359
  current_fname(pTsdb, mCurrent, TSDB_FCURRENT_M);
H
Hongze Cheng 已提交
360

H
Hongze Cheng 已提交
361
  if (taosCheckExistFile(fCurrent)) {  // current.json exists
H
Hongze Cheng 已提交
362
    code = load_fs(fCurrent, fs->cstate, &fs->neid);
H
Hongze Cheng 已提交
363
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
364

H
Hongze Cheng 已提交
365
    if (taosCheckExistFile(cCurrent)) {
H
Hongze Cheng 已提交
366 367 368
      // current.c.json exists

      fs->etype = TSDB_FEDIT_COMMIT;
H
Hongze Cheng 已提交
369
      if (rollback) {
H
Hongze Cheng 已提交
370
        code = abort_edit(fs);
H
Hongze Cheng 已提交
371 372
        TSDB_CHECK_CODE(code, lino, _exit);
      } else {
H
Hongze Cheng 已提交
373
        code = load_fs(cCurrent, fs->nstate, &fs->eid);
H
Hongze Cheng 已提交
374
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
375 376

        code = commit_edit(fs);
H
Hongze Cheng 已提交
377 378
        TSDB_CHECK_CODE(code, lino, _exit);
      }
H
Hongze Cheng 已提交
379 380 381 382
    } else if (taosCheckExistFile(mCurrent)) {
      // current.m.json exists
      fs->etype = TSDB_FEDIT_MERGE;
      code = abort_edit(fs);
H
Hongze Cheng 已提交
383 384
      TSDB_CHECK_CODE(code, lino, _exit);
    }
H
Hongze Cheng 已提交
385

H
Hongze Cheng 已提交
386
    code = scan_and_fix_fs(fs);
H
Hongze Cheng 已提交
387
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
388
  } else {
H
Hongze Cheng 已提交
389
    code = save_fs(fs->cstate, fCurrent);
H
Hongze Cheng 已提交
390
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
391 392 393 394
  }

_exit:
  if (code) {
H
Hongze Cheng 已提交
395
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
396
  } else {
H
Hongze Cheng 已提交
397
    tsdbInfo("vgId:%d %s success", TD_VID(pTsdb->pVnode), __func__);
H
Hongze Cheng 已提交
398
  }
H
Hongze Cheng 已提交
399 400 401
  return 0;
}

H
Hongze Cheng 已提交
402
static int32_t close_file_system(STFileSystem *pFS) {
H
Hongze Cheng 已提交
403
  ASSERTS(0, "TODO: Not implemented yet");
H
Hongze Cheng 已提交
404 405
  return 0;
}
H
Hongze Cheng 已提交
406

H
Hongze Cheng 已提交
407
static int32_t apply_edit(STFileSystem *pFS) {
H
Hongze Cheng 已提交
408
  int32_t code = 0;
H
Hongze Cheng 已提交
409
  ASSERTS(0, "TODO: Not implemented yet");
H
Hongze Cheng 已提交
410
  return code;
H
Hongze Cheng 已提交
411 412
}

H
Hongze Cheng 已提交
413
static int32_t fset_cmpr_fn(const struct STFileSet *pSet1, const struct STFileSet *pSet2) {
H
Hongze Cheng 已提交
414 415 416 417 418 419 420 421
  if (pSet1->fid < pSet2->fid) {
    return -1;
  } else if (pSet1->fid > pSet2->fid) {
    return 1;
  }
  return 0;
}

H
Hongze Cheng 已提交
422
static int32_t edit_fs(STFileSystem *pFS, const SArray *aFileOp) {
H
Hongze Cheng 已提交
423
  int32_t code = 0;
H
Hongze Cheng 已提交
424
  int32_t lino = 0;
H
Hongze Cheng 已提交
425

H
Hongze Cheng 已提交
426
  STFileSet *pSet = NULL;
H
Hongze Cheng 已提交
427
  for (int32_t iop = 0; iop < taosArrayGetSize(aFileOp); iop++) {
H
Hongze Cheng 已提交
428
    struct STFileOp *op = taosArrayGet(aFileOp, iop);
H
Hongze Cheng 已提交
429

H
Hongze Cheng 已提交
430 431 432
    if (pSet == NULL || pSet->fid != op->fid) {
      STFileSet fset = {.fid = op->fid};
      pSet = taosArraySearch(pFS->nstate, &fset, (__compar_fn_t)tsdbFSetCmprFn, TD_EQ);
H
Hongze Cheng 已提交
433 434
    }

H
Hongze Cheng 已提交
435
    // create fset if need
H
Hongze Cheng 已提交
436 437
    if (pSet == NULL) {
      ASSERT(op->oState.size == 0 && op->nState.size > 0);
H
Hongze Cheng 已提交
438 439 440 441 442 443 444 445 446

      STFileSet fset = {.fid = op->fid};
      int32_t   idx = taosArraySearchIdx(pFS->nstate, &fset, (__compar_fn_t)tsdbFSetCmprFn, TD_GT);

      if (idx < 0) idx = taosArrayGetSize(pFS->nstate);

      pSet = taosArrayInsert(pFS->nstate, idx, &fset);
      if (pSet == NULL) {
        code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
447
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
448 449 450
      }

      tsdbFileSetInit(pSet);
H
Hongze Cheng 已提交
451 452 453
    }

    code = tsdbFSetEdit(pSet, op);
H
Hongze Cheng 已提交
454
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
455 456
  }

H
Hongze Cheng 已提交
457
_exit:
H
Hongze Cheng 已提交
458 459 460
  return 0;
}

H
Hongze Cheng 已提交
461
int32_t tsdbOpenFS(STsdb *pTsdb, STFileSystem **fs, int8_t rollback) {
H
Hongze Cheng 已提交
462 463 464
  int32_t code;
  int32_t lino;

H
Hongze Cheng 已提交
465
  code = create_fs(pTsdb, fs);
H
Hongze Cheng 已提交
466 467
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
468
  code = open_fs(fs[0], rollback);
H
Hongze Cheng 已提交
469
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
470 471 472

_exit:
  if (code) {
H
Hongze Cheng 已提交
473
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
474
    destroy_fs(fs);
H
Hongze Cheng 已提交
475
  } else {
H
Hongze Cheng 已提交
476
    tsdbInfo("vgId:%d %s success", TD_VID(pTsdb->pVnode), __func__);
H
Hongze Cheng 已提交
477 478 479 480
  }
  return 0;
}

H
Hongze Cheng 已提交
481
int32_t tsdbCloseFS(STFileSystem **ppFS) {
H
Hongze Cheng 已提交
482 483
  if (ppFS[0] == NULL) return 0;
  close_file_system(ppFS[0]);
H
Hongze Cheng 已提交
484
  destroy_fs(ppFS);
H
Hongze Cheng 已提交
485 486
  return 0;
}
H
Hongze Cheng 已提交
487

H
Hongze Cheng 已提交
488 489 490 491 492 493
int32_t tsdbFSAllocEid(STFileSystem *pFS, int64_t *eid) {
  eid[0] = ++pFS->neid;  // TODO: use atomic operation
  return 0;
}

int32_t tsdbFSEditBegin(STFileSystem *fs, int64_t eid, const SArray *aFileOp, EFEditT etype) {
H
Hongze Cheng 已提交
494
  int32_t code = 0;
H
Hongze Cheng 已提交
495
  int32_t lino;
H
Hongze Cheng 已提交
496
  char    current_t[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
497

H
Hongze Cheng 已提交
498 499 500 501 502 503 504
  if (etype == TSDB_FEDIT_COMMIT) {
    current_fname(fs->pTsdb, current_t, TSDB_FCURRENT_C);
  } else {
    current_fname(fs->pTsdb, current_t, TSDB_FCURRENT_M);
  }

  tsem_wait(&fs->canEdit);
H
Hongze Cheng 已提交
505

H
Hongze Cheng 已提交
506
  fs->etype = etype;
H
Hongze Cheng 已提交
507
  fs->eid = eid;
H
Hongze Cheng 已提交
508

H
Hongze Cheng 已提交
509 510 511
  // edit
  code = edit_fs(fs, aFileOp);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
512

H
Hongze Cheng 已提交
513
  // save fs
H
Hongze Cheng 已提交
514
  code = save_fs(fs->nstate, current_t);
H
Hongze Cheng 已提交
515
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
516 517 518

_exit:
  if (code) {
H
Hongze Cheng 已提交
519 520
    tsdbError("vgId:%d %s failed at line %d since %s, eid:%" PRId64 " etype:%d", TD_VID(fs->pTsdb->pVnode), __func__,
              lino, tstrerror(code), fs->eid, etype);
H
Hongze Cheng 已提交
521
  } else {
H
Hongze Cheng 已提交
522
    tsdbInfo("vgId:%d %s done, eid:%" PRId64 " etype:%d", TD_VID(fs->pTsdb->pVnode), __func__, fs->eid, etype);
H
Hongze Cheng 已提交
523 524 525 526
  }
  return code;
}

H
Hongze Cheng 已提交
527 528 529
int32_t tsdbFSEditCommit(STFileSystem *fs) {
  int32_t code = commit_edit(fs);
  tsem_post(&fs->canEdit);
H
Hongze Cheng 已提交
530 531 532
  return code;
}

H
Hongze Cheng 已提交
533 534 535
int32_t tsdbFSEditAbort(STFileSystem *fs) {
  int32_t code = abort_edit(fs);
  tsem_post(&fs->canEdit);
H
Hongze Cheng 已提交
536
  return code;
H
Hongze Cheng 已提交
537 538 539 540 541 542
}

int32_t tsdbFSGetFSet(STFileSystem *fs, int32_t fid, const STFileSet **ppFSet) {
  STFileSet fset = {.fid = fid};
  ppFSet[0] = taosArraySearch(fs->cstate, &fset, (__compar_fn_t)tsdbFSetCmprFn, TD_EQ);
  return 0;
H
Hongze Cheng 已提交
543
}