TDengine/source/dnode/vnode/src/tsdb/tsdbFS2.c

1219 lines
32 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-06-14 06:05:41 +00:00
#include "tsdbFS2.h"
2023-11-02 12:13:29 +00:00
#include "cos.h"
2023-06-21 06:53:36 +00:00
#include "tsdbUpgrade.h"
#include "vnd.h"
2023-03-27 08:30:22 +00:00
2023-10-09 08:03:42 +00:00
#define BLOCK_COMMIT_FACTOR 3
extern void remove_file(const char *fname);
2023-06-05 01:36:26 +00:00
2023-09-01 05:24:47 +00:00
typedef struct STFileHashEntry {
struct STFileHashEntry *next;
char fname[TSDB_FILENAME_LEN];
} STFileHashEntry;
typedef struct {
int32_t numFile;
int32_t numBucket;
STFileHashEntry **buckets;
} STFileHash;
2023-05-08 05:20:15 +00:00
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]));
2024-07-18 09:17:02 +00:00
if (fs[0] == NULL) {
return TSDB_CODE_OUT_OF_MEMORY;
}
2023-05-15 09:14:45 +00:00
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);
fs[0]->fsstate = TSDB_FS_STATE_NORMAL;
2023-05-15 09:14:45 +00:00
fs[0]->neid = 0;
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-20 10:23:58 +00:00
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-06-21 06:53:36 +00:00
int32_t current_fname(STsdb *pTsdb, char *fname, EFCurrentT ftype) {
int32_t offset = 0;
vnodeGetPrimaryDir(pTsdb->path, pTsdb->pVnode->diskPrimary, pTsdb->pVnode->pTfs, fname, TSDB_FILENAME_LEN);
offset = strlen(fname);
snprintf(fname + offset, TSDB_FILENAME_LEN - offset - 1, "%s%s", TD_DIRSEP, 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) {
2024-07-18 09:17:02 +00:00
int32_t code = 0;
int32_t lino;
char *data = NULL;
TdFilePtr fp = NULL;
2023-05-08 08:16:45 +00:00
2024-07-18 09:17:02 +00:00
data = cJSON_PrintUnformatted(json);
if (data == NULL) {
TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
}
2023-05-08 08:16:45 +00:00
2024-07-18 09:17:02 +00:00
fp = taosOpenFile(fname, TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC | TD_FILE_WRITE_THROUGH);
2023-05-08 08:16:45 +00:00
if (fp == NULL) {
2024-07-18 09:17:02 +00:00
TSDB_CHECK_CODE(code = TAOS_SYSTEM_ERROR(code), lino, _exit);
2023-04-10 09:52:51 +00:00
}
2023-05-16 03:09:15 +00:00
if (taosWriteFile(fp, data, strlen(data)) < 0) {
2024-07-18 09:17:02 +00:00
TSDB_CHECK_CODE(code = TAOS_SYSTEM_ERROR(code), lino, _exit);
2023-04-10 09:52:51 +00:00
}
2023-05-08 08:16:45 +00:00
if (taosFsyncFile(fp) < 0) {
2024-07-18 09:17:02 +00:00
TSDB_CHECK_CODE(code = TAOS_SYSTEM_ERROR(code), lino, _exit);
2023-05-08 08:16:45 +00:00
}
2023-04-10 09:52:51 +00:00
_exit:
2024-07-18 09:17:02 +00:00
if (code) {
tsdbError("%s failed at %s:%d since %s", __func__, fname, __LINE__, tstrerror(code));
}
2023-05-08 08:16:45 +00:00
taosMemoryFree(data);
2024-07-18 09:17:02 +00:00
taosCloseFile(&fp);
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;
2024-07-18 09:17:02 +00:00
int32_t lino;
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);
2024-07-18 09:17:02 +00:00
if (fp == NULL) {
TSDB_CHECK_CODE(code = TAOS_SYSTEM_ERROR(errno), lino, _exit);
}
2023-05-08 10:15:31 +00:00
int64_t size;
if (taosFStatFile(fp, &size, NULL) < 0) {
2024-07-18 09:17:02 +00:00
TSDB_CHECK_CODE(code = TAOS_SYSTEM_ERROR(code), lino, _exit);
2023-05-08 10:15:31 +00:00
}
2023-05-16 03:09:15 +00:00
data = taosMemoryMalloc(size + 1);
2023-05-08 10:15:31 +00:00
if (data == NULL) {
2024-07-18 09:17:02 +00:00
TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
2023-05-08 10:15:31 +00:00
}
if (taosReadFile(fp, data, size) < 0) {
2024-07-18 09:17:02 +00:00
TSDB_CHECK_CODE(code = TAOS_SYSTEM_ERROR(code), lino, _exit);
2023-05-08 10:15:31 +00:00
}
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) {
2024-07-18 09:17:02 +00:00
TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
2023-05-08 10:15:31 +00:00
}
_exit:
2024-07-18 09:17:02 +00:00
if (code) {
tsdbError("%s failed at %s:%d since %s", __func__, fname, __LINE__, tstrerror(code));
json[0] = NULL;
}
2023-05-08 10:15:31 +00:00
taosCloseFile(&fp);
2024-07-18 09:17:02 +00:00
taosMemoryFree(data);
2023-05-08 10:15:31 +00:00
return code;
}
2023-06-21 06:53:36 +00:00
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();
2024-07-19 05:02:34 +00:00
if (json == NULL) {
TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
}
2023-04-10 09:52:51 +00:00
2023-05-08 08:16:45 +00:00
// fmtv
if (cJSON_AddNumberToObject(json, "fmtv", 1) == NULL) {
2024-07-18 09:17:02 +00:00
TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, 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-09-05 03:41:36 +00:00
if (!ajson) {
2024-07-18 09:17:02 +00:00
TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
2023-09-05 03:41:36 +00:00
}
2023-05-19 05:35:32 +00:00
const STFileSet *fset;
TARRAY2_FOREACH(arr, fset) {
cJSON *item = cJSON_CreateObject();
2023-09-05 03:41:36 +00:00
if (!item) {
2024-07-18 09:17:02 +00:00
TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
2023-09-05 03:41:36 +00:00
}
2023-05-16 02:34:02 +00:00
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
}
2023-09-26 01:23:22 +00:00
TARRAY2_SORT(arr, tsdbTFileSetCmprFn);
2023-05-08 10:15:31 +00:00
} else {
2023-09-05 03:41:36 +00:00
code = TSDB_CODE_FILE_CORRUPTED;
TSDB_CHECK_CODE(code, lino, _exit);
2023-05-08 10:15:31 +00:00
}
_exit:
if (code) {
2024-07-18 09:17:02 +00:00
tsdbError("%s failed at %sP%d since %s, fname:%s", __func__, __FILE__, lino, tstrerror(code), fname);
}
if (json) {
cJSON_Delete(json);
2023-05-08 10:15:31 +00:00
}
return code;
2023-04-07 07:12:31 +00:00
}
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;
2024-07-18 09:17:02 +00:00
int32_t lino;
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
2023-10-27 09:48:19 +00:00
tsdbTFileSetRemove(fset1);
i1++;
2023-05-17 07:18:08 +00:00
} else if (fset1->fid > fset2->fid) {
// create new file set with fid of fset2->fid
2023-10-27 09:48:19 +00:00
code = tsdbTFileSetInitCopy(fs->tsdb, fset2, &fset1);
2024-07-18 09:17:02 +00:00
TSDB_CHECK_CODE(code, lino, _exit);
2023-05-22 10:39:10 +00:00
code = TARRAY2_SORT_INSERT(fsetArray1, fset1, tsdbTFileSetCmprFn);
2024-07-18 09:17:02 +00:00
TSDB_CHECK_CODE(code, lino, _exit);
2023-05-17 07:18:08 +00:00
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);
2024-07-18 09:17:02 +00:00
TSDB_CHECK_CODE(code, lino, _exit);
2023-05-17 07:18:08 +00:00
i1++;
i2++;
}
} else if (fset1) {
2023-05-23 03:19:24 +00:00
// delete fset1
2023-10-27 09:48:19 +00:00
tsdbTFileSetRemove(fset1);
i1++;
2023-05-17 07:18:08 +00:00
} else {
// create new file set with fid of fset2->fid
2023-10-27 09:48:19 +00:00
code = tsdbTFileSetInitCopy(fs->tsdb, fset2, &fset1);
2024-07-18 09:17:02 +00:00
TSDB_CHECK_CODE(code, lino, _exit);
2023-05-22 10:39:10 +00:00
code = TARRAY2_SORT_INSERT(fsetArray1, fset1, tsdbTFileSetCmprFn);
2024-07-18 09:17:02 +00:00
TSDB_CHECK_CODE(code, lino, _exit);
2023-05-17 07:18:08 +00:00
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
2024-07-18 09:17:02 +00:00
_exit:
if (code) {
TSDB_ERROR_LOG(TD_VID(fs->tsdb->pVnode), lino, code);
}
return code;
2023-05-09 08:24:18 +00:00
}
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 06:50:37 +00:00
} else {
2024-05-23 09:57:07 +00:00
current_fname(fs->tsdb, current_t, TSDB_FCURRENT_M);
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-09-05 03:41:36 +00:00
code = TAOS_SYSTEM_ERROR(code);
TSDB_CHECK_CODE(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) {
2024-07-18 09:17:02 +00:00
tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(fs->tsdb->pVnode), __func__, __FILE__, 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
2023-09-01 05:24:47 +00:00
static int32_t tsdbFSDoSanAndFix(STFileSystem *fs);
static int32_t apply_abort(STFileSystem *fs) { return tsdbFSDoSanAndFix(fs); }
2023-05-09 08:24:18 +00:00
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 06:50:37 +00:00
} else {
2024-05-23 09:57:07 +00:00
current_fname(fs->tsdb, fname, TSDB_FCURRENT_M);
2023-05-09 06:50:37 +00:00
}
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-09-05 03:41:36 +00:00
code = TAOS_SYSTEM_ERROR(code);
TSDB_CHECK_CODE(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) {
2024-07-18 09:17:02 +00:00
tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(fs->tsdb->pVnode), __func__, __FILE__, 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 08:24:18 +00:00
}
return code;
2023-04-07 08:52:30 +00:00
}
2023-09-01 05:24:47 +00:00
static int32_t tsdbFSDoScanAndFixFile(STFileSystem *fs, const STFileObj *fobj) {
int32_t code = 0;
int32_t lino = 0;
// check file existence
if (!taosCheckExistFile(fobj->fname)) {
bool found = false;
if (tsS3Enabled && fobj->f->lcn > 1) {
char fname1[TSDB_FILENAME_LEN];
tsdbTFileLastChunkName(fs->tsdb, fobj->f, fname1);
if (!taosCheckExistFile(fname1)) {
code = TSDB_CODE_FILE_CORRUPTED;
tsdbError("vgId:%d %s failed since file:%s does not exist", TD_VID(fs->tsdb->pVnode), __func__, fname1);
return code;
2023-09-01 05:24:47 +00:00
}
found = true;
2023-09-01 05:24:47 +00:00
}
if (!found) {
code = TSDB_CODE_FILE_CORRUPTED;
tsdbError("vgId:%d %s failed since file:%s does not exist", TD_VID(fs->tsdb->pVnode), __func__, fobj->fname);
return code;
}
2023-09-01 05:24:47 +00:00
}
return 0;
}
static void tsdbFSDestroyFileObjHash(STFileHash *hash);
static int32_t tsdbFSAddEntryToFileObjHash(STFileHash *hash, const char *fname) {
STFileHashEntry *entry = taosMemoryMalloc(sizeof(*entry));
if (entry == NULL) return TSDB_CODE_OUT_OF_MEMORY;
2023-09-05 03:41:36 +00:00
strncpy(entry->fname, fname, TSDB_FILENAME_LEN);
2023-09-01 05:24:47 +00:00
uint32_t idx = MurmurHash3_32(fname, strlen(fname)) % hash->numBucket;
entry->next = hash->buckets[idx];
hash->buckets[idx] = entry;
hash->numFile++;
return 0;
}
static int32_t tsdbFSCreateFileObjHash(STFileSystem *fs, STFileHash *hash) {
int32_t code = 0;
2024-07-18 09:17:02 +00:00
int32_t lino;
2023-09-01 05:24:47 +00:00
char fname[TSDB_FILENAME_LEN];
// init hash table
hash->numFile = 0;
hash->numBucket = 4096;
hash->buckets = taosMemoryCalloc(hash->numBucket, sizeof(STFileHashEntry *));
if (hash->buckets == NULL) {
2024-07-18 09:17:02 +00:00
TSDB_CHECK_CODE(code = TSDB_CODE_OUT_OF_MEMORY, lino, _exit);
2023-09-01 05:24:47 +00:00
}
// vnode.json
current_fname(fs->tsdb, fname, TSDB_FCURRENT);
code = tsdbFSAddEntryToFileObjHash(hash, fname);
2024-07-18 09:17:02 +00:00
TSDB_CHECK_CODE(code, lino, _exit);
2023-09-01 05:24:47 +00:00
// other
STFileSet *fset = NULL;
TARRAY2_FOREACH(fs->fSetArr, fset) {
// data file
for (int32_t i = 0; i < TSDB_FTYPE_MAX; i++) {
if (fset->farr[i] != NULL) {
code = tsdbFSAddEntryToFileObjHash(hash, fset->farr[i]->fname);
2024-07-18 09:17:02 +00:00
TSDB_CHECK_CODE(code, lino, _exit);
2023-09-01 05:24:47 +00:00
}
}
// stt file
SSttLvl *lvl = NULL;
TARRAY2_FOREACH(fset->lvlArr, lvl) {
STFileObj *fobj;
TARRAY2_FOREACH(lvl->fobjArr, fobj) {
code = tsdbFSAddEntryToFileObjHash(hash, fobj->fname);
2024-07-18 09:17:02 +00:00
TSDB_CHECK_CODE(code, lino, _exit);
2023-09-01 05:24:47 +00:00
}
}
}
_exit:
if (code) {
2024-07-18 09:17:02 +00:00
TSDB_ERROR_LOG(TD_VID(fs->tsdb->pVnode), lino, code);
2023-09-01 05:24:47 +00:00
tsdbFSDestroyFileObjHash(hash);
}
return code;
}
static const STFileHashEntry *tsdbFSGetFileObjHashEntry(STFileHash *hash, const char *fname) {
uint32_t idx = MurmurHash3_32(fname, strlen(fname)) % hash->numBucket;
STFileHashEntry *entry = hash->buckets[idx];
while (entry) {
if (strcmp(entry->fname, fname) == 0) {
return entry;
}
entry = entry->next;
}
return NULL;
}
static void tsdbFSDestroyFileObjHash(STFileHash *hash) {
for (int32_t i = 0; i < hash->numBucket; i++) {
STFileHashEntry *entry = hash->buckets[i];
while (entry) {
STFileHashEntry *next = entry->next;
taosMemoryFree(entry);
entry = next;
}
}
taosMemoryFree(hash->buckets);
memset(hash, 0, sizeof(*hash));
}
static int32_t tsdbFSDoSanAndFix(STFileSystem *fs) {
int32_t code = 0;
int32_t lino = 0;
int32_t corrupt = false;
2023-09-01 05:24:47 +00:00
{ // scan each file
STFileSet *fset = NULL;
TARRAY2_FOREACH(fs->fSetArr, fset) {
// data file
for (int32_t ftype = 0; ftype < TSDB_FTYPE_MAX; ftype++) {
if (fset->farr[ftype] == NULL) continue;
STFileObj *fobj = fset->farr[ftype];
code = tsdbFSDoScanAndFixFile(fs, fobj);
if (code) {
fset->maxVerValid = (fobj->f->minVer <= fobj->f->maxVer) ? TMIN(fset->maxVerValid, fobj->f->minVer - 1) : -1;
corrupt = true;
}
2023-09-01 05:24:47 +00:00
}
// stt file
SSttLvl *lvl;
TARRAY2_FOREACH(fset->lvlArr, lvl) {
STFileObj *fobj;
TARRAY2_FOREACH(lvl->fobjArr, fobj) {
code = tsdbFSDoScanAndFixFile(fs, fobj);
if (code) {
2023-10-27 09:48:19 +00:00
fset->maxVerValid =
(fobj->f->minVer <= fobj->f->maxVer) ? TMIN(fset->maxVerValid, fobj->f->minVer - 1) : -1;
corrupt = true;
}
2023-09-01 05:24:47 +00:00
}
}
}
}
if (corrupt) {
2023-09-27 09:25:56 +00:00
tsdbError("vgId:%d, not to clear dangling files due to fset incompleteness", TD_VID(fs->tsdb->pVnode));
fs->fsstate = TSDB_FS_STATE_INCOMPLETE;
2023-09-20 07:38:34 +00:00
code = 0;
goto _exit;
}
2023-09-01 05:24:47 +00:00
{ // clear unreferenced files
2024-07-21 03:43:39 +00:00
STfsDir *dir = NULL;
2024-07-25 01:31:35 +00:00
TAOS_CHECK_GOTO(tfsOpendir(fs->tsdb->pVnode->pTfs, fs->tsdb->path, &dir), &lino, _exit);
2023-09-01 05:24:47 +00:00
STFileHash fobjHash = {0};
code = tsdbFSCreateFileObjHash(fs, &fobjHash);
if (code) goto _close_dir;
for (const STfsFile *file = NULL; (file = tfsReaddir(dir)) != NULL;) {
if (taosIsDir(file->aname)) continue;
2023-12-11 11:36:06 +00:00
if (tsdbFSGetFileObjHashEntry(&fobjHash, file->aname) == NULL &&
strncmp(file->aname + strlen(file->aname) - 3, ".cp", 3) &&
strncmp(file->aname + strlen(file->aname) - 5, ".data", 5)) {
remove_file(file->aname);
2023-09-01 05:24:47 +00:00
}
}
tsdbFSDestroyFileObjHash(&fobjHash);
_close_dir:
tfsClosedir(dir);
}
_exit:
if (code) {
TSDB_ERROR_LOG(TD_VID(fs->tsdb->pVnode), lino, code);
}
return code;
}
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-09-01 05:24:47 +00:00
// scan and fix
int32_t code = 0;
int32_t lino = 0;
code = tsdbFSDoSanAndFix(fs);
TSDB_CHECK_CODE(code, lino, _exit);
_exit:
if (code) {
TSDB_ERROR_LOG(TD_VID(fs->tsdb->pVnode), lino, code);
}
return code;
2023-04-07 07:12:31 +00:00
}
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-10-27 09:48:19 +00:00
code = tsdbTFileSetInitCopy(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-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-09-01 05:24:47 +00:00
return code;
2023-03-27 09:07:59 +00:00
}
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-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 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) {
2023-06-21 05:39:29 +00:00
int32_t code = 0;
int32_t lino = 0;
code = tsdbFSDupState(fs);
if (code) return code;
2023-05-19 09:20:53 +00:00
2023-06-21 05:39:29 +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-06-27 06:33:55 +00:00
STFileSet **fsetPtr = TARRAY2_SEARCH(fsetArray, &fset, tsdbTFileSetCmprFn, TD_EQ);
fset = (fsetPtr == NULL) ? NULL : *fsetPtr;
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-10-27 09:48:19 +00:00
// remove empty empty stt level and empty file set
2023-05-23 07:07:42 +00:00
int32_t i = 0;
while (i < TARRAY2_SIZE(fsetArray)) {
fset = TARRAY2_GET(fsetArray, i);
2023-10-27 09:48:19 +00:00
SSttLvl *lvl;
int32_t j = 0;
while (j < TARRAY2_SIZE(fset->lvlArr)) {
lvl = TARRAY2_GET(fset->lvlArr, j);
if (TARRAY2_SIZE(lvl->fobjArr) == 0) {
TARRAY2_REMOVE(fset->lvlArr, j, tsdbSttLvlClear);
} else {
j++;
}
}
2023-05-23 07:07:42 +00:00
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-06-29 06:03:01 +00:00
if (code) {
TSDB_ERROR_LOG(TD_VID(fs->tsdb->pVnode), lino, code);
}
2023-05-19 09:20:53 +00:00
return code;
2023-04-06 08:26:41 +00:00
}
2024-06-27 04:38:17 +00:00
// return error code
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-06-21 06:53:36 +00:00
code = tsdbCheckAndUpgradeFileSystem(pTsdb, rollback);
TSDB_CHECK_CODE(code, lino, _exit);
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
}
2023-09-01 05:24:47 +00:00
return code;
2023-03-27 09:07:59 +00:00
}
2023-12-04 05:40:28 +00:00
static int32_t tsdbFSSetBlockCommit(STFileSet *fset, bool block);
2023-12-07 09:37:47 +00:00
extern int32_t tsdbStopAllCompTask(STsdb *tsdb);
2023-12-04 05:40:28 +00:00
int32_t tsdbDisableAndCancelAllBgTask(STsdb *pTsdb) {
STFileSystem *fs = pTsdb->pFS;
2024-05-23 09:57:07 +00:00
SArray *channelArray = taosArrayInit(0, sizeof(SVAChannelID));
if (channelArray == NULL) {
return TSDB_CODE_OUT_OF_MEMORY;
}
2023-06-26 08:28:33 +00:00
2024-05-23 09:57:07 +00:00
taosThreadMutexLock(&pTsdb->mutex);
2023-12-04 05:40:28 +00:00
// disable
pTsdb->bgTaskDisabled = true;
// collect channel
2023-11-29 10:01:31 +00:00
STFileSet *fset;
TARRAY2_FOREACH(fs->fSetArr, fset) {
2024-05-23 09:57:07 +00:00
if (fset->channelOpened) {
taosArrayPush(channelArray, &fset->channel);
fset->channel = (SVAChannelID){0};
fset->mergeScheduled = false;
tsdbFSSetBlockCommit(fset, false);
fset->channelOpened = false;
2023-08-01 03:21:40 +00:00
}
2023-06-26 08:28:33 +00:00
}
2023-12-04 05:40:28 +00:00
2024-05-23 09:57:07 +00:00
taosThreadMutexUnlock(&pTsdb->mutex);
2023-06-26 08:28:33 +00:00
2023-11-29 10:01:31 +00:00
// destroy all channels
2024-05-23 09:57:07 +00:00
for (int32_t i = 0; i < taosArrayGetSize(channelArray); i++) {
SVAChannelID *channel = taosArrayGet(channelArray, i);
vnodeAChannelDestroy(channel, true);
}
taosArrayDestroy(channelArray);
2023-12-07 09:37:47 +00:00
#ifdef TD_ENTERPRISE
tsdbStopAllCompTask(pTsdb);
#endif
2023-12-04 05:40:28 +00:00
return 0;
}
int32_t tsdbEnableBgTask(STsdb *pTsdb) {
taosThreadMutexLock(&pTsdb->mutex);
pTsdb->bgTaskDisabled = false;
taosThreadMutexUnlock(&pTsdb->mutex);
2023-11-29 10:01:31 +00:00
return 0;
2023-06-26 08:28:33 +00:00
}
int32_t tsdbCloseFS(STFileSystem **fs) {
if (fs[0] == NULL) return 0;
2023-12-04 05:40:28 +00:00
tsdbDisableAndCancelAllBgTask((*fs)->tsdb);
2023-06-26 08:28:33 +00:00
close_file_system(fs[0]);
destroy_fs(fs);
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) {
2023-10-27 09:48:19 +00:00
taosThreadMutexLock(&fs->tsdb->mutex);
2023-06-01 07:08:45 +00:00
int64_t cid = ++fs->neid;
2023-10-27 09:48:19 +00:00
taosThreadMutexUnlock(&fs->tsdb->mutex);
2023-06-01 07:08:45 +00:00
return cid;
2023-05-11 10:16:55 +00:00
}
void tsdbFSUpdateEid(STFileSystem *fs, int64_t cid) {
taosThreadMutexLock(&fs->tsdb->mutex);
fs->neid = TMAX(fs->neid, cid);
taosThreadMutexUnlock(&fs->tsdb->mutex);
}
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
2024-05-23 09:57:07 +00:00
if (etype == TSDB_FEDIT_COMMIT) {
current_fname(fs->tsdb, current_t, TSDB_FCURRENT_C);
} else {
current_fname(fs->tsdb, current_t, TSDB_FCURRENT_M);
2023-05-09 08:46:23 +00:00
}
tsem_wait(&fs->canEdit);
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-10-27 09:48:19 +00:00
static int32_t tsdbFSSetBlockCommit(STFileSet *fset, bool block) {
if (block) {
2023-10-27 09:48:19 +00:00
fset->blockCommit = true;
} else {
2023-10-27 09:48:19 +00:00
fset->blockCommit = false;
if (fset->numWaitCommit > 0) {
taosThreadCondSignal(&fset->canCommit);
}
}
return 0;
}
2023-10-27 09:48:19 +00:00
int32_t tsdbFSCheckCommit(STsdb *tsdb, int32_t fid) {
taosThreadMutexLock(&tsdb->mutex);
STFileSet *fset;
tsdbFSGetFSet(tsdb->pFS, fid, &fset);
if (fset) {
while (fset->blockCommit) {
fset->numWaitCommit++;
taosThreadCondWait(&fset->canCommit, &tsdb->mutex);
fset->numWaitCommit--;
}
}
2023-10-27 09:48:19 +00:00
taosThreadMutexUnlock(&tsdb->mutex);
return 0;
}
2023-10-27 09:48:19 +00:00
// IMPORTANT: the caller must hold fs->tsdb->mutex
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);
ASSERT(code == 0);
2023-06-05 01:36:26 +00:00
TSDB_CHECK_CODE(code, lino, _exit);
2023-06-20 10:23:58 +00:00
// schedule merge
2023-10-27 09:48:19 +00:00
int32_t sttTrigger = fs->tsdb->pVnode->config.sttTrigger;
2023-12-04 05:40:28 +00:00
if (sttTrigger > 1 && !fs->tsdb->bgTaskDisabled) {
2023-06-05 01:36:26 +00:00
STFileSet *fset;
TARRAY2_FOREACH_REVERSE(fs->fSetArr, fset) {
2023-10-27 09:48:19 +00:00
if (TARRAY2_SIZE(fset->lvlArr) == 0) {
tsdbFSSetBlockCommit(fset, false);
continue;
}
2023-06-05 01:36:26 +00:00
2023-06-20 10:23:58 +00:00
SSttLvl *lvl = TARRAY2_FIRST(fset->lvlArr);
2023-10-27 09:48:19 +00:00
if (lvl->level != 0) {
tsdbFSSetBlockCommit(fset, false);
continue;
}
// bool skipMerge = false;
int32_t numFile = TARRAY2_SIZE(lvl->fobjArr);
2023-12-04 05:40:28 +00:00
if (numFile >= sttTrigger && (!fset->mergeScheduled)) {
code = tsdbTFileSetOpenChannel(fset);
TSDB_CHECK_CODE(code, lino, _exit);
SMergeArg *arg = taosMemoryMalloc(sizeof(*arg));
if (arg == NULL) {
code = TSDB_CODE_OUT_OF_MEMORY;
2023-11-29 10:01:31 +00:00
TSDB_CHECK_CODE(code, lino, _exit);
}
2023-11-29 10:01:31 +00:00
arg->tsdb = fs->tsdb;
arg->fid = fset->fid;
2023-11-29 10:01:31 +00:00
2024-05-23 09:57:07 +00:00
code = vnodeAsync(&fset->channel, EVA_PRIORITY_HIGH, tsdbMerge, taosMemoryFree, arg, NULL);
TSDB_CHECK_CODE(code, lino, _exit);
fset->mergeScheduled = true;
}
if (numFile >= sttTrigger * BLOCK_COMMIT_FACTOR) {
2023-10-27 09:48:19 +00:00
tsdbFSSetBlockCommit(fset, true);
} else {
tsdbFSSetBlockCommit(fset, false);
}
}
2023-10-27 09:48:19 +00:00
}
2023-06-05 01:36:26 +00:00
_exit:
if (code) {
tsdbError("vgId:%d %s failed at line %d since %s", TD_VID(fs->tsdb->pVnode), __func__, lino, tstrerror(code));
2023-06-05 01:36:26 +00:00
} else {
tsdbInfo("vgId:%d %s done, etype:%d", TD_VID(fs->tsdb->pVnode), __func__, fs->etype);
2023-06-05 01:36:26 +00:00
}
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) {
2023-06-27 06:33:55 +00:00
STFileSet tfset = {.fid = fid};
STFileSet *pset = &tfset;
STFileSet **fsetPtr = TARRAY2_SEARCH(fs->fSetArr, &pset, tsdbTFileSetCmprFn, TD_EQ);
fset[0] = (fsetPtr == NULL) ? NULL : fsetPtr[0];
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));
2023-09-05 03:41:36 +00:00
if (fsetArr[0] == NULL) return TSDB_CODE_OUT_OF_MEMORY;
2023-05-31 07:57:51 +00:00
TARRAY2_INIT(fsetArr[0]);
2023-05-31 02:59:47 +00:00
2023-10-27 09:48:19 +00:00
taosThreadMutexLock(&fs->tsdb->mutex);
2023-05-31 07:57:51 +00:00
TARRAY2_FOREACH(fs->fSetArr, fset) {
2023-10-27 09:48:19 +00:00
code = tsdbTFileSetInitCopy(fs->tsdb, fset, &fset1);
2023-05-31 02:59:47 +00:00
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;
}
2023-10-27 09:48:19 +00:00
taosThreadMutexUnlock(&fs->tsdb->mutex);
2023-05-31 02:59:47 +00:00
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-06-20 12:10:57 +00:00
taosMemoryFree(fsetArr[0]);
2023-05-31 07:57:51 +00:00
fsetArr[0] = NULL;
}
2023-05-31 02:59:47 +00:00
return 0;
2023-06-15 06:10:35 +00:00
}
int32_t tsdbFSCreateRefSnapshot(STFileSystem *fs, TFileSetArray **fsetArr) {
2023-10-27 09:48:19 +00:00
taosThreadMutexLock(&fs->tsdb->mutex);
int32_t code = tsdbFSCreateRefSnapshotWithoutLock(fs, fsetArr);
2023-10-27 09:48:19 +00:00
taosThreadMutexUnlock(&fs->tsdb->mutex);
return code;
}
int32_t tsdbFSCreateRefSnapshotWithoutLock(STFileSystem *fs, TFileSetArray **fsetArr) {
2023-06-15 06:10:35 +00:00
int32_t code = 0;
STFileSet *fset, *fset1;
fsetArr[0] = taosMemoryCalloc(1, sizeof(*fsetArr[0]));
2023-06-15 06:10:35 +00:00
if (fsetArr[0] == NULL) return TSDB_CODE_OUT_OF_MEMORY;
TARRAY2_FOREACH(fs->fSetArr, fset) {
2023-06-15 06:43:14 +00:00
code = tsdbTFileSetInitRef(fs->tsdb, fset, &fset1);
if (code) break;
2023-06-15 06:10:35 +00:00
code = TARRAY2_APPEND(fsetArr[0], fset1);
if (code) break;
}
if (code) {
2023-06-15 06:43:14 +00:00
TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
2023-06-15 06:10:35 +00:00
fsetArr[0] = NULL;
}
return code;
}
int32_t tsdbFSDestroyRefSnapshot(TFileSetArray **fsetArr) {
if (fsetArr[0]) {
2023-06-15 06:43:14 +00:00
TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
2023-06-20 06:46:32 +00:00
taosMemoryFreeClear(fsetArr[0]);
2023-06-15 06:10:35 +00:00
fsetArr[0] = NULL;
}
return 0;
2023-06-20 10:23:58 +00:00
}
static SHashObj *tsdbFSetRangeArrayToHash(TFileSetRangeArray *pRanges) {
int32_t capacity = TARRAY2_SIZE(pRanges) * 2;
SHashObj *pHash = taosHashInit(capacity, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
if (pHash == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
return NULL;
}
for (int32_t i = 0; i < TARRAY2_SIZE(pRanges); i++) {
STFileSetRange *u = TARRAY2_GET(pRanges, i);
int32_t fid = u->fid;
int32_t code = taosHashPut(pHash, &fid, sizeof(fid), u, sizeof(*u));
ASSERT(code == 0);
tsdbDebug("range diff hash fid:%d, sver:%" PRId64 ", ever:%" PRId64, u->fid, u->sver, u->ever);
}
return pHash;
}
int32_t tsdbFSCreateCopyRangedSnapshot(STFileSystem *fs, TFileSetRangeArray *pRanges, TFileSetArray **fsetArr,
TFileOpArray *fopArr) {
int32_t code = 0;
STFileSet *fset;
STFileSet *fset1;
SHashObj *pHash = NULL;
fsetArr[0] = taosMemoryMalloc(sizeof(TFileSetArray));
if (fsetArr == NULL) return TSDB_CODE_OUT_OF_MEMORY;
TARRAY2_INIT(fsetArr[0]);
if (pRanges) {
pHash = tsdbFSetRangeArrayToHash(pRanges);
if (pHash == NULL) {
code = TSDB_CODE_OUT_OF_MEMORY;
goto _out;
}
}
2023-10-27 09:48:19 +00:00
taosThreadMutexLock(&fs->tsdb->mutex);
TARRAY2_FOREACH(fs->fSetArr, fset) {
int64_t ever = VERSION_MAX;
if (pHash) {
2023-12-11 11:38:04 +00:00
int32_t fid = fset->fid;
STFileSetRange *u = taosHashGet(pHash, &fid, sizeof(fid));
if (u) {
ever = u->sver - 1;
}
}
code = tsdbTFileSetFilteredInitDup(fs->tsdb, fset, ever, &fset1, fopArr);
if (code) break;
code = TARRAY2_APPEND(fsetArr[0], fset1);
if (code) break;
}
2023-10-27 09:48:19 +00:00
taosThreadMutexUnlock(&fs->tsdb->mutex);
_out:
if (code) {
TARRAY2_DESTROY(fsetArr[0], tsdbTFileSetClear);
taosMemoryFree(fsetArr[0]);
fsetArr[0] = NULL;
}
if (pHash) {
taosHashCleanup(pHash);
pHash = NULL;
}
return code;
}
int32_t tsdbFSDestroyCopyRangedSnapshot(TFileSetArray **fsetArr) { return tsdbFSDestroyCopySnapshot(fsetArr); }
int32_t tsdbFSCreateRefRangedSnapshot(STFileSystem *fs, int64_t sver, int64_t ever, TFileSetRangeArray *pRanges,
TFileSetRangeArray **fsrArr) {
2023-12-11 11:38:04 +00:00
int32_t code = 0;
STFileSet *fset;
STFileSetRange *fsr1 = NULL;
2023-12-11 11:38:04 +00:00
SHashObj *pHash = NULL;
fsrArr[0] = taosMemoryCalloc(1, sizeof(*fsrArr[0]));
2023-09-05 07:14:43 +00:00
if (fsrArr[0] == NULL) {
code = TSDB_CODE_OUT_OF_MEMORY;
goto _out;
}
tsdbInfo("pRanges size:%d", (pRanges == NULL ? 0 : TARRAY2_SIZE(pRanges)));
if (pRanges) {
pHash = tsdbFSetRangeArrayToHash(pRanges);
if (pHash == NULL) {
code = TSDB_CODE_OUT_OF_MEMORY;
goto _out;
}
}
2023-09-11 02:54:28 +00:00
2023-10-27 09:48:19 +00:00
taosThreadMutexLock(&fs->tsdb->mutex);
TARRAY2_FOREACH(fs->fSetArr, fset) {
int64_t sver1 = sver;
int64_t ever1 = ever;
if (pHash) {
2023-12-11 11:38:04 +00:00
int32_t fid = fset->fid;
STFileSetRange *u = taosHashGet(pHash, &fid, sizeof(fid));
if (u) {
sver1 = u->sver;
tsdbDebug("range hash get fid:%d, sver:%" PRId64 ", ever:%" PRId64, u->fid, u->sver, u->ever);
}
}
if (sver1 > ever1) {
tsdbDebug("skip fid:%d, sver:%" PRId64 ", ever:%" PRId64, fset->fid, sver1, ever1);
continue;
}
tsdbDebug("fsrArr:%p, fid:%d, sver:%" PRId64 ", ever:%" PRId64, fsrArr, fset->fid, sver1, ever1);
code = tsdbTFileSetRangeInitRef(fs->tsdb, fset, sver1, ever1, &fsr1);
if (code) break;
code = TARRAY2_APPEND(fsrArr[0], fsr1);
2023-09-05 07:14:43 +00:00
if (code) break;
fsr1 = NULL;
}
2023-10-27 09:48:19 +00:00
taosThreadMutexUnlock(&fs->tsdb->mutex);
if (code) {
tsdbTFileSetRangeClear(&fsr1);
TARRAY2_DESTROY(fsrArr[0], tsdbTFileSetRangeClear);
fsrArr[0] = NULL;
}
2023-09-05 07:14:43 +00:00
_out:
if (pHash) {
taosHashCleanup(pHash);
pHash = NULL;
}
return code;
}
int32_t tsdbFSDestroyRefRangedSnapshot(TFileSetRangeArray **fsrArr) { return tsdbTFileSetRangeArrayDestroy(fsrArr); }
2024-05-23 09:57:07 +00:00
int32_t tsdbBeginTaskOnFileSet(STsdb *tsdb, int32_t fid, STFileSet **fset) {
int16_t sttTrigger = tsdb->pVnode->config.sttTrigger;
tsdbFSGetFSet(tsdb->pFS, fid, fset);
2024-05-29 06:10:34 +00:00
if (sttTrigger == 1 && (*fset)) {
2024-05-23 09:57:07 +00:00
for (;;) {
if ((*fset)->taskRunning) {
(*fset)->numWaitTask++;
taosThreadCondWait(&(*fset)->beginTask, &tsdb->mutex);
tsdbFSGetFSet(tsdb->pFS, fid, fset);
ASSERT(fset != NULL);
(*fset)->numWaitTask--;
ASSERT((*fset)->numWaitTask >= 0);
} else {
(*fset)->taskRunning = true;
break;
}
}
tsdbInfo("vgId:%d begin task on file set:%d", TD_VID(tsdb->pVnode), fid);
}
return 0;
}
int32_t tsdbFinishTaskOnFileSet(STsdb *tsdb, int32_t fid) {
int16_t sttTrigger = tsdb->pVnode->config.sttTrigger;
if (sttTrigger == 1) {
STFileSet *fset = NULL;
tsdbFSGetFSet(tsdb->pFS, fid, &fset);
if (fset != NULL && fset->taskRunning) {
fset->taskRunning = false;
if (fset->numWaitTask > 0) {
taosThreadCondSignal(&fset->beginTask);
}
tsdbInfo("vgId:%d finish task on file set:%d", TD_VID(tsdb->pVnode), fid);
}
}
return 0;
}