-
Notifications
You must be signed in to change notification settings - Fork 521
/
ChunkAllocationAfterGarbageCollectionTest.java
105 lines (88 loc) · 4.03 KB
/
ChunkAllocationAfterGarbageCollectionTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package net.openhft.chronicle.queue.impl.single;
import net.openhft.chronicle.bytes.NewChunkListener;
import net.openhft.chronicle.core.time.TimeProvider;
import net.openhft.chronicle.queue.DirectoryUtils;
import net.openhft.chronicle.queue.ExcerptAppender;
import net.openhft.chronicle.queue.RollCycles;
import net.openhft.chronicle.wire.DocumentContext;
import net.openhft.chronicle.wire.WireType;
import org.jetbrains.annotations.NotNull;
import org.junit.After;
import org.junit.Ignore;
import org.junit.Test;
import java.io.File;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport;
import java.util.stream.IntStream;
import static java.util.stream.IntStream.range;
public final class ChunkAllocationAfterGarbageCollectionTest {
private static final byte[] DATA = new byte[8192];
private final File path = DirectoryUtils.tempDir(ChunkAllocationAfterGarbageCollectionTest.class.getSimpleName());
@Ignore("demonstrates an issue")
@Test
public void pretoucherAppenderShouldNotResetToStartOfMappedFile() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
Executors.newSingleThreadExecutor().submit(() -> {
String lastAppenderHashCode = "";
Thread.currentThread().setName("pre-toucher-thread");
try (final SingleChronicleQueue queue = createQueue(path, System::currentTimeMillis)) {
while (!Thread.currentThread().isInterrupted()) {
lastAppenderHashCode = preTouch(lastAppenderHashCode, queue, new LoggingNewChunkListener());
LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(10L));
}
}
});
try (final SingleChronicleQueue queue = createQueue(path, System::currentTimeMillis)) {
final ExcerptAppender appender = queue.acquireAppender();
queue.storeForCycle(queue.cycle(), 0, true).bytes().
setNewChunkListener(new LoggingNewChunkListener());
appender.lazyIndexing(true);
IntStream.range(0, 5_000).forEach(i -> {
range(0, 100).forEach(j -> {
try (final DocumentContext ctx = appender.writingDocument()) {
ctx.wire().write().bytes(DATA);
}
});
latch.countDown();
LockSupport.parkNanos(TimeUnit.SECONDS.toNanos(1L));
if (i % 30 == 0) {
System.out.println("GC");
GcControls.waitForGcCycle();
}
});
}
}
@NotNull
private String preTouch(final String lastAppenderHashCode, final SingleChronicleQueue queue,
final LoggingNewChunkListener chunkListener) {
final ExcerptAppender appender = queue.acquireAppender();
queue.storeForCycle(queue.cycle(), 0, true).bytes().
setNewChunkListener(chunkListener);
if (!lastAppenderHashCode.equals(Integer.toHexString(System.identityHashCode(appender)))) {
System.out.println("Appender changed");
}
appender.pretouch();
return Integer.toHexString(System.identityHashCode(appender));
}
private static final class LoggingNewChunkListener implements NewChunkListener {
@Override
public void onNewChunk(final String filename, final int chunk, final long delayMicros) {
System.out.printf("%s chunk %d%n", Thread.currentThread().getName(), chunk);
}
}
@After
public void deleteDir() throws Exception {
DirectoryUtils.deleteDir(path);
}
private static SingleChronicleQueue createQueue(final File path, final TimeProvider timeProvider) {
return SingleChronicleQueueBuilder.
binary(path).
timeProvider(timeProvider).
rollCycle(RollCycles.DAILY).
testBlockSize().
wireType(WireType.BINARY).
build();
}
}