tsdbFS.c 14.9 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 17
#include "dev.h"

H
Hongze Cheng 已提交
18
static int32_t create_file_system(STsdb *pTsdb, struct STFileSystem **ppFS) {
H
Hongze Cheng 已提交
19 20 21 22
  if ((ppFS[0] = taosMemoryCalloc(1, sizeof(*ppFS[0]))) == NULL) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

H
Hongze Cheng 已提交
23 24 25 26
  if ((ppFS[0]->aFileSet = taosArrayInit(16, sizeof(struct SFileSet))) == NULL ||
      (ppFS[0]->nState = taosArrayInit(16, sizeof(struct SFileSet))) == NULL) {
    taosArrayDestroy(ppFS[0]->nState);
    taosArrayDestroy(ppFS[0]->aFileSet);
H
Hongze Cheng 已提交
27 28 29
    taosMemoryFree(ppFS[0]);
    return TSDB_CODE_OUT_OF_MEMORY;
  }
H
Hongze Cheng 已提交
30

H
Hongze Cheng 已提交
31
  ppFS[0]->pTsdb = pTsdb;
H
Hongze Cheng 已提交
32
  tsem_init(&ppFS[0]->canEdit, 0, 1);
H
Hongze Cheng 已提交
33

H
Hongze Cheng 已提交
34 35 36 37 38
  return 0;
}

static int32_t destroy_file_system(struct STFileSystem **ppFS) {
  if (ppFS[0]) {
H
Hongze Cheng 已提交
39
    taosArrayDestroy(ppFS[0]->aFileSet);
H
Hongze Cheng 已提交
40
    tsem_destroy(&ppFS[0]->canEdit);
H
Hongze Cheng 已提交
41 42 43 44 45 46
    taosMemoryFree(ppFS[0]);
    ppFS[0] = NULL;
  }
  return 0;
}

H
Hongze Cheng 已提交
47
static int32_t get_current_json(STsdb *pTsdb, char fname[]) {
H
Hongze Cheng 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
  if (pTsdb->pVnode->pTfs) {
    snprintf(fname,                                   //
             TSDB_FILENAME_LEN,                       //
             "%s%s%s%s%s",                            //
             tfsGetPrimaryPath(pTsdb->pVnode->pTfs),  //
             TD_DIRSEP,                               //
             pTsdb->path,                             //
             TD_DIRSEP,                               //
             "current.json");
  } else {
    snprintf(fname,              //
             TSDB_FILENAME_LEN,  //
             "%s%s%s",           //
             pTsdb->path,        //
             TD_DIRSEP,          //
             "current.json");
  }
H
Hongze Cheng 已提交
65
  return 0;
H
Hongze Cheng 已提交
66 67
}

H
Hongze Cheng 已提交
68
static int32_t get_current_temp(STsdb *pTsdb, char fname[], tsdb_fs_edit_t etype) {
H
Hongze Cheng 已提交
69 70
  switch (etype) {
    case TSDB_FS_EDIT_COMMIT:
H
Hongze Cheng 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
      if (pTsdb->pVnode->pTfs) {
        snprintf(fname,                                   //
                 TSDB_FILENAME_LEN,                       //
                 "%s%s%s%s%s",                            //
                 tfsGetPrimaryPath(pTsdb->pVnode->pTfs),  //
                 TD_DIRSEP,                               //
                 pTsdb->path,                             //
                 TD_DIRSEP,                               //
                 "current.json.commit");
      } else {
        snprintf(fname,              //
                 TSDB_FILENAME_LEN,  //
                 "%s%s%s",           //
                 pTsdb->path,        //
                 TD_DIRSEP,          //
                 "current.json.commit");
      }
H
Hongze Cheng 已提交
88 89
      break;
    default:
H
Hongze Cheng 已提交
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
      if (pTsdb->pVnode->pTfs) {
        snprintf(fname,                                   //
                 TSDB_FILENAME_LEN,                       //
                 "%s%s%s%s%s",                            //
                 tfsGetPrimaryPath(pTsdb->pVnode->pTfs),  //
                 TD_DIRSEP,                               //
                 pTsdb->path,                             //
                 TD_DIRSEP,                               //
                 "current.json.t");
      } else {
        snprintf(fname,              //
                 TSDB_FILENAME_LEN,  //
                 "%s%s%s",           //
                 pTsdb->path,        //
                 TD_DIRSEP,          //
                 "current.json.t");
      }
H
Hongze Cheng 已提交
107 108 109 110 111 112
      break;
  }

  return 0;
}

H
Hongze Cheng 已提交
113 114 115 116 117 118 119 120 121 122
static int32_t fs_to_json_str(struct STFileSystem *pFS, char **ppData) {
  int32_t code = 0;
  int32_t lino;

  cJSON *pJson = cJSON_CreateObject();
  if (pJson == NULL) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  /* format version */
H
Hongze Cheng 已提交
123 124 125
  TSDB_CHECK_NULL(                        //
      cJSON_AddNumberToObject(pJson,      //
                              "version",  //
H
Hongze Cheng 已提交
126 127 128 129 130 131 132
                              1 /* TODO */),
      code,   //
      lino,   //
      _exit,  //
      TSDB_CODE_OUT_OF_MEMORY);

  /* next edit id */
H
Hongze Cheng 已提交
133 134 135
  TSDB_CHECK_NULL(                        //
      cJSON_AddNumberToObject(pJson,      //
                              "edit id",  //
H
Hongze Cheng 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
                              pFS->nextEditId),
      code,   //
      lino,   //
      _exit,  //
      TSDB_CODE_OUT_OF_MEMORY);

  /* file sets */
  cJSON *aFileSetJson;
  TSDB_CHECK_NULL(                                                //
      aFileSetJson = cJSON_AddArrayToObject(pJson, "file sets"),  //
      code,                                                       //
      lino,                                                       //
      _exit,                                                      //
      TSDB_CODE_OUT_OF_MEMORY);

H
Hongze Cheng 已提交
151 152
  for (int32_t i = 0; i < taosArrayGetSize(pFS->nState); i++) {
    struct SFileSet *pFileSet = taosArrayGet(pFS->nState, i);
H
Hongze Cheng 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179

    code = tsdbFileSetToJson(aFileSetJson, pFileSet);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  ppData[0] = cJSON_Print(pJson);
  if (ppData[0] == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    TSDB_CHECK_CODE(code, lino, _exit);
  }

_exit:
  cJSON_Delete(pJson);
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s",  //
              TD_VID(pFS->pTsdb->pVnode),               //
              __func__,                                 //
              lino,                                     //
              tstrerror(code));
  }
  return code;
}

static int32_t fs_from_json_str(const char *pData, struct STFileSystem *pFS) {
  int32_t code = 0;
  int32_t lino;

H
Hongze Cheng 已提交
180
  ASSERTS(0, "TODO: Not implemented yet");
H
Hongze Cheng 已提交
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234

_exit:
  return code;
}

static int32_t save_fs_to_file(struct STFileSystem *pFS, const char *fname) {
  int32_t code = 0;
  int32_t lino;
  char   *pData = NULL;

  // to json string
  code = fs_to_json_str(pFS, &pData);
  TSDB_CHECK_CODE(code, lino, _exit);

  TdFilePtr fd = taosOpenFile(fname,                //
                              TD_FILE_WRITE         //
                                  | TD_FILE_CREATE  //
                                  | TD_FILE_TRUNC);
  if (fd == NULL) {
    code = TAOS_SYSTEM_ERROR(code);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  int64_t n = taosWriteFile(fd, pData, strlen(pData) + 1);
  if (n < 0) {
    code = TAOS_SYSTEM_ERROR(code);
    taosCloseFile(&fd);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  if (taosFsyncFile(fd) < 0) {
    code = TAOS_SYSTEM_ERROR(code);
    taosCloseFile(&fd);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

  taosCloseFile(&fd);

_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s",  //
              TD_VID(pFS->pTsdb->pVnode),               //
              __func__,                                 //
              lino,                                     //
              tstrerror(code));
  } else {
    tsdbDebug("vgId:%d %s success",        //
              TD_VID(pFS->pTsdb->pVnode),  //
              __func__);
  }
  if (pData) {
    taosMemoryFree(pData);
  }
  return code;
H
Hongze Cheng 已提交
235 236 237 238 239 240 241
}

static int32_t load_fs_from_file(const char *fname, struct STFileSystem *pFS) {
  ASSERTS(0, "TODO: Not implemented yet");
  return 0;
}

H
Hongze Cheng 已提交
242
static int32_t commit_edit(struct STFileSystem *pFS, tsdb_fs_edit_t etype) {
H
Hongze Cheng 已提交
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
  int32_t code;
  char    ofname[TSDB_FILENAME_LEN];
  char    nfname[TSDB_FILENAME_LEN];

  get_current_json(pFS->pTsdb, nfname);
  get_current_temp(pFS->pTsdb, ofname, etype);

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

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

H
Hongze Cheng 已提交
258 259 260
  return 0;
}

H
Hongze Cheng 已提交
261
static int32_t abort_edit(struct STFileSystem *pFS, tsdb_fs_edit_t etype) {
H
Hongze Cheng 已提交
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
  int32_t code;
  char    fname[TSDB_FILENAME_LEN];

  get_current_temp(pFS->pTsdb, fname, etype);

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

  return code;
}

static int32_t scan_file_system(struct STFileSystem *pFS) {
  // ASSERTS(0, "TODO: Not implemented yet");
  return 0;
}

static int32_t scan_and_schedule_merge(struct STFileSystem *pFS) {
  // ASSERTS(0, "TODO: Not implemented yet");
H
Hongze Cheng 已提交
280 281 282
  return 0;
}

H
Hongze Cheng 已提交
283
static int32_t open_file_system(struct STFileSystem *pFS, int8_t rollback) {
H
Hongze Cheng 已提交
284 285 286 287 288 289 290 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
  int32_t code = 0;
  int32_t lino;
  STsdb  *pTsdb = pFS->pTsdb;

  if (0) {
    ASSERTS(0, "Not implemented yet");
  } else {
    char current_json[TSDB_FILENAME_LEN];
    char current_json_commit[TSDB_FILENAME_LEN];
    char current_json_t[TSDB_FILENAME_LEN];

    get_current_json(pTsdb, current_json);
    get_current_temp(pTsdb, current_json_commit, TSDB_FS_EDIT_COMMIT);
    get_current_temp(pTsdb, current_json_t, TSDB_FS_EDIT_MERGE);

    if (taosCheckExistFile(current_json)) {  // current.json exists
      code = load_fs_from_file(current_json, pFS);
      TSDB_CHECK_CODE(code, lino, _exit);

      // check current.json.commit existence
      if (taosCheckExistFile(current_json_commit)) {
        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);
          TSDB_CHECK_CODE(code, lino, _exit);
        }
      }

      // check current.json.t existence
      if (taosCheckExistFile(current_json_t)) {
        code = abort_edit(pFS, TSDB_FS_EDIT_MERGE);
        TSDB_CHECK_CODE(code, lino, _exit);
      }
    } else {
      code = save_fs_to_file(pFS, current_json);
      TSDB_CHECK_CODE(code, lino, _exit);
    }
  }

H
Hongze Cheng 已提交
325 326 327 328 329 330
  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 已提交
331 332 333 334 335 336 337 338 339 340 341 342
_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s",  //
              TD_VID(pTsdb->pVnode),                    //
              __func__,                                 //
              lino,                                     //
              tstrerror(code));
  } else {
    tsdbInfo("vgId:%d %s success",   //
             TD_VID(pTsdb->pVnode),  //
             __func__);
  }
H
Hongze Cheng 已提交
343 344 345 346
  return 0;
}

static int32_t close_file_system(struct STFileSystem *pFS) {
H
Hongze Cheng 已提交
347
  ASSERTS(0, "TODO: Not implemented yet");
H
Hongze Cheng 已提交
348 349
  return 0;
}
H
Hongze Cheng 已提交
350

H
Hongze Cheng 已提交
351 352
static int32_t apply_edit(struct STFileSystem *pFS) {
  int32_t code = 0;
H
Hongze Cheng 已提交
353
  ASSERTS(0, "TODO: Not implemented yet");
H
Hongze Cheng 已提交
354
  return code;
H
Hongze Cheng 已提交
355 356
}

H
Hongze Cheng 已提交
357 358 359 360 361 362 363 364 365
static int32_t fset_cmpr_fn(const struct SFileSet *pSet1, const struct SFileSet *pSet2) {
  if (pSet1->fid < pSet2->fid) {
    return -1;
  } else if (pSet1->fid > pSet2->fid) {
    return 1;
  }
  return 0;
}

H
Hongze Cheng 已提交
366
static int32_t edit_fs(struct STFileSystem *pFS, const SArray *aFileOp) {
H
Hongze Cheng 已提交
367 368 369 370 371 372 373
  int32_t code = 0;
  int32_t lino;

  taosArrayClearEx(pFS->nState, NULL /* TODO */);

  // TODO: copy current state to new state

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

H
Hongze Cheng 已提交
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
    struct SFileSet tmpSet = {.fid = pOp->fid};

    int32_t idx = taosArraySearchIdx(  //
        pFS->nState,                   //
        &tmpSet,                       //
        (__compar_fn_t)fset_cmpr_fn,   //
        TD_GE);

    struct SFileSet *pSet;
    if (idx < 0) {
      pSet = NULL;
      idx = taosArrayGetSize(pFS->nState);
    } else {
      pSet = taosArrayGet(pFS->nState, idx);
    }

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

      if (taosArrayInsert(pFS->nState, idx, pSet) == NULL) {
        code = TSDB_CODE_OUT_OF_MEMORY;
        TSDB_CHECK_CODE(code, lino, _exit);
      }
    }

    // do opration on file set
H
Hongze Cheng 已提交
407
    TSDB_CHECK_CODE(                        //
H
Hongze Cheng 已提交
408
        code = tsdbFileSetEdit(pSet, pOp),  //
H
Hongze Cheng 已提交
409 410
        lino,                               //
        _exit);
H
Hongze Cheng 已提交
411 412
  }

H
Hongze Cheng 已提交
413 414
  // TODO: write new state to file

H
Hongze Cheng 已提交
415
_exit:
H
Hongze Cheng 已提交
416 417 418
  return 0;
}

H
Hongze Cheng 已提交
419
int32_t tsdbOpenFileSystem(STsdb *pTsdb, struct STFileSystem **ppFS, int8_t rollback) {
H
Hongze Cheng 已提交
420 421 422 423 424 425
  int32_t code;
  int32_t lino;

  code = create_file_system(pTsdb, ppFS);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
426
  code = open_file_system(ppFS[0], rollback);
H
Hongze Cheng 已提交
427 428 429 430
  if (code) {
    destroy_file_system(ppFS);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
431 432 433

_exit:
  if (code) {
H
Hongze Cheng 已提交
434 435 436 437 438
    tsdbError("vgId:%d %s failed at line %d since %s",  //
              TD_VID(pTsdb->pVnode),                    //
              __func__,                                 //
              lino,                                     //
              tstrerror(code));
H
Hongze Cheng 已提交
439
  } else {
H
Hongze Cheng 已提交
440 441 442
    tsdbInfo("vgId:%d %s success",   //
             TD_VID(pTsdb->pVnode),  //
             __func__);
H
Hongze Cheng 已提交
443 444 445 446 447 448 449 450 451 452
  }
  return 0;
}

int32_t tsdbCloseFileSystem(struct STFileSystem **ppFS) {
  if (ppFS[0] == NULL) return 0;
  close_file_system(ppFS[0]);
  destroy_file_system(ppFS);
  return 0;
}
H
Hongze Cheng 已提交
453

H
Hongze Cheng 已提交
454
int32_t tsdbFileSystemEditBegin(struct STFileSystem *pFS, const SArray *aFileOp, tsdb_fs_edit_t etype) {
H
Hongze Cheng 已提交
455
  int32_t code = 0;
H
Hongze Cheng 已提交
456
  int32_t lino;
H
Hongze Cheng 已提交
457 458
  char    fname[TSDB_FILENAME_LEN];

H
Hongze Cheng 已提交
459
  get_current_temp(pFS->pTsdb, fname, etype);
H
Hongze Cheng 已提交
460

H
Hongze Cheng 已提交
461
  tsem_wait(&pFS->canEdit);
H
Hongze Cheng 已提交
462

H
Hongze Cheng 已提交
463 464 465 466 467 468 469 470 471
  TSDB_CHECK_CODE(                   //
      code = edit_fs(pFS, aFileOp),  //
      lino,                          //
      _exit);

  TSDB_CHECK_CODE(                         //
      code = save_fs_to_file(pFS, fname),  //
      lino,                                //
      _exit);
H
Hongze Cheng 已提交
472 473 474 475 476 477 478 479

_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 已提交
480 481 482 483 484
  } else {
    tsdbInfo("vgId:%d %s done, etype:%d",  //
             TD_VID(pFS->pTsdb->pVnode),   //
             __func__,                     //
             etype);
H
Hongze Cheng 已提交
485 486 487 488
  }
  return code;
}

H
Hongze Cheng 已提交
489
int32_t tsdbFileSystemEditCommit(struct STFileSystem *pFS, tsdb_fs_edit_t etype) {
H
Hongze Cheng 已提交
490
  int32_t code = commit_edit(pFS, etype);
H
Hongze Cheng 已提交
491
  tsem_post(&pFS->canEdit);
H
Hongze Cheng 已提交
492
  if (code) {
H
Hongze Cheng 已提交
493 494 495
    tsdbError("vgId:%d %s failed since %s",  //
              TD_VID(pFS->pTsdb->pVnode),    //
              __func__,                      //
H
Hongze Cheng 已提交
496
              tstrerror(code));
H
Hongze Cheng 已提交
497 498 499 500 501
  } else {
    tsdbInfo("vgId:%d %s done, etype:%d",  //
             TD_VID(pFS->pTsdb->pVnode),   //
             __func__,                     //
             etype);
H
Hongze Cheng 已提交
502 503 504 505
  }
  return code;
}

H
Hongze Cheng 已提交
506
int32_t tsdbFileSystemEditAbort(struct STFileSystem *pFS, tsdb_fs_edit_t etype) {
H
Hongze Cheng 已提交
507
  int32_t code = abort_edit(pFS, etype);
H
Hongze Cheng 已提交
508
  if (code) {
H
Hongze Cheng 已提交
509 510 511 512 513
    tsdbError("vgId:%d %s failed since %s, etype:%d",  //
              TD_VID(pFS->pTsdb->pVnode),              //
              __func__,                                //
              tstrerror(code),                         //
              etype);
H
Hongze Cheng 已提交
514
  } else {
H
Hongze Cheng 已提交
515
  }
H
Hongze Cheng 已提交
516
  tsem_post(&pFS->canEdit);
H
Hongze Cheng 已提交
517 518
  return code;
}