Java源码示例:org.apache.curator.framework.api.DeleteBuilder
示例1
public void delete(String path, boolean isDeleteChildren) {
try {
if (!exist(path)) {
return;
}
DeleteBuilder builder = client.delete();
if (isDeleteChildren) {
builder.guaranteed().deletingChildrenIfNeeded().forPath(path);
return;
}
builder.guaranteed().forPath(path);
} catch (Throwable e) {
throw new ZookeeperException("Fail to delete node of path: {0}", e.getMessage());
}
}
示例2
@Test
public void shouldDeleteSetupInProgressNodeAfterCompletion() throws Exception {
Set<SetupStep> steps = new LinkedHashSet<>();
SetupStep setupStep1 = mock(SetupStep.class);
steps.add(setupStep1);
when(configuration.
getString(HAConfiguration.ATLAS_SERVER_HA_ZK_ROOT_KEY, HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT)).
thenReturn(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT);
when(configuration.getString(HAConfiguration.HA_ZOOKEEPER_ACL)).thenReturn("digest:user:pwd");
List<ACL> aclList = Arrays.asList(new ACL(ZooDefs.Perms.ALL, new Id("digest", "user:pwd")));
setupServerIdSelectionMocks();
DeleteBuilder deleteBuilder = setupSetupInProgressPathMocks(aclList).getRight();
InterProcessMutex lock = mock(InterProcessMutex.class);
when(curatorFactory.lockInstance(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT)).
thenReturn(lock);
SetupSteps setupSteps = new SetupSteps(steps, curatorFactory, configuration);
setupSteps.runSetup();
verify(deleteBuilder).forPath(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT+SetupSteps.SETUP_IN_PROGRESS_NODE);
}
示例3
public void delete(String path, boolean isDeleteChildren) {
try {
if (!exist(path)) {
return;
}
DeleteBuilder builder = client.delete();
if (isDeleteChildren) {
builder.guaranteed().deletingChildrenIfNeeded().forPath(path);
return;
}
builder.guaranteed().forPath(path);
} catch (Throwable e) {
throw new ZookeeperException("Fail to delete node of path: {0}", e.getMessage());
}
}
示例4
@Test
public void shouldDeleteSetupInProgressNodeAfterCompletion() throws Exception {
Set<SetupStep> steps = new LinkedHashSet<>();
SetupStep setupStep1 = mock(SetupStep.class);
steps.add(setupStep1);
when(configuration.
getString(HAConfiguration.ATLAS_SERVER_HA_ZK_ROOT_KEY, HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT)).
thenReturn(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT);
when(configuration.getString(HAConfiguration.HA_ZOOKEEPER_ACL)).thenReturn("digest:user:pwd");
List<ACL> aclList = Arrays.asList(new ACL(ZooDefs.Perms.ALL, new Id("digest", "user:pwd")));
setupServerIdSelectionMocks();
DeleteBuilder deleteBuilder = setupSetupInProgressPathMocks(aclList).getRight();
InterProcessMutex lock = mock(InterProcessMutex.class);
when(curatorFactory.lockInstance(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT)).
thenReturn(lock);
SetupSteps setupSteps = new SetupSteps(steps, curatorFactory, configuration);
setupSteps.runSetup();
verify(deleteBuilder).forPath(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT+SetupSteps.SETUP_IN_PROGRESS_NODE);
}
示例5
/**
* 删除节点
* @param curator
* @param path
* @param recursive
* @param backgroundCallback
* @throws Exception
*/
public static void zkDelete(CuratorFramework curator, String path, boolean recursive, BackgroundCallback backgroundCallback) throws Exception {
DeleteBuilder delete = curator.delete();
if(recursive) {
delete.deletingChildrenIfNeeded();
}
if(backgroundCallback != null) {
delete.inBackground(backgroundCallback);
}
delete.forPath(path);
}
示例6
private Pair<CreateBuilder, DeleteBuilder> setupSetupInProgressPathMocks(List<ACL> acls, Stat stat) throws Exception {
when(curatorFactory.clientInstance()).thenReturn(client);
CreateBuilder createBuilder = mock(CreateBuilder.class);
when(createBuilder.withACL(acls)).thenReturn(createBuilder);
when(client.create()).thenReturn(createBuilder);
DeleteBuilder deleteBuilder = mock(DeleteBuilder.class);
when(client.delete()).thenReturn(deleteBuilder);
Pair<CreateBuilder, DeleteBuilder> pair = Pair.of(createBuilder, deleteBuilder);
ExistsBuilder existsBuilder = mock(ExistsBuilder.class);
when(client.checkExists()).thenReturn(existsBuilder);
when(existsBuilder.forPath(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT+SetupSteps.SETUP_IN_PROGRESS_NODE)).
thenReturn(stat);
return pair;
}
示例7
/**
* Tests that if we attempt to delete a node that doesnt actually exist
* just silently returns.
*
* To simulate a race condition we do this using mocks.
*/
@Test
public void testDeleteNodeIfNoChildren_withNodeThatDoesntExist() throws Exception {
final String basePath = "/testDeleteNodeIfNoChildren_withNodeThatDoesntExist";
final CuratorFramework mockCurator = mock(CuratorFramework.class);
// Exists builder should return true saying our basePath exists.
final ExistsBuilder mockExistsBuilder = mock(ExistsBuilder.class);
when(mockExistsBuilder.forPath(eq(basePath))).thenReturn(new Stat());
when(mockCurator.checkExists()).thenReturn(mockExistsBuilder);
// When we look for children, make sure it returns an empty list.
final GetChildrenBuilder mockGetChildrenBuilder = mock(GetChildrenBuilder.class);
when(mockGetChildrenBuilder.forPath(eq(basePath))).thenReturn(new ArrayList<>());
when(mockCurator.getChildren()).thenReturn(mockGetChildrenBuilder);
// When we go to delete the actual node, we toss a no-node exception.
// This effectively simulates a race condition between checking if the node exists (our mock above says yes)
// and it being removed before we call delete on it.
final DeleteBuilder mockDeleteBuilder = mock(DeleteBuilder.class);
when(mockDeleteBuilder.forPath(eq(basePath))).thenThrow(new KeeperException.NoNodeException());
when(mockCurator.delete()).thenReturn(mockDeleteBuilder);
// Now create our helper
final CuratorHelper curatorHelper = new CuratorHelper(mockCurator, new HashMap<>());
// Call our method
curatorHelper.deleteNodeIfNoChildren(basePath);
}
示例8
@Override
public void remove(String uuid, Handler<AsyncResult<Record>> resultHandler) {
Objects.requireNonNull(uuid, "No registration id in the record");
Context context = Vertx.currentContext();
ensureConnected(x -> {
if (x.failed()) {
resultHandler.handle(Future.failedFuture(x.cause()));
} else {
getRecordById(context, uuid, record -> {
if (record == null) {
resultHandler.handle(Future.failedFuture("Unknown registration " + uuid));
} else {
try {
DeleteBuilder delete = client.delete();
if (guaranteed) {
delete.guaranteed();
}
delete
.deletingChildrenIfNeeded()
.inBackground((curatorFramework, curatorEvent)
-> callback(context, record, resultHandler, curatorEvent))
.withUnhandledErrorListener((s, throwable)
-> resultHandler.handle(Future.failedFuture(throwable)))
.forPath(getPath(uuid));
} catch (Exception e) {
resultHandler.handle(Future.failedFuture(e));
}
}
});
}
});
}
示例9
/**
* Test deleteNode method
* @throws Exception
*/
@Test
public void testDeleteNode() throws Exception {
CuratorStateManager spyStateManager = spy(new CuratorStateManager());
CuratorFramework mockClient = mock(CuratorFramework.class);
DeleteBuilder mockDeleteBuilder = mock(DeleteBuilder.class);
// Mockito doesn't support mock type-parametrized class, thus suppress the warning
@SuppressWarnings("rawtypes")
BackgroundPathable mockBackPathable = mock(BackgroundPathable.class);
doReturn(mockClient)
.when(spyStateManager).getCuratorClient();
doReturn(true)
.when(mockClient).blockUntilConnected(anyInt(), any(TimeUnit.class));
doReturn(mockDeleteBuilder)
.when(mockClient).delete();
doReturn(mockBackPathable)
.when(mockDeleteBuilder).withVersion(-1);
spyStateManager.initialize(config);
ListenableFuture<Boolean> result = spyStateManager.deleteExecutionState(PATH);
// Verify the node is deleted correctly
verify(mockDeleteBuilder).withVersion(-1);
assertTrue(result.get());
}
示例10
private Pair<CreateBuilder, DeleteBuilder> setupSetupInProgressPathMocks(List<ACL> acls, Stat stat) throws Exception {
when(curatorFactory.clientInstance()).thenReturn(client);
CreateBuilder createBuilder = mock(CreateBuilder.class);
when(createBuilder.withACL(acls)).thenReturn(createBuilder);
when(client.create()).thenReturn(createBuilder);
DeleteBuilder deleteBuilder = mock(DeleteBuilder.class);
when(client.delete()).thenReturn(deleteBuilder);
Pair<CreateBuilder, DeleteBuilder> pair = Pair.of(createBuilder, deleteBuilder);
ExistsBuilder existsBuilder = mock(ExistsBuilder.class);
when(client.checkExists()).thenReturn(existsBuilder);
when(existsBuilder.forPath(HAConfiguration.ATLAS_SERVER_ZK_ROOT_DEFAULT+SetupSteps.SETUP_IN_PROGRESS_NODE)).
thenReturn(stat);
return pair;
}
示例11
@Test
public void deleteShouldProperlyCatchNoNodeExceptionAndReturnFalse() throws Exception {
DeleteBuilder builder = mock(DeleteBuilder.class);
when(curatorFramework.delete()).thenReturn(builder);
when(builder.forPath(ConfigurationType.INDEXING.getZookeeperRoot() + "/bro")).thenThrow(KeeperException.NoNodeException.class);
assertFalse(sensorIndexingConfigService.delete("bro"));
}
示例12
@Test
public void deleteShouldProperlyCatchNonNoNodeExceptionAndThrowRestException() throws Exception {
DeleteBuilder builder = mock(DeleteBuilder.class);
when(curatorFramework.delete()).thenReturn(builder);
when(builder.forPath(ConfigurationType.INDEXING.getZookeeperRoot() + "/bro")).thenThrow(Exception.class);
assertThrows(RestException.class, () -> sensorIndexingConfigService.delete("bro"));
}
示例13
@Test
public void deleteShouldReturnTrueWhenClientSuccessfullyCallsDelete() throws Exception {
DeleteBuilder builder = mock(DeleteBuilder.class);
when(curatorFramework.delete()).thenReturn(builder);
assertTrue(sensorIndexingConfigService.delete("bro"));
verify(curatorFramework).delete();
}
示例14
@Test
public void deleteShouldProperlyCatchNoNodeExceptionAndReturnFalse() throws Exception {
DeleteBuilder builder = mock(DeleteBuilder.class);
when(curatorFramework.delete()).thenReturn(builder);
when(builder.forPath(ConfigurationType.GLOBAL.getZookeeperRoot())).thenThrow(KeeperException.NoNodeException.class);
assertFalse(globalConfigService.delete());
}
示例15
@Test
public void deleteShouldProperlyCatchNonNoNodeExceptionAndThrowRestException() throws Exception {
DeleteBuilder builder = mock(DeleteBuilder.class);
when(curatorFramework.delete()).thenReturn(builder);
when(builder.forPath(ConfigurationType.GLOBAL.getZookeeperRoot())).thenThrow(Exception.class);
assertThrows(RestException.class, () -> assertFalse(globalConfigService.delete()));
}
示例16
@Test
public void deleteShouldReturnTrueWhenClientSuccessfullyCallsDelete() throws Exception {
DeleteBuilder builder = mock(DeleteBuilder.class);
when(curatorFramework.delete()).thenReturn(builder);
assertTrue(globalConfigService.delete());
verify(curatorFramework).delete();
}
示例17
@Test
public void deleteShouldProperlyCatchNoNodeExceptionAndReturnFalse() throws Exception {
DeleteBuilder builder = mock(DeleteBuilder.class);
when(curatorFramework.delete()).thenReturn(builder);
when(builder.forPath(ConfigurationType.PARSER.getZookeeperRoot() + "/bro")).thenThrow(KeeperException.NoNodeException.class);
assertFalse(sensorParserConfigService.delete("bro"));
}
示例18
@Test
public void deleteShouldProperlyCatchNonNoNodeExceptionAndThrowRestException() throws Exception {
DeleteBuilder builder = mock(DeleteBuilder.class);
when(curatorFramework.delete()).thenReturn(builder);
when(builder.forPath(ConfigurationType.PARSER.getZookeeperRoot() + "/bro")).thenThrow(Exception.class);
assertThrows(RestException.class, () -> sensorParserConfigService.delete("bro"));
}
示例19
@Test
public void deleteShouldReturnTrueWhenClientSuccessfullyCallsDelete() throws Exception {
DeleteBuilder builder = mock(DeleteBuilder.class);
when(curatorFramework.delete()).thenReturn(builder);
assertTrue(sensorParserConfigService.delete("bro"));
verify(curatorFramework).delete();
}
示例20
@Test
public void deleteShouldProperlyCatchNoNodeExceptionAndReturnFalse() throws Exception {
DeleteBuilder builder = mock(DeleteBuilder.class);
when(curatorFramework.delete()).thenReturn(builder);
when(builder.forPath(ConfigurationType.ENRICHMENT.getZookeeperRoot() + "/bro")).thenThrow(KeeperException.NoNodeException.class);
assertFalse(sensorEnrichmentConfigService.delete("bro"));
}
示例21
@Test
public void deleteShouldProperlyCatchNonNoNodeExceptionAndThrowRestException() throws Exception {
DeleteBuilder builder = mock(DeleteBuilder.class);
when(curatorFramework.delete()).thenReturn(builder);
when(builder.forPath(ConfigurationType.ENRICHMENT.getZookeeperRoot() + "/bro")).thenThrow(Exception.class);
assertThrows(RestException.class, () -> sensorEnrichmentConfigService.delete("bro"));
}
示例22
@Test
public void deleteShouldReturnTrueWhenClientSuccessfullyCallsDelete() throws Exception {
DeleteBuilder builder = mock(DeleteBuilder.class);
when(curatorFramework.delete()).thenReturn(builder);
doNothing().when(builder).forPath(ConfigurationType.ENRICHMENT.getZookeeperRoot() + "/bro");
assertTrue(sensorEnrichmentConfigService.delete("bro"));
verify(curatorFramework).delete();
}
示例23
private Pair<CreateBuilder, DeleteBuilder> setupSetupInProgressPathMocks(List<ACL> acls) throws Exception {
return setupSetupInProgressPathMocks(acls, null);
}
示例24
@Override
public DeleteBuilder delete() {
return new MockDeleteBuilder();
}
示例25
private Pair<CreateBuilder, DeleteBuilder> setupSetupInProgressPathMocks(List<ACL> acls) throws Exception {
return setupSetupInProgressPathMocks(acls, null);
}