Skip to content

Commit 6ce603d

Browse files
aoeuicopybara-github
authored andcommitted
Decouple RemoteAnalysisMetadataWriter from
RemoteAnalysisCacheClient. These are not always the same backend. PiperOrigin-RevId: 824469729 Change-Id: I722dae089ff0be71ef9aec86c4fb50a119e2cacd
1 parent c3373ce commit 6ce603d

File tree

8 files changed

+88
-31
lines changed

8 files changed

+88
-31
lines changed

src/main/java/com/google/devtools/build/lib/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,7 @@ java_library(
523523
"//src/main/java/com/google/devtools/build/lib/skyframe/serialization/analysis:remote_analysis_caching_server_state",
524524
"//src/main/java/com/google/devtools/build/lib/skyframe/serialization/analysis:remote_analysis_caching_services_supplier",
525525
"//src/main/java/com/google/devtools/build/lib/skyframe/serialization/analysis:remote_analysis_json_log_writer",
526+
"//src/main/java/com/google/devtools/build/lib/skyframe/serialization/analysis:remote_analysis_metadata_writer",
526527
"//src/main/java/com/google/devtools/build/lib/unsafe:string",
527528
"//src/main/java/com/google/devtools/build/lib/util",
528529
"//src/main/java/com/google/devtools/build/lib/util:TestType",

src/main/java/com/google/devtools/build/lib/buildtool/BuildTool.java

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@
140140
import com.google.devtools.build.lib.skyframe.serialization.analysis.RemoteAnalysisCachingServerState;
141141
import com.google.devtools.build.lib.skyframe.serialization.analysis.RemoteAnalysisCachingServicesSupplier;
142142
import com.google.devtools.build.lib.skyframe.serialization.analysis.RemoteAnalysisJsonLogWriter;
143+
import com.google.devtools.build.lib.skyframe.serialization.analysis.RemoteAnalysisMetadataWriter;
143144
import com.google.devtools.build.lib.util.AbruptExitException;
144145
import com.google.devtools.build.lib.util.CrashFailureDetails;
145146
import com.google.devtools.build.lib.util.DetailedExitCode;
@@ -1087,18 +1088,20 @@ private void tryWriteSkycacheMetadata(
10871088
// This is a blocking call. We cannot finish the build until the metadata has been written
10881089
// and at this point there is nothing else to do in the build that could be done in
10891090
// parallel.
1090-
success =
1091-
dependenciesProvider
1092-
.getAnalysisCacheClient()
1093-
.addTopLevelTargets(
1094-
env.getCommandId().toString(),
1095-
skycacheMetadataParams.getEvaluatingVersion(),
1096-
skycacheMetadataParams.getConfigurationHash(),
1097-
skycacheMetadataParams.getBazelVersion(),
1098-
skycacheMetadataParams.getTargets(),
1099-
skycacheMetadataParams.getConfigFlags())
1100-
.get(SkycacheMetadataParams.TIMEOUT.toSeconds(), SECONDS);
1101-
} catch (TimeoutException | ExecutionException e) {
1091+
RemoteAnalysisMetadataWriter metadataWriter = dependenciesProvider.getMetadataWriter();
1092+
if (metadataWriter == null) {
1093+
message = "MetadataAnalysisCacheWriterService is unavailable";
1094+
} else {
1095+
success =
1096+
metadataWriter.addTopLevelTargets(
1097+
env.getCommandId().toString(),
1098+
skycacheMetadataParams.getEvaluatingVersion(),
1099+
skycacheMetadataParams.getConfigurationHash(),
1100+
skycacheMetadataParams.getBazelVersion(),
1101+
skycacheMetadataParams.getTargets(),
1102+
skycacheMetadataParams.getConfigFlags());
1103+
}
1104+
} catch (IOException e) {
11021105
// To avoid build failures for a UX-enhancing feature, errors writing build metadata do not
11031106
// cause the build to fail. Instead, we log the error and rely on external monitoring to
11041107
// detect issues with metadata writes.
@@ -1108,8 +1111,10 @@ private void tryWriteSkycacheMetadata(
11081111
env.getReporter().handle(Event.info("Skycache: Successfully wrote metadata to backend"));
11091112
} else {
11101113
env.getReporter()
1111-
.handle(Event.warn("Skycache: Failed to write metadata to backend:" + message));
1112-
logger.atSevere().log("Error writing metadata at end of build: %s", message);
1114+
.handle(
1115+
Event.warn(
1116+
"Skycache: Failed to write metadata to backend"
1117+
+ (message != null ? ": " + message : "")));
11131118
}
11141119
}
11151120

@@ -1278,7 +1283,8 @@ private static class RemoteAnalysisCachingDependenciesProviderImpl
12781283

12791284
private final Future<ObjectCodecs> objectCodecsFuture;
12801285
private final Future<FingerprintValueService> fingerprintValueServiceFuture;
1281-
@Nullable private final Future<RemoteAnalysisCacheClient> analysisCacheClient;
1286+
@Nullable private final Future<? extends RemoteAnalysisCacheClient> analysisCacheClient;
1287+
@Nullable private final Future<? extends RemoteAnalysisMetadataWriter> metadataWriter;
12821288
@Nullable private volatile AnalysisCacheInvalidator analysisCacheInvalidator;
12831289

12841290
// Non-final because the top level BuildConfigurationValue is determined just before analysis
@@ -1516,6 +1522,7 @@ private RemoteAnalysisCachingDependenciesProviderImpl(
15161522
servicesSupplier.configure(options, clientId, env.getBuildRequestId(), jsonLogWriter);
15171523
this.fingerprintValueServiceFuture = servicesSupplier.getFingerprintValueService();
15181524
this.analysisCacheClient = servicesSupplier.getAnalysisCacheClient();
1525+
this.metadataWriter = servicesSupplier.getMetadataWriter();
15191526
this.eventHandler = env.getReporter();
15201527
this.skycacheMetadataParams =
15211528
env.getBlazeWorkspace()
@@ -1654,6 +1661,20 @@ public RemoteAnalysisCacheClient getAnalysisCacheClient() {
16541661
}
16551662
}
16561663

1664+
@Override
1665+
@Nullable
1666+
public RemoteAnalysisMetadataWriter getMetadataWriter() {
1667+
if (metadataWriter == null) {
1668+
return null;
1669+
}
1670+
try {
1671+
return metadataWriter.get(CLIENT_LOOKUP_TIMEOUT_SEC, SECONDS);
1672+
} catch (InterruptedException | ExecutionException | TimeoutException e) {
1673+
logger.atWarning().withCause(e).log("Unable to initialize metadata writer");
1674+
return null;
1675+
}
1676+
}
1677+
16571678
@Nullable
16581679
@Override
16591680
public RemoteAnalysisJsonLogWriter getJsonLogWriter() {

src/main/java/com/google/devtools/build/lib/skyframe/serialization/analysis/BUILD

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ java_library(
3434
":remote_analysis_cache_client",
3535
":remote_analysis_caching_server_state",
3636
":remote_analysis_json_log_writer",
37+
":remote_analysis_metadata_writer",
3738
"//src/main/java/com/google/devtools/build/lib/analysis/config:build_options",
3839
"//src/main/java/com/google/devtools/build/lib/cmdline",
3940
"//src/main/java/com/google/devtools/build/lib/skyframe/serialization",
@@ -274,6 +275,7 @@ java_library(
274275
":options",
275276
":remote_analysis_cache_client",
276277
":remote_analysis_json_log_writer",
278+
":remote_analysis_metadata_writer",
277279
"//src/main/java/com/google/devtools/build/lib/skyframe/serialization",
278280
"//src/main/java/com/google/devtools/build/lib/util:abrupt_exit_exception",
279281
"//third_party:guava",
@@ -320,6 +322,11 @@ java_library(
320322
],
321323
)
322324

325+
java_library(
326+
name = "remote_analysis_metadata_writer",
327+
srcs = ["RemoteAnalysisMetadataWriter.java"],
328+
)
329+
323330
java_library(
324331
name = "missing_skyframe_entry_exception",
325332
srcs = ["MissingSkyframeEntryException.java"],

src/main/java/com/google/devtools/build/lib/skyframe/serialization/analysis/RemoteAnalysisCacheClient.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import com.google.common.util.concurrent.ListenableFuture;
1818
import com.google.devtools.build.lib.events.EventHandler;
1919
import com.google.protobuf.ByteString;
20-
import java.util.Collection;
2120

2221
/** Interface to the remote analysis cache. */
2322
public interface RemoteAnalysisCacheClient {
@@ -34,15 +33,6 @@ record Stats(long bytesSent, long bytesReceived, long requestsSent, long batches
3433
/** Returns the usage statistics. */
3534
Stats getStats();
3635

37-
/** Adds the cached targets into the metadata table */
38-
ListenableFuture<Boolean> addTopLevelTargets(
39-
String invocationId,
40-
long evaluatingVersion,
41-
String configurationHash,
42-
String bazelVersion,
43-
Collection<String> targets,
44-
Collection<String> configFlags);
45-
4636
/** Looks up the targets in the metadata table */
4737
void lookupTopLevelTargets(
4838
long evaluatingVersion,

src/main/java/com/google/devtools/build/lib/skyframe/serialization/analysis/RemoteAnalysisCachingDependenciesProvider.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ default boolean isRetrievalEnabled() {
7474

7575
RemoteAnalysisCacheClient getAnalysisCacheClient();
7676

77+
RemoteAnalysisMetadataWriter getMetadataWriter();
78+
7779
/** Returns the JSON log writer or null if this log is not enabled. */
7880
@Nullable
7981
RemoteAnalysisJsonLogWriter getJsonLogWriter();
@@ -158,6 +160,11 @@ public RemoteAnalysisCacheClient getAnalysisCacheClient() {
158160
throw new UnsupportedOperationException();
159161
}
160162

163+
@Override
164+
public RemoteAnalysisMetadataWriter getMetadataWriter() {
165+
throw new UnsupportedOperationException();
166+
}
167+
161168
@Override
162169
@Nullable
163170
public RemoteAnalysisJsonLogWriter getJsonLogWriter() {

src/main/java/com/google/devtools/build/lib/skyframe/serialization/analysis/RemoteAnalysisCachingOptions.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,7 @@ public RemoteAnalysisCacheModeConverter() {
223223
defaultValue = "true",
224224
documentationCategory = OptionDocumentationCategory.UNDOCUMENTED,
225225
effectTags = {OptionEffectTag.BAZEL_INTERNAL_CONFIGURATION},
226-
help =
227-
"A flag to switch on/off inserting and querying the metadata db (b/425247333). The idea"
228-
+ " is for this flag to only exist temporarily for a careful rollout of the feature"
229-
+ " then be deleted later. For writers it requires passing an analysis cache service"
230-
+ " address.")
226+
help = "A flag to switch on/off inserting and querying the metadata db (b/425247333).")
231227
public boolean analysisCacheEnableMetadataQueries;
232228

233229
@Option(

src/main/java/com/google/devtools/build/lib/skyframe/serialization/analysis/RemoteAnalysisCachingServicesSupplier.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,12 @@ default void configure(
6161
* <p>This may entail I/O so it is wrapped in a future.
6262
*/
6363
@Nullable // null if frontier-style invalidation is used instead of the cache service
64-
default ListenableFuture<RemoteAnalysisCacheClient> getAnalysisCacheClient() {
64+
default ListenableFuture<? extends RemoteAnalysisCacheClient> getAnalysisCacheClient() {
65+
return null;
66+
}
67+
68+
@Nullable
69+
default ListenableFuture<? extends RemoteAnalysisMetadataWriter> getMetadataWriter() {
6570
return null;
6671
}
6772

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2025 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.skyframe.serialization.analysis;
15+
16+
import java.io.IOException;
17+
import java.util.Collection;
18+
19+
/** Handle to a service that consumes metadata about uploaded analysis values. */
20+
public interface RemoteAnalysisMetadataWriter {
21+
/** Adds the cached targets into the metadata table */
22+
boolean addTopLevelTargets(
23+
String invocationId,
24+
long evaluatingVersion,
25+
String configurationHash,
26+
String blazeVersion,
27+
Collection<String> targets,
28+
Collection<String> configFlags)
29+
throws IOException;
30+
}

0 commit comments

Comments
 (0)