src/jdk.jfr/share/classes/jdk/jfr/internal/consumer/RepositoryFiles.java
author egahlin
Thu, 27 Jun 2019 10:19:32 +0200
branchJEP-349-branch
changeset 57433 83e4343a6984
child 57434 216bf2e3b542
permissions -rw-r--r--
Clean up and fix parser level filtering
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57433
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
     1
package jdk.jfr.internal.consumer;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
     2
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
     3
import java.io.IOException;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
     4
import java.nio.file.DirectoryStream;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
     5
import java.nio.file.Files;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
     6
import java.nio.file.Path;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
     7
import java.util.ArrayList;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
     8
import java.util.Collections;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
     9
import java.util.HashMap;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    10
import java.util.HashSet;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    11
import java.util.List;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    12
import java.util.Map;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    13
import java.util.Set;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    14
import java.util.SortedMap;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    15
import java.util.TreeMap;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    16
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    17
import jdk.jfr.internal.LogLevel;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    18
import jdk.jfr.internal.LogTag;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    19
import jdk.jfr.internal.Logger;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    20
import jdk.jfr.internal.Repository;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    21
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    22
public final class RepositoryFiles {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    23
    private final Path repostory;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    24
    private final SortedMap<Long, Path> pathSet = new TreeMap<>();
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    25
    private final Map<Path, Long> pathLookup = new HashMap<>();
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    26
    private volatile boolean closed;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    27
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    28
    public RepositoryFiles(Path repostory) {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    29
        this.repostory = repostory;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    30
    }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    31
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    32
    public long getTimestamp(Path p) {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    33
        return pathLookup.get(p);
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    34
    }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    35
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    36
    public Path nextPath(long startTimeNanos) {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    37
        while (!closed) {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    38
            SortedMap<Long, Path> after = pathSet.tailMap(startTimeNanos);
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    39
            if (!after.isEmpty()) {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    40
                Path path = after.get(after.firstKey());
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    41
                Logger.log(LogTag.JFR_SYSTEM_STREAMING, LogLevel.TRACE, "Return path " + path + " for start time nanos " + startTimeNanos);
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    42
                return path;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    43
            }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    44
            try {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    45
                if (updatePaths(repostory)) {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    46
                    continue;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    47
                }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    48
            } catch (IOException e) {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    49
                Logger.log(LogTag.JFR_SYSTEM_STREAMING, LogLevel.DEBUG, "IOException during repository file scan " + e.getMessage());
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    50
                // This can happen if a chunk is being removed
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    51
                // between the file was discovered and an instance
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    52
                // of an EventSet was constructed. Just ignore,
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    53
                // and retry later.
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    54
            }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    55
            try {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    56
                synchronized (pathSet) {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    57
                    pathSet.wait(1000);
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    58
                }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    59
            } catch (InterruptedException e) {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    60
                // ignore
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    61
            }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    62
        }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    63
        return null;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    64
    }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    65
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    66
    private boolean updatePaths(Path repo) throws IOException {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    67
        if (repo == null) {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    68
            repo = Repository.getRepository().getRepositoryPath().toPath();
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    69
        }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    70
        boolean foundNew = false;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    71
        List<Path> added = new ArrayList<>();
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    72
        Set<Path> current = new HashSet<>();
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    73
        if (!Files.exists(repo)) {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    74
            // Repository removed, probably due to shutdown
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    75
            return true;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    76
        }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    77
        try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(repo, "*.jfr")) {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    78
            for (Path p : dirStream) {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    79
                if (!pathLookup.containsKey(p)) {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    80
                    added.add(p);
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    81
                    Logger.log(LogTag.JFR_SYSTEM_STREAMING, LogLevel.DEBUG, "New file found: " + p.toAbsolutePath());
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    82
                }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    83
                current.add(p);
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    84
            }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    85
        }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    86
        List<Path> removed = new ArrayList<>();
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    87
        for (Path p : pathLookup.keySet()) {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    88
            if (!current.contains(p)) {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    89
                removed.add(p);
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    90
            }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    91
        }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    92
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    93
        for (Path remove : removed) {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    94
            Long time = pathLookup.get(remove);
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    95
            pathSet.remove(time);
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    96
            pathLookup.remove(remove);
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    97
        }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    98
        Collections.sort(added, (p1, p2) -> p1.compareTo(p2));
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    99
        for (Path p : added) {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   100
            // Only add files that have a complete header
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   101
            // as the JVM may be in progress writing the file
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   102
            long size = Files.size(p);
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   103
            if (size >= ChunkHeader.HEADER_SIZE) {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   104
                long startNanos = readStartTime(p);
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   105
                pathSet.put(startNanos, p);
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   106
                pathLookup.put(p, startNanos);
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   107
                foundNew = true;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   108
            }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   109
        }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   110
        return foundNew;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   111
    }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   112
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   113
    private long readStartTime(Path p) throws IOException {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   114
        try (RecordingInput in = new RecordingInput(p.toFile(), 100)) {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   115
            ChunkHeader c = new ChunkHeader(in);
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   116
            return c.getStartNanos();
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   117
        }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   118
    }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   119
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   120
    public void close() {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   121
        synchronized (pathSet) {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   122
            this.closed = true;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   123
            pathSet.notify();
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   124
        }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   125
    }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   126
}