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

236 lines
7.2 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
#include "tjson.h"
2023-01-11 02:44:07 +00:00
#include "vmInt.h"
2022-03-16 02:48:22 +00:00
#define MAX_CONTENT_LEN 2 * 1024 * 1024
2022-06-02 09:43:03 +00:00
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;
}
2023-01-11 02:44:07 +00:00
static int32_t vmDecodeVnodeList(SJson *pJson, SVnodeMgmt *pMgmt, SWrapperCfg **ppCfgs, int32_t *numOfVnodes) {
int32_t code = -1;
SWrapperCfg *pCfgs = NULL;
2023-01-11 04:24:44 +00:00
*ppCfgs = NULL;
2023-01-11 02:44:07 +00:00
SJson *vnodes = tjsonGetObjectItem(pJson, "vnodes");
if (vnodes == NULL) return -1;
int32_t vnodesNum = cJSON_GetArraySize(vnodes);
if (vnodesNum > 0) {
pCfgs = taosMemoryCalloc(vnodesNum, sizeof(SWrapperCfg));
if (pCfgs == NULL) return -1;
}
for (int32_t i = 0; i < vnodesNum; ++i) {
SJson *vnode = tjsonGetArrayItem(vnodes, i);
if (vnode == NULL) goto _OVER;
SWrapperCfg *pCfg = &pCfgs[i];
2023-01-11 04:24:44 +00:00
tjsonGetInt32ValueFromDouble(vnode, "vgId", pCfg->vgId, code);
2023-01-11 02:44:07 +00:00
if (code < 0) goto _OVER;
tjsonGetInt32ValueFromDouble(vnode, "dropped", pCfg->dropped, code);
if (code < 0) goto _OVER;
tjsonGetInt32ValueFromDouble(vnode, "vgVersion", pCfg->vgVersion, code);
if (code < 0) goto _OVER;
tjsonGetInt32ValueFromDouble(vnode, "diskPrimary", pCfg->diskPrimary, code);
if (code < 0) goto _OVER;
tjsonGetInt32ValueFromDouble(vnode, "toVgId", pCfg->toVgId, code);
if (code < 0) goto _OVER;
2023-01-11 02:44:07 +00:00
snprintf(pCfg->path, sizeof(pCfg->path), "%s%svnode%d", pMgmt->path, TD_DIRSEP, pCfg->vgId);
}
code = 0;
*ppCfgs = pCfgs;
*numOfVnodes = vnodesNum;
_OVER:
if (*ppCfgs == NULL) taosMemoryFree(pCfgs);
return code;
}
2022-05-11 10:23:08 +00:00
int32_t vmGetVnodeListFromFile(SVnodeMgmt *pMgmt, SWrapperCfg **ppCfgs, int32_t *numOfVnodes) {
2023-01-11 02:44:07 +00:00
int32_t code = -1;
TdFilePtr pFile = NULL;
char *pData = NULL;
SJson *pJson = 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;
snprintf(file, sizeof(file), "%s%svnodes.json", pMgmt->path, TD_DIRSEP);
2023-09-01 05:24:47 +00:00
if (taosStatFile(file, NULL, NULL, NULL) < 0) {
2023-01-11 02:44:07 +00:00
dInfo("vnode file:%s not exist", file);
return 0;
}
2022-03-16 02:48:22 +00:00
pFile = taosOpenFile(file, TD_FILE_READ);
if (pFile == NULL) {
2023-01-11 02:44:07 +00:00
terrno = TAOS_SYSTEM_ERROR(errno);
dError("failed to open vnode file:%s since %s", file, terrstr());
2022-05-08 14:20:53 +00:00
goto _OVER;
2022-03-16 02:48:22 +00:00
}
2023-01-11 02:44:07 +00:00
int64_t size = 0;
if (taosFStatFile(pFile, &size, NULL) < 0) {
terrno = TAOS_SYSTEM_ERROR(errno);
dError("failed to fstat mnode file:%s since %s", file, terrstr());
2022-05-08 14:20:53 +00:00
goto _OVER;
2022-03-16 02:48:22 +00:00
}
2023-01-11 02:44:07 +00:00
pData = taosMemoryMalloc(size + 1);
if (pData == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
2022-05-08 14:20:53 +00:00
goto _OVER;
2022-03-16 02:48:22 +00:00
}
2023-01-11 02:44:07 +00:00
if (taosReadFile(pFile, pData, size) != size) {
terrno = TAOS_SYSTEM_ERROR(errno);
dError("failed to read vnode file:%s since %s", file, terrstr());
2022-05-08 14:20:53 +00:00
goto _OVER;
2022-03-16 02:48:22 +00:00
}
2023-01-11 02:44:07 +00:00
pData[size] = '\0';
2022-03-16 02:48:22 +00:00
2023-01-11 02:44:07 +00:00
pJson = tjsonParse(pData);
if (pJson == NULL) {
terrno = TSDB_CODE_INVALID_JSON_FORMAT;
goto _OVER;
}
2022-03-16 02:48:22 +00:00
2023-01-11 02:44:07 +00:00
if (vmDecodeVnodeList(pJson, pMgmt, ppCfgs, numOfVnodes) < 0) {
terrno = TSDB_CODE_INVALID_JSON_FORMAT;
goto _OVER;
2022-03-16 02:48:22 +00:00
}
code = 0;
2023-01-11 02:44:07 +00:00
dInfo("succceed to read vnode file %s", file);
2022-03-16 02:48:22 +00:00
2022-05-08 14:20:53 +00:00
_OVER:
2023-01-11 02:44:07 +00:00
if (pData != NULL) taosMemoryFree(pData);
if (pJson != NULL) cJSON_Delete(pJson);
2022-03-16 02:48:22 +00:00
if (pFile != NULL) taosCloseFile(&pFile);
2023-01-11 02:44:07 +00:00
if (code != 0) {
dError("failed to read vnode file:%s since %s", file, terrstr());
}
2022-03-16 02:48:22 +00:00
return code;
}
static int32_t vmEncodeVnodeList(SJson *pJson, SVnodeObj **ppVnodes, int32_t numOfVnodes) {
SJson *vnodes = tjsonCreateArray();
if (vnodes == NULL) return -1;
if (tjsonAddItemToObject(pJson, "vnodes", vnodes) < 0) return -1;
for (int32_t i = 0; i < numOfVnodes; ++i) {
SVnodeObj *pVnode = ppVnodes[i];
if (pVnode == NULL) continue;
SJson *vnode = tjsonCreateObject();
if (vnode == NULL) return -1;
if (tjsonAddDoubleToObject(vnode, "vgId", pVnode->vgId) < 0) return -1;
if (tjsonAddDoubleToObject(vnode, "dropped", pVnode->dropped) < 0) return -1;
if (tjsonAddDoubleToObject(vnode, "vgVersion", pVnode->vgVersion) < 0) return -1;
if (tjsonAddDoubleToObject(vnode, "diskPrimary", pVnode->diskPrimary) < 0) return -1;
if (pVnode->toVgId && tjsonAddDoubleToObject(vnode, "toVgId", pVnode->toVgId) < 0) return -1;
if (tjsonAddItemToArray(vnodes, vnode) < 0) return -1;
}
return 0;
}
2022-05-11 10:23:08 +00:00
int32_t vmWriteVnodeListToFile(SVnodeMgmt *pMgmt) {
int32_t code = -1;
char *buffer = NULL;
SJson *pJson = NULL;
TdFilePtr pFile = NULL;
SVnodeObj **ppVnodes = NULL;
char file[PATH_MAX] = {0};
char realfile[PATH_MAX] = {0};
snprintf(file, sizeof(file), "%s%svnodes_tmp.json", pMgmt->path, TD_DIRSEP);
snprintf(realfile, sizeof(realfile), "%s%svnodes.json", pMgmt->path, TD_DIRSEP);
2022-03-16 02:48:22 +00:00
int32_t numOfVnodes = 0;
ppVnodes = vmGetVnodeListFromHash(pMgmt, &numOfVnodes);
if (ppVnodes == NULL) goto _OVER;
2022-03-16 02:48:22 +00:00
terrno = TSDB_CODE_OUT_OF_MEMORY;
pJson = tjsonCreateObject();
if (pJson == NULL) goto _OVER;
if (vmEncodeVnodeList(pJson, ppVnodes, numOfVnodes) != 0) goto _OVER;
buffer = tjsonToString(pJson);
if (buffer == NULL) goto _OVER;
terrno = 0;
pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
if (pFile == NULL) goto _OVER;
2022-09-27 08:02:40 +00:00
int32_t len = strlen(buffer);
if (taosWriteFile(pFile, buffer, len) <= 0) goto _OVER;
if (taosFsyncFile(pFile) < 0) goto _OVER;
2022-03-16 02:48:22 +00:00
taosCloseFile(&pFile);
if (taosRenameFile(file, realfile) != 0) goto _OVER;
code = 0;
dInfo("succeed to write vnodes file:%s, vnodes:%d", realfile, numOfVnodes);
_OVER:
if (pJson != NULL) tjsonDelete(pJson);
if (buffer != NULL) taosMemoryFree(buffer);
if (pFile != NULL) taosCloseFile(&pFile);
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
}
if (code != 0) {
if (terrno == 0) terrno = TAOS_SYSTEM_ERROR(errno);
dError("failed to write vnodes file:%s since %s, vnodes:%d", realfile, terrstr(), numOfVnodes);
}
return code;
}