TDengine/source/dnode/mgmt/node_util/src/dmFile.c

252 lines
7.2 KiB
C
Raw Normal View History

2022-03-17 02:48:39 +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/>.
*/
#define _DEFAULT_SOURCE
2022-05-11 13:19:33 +00:00
#include "dmUtil.h"
2022-03-17 02:48:39 +00:00
2022-03-25 03:44:40 +00:00
#define MAXLEN 1024
2022-05-11 09:09:14 +00:00
int32_t dmReadFile(const char *path, const char *name, bool *pDeployed) {
int32_t code = TSDB_CODE_INVALID_JSON_FORMAT;
2022-03-30 06:06:36 +00:00
int64_t len = 0;
char content[MAXLEN + 1] = {0};
cJSON *root = NULL;
2022-04-12 09:28:56 +00:00
char file[PATH_MAX] = {0};
2022-03-30 06:06:36 +00:00
TdFilePtr pFile = NULL;
2022-03-17 02:48:39 +00:00
2022-05-11 09:09:14 +00:00
snprintf(file, sizeof(file), "%s%s%s.json", path, TD_DIRSEP, name);
2022-03-17 02:48:39 +00:00
pFile = taosOpenFile(file, TD_FILE_READ);
if (pFile == NULL) {
code = 0;
2022-03-18 01:54:30 +00:00
goto _OVER;
2022-03-17 02:48:39 +00:00
}
2022-03-30 06:06:36 +00:00
len = taosReadFile(pFile, content, MAXLEN);
2022-03-17 02:48:39 +00:00
if (len <= 0) {
dError("failed to read %s since content is null", file);
2022-03-18 01:54:30 +00:00
goto _OVER;
2022-03-17 02:48:39 +00:00
}
root = cJSON_Parse(content);
if (root == NULL) {
dError("failed to read %s since invalid json format", file);
2022-03-18 01:54:30 +00:00
goto _OVER;
2022-03-17 02:48:39 +00:00
}
cJSON *deployed = cJSON_GetObjectItem(root, "deployed");
if (!deployed || deployed->type != cJSON_Number) {
dError("failed to read %s since deployed not found", file);
2022-03-18 01:54:30 +00:00
goto _OVER;
2022-03-17 02:48:39 +00:00
}
2022-03-17 10:18:02 +00:00
*pDeployed = deployed->valueint != 0;
2022-03-17 02:48:39 +00:00
2022-03-17 10:18:02 +00:00
dDebug("succcessed to read file %s, deployed:%d", file, *pDeployed);
2022-03-30 06:06:36 +00:00
code = 0;
2022-03-17 02:48:39 +00:00
2022-03-18 01:54:30 +00:00
_OVER:
2022-03-17 02:48:39 +00:00
if (root != NULL) cJSON_Delete(root);
if (pFile != NULL) taosCloseFile(&pFile);
terrno = code;
return code;
}
2022-05-11 09:09:14 +00:00
int32_t dmWriteFile(const char *path, const char *name, bool deployed) {
2022-03-30 06:06:36 +00:00
int32_t code = -1;
int32_t len = 0;
char content[MAXLEN + 1] = {0};
char file[PATH_MAX] = {0};
char realfile[PATH_MAX] = {0};
TdFilePtr pFile = NULL;
2022-05-11 09:09:14 +00:00
snprintf(file, sizeof(file), "%s%s%s.json", path, TD_DIRSEP, name);
snprintf(realfile, sizeof(realfile), "%s%s%s.json", path, TD_DIRSEP, name);
2022-03-17 02:48:39 +00:00
2022-04-11 10:55:43 +00:00
pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
2022-03-17 02:48:39 +00:00
if (pFile == NULL) {
2022-03-18 03:23:50 +00:00
terrno = TAOS_SYSTEM_ERROR(errno);
2022-03-17 02:48:39 +00:00
dError("failed to write %s since %s", file, terrstr());
2022-03-30 06:06:36 +00:00
goto _OVER;
2022-03-17 02:48:39 +00:00
}
2022-03-30 06:06:36 +00:00
len += snprintf(content + len, MAXLEN - len, "{\n");
len += snprintf(content + len, MAXLEN - len, " \"deployed\": %d\n", deployed);
len += snprintf(content + len, MAXLEN - len, "}\n");
2022-03-17 02:48:39 +00:00
2022-03-30 06:06:36 +00:00
if (taosWriteFile(pFile, content, len) != len) {
terrno = TAOS_SYSTEM_ERROR(errno);
dError("failed to write file:%s since %s", file, terrstr());
goto _OVER;
}
2022-03-17 02:48:39 +00:00
2022-03-30 06:06:36 +00:00
if (taosFsyncFile(pFile) != 0) {
terrno = TAOS_SYSTEM_ERROR(errno);
dError("failed to fsync file:%s since %s", file, terrstr());
goto _OVER;
}
2022-03-17 02:48:39 +00:00
2022-03-30 06:06:36 +00:00
taosCloseFile(&pFile);
2022-03-17 02:48:39 +00:00
if (taosRenameFile(file, realfile) != 0) {
2022-03-18 03:23:50 +00:00
terrno = TAOS_SYSTEM_ERROR(errno);
2022-03-17 02:48:39 +00:00
dError("failed to rename %s since %s", file, terrstr());
return -1;
}
2022-03-17 10:18:02 +00:00
dInfo("successed to write %s, deployed:%d", realfile, deployed);
2022-03-30 06:06:36 +00:00
code = 0;
_OVER:
if (pFile != NULL) {
taosCloseFile(&pFile);
}
return code;
}
2022-04-13 06:00:56 +00:00
TdFilePtr dmCheckRunning(const char *dataDir) {
2022-03-30 08:52:14 +00:00
char filepath[PATH_MAX] = {0};
snprintf(filepath, sizeof(filepath), "%s%s.running", dataDir, TD_DIRSEP);
2022-04-11 10:55:43 +00:00
TdFilePtr pFile = taosOpenFile(filepath, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
2022-03-30 08:52:14 +00:00
if (pFile == NULL) {
terrno = TAOS_SYSTEM_ERROR(errno);
2022-04-25 05:57:55 +00:00
dError("failed to open file:%s since %s", filepath, terrstr());
2022-03-30 08:52:14 +00:00
return NULL;
}
int32_t ret = taosLockFile(pFile);
if (ret != 0) {
terrno = TAOS_SYSTEM_ERROR(errno);
dError("failed to lock file:%s since %s", filepath, terrstr());
taosCloseFile(&pFile);
return NULL;
}
2022-06-02 05:57:39 +00:00
dDebug("lock file:%s to prevent repeated starts", filepath);
2022-03-30 08:52:14 +00:00
return pFile;
}
2022-05-13 01:44:46 +00:00
int32_t dmReadShmFile(const char *path, const char *name, EDndNodeType runType, SShm *pShm) {
2022-03-30 06:06:36 +00:00
int32_t code = -1;
char content[MAXLEN + 1] = {0};
char file[PATH_MAX] = {0};
cJSON *root = NULL;
TdFilePtr pFile = NULL;
2022-05-11 09:09:14 +00:00
snprintf(file, sizeof(file), "%s%sshmfile", path, TD_DIRSEP);
2022-03-30 08:52:14 +00:00
pFile = taosOpenFile(file, TD_FILE_READ);
2022-03-30 06:06:36 +00:00
if (pFile == NULL) {
2022-03-30 08:52:14 +00:00
code = 0;
2022-03-30 06:06:36 +00:00
goto _OVER;
}
if (taosReadFile(pFile, content, MAXLEN) > 0) {
root = cJSON_Parse(content);
if (root == NULL) {
terrno = TSDB_CODE_INVALID_JSON_FORMAT;
2022-05-11 09:09:14 +00:00
dError("node:%s, failed to read %s since invalid json format", name, file);
2022-03-30 06:06:36 +00:00
goto _OVER;
}
2022-04-13 07:52:14 +00:00
cJSON *shmid = cJSON_GetObjectItem(root, "shmid");
if (shmid && shmid->type == cJSON_Number) {
2022-05-11 09:09:14 +00:00
pShm->id = shmid->valueint;
2022-04-13 07:52:14 +00:00
}
cJSON *shmsize = cJSON_GetObjectItem(root, "shmsize");
if (shmsize && shmsize->type == cJSON_Number) {
2022-05-11 09:09:14 +00:00
pShm->size = shmsize->valueint;
2022-03-30 06:06:36 +00:00
}
}
2022-05-13 01:44:46 +00:00
if (!tsMultiProcess || runType == DNODE || runType == NODE_END) {
2022-05-11 09:09:14 +00:00
if (pShm->id >= 0) {
dDebug("node:%s, shmid:%d, is closed, size:%d", name, pShm->id, pShm->size);
taosDropShm(pShm);
2022-03-30 06:06:36 +00:00
}
} else {
2022-05-11 09:09:14 +00:00
if (taosAttachShm(pShm) != 0) {
2022-03-30 06:06:36 +00:00
terrno = TAOS_SYSTEM_ERROR(errno);
2022-05-11 09:09:14 +00:00
dError("shmid:%d, failed to attach shm since %s", pShm->id, terrstr());
2022-03-30 06:06:36 +00:00
goto _OVER;
}
2022-05-11 09:09:14 +00:00
dInfo("node:%s, shmid:%d is attached, size:%d", name, pShm->id, pShm->size);
2022-03-30 06:06:36 +00:00
}
2022-05-11 09:09:14 +00:00
dDebug("node:%s, successed to load %s", name, file);
2022-03-30 06:06:36 +00:00
code = 0;
_OVER:
if (root != NULL) cJSON_Delete(root);
2022-03-30 08:52:14 +00:00
if (pFile != NULL) taosCloseFile(&pFile);
2022-03-30 06:06:36 +00:00
return code;
}
2022-05-11 09:09:14 +00:00
int32_t dmWriteShmFile(const char *path, const char *name, const SShm *pShm) {
2022-03-30 06:06:36 +00:00
int32_t code = -1;
int32_t len = 0;
char content[MAXLEN + 1] = {0};
char file[PATH_MAX] = {0};
char realfile[PATH_MAX] = {0};
TdFilePtr pFile = NULL;
2022-05-11 09:09:14 +00:00
snprintf(file, sizeof(file), "%s%sshmfile.bak", path, TD_DIRSEP);
snprintf(realfile, sizeof(realfile), "%s%sshmfile", path, TD_DIRSEP);
2022-03-30 06:06:36 +00:00
2022-04-11 10:55:43 +00:00
pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
2022-03-30 06:06:36 +00:00
if (pFile == NULL) {
terrno = TAOS_SYSTEM_ERROR(errno);
2022-05-11 09:09:14 +00:00
dError("node:%s, failed to open file:%s since %s", name, file, terrstr());
2022-03-30 06:06:36 +00:00
goto _OVER;
}
len += snprintf(content + len, MAXLEN - len, "{\n");
2022-05-11 09:09:14 +00:00
len += snprintf(content + len, MAXLEN - len, " \"shmid\":%d,\n", pShm->id);
len += snprintf(content + len, MAXLEN - len, " \"shmsize\":%d\n", pShm->size);
2022-03-30 06:06:36 +00:00
len += snprintf(content + len, MAXLEN - len, "}\n");
if (taosWriteFile(pFile, content, len) != len) {
terrno = TAOS_SYSTEM_ERROR(errno);
2022-05-11 09:09:14 +00:00
dError("node:%s, failed to write file:%s since %s", name, file, terrstr());
2022-03-30 06:06:36 +00:00
goto _OVER;
}
if (taosFsyncFile(pFile) != 0) {
terrno = TAOS_SYSTEM_ERROR(errno);
2022-05-11 09:09:14 +00:00
dError("node:%s, failed to fsync file:%s since %s", name, file, terrstr());
2022-03-30 06:06:36 +00:00
goto _OVER;
}
taosCloseFile(&pFile);
if (taosRenameFile(file, realfile) != 0) {
terrno = TAOS_SYSTEM_ERROR(errno);
2022-05-11 09:09:14 +00:00
dError("node:%s, failed to rename %s to %s since %s", name, file, realfile, terrstr());
2022-03-30 06:06:36 +00:00
return -1;
}
2022-05-11 09:09:14 +00:00
dInfo("node:%s, successed to write %s", name, realfile);
2022-03-30 06:06:36 +00:00
code = 0;
_OVER:
if (pFile != NULL) {
taosCloseFile(&pFile);
}
return code;
2022-03-17 02:48:39 +00:00
}