Skip to content

Commit

Permalink
GH-4554 Close models without relying on finalize.
Browse files Browse the repository at this point in the history
  • Loading branch information
kenwenzel committed May 17, 2023
1 parent 9863514 commit 929f908
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,21 @@ public void close() throws SailException {
refbacks = null;
prepend = null;
observed = null;
if (approved instanceof AutoCloseable) {
try {
((AutoCloseable) approved).close();
} catch (Exception e) {
throw new SailException(e);
}
}
approved = null;
if (deprecated instanceof AutoCloseable) {
try {
((AutoCloseable) deprecated).close();
} catch (Exception e) {
throw new SailException(e);
}
}
deprecated = null;
approvedContexts = null;
deprecatedContexts = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*******************************************************************************/
package org.eclipse.rdf4j.sail.lmdb;

import java.io.Closeable;
import java.io.File;
import java.io.IOException;
import java.io.ObjectInputStream;
Expand Down Expand Up @@ -40,7 +41,7 @@
* estimated memory usage is more than the amount of free memory available. Once the threshold is cross this
* implementation seamlessly changes to a disk based {@link SailSourceModel}.
*/
abstract class MemoryOverflowModel extends AbstractModel {
abstract class MemoryOverflowModel extends AbstractModel implements AutoCloseable {

private static final long serialVersionUID = 4119844228099208169L;

Expand Down Expand Up @@ -246,7 +247,7 @@ private synchronized void checkMemoryOverflow() {
}

// Sync if either the estimated size of the next block is larger than remaining memory, or
// if less than 15% of the heap is still free (this last condition to avoid GC overhead limit)
// if less than 25% of the heap is still free (this last condition to avoid GC overhead limit)
if (freeToAllocateMemory < MIN_AVAILABLE_MEM_BEFORE_OVERFLOWING ||
freeToAllocateMemory < Math.max(0.25 * maxMemory, maxBlockSize)) {
logger.debug("syncing at {} triples. max block size: {}", size, maxBlockSize);
Expand All @@ -264,26 +265,7 @@ private synchronized void overflowToDisk() {
dataDir = Files.createTempDirectory("model").toFile();
logger.debug("memory overflow using temp directory {}", dataDir);
store = createSailStore(dataDir);
disk = new SailSourceModel(store, verifyAdditions) {

@Override
protected void finalize() throws Throwable {
logger.debug("finalizing {}", dataDir);
if (disk == this) {
try {
store.close();
} catch (SailException e) {
logger.error(e.toString(), e);
} finally {
FileUtil.deleteDir(dataDir);
dataDir = null;
store = null;
disk = null;
}
}
super.finalize();
}
};
disk = new SailSourceModel(store, verifyAdditions);
disk.addAll(memory);
memory = new LinkedHashModel(memory.getNamespaces(), LARGE_BLOCK);
logger.debug("overflow synced to disk");
Expand All @@ -292,4 +274,22 @@ protected void finalize() throws Throwable {
logger.error("Error while writing to overflow directory " + path, e);
}
}

@Override
public void close() throws IOException {
if (disk != null) {
logger.debug("closing {}", dataDir);
disk.close();
try {
store.close();
} catch (SailException e) {
logger.error(e.toString(), e);
} finally {
FileUtil.deleteDir(dataDir);
dataDir = null;
store = null;
disk = null;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,21 @@ private synchronized SailDataset dataset() throws SailException {
return dataset;
}

public void close() {
if (sink != null) {
try {
sink.flush();
} finally {
sink.close();
sink = null;
}
}
if (dataset != null) {
dataset.close();
dataset = null;
}
}

private boolean contains(SailDataset dataset, Resource subj, IRI pred, Value obj, Resource... contexts)
throws SailException {
if (dataset == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* estimated memory usage is more than the amount of free memory available. Once the threshold is cross this
* implementation seamlessly changes to a disk based {@link SailSourceModel}.
*/
abstract class MemoryOverflowModel extends AbstractModel {
abstract class MemoryOverflowModel extends AbstractModel implements AutoCloseable {

private static final long serialVersionUID = 4119844228099208169L;

Expand Down Expand Up @@ -246,7 +246,7 @@ private synchronized void checkMemoryOverflow() {
}

// Sync if either the estimated size of the next block is larger than remaining memory, or
// if less than 15% of the heap is still free (this last condition to avoid GC overhead limit)
// if less than 25% of the heap is still free (this last condition to avoid GC overhead limit)
if (freeToAllocateMemory < MIN_AVAILABLE_MEM_BEFORE_OVERFLOWING ||
freeToAllocateMemory < Math.max(0.25 * maxMemory, maxBlockSize)) {
logger.debug("syncing at {} triples. max block size: {}", size, maxBlockSize);
Expand All @@ -264,26 +264,7 @@ private synchronized void overflowToDisk() {
dataDir = Files.createTempDirectory("model").toFile();
logger.debug("memory overflow using temp directory {}", dataDir);
store = createSailStore(dataDir);
disk = new SailSourceModel(store, verifyAdditions) {

@Override
protected void finalize() throws Throwable {
logger.debug("finalizing {}", dataDir);
if (disk == this) {
try {
store.close();
} catch (SailException e) {
logger.error(e.toString(), e);
} finally {
FileUtil.deleteDir(dataDir);
dataDir = null;
store = null;
disk = null;
}
}
super.finalize();
}
};
disk = new SailSourceModel(store, verifyAdditions);
disk.addAll(memory);
memory = new LinkedHashModel(memory.getNamespaces(), LARGE_BLOCK);
logger.debug("overflow synced to disk");
Expand All @@ -292,4 +273,22 @@ protected void finalize() throws Throwable {
logger.error("Error while writing to overflow directory " + path, e);
}
}

@Override
public void close() throws IOException {
if (disk != null) {
logger.debug("closing {}", dataDir);
disk.close();
try {
store.close();
} catch (SailException e) {
logger.error(e.toString(), e);
} finally {
FileUtil.deleteDir(dataDir);
dataDir = null;
store = null;
disk = null;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,21 @@ private synchronized SailDataset dataset() throws SailException {
return dataset;
}

public void close() {
if (sink != null) {
try {
sink.flush();
} finally {
sink.close();
sink = null;
}
}
if (dataset != null) {
dataset.close();
dataset = null;
}
}

private boolean contains(SailDataset dataset, Resource subj, IRI pred, Value obj, Resource... contexts)
throws SailException {
if (dataset == null) {
Expand Down

0 comments on commit 929f908

Please sign in to comment.