tsdbFS2.c 21.8 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 "tsdbFS2.h"
H
Hongze Cheng 已提交
17

H
Hongze Cheng 已提交
18
extern int vnodeScheduleTask(int (*execute)(void *), void *arg);
H
Hongze Cheng 已提交
19
extern int vnodeScheduleTaskEx(int tpid, int (*execute)(void *), void *arg);
H
Hongze Cheng 已提交
20

H
Hongze Cheng 已提交
21 22
#define TSDB_FS_EDIT_MIN TSDB_FEDIT_COMMIT
#define TSDB_FS_EDIT_MAX (TSDB_FEDIT_MERGE + 1)
H
Hongze Cheng 已提交
23 24 25 26 27 28 29 30

enum {
  TSDB_FS_STATE_NONE = 0,
  TSDB_FS_STATE_OPEN,
  TSDB_FS_STATE_EDIT,
  TSDB_FS_STATE_CLOSE,
};

H
Hongze Cheng 已提交
31 32
typedef enum {
  TSDB_FCURRENT = 1,
H
Hongze Cheng 已提交
33 34
  TSDB_FCURRENT_C,  // for commit
  TSDB_FCURRENT_M,  // for merge
H
Hongze Cheng 已提交
35 36 37 38
} EFCurrentT;

static const char *gCurrentFname[] = {
    [TSDB_FCURRENT] = "current.json",
H
Hongze Cheng 已提交
39 40
    [TSDB_FCURRENT_C] = "current.c.json",
    [TSDB_FCURRENT_M] = "current.m.json",
H
Hongze Cheng 已提交
41 42
};

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

H
Hongze Cheng 已提交
47
  fs[0]->tsdb = pTsdb;
H
Hongze Cheng 已提交
48
  tsem_init(&fs[0]->canEdit, 0, 1);
H
Hongze Cheng 已提交
49
  fs[0]->state = TSDB_FS_STATE_NONE;
H
Hongze Cheng 已提交
50
  fs[0]->neid = 0;
H
Hongze Cheng 已提交
51 52
  TARRAY2_INIT(fs[0]->fSetArr);
  TARRAY2_INIT(fs[0]->fSetArrTmp);
H
Hongze Cheng 已提交
53

H
Hongze Cheng 已提交
54 55 56 57 58
  // background task queue
  taosThreadMutexInit(fs[0]->mutex, NULL);
  fs[0]->bgTaskQueue->next = fs[0]->bgTaskQueue;
  fs[0]->bgTaskQueue->prev = fs[0]->bgTaskQueue;

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

H
Hongze Cheng 已提交
62 63
static int32_t destroy_fs(STFileSystem **fs) {
  if (fs[0] == NULL) return 0;
H
Hongze Cheng 已提交
64 65 66 67
  taosThreadMutexDestroy(fs[0]->mutex);

  ASSERT(fs[0]->bgTaskNum == 0);

H
Hongze Cheng 已提交
68 69
  TARRAY2_DESTROY(fs[0]->fSetArr, NULL);
  TARRAY2_DESTROY(fs[0]->fSetArrTmp, NULL);
H
Hongze Cheng 已提交
70 71 72
  tsem_destroy(&fs[0]->canEdit);
  taosMemoryFree(fs[0]);
  fs[0] = NULL;
H
Hongze Cheng 已提交
73 74 75
  return 0;
}

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

H
Hongze Cheng 已提交
97
static int32_t save_json(const cJSON *json, const char *fname) {
H
Hongze Cheng 已提交
98
  int32_t code = 0;
H
Hongze Cheng 已提交
99

H
Hongze Cheng 已提交
100
  char *data = cJSON_PrintUnformatted(json);
H
Hongze Cheng 已提交
101 102 103 104 105 106
  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 已提交
107 108
  }

H
Hongze Cheng 已提交
109
  if (taosWriteFile(fp, data, strlen(data)) < 0) {
H
Hongze Cheng 已提交
110 111
    code = TAOS_SYSTEM_ERROR(code);
    goto _exit;
H
Hongze Cheng 已提交
112 113
  }

H
Hongze Cheng 已提交
114 115 116 117
  if (taosFsyncFile(fp) < 0) {
    code = TAOS_SYSTEM_ERROR(code);
    goto _exit;
  }
H
Hongze Cheng 已提交
118

H
Hongze Cheng 已提交
119
  taosCloseFile(&fp);
H
Hongze Cheng 已提交
120 121

_exit:
H
Hongze Cheng 已提交
122
  taosMemoryFree(data);
H
Hongze Cheng 已提交
123 124 125
  return code;
}

H
Hongze Cheng 已提交
126 127
static int32_t load_json(const char *fname, cJSON **json) {
  int32_t code = 0;
H
Hongze Cheng 已提交
128
  char   *data = NULL;
H
Hongze Cheng 已提交
129 130 131 132 133 134 135 136 137 138

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

H
Hongze Cheng 已提交
139
  data = taosMemoryMalloc(size + 1);
H
Hongze Cheng 已提交
140 141 142 143 144 145 146 147 148
  if (data == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
    goto _exit;
  }

  if (taosReadFile(fp, data, size) < 0) {
    code = TAOS_SYSTEM_ERROR(code);
    goto _exit;
  }
H
Hongze Cheng 已提交
149
  data[size] = '\0';
H
Hongze Cheng 已提交
150 151 152 153 154 155 156 157 158 159 160 161 162 163

  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 已提交
164
static int32_t save_fs(const TFileSetArray *arr, const char *fname) {
H
Hongze Cheng 已提交
165
  int32_t code = 0;
H
Hongze Cheng 已提交
166
  int32_t lino = 0;
H
Hongze Cheng 已提交
167

H
Hongze Cheng 已提交
168
  cJSON *json = cJSON_CreateObject();
H
Hongze Cheng 已提交
169
  if (!json) return TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
170

H
Hongze Cheng 已提交
171 172 173
  // fmtv
  if (cJSON_AddNumberToObject(json, "fmtv", 1) == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
174
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
175 176
  }

H
Hongze Cheng 已提交
177 178
  // fset
  cJSON *ajson = cJSON_AddArrayToObject(json, "fset");
H
Hongze Cheng 已提交
179
  if (!ajson) TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
H
Hongze Cheng 已提交
180 181 182
  const STFileSet *fset;
  TARRAY2_FOREACH(arr, fset) {
    cJSON *item = cJSON_CreateObject();
H
Hongze Cheng 已提交
183 184
    if (!item) TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
    cJSON_AddItemToArray(ajson, item);
H
Hongze Cheng 已提交
185

H
Hongze Cheng 已提交
186
    code = tsdbTFileSetToJson(fset, item);
H
Hongze Cheng 已提交
187 188 189
    TSDB_CHECK_CODE(code, lino, _exit);
  }

H
Hongze Cheng 已提交
190
  code = save_json(json, fname);
H
Hongze Cheng 已提交
191
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
192 193 194

_exit:
  if (code) {
H
Hongze Cheng 已提交
195
    tsdbError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
196
  }
H
Hongze Cheng 已提交
197
  cJSON_Delete(json);
H
Hongze Cheng 已提交
198
  return code;
H
Hongze Cheng 已提交
199 200
}

H
Hongze Cheng 已提交
201
static int32_t load_fs(STsdb *pTsdb, const char *fname, TFileSetArray *arr) {
H
Hongze Cheng 已提交
202 203 204
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
205
  TARRAY2_CLEAR(arr, tsdbTFileSetClear);
H
Hongze Cheng 已提交
206 207 208 209

  // load json
  cJSON *json = NULL;
  code = load_json(fname, &json);
H
Hongze Cheng 已提交
210
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
211 212

  // parse json
H
Hongze Cheng 已提交
213
  const cJSON *item1;
H
Hongze Cheng 已提交
214 215

  /* fmtv */
H
Hongze Cheng 已提交
216 217 218
  item1 = cJSON_GetObjectItem(json, "fmtv");
  if (cJSON_IsNumber(item1)) {
    ASSERT(item1->valuedouble == 1);
H
Hongze Cheng 已提交
219
  } else {
H
Hongze Cheng 已提交
220
    TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
H
Hongze Cheng 已提交
221 222 223
  }

  /* fset */
H
Hongze Cheng 已提交
224 225 226 227
  item1 = cJSON_GetObjectItem(json, "fset");
  if (cJSON_IsArray(item1)) {
    const cJSON *item2;
    cJSON_ArrayForEach(item2, item1) {
H
Hongze Cheng 已提交
228
      STFileSet *fset;
H
Hongze Cheng 已提交
229
      code = tsdbJsonToTFileSet(pTsdb, item2, &fset);
H
Hongze Cheng 已提交
230
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
231

H
Hongze Cheng 已提交
232
      code = TARRAY2_APPEND(arr, fset);
H
Hongze Cheng 已提交
233
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
234 235
    }
  } else {
H
Hongze Cheng 已提交
236
    TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
H
Hongze Cheng 已提交
237 238 239 240 241 242 243 244
  }

_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 已提交
245 246
}

H
Hongze Cheng 已提交
247 248 249 250 251 252 253 254
static bool is_same_file(const STFile *f1, const STFile f2) {
  if (f1->type != f2.type) return false;
  if (f1->did.level != f2.did.level) return false;
  if (f1->did.id != f2.did.id) return false;
  if (f1->cid != f2.cid) return false;
  return true;
}

H
Hongze Cheng 已提交
255
static int32_t apply_commit(STFileSystem *fs) {
H
Hongze Cheng 已提交
256
  int32_t        code = 0;
H
Hongze Cheng 已提交
257 258
  TFileSetArray *fsetArray1 = fs->fSetArr;
  TFileSetArray *fsetArray2 = fs->fSetArrTmp;
H
Hongze Cheng 已提交
259
  int32_t        i1 = 0, i2 = 0;
H
Hongze Cheng 已提交
260

H
Hongze Cheng 已提交
261
  while (i1 < TARRAY2_SIZE(fsetArray1) || i2 < TARRAY2_SIZE(fsetArray2)) {
H
Hongze Cheng 已提交
262 263
    STFileSet *fset1 = i1 < TARRAY2_SIZE(fsetArray1) ? TARRAY2_GET(fsetArray1, i1) : NULL;
    STFileSet *fset2 = i2 < TARRAY2_SIZE(fsetArray2) ? TARRAY2_GET(fsetArray2, i2) : NULL;
H
Hongze Cheng 已提交
264 265 266

    if (fset1 && fset2) {
      if (fset1->fid < fset2->fid) {
H
Hongze Cheng 已提交
267 268
        // delete fset1
        TARRAY2_REMOVE(fsetArray1, i1, tsdbTFileSetRemove);
H
Hongze Cheng 已提交
269 270
      } else if (fset1->fid > fset2->fid) {
        // create new file set with fid of fset2->fid
H
Hongze Cheng 已提交
271
        code = tsdbTFileSetInitDup(fs->tsdb, fset2, &fset1);
H
Hongze Cheng 已提交
272
        if (code) return code;
H
Hongze Cheng 已提交
273
        code = TARRAY2_SORT_INSERT(fsetArray1, fset1, tsdbTFileSetCmprFn);
H
Hongze Cheng 已提交
274 275
        if (code) return code;
        i1++;
H
Hongze Cheng 已提交
276
        i2++;
H
Hongze Cheng 已提交
277 278
      } else {
        // edit
H
Hongze Cheng 已提交
279
        code = tsdbTFileSetApplyEdit(fs->tsdb, fset2, fset1);
H
Hongze Cheng 已提交
280 281 282 283 284
        if (code) return code;
        i1++;
        i2++;
      }
    } else if (fset1) {
H
Hongze Cheng 已提交
285 286
      // delete fset1
      TARRAY2_REMOVE(fsetArray1, i1, tsdbTFileSetRemove);
H
Hongze Cheng 已提交
287 288
    } else {
      // create new file set with fid of fset2->fid
H
Hongze Cheng 已提交
289
      code = tsdbTFileSetInitDup(fs->tsdb, fset2, &fset1);
H
Hongze Cheng 已提交
290
      if (code) return code;
H
Hongze Cheng 已提交
291
      code = TARRAY2_SORT_INSERT(fsetArray1, fset1, tsdbTFileSetCmprFn);
H
Hongze Cheng 已提交
292 293
      if (code) return code;
      i1++;
H
Hongze Cheng 已提交
294
      i2++;
H
Hongze Cheng 已提交
295 296
    }
  }
H
Hongze Cheng 已提交
297

H
Hongze Cheng 已提交
298 299 300 301
  return 0;
}

static int32_t commit_edit(STFileSystem *fs) {
H
Hongze Cheng 已提交
302 303
  char current[TSDB_FILENAME_LEN];
  char current_t[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
304

H
Hongze Cheng 已提交
305
  current_fname(fs->tsdb, current, TSDB_FCURRENT);
H
Hongze Cheng 已提交
306
  if (fs->etype == TSDB_FEDIT_COMMIT) {
H
Hongze Cheng 已提交
307
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_C);
H
Hongze Cheng 已提交
308
  } else if (fs->etype == TSDB_FEDIT_MERGE) {
H
Hongze Cheng 已提交
309
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_M);
H
Hongze Cheng 已提交
310 311
  } else {
    ASSERT(0);
H
Hongze Cheng 已提交
312 313
  }

H
Hongze Cheng 已提交
314 315 316
  int32_t code;
  int32_t lino;
  if ((code = taosRenameFile(current_t, current))) {
H
Hongze Cheng 已提交
317
    TSDB_CHECK_CODE(code = TAOS_SYSTEM_ERROR(code), lino, _exit);
H
Hongze Cheng 已提交
318
  }
H
Hongze Cheng 已提交
319

H
Hongze Cheng 已提交
320
  code = apply_commit(fs);
H
Hongze Cheng 已提交
321
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
322

H
Hongze Cheng 已提交
323 324
_exit:
  if (code) {
H
Hongze Cheng 已提交
325
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(fs->tsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
326
  } else {
H
Hongze Cheng 已提交
327
    tsdbInfo("vgId:%d %s success, etype:%d", TD_VID(fs->tsdb->pVnode), __func__, fs->etype);
H
Hongze Cheng 已提交
328 329
  }
  return code;
H
Hongze Cheng 已提交
330 331
}

H
Hongze Cheng 已提交
332 333 334 335 336 337 338
// static int32_t
static int32_t apply_abort(STFileSystem *fs) {
  // TODO
  return 0;
}

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

H
Hongze Cheng 已提交
341
  if (fs->etype == TSDB_FEDIT_COMMIT) {
H
Hongze Cheng 已提交
342
    current_fname(fs->tsdb, fname, TSDB_FCURRENT_C);
H
Hongze Cheng 已提交
343
  } else if (fs->etype == TSDB_FEDIT_MERGE) {
H
Hongze Cheng 已提交
344
    current_fname(fs->tsdb, fname, TSDB_FCURRENT_M);
H
Hongze Cheng 已提交
345 346 347
  } else {
    ASSERT(0);
  }
H
Hongze Cheng 已提交
348

H
Hongze Cheng 已提交
349 350 351
  int32_t code;
  int32_t lino;
  if ((code = taosRemoveFile(fname))) {
H
Hongze Cheng 已提交
352
    TSDB_CHECK_CODE(code = TAOS_SYSTEM_ERROR(code), lino, _exit);
H
Hongze Cheng 已提交
353
  }
H
Hongze Cheng 已提交
354

H
Hongze Cheng 已提交
355
  code = apply_abort(fs);
H
Hongze Cheng 已提交
356
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
357 358 359

_exit:
  if (code) {
H
Hongze Cheng 已提交
360
    tsdbError("vgId:%d %s failed since %s", TD_VID(fs->tsdb->pVnode), __func__, tstrerror(code));
H
Hongze Cheng 已提交
361
  } else {
H
Hongze Cheng 已提交
362
    tsdbInfo("vgId:%d %s success, etype:%d", TD_VID(fs->tsdb->pVnode), __func__, fs->etype);
H
Hongze Cheng 已提交
363 364
  }
  return code;
H
Hongze Cheng 已提交
365 366
}

H
Hongze Cheng 已提交
367 368 369 370 371
static int32_t tsdbFSScanAndFix(STFileSystem *fs) {
  fs->neid = 0;

  // get max commit id
  const STFileSet *fset;
H
Hongze Cheng 已提交
372
  TARRAY2_FOREACH(fs->fSetArr, fset) { fs->neid = TMAX(fs->neid, tsdbTFileSetMaxCid(fset)); }
H
Hongze Cheng 已提交
373

H
Hongze Cheng 已提交
374
  // TODO
H
Hongze Cheng 已提交
375 376 377
  return 0;
}

H
Hongze Cheng 已提交
378 379 380 381 382
static int32_t update_fs_if_needed(STFileSystem *pFS) {
  // TODO
  return 0;
}

H
Hongze Cheng 已提交
383 384 385
static int32_t tsdbFSDupState(STFileSystem *fs) {
  int32_t code;

H
Hongze Cheng 已提交
386 387
  const TFileSetArray *src = fs->fSetArr;
  TFileSetArray       *dst = fs->fSetArrTmp;
H
Hongze Cheng 已提交
388

H
Hongze Cheng 已提交
389 390 391 392
  TARRAY2_CLEAR(dst, tsdbTFileSetClear);

  const STFileSet *fset1;
  TARRAY2_FOREACH(src, fset1) {
H
Hongze Cheng 已提交
393
    STFileSet *fset2;
H
Hongze Cheng 已提交
394
    code = tsdbTFileSetInitDup(fs->tsdb, fset1, &fset2);
H
Hongze Cheng 已提交
395
    if (code) return code;
H
Hongze Cheng 已提交
396
    code = TARRAY2_APPEND(dst, fset2);
H
Hongze Cheng 已提交
397 398 399 400 401 402
    if (code) return code;
  }

  return 0;
}

H
Hongze Cheng 已提交
403
static int32_t open_fs(STFileSystem *fs, int8_t rollback) {
H
Hongze Cheng 已提交
404
  int32_t code = 0;
H
Hongze Cheng 已提交
405
  int32_t lino = 0;
H
Hongze Cheng 已提交
406
  STsdb  *pTsdb = fs->tsdb;
H
Hongze Cheng 已提交
407

H
Hongze Cheng 已提交
408
  code = update_fs_if_needed(fs);
H
Hongze Cheng 已提交
409
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
410

H
Hongze Cheng 已提交
411 412 413
  char fCurrent[TSDB_FILENAME_LEN];
  char cCurrent[TSDB_FILENAME_LEN];
  char mCurrent[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
414

H
Hongze Cheng 已提交
415 416
  current_fname(pTsdb, fCurrent, TSDB_FCURRENT);
  current_fname(pTsdb, cCurrent, TSDB_FCURRENT_C);
H
Hongze Cheng 已提交
417
  current_fname(pTsdb, mCurrent, TSDB_FCURRENT_M);
H
Hongze Cheng 已提交
418

H
Hongze Cheng 已提交
419
  if (taosCheckExistFile(fCurrent)) {  // current.json exists
H
Hongze Cheng 已提交
420
    code = load_fs(pTsdb, fCurrent, fs->fSetArr);
H
Hongze Cheng 已提交
421
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
422

H
Hongze Cheng 已提交
423
    if (taosCheckExistFile(cCurrent)) {
H
Hongze Cheng 已提交
424 425 426
      // current.c.json exists

      fs->etype = TSDB_FEDIT_COMMIT;
H
Hongze Cheng 已提交
427
      if (rollback) {
H
Hongze Cheng 已提交
428
        code = abort_edit(fs);
H
Hongze Cheng 已提交
429 430
        TSDB_CHECK_CODE(code, lino, _exit);
      } else {
H
Hongze Cheng 已提交
431
        code = load_fs(pTsdb, cCurrent, fs->fSetArrTmp);
H
Hongze Cheng 已提交
432
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
433 434

        code = commit_edit(fs);
H
Hongze Cheng 已提交
435 436
        TSDB_CHECK_CODE(code, lino, _exit);
      }
H
Hongze Cheng 已提交
437 438 439 440
    } else if (taosCheckExistFile(mCurrent)) {
      // current.m.json exists
      fs->etype = TSDB_FEDIT_MERGE;
      code = abort_edit(fs);
H
Hongze Cheng 已提交
441 442
      TSDB_CHECK_CODE(code, lino, _exit);
    }
H
Hongze Cheng 已提交
443

H
Hongze Cheng 已提交
444
    code = tsdbFSDupState(fs);
H
Hongze Cheng 已提交
445 446
    TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
447
    code = tsdbFSScanAndFix(fs);
H
Hongze Cheng 已提交
448
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
449
  } else {
H
Hongze Cheng 已提交
450
    code = save_fs(fs->fSetArr, fCurrent);
H
Hongze Cheng 已提交
451
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
452 453 454 455
  }

_exit:
  if (code) {
H
Hongze Cheng 已提交
456
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
457
  } else {
H
Hongze Cheng 已提交
458
    tsdbInfo("vgId:%d %s success", TD_VID(pTsdb->pVnode), __func__);
H
Hongze Cheng 已提交
459
  }
H
Hongze Cheng 已提交
460 461 462
  return 0;
}

H
Hongze Cheng 已提交
463
static int32_t close_file_system(STFileSystem *fs) {
H
Hongze Cheng 已提交
464 465
  TARRAY2_CLEAR(fs->fSetArr, tsdbTFileSetClear);
  TARRAY2_CLEAR(fs->fSetArrTmp, tsdbTFileSetClear);
H
Hongze Cheng 已提交
466
  // TODO
H
Hongze Cheng 已提交
467 468
  return 0;
}
H
Hongze Cheng 已提交
469

H
Hongze Cheng 已提交
470
static int32_t apply_edit(STFileSystem *pFS) {
H
Hongze Cheng 已提交
471
  int32_t code = 0;
H
Hongze Cheng 已提交
472
  ASSERTS(0, "TODO: Not implemented yet");
H
Hongze Cheng 已提交
473
  return code;
H
Hongze Cheng 已提交
474 475
}

H
Hongze Cheng 已提交
476
static int32_t fset_cmpr_fn(const struct STFileSet *pSet1, const struct STFileSet *pSet2) {
H
Hongze Cheng 已提交
477 478 479 480 481 482 483 484
  if (pSet1->fid < pSet2->fid) {
    return -1;
  } else if (pSet1->fid > pSet2->fid) {
    return 1;
  }
  return 0;
}

H
Hongze Cheng 已提交
485 486 487
static int32_t edit_fs(STFileSystem *fs, const TFileOpArray *opArray) {
  int32_t        code = 0;
  int32_t        lino = 0;
H
Hongze Cheng 已提交
488
  TFileSetArray *fsetArray = fs->fSetArrTmp;
H
Hongze Cheng 已提交
489 490 491

  STFileSet      *fset = NULL;
  const STFileOp *op;
H
Hongze Cheng 已提交
492
  TARRAY2_FOREACH_PTR(opArray, op) {
H
Hongze Cheng 已提交
493 494 495
    if (!fset || fset->fid != op->fid) {
      STFileSet tfset = {.fid = op->fid};
      fset = &tfset;
H
Hongze Cheng 已提交
496
      fset = TARRAY2_SEARCH_EX(fsetArray, &fset, tsdbTFileSetCmprFn, TD_EQ);
H
Hongze Cheng 已提交
497 498 499 500 501

      if (!fset) {
        code = tsdbTFileSetInit(op->fid, &fset);
        TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
502
        code = TARRAY2_SORT_INSERT(fsetArray, fset, tsdbTFileSetCmprFn);
H
Hongze Cheng 已提交
503 504
        TSDB_CHECK_CODE(code, lino, _exit);
      }
H
Hongze Cheng 已提交
505 506
    }

H
Hongze Cheng 已提交
507
    code = tsdbTFileSetEdit(fs->tsdb, fset, op);
H
Hongze Cheng 已提交
508
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
509
  }
H
Hongze Cheng 已提交
510

H
Hongze Cheng 已提交
511 512 513 514 515 516 517 518 519
  // remove empty file set
  int32_t i = 0;
  while (i < TARRAY2_SIZE(fsetArray)) {
    fset = TARRAY2_GET(fsetArray, i);
    if (tsdbTFileSetIsEmpty(fset)) {
      TARRAY2_REMOVE(fsetArray, i, tsdbTFileSetClear);
    } else {
      i++;
    }
H
Hongze Cheng 已提交
520 521
  }

H
Hongze Cheng 已提交
522
_exit:
H
Hongze Cheng 已提交
523
  return code;
H
Hongze Cheng 已提交
524 525
}

H
Hongze Cheng 已提交
526
int32_t tsdbOpenFS(STsdb *pTsdb, STFileSystem **fs, int8_t rollback) {
H
Hongze Cheng 已提交
527 528 529
  int32_t code;
  int32_t lino;

H
Hongze Cheng 已提交
530
  code = create_fs(pTsdb, fs);
H
Hongze Cheng 已提交
531 532
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
533
  code = open_fs(fs[0], rollback);
H
Hongze Cheng 已提交
534
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
535 536 537

_exit:
  if (code) {
H
Hongze Cheng 已提交
538
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
539
    destroy_fs(fs);
H
Hongze Cheng 已提交
540
  } else {
H
Hongze Cheng 已提交
541
    tsdbInfo("vgId:%d %s success", TD_VID(pTsdb->pVnode), __func__);
H
Hongze Cheng 已提交
542 543 544 545
  }
  return 0;
}

H
Hongze Cheng 已提交
546
int32_t tsdbCloseFS(STFileSystem **ppFS) {
H
Hongze Cheng 已提交
547 548
  if (ppFS[0] == NULL) return 0;
  close_file_system(ppFS[0]);
H
Hongze Cheng 已提交
549
  destroy_fs(ppFS);
H
Hongze Cheng 已提交
550 551
  return 0;
}
H
Hongze Cheng 已提交
552

H
Hongze Cheng 已提交
553 554 555 556 557
int64_t tsdbFSAllocEid(STFileSystem *fs) {
  taosThreadRwlockRdlock(&fs->tsdb->rwLock);
  int64_t cid = ++fs->neid;
  taosThreadRwlockUnlock(&fs->tsdb->rwLock);
  return cid;
H
Hongze Cheng 已提交
558 559
}

H
Hongze Cheng 已提交
560
int32_t tsdbFSEditBegin(STFileSystem *fs, const TFileOpArray *opArray, EFEditT etype) {
H
Hongze Cheng 已提交
561
  int32_t code = 0;
H
Hongze Cheng 已提交
562
  int32_t lino;
H
Hongze Cheng 已提交
563
  char    current_t[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
564

H
Hongze Cheng 已提交
565 566
  switch (etype) {
    case TSDB_FEDIT_COMMIT:
H
Hongze Cheng 已提交
567
      current_fname(fs->tsdb, current_t, TSDB_FCURRENT_C);
H
Hongze Cheng 已提交
568 569
      break;
    case TSDB_FEDIT_MERGE:
H
Hongze Cheng 已提交
570
      current_fname(fs->tsdb, current_t, TSDB_FCURRENT_M);
H
Hongze Cheng 已提交
571 572 573
      break;
    default:
      ASSERT(0);
H
Hongze Cheng 已提交
574 575 576
  }

  tsem_wait(&fs->canEdit);
H
Hongze Cheng 已提交
577

H
Hongze Cheng 已提交
578
  fs->etype = etype;
H
Hongze Cheng 已提交
579

H
Hongze Cheng 已提交
580
  // edit
H
Hongze Cheng 已提交
581
  code = edit_fs(fs, opArray);
H
Hongze Cheng 已提交
582
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
583

H
Hongze Cheng 已提交
584
  // save fs
H
Hongze Cheng 已提交
585
  code = save_fs(fs->fSetArrTmp, current_t);
H
Hongze Cheng 已提交
586
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
587 588 589

_exit:
  if (code) {
H
Hongze Cheng 已提交
590
    tsdbError("vgId:%d %s failed at line %d since %s, etype:%d", TD_VID(fs->tsdb->pVnode), __func__, lino,
H
Hongze Cheng 已提交
591
              tstrerror(code), etype);
H
Hongze Cheng 已提交
592
  } else {
H
Hongze Cheng 已提交
593
    tsdbInfo("vgId:%d %s done, etype:%d", TD_VID(fs->tsdb->pVnode), __func__, etype);
H
Hongze Cheng 已提交
594 595 596 597
  }
  return code;
}

H
Hongze Cheng 已提交
598
int32_t tsdbFSEditCommit(STFileSystem *fs) {
H
Hongze Cheng 已提交
599 600 601 602 603 604 605
  int32_t code = 0;
  int32_t lino = 0;

  // commit
  code = commit_edit(fs);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
606 607
  // schedule merge
  if (fs->tsdb->pVnode->config.sttTrigger != 1) {
H
Hongze Cheng 已提交
608 609 610 611
    STFileSet *fset;
    TARRAY2_FOREACH_REVERSE(fs->fSetArr, fset) {
      if (TARRAY2_SIZE(fset->lvlArr) == 0) continue;

H
Hongze Cheng 已提交
612 613
      SSttLvl *lvl = TARRAY2_FIRST(fset->lvlArr);
      if (lvl->level != 0 || TARRAY2_SIZE(lvl->fobjArr) < fs->tsdb->pVnode->config.sttTrigger) continue;
H
Hongze Cheng 已提交
614

H
Hongze Cheng 已提交
615
      code = tsdbFSScheduleBgTask(fs, TSDB_BG_TASK_MERGER, tsdbMerge, fs->tsdb, NULL);
H
Hongze Cheng 已提交
616 617 618 619 620 621 622 623 624 625 626 627
      TSDB_CHECK_CODE(code, lino, _exit);

      break;
    }
  }

_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(fs->tsdb->pVnode), lino, code);
  } else {
    tsem_post(&fs->canEdit);
  }
H
Hongze Cheng 已提交
628 629 630
  return code;
}

H
Hongze Cheng 已提交
631 632 633
int32_t tsdbFSEditAbort(STFileSystem *fs) {
  int32_t code = abort_edit(fs);
  tsem_post(&fs->canEdit);
H
Hongze Cheng 已提交
634
  return code;
H
Hongze Cheng 已提交
635 636
}

H
Hongze Cheng 已提交
637 638 639
int32_t tsdbFSGetFSet(STFileSystem *fs, int32_t fid, STFileSet **fset) {
  STFileSet  tfset = {.fid = fid};
  STFileSet *pset = &tfset;
H
Hongze Cheng 已提交
640
  fset[0] = TARRAY2_SEARCH_EX(fs->fSetArr, &pset, tsdbTFileSetCmprFn, TD_EQ);
H
Hongze Cheng 已提交
641
  return 0;
H
Hongze Cheng 已提交
642 643
}

H
Hongze Cheng 已提交
644
int32_t tsdbFSCreateCopySnapshot(STFileSystem *fs, TFileSetArray **fsetArr) {
H
Hongze Cheng 已提交
645 646 647 648
  int32_t    code = 0;
  STFileSet *fset;
  STFileSet *fset1;

H
Hongze Cheng 已提交
649 650 651 652
  fsetArr[0] = taosMemoryMalloc(sizeof(TFileSetArray));
  if (fsetArr == NULL) return TSDB_CODE_OUT_OF_MEMORY;

  TARRAY2_INIT(fsetArr[0]);
H
Hongze Cheng 已提交
653 654

  taosThreadRwlockRdlock(&fs->tsdb->rwLock);
H
Hongze Cheng 已提交
655
  TARRAY2_FOREACH(fs->fSetArr, fset) {
H
Hongze Cheng 已提交
656
    code = tsdbTFileSetInitDup(fs->tsdb, fset, &fset1);
H
Hongze Cheng 已提交
657 658
    if (code) break;

H
Hongze Cheng 已提交
659
    code = TARRAY2_APPEND(fsetArr[0], fset1);
H
Hongze Cheng 已提交
660 661 662 663 664
    if (code) break;
  }
  taosThreadRwlockUnlock(&fs->tsdb->rwLock);

  if (code) {
H
Hongze Cheng 已提交
665
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
H
Hongze Cheng 已提交
666 667
    taosMemoryFree(fsetArr[0]);
    fsetArr[0] = NULL;
H
Hongze Cheng 已提交
668 669 670 671
  }
  return code;
}

H
Hongze Cheng 已提交
672 673
int32_t tsdbFSDestroyCopySnapshot(TFileSetArray **fsetArr) {
  if (fsetArr[0]) {
H
Hongze Cheng 已提交
674
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
H
Hongze Cheng 已提交
675 676
    fsetArr[0] = NULL;
  }
H
Hongze Cheng 已提交
677
  return 0;
H
Hongze Cheng 已提交
678 679 680 681 682 683
}

int32_t tsdbFSCreateRefSnapshot(STFileSystem *fs, TFileSetArray **fsetArr) {
  int32_t    code = 0;
  STFileSet *fset, *fset1;

684
  fsetArr[0] = taosMemoryCalloc(1, sizeof(*fsetArr[0]));
H
Hongze Cheng 已提交
685 686 687 688
  if (fsetArr[0] == NULL) return TSDB_CODE_OUT_OF_MEMORY;

  taosThreadRwlockRdlock(&fs->tsdb->rwLock);
  TARRAY2_FOREACH(fs->fSetArr, fset) {
H
Hongze Cheng 已提交
689 690
    code = tsdbTFileSetInitRef(fs->tsdb, fset, &fset1);
    if (code) break;
H
Hongze Cheng 已提交
691 692 693 694 695 696 697

    code = TARRAY2_APPEND(fsetArr[0], fset1);
    if (code) break;
  }
  taosThreadRwlockUnlock(&fs->tsdb->rwLock);

  if (code) {
H
Hongze Cheng 已提交
698
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
H
Hongze Cheng 已提交
699 700 701 702 703 704 705
    fsetArr[0] = NULL;
  }
  return code;
}

int32_t tsdbFSDestroyRefSnapshot(TFileSetArray **fsetArr) {
  if (fsetArr[0]) {
H
Hongze Cheng 已提交
706
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
H
Haojun Liao 已提交
707
    taosMemoryFreeClear(fsetArr[0]);
H
Hongze Cheng 已提交
708 709 710
    fsetArr[0] = NULL;
  }
  return 0;
H
Hongze Cheng 已提交
711 712 713 714 715 716 717 718 719 720 721 722 723
}

const char *gFSBgTaskName[] = {NULL, "MERGE", "RETENTION", "COMPACT"};

static int32_t tsdbFSRunBgTask(void *arg) {
  STFileSystem *fs = (STFileSystem *)arg;

  ASSERT(fs->bgTaskRunning != NULL);

  fs->bgTaskRunning->launchTime = taosGetTimestampMs();
  fs->bgTaskRunning->run(fs->bgTaskRunning->arg);
  fs->bgTaskRunning->finishTime = taosGetTimestampMs();

H
Hongze Cheng 已提交
724 725 726 727
  tsdbDebug("vgId:%d bg task:%s task id:%" PRId64 " finished, schedule time:%" PRId64 " launch time:%" PRId64
            " finish time:%" PRId64,
            TD_VID(fs->tsdb->pVnode), gFSBgTaskName[fs->bgTaskRunning->type], fs->bgTaskRunning->taskid,
            fs->bgTaskRunning->scheduleTime, fs->bgTaskRunning->launchTime, fs->bgTaskRunning->finishTime);
H
Hongze Cheng 已提交
728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842

  taosThreadMutexLock(fs->mutex);

  // free last
  if (fs->bgTaskRunning->numWait > 0) {
    taosThreadCondBroadcast(fs->bgTaskRunning->done);
  } else {
    taosThreadCondDestroy(fs->bgTaskRunning->done);
    taosMemoryFree(fs->bgTaskRunning);
  }
  fs->bgTaskRunning = NULL;

  // schedule next
  if (fs->bgTaskNum > 0) {
    // pop task from head
    fs->bgTaskRunning = fs->bgTaskQueue->next;
    fs->bgTaskRunning->prev->next = fs->bgTaskRunning->next;
    fs->bgTaskRunning->next->prev = fs->bgTaskRunning->prev;
    fs->bgTaskNum--;

    vnodeScheduleTaskEx(1, tsdbFSRunBgTask, arg);
  }

  taosThreadMutexUnlock(fs->mutex);
  return 0;
}

static int32_t tsdbFSScheduleBgTaskImpl(STFileSystem *fs, EFSBgTaskT type, int32_t (*run)(void *), void *arg,
                                        int64_t *taskid) {
  // check if same task is on
  if (fs->bgTaskRunning && fs->bgTaskRunning->type == type) {
    return 0;
  }

  for (STFSBgTask *task = fs->bgTaskQueue->next; task != fs->bgTaskQueue; task = task->next) {
    if (task->type == type) {
      return 0;
    }
  }

  // do schedule task
  STFSBgTask *task = taosMemoryCalloc(1, sizeof(STFSBgTask));
  if (task == NULL) return TSDB_CODE_OUT_OF_MEMORY;
  taosThreadCondInit(task->done, NULL);

  task->type = type;
  task->run = run;
  task->arg = arg;
  task->scheduleTime = taosGetTimestampMs();
  task->taskid = ++fs->taskid;

  if (fs->bgTaskRunning == NULL && fs->bgTaskNum == 0) {
    // launch task directly
    fs->bgTaskRunning = task;
    vnodeScheduleTaskEx(1, tsdbFSRunBgTask, fs);
  } else {
    // add to the queue tail
    fs->bgTaskNum++;
    task->next = fs->bgTaskQueue;
    task->prev = fs->bgTaskQueue->prev;
    task->prev->next = task;
    task->next->prev = task;
  }

  if (taskid) *taskid = task->taskid;
  return 0;
}

int32_t tsdbFSScheduleBgTask(STFileSystem *fs, EFSBgTaskT type, int32_t (*run)(void *), void *arg, int64_t *taskid) {
  taosThreadMutexLock(fs->mutex);
  int32_t code = tsdbFSScheduleBgTaskImpl(fs, type, run, arg, taskid);
  taosThreadMutexUnlock(fs->mutex);
  return code;
}

int32_t tsdbFSWaitBgTask(STFileSystem *fs, int64_t taskid) {
  STFSBgTask *task = NULL;

  taosThreadMutexLock(fs->mutex);

  if (fs->bgTaskRunning && fs->bgTaskRunning->taskid == taskid) {
    task = fs->bgTaskRunning;
  } else {
    for (STFSBgTask *taskt = fs->bgTaskQueue->next; taskt != fs->bgTaskQueue; taskt = taskt->next) {
      if (taskt->taskid == taskid) {
        task = taskt;
        break;
      }
    }
  }

  if (task) {
    task->numWait++;
    taosThreadCondWait(task->done, fs->mutex);
    task->numWait--;

    if (task->numWait == 0) {
      taosThreadCondDestroy(task->done);
      taosMemoryFree(task);
    }
  }

  taosThreadMutexUnlock(fs->mutex);
  return 0;
}

int32_t tsdbFSWaitAllBgTask(STFileSystem *fs) {
  taosThreadMutexLock(fs->mutex);

  while (fs->bgTaskRunning) {
    taosThreadCondWait(fs->bgTaskRunning->done, fs->mutex);
  }

  taosThreadMutexUnlock(fs->mutex);
  return 0;
H
Hongze Cheng 已提交
843
}