Add registerNotificationListeners()
[mdsal.git] / dom / mdsal-dom-broker / src / main / java / org / opendaylight / mdsal / dom / broker / DOMNotificationRouter.java
index c609f3389cd152542fd3b9614225069926547295..5e739888307dd06d025c7b09fba27daa1fd47821 100644 (file)
@@ -7,31 +7,29 @@
  */
 package org.opendaylight.mdsal.dom.broker;
 
-import static com.google.common.base.Preconditions.checkArgument;
-
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMultimap;
-import com.google.common.collect.ImmutableMultimap.Builder;
 import com.google.common.collect.Multimap;
 import com.google.common.collect.Multimaps;
+import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
+import com.google.common.util.concurrent.MoreExecutors;
 import com.google.common.util.concurrent.ThreadFactoryBuilder;
-import com.lmax.disruptor.EventHandler;
-import com.lmax.disruptor.InsufficientCapacityException;
-import com.lmax.disruptor.PhasedBackoffWaitStrategy;
-import com.lmax.disruptor.WaitStrategy;
-import com.lmax.disruptor.dsl.Disruptor;
-import com.lmax.disruptor.dsl.ProducerType;
-import java.util.Arrays;
+import java.util.ArrayList;
 import java.util.Collection;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.ScheduledFuture;
 import java.util.concurrent.ScheduledThreadPoolExecutor;
 import java.util.concurrent.TimeUnit;
+import javax.annotation.PreDestroy;
+import javax.inject.Inject;
+import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.mdsal.dom.api.DOMNotification;
 import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
 import org.opendaylight.mdsal.dom.api.DOMNotificationPublishService;
@@ -39,10 +37,20 @@ import org.opendaylight.mdsal.dom.api.DOMNotificationService;
 import org.opendaylight.mdsal.dom.spi.DOMNotificationSubscriptionListener;
 import org.opendaylight.mdsal.dom.spi.DOMNotificationSubscriptionListenerRegistry;
 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
+import org.opendaylight.yangtools.concepts.AbstractRegistration;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
+import org.opendaylight.yangtools.concepts.Registration;
 import org.opendaylight.yangtools.util.ListenerRegistry;
+import org.opendaylight.yangtools.util.concurrent.EqualityQueuedNotificationManager;
 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.opendaylight.yangtools.util.concurrent.QueuedNotificationManager;
+import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.Deactivate;
+import org.osgi.service.metatype.annotations.AttributeDefinition;
+import org.osgi.service.metatype.annotations.Designate;
+import org.osgi.service.metatype.annotations.ObjectClassDefinition;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -51,84 +59,99 @@ import org.slf4j.LoggerFactory;
  * routing of notifications from publishers to subscribers.
  *
  *<p>
- * Internal implementation works by allocating a two-handler Disruptor. The first handler delivers notifications
- * to subscribed listeners and the second one notifies whoever may be listening on the returned future. Registration
- * state tracking is performed by a simple immutable multimap -- when a registration or unregistration occurs we
- * re-generate the entire map from scratch and set it atomically. While registrations/unregistrations synchronize
- * on this instance, notifications do not take any locks here.
- *
- *<p>
- * The fully-blocking {@link #publish(long, DOMNotification, Collection)}
- * and non-blocking {@link #offerNotification(DOMNotification)}
- * are realized using the Disruptor's native operations. The bounded-blocking {@link
- * #offerNotification(DOMNotification, long, TimeUnit)}
- * is realized by arming a background wakeup interrupt.
+ * Internal implementation one by using a {@link QueuedNotificationManager}.
+ *</p>
  */
+@Component(immediate = true, configurationPid = "org.opendaylight.mdsal.dom.notification", service = {
+    DOMNotificationService.class, DOMNotificationPublishService.class,
+    DOMNotificationSubscriptionListenerRegistry.class
+})
+@Designate(ocd = DOMNotificationRouter.Config.class)
+// Non-final for testing
 public class DOMNotificationRouter implements AutoCloseable, DOMNotificationPublishService,
         DOMNotificationService, DOMNotificationSubscriptionListenerRegistry {
+    @ObjectClassDefinition()
+    public @interface Config {
+        @AttributeDefinition(name = "notification-queue-depth")
+        int queueDepth() default 65536;
+    }
+
+    @VisibleForTesting
+    abstract static sealed class Reg<T extends DOMNotificationListener> extends AbstractListenerRegistration<T> {
+        Reg(final @NonNull T listener) {
+            super(listener);
+        }
+    }
+
+    private final class SingleReg<T extends DOMNotificationListener> extends Reg<T> {
+        SingleReg(final @NonNull T listener) {
+            super(listener);
+        }
+
+        @Override
+        protected void removeRegistration() {
+            DOMNotificationRouter.this.removeRegistration(this);
+        }
+    }
+
+    private static final class ComponentReg extends Reg<DOMNotificationListener> {
+        ComponentReg(final @NonNull DOMNotificationListener listener) {
+            super(listener);
+        }
+
+        @Override
+        protected void removeRegistration() {
+            // No-op
+        }
+    }
 
     private static final Logger LOG = LoggerFactory.getLogger(DOMNotificationRouter.class);
     private static final ListenableFuture<Void> NO_LISTENERS = FluentFutures.immediateNullFluentFuture();
-    private static final WaitStrategy DEFAULT_STRATEGY = PhasedBackoffWaitStrategy.withLock(
-            1L, 30L, TimeUnit.MILLISECONDS);
-    private static final EventHandler<DOMNotificationRouterEvent> DISPATCH_NOTIFICATIONS =
-        (event, sequence, endOfBatch) -> event.deliverNotification();
-    private static final EventHandler<DOMNotificationRouterEvent> NOTIFY_FUTURE =
-        (event, sequence, endOfBatch) -> event.setFuture();
 
     private final ListenerRegistry<DOMNotificationSubscriptionListener> subscriptionListeners =
             ListenerRegistry.create();
-    private final Disruptor<DOMNotificationRouterEvent> disruptor;
+    private final EqualityQueuedNotificationManager<AbstractListenerRegistration<? extends DOMNotificationListener>,
+                DOMNotificationRouterEvent> queueNotificationManager;
     private final ScheduledThreadPoolExecutor observer;
     private final ExecutorService executor;
 
-    private volatile Multimap<SchemaPath, ListenerRegistration<? extends DOMNotificationListener>> listeners =
-            ImmutableMultimap.of();
-
-    @VisibleForTesting
-    DOMNotificationRouter(final int queueDepth, final WaitStrategy strategy) {
-        observer = new ScheduledThreadPoolExecutor(1,
-            new ThreadFactoryBuilder().setDaemon(true).setNameFormat("DOMNotificationRouter-observer-%d").build());
-        executor = Executors.newCachedThreadPool(
-            new ThreadFactoryBuilder().setDaemon(true).setNameFormat("DOMNotificationRouter-listeners-%d").build());
-        disruptor = new Disruptor<>(DOMNotificationRouterEvent.FACTORY, queueDepth,
-                new ThreadFactoryBuilder().setNameFormat("DOMNotificationRouter-disruptor-%d").build(),
-                ProducerType.MULTI, strategy);
-        disruptor.handleEventsWith(DISPATCH_NOTIFICATIONS);
-        disruptor.after(DISPATCH_NOTIFICATIONS).handleEventsWith(NOTIFY_FUTURE);
-        disruptor.start();
+    private volatile Multimap<Absolute, Reg<?>> listeners = ImmutableMultimap.of();
+
+    @Inject
+    public DOMNotificationRouter(final int maxQueueCapacity) {
+        observer = new ScheduledThreadPoolExecutor(1, new ThreadFactoryBuilder()
+            .setDaemon(true)
+            .setNameFormat("DOMNotificationRouter-observer-%d")
+            .build());
+        executor = Executors.newCachedThreadPool(new ThreadFactoryBuilder()
+            .setDaemon(true)
+            .setNameFormat("DOMNotificationRouter-listeners-%d")
+            .build());
+        queueNotificationManager = new EqualityQueuedNotificationManager<>("DOMNotificationRouter", executor,
+                maxQueueCapacity, DOMNotificationRouter::deliverEvents);
     }
 
-    public static DOMNotificationRouter create(final int queueDepth) {
-        return new DOMNotificationRouter(queueDepth, DEFAULT_STRATEGY);
+    @Activate
+    public DOMNotificationRouter(final Config config) {
+        this(config.queueDepth());
+        LOG.info("DOM Notification Router started");
     }
 
-    public static DOMNotificationRouter create(final int queueDepth, final long spinTime, final long parkTime,
-            final TimeUnit unit) {
-        checkArgument(Long.lowestOneBit(queueDepth) == Long.highestOneBit(queueDepth),
-                "Queue depth %s is not power-of-two", queueDepth);
-        return new DOMNotificationRouter(queueDepth, PhasedBackoffWaitStrategy.withLock(spinTime, parkTime, unit));
+    @Deprecated(forRemoval = true)
+    public static DOMNotificationRouter create(final int maxQueueCapacity) {
+        return new DOMNotificationRouter(maxQueueCapacity);
     }
 
     @Override
     public synchronized <T extends DOMNotificationListener> ListenerRegistration<T> registerNotificationListener(
-            final T listener, final Collection<SchemaPath> types) {
-        final ListenerRegistration<T> reg = new AbstractListenerRegistration<T>(listener) {
-            @Override
-            protected void removeRegistration() {
-                synchronized (DOMNotificationRouter.this) {
-                    replaceListeners(ImmutableMultimap.copyOf(Multimaps.filterValues(listeners,
-                        input -> input != this)));
-                }
-            }
-        };
+            final T listener, final Collection<Absolute> types) {
+        final var reg = new SingleReg<>(listener);
 
         if (!types.isEmpty()) {
-            final Builder<SchemaPath, ListenerRegistration<? extends DOMNotificationListener>> b =
-                    ImmutableMultimap.builder();
+            final var b = ImmutableMultimap.<Absolute, Reg<?>>builder();
             b.putAll(listeners);
 
-            for (final SchemaPath t : types) {
+            for (var t : types) {
                 b.put(t, reg);
             }
 
@@ -139,9 +162,32 @@ public class DOMNotificationRouter implements AutoCloseable, DOMNotificationPubl
     }
 
     @Override
-    public <T extends DOMNotificationListener> ListenerRegistration<T> registerNotificationListener(
-            final T listener, final SchemaPath... types) {
-        return registerNotificationListener(listener, Arrays.asList(types));
+    public synchronized Registration registerNotificationListeners(
+            final Map<Absolute, DOMNotificationListener> typeToListener) {
+        final var b = ImmutableMultimap.<Absolute, Reg<?>>builder();
+        b.putAll(listeners);
+
+        final var tmp = new HashMap<DOMNotificationListener, ComponentReg>();
+        for (var e : typeToListener.entrySet()) {
+            b.put(e.getKey(), tmp.computeIfAbsent(e.getValue(), ComponentReg::new));
+        }
+
+        final var regs = List.copyOf(tmp.values());
+        return new AbstractRegistration() {
+            @Override
+            protected void removeRegistration() {
+                regs.forEach(ComponentReg::close);
+                removeRegistrations(regs);
+            }
+        };
+    }
+
+    private synchronized void removeRegistration(final SingleReg<?> reg) {
+        replaceListeners(ImmutableMultimap.copyOf(Multimaps.filterValues(listeners, input -> input != reg)));
+    }
+
+    private synchronized void removeRegistrations(final List<ComponentReg> regs) {
+        replaceListeners(ImmutableMultimap.copyOf(Multimaps.filterValues(listeners, input -> !regs.contains(input))));
     }
 
     /**
@@ -149,17 +195,15 @@ public class DOMNotificationRouter implements AutoCloseable, DOMNotificationPubl
      *
      * @param newListeners is used to notify listenerTypes changed
      */
-    private void replaceListeners(
-            final Multimap<SchemaPath, ListenerRegistration<? extends DOMNotificationListener>> newListeners) {
+    private void replaceListeners(final Multimap<Absolute, Reg<?>> newListeners) {
         listeners = newListeners;
         notifyListenerTypesChanged(newListeners.keySet());
     }
 
     @SuppressWarnings("checkstyle:IllegalCatch")
-    private void notifyListenerTypesChanged(final Set<SchemaPath> typesAfter) {
+    private void notifyListenerTypesChanged(final Set<Absolute> typesAfter) {
         final List<? extends DOMNotificationSubscriptionListener> listenersAfter =
-                subscriptionListeners.getRegistrations().stream().map(ListenerRegistration::getInstance)
-                .collect(ImmutableList.toImmutableList());
+                subscriptionListeners.streamListeners().collect(ImmutableList.toImmutableList());
         executor.execute(() -> {
             for (final DOMNotificationSubscriptionListener subListener : listenersAfter) {
                 try {
@@ -174,67 +218,54 @@ public class DOMNotificationRouter implements AutoCloseable, DOMNotificationPubl
     @Override
     public <L extends DOMNotificationSubscriptionListener> ListenerRegistration<L> registerSubscriptionListener(
             final L listener) {
-        final Set<SchemaPath> initialTypes = listeners.keySet();
+        final Set<Absolute> initialTypes = listeners.keySet();
         executor.execute(() -> listener.onSubscriptionChanged(initialTypes));
         return subscriptionListeners.register(listener);
     }
 
-    private ListenableFuture<Void> publish(final long seq, final DOMNotification notification,
-            final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers) {
-        final DOMNotificationRouterEvent event = disruptor.get(seq);
-        final ListenableFuture<Void> future = event.initialize(notification, subscribers);
-        disruptor.getRingBuffer().publish(seq);
-        return future;
+    @VisibleForTesting
+    ListenableFuture<? extends Object> publish(final DOMNotification notification,
+            final Collection<Reg<?>> subscribers) {
+        final List<ListenableFuture<Void>> futures = new ArrayList<>(subscribers.size());
+        subscribers.forEach(subscriber -> {
+            final DOMNotificationRouterEvent event = new DOMNotificationRouterEvent(notification);
+            futures.add(event.future());
+            queueNotificationManager.submitNotification(subscriber, event);
+        });
+        return Futures.transform(Futures.successfulAsList(futures), ignored -> (Void)null,
+            MoreExecutors.directExecutor());
     }
 
     @Override
     public ListenableFuture<? extends Object> putNotification(final DOMNotification notification)
             throws InterruptedException {
-        final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers =
-                listeners.get(notification.getType());
+        final var subscribers = listeners.get(notification.getType());
         if (subscribers.isEmpty()) {
             return NO_LISTENERS;
         }
 
-        final long seq = disruptor.getRingBuffer().next();
-        return publish(seq, notification, subscribers);
-    }
-
-    @SuppressWarnings("checkstyle:IllegalCatch")
-    @VisibleForTesting
-    ListenableFuture<? extends Object> tryPublish(final DOMNotification notification,
-            final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers) {
-        final long seq;
-        try {
-            seq = disruptor.getRingBuffer().tryNext();
-        } catch (final InsufficientCapacityException e) {
-            return DOMNotificationPublishService.REJECTED;
-        }
-
-        return publish(seq, notification, subscribers);
+        return publish(notification, subscribers);
     }
 
     @Override
     public ListenableFuture<? extends Object> offerNotification(final DOMNotification notification) {
-        final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers =
-                listeners.get(notification.getType());
+        final var subscribers = listeners.get(notification.getType());
         if (subscribers.isEmpty()) {
             return NO_LISTENERS;
         }
 
-        return tryPublish(notification, subscribers);
+        return publish(notification, subscribers);
     }
 
     @Override
     public ListenableFuture<? extends Object> offerNotification(final DOMNotification notification, final long timeout,
             final TimeUnit unit) throws InterruptedException {
-        final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers =
-                listeners.get(notification.getType());
+        final var subscribers = listeners.get(notification.getType());
         if (subscribers.isEmpty()) {
             return NO_LISTENERS;
         }
         // Attempt to perform a non-blocking publish first
-        final ListenableFuture<?> noBlock = tryPublish(notification, subscribers);
+        final ListenableFuture<?> noBlock = publish(notification, subscribers);
         if (!DOMNotificationPublishService.REJECTED.equals(noBlock)) {
             return noBlock;
         }
@@ -253,11 +284,13 @@ public class DOMNotificationRouter implements AutoCloseable, DOMNotificationPubl
         }
     }
 
+    @PreDestroy
+    @Deactivate
     @Override
     public void close() {
-        disruptor.shutdown();
         observer.shutdown();
         executor.shutdown();
+        LOG.info("DOM Notification Router stopped");
     }
 
     @VisibleForTesting
@@ -271,7 +304,7 @@ public class DOMNotificationRouter implements AutoCloseable, DOMNotificationPubl
     }
 
     @VisibleForTesting
-    Multimap<SchemaPath, ?> listeners() {
+    Multimap<Absolute, ?> listeners() {
         return listeners;
     }
 
@@ -279,4 +312,16 @@ public class DOMNotificationRouter implements AutoCloseable, DOMNotificationPubl
     ListenerRegistry<DOMNotificationSubscriptionListener> subscriptionListeners() {
         return subscriptionListeners;
     }
+
+    private static void deliverEvents(final AbstractListenerRegistration<? extends DOMNotificationListener> reg,
+            final ImmutableList<DOMNotificationRouterEvent> events) {
+        if (reg.notClosed()) {
+            final DOMNotificationListener listener = reg.getInstance();
+            for (DOMNotificationRouterEvent event : events) {
+                event.deliverTo(listener);
+            }
+        } else {
+            events.forEach(DOMNotificationRouterEvent::clear);
+        }
+    }
 }