Merge pull request #19495 from taosdata/fix/TD-21646
enh: adjust some json file reader
This commit is contained in:
commit
ca5c3423b2
|
@ -14,8 +14,8 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#define _DEFAULT_SOURCE
|
#define _DEFAULT_SOURCE
|
||||||
#include "vmInt.h"
|
|
||||||
#include "tjson.h"
|
#include "tjson.h"
|
||||||
|
#include "vmInt.h"
|
||||||
|
|
||||||
#define MAX_CONTENT_LEN 2 * 1024 * 1024
|
#define MAX_CONTENT_LEN 2 * 1024 * 1024
|
||||||
|
|
||||||
|
@ -46,102 +46,108 @@ SVnodeObj **vmGetVnodeListFromHash(SVnodeMgmt *pMgmt, int32_t *numOfVnodes) {
|
||||||
return pVnodes;
|
return pVnodes;
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t vmGetVnodeListFromFile(SVnodeMgmt *pMgmt, SWrapperCfg **ppCfgs, int32_t *numOfVnodes) {
|
static int32_t vmDecodeVnodeList(SJson *pJson, SVnodeMgmt *pMgmt, SWrapperCfg **ppCfgs, int32_t *numOfVnodes) {
|
||||||
int32_t code = TSDB_CODE_INVALID_JSON_FORMAT;
|
int32_t code = -1;
|
||||||
int32_t len = 0;
|
|
||||||
int32_t maxLen = MAX_CONTENT_LEN;
|
|
||||||
char *content = taosMemoryCalloc(1, maxLen + 1);
|
|
||||||
cJSON *root = NULL;
|
|
||||||
FILE *fp = NULL;
|
|
||||||
char file[PATH_MAX] = {0};
|
|
||||||
SWrapperCfg *pCfgs = NULL;
|
SWrapperCfg *pCfgs = NULL;
|
||||||
TdFilePtr pFile = NULL;
|
*ppCfgs = NULL;
|
||||||
|
|
||||||
snprintf(file, sizeof(file), "%s%svnodes.json", pMgmt->path, TD_DIRSEP);
|
SJson *vnodes = tjsonGetObjectItem(pJson, "vnodes");
|
||||||
|
if (vnodes == NULL) return -1;
|
||||||
pFile = taosOpenFile(file, TD_FILE_READ);
|
|
||||||
if (pFile == NULL) {
|
|
||||||
dInfo("file %s not exist", file);
|
|
||||||
code = 0;
|
|
||||||
goto _OVER;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (content == NULL) {
|
|
||||||
terrno = TSDB_CODE_OUT_OF_MEMORY;
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
len = (int32_t)taosReadFile(pFile, content, maxLen);
|
|
||||||
if (len <= 0) {
|
|
||||||
dError("failed to read %s since content is null", file);
|
|
||||||
goto _OVER;
|
|
||||||
}
|
|
||||||
|
|
||||||
content[len] = 0;
|
|
||||||
root = cJSON_Parse(content);
|
|
||||||
if (root == NULL) {
|
|
||||||
dError("failed to read %s since invalid json format", file);
|
|
||||||
goto _OVER;
|
|
||||||
}
|
|
||||||
|
|
||||||
cJSON *vnodes = cJSON_GetObjectItem(root, "vnodes");
|
|
||||||
if (!vnodes || vnodes->type != cJSON_Array) {
|
|
||||||
dError("failed to read %s since vnodes not found", file);
|
|
||||||
goto _OVER;
|
|
||||||
}
|
|
||||||
|
|
||||||
int32_t vnodesNum = cJSON_GetArraySize(vnodes);
|
int32_t vnodesNum = cJSON_GetArraySize(vnodes);
|
||||||
if (vnodesNum > 0) {
|
if (vnodesNum > 0) {
|
||||||
pCfgs = taosMemoryCalloc(vnodesNum, sizeof(SWrapperCfg));
|
pCfgs = taosMemoryCalloc(vnodesNum, sizeof(SWrapperCfg));
|
||||||
if (pCfgs == NULL) {
|
if (pCfgs == NULL) return -1;
|
||||||
dError("failed to read %s since out of memory", file);
|
|
||||||
code = TSDB_CODE_OUT_OF_MEMORY;
|
|
||||||
goto _OVER;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int32_t i = 0; i < vnodesNum; ++i) {
|
for (int32_t i = 0; i < vnodesNum; ++i) {
|
||||||
cJSON *vnode = cJSON_GetArrayItem(vnodes, i);
|
SJson *vnode = tjsonGetArrayItem(vnodes, i);
|
||||||
|
if (vnode == NULL) goto _OVER;
|
||||||
|
|
||||||
SWrapperCfg *pCfg = &pCfgs[i];
|
SWrapperCfg *pCfg = &pCfgs[i];
|
||||||
|
tjsonGetInt32ValueFromDouble(vnode, "vgId", pCfg->vgId, code);
|
||||||
|
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;
|
||||||
|
|
||||||
cJSON *vgId = cJSON_GetObjectItem(vnode, "vgId");
|
|
||||||
if (!vgId || vgId->type != cJSON_Number) {
|
|
||||||
dError("failed to read %s since vgId not found", file);
|
|
||||||
taosMemoryFree(pCfgs);
|
|
||||||
goto _OVER;
|
|
||||||
}
|
|
||||||
pCfg->vgId = vgId->valueint;
|
|
||||||
snprintf(pCfg->path, sizeof(pCfg->path), "%s%svnode%d", pMgmt->path, TD_DIRSEP, pCfg->vgId);
|
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);
|
|
||||||
taosMemoryFree(pCfgs);
|
|
||||||
goto _OVER;
|
|
||||||
}
|
|
||||||
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);
|
|
||||||
taosMemoryFree(pCfgs);
|
|
||||||
goto _OVER;
|
|
||||||
}
|
|
||||||
pCfg->vgVersion = vgVersion->valueint;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
*ppCfgs = pCfgs;
|
|
||||||
}
|
|
||||||
|
|
||||||
*numOfVnodes = vnodesNum;
|
|
||||||
code = 0;
|
code = 0;
|
||||||
dInfo("succcessed to read file %s, numOfVnodes:%d", file, vnodesNum);
|
*ppCfgs = pCfgs;
|
||||||
|
*numOfVnodes = vnodesNum;
|
||||||
|
|
||||||
_OVER:
|
_OVER:
|
||||||
if (content != NULL) taosMemoryFree(content);
|
if (*ppCfgs == NULL) taosMemoryFree(pCfgs);
|
||||||
if (root != NULL) cJSON_Delete(root);
|
return code;
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t vmGetVnodeListFromFile(SVnodeMgmt *pMgmt, SWrapperCfg **ppCfgs, int32_t *numOfVnodes) {
|
||||||
|
int32_t code = -1;
|
||||||
|
TdFilePtr pFile = NULL;
|
||||||
|
char *pData = NULL;
|
||||||
|
SJson *pJson = NULL;
|
||||||
|
char file[PATH_MAX] = {0};
|
||||||
|
SWrapperCfg *pCfgs = NULL;
|
||||||
|
snprintf(file, sizeof(file), "%s%svnodes.json", pMgmt->path, TD_DIRSEP);
|
||||||
|
|
||||||
|
if (taosStatFile(file, NULL, NULL) < 0) {
|
||||||
|
dInfo("vnode file:%s not exist", file);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
pFile = taosOpenFile(file, TD_FILE_READ);
|
||||||
|
if (pFile == NULL) {
|
||||||
|
terrno = TAOS_SYSTEM_ERROR(errno);
|
||||||
|
dError("failed to open vnode file:%s since %s", file, terrstr());
|
||||||
|
goto _OVER;
|
||||||
|
}
|
||||||
|
|
||||||
|
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());
|
||||||
|
goto _OVER;
|
||||||
|
}
|
||||||
|
|
||||||
|
pData = taosMemoryMalloc(size + 1);
|
||||||
|
if (pData == NULL) {
|
||||||
|
terrno = TSDB_CODE_OUT_OF_MEMORY;
|
||||||
|
goto _OVER;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (taosReadFile(pFile, pData, size) != size) {
|
||||||
|
terrno = TAOS_SYSTEM_ERROR(errno);
|
||||||
|
dError("failed to read vnode file:%s since %s", file, terrstr());
|
||||||
|
goto _OVER;
|
||||||
|
}
|
||||||
|
|
||||||
|
pData[size] = '\0';
|
||||||
|
|
||||||
|
pJson = tjsonParse(pData);
|
||||||
|
if (pJson == NULL) {
|
||||||
|
terrno = TSDB_CODE_INVALID_JSON_FORMAT;
|
||||||
|
goto _OVER;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (vmDecodeVnodeList(pJson, pMgmt, ppCfgs, numOfVnodes) < 0) {
|
||||||
|
terrno = TSDB_CODE_INVALID_JSON_FORMAT;
|
||||||
|
goto _OVER;
|
||||||
|
}
|
||||||
|
|
||||||
|
code = 0;
|
||||||
|
dInfo("succceed to read vnode file %s", file);
|
||||||
|
|
||||||
|
_OVER:
|
||||||
|
if (pData != NULL) taosMemoryFree(pData);
|
||||||
|
if (pJson != NULL) cJSON_Delete(pJson);
|
||||||
if (pFile != NULL) taosCloseFile(&pFile);
|
if (pFile != NULL) taosCloseFile(&pFile);
|
||||||
|
|
||||||
terrno = code;
|
if (code != 0) {
|
||||||
|
dError("failed to read vnode file:%s since %s", file, terrstr());
|
||||||
|
}
|
||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -41,14 +41,49 @@ static void dmGetDnodeEp(SDnodeData *pData, int32_t dnodeId, char *pEp, char *pF
|
||||||
taosThreadRwlockUnlock(&pData->lock);
|
taosThreadRwlockUnlock(&pData->lock);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int32_t dmDecodeEps(SJson *pJson, SDnodeData *pData) {
|
||||||
|
int32_t code = 0;
|
||||||
|
|
||||||
|
tjsonGetInt32ValueFromDouble(pJson, "dnodeId", pData->dnodeId, code);
|
||||||
|
if (code < 0) return -1;
|
||||||
|
tjsonGetNumberValue(pJson, "dnodeVer", pData->dnodeVer, code);
|
||||||
|
if (code < 0) return -1;
|
||||||
|
tjsonGetNumberValue(pJson, "clusterId", pData->clusterId, code);
|
||||||
|
if (code < 0) return -1;
|
||||||
|
tjsonGetInt32ValueFromDouble(pJson, "dropped", pData->dropped, code);
|
||||||
|
if (code < 0) return -1;
|
||||||
|
|
||||||
|
SJson *dnodes = tjsonGetObjectItem(pJson, "dnodes");
|
||||||
|
if (dnodes == NULL) return 0;
|
||||||
|
int32_t numOfDnodes = tjsonGetArraySize(dnodes);
|
||||||
|
|
||||||
|
for (int32_t i = 0; i < numOfDnodes; ++i) {
|
||||||
|
SJson *dnode = tjsonGetArrayItem(dnodes, i);
|
||||||
|
if (dnode == NULL) return -1;
|
||||||
|
|
||||||
|
SDnodeEp dnodeEp = {0};
|
||||||
|
tjsonGetInt32ValueFromDouble(dnode, "id", dnodeEp.id, code);
|
||||||
|
if (code < 0) return -1;
|
||||||
|
code = tjsonGetStringValue(dnode, "fqdn", dnodeEp.ep.fqdn);
|
||||||
|
if (code < 0) return -1;
|
||||||
|
tjsonGetUInt16ValueFromDouble(dnode, "port", dnodeEp.ep.port, code);
|
||||||
|
if (code < 0) return -1;
|
||||||
|
tjsonGetInt8ValueFromDouble(dnode, "isMnode", dnodeEp.isMnode, code);
|
||||||
|
if (code < 0) return -1;
|
||||||
|
|
||||||
|
if (taosArrayPush(pData->dnodeEps, &dnodeEp) == NULL) return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int32_t dmReadEps(SDnodeData *pData) {
|
int32_t dmReadEps(SDnodeData *pData) {
|
||||||
int32_t code = TSDB_CODE_INVALID_JSON_FORMAT;
|
int32_t code = -1;
|
||||||
int32_t len = 0;
|
|
||||||
int32_t maxLen = 256 * 1024;
|
|
||||||
char *content = taosMemoryCalloc(1, maxLen + 1);
|
|
||||||
cJSON *root = NULL;
|
|
||||||
char file[PATH_MAX] = {0};
|
|
||||||
TdFilePtr pFile = NULL;
|
TdFilePtr pFile = NULL;
|
||||||
|
char *content = NULL;
|
||||||
|
SJson *pJson = NULL;
|
||||||
|
char file[PATH_MAX] = {0};
|
||||||
|
snprintf(file, sizeof(file), "%s%sdnode%sdnode.json", tsDataDir, TD_DIRSEP, TD_DIRSEP);
|
||||||
|
|
||||||
pData->dnodeEps = taosArrayInit(1, sizeof(SDnodeEp));
|
pData->dnodeEps = taosArrayInit(1, sizeof(SDnodeEp));
|
||||||
if (pData->dnodeEps == NULL) {
|
if (pData->dnodeEps == NULL) {
|
||||||
|
@ -56,113 +91,63 @@ int32_t dmReadEps(SDnodeData *pData) {
|
||||||
goto _OVER;
|
goto _OVER;
|
||||||
}
|
}
|
||||||
|
|
||||||
snprintf(file, sizeof(file), "%s%sdnode%sdnode.json", tsDataDir, TD_DIRSEP, TD_DIRSEP);
|
if (taosStatFile(file, NULL, NULL) < 0) {
|
||||||
|
dInfo("dnode file:%s not exist", file);
|
||||||
|
code = 0;
|
||||||
|
goto _OVER;
|
||||||
|
}
|
||||||
|
|
||||||
pFile = taosOpenFile(file, TD_FILE_READ);
|
pFile = taosOpenFile(file, TD_FILE_READ);
|
||||||
if (pFile == NULL) {
|
if (pFile == NULL) {
|
||||||
code = 0;
|
terrno = TAOS_SYSTEM_ERROR(errno);
|
||||||
|
dError("failed to open dnode file:%s since %s", file, terrstr());
|
||||||
goto _OVER;
|
goto _OVER;
|
||||||
}
|
}
|
||||||
|
|
||||||
len = (int32_t)taosReadFile(pFile, content, maxLen);
|
int64_t size = 0;
|
||||||
if (len <= 0) {
|
if (taosFStatFile(pFile, &size, NULL) < 0) {
|
||||||
dError("failed to read %s since content is null", file);
|
terrno = TAOS_SYSTEM_ERROR(errno);
|
||||||
|
dError("failed to fstat dnode file:%s since %s", file, terrstr());
|
||||||
goto _OVER;
|
goto _OVER;
|
||||||
}
|
}
|
||||||
|
|
||||||
content[len] = 0;
|
content = taosMemoryMalloc(size + 1);
|
||||||
root = cJSON_Parse(content);
|
if (content == NULL) {
|
||||||
if (root == NULL) {
|
terrno = TSDB_CODE_OUT_OF_MEMORY;
|
||||||
dError("failed to read %s since invalid json format", file);
|
|
||||||
goto _OVER;
|
goto _OVER;
|
||||||
}
|
}
|
||||||
|
|
||||||
cJSON *dnodeId = cJSON_GetObjectItem(root, "dnodeId");
|
if (taosReadFile(pFile, content, size) != size) {
|
||||||
if (!dnodeId || dnodeId->type != cJSON_Number) {
|
terrno = TAOS_SYSTEM_ERROR(errno);
|
||||||
dError("failed to read %s since dnodeId not found", file);
|
dError("failed to read dnode file:%s since %s", file, terrstr());
|
||||||
goto _OVER;
|
|
||||||
}
|
|
||||||
pData->dnodeId = dnodeId->valueint;
|
|
||||||
|
|
||||||
cJSON *dnodeVer = cJSON_GetObjectItem(root, "dnodeVer");
|
|
||||||
if (!dnodeVer || dnodeVer->type != cJSON_String) {
|
|
||||||
dError("failed to read %s since dnodeVer not found", file);
|
|
||||||
goto _OVER;
|
|
||||||
}
|
|
||||||
pData->dnodeVer = atoll(dnodeVer->valuestring);
|
|
||||||
|
|
||||||
cJSON *clusterId = cJSON_GetObjectItem(root, "clusterId");
|
|
||||||
if (!clusterId || clusterId->type != cJSON_String) {
|
|
||||||
dError("failed to read %s since clusterId not found", file);
|
|
||||||
goto _OVER;
|
|
||||||
}
|
|
||||||
pData->clusterId = atoll(clusterId->valuestring);
|
|
||||||
|
|
||||||
cJSON *dropped = cJSON_GetObjectItem(root, "dropped");
|
|
||||||
if (!dropped || dropped->type != cJSON_Number) {
|
|
||||||
dError("failed to read %s since dropped not found", file);
|
|
||||||
goto _OVER;
|
|
||||||
}
|
|
||||||
pData->dropped = dropped->valueint;
|
|
||||||
|
|
||||||
cJSON *dnodes = cJSON_GetObjectItem(root, "dnodes");
|
|
||||||
if (!dnodes || dnodes->type != cJSON_Array) {
|
|
||||||
dError("failed to read %s since dnodes not found", file);
|
|
||||||
goto _OVER;
|
goto _OVER;
|
||||||
}
|
}
|
||||||
|
|
||||||
int32_t numOfDnodes = cJSON_GetArraySize(dnodes);
|
content[size] = '\0';
|
||||||
if (numOfDnodes <= 0) {
|
|
||||||
dError("failed to read %s since numOfDnodes:%d invalid", file, numOfDnodes);
|
pJson = tjsonParse(content);
|
||||||
|
if (pJson == NULL) {
|
||||||
|
terrno = TSDB_CODE_INVALID_JSON_FORMAT;
|
||||||
goto _OVER;
|
goto _OVER;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int32_t i = 0; i < numOfDnodes; ++i) {
|
if (dmDecodeEps(pJson, pData) < 0) {
|
||||||
cJSON *node = cJSON_GetArrayItem(dnodes, i);
|
terrno = TSDB_CODE_INVALID_JSON_FORMAT;
|
||||||
if (node == NULL) break;
|
|
||||||
|
|
||||||
SDnodeEp dnodeEp = {0};
|
|
||||||
|
|
||||||
cJSON *did = cJSON_GetObjectItem(node, "id");
|
|
||||||
if (!did || did->type != cJSON_Number) {
|
|
||||||
dError("failed to read %s since dnodeId not found", file);
|
|
||||||
goto _OVER;
|
goto _OVER;
|
||||||
}
|
}
|
||||||
|
|
||||||
dnodeEp.id = did->valueint;
|
|
||||||
|
|
||||||
cJSON *dnodeFqdn = cJSON_GetObjectItem(node, "fqdn");
|
|
||||||
if (!dnodeFqdn || dnodeFqdn->type != cJSON_String || dnodeFqdn->valuestring == NULL) {
|
|
||||||
dError("failed to read %s since dnodeFqdn not found", file);
|
|
||||||
goto _OVER;
|
|
||||||
}
|
|
||||||
tstrncpy(dnodeEp.ep.fqdn, dnodeFqdn->valuestring, TSDB_FQDN_LEN);
|
|
||||||
|
|
||||||
cJSON *dnodePort = cJSON_GetObjectItem(node, "port");
|
|
||||||
if (!dnodePort || dnodePort->type != cJSON_Number) {
|
|
||||||
dError("failed to read %s since dnodePort not found", file);
|
|
||||||
goto _OVER;
|
|
||||||
}
|
|
||||||
|
|
||||||
dnodeEp.ep.port = dnodePort->valueint;
|
|
||||||
|
|
||||||
cJSON *isMnode = cJSON_GetObjectItem(node, "isMnode");
|
|
||||||
if (!isMnode || isMnode->type != cJSON_Number) {
|
|
||||||
dError("failed to read %s since isMnode not found", file);
|
|
||||||
goto _OVER;
|
|
||||||
}
|
|
||||||
dnodeEp.isMnode = isMnode->valueint;
|
|
||||||
|
|
||||||
taosArrayPush(pData->dnodeEps, &dnodeEp);
|
|
||||||
}
|
|
||||||
|
|
||||||
code = 0;
|
code = 0;
|
||||||
dDebug("succcessed to read file %s", file);
|
dInfo("succceed to read mnode file %s", file);
|
||||||
|
|
||||||
_OVER:
|
_OVER:
|
||||||
if (content != NULL) taosMemoryFree(content);
|
if (content != NULL) taosMemoryFree(content);
|
||||||
if (root != NULL) cJSON_Delete(root);
|
if (pJson != NULL) cJSON_Delete(pJson);
|
||||||
if (pFile != NULL) taosCloseFile(&pFile);
|
if (pFile != NULL) taosCloseFile(&pFile);
|
||||||
|
|
||||||
|
if (code != 0) {
|
||||||
|
dError("failed to read dnode file:%s since %s", file, terrstr());
|
||||||
|
}
|
||||||
|
|
||||||
if (taosArrayGetSize(pData->dnodeEps) == 0) {
|
if (taosArrayGetSize(pData->dnodeEps) == 0) {
|
||||||
SDnodeEp dnodeEp = {0};
|
SDnodeEp dnodeEp = {0};
|
||||||
dnodeEp.isMnode = 1;
|
dnodeEp.isMnode = 1;
|
||||||
|
@ -178,7 +163,6 @@ _OVER:
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
terrno = code;
|
|
||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -247,7 +231,7 @@ _OVER:
|
||||||
|
|
||||||
if (code != 0) {
|
if (code != 0) {
|
||||||
if (terrno == 0) terrno = TAOS_SYSTEM_ERROR(errno);
|
if (terrno == 0) terrno = TAOS_SYSTEM_ERROR(errno);
|
||||||
dInfo("succeed to write dnode file:%s since %s, dnodeVer:%" PRId64, realfile, terrstr(), pData->dnodeVer);
|
dError("failed to write dnode file:%s since %s, dnodeVer:%" PRId64, realfile, terrstr(), pData->dnodeVer);
|
||||||
}
|
}
|
||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,48 +19,81 @@
|
||||||
|
|
||||||
#define MAXLEN 1024
|
#define MAXLEN 1024
|
||||||
|
|
||||||
int32_t dmReadFile(const char *path, const char *name, bool *pDeployed) {
|
static int32_t dmDecodeFile(SJson *pJson, bool *deployed) {
|
||||||
int32_t code = TSDB_CODE_INVALID_JSON_FORMAT;
|
int32_t code = 0;
|
||||||
int64_t len = 0;
|
int32_t value = 0;
|
||||||
char content[MAXLEN + 1] = {0};
|
|
||||||
cJSON *root = NULL;
|
|
||||||
char file[PATH_MAX] = {0};
|
|
||||||
TdFilePtr pFile = NULL;
|
|
||||||
|
|
||||||
|
tjsonGetInt32ValueFromDouble(pJson, "deployed", value, code);
|
||||||
|
if (code < 0) return -1;
|
||||||
|
|
||||||
|
*deployed = (value != 0);
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t dmReadFile(const char *path, const char *name, bool *pDeployed) {
|
||||||
|
int32_t code = -1;
|
||||||
|
TdFilePtr pFile = NULL;
|
||||||
|
char *content = NULL;
|
||||||
|
SJson *pJson = NULL;
|
||||||
|
char file[PATH_MAX] = {0};
|
||||||
snprintf(file, sizeof(file), "%s%s%s.json", path, TD_DIRSEP, name);
|
snprintf(file, sizeof(file), "%s%s%s.json", path, TD_DIRSEP, name);
|
||||||
|
|
||||||
|
if (taosStatFile(file, NULL, NULL) < 0) {
|
||||||
|
dInfo("file:%s not exist", file);
|
||||||
|
code = 0;
|
||||||
|
goto _OVER;
|
||||||
|
}
|
||||||
|
|
||||||
pFile = taosOpenFile(file, TD_FILE_READ);
|
pFile = taosOpenFile(file, TD_FILE_READ);
|
||||||
if (pFile == NULL) {
|
if (pFile == NULL) {
|
||||||
|
terrno = TAOS_SYSTEM_ERROR(errno);
|
||||||
|
dError("failed to open file:%s since %s", file, terrstr());
|
||||||
|
goto _OVER;
|
||||||
|
}
|
||||||
|
|
||||||
|
int64_t size = 0;
|
||||||
|
if (taosFStatFile(pFile, &size, NULL) < 0) {
|
||||||
|
terrno = TAOS_SYSTEM_ERROR(errno);
|
||||||
|
dError("failed to fstat file:%s since %s", file, terrstr());
|
||||||
|
goto _OVER;
|
||||||
|
}
|
||||||
|
|
||||||
|
content = taosMemoryMalloc(size + 1);
|
||||||
|
if (content == NULL) {
|
||||||
|
terrno = TSDB_CODE_OUT_OF_MEMORY;
|
||||||
|
goto _OVER;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (taosReadFile(pFile, content, size) != size) {
|
||||||
|
terrno = TAOS_SYSTEM_ERROR(errno);
|
||||||
|
dError("failed to read file:%s since %s", file, terrstr());
|
||||||
|
goto _OVER;
|
||||||
|
}
|
||||||
|
|
||||||
|
content[size] = '\0';
|
||||||
|
|
||||||
|
pJson = tjsonParse(content);
|
||||||
|
if (pJson == NULL) {
|
||||||
|
terrno = TSDB_CODE_INVALID_JSON_FORMAT;
|
||||||
|
goto _OVER;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (dmDecodeFile(pJson, pDeployed) < 0) {
|
||||||
|
terrno = TSDB_CODE_INVALID_JSON_FORMAT;
|
||||||
|
goto _OVER;
|
||||||
|
}
|
||||||
|
|
||||||
code = 0;
|
code = 0;
|
||||||
goto _OVER;
|
dInfo("succceed to read mnode file %s", file);
|
||||||
}
|
|
||||||
|
|
||||||
len = taosReadFile(pFile, content, MAXLEN);
|
|
||||||
if (len <= 0) {
|
|
||||||
dError("failed to read %s since content is null", file);
|
|
||||||
goto _OVER;
|
|
||||||
}
|
|
||||||
|
|
||||||
root = cJSON_Parse(content);
|
|
||||||
if (root == NULL) {
|
|
||||||
dError("failed to read %s since invalid json format", file);
|
|
||||||
goto _OVER;
|
|
||||||
}
|
|
||||||
|
|
||||||
cJSON *deployed = cJSON_GetObjectItem(root, "deployed");
|
|
||||||
if (!deployed || deployed->type != cJSON_Number) {
|
|
||||||
dError("failed to read %s since deployed not found", file);
|
|
||||||
goto _OVER;
|
|
||||||
}
|
|
||||||
*pDeployed = deployed->valueint != 0;
|
|
||||||
|
|
||||||
dDebug("succcessed to read file %s, deployed:%d", file, *pDeployed);
|
|
||||||
code = 0;
|
|
||||||
|
|
||||||
_OVER:
|
_OVER:
|
||||||
if (root != NULL) cJSON_Delete(root);
|
if (content != NULL) taosMemoryFree(content);
|
||||||
|
if (pJson != NULL) cJSON_Delete(pJson);
|
||||||
if (pFile != NULL) taosCloseFile(&pFile);
|
if (pFile != NULL) taosCloseFile(&pFile);
|
||||||
|
|
||||||
terrno = code;
|
if (code != 0) {
|
||||||
|
dError("failed to read dnode file:%s since %s", file, terrstr());
|
||||||
|
}
|
||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue