You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by GitBox <gi...@apache.org> on 2022/02/22 11:31:19 UTC

[GitHub] [sling-org-apache-sling-distribution-journal] tmaret commented on a change in pull request #99: SLING-10077: Mode to raise events only locally

tmaret commented on a change in pull request #99:
URL: https://github.com/apache/sling-org-apache-sling-distribution-journal/pull/99#discussion_r811812613



##########
File path: src/main/java/org/apache/sling/distribution/journal/impl/publisher/DistributedEventNotifierManager.java
##########
@@ -0,0 +1,142 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.sling.distribution.journal.impl.publisher;
+
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.Map;
+
+import org.apache.sling.api.resource.ResourceResolverFactory;
+import org.apache.sling.discovery.TopologyEvent;
+import org.apache.sling.discovery.TopologyEventListener;
+import org.apache.sling.distribution.journal.MessagingProvider;
+import org.apache.sling.distribution.journal.bookkeeper.LocalStore;
+import org.apache.sling.distribution.journal.impl.discovery.TopologyChangeHandler;
+import org.apache.sling.distribution.journal.queue.PubQueueProvider;
+import org.apache.sling.distribution.journal.shared.Topics;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.event.EventAdmin;
+import org.osgi.service.metatype.annotations.AttributeDefinition;
+import org.osgi.service.metatype.annotations.Designate;
+import org.osgi.service.metatype.annotations.ObjectClassDefinition;
+
+import static org.apache.sling.discovery.TopologyEvent.Type;
+import static org.apache.sling.discovery.TopologyEvent.Type.TOPOLOGY_CHANGED;
+import static org.apache.sling.discovery.TopologyEvent.Type.TOPOLOGY_CHANGING;
+import static org.apache.sling.discovery.TopologyEvent.Type.TOPOLOGY_INIT;
+
+@Component(immediate = true, service = TopologyEventListener.class)
+@Designate(ocd = DistributedEventNotifierManager.Configuration.class)
+public class DistributedEventNotifierManager implements TopologyEventListener {
+
+    /*
+     * Register the package distributed event notifier service
+     * on all or only the leader instance in a cluster according
+     * to the configuration.
+     */
+
+    @Reference
+    private EventAdmin eventAdmin;
+
+    @Reference
+    private PubQueueProvider pubQueueCacheService;
+
+    @Reference
+    private MessagingProvider messagingProvider;
+
+    @Reference
+    private Topics topics;
+
+    @Reference
+    private ResourceResolverFactory resolverFactory;
+
+    private ServiceRegistration<TopologyChangeHandler> reg;
+
+    private BundleContext context;
+
+    private Configuration config;
+
+    private Map<String, LocalStore> localStores;
+
+    private boolean isLeader;
+
+    @Activate
+    public void activate(BundleContext context, Configuration config) {
+        this.context = context;
+        this.config = config;
+        this.localStores = new HashMap<>();
+        if (! config.deduplicateEvent()) {
+            registerService();
+        }
+    }
+
+    public void deactivate() {

Review comment:
       The method must be annotated with `@Deactivate` in order to be invoked by the OSGi framework.

##########
File path: src/main/java/org/apache/sling/distribution/journal/impl/publisher/DistributedEventNotifierManager.java
##########
@@ -0,0 +1,142 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 org.apache.sling.distribution.journal.impl.publisher;
+
+import java.util.HashMap;
+import java.util.Hashtable;
+import java.util.Map;
+
+import org.apache.sling.api.resource.ResourceResolverFactory;
+import org.apache.sling.discovery.TopologyEvent;
+import org.apache.sling.discovery.TopologyEventListener;
+import org.apache.sling.distribution.journal.MessagingProvider;
+import org.apache.sling.distribution.journal.bookkeeper.LocalStore;
+import org.apache.sling.distribution.journal.impl.discovery.TopologyChangeHandler;
+import org.apache.sling.distribution.journal.queue.PubQueueProvider;
+import org.apache.sling.distribution.journal.shared.Topics;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceRegistration;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Reference;
+import org.osgi.service.event.EventAdmin;
+import org.osgi.service.metatype.annotations.AttributeDefinition;
+import org.osgi.service.metatype.annotations.Designate;
+import org.osgi.service.metatype.annotations.ObjectClassDefinition;
+
+import static org.apache.sling.discovery.TopologyEvent.Type;
+import static org.apache.sling.discovery.TopologyEvent.Type.TOPOLOGY_CHANGED;
+import static org.apache.sling.discovery.TopologyEvent.Type.TOPOLOGY_CHANGING;
+import static org.apache.sling.discovery.TopologyEvent.Type.TOPOLOGY_INIT;
+
+@Component(immediate = true, service = TopologyEventListener.class)
+@Designate(ocd = DistributedEventNotifierManager.Configuration.class)
+public class DistributedEventNotifierManager implements TopologyEventListener {
+
+    /*
+     * Register the package distributed event notifier service
+     * on all or only the leader instance in a cluster according
+     * to the configuration.
+     */
+
+    @Reference
+    private EventAdmin eventAdmin;
+
+    @Reference
+    private PubQueueProvider pubQueueCacheService;
+
+    @Reference
+    private MessagingProvider messagingProvider;
+
+    @Reference
+    private Topics topics;
+
+    @Reference
+    private ResourceResolverFactory resolverFactory;
+
+    private ServiceRegistration<TopologyChangeHandler> reg;
+
+    private BundleContext context;
+
+    private Configuration config;
+
+    private Map<String, LocalStore> localStores;

Review comment:
       The `localStores` field should be kept only in `PackageDistributedNotifier`. The field would be instantiated directly in `PackageDistributedNotifier` instead of being passed by reference through the constructor.

##########
File path: src/main/java/org/apache/sling/distribution/journal/impl/publisher/PackageDistributedNotifier.java
##########
@@ -34,49 +30,69 @@
 import org.apache.sling.distribution.journal.queue.PubQueueProvider;
 import org.apache.sling.distribution.journal.queue.QueueItemFactory;
 import org.apache.sling.distribution.journal.shared.Topics;
-import org.apache.sling.distribution.journal.MessagingProvider;
-
-import org.apache.commons.lang3.StringUtils;
 import org.apache.sling.distribution.queue.DistributionQueueItem;
-import org.osgi.service.component.annotations.Activate;
-import org.osgi.service.component.annotations.Component;
-import org.osgi.service.component.annotations.Reference;
 import org.osgi.service.event.Event;
 import org.osgi.service.event.EventAdmin;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import javax.annotation.ParametersAreNonnullByDefault;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+import java.util.function.Consumer;
+import java.util.function.Supplier;
+import java.util.stream.LongStream;
+
+import static org.apache.sling.distribution.journal.impl.subscriber.DistributionSubscriber.escapeTopicName;
 import static org.apache.sling.distribution.packaging.DistributionPackageInfo.PROPERTY_REQUEST_DEEP_PATHS;
 import static org.apache.sling.distribution.packaging.DistributionPackageInfo.PROPERTY_REQUEST_PATHS;
 
-@Component(immediate = true)
 @ParametersAreNonnullByDefault
 public class PackageDistributedNotifier implements TopologyChangeHandler {
 
     private static final Logger LOG = LoggerFactory.getLogger(PackageDistributedNotifier.class);
 
-    @Reference
-    private EventAdmin eventAdmin;
+    public static final String STORE_TYPE_OFFSETS = "lastRaisedEventOffset";
+
+    private final Map<String, Long> lastDistributedOffsets = new HashMap<>();
 
-    @Reference
-    private PubQueueProvider pubQueueCacheService;
+    private final EventAdmin eventAdmin;
 
-    @Reference
-    private MessagingProvider messagingProvider;
+    private final PubQueueProvider pubQueueCacheService;
+
+    private  MessagingProvider messagingProvider;
 
-    @Reference
     private Topics topics;
 
+    private ResourceResolverFactory resolverFactory;
+
+    private Map<String, LocalStore> localStores;
+
     private Consumer<PackageDistributedMessage> sender;
 
-    private boolean sendMsg;
+    private final boolean sendMsg;
+
+    public PackageDistributedNotifier(EventAdmin eventAdmin, PubQueueProvider pubQueueCacheService, MessagingProvider messagingProvider, Topics topics, ResourceResolverFactory resolverFactory, Map<String, LocalStore> localStores) {
+        this.eventAdmin = eventAdmin;
+        this.pubQueueCacheService = pubQueueCacheService;
+        this.messagingProvider = messagingProvider;
+        this.topics = topics;
+        this.resolverFactory = resolverFactory;
+        this.localStores = localStores;
 
-    @Activate
-    public void activate() {
         sendMsg = StringUtils.isNotBlank(topics.getEventTopic());
         if (sendMsg) {
             sender = messagingProvider.createSender(topics.getEventTopic());
         }
+
+        // load the last distributed offset from the store
+        for (Map.Entry<String, LocalStore> localStoreEntry : this.localStores.entrySet()) {

Review comment:
       Currently `localStoreEntry` starts empty. The loop will never set the `lastDistributedOffsets` field from stored values. This could be solved by loading the `lastDistributedOffsets` if needed and upon use in the `processOffsets` method.
   
   This loop in the activate method would be removed. Computing `minOffset` in `processOffsets` would be done with somethings along the lines of
   
   ```
   long minOffset = Math.min(offsets.get().findFirst().getAsLong(), lastDistributedOffsets.computeIfAbsent(pubAgentName, this::storedLastDistributedOffset));
   ```
   and 
   ```
   private long storedLastDistributedOffset(String pubAgentName) {
       return localStores.computeIfAbsent(pubAgentName, this::newLocalStore).load(STORE_TYPE_OFFSETS, Long.MAX_VALUE);
   }
   ```
   
   The `storeLastDistributedOffset` would no longer create `LocalStore` with `computeIfAbsent` and instead simply skip stores not present in the `localStores` field.

##########
File path: src/main/java/org/apache/sling/distribution/journal/impl/publisher/PackageDistributedNotifier.java
##########
@@ -34,49 +30,69 @@
 import org.apache.sling.distribution.journal.queue.PubQueueProvider;
 import org.apache.sling.distribution.journal.queue.QueueItemFactory;
 import org.apache.sling.distribution.journal.shared.Topics;
-import org.apache.sling.distribution.journal.MessagingProvider;
-
-import org.apache.commons.lang3.StringUtils;
 import org.apache.sling.distribution.queue.DistributionQueueItem;
-import org.osgi.service.component.annotations.Activate;
-import org.osgi.service.component.annotations.Component;
-import org.osgi.service.component.annotations.Reference;
 import org.osgi.service.event.Event;
 import org.osgi.service.event.EventAdmin;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import javax.annotation.ParametersAreNonnullByDefault;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+import java.util.function.Consumer;
+import java.util.function.Supplier;
+import java.util.stream.LongStream;
+
+import static org.apache.sling.distribution.journal.impl.subscriber.DistributionSubscriber.escapeTopicName;
 import static org.apache.sling.distribution.packaging.DistributionPackageInfo.PROPERTY_REQUEST_DEEP_PATHS;
 import static org.apache.sling.distribution.packaging.DistributionPackageInfo.PROPERTY_REQUEST_PATHS;
 
-@Component(immediate = true)
 @ParametersAreNonnullByDefault
 public class PackageDistributedNotifier implements TopologyChangeHandler {
 
     private static final Logger LOG = LoggerFactory.getLogger(PackageDistributedNotifier.class);
 
-    @Reference
-    private EventAdmin eventAdmin;
+    public static final String STORE_TYPE_OFFSETS = "lastRaisedEventOffset";
+
+    private final Map<String, Long> lastDistributedOffsets = new HashMap<>();
 
-    @Reference
-    private PubQueueProvider pubQueueCacheService;
+    private final EventAdmin eventAdmin;
 
-    @Reference
-    private MessagingProvider messagingProvider;
+    private final PubQueueProvider pubQueueCacheService;
+
+    private  MessagingProvider messagingProvider;
 
-    @Reference
     private Topics topics;
 
+    private ResourceResolverFactory resolverFactory;
+
+    private Map<String, LocalStore> localStores;

Review comment:
       `localStores` should be a thread safe `ConcurrentMap` instead of `HashMap` because it'll be accessed from multiple threads: the task that invoke `storeLastDistributedOffset` and the thread that invoke `processOffsets`.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@sling.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org