Merge "ProjectRepairer: Pass remote rsync path via --protect-args"
diff --git a/src/main/java/com/googlesource/gerrit/plugins/replication/Destination.java b/src/main/java/com/googlesource/gerrit/plugins/replication/Destination.java index 5704255..94633eb 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/replication/Destination.java +++ b/src/main/java/com/googlesource/gerrit/plugins/replication/Destination.java
@@ -881,12 +881,20 @@ return r; } - List<URIish> getDistributedUris(Project.NameKey project, String urlMatch) { - return getDistributedUris(project, getURIs(project, urlMatch)); + List<URIish> urlsToReplicateTo(Project.NameKey project, String urlMatch) { + return urlsToReplicateTo(project, getURIs(project, urlMatch)); } - List<URIish> getDistributedUris(Project.NameKey project, List<URIish> candidates) { - return urlDistributor.select(project, candidates); + List<URIish> urlsToReplicateTo(Project.NameKey project, List<URIish> candidates) { + return urlDistributor.urlsToReplicateTo(project, candidates); + } + + List<URIish> urlsToCreateOn(Project.NameKey project, List<URIish> candidates) { + return urlDistributor.urlsToCreateOn(project, candidates); + } + + List<URIish> urlsToDeleteFrom(Project.NameKey project, List<URIish> candidates) { + return urlDistributor.urlsToDeleteFrom(project, candidates); } URIish getURI(URIish template, Project.NameKey project) throws URISyntaxException {
diff --git a/src/main/java/com/googlesource/gerrit/plugins/replication/DestinationsCollection.java b/src/main/java/com/googlesource/gerrit/plugins/replication/DestinationsCollection.java index f752a4a..79035e7 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/replication/DestinationsCollection.java +++ b/src/main/java/com/googlesource/gerrit/plugins/replication/DestinationsCollection.java
@@ -145,11 +145,30 @@ validUris.add(uri); } } - config.getDistributedUris(projectName, validUris).forEach(uri -> uris.put(config, uri)); + distributedUris(config, filterType, projectName, validUris) + .forEach(uri -> uris.put(config, uri)); } return uris; } + /** Asks the remote's url distribution strategy what the operation at hand should reach. */ + private static List<URIish> distributedUris( + Destination config, + FilterType filterType, + Project.NameKey projectName, + List<URIish> candidates) { + switch (filterType) { + case PROJECT_CREATION: + return config.urlsToCreateOn(projectName, candidates); + case PROJECT_DELETION: + return config.urlsToDeleteFrom(projectName, candidates); + case ALL: + default: + // A HEAD update, which replicates a ref like any other. + return config.urlsToReplicateTo(projectName, candidates); + } + } + @Override public List<Destination> getAll(FilterType filterType) { Predicate<? super Destination> filter;
diff --git a/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationQueue.java b/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationQueue.java index 2ff785e..b78ab6e 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationQueue.java +++ b/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationQueue.java
@@ -236,7 +236,7 @@ } } if (!refNamesToPush.isEmpty()) { - for (URIish uri : cfg.getDistributedUris(project, urlMatch)) { + for (URIish uri : cfg.urlsToReplicateTo(project, urlMatch)) { replicationTasksStorage.create( ReplicateRefUpdate.create( project.get(), refNamesToPush, uri, cfg.getRemoteConfigName()));
diff --git a/src/main/java/com/googlesource/gerrit/plugins/replication/UrlDistributionStrategy.java b/src/main/java/com/googlesource/gerrit/plugins/replication/UrlDistributionStrategy.java index 50d6504..92095df 100644 --- a/src/main/java/com/googlesource/gerrit/plugins/replication/UrlDistributionStrategy.java +++ b/src/main/java/com/googlesource/gerrit/plugins/replication/UrlDistributionStrategy.java
@@ -55,7 +55,7 @@ private final AtomicInteger index = new AtomicInteger(); @Override - public List<URIish> select(NameKey project, List<URIish> candidates) { + public List<URIish> urlsToReplicateTo(NameKey project, List<URIish> candidates) { if (candidates.isEmpty()) { return List.of(); } @@ -98,7 +98,7 @@ public Instance newInstance() { return new Instance() { @Override - public List<URIish> select(NameKey project, List<URIish> candidates) { + public List<URIish> urlsToReplicateTo(NameKey project, List<URIish> candidates) { if (candidates.isEmpty()) { return List.of(); } @@ -146,21 +146,56 @@ .orElse(ALL); } - /** A stateful executor for a {@link UrlDistributionStrategy} strategy. */ + /** + * A stateful executor for a {@link UrlDistributionStrategy} strategy. + * + * <p>A strategy is asked per reason rather than once for every call site, so that what an + * operation does when a remote has several URLs is decided here instead of by the callers. + */ @FunctionalInterface public interface Instance { /** - * Selects the URLs to push to out of the candidates for the given project. + * Selects the URLs to replicate a project to out of the candidates, which is what pushing its + * refs, creating it and updating its HEAD all follow. * * @param project project being replicated, used by project-affine strategies. * @param candidates URLs the project could be pushed to. * @return the subset of candidates to push to. */ - List<URIish> select(Project.NameKey project, List<URIish> candidates); + List<URIish> urlsToReplicateTo(Project.NameKey project, List<URIish> candidates); /** - * If a push to any URI returned by {@link #select(Project.NameKey, List)} fails, {@link - * #failover(List, URIish)}} is invoked to select the next URI for retry. + * Selects the URLs to create a project on out of the candidates. + * + * <p>Defaults to the URLs the project is replicated to, since its refs have to land somewhere + * that holds it, and is separate from them so that a strategy can create a project on URLs it + * would not push every ref to. + * + * @param project project being created. + * @param candidates URLs the project could be created on. + * @return the subset of candidates to create on. + */ + default List<URIish> urlsToCreateOn(Project.NameKey project, List<URIish> candidates) { + return urlsToReplicateTo(project, candidates); + } + + /** + * Selects the URLs to delete a project from out of the candidates. + * + * <p>Defaults to the URLs the project is replicated to, which is where it was written, and is + * separate from them so that a strategy can delete from URLs it would not push to. + * + * @param project project being deleted. + * @param candidates URLs the project could be deleted from. + * @return the subset of candidates to delete from. + */ + default List<URIish> urlsToDeleteFrom(Project.NameKey project, List<URIish> candidates) { + return urlsToReplicateTo(project, candidates); + } + + /** + * If a push to any URI returned by {@link #urlsToReplicateTo(Project.NameKey, List)} fails, + * {@link #failover(List, URIish)}} is invoked to select the next URI for retry. */ default URIish failover(List<URIish> candidates, URIish failed) { return failed;
diff --git a/src/test/java/com/googlesource/gerrit/plugins/replication/UrlDistributionStrategyTest.java b/src/test/java/com/googlesource/gerrit/plugins/replication/UrlDistributionStrategyTest.java index 2ded31e..1eaa6eb 100644 --- a/src/test/java/com/googlesource/gerrit/plugins/replication/UrlDistributionStrategyTest.java +++ b/src/test/java/com/googlesource/gerrit/plugins/replication/UrlDistributionStrategyTest.java
@@ -60,7 +60,7 @@ ImmutableList<URIish> candidates = uris("replica1", "replica2", "replica3"); UrlDistributionStrategy.Instance distributor = UrlDistributionStrategy.ALL.newInstance(); - assertThat(distributor.select(PROJECT, candidates)).isEqualTo(candidates); + assertThat(distributor.urlsToReplicateTo(PROJECT, candidates)).isEqualTo(candidates); } @Test @@ -69,9 +69,12 @@ UrlDistributionStrategy.Instance distributor = UrlDistributionStrategy.ROUND_ROBIN.newInstance(); - assertThat(distributor.select(PROJECT, candidates)).containsExactly(candidates.get(0)); - assertThat(distributor.select(PROJECT, candidates)).containsExactly(candidates.get(1)); - assertThat(distributor.select(PROJECT, candidates)).containsExactly(candidates.get(0)); + assertThat(distributor.urlsToReplicateTo(PROJECT, candidates)) + .containsExactly(candidates.get(0)); + assertThat(distributor.urlsToReplicateTo(PROJECT, candidates)) + .containsExactly(candidates.get(1)); + assertThat(distributor.urlsToReplicateTo(PROJECT, candidates)) + .containsExactly(candidates.get(0)); } @Test @@ -80,8 +83,10 @@ UrlDistributionStrategy.Instance distributor = UrlDistributionStrategy.ROUND_ROBIN.newInstance(); - assertThat(distributor.select(PROJECT, candidates)).containsExactly(candidates.get(0)); - assertThat(distributor.select(OTHER_PROJECT, candidates)).containsExactly(candidates.get(1)); + assertThat(distributor.urlsToReplicateTo(PROJECT, candidates)) + .containsExactly(candidates.get(0)); + assertThat(distributor.urlsToReplicateTo(OTHER_PROJECT, candidates)) + .containsExactly(candidates.get(1)); } @Test @@ -90,7 +95,7 @@ UrlDistributionStrategy.Instance distributor = UrlDistributionStrategy.PROJECT_SHARDED.newInstance(); - List<URIish> selected = distributor.select(PROJECT, candidates); + List<URIish> selected = distributor.urlsToReplicateTo(PROJECT, candidates); assertThat(selected).hasSize(1); assertThat(candidates).containsAtLeastElementsIn(selected); @@ -102,9 +107,9 @@ UrlDistributionStrategy.Instance distributor = UrlDistributionStrategy.PROJECT_SHARDED.newInstance(); - List<URIish> first = distributor.select(PROJECT, candidates); + List<URIish> first = distributor.urlsToReplicateTo(PROJECT, candidates); for (int i = 0; i < 10; i++) { - assertThat(distributor.select(PROJECT, candidates)).isEqualTo(first); + assertThat(distributor.urlsToReplicateTo(PROJECT, candidates)).isEqualTo(first); } } @@ -113,9 +118,13 @@ ImmutableList<URIish> candidates = uris("replica1", "replica2", "replica3"); List<URIish> first = - UrlDistributionStrategy.PROJECT_SHARDED.newInstance().select(PROJECT, candidates); + UrlDistributionStrategy.PROJECT_SHARDED + .newInstance() + .urlsToReplicateTo(PROJECT, candidates); List<URIish> second = - UrlDistributionStrategy.PROJECT_SHARDED.newInstance().select(PROJECT, candidates); + UrlDistributionStrategy.PROJECT_SHARDED + .newInstance() + .urlsToReplicateTo(PROJECT, candidates); assertThat(second).isEqualTo(first); } @@ -127,13 +136,13 @@ UrlDistributionStrategy.Instance distributor = UrlDistributionStrategy.PROJECT_SHARDED.newInstance(); - List<URIish> expected = distributor.select(PROJECT, candidates); + List<URIish> expected = distributor.urlsToReplicateTo(PROJECT, candidates); List<URIish> shuffled = new ArrayList<>(candidates); Random random = new Random(42); for (int i = 0; i < 20; i++) { Collections.shuffle(shuffled, random); - assertThat(distributor.select(PROJECT, shuffled)).isEqualTo(expected); + assertThat(distributor.urlsToReplicateTo(PROJECT, shuffled)).isEqualTo(expected); } } @@ -145,7 +154,7 @@ Set<URIish> selected = new HashSet<>(); for (int i = 0; i < 100; i++) { - selected.addAll(distributor.select(Project.nameKey("project" + i), candidates)); + selected.addAll(distributor.urlsToReplicateTo(Project.nameKey("project" + i), candidates)); } assertThat(selected).containsExactlyElementsIn(candidates); @@ -157,14 +166,29 @@ UrlDistributionStrategy.Instance distributor = UrlDistributionStrategy.PROJECT_SHARDED.newInstance(); - assertThat(distributor.select(PROJECT, candidates)).isEqualTo(candidates); - assertThat(distributor.select(OTHER_PROJECT, candidates)).isEqualTo(candidates); + assertThat(distributor.urlsToReplicateTo(PROJECT, candidates)).isEqualTo(candidates); + assertThat(distributor.urlsToReplicateTo(OTHER_PROJECT, candidates)).isEqualTo(candidates); } @Test public void shouldSelectNothingWhenThereAreNoCandidates() { for (UrlDistributionStrategy strategy : UrlDistributionStrategy.values()) { - assertThat(strategy.newInstance().select(PROJECT, List.of())).isEmpty(); + assertThat(strategy.newInstance().urlsToReplicateTo(PROJECT, List.of())).isEmpty(); + } + } + + @Test + public void shouldCreateAndDeleteAProjectWhereItIsReplicatedTo() throws Exception { + ImmutableList<URIish> candidates = uris("replica1", "replica2", "replica3"); + + // Each strategy creates and deletes where it writes: on every URL for all, on the one URL the + // others pick. A fresh instance per call keeps the round-robin rotation out of the comparison. + for (UrlDistributionStrategy strategy : UrlDistributionStrategy.values()) { + List<URIish> replicated = strategy.newInstance().urlsToReplicateTo(PROJECT, candidates); + assertThat(strategy.newInstance().urlsToCreateOn(PROJECT, candidates)) + .containsExactlyElementsIn(replicated); + assertThat(strategy.newInstance().urlsToDeleteFrom(PROJECT, candidates)) + .containsExactlyElementsIn(replicated); } } }