diff options
Diffstat (limited to 'src/main/java')
6 files changed, 127 insertions, 26 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/runtime/BlazeCommandDispatcher.java b/src/main/java/com/google/devtools/build/lib/runtime/BlazeCommandDispatcher.java index 453cd7c24b..54803ea944 100644 --- a/src/main/java/com/google/devtools/build/lib/runtime/BlazeCommandDispatcher.java +++ b/src/main/java/com/google/devtools/build/lib/runtime/BlazeCommandDispatcher.java @@ -65,6 +65,7 @@ import java.util.Collection; import java.util.HashSet; import java.util.LinkedHashSet; import java.util.List; +import java.util.Optional; import java.util.Set; import java.util.concurrent.atomic.AtomicReference; import java.util.logging.Level; @@ -261,9 +262,9 @@ public class BlazeCommandDispatcher { } /** - * Executes a single command. Returns the Unix exit status for the Blaze - * client process, or throws {@link ShutdownBlazeServerException} to - * indicate that a command wants to shutdown the Blaze server. + * Executes a single command. Returns the Unix exit status for the Blaze client process, or throws + * {@link ShutdownBlazeServerException} to indicate that a command wants to shutdown the Blaze + * server. */ int exec( InvocationPolicy invocationPolicy, @@ -271,7 +272,9 @@ public class BlazeCommandDispatcher { OutErr outErr, LockingMode lockingMode, String clientDescription, - long firstContactTime) throws ShutdownBlazeServerException, InterruptedException { + long firstContactTime, + Optional<List<Pair<String, String>>> startupOptionsTaggedWithBazelRc) + throws ShutdownBlazeServerException, InterruptedException { OriginalCommandLineEvent originalCommandLine = new OriginalCommandLineEvent(args); Preconditions.checkNotNull(clientDescription); if (args.isEmpty()) { // Default to help command if no arguments specified. @@ -327,8 +330,16 @@ public class BlazeCommandDispatcher { outErr.printErrLn("Server shut down " + shutdownReason); return ExitCode.LOCAL_ENVIRONMENTAL_ERROR.getNumericExitCode(); } - return execExclusively(originalCommandLine, invocationPolicy, args, outErr, firstContactTime, - commandName, command, waitTimeInMs); + return execExclusively( + originalCommandLine, + invocationPolicy, + args, + outErr, + firstContactTime, + commandName, + command, + waitTimeInMs, + startupOptionsTaggedWithBazelRc); } catch (ShutdownBlazeServerException e) { shutdownReason = "explicitly by client " + currentClientDescription; throw e; @@ -348,7 +359,8 @@ public class BlazeCommandDispatcher { long firstContactTime, String commandName, BlazeCommand command, - long waitTimeInMs) + long waitTimeInMs, + Optional<List<Pair<String, String>>> startupOptionsTaggedWithBazelRc) throws ShutdownBlazeServerException { // Record the start time for the profiler. Do not put anything before this! long execStartTimeNanos = runtime.getClock().nanoTime(); @@ -489,6 +501,39 @@ public class BlazeCommandDispatcher { } if (commonOptions.announceRcOptions) { + if (startupOptionsTaggedWithBazelRc.isPresent()) { + String lastBlazerc = ""; + List<String> accumulatedStartupOptions = new ArrayList<>(); + for (Pair<String, String> option : startupOptionsTaggedWithBazelRc.get()) { + // Do not include the command line options, marked by the empty string. + if (option.getFirst().isEmpty()) { + continue; + } + + // If we've moved to a new blazerc in the list, print out the info from the last one, + // and clear the accumulated list. + if (!lastBlazerc.isEmpty() && !option.getFirst().equals(lastBlazerc)) { + String logMessage = + String.format( + "Reading 'startup' options from %s: %s", + lastBlazerc, String.join(", ", accumulatedStartupOptions)); + reporter.handle(Event.info(logMessage)); + accumulatedStartupOptions = new ArrayList<>(); + } + + lastBlazerc = option.getFirst(); + accumulatedStartupOptions.add(option.getSecond()); + } + // Print out the final blazerc-grouped list, if any startup options were provided by + // blazerc. + if (!lastBlazerc.isEmpty()) { + String logMessage = + String.format( + "Reading 'startup' options from %s: %s", + lastBlazerc, String.join(", ", accumulatedStartupOptions)); + reporter.handle(Event.info(logMessage)); + } + } for (String note : rcfileNotes) { reporter.handle(Event.info(note)); } @@ -546,14 +591,21 @@ public class BlazeCommandDispatcher { } /** - * For testing ONLY. Same as {@link #exec}, but automatically - * uses the current time. + * For testing ONLY. Same as {@link #exec(InvocationPolicy, List, OutErr, LockingMode, String, + * long, Optional<List<Pair<String, String>>>)}, but automatically uses the current time. */ @VisibleForTesting - public int exec(List<String> args, LockingMode lockingMode, String clientDescription, - OutErr originalOutErr) throws ShutdownBlazeServerException, InterruptedException { - return exec(InvocationPolicy.getDefaultInstance(), args, originalOutErr, LockingMode.ERROR_OUT, - clientDescription, runtime.getClock().currentTimeMillis()); + public int exec( + List<String> args, LockingMode lockingMode, String clientDescription, OutErr originalOutErr) + throws ShutdownBlazeServerException, InterruptedException { + return exec( + InvocationPolicy.getDefaultInstance(), + args, + originalOutErr, + LockingMode.ERROR_OUT, + clientDescription, + runtime.getClock().currentTimeMillis(), + Optional.empty() /* startupOptionBundles */); } /** @@ -590,7 +642,7 @@ public class BlazeCommandDispatcher { // handler, and later replayed. ExitCode earlyExitCode = checkCwdInWorkspace(workspace, commandAnnotation, commandName, eventHandler); - if (earlyExitCode != ExitCode.SUCCESS) { + if (!earlyExitCode.equals(ExitCode.SUCCESS)) { return earlyExitCode; } diff --git a/src/main/java/com/google/devtools/build/lib/runtime/BlazeRuntime.java b/src/main/java/com/google/devtools/build/lib/runtime/BlazeRuntime.java index d7bd5941d8..2d5e8f0510 100644 --- a/src/main/java/com/google/devtools/build/lib/runtime/BlazeRuntime.java +++ b/src/main/java/com/google/devtools/build/lib/runtime/BlazeRuntime.java @@ -63,6 +63,7 @@ import com.google.devtools.build.lib.util.CustomExitCodePublisher; import com.google.devtools.build.lib.util.ExitCode; import com.google.devtools.build.lib.util.LoggingUtil; import com.google.devtools.build.lib.util.OS; +import com.google.devtools.build.lib.util.Pair; import com.google.devtools.build.lib.util.Preconditions; import com.google.devtools.build.lib.util.ProcessUtils; import com.google.devtools.build.lib.util.ThreadUtils; @@ -95,6 +96,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Locale; import java.util.Map; +import java.util.Optional; import java.util.UUID; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; @@ -741,12 +743,24 @@ public final class BlazeRuntime { return e.getExitCode().getNumericExitCode(); } + ImmutableList.Builder<Pair<String, String>> startupOptionsFromCommandLine = + ImmutableList.builder(); + for (String option : commandLineOptions.getStartupArgs()) { + startupOptionsFromCommandLine.add(new Pair<>("", option)); + } + BlazeCommandDispatcher dispatcher = new BlazeCommandDispatcher(runtime); try { LOG.info(getRequestLogString(commandLineOptions.getOtherArgs())); - return dispatcher.exec(policy, commandLineOptions.getOtherArgs(), OutErr.SYSTEM_OUT_ERR, - LockingMode.ERROR_OUT, "batch client", runtime.getClock().currentTimeMillis()); + return dispatcher.exec( + policy, + commandLineOptions.getOtherArgs(), + OutErr.SYSTEM_OUT_ERR, + LockingMode.ERROR_OUT, + "batch client", + runtime.getClock().currentTimeMillis(), + Optional.of(startupOptionsFromCommandLine.build())); } catch (BlazeCommandDispatcher.ShutdownBlazeServerException e) { return e.getExitStatus(); } catch (InterruptedException e) { diff --git a/src/main/java/com/google/devtools/build/lib/runtime/CommandExecutor.java b/src/main/java/com/google/devtools/build/lib/runtime/CommandExecutor.java index 3b70c8656a..3d2b46d310 100644 --- a/src/main/java/com/google/devtools/build/lib/runtime/CommandExecutor.java +++ b/src/main/java/com/google/devtools/build/lib/runtime/CommandExecutor.java @@ -13,12 +13,15 @@ // limitations under the License. package com.google.devtools.build.lib.runtime; +import com.google.devtools.build.lib.runtime.BlazeCommandDispatcher.LockingMode; import com.google.devtools.build.lib.runtime.proto.InvocationPolicyOuterClass.InvocationPolicy; import com.google.devtools.build.lib.server.ServerCommand; +import com.google.devtools.build.lib.util.Pair; import com.google.devtools.build.lib.util.io.OutErr; import java.io.PrintWriter; import java.io.StringWriter; import java.util.List; +import java.util.Optional; import java.util.logging.Logger; /** @@ -44,14 +47,22 @@ public class CommandExecutor implements ServerCommand { InvocationPolicy invocationPolicy, List<String> args, OutErr outErr, - BlazeCommandDispatcher.LockingMode lockingMode, + LockingMode lockingMode, String clientDescription, - long firstContactTime) throws InterruptedException { + long firstContactTime, + Optional<List<Pair<String, String>>> startupOptionsTaggedWithBazelRc) + throws InterruptedException { LOG.info(BlazeRuntime.getRequestLogString(args)); try { - return dispatcher.exec(invocationPolicy, args, outErr, lockingMode, clientDescription, - firstContactTime); + return dispatcher.exec( + invocationPolicy, + args, + outErr, + lockingMode, + clientDescription, + firstContactTime, + startupOptionsTaggedWithBazelRc); } catch (BlazeCommandDispatcher.ShutdownBlazeServerException e) { if (e.getCause() != null) { StringWriter message = new StringWriter(); diff --git a/src/main/java/com/google/devtools/build/lib/runtime/CommonCommandOptions.java b/src/main/java/com/google/devtools/build/lib/runtime/CommonCommandOptions.java index 9284db7688..d5131c29d7 100644 --- a/src/main/java/com/google/devtools/build/lib/runtime/CommonCommandOptions.java +++ b/src/main/java/com/google/devtools/build/lib/runtime/CommonCommandOptions.java @@ -190,9 +190,10 @@ public class CommonCommandOptions extends OptionsBase { public boolean announceRcOptions; /** - * These are the actual default overrides. Each value is a pair of (command name, value). + * These are the actual default overrides. Each value is a tuple of (bazelrc index, command name, + * value). The blazerc index is a number used to find the blazerc in --rc_source's values. * - * <p>For example: "--default_override=build=--cpu=piii" + * <p>For example: "--default_override=rc:build=--cpu=piii" */ @Option( name = "default_override", diff --git a/src/main/java/com/google/devtools/build/lib/server/GrpcServerImpl.java b/src/main/java/com/google/devtools/build/lib/server/GrpcServerImpl.java index 6abef0e4ac..3f3b46b413 100644 --- a/src/main/java/com/google/devtools/build/lib/server/GrpcServerImpl.java +++ b/src/main/java/com/google/devtools/build/lib/server/GrpcServerImpl.java @@ -31,9 +31,11 @@ import com.google.devtools.build.lib.server.CommandProtos.PingRequest; import com.google.devtools.build.lib.server.CommandProtos.PingResponse; import com.google.devtools.build.lib.server.CommandProtos.RunRequest; import com.google.devtools.build.lib.server.CommandProtos.RunResponse; +import com.google.devtools.build.lib.server.CommandProtos.StartupOption; import com.google.devtools.build.lib.util.BlazeClock; import com.google.devtools.build.lib.util.Clock; import com.google.devtools.build.lib.util.ExitCode; +import com.google.devtools.build.lib.util.Pair; import com.google.devtools.build.lib.util.Preconditions; import com.google.devtools.build.lib.util.ThreadUtils; import com.google.devtools.build.lib.util.io.OutErr; @@ -59,6 +61,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.UUID; import java.util.concurrent.Exchanger; import java.util.concurrent.ExecutionException; @@ -809,6 +812,16 @@ public class GrpcServerImpl implements RPCServer { String commandId; int exitCode; + // TODO(b/63925394): This information needs to be passed to the GotOptionsEvent, which does not + // currently have the explicit startup options. See Improved Command Line Reporting design doc + // for details. + // Convert the startup options record to Java strings, source first. + ImmutableList.Builder<Pair<String, String>> startupOptions = ImmutableList.builder(); + for (StartupOption option : request.getStartupOptionsList()) { + startupOptions.add( + new Pair<>(option.getSource().toString(CHARSET), option.getOption().toString(CHARSET))); + } + try (RunningCommand command = new RunningCommand()) { commandId = command.id; @@ -837,7 +850,8 @@ public class GrpcServerImpl implements RPCServer { rpcOutErr, request.getBlockForLock() ? LockingMode.WAIT : LockingMode.ERROR_OUT, request.getClientDescription(), - clock.currentTimeMillis()); + clock.currentTimeMillis(), + Optional.of(startupOptions.build())); } catch (OptionsParsingException e) { rpcOutErr.printErrLn(e.getMessage()); exitCode = ExitCode.COMMAND_LINE_ERROR.getNumericExitCode(); diff --git a/src/main/java/com/google/devtools/build/lib/server/ServerCommand.java b/src/main/java/com/google/devtools/build/lib/server/ServerCommand.java index ef5f727bdb..f0574cdbaa 100644 --- a/src/main/java/com/google/devtools/build/lib/server/ServerCommand.java +++ b/src/main/java/com/google/devtools/build/lib/server/ServerCommand.java @@ -15,8 +15,10 @@ package com.google.devtools.build.lib.server; import com.google.devtools.build.lib.runtime.BlazeCommandDispatcher; import com.google.devtools.build.lib.runtime.proto.InvocationPolicyOuterClass.InvocationPolicy; +import com.google.devtools.build.lib.util.Pair; import com.google.devtools.build.lib.util.io.OutErr; import java.util.List; +import java.util.Optional; /** * The {@link RPCServer} calls an arbitrary command implementing this @@ -25,8 +27,13 @@ import java.util.List; public interface ServerCommand { /** - * Executes the request, writing any output or error messages into err. - * Returns 0 on success; any other value or exception indicates an error. + * Executes the request, writing any output or error messages into err. Returns 0 on success; any + * other value or exception indicates an error. + * + * @param startupOptionsTaggedWithBazelRc List of startup options in Pair(bazelRc, option) form. + * The empty string bazelRc is interpreted as the command line, and option should be in + * --[no]flag or --flag=value form. If we don't have access to this information (--batch), + * leave this parameter as Optional.empty(). */ int exec( InvocationPolicy policy, @@ -34,7 +41,9 @@ public interface ServerCommand { OutErr outErr, BlazeCommandDispatcher.LockingMode lockingMode, String clientDescription, - long firstContactTime) throws InterruptedException; + long firstContactTime, + Optional<List<Pair<String, String>>> startupOptionsTaggedWithBazelRc) + throws InterruptedException; /** * Whether the server needs to be shut down. |