tsdbFS.c 14.1 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 40
};

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

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

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

H
Hongze Cheng 已提交
60 61 62
  return 0;
}

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

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

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

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

H
Hongze Cheng 已提交
100 101 102
_exit:
  return code;
}
H
Hongze Cheng 已提交
103

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

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

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

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

H
Hongze Cheng 已提交
126
  taosCloseFile(&fp);
H
Hongze Cheng 已提交
127 128

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

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

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

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

H
Hongze Cheng 已提交
183 184 185 186
  // eid
  if (cJSON_AddNumberToObject(json, "eid", eid) == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    TSDB_CHECK_CODE(code, lino, _exit)
H
Hongze Cheng 已提交
187 188
  }

H
Hongze Cheng 已提交
189 190 191 192 193 194 195 196
  // fset
  cJSON *ajson = cJSON_AddArrayToObject(json, "fset");
  if (ajson == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    TSDB_CHECK_CODE(code, lino, _exit)
  }
  for (int32_t i = 0; i < taosArrayGetSize(aTFileSet); i++) {
    STFileSet *pFileSet = (STFileSet *)taosArrayGet(aTFileSet, i);
H
Hongze Cheng 已提交
197
    cJSON     *item;
H
Hongze Cheng 已提交
198

H
Hongze Cheng 已提交
199
    if ((item = cJSON_CreateObject()) == NULL) {
H
Hongze Cheng 已提交
200 201 202 203
      code = TSDB_CODE_OUT_OF_MEMORY;
      TSDB_CHECK_CODE(code, lino, _exit)
    }

H
Hongze Cheng 已提交
204
    code = tsdbFileSetToJson(pFileSet, item);
H
Hongze Cheng 已提交
205
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
206

H
Hongze Cheng 已提交
207
    cJSON_AddItemToArray(ajson, item);
H
Hongze Cheng 已提交
208 209
  }

H
Hongze Cheng 已提交
210 211
  code = save_json(json, fname);
  TSDB_CHECK_CODE(code, lino, _exit)
H
Hongze Cheng 已提交
212 213 214

_exit:
  if (code) {
H
Hongze Cheng 已提交
215
    tsdbError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
216
  }
H
Hongze Cheng 已提交
217
  cJSON_Delete(json);
H
Hongze Cheng 已提交
218
  return code;
H
Hongze Cheng 已提交
219 220
}

H
Hongze Cheng 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
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);
  TSDB_CHECK_CODE(code, lino, _exit)

  // parse json
  const cJSON *item;

  /* fmtv */
  item = cJSON_GetObjectItem(json, "fmtv");
  if (cJSON_IsNumber(item)) {
    ASSERT(item->valuedouble == 1);
  } else {
H
Hongze Cheng 已提交
240
    TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit)
H
Hongze Cheng 已提交
241 242 243 244 245 246 247
  }

  /* eid */
  item = cJSON_GetObjectItem(json, "eid");
  if (cJSON_IsNumber(item)) {
    eid[0] = item->valuedouble;
  } else {
H
Hongze Cheng 已提交
248
    TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit)
H
Hongze Cheng 已提交
249 250 251 252 253 254 255
  }

  /* fset */
  item = cJSON_GetObjectItem(json, "fset");
  if (cJSON_IsArray(item)) {
    const cJSON *titem;
    cJSON_ArrayForEach(titem, item) {
H
Hongze Cheng 已提交
256 257 258
      STFileSet *pFileSet;
      if ((pFileSet = taosArrayReserve(aTFileSet, 1)) == NULL) {
        TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
H
Hongze Cheng 已提交
259 260 261 262 263 264
      }

      code = tsdbFileSetFromJson(titem, pFileSet);
      TSDB_CHECK_CODE(code, lino, _exit)
    }
  } else {
H
Hongze Cheng 已提交
265
    TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit)
H
Hongze Cheng 已提交
266 267 268 269 270 271 272 273
  }

_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 已提交
274 275
}

H
Hongze Cheng 已提交
276 277 278 279 280 281
static int32_t apply_commit(STFileSystem *fs) {
  // TODO
  return 0;
}

static int32_t commit_edit(STFileSystem *fs) {
H
Hongze Cheng 已提交
282 283
  char current[TSDB_FILENAME_LEN];
  char current_t[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
284

H
Hongze Cheng 已提交
285 286 287 288 289
  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 已提交
290 291
  } else {
    ASSERT(0);
H
Hongze Cheng 已提交
292 293
  }

H
Hongze Cheng 已提交
294 295 296 297 298
  int32_t code;
  int32_t lino;
  if ((code = taosRenameFile(current_t, current))) {
    TSDB_CHECK_CODE(code = TAOS_SYSTEM_ERROR(code), lino, _exit)
  }
H
Hongze Cheng 已提交
299

H
Hongze Cheng 已提交
300 301 302
  code = apply_commit(fs);
  TSDB_CHECK_CODE(code, lino, _exit)

H
Hongze Cheng 已提交
303 304
_exit:
  if (code) {
H
Hongze Cheng 已提交
305 306 307
    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 已提交
308 309
  }
  return code;
H
Hongze Cheng 已提交
310 311
}

H
Hongze Cheng 已提交
312 313 314 315 316 317 318
// static int32_t
static int32_t apply_abort(STFileSystem *fs) {
  // TODO
  return 0;
}

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

H
Hongze Cheng 已提交
321 322 323 324
  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 已提交
325 326 327
  } else {
    ASSERT(0);
  }
H
Hongze Cheng 已提交
328

H
Hongze Cheng 已提交
329 330 331 332 333
  int32_t code;
  int32_t lino;
  if ((code = taosRemoveFile(fname))) {
    TSDB_CHECK_CODE(code = TAOS_SYSTEM_ERROR(code), lino, _exit)
  }
H
Hongze Cheng 已提交
334

H
Hongze Cheng 已提交
335 336 337 338 339 340 341 342 343 344
  code = apply_abort(fs);
  TSDB_CHECK_CODE(code, lino, _exit)

_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 已提交
345 346
}

H
Hongze Cheng 已提交
347
static int32_t scan_and_fix_fs(STFileSystem *pFS) {
H
Hongze Cheng 已提交
348
  // TODO
H
Hongze Cheng 已提交
349 350 351
  return 0;
}

H
Hongze Cheng 已提交
352 353 354 355 356
static int32_t update_fs_if_needed(STFileSystem *pFS) {
  // TODO
  return 0;
}

H
Hongze Cheng 已提交
357
static int32_t open_fs(STFileSystem *fs, int8_t rollback) {
H
Hongze Cheng 已提交
358
  int32_t code = 0;
H
Hongze Cheng 已提交
359
  int32_t lino = 0;
H
Hongze Cheng 已提交
360
  STsdb  *pTsdb = fs->pTsdb;
H
Hongze Cheng 已提交
361

H
Hongze Cheng 已提交
362
  code = update_fs_if_needed(fs);
H
Hongze Cheng 已提交
363
  TSDB_CHECK_CODE(code, lino, _exit)
H
Hongze Cheng 已提交
364

H
Hongze Cheng 已提交
365 366 367
  char fCurrent[TSDB_FILENAME_LEN];
  char cCurrent[TSDB_FILENAME_LEN];
  char mCurrent[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
368

H
Hongze Cheng 已提交
369 370
  current_fname(pTsdb, fCurrent, TSDB_FCURRENT);
  current_fname(pTsdb, cCurrent, TSDB_FCURRENT_C);
H
Hongze Cheng 已提交
371
  current_fname(pTsdb, mCurrent, TSDB_FCURRENT_M);
H
Hongze Cheng 已提交
372

H
Hongze Cheng 已提交
373
  if (taosCheckExistFile(fCurrent)) {  // current.json exists
H
Hongze Cheng 已提交
374
    code = load_fs(fCurrent, fs->cstate, &fs->neid);
H
Hongze Cheng 已提交
375
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
376

H
Hongze Cheng 已提交
377
    if (taosCheckExistFile(cCurrent)) {
H
Hongze Cheng 已提交
378 379 380
      // current.c.json exists

      fs->etype = TSDB_FEDIT_COMMIT;
H
Hongze Cheng 已提交
381
      if (rollback) {
H
Hongze Cheng 已提交
382
        code = abort_edit(fs);
H
Hongze Cheng 已提交
383 384
        TSDB_CHECK_CODE(code, lino, _exit);
      } else {
H
Hongze Cheng 已提交
385 386 387 388
        code = load_fs(cCurrent, fs->nstate, &fs->eid);
        TSDB_CHECK_CODE(code, lino, _exit)

        code = commit_edit(fs);
H
Hongze Cheng 已提交
389 390
        TSDB_CHECK_CODE(code, lino, _exit);
      }
H
Hongze Cheng 已提交
391 392 393 394
    } else if (taosCheckExistFile(mCurrent)) {
      // current.m.json exists
      fs->etype = TSDB_FEDIT_MERGE;
      code = abort_edit(fs);
H
Hongze Cheng 已提交
395 396
      TSDB_CHECK_CODE(code, lino, _exit);
    }
H
Hongze Cheng 已提交
397

H
Hongze Cheng 已提交
398
    code = scan_and_fix_fs(fs);
H
Hongze Cheng 已提交
399
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
400
  } else {
H
Hongze Cheng 已提交
401
    code = save_fs(0, fs->nstate, fCurrent);
H
Hongze Cheng 已提交
402
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
403 404 405 406
  }

_exit:
  if (code) {
H
Hongze Cheng 已提交
407
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
408
  } else {
H
Hongze Cheng 已提交
409
    tsdbInfo("vgId:%d %s success", TD_VID(pTsdb->pVnode), __func__);
H
Hongze Cheng 已提交
410
  }
H
Hongze Cheng 已提交
411 412 413
  return 0;
}

H
Hongze Cheng 已提交
414
static int32_t close_file_system(STFileSystem *pFS) {
H
Hongze Cheng 已提交
415
  ASSERTS(0, "TODO: Not implemented yet");
H
Hongze Cheng 已提交
416 417
  return 0;
}
H
Hongze Cheng 已提交
418

H
Hongze Cheng 已提交
419
static int32_t apply_edit(STFileSystem *pFS) {
H
Hongze Cheng 已提交
420
  int32_t code = 0;
H
Hongze Cheng 已提交
421
  ASSERTS(0, "TODO: Not implemented yet");
H
Hongze Cheng 已提交
422
  return code;
H
Hongze Cheng 已提交
423 424
}

H
Hongze Cheng 已提交
425
static int32_t fset_cmpr_fn(const struct STFileSet *pSet1, const struct STFileSet *pSet2) {
H
Hongze Cheng 已提交
426 427 428 429 430 431 432 433
  if (pSet1->fid < pSet2->fid) {
    return -1;
  } else if (pSet1->fid > pSet2->fid) {
    return 1;
  }
  return 0;
}

H
Hongze Cheng 已提交
434
static int32_t edit_fs(STFileSystem *pFS, const SArray *aFileOp) {
H
Hongze Cheng 已提交
435 436 437
  int32_t code = 0;
  int32_t lino;

H
Hongze Cheng 已提交
438
  taosArrayClearEx(pFS->nstate, NULL /* TODO */);
H
Hongze Cheng 已提交
439 440 441

  // TODO: copy current state to new state

H
Hongze Cheng 已提交
442
  for (int32_t iop = 0; iop < taosArrayGetSize(aFileOp); iop++) {
H
Hongze Cheng 已提交
443
    struct STFileOp *pOp = taosArrayGet(aFileOp, iop);
H
Hongze Cheng 已提交
444

H
Hongze Cheng 已提交
445
    struct STFileSet tmpSet = {.fid = pOp->fid};
H
Hongze Cheng 已提交
446 447

    int32_t idx = taosArraySearchIdx(  //
H
Hongze Cheng 已提交
448
        pFS->nstate,                   //
H
Hongze Cheng 已提交
449 450 451 452
        &tmpSet,                       //
        (__compar_fn_t)fset_cmpr_fn,   //
        TD_GE);

H
Hongze Cheng 已提交
453
    struct STFileSet *pSet;
H
Hongze Cheng 已提交
454 455
    if (idx < 0) {
      pSet = NULL;
H
Hongze Cheng 已提交
456
      idx = taosArrayGetSize(pFS->nstate);
H
Hongze Cheng 已提交
457
    } else {
H
Hongze Cheng 已提交
458
      pSet = taosArrayGet(pFS->nstate, idx);
H
Hongze Cheng 已提交
459 460 461 462 463 464 465 466 467
    }

    if (pSet == NULL || pSet->fid != pOp->fid) {
      ASSERTS(pOp->op == TSDB_FOP_CREATE, "BUG: Invalid file operation");
      TSDB_CHECK_CODE(                                //
          code = tsdbFileSetCreate(pOp->fid, &pSet),  //
          lino,                                       //
          _exit);

H
Hongze Cheng 已提交
468
      if (taosArrayInsert(pFS->nstate, idx, pSet) == NULL) {
H
Hongze Cheng 已提交
469 470 471 472 473 474
        code = TSDB_CODE_OUT_OF_MEMORY;
        TSDB_CHECK_CODE(code, lino, _exit);
      }
    }

    // do opration on file set
H
Hongze Cheng 已提交
475
    TSDB_CHECK_CODE(                        //
H
Hongze Cheng 已提交
476
        code = tsdbFileSetEdit(pSet, pOp),  //
H
Hongze Cheng 已提交
477 478
        lino,                               //
        _exit);
H
Hongze Cheng 已提交
479 480
  }

H
Hongze Cheng 已提交
481 482
  // TODO: write new state to file

H
Hongze Cheng 已提交
483
_exit:
H
Hongze Cheng 已提交
484 485 486
  return 0;
}

H
Hongze Cheng 已提交
487
int32_t tsdbOpenFS(STsdb *pTsdb, STFileSystem **ppFS, int8_t rollback) {
H
Hongze Cheng 已提交
488 489 490
  int32_t code;
  int32_t lino;

H
Hongze Cheng 已提交
491
  code = create_fs(pTsdb, ppFS);
H
Hongze Cheng 已提交
492 493
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
494
  code = open_fs(ppFS[0], rollback);
H
Hongze Cheng 已提交
495
  TSDB_CHECK_CODE(code, lino, _exit)
H
Hongze Cheng 已提交
496 497 498

_exit:
  if (code) {
H
Hongze Cheng 已提交
499
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
500
    destroy_fs(ppFS);
H
Hongze Cheng 已提交
501
  } else {
H
Hongze Cheng 已提交
502
    tsdbInfo("vgId:%d %s success", TD_VID(pTsdb->pVnode), __func__);
H
Hongze Cheng 已提交
503 504 505 506
  }
  return 0;
}

H
Hongze Cheng 已提交
507
int32_t tsdbCloseFS(STFileSystem **ppFS) {
H
Hongze Cheng 已提交
508 509
  if (ppFS[0] == NULL) return 0;
  close_file_system(ppFS[0]);
H
Hongze Cheng 已提交
510
  destroy_fs(ppFS);
H
Hongze Cheng 已提交
511 512
  return 0;
}
H
Hongze Cheng 已提交
513

H
Hongze Cheng 已提交
514
int32_t tsdbFSEditBegin(STFileSystem *fs, const SArray *aFileOp, EFEditT etype) {
H
Hongze Cheng 已提交
515
  int32_t code = 0;
H
Hongze Cheng 已提交
516
  int32_t lino;
H
Hongze Cheng 已提交
517
  char    current_t[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
518

H
Hongze Cheng 已提交
519 520 521 522 523 524 525
  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 已提交
526

H
Hongze Cheng 已提交
527 528
  fs->etype = etype;
  fs->eid = ++fs->neid;
H
Hongze Cheng 已提交
529

H
Hongze Cheng 已提交
530 531 532
  // edit
  code = edit_fs(fs, aFileOp);
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
533

H
Hongze Cheng 已提交
534 535 536
  // save fs
  code = save_fs(fs->eid, fs->nstate, current_t);
  TSDB_CHECK_CODE(code, lino, _exit)
H
Hongze Cheng 已提交
537 538 539

_exit:
  if (code) {
H
Hongze Cheng 已提交
540 541
    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 已提交
542
  } else {
H
Hongze Cheng 已提交
543
    tsdbInfo("vgId:%d %s done, eid:%" PRId64 " etype:%d", TD_VID(fs->pTsdb->pVnode), __func__, fs->eid, etype);
H
Hongze Cheng 已提交
544 545 546 547
  }
  return code;
}

H
Hongze Cheng 已提交
548 549 550
int32_t tsdbFSEditCommit(STFileSystem *fs) {
  int32_t code = commit_edit(fs);
  tsem_post(&fs->canEdit);
H
Hongze Cheng 已提交
551 552 553
  return code;
}

H
Hongze Cheng 已提交
554 555 556
int32_t tsdbFSEditAbort(STFileSystem *fs) {
  int32_t code = abort_edit(fs);
  tsem_post(&fs->canEdit);
H
Hongze Cheng 已提交
557 558
  return code;
}