use of org.apache.zookeeper.test.ClientBase.CountdownWatcher 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(value = 90)
public void testReadOnlyClient() throws Exception {
qu.enableLocalSession(true);
qu.startQuorum();
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));
assertEquals(data, remoteData);
try {
zk.setData(node, "no way".getBytes(), -1);
fail("Write operation has succeeded during RO mode");
} catch (NotReadOnlyException e) {
// ok
}
watcher.reset();
qu.start(2);
assertTrue(ClientBase.waitForServerUp("127.0.0.1:" + qu.getPeer(2).clientPort, CONNECTION_TIMEOUT), "waiting for server up");
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();
}
use of org.apache.zookeeper.test.ClientBase.CountdownWatcher in project zookeeper by apache.
the class ReadOnlyModeTest method testMultiTransaction.
/**
* Test write operations using multi request.
*/
@Test
@Timeout(value = 90)
public void testMultiTransaction() throws Exception {
qu.enableLocalSession(true);
qu.startQuorum();
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);
zk.close();
watcher.waitForDisconnected(CONNECTION_TIMEOUT);
watcher.reset();
qu.shutdown(2);
zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT, watcher, true);
watcher.waitForConnected(CONNECTION_TIMEOUT);
assertEquals(States.CONNECTEDREADONLY, zk.getState(), "Should be in r-o mode");
// read operation during r/o mode
String remoteData = new String(zk.getData(node1, false, null));
assertEquals(data, remoteData, "Failed to read data in r-o mode");
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();
fail("Write operation using multi-transaction" + " api has succeeded during RO mode");
} catch (NotReadOnlyException e) {
// ok
}
assertNull(zk.exists(node2, false), "Should have created the znode:" + node2);
}
use of org.apache.zookeeper.test.ClientBase.CountdownWatcher in project zookeeper by apache.
the class NonRecoverableErrorTest method testZooKeeperServiceAvailableOnLeader.
/**
* Test case for https://issues.apache.org/jira/browse/ZOOKEEPER-2247.
* Test to verify that even after non recoverable error (error while
* writing transaction log), ZooKeeper is still available.
*/
@Test
@Timeout(value = 30)
public void testZooKeeperServiceAvailableOnLeader() throws Exception {
int SERVER_COUNT = 3;
final int[] clientPorts = new int[SERVER_COUNT];
StringBuilder sb = new StringBuilder();
String server;
for (int i = 0; i < SERVER_COUNT; i++) {
clientPorts[i] = PortAssignment.unique();
server = "server." + i + "=127.0.0.1:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ":participant;127.0.0.1:" + clientPorts[i];
sb.append(server + "\n");
}
String currentQuorumCfgSection = sb.toString();
MainThread[] mt = new MainThread[SERVER_COUNT];
for (int i = 0; i < SERVER_COUNT; i++) {
mt[i] = new MainThread(i, clientPorts[i], currentQuorumCfgSection, false);
mt[i].start();
}
// ensure server started
for (int i = 0; i < SERVER_COUNT; i++) {
assertTrue(ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[i], CONNECTION_TIMEOUT), "waiting for server " + i + " being up");
}
CountdownWatcher watcher = new CountdownWatcher();
ZooKeeper zk = new ZooKeeper("127.0.0.1:" + clientPorts[0], ClientBase.CONNECTION_TIMEOUT, watcher);
watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT);
String data = "originalData";
zk.create(NODE_PATH, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
// get information of current leader
QuorumPeer leader = getLeaderQuorumPeer(mt);
assertNotNull(leader, "Leader must have been elected by now");
// inject problem in leader
FileTxnSnapLog snapLog = leader.getActiveServer().getTxnLogFactory();
FileTxnSnapLog fileTxnSnapLogWithError = new FileTxnSnapLog(snapLog.getDataDir(), snapLog.getSnapDir()) {
@Override
public void commit() throws IOException {
throw new IOException("Input/output error");
}
};
ZKDatabase originalZKDatabase = leader.getActiveServer().getZKDatabase();
long leaderCurrentEpoch = leader.getCurrentEpoch();
ZKDatabase newDB = new ZKDatabase(fileTxnSnapLogWithError);
leader.getActiveServer().setZKDatabase(newDB);
try {
// do create operation, so that injected IOException is thrown
zk.create(uniqueZnode(), data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
fail("IOException is expected due to error injected to transaction log commit");
} catch (Exception e) {
// do nothing
}
// resetting watcher so that this watcher can be again used to ensure
// that the zkClient is able to re-establish connection with the
// newly elected zookeeper quorum.
watcher.reset();
waitForNewLeaderElection(leader, leaderCurrentEpoch);
// takes place
for (int i = 0; i < SERVER_COUNT; i++) {
assertTrue(ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[i], CONNECTION_TIMEOUT), "waiting for server " + i + " being up");
}
// revert back the error
leader.getActiveServer().setZKDatabase(originalZKDatabase);
// verify that now ZooKeeper service is up and running
leader = getLeaderQuorumPeer(mt);
assertNotNull(leader, "New leader must have been elected by now");
String uniqueNode = uniqueZnode();
watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT);
String createNode = zk.create(uniqueNode, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
// if node is created successfully then it means that ZooKeeper service
// is available
assertEquals(uniqueNode, createNode, "Failed to create znode");
zk.close();
// stop all severs
for (int i = 0; i < SERVER_COUNT; i++) {
mt[i].shutdown();
}
}
Aggregations