Refine search
private void determineLeadership() { ReentrantReadWriteLock.WriteLock lock = this.readWriteLock.writeLock(); lock.lock(); try { List<String> children = this.zooKeeper.getChildren().forPath(this.leaderElectionNode); Collections.sort(children); int idx = children.indexOf(this.nodeId); if (idx == 0) { Stat stat = this.zooKeeper.checkExists().forPath(this.leaderNode); if (stat == null) { this.zooKeeper.create().forPath(this.leaderNode, serializeMetadata(this.localMetadata)); } else { this.zooKeeper.setData().forPath(this.leaderNode, serializeMetadata(this.localMetadata)); } this.isLeader = true; } else { this.isLeader = false; String watchedNode = this.leaderElectionNode + "/" + children.get(idx - 1); this.zooKeeper.checkExists().usingWatcher(new DetermineLeadershipWatcher()).forPath(watchedNode); } findLeader(); } catch (KeeperException exc) { reset(); } catch (Throwable exc) { log.error("Fatal failure.", exc); this.fatalFailure = true; } finally { lock.unlock(); } }
@Test public void verifyZooKeeperToleratesOneNodeDataLoss() throws Exception { try { zk.curatorWithSuperAuth().create().forPath(FOO, FOO_DATA); assertArrayEquals(FOO_DATA, zk.curatorWithSuperAuth().getData().forPath(FOO)); } catch (KeeperException.NodeExistsException ignore) { // ignored } zk.stopPeer(0); zk.resetPeer(0); zk.startPeer(0); zk.stopPeer(1); assertArrayEquals(FOO_DATA, zk.curatorWithSuperAuth().getData().forPath(FOO)); }
@Test public void verifyCanCreateNodesWithOnePeerDead() throws Exception { // Kill two peers and bring one up to ensure that only two out of three peers are alive zk.stopPeer(0); zk.stopPeer(1); zk.awaitDown(5, MINUTES); zk.startPeer(1); zk.awaitUp(5, MINUTES); try { zk.curatorWithSuperAuth().create().forPath(FOO, FOO_DATA); assertArrayEquals(FOO_DATA, zk.curatorWithSuperAuth().getData().forPath(FOO)); } catch (KeeperException.NodeExistsException ignore) { // ignored } }
@Test public void verifyZooKeeperRecoversWithTwoPeersAlive() throws Exception { zk.stopPeer(0); zk.stopPeer(1); zk.awaitDown(5, MINUTES); zk.resetPeer(0); zk.startPeer(0); zk.awaitUp(5, MINUTES); try { zk.curatorWithSuperAuth().create().forPath(FOO, FOO_DATA); assertArrayEquals(FOO_DATA, zk.curatorWithSuperAuth().getData().forPath(FOO)); } catch (KeeperException.NodeExistsException ignore) { // ignored } }
@Test public void testLeavesBehindTurdlingsThatAlreadyExisted() throws Exception { curator.start(); curator.blockUntilConnected(); Announcer announcer = new Announcer(curator, exec); final byte[] billy = StringUtils.toUtf8("billy"); final String testPath = "/somewhere/test2"; final String parent = ZKPaths.getPathAndNode(testPath).getPath(); curator.create().forPath(parent); final Stat initialStat = curator.checkExists().forPath(parent); announcer.start(); try { Assert.assertEquals(initialStat.getMzxid(), curator.checkExists().forPath(parent).getMzxid()); awaitAnnounce(announcer, testPath, billy, true); Assert.assertEquals(initialStat.getMzxid(), curator.checkExists().forPath(parent).getMzxid()); } finally { announcer.stop(); } Assert.assertEquals(initialStat.getMzxid(), curator.checkExists().forPath(parent).getMzxid()); }
@Test public void verifyCanDeployWithOneNodeDeadAfterOneNodeDataLoss() throws Exception { // First deploy a job deploy(fooJob); // Create a node that we know is written after the job try { zkc.curatorWithSuperAuth().create().forPath("/barrier"); } catch (NodeExistsException ignore) { // ignored } // Wipe one zk peer zkc.stopPeer(0); zkc.resetPeer(0); zkc.startPeer(0); // Wait for the zk peer to recover Polling.await(LONG_WAIT_SECONDS, SECONDS, new Callable<Object>() { @Override public Object call() throws Exception { return zkc.peerCurator(0).checkExists().forPath("/barrier"); } }); // Then take down another peer zkc.stopPeer(1); // Now verify that we can still undeploy and deploy jobs undeploy(fooJob.getId()); deploy(barJob); }
/** * Pretend to be a Drillbit creating its ZK entry. Real Drillbits use a GUID * as the key, but we just use the host name, which is good enough for our * purposes here. * * @param zk * @param host * @throws Exception */ private void addDrillbit(CuratorFramework zk, String host) throws Exception { DrillbitEndpoint dbe = makeEndpoint(host); ServiceInstance<DrillbitEndpoint> si = ServiceInstance .<DrillbitEndpoint> builder().name(CLUSTER_ID).payload(dbe).build(); byte data[] = DrillServiceInstanceHelper.SERIALIZER.serialize(si); zk.create().forPath("/" + host, data); }