@Override public void await(long timeout, TimeUnit timeUnit) throws TimeoutException { // waitUntilExists signals timeout by returning false as opposed to throwing exception. We internally need to map // the non-existence to a TimeoutException in order to respect the contract defined in Latch interface boolean targetPathExists = zkUtils.getZkClient().waitUntilExists(targetPath, timeUnit, timeout); if (!targetPathExists) { throw new TimeoutException("Timed out waiting for the targetPath"); } }
@Override public void await(long timeout, TimeUnit timeUnit) throws TimeoutException { // waitUntilExists signals timeout by returning false as opposed to throwing exception. We internally need to map // the non-existence to a TimeoutException in order to respect the contract defined in Latch interface boolean targetPathExists = zkUtils.getZkClient().waitUntilExists(targetPath, timeUnit, timeout); if (!targetPathExists) { throw new TimeoutException("Timed out waiting for the targetPath"); } }
@Override public void await(long timeout, TimeUnit timeUnit) throws TimeoutException { // waitUntilExists signals timeout by returning false as opposed to throwing exception. We internally need to map // the non-existence to a TimeoutException in order to respect the contract defined in Latch interface boolean targetPathExists = zkUtils.getZkClient().waitUntilExists(targetPath, timeUnit, timeout); if (!targetPathExists) { throw new TimeoutException("Timed out waiting for the targetPath"); } }
@Override public void await(long timeout, TimeUnit timeUnit) throws TimeoutException { // waitUntilExists signals timeout by returning false as opposed to throwing exception. We internally need to map // the non-existence to a TimeoutException in order to respect the contract defined in Latch interface boolean targetPathExists = zkUtils.getZkClient().waitUntilExists(targetPath, timeUnit, timeout); if (!targetPathExists) { throw new TimeoutException("Timed out waiting for the targetPath"); } }
@Override public void await(long timeout, TimeUnit timeUnit) throws TimeoutException { // waitUntilExists signals timeout by returning false as opposed to throwing exception. We internally need to map // the non-existence to a TimeoutException in order to respect the contract defined in Latch interface boolean targetPathExists = zkUtils.getZkClient().waitUntilExists(targetPath, timeUnit, timeout); if (!targetPathExists) { throw new TimeoutException("Timed out waiting for the targetPath"); } }
@Override public ZNode createPersistent(final String path) { final String[] parts = StringUtils.split(path, ZookeeperConstants.PATH_SEPARATOR); final StringBuilder sb = new StringBuilder(); for (int i = 0; i < parts.length; i++) { sb.append(ZookeeperConstants.PATH_SEPARATOR).append(parts[i]); final String pathPart = sb.toString(); if (!zkClient.exists(pathPart)) { zkClient.createPersistent(pathPart); final boolean created = zkClient.waitUntilExists(pathPart, TimeUnit.SECONDS, nodeCreationTimeoutSeconds); if (!created) { throw new ZkNoNodeException("Persistent root node not created after " + nodeCreationTimeoutSeconds + " seconds, cannot proceed"); } } } return new ZNodeImpl(path); }
@Test public void test_createPersistentRootExists() { when(zkClient.exists(projectx_PATH)).thenReturn(true); when(zkClient.exists(ROOT_PATH)).thenReturn(true); when(zkClient.waitUntilExists(projectx_PATH, TimeUnit.SECONDS, 5)).thenReturn(true); when(zkClient.waitUntilExists(ROOT_PATH, TimeUnit.SECONDS, 5)).thenReturn(true); classUnderTest.createPersistent(ROOT_PATH); verify(zkClient).exists(projectx_PATH); verify(zkClient).exists(ROOT_PATH); verify(zkClient, times(0)).createPersistent(projectx_PATH); verify(zkClient, times(0)).createPersistent(ROOT_PATH); verify(zkClient, times(0)).waitUntilExists(ROOT_PATH, TimeUnit.SECONDS, 5); }
@Test(expected = ZkNoNodeException.class) public void test_createEphemeralSequentialFailure() { when(zkClient.waitUntilExists(EPHEMERAL_NODE, TimeUnit.SECONDS, 5)).thenReturn(false); classUnderTest.createEphemeralSequential(ROOT_PATH); }
@Override public SequentialZNode createEphemeralSequential(final String path, final Object data) { final String ephermalPath = path + ZookeeperConstants.PATH_SEPARATOR + ZookeeperConstants.EPHERMAL_PREFIX; final String nodeName = zkClient.createEphemeralSequential(ephermalPath, formatData(data)); final boolean created = zkClient.waitUntilExists(nodeName, TimeUnit.SECONDS, nodeCreationTimeoutSeconds); if (!created) { throw new ZkNoNodeException("Ephemeral node not created after " + nodeCreationTimeoutSeconds + " seconds, cannot proceed"); } return createNode(path, nodeName, data); }
@Test public void test_createPersistentRootMissing() { when(zkClient.exists(projectx_PATH)).thenReturn(false); when(zkClient.exists(ROOT_PATH)).thenReturn(false); when(zkClient.waitUntilExists(projectx_PATH, TimeUnit.SECONDS, 5)).thenReturn(true); when(zkClient.waitUntilExists(ROOT_PATH, TimeUnit.SECONDS, 5)).thenReturn(true); classUnderTest.createPersistent(ROOT_PATH); verify(zkClient).exists(projectx_PATH); verify(zkClient).exists(ROOT_PATH); verify(zkClient).createPersistent(projectx_PATH); verify(zkClient).createPersistent(ROOT_PATH); verify(zkClient).waitUntilExists(projectx_PATH, TimeUnit.SECONDS, 5); verify(zkClient).waitUntilExists(ROOT_PATH, TimeUnit.SECONDS, 5); }
@Test(expected = ZkNoNodeException.class) public void test_createPersistentRootMissingFailure() { when(zkClient.exists(projectx_PATH)).thenReturn(false); when(zkClient.waitUntilExists(projectx_PATH, TimeUnit.SECONDS, 5)).thenReturn(false); classUnderTest.createPersistent(ROOT_PATH); }
@Test public void test_createEphemeralSequential() { when(zkClient.createEphemeralSequential(EPHEMERAL_PATH, null)).thenReturn(EPHEMERAL_NODE); when(zkClient.waitUntilExists(EPHEMERAL_NODE, TimeUnit.SECONDS, 5)).thenReturn(true); final SequentialZNode node = classUnderTest.createEphemeralSequential(ROOT_PATH); verify(zkClient).createEphemeralSequential(EPHEMERAL_PATH, null); verify(zkClient).waitUntilExists(EPHEMERAL_NODE, TimeUnit.SECONDS, 5); assertEquals("incorrect sequence", 0, node.getSequence().intValue()); assertEquals("incorrect path", ROOT_PATH, node.getPath()); assertEquals("incorrect full path", EPHEMERAL_NODE, node.getFullPath()); }