polygene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From nic...@apache.org
Subject [11/13] polygene-java git commit: POLYGENE-308 - Finally found the evasive concurrency bug. AtomicInstancePool was not thread-safe after all, and is replaced by using the ConcurrentLinkedQueue in standard library instead. No idea why this was not used in
Date Fri, 04 May 2018 02:22:18 GMT
POLYGENE-308 - Finally found the evasive concurrency bug. AtomicInstancePool was not thread-safe
after all, and is replaced by using the ConcurrentLinkedQueue in standard library instead.
No idea why this was not used in the first place, possibly to add "removal" feature in the
future.


Project: http://git-wip-us.apache.org/repos/asf/polygene-java/repo
Commit: http://git-wip-us.apache.org/repos/asf/polygene-java/commit/393cf214
Tree: http://git-wip-us.apache.org/repos/asf/polygene-java/tree/393cf214
Diff: http://git-wip-us.apache.org/repos/asf/polygene-java/diff/393cf214

Branch: refs/heads/develop
Commit: 393cf214c92e3057788caf8fce29262534d3e5e2
Parents: 0cb52e7
Author: niclas <niclas@hedhman.org>
Authored: Thu May 3 08:21:39 2018 +0800
Committer: niclas <niclas@hedhman.org>
Committed: Thu May 3 08:21:39 2018 +0800

----------------------------------------------------------------------
 .../runtime/composite/AtomicInstancePool.java   | 59 --------------------
 .../runtime/composite/CompositeMethodModel.java |  7 ++-
 .../CompositeMethodInvocationTest.java          | 26 +++++++--
 3 files changed, 25 insertions(+), 67 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/polygene-java/blob/393cf214/core/runtime/src/main/java/org/apache/polygene/runtime/composite/AtomicInstancePool.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/polygene/runtime/composite/AtomicInstancePool.java
b/core/runtime/src/main/java/org/apache/polygene/runtime/composite/AtomicInstancePool.java
deleted file mode 100644
index 95cd988..0000000
--- a/core/runtime/src/main/java/org/apache/polygene/runtime/composite/AtomicInstancePool.java
+++ /dev/null
@@ -1,59 +0,0 @@
-/*
- *  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.polygene.runtime.composite;
-
-import java.util.concurrent.atomic.AtomicReference;
-
-/**
- * Method instance pool that keeps a linked list. Uses atomic reference
- * to ensure that instances are acquired and returned in a thread-safe
- * manner.
- */
-public final class AtomicInstancePool
-    implements InstancePool<CompositeMethodInstance>
-{
-    private final AtomicReference<CompositeMethodInstance> first = new AtomicReference<>();
-
-    @Override
-    public CompositeMethodInstance obtainInstance()
-    {
-        CompositeMethodInstance firstInstance;
-        do
-        {
-            firstInstance = first.get();
-        }
-        while( firstInstance != null && !first.compareAndSet( firstInstance, firstInstance.getNext()
) );
-
-        return firstInstance;
-    }
-
-    @Override
-    public void releaseInstance( CompositeMethodInstance compositeMethodInstance )
-    {
-        CompositeMethodInstance firstInstance;
-        do
-        {
-            firstInstance = first.get();
-            compositeMethodInstance.setNext( firstInstance );
-        }
-        while( !first.compareAndSet( firstInstance, compositeMethodInstance ) );
-    }
-}

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/393cf214/core/runtime/src/main/java/org/apache/polygene/runtime/composite/CompositeMethodModel.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/main/java/org/apache/polygene/runtime/composite/CompositeMethodModel.java
b/core/runtime/src/main/java/org/apache/polygene/runtime/composite/CompositeMethodModel.java
index ec6de3b..1ed4cdd 100644
--- a/core/runtime/src/main/java/org/apache/polygene/runtime/composite/CompositeMethodModel.java
+++ b/core/runtime/src/main/java/org/apache/polygene/runtime/composite/CompositeMethodModel.java
@@ -29,6 +29,7 @@ import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Objects;
+import java.util.concurrent.ConcurrentLinkedQueue;
 import java.util.stream.Collectors;
 import java.util.stream.Stream;
 import org.apache.polygene.api.common.ConstructionException;
@@ -56,7 +57,7 @@ public final class CompositeMethodModel
 
     // Context
 //    private final SynchronizedCompositeMethodInstancePool instancePool = new SynchronizedCompositeMethodInstancePool();
-    private final AtomicInstancePool instancePool = new AtomicInstancePool();
+    private final ConcurrentLinkedQueue<CompositeMethodInstance> instancePool = new
ConcurrentLinkedQueue<>();
     private final ConstraintsInstance constraintsInstance;
 
     public CompositeMethodModel( Method method,
@@ -111,13 +112,13 @@ public final class CompositeMethodModel
         }
         finally
         {
-            instancePool.releaseInstance( methodInstance );
+            instancePool.offer( methodInstance );
         }
     }
 
     private CompositeMethodInstance getInstance( ModuleDescriptor module )
     {
-        CompositeMethodInstance methodInstance = instancePool.obtainInstance();
+        CompositeMethodInstance methodInstance = instancePool.poll();
         if( methodInstance == null )
         {
             methodInstance = newCompositeMethodInstance( module );

http://git-wip-us.apache.org/repos/asf/polygene-java/blob/393cf214/core/runtime/src/test/java/org/apache/polygene/runtime/composite/CompositeMethodInvocationTest.java
----------------------------------------------------------------------
diff --git a/core/runtime/src/test/java/org/apache/polygene/runtime/composite/CompositeMethodInvocationTest.java
b/core/runtime/src/test/java/org/apache/polygene/runtime/composite/CompositeMethodInvocationTest.java
index 44d4ea3..bfa2a7e 100644
--- a/core/runtime/src/test/java/org/apache/polygene/runtime/composite/CompositeMethodInvocationTest.java
+++ b/core/runtime/src/test/java/org/apache/polygene/runtime/composite/CompositeMethodInvocationTest.java
@@ -1,3 +1,20 @@
+/*
+ *  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.polygene.runtime.composite;
 
 import org.apache.polygene.api.injection.scope.Service;
@@ -6,21 +23,20 @@ import org.apache.polygene.api.service.ServiceComposite;
 import org.apache.polygene.bootstrap.AssemblyException;
 import org.apache.polygene.bootstrap.ModuleAssembly;
 import org.apache.polygene.test.AbstractPolygeneTest;
+import org.junit.jupiter.api.RepeatedTest;
 import org.junit.jupiter.api.Test;
 
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.TimeUnit;
 
+/** Regression test POLYGENE-308
+ */
 public class CompositeMethodInvocationTest extends AbstractPolygeneTest {
 
     @Service
     MyService srv;
 
-    /**
-     * To correct, change instance pool type in CompositeMethodModel
-     * AtomicInstancePool -> SynchronizedCompositeMethodInstancePool
-     */
     @Test
     public void corruptedMethodInvocation() throws InterruptedException {
         srv.dummy(); // to avoid concurrent activation (it can have own issues)
@@ -33,7 +49,7 @@ public class CompositeMethodInvocationTest extends AbstractPolygeneTest
{
                 }
             });
         }
-        exe.awaitTermination(10, TimeUnit.MINUTES);
+        exe.awaitTermination(30, TimeUnit.SECONDS);
     }
 
     @Override


Mime
View raw message