Search in sources :

Example 1 with NotReadOnlyException

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

the class ReadOnlyModeTest method testMultiTransaction.

/**
     * Test write operations using multi request.
     */
@Test(timeout = 90000)
public void testMultiTransaction() throws Exception {
    CountdownWatcher watcher = new CountdownWatcher();
    ZooKeeper zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT, watcher, true);
    // ensure zk got connected
    watcher.waitForConnected(CONNECTION_TIMEOUT);
    final String data = "Data to be read in RO mode";
    final String node1 = "/tnode1";
    final String node2 = "/tnode2";
    zk.create(node1, data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    watcher.reset();
    qu.shutdown(2);
    watcher.waitForConnected(CONNECTION_TIMEOUT);
    Assert.assertEquals("Should be in r-o mode", States.CONNECTEDREADONLY, zk.getState());
    // read operation during r/o mode
    String remoteData = new String(zk.getData(node1, false, null));
    Assert.assertEquals("Failed to read data in r-o mode", data, remoteData);
    try {
        Transaction transaction = zk.transaction();
        transaction.setData(node1, "no way".getBytes(), -1);
        transaction.create(node2, data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        transaction.commit();
        Assert.fail("Write operation using multi-transaction" + " api has succeeded during RO mode");
    } catch (NotReadOnlyException e) {
    // ok
    }
    Assert.assertNull("Should have created the znode:" + node2, zk.exists(node2, false));
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) Transaction(org.apache.zookeeper.Transaction) CountdownWatcher(org.apache.zookeeper.test.ClientBase.CountdownWatcher) NotReadOnlyException(org.apache.zookeeper.KeeperException.NotReadOnlyException) Test(org.junit.Test)

Example 2 with NotReadOnlyException

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

the class ReadOnlyModeTest method testReadOnlyClient.

/**
     * Basic test of read-only client functionality. Tries to read and write
     * during read-only mode, then regains a quorum and tries to write again.
     */
@Test(timeout = 90000)
public void testReadOnlyClient() throws Exception {
    CountdownWatcher watcher = new CountdownWatcher();
    ZooKeeper zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT, watcher, true);
    // ensure zk got connected
    watcher.waitForConnected(CONNECTION_TIMEOUT);
    final String data = "Data to be read in RO mode";
    final String node = "/tnode";
    zk.create(node, data.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    watcher.reset();
    qu.shutdown(2);
    zk.close();
    // Re-connect the client (in case we were connected to the shut down
    // server and the local session was not persisted).
    zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT, watcher, true);
    watcher.waitForConnected(CONNECTION_TIMEOUT);
    // read operation during r/o mode
    String remoteData = new String(zk.getData(node, false, null));
    Assert.assertEquals(data, remoteData);
    try {
        zk.setData(node, "no way".getBytes(), -1);
        Assert.fail("Write operation has succeeded during RO mode");
    } catch (NotReadOnlyException e) {
    // ok
    }
    watcher.reset();
    qu.start(2);
    Assert.assertTrue("waiting for server up", ClientBase.waitForServerUp("127.0.0.1:" + qu.getPeer(2).clientPort, CONNECTION_TIMEOUT));
    zk.close();
    watcher.reset();
    // Re-connect the client (in case we were connected to the shut down
    // server and the local session was not persisted).
    zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT, watcher, true);
    watcher.waitForConnected(CONNECTION_TIMEOUT);
    zk.setData(node, "We're in the quorum now".getBytes(), -1);
    zk.close();
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) CountdownWatcher(org.apache.zookeeper.test.ClientBase.CountdownWatcher) NotReadOnlyException(org.apache.zookeeper.KeeperException.NotReadOnlyException) Test(org.junit.Test)

Aggregations

NotReadOnlyException (org.apache.zookeeper.KeeperException.NotReadOnlyException)2 ZooKeeper (org.apache.zookeeper.ZooKeeper)2 CountdownWatcher (org.apache.zookeeper.test.ClientBase.CountdownWatcher)2 Test (org.junit.Test)2 Transaction (org.apache.zookeeper.Transaction)1