Search in sources :

Example 1 with AutoCloseableLock

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

the class DaemonicCellState method invalidateIfBuckConfigHasChanged.

void invalidateIfBuckConfigHasChanged(Cell cell, Path buildFile) {
    // TODO(mzlee): Check whether usedConfigs includes the buildFileName
    ImmutableMap<String, ImmutableMap<String, Optional<String>>> usedConfigs;
    try (AutoCloseableLock readLock = rawAndComputedNodesLock.readLock()) {
        usedConfigs = buildFileConfigs.get(buildFile);
    }
    if (usedConfigs == null) {
        // TODO(mzlee): Figure out when/how we can safely update this
        this.cell.set(cell);
        return;
    }
    for (Map.Entry<String, ImmutableMap<String, Optional<String>>> keyEnt : usedConfigs.entrySet()) {
        for (Map.Entry<String, Optional<String>> valueEnt : keyEnt.getValue().entrySet()) {
            Optional<String> value = cell.getBuckConfig().getValue(keyEnt.getKey(), valueEnt.getKey());
            if (!value.equals(valueEnt.getValue())) {
                invalidatePath(buildFile);
                this.cell.set(cell);
                return;
            }
        }
    }
}
Also used : Optional(java.util.Optional) 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) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 2 with AutoCloseableLock

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

the class DaemonicCellState method invalidateIfEnvHasChanged.

Optional<MapDifference<String, String>> invalidateIfEnvHasChanged(Cell cell, Path buildFile) {
    // Invalidate if env vars have changed.
    ImmutableMap<String, Optional<String>> usedEnv;
    try (AutoCloseableLock readLock = rawAndComputedNodesLock.readLock()) {
        usedEnv = buildFileEnv.get(buildFile);
    }
    if (usedEnv == null) {
        this.cell.set(cell);
        return Optional.empty();
    }
    for (Map.Entry<String, Optional<String>> ent : usedEnv.entrySet()) {
        Optional<String> value = Optional.ofNullable(cell.getBuckConfig().getEnvironment().get(ent.getKey()));
        if (!value.equals(ent.getValue())) {
            invalidatePath(buildFile);
            this.cell.set(cell);
            return Optional.of(Maps.difference(value.map(v -> ImmutableMap.of(ent.getKey(), v)).orElse(ImmutableMap.of()), ent.getValue().map(v -> ImmutableMap.of(ent.getKey(), v)).orElse(ImmutableMap.of())));
        }
    }
    return Optional.empty();
}
Also used : Logger(com.facebook.buck.log.Logger) UnflavoredBuildTarget(com.facebook.buck.model.UnflavoredBuildTarget) ImmutableSet(com.google.common.collect.ImmutableSet) ImmutableMap(com.google.common.collect.ImmutableMap) BuildTargetException(com.facebook.buck.model.BuildTargetException) AutoCloseableReadWriteUpdateLock(com.facebook.buck.util.concurrent.AutoCloseableReadWriteUpdateLock) HashMap(java.util.HashMap) GuardedBy(javax.annotation.concurrent.GuardedBy) BuildTarget(com.facebook.buck.model.BuildTarget) Maps(com.google.common.collect.Maps) AtomicReference(java.util.concurrent.atomic.AtomicReference) SetMultimap(com.google.common.collect.SetMultimap) ConcurrentMap(java.util.concurrent.ConcurrentMap) MapDifference(com.google.common.collect.MapDifference) AutoCloseableLock(com.facebook.buck.util.concurrent.AutoCloseableLock) HashMultimap(com.google.common.collect.HashMultimap) Map(java.util.Map) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) Cell(com.facebook.buck.rules.Cell) Path(java.nio.file.Path) Optional(java.util.Optional) 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 3 with AutoCloseableLock

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

the class DaemonicParserState method getOrCreateCellState.

private DaemonicCellState getOrCreateCellState(Cell cell) {
    try (AutoCloseableLock writeLock = cellStateLock.writeLock()) {
        DaemonicCellState state = cellPathToDaemonicState.get(cell.getRoot());
        if (state == null) {
            state = new DaemonicCellState(cell, parsingThreads);
            cellPathToDaemonicState.put(cell.getRoot(), state);
        }
        return state;
    }
}
Also used : AutoCloseableLock(com.facebook.buck.util.concurrent.AutoCloseableLock)

Example 4 with AutoCloseableLock

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

the class DaemonicParserState method invalidateBasedOn.

public void invalidateBasedOn(WatchEvent<?> event) {
    if (!WatchEvents.isPathChangeEvent(event)) {
        // Non-path change event, likely an overflow due to many change events: invalidate everything.
        LOG.debug("Received non-path change event %s, assuming overflow and checking caches.", event);
        if (invalidateAllCaches()) {
            LOG.warn("Invalidated cache on watch event %s.", event);
            cacheInvalidatedByWatchOverflowCounter.inc();
        }
        return;
    }
    filesChangedCounter.inc();
    Path path = (Path) event.context();
    try (AutoCloseableLock readLock = cellStateLock.readLock()) {
        for (DaemonicCellState state : cellPathToDaemonicState.values()) {
            try {
                // rule key change.  For parsing, these are the only events we need to care about.
                if (isPathCreateOrDeleteEvent(event)) {
                    Cell cell = state.getCell();
                    BuildFileTree buildFiles = buildFileTrees.get(cell);
                    if (path.endsWith(cell.getBuildFileName())) {
                        LOG.debug("Build file %s changed, invalidating build file tree for cell %s", path, cell);
                        // If a build file has been added or removed, reconstruct the build file tree.
                        buildFileTrees.invalidate(cell);
                    }
                    // "containing" {@code path} unless its filename matches a temp file pattern.
                    if (!isTempFile(cell, path)) {
                        invalidateContainingBuildFile(cell, buildFiles, path);
                    } else {
                        LOG.debug("Not invalidating the owning build file of %s because it is a temporary file.", state.getCellRoot().resolve(path).toAbsolutePath().toString());
                    }
                }
            } catch (ExecutionException | UncheckedExecutionException e) {
                try {
                    Throwables.throwIfInstanceOf(e, BuildFileParseException.class);
                    Throwables.throwIfUnchecked(e);
                    throw new RuntimeException(e);
                } catch (BuildFileParseException bfpe) {
                    LOG.warn("Unable to parse already parsed build file.", bfpe);
                }
            }
        }
    }
    invalidatePath(path);
}
Also used : Path(java.nio.file.Path) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) AutoCloseableLock(com.facebook.buck.util.concurrent.AutoCloseableLock) BuildFileTree(com.facebook.buck.model.BuildFileTree) FilesystemBackedBuildFileTree(com.facebook.buck.model.FilesystemBackedBuildFileTree) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) Cell(com.facebook.buck.rules.Cell) BuildFileParseException(com.facebook.buck.json.BuildFileParseException)

Example 5 with AutoCloseableLock

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

the class DaemonicParserState method invalidateCellCaches.

public boolean invalidateCellCaches(Cell cell) {
    LOG.debug("Starting to invalidate caches for %s..", cell.getRoot());
    try (AutoCloseableLock writeLock = cellStateLock.writeLock()) {
        boolean invalidated = cellPathToDaemonicState.containsKey(cell.getRoot());
        cellPathToDaemonicState.remove(cell.getRoot());
        if (invalidated) {
            LOG.debug("Cell cache data invalidated.");
        } else {
            LOG.debug("Cell caches were empty, no data invalidated.");
        }
        return invalidated;
    }
}
Also used : AutoCloseableLock(com.facebook.buck.util.concurrent.AutoCloseableLock)

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