TDengine/source/dnode/vnode/src/tsdb/dev/tsdbFS.c

681 lines
17 KiB
C
Raw Normal View History

2023-03-23 09:15:51 +00:00
/*
* 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/>.
2023-03-24 09:57:37 +00:00
*/
2023-05-08 02:27:55 +00:00
#include "inc/tsdbFS.h"
2023-03-27 08:30:22 +00:00
2023-06-05 01:36:26 +00:00
extern int vnodeScheduleTask(int (*execute)(void *), void *arg);
2023-06-13 09:16:06 +00:00
extern int vnodeScheduleTaskEx(int tpid, int (*execute)(void *), void *arg);
2023-06-05 01:36:26 +00:00
2023-05-09 06:50:37 +00:00
#define TSDB_FS_EDIT_MIN TSDB_FEDIT_COMMIT
#define TSDB_FS_EDIT_MAX (TSDB_FEDIT_MERGE + 1)
2023-05-08 02:27:55 +00:00
enum {
TSDB_FS_STATE_NONE = 0,
TSDB_FS_STATE_OPEN,
TSDB_FS_STATE_EDIT,
TSDB_FS_STATE_CLOSE,
};
2023-05-08 05:20:15 +00:00
typedef enum {
TSDB_FCURRENT = 1,
2023-05-08 08:16:45 +00:00
TSDB_FCURRENT_C, // for commit
TSDB_FCURRENT_M, // for merge
2023-05-08 05:20:15 +00:00
} EFCurrentT;
static const char *gCurrentFname[] = {
[TSDB_FCURRENT] = "current.json",
2023-05-09 06:50:37 +00:00
[TSDB_FCURRENT_C] = "current.c.json",
[TSDB_FCURRENT_M] = "current.m.json",
2023-05-08 05:20:15 +00:00
};
2023-05-15 09:14:45 +00:00
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;
2023-05-31 02:59:47 +00:00
fs[0]->tsdb = pTsdb;
2023-05-15 09:14:45 +00:00
tsem_init(&fs[0]->canEdit, 0, 1);
2023-05-19 02:24:07 +00:00
fs[0]->state = TSDB_FS_STATE_NONE;
2023-05-15 09:14:45 +00:00
fs[0]->neid = 0;
2023-06-05 01:36:26 +00:00
fs[0]->mergeTaskOn = false;
2023-05-31 07:57:51 +00:00
TARRAY2_INIT(fs[0]->fSetArr);
TARRAY2_INIT(fs[0]->fSetArrTmp);
2023-04-07 07:12:31 +00:00
2023-03-27 09:07:59 +00:00
return 0;
}
2023-05-15 09:14:45 +00:00
static int32_t destroy_fs(STFileSystem **fs) {
if (fs[0] == NULL) return 0;
2023-06-08 02:31:19 +00:00
TARRAY2_DESTROY(fs[0]->fSetArr, NULL);
TARRAY2_DESTROY(fs[0]->fSetArrTmp, NULL);
2023-05-15 09:14:45 +00:00
tsem_destroy(&fs[0]->canEdit);
taosMemoryFree(fs[0]);
fs[0] = NULL;
2023-03-27 09:07:59 +00:00
return 0;
}
2023-05-08 05:20:15 +00:00
static int32_t current_fname(STsdb *pTsdb, char *fname, EFCurrentT ftype) {
2023-04-10 09:52:51 +00:00
if (pTsdb->pVnode->pTfs) {
snprintf(fname, //
TSDB_FILENAME_LEN, //
"%s%s%s%s%s", //
tfsGetPrimaryPath(pTsdb->pVnode->pTfs), //
TD_DIRSEP, //
pTsdb->path, //
TD_DIRSEP, //
2023-05-08 05:20:15 +00:00
gCurrentFname[ftype]);
2023-04-10 09:52:51 +00:00
} else {
snprintf(fname, //
TSDB_FILENAME_LEN, //
"%s%s%s", //
pTsdb->path, //
TD_DIRSEP, //
2023-05-08 05:20:15 +00:00
gCurrentFname[ftype]);
2023-04-06 08:26:41 +00:00
}
return 0;
}
2023-05-08 08:16:45 +00:00
static int32_t save_json(const cJSON *json, const char *fname) {
2023-05-08 10:15:31 +00:00
int32_t code = 0;
2023-05-08 08:16:45 +00:00
2023-05-19 05:35:32 +00:00
char *data = cJSON_PrintUnformatted(json);
2023-05-08 08:16:45 +00:00
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;
2023-04-10 09:52:51 +00:00
}
2023-05-16 03:09:15 +00:00
if (taosWriteFile(fp, data, strlen(data)) < 0) {
2023-05-08 08:16:45 +00:00
code = TAOS_SYSTEM_ERROR(code);
goto _exit;
2023-04-10 09:52:51 +00:00
}
2023-05-08 08:16:45 +00:00
if (taosFsyncFile(fp) < 0) {
code = TAOS_SYSTEM_ERROR(code);
goto _exit;
}
2023-04-10 09:52:51 +00:00
2023-05-08 08:16:45 +00:00
taosCloseFile(&fp);
2023-04-10 09:52:51 +00:00
_exit:
2023-05-08 08:16:45 +00:00
taosMemoryFree(data);
2023-04-10 09:52:51 +00:00
return code;
}
2023-05-08 10:15:31 +00:00
static int32_t load_json(const char *fname, cJSON **json) {
int32_t code = 0;
2023-05-16 03:09:15 +00:00
char *data = NULL;
2023-05-08 10:15:31 +00:00
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;
}
2023-05-16 03:09:15 +00:00
data = taosMemoryMalloc(size + 1);
2023-05-08 10:15:31 +00:00
if (data == NULL) {
code = TSDB_CODE_OUT_OF_MEMORY;
goto _exit;
}
if (taosReadFile(fp, data, size) < 0) {
code = TAOS_SYSTEM_ERROR(code);
goto _exit;
}
2023-05-16 03:09:15 +00:00
data[size] = '\0';
2023-05-08 10:15:31 +00:00
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;
}
2023-05-19 05:35:32 +00:00
static int32_t save_fs(const TFileSetArray *arr, const char *fname) {
2023-04-10 09:52:51 +00:00
int32_t code = 0;
2023-05-08 08:16:45 +00:00
int32_t lino = 0;
2023-04-10 09:52:51 +00:00
2023-05-08 08:16:45 +00:00
cJSON *json = cJSON_CreateObject();
2023-05-16 03:09:15 +00:00
if (!json) return TSDB_CODE_OUT_OF_MEMORY;
2023-04-10 09:52:51 +00:00
2023-05-08 08:16:45 +00:00
// fmtv
if (cJSON_AddNumberToObject(json, "fmtv", 1) == NULL) {
code = TSDB_CODE_OUT_OF_MEMORY;
2023-05-16 02:07:46 +00:00
TSDB_CHECK_CODE(code, lino, _exit);
2023-04-10 09:52:51 +00:00
}
2023-05-08 08:16:45 +00:00
// fset
cJSON *ajson = cJSON_AddArrayToObject(json, "fset");
2023-05-16 03:09:15 +00:00
if (!ajson) TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
2023-05-19 05:35:32 +00:00
const STFileSet *fset;
TARRAY2_FOREACH(arr, fset) {
cJSON *item = cJSON_CreateObject();
2023-05-16 02:34:02 +00:00
if (!item) TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
cJSON_AddItemToArray(ajson, item);
2023-05-08 08:16:45 +00:00
2023-05-19 07:16:01 +00:00
code = tsdbTFileSetToJson(fset, item);
2023-04-10 09:52:51 +00:00
TSDB_CHECK_CODE(code, lino, _exit);
}
2023-05-08 08:16:45 +00:00
code = save_json(json, fname);
2023-05-16 02:07:46 +00:00
TSDB_CHECK_CODE(code, lino, _exit);
2023-04-10 09:52:51 +00:00
_exit:
if (code) {
2023-05-08 08:16:45 +00:00
tsdbError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
2023-04-10 09:52:51 +00:00
}
2023-05-08 08:16:45 +00:00
cJSON_Delete(json);
2023-04-10 09:52:51 +00:00
return code;
2023-04-07 07:12:31 +00:00
}
2023-05-22 08:28:31 +00:00
static int32_t load_fs(STsdb *pTsdb, const char *fname, TFileSetArray *arr) {
2023-05-08 10:15:31 +00:00
int32_t code = 0;
int32_t lino = 0;
2023-05-19 08:44:32 +00:00
TARRAY2_CLEAR(arr, tsdbTFileSetClear);
2023-05-08 10:15:31 +00:00
// load json
cJSON *json = NULL;
code = load_json(fname, &json);
2023-05-16 02:07:46 +00:00
TSDB_CHECK_CODE(code, lino, _exit);
2023-05-08 10:15:31 +00:00
// parse json
2023-05-22 08:28:31 +00:00
const cJSON *item1;
2023-05-08 10:15:31 +00:00
/* fmtv */
2023-05-22 08:28:31 +00:00
item1 = cJSON_GetObjectItem(json, "fmtv");
if (cJSON_IsNumber(item1)) {
ASSERT(item1->valuedouble == 1);
2023-05-08 10:15:31 +00:00
} else {
2023-05-16 02:07:46 +00:00
TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
2023-05-08 10:15:31 +00:00
}
/* fset */
2023-05-22 08:28:31 +00:00
item1 = cJSON_GetObjectItem(json, "fset");
if (cJSON_IsArray(item1)) {
const cJSON *item2;
cJSON_ArrayForEach(item2, item1) {
2023-05-19 05:35:32 +00:00
STFileSet *fset;
2023-05-22 08:28:31 +00:00
code = tsdbJsonToTFileSet(pTsdb, item2, &fset);
2023-05-19 05:35:32 +00:00
TSDB_CHECK_CODE(code, lino, _exit);
2023-05-08 10:15:31 +00:00
2023-05-19 05:35:32 +00:00
code = TARRAY2_APPEND(arr, fset);
2023-05-16 02:07:46 +00:00
TSDB_CHECK_CODE(code, lino, _exit);
2023-05-08 10:15:31 +00:00
}
} else {
2023-05-16 02:07:46 +00:00
TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
2023-05-08 10:15:31 +00:00
}
_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;
2023-04-07 07:12:31 +00:00
}
2023-05-18 09:08:46 +00:00
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;
}
2023-05-09 08:24:18 +00:00
static int32_t apply_commit(STFileSystem *fs) {
2023-05-22 10:39:10 +00:00
int32_t code = 0;
2023-05-31 07:57:51 +00:00
TFileSetArray *fsetArray1 = fs->fSetArr;
TFileSetArray *fsetArray2 = fs->fSetArrTmp;
2023-05-22 10:39:10 +00:00
int32_t i1 = 0, i2 = 0;
2023-05-17 07:18:08 +00:00
2023-05-22 10:39:10 +00:00
while (i1 < TARRAY2_SIZE(fsetArray1) || i2 < TARRAY2_SIZE(fsetArray2)) {
2023-05-23 06:18:23 +00:00
STFileSet *fset1 = i1 < TARRAY2_SIZE(fsetArray1) ? TARRAY2_GET(fsetArray1, i1) : NULL;
STFileSet *fset2 = i2 < TARRAY2_SIZE(fsetArray2) ? TARRAY2_GET(fsetArray2, i2) : NULL;
2023-05-17 07:18:08 +00:00
if (fset1 && fset2) {
if (fset1->fid < fset2->fid) {
2023-05-23 03:19:24 +00:00
// delete fset1
TARRAY2_REMOVE(fsetArray1, i1, tsdbTFileSetRemove);
2023-05-17 07:18:08 +00:00
} else if (fset1->fid > fset2->fid) {
// create new file set with fid of fset2->fid
2023-05-31 02:59:47 +00:00
code = tsdbTFileSetInitEx(fs->tsdb, fset2, &fset1);
2023-05-19 09:56:21 +00:00
if (code) return code;
2023-05-22 10:39:10 +00:00
code = TARRAY2_SORT_INSERT(fsetArray1, fset1, tsdbTFileSetCmprFn);
2023-05-17 07:18:08 +00:00
if (code) return code;
i1++;
2023-05-23 01:58:40 +00:00
i2++;
2023-05-17 07:18:08 +00:00
} else {
// edit
2023-05-31 02:59:47 +00:00
code = tsdbTFileSetApplyEdit(fs->tsdb, fset2, fset1);
2023-05-17 07:18:08 +00:00
if (code) return code;
i1++;
i2++;
}
} else if (fset1) {
2023-05-23 03:19:24 +00:00
// delete fset1
TARRAY2_REMOVE(fsetArray1, i1, tsdbTFileSetRemove);
2023-05-17 07:18:08 +00:00
} else {
// create new file set with fid of fset2->fid
2023-05-31 02:59:47 +00:00
code = tsdbTFileSetInitEx(fs->tsdb, fset2, &fset1);
2023-05-19 09:56:21 +00:00
if (code) return code;
2023-05-22 10:39:10 +00:00
code = TARRAY2_SORT_INSERT(fsetArray1, fset1, tsdbTFileSetCmprFn);
2023-05-17 07:18:08 +00:00
if (code) return code;
i1++;
2023-05-23 01:58:40 +00:00
i2++;
2023-05-17 07:18:08 +00:00
}
}
2023-05-16 07:48:17 +00:00
2023-05-09 08:24:18 +00:00
return 0;
}
static int32_t commit_edit(STFileSystem *fs) {
2023-05-09 06:50:37 +00:00
char current[TSDB_FILENAME_LEN];
char current_t[TSDB_FILENAME_LEN];
2023-04-07 08:52:30 +00:00
2023-05-31 02:59:47 +00:00
current_fname(fs->tsdb, current, TSDB_FCURRENT);
2023-05-09 08:24:18 +00:00
if (fs->etype == TSDB_FEDIT_COMMIT) {
2023-05-31 02:59:47 +00:00
current_fname(fs->tsdb, current_t, TSDB_FCURRENT_C);
2023-05-09 08:24:18 +00:00
} else if (fs->etype == TSDB_FEDIT_MERGE) {
2023-05-31 02:59:47 +00:00
current_fname(fs->tsdb, current_t, TSDB_FCURRENT_M);
2023-05-09 06:50:37 +00:00
} else {
ASSERT(0);
2023-04-07 08:52:30 +00:00
}
2023-05-09 06:50:37 +00:00
int32_t code;
int32_t lino;
if ((code = taosRenameFile(current_t, current))) {
2023-05-16 02:07:46 +00:00
TSDB_CHECK_CODE(code = TAOS_SYSTEM_ERROR(code), lino, _exit);
2023-05-09 06:50:37 +00:00
}
2023-04-07 08:52:30 +00:00
2023-05-09 08:24:18 +00:00
code = apply_commit(fs);
2023-05-16 02:07:46 +00:00
TSDB_CHECK_CODE(code, lino, _exit);
2023-05-09 08:24:18 +00:00
2023-05-09 06:50:37 +00:00
_exit:
if (code) {
2023-05-31 02:59:47 +00:00
tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(fs->tsdb->pVnode), __func__, lino, tstrerror(code));
2023-05-09 08:24:18 +00:00
} else {
2023-05-31 02:59:47 +00:00
tsdbInfo("vgId:%d %s success, etype:%d", TD_VID(fs->tsdb->pVnode), __func__, fs->etype);
2023-05-09 06:50:37 +00:00
}
return code;
2023-04-07 07:12:31 +00:00
}
2023-05-09 08:24:18 +00:00
// static int32_t
static int32_t apply_abort(STFileSystem *fs) {
// TODO
return 0;
}
static int32_t abort_edit(STFileSystem *fs) {
2023-05-09 06:50:37 +00:00
char fname[TSDB_FILENAME_LEN];
2023-04-07 08:52:30 +00:00
2023-05-09 08:24:18 +00:00
if (fs->etype == TSDB_FEDIT_COMMIT) {
2023-05-31 02:59:47 +00:00
current_fname(fs->tsdb, fname, TSDB_FCURRENT_C);
2023-05-09 08:24:18 +00:00
} else if (fs->etype == TSDB_FEDIT_MERGE) {
2023-05-31 02:59:47 +00:00
current_fname(fs->tsdb, fname, TSDB_FCURRENT_M);
2023-05-09 06:50:37 +00:00
} else {
ASSERT(0);
}
2023-04-07 08:52:30 +00:00
2023-05-09 08:24:18 +00:00
int32_t code;
int32_t lino;
if ((code = taosRemoveFile(fname))) {
2023-05-16 02:07:46 +00:00
TSDB_CHECK_CODE(code = TAOS_SYSTEM_ERROR(code), lino, _exit);
2023-05-09 08:24:18 +00:00
}
2023-04-07 08:52:30 +00:00
2023-05-09 08:24:18 +00:00
code = apply_abort(fs);
2023-05-16 02:07:46 +00:00
TSDB_CHECK_CODE(code, lino, _exit);
2023-05-09 08:24:18 +00:00
_exit:
if (code) {
2023-05-31 02:59:47 +00:00
tsdbError("vgId:%d %s failed since %s", TD_VID(fs->tsdb->pVnode), __func__, tstrerror(code));
2023-05-09 08:24:18 +00:00
} else {
2023-05-31 02:59:47 +00:00
tsdbInfo("vgId:%d %s success, etype:%d", TD_VID(fs->tsdb->pVnode), __func__, fs->etype);
2023-05-09 08:24:18 +00:00
}
return code;
2023-04-07 08:52:30 +00:00
}
2023-05-22 08:28:31 +00:00
static int32_t tsdbFSScanAndFix(STFileSystem *fs) {
fs->neid = 0;
// get max commit id
const STFileSet *fset;
2023-05-31 07:57:51 +00:00
TARRAY2_FOREACH(fs->fSetArr, fset) { fs->neid = TMAX(fs->neid, tsdbTFileSetMaxCid(fset)); }
2023-05-22 08:28:31 +00:00
2023-05-09 06:50:37 +00:00
// TODO
2023-04-07 07:12:31 +00:00
return 0;
}
2023-05-08 05:20:15 +00:00
static int32_t update_fs_if_needed(STFileSystem *pFS) {
// TODO
return 0;
}
2023-05-22 08:28:31 +00:00
static int32_t tsdbFSDupState(STFileSystem *fs) {
int32_t code;
2023-05-31 07:57:51 +00:00
const TFileSetArray *src = fs->fSetArr;
TFileSetArray *dst = fs->fSetArrTmp;
2023-05-22 08:28:31 +00:00
2023-05-19 08:44:32 +00:00
TARRAY2_CLEAR(dst, tsdbTFileSetClear);
const STFileSet *fset1;
TARRAY2_FOREACH(src, fset1) {
2023-05-22 08:28:31 +00:00
STFileSet *fset2;
2023-05-31 02:59:47 +00:00
code = tsdbTFileSetInitEx(fs->tsdb, fset1, &fset2);
2023-05-19 08:44:32 +00:00
if (code) return code;
2023-05-22 08:28:31 +00:00
code = TARRAY2_APPEND(dst, fset2);
2023-05-19 08:44:32 +00:00
if (code) return code;
}
return 0;
}
2023-05-09 06:50:37 +00:00
static int32_t open_fs(STFileSystem *fs, int8_t rollback) {
2023-04-07 07:12:31 +00:00
int32_t code = 0;
2023-05-08 02:27:55 +00:00
int32_t lino = 0;
2023-05-31 02:59:47 +00:00
STsdb *pTsdb = fs->tsdb;
2023-04-07 07:12:31 +00:00
2023-05-09 06:50:37 +00:00
code = update_fs_if_needed(fs);
2023-05-16 02:07:46 +00:00
TSDB_CHECK_CODE(code, lino, _exit);
2023-04-07 07:12:31 +00:00
2023-05-08 05:20:15 +00:00
char fCurrent[TSDB_FILENAME_LEN];
char cCurrent[TSDB_FILENAME_LEN];
char mCurrent[TSDB_FILENAME_LEN];
2023-04-07 07:12:31 +00:00
2023-05-08 05:20:15 +00:00
current_fname(pTsdb, fCurrent, TSDB_FCURRENT);
current_fname(pTsdb, cCurrent, TSDB_FCURRENT_C);
2023-05-08 08:16:45 +00:00
current_fname(pTsdb, mCurrent, TSDB_FCURRENT_M);
2023-04-07 07:12:31 +00:00
2023-05-08 05:20:15 +00:00
if (taosCheckExistFile(fCurrent)) { // current.json exists
2023-05-31 07:57:51 +00:00
code = load_fs(pTsdb, fCurrent, fs->fSetArr);
2023-05-19 05:35:32 +00:00
TSDB_CHECK_CODE(code, lino, _exit);
2023-04-07 07:12:31 +00:00
2023-05-08 05:20:15 +00:00
if (taosCheckExistFile(cCurrent)) {
2023-05-09 06:50:37 +00:00
// current.c.json exists
fs->etype = TSDB_FEDIT_COMMIT;
2023-05-08 05:20:15 +00:00
if (rollback) {
2023-05-09 06:50:37 +00:00
code = abort_edit(fs);
2023-05-08 05:20:15 +00:00
TSDB_CHECK_CODE(code, lino, _exit);
} else {
2023-05-31 07:57:51 +00:00
code = load_fs(pTsdb, cCurrent, fs->fSetArrTmp);
2023-05-19 08:44:32 +00:00
TSDB_CHECK_CODE(code, lino, _exit);
2023-05-09 06:50:37 +00:00
code = commit_edit(fs);
2023-04-07 07:12:31 +00:00
TSDB_CHECK_CODE(code, lino, _exit);
}
2023-05-09 06:50:37 +00:00
} else if (taosCheckExistFile(mCurrent)) {
// current.m.json exists
fs->etype = TSDB_FEDIT_MERGE;
code = abort_edit(fs);
2023-04-07 07:12:31 +00:00
TSDB_CHECK_CODE(code, lino, _exit);
}
2023-05-08 08:16:45 +00:00
2023-05-22 08:28:31 +00:00
code = tsdbFSDupState(fs);
2023-05-19 08:44:32 +00:00
TSDB_CHECK_CODE(code, lino, _exit);
2023-05-22 08:28:31 +00:00
code = tsdbFSScanAndFix(fs);
2023-05-08 08:16:45 +00:00
TSDB_CHECK_CODE(code, lino, _exit);
2023-05-08 05:20:15 +00:00
} else {
2023-05-31 07:57:51 +00:00
code = save_fs(fs->fSetArr, fCurrent);
2023-05-19 05:35:32 +00:00
TSDB_CHECK_CODE(code, lino, _exit);
2023-04-07 07:12:31 +00:00
}
_exit:
if (code) {
2023-05-08 02:27:55 +00:00
tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
2023-04-07 07:12:31 +00:00
} else {
2023-05-08 02:27:55 +00:00
tsdbInfo("vgId:%d %s success", TD_VID(pTsdb->pVnode), __func__);
2023-04-07 07:12:31 +00:00
}
2023-03-27 09:07:59 +00:00
return 0;
}
2023-05-22 08:28:31 +00:00
static int32_t close_file_system(STFileSystem *fs) {
2023-05-31 07:57:51 +00:00
TARRAY2_CLEAR(fs->fSetArr, tsdbTFileSetClear);
TARRAY2_CLEAR(fs->fSetArrTmp, tsdbTFileSetClear);
2023-05-22 08:28:31 +00:00
// TODO
2023-03-27 08:30:22 +00:00
return 0;
}
2023-03-27 09:07:59 +00:00
2023-05-08 02:27:55 +00:00
static int32_t apply_edit(STFileSystem *pFS) {
2023-04-11 09:04:52 +00:00
int32_t code = 0;
2023-04-07 07:12:31 +00:00
ASSERTS(0, "TODO: Not implemented yet");
2023-04-11 09:04:52 +00:00
return code;
2023-04-06 08:26:41 +00:00
}
2023-05-08 02:27:55 +00:00
static int32_t fset_cmpr_fn(const struct STFileSet *pSet1, const struct STFileSet *pSet2) {
2023-04-12 07:18:12 +00:00
if (pSet1->fid < pSet2->fid) {
return -1;
} else if (pSet1->fid > pSet2->fid) {
return 1;
}
return 0;
}
2023-05-22 10:39:10 +00:00
static int32_t edit_fs(STFileSystem *fs, const TFileOpArray *opArray) {
int32_t code = 0;
int32_t lino = 0;
2023-05-31 07:57:51 +00:00
TFileSetArray *fsetArray = fs->fSetArrTmp;
2023-05-19 09:20:53 +00:00
STFileSet *fset = NULL;
const STFileOp *op;
2023-05-22 10:39:10 +00:00
TARRAY2_FOREACH_PTR(opArray, op) {
2023-05-19 09:20:53 +00:00
if (!fset || fset->fid != op->fid) {
STFileSet tfset = {.fid = op->fid};
fset = &tfset;
2023-05-23 03:19:24 +00:00
fset = TARRAY2_SEARCH_EX(fsetArray, &fset, tsdbTFileSetCmprFn, TD_EQ);
2023-05-19 09:20:53 +00:00
if (!fset) {
code = tsdbTFileSetInit(op->fid, &fset);
TSDB_CHECK_CODE(code, lino, _exit);
2023-05-22 10:39:10 +00:00
code = TARRAY2_SORT_INSERT(fsetArray, fset, tsdbTFileSetCmprFn);
2023-05-19 09:20:53 +00:00
TSDB_CHECK_CODE(code, lino, _exit);
}
2023-05-12 01:07:50 +00:00
}
2023-05-31 02:59:47 +00:00
code = tsdbTFileSetEdit(fs->tsdb, fset, op);
2023-05-16 02:07:46 +00:00
TSDB_CHECK_CODE(code, lino, _exit);
2023-05-22 10:39:10 +00:00
}
2023-05-19 09:20:53 +00:00
2023-05-23 07:07:42 +00:00
// 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++;
}
2023-04-11 07:34:40 +00:00
}
2023-04-11 09:04:52 +00:00
_exit:
2023-05-19 09:20:53 +00:00
return code;
2023-04-06 08:26:41 +00:00
}
2023-05-15 09:14:45 +00:00
int32_t tsdbOpenFS(STsdb *pTsdb, STFileSystem **fs, int8_t rollback) {
2023-03-27 09:07:59 +00:00
int32_t code;
int32_t lino;
2023-05-15 09:14:45 +00:00
code = create_fs(pTsdb, fs);
2023-03-27 09:07:59 +00:00
TSDB_CHECK_CODE(code, lino, _exit);
2023-05-15 09:14:45 +00:00
code = open_fs(fs[0], rollback);
2023-05-16 02:07:46 +00:00
TSDB_CHECK_CODE(code, lino, _exit);
2023-03-27 09:07:59 +00:00
_exit:
if (code) {
2023-05-08 02:27:55 +00:00
tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(pTsdb->pVnode), __func__, lino, tstrerror(code));
2023-05-15 09:14:45 +00:00
destroy_fs(fs);
2023-03-27 09:07:59 +00:00
} else {
2023-05-08 02:27:55 +00:00
tsdbInfo("vgId:%d %s success", TD_VID(pTsdb->pVnode), __func__);
2023-03-27 09:07:59 +00:00
}
return 0;
}
2023-05-09 08:24:18 +00:00
int32_t tsdbCloseFS(STFileSystem **ppFS) {
2023-03-27 09:07:59 +00:00
if (ppFS[0] == NULL) return 0;
close_file_system(ppFS[0]);
2023-05-08 05:20:15 +00:00
destroy_fs(ppFS);
2023-03-27 09:07:59 +00:00
return 0;
}
2023-04-06 08:26:41 +00:00
2023-06-01 07:08:45 +00:00
int64_t tsdbFSAllocEid(STFileSystem *fs) {
taosThreadRwlockRdlock(&fs->tsdb->rwLock);
int64_t cid = ++fs->neid;
taosThreadRwlockUnlock(&fs->tsdb->rwLock);
return cid;
2023-05-11 10:16:55 +00:00
}
2023-05-22 10:39:10 +00:00
int32_t tsdbFSEditBegin(STFileSystem *fs, const TFileOpArray *opArray, EFEditT etype) {
2023-04-06 08:26:41 +00:00
int32_t code = 0;
2023-04-11 07:34:40 +00:00
int32_t lino;
2023-05-09 08:46:23 +00:00
char current_t[TSDB_FILENAME_LEN];
2023-04-06 08:26:41 +00:00
2023-05-19 08:44:32 +00:00
switch (etype) {
case TSDB_FEDIT_COMMIT:
2023-05-31 02:59:47 +00:00
current_fname(fs->tsdb, current_t, TSDB_FCURRENT_C);
2023-05-19 08:44:32 +00:00
break;
case TSDB_FEDIT_MERGE:
2023-05-31 02:59:47 +00:00
current_fname(fs->tsdb, current_t, TSDB_FCURRENT_M);
2023-05-19 08:44:32 +00:00
break;
default:
ASSERT(0);
2023-05-09 08:46:23 +00:00
}
tsem_wait(&fs->canEdit);
2023-04-06 08:26:41 +00:00
2023-05-09 08:46:23 +00:00
fs->etype = etype;
2023-04-06 08:26:41 +00:00
2023-05-09 08:46:23 +00:00
// edit
2023-05-22 10:39:10 +00:00
code = edit_fs(fs, opArray);
2023-05-09 08:46:23 +00:00
TSDB_CHECK_CODE(code, lino, _exit);
2023-04-11 07:34:40 +00:00
2023-05-09 08:46:23 +00:00
// save fs
2023-05-31 07:57:51 +00:00
code = save_fs(fs->fSetArrTmp, current_t);
2023-05-19 08:44:32 +00:00
TSDB_CHECK_CODE(code, lino, _exit);
2023-04-06 08:26:41 +00:00
_exit:
if (code) {
2023-05-31 02:59:47 +00:00
tsdbError("vgId:%d %s failed at line %d since %s, etype:%d", TD_VID(fs->tsdb->pVnode), __func__, lino,
2023-05-19 08:44:32 +00:00
tstrerror(code), etype);
2023-04-07 08:52:30 +00:00
} else {
2023-05-31 02:59:47 +00:00
tsdbInfo("vgId:%d %s done, etype:%d", TD_VID(fs->tsdb->pVnode), __func__, etype);
2023-04-06 08:26:41 +00:00
}
return code;
}
2023-05-09 08:46:23 +00:00
int32_t tsdbFSEditCommit(STFileSystem *fs) {
2023-06-05 01:36:26 +00:00
int32_t code = 0;
int32_t lino = 0;
// commit
code = commit_edit(fs);
TSDB_CHECK_CODE(code, lino, _exit);
if (fs->etype == TSDB_FEDIT_MERGE) {
ASSERT(fs->mergeTaskOn);
fs->mergeTaskOn = false;
}
// check if need to merge
2023-06-12 08:12:29 +00:00
if (fs->tsdb->pVnode->config.sttTrigger > 1 && fs->mergeTaskOn == false) {
2023-06-05 01:36:26 +00:00
STFileSet *fset;
TARRAY2_FOREACH_REVERSE(fs->fSetArr, fset) {
if (TARRAY2_SIZE(fset->lvlArr) == 0) continue;
SSttLvl *lvl0 = TARRAY2_FIRST(fset->lvlArr);
if (lvl0->level != 0 || TARRAY2_SIZE(lvl0->fobjArr) == 0) continue;
STFileObj *fobj = TARRAY2_FIRST(lvl0->fobjArr);
if (fobj->f->stt->nseg < fs->tsdb->pVnode->config.sttTrigger) continue;
2023-06-13 09:16:06 +00:00
code = vnodeScheduleTaskEx(1, tsdbMerge, fs->tsdb);
2023-06-05 01:36:26 +00:00
TSDB_CHECK_CODE(code, lino, _exit);
fs->mergeTaskOn = true;
break;
}
}
_exit:
if (code) {
TSDB_ERROR_LOG(TD_VID(fs->tsdb->pVnode), lino, code);
} else {
tsem_post(&fs->canEdit);
}
2023-04-06 08:26:41 +00:00
return code;
}
2023-05-09 08:46:23 +00:00
int32_t tsdbFSEditAbort(STFileSystem *fs) {
int32_t code = abort_edit(fs);
tsem_post(&fs->canEdit);
2023-04-06 08:26:41 +00:00
return code;
2023-05-11 10:16:55 +00:00
}
2023-05-23 03:35:44 +00:00
int32_t tsdbFSGetFSet(STFileSystem *fs, int32_t fid, STFileSet **fset) {
STFileSet tfset = {.fid = fid};
STFileSet *pset = &tfset;
2023-05-31 07:57:51 +00:00
fset[0] = TARRAY2_SEARCH_EX(fs->fSetArr, &pset, tsdbTFileSetCmprFn, TD_EQ);
2023-05-11 10:16:55 +00:00
return 0;
2023-05-31 02:59:47 +00:00
}
2023-05-31 07:57:51 +00:00
int32_t tsdbFSCreateCopySnapshot(STFileSystem *fs, TFileSetArray **fsetArr) {
2023-05-31 02:59:47 +00:00
int32_t code = 0;
STFileSet *fset;
STFileSet *fset1;
2023-05-31 07:57:51 +00:00
fsetArr[0] = taosMemoryMalloc(sizeof(TFileSetArray));
if (fsetArr == NULL) return TSDB_CODE_OUT_OF_MEMORY;
TARRAY2_INIT(fsetArr[0]);
2023-05-31 02:59:47 +00:00
taosThreadRwlockRdlock(&fs->tsdb->rwLock);
2023-05-31 07:57:51 +00:00
TARRAY2_FOREACH(fs->fSetArr, fset) {
2023-05-31 02:59:47 +00:00
code = tsdbTFileSetInitEx(fs->tsdb, fset, &fset1);
if (code) break;
2023-05-31 07:57:51 +00:00
code = TARRAY2_APPEND(fsetArr[0], fset1);
2023-05-31 02:59:47 +00:00
if (code) break;
}
taosThreadRwlockUnlock(&fs->tsdb->rwLock);
if (code) {
2023-06-08 02:31:19 +00:00
TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
2023-05-31 07:57:51 +00:00
taosMemoryFree(fsetArr[0]);
fsetArr[0] = NULL;
2023-05-31 02:59:47 +00:00
}
return code;
}
2023-05-31 07:57:51 +00:00
int32_t tsdbFSDestroyCopySnapshot(TFileSetArray **fsetArr) {
if (fsetArr[0]) {
2023-06-08 02:31:19 +00:00
TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
2023-05-31 07:57:51 +00:00
fsetArr[0] = NULL;
}
2023-05-31 02:59:47 +00:00
return 0;
2023-04-06 08:26:41 +00:00
}