本文整理汇总了Java中org.apache.zookeeper.AsyncCallback类的典型用法代码示例。如果您正苦于以下问题:Java AsyncCallback类的具体用法?Java AsyncCallback怎么用?Java AsyncCallback使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
AsyncCallback类属于org.apache.zookeeper包,在下文中一共展示了AsyncCallback类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testCreateAsync
import org.apache.zookeeper.AsyncCallback; //导入依赖的package包/类
@Test
public void testCreateAsync()
throws IOException, KeeperException, InterruptedException {
AsyncCallback.Create2Callback callback = new AsyncCallback.Create2Callback() {
@Override
public void processResult(int rc, String path, Object ctx, String name, Stat stat) {
// NOP
}
};
zk.create("/foo", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_WITH_TTL, callback, null, 100);
final AtomicLong fakeElapsed = new AtomicLong(0);
ContainerManager containerManager = newContainerManager(fakeElapsed);
containerManager.checkContainers();
Assert.assertNotNull("Ttl node should not have been deleted yet", zk.exists("/foo", false));
fakeElapsed.set(1000);
containerManager.checkContainers();
Assert.assertNull("Ttl node should have been deleted", zk.exists("/foo", false));
}
示例2: asyncSetOfflineInZooKeeper
import org.apache.zookeeper.AsyncCallback; //导入依赖的package包/类
/**
* Set region as OFFLINED up in zookeeper asynchronously.
* @param state
* @return True if we succeeded, false otherwise (State was incorrect or failed
* updating zk).
*/
private boolean asyncSetOfflineInZooKeeper(final RegionState state,
final AsyncCallback.StringCallback cb, final ServerName destination) {
if (!state.isClosed() && !state.isOffline()) {
this.server.abort("Unexpected state trying to OFFLINE; " + state,
new IllegalStateException());
return false;
}
regionStates.updateRegionState(state.getRegion(), State.OFFLINE);
try {
ZKAssign.asyncCreateNodeOffline(watcher, state.getRegion(),
destination, cb, state);
} catch (KeeperException e) {
if (e instanceof NodeExistsException) {
LOG.warn("Node for " + state.getRegion() + " already exists");
} else {
server.abort("Unexpected ZK exception creating/setting node OFFLINE", e);
}
return false;
}
return true;
}
示例3: getLastCommitPositions
import org.apache.zookeeper.AsyncCallback; //导入依赖的package包/类
@Override
public Future<Map<String, DLSN>> getLastCommitPositions() {
final Promise<Map<String, DLSN>> result = new Promise<Map<String, DLSN>>();
try {
this.zkc.get().getChildren(this.zkPath, false, new AsyncCallback.Children2Callback() {
@Override
public void processResult(int rc, String path, Object ctx, List<String> children, Stat stat) {
if (KeeperException.Code.NONODE.intValue() == rc) {
result.setValue(new HashMap<String, DLSN>());
} else if (KeeperException.Code.OK.intValue() != rc) {
result.setException(KeeperException.create(KeeperException.Code.get(rc), path));
} else {
getLastCommitPositions(result, children);
}
}
}, null);
} catch (ZooKeeperClient.ZooKeeperConnectionException zkce) {
result.setException(zkce);
} catch (InterruptedException ie) {
result.setException(new DLInterruptedException("getLastCommitPositions was interrupted", ie));
}
return result;
}
示例4: zkAsyncCreateFullPathOptimistic
import org.apache.zookeeper.AsyncCallback; //导入依赖的package包/类
/**
* Asynchronously create zookeeper path recursively and optimistically
*
* @param zkc Zookeeper client
* @param pathToCreate Zookeeper full path
* @param parentPathShouldNotCreate zookeeper parent path should not be created
* @param data Zookeeper data
* @param acl Acl of the zk path
* @param createMode Create mode of zk path
*/
public static Future<BoxedUnit> zkAsyncCreateFullPathOptimistic(
final ZooKeeperClient zkc,
final String pathToCreate,
final Optional<String> parentPathShouldNotCreate,
final byte[] data,
final List<ACL> acl,
final CreateMode createMode) {
final Promise<BoxedUnit> result = new Promise<BoxedUnit>();
zkAsyncCreateFullPathOptimisticRecursive(zkc, pathToCreate, parentPathShouldNotCreate,
data, acl, createMode, new AsyncCallback.StringCallback() {
@Override
public void processResult(int rc, String path, Object ctx, String name) {
handleKeeperExceptionCode(rc, path, result);
}
}, result);
return result;
}
示例5: asyncSetOfflineInZooKeeper
import org.apache.zookeeper.AsyncCallback; //导入依赖的package包/类
/**
* Set region as OFFLINED up in zookeeper asynchronously.
* @param state
* @return True if we succeeded, false otherwise (State was incorrect or failed
* updating zk).
*/
boolean asyncSetOfflineInZooKeeper(final RegionState state,
final AsyncCallback.StringCallback cb, final Object ctx) {
if (!state.isClosed() && !state.isOffline()) {
new RuntimeException("Unexpected state trying to OFFLINE; " + state);
this.master.abort("Unexpected state trying to OFFLINE; " + state,
new IllegalStateException());
return false;
}
state.update(RegionState.State.OFFLINE);
try {
ZKAssign.asyncCreateNodeOffline(master.getZooKeeper(), state.getRegion(),
this.master.getServerName(), cb, ctx);
} catch (KeeperException e) {
// TODO: this error handling will never execute, as the callback is async.
if (e instanceof NodeExistsException) {
LOG.warn("Node for " + state.getRegion() + " already exists");
} else {
master.abort("Unexpected ZK exception creating/setting node OFFLINE", e);
}
return false;
}
return true;
}
示例6: processKeeperException
import org.apache.zookeeper.AsyncCallback; //导入依赖的package包/类
protected void processKeeperException(final KeeperException e, final Watcher watcher,final DataAction dataAction,
boolean asynchFlag) {
switch (e.code()) {
case BADVERSION:
if (asynchFlag) {
zooKeeper.getData(e.getPath(), watcher, new AsyncCallback.DataCallback() {
@Override
public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) {
ZData zData = ZDataImpl.newInstance(path, data, stat.getVersion());
updateData(zData, dataAction, watcher);
}
}, null);
} else {
}
break;
default:
logger.warn("encounter exception", e);
}
}
示例7: testNotCloseZkWhenPending
import org.apache.zookeeper.AsyncCallback; //导入依赖的package包/类
@Test
public void testNotCloseZkWhenPending() throws Exception {
ZooKeeper mockedZK = mock(ZooKeeper.class);
Exchanger<AsyncCallback.DataCallback> exchanger = new Exchanger<>();
doAnswer(i -> {
exchanger.exchange(i.getArgument(2));
return null;
}).when(mockedZK).getData(anyString(), anyBoolean(),
any(AsyncCallback.DataCallback.class), any());
doAnswer(i -> null).when(mockedZK).close();
when(mockedZK.getState()).thenReturn(ZooKeeper.States.CONNECTED);
RO_ZK.zookeeper = mockedZK;
CompletableFuture<byte[]> future = RO_ZK.get(PATH);
AsyncCallback.DataCallback callback = exchanger.exchange(null);
// 2 * keep alive time to ensure that we will not close the zk when there are pending requests
Thread.sleep(6000);
assertNotNull(RO_ZK.zookeeper);
verify(mockedZK, never()).close();
callback.processResult(Code.OK.intValue(), PATH, null, DATA, null);
assertArrayEquals(DATA, future.get());
// now we will close the idle connection.
waitForIdleConnectionClosed();
verify(mockedZK, times(1)).close();
}
示例8: asyncSetOfflineInZooKeeper
import org.apache.zookeeper.AsyncCallback; //导入依赖的package包/类
/**
* Set region as OFFLINED up in zookeeper asynchronously.
* @param state
* @return True if we succeeded, false otherwise (State was incorrect or failed
* updating zk).
*/
boolean asyncSetOfflineInZooKeeper(final RegionState state,
final AsyncCallback.StringCallback cb, final Object ctx) {
if (!state.isClosed() && !state.isOffline()) {
new RuntimeException("Unexpected state trying to OFFLINE; " + state);
this.master.abort("Unexpected state trying to OFFLINE; " + state,
new IllegalStateException());
return false;
}
state.update(RegionState.State.OFFLINE);
try {
ZKAssign.asyncCreateNodeOffline(master.getZooKeeper(), state.getRegion(),
this.master.getServerName(), cb, ctx);
} catch (KeeperException e) {
master.abort("Unexpected ZK exception creating/setting node OFFLINE", e);
return false;
}
return true;
}
示例9: performBackgroundOperation
import org.apache.zookeeper.AsyncCallback; //导入依赖的package包/类
@Override
public void performBackgroundOperation(final OperationAndData<CuratorMultiTransactionRecord> operationAndData) throws Exception
{
try
{
final TimeTrace trace = client.getZookeeperClient().startTracer("CuratorMultiTransactionImpl-Background");
AsyncCallback.MultiCallback callback = new AsyncCallback.MultiCallback()
{
@Override
public void processResult(int rc, String path, Object ctx, List<OpResult> opResults)
{
trace.commit();
List<CuratorTransactionResult> curatorResults = (opResults != null) ? CuratorTransactionImpl.wrapResults(client, opResults, operationAndData.getData()) : null;
CuratorEvent event = new CuratorEventImpl(client, CuratorEventType.TRANSACTION, rc, path, null, ctx, null, null, null, null, null, curatorResults);
client.processBackgroundOperation(operationAndData, event);
}
};
client.getZooKeeper().multi(operationAndData.getData(), callback, backgrounding.getContext());
}
catch ( Throwable e )
{
backgrounding.checkError(e, null);
}
}
示例10: performBackgroundOperation
import org.apache.zookeeper.AsyncCallback; //导入依赖的package包/类
@Override
public void performBackgroundOperation(final OperationAndData<String> operationAndData) throws Exception
{
final OperationTrace trace = client.getZookeeperClient().startAdvancedTracer("BackgroundSyncImpl");
final String data = operationAndData.getData();
client.getZooKeeper().sync
(
data,
new AsyncCallback.VoidCallback()
{
@Override
public void processResult(int rc, String path, Object ctx)
{
trace.setReturnCode(rc).setRequestBytesLength(data).commit();
CuratorEventImpl event = new CuratorEventImpl(client, CuratorEventType.SYNC, rc, path, null, ctx, null, null, null, null, null, null);
client.processBackgroundOperation(operationAndData, event);
}
},
context
);
}
示例11: performBackgroundOperation
import org.apache.zookeeper.AsyncCallback; //导入依赖的package包/类
@Override
public void performBackgroundOperation(final OperationAndData<String> operationAndData) throws Exception
{
try
{
final OperationTrace trace = client.getZookeeperClient().startAdvancedTracer("GetACLBuilderImpl-Background");
AsyncCallback.ACLCallback callback = new AsyncCallback.ACLCallback()
{
@Override
public void processResult(int rc, String path, Object ctx, List<ACL> acl, Stat stat)
{
trace.setReturnCode(rc).setPath(path).setStat(stat).commit();
CuratorEventImpl event = new CuratorEventImpl(client, CuratorEventType.GET_ACL, rc, path, null, ctx, stat, null, null, null, acl, null);
client.processBackgroundOperation(operationAndData, event);
}
};
client.getZooKeeper().getACL(operationAndData.getData(), responseStat, callback, backgrounding.getContext());
}
catch ( Throwable e )
{
backgrounding.checkError(e, null);
}
}
示例12: asyncSetOfflineInZooKeeper
import org.apache.zookeeper.AsyncCallback; //导入依赖的package包/类
/**
* Set region as OFFLINED up in zookeeper asynchronously.
* @param state
* @return True if we succeeded, false otherwise (State was incorrect or failed
* updating zk).
*/
private boolean asyncSetOfflineInZooKeeper(final RegionState state,
final AsyncCallback.StringCallback cb, final ServerName destination) {
if (!state.isClosed() && !state.isOffline()) {
this.server.abort("Unexpected state trying to OFFLINE; " + state,
new IllegalStateException());
return false;
}
regionStates.updateRegionState(
state.getRegion(), RegionState.State.OFFLINE);
try {
ZKAssign.asyncCreateNodeOffline(watcher, state.getRegion(),
destination, cb, state);
} catch (KeeperException e) {
if (e instanceof NodeExistsException) {
LOG.warn("Node for " + state.getRegion() + " already exists");
} else {
server.abort("Unexpected ZK exception creating/setting node OFFLINE", e);
}
return false;
}
return true;
}
示例13: MultiCallback
import org.apache.zookeeper.AsyncCallback; //导入依赖的package包/类
MultiCallback(int expected, AsyncCallback.VoidCallback cb, Object context) {
this.expected = expected;
this.cb = cb;
this.context = context;
if (expected == 0) {
cb.processResult(Code.OK.intValue(), null, context);
}
}
示例14: getAvailableBookies
import org.apache.zookeeper.AsyncCallback; //导入依赖的package包/类
/**
* This method asynchronously gets the set of available Bookies that the
* dead input bookie's data will be copied over into. If the user passed in
* a specific destination bookie, then just use that one. Otherwise, we'll
* randomly pick one of the other available bookies to use for each ledger
* fragment we are replicating.
*
* @param bookieSrc
* Source bookie that had a failure. We want to replicate the
* ledger fragments that were stored there.
* @param bookieDest
* Optional destination bookie that if passed, we will copy all
* of the ledger fragments from the source bookie over to it.
* @param cb
* RecoverCallback to invoke once all of the data on the dead
* bookie has been recovered and replicated.
* @param context
* Context for the RecoverCallback to call.
*/
private void getAvailableBookies(final InetSocketAddress bookieSrc, final InetSocketAddress bookieDest,
final RecoverCallback cb, final Object context) {
final List<InetSocketAddress> availableBookies = new LinkedList<InetSocketAddress>();
if (bookieDest != null) {
availableBookies.add(bookieDest);
// Now poll ZK to get the active ledgers
getActiveLedgers(bookieSrc, bookieDest, cb, context, availableBookies);
} else {
zk.getChildren(BOOKIES_PATH, null, new AsyncCallback.ChildrenCallback() {
@Override
public void processResult(int rc, String path, Object ctx, List<String> children) {
if (rc != Code.OK.intValue()) {
LOG.error("ZK error getting bookie nodes: ", KeeperException.create(KeeperException.Code
.get(rc), path));
cb.recoverComplete(BKException.Code.ZKException, context);
return;
}
for (String bookieNode : children) {
String parts[] = bookieNode.split(COLON);
if (parts.length < 2) {
LOG.error("Bookie Node retrieved from ZK has invalid name format: " + bookieNode);
cb.recoverComplete(BKException.Code.ZKException, context);
return;
}
availableBookies.add(new InetSocketAddress(parts[0], Integer.parseInt(parts[1])));
}
// Now poll ZK to get the active ledgers
getActiveLedgers(bookieSrc, bookieDest, cb, context, availableBookies);
}
}, null);
}
}
示例15: testGetACLAsync
import org.apache.zookeeper.AsyncCallback; //导入依赖的package包/类
/**
* get acl async
*/
@Test
@ZooConfig(initNodes = {"/node"}, async = true)
public void testGetACLAsync() throws Exception {
zooKeeper.getACL("/node", null, new AsyncCallback.ACLCallback() {
@Override
public void processResult(int rc, String path, Object ctx, List<ACL> acl, Stat stat) {
assertTrue(acl.equals(ZooDefs.Ids.OPEN_ACL_UNSAFE));
assertResult(rc, "/node", path, ctx, stat);
}
}, null);
}