Inherit remote fetch replication events from a common parent
Change I29da700b introduced a common parent for all events that
represent a remote replication of a particular ref.
Inheriting from this parent allows to adhere to the provided interface,
providing a way to consume all such events in a structured way,
regardless whether they were originated by push or pull-replication.
Bug: Issue 14843
Change-Id: Ia5659d5c9445eb118d65948c5671ff19f31b57ec
diff --git a/src/main/java/com/googlesource/gerrit/plugins/replication/pull/FetchRefReplicatedEvent.java b/src/main/java/com/googlesource/gerrit/plugins/replication/pull/FetchRefReplicatedEvent.java
index 8bf257e..0eabf42 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/replication/pull/FetchRefReplicatedEvent.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/replication/pull/FetchRefReplicatedEvent.java
@@ -15,30 +15,23 @@
package com.googlesource.gerrit.plugins.replication.pull;
import com.google.gerrit.entities.Project;
-import com.google.gerrit.server.events.RefEvent;
+import com.googlesource.gerrit.plugins.replication.events.RemoteRefReplicationEvent;
import java.util.Objects;
import org.eclipse.jgit.lib.RefUpdate;
+import org.eclipse.jgit.transport.URIish;
-public class FetchRefReplicatedEvent extends RefEvent {
+public class FetchRefReplicatedEvent extends RemoteRefReplicationEvent {
static final String TYPE = "fetch-ref-replicated";
- final String project;
- final String ref;
- final String sourceNode;
- final String status;
final RefUpdate.Result refUpdateResult;
public FetchRefReplicatedEvent(
String project,
String ref,
- String sourceNode,
+ URIish sourceUri,
ReplicationState.RefFetchResult status,
RefUpdate.Result refUpdateResult) {
- super(TYPE);
- this.project = project;
- this.ref = ref;
- this.sourceNode = sourceNode;
- this.status = status.toString();
+ super(TYPE, project, ref, sourceUri, status.toString());
this.refUpdateResult = refUpdateResult;
}
@@ -63,7 +56,7 @@
if (!Objects.equals(event.ref, this.ref)) {
return false;
}
- if (!Objects.equals(event.sourceNode, this.sourceNode)) {
+ if (!Objects.equals(event.targetUri, this.targetUri)) {
return false;
}
if (!Objects.equals(event.status, this.status)) {
@@ -81,8 +74,4 @@
public String getRefName() {
return ref;
}
-
- public String getSourceNode() {
- return sourceNode;
- }
}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/replication/pull/FetchReplicationScheduledEvent.java b/src/main/java/com/googlesource/gerrit/plugins/replication/pull/FetchReplicationScheduledEvent.java
index 9a29c86..4f96a8f 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/replication/pull/FetchReplicationScheduledEvent.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/replication/pull/FetchReplicationScheduledEvent.java
@@ -15,20 +15,14 @@
package com.googlesource.gerrit.plugins.replication.pull;
import com.google.gerrit.entities.Project;
-import com.google.gerrit.server.events.RefEvent;
+import com.googlesource.gerrit.plugins.replication.events.RemoteRefReplicationEvent;
+import org.eclipse.jgit.transport.URIish;
-public class FetchReplicationScheduledEvent extends RefEvent {
+public class FetchReplicationScheduledEvent extends RemoteRefReplicationEvent {
static final String TYPE = "fetch-ref-replication-scheduled";
- final String project;
- final String ref;
- final String sourceNode;
-
- public FetchReplicationScheduledEvent(String project, String ref, String sourceNode) {
- super(TYPE);
- this.project = project;
- this.ref = ref;
- this.sourceNode = sourceNode;
+ public FetchReplicationScheduledEvent(String project, String ref, URIish sourceUri) {
+ super(TYPE, project, ref, sourceUri, null);
}
@Override
@@ -40,8 +34,4 @@
public Project.NameKey getProjectNameKey() {
return Project.nameKey(project);
}
-
- public String getSourceNode() {
- return sourceNode;
- }
}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/replication/pull/FetchResultProcessing.java b/src/main/java/com/googlesource/gerrit/plugins/replication/pull/FetchResultProcessing.java
index ab16318..eb94ecc 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/replication/pull/FetchResultProcessing.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/replication/pull/FetchResultProcessing.java
@@ -55,7 +55,7 @@
// Default doing nothing
}
- public static String resolveNodeName(URIish uri) {
+ private static String resolveNodeName(URIish uri) {
StringBuilder sb = new StringBuilder();
if (uri.isRemote()) {
sb.append(uri.getHost());
@@ -120,8 +120,7 @@
try {
Context.setLocalEvent(true);
dispatcher.postEvent(
- new FetchRefReplicatedEvent(
- project, ref, resolveNodeName(uri), status, refUpdateResult));
+ new FetchRefReplicatedEvent(project, ref, uri, status, refUpdateResult));
} catch (PermissionBackendException e) {
logger.atSevere().withCause(e).log(
"Cannot post event for ref '%s', project %s", ref, project);
@@ -189,8 +188,7 @@
URIish uri,
ReplicationState.RefFetchResult result,
RefUpdate.Result refUpdateResult) {
- postEvent(
- new FetchRefReplicatedEvent(project, ref, resolveNodeName(uri), result, refUpdateResult));
+ postEvent(new FetchRefReplicatedEvent(project, ref, uri, result, refUpdateResult));
}
@Override
diff --git a/src/main/java/com/googlesource/gerrit/plugins/replication/pull/Source.java b/src/main/java/com/googlesource/gerrit/plugins/replication/pull/Source.java
index 2d8bce4..9346ce5 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/replication/pull/Source.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/replication/pull/Source.java
@@ -15,7 +15,6 @@
package com.googlesource.gerrit.plugins.replication.pull;
import static com.googlesource.gerrit.plugins.replication.ReplicationFileBasedConfig.replaceName;
-import static com.googlesource.gerrit.plugins.replication.pull.FetchResultProcessing.resolveNodeName;
import static com.googlesource.gerrit.plugins.replication.pull.ReplicationType.SYNC;
import com.google.common.base.Throwables;
@@ -747,10 +746,9 @@
private void postReplicationScheduledEvent(FetchOne fetchOp, String inputRef) {
Set<String> refs = inputRef == null ? fetchOp.getRefs() : ImmutableSet.of(inputRef);
Project.NameKey project = fetchOp.getProjectNameKey();
- String targetNode = resolveNodeName(fetchOp.getURI());
for (String ref : refs) {
FetchReplicationScheduledEvent event =
- new FetchReplicationScheduledEvent(project.get(), ref, targetNode);
+ new FetchReplicationScheduledEvent(project.get(), ref, fetchOp.getURI());
try {
eventDispatcher.get().postEvent(BranchNameKey.create(project, ref), event);
} catch (PermissionBackendException e) {
@@ -761,13 +759,16 @@
private void postReplicationFailedEvent(FetchOne fetchOp, RefUpdate.Result result) {
Project.NameKey project = fetchOp.getProjectNameKey();
- String sourceNode = resolveNodeName(fetchOp.getURI());
try {
Context.setLocalEvent(true);
for (String ref : fetchOp.getRefs()) {
FetchRefReplicatedEvent event =
new FetchRefReplicatedEvent(
- project.get(), ref, sourceNode, ReplicationState.RefFetchResult.FAILED, result);
+ project.get(),
+ ref,
+ fetchOp.getURI(),
+ ReplicationState.RefFetchResult.FAILED,
+ result);
try {
eventDispatcher.get().postEvent(BranchNameKey.create(project, ref), event);
} catch (PermissionBackendException e) {
diff --git a/src/main/java/com/googlesource/gerrit/plugins/replication/pull/api/ApplyObjectCommand.java b/src/main/java/com/googlesource/gerrit/plugins/replication/pull/api/ApplyObjectCommand.java
index 276aa16..cdbd91e 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/replication/pull/api/ApplyObjectCommand.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/replication/pull/api/ApplyObjectCommand.java
@@ -37,9 +37,11 @@
import com.googlesource.gerrit.plugins.replication.pull.fetch.ApplyObject;
import com.googlesource.gerrit.plugins.replication.pull.fetch.RefUpdateState;
import java.io.IOException;
+import java.net.URISyntaxException;
import java.util.Set;
import org.eclipse.jgit.lib.RefUpdate;
import org.eclipse.jgit.transport.RefSpec;
+import org.eclipse.jgit.transport.URIish;
public class ApplyObjectCommand {
@@ -86,10 +88,10 @@
new FetchRefReplicatedEvent(
name.get(),
refName,
- sourceLabel,
+ new URIish(sourceLabel),
getStatus(refUpdateState),
refUpdateState.getResult()));
- } catch (PermissionBackendException e) {
+ } catch (PermissionBackendException | URISyntaxException e) {
logger.atSevere().withCause(e).log(
"Cannot post event for ref '%s', project %s", refName, name);
} finally {
diff --git a/src/test/java/com/googlesource/gerrit/plugins/replication/pull/FetchGitUpdateProcessingTest.java b/src/test/java/com/googlesource/gerrit/plugins/replication/pull/FetchGitUpdateProcessingTest.java
index 77dc947..68044b4 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/replication/pull/FetchGitUpdateProcessingTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/replication/pull/FetchGitUpdateProcessingTest.java
@@ -35,6 +35,7 @@
private GitUpdateProcessing gitUpdateProcessing;
private CommandProcessing commandProcessing;
private Command sshCommandMock;
+ private static URIish sourceUri;
@Before
public void setUp() throws Exception {
@@ -42,6 +43,7 @@
gitUpdateProcessing = new GitUpdateProcessing(dispatcherMock);
sshCommandMock = mock(Command.class);
commandProcessing = new CommandProcessing(sshCommandMock, dispatcherMock);
+ sourceUri = new URIish("git://someHost/someProject.git");
}
@Test
@@ -51,14 +53,14 @@
new FetchRefReplicatedEvent(
"someProject",
"refs/heads/master",
- "someHost",
+ sourceUri,
RefFetchResult.SUCCEEDED,
RefUpdate.Result.NEW);
gitUpdateProcessing.onOneProjectReplicationDone(
"someProject",
"refs/heads/master",
- new URIish("git://someHost/someProject.git"),
+ sourceUri,
RefFetchResult.SUCCEEDED,
RefUpdate.Result.NEW);
verify(dispatcherMock, times(1)).postEvent(eq(expectedEvent));
@@ -71,7 +73,7 @@
new FetchRefReplicatedEvent(
"someProject",
"refs/heads/master",
- "someHost",
+ sourceUri,
RefFetchResult.SUCCEEDED,
RefUpdate.Result.NEW);
@@ -90,7 +92,7 @@
new FetchRefReplicatedEvent(
"someProject",
"refs/changes/01/1/1",
- "someHost",
+ sourceUri,
RefFetchResult.FAILED,
RefUpdate.Result.REJECTED_OTHER_REASON);
diff --git a/src/test/java/com/googlesource/gerrit/plugins/replication/pull/event/EventsSerializationTest.java b/src/test/java/com/googlesource/gerrit/plugins/replication/pull/event/EventsSerializationTest.java
new file mode 100644
index 0000000..efa521c
--- /dev/null
+++ b/src/test/java/com/googlesource/gerrit/plugins/replication/pull/event/EventsSerializationTest.java
@@ -0,0 +1,83 @@
+// Copyright (C) 2021 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.event;
+
+import static com.google.common.truth.Truth.assertThat;
+import static org.junit.Assert.assertTrue;
+
+import com.google.common.base.Objects;
+import com.google.gerrit.server.events.EventGsonProvider;
+import com.google.gson.Gson;
+import com.googlesource.gerrit.plugins.replication.pull.FetchRefReplicatedEvent;
+import com.googlesource.gerrit.plugins.replication.pull.FetchReplicationScheduledEvent;
+import com.googlesource.gerrit.plugins.replication.pull.ReplicationState;
+import org.eclipse.jgit.lib.RefUpdate;
+import org.eclipse.jgit.transport.URIish;
+import org.junit.Before;
+import org.junit.Test;
+
+public class EventsSerializationTest {
+ private static URIish sourceUri;
+ private static final Gson eventGson = new EventGsonProvider().get();
+ private static final String TEST_PROJECT = "test_project";
+ private static final String TEST_REF = "refs/heads/master";
+
+ @Before
+ public void setUp() throws Exception {
+ sourceUri = new URIish(String.format("git://aSourceNode/%s.git", TEST_PROJECT));
+ }
+
+ @Test
+ public void shouldSerializeFetchRefReplicatedEvent() {
+ FetchRefReplicatedEvent origEvent =
+ new FetchRefReplicatedEvent(
+ TEST_PROJECT,
+ TEST_REF,
+ sourceUri,
+ ReplicationState.RefFetchResult.SUCCEEDED,
+ RefUpdate.Result.FAST_FORWARD);
+
+ assertThat(origEvent)
+ .isEqualTo(eventGson.fromJson(eventGson.toJson(origEvent), FetchRefReplicatedEvent.class));
+ }
+
+ @Test
+ public void shouldSerializeFetchReplicationScheduledEvent() {
+ FetchReplicationScheduledEvent origEvent =
+ new FetchReplicationScheduledEvent(TEST_PROJECT, TEST_REF, sourceUri);
+
+ assertTrue(
+ equals(
+ origEvent,
+ eventGson.fromJson(eventGson.toJson(origEvent), FetchReplicationScheduledEvent.class)));
+ }
+
+ private boolean equals(FetchReplicationScheduledEvent scheduledEvent, Object other) {
+ if (!(other instanceof FetchReplicationScheduledEvent)) {
+ return false;
+ }
+ FetchReplicationScheduledEvent event = (FetchReplicationScheduledEvent) other;
+ if (!Objects.equal(event.project, scheduledEvent.project)) {
+ return false;
+ }
+ if (!Objects.equal(event.ref, scheduledEvent.ref)) {
+ return false;
+ }
+ if (!Objects.equal(event.targetUri, scheduledEvent.targetUri)) {
+ return false;
+ }
+ return Objects.equal(event.status, scheduledEvent.status);
+ }
+}
diff --git a/src/test/java/com/googlesource/gerrit/plugins/replication/pull/event/FetchRefReplicatedEventHandlerTest.java b/src/test/java/com/googlesource/gerrit/plugins/replication/pull/event/FetchRefReplicatedEventHandlerTest.java
index e528eca..81a4fc0 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/replication/pull/event/FetchRefReplicatedEventHandlerTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/replication/pull/event/FetchRefReplicatedEventHandlerTest.java
@@ -28,17 +28,20 @@
import com.googlesource.gerrit.plugins.replication.pull.FetchRefReplicatedEvent;
import com.googlesource.gerrit.plugins.replication.pull.ReplicationState;
import org.eclipse.jgit.lib.RefUpdate;
+import org.eclipse.jgit.transport.URIish;
import org.junit.Before;
import org.junit.Test;
public class FetchRefReplicatedEventHandlerTest {
private ChangeIndexer changeIndexerMock;
private FetchRefReplicatedEventHandler fetchRefReplicatedEventHandler;
+ private static URIish sourceUri;
@Before
public void setUp() throws Exception {
changeIndexerMock = mock(ChangeIndexer.class);
fetchRefReplicatedEventHandler = new FetchRefReplicatedEventHandler(changeIndexerMock);
+ sourceUri = new URIish("git://aSourceNode/testProject.git");
}
@Test
@@ -52,7 +55,7 @@
new FetchRefReplicatedEvent(
projectNameKey.get(),
ref,
- "aSourceNode",
+ sourceUri,
ReplicationState.RefFetchResult.SUCCEEDED,
RefUpdate.Result.FAST_FORWARD));
verify(changeIndexerMock, times(1)).index(eq(projectNameKey), eq(changeId));
@@ -70,7 +73,7 @@
new FetchRefReplicatedEvent(
projectNameKey.get(),
ref,
- "aSourceNode",
+ sourceUri,
ReplicationState.RefFetchResult.SUCCEEDED,
RefUpdate.Result.FAST_FORWARD));
verify(changeIndexerMock, never()).index(eq(projectNameKey), eq(changeId));
@@ -85,7 +88,7 @@
new FetchRefReplicatedEvent(
projectNameKey.get(),
ref,
- "aSourceNode",
+ sourceUri,
ReplicationState.RefFetchResult.SUCCEEDED,
RefUpdate.Result.FAST_FORWARD));
verify(changeIndexerMock, never()).index(eq(projectNameKey), eq(changeId));
@@ -97,7 +100,7 @@
new FetchRefReplicatedEvent(
Project.nameKey("testProject").get(),
"invalidRef",
- "aSourceNode",
+ sourceUri,
ReplicationState.RefFetchResult.SUCCEEDED,
RefUpdate.Result.FAST_FORWARD));
verify(changeIndexerMock, never()).index(any(), any());
@@ -111,7 +114,7 @@
new FetchRefReplicatedEvent(
projectNameKey.get(),
ref,
- "aSourceNode",
+ sourceUri,
ReplicationState.RefFetchResult.FAILED,
RefUpdate.Result.FAST_FORWARD));
verify(changeIndexerMock, never()).index(any(), any());
@@ -125,7 +128,7 @@
new FetchRefReplicatedEvent(
projectNameKey.get(),
ref,
- "aSourceNode",
+ sourceUri,
ReplicationState.RefFetchResult.NOT_ATTEMPTED,
RefUpdate.Result.FAST_FORWARD));
verify(changeIndexerMock, never()).index(any(), any());