curator-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From randg...@apache.org
Subject [21/32] git commit: wip
Date Mon, 22 Jul 2013 23:26:56 GMT
wip


Project: http://git-wip-us.apache.org/repos/asf/incubator-curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-curator/commit/0f00e956
Tree: http://git-wip-us.apache.org/repos/asf/incubator-curator/tree/0f00e956
Diff: http://git-wip-us.apache.org/repos/asf/incubator-curator/diff/0f00e956

Branch: refs/heads/CURATOR-14
Commit: 0f00e9565b526d483e358abbf73c3fa06612ac09
Parents: d8a6415
Author: randgalt <randgalt@apache.org>
Authored: Sun Jun 23 17:38:08 2013 -0500
Committer: randgalt <randgalt@apache.org>
Committed: Sun Jun 23 17:38:08 2013 -0500

----------------------------------------------------------------------
 .../x/discovery/DownInstanceManager.java        | 39 +++++++++-
 .../x/discovery/ServiceProviderBuilder.java     |  9 ++-
 .../x/discovery/TestDownInstanceManager.java    | 81 ++++++++++++++++++++
 3 files changed, 125 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-curator/blob/0f00e956/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/DownInstanceManager.java
----------------------------------------------------------------------
diff --git a/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/DownInstanceManager.java
b/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/DownInstanceManager.java
index 3e2d164..e14b9de 100644
--- a/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/DownInstanceManager.java
+++ b/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/DownInstanceManager.java
@@ -22,6 +22,12 @@ import java.util.concurrent.DelayQueue;
 import java.util.concurrent.Delayed;
 import java.util.concurrent.TimeUnit;
 
+/**
+ * Container for marking instance as temporarily down. Instances
+ * added to this container will not be selected by {@link ServiceProvider}. NOTE:
+ * you must add an instance of this class via {@link ServiceProviderBuilder#downInstanceManager(DownInstanceManager)}
+ * to get this behavior.
+ */
 public class DownInstanceManager
 {
     private final long timeoutMs;
@@ -41,7 +47,7 @@ public class DownInstanceManager
         {
             long elapsedMs = System.currentTimeMillis() - startMs;
             long remainingMs = timeoutMs - elapsedMs;
-            return (remainingMs > 0) ? TimeUnit.MILLISECONDS.convert(remainingMs, unit)
: 0;
+            return (remainingMs > 0) ? unit.convert(remainingMs, TimeUnit.MILLISECONDS)
: 0;
         }
 
         @Override
@@ -82,25 +88,52 @@ public class DownInstanceManager
         }
     }
 
+    /**
+     * @param timeout amount of time for instances to be unavailable
+     * @param unit time unit
+     */
     public DownInstanceManager(long timeout, TimeUnit unit)
     {
         timeoutMs = unit.toMillis(timeout);
     }
 
+    /**
+     * This instance will be unavailable for the configured timeout
+     *
+     * @param instance instance to mark unavailable
+     */
     public void add(ServiceInstance<?> instance)
     {
+        purge();
         queue.add(new Entry(instance));
     }
 
+    /**
+     * Return true of the given instance is currently unavailable
+     *
+     * @param instance instance to check
+     * @return true/false
+     */
     public boolean contains(ServiceInstance<?> instance)
     {
-        //noinspection StatementWithEmptyBody
-        while ( queue.poll() != null ){}    // pull out expired entries
+        purge();
         return queue.contains(new Entry(instance));
     }
 
+    /**
+     * Return true if there are instances currently unavailable
+     *
+     * @return true/false
+     */
     public boolean hasEntries()
     {
+        purge();
         return !queue.isEmpty();
     }
+
+    private void purge()
+    {
+        //noinspection StatementWithEmptyBody
+        while ( queue.poll() != null ){}    // pull out expired entries
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-curator/blob/0f00e956/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/ServiceProviderBuilder.java
----------------------------------------------------------------------
diff --git a/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/ServiceProviderBuilder.java
b/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/ServiceProviderBuilder.java
index 1c8a880..58ffc1b 100644
--- a/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/ServiceProviderBuilder.java
+++ b/curator-x-discovery/src/main/java/org/apache/curator/x/discovery/ServiceProviderBuilder.java
@@ -55,7 +55,7 @@ public interface ServiceProviderBuilder<T>
     public ServiceProviderBuilder<T> threadFactory(ThreadFactory threadFactory);
 
     /**
-     * optional To avoid herding in noisy scenarios, the cache should be padded to only update
1 per period.
+     * optional - To avoid herding in noisy scenarios, the cache should be padded to only
update 1 per period.
      * The refresh padding is that period in milliseconds. Set to 0 to turn off padding.
The default
      * is 1 second.
      *
@@ -64,5 +64,12 @@ public interface ServiceProviderBuilder<T>
      */
     public ServiceProviderBuilder<T> refreshPaddingMs(int refreshPaddingMs);
 
+    /**
+     * optional - add a down instance manager to this provider. The provider will ignore
instances
+     * that are currently unavailable as specified by the manager
+     *
+     * @param downInstanceManager manager
+     * @return this
+     */
     public ServiceProviderBuilder<T> downInstanceManager(DownInstanceManager downInstanceManager);
 }

http://git-wip-us.apache.org/repos/asf/incubator-curator/blob/0f00e956/curator-x-discovery/src/test/java/org/apache/curator/x/discovery/TestDownInstanceManager.java
----------------------------------------------------------------------
diff --git a/curator-x-discovery/src/test/java/org/apache/curator/x/discovery/TestDownInstanceManager.java
b/curator-x-discovery/src/test/java/org/apache/curator/x/discovery/TestDownInstanceManager.java
new file mode 100644
index 0000000..4633b9d
--- /dev/null
+++ b/curator-x-discovery/src/test/java/org/apache/curator/x/discovery/TestDownInstanceManager.java
@@ -0,0 +1,81 @@
+/**
+ * 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.curator.x.discovery;
+
+import org.apache.curator.x.discovery.details.ServiceDiscoveryImpl;
+import org.apache.curator.x.discovery.details.ServiceProviderImpl;
+import org.apache.curator.x.discovery.strategies.RandomStrategy;
+import org.testng.Assert;
+import org.testng.annotations.Test;
+import java.util.concurrent.TimeUnit;
+
+public class TestDownInstanceManager
+{
+    @Test
+    public void testBasic() throws Exception
+    {
+        ServiceInstance<Void> instance1 = ServiceInstance.<Void>builder().name("hey").id("1").build();
+        ServiceInstance<Void> instance2 = ServiceInstance.<Void>builder().name("hey").id("2").build();
+
+        DownInstanceManager downInstanceManager = new DownInstanceManager(10, TimeUnit.DAYS);
+        Assert.assertFalse(downInstanceManager.hasEntries());
+        Assert.assertFalse(downInstanceManager.contains(instance1));
+        Assert.assertFalse(downInstanceManager.contains(instance2));
+
+        downInstanceManager.add(instance1);
+        Assert.assertTrue(downInstanceManager.contains(instance1));
+        Assert.assertFalse(downInstanceManager.contains(instance2));
+    }
+
+    @Test
+    public void testExpiration() throws Exception
+    {
+        ServiceInstance<Void> instance1 = ServiceInstance.<Void>builder().name("hey").id("1").build();
+        ServiceInstance<Void> instance2 = ServiceInstance.<Void>builder().name("hey").id("2").build();
+
+        DownInstanceManager downInstanceManager = new DownInstanceManager(1, TimeUnit.SECONDS);
+
+        downInstanceManager.add(instance1);
+        Assert.assertTrue(downInstanceManager.contains(instance1));
+        Assert.assertFalse(downInstanceManager.contains(instance2));
+
+        Thread.sleep(1000);
+
+        Assert.assertFalse(downInstanceManager.contains(instance1));
+        Assert.assertFalse(downInstanceManager.contains(instance2));
+    }
+
+    @Test
+    public void testInProvider() throws Exception
+    {
+        ServiceInstance<Void> instance1 = ServiceInstance.<Void>builder().name("hey").id("1").build();
+        ServiceInstance<Void> instance2 = ServiceInstance.<Void>builder().name("hey").id("2").build();
+
+        DownInstanceManager downInstanceManager = new DownInstanceManager(1, TimeUnit.SECONDS);
+        ServiceDiscoveryImpl<Void> discovery = new ServiceDiscoveryImpl<Void>(null,
null, null, null);
+        ServiceProviderImpl<Void> provider = new ServiceProviderImpl<Void>
+        (
+            discovery,
+            "test",
+            new RandomStrategy<Void>(),
+            null,
+            downInstanceManager
+        );
+    }
+}


Mime
View raw message