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 20 21 22 23 24 25 26 27
#define TSDB_FS_EDIT_MIN TSDB_FS_EDIT_COMMIT
#define TSDB_FS_EDIT_MAX (TSDB_FS_EDIT_MERGE + 1)

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 36 37 38 39 40
} EFCurrentT;

static const char *gCurrentFname[] = {
    [TSDB_FCURRENT] = "current.json",
    [TSDB_FCURRENT_C] = "current.json.0",
    [TSDB_FCURRENT_M] = "current.json.1",
};

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]->nextEditId = 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 197 198 199 200 201 202 203 204
  // 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);

    cJSON *tjson = cJSON_CreateObject();
    if (tjson == NULL) {
      code = TSDB_CODE_OUT_OF_MEMORY;
      TSDB_CHECK_CODE(code, lino, _exit)
    }

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

    cJSON_AddItemToArray(ajson, tjson);
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 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
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 {
    code = TSDB_CODE_FILE_CORRUPTED;
    TSDB_CHECK_CODE(code, lino, _exit)
  }

  /* eid */
  item = cJSON_GetObjectItem(json, "eid");
  if (cJSON_IsNumber(item)) {
    eid[0] = item->valuedouble;
  } else {
    code = TSDB_CODE_FILE_CORRUPTED;
    TSDB_CHECK_CODE(code, lino, _exit)
  }

  /* fset */
  item = cJSON_GetObjectItem(json, "fset");
  if (cJSON_IsArray(item)) {
    const cJSON *titem;
    cJSON_ArrayForEach(titem, item) {
      STFileSet *pFileSet = taosArrayReserve(aTFileSet, 1);
      if (pFileSet == NULL) {
        code = TSDB_CODE_OUT_OF_MEMORY;
        TSDB_CHECK_CODE(code, lino, _exit);
      }

      code = tsdbFileSetFromJson(titem, pFileSet);
      TSDB_CHECK_CODE(code, lino, _exit)
    }
  } else {
    code = TSDB_CODE_FILE_CORRUPTED;
    TSDB_CHECK_CODE(code, lino, _exit)
  }

_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 已提交
278 279
}

H
Hongze Cheng 已提交
280
static int32_t commit_edit(STFileSystem *pFS, tsdb_fs_edit_t etype) {
H
Hongze Cheng 已提交
281 282 283 284
  int32_t code;
  char    ofname[TSDB_FILENAME_LEN];
  char    nfname[TSDB_FILENAME_LEN];

H
Hongze Cheng 已提交
285 286
  current_fname(pFS->pTsdb, nfname, TSDB_FCURRENT);
  current_fname(pFS->pTsdb, ofname, etype == TSDB_FS_EDIT_COMMIT ? TSDB_FCURRENT_C : TSDB_FCURRENT_M);
H
Hongze Cheng 已提交
287 288 289 290 291 292 293 294 295

  code = taosRenameFile(ofname, nfname);
  if (code) {
    code = TAOS_SYSTEM_ERROR(code);
    return code;
  }

  ASSERTS(0, "TODO: Do changes to pFS");

H
Hongze Cheng 已提交
296 297 298
  return 0;
}

H
Hongze Cheng 已提交
299
static int32_t abort_edit(STFileSystem *pFS, tsdb_fs_edit_t etype) {
H
Hongze Cheng 已提交
300 301 302
  int32_t code;
  char    fname[TSDB_FILENAME_LEN];

H
Hongze Cheng 已提交
303
  current_fname(pFS->pTsdb, fname, etype == TSDB_FS_EDIT_COMMIT ? TSDB_FCURRENT_C : TSDB_FCURRENT_M);
H
Hongze Cheng 已提交
304 305 306 307 308 309 310

  code = taosRemoveFile(fname);
  if (code) code = TAOS_SYSTEM_ERROR(code);

  return code;
}

H
Hongze Cheng 已提交
311
static int32_t scan_file_system(STFileSystem *pFS) {
H
Hongze Cheng 已提交
312 313 314 315
  // ASSERTS(0, "TODO: Not implemented yet");
  return 0;
}

H
Hongze Cheng 已提交
316
static int32_t scan_and_schedule_merge(STFileSystem *pFS) {
H
Hongze Cheng 已提交
317
  // ASSERTS(0, "TODO: Not implemented yet");
H
Hongze Cheng 已提交
318 319 320
  return 0;
}

H
Hongze Cheng 已提交
321 322 323 324 325 326
static int32_t update_fs_if_needed(STFileSystem *pFS) {
  // TODO
  return 0;
}

static int32_t open_fs(STFileSystem *pFS, int8_t rollback) {
H
Hongze Cheng 已提交
327
  int32_t code = 0;
H
Hongze Cheng 已提交
328
  int32_t lino = 0;
H
Hongze Cheng 已提交
329 330
  STsdb  *pTsdb = pFS->pTsdb;

H
Hongze Cheng 已提交
331 332
  code = update_fs_if_needed(pFS);
  TSDB_CHECK_CODE(code, lino, _exit)
H
Hongze Cheng 已提交
333

H
Hongze Cheng 已提交
334 335 336
  char fCurrent[TSDB_FILENAME_LEN];
  char cCurrent[TSDB_FILENAME_LEN];
  char mCurrent[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
337

H
Hongze Cheng 已提交
338 339
  current_fname(pTsdb, fCurrent, TSDB_FCURRENT);
  current_fname(pTsdb, cCurrent, TSDB_FCURRENT_C);
H
Hongze Cheng 已提交
340
  current_fname(pTsdb, mCurrent, TSDB_FCURRENT_M);
H
Hongze Cheng 已提交
341

H
Hongze Cheng 已提交
342
  if (taosCheckExistFile(fCurrent)) {  // current.json exists
H
Hongze Cheng 已提交
343
    code = load_fs(fCurrent, pFS->cstate, &pFS->nextEditId);
H
Hongze Cheng 已提交
344
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
345

H
Hongze Cheng 已提交
346 347 348 349 350 351 352
    // check current.json.commit existence
    if (taosCheckExistFile(cCurrent)) {
      if (rollback) {
        code = commit_edit(pFS, TSDB_FS_EDIT_COMMIT);
        TSDB_CHECK_CODE(code, lino, _exit);
      } else {
        code = abort_edit(pFS, TSDB_FS_EDIT_COMMIT);
H
Hongze Cheng 已提交
353 354
        TSDB_CHECK_CODE(code, lino, _exit);
      }
H
Hongze Cheng 已提交
355 356 357 358 359
    }

    // check current.json.t existence
    if (taosCheckExistFile(mCurrent)) {
      code = abort_edit(pFS, TSDB_FS_EDIT_MERGE);
H
Hongze Cheng 已提交
360 361
      TSDB_CHECK_CODE(code, lino, _exit);
    }
H
Hongze Cheng 已提交
362 363 364 365 366 367

    code = scan_file_system(pFS);
    TSDB_CHECK_CODE(code, lino, _exit);

    code = scan_and_schedule_merge(pFS);
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
368
  } else {
H
Hongze Cheng 已提交
369
    code = save_fs(0, pFS->nstate, fCurrent);
H
Hongze Cheng 已提交
370
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
371 372 373 374
  }

_exit:
  if (code) {
H
Hongze Cheng 已提交
375
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
376
  } else {
H
Hongze Cheng 已提交
377
    tsdbInfo("vgId:%d %s success", TD_VID(pTsdb->pVnode), __func__);
H
Hongze Cheng 已提交
378
  }
H
Hongze Cheng 已提交
379 380 381
  return 0;
}

H
Hongze Cheng 已提交
382
static int32_t close_file_system(STFileSystem *pFS) {
H
Hongze Cheng 已提交
383
  ASSERTS(0, "TODO: Not implemented yet");
H
Hongze Cheng 已提交
384 385
  return 0;
}
H
Hongze Cheng 已提交
386

H
Hongze Cheng 已提交
387
static int32_t apply_edit(STFileSystem *pFS) {
H
Hongze Cheng 已提交
388
  int32_t code = 0;
H
Hongze Cheng 已提交
389
  ASSERTS(0, "TODO: Not implemented yet");
H
Hongze Cheng 已提交
390
  return code;
H
Hongze Cheng 已提交
391 392
}

H
Hongze Cheng 已提交
393
static int32_t fset_cmpr_fn(const struct STFileSet *pSet1, const struct STFileSet *pSet2) {
H
Hongze Cheng 已提交
394 395 396 397 398 399 400 401
  if (pSet1->fid < pSet2->fid) {
    return -1;
  } else if (pSet1->fid > pSet2->fid) {
    return 1;
  }
  return 0;
}

H
Hongze Cheng 已提交
402
static int32_t edit_fs(STFileSystem *pFS, const SArray *aFileOp) {
H
Hongze Cheng 已提交
403 404 405
  int32_t code = 0;
  int32_t lino;

H
Hongze Cheng 已提交
406
  taosArrayClearEx(pFS->nstate, NULL /* TODO */);
H
Hongze Cheng 已提交
407 408 409

  // TODO: copy current state to new state

H
Hongze Cheng 已提交
410 411
  for (int32_t iop = 0; iop < taosArrayGetSize(aFileOp); iop++) {
    struct SFileOp *pOp = taosArrayGet(aFileOp, iop);
H
Hongze Cheng 已提交
412

H
Hongze Cheng 已提交
413
    struct STFileSet tmpSet = {.fid = pOp->fid};
H
Hongze Cheng 已提交
414 415

    int32_t idx = taosArraySearchIdx(  //
H
Hongze Cheng 已提交
416
        pFS->nstate,                   //
H
Hongze Cheng 已提交
417 418 419 420
        &tmpSet,                       //
        (__compar_fn_t)fset_cmpr_fn,   //
        TD_GE);

H
Hongze Cheng 已提交
421
    struct STFileSet *pSet;
H
Hongze Cheng 已提交
422 423
    if (idx < 0) {
      pSet = NULL;
H
Hongze Cheng 已提交
424
      idx = taosArrayGetSize(pFS->nstate);
H
Hongze Cheng 已提交
425
    } else {
H
Hongze Cheng 已提交
426
      pSet = taosArrayGet(pFS->nstate, idx);
H
Hongze Cheng 已提交
427 428 429 430 431 432 433 434 435
    }

    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 已提交
436
      if (taosArrayInsert(pFS->nstate, idx, pSet) == NULL) {
H
Hongze Cheng 已提交
437 438 439 440 441 442
        code = TSDB_CODE_OUT_OF_MEMORY;
        TSDB_CHECK_CODE(code, lino, _exit);
      }
    }

    // do opration on file set
H
Hongze Cheng 已提交
443
    TSDB_CHECK_CODE(                        //
H
Hongze Cheng 已提交
444
        code = tsdbFileSetEdit(pSet, pOp),  //
H
Hongze Cheng 已提交
445 446
        lino,                               //
        _exit);
H
Hongze Cheng 已提交
447 448
  }

H
Hongze Cheng 已提交
449 450
  // TODO: write new state to file

H
Hongze Cheng 已提交
451
_exit:
H
Hongze Cheng 已提交
452 453 454
  return 0;
}

H
Hongze Cheng 已提交
455
int32_t tsdbOpenFileSystem(STsdb *pTsdb, STFileSystem **ppFS, int8_t rollback) {
H
Hongze Cheng 已提交
456 457 458
  int32_t code;
  int32_t lino;

H
Hongze Cheng 已提交
459
  code = create_fs(pTsdb, ppFS);
H
Hongze Cheng 已提交
460 461
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
462
  code = open_fs(ppFS[0], rollback);
H
Hongze Cheng 已提交
463
  TSDB_CHECK_CODE(code, lino, _exit)
H
Hongze Cheng 已提交
464 465 466

_exit:
  if (code) {
H
Hongze Cheng 已提交
467
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
468
    destroy_fs(ppFS);
H
Hongze Cheng 已提交
469
  } else {
H
Hongze Cheng 已提交
470
    tsdbInfo("vgId:%d %s success", TD_VID(pTsdb->pVnode), __func__);
H
Hongze Cheng 已提交
471 472 473 474
  }
  return 0;
}

H
Hongze Cheng 已提交
475
int32_t tsdbCloseFileSystem(STFileSystem **ppFS) {
H
Hongze Cheng 已提交
476 477
  if (ppFS[0] == NULL) return 0;
  close_file_system(ppFS[0]);
H
Hongze Cheng 已提交
478
  destroy_fs(ppFS);
H
Hongze Cheng 已提交
479 480
  return 0;
}
H
Hongze Cheng 已提交
481

H
Hongze Cheng 已提交
482
int32_t tsdbFileSystemEditBegin(STFileSystem *pFS, const SArray *aFileOp, tsdb_fs_edit_t etype) {
H
Hongze Cheng 已提交
483
  int32_t code = 0;
H
Hongze Cheng 已提交
484
  int32_t lino;
H
Hongze Cheng 已提交
485 486
  char    fname[TSDB_FILENAME_LEN];

H
Hongze Cheng 已提交
487
  // current_fname(pFS->pTsdb, fname, etype == TSDB_FS_EDIT_COMMIT ? TSDB_FCURRENT_C : TSDB_FCURRENT_M);
H
Hongze Cheng 已提交
488

H
Hongze Cheng 已提交
489
  // tsem_wait(&pFS->canEdit);
H
Hongze Cheng 已提交
490

H
Hongze Cheng 已提交
491 492 493 494
  // TSDB_CHECK_CODE(                   //
  //     code = edit_fs(pFS, aFileOp),  //
  //     lino,                          //
  //     _exit);
H
Hongze Cheng 已提交
495

H
Hongze Cheng 已提交
496 497 498 499
  // TSDB_CHECK_CODE(                 //
  //     code = save_fs(pFS, fname),  //
  //     lino,                        //
  //     _exit);
H
Hongze Cheng 已提交
500 501 502 503 504 505 506 507

_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s",  //
              TD_VID(pFS->pTsdb->pVnode),               //
              __func__,                                 //
              lino,                                     //
              tstrerror(code));
H
Hongze Cheng 已提交
508 509 510 511 512
  } else {
    tsdbInfo("vgId:%d %s done, etype:%d",  //
             TD_VID(pFS->pTsdb->pVnode),   //
             __func__,                     //
             etype);
H
Hongze Cheng 已提交
513 514 515 516
  }
  return code;
}

H
Hongze Cheng 已提交
517
int32_t tsdbFileSystemEditCommit(STFileSystem *pFS, tsdb_fs_edit_t etype) {
H
Hongze Cheng 已提交
518
  int32_t code = commit_edit(pFS, etype);
H
Hongze Cheng 已提交
519
  tsem_post(&pFS->canEdit);
H
Hongze Cheng 已提交
520
  if (code) {
H
Hongze Cheng 已提交
521 522 523
    tsdbError("vgId:%d %s failed since %s",  //
              TD_VID(pFS->pTsdb->pVnode),    //
              __func__,                      //
H
Hongze Cheng 已提交
524
              tstrerror(code));
H
Hongze Cheng 已提交
525 526 527 528 529
  } else {
    tsdbInfo("vgId:%d %s done, etype:%d",  //
             TD_VID(pFS->pTsdb->pVnode),   //
             __func__,                     //
             etype);
H
Hongze Cheng 已提交
530 531 532 533
  }
  return code;
}

H
Hongze Cheng 已提交
534
int32_t tsdbFileSystemEditAbort(STFileSystem *pFS, tsdb_fs_edit_t etype) {
H
Hongze Cheng 已提交
535
  int32_t code = abort_edit(pFS, etype);
H
Hongze Cheng 已提交
536
  if (code) {
H
Hongze Cheng 已提交
537 538 539 540 541
    tsdbError("vgId:%d %s failed since %s, etype:%d",  //
              TD_VID(pFS->pTsdb->pVnode),              //
              __func__,                                //
              tstrerror(code),                         //
              etype);
H
Hongze Cheng 已提交
542
  } else {
H
Hongze Cheng 已提交
543
  }
H
Hongze Cheng 已提交
544
  tsem_post(&pFS->canEdit);
H
Hongze Cheng 已提交
545 546
  return code;
}