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(), 3, 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 RemotePeerBeanTest method testGetClientAddressShouldReturnEmptyStringWhenClientAddressIsNull.
/**
* Test case for https://issues.apache.org/jira/browse/ZOOKEEPER-2269
*/
@Test
public void testGetClientAddressShouldReturnEmptyStringWhenClientAddressIsNull() {
InetSocketAddress peerCommunicationAddress = null;
// Here peerCommunicationAddress is null, also clientAddr is null
QuorumServer peer = new QuorumServer(1, peerCommunicationAddress);
RemotePeerBean remotePeerBean = new RemotePeerBean(peer);
String clientAddress = remotePeerBean.getClientAddress();
assertNotNull(clientAddress);
assertEquals(0, clientAddress.length());
}
use of org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer in project zookeeper by apache.
the class Zab1_0Test method createQuorumPeer.
private QuorumPeer createQuorumPeer(File tmpDir) throws IOException, FileNotFoundException {
HashMap<Long, QuorumServer> peers = new HashMap<Long, QuorumServer>();
QuorumPeer peer = QuorumPeer.testingQuorumPeer();
peer.syncLimit = SYNC_LIMIT;
peer.initLimit = 2;
peer.tickTime = 2000;
peers.put(0L, new QuorumServer(0, new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", PortAssignment.unique())));
peers.put(1L, new QuorumServer(1, new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", PortAssignment.unique())));
peers.put(2L, new QuorumServer(2, new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", PortAssignment.unique())));
peer.setQuorumVerifier(new QuorumMaj(peers), false);
peer.setCnxnFactory(new NullServerCnxnFactory());
File version2 = new File(tmpDir, "version-2");
version2.mkdir();
ClientBase.createInitializeFile(tmpDir);
FileOutputStream fos;
fos = new FileOutputStream(new File(version2, "currentEpoch"));
fos.write("0\n".getBytes());
fos.close();
fos = new FileOutputStream(new File(version2, "acceptedEpoch"));
fos.write("0\n".getBytes());
fos.close();
return peer;
}
use of org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer in project zookeeper by apache.
the class Zab1_0Test method testObserverConversation.
public void testObserverConversation(ObserverConversation conversation) throws Exception {
File tmpDir = File.createTempFile("test", "dir", testData);
tmpDir.delete();
tmpDir.mkdir();
Thread observerThread = null;
ConversableObserver observer = null;
QuorumPeer peer = null;
try {
peer = createQuorumPeer(tmpDir);
peer.setSyncEnabled(true);
observer = createObserver(tmpDir, peer);
peer.observer = observer;
ServerSocket ss = new ServerSocket(0, 50, InetAddress.getByName("127.0.0.1"));
QuorumServer leaderQS = new QuorumServer(1, (InetSocketAddress) ss.getLocalSocketAddress());
observer.setLeaderQuorumServer(leaderQS);
final Observer observerForThread = observer;
observerThread = new Thread() {
public void run() {
try {
observerForThread.observeLeader();
} catch (Exception e) {
e.printStackTrace();
}
}
};
observerThread.start();
Socket leaderSocket = ss.accept();
InputArchive ia = BinaryInputArchive.getArchive(leaderSocket.getInputStream());
OutputArchive oa = BinaryOutputArchive.getArchive(leaderSocket.getOutputStream());
conversation.converseWithObserver(ia, oa, observer);
} finally {
if (observer != null) {
observer.shutdown();
}
if (observerThread != null) {
observerThread.interrupt();
observerThread.join();
}
if (peer != null) {
peer.shutdown();
}
TestUtils.deleteFileRecursively(tmpDir);
}
}
use of org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer in project zookeeper by apache.
the class TruncateTest method testTruncate.
@Test
public void testTruncate() throws Exception {
// Prime the server that is going to come in late with 50 txns
String hostPort = "127.0.0.1:" + PortAssignment.unique();
int maxCnxns = 100;
ServerCnxnFactory factory = ClientBase.createNewServerInstance(null, hostPort, maxCnxns);
ClientBase.startServerInstance(dataDir1, factory, hostPort);
ClientBase.shutdownServerInstance(factory, hostPort);
// standalone starts with 0 epoch while quorum starts with 1
File origfile = new File(new File(dataDir1, "version-2"), "snapshot.0");
File newfile = new File(new File(dataDir1, "version-2"), "snapshot.100000000");
origfile.renameTo(newfile);
factory = ClientBase.createNewServerInstance(null, hostPort, maxCnxns);
ClientBase.startServerInstance(dataDir1, factory, hostPort);
ZooKeeper zk = ClientBase.createZKClient(hostPort, 15000);
for (int i = 0; i < 50; i++) {
zk.create("/" + i, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
zk.close();
ZKDatabase zkDb;
{
ZooKeeperServer zs = ClientBase.getServer(factory);
zkDb = zs.getZKDatabase();
}
factory.shutdown();
try {
zkDb.close();
} catch (IOException ie) {
LOG.warn("Error closing logs ", ie);
}
int tickTime = 2000;
int initLimit = 3;
int syncLimit = 3;
int port1 = PortAssignment.unique();
int port2 = PortAssignment.unique();
int port3 = PortAssignment.unique();
// Start up two of the quorum and add 10 txns
Map<Long, QuorumServer> peers = new HashMap<Long, QuorumServer>();
peers.put(Long.valueOf(1), new QuorumServer(1, new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", port1)));
peers.put(Long.valueOf(2), new QuorumServer(2, new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", port2)));
peers.put(Long.valueOf(3), new QuorumServer(3, new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", port3)));
QuorumPeer s2 = new QuorumPeer(peers, dataDir2, dataDir2, port2, 3, 2, tickTime, initLimit, syncLimit);
s2.start();
QuorumPeer s3 = new QuorumPeer(peers, dataDir3, dataDir3, port3, 3, 3, tickTime, initLimit, syncLimit);
s3.start();
zk = ClientBase.createZKClient("127.0.0.1:" + port2, 15000);
for (int i = 0; i < 10; i++) {
zk.create("/" + i, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
}
zk.close();
final ZooKeeper zk2 = ClientBase.createZKClient("127.0.0.1:" + port2, 15000);
zk2.getData("/9", false, new Stat());
try {
zk2.getData("/10", false, new Stat());
Assert.fail("Should have gotten an error");
} catch (KeeperException.NoNodeException e) {
// this is what we want
}
QuorumPeer s1 = new QuorumPeer(peers, dataDir1, dataDir1, port1, 3, 1, tickTime, initLimit, syncLimit);
s1.start();
ZooKeeper zk1 = ClientBase.createZKClient("127.0.0.1:" + port1, 15000);
zk1.getData("/9", false, new Stat());
try {
// /10 wont work because the session expiration
// will match the zxid for /10 and so we wont
// actually truncate the zxid for /10 creation
// due to an artifact of switching the xid of the standalone
// /11 is the last entry in the log for the xid
// as a result /12 is the first of the truncated znodes to check for
zk1.getData("/12", false, new Stat());
Assert.fail("Should have gotten an error");
} catch (KeeperException.NoNodeException e) {
// this is what we want
}
zk1.close();
QuorumBase.shutdown(s1);
QuorumBase.shutdown(s2);
QuorumBase.shutdown(s3);
}
Aggregations