tsdbFS.c 13.4 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
  if (taosWriteFile(fp, data, strlen(data)) < 0) {
H
Hongze Cheng 已提交
115 116
    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
static int32_t load_json(const char *fname, cJSON **json) {
  int32_t code = 0;
H
Hongze Cheng 已提交
133
  char   *data = NULL;
H
Hongze Cheng 已提交
134 135 136 137 138 139 140 141 142 143

  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;
  }

H
Hongze Cheng 已提交
144
  data = taosMemoryMalloc(size + 1);
H
Hongze Cheng 已提交
145 146 147 148 149 150 151 152 153
  if (data == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

  if (taosReadFile(fp, data, size) < 0) {
    code = TAOS_SYSTEM_ERROR(code);
    goto _exit;
  }
H
Hongze Cheng 已提交
154
  data[size] = '\0';
H
Hongze Cheng 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168

  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 已提交
169
static int32_t save_fs(SArray *aTFileSet, const char *fname) {
H
Hongze Cheng 已提交
170
  int32_t code = 0;
H
Hongze Cheng 已提交
171
  int32_t lino = 0;
H
Hongze Cheng 已提交
172

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

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

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

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

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

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

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

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

  taosArrayClear(aTFileSet);

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

  // parse json
  const cJSON *item;

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

  /* fset */
  item = cJSON_GetObjectItem(json, "fset");
  if (cJSON_IsArray(item)) {
    const cJSON *titem;
    cJSON_ArrayForEach(titem, item) {
H
Hongze Cheng 已提交
235 236
      STFileSet *fset = taosArrayReserve(aTFileSet, 1);
      if (!fset) TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
H
Hongze Cheng 已提交
237

H
Hongze Cheng 已提交
238
      code = tsdbJsonToFileSet(titem, fset);
H
Hongze Cheng 已提交
239
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
240 241
    }
  } else {
H
Hongze Cheng 已提交
242
    TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
H
Hongze Cheng 已提交
243 244 245 246 247 248 249 250
  }

_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 已提交
251 252
}

H
Hongze Cheng 已提交
253 254 255 256 257 258
static int32_t apply_commit(STFileSystem *fs) {
  // TODO
  return 0;
}

static int32_t commit_edit(STFileSystem *fs) {
H
Hongze Cheng 已提交
259 260
  char current[TSDB_FILENAME_LEN];
  char current_t[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
261

H
Hongze Cheng 已提交
262 263 264 265 266
  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 已提交
267 268
  } else {
    ASSERT(0);
H
Hongze Cheng 已提交
269 270
  }

H
Hongze Cheng 已提交
271 272 273
  int32_t code;
  int32_t lino;
  if ((code = taosRenameFile(current_t, current))) {
H
Hongze Cheng 已提交
274
    TSDB_CHECK_CODE(code = TAOS_SYSTEM_ERROR(code), lino, _exit);
H
Hongze Cheng 已提交
275
  }
H
Hongze Cheng 已提交
276

H
Hongze Cheng 已提交
277
  code = apply_commit(fs);
H
Hongze Cheng 已提交
278
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
279

H
Hongze Cheng 已提交
280 281
_exit:
  if (code) {
H
Hongze Cheng 已提交
282 283 284
    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 已提交
285 286
  }
  return code;
H
Hongze Cheng 已提交
287 288
}

H
Hongze Cheng 已提交
289 290 291 292 293 294 295
// static int32_t
static int32_t apply_abort(STFileSystem *fs) {
  // TODO
  return 0;
}

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

H
Hongze Cheng 已提交
298 299 300 301
  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 已提交
302 303 304
  } else {
    ASSERT(0);
  }
H
Hongze Cheng 已提交
305

H
Hongze Cheng 已提交
306 307 308
  int32_t code;
  int32_t lino;
  if ((code = taosRemoveFile(fname))) {
H
Hongze Cheng 已提交
309
    TSDB_CHECK_CODE(code = TAOS_SYSTEM_ERROR(code), lino, _exit);
H
Hongze Cheng 已提交
310
  }
H
Hongze Cheng 已提交
311

H
Hongze Cheng 已提交
312
  code = apply_abort(fs);
H
Hongze Cheng 已提交
313
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
314 315 316 317 318 319 320 321

_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 已提交
322 323
}

H
Hongze Cheng 已提交
324
static int32_t scan_and_fix_fs(STFileSystem *pFS) {
H
Hongze Cheng 已提交
325
  // TODO
H
Hongze Cheng 已提交
326 327 328
  return 0;
}

H
Hongze Cheng 已提交
329 330 331 332 333
static int32_t update_fs_if_needed(STFileSystem *pFS) {
  // TODO
  return 0;
}

H
Hongze Cheng 已提交
334
static int32_t open_fs(STFileSystem *fs, int8_t rollback) {
H
Hongze Cheng 已提交
335
  int32_t code = 0;
H
Hongze Cheng 已提交
336
  int32_t lino = 0;
H
Hongze Cheng 已提交
337
  STsdb  *pTsdb = fs->pTsdb;
H
Hongze Cheng 已提交
338

H
Hongze Cheng 已提交
339
  code = update_fs_if_needed(fs);
H
Hongze Cheng 已提交
340
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
341

H
Hongze Cheng 已提交
342 343 344
  char fCurrent[TSDB_FILENAME_LEN];
  char cCurrent[TSDB_FILENAME_LEN];
  char mCurrent[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
345

H
Hongze Cheng 已提交
346 347
  current_fname(pTsdb, fCurrent, TSDB_FCURRENT);
  current_fname(pTsdb, cCurrent, TSDB_FCURRENT_C);
H
Hongze Cheng 已提交
348
  current_fname(pTsdb, mCurrent, TSDB_FCURRENT_M);
H
Hongze Cheng 已提交
349

H
Hongze Cheng 已提交
350
  if (taosCheckExistFile(fCurrent)) {  // current.json exists
H
Hongze Cheng 已提交
351
    code = load_fs(fCurrent, fs->cstate);
H
Hongze Cheng 已提交
352
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
353

H
Hongze Cheng 已提交
354
    if (taosCheckExistFile(cCurrent)) {
H
Hongze Cheng 已提交
355 356 357
      // current.c.json exists

      fs->etype = TSDB_FEDIT_COMMIT;
H
Hongze Cheng 已提交
358
      if (rollback) {
H
Hongze Cheng 已提交
359
        code = abort_edit(fs);
H
Hongze Cheng 已提交
360 361
        TSDB_CHECK_CODE(code, lino, _exit);
      } else {
H
Hongze Cheng 已提交
362
        code = load_fs(cCurrent, fs->nstate);
H
Hongze Cheng 已提交
363
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
364 365

        code = commit_edit(fs);
H
Hongze Cheng 已提交
366 367
        TSDB_CHECK_CODE(code, lino, _exit);
      }
H
Hongze Cheng 已提交
368 369 370 371
    } else if (taosCheckExistFile(mCurrent)) {
      // current.m.json exists
      fs->etype = TSDB_FEDIT_MERGE;
      code = abort_edit(fs);
H
Hongze Cheng 已提交
372 373
      TSDB_CHECK_CODE(code, lino, _exit);
    }
H
Hongze Cheng 已提交
374

H
Hongze Cheng 已提交
375
    code = scan_and_fix_fs(fs);
H
Hongze Cheng 已提交
376
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
377
  } else {
H
Hongze Cheng 已提交
378
    code = save_fs(fs->cstate, fCurrent);
H
Hongze Cheng 已提交
379
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
380 381 382 383
  }

_exit:
  if (code) {
H
Hongze Cheng 已提交
384
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
385
  } else {
H
Hongze Cheng 已提交
386
    tsdbInfo("vgId:%d %s success", TD_VID(pTsdb->pVnode), __func__);
H
Hongze Cheng 已提交
387
  }
H
Hongze Cheng 已提交
388 389 390
  return 0;
}

H
Hongze Cheng 已提交
391
static int32_t close_file_system(STFileSystem *pFS) {
H
Hongze Cheng 已提交
392
  ASSERTS(0, "TODO: Not implemented yet");
H
Hongze Cheng 已提交
393 394
  return 0;
}
H
Hongze Cheng 已提交
395

H
Hongze Cheng 已提交
396
static int32_t apply_edit(STFileSystem *pFS) {
H
Hongze Cheng 已提交
397
  int32_t code = 0;
H
Hongze Cheng 已提交
398
  ASSERTS(0, "TODO: Not implemented yet");
H
Hongze Cheng 已提交
399
  return code;
H
Hongze Cheng 已提交
400 401
}

H
Hongze Cheng 已提交
402
static int32_t fset_cmpr_fn(const struct STFileSet *pSet1, const struct STFileSet *pSet2) {
H
Hongze Cheng 已提交
403 404 405 406 407 408 409 410
  if (pSet1->fid < pSet2->fid) {
    return -1;
  } else if (pSet1->fid > pSet2->fid) {
    return 1;
  }
  return 0;
}

H
Hongze Cheng 已提交
411
static int32_t edit_fs(STFileSystem *pFS, const SArray *aFileOp) {
H
Hongze Cheng 已提交
412 413
  int32_t    code = 0;
  int32_t    lino = 0;
H
Hongze Cheng 已提交
414
  STFileSet *pSet = NULL;
H
Hongze Cheng 已提交
415

H
Hongze Cheng 已提交
416
  for (int32_t iop = 0; iop < taosArrayGetSize(aFileOp); iop++) {
H
Hongze Cheng 已提交
417
    struct STFileOp *op = taosArrayGet(aFileOp, iop);
H
Hongze Cheng 已提交
418

H
Hongze Cheng 已提交
419 420
    if (pSet == NULL || pSet->fid != op->fid) {
      STFileSet fset = {.fid = op->fid};
H
Hongze Cheng 已提交
421
      int32_t   idx = taosArraySearchIdx(pFS->nstate, &fset, (__compar_fn_t)tsdbFSetCmprFn, TD_GE);
H
Hongze Cheng 已提交
422

H
Hongze Cheng 已提交
423 424 425 426 427 428
      pSet = NULL;
      if (idx < 0) {
        idx = taosArrayGetSize(pFS->nstate);
      } else {
        pSet = taosArrayGet(pFS->nstate, idx);
        if (pSet->fid != op->fid) pSet = NULL;
H
Hongze Cheng 已提交
429 430
      }

H
Hongze Cheng 已提交
431 432 433 434 435
      if (!pSet) {
        pSet = taosArrayInsert(pFS->nstate, idx, &fset);
        if (!pSet) TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
        tsdbFileSetInit(pSet, op->fid);
      }
H
Hongze Cheng 已提交
436 437
    }

H
Hongze Cheng 已提交
438
    code = tsdbFileSetEdit(pSet, op);
H
Hongze Cheng 已提交
439
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
440 441
  }

H
Hongze Cheng 已提交
442
_exit:
H
Hongze Cheng 已提交
443 444 445
  return 0;
}

H
Hongze Cheng 已提交
446
int32_t tsdbOpenFS(STsdb *pTsdb, STFileSystem **fs, int8_t rollback) {
H
Hongze Cheng 已提交
447 448 449
  int32_t code;
  int32_t lino;

H
Hongze Cheng 已提交
450
  code = create_fs(pTsdb, fs);
H
Hongze Cheng 已提交
451 452
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
453
  code = open_fs(fs[0], rollback);
H
Hongze Cheng 已提交
454
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
455 456 457

_exit:
  if (code) {
H
Hongze Cheng 已提交
458
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
459
    destroy_fs(fs);
H
Hongze Cheng 已提交
460
  } else {
H
Hongze Cheng 已提交
461
    tsdbInfo("vgId:%d %s success", TD_VID(pTsdb->pVnode), __func__);
H
Hongze Cheng 已提交
462 463 464 465
  }
  return 0;
}

H
Hongze Cheng 已提交
466
int32_t tsdbCloseFS(STFileSystem **ppFS) {
H
Hongze Cheng 已提交
467 468
  if (ppFS[0] == NULL) return 0;
  close_file_system(ppFS[0]);
H
Hongze Cheng 已提交
469
  destroy_fs(ppFS);
H
Hongze Cheng 已提交
470 471
  return 0;
}
H
Hongze Cheng 已提交
472

H
Hongze Cheng 已提交
473 474 475 476 477 478
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 已提交
479
  int32_t code = 0;
H
Hongze Cheng 已提交
480
  int32_t lino;
H
Hongze Cheng 已提交
481
  char    current_t[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
482

H
Hongze Cheng 已提交
483 484 485 486 487 488 489
  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 已提交
490

H
Hongze Cheng 已提交
491
  fs->etype = etype;
H
Hongze Cheng 已提交
492
  fs->eid = eid;
H
Hongze Cheng 已提交
493

H
Hongze Cheng 已提交
494 495 496
  // edit
  code = edit_fs(fs, aFileOp);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
497

H
Hongze Cheng 已提交
498
  // save fs
H
Hongze Cheng 已提交
499
  code = save_fs(fs->nstate, current_t);
H
Hongze Cheng 已提交
500
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
501 502 503

_exit:
  if (code) {
H
Hongze Cheng 已提交
504 505
    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 已提交
506
  } else {
H
Hongze Cheng 已提交
507
    tsdbInfo("vgId:%d %s done, eid:%" PRId64 " etype:%d", TD_VID(fs->pTsdb->pVnode), __func__, fs->eid, etype);
H
Hongze Cheng 已提交
508 509 510 511
  }
  return code;
}

H
Hongze Cheng 已提交
512 513 514
int32_t tsdbFSEditCommit(STFileSystem *fs) {
  int32_t code = commit_edit(fs);
  tsem_post(&fs->canEdit);
H
Hongze Cheng 已提交
515 516 517
  return code;
}

H
Hongze Cheng 已提交
518 519 520
int32_t tsdbFSEditAbort(STFileSystem *fs) {
  int32_t code = abort_edit(fs);
  tsem_post(&fs->canEdit);
H
Hongze Cheng 已提交
521
  return code;
H
Hongze Cheng 已提交
522 523 524 525 526 527
}

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