53f24dcaa72e0006d51d3720c12ebfe561c8f407
[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.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.mdsal.dom.api.DOMNotification;
32 import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
33 import org.opendaylight.mdsal.dom.api.DOMNotificationPublishService;
34 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
35 import org.opendaylight.mdsal.dom.spi.DOMNotificationSubscriptionListener;
36 import org.opendaylight.mdsal.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)}
57  * and non-blocking {@link #offerNotification(DOMNotification)}
58  * are realized using the Disruptor's native operations. The bounded-blocking {@link
59  * #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.withLock(
68             1L, 30L, TimeUnit.MILLISECONDS);
69     private static final EventHandler<DOMNotificationRouterEvent> DISPATCH_NOTIFICATIONS =
70             new EventHandler<DOMNotificationRouterEvent>() {
71         @Override
72         public void onEvent(final DOMNotificationRouterEvent event, final long sequence,
73                 final boolean endOfBatch) throws Exception {
74             event.deliverNotification();
75
76         }
77     };
78     private static final EventHandler<DOMNotificationRouterEvent> NOTIFY_FUTURE =
79             new EventHandler<DOMNotificationRouterEvent>() {
80         @Override
81         public void onEvent(final DOMNotificationRouterEvent event, final long sequence, final boolean endOfBatch) {
82             event.setFuture();
83         }
84     };
85
86     private final Disruptor<DOMNotificationRouterEvent> disruptor;
87     private final ExecutorService executor;
88     private volatile Multimap<SchemaPath, ListenerRegistration<? extends
89             DOMNotificationListener>> listeners = ImmutableMultimap.of();
90     private final ListenerRegistry<DOMNotificationSubscriptionListener> subscriptionListeners =
91             ListenerRegistry.create();
92
93     @SuppressWarnings("unchecked")
94     private DOMNotificationRouter(final ExecutorService executor, final int queueDepth, final WaitStrategy strategy) {
95         this.executor = Preconditions.checkNotNull(executor);
96
97         disruptor = new Disruptor<>(DOMNotificationRouterEvent.FACTORY,
98                 queueDepth, executor, ProducerType.MULTI, strategy);
99         disruptor.handleEventsWith(DISPATCH_NOTIFICATIONS);
100         disruptor.after(DISPATCH_NOTIFICATIONS).handleEventsWith(NOTIFY_FUTURE);
101         disruptor.start();
102     }
103
104     public static DOMNotificationRouter create(final int queueDepth) {
105         final ExecutorService executor = Executors.newCachedThreadPool();
106
107         return new DOMNotificationRouter(executor, queueDepth, DEFAULT_STRATEGY);
108     }
109
110     public static DOMNotificationRouter create(final int queueDepth, final long spinTime,
111             final long parkTime, final TimeUnit unit) {
112         final ExecutorService executor = Executors.newCachedThreadPool();
113         final WaitStrategy strategy = PhasedBackoffWaitStrategy.withLock(spinTime, parkTime, unit);
114
115         return new DOMNotificationRouter(executor, queueDepth, strategy);
116     }
117
118     @Override
119     public synchronized <T extends DOMNotificationListener> ListenerRegistration<T> registerNotificationListener(
120             final T listener, final Collection<SchemaPath> types) {
121         final ListenerRegistration<T> reg = new AbstractListenerRegistration<T>(listener) {
122             @Override
123             protected void removeRegistration() {
124                 synchronized (DOMNotificationRouter.this) {
125                     replaceListeners(ImmutableMultimap.copyOf(Multimaps.filterValues(listeners,
126                         input -> input != this)));
127                 }
128             }
129         };
130
131         if (!types.isEmpty()) {
132             final Builder<SchemaPath, ListenerRegistration<? extends DOMNotificationListener>> b =
133                     ImmutableMultimap.builder();
134             b.putAll(listeners);
135
136             for (final SchemaPath t : types) {
137                 b.put(t, reg);
138             }
139
140             replaceListeners(b.build());
141         }
142
143         return reg;
144     }
145
146     @Override
147     public <T extends DOMNotificationListener> ListenerRegistration<T> registerNotificationListener(
148             final T listener, final SchemaPath... types) {
149         return registerNotificationListener(listener, Arrays.asList(types));
150     }
151
152     /**
153      * Swaps registered listeners and triggers notification update.
154      *
155      * @param newListeners is used to notify listenerTypes changed
156      */
157     private void replaceListeners(
158             final Multimap<SchemaPath, ListenerRegistration<? extends DOMNotificationListener>> newListeners) {
159         listeners = newListeners;
160         notifyListenerTypesChanged(newListeners.keySet());
161     }
162
163     @SuppressWarnings("checkstyle:IllegalCatch")
164     private void notifyListenerTypesChanged(final Set<SchemaPath> typesAfter) {
165         final List<ListenerRegistration<DOMNotificationSubscriptionListener>> listenersAfter =
166                 ImmutableList.copyOf(subscriptionListeners.getListeners());
167         executor.submit(() -> {
168             for (final ListenerRegistration<DOMNotificationSubscriptionListener> subListener : listenersAfter) {
169                 try {
170                     subListener.getInstance().onSubscriptionChanged(typesAfter);
171                 } catch (final Exception e) {
172                     LOG.warn("Uncaught exception during invoking listener {}", subListener.getInstance(), e);
173                 }
174             }
175         });
176     }
177
178     @Override
179     public <L extends DOMNotificationSubscriptionListener> ListenerRegistration<L> registerSubscriptionListener(
180             final L listener) {
181         final Set<SchemaPath> initialTypes = listeners.keySet();
182         executor.submit(() -> listener.onSubscriptionChanged(initialTypes));
183         return subscriptionListeners.registerWithType(listener);
184     }
185
186     private ListenableFuture<Void> publish(final long seq, final DOMNotification notification,
187             final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers) {
188         final DOMNotificationRouterEvent event = disruptor.get(seq);
189         final ListenableFuture<Void> future = event.initialize(notification, subscribers);
190         disruptor.getRingBuffer().publish(seq);
191         return future;
192     }
193
194     @Override
195     public ListenableFuture<? extends Object> putNotification(final DOMNotification notification)
196             throws InterruptedException {
197         final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers =
198                 listeners.get(notification.getType());
199         if (subscribers.isEmpty()) {
200             return NO_LISTENERS;
201         }
202
203         final long seq = disruptor.getRingBuffer().next();
204         return publish(seq, notification, subscribers);
205     }
206
207     @SuppressWarnings("checkstyle:IllegalCatch")
208     private ListenableFuture<? extends Object> tryPublish(final DOMNotification notification,
209             final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers) {
210         final long seq;
211         try {
212             seq = disruptor.getRingBuffer().tryNext();
213         } catch (final InsufficientCapacityException e) {
214             return DOMNotificationPublishService.REJECTED;
215         }
216
217         return publish(seq, notification, subscribers);
218     }
219
220     @Override
221     public ListenableFuture<? extends Object> offerNotification(final DOMNotification notification) {
222         final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers =
223                 listeners.get(notification.getType());
224         if (subscribers.isEmpty()) {
225             return NO_LISTENERS;
226         }
227
228         return tryPublish(notification, subscribers);
229     }
230
231     @Override
232     public ListenableFuture<? extends Object> offerNotification(final DOMNotification notification, final long timeout,
233             final TimeUnit unit) throws InterruptedException {
234         final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers =
235                 listeners.get(notification.getType());
236         if (subscribers.isEmpty()) {
237             return NO_LISTENERS;
238         }
239
240         // Attempt to perform a non-blocking publish first
241         final ListenableFuture<? extends Object> noBlock = tryPublish(notification, subscribers);
242         if (!DOMNotificationPublishService.REJECTED.equals(noBlock)) {
243             return noBlock;
244         }
245
246         /*
247          * FIXME: we need a background thread, which will watch out for blocking too long. Here
248          *        we will arm a tasklet for it and synchronize delivery of interrupt properly.
249          */
250         throw new UnsupportedOperationException("Not implemented yet");
251     }
252
253     @Override
254     public void close() {
255         disruptor.shutdown();
256         executor.shutdown();
257     }
258 }