Repository: karaf-cellar Updated Branches: refs/heads/master c90fe7165 -> a32c77a74
[KARAF-4940] Add cluster:shutdown command (and corresponding MBean operation) Project: http://git-wip-us.apache.org/repos/asf/karaf-cellar/repo Commit: http://git-wip-us.apache.org/repos/asf/karaf-cellar/commit/a32c77a7 Tree: http://git-wip-us.apache.org/repos/asf/karaf-cellar/tree/a32c77a7 Diff: http://git-wip-us.apache.org/repos/asf/karaf-cellar/diff/a32c77a7 Branch: refs/heads/master Commit: a32c77a74ff2f094f618259b2d5f48cff3ceaf04 Parents: c90fe71 Author: Jean-Baptiste Onofré <[email protected]> Authored: Wed May 10 19:16:26 2017 +0200 Committer: Jean-Baptiste Onofré <[email protected]> Committed: Wed May 10 19:16:26 2017 +0200 ---------------------------------------------------------------------- core/pom.xml | 4 ++ .../cellar/core/control/ShutdownCommand.java | 42 +++++++++++++ .../core/control/ShutdownCommandHandler.java | 62 ++++++++++++++++++++ .../cellar/core/control/ShutdownResult.java | 27 +++++++++ .../core/control/ShutdownResultHandler.java | 25 ++++++++ .../cellar/core/management/CellarMBean.java | 11 ++++ .../hazelcast/internal/osgi/Activator.java | 13 ++++ .../management/internal/CellarMBeanImpl.java | 16 +++++ .../karaf/cellar/shell/ShutdownCommand.java | 41 +++++++++++++ 9 files changed, 241 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/karaf-cellar/blob/a32c77a7/core/pom.xml ---------------------------------------------------------------------- diff --git a/core/pom.xml b/core/pom.xml index c491c91..08f06a6 100644 --- a/core/pom.xml +++ b/core/pom.xml @@ -52,6 +52,10 @@ <artifactId>org.apache.karaf.shell.core</artifactId> </dependency> <dependency> + <groupId>org.apache.karaf.features</groupId> + <artifactId>org.apache.karaf.features.core</artifactId> + </dependency> + <dependency> <groupId>org.apache.karaf</groupId> <artifactId>org.apache.karaf.util</artifactId> </dependency> http://git-wip-us.apache.org/repos/asf/karaf-cellar/blob/a32c77a7/core/src/main/java/org/apache/karaf/cellar/core/control/ShutdownCommand.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/karaf/cellar/core/control/ShutdownCommand.java b/core/src/main/java/org/apache/karaf/cellar/core/control/ShutdownCommand.java new file mode 100644 index 0000000..73dce5b --- /dev/null +++ b/core/src/main/java/org/apache/karaf/cellar/core/control/ShutdownCommand.java @@ -0,0 +1,42 @@ +/* + * Licensed 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.karaf.cellar.core.control; + +import org.apache.karaf.cellar.core.command.Command; + +/** + * Shutdown the whole cluster. + */ +public class ShutdownCommand extends Command<ShutdownResult> { + + private boolean halt; + + public ShutdownCommand(String id) { + super(id); + } + + public ShutdownCommand(String id, boolean halt) { + super(id); + this.halt = halt; + } + + public boolean isHalt() { + return halt; + } + + public void setHalt(boolean halt) { + this.halt = halt; + } + +} http://git-wip-us.apache.org/repos/asf/karaf-cellar/blob/a32c77a7/core/src/main/java/org/apache/karaf/cellar/core/control/ShutdownCommandHandler.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/karaf/cellar/core/control/ShutdownCommandHandler.java b/core/src/main/java/org/apache/karaf/cellar/core/control/ShutdownCommandHandler.java new file mode 100644 index 0000000..00904cb --- /dev/null +++ b/core/src/main/java/org/apache/karaf/cellar/core/control/ShutdownCommandHandler.java @@ -0,0 +1,62 @@ +/* + * Licensed 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.karaf.cellar.core.control; + +import org.apache.karaf.cellar.core.command.CommandHandler; +import org.apache.karaf.features.FeaturesService; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceReference; + +/** + * Cluster shutdown command handler. + */ +public class ShutdownCommandHandler extends CommandHandler<ShutdownCommand, ShutdownResult> { + + public static final String SWITCH_ID = "org.apache.karaf.cellar.command.shutdown.switch"; + private final Switch commandSwitch = new BasicSwitch(SWITCH_ID); + + private BundleContext bundleContext; + + @Override + public ShutdownResult execute(ShutdownCommand command) { + try { + if (command.isHalt()) { + bundleContext.getBundle(0).stop(); + } else { + ServiceReference<FeaturesService> ref = bundleContext.getServiceReference(FeaturesService.class); + FeaturesService featuresService = bundleContext.getService(ref); + featuresService.uninstallFeature("cellar"); + bundleContext.ungetService(ref); + } + } catch (Exception e) { + // nothing to do + } + return new ShutdownResult(command.getId()); + } + + public void setBundleContext(BundleContext bundleContext) { + this.bundleContext = bundleContext; + } + + @Override + public Class<ShutdownCommand> getType() { + return ShutdownCommand.class; + } + + @Override + public Switch getSwitch() { + return commandSwitch; + } + +} http://git-wip-us.apache.org/repos/asf/karaf-cellar/blob/a32c77a7/core/src/main/java/org/apache/karaf/cellar/core/control/ShutdownResult.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/karaf/cellar/core/control/ShutdownResult.java b/core/src/main/java/org/apache/karaf/cellar/core/control/ShutdownResult.java new file mode 100644 index 0000000..e980c4b --- /dev/null +++ b/core/src/main/java/org/apache/karaf/cellar/core/control/ShutdownResult.java @@ -0,0 +1,27 @@ +/* + * Licensed 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.karaf.cellar.core.control; + +import org.apache.karaf.cellar.core.command.Result; + +/** + * Shutdown result. + */ +public class ShutdownResult extends Result { + + public ShutdownResult(String id) { + super(id); + } + +} http://git-wip-us.apache.org/repos/asf/karaf-cellar/blob/a32c77a7/core/src/main/java/org/apache/karaf/cellar/core/control/ShutdownResultHandler.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/karaf/cellar/core/control/ShutdownResultHandler.java b/core/src/main/java/org/apache/karaf/cellar/core/control/ShutdownResultHandler.java new file mode 100644 index 0000000..f75f2e5 --- /dev/null +++ b/core/src/main/java/org/apache/karaf/cellar/core/control/ShutdownResultHandler.java @@ -0,0 +1,25 @@ +/* + * Licensed 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.karaf.cellar.core.control; + +import org.apache.karaf.cellar.core.command.ResultHandler; + +public class ShutdownResultHandler extends ResultHandler<ShutdownResult> { + + @Override + public Class<ShutdownResult> getType() { + return ShutdownResult.class; + } + +} http://git-wip-us.apache.org/repos/asf/karaf-cellar/blob/a32c77a7/core/src/main/java/org/apache/karaf/cellar/core/management/CellarMBean.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/karaf/cellar/core/management/CellarMBean.java b/core/src/main/java/org/apache/karaf/cellar/core/management/CellarMBean.java index 8d180b3..3336a87 100644 --- a/core/src/main/java/org/apache/karaf/cellar/core/management/CellarMBean.java +++ b/core/src/main/java/org/apache/karaf/cellar/core/management/CellarMBean.java @@ -21,6 +21,17 @@ import javax.management.openmbean.TabularData; public interface CellarMBean { /** + * Shutdown the cluster on all nodes, removing the cellar feature. + */ + void shutdown() throws Exception; + + /** + * Shutdown the cluster on all nodes, optionally stopping the target instances. + * @param poweroff true to poweroff the nodes, false else. + */ + void shutdown(boolean poweroff) throws Exception; + + /** * Force the sync of the different nodes in the cluster. * * @throws Exception in case of sync failure. http://git-wip-us.apache.org/repos/asf/karaf-cellar/blob/a32c77a7/hazelcast/src/main/java/org/apache/karaf/cellar/hazelcast/internal/osgi/Activator.java ---------------------------------------------------------------------- diff --git a/hazelcast/src/main/java/org/apache/karaf/cellar/hazelcast/internal/osgi/Activator.java b/hazelcast/src/main/java/org/apache/karaf/cellar/hazelcast/internal/osgi/Activator.java index 2ea943d..3de5799 100644 --- a/hazelcast/src/main/java/org/apache/karaf/cellar/hazelcast/internal/osgi/Activator.java +++ b/hazelcast/src/main/java/org/apache/karaf/cellar/hazelcast/internal/osgi/Activator.java @@ -279,6 +279,19 @@ public class Activator extends BaseActivator implements ManagedService { manageGroupResultHandler.setCommandStore(commandStore); register(EventHandler.class, manageGroupResultHandler); + LOGGER.debug("CELLAR HAZELCAST: register shutdown command handler"); + ShutdownCommandHandler shutdownCommandHandler = new ShutdownCommandHandler(); + shutdownCommandHandler.setBundleContext(bundleContext); + shutdownCommandHandler.setProducer(producer); + shutdownCommandHandler.setClusterManager(clusterManager); + shutdownCommandHandler.setGroupManager(groupManager); + register(EventHandler.class, shutdownCommandHandler); + + LOGGER.debug("CELLAR HAZELCAST: register shutdown command result handler"); + ShutdownResultHandler shutdownResultHandler = new ShutdownResultHandler(); + shutdownResultHandler.setCommandStore(commandStore); + register(EventHandler.class, shutdownCommandHandler); + LOGGER.debug("CELLAR HAZELCAST: start the synchronizer service tracker"); synchronizerServiceTracker = new ServiceTracker<Synchronizer, Synchronizer>(bundleContext, Synchronizer.class, new ServiceTrackerCustomizer<Synchronizer, Synchronizer>() { @Override http://git-wip-us.apache.org/repos/asf/karaf-cellar/blob/a32c77a7/hazelcast/src/main/java/org/apache/karaf/cellar/hazelcast/management/internal/CellarMBeanImpl.java ---------------------------------------------------------------------- diff --git a/hazelcast/src/main/java/org/apache/karaf/cellar/hazelcast/management/internal/CellarMBeanImpl.java b/hazelcast/src/main/java/org/apache/karaf/cellar/hazelcast/management/internal/CellarMBeanImpl.java index 33a5c30..73f06ac 100644 --- a/hazelcast/src/main/java/org/apache/karaf/cellar/hazelcast/management/internal/CellarMBeanImpl.java +++ b/hazelcast/src/main/java/org/apache/karaf/cellar/hazelcast/management/internal/CellarMBeanImpl.java @@ -321,4 +321,20 @@ public class CellarMBeanImpl extends StandardMBean implements CellarMBean { executionContext.execute(command); } + @Override + public void shutdown() throws Exception { + shutdown(false); + } + + @Override + public void shutdown(boolean poweroff) throws Exception { + ShutdownCommand command = new ShutdownCommand(clusterManager.generateId()); + + Set<Node> nodes = clusterManager.listNodes(); + + command.setDestination(nodes); + command.setHalt(poweroff); + executionContext.execute(command); + } + } http://git-wip-us.apache.org/repos/asf/karaf-cellar/blob/a32c77a7/shell/src/main/java/org/apache/karaf/cellar/shell/ShutdownCommand.java ---------------------------------------------------------------------- diff --git a/shell/src/main/java/org/apache/karaf/cellar/shell/ShutdownCommand.java b/shell/src/main/java/org/apache/karaf/cellar/shell/ShutdownCommand.java new file mode 100644 index 0000000..2f91dfc --- /dev/null +++ b/shell/src/main/java/org/apache/karaf/cellar/shell/ShutdownCommand.java @@ -0,0 +1,41 @@ +/* + * Licensed 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.karaf.cellar.shell; + +import org.apache.karaf.cellar.core.Node; +import org.apache.karaf.shell.api.action.Command; +import org.apache.karaf.shell.api.action.Option; +import org.apache.karaf.shell.api.action.lifecycle.Service; + +import java.util.Set; + +@Command(scope = "cluster", name = "shutdown", description = "Shutdown the cluster removing the Cellar feature on all node") +@Service +public class ShutdownCommand extends ClusterCommandSupport { + + @Option(name = "-p", aliases = {"--poweroff"}, description = "Completely stop the nodes", required = false, multiValued = false) + boolean halt; + + @Override + protected Object doExecute() throws Exception { + org.apache.karaf.cellar.core.control.ShutdownCommand command = new org.apache.karaf.cellar.core.control.ShutdownCommand(clusterManager.generateId()); + Set<Node> recipientList = clusterManager.listNodes(); + command.setDestination(recipientList); + command.setHalt(halt); + System.out.println("!! Cluster shutdown !!"); + executionContext.execute(command); + return null; + } + +}
