use of org.apache.zookeeper.Watcher in project KeptCollections by anthonyu.
the class KeptLock method lockIt.
private boolean lockIt(final long t, final TimeUnit tu) throws KeeperException, InterruptedException {
final CountDownLatch latch = new CountDownLatch(1);
// convert the given time to milliseconds and add it to the current time
final long last = System.currentTimeMillis() + TimeUnit.MILLISECONDS.convert(t, tu);
do if (this.keeper.exists(this.znode, new Watcher() {
@Override
public void process(final WatchedEvent event) {
if (event.getType() == EventType.NodeDeleted)
latch.countDown();
else if (event.getType() == EventType.NodeCreated)
// ignore it
;
else
throw new RuntimeException("unexpected event type" + event.getType());
}
}) != null) {
if (!latch.await(t, tu))
try {
this.keeper.create(this.znode, ManagementFactory.getRuntimeMXBean().getName().getBytes(), this.acl, CreateMode.EPHEMERAL);
return true;
} catch (final KeeperException.NodeExistsException e) {
// ignore it
} catch (final KeeperException e) {
throw e;
}
else
return false;
} else
try {
this.keeper.create(this.znode, ManagementFactory.getRuntimeMXBean().getName().getBytes(), this.acl, CreateMode.EPHEMERAL);
return true;
} catch (final KeeperException.NodeExistsException e) {
// ignore it
} catch (final KeeperException e) {
throw e;
} while (System.currentTimeMillis() < last);
return false;
}
use of org.apache.zookeeper.Watcher in project otter by alibaba.
the class ZooKeeperClientTest method testClient.
@Test
public void testClient() {
ZkClientx zk = ZooKeeperClient.getInstance();
// 强制获取zk中的地址信息
final ZooKeeper zkp = ((ZooKeeperx) zk.getConnection()).getZookeeper();
ClientCnxn cnxn = (ClientCnxn) ReflectionUtils.getField(clientCnxnField, zkp);
HostProvider hostProvider = (HostProvider) ReflectionUtils.getField(hostProviderField, cnxn);
List<InetSocketAddress> serverAddrs = (List<InetSocketAddress>) ReflectionUtils.getField(serverAddressesField, hostProvider);
want.number(serverAddrs.size()).isEqualTo(3);
String s1 = serverAddrs.get(0).getAddress().getHostAddress() + ":" + serverAddrs.get(0).getPort();
want.string(s1).isEqualTo(cluster1);
Stat stat = new Stat();
try {
zkp.getChildren("/otter/channel/304/388", false, stat);
System.out.println(stat.getCversion());
} catch (KeeperException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
} catch (InterruptedException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
// 测试下session timeout
final CountDownLatch latch = new CountDownLatch(1);
new Thread() {
public void run() {
try {
zkp.getChildren("/", false);
} catch (KeeperException e1) {
want.fail();
} catch (InterruptedException e1) {
want.fail();
}
int sessionTimeout = zkp.getSessionTimeout();
long sessionId = zkp.getSessionId();
byte[] passwd = zkp.getSessionPasswd();
try {
ZooKeeper newZk = new ZooKeeper(cluster1, sessionTimeout, new Watcher() {
public void process(WatchedEvent event) {
// do nothing
}
}, sessionId, passwd);
// 用老的sessionId连接上去,进行一次close操作后,让原先正在使用的出现SESSION_EXPIRED
newZk.close();
} catch (IOException e) {
want.fail();
} catch (InterruptedException e) {
want.fail();
}
latch.countDown();
}
}.start();
try {
latch.await();
} catch (InterruptedException e) {
want.fail();
}
zk.getChildren("/");
}
use of org.apache.zookeeper.Watcher in project commons by twitter.
the class ServerSetImplTest method testUnwatchOnException.
@Test
public void testUnwatchOnException() throws Exception {
IMocksControl control = createControl();
ZooKeeperClient zkClient = control.createMock(ZooKeeperClient.class);
Watcher onExpirationWatcher = control.createMock(Watcher.class);
expect(zkClient.registerExpirationHandler(anyObject(Command.class))).andReturn(onExpirationWatcher);
expect(zkClient.get()).andThrow(new InterruptedException());
expect(zkClient.unregister(onExpirationWatcher)).andReturn(true);
control.replay();
Group group = new Group(zkClient, ZooDefs.Ids.OPEN_ACL_UNSAFE, "/blabla");
ServerSetImpl serverset = new ServerSetImpl(zkClient, group);
try {
serverset.watch(new DynamicHostSet.HostChangeMonitor<ServiceInstance>() {
@Override
public void onChange(ImmutableSet<ServiceInstance> hostSet) {
}
});
fail("Expected MonitorException");
} catch (DynamicHostSet.MonitorException e) {
// expected
}
control.verify();
}
use of org.apache.zookeeper.Watcher in project commons by twitter.
the class ZooKeeperNode method init.
/**
* Initialize zookeeper tracking for this {@link Supplier}. Once this call returns, this object
* will be tracking data in zookeeper.
*
* @throws InterruptedException if the underlying zookeeper server transaction is interrupted
* @throws KeeperException if the server signals an error
* @throws ZooKeeperConnectionException if there was a problem connecting to the zookeeper
* cluster
*/
@VisibleForTesting
void init() throws InterruptedException, KeeperException, ZooKeeperConnectionException {
Watcher watcher = zkClient.registerExpirationHandler(new Command() {
@Override
public void execute() {
try {
synchronized (safeToRewatchLock) {
if (safeToRewatch) {
tryWatchDataNode();
}
}
} catch (InterruptedException e) {
LOG.log(Level.WARNING, "Interrupted while trying to re-establish watch.", e);
Thread.currentThread().interrupt();
}
}
});
try {
/*
* Synchronize to prevent the race of watchDataNode completing and then the session expiring
* before we update safeToRewatch.
*/
synchronized (safeToRewatchLock) {
watchDataNode();
safeToRewatch = true;
}
} catch (InterruptedException e) {
zkClient.unregister(watcher);
throw e;
} catch (KeeperException e) {
zkClient.unregister(watcher);
throw e;
} catch (ZooKeeperConnectionException e) {
zkClient.unregister(watcher);
throw e;
}
}
use of org.apache.zookeeper.Watcher in project distributedlog by twitter.
the class BKDistributedLogManager method createWriteHandler.
private void createWriteHandler(ZKLogMetadataForWriter logMetadata, boolean lockHandler, final Promise<BKLogWriteHandler> createPromise) {
OrderedScheduler lockStateExecutor = getLockStateExecutor(true);
// Build the locks
DistributedLock lock;
if (conf.isWriteLockEnabled()) {
lock = new ZKDistributedLock(lockStateExecutor, getLockFactory(true), logMetadata.getLockPath(), conf.getLockTimeoutMilliSeconds(), statsLogger);
} else {
lock = NopDistributedLock.INSTANCE;
}
// Build the ledger allocator
LedgerAllocator allocator;
try {
allocator = createLedgerAllocator(logMetadata);
} catch (IOException e) {
FutureUtils.setException(createPromise, e);
return;
}
// Make sure writer handler created before resources are initialized
final BKLogWriteHandler writeHandler = new BKLogWriteHandler(logMetadata, conf, writerZKCBuilder, writerBKCBuilder, writerMetadataStore, scheduler, allocator, statsLogger, perLogStatsLogger, alertStatsLogger, clientId, regionId, writeLimiter, featureProvider, dynConf, lock);
PermitManager manager = getLogSegmentRollingPermitManager();
if (manager instanceof Watcher) {
writeHandler.register((Watcher) manager);
}
if (lockHandler) {
writeHandler.lockHandler().addEventListener(new FutureEventListener<DistributedLock>() {
@Override
public void onSuccess(DistributedLock lock) {
FutureUtils.setValue(createPromise, writeHandler);
}
@Override
public void onFailure(final Throwable cause) {
writeHandler.asyncClose().ensure(new AbstractFunction0<BoxedUnit>() {
@Override
public BoxedUnit apply() {
FutureUtils.setException(createPromise, cause);
return BoxedUnit.UNIT;
}
});
}
});
} else {
FutureUtils.setValue(createPromise, writeHandler);
}
}
Aggregations