use of org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer in project zookeeper by apache.
the class ReconfigTest method testJMXBeanAfterRoleChange.
/**
* Tests verifies the jmx attributes of local and remote peer bean - change
* participant to observer role
*/
@Test
public void testJMXBeanAfterRoleChange() throws Exception {
// create 3 servers
qu = new QuorumUtil(1);
qu.disableJMXTest = true;
qu.startAll();
ZooKeeper[] zkArr = createHandles(qu);
ZooKeeperAdmin[] zkAdminArr = createAdminHandles(qu);
// changing a server's role / port is done by "adding" it with the same
// id but different role / port
List<String> joiningServers = new ArrayList<String>();
// assert remotePeerBean.1 of ReplicatedServer_2
int changingIndex = 1;
int replica2 = 2;
QuorumPeer peer2 = qu.getPeer(replica2).peer;
QuorumServer changingQS2 = peer2.getView().get(new Long(changingIndex));
String remotePeerBean2 = CommonNames.DOMAIN + ":name0=ReplicatedServer_id" + replica2 + ",name1=replica." + changingIndex;
assertRemotePeerMXBeanAttributes(changingQS2, remotePeerBean2);
// assert remotePeerBean.1 of ReplicatedServer_3
int replica3 = 3;
QuorumPeer peer3 = qu.getPeer(replica3).peer;
QuorumServer changingQS3 = peer3.getView().get(new Long(changingIndex));
String remotePeerBean3 = CommonNames.DOMAIN + ":name0=ReplicatedServer_id" + replica3 + ",name1=replica." + changingIndex;
assertRemotePeerMXBeanAttributes(changingQS3, remotePeerBean3);
String newRole = "observer";
ZooKeeper zk = zkArr[changingIndex];
ZooKeeperAdmin zkAdmin = zkAdminArr[changingIndex];
// exactly as it is now, except for role change
joiningServers.add("server." + changingIndex + "=127.0.0.1:" + qu.getPeer(changingIndex).peer.getQuorumAddress().getPort() + ":" + qu.getPeer(changingIndex).peer.getElectionAddress().getPort() + ":" + newRole + ";127.0.0.1:" + qu.getPeer(changingIndex).peer.getClientPort());
reconfig(zkAdmin, joiningServers, null, null, -1);
testNormalOperation(zkArr[changingIndex], zk);
Assert.assertTrue(qu.getPeer(changingIndex).peer.observer != null && qu.getPeer(changingIndex).peer.follower == null && qu.getPeer(changingIndex).peer.leader == null);
Assert.assertTrue(qu.getPeer(changingIndex).peer.getPeerState() == ServerState.OBSERVING);
QuorumPeer qp = qu.getPeer(changingIndex).peer;
String localPeerBeanName = CommonNames.DOMAIN + ":name0=ReplicatedServer_id" + changingIndex + ",name1=replica." + changingIndex;
// localPeerBean.1 of ReplicatedServer_1
assertLocalPeerMXBeanAttributes(qp, localPeerBeanName, true);
// assert remotePeerBean.1 of ReplicatedServer_2
changingQS2 = peer2.getView().get(new Long(changingIndex));
assertRemotePeerMXBeanAttributes(changingQS2, remotePeerBean2);
// assert remotePeerBean.1 of ReplicatedServer_3
changingQS3 = peer3.getView().get(new Long(changingIndex));
assertRemotePeerMXBeanAttributes(changingQS3, remotePeerBean3);
closeAllHandles(zkArr, zkAdminArr);
}
use of org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer in project zookeeper by apache.
the class ReconfigTest method testUnspecifiedClientAddress.
@Test
public void testUnspecifiedClientAddress() throws Exception {
int[] ports = new int[3];
for (int port : ports) {
port = PortAssignment.unique();
}
String server = "server.0=localhost:" + ports[0] + ":" + ports[1] + ";" + ports[2];
QuorumServer qs = new QuorumServer(0, server);
Assert.assertEquals(qs.clientAddr.getHostString(), "0.0.0.0");
Assert.assertEquals(qs.clientAddr.getPort(), ports[2]);
}
use of org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer in project rest.li by linkedin.
the class ZKQuorum method createNewPeerData.
private void createNewPeerData(int id) {
int clientPort = ZKTestUtil.getRandomPort();
int quorumPort = ZKTestUtil.getRandomPort() + 1000;
int electionPort = ZKTestUtil.getRandomPort() + 1001;
ZKPeer zkpeer = new ZKPeer(id, ZKTestUtil.createTempDir("zkdata" + id), ZKTestUtil.createTempDir("zklog" + id), HOST, clientPort, quorumPort, electionPort);
_peers.put(id, zkpeer);
_peersView.put(Long.valueOf(id), new QuorumServer(id, new InetSocketAddress(HOST, quorumPort), new InetSocketAddress(HOST, electionPort), LearnerType.PARTICIPANT));
_log.info("Created peer #" + id + " with ports:" + clientPort + "/" + quorumPort + "/" + electionPort + " peer server addr:" + _peersView.get(Long.valueOf(id)).addr + " peer server electionAddr:" + _peersView.get(Long.valueOf(id)).electionAddr);
}
use of org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer in project zookeeper by apache.
the class QuorumPeerInstance method configure.
public void configure(String params) {
if (clientAddr == null) {
String[] parts = params.split(" ");
// The first time we are configured, it is just to tell
// us which machine we are
serverId = Integer.parseInt(parts[0]);
if (LOG.isDebugEnabled()) {
LOG.debug("Setting up server " + serverId);
}
if (parts.length > 1 && parts[1].equals("false")) {
System.setProperty("zookeeper.leaderServes", "no");
} else {
System.setProperty("zookeeper.leaderServes", "yes");
}
// Let's grab two ports
try {
ServerSocket ss = new ServerSocket(0, 1, InetAddress.getLocalHost());
clientAddr = (InetSocketAddress) ss.getLocalSocketAddress();
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
ServerSocket ss = new ServerSocket(0, 1, InetAddress.getLocalHost());
quorumLeaderAddr = (InetSocketAddress) ss.getLocalSocketAddress();
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
ServerSocket ss = new ServerSocket(0, 1, InetAddress.getLocalHost());
quorumLeaderElectionAddr = (InetSocketAddress) ss.getLocalSocketAddress();
ss.close();
} catch (IOException e) {
e.printStackTrace();
}
String report = clientAddr.getHostString() + ':' + clientAddr.getPort() + ',' + quorumLeaderAddr.getHostString() + ':' + quorumLeaderAddr.getPort() + ':' + quorumLeaderElectionAddr.getPort();
try {
if (LOG.isDebugEnabled()) {
LOG.debug("Reporting " + report);
}
r.report(report);
} catch (Exception e) {
e.printStackTrace();
}
return;
} else {
int spaceIndex = params.indexOf(' ');
if (spaceIndex == -1) {
LOG.warn("looking for host:port,... start|stop, but found " + params);
return;
}
String quorumSpecs = params.substring(0, spaceIndex);
String cmd = params.substring(spaceIndex + 1);
if (LOG.isDebugEnabled()) {
LOG.debug("Running command: " + cmd);
}
if (!cmd.equals("start")) {
if (peer != null) {
peer.shutdown();
}
peer = null;
try {
for (int i = 0; i < 5; i++) {
Thread.sleep(500);
try {
// Wait until we can't connect
new Socket("127.0.0.1", clientAddr.getPort()).close();
} catch (IOException e) {
break;
}
}
r.report("stopped");
} catch (Exception e) {
LOG.error("Unhandled error", e);
}
return;
}
String[] parts = quorumSpecs.split(",");
peers = new HashMap<Long, QuorumServer>();
for (int i = 0; i < parts.length; i++) {
// parts[i] == "host:leaderPort:leaderElectionPort;clientPort"
String[] subparts = ((parts[i].split(";"))[0]).split(":");
String clientPort = (parts[i].split(";"))[1];
peers.put(Long.valueOf(i), new QuorumServer(i, new InetSocketAddress(subparts[0], Integer.parseInt(subparts[1])), new InetSocketAddress(subparts[0], Integer.parseInt(subparts[2])), new InetSocketAddress(subparts[0], Integer.parseInt(clientPort))));
}
try {
if (LOG.isDebugEnabled()) {
LOG.debug("Starting quorumPeer " + serverId + " on port " + clientAddr.getPort());
}
if (peer != null) {
LOG.warn("Peer " + serverId + " already started");
return;
}
System.err.println("SnapDir = " + snapDir + " LogDir = " + logDir);
peer = new QuorumPeer(peers, snapDir, logDir, clientAddr.getPort(), 0, serverId, tickTime, initLimit, syncLimit);
peer.start();
for (int i = 0; i < 5; i++) {
Thread.sleep(500);
try {
// Wait until we can connect
new Socket("127.0.0.1", clientAddr.getPort()).close();
break;
} catch (IOException e) {
}
}
r.report("started");
} catch (Exception e) {
LOG.error("Unhandled exception", e);
}
}
}
use of org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer in project zookeeper by apache.
the class QuorumMaj method toString.
public String toString() {
StringBuilder sw = new StringBuilder();
for (QuorumServer member : getAllMembers().values()) {
String key = "server." + member.id;
String value = member.toString();
sw.append(key);
sw.append('=');
sw.append(value);
sw.append('\n');
}
String hexVersion = Long.toHexString(version);
sw.append("version=");
sw.append(hexVersion);
return sw.toString();
}
Aggregations