Merge pull request #19418 from taosdata/fix/TD-21789
fix: handle error while write vnodes.json
This commit is contained in:
commit
10e7d88e49
|
@ -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:
|
||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int32_t mmEncodeOption(SJson *pJson, const SMnodeOpt *pOption) {
|
||||||
|
if (pOption->deploy && pOption->numOfReplicas > 0) {
|
||||||
|
if (tjsonAddDoubleToObject(pJson, "selfIndex", pOption->selfIndex) < 0) return -1;
|
||||||
|
|
||||||
|
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) {
|
||||||
|
SJson *replica = tjsonCreateObject();
|
||||||
|
if (replica == NULL) return -1;
|
||||||
|
|
||||||
|
const SReplica *pReplica = pOption->replicas + i;
|
||||||
|
if (tjsonAddDoubleToObject(replica, "id", pReplica->id) < 0) return -1;
|
||||||
|
if (tjsonAddStringToObject(replica, "fqdn", pReplica->fqdn) < 0) return -1;
|
||||||
|
if (tjsonAddDoubleToObject(replica, "port", pReplica->port) < 0) return -1;
|
||||||
|
if (tjsonAddItemToArray(replicas, replica) < 0) return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tjsonAddDoubleToObject(pJson, "deployed", pOption->deploy) < 0) return -1;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int32_t mmWriteFile(const char *path, const SMnodeOpt *pOption) {
|
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 file[PATH_MAX] = {0};
|
||||||
char realfile[PATH_MAX] = {0};
|
char realfile[PATH_MAX] = {0};
|
||||||
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);
|
||||||
|
|
||||||
TdFilePtr 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 (mmEncodeOption(pJson, pOption) != 0) goto _OVER;
|
||||||
return -1;
|
buffer = tjsonToString(pJson);
|
||||||
}
|
if (buffer == NULL) goto _OVER;
|
||||||
|
terrno = 0;
|
||||||
|
|
||||||
int32_t len = 0;
|
pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
|
||||||
int32_t maxLen = 4096;
|
if (pFile == NULL) goto _OVER;
|
||||||
char *content = taosMemoryCalloc(1, maxLen + 1);
|
|
||||||
|
|
||||||
len += snprintf(content + len, maxLen - len, "{\n");
|
int32_t len = strlen(buffer);
|
||||||
if (pOption->deploy && pOption->numOfReplicas > 0) {
|
if (taosWriteFile(pFile, buffer, len) <= 0) goto _OVER;
|
||||||
len += snprintf(content + len, maxLen - len, " \"selfIndex\": %d,\n", pOption->selfIndex);
|
if (taosFsyncFile(pFile) < 0) goto _OVER;
|
||||||
len += snprintf(content + len, maxLen - len, " \"replicas\": [{\n");
|
|
||||||
|
|
||||||
for (int32_t i = 0; i < pOption->numOfReplicas; ++i) {
|
|
||||||
const SReplica *pReplica = pOption->replicas + i;
|
|
||||||
if (pReplica != NULL && pReplica->id > 0) {
|
|
||||||
len += snprintf(content + len, maxLen - len, " \"id\": %d,\n", pReplica->id);
|
|
||||||
len += snprintf(content + len, maxLen - len, " \"fqdn\": \"%s\",\n", pReplica->fqdn);
|
|
||||||
len += snprintf(content + len, maxLen - len, " \"port\": %u\n", pReplica->port);
|
|
||||||
}
|
|
||||||
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);
|
|
||||||
taosFsyncFile(pFile);
|
|
||||||
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 @@
|
||||||
|
|
||||||
#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:
|
||||||
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");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
len += snprintf(content + len, maxLen - len, " ]\n");
|
|
||||||
len += snprintf(content + len, maxLen - len, "}\n");
|
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);
|
|
||||||
taosCloseFile(&pFile);
|
|
||||||
taosMemoryFree(content);
|
|
||||||
|
|
||||||
|
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;
|
||||||
|
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:
|
||||||
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;
|
||||||
}
|
}
|
|
@ -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:
|
||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int32_t dmEncodeEps(SJson *pJson, SDnodeData *pData) {
|
||||||
|
if (tjsonAddDoubleToObject(pJson, "dnodeId", pData->dnodeId) < 0) return -1;
|
||||||
|
if (tjsonAddIntegerToObject(pJson, "dnodeVer", pData->dnodeVer) < 0) return -1;
|
||||||
|
if (tjsonAddIntegerToObject(pJson, "clusterId", pData->clusterId) < 0) return -1;
|
||||||
|
if (tjsonAddDoubleToObject(pJson, "dropped", pData->dropped) < 0) return -1;
|
||||||
|
|
||||||
|
SJson *dnodes = tjsonCreateArray();
|
||||||
|
if (dnodes == NULL) return -1;
|
||||||
|
if (tjsonAddItemToObject(pJson, "dnodes", dnodes) < 0) return -1;
|
||||||
|
|
||||||
|
int32_t numOfEps = (int32_t)taosArrayGetSize(pData->dnodeEps);
|
||||||
|
for (int32_t i = 0; i < numOfEps; ++i) {
|
||||||
|
SDnodeEp *pDnodeEp = taosArrayGet(pData->dnodeEps, i);
|
||||||
|
SJson *dnode = tjsonCreateObject();
|
||||||
|
if (dnode == NULL) return -1;
|
||||||
|
|
||||||
|
if (tjsonAddDoubleToObject(dnode, "id", pDnodeEp->id) < 0) return -1;
|
||||||
|
if (tjsonAddStringToObject(dnode, "fqdn", pDnodeEp->ep.fqdn) < 0) return -1;
|
||||||
|
if (tjsonAddDoubleToObject(dnode, "port", pDnodeEp->ep.port) < 0) return -1;
|
||||||
|
if (tjsonAddDoubleToObject(dnode, "isMnode", pDnodeEp->isMnode) < 0) return -1;
|
||||||
|
if (tjsonAddItemToArray(dnodes, dnode) < 0) return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int32_t dmWriteEps(SDnodeData *pData) {
|
int32_t dmWriteEps(SDnodeData *pData) {
|
||||||
int32_t code = -1;
|
int32_t code = -1;
|
||||||
char *content = NULL;
|
char *buffer = NULL;
|
||||||
|
SJson *pJson = NULL;
|
||||||
TdFilePtr pFile = NULL;
|
TdFilePtr pFile = NULL;
|
||||||
|
|
||||||
char file[PATH_MAX] = {0};
|
char file[PATH_MAX] = {0};
|
||||||
char realfile[PATH_MAX] = {0};
|
char realfile[PATH_MAX] = {0};
|
||||||
snprintf(file, sizeof(file), "%s%sdnode%sdnode.json.bak", tsDataDir, TD_DIRSEP, TD_DIRSEP);
|
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);
|
snprintf(realfile, sizeof(realfile), "%s%sdnode%sdnode.json", tsDataDir, TD_DIRSEP, TD_DIRSEP);
|
||||||
|
|
||||||
|
terrno = TSDB_CODE_OUT_OF_MEMORY;
|
||||||
|
pJson = tjsonCreateObject();
|
||||||
|
if (pJson == NULL) goto _OVER;
|
||||||
|
if (dmEncodeEps(pJson, pData) != 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);
|
pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
|
||||||
if (pFile == NULL) {
|
if (pFile == NULL) goto _OVER;
|
||||||
dError("failed to open %s since %s", file, strerror(errno));
|
|
||||||
terrno = TAOS_SYSTEM_ERROR(errno);
|
|
||||||
goto _OVER;
|
|
||||||
}
|
|
||||||
|
|
||||||
int32_t len = 0;
|
int32_t len = strlen(buffer);
|
||||||
int32_t maxLen = 256 * 1024;
|
if (taosWriteFile(pFile, buffer, len) <= 0) goto _OVER;
|
||||||
content = taosMemoryCalloc(1, maxLen + 1);
|
if (taosFsyncFile(pFile) < 0) goto _OVER;
|
||||||
|
|
||||||
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);
|
|
||||||
for (int32_t i = 0; i < numOfEps; ++i) {
|
|
||||||
SDnodeEp *pDnodeEp = taosArrayGet(pData->dnodeEps, i);
|
|
||||||
len += snprintf(content + len, maxLen - len, " \"id\": %d,\n", pDnodeEp->id);
|
|
||||||
len += snprintf(content + len, maxLen - len, " \"fqdn\": \"%s\",\n", pDnodeEp->ep.fqdn);
|
|
||||||
len += snprintf(content + len, maxLen - len, " \"port\": %u,\n", pDnodeEp->ep.port);
|
|
||||||
len += snprintf(content + len, maxLen - len, " \"isMnode\": %d\n", pDnodeEp->isMnode);
|
|
||||||
if (i < numOfEps - 1) {
|
|
||||||
len += snprintf(content + len, maxLen - len, " },{\n");
|
|
||||||
} else {
|
|
||||||
len += snprintf(content + len, maxLen - len, " }]\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
len += snprintf(content + len, maxLen - len, "}\n");
|
|
||||||
|
|
||||||
if (taosWriteFile(pFile, content, len) != len) {
|
|
||||||
dError("failed to write %s since %s", file, strerror(errno));
|
|
||||||
terrno = TAOS_SYSTEM_ERROR(errno);
|
|
||||||
goto _OVER;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (taosFsyncFile(pFile) < 0) {
|
|
||||||
dError("failed to fsync %s since %s", file, strerror(errno));
|
|
||||||
terrno = TAOS_SYSTEM_ERROR(errno);
|
|
||||||
goto _OVER;
|
|
||||||
}
|
|
||||||
|
|
||||||
taosCloseFile(&pFile);
|
taosCloseFile(&pFile);
|
||||||
taosMemoryFreeClear(content);
|
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());
|
|
||||||
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) {
|
|
||||||
dError("failed to write file %s since %s", realfile, terrstr());
|
|
||||||
}
|
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
return code;
|
return code;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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:
|
||||||
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);
|
||||||
|
|
||||||
|
terrno = TSDB_CODE_OUT_OF_MEMORY;
|
||||||
|
pJson = tjsonCreateObject();
|
||||||
|
if (pJson == NULL) goto _OVER;
|
||||||
|
if (dmEncodeFile(pJson, deployed) != 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);
|
pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
|
||||||
if (pFile == NULL) {
|
if (pFile == NULL) goto _OVER;
|
||||||
terrno = TAOS_SYSTEM_ERROR(errno);
|
|
||||||
dError("failed to write %s since %s", file, terrstr());
|
|
||||||
goto _OVER;
|
|
||||||
}
|
|
||||||
|
|
||||||
len += snprintf(content + len, MAXLEN - len, "{\n");
|
int32_t len = strlen(buffer);
|
||||||
len += snprintf(content + len, MAXLEN - len, " \"deployed\": %d\n", deployed);
|
if (taosWriteFile(pFile, buffer, len) <= 0) goto _OVER;
|
||||||
len += snprintf(content + len, MAXLEN - len, "}\n");
|
if (taosFsyncFile(pFile) < 0) goto _OVER;
|
||||||
|
|
||||||
if (taosWriteFile(pFile, content, len) != len) {
|
|
||||||
terrno = TAOS_SYSTEM_ERROR(errno);
|
|
||||||
dError("failed to write file:%s since %s", file, terrstr());
|
|
||||||
goto _OVER;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (taosFsyncFile(pFile) != 0) {
|
|
||||||
terrno = TAOS_SYSTEM_ERROR(errno);
|
|
||||||
dError("failed to fsync file:%s since %s", file, terrstr());
|
|
||||||
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) {
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
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:
|
||||||
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;
|
||||||
|
|
Loading…
Reference in New Issue