You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by io...@apache.org on 2011/08/25 23:42:43 UTC
svn commit: r1161766 - in /karaf/cellar/trunk/shell/src/main:
java/org/apache/karaf/cellar/shell/group/ resources/OSGI-INF/blueprint/
Author: iocanel
Date: Thu Aug 25 21:42:43 2011
New Revision: 1161766
URL: http://svn.apache.org/viewvc?rev=1161766&view=rev
Log:
[KARAF-831] Added cluster:group-pick command. Also added output suppression support for all group commands. This is really useful for command composition.
Added:
karaf/cellar/trunk/shell/src/main/java/org/apache/karaf/cellar/shell/group/GroupPickCommand.java
Modified:
karaf/cellar/trunk/shell/src/main/java/org/apache/karaf/cellar/shell/group/GroupJoinCommand.java
karaf/cellar/trunk/shell/src/main/java/org/apache/karaf/cellar/shell/group/GroupListCommand.java
karaf/cellar/trunk/shell/src/main/java/org/apache/karaf/cellar/shell/group/GroupQuitCommand.java
karaf/cellar/trunk/shell/src/main/java/org/apache/karaf/cellar/shell/group/GroupSetCommand.java
karaf/cellar/trunk/shell/src/main/java/org/apache/karaf/cellar/shell/group/GroupSupport.java
karaf/cellar/trunk/shell/src/main/resources/OSGI-INF/blueprint/shell-cluster.xml
Modified: karaf/cellar/trunk/shell/src/main/java/org/apache/karaf/cellar/shell/group/GroupJoinCommand.java
URL: http://svn.apache.org/viewvc/karaf/cellar/trunk/shell/src/main/java/org/apache/karaf/cellar/shell/group/GroupJoinCommand.java?rev=1161766&r1=1161765&r2=1161766&view=diff
==============================================================================
--- karaf/cellar/trunk/shell/src/main/java/org/apache/karaf/cellar/shell/group/GroupJoinCommand.java (original)
+++ karaf/cellar/trunk/shell/src/main/java/org/apache/karaf/cellar/shell/group/GroupJoinCommand.java Thu Aug 25 21:42:43 2011
@@ -39,7 +39,7 @@ public class GroupJoinCommand extends Gr
*/
@Override
protected Object doExecute() throws Exception {
- return doExecute(ManageGroupAction.JOIN, group, nodes);
+ return doExecute(ManageGroupAction.JOIN, group, nodes,false);
}
}
Modified: karaf/cellar/trunk/shell/src/main/java/org/apache/karaf/cellar/shell/group/GroupListCommand.java
URL: http://svn.apache.org/viewvc/karaf/cellar/trunk/shell/src/main/java/org/apache/karaf/cellar/shell/group/GroupListCommand.java?rev=1161766&r1=1161765&r2=1161766&view=diff
==============================================================================
--- karaf/cellar/trunk/shell/src/main/java/org/apache/karaf/cellar/shell/group/GroupListCommand.java (original)
+++ karaf/cellar/trunk/shell/src/main/java/org/apache/karaf/cellar/shell/group/GroupListCommand.java Thu Aug 25 21:42:43 2011
@@ -36,7 +36,7 @@ public class GroupListCommand extends Gr
*/
@Override
protected Object doExecute() throws Exception {
- return doExecute(ManageGroupAction.LIST, null, nodes);
+ return doExecute(ManageGroupAction.LIST, null, nodes,false);
}
}
Added: karaf/cellar/trunk/shell/src/main/java/org/apache/karaf/cellar/shell/group/GroupPickCommand.java
URL: http://svn.apache.org/viewvc/karaf/cellar/trunk/shell/src/main/java/org/apache/karaf/cellar/shell/group/GroupPickCommand.java?rev=1161766&view=auto
==============================================================================
--- karaf/cellar/trunk/shell/src/main/java/org/apache/karaf/cellar/shell/group/GroupPickCommand.java (added)
+++ karaf/cellar/trunk/shell/src/main/java/org/apache/karaf/cellar/shell/group/GroupPickCommand.java Thu Aug 25 21:42:43 2011
@@ -0,0 +1,78 @@
+/*
+ * 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.group;
+
+import org.apache.felix.gogo.commands.Argument;
+import org.apache.felix.gogo.commands.Command;
+import org.apache.karaf.cellar.core.Group;
+import org.apache.karaf.cellar.core.Node;
+import org.apache.karaf.cellar.core.control.ManageGroupAction;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Set;
+
+/**
+ * @author: iocanel
+ */
+@Command(scope = "cluster", name = "group-pick", description = "Picks a number of nodes from one group and moves them into an other")
+public class GroupPickCommand extends GroupSupport {
+
+ @Argument(index = 0, name = "sourceGroupName", description = "The name of the source group that will act as a selection pool", required = true, multiValued = false)
+ String sourceGroupName;
+
+ @Argument(index = 1, name = "targetGroupName", description = "The name of the the destination group", required = true, multiValued = false)
+ String targetGroupName;
+
+ @Argument(index = 2, name = "count", description = "The number of nodes to transfer", required = false, multiValued = false)
+ int count=1;
+
+ @Override
+ protected Object doExecute() throws Exception {
+ Group sourceGroup = groupManager.findGroupByName(sourceGroupName);
+ if(sourceGroup != null) {
+ List<String> eligibleMembers = new LinkedList<String>();
+ Set<Node> groupMembers = sourceGroup.getMembers();
+
+ for(Node node:groupMembers) {
+ Set<Group> nodeGroups = groupManager.listGroups(node);
+ //If the node only belongs to the source group then it is eligible.
+ if(nodeGroups != null && nodeGroups.size() == 1) {
+ eligibleMembers.add(node.getId());
+ }
+ }
+
+ if(eligibleMembers.size() == 0) {
+ System.out.println("Could not find eligible members from transfer in group:"+sourceGroupName);
+ } else if(eligibleMembers.size() < count) {
+ System.out.println("There are fewer("+eligibleMembers.size()+") eligible members for transfer in group:"+sourceGroupName);
+ }
+
+ //TODO: The loop should not be necessary since the method already accepts a list. However this is breaks for some reason.
+ for(String eligible:eligibleMembers) {
+ List<String> recipient = new LinkedList<String>();
+ recipient.add(eligible);
+ doExecute(ManageGroupAction.SET, targetGroupName, recipient);
+ }
+
+ doExecute(ManageGroupAction.LIST, null, new ArrayList(),false);
+
+ } else System.err.println("Cannot find source group with name:"+sourceGroupName);
+ return null;
+ }
+}
Modified: karaf/cellar/trunk/shell/src/main/java/org/apache/karaf/cellar/shell/group/GroupQuitCommand.java
URL: http://svn.apache.org/viewvc/karaf/cellar/trunk/shell/src/main/java/org/apache/karaf/cellar/shell/group/GroupQuitCommand.java?rev=1161766&r1=1161765&r2=1161766&view=diff
==============================================================================
--- karaf/cellar/trunk/shell/src/main/java/org/apache/karaf/cellar/shell/group/GroupQuitCommand.java (original)
+++ karaf/cellar/trunk/shell/src/main/java/org/apache/karaf/cellar/shell/group/GroupQuitCommand.java Thu Aug 25 21:42:43 2011
@@ -39,7 +39,7 @@ public class GroupQuitCommand extends Gr
*/
@Override
protected Object doExecute() throws Exception {
- return doExecute(ManageGroupAction.QUIT, group, nodes);
+ return doExecute(ManageGroupAction.QUIT, group, nodes,false);
}
}
Modified: karaf/cellar/trunk/shell/src/main/java/org/apache/karaf/cellar/shell/group/GroupSetCommand.java
URL: http://svn.apache.org/viewvc/karaf/cellar/trunk/shell/src/main/java/org/apache/karaf/cellar/shell/group/GroupSetCommand.java?rev=1161766&r1=1161765&r2=1161766&view=diff
==============================================================================
--- karaf/cellar/trunk/shell/src/main/java/org/apache/karaf/cellar/shell/group/GroupSetCommand.java (original)
+++ karaf/cellar/trunk/shell/src/main/java/org/apache/karaf/cellar/shell/group/GroupSetCommand.java Thu Aug 25 21:42:43 2011
@@ -39,7 +39,7 @@ public class GroupSetCommand extends Gro
*/
@Override
protected Object doExecute() throws Exception {
- return doExecute(ManageGroupAction.SET, group, nodes);
+ return doExecute(ManageGroupAction.SET, group, nodes,false);
}
}
Modified: karaf/cellar/trunk/shell/src/main/java/org/apache/karaf/cellar/shell/group/GroupSupport.java
URL: http://svn.apache.org/viewvc/karaf/cellar/trunk/shell/src/main/java/org/apache/karaf/cellar/shell/group/GroupSupport.java?rev=1161766&r1=1161765&r2=1161766&view=diff
==============================================================================
--- karaf/cellar/trunk/shell/src/main/java/org/apache/karaf/cellar/shell/group/GroupSupport.java (original)
+++ karaf/cellar/trunk/shell/src/main/java/org/apache/karaf/cellar/shell/group/GroupSupport.java Thu Aug 25 21:42:43 2011
@@ -34,11 +34,26 @@ public abstract class GroupSupport exten
/**
* Executes the command.
- *
+ * @param action
+ * @param group
+ * @param nodes
* @return
* @throws Exception
*/
protected Object doExecute(ManageGroupAction action, String group, Collection<String> nodes) throws Exception {
+ return doExecute(action,group,nodes,true);
+ }
+
+ /**
+ * Executes the command.
+ * @param action
+ * @param group
+ * @param nodes
+ * @param supressOutput
+ * @return
+ * @throws Exception
+ */
+ protected Object doExecute(ManageGroupAction action, String group, Collection<String> nodes,Boolean supressOutput) throws Exception {
ManageGroupCommand command = new ManageGroupCommand(clusterManager.generateId());
Set<Node> recipientList = clusterManager.listNodes(nodes);
@@ -58,6 +73,7 @@ public abstract class GroupSupport exten
}
Map<Node, ManageGroupResult> results = executionContext.execute(command);
+ if(!supressOutput) {
if (results == null || results.isEmpty()) {
System.out.println("No result received within given timeout");
} else {
@@ -79,6 +95,7 @@ public abstract class GroupSupport exten
}
}
}
+ }
return null;
}
Modified: karaf/cellar/trunk/shell/src/main/resources/OSGI-INF/blueprint/shell-cluster.xml
URL: http://svn.apache.org/viewvc/karaf/cellar/trunk/shell/src/main/resources/OSGI-INF/blueprint/shell-cluster.xml?rev=1161766&r1=1161765&r2=1161766&view=diff
==============================================================================
--- karaf/cellar/trunk/shell/src/main/resources/OSGI-INF/blueprint/shell-cluster.xml (original)
+++ karaf/cellar/trunk/shell/src/main/resources/OSGI-INF/blueprint/shell-cluster.xml Thu Aug 25 21:42:43 2011
@@ -153,6 +153,17 @@
</completers>
</command>
+ <command name="cluster/group-pick">
+ <action class="org.apache.karaf.cellar.shell.group.GroupPickCommand">
+ <property name="clusterManager" ref="clusterManager"/>
+ <property name="groupManager" ref="groupManager"/>
+ <property name="executionContext" ref="executionContext"/>
+ </action>
+ <completers>
+ <ref component-id="allGroupCompleter"/>
+ </completers>
+ </command>
+
<command name="cluster/group-list">
<action class="org.apache.karaf.cellar.shell.group.GroupListCommand">
<property name="clusterManager" ref="clusterManager"/>
@@ -183,9 +194,9 @@
</command-bundle>
<!-- Reference to the Cluster Manager -->
- <reference id="clusterManager" interface="org.apache.karaf.cellar.core.ClusterManager"/>
- <reference id="groupManager" interface="org.apache.karaf.cellar.core.GroupManager"/>
- <reference id="executionContext" interface="org.apache.karaf.cellar.core.command.ExecutionContext"/>
+ <reference id="clusterManager" interface="org.apache.karaf.cellar.core.ClusterManager" availability="optional"/>
+ <reference id="groupManager" interface="org.apache.karaf.cellar.core.GroupManager" availability="optional"/>
+ <reference id="executionContext" interface="org.apache.karaf.cellar.core.command.ExecutionContext" availability="optional"/>
<!-- Completers -->
<bean id="allNodesCompleter" class="org.apache.karaf.cellar.core.completer.AllNodeCompleter">