Search in sources :

Example 11 with ActionInput

use of com.google.devtools.build.lib.actions.ActionInput in project bazel by bazelbuild.

the class SingleBuildFileCacheTest method testExceptionsCached.

@Test
public void testExceptionsCached() throws Exception {
    ActionInput empty = ActionInputHelper.fromPath("/noexist");
    IOException caught = null;
    try {
        underTest.getDigest(empty);
        fail("non existent file should raise exception");
    } catch (IOException expected) {
        caught = expected;
    }
    try {
        underTest.getSizeInBytes(empty);
        fail("non existent file should raise exception.");
    } catch (IOException expected) {
        assertSame(caught, expected);
    }
}
Also used : ActionInput(com.google.devtools.build.lib.actions.ActionInput) IOException(java.io.IOException) Test(org.junit.Test)

Example 12 with ActionInput

use of com.google.devtools.build.lib.actions.ActionInput in project bazel by bazelbuild.

the class SingleBuildFileCacheTest method testBasic.

@Test
public void testBasic() throws Exception {
    ActionInput empty = ActionInputHelper.fromPath("/empty");
    assertEquals(0, underTest.getSizeInBytes(empty));
    byte[] digestBytes = underTest.getDigest(empty);
    ByteString digest = ByteString.copyFromUtf8(BaseEncoding.base16().lowerCase().encode(digestBytes));
    assertEquals(EMPTY_MD5, digest.toStringUtf8());
    assertEquals("/empty", underTest.getInputFromDigest(digest).getExecPathString());
    assert (underTest.contentsAvailableLocally(digest));
    ByteString other = ByteString.copyFrom("f41d8cd98f00b204e9800998ecf8427e", "UTF-16");
    assert (!underTest.contentsAvailableLocally(other));
    assert (calls.containsKey("/empty"));
}
Also used : ActionInput(com.google.devtools.build.lib.actions.ActionInput) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Example 13 with ActionInput

use of com.google.devtools.build.lib.actions.ActionInput in project bazel by bazelbuild.

the class SingleBuildFileCacheTest method testCache.

@Test
public void testCache() throws Exception {
    ActionInput empty = ActionInputHelper.fromPath("/empty");
    underTest.getDigest(empty);
    assert (calls.containsKey("/empty"));
    assertEquals(1, (int) calls.get("/empty"));
    underTest.getDigest(empty);
    assertEquals(1, (int) calls.get("/empty"));
}
Also used : ActionInput(com.google.devtools.build.lib.actions.ActionInput) Test(org.junit.Test)

Example 14 with ActionInput

use of com.google.devtools.build.lib.actions.ActionInput in project bazel by bazelbuild.

the class WorkerFilesHash method getWorkerFilesHash.

public static HashCode getWorkerFilesHash(Iterable<? extends ActionInput> toolFiles, ActionExecutionContext actionExecutionContext) throws IOException {
    Hasher hasher = Hashing.sha256().newHasher();
    for (ActionInput tool : toolFiles) {
        hasher.putString(tool.getExecPathString(), Charset.defaultCharset());
        hasher.putBytes(actionExecutionContext.getActionInputFileCache().getDigest(tool));
    }
    return hasher.hash();
}
Also used : Hasher(com.google.common.hash.Hasher) ActionInput(com.google.devtools.build.lib.actions.ActionInput)

Example 15 with ActionInput

use of com.google.devtools.build.lib.actions.ActionInput in project bazel by bazelbuild.

the class WorkerSpawnStrategy method actuallyExec.

private void actuallyExec(Spawn spawn, ActionExecutionContext actionExecutionContext, AtomicReference<Class<? extends SpawnActionContext>> writeOutputFiles) throws ExecException, InterruptedException {
    Executor executor = actionExecutionContext.getExecutor();
    EventHandler eventHandler = executor.getEventHandler();
    if (executor.reportsSubcommands()) {
        executor.reportSubcommand(spawn);
    }
    // We assume that the spawn to be executed always gets at least one @flagfile.txt or
    // --flagfile=flagfile.txt argument, which contains the flags related to the work itself (as
    // opposed to start-up options for the executed tool). Thus, we can extract those elements from
    // its args and put them into the WorkRequest instead.
    List<String> flagfiles = new ArrayList<>();
    List<String> startupArgs = new ArrayList<>();
    for (String arg : spawn.getArguments()) {
        if (FLAG_FILE_PATTERN.matcher(arg).matches()) {
            flagfiles.add(arg);
        } else {
            startupArgs.add(arg);
        }
    }
    if (flagfiles.isEmpty()) {
        throw new UserExecException(String.format(ERROR_MESSAGE_PREFIX + REASON_NO_FLAGFILE, spawn.getMnemonic()));
    }
    if (Iterables.isEmpty(spawn.getToolFiles())) {
        throw new UserExecException(String.format(ERROR_MESSAGE_PREFIX + REASON_NO_TOOLS, spawn.getMnemonic()));
    }
    FileOutErr outErr = actionExecutionContext.getFileOutErr();
    ImmutableList<String> args = ImmutableList.<String>builder().addAll(startupArgs).add("--persistent_worker").addAll(MoreObjects.firstNonNull(extraFlags.get(spawn.getMnemonic()), ImmutableList.<String>of())).build();
    ImmutableMap<String, String> env = spawn.getEnvironment();
    try {
        ActionInputFileCache inputFileCache = actionExecutionContext.getActionInputFileCache();
        HashCode workerFilesHash = WorkerFilesHash.getWorkerFilesHash(spawn.getToolFiles(), actionExecutionContext);
        Map<PathFragment, Path> inputFiles = new SpawnHelpers(execRoot).getMounts(spawn, actionExecutionContext);
        Set<PathFragment> outputFiles = SandboxHelpers.getOutputFiles(spawn);
        WorkerKey key = new WorkerKey(args, env, execRoot, spawn.getMnemonic(), workerFilesHash, inputFiles, outputFiles, writeOutputFiles != null);
        WorkRequest.Builder requestBuilder = WorkRequest.newBuilder();
        for (String flagfile : flagfiles) {
            expandArgument(requestBuilder, flagfile);
        }
        List<ActionInput> inputs = ActionInputHelper.expandArtifacts(spawn.getInputFiles(), actionExecutionContext.getArtifactExpander());
        for (ActionInput input : inputs) {
            byte[] digestBytes = inputFileCache.getDigest(input);
            ByteString digest;
            if (digestBytes == null) {
                digest = ByteString.EMPTY;
            } else {
                digest = ByteString.copyFromUtf8(HashCode.fromBytes(digestBytes).toString());
            }
            requestBuilder.addInputsBuilder().setPath(input.getExecPathString()).setDigest(digest).build();
        }
        WorkResponse response = execInWorker(eventHandler, key, requestBuilder.build(), maxRetries, writeOutputFiles);
        outErr.getErrorStream().write(response.getOutputBytes().toByteArray());
        if (response.getExitCode() != 0) {
            throw new UserExecException(String.format("Worker process sent response with exit code: %d.", response.getExitCode()));
        }
    } catch (IOException e) {
        String message = CommandFailureUtils.describeCommandFailure(verboseFailures, spawn.getArguments(), env, execRoot.getPathString());
        throw new UserExecException(message, e);
    }
}
Also used : FileOutErr(com.google.devtools.build.lib.util.io.FileOutErr) ActionInput(com.google.devtools.build.lib.actions.ActionInput) ByteString(com.google.protobuf.ByteString) ArrayList(java.util.ArrayList) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) EventHandler(com.google.devtools.build.lib.events.EventHandler) ByteString(com.google.protobuf.ByteString) Executor(com.google.devtools.build.lib.actions.Executor) HashCode(com.google.common.hash.HashCode) Path(com.google.devtools.build.lib.vfs.Path) SpawnHelpers(com.google.devtools.build.lib.sandbox.SpawnHelpers) UserExecException(com.google.devtools.build.lib.actions.UserExecException) IOException(java.io.IOException) ActionInputFileCache(com.google.devtools.build.lib.actions.ActionInputFileCache) WorkRequest(com.google.devtools.build.lib.worker.WorkerProtocol.WorkRequest) WorkResponse(com.google.devtools.build.lib.worker.WorkerProtocol.WorkResponse)

Aggregations

ActionInput (com.google.devtools.build.lib.actions.ActionInput)19 Artifact (com.google.devtools.build.lib.actions.Artifact)7 ArrayList (java.util.ArrayList)7 IOException (java.io.IOException)6 Path (com.google.devtools.build.lib.vfs.Path)5 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)5 Test (org.junit.Test)5 Executor (com.google.devtools.build.lib.actions.Executor)4 Spawn (com.google.devtools.build.lib.actions.Spawn)3 UserExecException (com.google.devtools.build.lib.actions.UserExecException)3 ByteString (com.google.protobuf.ByteString)3 StatusRuntimeException (io.grpc.StatusRuntimeException)3 TreeMap (java.util.TreeMap)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 TreeFileArtifact (com.google.devtools.build.lib.actions.Artifact.TreeFileArtifact)2 ArtifactPrefixConflictException (com.google.devtools.build.lib.actions.ArtifactPrefixConflictException)2 BaseSpawn (com.google.devtools.build.lib.actions.BaseSpawn)2 ExecException (com.google.devtools.build.lib.actions.ExecException)2 EventHandler (com.google.devtools.build.lib.events.EventHandler)2 Action (com.google.devtools.build.lib.remote.RemoteProtocol.Action)2