private DistributedKeyManager getKeyManager(Injector injector, boolean expectLeader) throws Exception { ZKClientService zk = injector.getInstance(ZKClientService.class); zk.startAndWait(); WaitableDistributedKeyManager keyManager = new WaitableDistributedKeyManager(injector.getInstance(CConfiguration.class), injector.getInstance(Key.get(new TypeLiteral<Codec<KeyIdentifier>>() { })), zk); keyManager.startAndWait(); if (expectLeader) { keyManager.waitForLeader(5000, TimeUnit.MILLISECONDS); } return keyManager; }
public void waitForCurrentKey(long duration, TimeUnit unit) throws InterruptedException, TimeoutException { if (keyManager instanceof WaitableDistributedKeyManager) { WaitableDistributedKeyManager waitKeyManager = (WaitableDistributedKeyManager) keyManager; Stopwatch timer = new Stopwatch().start(); boolean hasKey = false; do { hasKey = waitKeyManager.getCurrentKey() != null; if (!hasKey) { unit.sleep(duration / 10); } } while (!hasKey && timer.elapsedTime(unit) < duration); if (!hasKey) { throw new TimeoutException("Timed out waiting for current key to be set"); } } } }
public void waitForKey(int keyId, long duration, TimeUnit unit) throws InterruptedException, TimeoutException { if (keyManager instanceof WaitableDistributedKeyManager) { WaitableDistributedKeyManager waitKeyManager = (WaitableDistributedKeyManager) keyManager; Stopwatch timer = new Stopwatch().start(); boolean hasKey = false; do { hasKey = waitKeyManager.hasKey(keyId); if (!hasKey) { unit.sleep(duration / 10); } } while (!hasKey && timer.elapsedTime(unit) < duration); if (!hasKey) { throw new TimeoutException("Timed out waiting for key " + keyId); } } }
public KeyIdentifier getCurrentKey() { if (keyManager instanceof WaitableDistributedKeyManager) { return ((WaitableDistributedKeyManager) keyManager).getCurrentKey(); } return null; }