提交 9d600dfa 编写于 作者: S Shengliang Guan

enh: dump sdb.data to json

上级 38394c18
......@@ -23,6 +23,8 @@ target_link_libraries(
PUBLIC common
PUBLIC os
)
IF (TD_LINUX)
target_link_libraries(
sdbDump
PUBLIC dnode
......@@ -37,3 +39,4 @@ target_include_directories(
PRIVATE "${TD_SOURCE_DIR}/source/dnode/mnode/sdb/inc"
PRIVATE "${TD_SOURCE_DIR}/source/dnode/mgmt/node_mgmt/inc"
)
ENDIF ()
\ No newline at end of file
......@@ -15,10 +15,200 @@
#define _DEFAULT_SOURCE
#include "dmMgmt.h"
#include "mndInt.h"
#include "sdbInt.h"
#include "tconfig.h"
#include "tjson.h"
int32_t main(int32_t argc, char *argv[]) {
char datafile[PATH_MAX] = {0};
#define TMP_SDB_DATA_DIR "/tmp/dumpsdb"
#define TMP_SDB_MNODE_DIR "/tmp/dumpsdb/mnode"
#define TMP_SDB_FILE "/tmp/dumpsdb/mnode/data/sdb.data"
#define TMP_SDB_PATH "/tmp/dumpsdb/mnode/data"
void reportStartup(const char *name, const char *desc) {}
void sendRsp(SRpcMsg *pMsg) { rpcFreeCont(pMsg->pCont); }
int32_t sendReq(const SEpSet *pEpSet, SRpcMsg *pMsg) {
terrno = TSDB_CODE_INVALID_PTR;
return -1;
}
char *i642str(int64_t val) {
static char str[24] = {0};
snprintf(str, sizeof(str), "%" PRId64, val);
return str;
}
void dumpFunc(SSdb *pSdb, SJson *json) {}
void dumpDb(SSdb *pSdb, SJson *json) {}
void dumpStb(SSdb *pSdb, SJson *json) {}
void dumpSma(SSdb *pSdb, SJson *json) {}
void dumpVgroup(SSdb *pSdb, SJson *json) {}
void dumpTopic(SSdb *pSdb, SJson *json) {}
void dumpConsumber(SSdb *pSdb, SJson *json) {}
void dumpSubscribe(SSdb *pSdb, SJson *json) {}
void dumpOffset(SSdb *pSdb, SJson *json) {}
void dumpStream(SSdb *pSdb, SJson *json) {}
void dumpAcct(SSdb *pSdb, SJson *json) {}
void dumpAuth(SSdb *pSdb, SJson *json) {}
void dumpUser(SSdb *pSdb, SJson *json) {
void *pIter = NULL;
SJson *items = tjsonCreateObject();
tjsonAddItemToObject(json, "users", items);
while (1) {
SUserObj *pObj = NULL;
pIter = sdbFetch(pSdb, SDB_USER, pIter, (void **)&pObj);
if (pIter == NULL) break;
SJson *item = tjsonCreateObject();
tjsonAddItemToObject(items, "user", item);
tjsonAddStringToObject(item, "name", pObj->user);
tjsonAddStringToObject(item, "pass", pObj->pass);
tjsonAddStringToObject(item, "acct", pObj->acct);
tjsonAddStringToObject(item, "createdTime", i642str(pObj->createdTime));
tjsonAddStringToObject(item, "updateTime", i642str(pObj->updateTime));
tjsonAddIntegerToObject(item, "superUser", pObj->superUser);
tjsonAddIntegerToObject(item, "authVersion", pObj->authVersion);
tjsonAddIntegerToObject(item, "numOfReadDbs", taosHashGetSize(pObj->readDbs));
tjsonAddIntegerToObject(item, "numOfWriteDbs", taosHashGetSize(pObj->writeDbs));
sdbRelease(pSdb, pObj);
}
}
void dumpDnode(SSdb *pSdb, SJson *json) {
void *pIter = NULL;
SJson *items = tjsonCreateObject();
tjsonAddItemToObject(json, "dnodes", items);
while (1) {
SDnodeObj *pObj = NULL;
pIter = sdbFetch(pSdb, SDB_DNODE, pIter, (void **)&pObj);
if (pIter == NULL) break;
SJson *item = tjsonCreateObject();
tjsonAddItemToObject(items, "dnode", item);
tjsonAddIntegerToObject(item, "id", pObj->id);
tjsonAddStringToObject(item, "createdTime", i642str(pObj->createdTime));
tjsonAddStringToObject(item, "updateTime", i642str(pObj->updateTime));
tjsonAddIntegerToObject(item, "port", pObj->port);
tjsonAddStringToObject(item, "fqdn", pObj->fqdn);
sdbRelease(pSdb, pObj);
}
}
void dumpBnode(SSdb *pSdb, SJson *json) {}
void dumpSnode(SSdb *pSdb, SJson *json) {}
void dumpQnode(SSdb *pSdb, SJson *json) {}
void dumpMnode(SSdb *pSdb, SJson *json) {}
void dumpCluster(SSdb *pSdb, SJson *json) {}
void dumpTrans(SSdb *pSdb, SJson *json) {}
void dumpHeader(SSdb *pSdb, SJson *json) {
tjsonAddIntegerToObject(json, "sver", 1);
tjsonAddStringToObject(json, "curVer", i642str(pSdb->curVer));
SJson *maxIdsJson = tjsonCreateObject();
tjsonAddItemToObject(json, "maxIds", maxIdsJson);
for (int32_t i = 0; i < SDB_MAX; ++i) {
int64_t maxId = 0;
if (i < SDB_MAX) {
maxId = pSdb->maxId[i];
}
tjsonAddStringToObject(maxIdsJson, sdbTableName(i), i642str(maxId));
}
SJson *tableVersJson = tjsonCreateObject();
tjsonAddItemToObject(json, "tableVers", tableVersJson);
for (int32_t i = 0; i < SDB_MAX; ++i) {
int64_t tableVer = 0;
if (i < SDB_MAX) {
tableVer = pSdb->tableVer[i];
}
tjsonAddStringToObject(tableVersJson, sdbTableName(i), i642str(tableVer));
}
}
int32_t dumpSdb() {
SMsgCb msgCb = {0};
msgCb.reportStartupFp = reportStartup;
msgCb.sendReqFp = sendReq;
msgCb.sendRspFp = sendRsp;
msgCb.mgmt = (SMgmtWrapper *)(&msgCb); // hack
tmsgSetDefault(&msgCb);
walInit();
SMnodeOpt opt = {.msgCb = msgCb};
SMnode *pMnode = mndOpen(TMP_SDB_MNODE_DIR, &opt);
if (pMnode == NULL) return -1;
SSdb *pSdb = pMnode->pSdb;
SJson *json = tjsonCreateObject();
dumpHeader(pSdb, json);
dumpFunc(pSdb, json);
dumpDb(pSdb, json);
dumpStb(pSdb, json);
dumpSma(pSdb, json);
dumpVgroup(pSdb, json);
dumpTopic(pSdb, json);
dumpConsumber(pSdb, json);
dumpSubscribe(pSdb, json);
dumpOffset(pSdb, json);
dumpStream(pSdb, json);
dumpAcct(pSdb, json);
dumpAuth(pSdb, json);
dumpUser(pSdb, json);
dumpDnode(pSdb, json);
dumpBnode(pSdb, json);
dumpSnode(pSdb, json);
dumpQnode(pSdb, json);
dumpMnode(pSdb, json);
dumpCluster(pSdb, json);
dumpTrans(pSdb, json);
char *pCont = tjsonToString(json);
int32_t contLen = strlen(pCont);
char file[] = "sdb.json";
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;
}
taosWriteFile(pFile, pCont, contLen);
taosWriteFile(pFile, "\n", 1);
taosFsyncFile(pFile);
taosCloseFile(&pFile);
tjsonDelete(json);
taosMemoryFree(pCont);
taosRemoveDir(TMP_SDB_DATA_DIR);
return 0;
}
int32_t parseArgs(int32_t argc, char *argv[]) {
char file[PATH_MAX] = {0};
for (int32_t i = 1; i < argc; ++i) {
if (strcmp(argv[i], "-c") == 0) {
......@@ -38,11 +228,15 @@ int32_t main(int32_t argc, char *argv[]) {
printf("file path overflow");
return -1;
}
tstrncpy(datafile, argv[i], PATH_MAX);
tstrncpy(file, argv[i], PATH_MAX);
} else {
printf("'-f' requires a parameter, default is %s\n", configDir);
return -1;
}
} else {
printf("-c Configuration directory. \n");
printf("-f Input sdb.data file. \n");
return -1;
}
}
......@@ -52,19 +246,24 @@ int32_t main(int32_t argc, char *argv[]) {
}
if (taosInitCfg(configDir, NULL, NULL, NULL, NULL, 0) != 0) {
uError("failed to dump since read config error");
taosCloseLog();
printf("failed to dump since read config error\n");
return -1;
}
if (datafile[0] == 0) {
snprintf(datafile, sizeof(datafile), "%s%sdata%smnode%sdata%ssdb.data", tsDataDir, TD_DIRSEP, TD_DIRSEP, TD_DIRSEP,
TD_DIRSEP);
if (file[0] == 0) {
snprintf(file, PATH_MAX, "%s/mnode/data/sdb.data", tsDataDir);
}
dInfo("dump %s to sdb.json", datafile);
taosCleanupCfg();
taosCloseLog();
strcpy(tsDataDir, TMP_SDB_DATA_DIR);
taosMulMkDir(TMP_SDB_PATH);
taosCopyFile(file, TMP_SDB_FILE);
return 0;
}
int32_t main(int32_t argc, char *argv[]) {
if (parseArgs(argc, argv) != 0) {
return -1;
}
return dumpSdb();
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册