You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2017/03/12 14:36:46 UTC

[2/3] camel git commit: CAMEL-10986: Make it easier to extend so we can have a route policy for master component as well.

CAMEL-10986: Make it easier to extend so we can have a route policy for master component as well.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/6b1ecf19
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/6b1ecf19
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/6b1ecf19

Branch: refs/heads/master
Commit: 6b1ecf191ab423c85c578dba5c5c1cc0ed173c9a
Parents: bc81588
Author: Claus Ibsen <da...@apache.org>
Authored: Sun Mar 12 14:43:33 2017 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Sun Mar 12 14:43:33 2017 +0100

----------------------------------------------------------------------
 .../zookeepermaster/MasterConsumer.java         | 110 ++++++++-----------
 .../ZookeeperGroupListenerSupport.java          | 109 ++++++++++++++++++
 .../zookeepermaster/ZookeeperGroupSupport.java  |   2 +
 3 files changed, 156 insertions(+), 65 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/6b1ecf19/components/camel-zookeeper-master/src/main/java/org/apache/camel/component/zookeepermaster/MasterConsumer.java
----------------------------------------------------------------------
diff --git a/components/camel-zookeeper-master/src/main/java/org/apache/camel/component/zookeepermaster/MasterConsumer.java b/components/camel-zookeeper-master/src/main/java/org/apache/camel/component/zookeepermaster/MasterConsumer.java
index 067d6a8..94bd384 100644
--- a/components/camel-zookeeper-master/src/main/java/org/apache/camel/component/zookeepermaster/MasterConsumer.java
+++ b/components/camel-zookeeper-master/src/main/java/org/apache/camel/component/zookeepermaster/MasterConsumer.java
@@ -25,8 +25,6 @@ import org.apache.camel.SuspendableService;
 import org.apache.camel.api.management.ManagedAttribute;
 import org.apache.camel.api.management.ManagedOperation;
 import org.apache.camel.api.management.ManagedResource;
-import org.apache.camel.component.zookeepermaster.group.Group;
-import org.apache.camel.component.zookeepermaster.group.GroupListener;
 import org.apache.camel.impl.DefaultConsumer;
 import org.apache.camel.util.ServiceHelper;
 import org.slf4j.Logger;
@@ -36,34 +34,30 @@ import org.slf4j.LoggerFactory;
  * A consumer which is only really active while it holds the master lock
  */
 @ManagedResource(description = "Managed ZooKeeper Master Consumer")
-public class MasterConsumer extends DefaultConsumer implements GroupListener {
+public class MasterConsumer extends DefaultConsumer {
     private static final transient Logger LOG = LoggerFactory.getLogger(MasterConsumer.class);
 
+    private ZookeeperGroupListenerSupport groupListener;
     private final MasterEndpoint endpoint;
     private final Processor processor;
     private Consumer delegate;
     private SuspendableService delegateService;
-    private final Group<CamelNodeState> singleton;
     private volatile CamelNodeState thisNodeState;
 
     public MasterConsumer(MasterEndpoint endpoint, Processor processor) {
         super(endpoint, processor);
         this.endpoint = endpoint;
         this.processor = processor;
-        MasterComponent component = endpoint.getComponent();
-        String path = component.getCamelClusterPath(endpoint.getGroupName());
-        this.singleton = component.createGroup(path);
-        this.singleton.add(this);
     }
 
     @ManagedAttribute(description = "Are we connected to ZooKeeper")
     public boolean isConnected() {
-        return singleton.isConnected();
+        return groupListener.getGroup().isConnected();
     }
 
     @ManagedAttribute(description = "Are we the master")
     public boolean isMaster() {
-        return singleton.isMaster();
+        return groupListener.getGroup().isMaster();
     }
 
     @ManagedOperation(description = "Information about all the slaves")
@@ -72,7 +66,7 @@ public class MasterConsumer extends DefaultConsumer implements GroupListener {
             return new ObjectMapper()
                 .enable(SerializationFeature.INDENT_OUTPUT)
                 .disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES)
-                .writeValueAsString(singleton.slaves());
+                .writeValueAsString(groupListener.getGroup().slaves());
         } catch (Exception e) {
             return null;
         }
@@ -80,7 +74,7 @@ public class MasterConsumer extends DefaultConsumer implements GroupListener {
 
     @ManagedOperation(description = "Information about the last event in the cluster group")
     public String lastEvent() {
-        Object event = singleton.getLastState();
+        Object event = groupListener.getGroup().getLastState();
         return event != null ? event.toString() : null;
     }
 
@@ -92,10 +86,15 @@ public class MasterConsumer extends DefaultConsumer implements GroupListener {
     @Override
     protected void doStart() throws Exception {
         super.doStart();
-        singleton.start();
+
+        String path = endpoint.getComponent().getCamelClusterPath(endpoint.getGroupName());
+        this.groupListener = new ZookeeperGroupListenerSupport(path, endpoint, onLockOwned(), onDisconnected());
+        this.groupListener.setCamelContext(endpoint.getCamelContext());
+        ServiceHelper.startService(groupListener);
+
         LOG.info("Attempting to become master for endpoint: " + endpoint + " in " + endpoint.getCamelContext() + " with singletonID: " + endpoint.getGroupName());
         thisNodeState = createNodeState();
-        singleton.update(thisNodeState);
+        groupListener.updateState(thisNodeState);
     }
 
     @Override
@@ -103,8 +102,9 @@ public class MasterConsumer extends DefaultConsumer implements GroupListener {
         try {
             stopConsumer();
         } finally {
-            singleton.close();
+            ServiceHelper.stopAndShutdownServices(groupListener);
         }
+        super.doStop();
     }
 
     private CamelNodeState createNodeState() {
@@ -114,7 +114,7 @@ public class MasterConsumer extends DefaultConsumer implements GroupListener {
         return state;
     }
 
-    protected void stopConsumer() throws Exception {
+    private void stopConsumer() throws Exception {
         ServiceHelper.stopAndShutdownServices(delegate);
         ServiceHelper.stopAndShutdownServices(endpoint.getConsumerEndpoint());
         delegate = null;
@@ -138,63 +138,43 @@ public class MasterConsumer extends DefaultConsumer implements GroupListener {
         super.doSuspend();
     }
 
-    @Override
-    public void groupEvent(Group group, GroupEvent event) {
-        switch (event) {
-        case CONNECTED:
-            break;
-        case CHANGED:
-            if (singleton.isConnected()) {
-                if (singleton.isMaster()) {
-                    if (LOG.isDebugEnabled()) {
-                        LOG.debug("Master/Standby endpoint is Master for:  " + endpoint + " in " + endpoint.getCamelContext());
-                    }
-                    onLockOwned();
-                } else {
-                    if (LOG.isDebugEnabled()) {
-                        LOG.debug("Master/Standby endpoint is Standby for: " + endpoint + " in " + endpoint.getCamelContext());
+    protected Runnable onLockOwned() {
+        return () -> {
+            if (delegate == null) {
+                try {
+                    // ensure endpoint is also started
+                    LOG.info("Elected as master. Starting consumer: {}", endpoint.getConsumerEndpoint());
+                    ServiceHelper.startService(endpoint.getConsumerEndpoint());
+
+                    delegate = endpoint.getConsumerEndpoint().createConsumer(processor);
+                    delegateService = null;
+                    if (delegate instanceof SuspendableService) {
+                        delegateService = (SuspendableService) delegate;
                     }
+
+                    // Lets show we are starting the consumer.
+                    thisNodeState = createNodeState();
+                    thisNodeState.started = true;
+                    groupListener.updateState(thisNodeState);
+
+                    ServiceHelper.startService(delegate);
+                } catch (Exception e) {
+                    LOG.error("Failed to start master consumer for: " + endpoint, e);
                 }
+
+                LOG.info("Elected as master. Consumer started: {}", endpoint.getConsumerEndpoint());
             }
-            break;
-        case DISCONNECTED:
-            try {
-                LOG.info("Disconnecting as master. Stopping consumer: {}", endpoint.getConsumerEndpoint());
-                stopConsumer();
-            } catch (Exception e) {
-                LOG.warn("Failed to stop master consumer for: " + endpoint + ". This exception is ignored.", e);
-            }
-            break;
-        default:
-            // noop
-        }
+        };
     }
 
-    protected void onLockOwned() {
-        if (delegate == null) {
+    protected Runnable onDisconnected() {
+        return () -> {
             try {
-                // ensure endpoint is also started
-                LOG.info("Elected as master. Starting consumer: {}", endpoint.getConsumerEndpoint());
-                ServiceHelper.startService(endpoint.getConsumerEndpoint());
-
-                delegate = endpoint.getConsumerEndpoint().createConsumer(processor);
-                delegateService = null;
-                if (delegate instanceof SuspendableService) {
-                    delegateService = (SuspendableService) delegate;
-                }
-
-                // Lets show we are starting the consumer.
-                thisNodeState = createNodeState();
-                thisNodeState.started = true;
-                singleton.update(thisNodeState);
-
-                ServiceHelper.startService(delegate);
+                stopConsumer();
             } catch (Exception e) {
-                LOG.error("Failed to start master consumer for: " + endpoint, e);
+                LOG.warn("Failed to stop master consumer for: " + endpoint, e);
             }
-
-            LOG.info("Elected as master. Consumer started: {}", endpoint.getConsumerEndpoint());
-        }
+        };
     }
 
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/6b1ecf19/components/camel-zookeeper-master/src/main/java/org/apache/camel/component/zookeepermaster/ZookeeperGroupListenerSupport.java
----------------------------------------------------------------------
diff --git a/components/camel-zookeeper-master/src/main/java/org/apache/camel/component/zookeepermaster/ZookeeperGroupListenerSupport.java b/components/camel-zookeeper-master/src/main/java/org/apache/camel/component/zookeepermaster/ZookeeperGroupListenerSupport.java
new file mode 100644
index 0000000..f775089
--- /dev/null
+++ b/components/camel-zookeeper-master/src/main/java/org/apache/camel/component/zookeepermaster/ZookeeperGroupListenerSupport.java
@@ -0,0 +1,109 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.zookeepermaster;
+
+import org.apache.camel.Endpoint;
+import org.apache.camel.component.zookeepermaster.group.Group;
+import org.apache.camel.component.zookeepermaster.group.GroupListener;
+import org.apache.camel.util.IOHelper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class ZookeeperGroupListenerSupport extends ZookeeperGroupSupport implements GroupListener {
+
+    private static final transient Logger LOG = LoggerFactory.getLogger(ZookeeperGroupListenerSupport.class);
+    private Group<CamelNodeState> singleton;
+    private final String clusterPath;
+    private final Endpoint endpoint;
+    private final Runnable onLockAcquired;
+    private final Runnable onDisconnected;
+
+    public ZookeeperGroupListenerSupport(String clusterPath, Endpoint endpoint, Runnable onLockAcquired, Runnable onDisconnected) {
+        this.clusterPath = clusterPath;
+        this.endpoint = endpoint;
+        this.onLockAcquired = onLockAcquired;
+        this.onDisconnected = onDisconnected;
+    }
+
+    public void updateState(CamelNodeState state) {
+        singleton.update(state);
+    }
+
+    @Override
+    protected void doStart() throws Exception {
+        super.doStart();
+
+        this.singleton = createGroup(clusterPath);
+        this.singleton.add(this);
+        singleton.start();
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+        IOHelper.close(singleton);
+        super.doStop();
+    }
+
+    public String getClusterPath() {
+        return clusterPath;
+    }
+
+    public Group<CamelNodeState> getGroup() {
+        return singleton;
+    }
+
+    @Override
+    public void groupEvent(Group group, GroupEvent event) {
+        switch (event) {
+        case CONNECTED:
+            break;
+        case CHANGED:
+            if (singleton.isConnected()) {
+                if (singleton.isMaster()) {
+                    if (LOG.isDebugEnabled()) {
+                        LOG.debug("Master/Standby endpoint is Master for:  " + endpoint + " in " + endpoint.getCamelContext());
+                    }
+                    onLockOwned();
+                } else {
+                    if (LOG.isDebugEnabled()) {
+                        LOG.debug("Master/Standby endpoint is Standby for: " + endpoint + " in " + endpoint.getCamelContext());
+                    }
+                }
+            }
+            break;
+        case DISCONNECTED:
+            try {
+                LOG.info("Disconnecting as master. Stopping consumer: {}", endpoint);
+                onDisconnected();
+            } catch (Exception e) {
+                LOG.warn("Failed to stop master consumer for: " + endpoint + ". This exception is ignored.", e);
+            }
+            break;
+        default:
+            // noop
+        }
+    }
+
+    protected void onDisconnected() {
+        onDisconnected.run();
+    }
+
+    protected void onLockOwned() {
+        onLockAcquired.run();
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/6b1ecf19/components/camel-zookeeper-master/src/main/java/org/apache/camel/component/zookeepermaster/ZookeeperGroupSupport.java
----------------------------------------------------------------------
diff --git a/components/camel-zookeeper-master/src/main/java/org/apache/camel/component/zookeepermaster/ZookeeperGroupSupport.java b/components/camel-zookeeper-master/src/main/java/org/apache/camel/component/zookeepermaster/ZookeeperGroupSupport.java
index ddfe77f..f35f1d3 100644
--- a/components/camel-zookeeper-master/src/main/java/org/apache/camel/component/zookeepermaster/ZookeeperGroupSupport.java
+++ b/components/camel-zookeeper-master/src/main/java/org/apache/camel/component/zookeepermaster/ZookeeperGroupSupport.java
@@ -25,6 +25,7 @@ import org.apache.camel.component.zookeepermaster.group.internal.ManagedGroupFac
 import org.apache.camel.component.zookeepermaster.group.internal.ManagedGroupFactoryBuilder;
 import org.apache.camel.spi.Metadata;
 import org.apache.camel.support.ServiceSupport;
+import org.apache.camel.util.ObjectHelper;
 import org.apache.curator.framework.CuratorFramework;
 import org.apache.curator.framework.CuratorFrameworkFactory;
 import org.apache.curator.framework.state.ConnectionState;
@@ -122,6 +123,7 @@ public class ZookeeperGroupSupport extends ServiceSupport implements CamelContex
 
     @Override
     protected void doStart() throws Exception {
+        ObjectHelper.notNull(camelContext, "CamelContext");
 
         // attempt to lookup curator framework from registry using the name curator
         if (curator == null) {