// 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.runtime;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.devtools.build.lib.events.Event.of;
import static com.google.devtools.build.lib.events.EventKind.PROGRESS;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Multimap;
import com.google.common.collect.Sets;
import com.google.common.eventbus.Subscribe;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.devtools.build.lib.actions.ActionExecutedEvent;
import com.google.devtools.build.lib.actions.Artifact;
import com.google.devtools.build.lib.actions.EventReportingArtifacts;
import com.google.devtools.build.lib.analysis.BuildInfoEvent;
import com.google.devtools.build.lib.analysis.NoBuildEvent;
import com.google.devtools.build.lib.analysis.extra.ExtraAction;
import com.google.devtools.build.lib.buildeventstream.AbortedEvent;
import com.google.devtools.build.lib.buildeventstream.AnnounceBuildEventTransportsEvent;
import com.google.devtools.build.lib.buildeventstream.ArtifactGroupNamer;
import com.google.devtools.build.lib.buildeventstream.BuildCompletingEvent;
import com.google.devtools.build.lib.buildeventstream.BuildEvent;
import com.google.devtools.build.lib.buildeventstream.BuildEventId;
import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos.Aborted.AbortReason;
import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos.BuildEventId.NamedSetOfFilesId;
import com.google.devtools.build.lib.buildeventstream.BuildEventTransport;
import com.google.devtools.build.lib.buildeventstream.BuildEventTransportClosedEvent;
import com.google.devtools.build.lib.buildeventstream.BuildEventWithConfiguration;
import com.google.devtools.build.lib.buildeventstream.BuildEventWithOrderConstraint;
import com.google.devtools.build.lib.buildeventstream.ChainableEvent;
import com.google.devtools.build.lib.buildeventstream.LastBuildEvent;
import com.google.devtools.build.lib.buildeventstream.NullConfiguration;
import com.google.devtools.build.lib.buildeventstream.ProgressEvent;
import com.google.devtools.build.lib.buildtool.BuildRequest;
import com.google.devtools.build.lib.buildtool.buildevent.BuildCompleteEvent;
import com.google.devtools.build.lib.buildtool.buildevent.BuildInterruptedEvent;
import com.google.devtools.build.lib.buildtool.buildevent.BuildStartingEvent;
import com.google.devtools.build.lib.buildtool.buildevent.NoAnalyzeEvent;
import com.google.devtools.build.lib.buildtool.buildevent.NoExecutionEvent;
import com.google.devtools.build.lib.collect.nestedset.NestedSet;
import com.google.devtools.build.lib.collect.nestedset.NestedSetView;
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.util.Pair;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
/**
* Listens for {@link BuildEvent}s and streams them to the provided {@link BuildEventTransport}s.
*
*
The streamer takes care of closing all {@link BuildEventTransport}s. It does so after having
* received a {@link BuildCompleteEvent}. Furthermore, it emits two event types to the
* {@code eventBus}. After having received the first {@link BuildEvent} it emits a
* {@link AnnounceBuildEventTransportsEvent} that contains a list of all its transports.
* Furthermore, after a transport has been closed, it emits
* a {@link BuildEventTransportClosedEvent}.
*/
public class BuildEventStreamer implements EventHandler {
private final Collection transports;
private final Reporter reporter;
private Set announcedEvents;
private final Set postedEvents = new HashSet<>();
private final Set configurationsPosted = new HashSet<>();
private List> bufferedStdoutStderrPairs = new ArrayList<>();
private final Multimap pendingEvents = HashMultimap.create();
private int progressCount;
private final CountingArtifactGroupNamer artifactGroupNamer = new CountingArtifactGroupNamer();
private OutErrProvider outErrProvider;
private AbortReason abortReason = AbortReason.UNKNOWN;
// Will be set to true if the build was invoked through "bazel test".
private boolean isTestCommand;
// After a BuildCompetingEvent we might expect a whitelisted set of events. If non-null,
// the streamer is restricted to only allow those events and fully close after having seen
// them.
private Set finalEventsToCome = null;
// True, if we already closed the stream.
private boolean closed;
private static final Logger logger = Logger.getLogger(BuildEventStreamer.class.getName());
/**
* Provider for stdout and stderr output.
*/
public interface OutErrProvider {
/**
* Return the chunk of stdout that was produced since the last call to this function (or the
* beginning of the build, for the first call). It is the responsibility of the class
* implementing this interface to properly synchronize with simultaneously written output.
*/
String getOut();
/**
* Return the chunk of stderr that was produced since the last call to this function (or the
* beginning of the build, for the first call). It is the responsibility of the class
* implementing this interface to properly synchronize with simultaneously written output.
*/
String getErr();
}
private static class CountingArtifactGroupNamer implements ArtifactGroupNamer {
private final Map