void DelInstance(const int iGroupIdx, MultiDatabase & oLogStorage, uint64_t llInstanceID) { string sValue; int ret = oLogStorage.Get(iGroupIdx, llInstanceID, sValue); if (ret != 0) { printf("get this instance %lu fail, do you want to delete it? (y/n)\n", llInstanceID); } else { printf("this instance %lu value size is %zu, do you want to delete it? (y/n)\n", llInstanceID, sValue.size()); } string c = "n"; cin >> c; if (c == "y") { WriteOptions oWriteOptions; oWriteOptions.bSync = true; ret = oLogStorage.ForceDel(oWriteOptions, iGroupIdx, llInstanceID); if (ret != 0) { printf("delete instance %lu fail\n", llInstanceID); } else { printf("delete instance %lu ok\n", llInstanceID); } } }
int main(int argc, char ** argv) { if (argc < 5) { printf("%s <paxos log dir> <group idx> <del> <instanceid>\n", argv[0]); return -1; } string sPaxosLogPath = argv[1]; int iGroupIdx = atoi(argv[2]); if (sPaxosLogPath.size() == 0) { printf("paxos log path not valid, path %s\n", sPaxosLogPath.c_str()); return -1; } MultiDatabase oDefaultLogStorage; int ret = oDefaultLogStorage.Init(sPaxosLogPath, iGroupIdx + 1); if (ret != 0) { printf("init log storage fail, ret %d\n", ret); return ret; } string sOP = string(argv[3]); uint64_t llInstanceID = strtoull(argv[4], NULL, 10); if (sOP == "del") { DelInstance(iGroupIdx, oDefaultLogStorage, llInstanceID); } return 0; }
TEST(MultiDatabase, ClearAllLog) { int iGroupCount = 2; MultiDatabase oDB; ASSERT_TRUE(InitDB(iGroupCount, oDB) == 0); const uint64_t llInstanceID = 3; std::string sValue = "hello paxos"; for (int iGroupIdx = 0; iGroupIdx < iGroupCount; iGroupIdx++) { WriteOptions oWriteOptions; oWriteOptions.bSync = true; ASSERT_TRUE(oDB.Put(oWriteOptions, iGroupIdx, llInstanceID, sValue) == 0); } for (int iGroupIdx = 0; iGroupIdx < iGroupCount; iGroupIdx++) { ASSERT_TRUE(oDB.ClearAllLog(iGroupIdx) == 0); } for (int iGroupIdx = 0; iGroupIdx < iGroupCount; iGroupIdx++) { const uint64_t llInstanceID = 3; std::string sGetValue; ASSERT_TRUE(oDB.Get(iGroupIdx, llInstanceID, sGetValue) == 1); } }
int InitDB(const int iGroupCount, MultiDatabase & oDB) { string sDBPath; int ret = MakeLogStoragePath(sDBPath); if (ret != 0) { return ret; } ret = oDB.Init(sDBPath, iGroupCount); if (ret != 0) { return ret; } for (int iGroupIdx = 0; iGroupIdx < iGroupCount; iGroupIdx++) { ret = oDB.ClearAllLog(iGroupIdx); if (ret != 0) { return ret; } } return 0; }
TEST(MultiDatabase, Set_Get_MasterVariables) { int iGroupCount = 2; MultiDatabase oDB; ASSERT_TRUE(InitDB(iGroupCount, oDB) == 0); string sBuffer = "234fj238j423j4l2k3j4lasklfjaslkfj28934j2l3j4lajflsjdlf"; WriteOptions oWriteOptions; oWriteOptions.bSync = true; for (int iGroupIdx = 0; iGroupIdx < iGroupCount; iGroupIdx++) { ASSERT_TRUE(oDB.SetMasterVariables(oWriteOptions, iGroupIdx, sBuffer) == 0); string sGetBuffer; ASSERT_TRUE(oDB.GetMasterVariables(iGroupIdx, sGetBuffer) == 0); EXPECT_TRUE(sBuffer == sGetBuffer); } }
TEST(MultiDatabase, Set_Get_MinChosenInstanceID) { int iGroupCount = 2; MultiDatabase oDB; ASSERT_TRUE(InitDB(iGroupCount, oDB) == 0); const uint64_t llMinChosenInstanceID = 102342342342lu; WriteOptions oWriteOptions; oWriteOptions.bSync = true; for (int iGroupIdx = 0; iGroupIdx < iGroupCount; iGroupIdx++) { ASSERT_TRUE(oDB.SetMinChosenInstanceID(oWriteOptions, iGroupIdx, llMinChosenInstanceID) == 0); uint64_t llGetMinChosenInstanceID = 0; ASSERT_TRUE(oDB.GetMinChosenInstanceID(iGroupIdx, llGetMinChosenInstanceID) == 0); EXPECT_TRUE(llMinChosenInstanceID == llGetMinChosenInstanceID); } }
TEST(MultiDatabase, GetMaxInstanceID) { int iGroupCount = 2; MultiDatabase oDB; ASSERT_TRUE(InitDB(iGroupCount, oDB) == 0); const uint64_t llInstanceID = 3; std::string sValue = "hello paxos"; WriteOptions oWriteOptions; oWriteOptions.bSync = true; for (int iGroupIdx = 0; iGroupIdx < iGroupCount; iGroupIdx++) { ASSERT_TRUE(oDB.Put(oWriteOptions, iGroupIdx, llInstanceID + 1, sValue) == 0); ASSERT_TRUE(oDB.Put(oWriteOptions, iGroupIdx, llInstanceID + 3, sValue) == 0); uint64_t llMaxInstanceID = 0; ASSERT_TRUE(oDB.GetMaxInstanceID(iGroupIdx, llMaxInstanceID) == 0); EXPECT_TRUE(llMaxInstanceID == llInstanceID + 3); } }