package jdk.jfr.internal.consumer;
import java.io.IOException;
import java.nio.file.Path;
import java.security.AccessControlContext;
import java.time.Duration;
import java.time.Instant;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.function.Consumer;
import jdk.jfr.Configuration;
import jdk.jfr.consumer.RecordedEvent;
import jdk.jfr.internal.JVM;
import jdk.jfr.internal.PlatformRecording;
import jdk.jfr.internal.Utils;
import jdk.jfr.internal.consumer.ChunkParser.ParserConfiguration;
public class EventDirectoryStream extends AbstractEventStream {
private final static Comparator<? super RecordedEvent> EVENT_COMPARATOR = JdkJfrConsumer.instance().eventComparator();
private final RepositoryFiles repositoryFiles;
private final FileAccess fileAccess;
private ChunkParser currentParser;
private long currentChunkStartNanos;
private RecordedEvent[] sortedCache;
private int threadExclusionLevel = 0;
protected volatile long maxSize;
protected volatile Duration maxAge;
private volatile Consumer<Long> onCompleteHandler;
public EventDirectoryStream(AccessControlContext acc, Path p, FileAccess fileAccess, PlatformRecording recording, List<Configuration> configurations) throws IOException {
super(acc, recording, configurations);
this.fileAccess = Objects.requireNonNull(fileAccess);
this.repositoryFiles = new RepositoryFiles(fileAccess, p);
}
@Override
public void close() {
setClosed(true);
dispatcher().runCloseActions();
repositoryFiles.close();
if (currentParser != null) {
currentParser.close();
onComplete(currentParser.getEndNanos());
}
}
public void setChunkCompleteHandler(Consumer<Long> handler) {
onCompleteHandler = handler;
}
private void onComplete(long epochNanos) {
Consumer<Long> handler = onCompleteHandler;
if (handler != null) {
handler.accept(epochNanos);
}
}
@Override
public void start() {
start(Utils.timeToNanos(Instant.now()));
}
@Override
public void startAsync() {
startAsync(Utils.timeToNanos(Instant.now()));
}
@Override
protected void process() throws IOException {
JVM jvm = JVM.getJVM();
Thread t = Thread.currentThread();
try {
if (jvm.isExcluded(t)) {
threadExclusionLevel++;
} else {
jvm.exclude(t);
}
processRecursionSafe();
} finally {
if (threadExclusionLevel > 0) {
threadExclusionLevel--;
} else {
jvm.include(t);
}
}
}
protected void processRecursionSafe() throws IOException {
Dispatcher lastDisp = null;
Dispatcher disp = dispatcher();
Path path;
boolean validStartTime = recording != null || disp.startTime != null;
if (validStartTime) {
path = repositoryFiles.firstPath(disp.startNanos, true);
} else {
path = repositoryFiles.lastPath(true);
}
if (path == null) {
return;
}
currentChunkStartNanos = repositoryFiles.getTimestamp(path);
try (RecordingInput input = new RecordingInput(path.toFile(), fileAccess)) {
currentParser = new ChunkParser(input, disp.parserConfiguration);
long segmentStart = currentParser.getStartNanos() + currentParser.getChunkDuration();
long filterStart = validStartTime ? disp.startNanos : segmentStart;
long filterEnd = disp.endTime != null ? disp.endNanos : Long.MAX_VALUE;
while (!isClosed()) {
onMetadata(currentParser);
while (!isClosed() && !currentParser.isChunkFinished()) {
disp = dispatcher();
if (disp != lastDisp) {
ParserConfiguration pc = disp.parserConfiguration;
pc.filterStart = filterStart;
pc.filterEnd = filterEnd;
currentParser.updateConfiguration(pc, true);
lastDisp = disp;
}
if (disp.parserConfiguration.isOrdered()) {
processOrdered(disp);
} else {
processUnordered(disp);
}
currentParser.resetCache();
if (currentParser.getStartNanos() + currentParser.getChunkDuration() > filterEnd) {
close();
return;
}
}
if (isLastChunk()) {
return;
}
if (repositoryFiles.hasFixedPath() && currentParser.isFinalChunk()) {
return;
}
if (isClosed()) {
return;
}
long durationNanos = currentParser.getChunkDuration();
long endChunkNanos = currentParser.getEndNanos();
if (durationNanos == 0) {
durationNanos++;
}
path = repositoryFiles.nextPath(currentChunkStartNanos + durationNanos, true);
if (path == null) {
return;
}
currentChunkStartNanos = repositoryFiles.getTimestamp(path);
input.setFile(path);
onComplete(endChunkNanos);
currentParser = currentParser.newChunkParser();
}
}
}
private boolean isLastChunk() {
if (recording == null) {
return false;
}
return recording.getFinalChunkStartNanos() >= currentParser.getStartNanos();
}
private void processOrdered(Dispatcher c) throws IOException {
if (sortedCache == null) {
sortedCache = new RecordedEvent[100_000];
}
int index = 0;
while (true) {
RecordedEvent e = currentParser.readStreamingEvent();
if (e == null) {
break;
}
if (index == sortedCache.length) {
sortedCache = Arrays.copyOf(sortedCache, sortedCache.length * 2);
}
sortedCache[index++] = e;
}
onMetadata(currentParser);
if (index == 0 && currentParser.isChunkFinished()) {
onFlush();
return;
}
if (index > 1) {
Arrays.sort(sortedCache, 0, index, EVENT_COMPARATOR);
}
for (int i = 0; i < index; i++) {
c.dispatch(sortedCache[i]);
}
onFlush();
return;
}
private boolean processUnordered(Dispatcher c) throws IOException {
while (true) {
RecordedEvent e = currentParser.readStreamingEvent();
if (e == null) {
onFlush();
return true;
}
onMetadata(currentParser);
c.dispatch(e);
}
}
public void setMaxSize(long maxSize) {
this.maxSize = maxSize;
}
public void setMaxAge(Duration maxAge) {
this.maxAge = maxAge;
}
}