Associate GitRepositoryManager indirectly through a provider Allow to have different implementations of the GitRepositoryManager through a generic provider. This allows to have the caller to have additional logic on the wrapped local disk implementation, such as cached-refdb. The existing direct binding to the LocalDiskRepositoryManager is used as legacy fallback. Change-Id: I5cf9edad521ccea438659f964a69eb8da1222889
diff --git a/src/main/java/com/googlesource/gerrit/plugins/replication/pull/LocalGitRepositoryManagerProvider.java b/src/main/java/com/googlesource/gerrit/plugins/replication/pull/LocalGitRepositoryManagerProvider.java new file mode 100644 index 0000000..ea8aa00 --- /dev/null +++ b/src/main/java/com/googlesource/gerrit/plugins/replication/pull/LocalGitRepositoryManagerProvider.java
@@ -0,0 +1,47 @@ +// Copyright (C) 2022 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.common.annotations.VisibleForTesting; +import com.google.common.base.MoreObjects; +import com.google.gerrit.common.Nullable; +import com.google.gerrit.server.git.GitRepositoryManager; +import com.google.gerrit.server.git.LocalDiskRepositoryManager; +import com.google.inject.Inject; +import com.google.inject.Provider; +import com.google.inject.Singleton; +import com.google.inject.name.Named; + +@Singleton +public class LocalGitRepositoryManagerProvider implements Provider<GitRepositoryManager> { + + @Inject(optional = true) + @Named("LocalDiskRepositoryManager") + @Nullable + private GitRepositoryManager gitRepositoryManager; + + private final LocalDiskRepositoryManager localDiskRepositoryManager; + + @VisibleForTesting + @Inject + public LocalGitRepositoryManagerProvider(LocalDiskRepositoryManager localDiskRepositoryManager) { + this.localDiskRepositoryManager = localDiskRepositoryManager; + } + + @Override + public GitRepositoryManager get() { + return MoreObjects.firstNonNull(gitRepositoryManager, localDiskRepositoryManager); + } +}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/replication/pull/api/DeleteRefCommand.java b/src/main/java/com/googlesource/gerrit/plugins/replication/pull/api/DeleteRefCommand.java index 2a3a79d..40e03f1 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/replication/pull/api/DeleteRefCommand.java +++ b/src/main/java/com/googlesource/gerrit/plugins/replication/pull/api/DeleteRefCommand.java
@@ -22,7 +22,7 @@ import com.google.gerrit.extensions.restapi.ResourceNotFoundException; import com.google.gerrit.extensions.restapi.RestApiException; import com.google.gerrit.server.events.EventDispatcher; -import com.google.gerrit.server.git.LocalDiskRepositoryManager; +import com.google.gerrit.server.git.GitRepositoryManager; import com.google.gerrit.server.permissions.PermissionBackend; import com.google.gerrit.server.permissions.PermissionBackendException; import com.google.gerrit.server.permissions.RefPermission; @@ -31,6 +31,7 @@ import com.google.inject.Inject; import com.googlesource.gerrit.plugins.replication.pull.Context; import com.googlesource.gerrit.plugins.replication.pull.FetchRefReplicatedEvent; +import com.googlesource.gerrit.plugins.replication.pull.LocalGitRepositoryManagerProvider; import com.googlesource.gerrit.plugins.replication.pull.PullReplicationStateLogger; import com.googlesource.gerrit.plugins.replication.pull.ReplicationState; import com.googlesource.gerrit.plugins.replication.pull.fetch.ApplyObject; @@ -49,7 +50,7 @@ private final DynamicItem<EventDispatcher> eventDispatcher; private final ProjectCache projectCache; private final PermissionBackend permissionBackend; - private final LocalDiskRepositoryManager gitManager; + private final GitRepositoryManager gitManager; @Inject public DeleteRefCommand( @@ -58,13 +59,13 @@ ApplyObject applyObject, PermissionBackend permissionBackend, DynamicItem<EventDispatcher> eventDispatcher, - LocalDiskRepositoryManager gitManager) { + LocalGitRepositoryManagerProvider gitManagerProvider) { this.fetchStateLog = fetchStateLog; this.projectCache = projectCache; this.applyObject = applyObject; this.eventDispatcher = eventDispatcher; this.permissionBackend = permissionBackend; - this.gitManager = gitManager; + this.gitManager = gitManagerProvider.get(); } public void deleteRef(Project.NameKey name, String refName, String sourceLabel)
diff --git a/src/main/java/com/googlesource/gerrit/plugins/replication/pull/fetch/ApplyObject.java b/src/main/java/com/googlesource/gerrit/plugins/replication/pull/fetch/ApplyObject.java index 2bb1caf..36356e9 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/replication/pull/fetch/ApplyObject.java +++ b/src/main/java/com/googlesource/gerrit/plugins/replication/pull/fetch/ApplyObject.java
@@ -15,8 +15,9 @@ package com.googlesource.gerrit.plugins.replication.pull.fetch; import com.google.gerrit.entities.Project; -import com.google.gerrit.server.git.LocalDiskRepositoryManager; +import com.google.gerrit.server.git.GitRepositoryManager; import com.google.inject.Inject; +import com.googlesource.gerrit.plugins.replication.pull.LocalGitRepositoryManagerProvider; import com.googlesource.gerrit.plugins.replication.pull.api.data.RevisionData; import com.googlesource.gerrit.plugins.replication.pull.api.data.RevisionObjectData; import com.googlesource.gerrit.plugins.replication.pull.api.exception.MissingParentObjectException; @@ -30,15 +31,15 @@ public class ApplyObject { - private final LocalDiskRepositoryManager gitManager; + private final GitRepositoryManager gitManager; - // NOTE: We do need specifically the LocalDiskRepositoryManager to make sure + // NOTE: We do need specifically the local GitRepositoryManager to make sure // to be able to write onto the directly physical repository without any wrapper. // Using for instance the multi-site wrapper injected by Guice would result // in a split-brain because of the misalignment of local vs. global refs values. @Inject - public ApplyObject(LocalDiskRepositoryManager gitManager) { - this.gitManager = gitManager; + public ApplyObject(LocalGitRepositoryManagerProvider gitManagerProvider) { + this.gitManager = gitManagerProvider.get(); } public RefUpdateState apply(Project.NameKey name, RefSpec refSpec, RevisionData[] revisionsData)
diff --git a/src/test/java/com/googlesource/gerrit/plugins/replication/pull/api/DeleteRefCommandTest.java b/src/test/java/com/googlesource/gerrit/plugins/replication/pull/api/DeleteRefCommandTest.java index 4415a4b..eb4d322 100644 --- a/src/test/java/com/googlesource/gerrit/plugins/replication/pull/api/DeleteRefCommandTest.java +++ b/src/test/java/com/googlesource/gerrit/plugins/replication/pull/api/DeleteRefCommandTest.java
@@ -33,6 +33,7 @@ import com.google.gerrit.server.project.ProjectCache; import com.google.gerrit.server.project.ProjectState; import com.googlesource.gerrit.plugins.replication.pull.FetchRefReplicatedEvent; +import com.googlesource.gerrit.plugins.replication.pull.LocalGitRepositoryManagerProvider; import com.googlesource.gerrit.plugins.replication.pull.PullReplicationStateLogger; import com.googlesource.gerrit.plugins.replication.pull.fetch.ApplyObject; import java.util.Optional; @@ -94,7 +95,7 @@ applyObject, permissionBackend, eventDispatcherDataItem, - gitManager); + new LocalGitRepositoryManagerProvider(gitManager)); } @Test