blob: 0c19d41faa0e752b41b0a927e30b579bc760f47c [file] [log] [blame]
aehlig61c0da12017-10-26 18:36:14 +02001// Copyright 2017 The Bazel Authors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14package com.google.devtools.build.lib.buildeventstream;
15
ulfjack8c214fd2019-04-29 05:02:54 -070016import com.google.common.base.MoreObjects;
aehlig61c0da12017-10-26 18:36:14 +020017import com.google.common.collect.ImmutableList;
felly2ffb0af2019-11-18 09:12:52 -080018import com.google.common.util.concurrent.Futures;
19import com.google.common.util.concurrent.ListenableFuture;
steinman81668112019-11-08 14:13:07 -080020import com.google.devtools.build.lib.buildeventstream.BuildEvent.LocalFile.LocalFileCompression;
tomlu2348a752018-07-04 08:55:42 -070021import com.google.devtools.build.lib.buildeventstream.BuildEvent.LocalFile.LocalFileType;
janakr3ca24682020-04-01 09:12:03 -070022import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos.BuildEventId;
aehlig61c0da12017-10-26 18:36:14 +020023import com.google.devtools.build.lib.util.Pair;
24import com.google.devtools.build.lib.vfs.Path;
25import com.google.protobuf.ByteString;
aehlig61c0da12017-10-26 18:36:14 +020026import java.util.Collection;
felly2ffb0af2019-11-18 09:12:52 -080027import java.util.concurrent.ExecutionException;
28import java.util.logging.Level;
29import java.util.logging.Logger;
aehlig61c0da12017-10-26 18:36:14 +020030
31/** Event reporting on statistics about the build. */
32public class BuildToolLogs implements BuildEventWithOrderConstraint {
ulfjack8d37e0b2018-11-20 03:10:13 -080033 /** These values are posted as byte strings to the BEP. */
aehligb064d532017-11-14 00:59:01 -080034 private final Collection<Pair<String, ByteString>> directValues;
felly2ffb0af2019-11-18 09:12:52 -080035 /** These values are posted as Future URIs to the BEP. */
36 private final Collection<Pair<String, ListenableFuture<String>>> futureUris;
ulfjack8d37e0b2018-11-20 03:10:13 -080037 /**
38 * These values are local files that are uploaded if required, and turned into URIs as part of the
39 * process.
40 */
ulfjack8c214fd2019-04-29 05:02:54 -070041 private final Collection<LogFileEntry> logFiles;
aehlig61c0da12017-10-26 18:36:14 +020042
felly2ffb0af2019-11-18 09:12:52 -080043 private static final Logger logger = Logger.getLogger(BuildToolLogs.class.getName());
44
aehlig61c0da12017-10-26 18:36:14 +020045 public BuildToolLogs(
tomlu18bd7de2018-09-26 10:36:38 -070046 Collection<Pair<String, ByteString>> directValues,
felly2ffb0af2019-11-18 09:12:52 -080047 Collection<Pair<String, ListenableFuture<String>>> futureUris,
ulfjack8c214fd2019-04-29 05:02:54 -070048 Collection<LogFileEntry> logFiles) {
aehlig61c0da12017-10-26 18:36:14 +020049 this.directValues = directValues;
felly2ffb0af2019-11-18 09:12:52 -080050 this.futureUris = futureUris;
aehlig61c0da12017-10-26 18:36:14 +020051 this.logFiles = logFiles;
52 }
53
54 @Override
55 public BuildEventId getEventId() {
janakr3ca24682020-04-01 09:12:03 -070056 return BuildEventIdUtil.buildToolLogs();
aehlig61c0da12017-10-26 18:36:14 +020057 }
58
59 @Override
60 public Collection<BuildEventId> getChildrenEvents() {
tomlu2348a752018-07-04 08:55:42 -070061 return ImmutableList.of();
aehlig61c0da12017-10-26 18:36:14 +020062 }
63
64 @Override
tomlu2348a752018-07-04 08:55:42 -070065 public Collection<LocalFile> referencedLocalFiles() {
66 ImmutableList.Builder<LocalFile> localFiles = ImmutableList.builder();
ulfjack8c214fd2019-04-29 05:02:54 -070067 for (LogFileEntry logFile : logFiles) {
68 localFiles.add(logFile.toLocalFile());
ulfjack68aa4102018-06-15 01:40:02 -070069 }
tomlu2348a752018-07-04 08:55:42 -070070 return localFiles.build();
ulfjack68aa4102018-06-15 01:40:02 -070071 }
72
73 @Override
felly2ffb0af2019-11-18 09:12:52 -080074 public Collection<ListenableFuture<String>> remoteUploads() {
75 ImmutableList.Builder<ListenableFuture<String>> remoteUploads = ImmutableList.builder();
76 for (Pair<String, ListenableFuture<String>> uploadPair : futureUris) {
77 remoteUploads.add(uploadPair.getSecond());
78 }
79 return remoteUploads.build();
80 }
81
82 @Override
ulfjack26e586d2018-05-17 08:42:13 -070083 public BuildEventStreamProtos.BuildEvent asStreamProto(BuildEventContext converters) {
aehlig61c0da12017-10-26 18:36:14 +020084 BuildEventStreamProtos.BuildToolLogs.Builder toolLogs =
85 BuildEventStreamProtos.BuildToolLogs.newBuilder();
aehligb064d532017-11-14 00:59:01 -080086 for (Pair<String, ByteString> direct : directValues) {
aehlig61c0da12017-10-26 18:36:14 +020087 toolLogs.addLog(
88 BuildEventStreamProtos.File.newBuilder()
89 .setName(direct.getFirst())
aehligb064d532017-11-14 00:59:01 -080090 .setContents(direct.getSecond())
aehlig61c0da12017-10-26 18:36:14 +020091 .build());
92 }
felly2ffb0af2019-11-18 09:12:52 -080093 for (Pair<String, ListenableFuture<String>> directFuturePair : futureUris) {
94 String name = directFuturePair.getFirst();
95 ListenableFuture<String> directFuture = directFuturePair.getSecond();
96 try {
felly341ecd72019-11-20 09:31:50 -080097 String uri =
98 directFuture.isDone() && !directFuture.isCancelled()
99 ? Futures.getDone(directFuture)
100 : null;
101 if (uri != null) {
102 toolLogs.addLog(
103 BuildEventStreamProtos.File.newBuilder()
104 .setName(name)
105 .setUri(Futures.getDone(directFuture))
106 .build());
107 }
felly2ffb0af2019-11-18 09:12:52 -0800108 } catch (ExecutionException e) {
109 logger.log(Level.WARNING, "Skipping build tool log upload " + name, e);
110 }
tomlu18bd7de2018-09-26 10:36:38 -0700111 }
ulfjack8c214fd2019-04-29 05:02:54 -0700112 for (LogFileEntry logFile : logFiles) {
113 String uri = converters.pathConverter().apply(logFile.localPath);
tomlufb8332f2018-07-11 12:21:23 -0700114 if (uri != null) {
115 toolLogs.addLog(
ulfjack8c214fd2019-04-29 05:02:54 -0700116 BuildEventStreamProtos.File.newBuilder().setName(logFile.name).setUri(uri).build());
tomlufb8332f2018-07-11 12:21:23 -0700117 }
aehlig61c0da12017-10-26 18:36:14 +0200118 }
119 return GenericBuildEvent.protoChaining(this).setBuildToolLogs(toolLogs.build()).build();
120 }
121
122 @Override
123 public Collection<BuildEventId> postedAfter() {
janakr3ca24682020-04-01 09:12:03 -0700124 return ImmutableList.of(BuildEventIdUtil.buildFinished());
aehlig61c0da12017-10-26 18:36:14 +0200125 }
ulfjack8c214fd2019-04-29 05:02:54 -0700126
127 /** A local log file. */
128 public static class LogFileEntry {
129 private final String name;
130 private final Path localPath;
131 private final LocalFileType fileType;
steinman81668112019-11-08 14:13:07 -0800132 private final LocalFileCompression compression;
ulfjack8c214fd2019-04-29 05:02:54 -0700133
steinman81668112019-11-08 14:13:07 -0800134 public LogFileEntry(
135 String name, Path localPath, LocalFileType fileType, LocalFileCompression compression) {
ulfjack8c214fd2019-04-29 05:02:54 -0700136 this.name = name;
137 this.localPath = localPath;
138 this.fileType = fileType;
steinman81668112019-11-08 14:13:07 -0800139 this.compression = compression;
ulfjack8c214fd2019-04-29 05:02:54 -0700140 }
141
142 public String getName() {
143 return name;
144 }
145
146 public Path getLocalPath() {
147 return localPath;
148 }
149
steinmanaef41932019-11-14 11:24:27 -0800150 public LocalFileCompression getCompression() {
151 return compression;
152 }
153
ulfjack8c214fd2019-04-29 05:02:54 -0700154 LocalFile toLocalFile() {
steinman81668112019-11-08 14:13:07 -0800155 return new LocalFile(localPath, fileType, compression);
ulfjack8c214fd2019-04-29 05:02:54 -0700156 }
157
158 @Override
159 public String toString() {
160 return MoreObjects.toStringHelper(this)
161 .add("name", name)
162 .add("localPath", localPath)
163 .add("fileType", fileType)
164 .toString();
165 }
166 }
aehlig61c0da12017-10-26 18:36:14 +0200167}