未验证 提交 10e7d88e 编写于 作者: S Shengliang Guan 提交者: GitHub

Merge pull request #19418 from taosdata/fix/TD-21789

fix: handle error while write vnodes.json
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#define _DEFAULT_SOURCE #define _DEFAULT_SOURCE
#include "mmInt.h" #include "mmInt.h"
#include "tjson.h"
int32_t mmReadFile(const char *path, SMnodeOpt *pOption) { int32_t mmReadFile(const char *path, SMnodeOpt *pOption) {
int32_t code = TSDB_CODE_INVALID_JSON_FORMAT; int32_t code = TSDB_CODE_INVALID_JSON_FORMAT;
...@@ -130,56 +131,70 @@ _OVER: ...@@ -130,56 +131,70 @@ _OVER:
return code; return code;
} }
int32_t mmWriteFile(const char *path, const SMnodeOpt *pOption) { static int32_t mmEncodeOption(SJson *pJson, const SMnodeOpt *pOption) {
char file[PATH_MAX] = {0};
char realfile[PATH_MAX] = {0};
snprintf(file, sizeof(file), "%s%smnode.json.bak", path, TD_DIRSEP);
snprintf(realfile, sizeof(realfile), "%s%smnode.json", path, TD_DIRSEP);
TdFilePtr pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
if (pFile == NULL) {
terrno = TAOS_SYSTEM_ERROR(errno);
dError("failed to write %s since %s", file, terrstr());
return -1;
}
int32_t len = 0;
int32_t maxLen = 4096;
char *content = taosMemoryCalloc(1, maxLen + 1);
len += snprintf(content + len, maxLen - len, "{\n");
if (pOption->deploy && pOption->numOfReplicas > 0) { if (pOption->deploy && pOption->numOfReplicas > 0) {
len += snprintf(content + len, maxLen - len, " \"selfIndex\": %d,\n", pOption->selfIndex); if (tjsonAddDoubleToObject(pJson, "selfIndex", pOption->selfIndex) < 0) return -1;
len += snprintf(content + len, maxLen - len, " \"replicas\": [{\n");
SJson *replicas = tjsonCreateArray();
if (replicas == NULL) return -1;
if (tjsonAddItemToObject(pJson, "replicas", replicas) < 0) return -1;
for (int32_t i = 0; i < pOption->numOfReplicas; ++i) { for (int32_t i = 0; i < pOption->numOfReplicas; ++i) {
SJson *replica = tjsonCreateObject();
if (replica == NULL) return -1;
const SReplica *pReplica = pOption->replicas + i; const SReplica *pReplica = pOption->replicas + i;
if (pReplica != NULL && pReplica->id > 0) { if (tjsonAddDoubleToObject(replica, "id", pReplica->id) < 0) return -1;
len += snprintf(content + len, maxLen - len, " \"id\": %d,\n", pReplica->id); if (tjsonAddStringToObject(replica, "fqdn", pReplica->fqdn) < 0) return -1;
len += snprintf(content + len, maxLen - len, " \"fqdn\": \"%s\",\n", pReplica->fqdn); if (tjsonAddDoubleToObject(replica, "port", pReplica->port) < 0) return -1;
len += snprintf(content + len, maxLen - len, " \"port\": %u\n", pReplica->port); if (tjsonAddItemToArray(replicas, replica) < 0) return -1;
}
if (i < pOption->numOfReplicas - 1) {
len += snprintf(content + len, maxLen - len, " },{\n");
} else {
len += snprintf(content + len, maxLen - len, " }],\n");
}
} }
} }
len += snprintf(content + len, maxLen - len, " \"deployed\": %d\n", pOption->deploy);
len += snprintf(content + len, maxLen - len, "}\n");
taosWriteFile(pFile, content, len); if (tjsonAddDoubleToObject(pJson, "deployed", pOption->deploy) < 0) return -1;
taosFsyncFile(pFile);
return 0;
}
int32_t mmWriteFile(const char *path, const SMnodeOpt *pOption) {
int32_t code = -1;
char *buffer = NULL;
SJson *pJson = NULL;
TdFilePtr pFile = NULL;
char file[PATH_MAX] = {0};
char realfile[PATH_MAX] = {0};
snprintf(file, sizeof(file), "%s%smnode.json.bak", path, TD_DIRSEP);
snprintf(realfile, sizeof(realfile), "%s%smnode.json", path, TD_DIRSEP);
terrno = TSDB_CODE_OUT_OF_MEMORY;
pJson = tjsonCreateObject();
if (pJson == NULL) goto _OVER;
if (mmEncodeOption(pJson, pOption) != 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;
int32_t len = strlen(buffer);
if (taosWriteFile(pFile, buffer, len) <= 0) goto _OVER;
if (taosFsyncFile(pFile) < 0) goto _OVER;
taosCloseFile(&pFile); taosCloseFile(&pFile);
taosMemoryFree(content); if (taosRenameFile(file, realfile) != 0) goto _OVER;
if (taosRenameFile(file, realfile) != 0) { code = 0;
terrno = TAOS_SYSTEM_ERROR(errno); dInfo("succeed to write mnode file:%s, deloyed:%d", realfile, pOption->deploy);
dError("failed to rename %s since %s", file, terrstr());
return -1;
}
dDebug("succeed to write %s, deployed:%d", realfile, pOption->deploy); _OVER:
return 0; if (pJson != NULL) tjsonDelete(pJson);
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 mnode file:%s since %s, deloyed:%d", realfile, terrstr(), pOption->deploy);
}
return code;
} }
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#define _DEFAULT_SOURCE #define _DEFAULT_SOURCE
#include "vmInt.h" #include "vmInt.h"
#include "tjson.h"
#define MAX_CONTENT_LEN 2 * 1024 * 1024 #define MAX_CONTENT_LEN 2 * 1024 * 1024
...@@ -144,65 +145,66 @@ _OVER: ...@@ -144,65 +145,66 @@ _OVER:
return code; return code;
} }
int32_t vmWriteVnodeListToFile(SVnodeMgmt *pMgmt) { static int32_t vmEncodeVnodeList(SJson *pJson, SVnodeObj **ppVnodes, int32_t numOfVnodes) {
int32_t code = 0; SJson *vnodes = tjsonCreateArray();
char file[PATH_MAX] = {0}; if (vnodes == NULL) return -1;
char realfile[PATH_MAX] = {0}; if (tjsonAddItemToObject(pJson, "vnodes", vnodes) < 0) return -1;
snprintf(file, sizeof(file), "%s%svnodes.json.bak", pMgmt->path, TD_DIRSEP);
snprintf(realfile, sizeof(file), "%s%svnodes.json", pMgmt->path, TD_DIRSEP);
TdFilePtr pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
if (pFile == NULL) {
terrno = TAOS_SYSTEM_ERROR(errno);
dError("failed to write %s since %s", file, terrstr());
return -1;
}
int32_t numOfVnodes = 0;
SVnodeObj **ppVnodes = vmGetVnodeListFromHash(pMgmt, &numOfVnodes);
if (ppVnodes == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
code = -1;
dError("failed to write %s while get vnodelist", file);
goto _OVER;
}
int32_t len = 0;
int32_t maxLen = MAX_CONTENT_LEN;
char *content = taosMemoryCalloc(1, maxLen + 1);
if (content == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
code = -1;
dError("failed to write %s while malloc content", file);
goto _OVER;
}
len += snprintf(content + len, maxLen - len, "{\n");
len += snprintf(content + len, maxLen - len, " \"vnodes\": [\n");
for (int32_t i = 0; i < numOfVnodes; ++i) { for (int32_t i = 0; i < numOfVnodes; ++i) {
SVnodeObj *pVnode = ppVnodes[i]; SVnodeObj *pVnode = ppVnodes[i];
if (pVnode == NULL) continue; if (pVnode == NULL) continue;
len += snprintf(content + len, maxLen - len, " {\n"); SJson *vnode = tjsonCreateObject();
len += snprintf(content + len, maxLen - len, " \"vgId\": %d,\n", pVnode->vgId); if (vnode == NULL) return -1;
len += snprintf(content + len, maxLen - len, " \"dropped\": %d,\n", pVnode->dropped); if (tjsonAddDoubleToObject(vnode, "vgId", pVnode->vgId) < 0) return -1;
len += snprintf(content + len, maxLen - len, " \"vgVersion\": %d\n", pVnode->vgVersion); if (tjsonAddDoubleToObject(vnode, "dropped", pVnode->dropped) < 0) return -1;
if (i < numOfVnodes - 1) { if (tjsonAddDoubleToObject(vnode, "vgVersion", pVnode->vgVersion) < 0) return -1;
len += snprintf(content + len, maxLen - len, " },\n"); if (tjsonAddItemToArray(vnodes, vnode) < 0) return -1;
} else {
len += snprintf(content + len, maxLen - len, " }\n");
}
} }
len += snprintf(content + len, maxLen - len, " ]\n");
len += snprintf(content + len, maxLen - len, "}\n"); return 0;
}
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.json.bak", pMgmt->path, TD_DIRSEP);
snprintf(realfile, sizeof(realfile), "%s%svnodes.json", pMgmt->path, TD_DIRSEP);
int32_t numOfVnodes = 0;
ppVnodes = vmGetVnodeListFromHash(pMgmt, &numOfVnodes);
if (ppVnodes == NULL) goto _OVER;
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; terrno = 0;
_OVER: pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
taosWriteFile(pFile, content, len); if (pFile == NULL) goto _OVER;
taosFsyncFile(pFile);
int32_t len = strlen(buffer);
if (taosWriteFile(pFile, buffer, len) <= 0) goto _OVER;
if (taosFsyncFile(pFile) < 0) goto _OVER;
taosCloseFile(&pFile); taosCloseFile(&pFile);
taosMemoryFree(content); 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);
if (ppVnodes != NULL) { if (ppVnodes != NULL) {
for (int32_t i = 0; i < numOfVnodes; ++i) { for (int32_t i = 0; i < numOfVnodes; ++i) {
SVnodeObj *pVnode = ppVnodes[i]; SVnodeObj *pVnode = ppVnodes[i];
...@@ -213,14 +215,9 @@ _OVER: ...@@ -213,14 +215,9 @@ _OVER:
taosMemoryFree(ppVnodes); taosMemoryFree(ppVnodes);
} }
if (code != 0) return -1;
dInfo("succeed to write %s, numOfVnodes:%d", realfile, numOfVnodes);
code = taosRenameFile(file, realfile);
if (code != 0) { if (code != 0) {
dError("failed to rename %s to %s", file, realfile); if (terrno == 0) terrno = TAOS_SYSTEM_ERROR(errno);
dError("failed to write vnodes file:%s since %s, vnodes:%d", realfile, terrstr(), numOfVnodes);
} }
return code; return code;
} }
\ No newline at end of file
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#define _DEFAULT_SOURCE #define _DEFAULT_SOURCE
#include "dmUtil.h" #include "dmUtil.h"
#include "tjson.h"
#include "tmisce.h" #include "tmisce.h"
static void dmPrintEps(SDnodeData *pData); static void dmPrintEps(SDnodeData *pData);
...@@ -181,81 +182,73 @@ _OVER: ...@@ -181,81 +182,73 @@ _OVER:
return code; return code;
} }
int32_t dmWriteEps(SDnodeData *pData) { static int32_t dmEncodeEps(SJson *pJson, SDnodeData *pData) {
int32_t code = -1; if (tjsonAddDoubleToObject(pJson, "dnodeId", pData->dnodeId) < 0) return -1;
char *content = NULL; if (tjsonAddIntegerToObject(pJson, "dnodeVer", pData->dnodeVer) < 0) return -1;
TdFilePtr pFile = NULL; if (tjsonAddIntegerToObject(pJson, "clusterId", pData->clusterId) < 0) return -1;
if (tjsonAddDoubleToObject(pJson, "dropped", pData->dropped) < 0) return -1;
char file[PATH_MAX] = {0}; SJson *dnodes = tjsonCreateArray();
char realfile[PATH_MAX] = {0}; if (dnodes == NULL) return -1;
snprintf(file, sizeof(file), "%s%sdnode%sdnode.json.bak", tsDataDir, TD_DIRSEP, TD_DIRSEP); if (tjsonAddItemToObject(pJson, "dnodes", dnodes) < 0) return -1;
snprintf(realfile, sizeof(realfile), "%s%sdnode%sdnode.json", tsDataDir, TD_DIRSEP, TD_DIRSEP);
pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
if (pFile == NULL) {
dError("failed to open %s since %s", file, strerror(errno));
terrno = TAOS_SYSTEM_ERROR(errno);
goto _OVER;
}
int32_t len = 0;
int32_t maxLen = 256 * 1024;
content = taosMemoryCalloc(1, maxLen + 1);
len += snprintf(content + len, maxLen - len, "{\n");
len += snprintf(content + len, maxLen - len, " \"dnodeId\": %d,\n", pData->dnodeId);
len += snprintf(content + len, maxLen - len, " \"dnodeVer\": \"%" PRId64 "\",\n", pData->dnodeVer);
len += snprintf(content + len, maxLen - len, " \"clusterId\": \"%" PRId64 "\",\n", pData->clusterId);
len += snprintf(content + len, maxLen - len, " \"dropped\": %d,\n", pData->dropped);
len += snprintf(content + len, maxLen - len, " \"dnodes\": [{\n");
int32_t numOfEps = (int32_t)taosArrayGetSize(pData->dnodeEps); int32_t numOfEps = (int32_t)taosArrayGetSize(pData->dnodeEps);
for (int32_t i = 0; i < numOfEps; ++i) { for (int32_t i = 0; i < numOfEps; ++i) {
SDnodeEp *pDnodeEp = taosArrayGet(pData->dnodeEps, i); SDnodeEp *pDnodeEp = taosArrayGet(pData->dnodeEps, i);
len += snprintf(content + len, maxLen - len, " \"id\": %d,\n", pDnodeEp->id); SJson *dnode = tjsonCreateObject();
len += snprintf(content + len, maxLen - len, " \"fqdn\": \"%s\",\n", pDnodeEp->ep.fqdn); if (dnode == NULL) return -1;
len += snprintf(content + len, maxLen - len, " \"port\": %u,\n", pDnodeEp->ep.port);
len += snprintf(content + len, maxLen - len, " \"isMnode\": %d\n", pDnodeEp->isMnode); if (tjsonAddDoubleToObject(dnode, "id", pDnodeEp->id) < 0) return -1;
if (i < numOfEps - 1) { if (tjsonAddStringToObject(dnode, "fqdn", pDnodeEp->ep.fqdn) < 0) return -1;
len += snprintf(content + len, maxLen - len, " },{\n"); if (tjsonAddDoubleToObject(dnode, "port", pDnodeEp->ep.port) < 0) return -1;
} else { if (tjsonAddDoubleToObject(dnode, "isMnode", pDnodeEp->isMnode) < 0) return -1;
len += snprintf(content + len, maxLen - len, " }]\n"); if (tjsonAddItemToArray(dnodes, dnode) < 0) return -1;
}
} }
len += snprintf(content + len, maxLen - len, "}\n");
if (taosWriteFile(pFile, content, len) != len) { return 0;
dError("failed to write %s since %s", file, strerror(errno)); }
terrno = TAOS_SYSTEM_ERROR(errno);
goto _OVER;
}
if (taosFsyncFile(pFile) < 0) { int32_t dmWriteEps(SDnodeData *pData) {
dError("failed to fsync %s since %s", file, strerror(errno)); int32_t code = -1;
terrno = TAOS_SYSTEM_ERROR(errno); char *buffer = NULL;
goto _OVER; SJson *pJson = NULL;
} TdFilePtr pFile = NULL;
char file[PATH_MAX] = {0};
char realfile[PATH_MAX] = {0};
snprintf(file, sizeof(file), "%s%sdnode%sdnode.json.bak", tsDataDir, TD_DIRSEP, TD_DIRSEP);
snprintf(realfile, sizeof(realfile), "%s%sdnode%sdnode.json", tsDataDir, TD_DIRSEP, TD_DIRSEP);
taosCloseFile(&pFile); terrno = TSDB_CODE_OUT_OF_MEMORY;
taosMemoryFreeClear(content); pJson = tjsonCreateObject();
if (pJson == NULL) goto _OVER;
if (dmEncodeEps(pJson, pData) != 0) goto _OVER;
buffer = tjsonToString(pJson);
if (buffer == NULL) goto _OVER;
terrno = 0;
if (taosRenameFile(file, realfile) != 0) { 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 rename %s since %s", file, terrstr());
goto _OVER; int32_t len = strlen(buffer);
} if (taosWriteFile(pFile, buffer, len) <= 0) goto _OVER;
if (taosFsyncFile(pFile) < 0) goto _OVER;
taosCloseFile(&pFile);
if (taosRenameFile(file, realfile) != 0) goto _OVER;
code = 0; code = 0;
pData->updateTime = taosGetTimestampMs(); pData->updateTime = taosGetTimestampMs();
dInfo("succeed to write %s, dnodeVer:%" PRId64, realfile, pData->dnodeVer); dInfo("succeed to write dnode file:%s, dnodeVer:%" PRId64, realfile, pData->dnodeVer);
_OVER: _OVER:
if (content != NULL) taosMemoryFreeClear(content); if (pJson != NULL) tjsonDelete(pJson);
if (buffer != NULL) taosMemoryFree(buffer);
if (pFile != NULL) taosCloseFile(&pFile); if (pFile != NULL) taosCloseFile(&pFile);
if (code != 0) { if (code != 0) {
dError("failed to write file %s since %s", realfile, terrstr()); if (terrno == 0) terrno = TAOS_SYSTEM_ERROR(errno);
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;
} }
......
...@@ -636,15 +636,20 @@ int32_t sdbStartWrite(SSdb *pSdb, SSdbIter **ppIter) { ...@@ -636,15 +636,20 @@ int32_t sdbStartWrite(SSdb *pSdb, SSdbIter **ppIter) {
} }
int32_t sdbStopWrite(SSdb *pSdb, SSdbIter *pIter, bool isApply, int64_t index, int64_t term, int64_t config) { int32_t sdbStopWrite(SSdb *pSdb, SSdbIter *pIter, bool isApply, int64_t index, int64_t term, int64_t config) {
int32_t code = 0; int32_t code = -1;
if (!isApply) { if (!isApply) {
mInfo("sdbiter:%p, not apply to sdb", pIter); mInfo("sdbiter:%p, not apply to sdb", pIter);
sdbCloseIter(pIter); code = 0;
return 0; goto _OVER;
}
if (taosFsyncFile(pIter->file) != 0) {
terrno = TAOS_SYSTEM_ERROR(errno);
mError("sdbiter:%p, failed to fasync file %s since %s", pIter, pIter->name, terrstr());
goto _OVER;
} }
taosFsyncFile(pIter->file);
taosCloseFile(&pIter->file); taosCloseFile(&pIter->file);
pIter->file = NULL; pIter->file = NULL;
...@@ -653,14 +658,12 @@ int32_t sdbStopWrite(SSdb *pSdb, SSdbIter *pIter, bool isApply, int64_t index, i ...@@ -653,14 +658,12 @@ int32_t sdbStopWrite(SSdb *pSdb, SSdbIter *pIter, bool isApply, int64_t index, i
if (taosRenameFile(pIter->name, datafile) != 0) { if (taosRenameFile(pIter->name, datafile) != 0) {
terrno = TAOS_SYSTEM_ERROR(errno); terrno = TAOS_SYSTEM_ERROR(errno);
mError("sdbiter:%p, failed to rename file %s to %s since %s", pIter, pIter->name, datafile, terrstr()); mError("sdbiter:%p, failed to rename file %s to %s since %s", pIter, pIter->name, datafile, terrstr());
sdbCloseIter(pIter); goto _OVER;
return -1;
} }
if (sdbReadFile(pSdb) != 0) { if (sdbReadFile(pSdb) != 0) {
mError("sdbiter:%p, failed to read from %s since %s", pIter, datafile, terrstr()); mError("sdbiter:%p, failed to read from %s since %s", pIter, datafile, terrstr());
sdbCloseIter(pIter); goto _OVER;
return -1;
} }
if (config > 0) { if (config > 0) {
...@@ -674,8 +677,11 @@ int32_t sdbStopWrite(SSdb *pSdb, SSdbIter *pIter, bool isApply, int64_t index, i ...@@ -674,8 +677,11 @@ int32_t sdbStopWrite(SSdb *pSdb, SSdbIter *pIter, bool isApply, int64_t index, i
} }
mInfo("sdbiter:%p, success applyed to sdb", pIter); mInfo("sdbiter:%p, success applyed to sdb", pIter);
code = 0;
_OVER:
sdbCloseIter(pIter); sdbCloseIter(pIter);
return 0; return code;
} }
int32_t sdbDoWrite(SSdb *pSdb, SSdbIter *pIter, void *pBuf, int32_t len) { int32_t sdbDoWrite(SSdb *pSdb, SSdbIter *pIter, void *pBuf, int32_t len) {
......
...@@ -71,31 +71,23 @@ int32_t syncWriteCfgFile(SSyncNode *pNode) { ...@@ -71,31 +71,23 @@ int32_t syncWriteCfgFile(SSyncNode *pNode) {
char file[PATH_MAX] = {0}; char file[PATH_MAX] = {0};
snprintf(file, sizeof(file), "%s.bak", realfile); snprintf(file, sizeof(file), "%s.bak", realfile);
pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
if (pFile == NULL) {
terrno = TAOS_SYSTEM_ERROR(errno);
sError("vgId:%d, failed to open sync cfg file:%s since %s", pNode->vgId, realfile, terrstr());
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 (tjsonAddObject(pJson, "RaftCfg", syncEncodeRaftCfg, pCfg) < 0) goto _OVER; if (tjsonAddObject(pJson, "RaftCfg", syncEncodeRaftCfg, pCfg) < 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);
if (taosRenameFile(file, realfile) != 0) { taosCloseFile(&pFile);
terrno = TAOS_SYSTEM_ERROR(errno); if (taosRenameFile(file, realfile) != 0) goto _OVER;
sError("vgId:%d, failed to rename sync cfg file:%s to %s since %s", pNode->vgId, file, realfile, terrstr());
goto _OVER;
}
code = 0; code = 0;
sInfo("vgId:%d, succeed to write sync cfg file:%s, len:%d", pNode->vgId, realfile, len); sInfo("vgId:%d, succeed to write sync cfg file:%s, len:%d", pNode->vgId, realfile, len);
...@@ -106,6 +98,7 @@ _OVER: ...@@ -106,6 +98,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);
sError("vgId:%d, failed to write sync cfg file:%s since %s", pNode->vgId, realfile, terrstr()); sError("vgId:%d, failed to write sync cfg file:%s since %s", pNode->vgId, realfile, terrstr());
} }
return code; return code;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册