You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@twill.apache.org by ch...@apache.org on 2014/04/25 20:13:38 UTC

git commit: (TWILL-75) Fix race condition in the InMemoryDiscoveryService that would raise concurrent modification exception.

Repository: incubator-twill
Updated Branches:
  refs/heads/master 0208a8433 -> fc265f1a2


(TWILL-75) Fix race condition in the InMemoryDiscoveryService that would raise concurrent modification exception.

Signed-off-by: Terence Yim <te...@continuuity.com>


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

Branch: refs/heads/master
Commit: fc265f1a2dcd7fe2af3f92ad4294446e6f744436
Parents: 0208a84
Author: Terence Yim <te...@continuuity.com>
Authored: Fri Apr 25 11:00:28 2014 -0700
Committer: Terence Yim <te...@continuuity.com>
Committed: Fri Apr 25 11:13:31 2014 -0700

----------------------------------------------------------------------
 .../discovery/DefaultServiceDiscovered.java     |  2 +-
 .../discovery/DiscoveryServiceTestBase.java     | 37 ++++++++++++++++++++
 2 files changed, 38 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-twill/blob/fc265f1a/twill-discovery-core/src/main/java/org/apache/twill/discovery/DefaultServiceDiscovered.java
----------------------------------------------------------------------
diff --git a/twill-discovery-core/src/main/java/org/apache/twill/discovery/DefaultServiceDiscovered.java b/twill-discovery-core/src/main/java/org/apache/twill/discovery/DefaultServiceDiscovered.java
index 9734683..63df8ad 100644
--- a/twill-discovery-core/src/main/java/org/apache/twill/discovery/DefaultServiceDiscovered.java
+++ b/twill-discovery-core/src/main/java/org/apache/twill/discovery/DefaultServiceDiscovered.java
@@ -49,7 +49,7 @@ final class DefaultServiceDiscovered implements ServiceDiscovered {
   }
 
   void setDiscoverables(Set<Discoverable> discoverables) {
-    this.discoverables.set(discoverables);
+    this.discoverables.set(ImmutableSet.copyOf(discoverables));
 
     // Collect all listeners with a read lock to the listener list.
     List<ListenerCaller> callers = Lists.newArrayList();

http://git-wip-us.apache.org/repos/asf/incubator-twill/blob/fc265f1a/twill-discovery-core/src/test/java/org/apache/twill/discovery/DiscoveryServiceTestBase.java
----------------------------------------------------------------------
diff --git a/twill-discovery-core/src/test/java/org/apache/twill/discovery/DiscoveryServiceTestBase.java b/twill-discovery-core/src/test/java/org/apache/twill/discovery/DiscoveryServiceTestBase.java
index 9d86963..2d1fae6 100644
--- a/twill-discovery-core/src/test/java/org/apache/twill/discovery/DiscoveryServiceTestBase.java
+++ b/twill-discovery-core/src/test/java/org/apache/twill/discovery/DiscoveryServiceTestBase.java
@@ -27,6 +27,7 @@ import org.junit.Assert;
 import org.junit.Test;
 
 import java.net.InetSocketAddress;
+import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ArrayBlockingQueue;
@@ -244,6 +245,42 @@ public abstract class DiscoveryServiceTestBase {
     Assert.assertTrue(waitTillExpected(0, discoveryServiceClient.discover("service3")));
   }
 
+  @Test
+  public void testIterator() throws InterruptedException {
+    // This test is to verify TWILL-75
+    Map.Entry<DiscoveryService, DiscoveryServiceClient> entry = create();
+    final DiscoveryService service = entry.getKey();
+    DiscoveryServiceClient client = entry.getValue();
+
+    final String serviceName = "iterator";
+    ServiceDiscovered discovered = client.discover(serviceName);
+
+    // Create a thread for performing registration.
+    Thread t = new Thread() {
+      @Override
+      public void run() {
+        service.register(new Discoverable() {
+          @Override
+          public String getName() {
+            return serviceName;
+          }
+
+          @Override
+          public InetSocketAddress getSocketAddress() {
+            return new InetSocketAddress(12345);
+          }
+        });
+      }
+    };
+
+    Iterator<Discoverable> iterator = discovered.iterator();
+    t.start();
+    t.join();
+
+    // This would throw exception if there is race condition.
+    Assert.assertFalse(iterator.hasNext());
+  }
+
   protected Cancellable register(DiscoveryService service, final String name, final String host, final int port) {
     return service.register(new Discoverable() {
       @Override