提交 5d641a89 编写于 作者: M Minglei Jin

fix(tdb/restore): restore tdb journal backward

上级 0bf98096
...@@ -947,6 +947,12 @@ static int tdbPagerRestore(SPager *pPager, const char *jFileName) { ...@@ -947,6 +947,12 @@ static int tdbPagerRestore(SPager *pPager, const char *jFileName) {
return 0; return 0;
} }
static int32_t txnIdCompareDesc(const void *pLeft, const void *pRight) {
int64_t lhs = *(int64_t *)pLeft;
int64_t rhs = *(int64_t *)pRight;
return lhs > rhs ? -1 : 1;
}
int tdbPagerRestoreJournals(SPager *pPager) { int tdbPagerRestoreJournals(SPager *pPager) {
tdbDirEntryPtr pDirEntry; tdbDirEntryPtr pDirEntry;
tdbDirPtr pDir = taosOpenDir(pPager->pEnv->dbName); tdbDirPtr pDir = taosOpenDir(pPager->pEnv->dbName);
...@@ -955,20 +961,29 @@ int tdbPagerRestoreJournals(SPager *pPager) { ...@@ -955,20 +961,29 @@ int tdbPagerRestoreJournals(SPager *pPager) {
return -1; return -1;
} }
SArray *pTxnList = taosArrayInit(16, sizeof(int64_t));
while ((pDirEntry = tdbReadDir(pDir)) != NULL) { while ((pDirEntry = tdbReadDir(pDir)) != NULL) {
char *name = tdbDirEntryBaseName(tdbGetDirEntryName(pDirEntry)); char *name = tdbDirEntryBaseName(tdbGetDirEntryName(pDirEntry));
if (strncmp(TDB_MAINDB_NAME "-journal", name, 16) == 0) { if (strncmp(TDB_MAINDB_NAME "-journal", name, 16) == 0) {
char jname[TD_PATH_MAX] = {0}; int64_t txnId = -1;
int dirLen = strlen(pPager->pEnv->dbName); sscanf(name, TDB_MAINDB_NAME "-journal.%" PRId64, &txnId);
memcpy(jname, pPager->pEnv->dbName, dirLen); taosArrayPush(pTxnList, &txnId);
jname[dirLen] = '/'; }
memcpy(jname + dirLen + 1, name, strlen(name)); }
if (tdbPagerRestore(pPager, jname) < 0) { taosArraySort(pTxnList, txnIdCompareDesc);
tdbCloseDir(&pDir); for (int i = 0; i < TARRAY_SIZE(pTxnList); ++i) {
int64_t *pTxnId = taosArrayGet(pTxnList, i);
tdbError("failed to restore file due to %s. jFileName:%s", strerror(errno), name); char jname[TD_PATH_MAX] = {0};
return -1; int dirLen = strlen(pPager->pEnv->dbName);
} memcpy(jname, pPager->pEnv->dbName, dirLen);
jname[dirLen] = '/';
sprintf(jname + dirLen + 1, TDB_MAINDB_NAME "-journal.%" PRId64, *pTxnId);
if (tdbPagerRestore(pPager, jname) < 0) {
tdbCloseDir(&pDir);
tdbError("failed to restore file due to %s. jFileName:%s", strerror(errno), jname);
return -1;
} }
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册