Search in sources :

Example 11 with UserExecException

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

the class RemoteSpawnStrategy method execLocally.

/**
   * Fallback: execute the spawn locally. If an ActionKey is provided, try to upload results to
   * remote action cache.
   */
private void execLocally(Spawn spawn, ActionExecutionContext actionExecutionContext, RemoteActionCache actionCache, ActionKey actionKey) throws ExecException, InterruptedException {
    standaloneStrategy.exec(spawn, actionExecutionContext);
    if (options.remoteLocalExecUploadResults && actionCache != null && actionKey != null) {
        ArrayList<Path> outputFiles = new ArrayList<>();
        for (ActionInput output : spawn.getOutputFiles()) {
            outputFiles.add(execRoot.getRelative(output.getExecPathString()));
        }
        try {
            ActionResult.Builder result = ActionResult.newBuilder();
            actionCache.uploadAllResults(execRoot, outputFiles, result);
            actionCache.setCachedActionResult(actionKey, result.build());
        // Handle all cache errors here.
        } catch (IOException e) {
            throw new UserExecException("Unexpected IO error.", e);
        } catch (UnsupportedOperationException e) {
            actionExecutionContext.getExecutor().getEventHandler().handle(Event.warn(spawn.getMnemonic() + " unsupported operation for action cache (" + e + ")"));
        } catch (StatusRuntimeException e) {
            actionExecutionContext.getExecutor().getEventHandler().handle(Event.warn(spawn.getMnemonic() + " failed uploading results (" + e + ")"));
        }
    }
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) ActionInput(com.google.devtools.build.lib.actions.ActionInput) ActionResult(com.google.devtools.build.lib.remote.RemoteProtocol.ActionResult) ArrayList(java.util.ArrayList) UserExecException(com.google.devtools.build.lib.actions.UserExecException) StatusRuntimeException(io.grpc.StatusRuntimeException) IOException(java.io.IOException)

Example 12 with UserExecException

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

the class SpawnGccStrategy method execWithReply.

@Override
public CppCompileActionContext.Reply execWithReply(CppCompileAction action, ActionExecutionContext actionExecutionContext) throws ExecException, InterruptedException {
    if (action.getDotdFile() != null && action.getDotdFile().artifact() == null) {
        throw new UserExecException("cannot execute remotely or locally: " + action.getPrimaryInput().getExecPathString());
    }
    Iterable<Artifact> inputs = Iterables.concat(action.getInputs(), action.getAdditionalInputs());
    Spawn spawn = new SimpleSpawn(action, ImmutableList.copyOf(action.getArgv()), ImmutableMap.copyOf(action.getEnvironment()), ImmutableMap.copyOf(action.getExecutionInfo()), EmptyRunfilesSupplier.INSTANCE, ImmutableList.<Artifact>copyOf(inputs), /*tools=*/
    ImmutableList.<Artifact>of(), /*filesetManifests=*/
    ImmutableList.<Artifact>of(), action.getOutputs().asList(), action.estimateResourceConsumptionLocal());
    actionExecutionContext.getExecutor().getSpawnActionContext(action.getMnemonic()).exec(spawn, actionExecutionContext);
    return null;
}
Also used : SimpleSpawn(com.google.devtools.build.lib.actions.SimpleSpawn) UserExecException(com.google.devtools.build.lib.actions.UserExecException) Spawn(com.google.devtools.build.lib.actions.Spawn) SimpleSpawn(com.google.devtools.build.lib.actions.SimpleSpawn) Artifact(com.google.devtools.build.lib.actions.Artifact)

Example 13 with UserExecException

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

the class SandboxStrategy method runSpawn.

protected void runSpawn(Spawn spawn, ActionExecutionContext actionExecutionContext, Map<String, String> spawnEnvironment, SandboxExecRoot sandboxExecRoot, Set<PathFragment> outputs, SandboxRunner runner, AtomicReference<Class<? extends SpawnActionContext>> writeOutputFiles) throws ExecException, InterruptedException {
    EventHandler eventHandler = actionExecutionContext.getExecutor().getEventHandler();
    ExecException execException = null;
    OutErr outErr = actionExecutionContext.getFileOutErr();
    try {
        runner.run(spawn.getArguments(), spawnEnvironment, outErr, Spawns.getTimeoutSeconds(spawn), SandboxHelpers.shouldAllowNetwork(buildRequest, spawn), sandboxOptions.sandboxDebug, sandboxOptions.sandboxFakeHostname);
    } catch (ExecException e) {
        execException = e;
    }
    if (writeOutputFiles != null && !writeOutputFiles.compareAndSet(null, SandboxStrategy.class)) {
        throw new InterruptedException();
    }
    try {
        // We copy the outputs even when the command failed, otherwise StandaloneTestStrategy
        // won't be able to get the test logs of a failed test. (We should probably do this in
        // some better way.)
        sandboxExecRoot.copyOutputs(execRoot, outputs);
    } catch (IOException e) {
        if (execException == null) {
            throw new UserExecException("Could not move output artifacts from sandboxed execution", e);
        } else {
            // Catch the IOException and turn it into an error message, otherwise this might hide an
            // exception thrown during runner.run earlier.
            eventHandler.handle(Event.error("I/O exception while extracting output artifacts from sandboxed execution: " + e));
        }
    }
    if (execException != null) {
        outErr.printErr("Use --strategy=" + spawn.getMnemonic() + "=standalone to disable sandboxing for the failing actions.\n");
        throw execException;
    }
}
Also used : OutErr(com.google.devtools.build.lib.util.io.OutErr) EnvironmentalExecException(com.google.devtools.build.lib.actions.EnvironmentalExecException) UserExecException(com.google.devtools.build.lib.actions.UserExecException) ExecException(com.google.devtools.build.lib.actions.ExecException) UserExecException(com.google.devtools.build.lib.actions.UserExecException) EventHandler(com.google.devtools.build.lib.events.EventHandler) IOException(java.io.IOException)

Example 14 with UserExecException

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

the class LinuxSandboxedStrategy method actuallyExec.

public void actuallyExec(Spawn spawn, ActionExecutionContext actionExecutionContext, AtomicReference<Class<? extends SpawnActionContext>> writeOutputFiles) throws ExecException, InterruptedException {
    Executor executor = actionExecutionContext.getExecutor();
    SandboxHelpers.reportSubcommand(executor, spawn);
    // Each invocation of "exec" gets its own sandbox.
    Path sandboxPath = SandboxHelpers.getSandboxRoot(blazeDirs, productName, uuid, execCounter);
    Path sandboxExecRoot = sandboxPath.getRelative("execroot").getRelative(execRoot.getBaseName());
    Path sandboxTempDir = sandboxPath.getRelative("tmp");
    Set<Path> writableDirs = getWritableDirs(sandboxExecRoot, spawn.getEnvironment());
    SymlinkedExecRoot symlinkedExecRoot = new SymlinkedExecRoot(sandboxExecRoot);
    ImmutableSet<PathFragment> outputs = SandboxHelpers.getOutputFiles(spawn);
    try {
        symlinkedExecRoot.createFileSystem(getMounts(spawn, actionExecutionContext), outputs, writableDirs);
        sandboxTempDir.createDirectory();
    } catch (IOException e) {
        throw new UserExecException("I/O error during sandboxed execution", e);
    }
    SandboxRunner runner = getSandboxRunner(spawn, sandboxPath, sandboxExecRoot, sandboxTempDir);
    try {
        runSpawn(spawn, actionExecutionContext, spawn.getEnvironment(), symlinkedExecRoot, outputs, runner, writeOutputFiles);
    } finally {
        if (!sandboxOptions.sandboxDebug) {
            try {
                FileSystemUtils.deleteTree(sandboxPath);
            } catch (IOException e) {
                executor.getEventHandler().handle(Event.warn(String.format("Cannot delete sandbox directory after action execution: %s (%s)", sandboxPath.getPathString(), e)));
            }
        }
    }
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) Executor(com.google.devtools.build.lib.actions.Executor) PathFragment(com.google.devtools.build.lib.vfs.PathFragment) UserExecException(com.google.devtools.build.lib.actions.UserExecException) IOException(java.io.IOException)

Example 15 with UserExecException

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

the class LinuxSandboxedStrategy method validateBindMounts.

/**
   * This method does the following things: - If mount source does not exist on the host system,
   * throw an error message - If mount target exists, check whether the source and target are of the
   * same type - If mount target does not exist on the host system, throw an error message
   *
   * @param bindMounts the bind mounts map with target as key and source as value
   * @throws UserExecException
   */
private void validateBindMounts(SortedMap<Path, Path> bindMounts) throws UserExecException {
    for (SortedMap.Entry<Path, Path> bindMount : bindMounts.entrySet()) {
        final Path source = bindMount.getValue();
        final Path target = bindMount.getKey();
        // Mount source should exist in the file system
        if (!source.exists()) {
            throw new UserExecException(String.format("Mount source '%s' does not exist.", source));
        }
        // If target exists, but is not of the same type as the source, then we cannot mount it.
        if (target.exists()) {
            boolean areBothDirectories = source.isDirectory() && target.isDirectory();
            boolean isSourceFile = source.isFile() || source.isSymbolicLink();
            boolean isTargetFile = target.isFile() || target.isSymbolicLink();
            boolean areBothFiles = isSourceFile && isTargetFile;
            if (!(areBothDirectories || areBothFiles)) {
                // Source and target are not of the same type; we cannot mount it.
                throw new UserExecException(String.format("Mount target '%s' is not of the same type as mount source '%s'.", target, source));
            }
        } else {
            // Mount target should exist in the file system
            throw new UserExecException(String.format("Mount target '%s' does not exist. Bazel only supports bind mounting on top of " + "existing files/directories. Please create an empty file or directory at " + "the mount target path according to the type of mount source.", target));
        }
    }
}
Also used : Path(com.google.devtools.build.lib.vfs.Path) SortedMap(java.util.SortedMap) UserExecException(com.google.devtools.build.lib.actions.UserExecException)

Aggregations

UserExecException (com.google.devtools.build.lib.actions.UserExecException)15 IOException (java.io.IOException)11 Path (com.google.devtools.build.lib.vfs.Path)6 Executor (com.google.devtools.build.lib.actions.Executor)5 AbnormalTerminationException (com.google.devtools.build.lib.shell.AbnormalTerminationException)4 Command (com.google.devtools.build.lib.shell.Command)4 CommandException (com.google.devtools.build.lib.shell.CommandException)4 TerminationStatus (com.google.devtools.build.lib.shell.TerminationStatus)4 ActionInput (com.google.devtools.build.lib.actions.ActionInput)3 EventHandler (com.google.devtools.build.lib.events.EventHandler)3 PathFragment (com.google.devtools.build.lib.vfs.PathFragment)3 ArrayList (java.util.ArrayList)3 ActionResult (com.google.devtools.build.lib.remote.RemoteProtocol.ActionResult)2 CommandResult (com.google.devtools.build.lib.shell.CommandResult)2 FileOutErr (com.google.devtools.build.lib.util.io.FileOutErr)2 WorkResponse (com.google.devtools.build.lib.worker.WorkerProtocol.WorkResponse)2 ByteString (com.google.protobuf.ByteString)2 StatusRuntimeException (io.grpc.StatusRuntimeException)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 HashCode (com.google.common.hash.HashCode)1