Return-Path: X-Original-To: apmail-cxf-commits-archive@www.apache.org Delivered-To: apmail-cxf-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id C0BC9D485 for ; Wed, 31 Oct 2012 12:07:53 +0000 (UTC) Received: (qmail 24241 invoked by uid 500); 31 Oct 2012 12:07:53 -0000 Delivered-To: apmail-cxf-commits-archive@cxf.apache.org Received: (qmail 24178 invoked by uid 500); 31 Oct 2012 12:07:53 -0000 Mailing-List: contact commits-help@cxf.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cxf.apache.org Delivered-To: mailing list commits@cxf.apache.org Received: (qmail 24168 invoked by uid 99); 31 Oct 2012 12:07:53 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 31 Oct 2012 12:07:53 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 31 Oct 2012 12:07:48 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 3A1E323888FE; Wed, 31 Oct 2012 12:07:03 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1404107 - in /cxf/dosgi/trunk/discovery/distributed/zookeeper-server: ./ src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/server/ src/main/resources/ src/main/resources/OSGI-INF/ src/main/resources/OSGI-INF/metatype/ src/test/java/or... Date: Wed, 31 Oct 2012 12:07:02 -0000 To: commits@cxf.apache.org From: cschneider@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20121031120703.3A1E323888FE@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: cschneider Date: Wed Oct 31 12:07:02 2012 New Revision: 1404107 URL: http://svn.apache.org/viewvc?rev=1404107&view=rev Log: DOSGI-136 Refactoring zookeeper starter, improved error handling, metatype configs Added: cxf/dosgi/trunk/discovery/distributed/zookeeper-server/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/server/ZookeeperStarter.java - copied, changed from r1402482, cxf/dosgi/trunk/discovery/distributed/zookeeper-server/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/server/ManagedService.java cxf/dosgi/trunk/discovery/distributed/zookeeper-server/src/main/resources/ cxf/dosgi/trunk/discovery/distributed/zookeeper-server/src/main/resources/OSGI-INF/ cxf/dosgi/trunk/discovery/distributed/zookeeper-server/src/main/resources/OSGI-INF/metatype/ cxf/dosgi/trunk/discovery/distributed/zookeeper-server/src/main/resources/OSGI-INF/metatype/zookeeper.xml (with props) cxf/dosgi/trunk/discovery/distributed/zookeeper-server/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/server/ZookeeperStarterTest.java - copied, changed from r1402482, cxf/dosgi/trunk/discovery/distributed/zookeeper-server/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/server/ManagedServiceTest.java Removed: cxf/dosgi/trunk/discovery/distributed/zookeeper-server/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/server/ManagedService.java cxf/dosgi/trunk/discovery/distributed/zookeeper-server/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/server/ActivatorTest.java cxf/dosgi/trunk/discovery/distributed/zookeeper-server/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/server/ManagedServiceTest.java Modified: cxf/dosgi/trunk/discovery/distributed/zookeeper-server/pom.xml cxf/dosgi/trunk/discovery/distributed/zookeeper-server/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/server/Activator.java Modified: cxf/dosgi/trunk/discovery/distributed/zookeeper-server/pom.xml URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/zookeeper-server/pom.xml?rev=1404107&r1=1404106&r2=1404107&view=diff ============================================================================== --- cxf/dosgi/trunk/discovery/distributed/zookeeper-server/pom.xml (original) +++ cxf/dosgi/trunk/discovery/distributed/zookeeper-server/pom.xml Wed Oct 31 12:07:02 2012 @@ -103,7 +103,7 @@ ZooKeeper server control bundle - This bundle can run the ZooKeeper server in an OSGi Framework + Runs the ZooKeeper server ${project.artifactId} org.apache.cxf.dosgi.discovery.zookeeper.server.Activator * Modified: cxf/dosgi/trunk/discovery/distributed/zookeeper-server/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/server/Activator.java URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/zookeeper-server/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/server/Activator.java?rev=1404107&r1=1404106&r2=1404107&view=diff ============================================================================== --- cxf/dosgi/trunk/discovery/distributed/zookeeper-server/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/server/Activator.java (original) +++ cxf/dosgi/trunk/discovery/distributed/zookeeper-server/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/server/Activator.java Wed Oct 31 12:07:02 2012 @@ -17,33 +17,26 @@ */ package org.apache.cxf.dosgi.discovery.zookeeper.server; +import java.util.Dictionary; import java.util.Hashtable; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceRegistration; +import org.osgi.framework.Constants; public class Activator implements BundleActivator { - ManagedService ms; - ServiceRegistration reg; + ZookeeperStarter zkStarter; public void start(BundleContext context) throws Exception { - ms = new ManagedService(context); - Hashtable props = new Hashtable(); - ms.setDefaults(props); - - // cannot use the org.osgi.service.cm.ManagedService class object directly from - // Activator because we have an optional dependency on it... - reg = context.registerService(org.osgi.service.cm.ManagedService.class.getName(), ms, props); - ms.setRegistration(reg); + zkStarter = new ZookeeperStarter(context); + Dictionary props = new Hashtable(); + props.put(Constants.SERVICE_PID, "org.apache.cxf.dosgi.discovery.zookeeper.server"); + context.registerService(org.osgi.service.cm.ManagedService.class.getName(), zkStarter, props ); } public void stop(BundleContext context) throws Exception { - if (reg != null) { - reg.unregister(); - } - if (ms != null) { - ms.shutdown(); + if (zkStarter != null) { + zkStarter.shutdown(); } } } Copied: cxf/dosgi/trunk/discovery/distributed/zookeeper-server/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/server/ZookeeperStarter.java (from r1402482, cxf/dosgi/trunk/discovery/distributed/zookeeper-server/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/server/ManagedService.java) URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/zookeeper-server/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/server/ZookeeperStarter.java?p2=cxf/dosgi/trunk/discovery/distributed/zookeeper-server/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/server/ZookeeperStarter.java&p1=cxf/dosgi/trunk/discovery/distributed/zookeeper-server/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/server/ManagedService.java&r1=1402482&r2=1404107&rev=1404107&view=diff ============================================================================== --- cxf/dosgi/trunk/discovery/distributed/zookeeper-server/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/server/ManagedService.java (original) +++ cxf/dosgi/trunk/discovery/distributed/zookeeper-server/src/main/java/org/apache/cxf/dosgi/discovery/zookeeper/server/ZookeeperStarter.java Wed Oct 31 12:07:02 2012 @@ -23,129 +23,109 @@ import java.util.Dictionary; import java.util.Enumeration; import java.util.Properties; +import org.apache.log4j.Level; import org.apache.log4j.Logger; import org.apache.zookeeper.server.ServerConfig; import org.apache.zookeeper.server.ZooKeeperServerMain; import org.apache.zookeeper.server.quorum.QuorumPeerConfig; +import org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException; import org.osgi.framework.BundleContext; -import org.osgi.framework.Constants; -import org.osgi.framework.ServiceRegistration; import org.osgi.service.cm.ConfigurationException; -public class ManagedService implements org.osgi.service.cm.ManagedService { - private static final Logger LOG = Logger.getLogger(ManagedService.class); +public class ZookeeperStarter implements org.osgi.service.cm.ManagedService { + private static final Logger LOG = Logger.getLogger(ZookeeperStarter.class); private final BundleContext bundleContext; - ServiceRegistration serviceRegistration; - MyZooKeeperServerMain main; - Thread zkMainThread; - - public ManagedService(BundleContext ctx) { + protected MyZooKeeperServerMain main; + private Thread zkMainThread; + + public ZookeeperStarter(BundleContext ctx) { bundleContext = ctx; } - - public synchronized void shutdown() { + + synchronized void shutdown() { if (main != null) { LOG.info("Shutting down ZooKeeper server"); - main.shutdown(); try { - zkMainThread.join(); - } catch (InterruptedException e) { - // ignore + main.shutdown(); + if (zkMainThread != null) { + zkMainThread.join(); + } + } catch (Throwable e) { + LOG.log(Level.ERROR, e.getMessage(), e); } main = null; zkMainThread = null; } } - - @SuppressWarnings("unchecked") + + @SuppressWarnings("rawtypes") public void setDefaults(Dictionary dict) throws IOException { - setDefault(dict, "tickTime", "2000"); + setDefault(dict, "tickTime", "2000"); setDefault(dict, "initLimit", "10"); setDefault(dict, "syncLimit", "5"); - setDefault(dict, "dataDir", new File(bundleContext.getDataFile(""), "zkdata").getCanonicalPath()); - setDefault(dict, Constants.SERVICE_PID, "org.apache.cxf.dosgi.discovery.zookeeper.server"); + setDefault(dict, "clientPort", "2181"); + setDefault(dict, "dataDir", new File(bundleContext.getDataFile(""), "zkdata").getCanonicalPath()); } - - @SuppressWarnings("unchecked") + + @SuppressWarnings({ "rawtypes", "unchecked" }) private void setDefault(Dictionary dict, String key, String value) { if (dict.get(key) == null) { dict.put(key, value); } } - @SuppressWarnings("unchecked") + @SuppressWarnings("rawtypes") public synchronized void updated(Dictionary dict) throws ConfigurationException { + shutdown(); if (dict == null) { - shutdown(); return; } - - if (main != null) { - // stop the current instance - shutdown(); - // then reconfigure and start again. - } - - if (dict.get("clientPort") == null) { - LOG.info("Ignoring configuration update because required property 'clientPort' isn't set."); - return; + try { + setDefaults(dict); + QuorumPeerConfig config = parseConfig(dict); + startFromConfig(config); + LOG.info("Applied configuration update :" + dict); + } catch (Exception th) { + LOG.error("Problem applying configuration update: " + dict, th); } - + } + + @SuppressWarnings("rawtypes") + private QuorumPeerConfig parseConfig(Dictionary dict) throws IOException, ConfigException { Properties props = new Properties(); - for (Enumeration e = dict.keys(); e.hasMoreElements(); ) { + for (Enumeration e = dict.keys(); e.hasMoreElements();) { Object key = e.nextElement(); props.put(key, dict.get(key)); } - - try { - setDefaults(props); - if (serviceRegistration != null) { - serviceRegistration.setProperties(props); - } - - QuorumPeerConfig config = new QuorumPeerConfig(); - config.parseProperties(props); - final ServerConfig serverConfig = new ServerConfig(); - serverConfig.readFrom(config); - - main = getZooKeeperMain(); - zkMainThread = new Thread(new Runnable() { - public void run() { - try { - main.runFromConfig(serverConfig); - } catch (IOException e) { - LOG.error("Problem running ZooKeeper server.", e); - } - } - }); - startThread(); - - LOG.info("Applied configuration update :" + props); - } catch (Exception th) { - LOG.error("Problem applying configuration update: " + props, th); - } + QuorumPeerConfig config = new QuorumPeerConfig(); + config.parseProperties(props); + return config; } - // Isolated for testing - void startThread() { + protected void startFromConfig(QuorumPeerConfig config) throws IOException, InterruptedException { + final ServerConfig serverConfig = new ServerConfig(); + serverConfig.readFrom(config); + + main = new MyZooKeeperServerMain(); + zkMainThread = new Thread(new Runnable() { + public void run() { + try { + main.runFromConfig(serverConfig); + } catch (Throwable e) { + LOG.error("Problem running ZooKeeper server.", e); + } + } + }); zkMainThread.start(); } - // Isolated for testing - MyZooKeeperServerMain getZooKeeperMain() { - return new MyZooKeeperServerMain(); - } - - public void setRegistration(ServiceRegistration reg) { - serviceRegistration = reg; - } - + // Make the shutdown accessible from here static class MyZooKeeperServerMain extends ZooKeeperServerMain { @Override protected void shutdown() { super.shutdown(); - // Make the shutdown accessible from here. } } + } Added: cxf/dosgi/trunk/discovery/distributed/zookeeper-server/src/main/resources/OSGI-INF/metatype/zookeeper.xml URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/zookeeper-server/src/main/resources/OSGI-INF/metatype/zookeeper.xml?rev=1404107&view=auto ============================================================================== --- cxf/dosgi/trunk/discovery/distributed/zookeeper-server/src/main/resources/OSGI-INF/metatype/zookeeper.xml (added) +++ cxf/dosgi/trunk/discovery/distributed/zookeeper-server/src/main/resources/OSGI-INF/metatype/zookeeper.xml Wed Oct 31 12:07:02 2012 @@ -0,0 +1,34 @@ + + + + + + + + + + + + + \ No newline at end of file Propchange: cxf/dosgi/trunk/discovery/distributed/zookeeper-server/src/main/resources/OSGI-INF/metatype/zookeeper.xml ------------------------------------------------------------------------------ svn:mime-type = text/plain Copied: cxf/dosgi/trunk/discovery/distributed/zookeeper-server/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/server/ZookeeperStarterTest.java (from r1402482, cxf/dosgi/trunk/discovery/distributed/zookeeper-server/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/server/ManagedServiceTest.java) URL: http://svn.apache.org/viewvc/cxf/dosgi/trunk/discovery/distributed/zookeeper-server/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/server/ZookeeperStarterTest.java?p2=cxf/dosgi/trunk/discovery/distributed/zookeeper-server/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/server/ZookeeperStarterTest.java&p1=cxf/dosgi/trunk/discovery/distributed/zookeeper-server/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/server/ManagedServiceTest.java&r1=1402482&r2=1404107&rev=1404107&view=diff ============================================================================== --- cxf/dosgi/trunk/discovery/distributed/zookeeper-server/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/server/ManagedServiceTest.java (original) +++ cxf/dosgi/trunk/discovery/distributed/zookeeper-server/src/test/java/org/apache/cxf/dosgi/discovery/zookeeper/server/ZookeeperStarterTest.java Wed Oct 31 12:07:02 2012 @@ -17,173 +17,66 @@ */ package org.apache.cxf.dosgi.discovery.zookeeper.server; +import static org.easymock.EasyMock.expect; +import static org.easymock.classextension.EasyMock.replay; +import static org.easymock.classextension.EasyMock.verify; + import java.io.File; -import java.util.Dictionary; +import java.io.IOException; import java.util.Hashtable; import junit.framework.TestCase; -import org.apache.cxf.dosgi.discovery.zookeeper.server.ManagedService.MyZooKeeperServerMain; -import org.apache.zookeeper.ZooKeeperMain; -import org.apache.zookeeper.server.ServerConfig; -import org.easymock.IAnswer; +import org.apache.cxf.dosgi.discovery.zookeeper.server.ZookeeperStarter.MyZooKeeperServerMain; +import org.apache.zookeeper.server.quorum.QuorumPeerConfig; import org.easymock.classextension.EasyMock; +import org.easymock.classextension.IMocksControl; import org.osgi.framework.BundleContext; -import org.osgi.framework.Constants; -import org.osgi.framework.ServiceRegistration; -import org.osgi.service.cm.ConfigurationException; -public class ManagedServiceTest extends TestCase { - public void testManagedService() throws Exception { +public class ZookeeperStarterTest extends TestCase { + public void testUpdateConfig() throws Exception { final File tempDir = new File(System.getProperty("java.io.tmpdir")); - - final ManagedService.MyZooKeeperServerMain zkMain = - EasyMock.createMock(ManagedService.MyZooKeeperServerMain.class); - zkMain.runFromConfig((ServerConfig) EasyMock.anyObject()); - EasyMock.expectLastCall().andAnswer(new IAnswer() { - public Object answer() throws Throwable { - ServerConfig sc = (ServerConfig) EasyMock.getCurrentArguments()[0]; - assertEquals(new File(tempDir, "zkdata").getCanonicalFile().toString(), - sc.getDataDir()); - assertEquals(1234, sc.getClientPortAddress().getPort()); - assertEquals(2000, sc.getTickTime()); - return null; - } - }); - EasyMock.replay(zkMain); - - BundleContext bc = EasyMock.createMock(BundleContext.class); - EasyMock.expect(bc.getDataFile("")).andReturn(tempDir); - EasyMock.replay(bc); - - final StringBuilder threadStatus = new StringBuilder(); - ManagedService ms = new ManagedService(bc) { - @Override - ManagedService.MyZooKeeperServerMain getZooKeeperMain() { - return zkMain; + IMocksControl control = EasyMock.createControl(); + BundleContext bc = control.createMock(BundleContext.class); + expect(bc.getDataFile("")).andReturn(tempDir); + final MyZooKeeperServerMain mockServer = control.createMock(MyZooKeeperServerMain.class); + control.replay(); + + ZookeeperStarter starter = new ZookeeperStarter(bc) { + + @Override + protected void startFromConfig(QuorumPeerConfig config) throws IOException, InterruptedException { + assertEquals(1234, config.getClientPortAddress().getPort()); + assertEquals(tempDir + File.separator + "zkdata", config.getDataDir()); + assertEquals(2000, config.getTickTime()); + assertEquals(10, config.getInitLimit()); + assertEquals(5, config.getSyncLimit()); + this.main = mockServer; } - @Override - void startThread() { - threadStatus.append("started"); - } }; - - ServiceRegistration sreg = EasyMock.createMock(ServiceRegistration.class); - final Hashtable expected = new Hashtable(); - expected.put("tickTime", "2000"); - expected.put("initLimit", "10"); - expected.put("syncLimit", "5"); - expected.put("dataDir", new File(tempDir, "zkdata").getCanonicalFile().toString()); - expected.put(Constants.SERVICE_PID, "org.apache.cxf.dosgi.discovery.zookeeper.server"); - expected.put("clientPort", "1234"); - sreg.setProperties(expected); - EasyMock.expectLastCall(); - EasyMock.replay(sreg); - ms.setRegistration(sreg); - Hashtable props = new Hashtable(); - props.put(Constants.SERVICE_PID, "org.apache.cxf.dosgi.discovery.zookeeper.server"); props.put("clientPort", "1234"); - - ms.updated(props); - EasyMock.verify(sreg); + starter.updated(props); + assertNotNull(starter.main); - assertEquals("started", threadStatus.toString()); - ms.zkMainThread.run(); - - EasyMock.verify(zkMain); - EasyMock.verify(bc); - - EasyMock.reset(zkMain); - zkMain.shutdown(); - EasyMock.expectLastCall(); - EasyMock.verify(); - - assertNotNull(ms.main); - ms.shutdown(); - assertNull(ms.main); - assertNull(ms.zkMainThread); + control.verify(); } public void testRemoveConfiguration() throws Exception { BundleContext bc = EasyMock.createMock(BundleContext.class); + MyZooKeeperServerMain zkServer = EasyMock.createMock(MyZooKeeperServerMain.class); + zkServer.shutdown(); + EasyMock.expectLastCall(); - final StringBuilder shutDownTracker = new StringBuilder(); - ManagedService ms = new ManagedService(bc) { - @Override - public synchronized void shutdown() { - shutDownTracker.append("called"); - } - - @Override - void startThread() {} - }; - - assertEquals("Precondition failed", 0, shutDownTracker.length()); - ms.updated(null); - assertEquals("called", shutDownTracker.toString()); - // check that it didn't get reinitialized TODO - } - - public void testNewConfiguration() throws Exception { - BundleContext bc = EasyMock.createMock(BundleContext.class); - - final StringBuilder shutDownTracker = new StringBuilder(); - ManagedService ms = new ManagedService(bc) { - @Override - public synchronized void shutdown() { - shutDownTracker.append("called"); - } + replay(zkServer); - @Override - void startThread() {} - }; - - assertEquals("Precondition failed", 0, shutDownTracker.length()); - assertNull("Precondition failed", ms.main); - assertNull("Precondition failed", ms.zkMainThread); + ZookeeperStarter starter = new ZookeeperStarter(bc); + starter.main = zkServer; + starter.updated(null); - Hashtable props = new Hashtable(); - props.put("clientPort", "9911"); - ms.updated(props); - assertEquals("Shutdown should not have been called", 0, shutDownTracker.length()); - assertNotNull(ms.main); - assertNotNull(ms.zkMainThread); + verify(zkServer); + assertNull("main should be null", starter.main); } - public void testChangeConfiguration() throws Exception { - BundleContext bc = EasyMock.createMock(BundleContext.class); - - final StringBuilder shutDownTracker = new StringBuilder(); - ManagedService ms = new ManagedService(bc) { - @Override - public synchronized void shutdown() { - shutDownTracker.append("called"); - } - - @Override - void startThread() {} - }; - - MyZooKeeperServerMain initialMsMain = - EasyMock.createMock(ManagedService.MyZooKeeperServerMain.class); - ms.main = initialMsMain; - Thread initialZkThread = new Thread(); - ms.zkMainThread = initialZkThread; - - assertEquals("Precondition failed", 0, shutDownTracker.length()); - assertNotNull("Precondition failed", ms.main); - assertNotNull("Precondition failed", ms.zkMainThread); - - Hashtable props = new Hashtable(); - props.put("clientPort", "9911"); - ms.updated(props); - assertEquals("We are reconfiguring, so shutdown should be called", - "called", shutDownTracker.toString()); - assertNotNull(ms.main); - assertNotNull(ms.zkMainThread); - assertNotSame(ms.main, initialMsMain); - assertNotSame(ms.zkMainThread, initialZkThread); - } }