tsdbFS.c 7.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 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 23 24 25 26
  if ((ppFS[0] = taosMemoryCalloc(1, sizeof(*ppFS[0]))) == NULL) {
    return TSDB_CODE_OUT_OF_MEMORY;
  }

  if ((ppFS[0]->aFileSet = taosArrayInit(16, sizeof(struct SFileSet))) == NULL) {
    taosMemoryFree(ppFS[0]);
    return TSDB_CODE_OUT_OF_MEMORY;
  }
H
Hongze Cheng 已提交
27

H
Hongze Cheng 已提交
28
  ppFS[0]->pTsdb = pTsdb;
H
Hongze Cheng 已提交
29 30
  tsem_init(&ppFS[0]->canEdit, 0, 1);

H
Hongze Cheng 已提交
31 32 33 34 35
  return 0;
}

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

H
Hongze Cheng 已提交
44 45
static int32_t get_current_json(STsdb *pTsdb, char fname[]) {
  return snprintf(fname, TSDB_FILENAME_LEN, "%s%s%s", pTsdb->path, TD_DIRSEP, "current.json"), 0;
H
Hongze Cheng 已提交
46 47
}

H
Hongze Cheng 已提交
48
static int32_t get_current_temp(STsdb *pTsdb, char fname[], EFsEditType etype) {
H
Hongze Cheng 已提交
49 50 51 52 53 54 55 56 57 58 59 60
  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 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
static int32_t save_fs_to_file(struct STFileSystem *pFS, const char *fname) {
  ASSERTS(0, "TODO: Not implemented yet");
  return 0;
}

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

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

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

H
Hongze Cheng 已提交
81
static int32_t open_file_system(struct STFileSystem *pFS, int8_t rollback) {
H
Hongze Cheng 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 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
  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);
        }
      } else {
        ASSERTS(1, "Do nothing");
      }

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

_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 已提交
137 138 139 140
  return 0;
}

static int32_t close_file_system(struct STFileSystem *pFS) {
H
Hongze Cheng 已提交
141
  ASSERTS(0, "TODO: Not implemented yet");
H
Hongze Cheng 已提交
142 143
  return 0;
}
H
Hongze Cheng 已提交
144

H
Hongze Cheng 已提交
145
static int32_t write_fs_to_file(struct STFileSystem *pFS, const char *fname) {
H
Hongze Cheng 已提交
146
  ASSERTS(0, "TODO: Not implemented yet");
H
Hongze Cheng 已提交
147 148 149 150
  return 0;
}

static int32_t read_fs_from_file(struct STFileSystem *pFS, const char *fname) {
H
Hongze Cheng 已提交
151
  ASSERTS(0, "TODO: Not implemented yet");
H
Hongze Cheng 已提交
152 153 154
  return 0;
}

H
Hongze Cheng 已提交
155
int32_t tsdbOpenFileSystem(STsdb *pTsdb, struct STFileSystem **ppFS, int8_t rollback) {
H
Hongze Cheng 已提交
156 157 158 159 160 161
  int32_t code;
  int32_t lino;

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

H
Hongze Cheng 已提交
162
  code = open_file_system(ppFS[0], rollback);
H
Hongze Cheng 已提交
163 164 165 166
  if (code) {
    destroy_file_system(ppFS);
    TSDB_CHECK_CODE(code, lino, _exit);
  }
H
Hongze Cheng 已提交
167 168 169

_exit:
  if (code) {
H
Hongze Cheng 已提交
170 171 172 173 174
    tsdbError("vgId:%d %s failed at line %d since %s",  //
              TD_VID(pTsdb->pVnode),                    //
              __func__,                                 //
              lino,                                     //
              tstrerror(code));
H
Hongze Cheng 已提交
175
  } else {
H
Hongze Cheng 已提交
176 177 178
    tsdbInfo("vgId:%d %s success",   //
             TD_VID(pTsdb->pVnode),  //
             __func__);
H
Hongze Cheng 已提交
179 180 181 182 183 184 185 186 187 188 189
  }
  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 已提交
190 191 192 193 194 195

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

H
Hongze Cheng 已提交
196
  get_current_temp(pFS->pTsdb, fname, etype);
H
Hongze Cheng 已提交
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219

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

H
Hongze Cheng 已提交
220 221
  get_current_json(pFS->pTsdb, nfname);
  get_current_temp(pFS->pTsdb, ofname, etype);
H
Hongze Cheng 已提交
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245

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

H
Hongze Cheng 已提交
246
  get_current_temp(pFS->pTsdb, fname, etype);
H
Hongze Cheng 已提交
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261

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