Search in sources :

Example 11 with SnapshotHierarchy

use of org.gradle.internal.snapshot.SnapshotHierarchy in project gradle by gradle.

the class NonHierarchicalFileWatcherUpdater method handleVirtualFileSystemContentsChanged.

@Override
protected boolean handleVirtualFileSystemContentsChanged(Collection<FileSystemLocationSnapshot> removedSnapshots, Collection<FileSystemLocationSnapshot> addedSnapshots, SnapshotHierarchy root) {
    Map<String, Integer> changedWatchedDirectories = new HashMap<>();
    removedSnapshots.stream().filter(watchableHierarchies::shouldWatch).forEach(snapshot -> {
        String previousWatchedRoot = watchedDirectoryForSnapshot.remove(snapshot.getAbsolutePath());
        decrement(previousWatchedRoot, changedWatchedDirectories);
        snapshot.accept(new SubdirectoriesToWatchVisitor(path -> decrement(path, changedWatchedDirectories)));
    });
    addedSnapshots.stream().filter(watchableHierarchies::shouldWatch).forEach(snapshot -> {
        File directoryToWatchForRoot = SnapshotWatchedDirectoryFinder.getDirectoryToWatch(snapshot);
        String pathToWatchForRoot = directoryToWatchForRoot.getAbsolutePath();
        if (!watchableHierarchies.isInWatchableHierarchy(pathToWatchForRoot)) {
            return;
        }
        watchedDirectoryForSnapshot.put(snapshot.getAbsolutePath(), pathToWatchForRoot);
        increment(pathToWatchForRoot, changedWatchedDirectories);
        snapshot.accept(new SubdirectoriesToWatchVisitor(path -> increment(path, changedWatchedDirectories)));
    });
    if (changedWatchedDirectories.isEmpty()) {
        return false;
    }
    updateWatchedDirectories(changedWatchedDirectories);
    return true;
}
Also used : SnapshotVisitResult(org.gradle.internal.snapshot.SnapshotVisitResult) WatchingNotSupportedException(org.gradle.internal.watch.WatchingNotSupportedException) FileHierarchySet(org.gradle.internal.file.FileHierarchySet) Multiset(com.google.common.collect.Multiset) RootTrackingFileSystemSnapshotHierarchyVisitor(org.gradle.internal.snapshot.RootTrackingFileSystemSnapshotHierarchyVisitor) LoggerFactory(org.slf4j.LoggerFactory) FileWatcher(net.rubygrapefruit.platform.file.FileWatcher) HashMap(java.util.HashMap) HashSet(java.util.HashSet) HashMultiset(com.google.common.collect.HashMultiset) MissingFileSnapshot(org.gradle.internal.snapshot.MissingFileSnapshot) Map(java.util.Map) FileWatcherProbeRegistry(org.gradle.internal.watch.registry.FileWatcherProbeRegistry) Nullable(javax.annotation.Nullable) Logger(org.slf4j.Logger) ImmutableMap(com.google.common.collect.ImmutableMap) FileSystemLocationSnapshot(org.gradle.internal.snapshot.FileSystemLocationSnapshot) Collection(java.util.Collection) Set(java.util.Set) SnapshotHierarchy(org.gradle.internal.snapshot.SnapshotHierarchy) File(java.io.File) Consumer(java.util.function.Consumer) NativeException(net.rubygrapefruit.platform.NativeException) DirectorySnapshot(org.gradle.internal.snapshot.DirectorySnapshot) RegularFileSnapshot(org.gradle.internal.snapshot.RegularFileSnapshot) FileSystemLocationSnapshotTransformer(org.gradle.internal.snapshot.FileSystemLocationSnapshot.FileSystemLocationSnapshotTransformer) HashMap(java.util.HashMap) File(java.io.File)

Example 12 with SnapshotHierarchy

use of org.gradle.internal.snapshot.SnapshotHierarchy in project gradle by gradle.

the class WatchableHierarchies method removeUnwatchableContentOnBuildFinished.

@CheckReturnValue
public SnapshotHierarchy removeUnwatchableContentOnBuildFinished(SnapshotHierarchy root, Predicate<File> isWatchedHierarchy, int maximumNumberOfWatchedHierarchies, List<File> unsupportedFileSystems, Invalidator invalidator) {
    SnapshotHierarchy newRoot;
    newRoot = removeWatchedHierarchiesOverLimit(root, isWatchedHierarchy, maximumNumberOfWatchedHierarchies, invalidator);
    newRoot = removeUnwatchedSnapshots(newRoot, invalidator);
    if (!unsupportedFileSystems.isEmpty()) {
        newRoot = removeUnwatchableFileSystems(newRoot, unsupportedFileSystems, invalidator);
    }
    watchableHierarchiesSinceLastBuildFinish.clear();
    return newRoot;
}
Also used : SnapshotHierarchy(org.gradle.internal.snapshot.SnapshotHierarchy) CheckReturnValue(javax.annotation.CheckReturnValue)

Example 13 with SnapshotHierarchy

use of org.gradle.internal.snapshot.SnapshotHierarchy in project gradle by gradle.

the class WatchableHierarchies method removeWatchedHierarchiesOverLimit.

@CheckReturnValue
private SnapshotHierarchy removeWatchedHierarchiesOverLimit(SnapshotHierarchy root, Predicate<File> isWatchedHierarchy, int maximumNumberOfWatchedHierarchies, Invalidator invalidator) {
    hierarchies.removeIf(hierarchy -> !isWatchedHierarchy.test(hierarchy));
    SnapshotHierarchy result = root;
    int toRemove = hierarchies.size() - maximumNumberOfWatchedHierarchies;
    if (toRemove > 0) {
        LOGGER.info("Watching too many directories in the file system (watching {}, limit {}), dropping some state from the virtual file system", hierarchies.size(), maximumNumberOfWatchedHierarchies);
        for (int i = 0; i < toRemove; i++) {
            File locationToRemove = hierarchies.removeLast();
            result = invalidator.invalidate(locationToRemove.toString(), result);
        }
    }
    watchableFiles = buildWatchableFilesFromHierarchies(hierarchies);
    return result;
}
Also used : SnapshotHierarchy(org.gradle.internal.snapshot.SnapshotHierarchy) File(java.io.File) CheckReturnValue(javax.annotation.CheckReturnValue)

Example 14 with SnapshotHierarchy

use of org.gradle.internal.snapshot.SnapshotHierarchy in project gradle by gradle.

the class WatchingVirtualFileSystem method updateNotifyingListeners.

@Override
protected SnapshotHierarchy updateNotifyingListeners(UpdateFunction updateFunction) {
    if (watchRegistry == null) {
        return updateFunction.update(SnapshotHierarchy.NodeDiffListener.NOOP);
    } else {
        SnapshotCollectingDiffListener diffListener = new SnapshotCollectingDiffListener();
        SnapshotHierarchy newRoot = updateFunction.update(diffListener);
        return withWatcherChangeErrorHandling(newRoot, () -> diffListener.publishSnapshotDiff((removedSnapshots, addedSnapshots) -> watchRegistry.virtualFileSystemContentsChanged(removedSnapshots, addedSnapshots, newRoot)));
    }
}
Also used : BuildStartedFileSystemWatchingBuildOperationType(org.gradle.internal.watch.vfs.BuildStartedFileSystemWatchingBuildOperationType) FileSystemWatchingStatistics(org.gradle.internal.watch.vfs.FileSystemWatchingStatistics) FileWatcherRegistry(org.gradle.internal.watch.registry.FileWatcherRegistry) WatchingNotSupportedException(org.gradle.internal.watch.WatchingNotSupportedException) BuildOperationRunner(org.gradle.internal.operations.BuildOperationRunner) LoggerFactory(org.slf4j.LoggerFactory) DaemonDocumentationIndex(org.gradle.internal.watch.registry.impl.DaemonDocumentationIndex) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) WatchLogging(org.gradle.internal.watch.vfs.WatchLogging) ImmutableList(com.google.common.collect.ImmutableList) BuildFinishedFileSystemWatchingBuildOperationType(org.gradle.internal.watch.vfs.BuildFinishedFileSystemWatchingBuildOperationType) Path(java.nio.file.Path) Nullable(javax.annotation.Nullable) LinkedHashSet(java.util.LinkedHashSet) WatchableFileSystemDetector(org.gradle.internal.watch.vfs.WatchableFileSystemDetector) FileSystemNode(org.gradle.internal.snapshot.FileSystemNode) Logger(org.slf4j.Logger) VfsRootReference(org.gradle.internal.vfs.impl.VfsRootReference) WatchMode(org.gradle.internal.watch.registry.WatchMode) SnapshotCollectingDiffListener(org.gradle.internal.watch.registry.impl.SnapshotCollectingDiffListener) Set(java.util.Set) CallableBuildOperation(org.gradle.internal.operations.CallableBuildOperation) IOException(java.io.IOException) BuildOperationDescriptor(org.gradle.internal.operations.BuildOperationDescriptor) SnapshotHierarchy(org.gradle.internal.snapshot.SnapshotHierarchy) BuildLifecycleAwareVirtualFileSystem(org.gradle.internal.watch.vfs.BuildLifecycleAwareVirtualFileSystem) FileSystemWatchingInformation(org.gradle.internal.watch.vfs.FileSystemWatchingInformation) File(java.io.File) InotifyWatchesLimitTooLowException(net.rubygrapefruit.platform.internal.jni.InotifyWatchesLimitTooLowException) CheckReturnValue(javax.annotation.CheckReturnValue) List(java.util.List) FileChangeListeners(org.gradle.internal.watch.vfs.FileChangeListeners) VfsLogging(org.gradle.internal.watch.vfs.VfsLogging) NativeException(net.rubygrapefruit.platform.NativeException) FileWatcherRegistryFactory(org.gradle.internal.watch.registry.FileWatcherRegistryFactory) Closeable(java.io.Closeable) BuildOperationContext(org.gradle.internal.operations.BuildOperationContext) AbstractVirtualFileSystem(org.gradle.internal.vfs.impl.AbstractVirtualFileSystem) InotifyInstanceLimitTooLowException(net.rubygrapefruit.platform.internal.jni.InotifyInstanceLimitTooLowException) SnapshotCollectingDiffListener(org.gradle.internal.watch.registry.impl.SnapshotCollectingDiffListener) SnapshotHierarchy(org.gradle.internal.snapshot.SnapshotHierarchy)

Example 15 with SnapshotHierarchy

use of org.gradle.internal.snapshot.SnapshotHierarchy in project gradle by gradle.

the class WatchingVirtualFileSystem method afterBuildStarted.

@Override
public boolean afterBuildStarted(WatchMode watchMode, VfsLogging vfsLogging, WatchLogging watchLogging, BuildOperationRunner buildOperationRunner) {
    warningLogger = watchMode.loggerForWarnings(LOGGER);
    stateInvalidatedAtStartOfBuild = false;
    reasonForNotWatchingFiles = null;
    rootReference.update(currentRoot -> buildOperationRunner.call(new CallableBuildOperation<SnapshotHierarchy>() {

        @Override
        public SnapshotHierarchy call(BuildOperationContext context) {
            if (watchMode.isEnabled()) {
                SnapshotHierarchy newRoot;
                boolean couldDetectUnsupportedFileSystems;
                try {
                    unsupportedFileSystems.clear();
                    if (watchMode == WatchMode.DEFAULT) {
                        watchableFileSystemDetector.detectUnsupportedFileSystems().forEach(unsupportedFileSystems::add);
                    }
                    couldDetectUnsupportedFileSystems = true;
                } catch (NativeException e) {
                    couldDetectUnsupportedFileSystems = false;
                    LOGGER.info("Unable to list file systems to check whether they can be watched. Disabling watching. Reason: {}", e.getMessage());
                }
                FileSystemWatchingStatistics statisticsSinceLastBuild;
                if (watchRegistry == null) {
                    if (couldDetectUnsupportedFileSystems) {
                        context.setStatus("Starting file system watching");
                        newRoot = startWatching(currentRoot, watchMode, unsupportedFileSystems);
                    } else {
                        newRoot = currentRoot.empty();
                    }
                    statisticsSinceLastBuild = null;
                } else {
                    FileWatcherRegistry.FileWatchingStatistics statistics = watchRegistry.getAndResetStatistics();
                    if (hasDroppedStateBecauseOfErrorsReceivedWhileWatching(statistics) || !couldDetectUnsupportedFileSystems) {
                        newRoot = stopWatchingAndInvalidateHierarchyAfterError(currentRoot);
                    } else {
                        newRoot = watchRegistry.updateVfsOnBuildStarted(currentRoot, watchMode, unsupportedFileSystems);
                    }
                    stateInvalidatedAtStartOfBuild = newRoot != currentRoot;
                    statisticsSinceLastBuild = new DefaultFileSystemWatchingStatistics(statistics, newRoot);
                    if (vfsLogging == VfsLogging.VERBOSE) {
                        LOGGER.warn("Received {} file system events since last build while watching {} locations", statisticsSinceLastBuild.getNumberOfReceivedEvents(), statisticsSinceLastBuild.getNumberOfWatchedHierarchies());
                        LOGGER.warn("Virtual file system retained information about {} files, {} directories and {} missing files since last build", statisticsSinceLastBuild.getRetainedRegularFiles(), statisticsSinceLastBuild.getRetainedDirectories(), statisticsSinceLastBuild.getRetainedMissingFiles());
                        if (stateInvalidatedAtStartOfBuild) {
                            LOGGER.warn("Parts of the virtual file system have been invalidated since they didn't support watching");
                        }
                    }
                }
                if (watchRegistry != null) {
                    watchRegistry.setDebugLoggingEnabled(watchLogging == WatchLogging.DEBUG);
                }
                context.setResult(new BuildStartedFileSystemWatchingBuildOperationType.Result() {

                    @Override
                    public boolean isWatchingEnabled() {
                        return true;
                    }

                    @Override
                    public boolean isStartedWatching() {
                        return statisticsSinceLastBuild == null;
                    }

                    @Override
                    public FileSystemWatchingStatistics getStatistics() {
                        return statisticsSinceLastBuild;
                    }
                });
                return newRoot;
            } else {
                context.setResult(BuildStartedFileSystemWatchingBuildOperationType.Result.WATCHING_DISABLED);
                return stopWatchingAndInvalidateHierarchy(currentRoot);
            }
        }

        @Override
        public BuildOperationDescriptor.Builder description() {
            return BuildOperationDescriptor.displayName(BuildStartedFileSystemWatchingBuildOperationType.DISPLAY_NAME).details(BuildStartedFileSystemWatchingBuildOperationType.Details.INSTANCE);
        }
    }));
    return watchRegistry != null;
}
Also used : BuildOperationContext(org.gradle.internal.operations.BuildOperationContext) FileSystemWatchingStatistics(org.gradle.internal.watch.vfs.FileSystemWatchingStatistics) BuildOperationDescriptor(org.gradle.internal.operations.BuildOperationDescriptor) BuildStartedFileSystemWatchingBuildOperationType(org.gradle.internal.watch.vfs.BuildStartedFileSystemWatchingBuildOperationType) SnapshotHierarchy(org.gradle.internal.snapshot.SnapshotHierarchy) CallableBuildOperation(org.gradle.internal.operations.CallableBuildOperation) FileWatcherRegistry(org.gradle.internal.watch.registry.FileWatcherRegistry) NativeException(net.rubygrapefruit.platform.NativeException)

Aggregations

SnapshotHierarchy (org.gradle.internal.snapshot.SnapshotHierarchy)15 CheckReturnValue (javax.annotation.CheckReturnValue)8 File (java.io.File)6 NativeException (net.rubygrapefruit.platform.NativeException)4 Logger (org.slf4j.Logger)4 LoggerFactory (org.slf4j.LoggerFactory)4 FileHierarchySet (org.gradle.internal.file.FileHierarchySet)3 BuildOperationContext (org.gradle.internal.operations.BuildOperationContext)3 BuildOperationDescriptor (org.gradle.internal.operations.BuildOperationDescriptor)3 CallableBuildOperation (org.gradle.internal.operations.CallableBuildOperation)3 FileSystemLocationSnapshot (org.gradle.internal.snapshot.FileSystemLocationSnapshot)3 WatchingNotSupportedException (org.gradle.internal.watch.WatchingNotSupportedException)3 FileWatcherRegistry (org.gradle.internal.watch.registry.FileWatcherRegistry)3 FileSystemWatchingStatistics (org.gradle.internal.watch.vfs.FileSystemWatchingStatistics)3 IOException (java.io.IOException)2 Collection (java.util.Collection)2 List (java.util.List)2 Set (java.util.Set)2 Nullable (javax.annotation.Nullable)2 InotifyInstanceLimitTooLowException (net.rubygrapefruit.platform.internal.jni.InotifyInstanceLimitTooLowException)2