use of org.apache.zookeeper.admin.ZooKeeperAdmin in project zookeeper by apache.
the class ReconfigLegacyTest method testReconfigRemoveClientFromStatic.
/**
* {@link https://issues.apache.org/jira/browse/ZOOKEEPER-1992}
* 1. When a server starts from old style static config, without a client port in the server
* specification, it should keep the client port in static config file.
* 2. After port reconfig, the old port should be removed from static file
* and new port added to dynamic file.
* @throws Exception
*/
@Test
public void testReconfigRemoveClientFromStatic() throws Exception {
final int[] clientPorts = new int[SERVER_COUNT];
final int[] quorumPorts = new int[SERVER_COUNT];
final int[] electionPorts = new int[SERVER_COUNT];
final int changedServerId = 0;
final int newClientPort = PortAssignment.unique();
StringBuilder sb = new StringBuilder();
ArrayList<String> allServers = new ArrayList<String>();
ArrayList<String> newServers = new ArrayList<String>();
for (int i = 0; i < SERVER_COUNT; i++) {
clientPorts[i] = PortAssignment.unique();
quorumPorts[i] = PortAssignment.unique();
electionPorts[i] = PortAssignment.unique();
String server = "server." + i + "=localhost:" + quorumPorts[i] + ":" + electionPorts[i] + ":participant";
allServers.add(server);
sb.append(server + "\n");
if (i == changedServerId) {
newServers.add(server + ";0.0.0.0:" + newClientPort);
} else {
newServers.add(server);
}
}
String quorumCfgSection = sb.toString();
MainThread[] mt = new MainThread[SERVER_COUNT];
ZooKeeper[] zk = new ZooKeeper[SERVER_COUNT];
ZooKeeperAdmin[] zkAdmin = new ZooKeeperAdmin[SERVER_COUNT];
// Start the servers with a static config file, without a dynamic config file.
for (int i = 0; i < SERVER_COUNT; i++) {
mt[i] = new MainThread(i, clientPorts[i], quorumCfgSection, false);
mt[i].start();
}
// port in static config file.
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]);
zkAdmin[i] = new ZooKeeperAdmin("127.0.0.1:" + clientPorts[i], ClientBase.CONNECTION_TIMEOUT, this);
zkAdmin[i].addAuthInfo("digest", "super:test".getBytes());
ReconfigTest.testServerHasConfig(zk[i], allServers, null);
Properties cfg = readPropertiesFromFile(mt[i].confFile);
Assert.assertTrue(cfg.containsKey("dynamicConfigFile"));
Assert.assertTrue(cfg.containsKey("clientPort"));
}
ReconfigTest.testNormalOperation(zk[0], zk[1]);
ReconfigTest.reconfig(zkAdmin[1], null, null, newServers, -1);
ReconfigTest.testNormalOperation(zk[0], zk[1]);
// Sleep since writing the config files may take time.
Thread.sleep(1000);
for (int i = 0; i < SERVER_COUNT; i++) {
ReconfigTest.testServerHasConfig(zk[i], newServers, null);
Properties staticCfg = readPropertiesFromFile(mt[i].confFile);
if (i == changedServerId) {
Assert.assertFalse(staticCfg.containsKey("clientPort"));
} else {
Assert.assertTrue(staticCfg.containsKey("clientPort"));
}
}
for (int i = 0; i < SERVER_COUNT; i++) {
mt[i].shutdown();
zk[i].close();
zkAdmin[i].close();
}
}
use of org.apache.zookeeper.admin.ZooKeeperAdmin in project zookeeper by apache.
the class ReconfigCommand method exec.
@Override
public boolean exec() throws CliException {
try {
Stat stat = new Stat();
if (!(zk instanceof ZooKeeperAdmin)) {
// ZooKeeper object to ZooKeeperMain.
return false;
}
byte[] curConfig = ((ZooKeeperAdmin) zk).reconfigure(joining, leaving, members, version, stat);
out.println("Committed new configuration:\n" + new String(curConfig));
if (cl.hasOption("s")) {
new StatPrinter(out).print(stat);
}
} catch (KeeperException | InterruptedException ex) {
throw new CliWrapperException(ex);
}
return false;
}
use of org.apache.zookeeper.admin.ZooKeeperAdmin in project zookeeper by apache.
the class ReconfigExceptionTest method resetZKAdmin.
// Utility method that recreates a new ZooKeeperAdmin handle, and wait for the handle to connect to
// quorum servers.
private void resetZKAdmin() throws InterruptedException {
String cnxString;
ClientBase.CountdownWatcher watcher = new ClientBase.CountdownWatcher();
try {
cnxString = "127.0.0.1:" + qu.getPeer(1).peer.getClientPort();
if (zkAdmin != null) {
zkAdmin.close();
}
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 testRemoveAddOne.
@Test
public void testRemoveAddOne() 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>();
int leaderIndex = getLeaderId(qu);
// during first iteration, leavingIndex will correspond to a follower
// during second iteration leavingIndex will be the index of the leader
int leavingIndex = (leaderIndex == 1) ? 2 : 1;
for (int i = 0; i < 2; i++) {
// some of the operations will be executed by a client connected to
// the removed server
// while others are invoked by a client connected to some other
// server.
// when we're removing the leader, zk1 will be the client connected
// to removed server
ZooKeeper zk1 = (leavingIndex == leaderIndex) ? zkArr[leaderIndex] : zkArr[(leaderIndex % qu.ALL) + 1];
ZooKeeper zk2 = (leavingIndex == leaderIndex) ? zkArr[(leaderIndex % qu.ALL) + 1] : zkArr[leaderIndex];
ZooKeeperAdmin zkAdmin1 = (leavingIndex == leaderIndex) ? zkAdminArr[leaderIndex] : zkAdminArr[(leaderIndex % qu.ALL) + 1];
ZooKeeperAdmin zkAdmin2 = (leavingIndex == leaderIndex) ? zkAdminArr[(leaderIndex % qu.ALL) + 1] : zkAdminArr[leaderIndex];
leavingServers.add(Integer.toString(leavingIndex));
// remember this server so we can add it back later
joiningServers.add("server." + leavingIndex + "=localhost:" + qu.getPeer(leavingIndex).peer.getQuorumAddress().getPort() + ":" + qu.getPeer(leavingIndex).peer.getElectionAddress().getPort() + ":participant;localhost:" + qu.getPeer(leavingIndex).peer.getClientPort());
String configStr = reconfig(zkAdmin1, null, leavingServers, null, -1);
testServerHasConfig(zk2, null, leavingServers);
testNormalOperation(zk2, zk1);
QuorumVerifier qv = qu.getPeer(1).peer.configFromString(configStr);
long version = qv.getVersion();
// checks that conditioning on version works properly
try {
reconfig(zkAdmin2, joiningServers, null, null, version + 1);
Assert.fail("reconfig succeeded even though version condition was incorrect!");
} catch (KeeperException.BadVersionException e) {
}
reconfig(zkAdmin2, joiningServers, null, null, version);
testNormalOperation(zk1, zk2);
testServerHasConfig(zk1, joiningServers, null);
// second iteration of the loop will remove the leader
// and add it back (as follower)
leavingIndex = leaderIndex = getLeaderId(qu);
leavingServers.clear();
joiningServers.clear();
}
closeAllHandles(zkArr, zkAdminArr);
}
use of org.apache.zookeeper.admin.ZooKeeperAdmin in project zookeeper by apache.
the class ReconfigTest method createAdminHandles.
public static ZooKeeperAdmin[] createAdminHandles(QuorumUtil qu) throws IOException {
// create an extra handle, so we can index the handles from 1 to qu.ALL
// using the server id.
ZooKeeperAdmin[] zkAdminArr = new ZooKeeperAdmin[qu.ALL + 1];
// not used.
zkAdminArr[0] = null;
for (int i = 1; i <= qu.ALL; i++) {
// server ids are 1, 2 and 3
zkAdminArr[i] = new ZooKeeperAdmin("127.0.0.1:" + qu.getPeer(i).peer.getClientPort(), ClientBase.CONNECTION_TIMEOUT, new Watcher() {
public void process(WatchedEvent event) {
}
});
zkAdminArr[i].addAuthInfo("digest", "super:test".getBytes());
}
return zkAdminArr;
}
Aggregations