tsdbFS.c 4.7 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 19 20
static int32_t create_file_system(STsdb *pTsdb, struct STFileSystem **ppFS) {
  ppFS[0] = taosMemoryCalloc(1, sizeof(*ppFS[0]));
  if (ppFS[0] == NULL) return TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
21

H
Hongze Cheng 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34
  ppFS[0]->pTsdb = pTsdb;
  return 0;
}

static int32_t destroy_file_system(struct STFileSystem **ppFS) {
  if (ppFS[0]) {
    taosMemoryFree(ppFS[0]->aFileSet);
    taosMemoryFree(ppFS[0]);
    ppFS[0] = NULL;
  }
  return 0;
}

H
Hongze Cheng 已提交
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
static int32_t get_current_file_name(STsdb *pTsdb, char fname[]) {
  snprintf(fname, TSDB_FILENAME_LEN, "%s%s%s", pTsdb->path, TD_DIRSEP, "current.json");
  return 0;
}

static int32_t get_temp_current_file_name(STsdb *pTsdb, char fname[], EFsEditType etype) {
  switch (etype) {
    case TSDB_FS_EDIT_COMMIT:
      snprintf(fname, TSDB_FILENAME_LEN, "%s%s%s", pTsdb->path, TD_DIRSEP, "current.json.commit");
      break;
    default:
      snprintf(fname, TSDB_FILENAME_LEN, "%s%s%s", pTsdb->path, TD_DIRSEP, "current.json.t");
      break;
  }

  return 0;
}

H
Hongze Cheng 已提交
53
static int32_t open_file_system(struct STFileSystem *pFS, int8_t rollback) {
H
Hongze Cheng 已提交
54 55 56 57 58
  // TODO
  return 0;
}

static int32_t close_file_system(struct STFileSystem *pFS) {
H
Hongze Cheng 已提交
59 60 61
  // TODO
  return 0;
}
H
Hongze Cheng 已提交
62

H
Hongze Cheng 已提交
63 64 65 66 67 68 69 70 71 72
static int32_t write_fs_to_file(struct STFileSystem *pFS, const char *fname) {
  // TODO
  return 0;
}

static int32_t read_fs_from_file(struct STFileSystem *pFS, const char *fname) {
  // TODO
  return 0;
}

H
Hongze Cheng 已提交
73
int32_t tsdbOpenFileSystem(STsdb *pTsdb, struct STFileSystem **ppFS, int8_t rollback) {
H
Hongze Cheng 已提交
74 75 76 77 78 79
  int32_t code;
  int32_t lino;

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

H
Hongze Cheng 已提交
80
  code = open_file_system(ppFS[0], rollback);
H
Hongze Cheng 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
  TSDB_CHECK_CODE(code, lino, _exit);

_exit:
  if (code) {
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
    if (ppFS[0]) destroy_file_system(ppFS);
  } else {
    tsdbInfo("vgId:%d %s success", TD_VID(pTsdb->pVnode), __func__);
  }
  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 已提交
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 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 168 169 170 171

int32_t tsdbFileSystemEditBegin(struct STFileSystem *pFS, const SArray *aFileOp, EFsEditType etype) {
  int32_t code = 0;
  int32_t lino = 0;
  char    fname[TSDB_FILENAME_LEN];

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

  tsem_wait(&pFS->canEdit);

  code = write_fs_to_file(pFS, fname);
  TSDB_CHECK_CODE(code, lino, _exit);

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

int32_t tsdbFileSystemEditCommit(struct STFileSystem *pFS, EFsEditType etype) {
  int32_t code = 0;
  int32_t lino = 0;
  char    ofname[TSDB_FILENAME_LEN];
  char    nfname[TSDB_FILENAME_LEN];

  get_current_file_name(pFS->pTsdb, nfname);
  get_temp_current_file_name(pFS->pTsdb, ofname, etype);

  code = taosRenameFile(ofname, nfname);
  if (code) {
    code = TAOS_SYSTEM_ERROR(errno);
    TSDB_CHECK_CODE(code, lino, _exit);
  }

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

int32_t tsdbFileSystemEditAbort(struct STFileSystem *pFS, EFsEditType etype) {
  int32_t code = 0;
  int32_t lino = 0;
  char    fname[TSDB_FILENAME_LEN];

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

  code = taosRemoveFile(fname);
  TSDB_CHECK_CODE(code, lino, _exit);

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