Revert "Remove publishing of stream events"
Revert "Remove publishing of stream events"
Revert submission 312871-centralize-stream-events-handling
Reason for revert: It broke existing functionality of events-* plugins
Reverted Changes:
I79a059deb:Remove publishing of stream events
I68906e9b4:Remove publishing of stream events
I632f7b900:Remove publishing of stream events
Iafe5a8155:Leverage stream events publishing from the events-...
Change-Id: Iccbfa651600c89e30705fe16d7149e04a7be9ec0
diff --git a/src/main/java/com/googlesource/gerrit/plugins/pubsub/Manager.java b/src/main/java/com/googlesource/gerrit/plugins/pubsub/Manager.java
index 1d5f649..01bd857 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/pubsub/Manager.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/pubsub/Manager.java
@@ -26,11 +26,16 @@
private final Set<TopicSubscriber> consumers;
private final BrokerApi brokerApi;
+ private final PubSubEventListener pubSubEventListener;
@Inject
- public Manager(Set<TopicSubscriber> consumers, BrokerApi brokerApi) {
+ public Manager(
+ Set<TopicSubscriber> consumers,
+ BrokerApi brokerApi,
+ PubSubEventListener pubSubEventListener) {
this.consumers = consumers;
this.brokerApi = brokerApi;
+ this.pubSubEventListener = pubSubEventListener;
}
@Override
@@ -43,5 +48,6 @@
@Override
public void stop() {
brokerApi.disconnect();
+ pubSubEventListener.disconnect();
}
}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/pubsub/Module.java b/src/main/java/com/googlesource/gerrit/plugins/pubsub/Module.java
index d1723c7..50fad61 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/pubsub/Module.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/pubsub/Module.java
@@ -18,6 +18,7 @@
import com.google.gerrit.extensions.config.FactoryModule;
import com.google.gerrit.extensions.events.LifecycleListener;
import com.google.gerrit.extensions.registration.DynamicSet;
+import com.google.gerrit.server.events.EventListener;
import com.google.inject.Inject;
import com.google.inject.Scopes;
import com.googlesource.gerrit.plugins.pubsub.local.EnvironmentChecker;
@@ -39,6 +40,7 @@
@Override
protected void configure() {
DynamicSet.bind(binder(), LifecycleListener.class).to(Manager.class);
+ DynamicSet.bind(binder(), EventListener.class).to(PubSubEventListener.class);
factory(PubSubPublisher.Factory.class);
factory(PubSubEventSubscriber.Factory.class);
diff --git a/src/main/java/com/googlesource/gerrit/plugins/pubsub/PubSubConfiguration.java b/src/main/java/com/googlesource/gerrit/plugins/pubsub/PubSubConfiguration.java
index 465c913..614d0aa 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/pubsub/PubSubConfiguration.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/pubsub/PubSubConfiguration.java
@@ -28,6 +28,7 @@
private static final String DEFAULT_ACK_DEADLINE_SECONDS = "10";
private static final String DEFAULT_SUBSCTIPRION_TIMEOUT = "10";
private static final String DEFAULT_SHUTDOWN_TIMEOUT = "10";
+ private static final String DEFAULT_STREAM_EVENTS_TOPIC = "gerrit";
private final String gcloudProject;
private final String subscriptionId;
@@ -36,6 +37,7 @@
private final Integer ackDeadlineSeconds;
private final Long subscribtionTimeoutInSeconds;
private final Long shutdownTimeoutInSeconds;
+ private final String streamEventsTopic;
private final PluginConfig fromGerritConfig;
@Inject
@@ -47,6 +49,8 @@
this.gcloudProject = getMandatoryString("gcloudProject");
this.subscriptionId = getMandatoryString("subscriptionId", instanceId);
this.privateKeyLocation = getMandatoryString("privateKeyLocation");
+ this.streamEventsTopic =
+ fromGerritConfig.getString("streamEventsTopic", DEFAULT_STREAM_EVENTS_TOPIC);
this.numberOfSubscribers =
Integer.parseInt(
fromGerritConfig.getString("numberOfSubscribers", DEFAULT_NUMBER_OF_SUBSCRIBERS));
@@ -90,6 +94,10 @@
return shutdownTimeoutInSeconds;
}
+ public String getStreamEventsTopic() {
+ return streamEventsTopic;
+ }
+
private String getMandatoryString(String name) throws IllegalStateException {
return getMandatoryString(name, null);
}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/pubsub/PubSubEventListener.java b/src/main/java/com/googlesource/gerrit/plugins/pubsub/PubSubEventListener.java
new file mode 100644
index 0000000..29ec263
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/pubsub/PubSubEventListener.java
@@ -0,0 +1,46 @@
+// 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.pubsub;
+
+import com.google.common.flogger.FluentLogger;
+import com.google.gerrit.server.events.Event;
+import com.google.gerrit.server.events.EventListener;
+import com.google.inject.Inject;
+import com.google.inject.Singleton;
+
+@Singleton
+public class PubSubEventListener implements EventListener {
+ private static final FluentLogger logger = FluentLogger.forEnclosingClass();
+
+ private final PubSubPublisher publisher;
+
+ @Inject
+ public PubSubEventListener(
+ PubSubPublisher.Factory publisherFactory, PubSubConfiguration configuration) {
+ this.publisher = publisherFactory.create(configuration.getStreamEventsTopic());
+ }
+
+ @Override
+ public void onEvent(Event event) {
+ publisher.publish(event);
+ }
+
+ public void disconnect() {
+ try {
+ publisher.close();
+ } catch (InterruptedException e) {
+ logger.atSevere().withCause(e).log("Disconnect failed");
+ }
+ }
+}
diff --git a/src/main/resources/Documentation/config.md b/src/main/resources/Documentation/config.md
index d5f9643..f3263d3 100644
--- a/src/main/resources/Documentation/config.md
+++ b/src/main/resources/Documentation/config.md
@@ -42,3 +42,8 @@
`plugin.events-gcloud-pubsub.subscribtionTimeoutInSeconds`
: Optional. Maximum time in seconds to wait for the subscriber to connect to GCloud PubSub topic.
Default: 10
+
+`plugin.events-gcloud-pubsub.streamEventsTopic`
+: Optional. Name of the GCloud PubSub topic for stream events. events-gcloud-pubsub plugin exposes
+ all stream events under this topic name.
+ Default: gerrit
diff --git a/src/test/java/com/googlesource/gerrit/plugins/pubsub/PubSubBrokerApiIT.java b/src/test/java/com/googlesource/gerrit/plugins/pubsub/PubSubBrokerApiIT.java
index 4a68b8b..d202f25 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/pubsub/PubSubBrokerApiIT.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/pubsub/PubSubBrokerApiIT.java
@@ -15,6 +15,8 @@
package com.googlesource.gerrit.plugins.pubsub;
import static com.google.common.truth.Truth.assertThat;
+import static java.util.stream.Collectors.counting;
+import static java.util.stream.Collectors.groupingBy;
import com.gerritforge.gerrit.eventbroker.BrokerApi;
import com.google.api.gax.core.NoCredentialsProvider;
@@ -36,18 +38,22 @@
import com.google.gerrit.server.events.Event;
import com.google.gerrit.server.events.EventGson;
import com.google.gerrit.server.events.ProjectCreatedEvent;
+import com.google.gerrit.server.events.RefUpdatedEvent;
import com.google.gson.Gson;
import com.google.inject.Inject;
import com.google.pubsub.v1.ProjectSubscriptionName;
import com.google.pubsub.v1.PullRequest;
import com.google.pubsub.v1.PullResponse;
import com.google.pubsub.v1.PushConfig;
+import com.google.pubsub.v1.ReceivedMessage;
import com.google.pubsub.v1.TopicName;
import com.googlesource.gerrit.plugins.pubsub.local.EnvironmentChecker;
import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import java.io.IOException;
import java.time.Duration;
+import java.util.List;
+import java.util.Map;
import java.util.function.Consumer;
import org.junit.Test;
import org.testcontainers.containers.PubSubEmulatorContainer;
@@ -129,6 +135,36 @@
@GerritConfig(
name = "plugin.events-gcloud-pubsub.privateKeyLocation",
value = PRIVATE_KEY_LOCATION)
+ public void shouldProduceStreamEvents() throws Exception {
+ String subscriptionId = "gerrit-subscription-id";
+ String topicId = "gerrit";
+ createSubscription(subscriptionId, topicId, channelProvider, credentialsProvider);
+
+ createChange();
+
+ readMessageAndValidate(
+ (pullResponse) -> {
+ List<ReceivedMessage> messages = pullResponse.getReceivedMessagesList();
+ assertThat(messages).hasSize(4);
+ Map<String, Long> messageTypeCount =
+ messages.stream()
+ .map(m -> gson.fromJson(m.getMessage().getData().toStringUtf8(), Map.class))
+ .map(m -> m.get("type").toString())
+ .collect(groupingBy(t -> t, counting()));
+
+ assertThat(messageTypeCount.get(RefUpdatedEvent.TYPE)).isEqualTo(3);
+ assertThat(messageTypeCount.get("patchset-created")).isEqualTo(1);
+ },
+ PROJECT_ID,
+ subscriptionId);
+ }
+
+ @Test
+ @GerritConfig(name = "plugin.events-gcloud-pubsub.gcloudProject", value = PROJECT_ID)
+ @GerritConfig(name = "plugin.events-gcloud-pubsub.subscriptionId", value = SUBSCRIPTION_ID)
+ @GerritConfig(
+ name = "plugin.events-gcloud-pubsub.privateKeyLocation",
+ value = PRIVATE_KEY_LOCATION)
public void shouldConsumeEvent() throws InterruptedException {
Event event = new ProjectCreatedEvent();
event.instanceId = DEFAULT_INSTANCE_ID;