You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2013/04/29 14:12:45 UTC

svn commit: r1476981 - in /sling/trunk/bundles/extensions/event/src: main/java/org/apache/sling/event/impl/jobs/TopologyCapabilities.java test/java/org/apache/sling/event/impl/jobs/InstanceDescriptionComparatorTest.java

Author: cziegeler
Date: Mon Apr 29 12:12:44 2013
New Revision: 1476981

URL: http://svn.apache.org/r1476981
Log:
SLING-2829 : Sort instances correctly if there is more than a single cluster view

Added:
    sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/impl/jobs/InstanceDescriptionComparatorTest.java   (with props)
Modified:
    sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/TopologyCapabilities.java

Modified: sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/TopologyCapabilities.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/TopologyCapabilities.java?rev=1476981&r1=1476980&r2=1476981&view=diff
==============================================================================
--- sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/TopologyCapabilities.java (original)
+++ sling/trunk/bundles/extensions/event/src/main/java/org/apache/sling/event/impl/jobs/TopologyCapabilities.java Mon Apr 29 12:12:44 2013
@@ -60,22 +60,40 @@ public class TopologyCapabilities {
     /** All instances. */
     private final Map<String, String> allInstances;
 
-    private static final class InstanceDescriptionComparator implements Comparator<InstanceDescription> {
+    /** Instance comparator. */
+    private final InstanceDescriptionComparator instanceComparator;
+
+    public static final class InstanceDescriptionComparator implements Comparator<InstanceDescription> {
+
+        private final String localClusterId;
+
+        public InstanceDescriptionComparator(final String clusterId) {
+            this.localClusterId = clusterId;
+        }
 
         @Override
         public int compare(final InstanceDescription o1, final InstanceDescription o2) {
             if ( o1.getSlingId().equals(o2.getSlingId()) ) {
                 return 0;
             }
-            if ( o1.isLeader() ) {
+            final boolean o1IsLocalCluster = localClusterId.equals(o1.getClusterView().getId());
+            final boolean o2IsLocalCluster = localClusterId.equals(o2.getClusterView().getId());
+            if ( o1IsLocalCluster && !o2IsLocalCluster ) {
                 return -1;
-            } else if ( o2.isLeader() ) {
+            }
+            if ( !o1IsLocalCluster && o2IsLocalCluster ) {
                 return 1;
             }
+            if ( o1IsLocalCluster ) {
+                if ( o1.isLeader() && !o2.isLeader() ) {
+                    return -1;
+                } else if ( o2.isLeader() && !o1.isLeader() ) {
+                    return 1;
+                }
+            }
             return o1.getSlingId().compareTo(o2.getSlingId());
         }
     }
-    private static final InstanceDescriptionComparator COMPARATOR = new InstanceDescriptionComparator();
 
     public static Map<String, String> getAllInstancesMap(final TopologyView view) {
         final Map<String, String> allInstances = new TreeMap<String, String>();
@@ -92,6 +110,7 @@ public class TopologyCapabilities {
     }
 
     public TopologyCapabilities(final TopologyView view, final long changeCount) {
+        this.instanceComparator = new InstanceDescriptionComparator(view.getLocalInstance().getClusterView().getId());
         this.changeCount = changeCount;
         this.isLeader = view.getLocalInstance().isLeader();
         this.allInstances = getAllInstancesMap(view);
@@ -107,7 +126,7 @@ public class TopologyCapabilities {
                         newCaps.put(topic, list);
                     }
                     list.add(desc);
-                    Collections.sort(list, COMPARATOR);
+                    Collections.sort(list, this.instanceComparator);
                 }
             }
         }
@@ -166,7 +185,7 @@ public class TopologyCapabilities {
         if ( bridgedTargets != null ) {
             potentialTargets.addAll(bridgedTargets);
         }
-        Collections.sort(potentialTargets, COMPARATOR);
+        Collections.sort(potentialTargets, this.instanceComparator);
 
         return potentialTargets;
     }
@@ -180,7 +199,7 @@ public class TopologyCapabilities {
 
         if ( potentialTargets != null && potentialTargets.size() > 0 ) {
             if ( queueInfo.queueConfiguration.getType() == QueueConfiguration.Type.ORDERED ) {
-                // for ordered queues we always pick the first as we have to pick the same target
+                // for ordered queues we always pick the first as we have to pick the same target on each cluster view
                 // on all instances (TODO - we could try to do some round robin of the whole queue)
                 return potentialTargets.get(0).getSlingId();
             }

Added: sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/impl/jobs/InstanceDescriptionComparatorTest.java
URL: http://svn.apache.org/viewvc/sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/impl/jobs/InstanceDescriptionComparatorTest.java?rev=1476981&view=auto
==============================================================================
--- sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/impl/jobs/InstanceDescriptionComparatorTest.java (added)
+++ sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/impl/jobs/InstanceDescriptionComparatorTest.java Mon Apr 29 12:12:44 2013
@@ -0,0 +1,202 @@
+/*
+ * 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.sling.event.impl.jobs;
+
+import static org.junit.Assert.assertEquals;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.sling.discovery.ClusterView;
+import org.apache.sling.discovery.InstanceDescription;
+
+public class InstanceDescriptionComparatorTest {
+
+
+    @org.junit.Test public void testSingleClusterThreeInstances() {
+        final Instance cl1in1 = new Instance("1", "A", false, true);
+        final Instance cl1in2 = new Instance("1", "B", true, false);
+        final Instance cl1in3 = new Instance("1", "C", false, false);
+
+        final List<InstanceDescription> desc = new ArrayList<InstanceDescription>();
+        desc.add(cl1in2);
+        desc.add(cl1in1);
+        desc.add(cl1in3);
+        Collections.sort(desc, new TopologyCapabilities.InstanceDescriptionComparator("1"));
+
+        assertEquals("Instance0: ", cl1in2.getSlingId(), desc.get(0).getSlingId());
+        assertEquals("Instance1: ", cl1in1.getSlingId(), desc.get(1).getSlingId());
+        assertEquals("Instance2: ", cl1in3.getSlingId(), desc.get(2).getSlingId());
+    }
+
+    @org.junit.Test public void testTwoClustersThreeInstances() {
+        final Instance cl1in1 = new Instance("1", "A", false, true);
+        final Instance cl1in2 = new Instance("1", "B", true, false);
+        final Instance cl1in3 = new Instance("1", "C", false, false);
+        final Instance cl2in1 = new Instance("2", "D", false, false);
+        final Instance cl2in2 = new Instance("2", "E", false, false);
+        final Instance cl2in3 = new Instance("2", "F", true, false);
+
+        final List<InstanceDescription> desc = new ArrayList<InstanceDescription>();
+        desc.add(cl2in3);
+        desc.add(cl1in2);
+        desc.add(cl2in1);
+        desc.add(cl1in3);
+        desc.add(cl2in2);
+        desc.add(cl1in1);
+        Collections.sort(desc, new TopologyCapabilities.InstanceDescriptionComparator("1"));
+
+        assertEquals("Instance0: ", cl1in2.getSlingId(), desc.get(0).getSlingId());
+        assertEquals("Instance1: ", cl1in1.getSlingId(), desc.get(1).getSlingId());
+        assertEquals("Instance2: ", cl1in3.getSlingId(), desc.get(2).getSlingId());
+        assertEquals("Instance3: ", cl2in1.getSlingId(), desc.get(3).getSlingId());
+        assertEquals("Instance4: ", cl2in2.getSlingId(), desc.get(4).getSlingId());
+        assertEquals("Instance5: ", cl2in3.getSlingId(), desc.get(5).getSlingId());
+
+        Collections.sort(desc, new TopologyCapabilities.InstanceDescriptionComparator("2"));
+        assertEquals("Instance0: ", cl2in3.getSlingId(), desc.get(0).getSlingId());
+        assertEquals("Instance1: ", cl2in1.getSlingId(), desc.get(1).getSlingId());
+        assertEquals("Instance2: ", cl2in2.getSlingId(), desc.get(2).getSlingId());
+        assertEquals("Instance3: ", cl1in1.getSlingId(), desc.get(3).getSlingId());
+        assertEquals("Instance4: ", cl1in2.getSlingId(), desc.get(4).getSlingId());
+        assertEquals("Instance5: ", cl1in3.getSlingId(), desc.get(5).getSlingId());
+    }
+
+    @org.junit.Test public void testThreeClustersThreeInstances() {
+        final Instance cl1in1 = new Instance("1", "A", false, true);
+        final Instance cl1in2 = new Instance("1", "B", true, false);
+        final Instance cl1in3 = new Instance("1", "C", false, false);
+        final Instance cl15in1 = new Instance("15", "Z", true, false);
+        final Instance cl2in1 = new Instance("2", "D", true, false);
+        final Instance cl2in2 = new Instance("2", "E", false, false);
+        final Instance cl2in3 = new Instance("2", "F", false, false);
+
+        final List<InstanceDescription> desc = new ArrayList<InstanceDescription>();
+        desc.add(cl2in3);
+        desc.add(cl1in2);
+        desc.add(cl2in1);
+        desc.add(cl15in1);
+        desc.add(cl1in3);
+        desc.add(cl2in2);
+        desc.add(cl1in1);
+        Collections.sort(desc, new TopologyCapabilities.InstanceDescriptionComparator("1"));
+
+        assertEquals("Instance0: ", cl1in2.getSlingId(), desc.get(0).getSlingId());
+        assertEquals("Instance1: ", cl1in1.getSlingId(), desc.get(1).getSlingId());
+        assertEquals("Instance2: ", cl1in3.getSlingId(), desc.get(2).getSlingId());
+        assertEquals("Instance3: ", cl2in1.getSlingId(), desc.get(3).getSlingId());
+        assertEquals("Instance4: ", cl2in2.getSlingId(), desc.get(4).getSlingId());
+        assertEquals("Instance5: ", cl2in3.getSlingId(), desc.get(5).getSlingId());
+        assertEquals("Instance6: ", cl15in1.getSlingId(), desc.get(6).getSlingId());
+
+        Collections.sort(desc, new TopologyCapabilities.InstanceDescriptionComparator("2"));
+        assertEquals("Instance0: ", cl2in1.getSlingId(), desc.get(0).getSlingId());
+        assertEquals("Instance1: ", cl2in2.getSlingId(), desc.get(1).getSlingId());
+        assertEquals("Instance2: ", cl2in3.getSlingId(), desc.get(2).getSlingId());
+        assertEquals("Instance3: ", cl1in1.getSlingId(), desc.get(3).getSlingId());
+        assertEquals("Instance4: ", cl1in2.getSlingId(), desc.get(4).getSlingId());
+        assertEquals("Instance5: ", cl1in3.getSlingId(), desc.get(5).getSlingId());
+        assertEquals("Instance6: ", cl15in1.getSlingId(), desc.get(6).getSlingId());
+
+        Collections.sort(desc, new TopologyCapabilities.InstanceDescriptionComparator("15"));
+        assertEquals("Instance0: ", cl15in1.getSlingId(), desc.get(0).getSlingId());
+        assertEquals("Instance1: ", cl1in1.getSlingId(), desc.get(1).getSlingId());
+        assertEquals("Instance2: ", cl1in2.getSlingId(), desc.get(2).getSlingId());
+        assertEquals("Instance3: ", cl1in3.getSlingId(), desc.get(3).getSlingId());
+        assertEquals("Instance4: ", cl2in1.getSlingId(), desc.get(4).getSlingId());
+        assertEquals("Instance5: ", cl2in2.getSlingId(), desc.get(5).getSlingId());
+        assertEquals("Instance6: ", cl2in3.getSlingId(), desc.get(6).getSlingId());
+
+        Collections.sort(desc, new TopologyCapabilities.InstanceDescriptionComparator("4"));
+        assertEquals("Instance0: ", cl1in1.getSlingId(), desc.get(0).getSlingId());
+        assertEquals("Instance1: ", cl1in2.getSlingId(), desc.get(1).getSlingId());
+        assertEquals("Instance2: ", cl1in3.getSlingId(), desc.get(2).getSlingId());
+        assertEquals("Instance3: ", cl2in1.getSlingId(), desc.get(3).getSlingId());
+        assertEquals("Instance4: ", cl2in2.getSlingId(), desc.get(4).getSlingId());
+        assertEquals("Instance5: ", cl2in3.getSlingId(), desc.get(5).getSlingId());
+        assertEquals("Instance6: ", cl15in1.getSlingId(), desc.get(6).getSlingId());
+    }
+
+    private static final class Instance implements InstanceDescription {
+
+        private final String clusterId;
+        private final String instanceId;
+        private final boolean isLeader;
+        private final boolean isLocal;
+
+        public Instance(final String clusterId, final String instanceId, final boolean isLeader, final boolean isLocal) {
+            this.clusterId = clusterId;
+            this.instanceId = instanceId;
+            this.isLeader = isLeader;
+            this.isLocal = isLocal;
+        }
+
+        @Override
+        public ClusterView getClusterView() {
+            return new ClusterView() {
+
+                @Override
+                public InstanceDescription getLeader() {
+                    // TODO Auto-generated method stub
+                    return null;
+                }
+
+                @Override
+                public List<InstanceDescription> getInstances() {
+                    // TODO Auto-generated method stub
+                    return null;
+                }
+
+                @Override
+                public String getId() {
+                    return clusterId;
+                }
+            };
+        }
+
+        @Override
+        public boolean isLeader() {
+            return this.isLeader;
+        }
+
+        @Override
+        public boolean isLocal() {
+            return this.isLocal;
+        }
+
+        @Override
+        public String getSlingId() {
+            return this.instanceId;
+        }
+
+        @Override
+        public String getProperty(String name) {
+            // TODO Auto-generated method stub
+            return null;
+        }
+
+        @Override
+        public Map<String, String> getProperties() {
+            // TODO Auto-generated method stub
+            return null;
+        }
+    }
+}

Propchange: sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/impl/jobs/InstanceDescriptionComparatorTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/impl/jobs/InstanceDescriptionComparatorTest.java
------------------------------------------------------------------------------
    svn:keywords = author date id revision rev url

Propchange: sling/trunk/bundles/extensions/event/src/test/java/org/apache/sling/event/impl/jobs/InstanceDescriptionComparatorTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain