X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-dom-broker%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fmd%2Fsal%2Fdom%2Fbroker%2Fimpl%2FDOMNotificationRouter.java;h=95d9e2171bcc562337987029d6720dd64dde9ab2;hb=0b161730fd648bfa4c953e84b2d6a66972bc4da2;hp=2dc1d24369ccc5be75da82a2f6cb0b315402a55c;hpb=4d4d6663416b8aa05b17e6b3176c2d530bfa6cc4;p=controller.git 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 2dc1d24369..95d9e2171b 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 @@ -45,14 +45,18 @@ 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, @@ -60,22 +64,26 @@ public final class DOMNotificationRouter implements AutoCloseable, DOMNotificati 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 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 final ListenerRegistry subscriptionListeners = ListenerRegistry.create(); + private volatile Multimap> listeners + = ImmutableMultimap.of(); + private final ListenerRegistry subscriptionListeners = ListenerRegistry + .create(); @SuppressWarnings("unchecked") private DOMNotificationRouter(final ExecutorService executor, final int queueDepth, final WaitStrategy strategy) { this.executor = Preconditions.checkNotNull(executor); - disruptor = new Disruptor<>(DOMNotificationRouterEvent.FACTORY, queueDepth, executor, ProducerType.MULTI, strategy); + disruptor = new Disruptor<>(DOMNotificationRouterEvent.FACTORY, queueDepth, executor, ProducerType.MULTI, + strategy); disruptor.handleEventsWith(DISPATCH_NOTIFICATIONS); disruptor.after(DISPATCH_NOTIFICATIONS).handleEventsWith(NOTIFY_FUTURE); disruptor.start(); @@ -87,9 +95,10 @@ public final class DOMNotificationRouter implements AutoCloseable, DOMNotificati return new DOMNotificationRouter(executor, queueDepth, DEFAULT_STRATEGY); } - public static DOMNotificationRouter create(final int queueDepth, final long spinTime, final long parkTime, final TimeUnit unit) { + 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); + "Queue depth %s is not power-of-two", queueDepth); final ExecutorService executor = Executors.newCachedThreadPool(); final WaitStrategy strategy = PhasedBackoffWaitStrategy.withLock(spinTime, parkTime, unit); @@ -97,7 +106,8 @@ public final class DOMNotificationRouter implements AutoCloseable, DOMNotificati } @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() { @@ -110,7 +120,8 @@ public final class DOMNotificationRouter implements AutoCloseable, DOMNotificati }; if (!types.isEmpty()) { - final Builder> b = ImmutableMultimap.builder(); + final Builder> b = ImmutableMultimap + .builder(); b.putAll(listeners); for (final SchemaPath t : types) { @@ -124,14 +135,17 @@ public final class DOMNotificationRouter implements AutoCloseable, DOMNotificati } @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)); } /** - * Swaps registered listeners and triggers notification update + * Swaps registered listeners and triggers notification update. * - * @param newListeners + * @param newListeners listeners */ private void replaceListeners( final Multimap> newListeners) { @@ -139,9 +153,11 @@ public final class DOMNotificationRouter implements AutoCloseable, DOMNotificati notifyListenerTypesChanged(newListeners.keySet()); } + @SuppressWarnings("checkstyle:IllegalCatch") private void notifyListenerTypesChanged(final Set typesAfter) { - final List> listenersAfter =ImmutableList.copyOf(subscriptionListeners.getListeners()); - executor.submit(() -> { + final List> listenersAfter = ImmutableList + .copyOf(subscriptionListeners.getListeners()); + executor.execute(() -> { for (final ListenerRegistration subListener : listenersAfter) { try { subListener.getInstance().onSubscriptionChanged(typesAfter); @@ -156,11 +172,13 @@ public final class DOMNotificationRouter implements AutoCloseable, DOMNotificati public ListenerRegistration registerSubscriptionListener( final L listener) { final Set initialTypes = listeners.keySet(); - executor.submit(() -> listener.onSubscriptionChanged(initialTypes)); + executor.execute(() -> listener.onSubscriptionChanged(initialTypes)); return subscriptionListeners.registerWithType(listener); } - private ListenableFuture publish(final long seq, final DOMNotification notification, final Collection> subscribers) { + 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); @@ -169,7 +187,8 @@ public final class DOMNotificationRouter implements AutoCloseable, DOMNotificati @Override public ListenableFuture putNotification(final DOMNotification notification) throws InterruptedException { - final Collection> subscribers = listeners.get(notification.getType()); + final Collection> subscribers = listeners + .get(notification.getType()); if (subscribers.isEmpty()) { return NO_LISTENERS; } @@ -178,10 +197,12 @@ 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(); + seq = disruptor.getRingBuffer().tryNext(); } catch (final InsufficientCapacityException e) { return DOMNotificationPublishService.REJECTED; } @@ -191,7 +212,8 @@ public final class DOMNotificationRouter implements AutoCloseable, DOMNotificati @Override public ListenableFuture offerNotification(final DOMNotification notification) { - final Collection> subscribers = listeners.get(notification.getType()); + final Collection> subscribers = listeners + .get(notification.getType()); if (subscribers.isEmpty()) { return NO_LISTENERS; } @@ -201,8 +223,9 @@ 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()); + final TimeUnit unit) throws InterruptedException { + final Collection> subscribers = listeners + .get(notification.getType()); if (subscribers.isEmpty()) { return NO_LISTENERS; }