本文整理汇总了Java中org.apache.zookeeper.test.ClientBase.CountdownWatcher.reset方法的典型用法代码示例。如果您正苦于以下问题:Java CountdownWatcher.reset方法的具体用法?Java CountdownWatcher.reset怎么用?Java CountdownWatcher.reset使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.zookeeper.test.ClientBase.CountdownWatcher
的用法示例。
在下文中一共展示了CountdownWatcher.reset方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testSessionEstablishment
import org.apache.zookeeper.test.ClientBase.CountdownWatcher; //导入方法依赖的package包/类
/**
* Tests a situation when client firstly connects to a read-only server and
* then connects to a majority server. Transition should be transparent for
* the user.
*/
@Test(timeout = 90000)
public void testSessionEstablishment() throws Exception {
qu.shutdown(2);
CountdownWatcher watcher = new CountdownWatcher();
ZooKeeper zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT,
watcher, true);
watcher.waitForConnected(CONNECTION_TIMEOUT);
Assert.assertSame("should be in r/o mode", States.CONNECTEDREADONLY, zk
.getState());
long fakeId = zk.getSessionId();
watcher.reset();
qu.start(2);
Assert.assertTrue("waiting for server up", ClientBase.waitForServerUp(
"127.0.0.1:" + qu.getPeer(2).clientPort, CONNECTION_TIMEOUT));
watcher.waitForConnected(CONNECTION_TIMEOUT);
zk.create("/test", "test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
Assert.assertFalse("fake session and real session have same id", zk
.getSessionId() == fakeId);
zk.close();
}
示例2: testSessionEstablishment
import org.apache.zookeeper.test.ClientBase.CountdownWatcher; //导入方法依赖的package包/类
/**
* Tests a situation when client firstly connects to a read-only server and
* then connects to a majority server. Transition should be transparent for
* the user.
*/
@Test
public void testSessionEstablishment() throws Exception {
qu.shutdown(2);
CountdownWatcher watcher = new CountdownWatcher();
ZooKeeper zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT,
watcher, true);
watcher.waitForConnected(CONNECTION_TIMEOUT);
Assert.assertSame("should be in r/o mode", States.CONNECTEDREADONLY, zk
.getState());
long fakeId = zk.getSessionId();
watcher.reset();
qu.start(2);
Assert.assertTrue("waiting for server up", ClientBase.waitForServerUp(
"127.0.0.1:" + qu.getPeer(2).clientPort, CONNECTION_TIMEOUT));
watcher.waitForConnected(CONNECTION_TIMEOUT);
zk.create("/test", "test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
Assert.assertFalse("fake session and real session have same id", zk
.getSessionId() == fakeId);
zk.close();
}
示例3: testMultiTransaction
import org.apache.zookeeper.test.ClientBase.CountdownWatcher; //导入方法依赖的package包/类
/**
* 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);
watcher.waitForConnected(CONNECTION_TIMEOUT); // ensure zk got connected
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));
}
示例4: testReadOnlyClient
import org.apache.zookeeper.test.ClientBase.CountdownWatcher; //导入方法依赖的package包/类
/**
* 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);
watcher.waitForConnected(CONNECTION_TIMEOUT); // ensure zk got connected
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);
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));
watcher.waitForConnected(CONNECTION_TIMEOUT);
zk.setData(node, "We're in the quorum now".getBytes(), -1);
zk.close();
}
示例5: testSessionEstablishment
import org.apache.zookeeper.test.ClientBase.CountdownWatcher; //导入方法依赖的package包/类
/**
* Tests a situation when client firstly connects to a read-only server and
* then connects to a majority server. Transition should be transparent for
* the user.
*/
@Test(timeout = 90000)
public void testSessionEstablishment() throws Exception {
qu.shutdown(2);
CountdownWatcher watcher = new CountdownWatcher();
ZooKeeper zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT,
watcher, true);
watcher.waitForConnected(CONNECTION_TIMEOUT);
Assert.assertSame("should be in r/o mode", States.CONNECTEDREADONLY, zk
.getState());
long fakeId = zk.getSessionId();
LOG.info("Connected as r/o mode with state {} and session id {}",
zk.getState(), fakeId);
watcher.reset();
qu.start(2);
Assert.assertTrue("waiting for server up", ClientBase.waitForServerUp(
"127.0.0.1:" + qu.getPeer(2).clientPort, CONNECTION_TIMEOUT));
LOG.info("Server 127.0.0.1:{} is up", qu.getPeer(2).clientPort);
// ZOOKEEPER-2722: wait until we can connect to a read-write server after the quorum
// is formed. Otherwise, it is possible that client first connects to a read-only server,
// then drops the connection because of shutting down of the read-only server caused
// by leader election / quorum forming between the read-only server and the newly started
// server. If we happen to execute the zk.create after the read-only server is shutdown and
// before the quorum is formed, we will get a ConnectLossException.
watcher.waitForSyncConnected(CONNECTION_TIMEOUT);
Assert.assertEquals("Should be in read-write mode", States.CONNECTED,
zk.getState());
LOG.info("Connected as rw mode with state {} and session id {}",
zk.getState(), zk.getSessionId());
zk.create("/test", "test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
Assert.assertFalse("fake session and real session have same id", zk
.getSessionId() == fakeId);
zk.close();
}
示例6: testRelectionWithValidCredentials
import org.apache.zookeeper.test.ClientBase.CountdownWatcher; //导入方法依赖的package包/类
/**
* Test to verify that server is able to reform quorum if the Leader goes
* down.
*/
@Test(timeout = 30000)
public void testRelectionWithValidCredentials() throws Exception {
Map<String, String> authConfigs = new HashMap<String, String>();
authConfigs.put(QuorumAuth.QUORUM_SASL_AUTH_ENABLED, "true");
authConfigs.put(QuorumAuth.QUORUM_SERVER_SASL_AUTH_REQUIRED, "true");
authConfigs.put(QuorumAuth.QUORUM_LEARNER_SASL_AUTH_REQUIRED, "true");
String connectStr = startQuorum(3, authConfigs, 3, false);
CountdownWatcher watcher = new CountdownWatcher();
zk = new ZooKeeper(connectStr, ClientBase.CONNECTION_TIMEOUT, watcher);
watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT);
zk.create("/myTestRoot", new byte[0], Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT_SEQUENTIAL);
watcher.reset();
// Shutdown Leader to trigger re-election
QuorumPeer leaderQP = getLeaderQuorumPeer(mt);
LOG.info("Shutdown Leader sid:{} to trigger quorum leader-election",
leaderQP.getId());
shutdownQP(leaderQP);
// Wait for quorum formation
QuorumPeer newLeaderQP = waitForLeader();
assertNotNull("New leader must have been elected by now", newLeaderQP);
watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT);
zk.create("/myTestRoot", new byte[0], Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT_SEQUENTIAL);
}
示例7: testReadOnlyClient
import org.apache.zookeeper.test.ClientBase.CountdownWatcher; //导入方法依赖的package包/类
/**
* 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
public void testReadOnlyClient() throws Exception {
CountdownWatcher watcher = new CountdownWatcher();
ZooKeeper zk = new ZooKeeper(qu.getConnString(), CONNECTION_TIMEOUT,
watcher, true);
watcher.waitForConnected(CONNECTION_TIMEOUT); // ensure zk got connected
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);
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));
watcher.waitForConnected(CONNECTION_TIMEOUT);
zk.setData(node, "We're in the quorum now".getBytes(), -1);
zk.close();
}
示例8: testSeekForRwServer
import org.apache.zookeeper.test.ClientBase.CountdownWatcher; //导入方法依赖的package包/类
/**
* Ensures that client seeks for r/w servers while it's connected to r/o
* server.
*/
@SuppressWarnings("deprecation")
@Test(timeout = 90000)
public void testSeekForRwServer() throws Exception {
// setup the logger to capture all logs
Layout layout = Logger.getRootLogger().getAppender("CONSOLE")
.getLayout();
ByteArrayOutputStream os = new ByteArrayOutputStream();
WriterAppender appender = new WriterAppender(layout, os);
appender.setImmediateFlush(true);
appender.setThreshold(Level.INFO);
Logger zlogger = Logger.getLogger("org.apache.zookeeper");
zlogger.addAppender(appender);
try {
qu.shutdown(2);
CountdownWatcher watcher = new CountdownWatcher();
ZooKeeper zk = new ZooKeeper(qu.getConnString(),
CONNECTION_TIMEOUT, watcher, true);
watcher.waitForConnected(CONNECTION_TIMEOUT);
// if we don't suspend a peer it will rejoin a quorum
qu.getPeer(1).peer.suspend();
// start two servers to form a quorum; client should detect this and
// connect to one of them
watcher.reset();
qu.start(2);
qu.start(3);
ClientBase.waitForServerUp(qu.getConnString(), 2000);
watcher.waitForConnected(CONNECTION_TIMEOUT);
zk.create("/test", "test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
// resume poor fellow
qu.getPeer(1).peer.resume();
} finally {
zlogger.removeAppender(appender);
}
os.close();
LineNumberReader r = new LineNumberReader(new StringReader(os
.toString()));
String line;
Pattern p = Pattern.compile(".*Majority server found.*");
boolean found = false;
while ((line = r.readLine()) != null) {
if (p.matcher(line).matches()) {
found = true;
break;
}
}
Assert.assertTrue(
"Majority server wasn't found while connected to r/o server",
found);
}
示例9: testOpenCloseSession
import org.apache.zookeeper.test.ClientBase.CountdownWatcher; //导入方法依赖的package包/类
/**
* Test that a CloseSession request generated by both the server (client
* disconnect) or by the client (client explicitly issue close()) doesn't
* get committed by the ensemble
*/
public void testOpenCloseSession(boolean onLeader) throws Exception {
int leaderIdx = qb.getLeaderIndex();
Assert.assertFalse("No leader in quorum?", leaderIdx == -1);
int followerIdx = (leaderIdx + 1) % 5;
int testPeerIdx = onLeader ? leaderIdx : followerIdx;
int verifyPeerIdx = onLeader ? followerIdx : leaderIdx;
String hostPorts[] = qb.hostPort.split(",");
CountdownWatcher watcher = new CountdownWatcher();
DisconnectableZooKeeper client = new DisconnectableZooKeeper(
hostPorts[testPeerIdx], CONNECTION_TIMEOUT, watcher);
watcher.waitForConnected(CONNECTION_TIMEOUT);
long localSessionId1 = client.getSessionId();
// Cut the connection, so the server will create closeSession as part
// of expiring the session.
client.dontReconnect();
client.disconnect();
watcher.reset();
// We don't validate right away, will do another session create first
ZooKeeper zk = qb.createClient(watcher, hostPorts[testPeerIdx],
CONNECTION_TIMEOUT);
watcher.waitForConnected(CONNECTION_TIMEOUT);
long localSessionId2 = zk.getSessionId();
// Send closeSession request.
zk.close();
watcher.reset();
// This should be enough time for the first session to expire and for
// the closeSession request to propagate to other machines (if there is a bug)
// Since it is time sensitive, we have false negative when test
// machine is under load
Thread.sleep(CONNECTION_TIMEOUT * 2);
// Validate that we don't see any txn from the first session
validateRequestLog(localSessionId1, verifyPeerIdx);
// Validate that we don't see any txn from the second session
validateRequestLog(localSessionId2, verifyPeerIdx);
qb.shutdownServers();
}
示例10: testLocalSessions
import org.apache.zookeeper.test.ClientBase.CountdownWatcher; //导入方法依赖的package包/类
private void testLocalSessions(boolean testLeader) throws Exception {
String nodePrefix = "/testLocalSessions-"
+ (testLeader ? "leaderTest-" : "followerTest-");
int leaderIdx = qb.getLeaderIndex();
Assert.assertFalse("No leader in quorum?", leaderIdx == -1);
int followerIdx = (leaderIdx + 1) % 5;
int testPeerIdx = testLeader ? leaderIdx : followerIdx;
String hostPorts[] = qb.hostPort.split(",");
CountdownWatcher watcher = new CountdownWatcher();
ZooKeeper zk = qb.createClient(watcher, hostPorts[testPeerIdx],
CONNECTION_TIMEOUT);
watcher.waitForConnected(CONNECTION_TIMEOUT);
long localSessionId = zk.getSessionId();
// Try creating some data.
for (int i = 0; i < 5; i++) {
zk.create(nodePrefix + i, new byte[0],
ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
// Now, try an ephemeral node. This should fail since we
// cannot create ephemeral nodes on a local session.
try {
zk.create(nodePrefix + "ephemeral", new byte[0],
ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
Assert.fail("Ephemeral node creation should fail.");
} catch (KeeperException.EphemeralOnLocalSessionException e) {
}
// Close the session.
zk.close();
// Validate data on both follower and leader
Map<String, Integer> peers = new HashMap<String, Integer>();
peers.put("leader", leaderIdx);
peers.put("follower", followerIdx);
for (Entry<String, Integer> entry: peers.entrySet()) {
watcher.reset();
// Try reconnecting with a new session.
// The data should be persisted, even though the session was not.
zk = qb.createClient(watcher, hostPorts[entry.getValue()],
CONNECTION_TIMEOUT);
watcher.waitForConnected(CONNECTION_TIMEOUT);
long newSessionId = zk.getSessionId();
Assert.assertFalse(newSessionId == localSessionId);
for (int i = 0; i < 5; i++) {
Assert.assertNotNull("Data not exists in " + entry.getKey(),
zk.exists(nodePrefix + i, null));
}
// We may get the correct exception but the txn may go through
Assert.assertNull("Data exists in " + entry.getKey(),
zk.exists(nodePrefix + "ephemeral", null));
zk.close();
}
qb.shutdownServers();
}
示例11: testUpgradeWithEphemeral
import org.apache.zookeeper.test.ClientBase.CountdownWatcher; //导入方法依赖的package包/类
private void testUpgradeWithEphemeral(boolean testLeader)
throws Exception {
String nodePrefix = "/testUpgrade-"
+ (testLeader ? "leaderTest-" : "followerTest-");
int leaderIdx = qb.getLeaderIndex();
Assert.assertFalse("No leader in quorum?", leaderIdx == -1);
int followerIdx = (leaderIdx + 1) % 5;
int otherFollowerIdx = (leaderIdx + 2) % 5;
int testPeerIdx = testLeader ? leaderIdx : followerIdx;
String hostPorts[] = qb.hostPort.split(",");
CountdownWatcher watcher = new CountdownWatcher();
DisconnectableZooKeeper zk = new DisconnectableZooKeeper(
hostPorts[testPeerIdx], CONNECTION_TIMEOUT, watcher);
watcher.waitForConnected(CONNECTION_TIMEOUT);
// Create some ephemeral nodes. This should force the session to
// be propagated to the other servers in the ensemble.
for (int i = 0; i < 5; i++) {
zk.create(nodePrefix + i, new byte[0],
ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
}
// We should be able to reconnect with the same session id on a
// different server, since it has been propagated.
long localSessionId = zk.getSessionId();
byte[] localSessionPwd = zk.getSessionPasswd().clone();
zk.disconnect();
watcher.reset();
zk = new DisconnectableZooKeeper(
hostPorts[otherFollowerIdx], CONNECTION_TIMEOUT, watcher,
localSessionId, localSessionPwd);
watcher.waitForConnected(CONNECTION_TIMEOUT);
// The created ephemeral nodes are still around.
for (int i = 0; i < 5; i++) {
Assert.assertNotNull(zk.exists(nodePrefix + i, null));
}
// When we explicitly close the session, we should not be able to
// reconnect with the same session id
zk.close();
try {
watcher.reset();
zk = new DisconnectableZooKeeper(
hostPorts[otherFollowerIdx], CONNECTION_TIMEOUT, watcher,
localSessionId, localSessionPwd);
zk.exists(nodePrefix + "0", null);
Assert.fail("Reconnecting to a closed session ID should fail.");
} catch (KeeperException.SessionExpiredException e) {
}
watcher.reset();
// And the ephemeral nodes will be gone since the session died.
zk = new DisconnectableZooKeeper(
hostPorts[testPeerIdx], CONNECTION_TIMEOUT, watcher);
watcher.waitForConnected(CONNECTION_TIMEOUT);
for (int i = 0; i < 5; i++) {
Assert.assertNull(zk.exists(nodePrefix + i, null));
}
}
示例12: testSeekForRwServer
import org.apache.zookeeper.test.ClientBase.CountdownWatcher; //导入方法依赖的package包/类
/**
* Ensures that client seeks for r/w servers while it's connected to r/o
* server.
*/
@SuppressWarnings("deprecation")
@Test(timeout = 90000)
public void testSeekForRwServer() throws Exception {
// setup the logger to capture all logs
Layout layout = Logger.getRootLogger().getAppender("CONSOLE")
.getLayout();
ByteArrayOutputStream os = new ByteArrayOutputStream();
WriterAppender appender = new WriterAppender(layout, os);
appender.setImmediateFlush(true);
appender.setThreshold(Level.INFO);
Logger zlogger = Logger.getLogger("org.apache.zookeeper");
zlogger.addAppender(appender);
try {
qu.shutdown(2);
CountdownWatcher watcher = new CountdownWatcher();
ZooKeeper zk = new ZooKeeper(qu.getConnString(),
CONNECTION_TIMEOUT, watcher, true);
watcher.waitForConnected(CONNECTION_TIMEOUT);
// if we don't suspend a peer it will rejoin a quorum
qu.getPeer(1).peer.suspend();
// start two servers to form a quorum; client should detect this and
// connect to one of them
watcher.reset();
qu.start(2);
qu.start(3);
ClientBase.waitForServerUp(qu.getConnString(), 2000);
watcher.waitForConnected(CONNECTION_TIMEOUT);
zk.create("/test", "test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
// resume poor fellow
qu.getPeer(1).peer.resume();
} finally {
zlogger.removeAppender(appender);
}
os.close();
LineNumberReader r = new LineNumberReader(new StringReader(os
.toString()));
String line;
Pattern p = Pattern.compile(".*Majority server found.*");
boolean found = false;
while ((line = r.readLine()) != null) {
if (p.matcher(line).matches()) {
found = true;
break;
}
}
Assert.assertTrue(
"Majority server wasn't found while connected to r/o server",
found);
}
示例13: testSeekForRwServer
import org.apache.zookeeper.test.ClientBase.CountdownWatcher; //导入方法依赖的package包/类
/**
* Ensures that client seeks for r/w servers while it's connected to r/o
* server.
*/
@SuppressWarnings("deprecation")
@Test
public void testSeekForRwServer() throws Exception {
// setup the logger to capture all logs
Layout layout = Logger.getRootLogger().getAppender("CONSOLE")
.getLayout();
ByteArrayOutputStream os = new ByteArrayOutputStream();
WriterAppender appender = new WriterAppender(layout, os);
appender.setImmediateFlush(true);
appender.setThreshold(Level.INFO);
Logger zlogger = Logger.getLogger("org.apache.zookeeper");
zlogger.addAppender(appender);
try {
qu.shutdown(2);
CountdownWatcher watcher = new CountdownWatcher();
ZooKeeper zk = new ZooKeeper(qu.getConnString(),
CONNECTION_TIMEOUT, watcher, true);
watcher.waitForConnected(CONNECTION_TIMEOUT);
// if we don't suspend a peer it will rejoin a quorum
qu.getPeer(1).peer.suspend();
// start two servers to form a quorum; client should detect this and
// connect to one of them
watcher.reset();
qu.start(2);
qu.start(3);
ClientBase.waitForServerUp(qu.getConnString(), 2000);
watcher.waitForConnected(CONNECTION_TIMEOUT);
zk.create("/test", "test".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
// resume poor fellow
qu.getPeer(1).peer.resume();
} finally {
zlogger.removeAppender(appender);
}
os.close();
LineNumberReader r = new LineNumberReader(new StringReader(os
.toString()));
String line;
Pattern p = Pattern.compile(".*Majority server found.*");
boolean found = false;
while ((line = r.readLine()) != null) {
if (p.matcher(line).matches()) {
found = true;
break;
}
}
Assert.assertTrue(
"Majority server wasn't found while connected to r/o server",
found);
}
示例14: testReadOnlyClient
import org.apache.zookeeper.test.ClientBase.CountdownWatcher; //导入方法依赖的package包/类
/**
* 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);
watcher.waitForConnected(CONNECTION_TIMEOUT); // ensure zk got connected
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();
}
示例15: testLocalSessions
import org.apache.zookeeper.test.ClientBase.CountdownWatcher; //导入方法依赖的package包/类
private void testLocalSessions(boolean testLeader) throws Exception {
String nodePrefix = "/testLocalSessions-"
+ (testLeader ? "leaderTest-" : "followerTest-");
int leaderIdx = qb.getLeaderIndex();
Assert.assertFalse("No leader in quorum?", leaderIdx == -1);
int followerIdx = (leaderIdx + 1) % 5;
int testPeerIdx = testLeader ? leaderIdx : followerIdx;
String hostPorts[] = qb.hostPort.split(",");
CountdownWatcher watcher = new CountdownWatcher();
ZooKeeper zk = qb.createClient(watcher, hostPorts[testPeerIdx],
CONNECTION_TIMEOUT);
watcher.waitForConnected(CONNECTION_TIMEOUT);
long localSessionId = zk.getSessionId();
// Try creating some data.
for (int i = 0; i < 5; i++) {
zk.create(nodePrefix + i, new byte[0],
ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
// Now, try an ephemeral node. This should fail since we
// cannot create ephemeral nodes on a local session.
try {
zk.create(nodePrefix + "ephemeral", new byte[0],
ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
Assert.fail("Ephemeral node creation should fail.");
} catch (KeeperException.EphemeralOnLocalSessionException e) {
}
// Close the session.
zk.close();
// Validate data on both follower and leader
HashMap<String, Integer> peers = new HashMap<String, Integer>();
peers.put("leader", leaderIdx);
peers.put("follower", followerIdx);
for (Entry<String, Integer> entry: peers.entrySet()) {
watcher.reset();
// Try reconnecting with a new session.
// The data should be persisted, even though the session was not.
zk = qb.createClient(watcher, hostPorts[entry.getValue()],
CONNECTION_TIMEOUT);
watcher.waitForConnected(CONNECTION_TIMEOUT);
long newSessionId = zk.getSessionId();
Assert.assertFalse(newSessionId == localSessionId);
for (int i = 0; i < 5; i++) {
Assert.assertNotNull("Data not exists in " + entry.getKey(),
zk.exists(nodePrefix + i, null));
}
// We may get the correct exception but the txn may go through
Assert.assertNull("Data exists in " + entry.getKey(),
zk.exists(nodePrefix + "ephemeral", null));
zk.close();
}
qb.shutdownServers();
}