Search in sources :

Example 6 with ConnectionLossException

use of org.apache.zookeeper.KeeperException.ConnectionLossException in project zookeeper by apache.

the class InstanceManager method reconfigureInstance.

public void reconfigureInstance(String name, String params) throws NoAssignmentException, InterruptedException, KeeperException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Reconfiguring " + name + " with " + params);
    }
    Assigned assigned = instanceToAssignment.get(name);
    if (assigned == null) {
        throw new NoAssignmentException();
    }
    KeeperException lastException = null;
    for (int i = 0; i < maxTries; i++) {
        try {
            zk.setData(assignmentsNode + '/' + assigned.container + '/' + name, ("update " + params).getBytes(), -1);
            break;
        } catch (ConnectionLossException e) {
            lastException = e;
        }
    }
    if (lastException != null) {
        throw lastException;
    }
}
Also used : ConnectionLossException(org.apache.zookeeper.KeeperException.ConnectionLossException) KeeperException(org.apache.zookeeper.KeeperException)

Example 7 with ConnectionLossException

use of org.apache.zookeeper.KeeperException.ConnectionLossException in project zookeeper by apache.

the class ObserverTest method testObserver.

/**
 * This test ensures two things:
 * 1. That Observers can successfully proxy requests to the ensemble.
 * 2. That Observers don't participate in leader elections.
 * The second is tested by constructing an ensemble where a leader would
 * be elected if and only if an Observer voted.
 * @throws Exception
 */
@Test
public void testObserver() throws Exception {
    ClientBase.setupTestEnv();
    // We expect two notifications before we want to continue
    latch = new CountDownLatch(2);
    final int PORT_QP1 = PortAssignment.unique();
    final int PORT_QP2 = PortAssignment.unique();
    final int PORT_OBS = PortAssignment.unique();
    final int PORT_QP_LE1 = PortAssignment.unique();
    final int PORT_QP_LE2 = PortAssignment.unique();
    final int PORT_OBS_LE = PortAssignment.unique();
    final int CLIENT_PORT_QP1 = PortAssignment.unique();
    final int CLIENT_PORT_QP2 = PortAssignment.unique();
    final int CLIENT_PORT_OBS = PortAssignment.unique();
    String quorumCfgSection = "server.1=127.0.0.1:" + (PORT_QP1) + ":" + (PORT_QP_LE1) + ";" + CLIENT_PORT_QP1 + "\nserver.2=127.0.0.1:" + (PORT_QP2) + ":" + (PORT_QP_LE2) + ";" + CLIENT_PORT_QP2 + "\nserver.3=127.0.0.1:" + (PORT_OBS) + ":" + (PORT_OBS_LE) + ":observer" + ";" + CLIENT_PORT_OBS;
    MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection);
    MainThread q2 = new MainThread(2, CLIENT_PORT_QP2, quorumCfgSection);
    MainThread q3 = new MainThread(3, CLIENT_PORT_OBS, quorumCfgSection);
    q1.start();
    q2.start();
    q3.start();
    Assert.assertTrue("waiting for server 1 being up", ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP1, CONNECTION_TIMEOUT));
    Assert.assertTrue("waiting for server 2 being up", ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP2, CONNECTION_TIMEOUT));
    Assert.assertTrue("waiting for server 3 being up", ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_OBS, CONNECTION_TIMEOUT));
    zk = new ZooKeeper("127.0.0.1:" + CLIENT_PORT_OBS, ClientBase.CONNECTION_TIMEOUT, this);
    zk.create("/obstest", "test".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    // Assert that commands are getting forwarded correctly
    Assert.assertEquals(new String(zk.getData("/obstest", null, null)), "test");
    // Now check that other commands don't blow everything up
    zk.sync("/", null, null);
    zk.setData("/obstest", "test2".getBytes(), -1);
    zk.getChildren("/", false);
    Assert.assertEquals(zk.getState(), States.CONNECTED);
    LOG.info("Shutting down server 2");
    // Now kill one of the other real servers
    q2.shutdown();
    Assert.assertTrue("Waiting for server 2 to shut down", ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP2, ClientBase.CONNECTION_TIMEOUT));
    LOG.info("Server 2 down");
    // Now the resulting ensemble shouldn't be quorate
    latch.await();
    Assert.assertNotSame("Client is still connected to non-quorate cluster", KeeperState.SyncConnected, lastEvent.getState());
    LOG.info("Latch returned");
    try {
        Assert.assertFalse("Shouldn't get a response when cluster not quorate!", new String(zk.getData("/obstest", null, null)).equals("test"));
    } catch (ConnectionLossException c) {
        LOG.info("Connection loss exception caught - ensemble not quorate (this is expected)");
    }
    latch = new CountDownLatch(1);
    LOG.info("Restarting server 2");
    // Bring it back
    q2 = new MainThread(2, CLIENT_PORT_QP2, quorumCfgSection);
    q2.start();
    LOG.info("Waiting for server 2 to come up");
    Assert.assertTrue("waiting for server 2 being up", ClientBase.waitForServerUp("127.0.0.1:" + CLIENT_PORT_QP2, CONNECTION_TIMEOUT));
    LOG.info("Server 2 started, waiting for latch");
    latch.await();
    // It's possible our session expired - but this is ok, shows we
    // were able to talk to the ensemble
    Assert.assertTrue("Client didn't reconnect to quorate ensemble (state was" + lastEvent.getState() + ")", (KeeperState.SyncConnected == lastEvent.getState() || KeeperState.Expired == lastEvent.getState()));
    LOG.info("Shutting down all servers");
    q1.shutdown();
    q2.shutdown();
    q3.shutdown();
    LOG.info("Closing zk client");
    zk.close();
    Assert.assertTrue("Waiting for server 1 to shut down", ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP1, ClientBase.CONNECTION_TIMEOUT));
    Assert.assertTrue("Waiting for server 2 to shut down", ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_QP2, ClientBase.CONNECTION_TIMEOUT));
    Assert.assertTrue("Waiting for server 3 to shut down", ClientBase.waitForServerDown("127.0.0.1:" + CLIENT_PORT_OBS, ClientBase.CONNECTION_TIMEOUT));
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) CountDownLatch(java.util.concurrent.CountDownLatch) ConnectionLossException(org.apache.zookeeper.KeeperException.ConnectionLossException) Test(org.junit.Test)

Example 8 with ConnectionLossException

use of org.apache.zookeeper.KeeperException.ConnectionLossException in project bookkeeper by apache.

the class BookKeeperTest method testConstructionNotConnectedExplicitZk.

@Test
public void testConstructionNotConnectedExplicitZk() throws Exception {
    ClientConfiguration conf = new ClientConfiguration();
    conf.setZkServers(zkUtil.getZooKeeperConnectString()).setZkTimeout(20000);
    CountDownLatch l = new CountDownLatch(1);
    zkUtil.sleepServer(200, TimeUnit.MILLISECONDS, l);
    l.await();
    ZooKeeper zk = new ZooKeeper(zkUtil.getZooKeeperConnectString(), 50, event -> {
    });
    assertFalse("ZK shouldn't have connected yet", zk.getState().isConnected());
    try {
        BookKeeper bkc = new BookKeeper(conf, zk);
        fail("Shouldn't be able to construct with unconnected zk");
    } catch (IOException cle) {
        // correct behaviour
        assertTrue(cle.getCause() instanceof ConnectionLossException);
    }
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) ConnectionLossException(org.apache.zookeeper.KeeperException.ConnectionLossException) ClientConfiguration(org.apache.bookkeeper.conf.ClientConfiguration) Test(org.junit.Test)

Example 9 with ConnectionLossException

use of org.apache.zookeeper.KeeperException.ConnectionLossException in project accumulo by apache.

the class ZooReaderWriterTest method testMutateWithRetryOnSetData.

@Test
public void testMutateWithRetryOnSetData() throws Exception {
    final String path = "/foo";
    final byte[] value = new byte[] { 0 };
    final List<ACL> acls = Collections.emptyList();
    final byte[] mutatedBytes = new byte[] { 1 };
    Mutator mutator = new Mutator() {

        @Override
        public byte[] mutate(byte[] currentValue) throws Exception {
            return mutatedBytes;
        }
    };
    Method getDataMethod = ZooReaderWriter.class.getMethod("getData", String.class, boolean.class, Stat.class);
    zrw = EasyMock.createMockBuilder(ZooReaderWriter.class).addMockedMethods("getRetryFactory", "getZooKeeper").addMockedMethod(getDataMethod).createMock();
    EasyMock.expect(zrw.getRetryFactory()).andReturn(retryFactory).anyTimes();
    EasyMock.expect(zrw.getZooKeeper()).andReturn(zk).anyTimes();
    Stat stat = new Stat();
    zk.create(path, value, acls, CreateMode.PERSISTENT);
    EasyMock.expectLastCall().andThrow(new NodeExistsException()).once();
    EasyMock.expect(zrw.getData(path, false, stat)).andReturn(new byte[] { 3 }).times(2);
    // BadVersionException should retry
    EasyMock.expect(zk.setData(path, mutatedBytes, 0)).andThrow(new ConnectionLossException());
    EasyMock.expect(retry.canRetry()).andReturn(true);
    retry.useRetry();
    EasyMock.expectLastCall();
    retry.waitForNextAttempt();
    EasyMock.expectLastCall();
    // Let 2nd setData succeed
    EasyMock.expect(zk.setData(path, mutatedBytes, 0)).andReturn(null);
    EasyMock.replay(zk, zrw, retryFactory, retry);
    Assert.assertArrayEquals(new byte[] { 1 }, zrw.mutate(path, value, acls, mutator));
    EasyMock.verify(zk, zrw, retryFactory, retry);
}
Also used : Stat(org.apache.zookeeper.data.Stat) Mutator(org.apache.accumulo.fate.zookeeper.IZooReaderWriter.Mutator) NodeExistsException(org.apache.zookeeper.KeeperException.NodeExistsException) ACL(org.apache.zookeeper.data.ACL) Method(java.lang.reflect.Method) ConnectionLossException(org.apache.zookeeper.KeeperException.ConnectionLossException) Test(org.junit.Test)

Example 10 with ConnectionLossException

use of org.apache.zookeeper.KeeperException.ConnectionLossException in project oozie by apache.

the class TestZKLocksService method testRetriableRelease.

public void testRetriableRelease() throws Exception {
    final String path = UUID.randomUUID().toString();
    ZKLocksService zkls = new ZKLocksService();
    try {
        zkls.init(Services.get());
        InterProcessReadWriteLock lockEntry = Mockito.mock(InterProcessReadWriteLock.class);
        final InterProcessMutex writeLock = Mockito.mock(InterProcessMutex.class);
        Mockito.when(lockEntry.writeLock()).thenReturn(writeLock);
        Mockito.doThrow(new ConnectionLossException()).when(writeLock).release();
        Mockito.doNothing().when(writeLock).acquire();
        // put mocked lockEntry
        zkls.getLocks().putIfAbsent(path, lockEntry);
        LockToken lock = zkls.getWriteLock(path, -1);
        final boolean[] lockReleased = new boolean[] { false };
        Runnable exceptionStopper = new Runnable() {

            @Override
            public void run() {
                try {
                    // Stop the exception on release() after some time in other thread
                    Thread.sleep(TimeUnit.SECONDS.toMillis(13));
                    Mockito.doAnswer(new Answer() {

                        @Override
                        public Object answer(InvocationOnMock invocation) throws Throwable {
                            lockReleased[0] = true;
                            return null;
                        }
                    }).when(writeLock).release();
                } catch (Exception e) {
                    log.error(e);
                    fail("Test case failed due to " + e.getMessage());
                }
            }
        };
        new Thread(exceptionStopper).start();
        // Try to release the lock
        lock.release();
        assertEquals("Failing the test case. The lock should have been released", true, lockReleased[0]);
    } finally {
        zkls.destroy();
    }
}
Also used : LockToken(org.apache.oozie.lock.LockToken) ZKLockToken(org.apache.oozie.service.ZKLocksService.ZKLockToken) ConnectionLossException(org.apache.zookeeper.KeeperException.ConnectionLossException) InterProcessReadWriteLock(org.apache.curator.framework.recipes.locks.InterProcessReadWriteLock) InterProcessMutex(org.apache.curator.framework.recipes.locks.InterProcessMutex) ConnectionLossException(org.apache.zookeeper.KeeperException.ConnectionLossException) Answer(org.mockito.stubbing.Answer) InvocationOnMock(org.mockito.invocation.InvocationOnMock)

Aggregations

ConnectionLossException (org.apache.zookeeper.KeeperException.ConnectionLossException)14 KeeperException (org.apache.zookeeper.KeeperException)8 Test (org.junit.Test)6 ZooKeeper (org.apache.zookeeper.ZooKeeper)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 NoNodeException (org.apache.zookeeper.KeeperException.NoNodeException)4 Stat (org.apache.zookeeper.data.Stat)4 IOException (java.io.IOException)3 TimeoutException (java.util.concurrent.TimeoutException)2 ZooKeeperException (org.apache.solr.common.cloud.ZooKeeperException)2 NodeExistsException (org.apache.zookeeper.KeeperException.NodeExistsException)2 InvocationOnMock (org.mockito.invocation.InvocationOnMock)2 JobId (com.spotify.helios.common.descriptors.JobId)1 TaskStatusEvent (com.spotify.helios.common.descriptors.TaskStatusEvent)1 DefaultZooKeeperClient (com.spotify.helios.servicescommon.coordination.DefaultZooKeeperClient)1 ZooKeeperClient (com.spotify.helios.servicescommon.coordination.ZooKeeperClient)1 ZooKeeperConnectionException (com.twitter.common.zookeeper.ZooKeeperClient.ZooKeeperConnectionException)1 BaseZooKeeperTest (com.twitter.common.zookeeper.testing.BaseZooKeeperTest)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 Method (java.lang.reflect.Method)1