use of org.apache.zookeeper.admin.ZooKeeperAdmin in project zookeeper by apache.
the class ReconfigMisconfigTest method instantiateZKAdmin.
private void instantiateZKAdmin() throws InterruptedException {
String cnxString;
ClientBase.CountdownWatcher watcher = new ClientBase.CountdownWatcher();
try {
cnxString = "127.0.0.1:" + qu.getPeer(1).peer.getClientPort();
zkAdmin = new ZooKeeperAdmin(cnxString, ClientBase.CONNECTION_TIMEOUT, watcher);
} catch (IOException e) {
Assert.fail("Fail to create ZooKeeperAdmin handle.");
return;
}
try {
watcher.waitForConnected(ClientBase.CONNECTION_TIMEOUT);
} catch (InterruptedException | TimeoutException e) {
Assert.fail("ZooKeeper admin client can not connect to " + cnxString);
}
}
use of org.apache.zookeeper.admin.ZooKeeperAdmin in project zookeeper by apache.
the class ReconfigTest method testPortChangeToBlockedPort.
private void testPortChangeToBlockedPort(boolean testLeader) throws Exception {
// create 3 servers
qu = new QuorumUtil(1);
qu.disableJMXTest = true;
qu.startAll();
ZooKeeper[] zkArr = createHandles(qu);
ZooKeeperAdmin[] zkAdminArr = createAdminHandles(qu);
List<String> joiningServers = new ArrayList<String>();
int leaderIndex = getLeaderId(qu);
int followerIndex = leaderIndex == 1 ? 2 : 1;
int serverIndex = testLeader ? leaderIndex : followerIndex;
int reconfigIndex = testLeader ? followerIndex : leaderIndex;
// modify server's client port
int quorumPort = qu.getPeer(serverIndex).peer.getQuorumAddress().getPort();
int electionPort = qu.getPeer(serverIndex).peer.getElectionAddress().getPort();
int oldClientPort = qu.getPeer(serverIndex).peer.getClientPort();
int newClientPort = PortAssignment.unique();
try (ServerSocket ss = new ServerSocket()) {
ss.bind(new InetSocketAddress(getLoopbackAddress(), newClientPort));
joiningServers.add("server." + serverIndex + "=localhost:" + quorumPort + ":" + electionPort + ":participant;localhost:" + newClientPort);
// create a /test znode and check that read/write works before
// any reconfig is invoked
testNormalOperation(zkArr[followerIndex], zkArr[leaderIndex]);
// Reconfigure
reconfig(zkAdminArr[reconfigIndex], joiningServers, null, null, -1);
Thread.sleep(1000);
// The follower reconfiguration will have failed
zkArr[serverIndex].close();
zkArr[serverIndex] = new ZooKeeper("127.0.0.1:" + newClientPort, ClientBase.CONNECTION_TIMEOUT, new Watcher() {
public void process(WatchedEvent event) {
}
});
zkAdminArr[serverIndex].close();
zkAdminArr[serverIndex] = new ZooKeeperAdmin("127.0.0.1:" + newClientPort, ClientBase.CONNECTION_TIMEOUT, new Watcher() {
public void process(WatchedEvent event) {
}
});
try {
Thread.sleep(1000);
zkArr[serverIndex].setData("/test", "teststr".getBytes(), -1);
Assert.fail("New client connected to new client port!");
} catch (KeeperException.ConnectionLossException e) {
// Exception is expected
}
try (ServerSocket ss2 = new ServerSocket()) {
ss2.bind(new InetSocketAddress(getLoopbackAddress(), oldClientPort));
}
// Move back to the old port
joiningServers.clear();
joiningServers.add("server." + serverIndex + "=localhost:" + quorumPort + ":" + electionPort + ":participant;localhost:" + oldClientPort);
reconfig(zkAdminArr[reconfigIndex], joiningServers, null, null, -1);
zkArr[serverIndex].close();
zkArr[serverIndex] = new ZooKeeper("127.0.0.1:" + oldClientPort, ClientBase.CONNECTION_TIMEOUT, new Watcher() {
public void process(WatchedEvent event) {
}
});
testNormalOperation(zkArr[followerIndex], zkArr[leaderIndex]);
testServerHasConfig(zkArr[serverIndex], joiningServers, null);
Assert.assertEquals(oldClientPort, qu.getPeer(serverIndex).peer.getClientPort());
}
closeAllHandles(zkArr, zkAdminArr);
}
use of org.apache.zookeeper.admin.ZooKeeperAdmin in project zookeeper by apache.
the class StandaloneTest method testStandaloneReconfigFails.
/**
* Verify that reconfiguration in standalone mode fails with
* KeeperException.UnimplementedException.
*/
@Test
public void testStandaloneReconfigFails() throws Exception {
ClientBase.setupTestEnv();
final int CLIENT_PORT = PortAssignment.unique();
final String HOSTPORT = "127.0.0.1:" + CLIENT_PORT;
File tmpDir = ClientBase.createTmpDir();
ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
ServerCnxnFactory f = ServerCnxnFactory.createFactory(CLIENT_PORT, -1);
f.startup(zks);
Assert.assertTrue("waiting for server being up ", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT));
CountdownWatcher watcher = new CountdownWatcher();
ZooKeeper zk = new ZooKeeper(HOSTPORT, CONNECTION_TIMEOUT, watcher);
ZooKeeperAdmin zkAdmin = new ZooKeeperAdmin(HOSTPORT, CONNECTION_TIMEOUT, watcher);
watcher.waitForConnected(CONNECTION_TIMEOUT);
List<String> joiners = new ArrayList<String>();
joiners.add("server.2=localhost:1234:1235;1236");
// generate some transactions that will get logged
try {
zkAdmin.addAuthInfo("digest", "super:test".getBytes());
zkAdmin.reconfigure(joiners, null, null, -1, new Stat());
Assert.fail("Reconfiguration in standalone should trigger " + "UnimplementedException");
} catch (KeeperException.UnimplementedException ex) {
// expected
}
zk.close();
zks.shutdown();
f.shutdown();
Assert.assertTrue("waiting for server being down ", ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT));
}
use of org.apache.zookeeper.admin.ZooKeeperAdmin in project zookeeper by apache.
the class ReconfigTest method testPortChange.
@Test
public void testPortChange() throws Exception {
// create 3 servers
qu = new QuorumUtil(1);
qu.disableJMXTest = true;
qu.startAll();
ZooKeeper[] zkArr = createHandles(qu);
ZooKeeperAdmin[] zkAdminArr = createAdminHandles(qu);
List<String> joiningServers = new ArrayList<String>();
int leaderIndex = getLeaderId(qu);
int followerIndex = leaderIndex == 1 ? 2 : 1;
// modify follower's client port
int quorumPort = qu.getPeer(followerIndex).peer.getQuorumAddress().getPort();
int electionPort = qu.getPeer(followerIndex).peer.getElectionAddress().getPort();
int oldClientPort = qu.getPeer(followerIndex).peer.getClientPort();
int newClientPort = PortAssignment.unique();
joiningServers.add("server." + followerIndex + "=localhost:" + quorumPort + ":" + electionPort + ":participant;localhost:" + newClientPort);
// create a /test znode and check that read/write works before
// any reconfig is invoked
testNormalOperation(zkArr[followerIndex], zkArr[leaderIndex]);
reconfig(zkAdminArr[followerIndex], joiningServers, null, null, -1);
try {
for (int i = 0; i < 20; i++) {
Thread.sleep(1000);
zkArr[followerIndex].setData("/test", "teststr".getBytes(), -1);
}
} catch (KeeperException.ConnectionLossException e) {
Assert.fail("Existing client disconnected when client port changed!");
}
zkArr[followerIndex].close();
zkArr[followerIndex] = new ZooKeeper("127.0.0.1:" + oldClientPort, ClientBase.CONNECTION_TIMEOUT, new Watcher() {
public void process(WatchedEvent event) {
}
});
zkAdminArr[followerIndex].close();
zkAdminArr[followerIndex] = new ZooKeeperAdmin("127.0.0.1:" + oldClientPort, ClientBase.CONNECTION_TIMEOUT, new Watcher() {
public void process(WatchedEvent event) {
}
});
zkAdminArr[followerIndex].addAuthInfo("digest", "super:test".getBytes());
for (int i = 0; i < 10; i++) {
try {
Thread.sleep(1000);
zkArr[followerIndex].setData("/test", "teststr".getBytes(), -1);
Assert.fail("New client connected to old client port!");
} catch (KeeperException.ConnectionLossException e) {
}
}
zkArr[followerIndex].close();
zkArr[followerIndex] = new ZooKeeper("127.0.0.1:" + newClientPort, ClientBase.CONNECTION_TIMEOUT, new Watcher() {
public void process(WatchedEvent event) {
}
});
zkAdminArr[followerIndex].close();
zkAdminArr[followerIndex] = new ZooKeeperAdmin("127.0.0.1:" + newClientPort, ClientBase.CONNECTION_TIMEOUT, new Watcher() {
public void process(WatchedEvent event) {
}
});
zkAdminArr[followerIndex].addAuthInfo("digest", "super:test".getBytes());
testNormalOperation(zkArr[followerIndex], zkArr[leaderIndex]);
testServerHasConfig(zkArr[followerIndex], joiningServers, null);
Assert.assertEquals(newClientPort, qu.getPeer(followerIndex).peer.getClientPort());
joiningServers.clear();
// change leader's leading port - should renounce leadership
int newQuorumPort = PortAssignment.unique();
joiningServers.add("server." + leaderIndex + "=localhost:" + newQuorumPort + ":" + qu.getPeer(leaderIndex).peer.getElectionAddress().getPort() + ":participant;localhost:" + qu.getPeer(leaderIndex).peer.getClientPort());
reconfig(zkAdminArr[leaderIndex], joiningServers, null, null, -1);
testNormalOperation(zkArr[followerIndex], zkArr[leaderIndex]);
Assert.assertTrue(qu.getPeer(leaderIndex).peer.getQuorumAddress().getPort() == newQuorumPort);
// the leader changed
Assert.assertTrue(getLeaderId(qu) != leaderIndex);
joiningServers.clear();
for (int i = 1; i <= 3; i++) {
joiningServers.add("server." + i + "=localhost:" + qu.getPeer(i).peer.getQuorumAddress().getPort() + ":" + PortAssignment.unique() + ":participant;localhost:" + qu.getPeer(i).peer.getClientPort());
}
reconfig(zkAdminArr[1], joiningServers, null, null, -1);
leaderIndex = getLeaderId(qu);
int follower1 = leaderIndex == 1 ? 2 : 1;
int follower2 = 1;
while (follower2 == leaderIndex || follower2 == follower1) follower2++;
// lets kill the leader and see if a new one is elected
qu.shutdown(getLeaderId(qu));
testNormalOperation(zkArr[follower2], zkArr[follower1]);
testServerHasConfig(zkArr[follower1], joiningServers, null);
testServerHasConfig(zkArr[follower2], joiningServers, null);
closeAllHandles(zkArr, zkAdminArr);
}
use of org.apache.zookeeper.admin.ZooKeeperAdmin in project zookeeper by apache.
the class ReconfigTest method testJMXBeanAfterRemoveAddOne.
/**
* Tests verifies the jmx attributes of local and remote peer bean - remove
* one quorum peer and again adding it back
*/
@Test
public void testJMXBeanAfterRemoveAddOne() throws Exception {
// create 3 servers
qu = new QuorumUtil(1);
qu.disableJMXTest = true;
qu.startAll();
ZooKeeper[] zkArr = createHandles(qu);
ZooKeeperAdmin[] zkAdminArr = createAdminHandles(qu);
List<String> leavingServers = new ArrayList<String>();
List<String> joiningServers = new ArrayList<String>();
// assert remotePeerBean.1 of ReplicatedServer_2
int leavingIndex = 1;
int replica2 = 2;
QuorumPeer peer2 = qu.getPeer(replica2).peer;
QuorumServer leavingQS2 = peer2.getView().get(new Long(leavingIndex));
String remotePeerBean2 = CommonNames.DOMAIN + ":name0=ReplicatedServer_id" + replica2 + ",name1=replica." + leavingIndex;
assertRemotePeerMXBeanAttributes(leavingQS2, remotePeerBean2);
// assert remotePeerBean.1 of ReplicatedServer_3
int replica3 = 3;
QuorumPeer peer3 = qu.getPeer(replica3).peer;
QuorumServer leavingQS3 = peer3.getView().get(new Long(leavingIndex));
String remotePeerBean3 = CommonNames.DOMAIN + ":name0=ReplicatedServer_id" + replica3 + ",name1=replica." + leavingIndex;
assertRemotePeerMXBeanAttributes(leavingQS3, remotePeerBean3);
ZooKeeper zk = zkArr[leavingIndex];
ZooKeeperAdmin zkAdmin = zkAdminArr[leavingIndex];
leavingServers.add(Integer.toString(leavingIndex));
// remember this server so we can add it back later
joiningServers.add("server." + leavingIndex + "=127.0.0.1:" + qu.getPeer(leavingIndex).peer.getQuorumAddress().getPort() + ":" + qu.getPeer(leavingIndex).peer.getElectionAddress().getPort() + ":participant;127.0.0.1:" + qu.getPeer(leavingIndex).peer.getClientPort());
// Remove ReplicatedServer_1 from the ensemble
reconfig(zkAdmin, null, leavingServers, null, -1);
// localPeerBean.1 of ReplicatedServer_1
QuorumPeer removedPeer = qu.getPeer(leavingIndex).peer;
String localPeerBean = CommonNames.DOMAIN + ":name0=ReplicatedServer_id" + leavingIndex + ",name1=replica." + leavingIndex;
assertLocalPeerMXBeanAttributes(removedPeer, localPeerBean, false);
// remotePeerBean.1 shouldn't exists in ReplicatedServer_2
JMXEnv.ensureNone(remotePeerBean2);
// remotePeerBean.1 shouldn't exists in ReplicatedServer_3
JMXEnv.ensureNone(remotePeerBean3);
// Add ReplicatedServer_1 back to the ensemble
reconfig(zkAdmin, joiningServers, null, null, -1);
// localPeerBean.1 of ReplicatedServer_1
assertLocalPeerMXBeanAttributes(removedPeer, localPeerBean, true);
// assert remotePeerBean.1 of ReplicatedServer_2
leavingQS2 = peer2.getView().get(new Long(leavingIndex));
assertRemotePeerMXBeanAttributes(leavingQS2, remotePeerBean2);
// assert remotePeerBean.1 of ReplicatedServer_3
leavingQS3 = peer3.getView().get(new Long(leavingIndex));
assertRemotePeerMXBeanAttributes(leavingQS3, remotePeerBean3);
closeAllHandles(zkArr, zkAdminArr);
}
Aggregations