From commits-return-3541-archive-asf-public=cust-asf.ponee.io@polygene.apache.org Fri May 4 04:22:12 2018 Return-Path: X-Original-To: archive-asf-public@cust-asf.ponee.io Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx-eu-01.ponee.io (Postfix) with SMTP id 684791807A5 for ; Fri, 4 May 2018 04:22:11 +0200 (CEST) Received: (qmail 93065 invoked by uid 500); 4 May 2018 02:22:10 -0000 Mailing-List: contact commits-help@polygene.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@polygene.apache.org Delivered-To: mailing list commits@polygene.apache.org Received: (qmail 92827 invoked by uid 99); 4 May 2018 02:22:09 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 04 May 2018 02:22:09 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id D94B6F6954; Fri, 4 May 2018 02:22:08 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: niclas@apache.org To: commits@polygene.apache.org Date: Fri, 04 May 2018 02:22:18 -0000 Message-Id: In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer 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 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 Authored: Thu May 3 08:21:39 2018 +0800 Committer: niclas 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 -{ - private final AtomicReference 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 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