Search in sources :

Example 46 with Watcher

use of org.apache.zookeeper.Watcher in project zookeeper by apache.

the class ZooInspectorManagerImpl method connect.

/*
     * (non-Javadoc)
     * 
     * @see
     * org.apache.zookeeper.inspector.manager.ZooInspectorManager#connect(java
     * .util.Properties)
     */
public boolean connect(Properties connectionProps) {
    try {
        if (this.zooKeeper == null) {
            String connectString = connectionProps.getProperty(CONNECT_STRING);
            String sessionTimeout = connectionProps.getProperty(SESSION_TIMEOUT);
            String encryptionManager = connectionProps.getProperty(DATA_ENCRYPTION_MANAGER);
            String authScheme = connectionProps.getProperty(AUTH_SCHEME_KEY);
            String authData = connectionProps.getProperty(AUTH_DATA_KEY);
            if (connectString == null || sessionTimeout == null) {
                throw new IllegalArgumentException("Both connect string and session timeout are required.");
            }
            if (encryptionManager == null) {
                this.encryptionManager = new BasicDataEncryptionManager();
            } else {
                Class<?> clazz = Class.forName(encryptionManager);
                if (Arrays.asList(clazz.getInterfaces()).contains(DataEncryptionManager.class)) {
                    this.encryptionManager = (DataEncryptionManager) Class.forName(encryptionManager).newInstance();
                } else {
                    throw new IllegalArgumentException("Data encryption manager must implement DataEncryptionManager interface");
                }
            }
            this.connectString = connectString;
            this.sessionTimeout = Integer.valueOf(sessionTimeout);
            this.zooKeeper = new ZooKeeperRetry(connectString, Integer.valueOf(sessionTimeout), new Watcher() {

                public void process(WatchedEvent event) {
                    if (event.getState() == KeeperState.Expired) {
                        connected = false;
                    }
                }
            });
            if (authData != null && authData.length() > 0) {
                this.zooKeeper.addAuthInfo(authScheme, authData.getBytes());
            }
            ((ZooKeeperRetry) this.zooKeeper).setRetryLimit(10);
            connected = ((ZooKeeperRetry) this.zooKeeper).testConnection();
        }
    } catch (Exception e) {
        connected = false;
        e.printStackTrace();
    }
    if (!connected) {
        disconnect();
    } else {
        this.nodesCache = new NodesCache(zooKeeper);
    }
    return connected;
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) ZooKeeperRetry(org.apache.zookeeper.retry.ZooKeeperRetry) Watcher(org.apache.zookeeper.Watcher) BasicDataEncryptionManager(org.apache.zookeeper.inspector.encryption.BasicDataEncryptionManager) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException)

Example 47 with Watcher

use of org.apache.zookeeper.Watcher in project zookeeper by apache.

the class QuorumTest method testNoLogBeforeLeaderEstablishment.

/**
     * Tests if closeSession can be logged before a leader gets established, which
     * could lead to a locked-out follower (see ZOOKEEPER-790). 
     * 
     * The test works as follows. It has a client connecting to a follower f and
     * sending batches of 1,000 updates. The goal is that f has a zxid higher than
     * all other servers in the initial leader election. This way we can crash and
     * recover the follower so that the follower believes it is the leader once it
     * recovers (LE optimization: once a server receives a message from all other 
     * servers, it picks a leader.
     * 
     * It also makes the session timeout very short so that we force the false 
     * leader to close the session and write it to the log in the buggy code (before 
     * ZOOKEEPER-790). Once f drops leadership and finds the current leader, its epoch
     * is higher, and it rejects the leader. Now, if we prevent the leader from closing
     * the session by only starting up (see Leader.lead()) once it obtains a quorum of 
     * supporters, then f will find the current leader and support it because it won't
     * have a highe epoch.
     * 
     */
@Test
public void testNoLogBeforeLeaderEstablishment() throws Exception {
    final Semaphore sem = new Semaphore(0);
    qu = new QuorumUtil(2, 10);
    qu.startQuorum();
    int index = 1;
    while (qu.getPeer(index).peer.leader == null) index++;
    Leader leader = qu.getPeer(index).peer.leader;
    Assert.assertNotNull(leader);
    /*
         * Reusing the index variable to select a follower to connect to
         */
    index = (index == 1) ? 2 : 1;
    ZooKeeper zk = new DisconnectableZooKeeper("127.0.0.1:" + qu.getPeer(index).peer.getClientPort(), ClientBase.CONNECTION_TIMEOUT, new Watcher() {

        public void process(WatchedEvent event) {
        }
    });
    zk.create("/blah", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    for (int i = 0; i < 50000; i++) {
        zk.setData("/blah", new byte[0], -1, new AsyncCallback.StatCallback() {

            public void processResult(int rc, String path, Object ctx, Stat stat) {
                counter++;
                if (rc != 0) {
                    errors++;
                }
                if (counter == 20000) {
                    sem.release();
                }
            }
        }, null);
        if (i == 5000) {
            qu.shutdown(index);
            LOG.info("Shutting down s1");
        }
        if (i == 12000) {
            qu.start(index);
            LOG.info("Setting up server: " + index);
        }
        if ((i % 1000) == 0) {
            Thread.sleep(500);
        }
    }
    // Wait until all updates return
    sem.tryAcquire(15, TimeUnit.SECONDS);
    // Verify that server is following and has the same epoch as the leader
    Assert.assertTrue("Not following", qu.getPeer(index).peer.follower != null);
    long epochF = (qu.getPeer(index).peer.getActiveServer().getZxid() >> 32L);
    long epochL = (leader.getEpoch() >> 32L);
    Assert.assertTrue("Zxid: " + qu.getPeer(index).peer.getActiveServer().getZxid() + "Current epoch: " + epochF, epochF == epochL);
    zk.close();
}
Also used : Leader(org.apache.zookeeper.server.quorum.Leader) AsyncCallback(org.apache.zookeeper.AsyncCallback) Watcher(org.apache.zookeeper.Watcher) CountdownWatcher(org.apache.zookeeper.test.ClientBase.CountdownWatcher) Semaphore(java.util.concurrent.Semaphore) WatchedEvent(org.apache.zookeeper.WatchedEvent) ZooKeeper(org.apache.zookeeper.ZooKeeper) Stat(org.apache.zookeeper.data.Stat) Test(org.junit.Test)

Example 48 with Watcher

use of org.apache.zookeeper.Watcher in project zookeeper by apache.

the class ReconfigTest method createAdminHandles.

public static ZooKeeperAdmin[] createAdminHandles(QuorumUtil qu) throws IOException {
    // create an extra handle, so we can index the handles from 1 to qu.ALL
    // using the server id.
    ZooKeeperAdmin[] zkAdminArr = new ZooKeeperAdmin[qu.ALL + 1];
    // not used.
    zkAdminArr[0] = null;
    for (int i = 1; i <= qu.ALL; i++) {
        // server ids are 1, 2 and 3
        zkAdminArr[i] = new ZooKeeperAdmin("127.0.0.1:" + qu.getPeer(i).peer.getClientPort(), ClientBase.CONNECTION_TIMEOUT, new Watcher() {

            public void process(WatchedEvent event) {
            }
        });
        zkAdminArr[i].addAuthInfo("digest", "super:test".getBytes());
    }
    return zkAdminArr;
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) Watcher(org.apache.zookeeper.Watcher) ZooKeeperAdmin(org.apache.zookeeper.admin.ZooKeeperAdmin)

Example 49 with Watcher

use of org.apache.zookeeper.Watcher in project rest.li by linkedin.

the class SymlinkAwareZooKeeperTest method testInvalidSymlinkWithExists.

@Test
public void testInvalidSymlinkWithExists() throws ExecutionException, InterruptedException {
    final CountDownLatch latch1 = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    final AsyncCallback.StatCallback callback = new AsyncCallback.StatCallback() {

        @Override
        public void processResult(int rc, String path, Object ctx, Stat stat) {
            Assert.assertEquals(path, "/foo/$link");
            KeeperException.Code result = KeeperException.Code.get(rc);
            Assert.assertEquals(result, KeeperException.Code.NONODE);
            latch1.countDown();
        }
    };
    final Watcher watcher = new Watcher() {

        @Override
        public void process(WatchedEvent event) {
            Assert.assertEquals(event.getType(), Event.EventType.NodeDataChanged);
            latch2.countDown();
        }
    };
    FutureCallback<None> fcb = new FutureCallback<None>();
    _zkClient.setSymlinkData("/foo/$link", "INVALID", fcb);
    fcb.get();
    _zkClient.getZooKeeper().exists("/foo/$link", watcher, callback, null);
    latch1.await(30, TimeUnit.SECONDS);
    _zkClient.setSymlinkData("/foo/$link", "/foo/bar", fcb);
    if (!latch2.await(30, TimeUnit.SECONDS)) {
        Assert.fail("Exists Watch is not triggered");
    }
}
Also used : AsyncCallback(org.apache.zookeeper.AsyncCallback) Watcher(org.apache.zookeeper.Watcher) CountDownLatch(java.util.concurrent.CountDownLatch) WatchedEvent(org.apache.zookeeper.WatchedEvent) Stat(org.apache.zookeeper.data.Stat) None(com.linkedin.common.util.None) KeeperException(org.apache.zookeeper.KeeperException) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 50 with Watcher

use of org.apache.zookeeper.Watcher in project rest.li by linkedin.

the class SymlinkAwareZooKeeperTest method testSymlinkWithExistWatch2.

@Test
public void testSymlinkWithExistWatch2() throws InterruptedException, ExecutionException {
    final CountDownLatch latch = new CountDownLatch(1);
    final CountDownLatch latch2 = new CountDownLatch(1);
    final AsyncCallback.StatCallback existCallback = new AsyncCallback.StatCallback() {

        @Override
        public void processResult(int rc, String path, Object ctx, Stat stat) {
            KeeperException.Code result = KeeperException.Code.get(rc);
            Assert.assertEquals(result, KeeperException.Code.OK);
            latch.countDown();
        }
    };
    Watcher existWatch = new Watcher() {

        @Override
        public void process(WatchedEvent event) {
            Assert.assertEquals(event.getType(), Event.EventType.NodeDataChanged);
            _zkClient.getZooKeeper().exists(event.getPath(), null, existCallback, null);
        }
    };
    AsyncCallback.StatCallback existCallback2 = new AsyncCallback.StatCallback() {

        @Override
        public void processResult(int rc, String path, Object ctx, Stat stat) {
            KeeperException.Code result = KeeperException.Code.get(rc);
            Assert.assertEquals(result, KeeperException.Code.NONODE);
            latch2.countDown();
        }
    };
    // symlink: /foo/$link/foo -> /foo/bar/foo, which doesn't exist
    _zkClient.getZooKeeper().exists("/foo/$link/foo", existWatch, existCallback2, null);
    latch2.await(30, TimeUnit.SECONDS);
    // update symlink. now it points to /bar/foo, which does exist.
    _zkClient.setSymlinkData("/foo/$link", "/bar", new FutureCallback<None>());
    latch.await(30, TimeUnit.SECONDS);
    // restore symlink
    _zkClient.setSymlinkData("/foo/$link", "/foo/bar", new FutureCallback<None>());
}
Also used : AsyncCallback(org.apache.zookeeper.AsyncCallback) Watcher(org.apache.zookeeper.Watcher) CountDownLatch(java.util.concurrent.CountDownLatch) WatchedEvent(org.apache.zookeeper.WatchedEvent) Stat(org.apache.zookeeper.data.Stat) None(com.linkedin.common.util.None) KeeperException(org.apache.zookeeper.KeeperException) Test(org.testng.annotations.Test)

Aggregations

Watcher (org.apache.zookeeper.Watcher)78 WatchedEvent (org.apache.zookeeper.WatchedEvent)62 KeeperException (org.apache.zookeeper.KeeperException)34 CountDownLatch (java.util.concurrent.CountDownLatch)26 ZooKeeper (org.apache.zookeeper.ZooKeeper)24 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 Set (java.util.Set)5 HashMap (java.util.HashMap)4 TimeoutException (java.util.concurrent.TimeoutException)4 FutureCallback (com.google.common.util.concurrent.FutureCallback)3 FutureCallback (com.linkedin.common.callback.FutureCallback)3