diff options
author | buchgr <buchgr@google.com> | 2017-06-14 12:37:48 +0200 |
---|---|---|
committer | Yun Peng <pcloudy@google.com> | 2017-06-14 13:17:24 +0200 |
commit | 624c706e49076912350277db746f5d43c484e591 (patch) | |
tree | 56aa2bba4b0d2763ca334817ee97c94517db0b79 /src/main/java/com/google/devtools/build/lib/buildeventservice | |
parent | 746655f21f2f555606db84183e5f90cee9cb3dd5 (diff) |
BES: Consolidate BuildEventServiceModule and BuildEventStreamerModule.
After having open sourced the Build Event Service code, there is no more need
to have two separate bazel modules that both create a BuildEventStreamer.
This change merges the BuildEventStreamerModule logic into the
BuildEventServiceModule.
DELTA=677 (330 added, 316 deleted, 31 changed)
DELTA_BY_EXTENSION=java=293,oss=32
RELNOTES: None.
PiperOrigin-RevId: 158960687
Diffstat (limited to 'src/main/java/com/google/devtools/build/lib/buildeventservice')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/buildeventservice/BuildEventServiceModule.java | 132 | ||||
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/buildeventservice/client/BUILD | 1 |
2 files changed, 89 insertions, 44 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/buildeventservice/BuildEventServiceModule.java b/src/main/java/com/google/devtools/build/lib/buildeventservice/BuildEventServiceModule.java index ad2b3fd9db..ad33471212 100644 --- a/src/main/java/com/google/devtools/build/lib/buildeventservice/BuildEventServiceModule.java +++ b/src/main/java/com/google/devtools/build/lib/buildeventservice/BuildEventServiceModule.java @@ -20,6 +20,7 @@ import static com.google.common.base.Strings.isNullOrEmpty; import static com.google.devtools.build.lib.buildeventservice.BuildEventServiceTransport.UPLOAD_FAILED_MESSAGE; import static java.lang.String.format; +import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.common.eventbus.Subscribe; @@ -27,8 +28,12 @@ import com.google.devtools.build.lib.authandtls.AuthAndTLSOptions; import com.google.devtools.build.lib.buildeventservice.client.BuildEventServiceClient; import com.google.devtools.build.lib.buildeventstream.BuildEvent; import com.google.devtools.build.lib.buildeventstream.BuildEventTransport; +import com.google.devtools.build.lib.buildeventstream.PathConverter; +import com.google.devtools.build.lib.buildeventstream.transports.BuildEventStreamOptions; +import com.google.devtools.build.lib.buildeventstream.transports.BuildEventTransportFactory; import com.google.devtools.build.lib.events.Event; import com.google.devtools.build.lib.events.EventHandler; +import com.google.devtools.build.lib.events.Reporter; import com.google.devtools.build.lib.runtime.BlazeModule; import com.google.devtools.build.lib.runtime.BuildEventStreamer; import com.google.devtools.build.lib.runtime.Command; @@ -47,10 +52,11 @@ import java.util.logging.Logger; import javax.annotation.Nullable; /** - * Module responsible for the {@link BuildEventTransport} and its {@link BuildEventStreamer}. + * Module responsible for the Build Event Transport (BEP) and Build Event Service (BES) + * functionality. * * Implementors of this class have to overwrite {@link #optionsClass()} and - * {@link #createBesClient(BuildEventServiceOptions)}. + * {@link #createBesClient(T besOptions, AuthAndTLSOptions authAndTLSOptions)}. */ public abstract class BuildEventServiceModule<T extends BuildEventServiceOptions> extends BlazeModule { @@ -78,7 +84,7 @@ public abstract class BuildEventServiceModule<T extends BuildEventServiceOptions @Override public Iterable<Class<? extends OptionsBase>> getCommandOptions(Command command) { - return ImmutableList.of(optionsClass(), AuthAndTLSOptions.class); + return ImmutableList.of(optionsClass(), AuthAndTLSOptions.class, BuildEventStreamOptions.class); } @Override @@ -98,6 +104,8 @@ public abstract class BuildEventServiceModule<T extends BuildEventServiceOptions commandEnvironment.getReporter(), commandEnvironment.getBlazeModuleEnvironment(), commandEnvironment.getRuntime().getClock(), + commandEnvironment.getRuntime().getPathToUriConverter(), + commandEnvironment.getReporter(), commandEnvironment.getClientEnv().get("BAZEL_INTERNAL_BUILD_REQUEST_ID"), commandEnvironment.getCommandId().toString()); if (streamer != null) { @@ -144,6 +152,8 @@ public abstract class BuildEventServiceModule<T extends BuildEventServiceOptions } commandEnvironment.getEventBus().unregister(buildEventRecorder); this.buildEventRecorder = null; + this.out = null; + this.err = null; } @Override @@ -159,67 +169,101 @@ public abstract class BuildEventServiceModule<T extends BuildEventServiceOptions * @param buildRequestId if {@code null} or {@code ""} a random UUID is used instead. */ @Nullable - private BuildEventStreamer tryCreateStreamer( + @VisibleForTesting + BuildEventStreamer tryCreateStreamer( OptionsProvider optionsProvider, EventHandler commandLineReporter, ModuleEnvironment moduleEnvironment, Clock clock, + PathConverter pathConverter, + Reporter reporter, String buildRequestId, String invocationId) { - T besOptions = null; try { - besOptions = + T besOptions = checkNotNull( optionsProvider.getOptions(optionsClass()), "Could not get BuildEventServiceOptions."); AuthAndTLSOptions authTlsOptions = checkNotNull(optionsProvider.getOptions(AuthAndTLSOptions.class), "Could not get AuthAndTLSOptions."); - if (isNullOrEmpty(besOptions.besBackend)) { - logger.fine("BuildEventServiceTransport is disabled."); - } else { - logger.fine(format("Will create BuildEventServiceTransport streaming to '%s'", - besOptions.besBackend)); - - buildRequestId = isNullOrEmpty(buildRequestId) - ? UUID.randomUUID().toString() - : buildRequestId; - commandLineReporter.handle( - Event.info( - format( - "Streaming Build Event Protocol to %s build_request_id: %s invocation_id: %s", - besOptions.besBackend, buildRequestId, invocationId))); - - BuildEventTransport besTransport = - new BuildEventServiceTransport( - createBesClient(besOptions, authTlsOptions), - besOptions.besTimeout, - besOptions.besBestEffort, - besOptions.besLifecycleEvents, - buildRequestId, - invocationId, - moduleEnvironment, - clock, - commandEnvironment.getRuntime().getPathToUriConverter(), - commandLineReporter, - besOptions.projectId); - logger.fine("BuildEventServiceTransport was created successfully"); - return new BuildEventStreamer(ImmutableSet.of(besTransport), - commandEnvironment.getReporter()); + BuildEventStreamOptions bepOptions = + checkNotNull(optionsProvider.getOptions(BuildEventStreamOptions.class), + "Could not get BuildEventStreamOptions."); + + BuildEventTransport besTransport = null; + try { + besTransport = tryCreateBesTransport(besOptions, authTlsOptions, buildRequestId, + invocationId, moduleEnvironment, clock, pathConverter, commandLineReporter); + } catch (Exception e) { + if (besOptions.besBestEffort) { + commandLineReporter.handle(Event.warn(format(UPLOAD_FAILED_MESSAGE, e.getMessage()))); + } else { + commandLineReporter.handle(Event.error(format(UPLOAD_FAILED_MESSAGE, e.getMessage()))); + moduleEnvironment.exit(new AbruptExitException(ExitCode.PUBLISH_ERROR)); + return null; + } } - } catch (Exception e) { - if (besOptions != null && besOptions.besBestEffort) { - commandLineReporter.handle(Event.warn(format(UPLOAD_FAILED_MESSAGE, e.getMessage()))); - } else { - commandLineReporter.handle(Event.error(format(UPLOAD_FAILED_MESSAGE, e.getMessage()))); - moduleEnvironment.exit(new AbruptExitException(ExitCode.PUBLISH_ERROR)); + + ImmutableSet<BuildEventTransport> bepTransports = + BuildEventTransportFactory.createFromOptions(bepOptions, pathConverter); + + ImmutableSet.Builder<BuildEventTransport> transportsBuilder = + ImmutableSet.<BuildEventTransport>builder().addAll(bepTransports); + if (besTransport != null) { + transportsBuilder.add(besTransport); + } + + ImmutableSet<BuildEventTransport> transports = transportsBuilder.build(); + if (!transports.isEmpty()) { + return new BuildEventStreamer(transports, reporter); } + } catch (Exception e) { + moduleEnvironment.exit(new AbruptExitException(ExitCode.LOCAL_ENVIRONMENTAL_ERROR, e)); } return null; } + @Nullable + private BuildEventTransport tryCreateBesTransport(T besOptions, AuthAndTLSOptions authTlsOptions, + String buildRequestId, String invocationId, ModuleEnvironment moduleEnvironment, Clock clock, + PathConverter pathConverter, EventHandler commandLineReporter) { + if (isNullOrEmpty(besOptions.besBackend)) { + logger.fine("BuildEventServiceTransport is disabled."); + return null; + } else { + logger.fine(format("Will create BuildEventServiceTransport streaming to '%s'", + besOptions.besBackend)); + + buildRequestId = isNullOrEmpty(buildRequestId) + ? UUID.randomUUID().toString() + : buildRequestId; + commandLineReporter.handle( + Event.info( + format( + "Streaming Build Event Protocol to %s build_request_id: %s invocation_id: %s", + besOptions.besBackend, buildRequestId, invocationId))); + + BuildEventTransport besTransport = + new BuildEventServiceTransport( + createBesClient(besOptions, authTlsOptions), + besOptions.besTimeout, + besOptions.besBestEffort, + besOptions.besLifecycleEvents, + buildRequestId, + invocationId, + moduleEnvironment, + clock, + pathConverter, + commandLineReporter, + besOptions.projectId); + logger.fine("BuildEventServiceTransport was created successfully"); + return besTransport; + } + } + protected abstract Class<T> optionsClass(); protected abstract BuildEventServiceClient createBesClient(T besOptions, AuthAndTLSOptions authAndTLSOptions); -}
\ No newline at end of file +} diff --git a/src/main/java/com/google/devtools/build/lib/buildeventservice/client/BUILD b/src/main/java/com/google/devtools/build/lib/buildeventservice/client/BUILD index 3a1032a630..23d349250a 100644 --- a/src/main/java/com/google/devtools/build/lib/buildeventservice/client/BUILD +++ b/src/main/java/com/google/devtools/build/lib/buildeventservice/client/BUILD @@ -9,6 +9,7 @@ java_library( srcs = glob(["*.java"]), visibility = [ "//src/main/java/com/google/devtools/build/lib:__pkg__", + "//src/test/java/com/google/devtools/build/lib/buildeventservice:__pkg__", ], runtime_deps = [ # This is required for client TLS. |