From e2aa4f8af2465ea3b8c02e108bd7eddc7e47bbe5 Mon Sep 17 00:00:00 2001 From: deffpuzzl Date: Sat, 23 Jun 2018 23:52:28 +0800 Subject: [PATCH] add reboot v1.2.4 table backup function --- include/tstr.h | 2 ++ src/func.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/stvm.c | 63 ++++++++++++++++++++++++++-------- src/tree.c | 25 +++++++++++--- 4 files changed, 162 insertions(+), 20 deletions(-) diff --git a/include/tstr.h b/include/tstr.h index 09c8bd7..f61951f 100644 --- a/include/tstr.h +++ b/include/tstr.h @@ -62,6 +62,8 @@ #include #include #include +#define _GNU_SOURCE +#include typedef short int sint; typedef unsigned int uint; diff --git a/src/func.c b/src/func.c index 5544365..fc55903 100644 --- a/src/func.c +++ b/src/func.c @@ -19,12 +19,14 @@ * POSSIBILITY OF SUCH DAMAGE. */ +#define _GNU_SOURCE #include "tvm.h" /************************************************************************************************ function ************************************************************************************************/ extern void vCondInsInit(FdCond *pstCond, TABLE t); +extern long _lDumpTable(SATvm *pstSavm, TABLE t, char *pszFile); /************************************************************************************************* description:dump the unused @@ -100,6 +102,96 @@ long lUnuseDump(SATvm *pstSavm, TABLE t) return RC_SUCC; } +/************************************************************************************************* + description:exit and backup tables + parameters: + pstSavm --stvm handle + return: + RC_SUCC --success + RC_FAIL --failure + *************************************************************************************************/ +long lBackupTables(SATvm *pstSavm) +{ + size_t lOut = 0, i; + char szFile[512]; + TIndex *pstIndex = NULL; + + memset(szFile, 0, sizeof(szFile)); + if(RC_SUCC != lExportTable(SYS_TVM_INDEX, &lOut, (void *)&pstIndex)) + return RC_FAIL; + + snprintf(szFile, sizeof(szFile), "%s/backup", getenv("TVMDBD")); + if(0 != mkdir(szFile, S_IRWXU | S_IRGRP)) + { + if(EEXIST != errno) + { + vRedeError(pstSavm->m_lErrno = 127, strerror(errno)); + return RC_FAIL; + } + } + + for(i = 0; i < lOut; i ++) + { + if((TYPE_SYSTEM == pstIndex[i].m_lType || TYPE_INCORE == pstIndex[i].m_lType) && + SYS_TVM_SEQUE != pstIndex[i].m_table) + continue; + + memset(szFile, 0, sizeof(szFile)); + snprintf(szFile, sizeof(szFile), "%s/backup/%d.sdb", getenv("TVMDBD"), + pstIndex[i].m_table); + _lDumpTable(pstSavm, pstIndex[i].m_table, szFile); + } + + TFree(pstIndex); + + return RC_SUCC; +} + +/************************************************************************************************* + description:boot and restore tables + parameters: + pstSavm --stvm handle + return: + RC_SUCC --success + RC_FAIL --failure + *************************************************************************************************/ +long lRestoreTables(SATvm *pstSavm) +{ + DIR *dir; + struct dirent *pr; + char szPath[512], szFile[128]; + + memset(szPath, 0, sizeof(szPath)); + memset(szFile, 0, sizeof(szFile)); + snprintf(szPath, sizeof(szPath), "%s/backup", getenv("TVMDBD")); + if ((NULL == (dir = opendir(szPath)))) + { + vRedeError(pstSavm->m_lErrno = 127, strerror(errno)); + return RC_FAIL; + } + + while(pr = readdir(dir)) + { + if (NULL == strcasestr(pr->d_name, ".sdb")) + continue; + + if(DT_REG != pr->d_type) + continue; + + memset(szFile, 0, sizeof(szFile)); + snprintf(szFile, sizeof(szFile), "%s/%s", szPath, pr->d_name); + if(RC_SUCC != lMountTable(pstSavm, szFile)) + { + fprintf(stderr, "Warning:restore table failed, %s", + sGetTError(pstSavm->m_lErrno)); + continue; + } + } + + closedir(dir); + + return RC_SUCC; +} /**************************************************************************************** code end diff --git a/src/stvm.c b/src/stvm.c index d4fdd54..35c2026 100644 --- a/src/stvm.c +++ b/src/stvm.c @@ -38,6 +38,8 @@ extern char **environ; extern long lShutdownTvm(); extern void vSetNode(char *s); extern long lStartupTvm(TBoot *pstBoot); +extern long lBackupTables(SATvm *pstSavm); +extern long lRestoreTables(SATvm *pstSavm); extern long lMountTable(SATvm *pstSavm, char *pszFile); extern long lUnuseDump(SATvm *pstSavm, TABLE t); @@ -4019,7 +4021,8 @@ long lExecuteTvm(SATvm *pstSavm, char *pszSQL) **************************************************************************************************/ long lStartSystem(TBoot *pstBoot, char *pszMode) { - Benum eMode = 0; + long i, n; + Benum eMode = 0, eRestore = 0; SATvm *pstSavm = (SATvm *)pGetSATvm(); if(0 != access(getenv("TVMCFG"), R_OK)) @@ -4029,26 +4032,50 @@ long lStartSystem(TBoot *pstBoot, char *pszMode) return RC_FAIL; } - if(pszMode && !strcmp(pszMode, "o")) // offline + for(i = 0, n = NULL == pszMode ? 0 : strlen(pszMode); i < n; i ++) { - fprintf(stdout, "Warning:TVM will start offline\n"); - fflush(stdout); - eMode = 1; + switch(pszMode[i]) + { + case 'o': // offline + fprintf(stdout, "Warning:STVM will start offline ...\n"); + eMode = 1; + break; + case 'r': + fprintf(stdout, "STVM will do not restore tables ...\n"); + eRestore = 1; + break; + default: + break; + } } - + if(!bIsTvmBoot()) { - if(RC_SUCC != lStartupTvm(pstBoot)) - { - fprintf(stderr, "failed to boot TVM, %s\n", sGetTError(pstSavm->m_lErrno)); - return RC_FAIL; - } - } + if(RC_SUCC != lStartupTvm(pstBoot)) + { + fprintf(stderr, "failed to boot TVM, %s\n", sGetTError(pstSavm->m_lErrno)); + return RC_FAIL; + } - if(RC_SUCC != lBootLocal(pstSavm, pstBoot, eMode)) + if(RC_SUCC != lBootLocal(pstSavm, pstBoot, eMode)) + { + fprintf(stderr, "failed to boot LIS, %s\n", sGetTError(pstSavm->m_lErrno)); + return RC_SUCC; + } + + if(0 == eRestore && lRestoreTables(pstSavm)) + { + fprintf(stderr, "restore tables failed, %s\n", sGetTError(pstSavm->m_lErrno)); + return RC_FAIL; + } + } + else { - fprintf(stderr, "failed to boot LIS, %s\n", sGetTError(pstSavm->m_lErrno)); - return RC_SUCC; + if(RC_SUCC != lBootLocal(pstSavm, pstBoot, eMode)) + { + fprintf(stderr, "failed to boot LIS, %s\n", sGetTError(pstSavm->m_lErrno)); + return RC_SUCC; + } } fprintf(stderr, "start TVM : (%s)\n", sGetTError(pstSavm->m_lErrno)); @@ -4105,6 +4132,12 @@ long lStopSystem(TBoot *pstBoot, char *pszApp) if(!bIsTvmBoot()) return RC_SUCC; + if(RC_SUCC != lBackupTables(pstSavm)) + { + fprintf(stderr, "backup tables failed, %s\n", sGetTError(pstSavm->m_lErrno)); + return RC_FAIL; + } + if(RC_SUCC != lShutdownTvm()) { fprintf(stderr, "showdown node failed, %s\n", sGetTError(pstSavm->m_lErrno)); diff --git a/src/tree.c b/src/tree.c index fc65d33..c0fa570 100644 --- a/src/tree.c +++ b/src/tree.c @@ -11084,10 +11084,9 @@ void _vDumpQueue(SATvm *pstSavm, void *pvAddr, FILE *fp) RC_SUCC --success RC_FAIL --failure *************************************************************************************************/ -long lDumpTable(SATvm *pstSavm, TABLE t) +long _lDumpTable(SATvm *pstSavm, TABLE t, char *pszFile) { FILE *fp = NULL; - char szFile[512]; RunTime *pstRun = NULL; if(!pstSavm) @@ -11096,7 +11095,6 @@ long lDumpTable(SATvm *pstSavm, TABLE t) return RC_FAIL; } - memset(szFile, 0, sizeof(szFile)); if(RC_SUCC != lInitSATvm(pstSavm, t)) return RC_FAIL; @@ -11109,8 +11107,7 @@ long lDumpTable(SATvm *pstSavm, TABLE t) return RC_FAIL; } - snprintf(szFile, sizeof(szFile), "%s/%d.sdb", getenv("TVMDBD"), t); - if(NULL == (fp = fopen(szFile, "wb"))) + if(NULL == (fp = fopen(pszFile, "wb"))) { pstSavm->m_lErrno = FILE_NOT_RSET; return RC_FAIL; @@ -11131,6 +11128,24 @@ long lDumpTable(SATvm *pstSavm, TABLE t) return RC_SUCC; } +/************************************************************************************************* + description:Dump table + parameters: + pstSavm --stvm handle + t --table + return: + RC_SUCC --success + RC_FAIL --failure + *************************************************************************************************/ +long lDumpTable(SATvm *pstSavm, TABLE t) +{ + char szFile[512]; + + memset(szFile, 0, sizeof(szFile)); + snprintf(szFile, sizeof(szFile), "%s/%d.sdb", getenv("TVMDBD"), t); + return _lDumpTable(pstSavm, t, szFile); +} + /************************************************************************************************* description:lMountTable parameters: