TDengine/source/dnode/mgmt/mgmt_vnode/src/vmFile.c

218 lines
6.6 KiB
C
Raw Normal View History

2022-03-16 02:48:22 +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-03-16 11:39:43 +00:00
#include "vmInt.h"
2022-03-16 02:48:22 +00:00
2022-06-02 09:43:03 +00:00
#define MAX_CONTENT_LEN 1024 * 1024
2022-05-11 10:23:08 +00:00
SVnodeObj **vmGetVnodeListFromHash(SVnodeMgmt *pMgmt, int32_t *numOfVnodes) {
2022-05-19 07:08:32 +00:00
taosThreadRwlockRdlock(&pMgmt->lock);
2022-03-16 02:48:22 +00:00
int32_t num = 0;
int32_t size = taosHashGetSize(pMgmt->hash);
2022-03-25 16:29:53 +00:00
SVnodeObj **pVnodes = taosMemoryCalloc(size, sizeof(SVnodeObj *));
2022-03-16 02:48:22 +00:00
void *pIter = taosHashIterate(pMgmt->hash, NULL);
while (pIter) {
SVnodeObj **ppVnode = pIter;
SVnodeObj *pVnode = *ppVnode;
if (pVnode && num < size) {
int32_t refCount = atomic_add_fetch_32(&pVnode->refCount, 1);
// dTrace("vgId:%d, acquire vnode list, ref:%d", pVnode->vgId, refCount);
2022-07-05 09:38:21 +00:00
pVnodes[num++] = (*ppVnode);
2022-03-16 02:48:22 +00:00
pIter = taosHashIterate(pMgmt->hash, pIter);
} else {
taosHashCancelIterate(pMgmt->hash, pIter);
}
}
2022-05-19 07:08:32 +00:00
taosThreadRwlockUnlock(&pMgmt->lock);
2022-03-16 02:48:22 +00:00
*numOfVnodes = num;
return pVnodes;
}
2022-05-11 10:23:08 +00:00
int32_t vmGetVnodeListFromFile(SVnodeMgmt *pMgmt, SWrapperCfg **ppCfgs, int32_t *numOfVnodes) {
int32_t code = TSDB_CODE_INVALID_JSON_FORMAT;
2022-03-16 02:48:22 +00:00
int32_t len = 0;
2022-06-02 09:43:03 +00:00
int32_t maxLen = MAX_CONTENT_LEN;
2022-03-25 16:29:53 +00:00
char *content = taosMemoryCalloc(1, maxLen + 1);
2022-03-16 02:48:22 +00:00
cJSON *root = NULL;
FILE *fp = NULL;
2022-05-08 14:20:53 +00:00
char file[PATH_MAX] = {0};
2022-03-16 02:48:22 +00:00
SWrapperCfg *pCfgs = NULL;
TdFilePtr pFile = NULL;
snprintf(file, sizeof(file), "%s%svnodes.json", pMgmt->path, TD_DIRSEP);
pFile = taosOpenFile(file, TD_FILE_READ);
if (pFile == NULL) {
dDebug("file %s not exist", file);
code = 0;
2022-05-08 14:20:53 +00:00
goto _OVER;
2022-03-16 02:48:22 +00:00
}
if (content == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
return -1;
}
2022-03-16 02:48:22 +00:00
len = (int32_t)taosReadFile(pFile, content, maxLen);
if (len <= 0) {
dError("failed to read %s since content is null", file);
2022-05-08 14:20:53 +00:00
goto _OVER;
2022-03-16 02:48:22 +00:00
}
content[len] = 0;
root = cJSON_Parse(content);
if (root == NULL) {
dError("failed to read %s since invalid json format", file);
2022-05-08 14:20:53 +00:00
goto _OVER;
2022-03-16 02:48:22 +00:00
}
cJSON *vnodes = cJSON_GetObjectItem(root, "vnodes");
if (!vnodes || vnodes->type != cJSON_Array) {
dError("failed to read %s since vnodes not found", file);
2022-05-08 14:20:53 +00:00
goto _OVER;
2022-03-16 02:48:22 +00:00
}
int32_t vnodesNum = cJSON_GetArraySize(vnodes);
if (vnodesNum > 0) {
2022-03-25 16:29:53 +00:00
pCfgs = taosMemoryCalloc(vnodesNum, sizeof(SWrapperCfg));
2022-03-16 02:48:22 +00:00
if (pCfgs == NULL) {
dError("failed to read %s since out of memory", file);
2022-10-09 09:48:06 +00:00
code = TSDB_CODE_OUT_OF_MEMORY;
2022-05-08 14:20:53 +00:00
goto _OVER;
2022-03-16 02:48:22 +00:00
}
for (int32_t i = 0; i < vnodesNum; ++i) {
cJSON *vnode = cJSON_GetArrayItem(vnodes, i);
SWrapperCfg *pCfg = &pCfgs[i];
cJSON *vgId = cJSON_GetObjectItem(vnode, "vgId");
if (!vgId || vgId->type != cJSON_Number) {
dError("failed to read %s since vgId not found", file);
2022-10-09 09:48:06 +00:00
taosMemoryFree(pCfgs);
2022-05-08 14:20:53 +00:00
goto _OVER;
2022-03-16 02:48:22 +00:00
}
pCfg->vgId = vgId->valueint;
snprintf(pCfg->path, sizeof(pCfg->path), "%s%svnode%d", pMgmt->path, TD_DIRSEP, pCfg->vgId);
cJSON *dropped = cJSON_GetObjectItem(vnode, "dropped");
if (!dropped || dropped->type != cJSON_Number) {
dError("failed to read %s since dropped not found", file);
2022-10-09 09:48:06 +00:00
taosMemoryFree(pCfgs);
2022-05-08 14:20:53 +00:00
goto _OVER;
2022-03-16 02:48:22 +00:00
}
pCfg->dropped = dropped->valueint;
cJSON *vgVersion = cJSON_GetObjectItem(vnode, "vgVersion");
if (!vgVersion || vgVersion->type != cJSON_Number) {
dError("failed to read %s since vgVersion not found", file);
2022-10-09 09:48:06 +00:00
taosMemoryFree(pCfgs);
2022-05-08 14:20:53 +00:00
goto _OVER;
2022-03-16 02:48:22 +00:00
}
pCfg->vgVersion = vgVersion->valueint;
}
*ppCfgs = pCfgs;
}
*numOfVnodes = vnodesNum;
code = 0;
2022-06-02 09:43:03 +00:00
dDebug("succcessed to read file %s, numOfVnodes:%d", file, vnodesNum);
2022-03-16 02:48:22 +00:00
2022-05-08 14:20:53 +00:00
_OVER:
2022-03-25 16:29:53 +00:00
if (content != NULL) taosMemoryFree(content);
2022-03-16 02:48:22 +00:00
if (root != NULL) cJSON_Delete(root);
if (pFile != NULL) taosCloseFile(&pFile);
terrno = code;
return code;
}
2022-05-11 10:23:08 +00:00
int32_t vmWriteVnodeListToFile(SVnodeMgmt *pMgmt) {
2022-10-10 02:51:16 +00:00
int32_t code = 0;
2022-10-09 09:48:06 +00:00
char file[PATH_MAX] = {0};
char realfile[PATH_MAX] = {0};
2022-03-16 02:48:22 +00:00
snprintf(file, sizeof(file), "%s%svnodes.json.bak", pMgmt->path, TD_DIRSEP);
snprintf(realfile, sizeof(file), "%s%svnodes.json", pMgmt->path, TD_DIRSEP);
2022-04-11 10:55:43 +00:00
TdFilePtr pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
2022-03-16 02:48:22 +00:00
if (pFile == NULL) {
terrno = TAOS_SYSTEM_ERROR(errno);
dError("failed to write %s since %s", file, terrstr());
return -1;
}
int32_t numOfVnodes = 0;
2022-10-10 02:51:16 +00:00
SVnodeObj **ppVnodes = vmGetVnodeListFromHash(pMgmt, &numOfVnodes);
if (ppVnodes == NULL) {
2022-10-08 03:29:46 +00:00
terrno = TSDB_CODE_OUT_OF_MEMORY;
2022-10-10 02:51:16 +00:00
code = -1;
2022-10-08 03:29:46 +00:00
goto _OVER;
}
2022-03-16 02:48:22 +00:00
int32_t len = 0;
2022-06-02 09:43:03 +00:00
int32_t maxLen = MAX_CONTENT_LEN;
2022-03-25 16:29:53 +00:00
char *content = taosMemoryCalloc(1, maxLen + 1);
2022-09-27 09:16:12 +00:00
if (content == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
2022-10-10 02:51:16 +00:00
code = -1;
2022-09-27 08:02:40 +00:00
goto _OVER;
}
2022-03-16 02:48:22 +00:00
len += snprintf(content + len, maxLen - len, "{\n");
len += snprintf(content + len, maxLen - len, " \"vnodes\": [\n");
for (int32_t i = 0; i < numOfVnodes; ++i) {
2022-10-10 02:51:16 +00:00
SVnodeObj *pVnode = ppVnodes[i];
2022-09-27 08:02:40 +00:00
if (pVnode == NULL) continue;
2022-03-16 02:48:22 +00:00
len += snprintf(content + len, maxLen - len, " {\n");
len += snprintf(content + len, maxLen - len, " \"vgId\": %d,\n", pVnode->vgId);
len += snprintf(content + len, maxLen - len, " \"dropped\": %d,\n", pVnode->dropped);
len += snprintf(content + len, maxLen - len, " \"vgVersion\": %d\n", pVnode->vgVersion);
2022-03-16 02:48:22 +00:00
if (i < numOfVnodes - 1) {
len += snprintf(content + len, maxLen - len, " },\n");
} else {
len += snprintf(content + len, maxLen - len, " }\n");
}
}
len += snprintf(content + len, maxLen - len, " ]\n");
len += snprintf(content + len, maxLen - len, "}\n");
2022-09-27 08:02:40 +00:00
terrno = 0;
_OVER:
2022-03-16 02:48:22 +00:00
taosWriteFile(pFile, content, len);
taosFsyncFile(pFile);
taosCloseFile(&pFile);
2022-03-25 16:29:53 +00:00
taosMemoryFree(content);
2022-03-16 02:48:22 +00:00
2022-10-10 02:51:16 +00:00
if (ppVnodes != NULL) {
2022-10-11 01:23:47 +00:00
for (int32_t i = 0; i < numOfVnodes; ++i) {
SVnodeObj *pVnode = ppVnodes[i];
if (pVnode != NULL) {
vmReleaseVnode(pMgmt, pVnode);
}
}
2022-10-10 02:51:16 +00:00
taosMemoryFree(ppVnodes);
2022-03-16 02:48:22 +00:00
}
2022-10-10 02:51:16 +00:00
if (code != 0) return -1;
2022-09-27 08:02:40 +00:00
2022-06-02 09:43:03 +00:00
dDebug("successed to write %s, numOfVnodes:%d", realfile, numOfVnodes);
2022-03-16 02:48:22 +00:00
return taosRenameFile(file, realfile);
}