src/jdk.jfr/share/classes/jdk/jfr/internal/consumer/RepositoryFiles.java
author egahlin
Mon, 21 Oct 2019 18:25:56 +0200
branchJEP-349-branch
changeset 58719 be37d9dc96d1
parent 58200 2d147d680311
child 58806 a7d850b47b19
permissions -rw-r--r--
Add logging for parsing chunk start time
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
 */
58200
2d147d680311 Cleaning up
egahlin
parents: 58197
diff changeset
    25
57433
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    26
package jdk.jfr.internal.consumer;
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
import java.io.IOException;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    29
import java.nio.file.DirectoryStream;
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.NavigableMap;
57433
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    38
import java.util.Set;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    39
import java.util.SortedMap;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    40
import java.util.TreeMap;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    41
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    42
import jdk.jfr.internal.LogLevel;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    43
import jdk.jfr.internal.LogTag;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    44
import jdk.jfr.internal.Logger;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    45
import jdk.jfr.internal.Repository;
57628
f5f590eaecf5 Fix EventStream:setStartTime
egahlin
parents: 57604
diff changeset
    46
import jdk.jfr.internal.SecuritySupport.SafePath;
57433
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 {
57946
b77540d274f9 Add mechanism för new file notification
egahlin
parents: 57920
diff changeset
    49
    private static final Object WAIT_OBJECT = new Object();
b77540d274f9 Add mechanism för new file notification
egahlin
parents: 57920
diff changeset
    50
    public static void notifyNewFile() {
b77540d274f9 Add mechanism för new file notification
egahlin
parents: 57920
diff changeset
    51
        synchronized (WAIT_OBJECT) {
b77540d274f9 Add mechanism för new file notification
egahlin
parents: 57920
diff changeset
    52
            WAIT_OBJECT.notifyAll();
b77540d274f9 Add mechanism för new file notification
egahlin
parents: 57920
diff changeset
    53
        }
b77540d274f9 Add mechanism för new file notification
egahlin
parents: 57920
diff changeset
    54
    }
b77540d274f9 Add mechanism för new file notification
egahlin
parents: 57920
diff changeset
    55
57690
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57640
diff changeset
    56
    private final FileAccess fileAccess;
57449
099789ceff7d Provide implementation for all added API methods
egahlin
parents: 57434
diff changeset
    57
    private final NavigableMap<Long, Path> pathSet = new TreeMap<>();
57433
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    58
    private final Map<Path, Long> pathLookup = new HashMap<>();
57754
5693904ecbde Add streaming support for repository migration
egahlin
parents: 57717
diff changeset
    59
    private final Path repository;
58200
2d147d680311 Cleaning up
egahlin
parents: 58197
diff changeset
    60
    private final Object waitObject;
2d147d680311 Cleaning up
egahlin
parents: 58197
diff changeset
    61
57433
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    62
    private volatile boolean closed;
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    63
58197
0ef79bd7fb5c Remove unused methods etc.
egahlin
parents: 57946
diff changeset
    64
    RepositoryFiles(FileAccess fileAccess, Path repository) {
57717
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents: 57690
diff changeset
    65
        this.repository = repository;
57690
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57640
diff changeset
    66
        this.fileAccess = fileAccess;
57946
b77540d274f9 Add mechanism för new file notification
egahlin
parents: 57920
diff changeset
    67
        this.waitObject = repository == null ? WAIT_OBJECT : new Object();
57433
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    68
    }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    69
58197
0ef79bd7fb5c Remove unused methods etc.
egahlin
parents: 57946
diff changeset
    70
    long getTimestamp(Path p) {
57433
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    71
        return pathLookup.get(p);
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    72
    }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
    73
58197
0ef79bd7fb5c Remove unused methods etc.
egahlin
parents: 57946
diff changeset
    74
    Path lastPath() {
57717
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents: 57690
diff changeset
    75
        if (waitForPaths()) {
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents: 57690
diff changeset
    76
            return pathSet.lastEntry().getValue();
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents: 57690
diff changeset
    77
        }
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents: 57690
diff changeset
    78
        return null; // closed
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents: 57690
diff changeset
    79
    }
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents: 57690
diff changeset
    80
58197
0ef79bd7fb5c Remove unused methods etc.
egahlin
parents: 57946
diff changeset
    81
    Path firstPath(long startTimeNanos) {
57717
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents: 57690
diff changeset
    82
        if (waitForPaths()) {
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents: 57690
diff changeset
    83
            // Pick closest chunk before timestamp
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents: 57690
diff changeset
    84
            Long time = pathSet.floorKey(startTimeNanos);
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents: 57690
diff changeset
    85
            if (time != null) {
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents: 57690
diff changeset
    86
                startTimeNanos = time;
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents: 57690
diff changeset
    87
            }
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents: 57690
diff changeset
    88
            return path(startTimeNanos);
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents: 57690
diff changeset
    89
        }
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents: 57690
diff changeset
    90
        return null; // closed
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents: 57690
diff changeset
    91
    }
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents: 57690
diff changeset
    92
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents: 57690
diff changeset
    93
    private boolean waitForPaths() {
57640
46a77fccd251 Fix first(...), next(...) and last() path from RepositoryFiles
egahlin
parents: 57638
diff changeset
    94
        while (!closed) {
46a77fccd251 Fix first(...), next(...) and last() path from RepositoryFiles
egahlin
parents: 57638
diff changeset
    95
            try {
57690
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57640
diff changeset
    96
                if (updatePaths()) {
57640
46a77fccd251 Fix first(...), next(...) and last() path from RepositoryFiles
egahlin
parents: 57638
diff changeset
    97
                    break;
46a77fccd251 Fix first(...), next(...) and last() path from RepositoryFiles
egahlin
parents: 57638
diff changeset
    98
                }
46a77fccd251 Fix first(...), next(...) and last() path from RepositoryFiles
egahlin
parents: 57638
diff changeset
    99
            } catch (IOException e) {
57717
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents: 57690
diff changeset
   100
                Logger.log(LogTag.JFR_SYSTEM_STREAMING, LogLevel.DEBUG, "IOException during repository file scan " + e.getMessage());
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents: 57690
diff changeset
   101
                // This can happen if a chunk is being removed
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents: 57690
diff changeset
   102
                // between the file was discovered and an instance
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents: 57690
diff changeset
   103
                // was accessed, or if new file has been written yet
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents: 57690
diff changeset
   104
                // Just ignore, and retry later.
57640
46a77fccd251 Fix first(...), next(...) and last() path from RepositoryFiles
egahlin
parents: 57638
diff changeset
   105
            }
57717
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents: 57690
diff changeset
   106
            nap();
57640
46a77fccd251 Fix first(...), next(...) and last() path from RepositoryFiles
egahlin
parents: 57638
diff changeset
   107
        }
57717
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents: 57690
diff changeset
   108
        return !closed;
57449
099789ceff7d Provide implementation for all added API methods
egahlin
parents: 57434
diff changeset
   109
    }
099789ceff7d Provide implementation for all added API methods
egahlin
parents: 57434
diff changeset
   110
58197
0ef79bd7fb5c Remove unused methods etc.
egahlin
parents: 57946
diff changeset
   111
    Path nextPath(long startTimeNanos) {
57717
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents: 57690
diff changeset
   112
        return path(startTimeNanos);
57640
46a77fccd251 Fix first(...), next(...) and last() path from RepositoryFiles
egahlin
parents: 57638
diff changeset
   113
    }
46a77fccd251 Fix first(...), next(...) and last() path from RepositoryFiles
egahlin
parents: 57638
diff changeset
   114
57717
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents: 57690
diff changeset
   115
    private Path path(long timestamp) {
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents: 57690
diff changeset
   116
        if (closed) {
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents: 57690
diff changeset
   117
            return null;
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents: 57690
diff changeset
   118
        }
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents: 57690
diff changeset
   119
        while (true) {
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents: 57690
diff changeset
   120
            SortedMap<Long, Path> after = pathSet.tailMap(timestamp);
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents: 57690
diff changeset
   121
            if (!after.isEmpty()) {
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents: 57690
diff changeset
   122
                Path path = after.get(after.firstKey());
57920
de358b994ad9 Reduce allocation from logging
egahlin
parents: 57754
diff changeset
   123
                if (Logger.shouldLog(LogTag.JFR_SYSTEM_STREAMING, LogLevel.TRACE)) {
de358b994ad9 Reduce allocation from logging
egahlin
parents: 57754
diff changeset
   124
                    Logger.log(LogTag.JFR_SYSTEM_STREAMING, LogLevel.TRACE, "Return path " + path + " for start time nanos " + timestamp);
de358b994ad9 Reduce allocation from logging
egahlin
parents: 57754
diff changeset
   125
                }
57717
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents: 57690
diff changeset
   126
                return path;
57433
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   127
            }
57717
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents: 57690
diff changeset
   128
            if (!waitForPaths()) {
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents: 57690
diff changeset
   129
                return null; // closed
57433
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   130
            }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   131
        }
57717
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents: 57690
diff changeset
   132
    }
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents: 57690
diff changeset
   133
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents: 57690
diff changeset
   134
    private void nap() {
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents: 57690
diff changeset
   135
        try {
57946
b77540d274f9 Add mechanism för new file notification
egahlin
parents: 57920
diff changeset
   136
            synchronized (waitObject) {
b77540d274f9 Add mechanism för new file notification
egahlin
parents: 57920
diff changeset
   137
                waitObject.wait(1000);
57717
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents: 57690
diff changeset
   138
            }
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents: 57690
diff changeset
   139
        } catch (InterruptedException e) {
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents: 57690
diff changeset
   140
            // ignore
4ce66d271065 Security handling, but two tests fails with driver
egahlin
parents: 57690
diff changeset
   141
        }
57433
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   142
    }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   143
57690
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57640
diff changeset
   144
    private boolean updatePaths() throws IOException {
57754
5693904ecbde Add streaming support for repository migration
egahlin
parents: 57717
diff changeset
   145
        boolean foundNew = false;
5693904ecbde Add streaming support for repository migration
egahlin
parents: 57717
diff changeset
   146
        Path repoPath = repository;
5693904ecbde Add streaming support for repository migration
egahlin
parents: 57717
diff changeset
   147
        if (repoPath == null) {
5693904ecbde Add streaming support for repository migration
egahlin
parents: 57717
diff changeset
   148
            // Always get the latest repository if 'jcmd JFR.configure
5693904ecbde Add streaming support for repository migration
egahlin
parents: 57717
diff changeset
   149
            // repositorypath=...' has been executed
5693904ecbde Add streaming support for repository migration
egahlin
parents: 57717
diff changeset
   150
            SafePath sf = Repository.getRepository().getRepositoryPath();
5693904ecbde Add streaming support for repository migration
egahlin
parents: 57717
diff changeset
   151
            if (sf == null) {
5693904ecbde Add streaming support for repository migration
egahlin
parents: 57717
diff changeset
   152
                return false; // not initialized
57690
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57640
diff changeset
   153
            }
57754
5693904ecbde Add streaming support for repository migration
egahlin
parents: 57717
diff changeset
   154
            repoPath = sf.toPath();
57433
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   155
        }
57754
5693904ecbde Add streaming support for repository migration
egahlin
parents: 57717
diff changeset
   156
5693904ecbde Add streaming support for repository migration
egahlin
parents: 57717
diff changeset
   157
        try (DirectoryStream<Path> dirStream = fileAccess.newDirectoryStream(repoPath)) {
5693904ecbde Add streaming support for repository migration
egahlin
parents: 57717
diff changeset
   158
            List<Path> added = new ArrayList<>();
5693904ecbde Add streaming support for repository migration
egahlin
parents: 57717
diff changeset
   159
            Set<Path> current = new HashSet<>();
57433
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   160
            for (Path p : dirStream) {
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   161
                if (!pathLookup.containsKey(p)) {
57604
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents: 57449
diff changeset
   162
                    String s = p.toString();
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents: 57449
diff changeset
   163
                    if (s.endsWith(".jfr")) {
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents: 57449
diff changeset
   164
                        added.add(p);
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents: 57449
diff changeset
   165
                        Logger.log(LogTag.JFR_SYSTEM_STREAMING, LogLevel.DEBUG, "New file found: " + p.toAbsolutePath());
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents: 57449
diff changeset
   166
                    }
838f9a7635b6 Cleaner stream reconfiguration + reduced allocation in JFR framework
egahlin
parents: 57449
diff changeset
   167
                    current.add(p);
57433
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   168
                }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   169
            }
57754
5693904ecbde Add streaming support for repository migration
egahlin
parents: 57717
diff changeset
   170
            List<Path> removed = new ArrayList<>();
5693904ecbde Add streaming support for repository migration
egahlin
parents: 57717
diff changeset
   171
            for (Path p : pathLookup.keySet()) {
5693904ecbde Add streaming support for repository migration
egahlin
parents: 57717
diff changeset
   172
                if (!current.contains(p)) {
5693904ecbde Add streaming support for repository migration
egahlin
parents: 57717
diff changeset
   173
                    removed.add(p);
5693904ecbde Add streaming support for repository migration
egahlin
parents: 57717
diff changeset
   174
                }
5693904ecbde Add streaming support for repository migration
egahlin
parents: 57717
diff changeset
   175
            }
5693904ecbde Add streaming support for repository migration
egahlin
parents: 57717
diff changeset
   176
5693904ecbde Add streaming support for repository migration
egahlin
parents: 57717
diff changeset
   177
            for (Path remove : removed) {
5693904ecbde Add streaming support for repository migration
egahlin
parents: 57717
diff changeset
   178
                Long time = pathLookup.get(remove);
5693904ecbde Add streaming support for repository migration
egahlin
parents: 57717
diff changeset
   179
                pathSet.remove(time);
5693904ecbde Add streaming support for repository migration
egahlin
parents: 57717
diff changeset
   180
                pathLookup.remove(remove);
57433
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   181
            }
57754
5693904ecbde Add streaming support for repository migration
egahlin
parents: 57717
diff changeset
   182
            Collections.sort(added, (p1, p2) -> p1.compareTo(p2));
5693904ecbde Add streaming support for repository migration
egahlin
parents: 57717
diff changeset
   183
            for (Path p : added) {
5693904ecbde Add streaming support for repository migration
egahlin
parents: 57717
diff changeset
   184
                // Only add files that have a complete header
5693904ecbde Add streaming support for repository migration
egahlin
parents: 57717
diff changeset
   185
                // as the JVM may be in progress writing the file
5693904ecbde Add streaming support for repository migration
egahlin
parents: 57717
diff changeset
   186
                long size = fileAccess.fileSize(p);
58197
0ef79bd7fb5c Remove unused methods etc.
egahlin
parents: 57946
diff changeset
   187
                if (size >= ChunkHeader.headerSize()) {
57754
5693904ecbde Add streaming support for repository migration
egahlin
parents: 57717
diff changeset
   188
                    long startNanos = readStartTime(p);
5693904ecbde Add streaming support for repository migration
egahlin
parents: 57717
diff changeset
   189
                    pathSet.put(startNanos, p);
5693904ecbde Add streaming support for repository migration
egahlin
parents: 57717
diff changeset
   190
                    pathLookup.put(p, startNanos);
5693904ecbde Add streaming support for repository migration
egahlin
parents: 57717
diff changeset
   191
                    foundNew = true;
5693904ecbde Add streaming support for repository migration
egahlin
parents: 57717
diff changeset
   192
                }
5693904ecbde Add streaming support for repository migration
egahlin
parents: 57717
diff changeset
   193
            }
5693904ecbde Add streaming support for repository migration
egahlin
parents: 57717
diff changeset
   194
            return foundNew;
57433
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   195
        }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   196
    }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   197
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   198
    private long readStartTime(Path p) throws IOException {
57690
9316d02dd4a5 Add EventStream::setEndTime(...) and a first stab at priviliged access to local repository
egahlin
parents: 57640
diff changeset
   199
        try (RecordingInput in = new RecordingInput(p.toFile(), fileAccess, 100)) {
58719
be37d9dc96d1 Add logging for parsing chunk start time
egahlin
parents: 58200
diff changeset
   200
            Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.INFO, "Parsing header for chunk start time");
57433
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   201
            ChunkHeader c = new ChunkHeader(in);
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   202
            return c.getStartNanos();
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   203
        }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   204
    }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   205
58197
0ef79bd7fb5c Remove unused methods etc.
egahlin
parents: 57946
diff changeset
   206
    void close() {
57946
b77540d274f9 Add mechanism för new file notification
egahlin
parents: 57920
diff changeset
   207
        synchronized (waitObject) {
57433
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   208
            this.closed = true;
57946
b77540d274f9 Add mechanism för new file notification
egahlin
parents: 57920
diff changeset
   209
            waitObject.notify();
57433
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   210
        }
83e4343a6984 Clean up and fix parser level filtering
egahlin
parents:
diff changeset
   211
    }
57946
b77540d274f9 Add mechanism för new file notification
egahlin
parents: 57920
diff changeset
   212
}