enh: read mnode file
This commit is contained in:
parent
66f2cc2704
commit
012dbf3176
|
@ -17,117 +17,97 @@
|
||||||
#include "mmInt.h"
|
#include "mmInt.h"
|
||||||
#include "tjson.h"
|
#include "tjson.h"
|
||||||
|
|
||||||
int32_t mmReadFile(const char *path, SMnodeOpt *pOption) {
|
static int32_t mmDecodeOption(SJson *pJson, SMnodeOpt *pOption) {
|
||||||
int32_t code = TSDB_CODE_INVALID_JSON_FORMAT;
|
int32_t code = 0;
|
||||||
int32_t len = 0;
|
|
||||||
int32_t maxLen = 4096;
|
|
||||||
char *content = taosMemoryCalloc(1, maxLen + 1);
|
|
||||||
cJSON *root = NULL;
|
|
||||||
char file[PATH_MAX] = {0};
|
|
||||||
TdFilePtr pFile = NULL;
|
|
||||||
|
|
||||||
|
tjsonGetInt32ValueFromDouble(pJson, "deployed", pOption->deploy, code);
|
||||||
|
if (code < 0) return -1;
|
||||||
|
tjsonGetInt32ValueFromDouble(pJson, "selfIndex", pOption->selfIndex, code);
|
||||||
|
if (code < 0) return 0;
|
||||||
|
|
||||||
|
SJson *replicas = tjsonGetObjectItem(pJson, "replicas");
|
||||||
|
if (replicas == NULL) return 0;
|
||||||
|
pOption->numOfReplicas = tjsonGetArraySize(replicas);
|
||||||
|
|
||||||
|
for (int32_t i = 0; i < pOption->numOfReplicas; ++i) {
|
||||||
|
SJson *replica = tjsonGetArrayItem(replicas, i);
|
||||||
|
if (replica == NULL) return -1;
|
||||||
|
|
||||||
|
SReplica *pReplica = pOption->replicas + i;
|
||||||
|
tjsonGetInt32ValueFromDouble(replica, "id", pReplica->id, code);
|
||||||
|
if (code < 0) return -1;
|
||||||
|
code = tjsonGetStringValue(replica, "fqdn", pReplica->fqdn);
|
||||||
|
if (code < 0) return -1;
|
||||||
|
tjsonGetUInt16ValueFromDouble(replica, "port", pReplica->port, code);
|
||||||
|
if (code < 0) return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t mmReadFile(const char *path, SMnodeOpt *pOption) {
|
||||||
|
int32_t code = -1;
|
||||||
|
TdFilePtr pFile = NULL;
|
||||||
|
char *pData = NULL;
|
||||||
|
SJson *pJson = NULL;
|
||||||
|
char file[PATH_MAX] = {0};
|
||||||
snprintf(file, sizeof(file), "%s%smnode.json", path, TD_DIRSEP);
|
snprintf(file, sizeof(file), "%s%smnode.json", path, TD_DIRSEP);
|
||||||
|
|
||||||
|
if (taosStatFile(file, NULL, NULL) < 0) {
|
||||||
|
dInfo("mnode file:%s not exist", file);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
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 mnode 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 mnode file:%s since %s", file, terrstr());
|
||||||
goto _OVER;
|
goto _OVER;
|
||||||
}
|
}
|
||||||
|
|
||||||
content[len] = 0;
|
pData = taosMemoryMalloc(size + 1);
|
||||||
root = cJSON_Parse(content);
|
if (pData == 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 *deployed = cJSON_GetObjectItem(root, "deployed");
|
if (taosReadFile(pFile, pData, size) != size) {
|
||||||
if (!deployed || deployed->type != cJSON_Number) {
|
terrno = TAOS_SYSTEM_ERROR(errno);
|
||||||
dError("failed to read %s since deployed not found", file);
|
dError("failed to read mnode file:%s since %s", file, terrstr());
|
||||||
goto _OVER;
|
goto _OVER;
|
||||||
}
|
}
|
||||||
pOption->deploy = deployed->valueint;
|
|
||||||
|
|
||||||
cJSON *selfIndex = cJSON_GetObjectItem(root, "selfIndex");
|
pData[size] = '\0';
|
||||||
if (selfIndex) {
|
|
||||||
if (selfIndex->type != cJSON_Number) {
|
pJson = tjsonParse(pData);
|
||||||
dError("failed to read %s since selfIndex not found", file);
|
if (pJson == NULL) {
|
||||||
goto _OVER;
|
terrno = TSDB_CODE_INVALID_JSON_FORMAT;
|
||||||
}
|
goto _OVER;
|
||||||
pOption->selfIndex = selfIndex->valueint;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
cJSON *replicas = cJSON_GetObjectItem(root, "replicas");
|
if (mmDecodeOption(pJson, pOption) < 0) {
|
||||||
if (replicas) {
|
terrno = TSDB_CODE_INVALID_JSON_FORMAT;
|
||||||
if (replicas->type != cJSON_Array) {
|
goto _OVER;
|
||||||
dError("failed to read %s since replicas not found", file);
|
|
||||||
goto _OVER;
|
|
||||||
}
|
|
||||||
|
|
||||||
int32_t numOfReplicas = cJSON_GetArraySize(replicas);
|
|
||||||
if (numOfReplicas <= 0) {
|
|
||||||
dError("failed to read %s since numOfReplicas:%d invalid", file, numOfReplicas);
|
|
||||||
goto _OVER;
|
|
||||||
}
|
|
||||||
pOption->numOfReplicas = numOfReplicas;
|
|
||||||
|
|
||||||
for (int32_t i = 0; i < numOfReplicas; ++i) {
|
|
||||||
SReplica *pReplica = pOption->replicas + i;
|
|
||||||
|
|
||||||
cJSON *replica = cJSON_GetArrayItem(replicas, i);
|
|
||||||
if (replica == NULL) break;
|
|
||||||
|
|
||||||
cJSON *id = cJSON_GetObjectItem(replica, "id");
|
|
||||||
if (id) {
|
|
||||||
if (id->type != cJSON_Number) {
|
|
||||||
dError("failed to read %s since id not found", file);
|
|
||||||
goto _OVER;
|
|
||||||
}
|
|
||||||
if (pReplica) {
|
|
||||||
pReplica->id = id->valueint;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
cJSON *fqdn = cJSON_GetObjectItem(replica, "fqdn");
|
|
||||||
if (fqdn) {
|
|
||||||
if (fqdn->type != cJSON_String || fqdn->valuestring == NULL) {
|
|
||||||
dError("failed to read %s since fqdn not found", file);
|
|
||||||
goto _OVER;
|
|
||||||
}
|
|
||||||
if (pReplica) {
|
|
||||||
tstrncpy(pReplica->fqdn, fqdn->valuestring, TSDB_FQDN_LEN);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
cJSON *port = cJSON_GetObjectItem(replica, "port");
|
|
||||||
if (port) {
|
|
||||||
if (port->type != cJSON_Number) {
|
|
||||||
dError("failed to read %s since port not found", file);
|
|
||||||
goto _OVER;
|
|
||||||
}
|
|
||||||
if (pReplica) {
|
|
||||||
pReplica->port = (uint16_t)port->valueint;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
code = 0;
|
code = 0;
|
||||||
|
dInfo("succceed to read mnode file %s", file);
|
||||||
|
|
||||||
_OVER:
|
_OVER:
|
||||||
if (content != NULL) taosMemoryFree(content);
|
if (pData != NULL) taosMemoryFree(pData);
|
||||||
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) {
|
|
||||||
dDebug("succcessed to read file %s, deployed:%d", file, pOption->deploy);
|
|
||||||
}
|
|
||||||
|
|
||||||
terrno = code;
|
if (code != 0) {
|
||||||
|
dError("failed to read mnode file:%s since %s", file, terrstr());
|
||||||
|
}
|
||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue