HBASE-3052 Add ability to have multiple ZK servers in a quorum in MiniZooKeeperCluster for test writing

git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@1084008 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Stack 2011-03-21 22:48:47 +00:00
parent c4fb32cc22
commit f1996d4932
4 changed files with 136 additions and 16 deletions

View File

@ -86,6 +86,8 @@ Release 0.91.0 - Unreleased
needs an upper bound added (Ted Yu via Stack) needs an upper bound added (Ted Yu via Stack)
HBASE-3676 Update region server load for AssignmentManager through HBASE-3676 Update region server load for AssignmentManager through
regionServerReport() (Ted Yu via Stack) regionServerReport() (Ted Yu via Stack)
HBASE-3052 Add ability to have multiple ZK servers in a quorum in
MiniZooKeeperCluster for test writing (Liyin Tang via Stack)
TASK TASK
HBASE-3559 Move report of split to master OFF the heartbeat channel HBASE-3559 Move report of split to master OFF the heartbeat channel

View File

@ -28,6 +28,8 @@ import java.io.Reader;
import java.net.BindException; import java.net.BindException;
import java.net.InetSocketAddress; import java.net.InetSocketAddress;
import java.net.Socket; import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -48,14 +50,21 @@ public class MiniZooKeeperCluster {
private static final int CONNECTION_TIMEOUT = 30000; private static final int CONNECTION_TIMEOUT = 30000;
private boolean started; private boolean started;
private int clientPort = 21818; // use non-standard port
private int defaultClientPort = 21818; // use non-standard port
private int clientPort = defaultClientPort;
private int zooKeeperCandidateNum = 0;
private NIOServerCnxn.Factory standaloneServerFactory; private NIOServerCnxn.Factory standaloneServerFactory;
private int currentZooKeeper;
private List<ZooKeeperServer> zooKeeperServers;
private int tickTime = 0; private int tickTime = 0;
/** Create mini ZooKeeper cluster. */ /** Create mini ZooKeeper cluster. */
public MiniZooKeeperCluster() { public MiniZooKeeperCluster() {
this.started = false; this.started = false;
currentZooKeeper = -1;
zooKeeperServers = new ArrayList();
} }
public void setClientPort(int clientPort) { public void setClientPort(int clientPort) {
@ -70,6 +79,10 @@ public class MiniZooKeeperCluster {
this.tickTime = tickTime; this.tickTime = tickTime;
} }
public int getZooKeeperCandidateNum() {
return zooKeeperCandidateNum;
}
// / XXX: From o.a.zk.t.ClientBase // / XXX: From o.a.zk.t.ClientBase
private static void setupTestEnv() { private static void setupTestEnv() {
// during the tests we run with 100K prealloc in the logs. // during the tests we run with 100K prealloc in the logs.
@ -80,20 +93,29 @@ public class MiniZooKeeperCluster {
FileTxnLog.setPreallocSize(100); FileTxnLog.setPreallocSize(100);
} }
public int startup(File baseDir) throws IOException,
InterruptedException {
return startup(baseDir,1);
}
/** /**
* @param baseDir * @param baseDir
* @param numZooKeeperServers
* @return ClientPort server bound to. * @return ClientPort server bound to.
* @throws IOException * @throws IOException
* @throws InterruptedException * @throws InterruptedException
*/ */
public int startup(File baseDir) throws IOException, public int startup(File baseDir, int numZooKeeperServers) throws IOException,
InterruptedException { InterruptedException {
if (numZooKeeperServers <= 0)
return -1;
setupTestEnv(); setupTestEnv();
shutdown(); shutdown();
File dir = new File(baseDir, "zookeeper").getAbsoluteFile(); for (int i = 0; i < numZooKeeperServers; i++) {
File dir = new File(baseDir, "zookeeper_"+i).getAbsoluteFile();
recreateDir(dir); recreateDir(dir);
int tickTimeToUse; int tickTimeToUse;
@ -103,6 +125,8 @@ public class MiniZooKeeperCluster {
tickTimeToUse = TICK_TIME; tickTimeToUse = TICK_TIME;
} }
ZooKeeperServer server = new ZooKeeperServer(dir, dir, tickTimeToUse); ZooKeeperServer server = new ZooKeeperServer(dir, dir, tickTimeToUse);
zooKeeperServers.add(server);
}
while (true) { while (true) {
try { try {
standaloneServerFactory = standaloneServerFactory =
@ -115,13 +139,14 @@ public class MiniZooKeeperCluster {
} }
break; break;
} }
standaloneServerFactory.startup(server); currentZooKeeper = 0;
standaloneServerFactory.startup(zooKeeperServers.get(currentZooKeeper));
if (!waitForServerUp(clientPort, CONNECTION_TIMEOUT)) { if (!waitForServerUp(clientPort, CONNECTION_TIMEOUT)) {
throw new IOException("Waiting for startup of standalone server"); throw new IOException("Waiting for startup of standalone server");
} }
started = true; started = true;
zooKeeperCandidateNum = numZooKeeperServers-1;
LOG.info("Started MiniZK Server on client port: " + clientPort); LOG.info("Started MiniZK Server on client port: " + clientPort);
return clientPort; return clientPort;
} }
@ -151,6 +176,56 @@ public class MiniZooKeeperCluster {
} }
started = false; started = false;
zooKeeperCandidateNum = 0;
LOG.info("Shutdown MiniZK Server on client port: " + clientPort);
}
/**@return clientPort return clientPort if there is another ZooKeeper Candidate can run; return
* -1, if there is no candidates.
* @throws IOException
* @throws InterruptedException
*/
public int killCurrentZooKeeper() throws IOException,
InterruptedException {
if (!started) {
return -1;
}
// Shutdown the current one
standaloneServerFactory.shutdown();
if (!waitForServerDown(clientPort, CONNECTION_TIMEOUT)) {
throw new IOException("Waiting for shutdown of standalone server");
}
LOG.info("Kill the current MiniZK Server on client port: "
+ clientPort);
if (zooKeeperCandidateNum == 0) {
return -1;
}
// Start another ZooKeeper Server
clientPort = defaultClientPort;
while (true) {
try {
standaloneServerFactory =
new NIOServerCnxn.Factory(new InetSocketAddress(clientPort));
} catch (BindException e) {
LOG.info("Failed binding ZK Server to client port: " + clientPort);
//this port is already in use. try to use another
clientPort++;
continue;
}
break;
}
currentZooKeeper = (++currentZooKeeper) % zooKeeperServers.size() ;
standaloneServerFactory.startup(zooKeeperServers.get(currentZooKeeper));
if (!waitForServerUp(clientPort, CONNECTION_TIMEOUT)) {
throw new IOException("Waiting for startup of standalone server");
}
started = true;
zooKeeperCandidateNum--;
LOG.info("Started another candidate MiniZK Server on client port: " + clientPort);
return clientPort;
} }
// XXX: From o.a.zk.t.ClientBase // XXX: From o.a.zk.t.ClientBase

View File

@ -245,18 +245,38 @@ public class HBaseTestingUtility {
* @return zk cluster started. * @return zk cluster started.
*/ */
public MiniZooKeeperCluster startMiniZKCluster() throws Exception { public MiniZooKeeperCluster startMiniZKCluster() throws Exception {
return startMiniZKCluster(setupClusterTestBuildDir()); return startMiniZKCluster(setupClusterTestBuildDir(),1);
}
/**
* Call this if you only want a zk cluster.
* @param zooKeeperServerNum
* @see #startMiniZKCluster() if you want zk + dfs + hbase mini cluster.
* @throws Exception
* @see #shutdownMiniZKCluster()
* @return zk cluster started.
*/
public MiniZooKeeperCluster startMiniZKCluster(int zooKeeperServerNum)
throws Exception {
return startMiniZKCluster(setupClusterTestBuildDir(), zooKeeperServerNum);
} }
private MiniZooKeeperCluster startMiniZKCluster(final File dir) private MiniZooKeeperCluster startMiniZKCluster(final File dir)
throws Exception {
return startMiniZKCluster(dir,1);
}
private MiniZooKeeperCluster startMiniZKCluster(final File dir,
int zooKeeperServerNum)
throws Exception { throws Exception {
this.passedZkCluster = false; this.passedZkCluster = false;
if (this.zkCluster != null) { if (this.zkCluster != null) {
throw new IOException("Cluster already running at " + dir); throw new IOException("Cluster already running at " + dir);
} }
this.zkCluster = new MiniZooKeeperCluster(); this.zkCluster = new MiniZooKeeperCluster();
int clientPort = this.zkCluster.startup(dir); int clientPort = this.zkCluster.startup(dir,zooKeeperServerNum);
this.conf.set("hbase.zookeeper.property.clientPort", this.conf.set("hbase.zookeeper.property.clientPort",
Integer.toString(clientPort)); Integer.toString(clientPort));
return this.zkCluster; return this.zkCluster;

View File

@ -36,6 +36,7 @@ import org.apache.hadoop.hbase.client.HTable;
import org.apache.hadoop.hbase.client.Put; import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result; import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.hbase.zookeeper.MiniZooKeeperCluster;
import org.apache.hadoop.hdfs.MiniDFSCluster; import org.apache.hadoop.hdfs.MiniDFSCluster;
import org.junit.After; import org.junit.After;
import org.junit.AfterClass; import org.junit.AfterClass;
@ -137,6 +138,28 @@ public class TestHBaseTestingUtility {
} }
} }
@Test public void testMiniZooKeeper() throws Exception {
MiniZooKeeperCluster cluster1 = this.hbt.startMiniZKCluster();
try {
assertEquals(0, cluster1.getZooKeeperCandidateNum());
assertTrue((cluster1.killCurrentZooKeeper() == -1));
} finally {
cluster1.shutdown();
}
this.hbt.shutdownMiniZKCluster();
MiniZooKeeperCluster cluster2 = this.hbt.startMiniZKCluster(5);
try {
assertEquals(4, cluster2.getZooKeeperCandidateNum());
assertTrue((cluster2.killCurrentZooKeeper() > 0));
assertTrue((cluster2.killCurrentZooKeeper() > 0));
assertEquals(2, cluster2.getZooKeeperCandidateNum());
} finally {
cluster2.shutdown();
}
}
@Test public void testMiniDFSCluster() throws Exception { @Test public void testMiniDFSCluster() throws Exception {
MiniDFSCluster cluster = this.hbt.startMiniDFSCluster(1); MiniDFSCluster cluster = this.hbt.startMiniDFSCluster(1);
FileSystem dfs = cluster.getFileSystem(); FileSystem dfs = cluster.getFileSystem();