Bump to odlparent 3.1.0 and yangtools 2.0.3
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / main / java / org / opendaylight / controller / md / sal / dom / broker / impl / 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.controller.md.sal.dom.broker.impl;
9
10 import com.google.common.base.Preconditions;
11 import com.google.common.collect.ImmutableList;
12 import com.google.common.collect.ImmutableMultimap;
13 import com.google.common.collect.ImmutableMultimap.Builder;
14 import com.google.common.collect.Multimap;
15 import com.google.common.collect.Multimaps;
16 import com.google.common.util.concurrent.Futures;
17 import com.google.common.util.concurrent.ListenableFuture;
18 import com.lmax.disruptor.EventHandler;
19 import com.lmax.disruptor.InsufficientCapacityException;
20 import com.lmax.disruptor.PhasedBackoffWaitStrategy;
21 import com.lmax.disruptor.WaitStrategy;
22 import com.lmax.disruptor.dsl.Disruptor;
23 import com.lmax.disruptor.dsl.ProducerType;
24 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
25 import java.util.Arrays;
26 import java.util.Collection;
27 import java.util.List;
28 import java.util.Set;
29 import java.util.concurrent.ExecutorService;
30 import java.util.concurrent.Executors;
31 import java.util.concurrent.TimeUnit;
32 import org.opendaylight.controller.md.sal.dom.api.DOMNotification;
33 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationListener;
34 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationPublishService;
35 import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
36 import org.opendaylight.controller.md.sal.dom.spi.DOMNotificationSubscriptionListener;
37 import org.opendaylight.controller.md.sal.dom.spi.DOMNotificationSubscriptionListenerRegistry;
38 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
39 import org.opendaylight.yangtools.concepts.ListenerRegistration;
40 import org.opendaylight.yangtools.util.ListenerRegistry;
41 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 /**
46  * Joint implementation of {@link DOMNotificationPublishService} and {@link DOMNotificationService}. Provides
47  * routing of notifications from publishers to subscribers.
48  *
49  * <p>
50  * Internal implementation works by allocating a two-handler Disruptor. The first handler delivers notifications
51  * to subscribed listeners and the second one notifies whoever may be listening on the returned future. Registration
52  * state tracking is performed by a simple immutable multimap -- when a registration or unregistration occurs we
53  * re-generate the entire map from scratch and set it atomically. While registrations/unregistrations synchronize
54  * on this instance, notifications do not take any locks here.
55  *
56  * <p>
57  * The fully-blocking {@link #publish(long, DOMNotification, Collection)} and non-blocking
58  * {@link #offerNotification(DOMNotification)}
59  * are realized using the Disruptor's native operations. The bounded-blocking
60  * {@link #offerNotification(DOMNotification, long, TimeUnit)}
61  * is realized by arming a background wakeup interrupt.
62  */
63 @SuppressFBWarnings(value = "NP_NONNULL_PARAM_VIOLATION", justification = "Void is the only allowed value")
64 public final class DOMNotificationRouter implements AutoCloseable, DOMNotificationPublishService,
65         DOMNotificationService, DOMNotificationSubscriptionListenerRegistry {
66
67     private static final Logger LOG = LoggerFactory.getLogger(DOMNotificationRouter.class);
68     private static final ListenableFuture<Void> NO_LISTENERS = Futures.immediateFuture(null);
69     private static final WaitStrategy DEFAULT_STRATEGY = PhasedBackoffWaitStrategy
70             .withLock(1L, 30L, TimeUnit.MILLISECONDS);
71     private static final EventHandler<DOMNotificationRouterEvent> DISPATCH_NOTIFICATIONS
72             = (event, sequence, endOfBatch) -> event.deliverNotification();
73     private static final EventHandler<DOMNotificationRouterEvent> NOTIFY_FUTURE = (event, sequence, endOfBatch) -> event
74             .setFuture();
75
76     private final Disruptor<DOMNotificationRouterEvent> disruptor;
77     private final ExecutorService executor;
78     private volatile Multimap<SchemaPath, ListenerRegistration<? extends DOMNotificationListener>> listeners
79             = ImmutableMultimap.of();
80     private final ListenerRegistry<DOMNotificationSubscriptionListener> subscriptionListeners = ListenerRegistry
81             .create();
82
83     @SuppressWarnings("unchecked")
84     private DOMNotificationRouter(final ExecutorService executor, final int queueDepth, final WaitStrategy strategy) {
85         this.executor = Preconditions.checkNotNull(executor);
86
87         disruptor = new Disruptor<>(DOMNotificationRouterEvent.FACTORY, queueDepth, executor, ProducerType.MULTI,
88                                     strategy);
89         disruptor.handleEventsWith(DISPATCH_NOTIFICATIONS);
90         disruptor.after(DISPATCH_NOTIFICATIONS).handleEventsWith(NOTIFY_FUTURE);
91         disruptor.start();
92     }
93
94     public static DOMNotificationRouter create(final int queueDepth) {
95         final ExecutorService executor = Executors.newCachedThreadPool();
96
97         return new DOMNotificationRouter(executor, queueDepth, DEFAULT_STRATEGY);
98     }
99
100     public static DOMNotificationRouter create(final int queueDepth, final long spinTime, final long parkTime,
101                                                final TimeUnit unit) {
102         Preconditions.checkArgument(Long.lowestOneBit(queueDepth) == Long.highestOneBit(queueDepth),
103                                     "Queue depth %s is not power-of-two", queueDepth);
104         final ExecutorService executor = Executors.newCachedThreadPool();
105         final WaitStrategy strategy = PhasedBackoffWaitStrategy.withLock(spinTime, parkTime, unit);
106
107         return new DOMNotificationRouter(executor, queueDepth, strategy);
108     }
109
110     @Override
111     public synchronized <T extends DOMNotificationListener> ListenerRegistration<T> registerNotificationListener(
112             final T listener, final Collection<SchemaPath> types) {
113         final ListenerRegistration<T> reg = new AbstractListenerRegistration<T>(listener) {
114             @Override
115             protected void removeRegistration() {
116                 final ListenerRegistration<T> me = this;
117
118                 synchronized (DOMNotificationRouter.this) {
119                     replaceListeners(ImmutableMultimap.copyOf(Multimaps.filterValues(listeners, input -> input != me)));
120                 }
121             }
122         };
123
124         if (!types.isEmpty()) {
125             final Builder<SchemaPath, ListenerRegistration<? extends DOMNotificationListener>> b = ImmutableMultimap
126                     .builder();
127             b.putAll(listeners);
128
129             for (final SchemaPath t : types) {
130                 b.put(t, reg);
131             }
132
133             replaceListeners(b.build());
134         }
135
136         return reg;
137     }
138
139     @Override
140     public <T extends DOMNotificationListener> ListenerRegistration<T> registerNotificationListener(final T listener,
141                                                                                                     final
142                                                                                                     SchemaPath...
143                                                                                                             types) {
144         return registerNotificationListener(listener, Arrays.asList(types));
145     }
146
147     /**
148      * Swaps registered listeners and triggers notification update.
149      *
150      * @param newListeners listeners
151      */
152     private void replaceListeners(
153             final Multimap<SchemaPath, ListenerRegistration<? extends DOMNotificationListener>> newListeners) {
154         listeners = newListeners;
155         notifyListenerTypesChanged(newListeners.keySet());
156     }
157
158     @SuppressWarnings("checkstyle:IllegalCatch")
159     private void notifyListenerTypesChanged(final Set<SchemaPath> typesAfter) {
160         final List<ListenerRegistration<DOMNotificationSubscriptionListener>> listenersAfter = ImmutableList
161                 .copyOf(subscriptionListeners.getListeners());
162         executor.execute(() -> {
163             for (final ListenerRegistration<DOMNotificationSubscriptionListener> subListener : listenersAfter) {
164                 try {
165                     subListener.getInstance().onSubscriptionChanged(typesAfter);
166                 } catch (final Exception e) {
167                     LOG.warn("Uncaught exception during invoking listener {}", subListener.getInstance(), e);
168                 }
169             }
170         });
171     }
172
173     @Override
174     public <L extends DOMNotificationSubscriptionListener> ListenerRegistration<L> registerSubscriptionListener(
175             final L listener) {
176         final Set<SchemaPath> initialTypes = listeners.keySet();
177         executor.execute(() -> listener.onSubscriptionChanged(initialTypes));
178         return subscriptionListeners.registerWithType(listener);
179     }
180
181     private ListenableFuture<Void> publish(final long seq, final DOMNotification notification,
182                                            final Collection<ListenerRegistration<? extends DOMNotificationListener>>
183                                                    subscribers) {
184         final DOMNotificationRouterEvent event = disruptor.get(seq);
185         final ListenableFuture<Void> future = event.initialize(notification, subscribers);
186         disruptor.getRingBuffer().publish(seq);
187         return future;
188     }
189
190     @Override
191     public ListenableFuture<?> putNotification(final DOMNotification notification) throws InterruptedException {
192         final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers = listeners
193                 .get(notification.getType());
194         if (subscribers.isEmpty()) {
195             return NO_LISTENERS;
196         }
197
198         final long seq = disruptor.getRingBuffer().next();
199         return publish(seq, notification, subscribers);
200     }
201
202     private ListenableFuture<?> tryPublish(final DOMNotification notification,
203                                            final Collection<ListenerRegistration<? extends DOMNotificationListener>>
204                                                    subscribers) {
205         final long seq;
206         try {
207             seq = disruptor.getRingBuffer().tryNext();
208         } catch (final InsufficientCapacityException e) {
209             return DOMNotificationPublishService.REJECTED;
210         }
211
212         return publish(seq, notification, subscribers);
213     }
214
215     @Override
216     public ListenableFuture<?> offerNotification(final DOMNotification notification) {
217         final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers = listeners
218                 .get(notification.getType());
219         if (subscribers.isEmpty()) {
220             return NO_LISTENERS;
221         }
222
223         return tryPublish(notification, subscribers);
224     }
225
226     @Override
227     public ListenableFuture<?> offerNotification(final DOMNotification notification, final long timeout,
228                                                  final TimeUnit unit) throws InterruptedException {
229         final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers = listeners
230                 .get(notification.getType());
231         if (subscribers.isEmpty()) {
232             return NO_LISTENERS;
233         }
234
235         // Attempt to perform a non-blocking publish first
236         final ListenableFuture<?> noBlock = tryPublish(notification, subscribers);
237         if (!DOMNotificationPublishService.REJECTED.equals(noBlock)) {
238             return noBlock;
239         }
240
241         /*
242          * FIXME: we need a background thread, which will watch out for blocking too long. Here
243          *        we will arm a tasklet for it and synchronize delivery of interrupt properly.
244          */
245         throw new UnsupportedOperationException("Not implemented yet");
246     }
247
248     @Override
249     public void close() {
250         disruptor.shutdown();
251         executor.shutdown();
252     }
253 }