| 1 | #include "ZooKeeperHolder.h" |
|---|---|
| 2 | |
| 3 | |
| 4 | namespace DB |
| 5 | { |
| 6 | namespace ErrorCodes |
| 7 | { |
| 8 | extern const int NULL_POINTER_DEREFERENCE; |
| 9 | } |
| 10 | } |
| 11 | |
| 12 | |
| 13 | using namespace zkutil; |
| 14 | |
| 15 | ZooKeeperHolder::UnstorableZookeeperHandler ZooKeeperHolder::getZooKeeper() |
| 16 | { |
| 17 | std::unique_lock lock(mutex); |
| 18 | return UnstorableZookeeperHandler(ptr); |
| 19 | } |
| 20 | |
| 21 | void ZooKeeperHolder::initFromInstance(const ZooKeeper::Ptr & zookeeper_ptr) |
| 22 | { |
| 23 | ptr = zookeeper_ptr; |
| 24 | } |
| 25 | |
| 26 | bool ZooKeeperHolder::replaceZooKeeperSessionToNewOne() |
| 27 | { |
| 28 | std::unique_lock lock(mutex); |
| 29 | |
| 30 | if (ptr.unique()) |
| 31 | { |
| 32 | ptr = ptr->startNewSession(); |
| 33 | return true; |
| 34 | } |
| 35 | else |
| 36 | { |
| 37 | LOG_ERROR(log, "replaceZooKeeperSessionToNewOne(): Fail to replace zookeeper session to new one because handlers for old zookeeper session still exists."); |
| 38 | return false; |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | bool ZooKeeperHolder::isSessionExpired() const |
| 43 | { |
| 44 | return ptr ? ptr->expired() : false; |
| 45 | } |
| 46 | |
| 47 | |
| 48 | std::string ZooKeeperHolder::nullptr_exception_message = |
| 49 | "UnstorableZookeeperHandler::zk_ptr is nullptr. " |
| 50 | "ZooKeeperHolder should be initialized before sending any request to ZooKeeper"; |
| 51 | |
| 52 | ZooKeeperHolder::UnstorableZookeeperHandler::UnstorableZookeeperHandler(ZooKeeper::Ptr zk_ptr_) |
| 53 | : zk_ptr(zk_ptr_) |
| 54 | { |
| 55 | } |
| 56 | |
| 57 | ZooKeeper * ZooKeeperHolder::UnstorableZookeeperHandler::operator->() |
| 58 | { |
| 59 | if (zk_ptr == nullptr) |
| 60 | throw DB::Exception(nullptr_exception_message, DB::ErrorCodes::NULL_POINTER_DEREFERENCE); |
| 61 | |
| 62 | return zk_ptr.get(); |
| 63 | } |
| 64 | |
| 65 | const ZooKeeper * ZooKeeperHolder::UnstorableZookeeperHandler::operator->() const |
| 66 | { |
| 67 | if (zk_ptr == nullptr) |
| 68 | throw DB::Exception(nullptr_exception_message, DB::ErrorCodes::NULL_POINTER_DEREFERENCE); |
| 69 | return zk_ptr.get(); |
| 70 | } |
| 71 | |
| 72 | ZooKeeper & ZooKeeperHolder::UnstorableZookeeperHandler::operator*() |
| 73 | { |
| 74 | if (zk_ptr == nullptr) |
| 75 | throw DB::Exception(nullptr_exception_message, DB::ErrorCodes::NULL_POINTER_DEREFERENCE); |
| 76 | return *zk_ptr; |
| 77 | } |
| 78 | |
| 79 | const ZooKeeper & ZooKeeperHolder::UnstorableZookeeperHandler::operator*() const |
| 80 | { |
| 81 | if (zk_ptr == nullptr) |
| 82 | throw DB::Exception(nullptr_exception_message, DB::ErrorCodes::NULL_POINTER_DEREFERENCE); |
| 83 | return *zk_ptr; |
| 84 | } |
| 85 |