Add DOM blueprint XML
[mdsal.git] / dom / mdsal-dom-broker / src / main / java / org / opendaylight / mdsal / dom / broker / DOMNotificationRouter.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8 package org.opendaylight.mdsal.dom.broker;
9
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;
29 import java.util.Set;
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;
47
48
49 /**
50  * Joint implementation of {@link DOMNotificationPublishService} and {@link DOMNotificationService}. Provides
51  * routing of notifications from publishers to subscribers.
52  *
53  *<p>
54  * Internal implementation works by allocating a two-handler Disruptor. The first handler delivers notifications
55  * to subscribed listeners and the second one notifies whoever may be listening on the returned future. Registration
56  * state tracking is performed by a simple immutable multimap -- when a registration or unregistration occurs we
57  * re-generate the entire map from scratch and set it atomically. While registrations/unregistrations synchronize
58  * on this instance, notifications do not take any locks here.
59  *
60  *<p>
61  * The fully-blocking {@link #publish(long, DOMNotification, Collection)}
62  * and non-blocking {@link #offerNotification(DOMNotification)}
63  * are realized using the Disruptor's native operations. The bounded-blocking {@link
64  * #offerNotification(DOMNotification, long, TimeUnit)}
65  * is realized by arming a background wakeup interrupt.
66  */
67 public class DOMNotificationRouter implements AutoCloseable, DOMNotificationPublishService,
68         DOMNotificationService, DOMNotificationSubscriptionListenerRegistry {
69
70     private static final Logger LOG = LoggerFactory.getLogger(DOMNotificationRouter.class);
71     private static final ListenableFuture<Void> NO_LISTENERS = Futures.immediateFuture(null);
72     private static final WaitStrategy DEFAULT_STRATEGY = PhasedBackoffWaitStrategy.withLock(
73             1L, 30L, TimeUnit.MILLISECONDS);
74     private static final EventHandler<DOMNotificationRouterEvent> DISPATCH_NOTIFICATIONS =
75         (event, sequence, endOfBatch) -> event.deliverNotification();
76     private static final EventHandler<DOMNotificationRouterEvent> NOTIFY_FUTURE =
77         (event, sequence, endOfBatch) -> event.setFuture();
78
79     private final Disruptor<DOMNotificationRouterEvent> disruptor;
80     private final ExecutorService executor;
81     private volatile Multimap<SchemaPath, ListenerRegistration<? extends
82             DOMNotificationListener>> listeners = ImmutableMultimap.of();
83     private final ListenerRegistry<DOMNotificationSubscriptionListener> subscriptionListeners =
84             ListenerRegistry.create();
85     private final ScheduledThreadPoolExecutor observer;
86
87     @SuppressWarnings("unchecked")
88     @VisibleForTesting
89     DOMNotificationRouter(final ExecutorService executor, final int queueDepth, final WaitStrategy strategy) {
90         this.executor = Preconditions.checkNotNull(executor);
91         this.observer = new ScheduledThreadPoolExecutor(1, new ThreadFactoryBuilder()
92                 .setDaemon(true).setNameFormat("DOMNotificationRouter-%d").build());
93         disruptor = new Disruptor<>(DOMNotificationRouterEvent.FACTORY,
94                 queueDepth, executor, ProducerType.MULTI, strategy);
95         disruptor.handleEventsWith(DISPATCH_NOTIFICATIONS);
96         disruptor.after(DISPATCH_NOTIFICATIONS).handleEventsWith(NOTIFY_FUTURE);
97         disruptor.start();
98     }
99
100     public static DOMNotificationRouter create(final int queueDepth) {
101         final ExecutorService executor = Executors.newCachedThreadPool();
102
103         return new DOMNotificationRouter(executor, queueDepth, DEFAULT_STRATEGY);
104     }
105
106     public static DOMNotificationRouter create(final int queueDepth, final long spinTime,
107             final long parkTime, final TimeUnit unit) {
108         Preconditions.checkArgument(Long.lowestOneBit(queueDepth) == Long.highestOneBit(queueDepth),
109                 "Queue depth %s is not power-of-two", queueDepth);
110         final ExecutorService executor = Executors.newCachedThreadPool();
111         final WaitStrategy strategy = PhasedBackoffWaitStrategy.withLock(spinTime, parkTime, unit);
112
113         return new DOMNotificationRouter(executor, queueDepth, strategy);
114     }
115
116     @Override
117     public synchronized <T extends DOMNotificationListener> ListenerRegistration<T> registerNotificationListener(
118             final T listener, final Collection<SchemaPath> types) {
119         final ListenerRegistration<T> reg = new AbstractListenerRegistration<T>(listener) {
120             @Override
121             protected void removeRegistration() {
122                 synchronized (DOMNotificationRouter.this) {
123                     replaceListeners(ImmutableMultimap.copyOf(Multimaps.filterValues(listeners,
124                         input -> input != this)));
125                 }
126             }
127         };
128
129         if (!types.isEmpty()) {
130             final Builder<SchemaPath, ListenerRegistration<? extends DOMNotificationListener>> b =
131                     ImmutableMultimap.builder();
132             b.putAll(listeners);
133
134             for (final SchemaPath t : types) {
135                 b.put(t, reg);
136             }
137
138             replaceListeners(b.build());
139         }
140
141         return reg;
142     }
143
144     @Override
145     public <T extends DOMNotificationListener> ListenerRegistration<T> registerNotificationListener(
146             final T listener, final SchemaPath... types) {
147         return registerNotificationListener(listener, Arrays.asList(types));
148     }
149
150     /**
151      * Swaps registered listeners and triggers notification update.
152      *
153      * @param newListeners is used to notify listenerTypes changed
154      */
155     private void replaceListeners(
156             final Multimap<SchemaPath, ListenerRegistration<? extends DOMNotificationListener>> newListeners) {
157         listeners = newListeners;
158         notifyListenerTypesChanged(newListeners.keySet());
159     }
160
161     @SuppressWarnings("checkstyle:IllegalCatch")
162     private void notifyListenerTypesChanged(final Set<SchemaPath> typesAfter) {
163         final List<ListenerRegistration<DOMNotificationSubscriptionListener>> listenersAfter =
164                 ImmutableList.copyOf(subscriptionListeners.getListeners());
165         executor.submit(() -> {
166             for (final ListenerRegistration<DOMNotificationSubscriptionListener> subListener : listenersAfter) {
167                 try {
168                     subListener.getInstance().onSubscriptionChanged(typesAfter);
169                 } catch (final Exception e) {
170                     LOG.warn("Uncaught exception during invoking listener {}", subListener.getInstance(), e);
171                 }
172             }
173         });
174     }
175
176     @Override
177     public <L extends DOMNotificationSubscriptionListener> ListenerRegistration<L> registerSubscriptionListener(
178             final L listener) {
179         final Set<SchemaPath> initialTypes = listeners.keySet();
180         executor.submit(() -> listener.onSubscriptionChanged(initialTypes));
181         return subscriptionListeners.registerWithType(listener);
182     }
183
184     private ListenableFuture<Void> publish(final long seq, final DOMNotification notification,
185             final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers) {
186         final DOMNotificationRouterEvent event = disruptor.get(seq);
187         final ListenableFuture<Void> future = event.initialize(notification, subscribers);
188         disruptor.getRingBuffer().publish(seq);
189         return future;
190     }
191
192     @Override
193     public ListenableFuture<? extends Object> putNotification(final DOMNotification notification)
194             throws InterruptedException {
195         final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers =
196                 listeners.get(notification.getType());
197         if (subscribers.isEmpty()) {
198             return NO_LISTENERS;
199         }
200
201         final long seq = disruptor.getRingBuffer().next();
202         return publish(seq, notification, subscribers);
203     }
204
205     @SuppressWarnings("checkstyle:IllegalCatch")
206     @VisibleForTesting
207     ListenableFuture<? extends Object> tryPublish(final DOMNotification notification,
208             final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers) {
209         final long seq;
210         try {
211             seq = disruptor.getRingBuffer().tryNext();
212         } catch (final InsufficientCapacityException e) {
213             return DOMNotificationPublishService.REJECTED;
214         }
215
216         return publish(seq, notification, subscribers);
217     }
218
219     @Override
220     public ListenableFuture<? extends Object> offerNotification(final DOMNotification notification) {
221         final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers =
222                 listeners.get(notification.getType());
223         if (subscribers.isEmpty()) {
224             return NO_LISTENERS;
225         }
226
227         return tryPublish(notification, subscribers);
228     }
229
230     @Override
231     public ListenableFuture<? extends Object> offerNotification(final DOMNotification notification, final long timeout,
232             final TimeUnit unit) throws InterruptedException {
233         final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers =
234                 listeners.get(notification.getType());
235         if (subscribers.isEmpty()) {
236             return NO_LISTENERS;
237         }
238         // Attempt to perform a non-blocking publish first
239         final ListenableFuture<?> noBlock = tryPublish(notification, subscribers);
240         if (!DOMNotificationPublishService.REJECTED.equals(noBlock)) {
241             return noBlock;
242         }
243
244         try {
245             final Thread publishThread = Thread.currentThread();
246             ScheduledFuture<?> timerTask = observer.schedule(publishThread::interrupt, timeout, unit);
247             final ListenableFuture<?> withBlock = putNotification(notification);
248             timerTask.cancel(true);
249             if (observer.getQueue().size() > 50) {
250                 observer.purge();
251             }
252             return withBlock;
253         } catch (InterruptedException e) {
254             return DOMNotificationPublishService.REJECTED;
255         }
256     }
257
258     @Override
259     public void close() {
260         observer.shutdown();
261         disruptor.shutdown();
262         executor.shutdown();
263     }
264
265     @VisibleForTesting
266     ExecutorService executor() {
267         return executor;
268     }
269
270     @VisibleForTesting
271     Multimap<SchemaPath, ?> listeners() {
272         return listeners;
273     }
274
275     @VisibleForTesting
276     ListenerRegistry<DOMNotificationSubscriptionListener> subscriptionListeners() {
277         return subscriptionListeners;
278     }
279
280 }