Search in sources :

Example 6 with AutoCloseableLock

use of com.facebook.buck.util.concurrent.AutoCloseableLock in project buck by facebook.

the class DaemonicParserState method invalidateIfProjectBuildFileParserStateChanged.

private void invalidateIfProjectBuildFileParserStateChanged(Cell cell) {
    Iterable<String> defaultIncludes = cell.getBuckConfig().getView(ParserConfig.class).getDefaultIncludes();
    boolean invalidatedByDefaultIncludesChange = false;
    Iterable<String> expected;
    try (AutoCloseableLock readLock = cachedStateLock.readLock()) {
        expected = cachedIncludes.get(cell.getRoot());
        if (expected == null || !Iterables.elementsEqual(defaultIncludes, expected)) {
            // Someone's changed the default includes. That's almost definitely caused all our lovingly
            // cached data to be enormously wonky.
            invalidatedByDefaultIncludesChange = true;
        }
        if (!invalidatedByDefaultIncludesChange) {
            return;
        }
    }
    try (AutoCloseableLock writeLock = cachedStateLock.writeLock()) {
        cachedIncludes.put(cell.getRoot(), defaultIncludes);
    }
    synchronized (this) {
        if (invalidateCellCaches(cell) && invalidatedByDefaultIncludesChange) {
            LOG.warn("Invalidating cache on default includes change (%s != %s)", expected, defaultIncludes);
            cacheInvalidatedByDefaultIncludesChangeCounter.inc();
        }
    }
}
Also used : AutoCloseableLock(com.facebook.buck.util.concurrent.AutoCloseableLock)

Example 7 with AutoCloseableLock

use of com.facebook.buck.util.concurrent.AutoCloseableLock in project buck by facebook.

the class DaemonicCellState method invalidatePath.

int invalidatePath(Path path) {
    try (AutoCloseableLock writeLock = rawAndComputedNodesLock.writeLock()) {
        int invalidatedRawNodes = 0;
        ImmutableSet<Map<String, Object>> rawNodes = allRawNodes.getIfPresent(path);
        if (rawNodes != null) {
            // Increment the counter
            invalidatedRawNodes = rawNodes.size();
            for (Map<String, Object> rawNode : rawNodes) {
                UnflavoredBuildTarget target = RawNodeParsePipeline.parseBuildTargetFromRawRule(cellRoot, rawNode, path);
                LOG.debug("Invalidating target for path %s: %s", path, target);
                for (CacheImpl<?> cache : typedNodeCaches.values()) {
                    cache.allComputedNodes.invalidateAll(targetsCornucopia.get(target));
                }
                targetsCornucopia.removeAll(target);
            }
            allRawNodes.invalidate(path);
        }
        // We may have been given a file that other build files depend on. Iteratively remove those.
        Iterable<Path> dependents = buildFileDependents.get(path);
        LOG.debug("Invalidating dependents for path %s: %s", path, dependents);
        for (Path dependent : dependents) {
            if (dependent.equals(path)) {
                continue;
            }
            invalidatedRawNodes += invalidatePath(dependent);
        }
        buildFileDependents.removeAll(path);
        buildFileConfigs.remove(path);
        buildFileEnv.remove(path);
        return invalidatedRawNodes;
    }
}
Also used : Path(java.nio.file.Path) AutoCloseableLock(com.facebook.buck.util.concurrent.AutoCloseableLock) UnflavoredBuildTarget(com.facebook.buck.model.UnflavoredBuildTarget) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map)

Example 8 with AutoCloseableLock

use of com.facebook.buck.util.concurrent.AutoCloseableLock in project buck by facebook.

the class DaemonicCellState method putRawNodesIfNotPresentAndStripMetaEntries.

ImmutableSet<Map<String, Object>> putRawNodesIfNotPresentAndStripMetaEntries(final Path buildFile, final ImmutableSet<Map<String, Object>> withoutMetaIncludes, final ImmutableSet<Path> dependentsOfEveryNode, ImmutableMap<String, ImmutableMap<String, Optional<String>>> configs, ImmutableMap<String, Optional<String>> env) {
    try (AutoCloseableLock writeLock = rawAndComputedNodesLock.writeLock()) {
        ImmutableSet<Map<String, Object>> updated = allRawNodes.putIfAbsentAndGet(buildFile, withoutMetaIncludes);
        buildFileConfigs.put(buildFile, configs);
        buildFileEnv.put(buildFile, env);
        if (updated == withoutMetaIncludes) {
            // the "dependentsOfEveryNode" set.
            for (Path dependent : dependentsOfEveryNode) {
                buildFileDependents.put(dependent, buildFile);
            }
        }
        return updated;
    }
}
Also used : Path(java.nio.file.Path) AutoCloseableLock(com.facebook.buck.util.concurrent.AutoCloseableLock) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map)

Example 9 with AutoCloseableLock

use of com.facebook.buck.util.concurrent.AutoCloseableLock in project buck by facebook.

the class DaemonicCellState method getOrCreateCache.

@SuppressWarnings("unchecked")
public <T> CacheImpl<T> getOrCreateCache(Class<T> type) {
    try (AutoCloseableLock updateLock = rawAndComputedNodesLock.updateLock()) {
        CacheImpl<?> cache = typedNodeCaches.get(type);
        if (cache == null) {
            try (AutoCloseableLock writeLock = rawAndComputedNodesLock.writeLock()) {
                cache = new CacheImpl<>();
                typedNodeCaches.put(type, cache);
            }
        }
        return (CacheImpl<T>) cache;
    }
}
Also used : AutoCloseableLock(com.facebook.buck.util.concurrent.AutoCloseableLock)

Example 10 with AutoCloseableLock

use of com.facebook.buck.util.concurrent.AutoCloseableLock in project buck by facebook.

the class DaemonicParserState method invalidateIfBuckConfigOrEnvHasChanged.

private synchronized void invalidateIfBuckConfigOrEnvHasChanged(Cell cell, Path buildFile) {
    try (AutoCloseableLock readLock = cellStateLock.readLock()) {
        DaemonicCellState state = cellPathToDaemonicState.get(cell.getRoot());
        if (state == null) {
            return;
        }
        // Invalidates and also keeps the state cell up-to-date
        state.invalidateIfBuckConfigHasChanged(cell, buildFile);
        Optional<MapDifference<String, String>> envDiff = state.invalidateIfEnvHasChanged(cell, buildFile);
        if (envDiff.isPresent()) {
            MapDifference<String, String> diff = envDiff.get();
            LOG.warn("Invalidating cache on environment change (%s)", diff);
            Set<String> environmentChanges = new HashSet<>();
            environmentChanges.addAll(diff.entriesOnlyOnLeft().keySet());
            environmentChanges.addAll(diff.entriesOnlyOnRight().keySet());
            environmentChanges.addAll(diff.entriesDiffering().keySet());
            cacheInvalidatedByEnvironmentVariableChangeCounter.addAll(environmentChanges);
            broadcastEventListener.broadcast(ParsingEvent.environmentalChange(environmentChanges.toString()));
        }
    }
}
Also used : MapDifference(com.google.common.collect.MapDifference) AutoCloseableLock(com.facebook.buck.util.concurrent.AutoCloseableLock) HashSet(java.util.HashSet)

Aggregations

AutoCloseableLock (com.facebook.buck.util.concurrent.AutoCloseableLock)12 Path (java.nio.file.Path)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 HashMap (java.util.HashMap)4 Map (java.util.Map)4 ConcurrentMap (java.util.concurrent.ConcurrentMap)4 UnflavoredBuildTarget (com.facebook.buck.model.UnflavoredBuildTarget)2 Cell (com.facebook.buck.rules.Cell)2 MapDifference (com.google.common.collect.MapDifference)2 HashSet (java.util.HashSet)2 Optional (java.util.Optional)2 BuildFileParseException (com.facebook.buck.json.BuildFileParseException)1 Logger (com.facebook.buck.log.Logger)1 BuildFileTree (com.facebook.buck.model.BuildFileTree)1 BuildTarget (com.facebook.buck.model.BuildTarget)1 BuildTargetException (com.facebook.buck.model.BuildTargetException)1 FilesystemBackedBuildFileTree (com.facebook.buck.model.FilesystemBackedBuildFileTree)1 AutoCloseableReadWriteUpdateLock (com.facebook.buck.util.concurrent.AutoCloseableReadWriteUpdateLock)1 Preconditions (com.google.common.base.Preconditions)1 HashMultimap (com.google.common.collect.HashMultimap)1