2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.md.sal.dom.broker.impl;
10 import org.opendaylight.mdsal.dom.api.DOMNotification;
11 import org.opendaylight.mdsal.dom.api.DOMNotificationListener;
12 import org.opendaylight.mdsal.dom.api.DOMNotificationPublishService;
13 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
15 import com.google.common.base.Preconditions;
16 import com.google.common.base.Predicate;
17 import com.google.common.collect.ImmutableList;
18 import com.google.common.collect.ImmutableMultimap;
19 import com.google.common.collect.ImmutableMultimap.Builder;
20 import com.google.common.collect.Multimap;
21 import com.google.common.collect.Multimaps;
22 import com.google.common.util.concurrent.Futures;
23 import com.google.common.util.concurrent.ListenableFuture;
24 import com.lmax.disruptor.EventHandler;
25 import com.lmax.disruptor.InsufficientCapacityException;
26 import com.lmax.disruptor.PhasedBackoffWaitStrategy;
27 import com.lmax.disruptor.WaitStrategy;
28 import com.lmax.disruptor.dsl.Disruptor;
29 import com.lmax.disruptor.dsl.ProducerType;
30 import java.util.Arrays;
31 import java.util.Collection;
32 import java.util.List;
34 import java.util.concurrent.ExecutorService;
35 import java.util.concurrent.Executors;
36 import java.util.concurrent.TimeUnit;
37 import org.opendaylight.controller.md.sal.dom.spi.DOMNotificationSubscriptionListener;
38 import org.opendaylight.controller.md.sal.dom.spi.DOMNotificationSubscriptionListenerRegistry;
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;
47 * Joint implementation of {@link DOMNotificationPublishService} and {@link DOMNotificationService}. Provides
48 * routing of notifications from publishers to subscribers.
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.
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.
60 public final class DOMNotificationRouter implements AutoCloseable, DOMNotificationPublishService,
61 DOMNotificationService, DOMNotificationSubscriptionListenerRegistry {
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>() {
68 public void onEvent(final DOMNotificationRouterEvent event, final long sequence, final boolean endOfBatch) throws Exception {
69 event.deliverNotification();
73 private static final EventHandler<DOMNotificationRouterEvent> NOTIFY_FUTURE = new EventHandler<DOMNotificationRouterEvent>() {
75 public void onEvent(final DOMNotificationRouterEvent event, final long sequence, final boolean endOfBatch) {
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();
85 @SuppressWarnings("unchecked")
86 private DOMNotificationRouter(final ExecutorService executor, final int queueDepth, final WaitStrategy strategy) {
87 this.executor = Preconditions.checkNotNull(executor);
89 disruptor = new Disruptor<>(DOMNotificationRouterEvent.FACTORY, queueDepth, executor, ProducerType.MULTI, strategy);
90 disruptor.handleEventsWith(DISPATCH_NOTIFICATIONS);
91 disruptor.after(DISPATCH_NOTIFICATIONS).handleEventsWith(NOTIFY_FUTURE);
95 public static DOMNotificationRouter create(final int queueDepth) {
96 final ExecutorService executor = Executors.newCachedThreadPool();
98 return new DOMNotificationRouter(executor, queueDepth, DEFAULT_STRATEGY);
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);
105 return new DOMNotificationRouter(executor, queueDepth, strategy);
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) {
112 protected void removeRegistration() {
113 final ListenerRegistration<T> me = this;
115 synchronized (DOMNotificationRouter.this) {
116 replaceListeners(ImmutableMultimap.copyOf(Multimaps.filterValues(listeners, new Predicate<ListenerRegistration<? extends DOMNotificationListener>>() {
118 public boolean apply(final ListenerRegistration<? extends DOMNotificationListener> input) {
126 if (!types.isEmpty()) {
127 final Builder<SchemaPath, ListenerRegistration<? extends DOMNotificationListener>> b = ImmutableMultimap.builder();
130 for (final SchemaPath t : types) {
134 replaceListeners(b.build());
141 public <T extends DOMNotificationListener> ListenerRegistration<T> registerNotificationListener(final T listener, final SchemaPath... types) {
142 return registerNotificationListener(listener, Arrays.asList(types));
146 * Swaps registered listeners and triggers notification update
148 * @param newListeners
150 private void replaceListeners(
151 final Multimap<SchemaPath, ListenerRegistration<? extends DOMNotificationListener>> newListeners) {
152 listeners = newListeners;
153 notifyListenerTypesChanged(newListeners.keySet());
156 private void notifyListenerTypesChanged(final Set<SchemaPath> typesAfter) {
157 final List<ListenerRegistration<DOMNotificationSubscriptionListener>> listenersAfter =ImmutableList.copyOf(subscriptionListeners.getListeners());
158 executor.submit(new Runnable() {
162 for (final ListenerRegistration<DOMNotificationSubscriptionListener> subListener : listenersAfter) {
164 subListener.getInstance().onSubscriptionChanged(typesAfter);
165 } catch (final Exception e) {
166 LOG.warn("Uncaught exception during invoking listener {}", subListener.getInstance(), e);
174 public <L extends DOMNotificationSubscriptionListener> ListenerRegistration<L> registerSubscriptionListener(
176 final Set<SchemaPath> initialTypes = listeners.keySet();
177 executor.submit(new Runnable() {
181 listener.onSubscriptionChanged(initialTypes);
184 return subscriptionListeners.registerWithType(listener);
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);
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()) {
201 final long seq = disruptor.getRingBuffer().next();
202 return publish(seq, notification, subscribers);
205 private ListenableFuture<? extends Object> tryPublish(final DOMNotification notification, final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers) {
208 seq = disruptor.getRingBuffer().tryNext();
209 } catch (final InsufficientCapacityException e) {
210 return DOMNotificationPublishService.REJECTED;
213 return publish(seq, notification, subscribers);
217 public ListenableFuture<? extends Object> offerNotification(final DOMNotification notification) {
218 final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers = listeners.get(notification.getType());
219 if (subscribers.isEmpty()) {
223 return tryPublish(notification, subscribers);
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()) {
234 // Attempt to perform a non-blocking publish first
235 final ListenableFuture<? extends Object> noBlock = tryPublish(notification, subscribers);
236 if (!DOMNotificationPublishService.REJECTED.equals(noBlock)) {
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.
244 throw new UnsupportedOperationException("Not implemented yet");
248 public void close() {
249 disruptor.shutdown();