package jdk.jfr.internal.consumer;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.NavigableMap;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import jdk.jfr.internal.LogLevel;
import jdk.jfr.internal.LogTag;
import jdk.jfr.internal.Logger;
import jdk.jfr.internal.Repository;
import jdk.jfr.internal.SecuritySupport.SafePath;
public final class RepositoryFiles {
private static final Object WAIT_OBJECT = new Object();
public static void notifyNewFile() {
synchronized (WAIT_OBJECT) {
WAIT_OBJECT.notifyAll();
}
}
private final FileAccess fileAccess;
private final NavigableMap<Long, Path> pathSet = new TreeMap<>();
private final Map<Path, Long> pathLookup = new HashMap<>();
private final Path repository;
private final Object waitObject;
private volatile boolean closed;
public RepositoryFiles(FileAccess fileAccess, Path repository) {
this.repository = repository;
this.fileAccess = fileAccess;
this.waitObject = repository == null ? WAIT_OBJECT : new Object();
}
long getTimestamp(Path p) {
return pathLookup.get(p);
}
public Path lastPath(boolean wait) {
if (updatePaths(wait)) {
return pathSet.lastEntry().getValue();
}
return null;
}
public Path firstPath(long startTimeNanos, boolean wait) {
if (updatePaths(wait)) {
Long time = pathSet.floorKey(startTimeNanos);
if (time != null) {
startTimeNanos = time;
}
return path(startTimeNanos, wait);
}
return null;
}
private boolean updatePaths(boolean wait) {
int beforeSize = pathLookup.size();
while (!closed) {
try {
if (updatePaths()) {
break;
}
} catch (IOException e) {
Logger.log(LogTag.JFR_SYSTEM_STREAMING, LogLevel.DEBUG, "IOException during repository file scan " + e.getMessage());
}
if (wait) {
nap();
} else {
return pathLookup.size() > beforeSize;
}
}
return !closed;
}
public Path nextPath(long startTimeNanos, boolean wait) {
if (closed) {
return null;
}
Path path = pathSet.get(startTimeNanos);
if (path != null) {
return path;
}
try {
updatePaths();
} catch (IOException e) {
}
return path(startTimeNanos, wait);
}
private Path path(long timestamp, boolean wait) {
if (closed) {
return null;
}
while (true) {
SortedMap<Long, Path> after = pathSet.tailMap(timestamp);
if (!after.isEmpty()) {
Path path = after.get(after.firstKey());
if (Logger.shouldLog(LogTag.JFR_SYSTEM_STREAMING, LogLevel.TRACE)) {
Logger.log(LogTag.JFR_SYSTEM_STREAMING, LogLevel.TRACE, "Return path " + path + " for start time nanos " + timestamp);
}
return path;
}
if (!updatePaths(wait)) {
return null;
}
}
}
private void nap() {
try {
synchronized (waitObject) {
waitObject.wait(1000);
}
} catch (InterruptedException e) {
}
}
private boolean updatePaths() throws IOException {
boolean foundNew = false;
Path repoPath = repository;
if (repoPath == null) {
SafePath sf = Repository.getRepository().getRepositoryPath();
if (sf == null) {
return false;
}
repoPath = sf.toPath();
}
try (DirectoryStream<Path> dirStream = fileAccess.newDirectoryStream(repoPath)) {
List<Path> added = new ArrayList<>();
Set<Path> current = new HashSet<>();
for (Path p : dirStream) {
if (!pathLookup.containsKey(p)) {
String s = p.toString();
if (s.endsWith(".jfr")) {
added.add(p);
Logger.log(LogTag.JFR_SYSTEM_STREAMING, LogLevel.DEBUG, "New file found: " + p.toAbsolutePath());
}
current.add(p);
}
}
List<Path> removed = new ArrayList<>();
for (Path p : pathLookup.keySet()) {
if (!current.contains(p)) {
removed.add(p);
}
}
for (Path remove : removed) {
Long time = pathLookup.get(remove);
pathSet.remove(time);
pathLookup.remove(remove);
}
Collections.sort(added, (p1, p2) -> p1.compareTo(p2));
for (Path p : added) {
long size = fileAccess.fileSize(p);
if (size >= ChunkHeader.headerSize()) {
long startNanos = readStartTime(p);
pathSet.put(startNanos, p);
pathLookup.put(p, startNanos);
foundNew = true;
}
}
return foundNew;
}
}
private long readStartTime(Path p) throws IOException {
try (RecordingInput in = new RecordingInput(p.toFile(), fileAccess, 100)) {
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Parsing header for chunk start time");
ChunkHeader c = new ChunkHeader(in);
return c.getStartNanos();
}
}
void close() {
synchronized (waitObject) {
this.closed = true;
waitObject.notify();
}
}
public boolean hasFixedPath() {
return repository != null;
}
}