Miscellanous

This commit is contained in:
Alexey Milovidov 2020-10-09 22:15:51 +03:00
parent ce7b8aefd2
commit 7b39906d6a

View File

@ -7,8 +7,9 @@
int main(int argc, char ** argv) int main(int argc, char ** argv)
try
{ {
try
{
boost::program_options::options_description desc("Allowed options"); boost::program_options::options_description desc("Allowed options");
desc.add_options() desc.add_options()
("help,h", "produce help message") ("help,h", "produce help message")
@ -39,6 +40,19 @@ try
size_t num_reconnects = 0; size_t num_reconnects = 0;
constexpr size_t max_reconnects = 100; constexpr size_t max_reconnects = 100;
auto ensureSession = [&]
{
if (zookeeper->expired())
{
if (num_reconnects == max_reconnects)
return false;
++num_reconnects;
std::cerr << "num_reconnects: " << num_reconnects << "\n";
zookeeper = zookeeper->startNewSession();
}
return true;
};
for (auto it = list_futures.begin(); it != list_futures.end(); ++it) for (auto it = list_futures.begin(); it != list_futures.end(); ++it)
{ {
Coordination::ListResponse response; Coordination::ListResponse response;
@ -56,14 +70,8 @@ try
else if (Coordination::isHardwareError(e.code)) else if (Coordination::isHardwareError(e.code))
{ {
/// Reinitialize the session and move the node to the end of the queue for later retry. /// Reinitialize the session and move the node to the end of the queue for later retry.
if (zookeeper->expired()) if (!ensureSession())
{
if (num_reconnects == max_reconnects)
throw; throw;
++num_reconnects;
zookeeper = zookeeper->startNewSession();
}
list_futures.emplace_back(it->first, zookeeper->asyncGetChildren(it->first)); list_futures.emplace_back(it->first, zookeeper->asyncGetChildren(it->first));
continue; continue;
} }
@ -76,12 +84,17 @@ try
for (const auto & name : response.names) for (const auto & name : response.names)
{ {
std::string child_path = it->first == "/" ? it->first + name : it->first + '/' + name; std::string child_path = it->first == "/" ? it->first + name : it->first + '/' + name;
ensureSession();
list_futures.emplace_back(child_path, zookeeper->asyncGetChildren(child_path)); list_futures.emplace_back(child_path, zookeeper->asyncGetChildren(child_path));
} }
} }
}
catch (...) return 0;
{ }
catch (...)
{
std::cerr << DB::getCurrentExceptionMessage(true) << '\n'; std::cerr << DB::getCurrentExceptionMessage(true) << '\n';
throw; return 1;
}
} }