forked from apache/jena
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
apacheGH-2787: Deadlock in
JenaSystem.init()
- Restored the early return from `JenaSystem.init()` as implemented prior to [commit 8654c07](apache@8654c07). - Simplified initialization using the "Initialization-on-demand holder idiom" for better efficiency and thread safety. - Added integration tests for `JenaSystem.init()`, including tests for parallel execution cases. - Implemented additional integration tests that start with a fresh JVM for each run, using JMH as the test runner. - Added `org.openjdk.jmh` as a dependency in `jena-integration-tests`.
- Loading branch information
arne-bdt
committed
Oct 25, 2024
1 parent
4247422
commit bae591d
Showing
5 changed files
with
262 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
jena-integration-tests/src/test/java/org/apache/jena/sys/TS_Sys.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* | ||
* 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.jena.sys; | ||
|
||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Suite; | ||
|
||
@RunWith(Suite.class) | ||
@Suite.SuiteClasses( | ||
{ TestJenaSystem.class | ||
, TestJenaSystemWithFreshJVM.class | ||
}) | ||
|
||
public class TS_Sys { } |
83 changes: 83 additions & 0 deletions
83
jena-integration-tests/src/test/java/org/apache/jena/sys/TestJenaSystem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
/* | ||
* 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.jena.sys; | ||
|
||
import org.apache.jena.rdf.model.ModelFactory; | ||
import org.junit.Test; | ||
|
||
|
||
import java.time.Duration; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.HashSet; | ||
import java.util.concurrent.Executors; | ||
import java.util.stream.IntStream; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class TestJenaSystem { | ||
|
||
/** | ||
* Test that the first initialization of JenaSystem is successful. | ||
* This code was able to reproduce a deadlock for <a href="https://github.com/apache/jena/issues/2787>GitHub issue 2787</a>, | ||
* which is now fixed. | ||
* It needs to be run in a separate JVM to run with a not initialized JenaSystem. | ||
* <p /> | ||
* The name is chosen so that it is executed first. | ||
*/ | ||
@Test | ||
public void testFirstInitParallel() { | ||
|
||
var pool = Executors.newFixedThreadPool(2); | ||
try { | ||
var futures = IntStream.range(1, 3) | ||
.mapToObj(i -> pool.submit(() -> { | ||
if (i % 2 == 0) { | ||
ModelFactory.createDefaultModel(); | ||
} else { | ||
JenaSystem.init(); | ||
} | ||
|
||
return i; | ||
})) | ||
.toList(); | ||
|
||
var intSet = new HashSet<Integer>(); | ||
assertTimeoutPreemptively( | ||
Duration.of(4, ChronoUnit.SECONDS), | ||
() -> { | ||
for (var future : futures) { | ||
intSet.add(future.get()); | ||
} | ||
}); | ||
|
||
assertEquals(2, intSet.size()); | ||
} finally { | ||
pool.shutdown(); | ||
} | ||
} | ||
|
||
/** | ||
* Test that the first initialization of JenaSystem is successful. | ||
* This test is placed in the integration tests module to ensure that the initialization | ||
* is successful when multiple modules are loaded. | ||
*/ | ||
@Test | ||
public void testLaterInit() { | ||
assertDoesNotThrow(() -> JenaSystem.init()); | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
jena-integration-tests/src/test/java/org/apache/jena/sys/TestJenaSystemWithFreshJVM.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* 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.jena.sys; | ||
|
||
import org.apache.jena.rdf.model.ModelFactory; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.openjdk.jmh.annotations.Benchmark; | ||
import org.openjdk.jmh.annotations.Mode; | ||
import org.openjdk.jmh.annotations.Scope; | ||
import org.openjdk.jmh.annotations.State; | ||
import org.openjdk.jmh.runner.Runner; | ||
import org.openjdk.jmh.runner.options.OptionsBuilder; | ||
import org.openjdk.jmh.runner.options.TimeValue; | ||
|
||
import java.time.Duration; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.HashSet; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.stream.IntStream; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
/** | ||
* This test is not a benchmark but a test to ensure that the JenaSystem can be initialized in a fresh JVM. | ||
* In <a href="https://github.com/apache/jena/issues/2787>GitHub issue 2787</a>, a deadlock was observed when JenaSystem | ||
* was initialized in parallel with ModelFactory.createDefaultModel() or other classes that use a static initialize to | ||
* initialize JenaSystem. This test is used to reproduce the deadlock and ensure that it is fixed. | ||
*/ | ||
@State(Scope.Benchmark) | ||
public class TestJenaSystemWithFreshJVM { | ||
|
||
@Benchmark | ||
public void initParallel() { | ||
|
||
var pool = Executors.newFixedThreadPool(2); | ||
try { | ||
var futures = IntStream.range(1, 3) | ||
.mapToObj(i -> pool.submit(() -> { | ||
if (i % 2 == 0) { | ||
ModelFactory.createDefaultModel(); | ||
} | ||
else { | ||
JenaSystem.init(); | ||
} | ||
|
||
return i; | ||
})) | ||
.toList(); | ||
|
||
var intSet = new HashSet<Integer>(); | ||
assertTimeoutPreemptively( | ||
Duration.of(4, ChronoUnit.SECONDS), | ||
() -> { | ||
for (var future : futures) { | ||
intSet.add(future.get()); | ||
} | ||
}); | ||
|
||
assertEquals(2, intSet.size()); | ||
} finally { | ||
pool.shutdown(); | ||
} | ||
} | ||
|
||
/** | ||
* Test that the first initialization of JenaSystem is successful. | ||
* This test is placed in the integration tests module to ensure that the initialization | ||
* is successful when multiple modules are loaded. | ||
* <p> | ||
* The test is called as benchmark to ensure that the JVM is fresh. | ||
*/ | ||
@Benchmark | ||
public void init() { | ||
JenaSystem.init(); | ||
} | ||
|
||
@Test | ||
public void benchmark() throws Exception { | ||
var opt = new OptionsBuilder() | ||
.include(this.getClass().getName()) | ||
.mode(Mode.AverageTime) | ||
.timeUnit(TimeUnit.SECONDS) | ||
.warmupTime(TimeValue.NONE) | ||
.warmupIterations(0) // we don't need warmup | ||
.measurementIterations(1) // we only need one iteration | ||
.measurementTime(TimeValue.NONE) | ||
.threads(1) // we only need one thread | ||
.forks(1) // we only need one fork | ||
.shouldFailOnError(true) // this is important to fail the test if the benchmark fails | ||
.timeout(TimeValue.seconds(6)) // 6 seconds should be enough, even on slow machines | ||
.build(); | ||
var results = new Runner(opt).run(); | ||
Assert.assertNotNull(results); | ||
} | ||
} |