提交 7607a978 编写于 作者: S Shengliang Guan

fix: handle error while write json file

上级 675f0057
...@@ -166,22 +166,22 @@ int32_t mmWriteFile(const char *path, const SMnodeOpt *pOption) { ...@@ -166,22 +166,22 @@ int32_t mmWriteFile(const char *path, const SMnodeOpt *pOption) {
snprintf(file, sizeof(file), "%s%smnode.json.bak", path, TD_DIRSEP); snprintf(file, sizeof(file), "%s%smnode.json.bak", path, TD_DIRSEP);
snprintf(realfile, sizeof(realfile), "%s%smnode.json", path, TD_DIRSEP); snprintf(realfile, sizeof(realfile), "%s%smnode.json", path, TD_DIRSEP);
pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
if (pFile == NULL) goto _OVER;
terrno = TSDB_CODE_OUT_OF_MEMORY; terrno = TSDB_CODE_OUT_OF_MEMORY;
pJson = tjsonCreateObject(); pJson = tjsonCreateObject();
if (pJson == NULL) goto _OVER; if (pJson == NULL) goto _OVER;
if (mmEncodeOption(pJson, pOption) != 0) goto _OVER; if (mmEncodeOption(pJson, pOption) != 0) goto _OVER;
buffer = tjsonToString(pJson); buffer = tjsonToString(pJson);
if (buffer == NULL) goto _OVER; if (buffer == NULL) goto _OVER;
terrno = 0;
pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
if (pFile == NULL) goto _OVER;
int32_t len = strlen(buffer); int32_t len = strlen(buffer);
if (taosWriteFile(pFile, buffer, len) <= 0) goto _OVER; if (taosWriteFile(pFile, buffer, len) <= 0) goto _OVER;
if (taosFsyncFile(pFile) < 0) goto _OVER; if (taosFsyncFile(pFile) < 0) goto _OVER;
taosCloseFile(&pFile);
taosCloseFile(&pFile);
if (taosRenameFile(file, realfile) != 0) goto _OVER; if (taosRenameFile(file, realfile) != 0) goto _OVER;
code = 0; code = 0;
...@@ -193,6 +193,7 @@ _OVER: ...@@ -193,6 +193,7 @@ _OVER:
if (pFile != NULL) taosCloseFile(&pFile); if (pFile != NULL) taosCloseFile(&pFile);
if (code != 0) { if (code != 0) {
if (terrno == 0) terrno = TAOS_SYSTEM_ERROR(errno);
dError("failed to write mnode file:%s since %s, deloyed:%d", realfile, terrstr(), pOption->deploy); dError("failed to write mnode file:%s since %s, deloyed:%d", realfile, terrstr(), pOption->deploy);
} }
return code; return code;
......
...@@ -176,9 +176,6 @@ int32_t vmWriteVnodeListToFile(SVnodeMgmt *pMgmt) { ...@@ -176,9 +176,6 @@ int32_t vmWriteVnodeListToFile(SVnodeMgmt *pMgmt) {
snprintf(file, sizeof(file), "%s%svnodes.json.bak", pMgmt->path, TD_DIRSEP); snprintf(file, sizeof(file), "%s%svnodes.json.bak", pMgmt->path, TD_DIRSEP);
snprintf(realfile, sizeof(realfile), "%s%svnodes.json", pMgmt->path, TD_DIRSEP); snprintf(realfile, sizeof(realfile), "%s%svnodes.json", pMgmt->path, TD_DIRSEP);
pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
if (pFile == NULL) goto _OVER;
int32_t numOfVnodes = 0; int32_t numOfVnodes = 0;
ppVnodes = vmGetVnodeListFromHash(pMgmt, &numOfVnodes); ppVnodes = vmGetVnodeListFromHash(pMgmt, &numOfVnodes);
if (ppVnodes == NULL) goto _OVER; if (ppVnodes == NULL) goto _OVER;
...@@ -187,15 +184,18 @@ int32_t vmWriteVnodeListToFile(SVnodeMgmt *pMgmt) { ...@@ -187,15 +184,18 @@ int32_t vmWriteVnodeListToFile(SVnodeMgmt *pMgmt) {
pJson = tjsonCreateObject(); pJson = tjsonCreateObject();
if (pJson == NULL) goto _OVER; if (pJson == NULL) goto _OVER;
if (vmEncodeVnodeList(pJson, ppVnodes, numOfVnodes) != 0) goto _OVER; if (vmEncodeVnodeList(pJson, ppVnodes, numOfVnodes) != 0) goto _OVER;
buffer = tjsonToString(pJson); buffer = tjsonToString(pJson);
if (buffer == NULL) goto _OVER; if (buffer == NULL) goto _OVER;
terrno = 0;
pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
if (pFile == NULL) goto _OVER;
int32_t len = strlen(buffer); int32_t len = strlen(buffer);
if (taosWriteFile(pFile, buffer, len) <= 0) goto _OVER; if (taosWriteFile(pFile, buffer, len) <= 0) goto _OVER;
if (taosFsyncFile(pFile) < 0) goto _OVER; if (taosFsyncFile(pFile) < 0) goto _OVER;
taosCloseFile(&pFile);
taosCloseFile(&pFile);
if (taosRenameFile(file, realfile) != 0) goto _OVER; if (taosRenameFile(file, realfile) != 0) goto _OVER;
code = 0; code = 0;
...@@ -216,6 +216,7 @@ _OVER: ...@@ -216,6 +216,7 @@ _OVER:
} }
if (code != 0) { 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); dError("failed to write vnodes file:%s since %s, vnodes:%d", realfile, terrstr(), numOfVnodes);
} }
return code; return code;
......
...@@ -225,15 +225,15 @@ int32_t dmWriteEps(SDnodeData *pData) { ...@@ -225,15 +225,15 @@ int32_t dmWriteEps(SDnodeData *pData) {
pJson = tjsonCreateObject(); pJson = tjsonCreateObject();
if (pJson == NULL) goto _OVER; if (pJson == NULL) goto _OVER;
if (dmEncodeEps(pJson, pData) != 0) goto _OVER; if (dmEncodeEps(pJson, pData) != 0) goto _OVER;
buffer = tjsonToString(pJson); buffer = tjsonToString(pJson);
if (buffer == NULL) goto _OVER; if (buffer == NULL) goto _OVER;
terrno = 0;
int32_t len = strlen(buffer); int32_t len = strlen(buffer);
if (taosWriteFile(pFile, buffer, len) <= 0) goto _OVER; if (taosWriteFile(pFile, buffer, len) <= 0) goto _OVER;
if (taosFsyncFile(pFile) < 0) goto _OVER; if (taosFsyncFile(pFile) < 0) goto _OVER;
taosCloseFile(&pFile);
taosCloseFile(&pFile);
if (taosRenameFile(file, realfile) != 0) goto _OVER; if (taosRenameFile(file, realfile) != 0) goto _OVER;
code = 0; code = 0;
...@@ -246,6 +246,7 @@ _OVER: ...@@ -246,6 +246,7 @@ _OVER:
if (pFile != NULL) taosCloseFile(&pFile); if (pFile != NULL) taosCloseFile(&pFile);
if (code != 0) { if (code != 0) {
if (terrno == 0) terrno = TAOS_SYSTEM_ERROR(errno);
dInfo("succeed to write dnode file:%s since %s, dnodeVer:%" PRId64, realfile, terrstr(), pData->dnodeVer); dInfo("succeed to write dnode file:%s since %s, dnodeVer:%" PRId64, realfile, terrstr(), pData->dnodeVer);
} }
return code; return code;
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#define _DEFAULT_SOURCE #define _DEFAULT_SOURCE
#include "dmUtil.h" #include "dmUtil.h"
#include "tjson.h"
#define MAXLEN 1024 #define MAXLEN 1024
...@@ -63,56 +64,51 @@ _OVER: ...@@ -63,56 +64,51 @@ _OVER:
return code; return code;
} }
static int32_t dmEncodeFile(SJson *pJson, bool deployed) {
if (tjsonAddDoubleToObject(pJson, "deployed", deployed) < 0) return -1;
return 0;
}
int32_t dmWriteFile(const char *path, const char *name, bool deployed) { int32_t dmWriteFile(const char *path, const char *name, bool deployed) {
int32_t code = -1; int32_t code = -1;
int32_t len = 0; char *buffer = NULL;
char content[MAXLEN + 1] = {0}; SJson *pJson = NULL;
TdFilePtr pFile = NULL;
char file[PATH_MAX] = {0}; char file[PATH_MAX] = {0};
char realfile[PATH_MAX] = {0}; char realfile[PATH_MAX] = {0};
TdFilePtr pFile = NULL;
snprintf(file, sizeof(file), "%s%s%s.json", path, TD_DIRSEP, name); snprintf(file, sizeof(file), "%s%s%s.json", path, TD_DIRSEP, name);
snprintf(realfile, sizeof(realfile), "%s%s%s.json", path, TD_DIRSEP, name); snprintf(realfile, sizeof(realfile), "%s%s%s.json", path, TD_DIRSEP, name);
pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC); terrno = TSDB_CODE_OUT_OF_MEMORY;
if (pFile == NULL) { pJson = tjsonCreateObject();
terrno = TAOS_SYSTEM_ERROR(errno); if (pJson == NULL) goto _OVER;
dError("failed to write %s since %s", file, terrstr()); if (dmEncodeFile(pJson, deployed) != 0) goto _OVER;
goto _OVER; buffer = tjsonToString(pJson);
} if (buffer == NULL) goto _OVER;
terrno = 0;
len += snprintf(content + len, MAXLEN - len, "{\n");
len += snprintf(content + len, MAXLEN - len, " \"deployed\": %d\n", deployed);
len += snprintf(content + len, MAXLEN - len, "}\n");
if (taosWriteFile(pFile, content, len) != len) { pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
terrno = TAOS_SYSTEM_ERROR(errno); if (pFile == NULL) goto _OVER;
dError("failed to write file:%s since %s", file, terrstr());
goto _OVER;
}
if (taosFsyncFile(pFile) != 0) { int32_t len = strlen(buffer);
terrno = TAOS_SYSTEM_ERROR(errno); if (taosWriteFile(pFile, buffer, len) <= 0) goto _OVER;
dError("failed to fsync file:%s since %s", file, terrstr()); if (taosFsyncFile(pFile) < 0) goto _OVER;
goto _OVER;
}
taosCloseFile(&pFile); taosCloseFile(&pFile);
if (taosRenameFile(file, realfile) != 0) goto _OVER;
if (taosRenameFile(file, realfile) != 0) {
terrno = TAOS_SYSTEM_ERROR(errno);
dError("failed to rename %s since %s", file, terrstr());
return -1;
}
dInfo("succeed to write %s, deployed:%d", realfile, deployed);
code = 0; code = 0;
dInfo("succeed to write file:%s, deloyed:%d", realfile, deployed);
_OVER: _OVER:
if (pFile != NULL) { if (pJson != NULL) tjsonDelete(pJson);
taosCloseFile(&pFile); if (buffer != NULL) taosMemoryFree(buffer);
} if (pFile != NULL) taosCloseFile(&pFile);
if (code != 0) {
if (terrno == 0) terrno = TAOS_SYSTEM_ERROR(errno);
dError("failed to write file:%s since %s, deloyed:%d", realfile, terrstr(), deployed);
}
return code; return code;
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册