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