tsdbFS2.c 23.0 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
#include "tsdbUpgrade.h"
H
Hongze Cheng 已提交
18

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

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

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

H
Hongze Cheng 已提交
32 33
static const char *gCurrentFname[] = {
    [TSDB_FCURRENT] = "current.json",
H
Hongze Cheng 已提交
34 35
    [TSDB_FCURRENT_C] = "current.c.json",
    [TSDB_FCURRENT_M] = "current.m.json",
H
Hongze Cheng 已提交
36 37
};

H
Hongze Cheng 已提交
38 39 40 41
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 已提交
42
  fs[0]->tsdb = pTsdb;
H
Hongze Cheng 已提交
43
  tsem_init(&fs[0]->canEdit, 0, 1);
H
Hongze Cheng 已提交
44
  fs[0]->state = TSDB_FS_STATE_NONE;
H
Hongze Cheng 已提交
45
  fs[0]->neid = 0;
H
Hongze Cheng 已提交
46 47
  TARRAY2_INIT(fs[0]->fSetArr);
  TARRAY2_INIT(fs[0]->fSetArrTmp);
H
Hongze Cheng 已提交
48

H
Hongze Cheng 已提交
49 50 51 52 53
  // 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 已提交
54 55 56
  return 0;
}

H
Hongze Cheng 已提交
57 58
static int32_t destroy_fs(STFileSystem **fs) {
  if (fs[0] == NULL) return 0;
H
Hongze Cheng 已提交
59 60 61 62
  taosThreadMutexDestroy(fs[0]->mutex);

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

H
Hongze Cheng 已提交
63 64
  TARRAY2_DESTROY(fs[0]->fSetArr, NULL);
  TARRAY2_DESTROY(fs[0]->fSetArrTmp, NULL);
H
Hongze Cheng 已提交
65 66 67
  tsem_destroy(&fs[0]->canEdit);
  taosMemoryFree(fs[0]);
  fs[0] = NULL;
H
Hongze Cheng 已提交
68 69 70
  return 0;
}

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

H
Hongze Cheng 已提交
92
static int32_t save_json(const cJSON *json, const char *fname) {
H
Hongze Cheng 已提交
93
  int32_t code = 0;
H
Hongze Cheng 已提交
94

H
Hongze Cheng 已提交
95
  char *data = cJSON_PrintUnformatted(json);
H
Hongze Cheng 已提交
96 97 98 99 100 101
  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 已提交
102 103
  }

H
Hongze Cheng 已提交
104
  if (taosWriteFile(fp, data, strlen(data)) < 0) {
H
Hongze Cheng 已提交
105 106
    code = TAOS_SYSTEM_ERROR(code);
    goto _exit;
H
Hongze Cheng 已提交
107 108
  }

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

H
Hongze Cheng 已提交
114
  taosCloseFile(&fp);
H
Hongze Cheng 已提交
115 116

_exit:
H
Hongze Cheng 已提交
117
  taosMemoryFree(data);
H
Hongze Cheng 已提交
118 119 120
  return code;
}

H
Hongze Cheng 已提交
121 122
static int32_t load_json(const char *fname, cJSON **json) {
  int32_t code = 0;
H
Hongze Cheng 已提交
123
  char   *data = NULL;
H
Hongze Cheng 已提交
124 125 126 127 128 129 130 131 132 133

  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 已提交
134
  data = taosMemoryMalloc(size + 1);
H
Hongze Cheng 已提交
135 136 137 138 139 140 141 142 143
  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 已提交
144
  data[size] = '\0';
H
Hongze Cheng 已提交
145 146 147 148 149 150 151 152 153 154 155 156 157 158

  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 已提交
159
int32_t save_fs(const TFileSetArray *arr, const char *fname) {
H
Hongze Cheng 已提交
160
  int32_t code = 0;
H
Hongze Cheng 已提交
161
  int32_t lino = 0;
H
Hongze Cheng 已提交
162

H
Hongze Cheng 已提交
163
  cJSON *json = cJSON_CreateObject();
H
Hongze Cheng 已提交
164
  if (!json) return TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
165

H
Hongze Cheng 已提交
166 167 168
  // fmtv
  if (cJSON_AddNumberToObject(json, "fmtv", 1) == NULL) {
    code = TSDB_CODE_OUT_OF_MEMORY;
H
Hongze Cheng 已提交
169
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
170 171
  }

H
Hongze Cheng 已提交
172 173
  // fset
  cJSON *ajson = cJSON_AddArrayToObject(json, "fset");
H
Hongze Cheng 已提交
174
  if (!ajson) TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
H
Hongze Cheng 已提交
175 176 177
  const STFileSet *fset;
  TARRAY2_FOREACH(arr, fset) {
    cJSON *item = cJSON_CreateObject();
H
Hongze Cheng 已提交
178 179
    if (!item) TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
    cJSON_AddItemToArray(ajson, item);
H
Hongze Cheng 已提交
180

H
Hongze Cheng 已提交
181
    code = tsdbTFileSetToJson(fset, item);
H
Hongze Cheng 已提交
182 183 184
    TSDB_CHECK_CODE(code, lino, _exit);
  }

H
Hongze Cheng 已提交
185
  code = save_json(json, fname);
H
Hongze Cheng 已提交
186
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
187 188 189

_exit:
  if (code) {
H
Hongze Cheng 已提交
190
    tsdbError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
191
  }
H
Hongze Cheng 已提交
192
  cJSON_Delete(json);
H
Hongze Cheng 已提交
193
  return code;
H
Hongze Cheng 已提交
194 195
}

H
Hongze Cheng 已提交
196
static int32_t load_fs(STsdb *pTsdb, const char *fname, TFileSetArray *arr) {
H
Hongze Cheng 已提交
197 198 199
  int32_t code = 0;
  int32_t lino = 0;

H
Hongze Cheng 已提交
200
  TARRAY2_CLEAR(arr, tsdbTFileSetClear);
H
Hongze Cheng 已提交
201 202 203 204

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

  // parse json
H
Hongze Cheng 已提交
208
  const cJSON *item1;
H
Hongze Cheng 已提交
209 210

  /* fmtv */
H
Hongze Cheng 已提交
211 212 213
  item1 = cJSON_GetObjectItem(json, "fmtv");
  if (cJSON_IsNumber(item1)) {
    ASSERT(item1->valuedouble == 1);
H
Hongze Cheng 已提交
214
  } else {
H
Hongze Cheng 已提交
215
    TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
H
Hongze Cheng 已提交
216 217 218
  }

  /* fset */
H
Hongze Cheng 已提交
219 220 221 222
  item1 = cJSON_GetObjectItem(json, "fset");
  if (cJSON_IsArray(item1)) {
    const cJSON *item2;
    cJSON_ArrayForEach(item2, item1) {
H
Hongze Cheng 已提交
223
      STFileSet *fset;
H
Hongze Cheng 已提交
224
      code = tsdbJsonToTFileSet(pTsdb, item2, &fset);
H
Hongze Cheng 已提交
225
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
226

H
Hongze Cheng 已提交
227
      code = TARRAY2_APPEND(arr, fset);
H
Hongze Cheng 已提交
228
      TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
229 230
    }
  } else {
H
Hongze Cheng 已提交
231
    TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
H
Hongze Cheng 已提交
232 233 234 235 236 237 238 239
  }

_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 已提交
240 241
}

H
Hongze Cheng 已提交
242 243 244 245 246 247 248 249
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 已提交
250
static int32_t apply_commit(STFileSystem *fs) {
H
Hongze Cheng 已提交
251
  int32_t        code = 0;
H
Hongze Cheng 已提交
252 253
  TFileSetArray *fsetArray1 = fs->fSetArr;
  TFileSetArray *fsetArray2 = fs->fSetArrTmp;
H
Hongze Cheng 已提交
254
  int32_t        i1 = 0, i2 = 0;
H
Hongze Cheng 已提交
255

H
Hongze Cheng 已提交
256
  while (i1 < TARRAY2_SIZE(fsetArray1) || i2 < TARRAY2_SIZE(fsetArray2)) {
H
Hongze Cheng 已提交
257 258
    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 已提交
259 260 261

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

H
Hongze Cheng 已提交
293 294 295 296
  return 0;
}

static int32_t commit_edit(STFileSystem *fs) {
H
Hongze Cheng 已提交
297 298
  char current[TSDB_FILENAME_LEN];
  char current_t[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
299

H
Hongze Cheng 已提交
300
  current_fname(fs->tsdb, current, TSDB_FCURRENT);
H
Hongze Cheng 已提交
301
  if (fs->etype == TSDB_FEDIT_COMMIT) {
H
Hongze Cheng 已提交
302
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_C);
H
Hongze Cheng 已提交
303
  } else if (fs->etype == TSDB_FEDIT_MERGE) {
H
Hongze Cheng 已提交
304
    current_fname(fs->tsdb, current_t, TSDB_FCURRENT_M);
H
Hongze Cheng 已提交
305 306
  } else {
    ASSERT(0);
H
Hongze Cheng 已提交
307 308
  }

H
Hongze Cheng 已提交
309 310 311
  int32_t code;
  int32_t lino;
  if ((code = taosRenameFile(current_t, current))) {
H
Hongze Cheng 已提交
312
    TSDB_CHECK_CODE(code = TAOS_SYSTEM_ERROR(code), lino, _exit);
H
Hongze Cheng 已提交
313
  }
H
Hongze Cheng 已提交
314

H
Hongze Cheng 已提交
315
  code = apply_commit(fs);
H
Hongze Cheng 已提交
316
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
317

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

H
Hongze Cheng 已提交
327 328 329 330 331 332 333
// static int32_t
static int32_t apply_abort(STFileSystem *fs) {
  // TODO
  return 0;
}

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

H
Hongze Cheng 已提交
336
  if (fs->etype == TSDB_FEDIT_COMMIT) {
H
Hongze Cheng 已提交
337
    current_fname(fs->tsdb, fname, TSDB_FCURRENT_C);
H
Hongze Cheng 已提交
338
  } else if (fs->etype == TSDB_FEDIT_MERGE) {
H
Hongze Cheng 已提交
339
    current_fname(fs->tsdb, fname, TSDB_FCURRENT_M);
H
Hongze Cheng 已提交
340 341 342
  } else {
    ASSERT(0);
  }
H
Hongze Cheng 已提交
343

H
Hongze Cheng 已提交
344 345 346
  int32_t code;
  int32_t lino;
  if ((code = taosRemoveFile(fname))) {
H
Hongze Cheng 已提交
347
    TSDB_CHECK_CODE(code = TAOS_SYSTEM_ERROR(code), lino, _exit);
H
Hongze Cheng 已提交
348
  }
H
Hongze Cheng 已提交
349

H
Hongze Cheng 已提交
350
  code = apply_abort(fs);
H
Hongze Cheng 已提交
351
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
352 353 354

_exit:
  if (code) {
H
Hongze Cheng 已提交
355
    tsdbError("vgId:%d %s failed since %s", TD_VID(fs->tsdb->pVnode), __func__, tstrerror(code));
H
Hongze Cheng 已提交
356
  } else {
H
Hongze Cheng 已提交
357
    tsdbInfo("vgId:%d %s success, etype:%d", TD_VID(fs->tsdb->pVnode), __func__, fs->etype);
H
Hongze Cheng 已提交
358 359
  }
  return code;
H
Hongze Cheng 已提交
360 361
}

H
Hongze Cheng 已提交
362 363 364 365 366
static int32_t tsdbFSScanAndFix(STFileSystem *fs) {
  fs->neid = 0;

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

H
Hongze Cheng 已提交
369
  // TODO
H
Hongze Cheng 已提交
370 371 372
  return 0;
}

H
Hongze Cheng 已提交
373 374 375
static int32_t tsdbFSDupState(STFileSystem *fs) {
  int32_t code;

H
Hongze Cheng 已提交
376 377
  const TFileSetArray *src = fs->fSetArr;
  TFileSetArray       *dst = fs->fSetArrTmp;
H
Hongze Cheng 已提交
378

H
Hongze Cheng 已提交
379 380 381 382
  TARRAY2_CLEAR(dst, tsdbTFileSetClear);

  const STFileSet *fset1;
  TARRAY2_FOREACH(src, fset1) {
H
Hongze Cheng 已提交
383
    STFileSet *fset2;
H
Hongze Cheng 已提交
384
    code = tsdbTFileSetInitDup(fs->tsdb, fset1, &fset2);
H
Hongze Cheng 已提交
385
    if (code) return code;
H
Hongze Cheng 已提交
386
    code = TARRAY2_APPEND(dst, fset2);
H
Hongze Cheng 已提交
387 388 389 390 391 392
    if (code) return code;
  }

  return 0;
}

H
Hongze Cheng 已提交
393
static int32_t open_fs(STFileSystem *fs, int8_t rollback) {
H
Hongze Cheng 已提交
394
  int32_t code = 0;
H
Hongze Cheng 已提交
395
  int32_t lino = 0;
H
Hongze Cheng 已提交
396
  STsdb  *pTsdb = fs->tsdb;
H
Hongze Cheng 已提交
397

H
Hongze Cheng 已提交
398 399 400
  char fCurrent[TSDB_FILENAME_LEN];
  char cCurrent[TSDB_FILENAME_LEN];
  char mCurrent[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
401

H
Hongze Cheng 已提交
402 403
  current_fname(pTsdb, fCurrent, TSDB_FCURRENT);
  current_fname(pTsdb, cCurrent, TSDB_FCURRENT_C);
H
Hongze Cheng 已提交
404
  current_fname(pTsdb, mCurrent, TSDB_FCURRENT_M);
H
Hongze Cheng 已提交
405

H
Hongze Cheng 已提交
406
  if (taosCheckExistFile(fCurrent)) {  // current.json exists
H
Hongze Cheng 已提交
407
    code = load_fs(pTsdb, fCurrent, fs->fSetArr);
H
Hongze Cheng 已提交
408
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
409

H
Hongze Cheng 已提交
410
    if (taosCheckExistFile(cCurrent)) {
H
Hongze Cheng 已提交
411 412 413
      // current.c.json exists

      fs->etype = TSDB_FEDIT_COMMIT;
H
Hongze Cheng 已提交
414
      if (rollback) {
H
Hongze Cheng 已提交
415
        code = abort_edit(fs);
H
Hongze Cheng 已提交
416 417
        TSDB_CHECK_CODE(code, lino, _exit);
      } else {
H
Hongze Cheng 已提交
418
        code = load_fs(pTsdb, cCurrent, fs->fSetArrTmp);
H
Hongze Cheng 已提交
419
        TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
420 421

        code = commit_edit(fs);
H
Hongze Cheng 已提交
422 423
        TSDB_CHECK_CODE(code, lino, _exit);
      }
H
Hongze Cheng 已提交
424 425 426 427
    } else if (taosCheckExistFile(mCurrent)) {
      // current.m.json exists
      fs->etype = TSDB_FEDIT_MERGE;
      code = abort_edit(fs);
H
Hongze Cheng 已提交
428 429
      TSDB_CHECK_CODE(code, lino, _exit);
    }
H
Hongze Cheng 已提交
430

H
Hongze Cheng 已提交
431
    code = tsdbFSDupState(fs);
H
Hongze Cheng 已提交
432 433
    TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
434
    code = tsdbFSScanAndFix(fs);
H
Hongze Cheng 已提交
435
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
436
  } else {
H
Hongze Cheng 已提交
437
    code = save_fs(fs->fSetArr, fCurrent);
H
Hongze Cheng 已提交
438
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
439 440 441 442
  }

_exit:
  if (code) {
H
Hongze Cheng 已提交
443
    tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
H
Hongze Cheng 已提交
444
  } else {
H
Hongze Cheng 已提交
445
    tsdbInfo("vgId:%d %s success", TD_VID(pTsdb->pVnode), __func__);
H
Hongze Cheng 已提交
446
  }
H
Hongze Cheng 已提交
447 448 449
  return 0;
}

H
Hongze Cheng 已提交
450
static int32_t close_file_system(STFileSystem *fs) {
H
Hongze Cheng 已提交
451 452
  TARRAY2_CLEAR(fs->fSetArr, tsdbTFileSetClear);
  TARRAY2_CLEAR(fs->fSetArrTmp, tsdbTFileSetClear);
H
Hongze Cheng 已提交
453
  // TODO
H
Hongze Cheng 已提交
454 455
  return 0;
}
H
Hongze Cheng 已提交
456

H
Hongze Cheng 已提交
457
static int32_t apply_edit(STFileSystem *pFS) {
H
Hongze Cheng 已提交
458
  int32_t code = 0;
H
Hongze Cheng 已提交
459
  ASSERTS(0, "TODO: Not implemented yet");
H
Hongze Cheng 已提交
460
  return code;
H
Hongze Cheng 已提交
461 462
}

H
Hongze Cheng 已提交
463
static int32_t fset_cmpr_fn(const struct STFileSet *pSet1, const struct STFileSet *pSet2) {
H
Hongze Cheng 已提交
464 465 466 467 468 469 470 471
  if (pSet1->fid < pSet2->fid) {
    return -1;
  } else if (pSet1->fid > pSet2->fid) {
    return 1;
  }
  return 0;
}

H
Hongze Cheng 已提交
472
static int32_t edit_fs(STFileSystem *fs, const TFileOpArray *opArray) {
H
more  
Hongze Cheng 已提交
473 474 475 476 477
  int32_t code = 0;
  int32_t lino = 0;

  code = tsdbFSDupState(fs);
  if (code) return code;
H
Hongze Cheng 已提交
478

H
more  
Hongze Cheng 已提交
479
  TFileSetArray  *fsetArray = fs->fSetArrTmp;
H
Hongze Cheng 已提交
480 481
  STFileSet      *fset = NULL;
  const STFileOp *op;
H
Hongze Cheng 已提交
482
  TARRAY2_FOREACH_PTR(opArray, op) {
H
Hongze Cheng 已提交
483 484 485
    if (!fset || fset->fid != op->fid) {
      STFileSet tfset = {.fid = op->fid};
      fset = &tfset;
H
Hongze Cheng 已提交
486 487
      STFileSet **fsetPtr = TARRAY2_SEARCH(fsetArray, &fset, tsdbTFileSetCmprFn, TD_EQ);
      fset = (fsetPtr == NULL) ? NULL : *fsetPtr;
H
Hongze Cheng 已提交
488 489 490 491 492

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

H
Hongze Cheng 已提交
493
        code = TARRAY2_SORT_INSERT(fsetArray, fset, tsdbTFileSetCmprFn);
H
Hongze Cheng 已提交
494 495
        TSDB_CHECK_CODE(code, lino, _exit);
      }
H
Hongze Cheng 已提交
496 497
    }

H
Hongze Cheng 已提交
498
    code = tsdbTFileSetEdit(fs->tsdb, fset, op);
H
Hongze Cheng 已提交
499
    TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
500
  }
H
Hongze Cheng 已提交
501

H
Hongze Cheng 已提交
502 503 504 505 506 507 508 509 510
  // 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 已提交
511 512
  }

H
Hongze Cheng 已提交
513
_exit:
H
Hongze Cheng 已提交
514 515 516
  if (code) {
    TSDB_ERROR_LOG(TD_VID(fs->tsdb->pVnode), lino, code);
  }
H
Hongze Cheng 已提交
517
  return code;
H
Hongze Cheng 已提交
518 519
}

H
Hongze Cheng 已提交
520
int32_t tsdbOpenFS(STsdb *pTsdb, STFileSystem **fs, int8_t rollback) {
H
Hongze Cheng 已提交
521 522 523
  int32_t code;
  int32_t lino;

H
Hongze Cheng 已提交
524 525 526
  code = tsdbCheckAndUpgradeFileSystem(pTsdb, rollback);
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
527
  code = create_fs(pTsdb, fs);
H
Hongze Cheng 已提交
528 529
  TSDB_CHECK_CODE(code, lino, _exit);

H
Hongze Cheng 已提交
530
  code = open_fs(fs[0], rollback);
H
Hongze Cheng 已提交
531
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
532 533 534

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

H
Hongze Cheng 已提交
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575
static void tsdbDoWaitBgTask(STFileSystem *fs, STFSBgTask *task) {
  task->numWait++;
  taosThreadCondWait(task->done, fs->mutex);
  task->numWait--;

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

static void tsdbDoDoneBgTask(STFileSystem *fs, STFSBgTask *task) {
  if (task->numWait > 0) {
    taosThreadCondBroadcast(task->done);
  } else {
    taosThreadCondDestroy(task->done);
    taosMemoryFree(task);
  }
}

int32_t tsdbCloseFS(STFileSystem **fs) {
  if (fs[0] == NULL) return 0;

  taosThreadMutexLock(fs[0]->mutex);
  fs[0]->stop = true;

  if (fs[0]->bgTaskRunning) {
    tsdbDoWaitBgTask(fs[0], fs[0]->bgTaskRunning);
  }
  taosThreadMutexUnlock(fs[0]->mutex);

  close_file_system(fs[0]);
  destroy_fs(fs);
H
Hongze Cheng 已提交
576 577
  return 0;
}
H
Hongze Cheng 已提交
578

H
Hongze Cheng 已提交
579 580 581 582 583
int64_t tsdbFSAllocEid(STFileSystem *fs) {
  taosThreadRwlockRdlock(&fs->tsdb->rwLock);
  int64_t cid = ++fs->neid;
  taosThreadRwlockUnlock(&fs->tsdb->rwLock);
  return cid;
H
Hongze Cheng 已提交
584 585
}

H
Hongze Cheng 已提交
586
int32_t tsdbFSEditBegin(STFileSystem *fs, const TFileOpArray *opArray, EFEditT etype) {
H
Hongze Cheng 已提交
587
  int32_t code = 0;
H
Hongze Cheng 已提交
588
  int32_t lino;
H
Hongze Cheng 已提交
589
  char    current_t[TSDB_FILENAME_LEN];
H
Hongze Cheng 已提交
590

H
Hongze Cheng 已提交
591 592
  switch (etype) {
    case TSDB_FEDIT_COMMIT:
H
Hongze Cheng 已提交
593
      current_fname(fs->tsdb, current_t, TSDB_FCURRENT_C);
H
Hongze Cheng 已提交
594 595
      break;
    case TSDB_FEDIT_MERGE:
H
Hongze Cheng 已提交
596
      current_fname(fs->tsdb, current_t, TSDB_FCURRENT_M);
H
Hongze Cheng 已提交
597 598 599
      break;
    default:
      ASSERT(0);
H
Hongze Cheng 已提交
600 601 602 603
  }

  tsem_wait(&fs->canEdit);
  fs->etype = etype;
H
Hongze Cheng 已提交
604

H
Hongze Cheng 已提交
605
  // edit
H
Hongze Cheng 已提交
606
  code = edit_fs(fs, opArray);
H
Hongze Cheng 已提交
607
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
608

H
Hongze Cheng 已提交
609
  // save fs
H
Hongze Cheng 已提交
610
  code = save_fs(fs->fSetArrTmp, current_t);
H
Hongze Cheng 已提交
611
  TSDB_CHECK_CODE(code, lino, _exit);
H
Hongze Cheng 已提交
612 613 614

_exit:
  if (code) {
H
Hongze Cheng 已提交
615
    tsdbError("vgId:%d %s failed at line %d since %s, etype:%d", TD_VID(fs->tsdb->pVnode), __func__, lino,
H
Hongze Cheng 已提交
616
              tstrerror(code), etype);
H
Hongze Cheng 已提交
617
  } else {
H
Hongze Cheng 已提交
618
    tsdbInfo("vgId:%d %s done, etype:%d", TD_VID(fs->tsdb->pVnode), __func__, etype);
H
Hongze Cheng 已提交
619 620 621 622
  }
  return code;
}

H
Hongze Cheng 已提交
623
int32_t tsdbFSEditCommit(STFileSystem *fs) {
H
Hongze Cheng 已提交
624 625 626 627 628 629 630
  int32_t code = 0;
  int32_t lino = 0;

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

H
Hongze Cheng 已提交
631 632
  // schedule merge
  if (fs->tsdb->pVnode->config.sttTrigger != 1) {
H
Hongze Cheng 已提交
633 634 635 636
    STFileSet *fset;
    TARRAY2_FOREACH_REVERSE(fs->fSetArr, fset) {
      if (TARRAY2_SIZE(fset->lvlArr) == 0) continue;

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

H
Hongze Cheng 已提交
640
      code = tsdbFSScheduleBgTask(fs, TSDB_BG_TASK_MERGER, tsdbMerge, fs->tsdb, NULL);
H
Hongze Cheng 已提交
641 642 643 644 645 646 647 648 649 650
      TSDB_CHECK_CODE(code, lino, _exit);

      break;
    }
  }

_exit:
  if (code) {
    TSDB_ERROR_LOG(TD_VID(fs->tsdb->pVnode), lino, code);
  } else {
H
Hongze Cheng 已提交
651
    tsdbDebug("vgId:%d %s done, etype:%d", TD_VID(fs->tsdb->pVnode), __func__, fs->etype);
H
Hongze Cheng 已提交
652 653
    tsem_post(&fs->canEdit);
  }
H
Hongze Cheng 已提交
654 655 656
  return code;
}

H
Hongze Cheng 已提交
657 658 659
int32_t tsdbFSEditAbort(STFileSystem *fs) {
  int32_t code = abort_edit(fs);
  tsem_post(&fs->canEdit);
H
Hongze Cheng 已提交
660
  return code;
H
Hongze Cheng 已提交
661 662
}

H
Hongze Cheng 已提交
663
int32_t tsdbFSGetFSet(STFileSystem *fs, int32_t fid, STFileSet **fset) {
H
Hongze Cheng 已提交
664 665 666 667
  STFileSet   tfset = {.fid = fid};
  STFileSet  *pset = &tfset;
  STFileSet **fsetPtr = TARRAY2_SEARCH(fs->fSetArr, &pset, tsdbTFileSetCmprFn, TD_EQ);
  fset[0] = (fsetPtr == NULL) ? NULL : fsetPtr[0];
H
Hongze Cheng 已提交
668
  return 0;
H
Hongze Cheng 已提交
669 670
}

H
Hongze Cheng 已提交
671
int32_t tsdbFSCreateCopySnapshot(STFileSystem *fs, TFileSetArray **fsetArr) {
H
Hongze Cheng 已提交
672 673 674 675
  int32_t    code = 0;
  STFileSet *fset;
  STFileSet *fset1;

H
Hongze Cheng 已提交
676 677 678 679
  fsetArr[0] = taosMemoryMalloc(sizeof(TFileSetArray));
  if (fsetArr == NULL) return TSDB_CODE_OUT_OF_MEMORY;

  TARRAY2_INIT(fsetArr[0]);
H
Hongze Cheng 已提交
680 681

  taosThreadRwlockRdlock(&fs->tsdb->rwLock);
H
Hongze Cheng 已提交
682
  TARRAY2_FOREACH(fs->fSetArr, fset) {
H
Hongze Cheng 已提交
683
    code = tsdbTFileSetInitDup(fs->tsdb, fset, &fset1);
H
Hongze Cheng 已提交
684 685
    if (code) break;

H
Hongze Cheng 已提交
686
    code = TARRAY2_APPEND(fsetArr[0], fset1);
H
Hongze Cheng 已提交
687 688 689 690 691
    if (code) break;
  }
  taosThreadRwlockUnlock(&fs->tsdb->rwLock);

  if (code) {
H
Hongze Cheng 已提交
692
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
H
Hongze Cheng 已提交
693 694
    taosMemoryFree(fsetArr[0]);
    fsetArr[0] = NULL;
H
Hongze Cheng 已提交
695 696 697 698
  }
  return code;
}

H
Hongze Cheng 已提交
699 700
int32_t tsdbFSDestroyCopySnapshot(TFileSetArray **fsetArr) {
  if (fsetArr[0]) {
H
Hongze Cheng 已提交
701
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
H
Hongze Cheng 已提交
702
    taosMemoryFree(fsetArr[0]);
H
Hongze Cheng 已提交
703 704
    fsetArr[0] = NULL;
  }
H
Hongze Cheng 已提交
705
  return 0;
H
Hongze Cheng 已提交
706 707 708 709 710 711
}

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

712
  fsetArr[0] = taosMemoryCalloc(1, sizeof(*fsetArr[0]));
H
Hongze Cheng 已提交
713 714 715 716
  if (fsetArr[0] == NULL) return TSDB_CODE_OUT_OF_MEMORY;

  taosThreadRwlockRdlock(&fs->tsdb->rwLock);
  TARRAY2_FOREACH(fs->fSetArr, fset) {
H
Hongze Cheng 已提交
717 718
    code = tsdbTFileSetInitRef(fs->tsdb, fset, &fset1);
    if (code) break;
H
Hongze Cheng 已提交
719 720 721 722 723 724 725

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

  if (code) {
H
Hongze Cheng 已提交
726
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
H
Hongze Cheng 已提交
727 728 729 730 731 732 733
    fsetArr[0] = NULL;
  }
  return code;
}

int32_t tsdbFSDestroyRefSnapshot(TFileSetArray **fsetArr) {
  if (fsetArr[0]) {
H
Hongze Cheng 已提交
734
    TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
H
Haojun Liao 已提交
735
    taosMemoryFreeClear(fsetArr[0]);
H
Hongze Cheng 已提交
736 737 738
    fsetArr[0] = NULL;
  }
  return 0;
H
Hongze Cheng 已提交
739 740 741 742 743 744 745 746 747 748 749 750 751
}

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 已提交
752 753 754 755
  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 已提交
756 757 758 759

  taosThreadMutexLock(fs->mutex);

  // free last
H
Hongze Cheng 已提交
760
  tsdbDoDoneBgTask(fs, fs->bgTaskRunning);
H
Hongze Cheng 已提交
761 762 763 764
  fs->bgTaskRunning = NULL;

  // schedule next
  if (fs->bgTaskNum > 0) {
H
Hongze Cheng 已提交
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780
    if (fs->stop) {
      while (fs->bgTaskNum > 0) {
        STFSBgTask *task = fs->bgTaskQueue->next;
        task->prev->next = task->next;
        task->next->prev = task->prev;
        fs->bgTaskNum--;
        tsdbDoDoneBgTask(fs, task);
      }
    } else {
      // 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);
    }
H
Hongze Cheng 已提交
781 782 783 784 785 786 787 788
  }

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

static int32_t tsdbFSScheduleBgTaskImpl(STFileSystem *fs, EFSBgTaskT type, int32_t (*run)(void *), void *arg,
                                        int64_t *taskid) {
H
Hongze Cheng 已提交
789 790 791 792
  if (fs->stop) {
    return 0;  // TODO: use a better error code
  }

H
Hongze Cheng 已提交
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 843 844 845 846 847 848 849 850 851 852 853 854 855
  // 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) {
H
Hongze Cheng 已提交
856
    tsdbDoWaitBgTask(fs, task);
H
Hongze Cheng 已提交
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871
  }

  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 已提交
872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
}

static int32_t tsdbFSDoDisableBgTask(STFileSystem *fs) {
  // TODO
  return 0;
}

int32_t tsdbFSDisableBgTask(STFileSystem *fs) {
  taosThreadMutexLock(fs->mutex);
  int32_t code = tsdbFSDoDisableBgTask(fs);
  taosThreadMutexUnlock(fs->mutex);
  return code;
}

int32_t tsdbFSEnableBgTask(STFileSystem *fs) {
  // TODO
  return 0;
H
Hongze Cheng 已提交
889
}