brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From henev...@apache.org
Subject [3/5] brooklyn-server git commit: allow simple constant quora to be set as a constant (no need for complex range)
Date Mon, 24 Jul 2017 16:34:51 GMT
allow simple constant quora to be set as a constant (no need for complex range)


Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/4d824127
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/4d824127
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/4d824127

Branch: refs/heads/master
Commit: 4d824127a31b56a411c29a7c4f941471ea436eed
Parents: d85ffa5
Author: Alex Heneveld <alex.heneveld@cloudsoftcorp.com>
Authored: Mon Jul 24 16:49:58 2017 +0100
Committer: Alex Heneveld <alex.heneveld@cloudsoftcorp.com>
Committed: Mon Jul 24 17:08:24 2017 +0100

----------------------------------------------------------------------
 .../brooklyn/util/collections/QuorumCheck.java  | 21 ++++++++++++++++++--
 .../coerce/CommonAdaptorTypeCoercions.java      | 12 +++++++++++
 .../util/collections/QuorumChecksTest.java      | 10 +++++++---
 3 files changed, 38 insertions(+), 5 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/4d824127/utils/common/src/main/java/org/apache/brooklyn/util/collections/QuorumCheck.java
----------------------------------------------------------------------
diff --git a/utils/common/src/main/java/org/apache/brooklyn/util/collections/QuorumCheck.java
b/utils/common/src/main/java/org/apache/brooklyn/util/collections/QuorumCheck.java
index bc96763..c150f47 100644
--- a/utils/common/src/main/java/org/apache/brooklyn/util/collections/QuorumCheck.java
+++ b/utils/common/src/main/java/org/apache/brooklyn/util/collections/QuorumCheck.java
@@ -19,6 +19,7 @@
 package org.apache.brooklyn.util.collections;
 
 import java.io.Serializable;
+import java.util.Collection;
 import java.util.Iterator;
 import java.util.List;
 
@@ -112,7 +113,9 @@ public interface QuorumCheck {
          */
         @SuppressWarnings({ "rawtypes", "unchecked" })
         public static QuorumCheck newLinearRange(String range, String name) {
-            return LinearRangeQuorumCheck.of(name, (Iterable)Iterables.getOnlyElement( Yamls.parseAll(range)
));
+            Object input = Iterables.getOnlyElement( Yamls.parseAll(range) );
+            if (input instanceof Iterable) return LinearRangeQuorumCheck.of(name, (Iterable)input);
+            throw new IllegalArgumentException("Invalid input to linear range quorum check;
should be a list of points (not '"+range+"')");
         }
         
         private static final List<QuorumCheck> NAMED_CHECKS = MutableList
@@ -126,7 +129,21 @@ public interface QuorumCheck {
                         return qc;
                 }
             }
-            return newLinearRange(nameOrRange);
+            // parse YAML
+            Object input = Iterables.getOnlyElement( Yamls.parseAll(nameOrRange) );
+            if (input instanceof Collection) return of((Collection<?>)input);
+            if (input instanceof Integer || input instanceof Long) return of((int)((Number)input));
+            // TODO also accept "50%", and "50%,1"
+            throw new IllegalArgumentException("Unknown quorum check format '"+input+"'");
+        }
+        
+        @SuppressWarnings({ "unchecked", "rawtypes" })
+        public static QuorumCheck of(Collection<?> pointsForLinearRange) {
+            return LinearRangeQuorumCheck.of(null, (Iterable)pointsForLinearRange);
+        }
+        
+        public static QuorumCheck of(Integer constant) {
+            return newInstance(constant, 0.0, constant>0);
         }
     }
     

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/4d824127/utils/common/src/main/java/org/apache/brooklyn/util/javalang/coerce/CommonAdaptorTypeCoercions.java
----------------------------------------------------------------------
diff --git a/utils/common/src/main/java/org/apache/brooklyn/util/javalang/coerce/CommonAdaptorTypeCoercions.java
b/utils/common/src/main/java/org/apache/brooklyn/util/javalang/coerce/CommonAdaptorTypeCoercions.java
index ae4c8b3..8d6e924 100644
--- a/utils/common/src/main/java/org/apache/brooklyn/util/javalang/coerce/CommonAdaptorTypeCoercions.java
+++ b/utils/common/src/main/java/org/apache/brooklyn/util/javalang/coerce/CommonAdaptorTypeCoercions.java
@@ -257,6 +257,18 @@ public class CommonAdaptorTypeCoercions {
                 return QuorumChecks.of(input);
             }
         });
+        registerAdapter(Integer.class, QuorumCheck.class, new Function<Integer,QuorumCheck>()
{
+            @Override
+            public QuorumCheck apply(final Integer input) {
+                return QuorumChecks.of(input);
+            }
+        });
+        registerAdapter(Collection.class, QuorumCheck.class, new Function<Collection,QuorumCheck>()
{
+            @Override
+            public QuorumCheck apply(final Collection input) {
+                return QuorumChecks.of(input);
+            }
+        });
         registerAdapter(String.class, TimeZone.class, new Function<String,TimeZone>()
{
             @Override
             public TimeZone apply(final String input) {

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/4d824127/utils/common/src/test/java/org/apache/brooklyn/util/collections/QuorumChecksTest.java
----------------------------------------------------------------------
diff --git a/utils/common/src/test/java/org/apache/brooklyn/util/collections/QuorumChecksTest.java
b/utils/common/src/test/java/org/apache/brooklyn/util/collections/QuorumChecksTest.java
index 4210921..0d996e8 100644
--- a/utils/common/src/test/java/org/apache/brooklyn/util/collections/QuorumChecksTest.java
+++ b/utils/common/src/test/java/org/apache/brooklyn/util/collections/QuorumChecksTest.java
@@ -99,7 +99,11 @@ public class QuorumChecksTest {
         Assert.assertTrue(q.isQuorate(31, 300));
     }
     
-    
-    
-    
+    @Test
+    public void testConstantQuorum() {
+        QuorumCheck q = QuorumChecks.of("2");
+        Assert.assertTrue(q.isQuorate(2, 2));
+        Assert.assertTrue(q.isQuorate(2, 10));
+        Assert.assertFalse(q.isQuorate(1, 1));
+    }     
 }


Mime
View raw message