src/jdk.jfr/share/classes/jdk/jfr/internal/consumer/RepositoryFiles.java
author egahlin
Wed, 03 Jul 2019 22:51:44 +0200
branchJEP-349-branch
changeset 57449 099789ceff7d
parent 57434 216bf2e3b542
child 57604 838f9a7635b6
permissions -rw-r--r--
Provide implementation for all added API methods
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
57434
216bf2e3b542 Add support for EventStrem::openRepository
egahlin
parents: 57433
diff changeset
     1
/*
216bf2e3b542 Add support for EventStrem::openRepository
egahlin
parents: 57433
diff changeset
     2
 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
216bf2e3b542 Add support for EventStrem::openRepository
egahlin
parents: 57433
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
216bf2e3b542 Add support for EventStrem::openRepository
egahlin
parents: 57433
diff changeset
     4
 *
216bf2e3b542 Add support for EventStrem::openRepository
egahlin
parents: 57433
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
216bf2e3b542 Add support for EventStrem::openRepository
egahlin
parents: 57433
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
216bf2e3b542 Add support for EventStrem::openRepository
egahlin
parents: 57433
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
216bf2e3b542 Add support for EventStrem::openRepository
egahlin
parents: 57433
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
216bf2e3b542 Add support for EventStrem::openRepository
egahlin
parents: 57433
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
216bf2e3b542 Add support for EventStrem::openRepository
egahlin
parents: 57433
diff changeset
    10
 *
216bf2e3b542 Add support for EventStrem::openRepository
egahlin
parents: 57433
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
216bf2e3b542 Add support for EventStrem::openRepository
egahlin
parents: 57433
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
216bf2e3b542 Add support for EventStrem::openRepository
egahlin
parents: 57433
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
216bf2e3b542 Add support for EventStrem::openRepository
egahlin
parents: 57433
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
216bf2e3b542 Add support for EventStrem::openRepository
egahlin
parents: 57433
diff changeset
    15
 * accompanied this code).
216bf2e3b542 Add support for EventStrem::openRepository
egahlin
parents: 57433
diff changeset
    16
 *
216bf2e3b542 Add support for EventStrem::openRepository
egahlin
parents: 57433
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
216bf2e3b542 Add support for EventStrem::openRepository
egahlin
parents: 57433
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
216bf2e3b542 Add support for EventStrem::openRepository
egahlin
parents: 57433
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
216bf2e3b542 Add support for EventStrem::openRepository
egahlin
parents: 57433
diff changeset
    20
 *
216bf2e3b542 Add support for EventStrem::openRepository
egahlin
parents: 57433
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
216bf2e3b542 Add support for EventStrem::openRepository
egahlin
parents: 57433
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
216bf2e3b542 Add support for EventStrem::openRepository
egahlin
parents: 57433
diff changeset
    23
 * questions.
216bf2e3b542 Add support for EventStrem::openRepository
egahlin
parents: 57433
diff changeset
    24
 */
57433
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    25
package jdk.jfr.internal.consumer;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    26
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    27
import java.io.IOException;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    28
import java.nio.file.DirectoryStream;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    29
import java.nio.file.Files;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    30
import java.nio.file.Path;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    31
import java.util.ArrayList;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    32
import java.util.Collections;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    33
import java.util.HashMap;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    34
import java.util.HashSet;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    35
import java.util.List;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    36
import java.util.Map;
57449
099789ceff7d Provide implementation for all added API methods
egahlin
parents: 57434
diff changeset
    37
import java.util.Map.Entry;
099789ceff7d Provide implementation for all added API methods
egahlin
parents: 57434
diff changeset
    38
import java.util.NavigableMap;
57433
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    39
import java.util.Set;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    40
import java.util.SortedMap;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    41
import java.util.TreeMap;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    42
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    43
import jdk.jfr.internal.LogLevel;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    44
import jdk.jfr.internal.LogTag;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    45
import jdk.jfr.internal.Logger;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    46
import jdk.jfr.internal.Repository;
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
public final class RepositoryFiles {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    49
    private final Path repostory;
57449
099789ceff7d Provide implementation for all added API methods
egahlin
parents: 57434
diff changeset
    50
    private final NavigableMap<Long, Path> pathSet = new TreeMap<>();
57433
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    51
    private final Map<Path, Long> pathLookup = new HashMap<>();
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    52
    private volatile boolean closed;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    53
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    54
    public RepositoryFiles(Path repostory) {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    55
        this.repostory = repostory;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    56
    }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    57
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    58
    public long getTimestamp(Path p) {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    59
        return pathLookup.get(p);
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    60
    }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    61
57449
099789ceff7d Provide implementation for all added API methods
egahlin
parents: 57434
diff changeset
    62
    public Path lastPath() {
099789ceff7d Provide implementation for all added API methods
egahlin
parents: 57434
diff changeset
    63
        return nextPath(-1);
099789ceff7d Provide implementation for all added API methods
egahlin
parents: 57434
diff changeset
    64
    }
099789ceff7d Provide implementation for all added API methods
egahlin
parents: 57434
diff changeset
    65
57433
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    66
    public Path nextPath(long startTimeNanos) {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    67
        while (!closed) {
57449
099789ceff7d Provide implementation for all added API methods
egahlin
parents: 57434
diff changeset
    68
            if (startTimeNanos == -1) {
099789ceff7d Provide implementation for all added API methods
egahlin
parents: 57434
diff changeset
    69
                Entry<Long, Path> e =  pathSet.lastEntry();
099789ceff7d Provide implementation for all added API methods
egahlin
parents: 57434
diff changeset
    70
                if (e != null) {
099789ceff7d Provide implementation for all added API methods
egahlin
parents: 57434
diff changeset
    71
                    return e.getValue();
099789ceff7d Provide implementation for all added API methods
egahlin
parents: 57434
diff changeset
    72
                }
099789ceff7d Provide implementation for all added API methods
egahlin
parents: 57434
diff changeset
    73
            }
57433
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    74
            SortedMap<Long, Path> after = pathSet.tailMap(startTimeNanos);
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    75
            if (!after.isEmpty()) {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    76
                Path path = after.get(after.firstKey());
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    77
                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
    78
                return path;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    79
            }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    80
            try {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    81
                if (updatePaths(repostory)) {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    82
                    continue;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    83
                }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    84
            } catch (IOException e) {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    85
                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
    86
                // This can happen if a chunk is being removed
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    87
                // between the file was discovered and an instance
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    88
                // of an EventSet was constructed. Just ignore,
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    89
                // and retry later.
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
            try {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    92
                synchronized (pathSet) {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    93
                    pathSet.wait(1000);
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    94
                }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    95
            } catch (InterruptedException e) {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    96
                // ignore
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
        }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    99
        return null;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   100
    }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   101
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   102
    private boolean updatePaths(Path repo) throws IOException {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   103
        if (repo == null) {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   104
            repo = Repository.getRepository().getRepositoryPath().toPath();
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   105
        }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   106
        boolean foundNew = false;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   107
        List<Path> added = new ArrayList<>();
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   108
        Set<Path> current = new HashSet<>();
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   109
        if (!Files.exists(repo)) {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   110
            // Repository removed, probably due to shutdown
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   111
            return true;
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
        try (DirectoryStream<Path> dirStream = Files.newDirectoryStream(repo, "*.jfr")) {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   114
            for (Path p : dirStream) {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   115
                if (!pathLookup.containsKey(p)) {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   116
                    added.add(p);
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   117
                    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
   118
                }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   119
                current.add(p);
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   120
            }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   121
        }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   122
        List<Path> removed = new ArrayList<>();
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   123
        for (Path p : pathLookup.keySet()) {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   124
            if (!current.contains(p)) {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   125
                removed.add(p);
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   126
            }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   127
        }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   128
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   129
        for (Path remove : removed) {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   130
            Long time = pathLookup.get(remove);
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   131
            pathSet.remove(time);
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   132
            pathLookup.remove(remove);
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   133
        }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   134
        Collections.sort(added, (p1, p2) -> p1.compareTo(p2));
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   135
        for (Path p : added) {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   136
            // Only add files that have a complete header
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   137
            // as the JVM may be in progress writing the file
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   138
            long size = Files.size(p);
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   139
            if (size >= ChunkHeader.HEADER_SIZE) {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   140
                long startNanos = readStartTime(p);
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   141
                pathSet.put(startNanos, p);
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   142
                pathLookup.put(p, startNanos);
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   143
                foundNew = true;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   144
            }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   145
        }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   146
        return foundNew;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   147
    }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   148
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   149
    private long readStartTime(Path p) throws IOException {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   150
        try (RecordingInput in = new RecordingInput(p.toFile(), 100)) {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   151
            ChunkHeader c = new ChunkHeader(in);
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   152
            return c.getStartNanos();
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   153
        }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   154
    }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   155
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   156
    public void close() {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   157
        synchronized (pathSet) {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   158
            this.closed = true;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   159
            pathSet.notify();
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   160
        }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   161
    }
57449
099789ceff7d Provide implementation for all added API methods
egahlin
parents: 57434
diff changeset
   162
099789ceff7d Provide implementation for all added API methods
egahlin
parents: 57434
diff changeset
   163
57433
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   164
}