Search in sources :

Example 21 with Watcher

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;
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) Watcher(org.apache.zookeeper.Watcher) CountDownLatch(java.util.concurrent.CountDownLatch) KeeperException(org.apache.zookeeper.KeeperException)

Example 22 with Watcher

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("/");
}
Also used : InetSocketAddress(java.net.InetSocketAddress) StaticHostProvider(org.apache.zookeeper.client.StaticHostProvider) HostProvider(org.apache.zookeeper.client.HostProvider) Watcher(org.apache.zookeeper.Watcher) IOException(java.io.IOException) ZkClientx(com.alibaba.otter.shared.common.utils.zookeeper.ZkClientx) ClientCnxn(org.apache.zookeeper.ClientCnxn) CountDownLatch(java.util.concurrent.CountDownLatch) WatchedEvent(org.apache.zookeeper.WatchedEvent) ZooKeeper(org.apache.zookeeper.ZooKeeper) Stat(org.apache.zookeeper.data.Stat) List(java.util.List) ZooKeeperx(com.alibaba.otter.shared.common.utils.zookeeper.ZooKeeperx) KeeperException(org.apache.zookeeper.KeeperException) Test(org.testng.annotations.Test) BaseOtterTest(com.alibaba.otter.shared.arbitrate.BaseOtterTest)

Example 23 with Watcher

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();
}
Also used : Group(com.twitter.common.zookeeper.Group) Watcher(org.apache.zookeeper.Watcher) ServiceInstance(com.twitter.thrift.ServiceInstance) DynamicHostSet(com.twitter.common.net.pool.DynamicHostSet) IMocksControl(org.easymock.IMocksControl) ZooKeeperClient(com.twitter.common.zookeeper.ZooKeeperClient) Command(com.twitter.common.base.Command) Override(java.lang.Override) BaseZooKeeperTest(com.twitter.common.zookeeper.testing.BaseZooKeeperTest) Test(org.junit.Test)

Example 24 with Watcher

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;
    }
}
Also used : Command(com.twitter.common.base.Command) Watcher(org.apache.zookeeper.Watcher) ZooKeeperConnectionException(com.twitter.common.zookeeper.ZooKeeperClient.ZooKeeperConnectionException) KeeperException(org.apache.zookeeper.KeeperException) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 25 with Watcher

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);
    }
}
Also used : DistributedLock(com.twitter.distributedlog.lock.DistributedLock) NopDistributedLock(com.twitter.distributedlog.lock.NopDistributedLock) ZKDistributedLock(com.twitter.distributedlog.lock.ZKDistributedLock) PermitManager(com.twitter.distributedlog.util.PermitManager) LedgerAllocator(com.twitter.distributedlog.bk.LedgerAllocator) SimpleLedgerAllocator(com.twitter.distributedlog.bk.SimpleLedgerAllocator) Watcher(org.apache.zookeeper.Watcher) IOException(java.io.IOException) AbstractFunction0(scala.runtime.AbstractFunction0) ZKDistributedLock(com.twitter.distributedlog.lock.ZKDistributedLock) OrderedScheduler(com.twitter.distributedlog.util.OrderedScheduler)

Aggregations

Watcher (org.apache.zookeeper.Watcher)78 WatchedEvent (org.apache.zookeeper.WatchedEvent)62 KeeperException (org.apache.zookeeper.KeeperException)35 CountDownLatch (java.util.concurrent.CountDownLatch)25 ZooKeeper (org.apache.zookeeper.ZooKeeper)25 Stat (org.apache.zookeeper.data.Stat)21 Test (org.junit.Test)18 IOException (java.io.IOException)11 AsyncCallback (org.apache.zookeeper.AsyncCallback)10 List (java.util.List)8 Test (org.testng.annotations.Test)8 None (com.linkedin.common.util.None)7 HashSet (java.util.HashSet)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 Map (java.util.Map)5 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 Set (java.util.Set)4 TimeoutException (java.util.concurrent.TimeoutException)4 CountdownWatcher (org.apache.zookeeper.test.ClientBase.CountdownWatcher)4