Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 50 additions & 8 deletions service/zookeeper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

*/

#include <signal.h>

#include <mutex>
#include <unordered_set>

#include "soa/service/zookeeper.h"
#include "jml/arch/timers.h"
#include "jml/arch/backtrace.h"
Expand All @@ -13,14 +18,9 @@ using namespace std;

namespace Datacratic {

namespace {
class ZookeeperConnection;

struct Init {
Init()
{
zoo_set_debug_level(ZOO_LOG_LEVEL_ERROR);
}
} init;
namespace {

void zk_callback(zhandle_t * ah, int type, int state, const char * path, void * user) {
uintptr_t cbid = reinterpret_cast<uintptr_t>(user);
Expand All @@ -36,6 +36,37 @@ void zk_callback(zhandle_t * ah, int type, int state, const char * path, void *
}
}

__attribute__((init_priority(101)))
mutex closeMutex;

__attribute__((init_priority(101)))
unordered_set<ZookeeperConnection *> zookeeperConnections;

struct sigaction oldAction;

__attribute__((constructor))
void
initHandlers() {
zoo_set_debug_level(ZOO_LOG_LEVEL_ERROR);

sigset_t mask;
sigemptyset(&mask);
struct sigaction action = {
[] (int signal) {
unique_lock<mutex> lock{closeMutex};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could cause deadlocks right? Locking something is a signal is risky.

for (auto zookeeperConnection : zookeeperConnections) {
zookeeperConnection->close();
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are we sure this is safe i.e. this only calls async-signal-safe functions:
http://man7.org/linux/man-pages/man7/signal.7.html

sigaction(SIGTERM, &oldAction, nullptr);
raise(SIGTERM);
},
mask,
0,
nullptr
};
sigaction(SIGTERM, &action, &oldAction);
}

} // file scope

ZookeeperCallbackManager &ZookeeperCallbackManager::instance()
Expand Down Expand Up @@ -120,8 +151,19 @@ ZookeeperConnection()
handle(0),
callbackMgr_(ZookeeperCallbackManager::instance())
{
unique_lock<mutex> lock{closeMutex};
zookeeperConnections.insert(this);
}


ZookeeperConnection::
~ZookeeperConnection() {
close();
{
unique_lock<mutex> lock{closeMutex};
zookeeperConnections.erase(this);
}
}

std::string
ZookeeperConnection::
printEvent(int eventType)
Expand Down
2 changes: 1 addition & 1 deletion service/zookeeper.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ struct ZookeeperConnection {


ZookeeperConnection();
~ZookeeperConnection() { close(); }
~ZookeeperConnection();

static std::string printEvent(int eventType);

Expand Down
2 changes: 1 addition & 1 deletion service/zookeeper_configuration_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ ZookeeperConfigurationService(std::string host,
std::string location,
int timeout)
{
init(std::move(host), std::move(prefix), std::move(location));
init(std::move(host), std::move(prefix), std::move(location), timeout);
}

ZookeeperConfigurationService::
Expand Down
2 changes: 1 addition & 1 deletion service/zookeeper_configuration_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct ZookeeperConfigurationService
void init(std::string host,
std::string prefix,
std::string location,
int timeout = 5);
int timeout);

virtual Json::Value getJson(const std::string & value,
Watch watch = Watch());
Expand Down