浏览代码

ZOOKEEPER-431. Expose methods to ease ZK integration

git-svn-id: https://svn.apache.org/repos/asf/hadoop/zookeeper/trunk@781914 13f79535-47bb-0310-9956-ffa450edef68
Benjamin Reed 16 年之前
父节点
当前提交
a7521e4091

+ 2 - 0
CHANGES.txt

@@ -185,6 +185,8 @@ IMPROVEMENTS:
   ZOOKEEPER-383. Asynchronous version of createLedger(). (flavio via mahadev)
   ZOOKEEPER-383. Asynchronous version of createLedger(). (flavio via mahadev)
 
 
   ZOOKEEPER-358. Throw exception when ledger does not exist. (flavio via breed)
   ZOOKEEPER-358. Throw exception when ledger does not exist. (flavio via breed)
+
+  ZOOKEEPER-431. Expose methods to ease ZK integration. (Jean-Daniel via breed)
  
  
 NEW FEATURES:
 NEW FEATURES:
 
 

+ 12 - 4
src/java/main/org/apache/zookeeper/server/ServerConfig.java

@@ -67,10 +67,18 @@ public class ServerConfig {
 
 
         // let qpconfig parse the file and then pull the stuff we are
         // let qpconfig parse the file and then pull the stuff we are
         // interested in
         // interested in
-        clientPort = config.getClientPort();
-        dataDir = config.getDataDir();
-        dataLogDir = config.getDataLogDir();
-        tickTime = config.getTickTime();
+        readFrom(config);
+    }
+
+    /**
+     * Read attributes from a QuorumPeerConfig.
+     * @param config
+     */
+    public void readFrom(QuorumPeerConfig config) {
+      clientPort = config.getClientPort();
+      dataDir = config.getDataDir();
+      dataLogDir = config.getDataLogDir();
+      tickTime = config.getTickTime();
     }
     }
 
 
     public int getClientPort() { return clientPort; }
     public int getClientPort() { return clientPort; }

+ 9 - 0
src/java/main/org/apache/zookeeper/server/ZooKeeperServerMain.java

@@ -82,6 +82,15 @@ public class ZooKeeperServerMain {
             config.parse(args);
             config.parse(args);
         }
         }
 
 
+        runFromConfig(config);
+    }
+
+    /**
+     * Run from a ServerConfig.
+     * @param config ServerConfig to use.
+     * @throws IOException
+     */
+    public void runFromConfig(ServerConfig config) throws IOException {
         LOG.info("Starting server");
         LOG.info("Starting server");
         try {
         try {
             // Note that this thread isn't going to be doing anything else,
             // Note that this thread isn't going to be doing anything else,

+ 9 - 1
src/java/main/org/apache/zookeeper/server/quorum/QuorumPeerConfig.java

@@ -100,7 +100,13 @@ public class QuorumPeerConfig {
         }
         }
     }
     }
 
 
-    protected void parseProperties(Properties zkProp)
+    /**
+     * Parse config from a Properties.
+     * @param zkProp Properties to parse from.
+     * @throws IOException
+     * @throws ConfigException
+     */
+    public void parseProperties(Properties zkProp)
     throws IOException, ConfigException {
     throws IOException, ConfigException {
         for (Entry<Object, Object> entry : zkProp.entrySet()) {
         for (Entry<Object, Object> entry : zkProp.entrySet()) {
             String key = entry.getKey().toString().trim();
             String key = entry.getKey().toString().trim();
@@ -263,4 +269,6 @@ public class QuorumPeerConfig {
     }
     }
 
 
     public long getServerId() { return serverId; }
     public long getServerId() { return serverId; }
+
+    public boolean isDistributed() { return servers.size() > 1; }
 }
 }

+ 35 - 31
src/java/main/org/apache/zookeeper/server/quorum/QuorumPeerMain.java

@@ -99,37 +99,7 @@ public class QuorumPeerMain {
         }
         }
 
 
         if (args.length == 1 && config.servers.size() > 0) {
         if (args.length == 1 && config.servers.size() > 0) {
-            try {
-                ManagedUtil.registerLog4jMBeans();
-            } catch (JMException e) {
-                LOG.warn("Unable to register log4j JMX control", e);
-            }
-
-            LOG.info("Starting quorum peer");
-            try {
-                NIOServerCnxn.Factory cnxnFactory =
-                    new NIOServerCnxn.Factory(config.getClientPort());
-
-                quorumPeer = new QuorumPeer();
-                quorumPeer.setClientPort(config.getClientPort());
-                quorumPeer.setTxnFactory(new FileTxnSnapLog(
-                            new File(config.getDataLogDir()),
-                            new File(config.getDataDir())));
-                quorumPeer.setQuorumPeers(config.getServers());
-                quorumPeer.setElectionType(config.getElectionAlg());
-                quorumPeer.setMyid(config.getServerId());
-                quorumPeer.setTickTime(config.getTickTime());
-                quorumPeer.setInitLimit(config.getInitLimit());
-                quorumPeer.setSyncLimit(config.getSyncLimit());
-                quorumPeer.setQuorumVerifier(config.getQuorumVerifier());
-                quorumPeer.setCnxnFactory(cnxnFactory);
-
-                quorumPeer.start();
-                quorumPeer.join();
-            } catch (InterruptedException e) {
-                // warn, but generally this is ok
-                LOG.warn("Quorum Peer interrupted", e);
-            }
+            runFromConfig(config);
         } else {
         } else {
             LOG.warn("Either no config or no quorum defined in config, running "
             LOG.warn("Either no config or no quorum defined in config, running "
                     + " in standalone mode");
                     + " in standalone mode");
@@ -138,6 +108,40 @@ public class QuorumPeerMain {
         }
         }
     }
     }
 
 
+    public void runFromConfig(QuorumPeerConfig config) throws IOException {
+      try {
+          ManagedUtil.registerLog4jMBeans();
+      } catch (JMException e) {
+          LOG.warn("Unable to register log4j JMX control", e);
+      }
+  
+      LOG.info("Starting quorum peer");
+      try {
+          NIOServerCnxn.Factory cnxnFactory =
+              new NIOServerCnxn.Factory(config.getClientPort());
+  
+          quorumPeer = new QuorumPeer();
+          quorumPeer.setClientPort(config.getClientPort());
+          quorumPeer.setTxnFactory(new FileTxnSnapLog(
+                      new File(config.getDataLogDir()),
+                      new File(config.getDataDir())));
+          quorumPeer.setQuorumPeers(config.getServers());
+          quorumPeer.setElectionType(config.getElectionAlg());
+          quorumPeer.setMyid(config.getServerId());
+          quorumPeer.setTickTime(config.getTickTime());
+          quorumPeer.setInitLimit(config.getInitLimit());
+          quorumPeer.setSyncLimit(config.getSyncLimit());
+          quorumPeer.setQuorumVerifier(config.getQuorumVerifier());
+          quorumPeer.setCnxnFactory(cnxnFactory);
+  
+          quorumPeer.start();
+          quorumPeer.join();
+      } catch (InterruptedException e) {
+          // warn, but generally this is ok
+          LOG.warn("Quorum Peer interrupted", e);
+      }
+    }
+
     protected void shutdown() {
     protected void shutdown() {
         quorumPeer.shutdown();
         quorumPeer.shutdown();
     }
     }