aehlig | 61c0da1 | 2017-10-26 18:36:14 +0200 | [diff] [blame] | 1 | // 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. |
| 14 | package com.google.devtools.build.lib.buildeventstream; |
| 15 | |
ulfjack | 8c214fd | 2019-04-29 05:02:54 -0700 | [diff] [blame] | 16 | import com.google.common.base.MoreObjects; |
aehlig | 61c0da1 | 2017-10-26 18:36:14 +0200 | [diff] [blame] | 17 | import com.google.common.collect.ImmutableList; |
felly | 2ffb0af | 2019-11-18 09:12:52 -0800 | [diff] [blame] | 18 | import com.google.common.util.concurrent.Futures; |
| 19 | import com.google.common.util.concurrent.ListenableFuture; |
steinman | 8166811 | 2019-11-08 14:13:07 -0800 | [diff] [blame] | 20 | import com.google.devtools.build.lib.buildeventstream.BuildEvent.LocalFile.LocalFileCompression; |
tomlu | 2348a75 | 2018-07-04 08:55:42 -0700 | [diff] [blame] | 21 | import com.google.devtools.build.lib.buildeventstream.BuildEvent.LocalFile.LocalFileType; |
janakr | 3ca2468 | 2020-04-01 09:12:03 -0700 | [diff] [blame^] | 22 | import com.google.devtools.build.lib.buildeventstream.BuildEventStreamProtos.BuildEventId; |
aehlig | 61c0da1 | 2017-10-26 18:36:14 +0200 | [diff] [blame] | 23 | import com.google.devtools.build.lib.util.Pair; |
| 24 | import com.google.devtools.build.lib.vfs.Path; |
| 25 | import com.google.protobuf.ByteString; |
aehlig | 61c0da1 | 2017-10-26 18:36:14 +0200 | [diff] [blame] | 26 | import java.util.Collection; |
felly | 2ffb0af | 2019-11-18 09:12:52 -0800 | [diff] [blame] | 27 | import java.util.concurrent.ExecutionException; |
| 28 | import java.util.logging.Level; |
| 29 | import java.util.logging.Logger; |
aehlig | 61c0da1 | 2017-10-26 18:36:14 +0200 | [diff] [blame] | 30 | |
| 31 | /** Event reporting on statistics about the build. */ |
| 32 | public class BuildToolLogs implements BuildEventWithOrderConstraint { |
ulfjack | 8d37e0b | 2018-11-20 03:10:13 -0800 | [diff] [blame] | 33 | /** These values are posted as byte strings to the BEP. */ |
aehlig | b064d53 | 2017-11-14 00:59:01 -0800 | [diff] [blame] | 34 | private final Collection<Pair<String, ByteString>> directValues; |
felly | 2ffb0af | 2019-11-18 09:12:52 -0800 | [diff] [blame] | 35 | /** These values are posted as Future URIs to the BEP. */ |
| 36 | private final Collection<Pair<String, ListenableFuture<String>>> futureUris; |
ulfjack | 8d37e0b | 2018-11-20 03:10:13 -0800 | [diff] [blame] | 37 | /** |
| 38 | * These values are local files that are uploaded if required, and turned into URIs as part of the |
| 39 | * process. |
| 40 | */ |
ulfjack | 8c214fd | 2019-04-29 05:02:54 -0700 | [diff] [blame] | 41 | private final Collection<LogFileEntry> logFiles; |
aehlig | 61c0da1 | 2017-10-26 18:36:14 +0200 | [diff] [blame] | 42 | |
felly | 2ffb0af | 2019-11-18 09:12:52 -0800 | [diff] [blame] | 43 | private static final Logger logger = Logger.getLogger(BuildToolLogs.class.getName()); |
| 44 | |
aehlig | 61c0da1 | 2017-10-26 18:36:14 +0200 | [diff] [blame] | 45 | public BuildToolLogs( |
tomlu | 18bd7de | 2018-09-26 10:36:38 -0700 | [diff] [blame] | 46 | Collection<Pair<String, ByteString>> directValues, |
felly | 2ffb0af | 2019-11-18 09:12:52 -0800 | [diff] [blame] | 47 | Collection<Pair<String, ListenableFuture<String>>> futureUris, |
ulfjack | 8c214fd | 2019-04-29 05:02:54 -0700 | [diff] [blame] | 48 | Collection<LogFileEntry> logFiles) { |
aehlig | 61c0da1 | 2017-10-26 18:36:14 +0200 | [diff] [blame] | 49 | this.directValues = directValues; |
felly | 2ffb0af | 2019-11-18 09:12:52 -0800 | [diff] [blame] | 50 | this.futureUris = futureUris; |
aehlig | 61c0da1 | 2017-10-26 18:36:14 +0200 | [diff] [blame] | 51 | this.logFiles = logFiles; |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public BuildEventId getEventId() { |
janakr | 3ca2468 | 2020-04-01 09:12:03 -0700 | [diff] [blame^] | 56 | return BuildEventIdUtil.buildToolLogs(); |
aehlig | 61c0da1 | 2017-10-26 18:36:14 +0200 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | @Override |
| 60 | public Collection<BuildEventId> getChildrenEvents() { |
tomlu | 2348a75 | 2018-07-04 08:55:42 -0700 | [diff] [blame] | 61 | return ImmutableList.of(); |
aehlig | 61c0da1 | 2017-10-26 18:36:14 +0200 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | @Override |
tomlu | 2348a75 | 2018-07-04 08:55:42 -0700 | [diff] [blame] | 65 | public Collection<LocalFile> referencedLocalFiles() { |
| 66 | ImmutableList.Builder<LocalFile> localFiles = ImmutableList.builder(); |
ulfjack | 8c214fd | 2019-04-29 05:02:54 -0700 | [diff] [blame] | 67 | for (LogFileEntry logFile : logFiles) { |
| 68 | localFiles.add(logFile.toLocalFile()); |
ulfjack | 68aa410 | 2018-06-15 01:40:02 -0700 | [diff] [blame] | 69 | } |
tomlu | 2348a75 | 2018-07-04 08:55:42 -0700 | [diff] [blame] | 70 | return localFiles.build(); |
ulfjack | 68aa410 | 2018-06-15 01:40:02 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Override |
felly | 2ffb0af | 2019-11-18 09:12:52 -0800 | [diff] [blame] | 74 | 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 |
ulfjack | 26e586d | 2018-05-17 08:42:13 -0700 | [diff] [blame] | 83 | public BuildEventStreamProtos.BuildEvent asStreamProto(BuildEventContext converters) { |
aehlig | 61c0da1 | 2017-10-26 18:36:14 +0200 | [diff] [blame] | 84 | BuildEventStreamProtos.BuildToolLogs.Builder toolLogs = |
| 85 | BuildEventStreamProtos.BuildToolLogs.newBuilder(); |
aehlig | b064d53 | 2017-11-14 00:59:01 -0800 | [diff] [blame] | 86 | for (Pair<String, ByteString> direct : directValues) { |
aehlig | 61c0da1 | 2017-10-26 18:36:14 +0200 | [diff] [blame] | 87 | toolLogs.addLog( |
| 88 | BuildEventStreamProtos.File.newBuilder() |
| 89 | .setName(direct.getFirst()) |
aehlig | b064d53 | 2017-11-14 00:59:01 -0800 | [diff] [blame] | 90 | .setContents(direct.getSecond()) |
aehlig | 61c0da1 | 2017-10-26 18:36:14 +0200 | [diff] [blame] | 91 | .build()); |
| 92 | } |
felly | 2ffb0af | 2019-11-18 09:12:52 -0800 | [diff] [blame] | 93 | for (Pair<String, ListenableFuture<String>> directFuturePair : futureUris) { |
| 94 | String name = directFuturePair.getFirst(); |
| 95 | ListenableFuture<String> directFuture = directFuturePair.getSecond(); |
| 96 | try { |
felly | 341ecd7 | 2019-11-20 09:31:50 -0800 | [diff] [blame] | 97 | 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 | } |
felly | 2ffb0af | 2019-11-18 09:12:52 -0800 | [diff] [blame] | 108 | } catch (ExecutionException e) { |
| 109 | logger.log(Level.WARNING, "Skipping build tool log upload " + name, e); |
| 110 | } |
tomlu | 18bd7de | 2018-09-26 10:36:38 -0700 | [diff] [blame] | 111 | } |
ulfjack | 8c214fd | 2019-04-29 05:02:54 -0700 | [diff] [blame] | 112 | for (LogFileEntry logFile : logFiles) { |
| 113 | String uri = converters.pathConverter().apply(logFile.localPath); |
tomlu | fb8332f | 2018-07-11 12:21:23 -0700 | [diff] [blame] | 114 | if (uri != null) { |
| 115 | toolLogs.addLog( |
ulfjack | 8c214fd | 2019-04-29 05:02:54 -0700 | [diff] [blame] | 116 | BuildEventStreamProtos.File.newBuilder().setName(logFile.name).setUri(uri).build()); |
tomlu | fb8332f | 2018-07-11 12:21:23 -0700 | [diff] [blame] | 117 | } |
aehlig | 61c0da1 | 2017-10-26 18:36:14 +0200 | [diff] [blame] | 118 | } |
| 119 | return GenericBuildEvent.protoChaining(this).setBuildToolLogs(toolLogs.build()).build(); |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public Collection<BuildEventId> postedAfter() { |
janakr | 3ca2468 | 2020-04-01 09:12:03 -0700 | [diff] [blame^] | 124 | return ImmutableList.of(BuildEventIdUtil.buildFinished()); |
aehlig | 61c0da1 | 2017-10-26 18:36:14 +0200 | [diff] [blame] | 125 | } |
ulfjack | 8c214fd | 2019-04-29 05:02:54 -0700 | [diff] [blame] | 126 | |
| 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; |
steinman | 8166811 | 2019-11-08 14:13:07 -0800 | [diff] [blame] | 132 | private final LocalFileCompression compression; |
ulfjack | 8c214fd | 2019-04-29 05:02:54 -0700 | [diff] [blame] | 133 | |
steinman | 8166811 | 2019-11-08 14:13:07 -0800 | [diff] [blame] | 134 | public LogFileEntry( |
| 135 | String name, Path localPath, LocalFileType fileType, LocalFileCompression compression) { |
ulfjack | 8c214fd | 2019-04-29 05:02:54 -0700 | [diff] [blame] | 136 | this.name = name; |
| 137 | this.localPath = localPath; |
| 138 | this.fileType = fileType; |
steinman | 8166811 | 2019-11-08 14:13:07 -0800 | [diff] [blame] | 139 | this.compression = compression; |
ulfjack | 8c214fd | 2019-04-29 05:02:54 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | public String getName() { |
| 143 | return name; |
| 144 | } |
| 145 | |
| 146 | public Path getLocalPath() { |
| 147 | return localPath; |
| 148 | } |
| 149 | |
steinman | aef4193 | 2019-11-14 11:24:27 -0800 | [diff] [blame] | 150 | public LocalFileCompression getCompression() { |
| 151 | return compression; |
| 152 | } |
| 153 | |
ulfjack | 8c214fd | 2019-04-29 05:02:54 -0700 | [diff] [blame] | 154 | LocalFile toLocalFile() { |
steinman | 8166811 | 2019-11-08 14:13:07 -0800 | [diff] [blame] | 155 | return new LocalFile(localPath, fileType, compression); |
ulfjack | 8c214fd | 2019-04-29 05:02:54 -0700 | [diff] [blame] | 156 | } |
| 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 | } |
aehlig | 61c0da1 | 2017-10-26 18:36:14 +0200 | [diff] [blame] | 167 | } |