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