diff --git a/planetiler-core/src/main/java/com/onthegomap/planetiler/collection/ArrayLongLongMapMmap.java b/planetiler-core/src/main/java/com/onthegomap/planetiler/collection/ArrayLongLongMapMmap.java index 720031f799..e208248b25 100644 --- a/planetiler-core/src/main/java/com/onthegomap/planetiler/collection/ArrayLongLongMapMmap.java +++ b/planetiler-core/src/main/java/com/onthegomap/planetiler/collection/ArrayLongLongMapMmap.java @@ -1,5 +1,6 @@ package com.onthegomap.planetiler.collection; +import static com.onthegomap.planetiler.util.Exceptions.throwFatalException; import static java.nio.file.StandardOpenOption.CREATE; import static java.nio.file.StandardOpenOption.READ; import static java.nio.file.StandardOpenOption.WRITE; @@ -57,7 +58,7 @@ class ArrayLongLongMapMmap implements LongLongMap.ParallelWrites { private final ConcurrentHashMap writeBuffers = new ConcurrentHashMap<>(); private final Semaphore activeSegments; private final BitSet usedSegments = new BitSet(); - FileChannel writeChannel; + private FileChannel writeChannel; private MappedByteBuffer[] segmentsArray; private FileChannel readChannel = null; private volatile int tail = 0; @@ -161,11 +162,19 @@ public long estimateMemoryUsageBytes() { @Override public void close() throws IOException { + if (segmentsArray != null) { + ByteBufferUtil.free(segmentsArray); + segmentsArray = null; + } + if (writeChannel != null) { + writeChannel.close(); + writeChannel = null; + } if (readChannel != null) { readChannel.close(); readChannel = null; - FileUtils.delete(path); } + FileUtils.delete(path); } /** @@ -226,9 +235,9 @@ ByteBuffer await() { return result.get(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); - throw new RuntimeException(e); + return throwFatalException(e); } catch (ExecutionException e) { - throw new RuntimeException(e); + return throwFatalException(e); } } @@ -238,7 +247,7 @@ void allocate() { activeSegments.acquire(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); - throw new RuntimeException(e); + throwFatalException(e); } synchronized (usedSegments) { usedSegments.set(id); @@ -254,11 +263,11 @@ void flushToDisk() { activeSegments.release(); } catch (InterruptedException e) { Thread.currentThread().interrupt(); - throw new RuntimeException(e); + throwFatalException(e); } catch (IOException e) { throw new UncheckedIOException(e); } catch (ExecutionException e) { - throw new RuntimeException(e); + throwFatalException(e); } } } diff --git a/planetiler-core/src/test/java/com/onthegomap/planetiler/collection/LongLongMapTest.java b/planetiler-core/src/test/java/com/onthegomap/planetiler/collection/LongLongMapTest.java index ec15d4af07..34b584fec8 100644 --- a/planetiler-core/src/test/java/com/onthegomap/planetiler/collection/LongLongMapTest.java +++ b/planetiler-core/src/test/java/com/onthegomap/planetiler/collection/LongLongMapTest.java @@ -7,7 +7,9 @@ import com.onthegomap.planetiler.reader.osm.OsmReader; import com.onthegomap.planetiler.util.Format; import com.onthegomap.planetiler.util.ResourceUsage; +import java.io.IOException; import java.nio.file.Path; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; @@ -23,6 +25,11 @@ public void setupSequentialWriter(@TempDir Path path) { this.sequential = createSequentialWriter(path); } + @AfterEach + public void closeSequentialWriter() throws IOException { + this.sequential.close(); + } + @Test public void missingValue() { assertEquals(Long.MIN_VALUE, sequential.get(0)); diff --git a/planetiler-core/src/test/java/com/onthegomap/planetiler/collection/ParallelLongLongMapTest.java b/planetiler-core/src/test/java/com/onthegomap/planetiler/collection/ParallelLongLongMapTest.java index 7885cec2b5..e308b6e7fd 100644 --- a/planetiler-core/src/test/java/com/onthegomap/planetiler/collection/ParallelLongLongMapTest.java +++ b/planetiler-core/src/test/java/com/onthegomap/planetiler/collection/ParallelLongLongMapTest.java @@ -6,6 +6,7 @@ import java.nio.file.Path; import java.util.concurrent.BrokenBarrierException; import java.util.concurrent.CyclicBarrier; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Timeout; @@ -19,7 +20,7 @@ public abstract class ParallelLongLongMapTest extends LongLongMapTest { @Test @Timeout(10) - void testWaitForBothWritersToClose() throws InterruptedException { + void testWaitForBothWritersToClose() { var writer1 = parallel.newWriter(); var writer2 = parallel.newWriter(); writer1.put(0, 1); @@ -107,6 +108,11 @@ public void setupParallelWriter(@TempDir Path path) { this.parallel = create(path); } + @AfterEach + public void closeParallelWriter() throws IOException { + this.parallel.close(); + } + @Override protected LongLongMap.SequentialWrites createSequentialWriter(Path path) { var sequentialMap = create(path); diff --git a/pom.xml b/pom.xml index 37d22bdab3..91865e07b4 100644 --- a/pom.xml +++ b/pom.xml @@ -201,6 +201,12 @@ org.apache.maven.plugins maven-surefire-plugin 3.0.0-M6 + + + + + + org.apache.maven.plugins