5d6010d6e2bae892a3a171a357034a433173c7d9
[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 org.opendaylight.mdsal.dom.spi.DOMNotificationSubscriptionListener;
11 import org.opendaylight.mdsal.dom.spi.DOMNotificationSubscriptionListenerRegistry;
12
13 import org.opendaylight.mdsal.dom.api.DOMNotification;
14 import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
15 import org.opendaylight.mdsal.dom.api.DOMNotificationPublishService;
16 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
17 import com.google.common.base.Preconditions;
18 import com.google.common.base.Predicate;
19 import com.google.common.collect.ImmutableList;
20 import com.google.common.collect.ImmutableMultimap;
21 import com.google.common.collect.ImmutableMultimap.Builder;
22 import com.google.common.collect.Multimap;
23 import com.google.common.collect.Multimaps;
24 import com.google.common.util.concurrent.Futures;
25 import com.google.common.util.concurrent.ListenableFuture;
26 import com.lmax.disruptor.EventHandler;
27 import com.lmax.disruptor.InsufficientCapacityException;
28 import com.lmax.disruptor.PhasedBackoffWaitStrategy;
29 import com.lmax.disruptor.WaitStrategy;
30 import com.lmax.disruptor.dsl.Disruptor;
31 import com.lmax.disruptor.dsl.ProducerType;
32 import java.util.Arrays;
33 import java.util.Collection;
34 import java.util.List;
35 import java.util.Set;
36 import java.util.concurrent.ExecutorService;
37 import java.util.concurrent.Executors;
38 import java.util.concurrent.TimeUnit;
39 import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
40 import org.opendaylight.yangtools.concepts.ListenerRegistration;
41 import org.opendaylight.yangtools.util.ListenerRegistry;
42 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 /**
47  * Joint implementation of {@link DOMNotificationPublishService} and {@link DOMNotificationService}. Provides
48  * routing of notifications from publishers to subscribers.
49  *
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  * The fully-blocking {@link #publish(long, DOMNotification, Collection)} and non-blocking {@link #offerNotification(DOMNotification)}
57  * are realized using the Disruptor's native operations. The bounded-blocking {@link #offerNotification(DOMNotification, long, TimeUnit)}
58  * is realized by arming a background wakeup interrupt.
59  */
60 public final class DOMNotificationRouter implements AutoCloseable, DOMNotificationPublishService,
61         DOMNotificationService, DOMNotificationSubscriptionListenerRegistry {
62
63     private static final Logger LOG = LoggerFactory.getLogger(DOMNotificationRouter.class);
64     private static final ListenableFuture<Void> NO_LISTENERS = Futures.immediateFuture(null);
65     private static final WaitStrategy DEFAULT_STRATEGY = PhasedBackoffWaitStrategy.withLock(1L, 30L, TimeUnit.MILLISECONDS);
66     private static final EventHandler<DOMNotificationRouterEvent> DISPATCH_NOTIFICATIONS = new EventHandler<DOMNotificationRouterEvent>() {
67         @Override
68         public void onEvent(final DOMNotificationRouterEvent event, final long sequence, final boolean endOfBatch) throws Exception {
69             event.deliverNotification();
70
71         }
72     };
73     private static final EventHandler<DOMNotificationRouterEvent> NOTIFY_FUTURE = new EventHandler<DOMNotificationRouterEvent>() {
74         @Override
75         public void onEvent(final DOMNotificationRouterEvent event, final long sequence, final boolean endOfBatch) {
76             event.setFuture();
77         }
78     };
79
80     private final Disruptor<DOMNotificationRouterEvent> disruptor;
81     private final ExecutorService executor;
82     private volatile Multimap<SchemaPath, ListenerRegistration<? extends DOMNotificationListener>> listeners = ImmutableMultimap.of();
83     private final ListenerRegistry<DOMNotificationSubscriptionListener> subscriptionListeners = ListenerRegistry.create();
84
85     @SuppressWarnings("unchecked")
86     private DOMNotificationRouter(final ExecutorService executor, final int queueDepth, final WaitStrategy strategy) {
87         this.executor = Preconditions.checkNotNull(executor);
88
89         disruptor = new Disruptor<>(DOMNotificationRouterEvent.FACTORY, queueDepth, executor, ProducerType.MULTI, strategy);
90         disruptor.handleEventsWith(DISPATCH_NOTIFICATIONS);
91         disruptor.after(DISPATCH_NOTIFICATIONS).handleEventsWith(NOTIFY_FUTURE);
92         disruptor.start();
93     }
94
95     public static DOMNotificationRouter create(final int queueDepth) {
96         final ExecutorService executor = Executors.newCachedThreadPool();
97
98         return new DOMNotificationRouter(executor, queueDepth, DEFAULT_STRATEGY);
99     }
100
101     public static DOMNotificationRouter create(final int queueDepth, final long spinTime, final long parkTime, final TimeUnit unit) {
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(final T listener, final Collection<SchemaPath> types) {
110         final ListenerRegistration<T> reg = new AbstractListenerRegistration<T>(listener) {
111             @Override
112             protected void removeRegistration() {
113                 final ListenerRegistration<T> me = this;
114
115                 synchronized (DOMNotificationRouter.this) {
116                     replaceListeners(ImmutableMultimap.copyOf(Multimaps.filterValues(listeners, new Predicate<ListenerRegistration<? extends DOMNotificationListener>>() {
117                         @Override
118                         public boolean apply(final ListenerRegistration<? extends DOMNotificationListener> input) {
119                             return input != me;
120                         }
121                     })));
122                 }
123             }
124         };
125
126         if (!types.isEmpty()) {
127             final Builder<SchemaPath, ListenerRegistration<? extends DOMNotificationListener>> b = ImmutableMultimap.builder();
128             b.putAll(listeners);
129
130             for (final SchemaPath t : types) {
131                 b.put(t, reg);
132             }
133
134             replaceListeners(b.build());
135         }
136
137         return reg;
138     }
139
140     @Override
141     public <T extends DOMNotificationListener> ListenerRegistration<T> registerNotificationListener(final T listener, final SchemaPath... types) {
142         return registerNotificationListener(listener, Arrays.asList(types));
143     }
144
145     /**
146      * Swaps registered listeners and triggers notification update
147      *
148      * @param newListeners
149      */
150     private void replaceListeners(
151             final Multimap<SchemaPath, ListenerRegistration<? extends DOMNotificationListener>> newListeners) {
152         listeners = newListeners;
153         notifyListenerTypesChanged(newListeners.keySet());
154     }
155
156     private void notifyListenerTypesChanged(final Set<SchemaPath> typesAfter) {
157         final List<ListenerRegistration<DOMNotificationSubscriptionListener>> listenersAfter =ImmutableList.copyOf(subscriptionListeners.getListeners());
158         executor.submit(new Runnable() {
159
160             @Override
161             public void run() {
162                 for (final ListenerRegistration<DOMNotificationSubscriptionListener> subListener : listenersAfter) {
163                     try {
164                         subListener.getInstance().onSubscriptionChanged(typesAfter);
165                     } catch (final Exception e) {
166                         LOG.warn("Uncaught exception during invoking listener {}", subListener.getInstance(), e);
167                     }
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.submit(new Runnable() {
178
179             @Override
180             public void run() {
181                 listener.onSubscriptionChanged(initialTypes);
182             }
183         });
184         return subscriptionListeners.registerWithType(listener);
185     }
186
187     private ListenableFuture<Void> publish(final long seq, final DOMNotification notification, 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) throws InterruptedException {
196         final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers = 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     private ListenableFuture<? extends Object> tryPublish(final DOMNotification notification, final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers) {
206         final long seq;
207         try {
208              seq = disruptor.getRingBuffer().tryNext();
209         } catch (final InsufficientCapacityException e) {
210             return DOMNotificationPublishService.REJECTED;
211         }
212
213         return publish(seq, notification, subscribers);
214     }
215
216     @Override
217     public ListenableFuture<? extends Object> offerNotification(final DOMNotification notification) {
218         final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers = listeners.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<? extends Object> offerNotification(final DOMNotification notification, final long timeout,
228             final TimeUnit unit) throws InterruptedException {
229         final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers = listeners.get(notification.getType());
230         if (subscribers.isEmpty()) {
231             return NO_LISTENERS;
232         }
233
234         // Attempt to perform a non-blocking publish first
235         final ListenableFuture<? extends Object> noBlock = tryPublish(notification, subscribers);
236         if (!DOMNotificationPublishService.REJECTED.equals(noBlock)) {
237             return noBlock;
238         }
239
240         /*
241          * FIXME: we need a background thread, which will watch out for blocking too long. Here
242          *        we will arm a tasklet for it and synchronize delivery of interrupt properly.
243          */
244         throw new UnsupportedOperationException("Not implemented yet");
245     }
246
247     @Override
248     public void close() {
249         disruptor.shutdown();
250         executor.shutdown();
251     }
252 }