2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.mdsal.dom.broker;
10 import com.google.common.annotations.VisibleForTesting;
11 import com.google.common.base.Preconditions;
12 import com.google.common.collect.ImmutableList;
13 import com.google.common.collect.ImmutableMultimap;
14 import com.google.common.collect.ImmutableMultimap.Builder;
15 import com.google.common.collect.Multimap;
16 import com.google.common.collect.Multimaps;
17 import com.google.common.util.concurrent.Futures;
18 import com.google.common.util.concurrent.ListenableFuture;
19 import com.google.common.util.concurrent.ThreadFactoryBuilder;
20 import com.lmax.disruptor.EventHandler;
21 import com.lmax.disruptor.InsufficientCapacityException;
22 import com.lmax.disruptor.PhasedBackoffWaitStrategy;
23 import com.lmax.disruptor.WaitStrategy;
24 import com.lmax.disruptor.dsl.Disruptor;
25 import com.lmax.disruptor.dsl.ProducerType;
26 import java.util.Arrays;
27 import java.util.Collection;
28 import java.util.List;
30 import java.util.concurrent.ExecutorService;
31 import java.util.concurrent.Executors;
32 import java.util.concurrent.ScheduledFuture;
33 import java.util.concurrent.ScheduledThreadPoolExecutor;
34 import java.util.concurrent.TimeUnit;
35 import org.opendaylight.mdsal.dom.api.DOMNotification;
36 import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
37 import org.opendaylight.mdsal.dom.api.DOMNotificationPublishService;
38 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
39 import org.opendaylight.mdsal.dom.spi.DOMNotificationSubscriptionListener;
40 import org.opendaylight.mdsal.dom.spi.DOMNotificationSubscriptionListenerRegistry;
41 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
42 import org.opendaylight.yangtools.concepts.ListenerRegistration;
43 import org.opendaylight.yangtools.util.ListenerRegistry;
44 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
49 * Joint implementation of {@link DOMNotificationPublishService} and {@link DOMNotificationService}. Provides
50 * routing of notifications from publishers to subscribers.
53 * Internal implementation works by allocating a two-handler Disruptor. The first handler delivers notifications
54 * to subscribed listeners and the second one notifies whoever may be listening on the returned future. Registration
55 * state tracking is performed by a simple immutable multimap -- when a registration or unregistration occurs we
56 * re-generate the entire map from scratch and set it atomically. While registrations/unregistrations synchronize
57 * on this instance, notifications do not take any locks here.
60 * The fully-blocking {@link #publish(long, DOMNotification, Collection)}
61 * and non-blocking {@link #offerNotification(DOMNotification)}
62 * are realized using the Disruptor's native operations. The bounded-blocking {@link
63 * #offerNotification(DOMNotification, long, TimeUnit)}
64 * is realized by arming a background wakeup interrupt.
66 public class DOMNotificationRouter implements AutoCloseable, DOMNotificationPublishService,
67 DOMNotificationService, DOMNotificationSubscriptionListenerRegistry {
69 private static final Logger LOG = LoggerFactory.getLogger(DOMNotificationRouter.class);
70 private static final ListenableFuture<Void> NO_LISTENERS = Futures.immediateFuture(null);
71 private static final WaitStrategy DEFAULT_STRATEGY = PhasedBackoffWaitStrategy.withLock(
72 1L, 30L, TimeUnit.MILLISECONDS);
73 private static final EventHandler<DOMNotificationRouterEvent> DISPATCH_NOTIFICATIONS =
74 (event, sequence, endOfBatch) -> event.deliverNotification();
75 private static final EventHandler<DOMNotificationRouterEvent> NOTIFY_FUTURE =
76 (event, sequence, endOfBatch) -> event.setFuture();
78 private final Disruptor<DOMNotificationRouterEvent> disruptor;
79 private final ExecutorService executor;
80 private volatile Multimap<SchemaPath, ListenerRegistration<? extends
81 DOMNotificationListener>> listeners = ImmutableMultimap.of();
82 private final ListenerRegistry<DOMNotificationSubscriptionListener> subscriptionListeners =
83 ListenerRegistry.create();
84 private final ScheduledThreadPoolExecutor observer;
87 DOMNotificationRouter(final ExecutorService executor, final int queueDepth, final WaitStrategy strategy) {
88 this.executor = Preconditions.checkNotNull(executor);
89 this.observer = new ScheduledThreadPoolExecutor(1, new ThreadFactoryBuilder()
90 .setDaemon(true).setNameFormat("DOMNotificationRouter-%d").build());
91 disruptor = new Disruptor<>(DOMNotificationRouterEvent.FACTORY,
92 queueDepth, executor, ProducerType.MULTI, strategy);
93 disruptor.handleEventsWith(DISPATCH_NOTIFICATIONS);
94 disruptor.after(DISPATCH_NOTIFICATIONS).handleEventsWith(NOTIFY_FUTURE);
98 public static DOMNotificationRouter create(final int queueDepth) {
99 final ExecutorService executor = Executors.newCachedThreadPool();
101 return new DOMNotificationRouter(executor, queueDepth, DEFAULT_STRATEGY);
104 public static DOMNotificationRouter create(final int queueDepth, final long spinTime,
105 final long parkTime, final TimeUnit unit) {
106 Preconditions.checkArgument(Long.lowestOneBit(queueDepth) == Long.highestOneBit(queueDepth),
107 "Queue depth %s is not power-of-two", queueDepth);
108 final ExecutorService executor = Executors.newCachedThreadPool();
109 final WaitStrategy strategy = PhasedBackoffWaitStrategy.withLock(spinTime, parkTime, unit);
111 return new DOMNotificationRouter(executor, queueDepth, strategy);
115 public synchronized <T extends DOMNotificationListener> ListenerRegistration<T> registerNotificationListener(
116 final T listener, final Collection<SchemaPath> types) {
117 final ListenerRegistration<T> reg = new AbstractListenerRegistration<T>(listener) {
119 protected void removeRegistration() {
120 synchronized (DOMNotificationRouter.this) {
121 replaceListeners(ImmutableMultimap.copyOf(Multimaps.filterValues(listeners,
122 input -> input != this)));
127 if (!types.isEmpty()) {
128 final Builder<SchemaPath, ListenerRegistration<? extends DOMNotificationListener>> b =
129 ImmutableMultimap.builder();
132 for (final SchemaPath t : types) {
136 replaceListeners(b.build());
143 public <T extends DOMNotificationListener> ListenerRegistration<T> registerNotificationListener(
144 final T listener, final SchemaPath... types) {
145 return registerNotificationListener(listener, Arrays.asList(types));
149 * Swaps registered listeners and triggers notification update.
151 * @param newListeners is used to notify listenerTypes changed
153 private void replaceListeners(
154 final Multimap<SchemaPath, ListenerRegistration<? extends DOMNotificationListener>> newListeners) {
155 listeners = newListeners;
156 notifyListenerTypesChanged(newListeners.keySet());
159 @SuppressWarnings("checkstyle:IllegalCatch")
160 private void notifyListenerTypesChanged(final Set<SchemaPath> typesAfter) {
161 final List<ListenerRegistration<DOMNotificationSubscriptionListener>> listenersAfter =
162 ImmutableList.copyOf(subscriptionListeners.getListeners());
163 executor.execute(() -> {
164 for (final ListenerRegistration<DOMNotificationSubscriptionListener> subListener : listenersAfter) {
166 subListener.getInstance().onSubscriptionChanged(typesAfter);
167 } catch (final Exception e) {
168 LOG.warn("Uncaught exception during invoking listener {}", subListener.getInstance(), e);
175 public <L extends DOMNotificationSubscriptionListener> ListenerRegistration<L> registerSubscriptionListener(
177 final Set<SchemaPath> initialTypes = listeners.keySet();
178 executor.execute(() -> listener.onSubscriptionChanged(initialTypes));
179 return subscriptionListeners.registerWithType(listener);
182 private ListenableFuture<Void> publish(final long seq, final DOMNotification notification,
183 final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers) {
184 final DOMNotificationRouterEvent event = disruptor.get(seq);
185 final ListenableFuture<Void> future = event.initialize(notification, subscribers);
186 disruptor.getRingBuffer().publish(seq);
191 public ListenableFuture<? extends Object> putNotification(final DOMNotification notification)
192 throws InterruptedException {
193 final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers =
194 listeners.get(notification.getType());
195 if (subscribers.isEmpty()) {
199 final long seq = disruptor.getRingBuffer().next();
200 return publish(seq, notification, subscribers);
203 @SuppressWarnings("checkstyle:IllegalCatch")
205 ListenableFuture<? extends Object> tryPublish(final DOMNotification notification,
206 final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers) {
209 seq = disruptor.getRingBuffer().tryNext();
210 } catch (final InsufficientCapacityException e) {
211 return DOMNotificationPublishService.REJECTED;
214 return publish(seq, notification, subscribers);
218 public ListenableFuture<? extends Object> offerNotification(final DOMNotification notification) {
219 final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers =
220 listeners.get(notification.getType());
221 if (subscribers.isEmpty()) {
225 return tryPublish(notification, subscribers);
229 public ListenableFuture<? extends Object> offerNotification(final DOMNotification notification, final long timeout,
230 final TimeUnit unit) throws InterruptedException {
231 final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers =
232 listeners.get(notification.getType());
233 if (subscribers.isEmpty()) {
236 // Attempt to perform a non-blocking publish first
237 final ListenableFuture<?> noBlock = tryPublish(notification, subscribers);
238 if (!DOMNotificationPublishService.REJECTED.equals(noBlock)) {
243 final Thread publishThread = Thread.currentThread();
244 ScheduledFuture<?> timerTask = observer.schedule(publishThread::interrupt, timeout, unit);
245 final ListenableFuture<?> withBlock = putNotification(notification);
246 timerTask.cancel(true);
247 if (observer.getQueue().size() > 50) {
251 } catch (InterruptedException e) {
252 return DOMNotificationPublishService.REJECTED;
257 public void close() {
259 disruptor.shutdown();
264 ExecutorService executor() {
269 Multimap<SchemaPath, ?> listeners() {
274 ListenerRegistry<DOMNotificationSubscriptionListener> subscriptionListeners() {
275 return subscriptionListeners;