use of org.apache.zookeeper.ZooKeeper in project zookeeper by apache.
the class QuorumPeerMainTest method testBadPackets.
/**
* verify if bad packets are being handled properly
* at the quorum port
* @throws Exception
*/
@Test
public void testBadPackets() throws Exception {
ClientBase.setupTestEnv();
final int CLIENT_PORT_QP1 = PortAssignment.unique();
final int CLIENT_PORT_QP2 = PortAssignment.unique();
int electionPort1 = PortAssignment.unique();
int electionPort2 = PortAssignment.unique();
String quorumCfgSection = "server.1=127.0.0.1:" + PortAssignment.unique() + ":" + electionPort1 + ";" + CLIENT_PORT_QP1 + "\nserver.2=127.0.0.1:" + PortAssignment.unique() + ":" + electionPort2 + ";" + CLIENT_PORT_QP2;
MainThread q1 = new MainThread(1, CLIENT_PORT_QP1, quorumCfgSection);
MainThread q2 = new MainThread(2, CLIENT_PORT_QP2, quorumCfgSection);
q1.start();
q2.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));
byte[] b = new byte[4];
int length = 1024 * 1024 * 1024;
ByteBuffer buff = ByteBuffer.wrap(b);
buff.putInt(length);
buff.position(0);
SocketChannel s = SocketChannel.open(new InetSocketAddress("127.0.0.1", electionPort1));
s.write(buff);
s.close();
buff.position(0);
s = SocketChannel.open(new InetSocketAddress("127.0.0.1", electionPort2));
s.write(buff);
s.close();
ZooKeeper zk = new ZooKeeper("127.0.0.1:" + CLIENT_PORT_QP1, ClientBase.CONNECTION_TIMEOUT, this);
waitForOne(zk, States.CONNECTED);
zk.create("/foo_q1", "foobar1".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
Assert.assertEquals(new String(zk.getData("/foo_q1", null, null)), "foobar1");
zk.close();
q1.shutdown();
q2.shutdown();
}
use of org.apache.zookeeper.ZooKeeper in project zookeeper by apache.
the class QuorumPeerMainTest method waitForAll.
private void waitForAll(ZooKeeper[] zks, States state) throws InterruptedException {
int iterations = ClientBase.CONNECTION_TIMEOUT / 1000;
boolean someoneNotConnected = true;
while (someoneNotConnected) {
if (iterations-- == 0) {
ClientBase.logAllStackTraces();
throw new RuntimeException("Waiting too long");
}
someoneNotConnected = false;
for (ZooKeeper zk : zks) {
if (zk.getState() != state) {
someoneNotConnected = true;
break;
}
}
Thread.sleep(1000);
}
}
use of org.apache.zookeeper.ZooKeeper in project zookeeper by apache.
the class ReconfigFailureCasesTest method testObserverToParticipantConversionFails.
/*
* Converting an observer into a participant may sometimes fail with a
* NewConfigNoQuorum exception. This test-case demonstrates the scenario.
* Current configuration is (A, B, C, D), where A, B and C are participant
* and D is an observer. Suppose that B has crashed (or never booted). If a
* reconfiguration is submitted where D is said to become a participant, it
* will fail with NewConfigNoQuorum since in this configuration, a majority
* of voters in the new configuration (any 3 voters), must be connected and
* up-to-date with the leader. An observer cannot acknowledge the history
* prefix sent during reconfiguration, and therefore it does not count towards
* these 3 required servers and the reconfiguration will be aborted. In case
* this happens, a client can achieve the same task by two reconfig commands:
* first invoke a reconfig to remove D from the configuration and then invoke a
* second command to add it back as a participant (follower). During the
* intermediate state D is a non-voting follower and can ACK the state
* transfer performed during the second reconfig command.
*/
@Test
public void testObserverToParticipantConversionFails() throws Exception {
ClientBase.setupTestEnv();
final int SERVER_COUNT = 4;
int[][] ports = ReconfigRecoveryTest.generatePorts(SERVER_COUNT);
// generate old config string
HashSet<Integer> observers = new HashSet<Integer>();
observers.add(3);
StringBuilder sb = ReconfigRecoveryTest.generateConfig(SERVER_COUNT, ports, observers);
String currentQuorumCfgSection = sb.toString();
String nextQuorumCfgSection = currentQuorumCfgSection.replace("observer", "participant");
MainThread[] mt = new MainThread[SERVER_COUNT];
ZooKeeper[] zk = new ZooKeeper[SERVER_COUNT];
ZooKeeperAdmin[] zkAdmin = new ZooKeeperAdmin[SERVER_COUNT];
// Server 0 stays down
for (int i = 1; i < SERVER_COUNT; i++) {
mt[i] = new MainThread(i, ports[i][2], currentQuorumCfgSection, true, "100000000");
mt[i].start();
zk[i] = new ZooKeeper("127.0.0.1:" + ports[i][2], ClientBase.CONNECTION_TIMEOUT, this);
zkAdmin[i] = new ZooKeeperAdmin("127.0.0.1:" + ports[i][2], ClientBase.CONNECTION_TIMEOUT, this);
zkAdmin[i].addAuthInfo("digest", "super:test".getBytes());
}
for (int i = 1; i < SERVER_COUNT; i++) {
Assert.assertTrue("waiting for server " + i + " being up", ClientBase.waitForServerUp("127.0.0.1:" + ports[i][2], CONNECTION_TIMEOUT * 2));
}
try {
zkAdmin[1].reconfigure("", "", nextQuorumCfgSection, -1, new Stat());
Assert.fail("Reconfig should have failed with NewConfigNoQuorum");
} catch (NewConfigNoQuorum e) {
// This is expected case since server 0 is down and 3 can't vote
// (observer in current role) and we need 3 votes from 0, 1, 2, 3,
} catch (Exception e) {
Assert.fail("Reconfig should have failed with NewConfigNoQuorum");
}
// In this scenario to change 3's role to participant we need to remove it first
ArrayList<String> leavingServers = new ArrayList<String>();
leavingServers.add("3");
ReconfigTest.reconfig(zkAdmin[1], null, leavingServers, null, -1);
ReconfigTest.testNormalOperation(zk[2], zk[3]);
ReconfigTest.testServerHasConfig(zk[3], null, leavingServers);
// Now we're adding it back as a participant and everything should work.
List<String> newMembers = Arrays.asList(nextQuorumCfgSection.split("\n"));
ReconfigTest.reconfig(zkAdmin[1], null, null, newMembers, -1);
ReconfigTest.testNormalOperation(zk[2], zk[3]);
for (int i = 1; i < SERVER_COUNT; i++) {
ReconfigTest.testServerHasConfig(zk[i], newMembers, null);
}
for (int i = 1; i < SERVER_COUNT; i++) {
zk[i].close();
zkAdmin[i].close();
mt[i].shutdown();
}
}
use of org.apache.zookeeper.ZooKeeper in project zookeeper by apache.
the class ReconfigFailureCasesTest method testReconfigVersionConditionFails.
/*
* Tests that a conditional reconfig fails if the specified version doesn't correspond
* to the version of the current config.
*/
@Test
public void testReconfigVersionConditionFails() throws Exception {
// create 3 servers
qu = new QuorumUtil(1);
qu.disableJMXTest = true;
qu.startAll();
ZooKeeper[] zkArr = ReconfigTest.createHandles(qu);
ZooKeeperAdmin[] zkAdminArr = ReconfigTest.createAdminHandles(qu);
List<String> leavingServers = new ArrayList<String>();
leavingServers.add("3");
try {
zkAdminArr[1].reconfigure(null, leavingServers, null, 8, null);
Assert.fail("Reconfig should have failed since the current config version is not 8");
} catch (KeeperException.BadVersionException e) {
// We expect this to happen.
} catch (Exception e) {
Assert.fail("Should have been BadVersionException!");
}
ReconfigTest.closeAllHandles(zkArr, zkAdminArr);
}
use of org.apache.zookeeper.ZooKeeper in project zookeeper by apache.
the class ReconfigLegacyTest method testConfigFileBackwardCompatibility.
/**
* This test checks that when started with a single static config file the
* servers will create a valid dynamic config file. Also checks that when
* the static config includes a clientPort but the dynamic definition also
* includes it, the static definition is erased.
*/
@Test
public void testConfigFileBackwardCompatibility() throws Exception {
final int[] clientPorts = new int[SERVER_COUNT];
StringBuilder sb = new StringBuilder();
String server;
ArrayList<String> allServers = new ArrayList<String>();
for (int i = 0; i < SERVER_COUNT; i++) {
clientPorts[i] = PortAssignment.unique();
server = "server." + i + "=localhost:" + PortAssignment.unique() + ":" + PortAssignment.unique() + ":participant;localhost:" + clientPorts[i];
allServers.add(server);
sb.append(server + "\n");
}
String currentQuorumCfgSection = sb.toString();
MainThread[] mt = new MainThread[SERVER_COUNT];
ZooKeeper[] zk = new ZooKeeper[SERVER_COUNT];
// config file.
for (int i = 0; i < SERVER_COUNT; i++) {
mt[i] = new MainThread(i, clientPorts[i], currentQuorumCfgSection, "participant", false);
// check that a dynamic configuration file doesn't exist
Assert.assertEquals(mt[i].getDynamicFiles().length, 0);
mt[i].start();
}
// Check that the static config was split into static and dynamic files correctly.
for (int i = 0; i < SERVER_COUNT; i++) {
Assert.assertTrue("waiting for server " + i + " being up", ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[i], CONNECTION_TIMEOUT));
zk[i] = ClientBase.createZKClient("127.0.0.1:" + clientPorts[i]);
File[] dynamicFiles = mt[i].getDynamicFiles();
Assert.assertTrue(dynamicFiles.length == 1);
ReconfigTest.testServerHasConfig(zk[i], allServers, null);
// check that static config file doesn't include membership info
// and has a pointer to the dynamic configuration file
// check that static config file doesn't include peerType info
Properties cfg = readPropertiesFromFile(mt[i].confFile);
for (int j = 0; j < SERVER_COUNT; j++) {
Assert.assertFalse(cfg.containsKey("server." + j));
}
Assert.assertFalse(cfg.containsKey("peerType"));
Assert.assertTrue(cfg.containsKey("dynamicConfigFile"));
Assert.assertFalse(cfg.containsKey("clientPort"));
// check that the dynamic configuration file contains the membership info
cfg = readPropertiesFromFile(dynamicFiles[0]);
for (int j = 0; j < SERVER_COUNT; j++) {
String serverLine = cfg.getProperty("server." + j, "");
Assert.assertEquals(allServers.get(j), "server." + j + "=" + serverLine);
}
Assert.assertFalse(cfg.containsKey("dynamicConfigFile"));
}
ReconfigTest.testNormalOperation(zk[0], zk[1]);
// now shut down the servers and restart them
for (int i = 0; i < SERVER_COUNT; i++) {
zk[i].close();
mt[i].shutdown();
}
for (int i = 0; i < SERVER_COUNT; i++) {
mt[i].start();
}
for (int i = 0; i < SERVER_COUNT; i++) {
Assert.assertTrue("waiting for server " + i + " being up", ClientBase.waitForServerUp("127.0.0.1:" + clientPorts[i], CONNECTION_TIMEOUT));
zk[i] = ClientBase.createZKClient("127.0.0.1:" + clientPorts[i]);
ReconfigTest.testServerHasConfig(zk[i], allServers, null);
}
ReconfigTest.testNormalOperation(zk[0], zk[1]);
for (int i = 0; i < SERVER_COUNT; i++) {
mt[i].shutdown();
zk[i].close();
}
}
Aggregations