Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | package com.google.devtools.build.lib.skyframe; |
| 15 | |
tomlu | a155b53 | 2017-11-08 20:12:47 +0100 | [diff] [blame] | 16 | import com.google.common.base.Preconditions; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 17 | import com.google.common.base.Predicate; |
Rumou Duan | 45e8e57 | 2016-06-17 16:43:44 +0000 | [diff] [blame] | 18 | import com.google.common.base.Supplier; |
| 19 | import com.google.common.base.Suppliers; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 20 | import com.google.common.base.Throwables; |
| 21 | import com.google.common.collect.ImmutableList; |
Eric Fellheimer | e659072 | 2015-11-17 17:07:48 +0000 | [diff] [blame] | 22 | import com.google.common.collect.ImmutableSet; |
Rumou Duan | 45e8e57 | 2016-06-17 16:43:44 +0000 | [diff] [blame] | 23 | import com.google.common.collect.ImmutableSortedSet; |
Miguel Alcon Pinto | 7cf2365 | 2015-03-10 21:27:48 +0000 | [diff] [blame] | 24 | import com.google.common.collect.Range; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 25 | import com.google.common.collect.Sets; |
| 26 | import com.google.common.util.concurrent.ThreadFactoryBuilder; |
| 27 | import com.google.devtools.build.lib.actions.Artifact; |
shahan | 602cc85 | 2018-06-06 20:09:57 -0700 | [diff] [blame] | 28 | import com.google.devtools.build.lib.actions.FileValue; |
Eric Fellheimer | 6a9d7e5 | 2015-06-18 22:08:32 +0000 | [diff] [blame] | 29 | import com.google.devtools.build.lib.concurrent.ExecutorUtil; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 30 | import com.google.devtools.build.lib.concurrent.Sharder; |
| 31 | import com.google.devtools.build.lib.concurrent.ThrowableRecordingRunnableWrapper; |
Nathan Harmata | e1b61d0 | 2015-10-06 00:09:42 +0000 | [diff] [blame] | 32 | import com.google.devtools.build.lib.profiler.AutoProfiler; |
| 33 | import com.google.devtools.build.lib.profiler.AutoProfiler.ElapsedTimeReceiver; |
twerth | 646dfd1 | 2018-07-04 01:58:40 -0700 | [diff] [blame] | 34 | import com.google.devtools.build.lib.profiler.Profiler; |
| 35 | import com.google.devtools.build.lib.profiler.SilentCloseable; |
Janak Ramakrishnan | 6ddbb6e | 2015-07-28 21:39:22 +0000 | [diff] [blame] | 36 | import com.google.devtools.build.lib.skyframe.SkyValueDirtinessChecker.DirtyResult; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 37 | import com.google.devtools.build.lib.util.LoggingUtil; |
| 38 | import com.google.devtools.build.lib.util.Pair; |
| 39 | import com.google.devtools.build.lib.util.io.TimestampGranularityMonitor; |
| 40 | import com.google.devtools.build.lib.vfs.BatchStat; |
| 41 | import com.google.devtools.build.lib.vfs.FileStatusWithDigest; |
Eric Fellheimer | e659072 | 2015-11-17 17:07:48 +0000 | [diff] [blame] | 42 | import com.google.devtools.build.lib.vfs.ModifiedFileSet; |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 43 | import com.google.devtools.build.lib.vfs.Path; |
Eric Fellheimer | e659072 | 2015-11-17 17:07:48 +0000 | [diff] [blame] | 44 | import com.google.devtools.build.lib.vfs.PathFragment; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 45 | import com.google.devtools.build.skyframe.Differencer; |
janakr | e54491e | 2018-07-11 16:29:13 -0700 | [diff] [blame] | 46 | import com.google.devtools.build.skyframe.FunctionHermeticity; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 47 | import com.google.devtools.build.skyframe.SkyFunctionName; |
| 48 | import com.google.devtools.build.skyframe.SkyKey; |
| 49 | import com.google.devtools.build.skyframe.SkyValue; |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 50 | import com.google.devtools.build.skyframe.WalkableGraph; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 51 | import java.io.IOException; |
| 52 | import java.util.Collection; |
| 53 | import java.util.Collections; |
| 54 | import java.util.HashMap; |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 55 | import java.util.HashSet; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 56 | import java.util.List; |
| 57 | import java.util.Map; |
Rumou Duan | 45e8e57 | 2016-06-17 16:43:44 +0000 | [diff] [blame] | 58 | import java.util.NavigableSet; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 59 | import java.util.Set; |
| 60 | import java.util.concurrent.ConcurrentHashMap; |
| 61 | import java.util.concurrent.ExecutorService; |
| 62 | import java.util.concurrent.Executors; |
cushon | 4d70fae | 2017-04-11 01:01:13 +0000 | [diff] [blame] | 63 | import java.util.concurrent.Future; |
Nathan Harmata | e1b61d0 | 2015-10-06 00:09:42 +0000 | [diff] [blame] | 64 | import java.util.concurrent.TimeUnit; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 65 | import java.util.concurrent.atomic.AtomicInteger; |
| 66 | import java.util.logging.Level; |
| 67 | import java.util.logging.Logger; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 68 | import javax.annotation.Nullable; |
| 69 | |
| 70 | /** |
| 71 | * A helper class to find dirty values by accessing the filesystem directly (contrast with |
| 72 | * {@link DiffAwareness}). |
| 73 | */ |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 74 | public class FilesystemValueChecker { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 75 | |
Nathan Harmata | 3a509bd | 2015-10-06 01:00:47 +0000 | [diff] [blame] | 76 | private static final int DIRTINESS_CHECK_THREADS = 200; |
lberki | 97abb52 | 2017-09-04 18:51:57 +0200 | [diff] [blame] | 77 | private static final Logger logger = Logger.getLogger(FilesystemValueChecker.class.getName()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 78 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 79 | private static final Predicate<SkyKey> ACTION_FILTER = |
| 80 | SkyFunctionName.functionIs(SkyFunctions.ACTION_EXECUTION); |
| 81 | |
| 82 | private final TimestampGranularityMonitor tsgm; |
Nathan Harmata | 9b38b2c | 2015-08-27 16:11:07 +0000 | [diff] [blame] | 83 | @Nullable |
Miguel Alcon Pinto | 7cf2365 | 2015-03-10 21:27:48 +0000 | [diff] [blame] | 84 | private final Range<Long> lastExecutionTimeRange; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 85 | private AtomicInteger modifiedOutputFilesCounter = new AtomicInteger(0); |
Miguel Alcon Pinto | 7cf2365 | 2015-03-10 21:27:48 +0000 | [diff] [blame] | 86 | private AtomicInteger modifiedOutputFilesIntraBuildCounter = new AtomicInteger(0); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 87 | |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 88 | public FilesystemValueChecker(@Nullable TimestampGranularityMonitor tsgm, |
| 89 | @Nullable Range<Long> lastExecutionTimeRange) { |
Nathan Harmata | 2ff0a6d | 2015-08-12 21:10:56 +0000 | [diff] [blame] | 90 | this.tsgm = tsgm; |
| 91 | this.lastExecutionTimeRange = lastExecutionTimeRange; |
| 92 | } |
| 93 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 94 | /** |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 95 | * Returns a {@link Differencer.DiffWithDelta} containing keys from the give map that are dirty |
| 96 | * according to the passed-in {@code dirtinessChecker}. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 97 | */ |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 98 | // TODO(bazel-team): Refactor these methods so that FilesystemValueChecker only operates on a |
| 99 | // WalkableGraph. |
| 100 | Differencer.DiffWithDelta getDirtyKeys(Map<SkyKey, SkyValue> valuesMap, |
| 101 | SkyValueDirtinessChecker dirtinessChecker) throws InterruptedException { |
| 102 | return getDirtyValues(new MapBackedValueFetcher(valuesMap), valuesMap.keySet(), |
| 103 | dirtinessChecker, /*checkMissingValues=*/false); |
Nathan Harmata | 2ff0a6d | 2015-08-12 21:10:56 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Returns a {@link Differencer.DiffWithDelta} containing keys that are dirty according to the |
| 108 | * passed-in {@code dirtinessChecker}. |
| 109 | */ |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 110 | public Differencer.DiffWithDelta getNewAndOldValues(Map<SkyKey, SkyValue> valuesMap, |
| 111 | Iterable<SkyKey> keys, SkyValueDirtinessChecker dirtinessChecker) |
| 112 | throws InterruptedException { |
| 113 | return getDirtyValues(new MapBackedValueFetcher(valuesMap), keys, |
| 114 | dirtinessChecker, /*checkMissingValues=*/true); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * Returns a {@link Differencer.DiffWithDelta} containing keys that are dirty according to the |
| 119 | * passed-in {@code dirtinessChecker}. |
| 120 | */ |
| 121 | public Differencer.DiffWithDelta getNewAndOldValues(WalkableGraph walkableGraph, |
| 122 | Iterable<SkyKey> keys, SkyValueDirtinessChecker dirtinessChecker) |
| 123 | throws InterruptedException { |
| 124 | return getDirtyValues(new WalkableGraphBackedValueFetcher(walkableGraph), keys, |
| 125 | dirtinessChecker, /*checkMissingValues=*/true); |
| 126 | } |
| 127 | |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 128 | private interface ValueFetcher { |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 129 | @Nullable |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 130 | SkyValue get(SkyKey key) throws InterruptedException; |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | private static class WalkableGraphBackedValueFetcher implements ValueFetcher { |
| 134 | private final WalkableGraph walkableGraph; |
| 135 | |
| 136 | private WalkableGraphBackedValueFetcher(WalkableGraph walkableGraph) { |
| 137 | this.walkableGraph = walkableGraph; |
| 138 | } |
| 139 | |
| 140 | @Override |
| 141 | @Nullable |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 142 | public SkyValue get(SkyKey key) throws InterruptedException { |
Janak Ramakrishnan | 112840b | 2016-12-29 21:49:56 +0000 | [diff] [blame] | 143 | return walkableGraph.getValue(key); |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
| 147 | private static class MapBackedValueFetcher implements ValueFetcher { |
| 148 | private final Map<SkyKey, SkyValue> valuesMap; |
| 149 | |
| 150 | private MapBackedValueFetcher(Map<SkyKey, SkyValue> valuesMap) { |
| 151 | this.valuesMap = valuesMap; |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | @Nullable |
| 156 | public SkyValue get(SkyKey key) { |
| 157 | return valuesMap.get(key); |
| 158 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Return a collection of action values which have output files that are not in-sync with |
| 163 | * the on-disk file value (were modified externally). |
| 164 | */ |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 165 | Collection<SkyKey> getDirtyActionValues(Map<SkyKey, SkyValue> valuesMap, |
Eric Fellheimer | e659072 | 2015-11-17 17:07:48 +0000 | [diff] [blame] | 166 | @Nullable final BatchStat batchStatter, ModifiedFileSet modifiedOutputFiles) |
| 167 | throws InterruptedException { |
| 168 | if (modifiedOutputFiles == ModifiedFileSet.NOTHING_MODIFIED) { |
lberki | 97abb52 | 2017-09-04 18:51:57 +0200 | [diff] [blame] | 169 | logger.info("Not checking for dirty actions since nothing was modified"); |
Eric Fellheimer | e659072 | 2015-11-17 17:07:48 +0000 | [diff] [blame] | 170 | return ImmutableList.of(); |
| 171 | } |
lberki | 97abb52 | 2017-09-04 18:51:57 +0200 | [diff] [blame] | 172 | logger.info("Accumulating dirty actions"); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 173 | final int numOutputJobs = Runtime.getRuntime().availableProcessors() * 4; |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 174 | final Set<SkyKey> actionSkyKeys = new HashSet<>(); |
twerth | 646dfd1 | 2018-07-04 01:58:40 -0700 | [diff] [blame] | 175 | try (SilentCloseable c = Profiler.instance().profile("getDirtyActionValues.filter_actions")) { |
| 176 | for (SkyKey key : valuesMap.keySet()) { |
| 177 | if (ACTION_FILTER.apply(key)) { |
| 178 | actionSkyKeys.add(key); |
| 179 | } |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 180 | } |
| 181 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 182 | final Sharder<Pair<SkyKey, ActionExecutionValue>> outputShards = |
| 183 | new Sharder<>(numOutputJobs, actionSkyKeys.size()); |
| 184 | |
| 185 | for (SkyKey key : actionSkyKeys) { |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 186 | outputShards.add(Pair.of(key, (ActionExecutionValue) valuesMap.get(key))); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 187 | } |
lberki | 97abb52 | 2017-09-04 18:51:57 +0200 | [diff] [blame] | 188 | logger.info("Sharded action values for batching"); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 189 | |
| 190 | ExecutorService executor = Executors.newFixedThreadPool( |
| 191 | numOutputJobs, |
| 192 | new ThreadFactoryBuilder().setNameFormat("FileSystem Output File Invalidator %d").build()); |
| 193 | |
| 194 | Collection<SkyKey> dirtyKeys = Sets.newConcurrentHashSet(); |
| 195 | ThrowableRecordingRunnableWrapper wrapper = |
| 196 | new ThrowableRecordingRunnableWrapper("FileSystemValueChecker#getDirtyActionValues"); |
| 197 | |
| 198 | modifiedOutputFilesCounter.set(0); |
Miguel Alcon Pinto | 7cf2365 | 2015-03-10 21:27:48 +0000 | [diff] [blame] | 199 | modifiedOutputFilesIntraBuildCounter.set(0); |
Rumou Duan | 45e8e57 | 2016-06-17 16:43:44 +0000 | [diff] [blame] | 200 | final ImmutableSet<PathFragment> knownModifiedOutputFiles = |
Eric Fellheimer | e659072 | 2015-11-17 17:07:48 +0000 | [diff] [blame] | 201 | modifiedOutputFiles == ModifiedFileSet.EVERYTHING_MODIFIED |
| 202 | ? null |
| 203 | : modifiedOutputFiles.modifiedSourceFiles(); |
Rumou Duan | 45e8e57 | 2016-06-17 16:43:44 +0000 | [diff] [blame] | 204 | |
| 205 | // Initialized lazily through a supplier because it is only used to check modified |
| 206 | // TreeArtifacts, which are not frequently used in builds. |
| 207 | Supplier<NavigableSet<PathFragment>> sortedKnownModifiedOutputFiles = |
| 208 | Suppliers.memoize(new Supplier<NavigableSet<PathFragment>>() { |
| 209 | @Override |
| 210 | public NavigableSet<PathFragment> get() { |
| 211 | if (knownModifiedOutputFiles == null) { |
| 212 | return null; |
| 213 | } else { |
| 214 | return ImmutableSortedSet.copyOf(knownModifiedOutputFiles); |
| 215 | } |
| 216 | } |
| 217 | }); |
| 218 | |
twerth | 646dfd1 | 2018-07-04 01:58:40 -0700 | [diff] [blame] | 219 | boolean interrupted; |
| 220 | try (SilentCloseable c = Profiler.instance().profile("getDirtyActionValues.stat_files")) { |
| 221 | for (List<Pair<SkyKey, ActionExecutionValue>> shard : outputShards) { |
| 222 | Runnable job = |
| 223 | (batchStatter == null) |
| 224 | ? outputStatJob( |
| 225 | dirtyKeys, shard, knownModifiedOutputFiles, sortedKnownModifiedOutputFiles) |
| 226 | : batchStatJob( |
| 227 | dirtyKeys, |
| 228 | shard, |
| 229 | batchStatter, |
| 230 | knownModifiedOutputFiles, |
| 231 | sortedKnownModifiedOutputFiles); |
| 232 | Future<?> unused = executor.submit(wrapper.wrap(job)); |
| 233 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 234 | |
twerth | 646dfd1 | 2018-07-04 01:58:40 -0700 | [diff] [blame] | 235 | interrupted = ExecutorUtil.interruptibleShutdown(executor); |
| 236 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 237 | Throwables.propagateIfPossible(wrapper.getFirstThrownError()); |
lberki | 97abb52 | 2017-09-04 18:51:57 +0200 | [diff] [blame] | 238 | logger.info("Completed output file stat checks"); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 239 | if (interrupted) { |
| 240 | throw new InterruptedException(); |
| 241 | } |
| 242 | return dirtyKeys; |
| 243 | } |
| 244 | |
| 245 | private Runnable batchStatJob(final Collection<SkyKey> dirtyKeys, |
Eric Fellheimer | e659072 | 2015-11-17 17:07:48 +0000 | [diff] [blame] | 246 | final List<Pair<SkyKey, ActionExecutionValue>> shard, |
Rumou Duan | 45e8e57 | 2016-06-17 16:43:44 +0000 | [diff] [blame] | 247 | final BatchStat batchStatter, final ImmutableSet<PathFragment> knownModifiedOutputFiles, |
| 248 | final Supplier<NavigableSet<PathFragment>> sortedKnownModifiedOutputFiles) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 249 | return new Runnable() { |
| 250 | @Override |
| 251 | public void run() { |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 252 | Map<Artifact, Pair<SkyKey, ActionExecutionValue>> fileToKeyAndValue = new HashMap<>(); |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 253 | Map<Artifact, Pair<SkyKey, ActionExecutionValue>> treeArtifactsToKeyAndValue = |
| 254 | new HashMap<>(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 255 | for (Pair<SkyKey, ActionExecutionValue> keyAndValue : shard) { |
| 256 | ActionExecutionValue actionValue = keyAndValue.getSecond(); |
| 257 | if (actionValue == null) { |
| 258 | dirtyKeys.add(keyAndValue.getFirst()); |
| 259 | } else { |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 260 | for (Artifact artifact : actionValue.getAllFileValues().keySet()) { |
| 261 | if (shouldCheckFile(knownModifiedOutputFiles, artifact)) { |
| 262 | fileToKeyAndValue.put(artifact, keyAndValue); |
Eric Fellheimer | e659072 | 2015-11-17 17:07:48 +0000 | [diff] [blame] | 263 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 264 | } |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 265 | |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 266 | for (Artifact artifact : actionValue.getAllTreeArtifactValues().keySet()) { |
Rumou Duan | 45e8e57 | 2016-06-17 16:43:44 +0000 | [diff] [blame] | 267 | if (shouldCheckTreeArtifact(sortedKnownModifiedOutputFiles.get(), artifact)) { |
| 268 | treeArtifactsToKeyAndValue.put(artifact, keyAndValue); |
| 269 | } |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 270 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 274 | List<Artifact> artifacts = ImmutableList.copyOf(fileToKeyAndValue.keySet()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 275 | List<FileStatusWithDigest> stats; |
| 276 | try { |
lberki | 97abb52 | 2017-09-04 18:51:57 +0200 | [diff] [blame] | 277 | stats = |
| 278 | batchStatter.batchStat( |
| 279 | /*includeDigest=*/ true, |
| 280 | /*includeLinks=*/ true, |
| 281 | Artifact.asPathFragments(artifacts)); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 282 | } catch (IOException e) { |
| 283 | // Batch stat did not work. Log an exception and fall back on system calls. |
| 284 | LoggingUtil.logToRemote(Level.WARNING, "Unable to process batch stat", e); |
lberki | 97abb52 | 2017-09-04 18:51:57 +0200 | [diff] [blame] | 285 | logger.log(Level.WARNING, "Unable to process batch stat", e); |
Rumou Duan | 45e8e57 | 2016-06-17 16:43:44 +0000 | [diff] [blame] | 286 | outputStatJob(dirtyKeys, shard, knownModifiedOutputFiles, sortedKnownModifiedOutputFiles) |
| 287 | .run(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 288 | return; |
| 289 | } catch (InterruptedException e) { |
| 290 | // We handle interrupt in the main thread. |
| 291 | return; |
| 292 | } |
| 293 | |
lberki | 97abb52 | 2017-09-04 18:51:57 +0200 | [diff] [blame] | 294 | Preconditions.checkState( |
| 295 | artifacts.size() == stats.size(), |
| 296 | "artifacts.size() == %s stats.size() == %s", |
| 297 | artifacts.size(), |
| 298 | stats.size()); |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 299 | for (int i = 0; i < artifacts.size(); i++) { |
| 300 | Artifact artifact = artifacts.get(i); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 301 | FileStatusWithDigest stat = stats.get(i); |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 302 | Pair<SkyKey, ActionExecutionValue> keyAndValue = fileToKeyAndValue.get(artifact); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 303 | ActionExecutionValue actionValue = keyAndValue.getSecond(); |
| 304 | SkyKey key = keyAndValue.getFirst(); |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 305 | FileValue lastKnownData = actionValue.getAllFileValues().get(artifact); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 306 | try { |
lberki | 97abb52 | 2017-09-04 18:51:57 +0200 | [diff] [blame] | 307 | FileValue newData = ActionMetadataHandler.fileValueFromArtifact(artifact, stat, tsgm); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 308 | if (!newData.equals(lastKnownData)) { |
lberki | 97abb52 | 2017-09-04 18:51:57 +0200 | [diff] [blame] | 309 | updateIntraBuildModifiedCounter( |
| 310 | stat != null ? stat.getLastChangeTime() : -1, |
| 311 | lastKnownData.isSymlink(), |
| 312 | newData.isSymlink()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 313 | modifiedOutputFilesCounter.getAndIncrement(); |
| 314 | dirtyKeys.add(key); |
| 315 | } |
| 316 | } catch (IOException e) { |
| 317 | // This is an unexpected failure getting a digest or symlink target. |
| 318 | modifiedOutputFilesCounter.getAndIncrement(); |
| 319 | dirtyKeys.add(key); |
| 320 | } |
| 321 | } |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 322 | |
| 323 | // Unfortunately, there exists no facility to batch list directories. |
| 324 | // We must use direct filesystem calls. |
| 325 | for (Map.Entry<Artifact, Pair<SkyKey, ActionExecutionValue>> entry : |
| 326 | treeArtifactsToKeyAndValue.entrySet()) { |
| 327 | Artifact artifact = entry.getKey(); |
| 328 | if (treeArtifactIsDirty( |
| 329 | entry.getKey(), entry.getValue().getSecond().getTreeArtifactValue(artifact))) { |
| 330 | Path path = artifact.getPath(); |
| 331 | // Count the changed directory as one "file". |
| 332 | // TODO(bazel-team): There are no tests for this codepath. |
| 333 | try { |
lberki | 97abb52 | 2017-09-04 18:51:57 +0200 | [diff] [blame] | 334 | updateIntraBuildModifiedCounter( |
| 335 | path.exists() ? path.getLastModifiedTime() : -1, false, path.isSymbolicLink()); |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 336 | } catch (IOException e) { |
| 337 | // Do nothing here. |
| 338 | } |
| 339 | |
| 340 | modifiedOutputFilesCounter.getAndIncrement(); |
| 341 | dirtyKeys.add(entry.getValue().getFirst()); |
| 342 | } |
| 343 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 344 | } |
| 345 | }; |
| 346 | } |
| 347 | |
Miguel Alcon Pinto | bdfdd09 | 2015-04-10 19:04:49 +0000 | [diff] [blame] | 348 | private void updateIntraBuildModifiedCounter(long time, boolean oldWasSymlink, |
Nathan Harmata | 7a34427 | 2015-06-05 20:52:38 +0000 | [diff] [blame] | 349 | boolean newIsSymlink) { |
Miguel Alcon Pinto | bdfdd09 | 2015-04-10 19:04:49 +0000 | [diff] [blame] | 350 | if (lastExecutionTimeRange != null |
| 351 | && lastExecutionTimeRange.contains(time) |
| 352 | && !(oldWasSymlink && newIsSymlink)) { |
Miguel Alcon Pinto | 7cf2365 | 2015-03-10 21:27:48 +0000 | [diff] [blame] | 353 | modifiedOutputFilesIntraBuildCounter.incrementAndGet(); |
| 354 | } |
| 355 | } |
| 356 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 357 | private Runnable outputStatJob(final Collection<SkyKey> dirtyKeys, |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 358 | final List<Pair<SkyKey, ActionExecutionValue>> shard, |
Rumou Duan | 45e8e57 | 2016-06-17 16:43:44 +0000 | [diff] [blame] | 359 | final ImmutableSet<PathFragment> knownModifiedOutputFiles, |
| 360 | final Supplier<NavigableSet<PathFragment>> sortedKnownModifiedOutputFiles) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 361 | return new Runnable() { |
| 362 | @Override |
| 363 | public void run() { |
| 364 | for (Pair<SkyKey, ActionExecutionValue> keyAndValue : shard) { |
| 365 | ActionExecutionValue value = keyAndValue.getSecond(); |
Eric Fellheimer | e659072 | 2015-11-17 17:07:48 +0000 | [diff] [blame] | 366 | if (value == null |
Rumou Duan | 45e8e57 | 2016-06-17 16:43:44 +0000 | [diff] [blame] | 367 | || actionValueIsDirtyWithDirectSystemCalls( |
| 368 | value, knownModifiedOutputFiles, sortedKnownModifiedOutputFiles)) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 369 | dirtyKeys.add(keyAndValue.getFirst()); |
| 370 | } |
| 371 | } |
| 372 | } |
| 373 | }; |
| 374 | } |
| 375 | |
| 376 | /** |
Miguel Alcon Pinto | 7cf2365 | 2015-03-10 21:27:48 +0000 | [diff] [blame] | 377 | * Returns the number of modified output files inside of dirty actions. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 378 | */ |
| 379 | int getNumberOfModifiedOutputFiles() { |
| 380 | return modifiedOutputFilesCounter.get(); |
| 381 | } |
| 382 | |
Janak Ramakrishnan | 6ddbb6e | 2015-07-28 21:39:22 +0000 | [diff] [blame] | 383 | /** Returns the number of modified output files that occur during the previous build. */ |
| 384 | int getNumberOfModifiedOutputFilesDuringPreviousBuild() { |
Miguel Alcon Pinto | 7cf2365 | 2015-03-10 21:27:48 +0000 | [diff] [blame] | 385 | return modifiedOutputFilesIntraBuildCounter.get(); |
| 386 | } |
| 387 | |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 388 | private boolean treeArtifactIsDirty(Artifact artifact, TreeArtifactValue value) { |
| 389 | if (artifact.getPath().isSymbolicLink()) { |
| 390 | // TreeArtifacts may not be symbolic links. |
| 391 | return true; |
| 392 | } |
| 393 | |
| 394 | // There doesn't appear to be any facility to batch list directories... we must |
| 395 | // do things the 'slow' way. |
| 396 | try { |
felly | 09efb3f | 2018-07-26 07:46:15 -0700 | [diff] [blame] | 397 | Set<PathFragment> currentDirectoryValue = |
| 398 | TreeArtifactValue.explodeDirectory(artifact.getPath()); |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 399 | Set<PathFragment> valuePaths = value.getChildPaths(); |
| 400 | return !currentDirectoryValue.equals(valuePaths); |
Rumou Duan | 9ad28cd | 2016-10-19 19:28:06 +0000 | [diff] [blame] | 401 | } catch (IOException e) { |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 402 | return true; |
| 403 | } |
| 404 | } |
| 405 | |
Eric Fellheimer | e659072 | 2015-11-17 17:07:48 +0000 | [diff] [blame] | 406 | private boolean actionValueIsDirtyWithDirectSystemCalls(ActionExecutionValue actionValue, |
Rumou Duan | 45e8e57 | 2016-06-17 16:43:44 +0000 | [diff] [blame] | 407 | ImmutableSet<PathFragment> knownModifiedOutputFiles, |
| 408 | Supplier<NavigableSet<PathFragment>> sortedKnownModifiedOutputFiles) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 409 | boolean isDirty = false; |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 410 | for (Map.Entry<Artifact, FileValue> entry : actionValue.getAllFileValues().entrySet()) { |
| 411 | Artifact file = entry.getKey(); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 412 | FileValue lastKnownData = entry.getValue(); |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 413 | if (shouldCheckFile(knownModifiedOutputFiles, file)) { |
Eric Fellheimer | e659072 | 2015-11-17 17:07:48 +0000 | [diff] [blame] | 414 | try { |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 415 | FileValue fileValue = ActionMetadataHandler.fileValueFromArtifact(file, null, |
| 416 | tsgm); |
Eric Fellheimer | e659072 | 2015-11-17 17:07:48 +0000 | [diff] [blame] | 417 | if (!fileValue.equals(lastKnownData)) { |
| 418 | updateIntraBuildModifiedCounter(fileValue.exists() |
| 419 | ? fileValue.realRootedPath().asPath().getLastModifiedTime() |
| 420 | : -1, lastKnownData.isSymlink(), fileValue.isSymlink()); |
| 421 | modifiedOutputFilesCounter.getAndIncrement(); |
| 422 | isDirty = true; |
| 423 | } |
| 424 | } catch (IOException e) { |
| 425 | // This is an unexpected failure getting a digest or symlink target. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 426 | modifiedOutputFilesCounter.getAndIncrement(); |
| 427 | isDirty = true; |
| 428 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 429 | } |
| 430 | } |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 431 | |
| 432 | for (Map.Entry<Artifact, TreeArtifactValue> entry : |
| 433 | actionValue.getAllTreeArtifactValues().entrySet()) { |
| 434 | Artifact artifact = entry.getKey(); |
Rumou Duan | 45e8e57 | 2016-06-17 16:43:44 +0000 | [diff] [blame] | 435 | |
| 436 | if (shouldCheckTreeArtifact(sortedKnownModifiedOutputFiles.get(), artifact) |
| 437 | && treeArtifactIsDirty(artifact, entry.getValue())) { |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 438 | Path path = artifact.getPath(); |
| 439 | // Count the changed directory as one "file". |
| 440 | try { |
| 441 | updateIntraBuildModifiedCounter(path.exists() |
| 442 | ? path.getLastModifiedTime() |
| 443 | : -1, false, path.isSymbolicLink()); |
| 444 | } catch (IOException e) { |
| 445 | // Do nothing here. |
| 446 | } |
| 447 | |
| 448 | modifiedOutputFilesCounter.getAndIncrement(); |
| 449 | isDirty = true; |
| 450 | } |
| 451 | } |
| 452 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 453 | return isDirty; |
| 454 | } |
| 455 | |
Michael Thvedt | e4a7b079 | 2016-02-09 12:15:53 +0000 | [diff] [blame] | 456 | private static boolean shouldCheckFile(ImmutableSet<PathFragment> knownModifiedOutputFiles, |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 457 | Artifact artifact) { |
Eric Fellheimer | e659072 | 2015-11-17 17:07:48 +0000 | [diff] [blame] | 458 | return knownModifiedOutputFiles == null |
Rumou Duan | a77f32c | 2016-04-13 21:59:21 +0000 | [diff] [blame] | 459 | || knownModifiedOutputFiles.contains(artifact.getExecPath()); |
Eric Fellheimer | e659072 | 2015-11-17 17:07:48 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Rumou Duan | 45e8e57 | 2016-06-17 16:43:44 +0000 | [diff] [blame] | 462 | private static boolean shouldCheckTreeArtifact( |
| 463 | @Nullable NavigableSet<PathFragment> knownModifiedOutputFiles, Artifact treeArtifact) { |
| 464 | // If null, everything needs to be checked. |
| 465 | if (knownModifiedOutputFiles == null) { |
| 466 | return true; |
| 467 | } |
| 468 | |
| 469 | // Here we do the following to see whether a TreeArtifact is modified: |
| 470 | // 1. Sort the set of modified file paths in lexicographical order using TreeSet. |
| 471 | // 2. Get the first modified output file path that is greater than or equal to the exec path of |
| 472 | // the TreeArtifact to check. |
| 473 | // 3. Check whether the returned file path contains the exec path of the TreeArtifact as a |
| 474 | // prefix path. |
| 475 | PathFragment artifactExecPath = treeArtifact.getExecPath(); |
| 476 | PathFragment headPath = knownModifiedOutputFiles.ceiling(artifactExecPath); |
| 477 | |
| 478 | return headPath != null && headPath.startsWith(artifactExecPath); |
| 479 | } |
| 480 | |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 481 | private BatchDirtyResult getDirtyValues(ValueFetcher fetcher, |
| 482 | Iterable<SkyKey> keys, final SkyValueDirtinessChecker checker, |
Nathan Harmata | 2ff0a6d | 2015-08-12 21:10:56 +0000 | [diff] [blame] | 483 | final boolean checkMissingValues) throws InterruptedException { |
Janak Ramakrishnan | 6ddbb6e | 2015-07-28 21:39:22 +0000 | [diff] [blame] | 484 | ExecutorService executor = |
| 485 | Executors.newFixedThreadPool( |
| 486 | DIRTINESS_CHECK_THREADS, |
| 487 | new ThreadFactoryBuilder().setNameFormat("FileSystem Value Invalidator %d").build()); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 488 | |
| 489 | final BatchDirtyResult batchResult = new BatchDirtyResult(); |
| 490 | ThrowableRecordingRunnableWrapper wrapper = |
| 491 | new ThrowableRecordingRunnableWrapper("FilesystemValueChecker#getDirtyValues"); |
Nathan Harmata | e1b61d0 | 2015-10-06 00:09:42 +0000 | [diff] [blame] | 492 | final AtomicInteger numKeysScanned = new AtomicInteger(0); |
| 493 | final AtomicInteger numKeysChecked = new AtomicInteger(0); |
lberki | 97abb52 | 2017-09-04 18:51:57 +0200 | [diff] [blame] | 494 | ElapsedTimeReceiver elapsedTimeReceiver = |
felly | b6f8642 | 2018-02-20 19:01:35 -0800 | [diff] [blame] | 495 | elapsedTimeNanos -> { |
| 496 | if (elapsedTimeNanos > 0) { |
| 497 | logger.info( |
| 498 | String.format( |
| 499 | "Spent %d ms checking %d filesystem nodes (%d scanned)", |
| 500 | TimeUnit.MILLISECONDS.convert(elapsedTimeNanos, TimeUnit.NANOSECONDS), |
| 501 | numKeysChecked.get(), |
| 502 | numKeysScanned.get())); |
Nathan Harmata | e1b61d0 | 2015-10-06 00:09:42 +0000 | [diff] [blame] | 503 | } |
lberki | 97abb52 | 2017-09-04 18:51:57 +0200 | [diff] [blame] | 504 | }; |
Nathan Harmata | e1b61d0 | 2015-10-06 00:09:42 +0000 | [diff] [blame] | 505 | try (AutoProfiler prof = AutoProfiler.create(elapsedTimeReceiver)) { |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 506 | for (final SkyKey key : keys) { |
Nathan Harmata | 3a509bd | 2015-10-06 01:00:47 +0000 | [diff] [blame] | 507 | numKeysScanned.incrementAndGet(); |
| 508 | if (!checker.applies(key)) { |
| 509 | continue; |
| 510 | } |
janakr | e54491e | 2018-07-11 16:29:13 -0700 | [diff] [blame] | 511 | Preconditions.checkState( |
| 512 | key.functionName().getHermeticity() == FunctionHermeticity.NONHERMETIC, |
| 513 | "Only non-hermetic keys can be dirty roots: %s", |
| 514 | key); |
Nathan Harmata | e1b61d0 | 2015-10-06 00:09:42 +0000 | [diff] [blame] | 515 | executor.execute( |
| 516 | wrapper.wrap( |
felly | b6f8642 | 2018-02-20 19:01:35 -0800 | [diff] [blame] | 517 | () -> { |
| 518 | SkyValue value; |
| 519 | try { |
| 520 | value = fetcher.get(key); |
| 521 | } catch (InterruptedException e) { |
| 522 | // Exit fast. Interrupt is handled below on the main thread. |
| 523 | return; |
| 524 | } |
| 525 | if (!checkMissingValues && value == null) { |
| 526 | return; |
| 527 | } |
| 528 | |
| 529 | numKeysChecked.incrementAndGet(); |
| 530 | DirtyResult result = checker.check(key, value, tsgm); |
| 531 | if (result.isDirty()) { |
| 532 | batchResult.add(key, value, result.getNewValue()); |
Janak Ramakrishnan | 6ddbb6e | 2015-07-28 21:39:22 +0000 | [diff] [blame] | 533 | } |
Nathan Harmata | e1b61d0 | 2015-10-06 00:09:42 +0000 | [diff] [blame] | 534 | })); |
| 535 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 536 | |
Nathan Harmata | e1b61d0 | 2015-10-06 00:09:42 +0000 | [diff] [blame] | 537 | boolean interrupted = ExecutorUtil.interruptibleShutdown(executor); |
| 538 | Throwables.propagateIfPossible(wrapper.getFirstThrownError()); |
| 539 | if (interrupted) { |
| 540 | throw new InterruptedException(); |
| 541 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 542 | } |
| 543 | return batchResult; |
| 544 | } |
| 545 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 546 | /** |
Nathan Harmata | 8cd2978 | 2015-11-10 03:24:01 +0000 | [diff] [blame] | 547 | * Result of a batch call to {@link SkyValueDirtinessChecker#check}. Partitions the dirty |
Janak Ramakrishnan | 6ddbb6e | 2015-07-28 21:39:22 +0000 | [diff] [blame] | 548 | * values based on whether we have a new value available for them or not. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 549 | */ |
Nathan Harmata | 2ff0a6d | 2015-08-12 21:10:56 +0000 | [diff] [blame] | 550 | private static class BatchDirtyResult implements Differencer.DiffWithDelta { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 551 | |
| 552 | private final Set<SkyKey> concurrentDirtyKeysWithoutNewValues = |
| 553 | Collections.newSetFromMap(new ConcurrentHashMap<SkyKey, Boolean>()); |
Nathan Harmata | 2ff0a6d | 2015-08-12 21:10:56 +0000 | [diff] [blame] | 554 | private final ConcurrentHashMap<SkyKey, Delta> concurrentDirtyKeysWithNewAndOldValues = |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 555 | new ConcurrentHashMap<>(); |
| 556 | |
Nathan Harmata | 2ff0a6d | 2015-08-12 21:10:56 +0000 | [diff] [blame] | 557 | private void add(SkyKey key, @Nullable SkyValue oldValue, @Nullable SkyValue newValue) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 558 | if (newValue == null) { |
| 559 | concurrentDirtyKeysWithoutNewValues.add(key); |
| 560 | } else { |
Nathan Harmata | 2ff0a6d | 2015-08-12 21:10:56 +0000 | [diff] [blame] | 561 | if (oldValue == null) { |
| 562 | concurrentDirtyKeysWithNewAndOldValues.put(key, new Delta(newValue)); |
| 563 | } else { |
| 564 | concurrentDirtyKeysWithNewAndOldValues.put(key, new Delta(oldValue, newValue)); |
| 565 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 566 | } |
| 567 | } |
| 568 | |
| 569 | @Override |
Michajlo Matijkiw | 2b71efe | 2015-06-19 19:23:16 +0000 | [diff] [blame] | 570 | public Collection<SkyKey> changedKeysWithoutNewValues() { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 571 | return concurrentDirtyKeysWithoutNewValues; |
| 572 | } |
| 573 | |
| 574 | @Override |
Nathan Harmata | 2ff0a6d | 2015-08-12 21:10:56 +0000 | [diff] [blame] | 575 | public Map<SkyKey, Delta> changedKeysWithNewAndOldValues() { |
| 576 | return concurrentDirtyKeysWithNewAndOldValues; |
| 577 | } |
| 578 | |
| 579 | @Override |
| 580 | public Map<SkyKey, SkyValue> changedKeysWithNewValues() { |
| 581 | return Delta.newValues(concurrentDirtyKeysWithNewAndOldValues); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 582 | } |
| 583 | } |
| 584 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 585 | } |