aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/worker/WorkerFilesHash.java
blob: f0246c3b37940cda4e2702bde9543a4e5d3f8318 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// Copyright 2016 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.devtools.build.lib.worker;

import com.google.common.base.Preconditions;
import com.google.common.hash.HashCode;
import com.google.common.hash.Hasher;
import com.google.common.hash.Hashing;
import com.google.devtools.build.lib.actions.ActionInput;
import com.google.devtools.build.lib.actions.ActionInputHelper;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.actions.Artifact.ArtifactExpander;
import com.google.devtools.build.lib.actions.FileArtifactValue;
import com.google.devtools.build.lib.actions.MetadataProvider;
import com.google.devtools.build.lib.actions.Spawn;
import com.google.devtools.build.lib.vfs.PathFragment;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * Calculates the hash based on the files, which should be unchanged on disk for a worker to get
 * reused.
 */
class WorkerFilesHash {

  static HashCode getCombinedHash(SortedMap<PathFragment, HashCode> workerFilesMap) {
    Hasher hasher = Hashing.sha256().newHasher();
    for (Map.Entry<PathFragment, HashCode> workerFile : workerFilesMap.entrySet()) {
      hasher.putString(workerFile.getKey().getPathString(), Charset.defaultCharset());
      hasher.putBytes(workerFile.getValue().asBytes());
    }
    return hasher.hash();
  }

  /**
   * Return a map that contains the execroot relative path and hash of each tool and runfiles
   * artifact of the given spawn.
   */
  static SortedMap<PathFragment, HashCode> getWorkerFilesWithHashes(
      Spawn spawn, ArtifactExpander artifactExpander, MetadataProvider actionInputFileCache)
      throws IOException {
    TreeMap<PathFragment, HashCode> workerFilesMap = new TreeMap<>();

    List<ActionInput> tools =
        ActionInputHelper.expandArtifacts(spawn.getToolFiles(), artifactExpander);
    for (ActionInput tool : tools) {
      workerFilesMap.put(
          tool.getExecPath(),
          HashCode.fromBytes(actionInputFileCache.getMetadata(tool).getDigest()));
    }

    for (Map.Entry<PathFragment, Map<PathFragment, Artifact>> rootAndMappings :
        spawn.getRunfilesSupplier().getMappings().entrySet()) {
      PathFragment root = rootAndMappings.getKey();
      Preconditions.checkState(!root.isAbsolute(), root);
      for (Map.Entry<PathFragment, Artifact> mapping : rootAndMappings.getValue().entrySet()) {
        Artifact localArtifact = mapping.getValue();
        if (localArtifact != null) {
          FileArtifactValue metadata = actionInputFileCache.getMetadata(localArtifact);
          if (metadata.getType().isFile()) {
            workerFilesMap.put(
                root.getRelative(mapping.getKey()),
                HashCode.fromBytes(metadata.getDigest()));
          }
        }
      }
    }

    return workerFilesMap;
  }
}