Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/closeables weak ref #1443

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import org.jetbrains.annotations.Nullable;

import java.io.*;
import java.lang.ref.WeakReference;
import java.security.SecureRandom;
import java.text.ParseException;
import java.time.ZoneId;
Expand Down Expand Up @@ -110,7 +111,7 @@
private final TimeProvider time;
@NotNull
private final BiFunction<RollingChronicleQueue, Wire, SingleChronicleQueueStore> storeFactory;
private final Set<Closeable> closers = Collections.newSetFromMap(new IdentityHashMap<>());
private final Set<WeakReference<Closeable>> closers = Collections.newSetFromMap(new IdentityHashMap<>());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to ensure these resources are closed if discarded, otherwise, the resources they reference won't be cleaned up except by further GCs, if they are closed on being discarded.

private final boolean readOnly;
@NotNull
private final CycleCalculator cycleCalculator;
Expand Down Expand Up @@ -716,16 +717,19 @@ public NavigableSet<Long> listCyclesBetween(int lowerCycle, int upperCycle) {
public <T> void addCloseListener(Closeable key) {
synchronized (closers) {
if (!closers.isEmpty())
closers.removeIf(Closeable::isClosed);
closers.add(key);
closers.removeIf(wrc -> {
final Closeable closeable = wrc.get();
return closeable != null || closeable.isClosed();
});
closers.add(new WeakReference<>(key));
}
}

@SuppressWarnings("unchecked")
@Override
protected void performClose() {
synchronized (closers) {
metaStoreMap.values().forEach(Closeable::closeQuietly);
Closeable.closeQuietly(metaStoreMap.values());
metaStoreMap.clear();
closers.forEach(Closeable::closeQuietly);
closers.clear();
Expand All @@ -746,7 +750,7 @@ protected void performClose() {

// close it if we created it.
if (eventLoop instanceof OnDemandEventLoop)
eventLoop.close();
Closeable.closeQuietly(eventLoop);
}

@Override
Expand Down Expand Up @@ -911,9 +915,14 @@ private ToIntFunction<String> fileNameToCycleFunction() {
return name -> dateCache.parseCount(name.substring(0, name.length() - SUFFIX.length()));
}

@Deprecated(/* to be removed in x.25 */)
void removeCloseListener(final StoreTailer storeTailer) {
removeCloseListener((java.io.Closeable) storeTailer);
}

void removeCloseListener(final java.io.Closeable closeable) {
synchronized (closers) {
closers.remove(storeTailer);
closers.removeIf(wrc -> wrc.get() == closeable);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,7 @@ public void writeBytes(@NotNull final WriteBytesMarshallable marshallable) {

@Override
protected void performClose() {
// queue.removeCloseListener(this);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see comments - will come back to

releaseBytesFor(wireForIndex);
releaseBytesFor(wire);
releaseBytesFor(bufferWire);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ public DocumentContext readingDocument() {

@Override
protected void performClose() {
// queue.removeCloseListener((java.io.Closeable) this);
Closeable.closeQuietly(indexUpdater);
// the wire ref count will be released here by setting it to null
context.wire(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ static void doPerfTest(TestWriter<Bytes<?>> writer, TestReader<Bytes<?>> reader,
Histogram readHdr = new Histogram(30, 7);
String file = OS.getTarget() + "/deleteme-" + Time.uniqueId();
try (ChronicleQueue chronicle = single(file).blockSize(64 << 20).build();
ExcerptAppender appender = chronicle.createAppender()) {
ExcerptAppender appender = chronicle.createAppender();
ExcerptTailer tailer = chronicle.createTailer()) {
UncheckedBytes bytes = new UncheckedBytes(BytesStore.empty().bytesForRead());
for (int i = 0; i < count; i++) {
long start = System.nanoTime();
Expand All @@ -82,7 +83,6 @@ static void doPerfTest(TestWriter<Bytes<?>> writer, TestReader<Bytes<?>> reader,
writeHdr.sample(time);
}

ExcerptTailer tailer = chronicle.createTailer();
for (int i = 0; i < count; i++) {
long start2 = System.nanoTime();
try (DocumentContext dc = tailer.readingDocument()) {
Expand Down
33 changes: 17 additions & 16 deletions src/test/java/net/openhft/chronicle/queue/StridingAQueueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,23 @@ public void testStriding() {

assertEquals(getExpected(), queue.dump().replaceAll("(?m)^#.+$\\n", ""));
StringWriter sw = new StringWriter();
ExcerptTailer tailer = queue.createTailer().direction(TailerDirection.BACKWARD).toEnd().striding(true);
MethodReader reader = tailer.methodReader(Mocker.logging(SAQMessage.class, "", sw));
while (reader.readOne()) ;
assertEquals("hi[4, 9]\n" +
"hi[4, 8]\n" +
"hi[4, 4]\n" +
"hi[4, 0]\n" +
"hi[3, 8]\n" +
"hi[3, 4]\n" +
"hi[3, 0]\n" +
"hi[2, 7]\n" +
"hi[2, 5]\n" +
"hi[2, 1]\n" +
"hi[1, 4]\n" +
"hi[1, 0]\n",
sw.toString().replace("\r", ""));
try (ExcerptTailer tailer = queue.createTailer().direction(TailerDirection.BACKWARD).toEnd().striding(true)) {
MethodReader reader = tailer.methodReader(Mocker.logging(SAQMessage.class, "", sw));
while (reader.readOne()) ;
assertEquals("hi[4, 9]\n" +
"hi[4, 8]\n" +
"hi[4, 4]\n" +
"hi[4, 0]\n" +
"hi[3, 8]\n" +
"hi[3, 4]\n" +
"hi[3, 0]\n" +
"hi[2, 7]\n" +
"hi[2, 5]\n" +
"hi[2, 1]\n" +
"hi[1, 4]\n" +
"hi[1, 0]\n",
sw.toString().replace("\r", ""));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,8 @@ private void doTest(@NotNull String... problematic) {
.blockSize(Maths.nextPower2(EXTREMELY_LARGE.length() * 4, 256 << 10))
// .testBlockSize() not suitable as large message sizes.
.build();
ExcerptAppender appender = theQueue.createAppender()) {

ExcerptTailer tailer = theQueue.createTailer();
ExcerptAppender appender = theQueue.createAppender();
ExcerptTailer tailer = theQueue.createTailer()) {

StringBuilder tmpReadback = new StringBuilder();

Expand Down