Skip to content
This repository was archived by the owner on May 12, 2021. It is now read-only.

Commit 393cf21

Browse files
committed
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.
1 parent 0cb52e7 commit 393cf21

3 files changed

Lines changed: 25 additions & 67 deletions

File tree

core/runtime/src/main/java/org/apache/polygene/runtime/composite/AtomicInstancePool.java

Lines changed: 0 additions & 59 deletions
This file was deleted.

core/runtime/src/main/java/org/apache/polygene/runtime/composite/CompositeMethodModel.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.Arrays;
3030
import java.util.List;
3131
import java.util.Objects;
32+
import java.util.concurrent.ConcurrentLinkedQueue;
3233
import java.util.stream.Collectors;
3334
import java.util.stream.Stream;
3435
import org.apache.polygene.api.common.ConstructionException;
@@ -56,7 +57,7 @@ public final class CompositeMethodModel
5657

5758
// Context
5859
// private final SynchronizedCompositeMethodInstancePool instancePool = new SynchronizedCompositeMethodInstancePool();
59-
private final AtomicInstancePool instancePool = new AtomicInstancePool();
60+
private final ConcurrentLinkedQueue<CompositeMethodInstance> instancePool = new ConcurrentLinkedQueue<>();
6061
private final ConstraintsInstance constraintsInstance;
6162

6263
public CompositeMethodModel( Method method,
@@ -111,13 +112,13 @@ public Object invoke( Object composite, Object[] params, MixinsInstance mixins,
111112
}
112113
finally
113114
{
114-
instancePool.releaseInstance( methodInstance );
115+
instancePool.offer( methodInstance );
115116
}
116117
}
117118

118119
private CompositeMethodInstance getInstance( ModuleDescriptor module )
119120
{
120-
CompositeMethodInstance methodInstance = instancePool.obtainInstance();
121+
CompositeMethodInstance methodInstance = instancePool.poll();
121122
if( methodInstance == null )
122123
{
123124
methodInstance = newCompositeMethodInstance( module );

core/runtime/src/test/java/org/apache/polygene/runtime/composite/CompositeMethodInvocationTest.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
118
package org.apache.polygene.runtime.composite;
219

320
import org.apache.polygene.api.injection.scope.Service;
@@ -6,21 +23,20 @@
623
import org.apache.polygene.bootstrap.AssemblyException;
724
import org.apache.polygene.bootstrap.ModuleAssembly;
825
import org.apache.polygene.test.AbstractPolygeneTest;
26+
import org.junit.jupiter.api.RepeatedTest;
927
import org.junit.jupiter.api.Test;
1028

1129
import java.util.concurrent.ExecutorService;
1230
import java.util.concurrent.Executors;
1331
import java.util.concurrent.TimeUnit;
1432

33+
/** Regression test POLYGENE-308
34+
*/
1535
public class CompositeMethodInvocationTest extends AbstractPolygeneTest {
1636

1737
@Service
1838
MyService srv;
1939

20-
/**
21-
* To correct, change instance pool type in CompositeMethodModel
22-
* AtomicInstancePool -> SynchronizedCompositeMethodInstancePool
23-
*/
2440
@Test
2541
public void corruptedMethodInvocation() throws InterruptedException {
2642
srv.dummy(); // to avoid concurrent activation (it can have own issues)
@@ -33,7 +49,7 @@ public void corruptedMethodInvocation() throws InterruptedException {
3349
}
3450
});
3551
}
36-
exe.awaitTermination(10, TimeUnit.MINUTES);
52+
exe.awaitTermination(30, TimeUnit.SECONDS);
3753
}
3854

3955
@Override

0 commit comments

Comments
 (0)