use of org.apache.curator.framework.api.CuratorEvent in project druid by druid-io.
the class AnnouncerTest method testSessionKilled.
@Test(timeout = 60_000L)
public void testSessionKilled() throws Exception {
curator.start();
curator.blockUntilConnected();
Announcer announcer = new Announcer(curator, exec);
try {
curator.inTransaction().create().forPath("/somewhere").and().commit();
announcer.start();
final byte[] billy = "billy".getBytes();
final String testPath1 = "/test1";
final String testPath2 = "/somewhere/test2";
final Set<String> paths = Sets.newHashSet(testPath1, testPath2);
announcer.announce(testPath1, billy);
announcer.announce(testPath2, billy);
Assert.assertArrayEquals(billy, curator.getData().decompressed().forPath(testPath1));
Assert.assertArrayEquals(billy, curator.getData().decompressed().forPath(testPath2));
final CountDownLatch latch = new CountDownLatch(1);
curator.getCuratorListenable().addListener(new CuratorListener() {
@Override
public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception {
if (event.getType() == CuratorEventType.CREATE) {
paths.remove(event.getPath());
if (paths.isEmpty()) {
latch.countDown();
}
}
}
});
KillSession.kill(curator.getZookeeperClient().getZooKeeper(), server.getConnectString());
Assert.assertTrue(timing.forWaiting().awaitLatch(latch));
Assert.assertArrayEquals(billy, curator.getData().decompressed().forPath(testPath1));
Assert.assertArrayEquals(billy, curator.getData().decompressed().forPath(testPath2));
announcer.stop();
while ((curator.checkExists().forPath(testPath1) != null) || (curator.checkExists().forPath(testPath2) != null)) {
Thread.sleep(100);
}
Assert.assertNull(curator.checkExists().forPath(testPath1));
Assert.assertNull(curator.checkExists().forPath(testPath2));
} finally {
announcer.stop();
}
}
use of org.apache.curator.framework.api.CuratorEvent in project flink by apache.
the class ZooKeeperCompletedCheckpointStore method remove.
/**
* Removes the state handle from ZooKeeper, discards the checkpoints, and the state handle.
*/
private void remove(final Tuple2<RetrievableStateHandle<CompletedCheckpoint>, String> stateHandleAndPath, final Callable<Void> action) throws Exception {
BackgroundCallback callback = new BackgroundCallback() {
@Override
public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
final long checkpointId = pathToCheckpointId(stateHandleAndPath.f1);
try {
if (event.getType() == CuratorEventType.DELETE) {
if (event.getResultCode() == 0) {
Exception exception = null;
if (null != action) {
try {
action.call();
} catch (Exception e) {
exception = new Exception("Could not execute callable action " + "for checkpoint " + checkpointId + '.', e);
}
}
try {
// Discard the state handle
stateHandleAndPath.f0.discardState();
} catch (Exception e) {
Exception newException = new Exception("Could not discard meta " + "data for completed checkpoint " + checkpointId + '.', e);
if (exception == null) {
exception = newException;
} else {
exception.addSuppressed(newException);
}
}
if (exception != null) {
throw exception;
}
} else {
throw new IllegalStateException("Unexpected result code " + event.getResultCode() + " in '" + event + "' callback.");
}
} else {
throw new IllegalStateException("Unexpected event type " + event.getType() + " in '" + event + "' callback.");
}
} catch (Exception e) {
LOG.warn("Failed to discard checkpoint {}.", checkpointId, e);
}
}
};
// Remove state handle from ZooKeeper first. If this fails, we can still recover, but if
// we remove a state handle and fail to remove it from ZooKeeper, we end up in an
// inconsistent state.
checkpointsInZooKeeper.remove(stateHandleAndPath.f1, callback);
}
use of org.apache.curator.framework.api.CuratorEvent in project storm by apache.
the class Zookeeper method mkClientImpl.
public CuratorFramework mkClientImpl(Map conf, List<String> servers, Object port, String root, final WatcherCallBack watcher, Map authConf) {
CuratorFramework fk;
if (authConf != null) {
fk = Utils.newCurator(conf, servers, port, root, new ZookeeperAuthInfo(authConf));
} else {
fk = Utils.newCurator(conf, servers, port, root);
}
fk.getCuratorListenable().addListener(new CuratorListener() {
@Override
public void eventReceived(CuratorFramework _fk, CuratorEvent e) throws Exception {
if (e.getType().equals(CuratorEventType.WATCHED)) {
WatchedEvent event = e.getWatchedEvent();
watcher.execute(event.getState(), event.getType(), event.getPath());
}
}
});
LOG.info("Staring ZK Curator");
fk.start();
return fk;
}
use of org.apache.curator.framework.api.CuratorEvent in project heron by twitter.
the class CuratorStateManagerTest method testGetNodeData.
/**
* Test getNodeData method
* @throws Exception
*/
@Test
public void testGetNodeData() throws Exception {
CuratorStateManager spyStateManager = spy(new CuratorStateManager());
final CuratorFramework mockClient = mock(CuratorFramework.class);
GetDataBuilder mockGetBuilder = mock(GetDataBuilder.class);
// Mockito doesn't support mock type-parametrized class, thus suppress the warning
@SuppressWarnings("rawtypes") BackgroundPathable mockBackPathable = mock(BackgroundPathable.class);
final CuratorEvent mockEvent = mock(CuratorEvent.class);
Message.Builder mockBuilder = mock(Message.Builder.class);
Message mockMessage = mock(Message.class);
final byte[] data = "wy_1989".getBytes();
doReturn(mockMessage).when(mockBuilder).build();
doReturn(data).when(mockEvent).getData();
doReturn(PATH).when(mockEvent).getPath();
doReturn(mockClient).when(spyStateManager).getCuratorClient();
doReturn(true).when(mockClient).blockUntilConnected(anyInt(), any(TimeUnit.class));
doReturn(mockGetBuilder).when(mockClient).getData();
doReturn(mockBackPathable).when(mockGetBuilder).usingWatcher(any(Watcher.class));
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
Object[] objests = invocationOnMock.getArguments();
// the first object is the BackgroundCallback
((BackgroundCallback) objests[0]).processResult(mockClient, mockEvent);
return null;
}
}).when(mockBackPathable).inBackground(any(BackgroundCallback.class));
spyStateManager.initialize(config);
// Verify the data on node is fetched correctly
ListenableFuture<Message> result = spyStateManager.getNodeData(null, PATH, mockBuilder);
assertTrue(result.get().equals(mockMessage));
}
use of org.apache.curator.framework.api.CuratorEvent in project flink by apache.
the class ZooKeeperCompletedCheckpointStoreTest method testCheckpointRecovery.
/**
* Tests that the completed checkpoint store can retrieve all checkpoints stored in ZooKeeper
* and ignores those which cannot be retrieved via their state handles.
*/
@Test
public void testCheckpointRecovery() throws Exception {
final List<Tuple2<RetrievableStateHandle<CompletedCheckpoint>, String>> checkpointsInZooKeeper = new ArrayList<>(4);
final CompletedCheckpoint completedCheckpoint1 = mock(CompletedCheckpoint.class);
when(completedCheckpoint1.getCheckpointID()).thenReturn(1L);
final CompletedCheckpoint completedCheckpoint2 = mock(CompletedCheckpoint.class);
when(completedCheckpoint2.getCheckpointID()).thenReturn(2L);
final Collection<Long> expectedCheckpointIds = new HashSet<>(2);
expectedCheckpointIds.add(1L);
expectedCheckpointIds.add(2L);
final RetrievableStateHandle<CompletedCheckpoint> failingRetrievableStateHandle = mock(RetrievableStateHandle.class);
when(failingRetrievableStateHandle.retrieveState()).thenThrow(new Exception("Test exception"));
final RetrievableStateHandle<CompletedCheckpoint> retrievableStateHandle1 = mock(RetrievableStateHandle.class);
when(retrievableStateHandle1.retrieveState()).thenReturn(completedCheckpoint1);
final RetrievableStateHandle<CompletedCheckpoint> retrievableStateHandle2 = mock(RetrievableStateHandle.class);
when(retrievableStateHandle2.retrieveState()).thenReturn(completedCheckpoint2);
checkpointsInZooKeeper.add(Tuple2.of(retrievableStateHandle1, "/foobar1"));
checkpointsInZooKeeper.add(Tuple2.of(failingRetrievableStateHandle, "/failing1"));
checkpointsInZooKeeper.add(Tuple2.of(retrievableStateHandle2, "/foobar2"));
checkpointsInZooKeeper.add(Tuple2.of(failingRetrievableStateHandle, "/failing2"));
final CuratorFramework client = mock(CuratorFramework.class, Mockito.RETURNS_DEEP_STUBS);
final RetrievableStateStorageHelper<CompletedCheckpoint> storageHelperMock = mock(RetrievableStateStorageHelper.class);
ZooKeeperStateHandleStore<CompletedCheckpoint> zooKeeperStateHandleStoreMock = spy(new ZooKeeperStateHandleStore<>(client, storageHelperMock, Executors.directExecutor()));
whenNew(ZooKeeperStateHandleStore.class).withAnyArguments().thenReturn(zooKeeperStateHandleStoreMock);
doReturn(checkpointsInZooKeeper).when(zooKeeperStateHandleStoreMock).getAllSortedByName();
final int numCheckpointsToRetain = 1;
// Mocking for the delete operation on the CuratorFramework client
// It assures that the callback is executed synchronously
final EnsurePath ensurePathMock = mock(EnsurePath.class);
final CuratorEvent curatorEventMock = mock(CuratorEvent.class);
when(curatorEventMock.getType()).thenReturn(CuratorEventType.DELETE);
when(curatorEventMock.getResultCode()).thenReturn(0);
when(client.newNamespaceAwareEnsurePath(anyString())).thenReturn(ensurePathMock);
when(client.delete().deletingChildrenIfNeeded().inBackground(any(BackgroundCallback.class), any(Executor.class))).thenAnswer(new Answer<Pathable<Void>>() {
@Override
public Pathable<Void> answer(InvocationOnMock invocation) throws Throwable {
final BackgroundCallback callback = (BackgroundCallback) invocation.getArguments()[0];
Pathable<Void> result = mock(Pathable.class);
when(result.forPath(anyString())).thenAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
callback.processResult(client, curatorEventMock);
return null;
}
});
return result;
}
});
final String checkpointsPath = "foobar";
final RetrievableStateStorageHelper<CompletedCheckpoint> stateSotrage = mock(RetrievableStateStorageHelper.class);
ZooKeeperCompletedCheckpointStore zooKeeperCompletedCheckpointStore = new ZooKeeperCompletedCheckpointStore(numCheckpointsToRetain, client, checkpointsPath, stateSotrage, Executors.directExecutor());
zooKeeperCompletedCheckpointStore.recover();
CompletedCheckpoint latestCompletedCheckpoint = zooKeeperCompletedCheckpointStore.getLatestCheckpoint();
// check that we return the latest retrievable checkpoint
// this should remove the latest checkpoint because it is broken
assertEquals(completedCheckpoint2.getCheckpointID(), latestCompletedCheckpoint.getCheckpointID());
// this should remove the second broken checkpoint because we're iterating over all checkpoints
List<CompletedCheckpoint> completedCheckpoints = zooKeeperCompletedCheckpointStore.getAllCheckpoints();
Collection<Long> actualCheckpointIds = new HashSet<>(completedCheckpoints.size());
for (CompletedCheckpoint completedCheckpoint : completedCheckpoints) {
actualCheckpointIds.add(completedCheckpoint.getCheckpointID());
}
assertEquals(expectedCheckpointIds, actualCheckpointIds);
// check that we did not discard any of the state handles which were retrieved
verify(retrievableStateHandle1, never()).discardState();
verify(retrievableStateHandle2, never()).discardState();
// check that we have discarded the state handles which could not be retrieved
verify(failingRetrievableStateHandle, times(2)).discardState();
}
Aggregations