Introduce ReplicationTaskId as thread-local Allow other plugins and classes to access the current replication task id through a thread-local. Change-Id: Iacacda973fb5635877b4c68d468542dc69627181
diff --git a/src/main/java/com/googlesource/gerrit/plugins/replication/pull/FetchOne.java b/src/main/java/com/googlesource/gerrit/plugins/replication/pull/FetchOne.java index 7f69304..af944dc 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/replication/pull/FetchOne.java +++ b/src/main/java/com/googlesource/gerrit/plugins/replication/pull/FetchOne.java
@@ -15,6 +15,7 @@ package com.googlesource.gerrit.plugins.replication.pull; import static com.googlesource.gerrit.plugins.replication.pull.PullReplicationLogger.repLog; +import static com.googlesource.gerrit.plugins.replication.pull.ReplicationTaskId.withTaskId; import static java.util.concurrent.TimeUnit.NANOSECONDS; import com.google.common.annotations.VisibleForTesting; @@ -380,7 +381,7 @@ long startedAt = context.getStartTime(); long delay = NANOSECONDS.toMillis(startedAt - createdAt); git = gitManager.openRepository(projectName); - List<FetchRefSpec> fetchRefSpecs = runImpl(); + List<FetchRefSpec> fetchRefSpecs = withTaskId(taskIdHex, this::runImpl); if (fetchRefSpecs.isEmpty()) { repLog.info( @@ -580,18 +581,22 @@ * @return The list of refSpecs to fetch */ public List<FetchRefSpec> getFetchRefSpecs(boolean lock) throws IOException { - List<FetchRefSpec> configRefSpecs = - config.getFetchRefSpecs().stream().map(FetchRefSpec::fromRefSpec).toList(); + return withTaskId( + taskIdHex, + () -> { + List<FetchRefSpec> configRefSpecs = + config.getFetchRefSpecs().stream().map(FetchRefSpec::fromRefSpec).toList(); - if (delta.isEmpty() && replicationFetchFilter().isEmpty()) { - return configRefSpecs; - } + if (delta.isEmpty() && replicationFetchFilter().isEmpty()) { + return configRefSpecs; + } - return runRefsFilter(computeDeltaIfNeeded(), lock).stream() - .map(ref -> refToFetchRefSpec(ref, configRefSpecs)) - .filter(Optional::isPresent) - .map(Optional::get) - .collect(Collectors.toList()); + return runRefsFilter(computeDeltaIfNeeded(), lock).stream() + .map(ref -> refToFetchRefSpec(ref, configRefSpecs)) + .filter(Optional::isPresent) + .map(Optional::get) + .collect(Collectors.toList()); + }); } public void unlockRefSpecs(Map<String, AutoCloseable> locks) {
diff --git a/src/main/java/com/googlesource/gerrit/plugins/replication/pull/ReplicationTaskId.java b/src/main/java/com/googlesource/gerrit/plugins/replication/pull/ReplicationTaskId.java new file mode 100644 index 0000000..40b3ec0 --- /dev/null +++ b/src/main/java/com/googlesource/gerrit/plugins/replication/pull/ReplicationTaskId.java
@@ -0,0 +1,46 @@ +// Copyright (C) 2026 The Android Open Source Project +// +// 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.googlesource.gerrit.plugins.replication.pull; + +import com.google.gerrit.common.Nullable; +import java.io.IOException; + +public class ReplicationTaskId { + private static final ThreadLocal<String> currentTaskId = new ThreadLocal<>(); + + @FunctionalInterface + public interface ReplicationBody<T> { + T apply() throws IOException; + } + + static <T> T withTaskId(String taskId, ReplicationBody<T> body) throws IOException { + String oldTaskId = currentTaskId.get(); + currentTaskId.set(taskId); + try { + return body.apply(); + } finally { + if (oldTaskId == null) { + currentTaskId.remove(); + } else { + currentTaskId.set(oldTaskId); + } + } + } + + @Nullable + public static String get() { + return currentTaskId.get(); + } +}