private void reset() { ReentrantReadWriteLock.WriteLock lock = this.readWriteLock.writeLock(); lock.lock(); try { if (this.zooKeeper != null) { this.zooKeeper.close(); } this.zooKeeper = CuratorFrameworkFactory.builder().retryPolicy(new ExponentialBackoffRetry(100, 3)) .connectString(this.zkConnectString).build(); this.zooKeeper.start(); if (!this.zooKeeper.blockUntilConnected(1, TimeUnit.SECONDS)) { throw new RuntimeException("Could not connect to Zookeeper."); } String nodePath = this.zooKeeper.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL) .forPath(this.leaderElectionNode + "/p_"); this.nodeId = nodePath.substring(nodePath.lastIndexOf("/") + 1); determineLeadership(); } catch (Throwable exc) { throw new RuntimeException(exc); } finally { lock.unlock(); } }
private void findLeader() { ReentrantReadWriteLock.WriteLock lock = this.readWriteLock.writeLock(); lock.lock(); try { if (this.zooKeeper.checkExists().usingWatcher(new FindLeaderWatcher()).forPath(this.leaderNode) == null) { determineLeadership(); } byte[] leaderData = this.zooKeeper.getData().usingWatcher(new FindLeaderWatcher()).forPath(this.leaderNode); this.leaderMetadata = deserializeMetadata(leaderData); } catch (KeeperException exc) { reset(); } catch (Throwable exc) { log.error("Fatal failure.", exc); this.fatalFailure = true; } finally { lock.unlock(); } }
private void reset() { ReentrantReadWriteLock.WriteLock lock = this.readWriteLock.writeLock(); lock.lock(); try { if (this.zooKeeper != null) { this.zooKeeper.close(); } this.zooKeeper = CuratorFrameworkFactory.builder().retryPolicy(new ExponentialBackoffRetry(100, 3)) .connectString(this.zkConnectString).build(); this.zooKeeper.start(); if (!this.zooKeeper.blockUntilConnected(1, TimeUnit.SECONDS)) { throw new RuntimeException("Could not connect to Zookeeper."); } String nodePath = this.zooKeeper.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL) .forPath(this.leaderElectionNode + "/p_"); this.nodeId = nodePath.substring(nodePath.lastIndexOf("/") + 1); determineLeadership(); } catch (Throwable exc) { throw new RuntimeException(exc); } finally { lock.unlock(); } }
private void findLeader() { ReentrantReadWriteLock.WriteLock lock = this.readWriteLock.writeLock(); lock.lock(); try { if (this.zooKeeper.checkExists().usingWatcher(new FindLeaderWatcher()).forPath(this.leaderNode) == null) { determineLeadership(); } byte[] leaderData = this.zooKeeper.getData().usingWatcher(new FindLeaderWatcher()).forPath(this.leaderNode); this.leaderMetadata = deserializeMetadata(leaderData); } catch (KeeperException exc) { reset(); } catch (Throwable exc) { log.error("Fatal failure.", exc); this.fatalFailure = true; } finally { lock.unlock(); } }