import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
+import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableMultimap.Builder;
import com.google.common.collect.Multimap;
import com.lmax.disruptor.dsl.ProducerType;
import java.util.Arrays;
import java.util.Collection;
+import java.util.List;
+import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import org.opendaylight.controller.md.sal.dom.api.DOMNotificationListener;
import org.opendaylight.controller.md.sal.dom.api.DOMNotificationPublishService;
import org.opendaylight.controller.md.sal.dom.api.DOMNotificationService;
+import org.opendaylight.controller.md.sal.dom.spi.DOMNotificationSubscriptionListener;
+import org.opendaylight.controller.md.sal.dom.spi.DOMNotificationSubscriptionListenerRegistry;
import org.opendaylight.yangtools.concepts.AbstractListenerRegistration;
import org.opendaylight.yangtools.concepts.ListenerRegistration;
+import org.opendaylight.yangtools.util.ListenerRegistry;
import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
/**
* Joint implementation of {@link DOMNotificationPublishService} and {@link DOMNotificationService}. Provides
* are realized using the Disruptor's native operations. The bounded-blocking {@link #offerNotification(DOMNotification, long, TimeUnit)}
* is realized by arming a background wakeup interrupt.
*/
-public final class DOMNotificationRouter implements AutoCloseable, DOMNotificationPublishService, DOMNotificationService {
+public final class DOMNotificationRouter implements AutoCloseable, DOMNotificationPublishService,
+ DOMNotificationService, DOMNotificationSubscriptionListenerRegistry {
+
+ private static final Logger LOG = LoggerFactory.getLogger(DOMNotificationRouter.class);
private static final ListenableFuture<Void> NO_LISTENERS = Futures.immediateFuture(null);
private static final WaitStrategy DEFAULT_STRATEGY = PhasedBackoffWaitStrategy.withLock(1L, 30L, TimeUnit.MILLISECONDS);
private static final EventHandler<DOMNotificationRouterEvent> DISPATCH_NOTIFICATIONS = new EventHandler<DOMNotificationRouterEvent>() {
private final Disruptor<DOMNotificationRouterEvent> disruptor;
private final ExecutorService executor;
private volatile Multimap<SchemaPath, ListenerRegistration<? extends DOMNotificationListener>> listeners = ImmutableMultimap.of();
+ private final ListenerRegistry<DOMNotificationSubscriptionListener> subscriptionListeners = ListenerRegistry.create();
- private DOMNotificationRouter(final ExecutorService executor, final Disruptor<DOMNotificationRouterEvent> disruptor) {
+ @SuppressWarnings("unchecked")
+ private DOMNotificationRouter(final ExecutorService executor, final int queueDepth, final WaitStrategy strategy) {
this.executor = Preconditions.checkNotNull(executor);
- this.disruptor = Preconditions.checkNotNull(disruptor);
+
+ disruptor = new Disruptor<>(DOMNotificationRouterEvent.FACTORY, queueDepth, executor, ProducerType.MULTI, strategy);
+ disruptor.handleEventsWith(DISPATCH_NOTIFICATIONS);
+ disruptor.after(DISPATCH_NOTIFICATIONS).handleEventsWith(NOTIFY_FUTURE);
+ disruptor.start();
}
- @SuppressWarnings("unchecked")
public static DOMNotificationRouter create(final int queueDepth) {
final ExecutorService executor = Executors.newCachedThreadPool();
- final Disruptor<DOMNotificationRouterEvent> disruptor = new Disruptor<>(DOMNotificationRouterEvent.FACTORY, queueDepth, executor, ProducerType.MULTI, DEFAULT_STRATEGY);
- disruptor.handleEventsWith(DISPATCH_NOTIFICATIONS);
- disruptor.after(DISPATCH_NOTIFICATIONS).handleEventsWith(NOTIFY_FUTURE);
- disruptor.start();
+ return new DOMNotificationRouter(executor, queueDepth, DEFAULT_STRATEGY);
+ }
- return new DOMNotificationRouter(executor, disruptor);
+ public static DOMNotificationRouter create(final int queueDepth, final long spinTime, final long parkTime, final TimeUnit unit) {
+ Preconditions.checkArgument(Long.lowestOneBit(queueDepth) == Long.highestOneBit(queueDepth),
+ "Queue depth %s is not power-of-two", queueDepth);
+ final ExecutorService executor = Executors.newCachedThreadPool();
+ final WaitStrategy strategy = PhasedBackoffWaitStrategy.withLock(spinTime, parkTime, unit);
+
+ return new DOMNotificationRouter(executor, queueDepth, strategy);
}
@Override
final ListenerRegistration<T> me = this;
synchronized (DOMNotificationRouter.this) {
- listeners = ImmutableMultimap.copyOf(Multimaps.filterValues(listeners, new Predicate<ListenerRegistration<? extends DOMNotificationListener>>() {
+ replaceListeners(ImmutableMultimap.copyOf(Multimaps.filterValues(listeners, new Predicate<ListenerRegistration<? extends DOMNotificationListener>>() {
@Override
public boolean apply(final ListenerRegistration<? extends DOMNotificationListener> input) {
return input != me;
}
- }));
+ })));
}
}
};
final Builder<SchemaPath, ListenerRegistration<? extends DOMNotificationListener>> b = ImmutableMultimap.builder();
b.putAll(listeners);
- for (SchemaPath t : types) {
+ for (final SchemaPath t : types) {
b.put(t, reg);
}
- listeners = b.build();
+ replaceListeners(b.build());
}
return reg;
return registerNotificationListener(listener, Arrays.asList(types));
}
+ /**
+ * Swaps registered listeners and triggers notification update
+ *
+ * @param newListeners
+ */
+ private void replaceListeners(
+ final Multimap<SchemaPath, ListenerRegistration<? extends DOMNotificationListener>> newListeners) {
+ listeners = newListeners;
+ notifyListenerTypesChanged(newListeners.keySet());
+ }
+
+ private void notifyListenerTypesChanged(final Set<SchemaPath> typesAfter) {
+ final List<ListenerRegistration<DOMNotificationSubscriptionListener>> listenersAfter =ImmutableList.copyOf(subscriptionListeners.getListeners());
+ executor.submit(new Runnable() {
+
+ @Override
+ public void run() {
+ for (final ListenerRegistration<DOMNotificationSubscriptionListener> subListener : listenersAfter) {
+ try {
+ subListener.getInstance().onSubscriptionChanged(typesAfter);
+ } catch (final Exception e) {
+ LOG.warn("Uncaught exception during invoking listener {}", subListener.getInstance(), e);
+ }
+ }
+ }
+ });
+ }
+
+ @Override
+ public <L extends DOMNotificationSubscriptionListener> ListenerRegistration<L> registerSubscriptionListener(
+ final L listener) {
+ final Set<SchemaPath> initialTypes = listeners.keySet();
+ executor.submit(new Runnable() {
+
+ @Override
+ public void run() {
+ listener.onSubscriptionChanged(initialTypes);
+ }
+ });
+ return subscriptionListeners.registerWithType(listener);
+ }
+
private ListenableFuture<Void> publish(final long seq, final DOMNotification notification, final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers) {
final DOMNotificationRouterEvent event = disruptor.get(seq);
final ListenableFuture<Void> future = event.initialize(notification, subscribers);
}
@Override
- public ListenableFuture<? extends Object> putNotification(final DOMNotification notification) throws InterruptedException {
+ public ListenableFuture<?> putNotification(final DOMNotification notification) throws InterruptedException {
final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers = listeners.get(notification.getType());
if (subscribers.isEmpty()) {
return NO_LISTENERS;
return publish(seq, notification, subscribers);
}
- private ListenableFuture<? extends Object> tryPublish(final DOMNotification notification, final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers) {
+ private ListenableFuture<?> tryPublish(final DOMNotification notification, final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers) {
final long seq;
try {
seq = disruptor.getRingBuffer().tryNext();
- } catch (InsufficientCapacityException e) {
+ } catch (final InsufficientCapacityException e) {
return DOMNotificationPublishService.REJECTED;
}
}
@Override
- public ListenableFuture<? extends Object> offerNotification(final DOMNotification notification) {
+ public ListenableFuture<?> offerNotification(final DOMNotification notification) {
final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers = listeners.get(notification.getType());
if (subscribers.isEmpty()) {
return NO_LISTENERS;
}
@Override
- public ListenableFuture<? extends Object> offerNotification(final DOMNotification notification, final long timeout,
+ public ListenableFuture<?> offerNotification(final DOMNotification notification, final long timeout,
final TimeUnit unit) throws InterruptedException {
final Collection<ListenerRegistration<? extends DOMNotificationListener>> subscribers = listeners.get(notification.getType());
if (subscribers.isEmpty()) {
}
// Attempt to perform a non-blocking publish first
- final ListenableFuture<? extends Object> noBlock = tryPublish(notification, subscribers);
+ final ListenableFuture<?> noBlock = tryPublish(notification, subscribers);
if (!DOMNotificationPublishService.REJECTED.equals(noBlock)) {
return noBlock;
}