X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-dom-broker%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fmd%2Fsal%2Fdom%2Fbroker%2Fimpl%2FDOMNotificationRouter.java;h=bb2275f1fb508643225ffc00263f4747172444da;hp=aac425b3d400ca9ed530230a0359740f4f3db74c;hb=4ef15f7a7e3fb5bcaa6a3202d268a5c945e0aa71;hpb=83dfe301bf2a2b1eff6883a2af3282c95d5a752e diff --git a/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/md/sal/dom/broker/impl/DOMNotificationRouter.java b/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/md/sal/dom/broker/impl/DOMNotificationRouter.java index aac425b3d4..bb2275f1fb 100644 --- a/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/md/sal/dom/broker/impl/DOMNotificationRouter.java +++ b/opendaylight/md-sal/sal-dom-broker/src/main/java/org/opendaylight/controller/md/sal/dom/broker/impl/DOMNotificationRouter.java @@ -8,7 +8,7 @@ package org.opendaylight.controller.md.sal.dom.broker.impl; import com.google.common.base.Preconditions; -import com.google.common.base.Predicate; +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; @@ -17,12 +17,14 @@ import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.ListenableFuture; import com.lmax.disruptor.EventHandler; import com.lmax.disruptor.InsufficientCapacityException; -import com.lmax.disruptor.SleepingWaitStrategy; +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.Collection; +import java.util.List; +import java.util.Set; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.TimeUnit; @@ -30,99 +32,153 @@ import org.opendaylight.controller.md.sal.dom.api.DOMNotification; import org.opendaylight.controller.md.sal.dom.api.DOMNotificationListener; import org.opendaylight.controller.md.sal.dom.api.DOMNotificationPublishService; import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService; +import org.opendaylight.controller.md.sal.dom.spi.DOMNotificationSubscriptionListener; +import org.opendaylight.controller.md.sal.dom.spi.DOMNotificationSubscriptionListenerRegistry; import org.opendaylight.yangtools.concepts.AbstractListenerRegistration; import org.opendaylight.yangtools.concepts.ListenerRegistration; +import org.opendaylight.yangtools.util.ListenerRegistry; import org.opendaylight.yangtools.yang.model.api.SchemaPath; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; /** * Joint implementation of {@link DOMNotificationPublishService} and {@link DOMNotificationService}. Provides * routing of notifications from publishers to subscribers. * + *

* 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. * - * 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)} + *

+ * 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. */ -public final class DOMNotificationRouter implements AutoCloseable, DOMNotificationPublishService, DOMNotificationService { - private static final ListenableFuture NO_LISTENERS = Futures.immediateFuture(null); - private static final WaitStrategy DEFAULT_STRATEGY = new SleepingWaitStrategy(); - private static final EventHandler DISPATCH_NOTIFICATIONS = new EventHandler() { - @Override - public void onEvent(final DOMNotificationRouterEvent event, final long sequence, final boolean endOfBatch) throws Exception { - event.deliverNotification(); +public final class DOMNotificationRouter implements AutoCloseable, DOMNotificationPublishService, + DOMNotificationService, DOMNotificationSubscriptionListenerRegistry { - } - }; - private static final EventHandler NOTIFY_FUTURE = new EventHandler() { - @Override - public void onEvent(final DOMNotificationRouterEvent event, final long sequence, final boolean endOfBatch) { - event.setFuture(); - } - }; + private static final Logger LOG = LoggerFactory.getLogger(DOMNotificationRouter.class); + private static final ListenableFuture NO_LISTENERS = Futures.immediateFuture(null); + private static final WaitStrategy DEFAULT_STRATEGY = PhasedBackoffWaitStrategy + .withLock(1L, 30L, TimeUnit.MILLISECONDS); + private static final EventHandler DISPATCH_NOTIFICATIONS + = (event, sequence, endOfBatch) -> event.deliverNotification(); + private static final EventHandler NOTIFY_FUTURE = (event, sequence, endOfBatch) -> event + .setFuture(); private final Disruptor disruptor; private final ExecutorService executor; - private volatile Multimap> listeners = ImmutableMultimap.of(); + private volatile Multimap> listeners + = ImmutableMultimap.of(); + private final ListenerRegistry subscriptionListeners = ListenerRegistry + .create(); - private DOMNotificationRouter(final ExecutorService executor, final Disruptor disruptor) { + @SuppressWarnings("unchecked") + private DOMNotificationRouter(final ExecutorService executor, final int queueDepth, final WaitStrategy strategy) { this.executor = Preconditions.checkNotNull(executor); - this.disruptor = Preconditions.checkNotNull(disruptor); + + disruptor = new Disruptor<>(DOMNotificationRouterEvent.FACTORY, queueDepth, executor, ProducerType.MULTI, + strategy); + disruptor.handleEventsWith(DISPATCH_NOTIFICATIONS); + disruptor.after(DISPATCH_NOTIFICATIONS).handleEventsWith(NOTIFY_FUTURE); + disruptor.start(); } - @SuppressWarnings("unchecked") public static DOMNotificationRouter create(final int queueDepth) { final ExecutorService executor = Executors.newCachedThreadPool(); - final Disruptor disruptor = new Disruptor<>(DOMNotificationRouterEvent.FACTORY, queueDepth, executor, ProducerType.MULTI, DEFAULT_STRATEGY); - disruptor.after(DISPATCH_NOTIFICATIONS).handleEventsWith(NOTIFY_FUTURE); - disruptor.start(); + return new DOMNotificationRouter(executor, queueDepth, DEFAULT_STRATEGY); + } - return new DOMNotificationRouter(executor, disruptor); + public static DOMNotificationRouter create(final int queueDepth, final long spinTime, final long parkTime, + final TimeUnit unit) { + Preconditions.checkArgument(Long.lowestOneBit(queueDepth) == Long.highestOneBit(queueDepth), + "Queue depth %s is not power-of-two", queueDepth); + final ExecutorService executor = Executors.newCachedThreadPool(); + final WaitStrategy strategy = PhasedBackoffWaitStrategy.withLock(spinTime, parkTime, unit); + + return new DOMNotificationRouter(executor, queueDepth, strategy); } @Override - public synchronized ListenerRegistration registerNotificationListener(final T listener, final Collection types) { + public synchronized ListenerRegistration registerNotificationListener( + final T listener, final Collection types) { final ListenerRegistration reg = new AbstractListenerRegistration(listener) { @Override protected void removeRegistration() { final ListenerRegistration me = this; synchronized (DOMNotificationRouter.this) { - listeners = ImmutableMultimap.copyOf(Multimaps.filterValues(listeners, new Predicate>() { - @Override - public boolean apply(final ListenerRegistration input) { - return input != me; - } - })); + replaceListeners(ImmutableMultimap.copyOf(Multimaps.filterValues(listeners, input -> input != me))); } } }; if (!types.isEmpty()) { - final Builder> b = ImmutableMultimap.builder(); + final Builder> b = ImmutableMultimap + .builder(); b.putAll(listeners); - for (SchemaPath t : types) { + for (final SchemaPath t : types) { b.put(t, reg); } - listeners = b.build(); + replaceListeners(b.build()); } return reg; } @Override - public ListenerRegistration registerNotificationListener(final T listener, final SchemaPath... types) { + public ListenerRegistration registerNotificationListener(final T listener, + final + SchemaPath... + types) { return registerNotificationListener(listener, Arrays.asList(types)); } - private ListenableFuture publish(final long seq, final DOMNotification notification, final Collection> subscribers) { + /** + * Swaps registered listeners and triggers notification update. + * + * @param newListeners listeners + */ + private void replaceListeners( + final Multimap> newListeners) { + listeners = newListeners; + notifyListenerTypesChanged(newListeners.keySet()); + } + + @SuppressWarnings("checkstyle:IllegalCatch") + private void notifyListenerTypesChanged(final Set typesAfter) { + final List> listenersAfter = ImmutableList + .copyOf(subscriptionListeners.getListeners()); + executor.submit(() -> { + for (final ListenerRegistration subListener : listenersAfter) { + try { + subListener.getInstance().onSubscriptionChanged(typesAfter); + } catch (final Exception e) { + LOG.warn("Uncaught exception during invoking listener {}", subListener.getInstance(), e); + } + } + }); + } + + @Override + public ListenerRegistration registerSubscriptionListener( + final L listener) { + final Set initialTypes = listeners.keySet(); + executor.submit(() -> listener.onSubscriptionChanged(initialTypes)); + return subscriptionListeners.registerWithType(listener); + } + + private ListenableFuture publish(final long seq, final DOMNotification notification, + final Collection> + subscribers) { final DOMNotificationRouterEvent event = disruptor.get(seq); final ListenableFuture future = event.initialize(notification, subscribers); disruptor.getRingBuffer().publish(seq); @@ -130,8 +186,9 @@ public final class DOMNotificationRouter implements AutoCloseable, DOMNotificati } @Override - public ListenableFuture putNotification(final DOMNotification notification) throws InterruptedException { - final Collection> subscribers = listeners.get(notification.getType()); + public ListenableFuture putNotification(final DOMNotification notification) throws InterruptedException { + final Collection> subscribers = listeners + .get(notification.getType()); if (subscribers.isEmpty()) { return NO_LISTENERS; } @@ -140,11 +197,13 @@ public final class DOMNotificationRouter implements AutoCloseable, DOMNotificati return publish(seq, notification, subscribers); } - private ListenableFuture tryPublish(final DOMNotification notification, final Collection> subscribers) { + private ListenableFuture tryPublish(final DOMNotification notification, + final Collection> + subscribers) { final long seq; try { - seq = disruptor.getRingBuffer().tryNext(); - } catch (InsufficientCapacityException e) { + seq = disruptor.getRingBuffer().tryNext(); + } catch (final InsufficientCapacityException e) { return DOMNotificationPublishService.REJECTED; } @@ -152,8 +211,9 @@ public final class DOMNotificationRouter implements AutoCloseable, DOMNotificati } @Override - public ListenableFuture offerNotification(final DOMNotification notification) { - final Collection> subscribers = listeners.get(notification.getType()); + public ListenableFuture offerNotification(final DOMNotification notification) { + final Collection> subscribers = listeners + .get(notification.getType()); if (subscribers.isEmpty()) { return NO_LISTENERS; } @@ -162,15 +222,16 @@ public final class DOMNotificationRouter implements AutoCloseable, DOMNotificati } @Override - public ListenableFuture offerNotification(final DOMNotification notification, final long timeout, - final TimeUnit unit) throws InterruptedException { - final Collection> subscribers = listeners.get(notification.getType()); + public ListenableFuture offerNotification(final DOMNotification notification, final long timeout, + final TimeUnit unit) throws InterruptedException { + final Collection> 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 = tryPublish(notification, subscribers); if (!DOMNotificationPublishService.REJECTED.equals(noBlock)) { return noBlock; }