|
| 1 | +/* |
| 2 | + * Copyright 2008-present MongoDB, Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.mongodb.internal.connection; |
| 18 | + |
| 19 | +import com.mongodb.annotations.ThreadSafe; |
| 20 | +import com.mongodb.connection.ClusterId; |
| 21 | +import com.mongodb.event.ClusterClosedEvent; |
| 22 | +import com.mongodb.event.ClusterDescriptionChangedEvent; |
| 23 | +import com.mongodb.event.ClusterListener; |
| 24 | +import com.mongodb.event.ClusterOpeningEvent; |
| 25 | +import com.mongodb.event.ServerClosedEvent; |
| 26 | +import com.mongodb.event.ServerDescriptionChangedEvent; |
| 27 | +import com.mongodb.event.ServerHeartbeatFailedEvent; |
| 28 | +import com.mongodb.event.ServerHeartbeatStartedEvent; |
| 29 | +import com.mongodb.event.ServerHeartbeatSucceededEvent; |
| 30 | +import com.mongodb.event.ServerListener; |
| 31 | +import com.mongodb.event.ServerMonitorListener; |
| 32 | +import com.mongodb.event.ServerOpeningEvent; |
| 33 | +import com.mongodb.internal.VisibleForTesting; |
| 34 | + |
| 35 | +import java.util.concurrent.BlockingQueue; |
| 36 | +import java.util.concurrent.LinkedBlockingQueue; |
| 37 | +import java.util.function.Supplier; |
| 38 | + |
| 39 | +import static com.mongodb.assertions.Assertions.notNull; |
| 40 | +import static com.mongodb.internal.VisibleForTesting.AccessModifier.PRIVATE; |
| 41 | + |
| 42 | +/** |
| 43 | + * An implementation of a listener for all cluster-related events. Its purpose is the following: |
| 44 | + * |
| 45 | + * 1. To ensure that cluster-related events are delivered one at a time, with happens-before semantics |
| 46 | + * 2. To ensure that application-provided event listener methods do not execute within critical sections of the driver |
| 47 | + * |
| 48 | + * This is done by adding all events to an unbounded blocking queue, and then publishing them from a dedicated thread by taking |
| 49 | + * them off the queue one at a time. |
| 50 | + * |
| 51 | + * There is an assumption that the last event that should be published is the {@link ClusterClosedEvent}. Once that event is published, |
| 52 | + * the publishing thread is allowed to die. |
| 53 | + */ |
| 54 | +@ThreadSafe |
| 55 | +final class AsynchronousClusterEventListener implements ClusterListener, ServerListener, ServerMonitorListener { |
| 56 | + private final BlockingQueue<Supplier<Boolean>> eventPublishers = new LinkedBlockingQueue<>(); |
| 57 | + private final ClusterListener clusterListener; |
| 58 | + private final ServerListener serverListener; |
| 59 | + private final ServerMonitorListener serverMonitorListener; |
| 60 | + |
| 61 | + private final Thread publishingThread; |
| 62 | + |
| 63 | + @FunctionalInterface |
| 64 | + private interface VoidFunction<T> { |
| 65 | + void apply(T t); |
| 66 | + } |
| 67 | + |
| 68 | + static AsynchronousClusterEventListener startNew(final ClusterId clusterId, final ClusterListener clusterListener, |
| 69 | + final ServerListener serverListener, final ServerMonitorListener serverMonitorListener) { |
| 70 | + AsynchronousClusterEventListener result = new AsynchronousClusterEventListener(clusterId, clusterListener, serverListener, |
| 71 | + serverMonitorListener); |
| 72 | + result.publishingThread.start(); |
| 73 | + return result; |
| 74 | + } |
| 75 | + |
| 76 | + private AsynchronousClusterEventListener(final ClusterId clusterId, final ClusterListener clusterListener, |
| 77 | + final ServerListener serverListener, final ServerMonitorListener serverMonitorListener) { |
| 78 | + this.clusterListener = notNull("clusterListener", clusterListener); |
| 79 | + this.serverListener = notNull("serverListener", serverListener); |
| 80 | + this.serverMonitorListener = notNull("serverMonitorListener", serverMonitorListener); |
| 81 | + publishingThread = new Thread(this::publishEvents, "cluster-event-publisher-" + clusterId.getValue()); |
| 82 | + publishingThread.setDaemon(true); |
| 83 | + } |
| 84 | + |
| 85 | + @VisibleForTesting(otherwise = PRIVATE) |
| 86 | + Thread getPublishingThread() { |
| 87 | + return publishingThread; |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public void clusterOpening(final ClusterOpeningEvent event) { |
| 92 | + addClusterEventInvocation(clusterListener -> clusterListener.clusterOpening(event), false); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public void clusterClosed(final ClusterClosedEvent event) { |
| 97 | + addClusterEventInvocation(clusterListener -> clusterListener.clusterClosed(event), true); |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public void clusterDescriptionChanged(final ClusterDescriptionChangedEvent event) { |
| 102 | + addClusterEventInvocation(clusterListener -> clusterListener.clusterDescriptionChanged(event), false); |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public void serverOpening(final ServerOpeningEvent event) { |
| 107 | + addServerEventInvocation(serverListener -> serverListener.serverOpening(event)); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public void serverClosed(final ServerClosedEvent event) { |
| 112 | + addServerEventInvocation(serverListener -> serverListener.serverClosed(event)); |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public void serverDescriptionChanged(final ServerDescriptionChangedEvent event) { |
| 117 | + addServerEventInvocation(serverListener -> serverListener.serverDescriptionChanged(event)); |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public void serverHearbeatStarted(final ServerHeartbeatStartedEvent event) { |
| 122 | + addServerMonitorEventInvocation(serverMonitorListener -> serverMonitorListener.serverHearbeatStarted(event)); |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public void serverHeartbeatSucceeded(final ServerHeartbeatSucceededEvent event) { |
| 127 | + addServerMonitorEventInvocation(serverMonitorListener -> serverMonitorListener.serverHeartbeatSucceeded(event)); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public void serverHeartbeatFailed(final ServerHeartbeatFailedEvent event) { |
| 132 | + addServerMonitorEventInvocation(serverMonitorListener -> serverMonitorListener.serverHeartbeatFailed(event)); |
| 133 | + } |
| 134 | + |
| 135 | + private void addClusterEventInvocation(final VoidFunction<ClusterListener> eventPublisher, final boolean isLastEvent) { |
| 136 | + addEvent(() -> { |
| 137 | + eventPublisher.apply(clusterListener); |
| 138 | + return isLastEvent; |
| 139 | + }); |
| 140 | + } |
| 141 | + |
| 142 | + private void addServerEventInvocation(final VoidFunction<ServerListener> eventPublisher) { |
| 143 | + addEvent(() -> { |
| 144 | + eventPublisher.apply(serverListener); |
| 145 | + return false; |
| 146 | + }); |
| 147 | + } |
| 148 | + |
| 149 | + private void addServerMonitorEventInvocation(final VoidFunction<ServerMonitorListener> eventPublisher) { |
| 150 | + addEvent(() -> { |
| 151 | + eventPublisher.apply(serverMonitorListener); |
| 152 | + return false; |
| 153 | + }); |
| 154 | + } |
| 155 | + |
| 156 | + private void addEvent(final Supplier<Boolean> supplier) { |
| 157 | + // protect against rogue publishers |
| 158 | + if (!publishingThread.isAlive()) { |
| 159 | + return; |
| 160 | + } |
| 161 | + eventPublishers.add(supplier); |
| 162 | + } |
| 163 | + |
| 164 | + private void publishEvents() { |
| 165 | + while (true) { |
| 166 | + try { |
| 167 | + Supplier<Boolean> eventPublisher = eventPublishers.take(); |
| 168 | + boolean isLastEvent = eventPublisher.get(); |
| 169 | + if (isLastEvent) { |
| 170 | + break; |
| 171 | + } |
| 172 | + } catch (RuntimeException | InterruptedException e) { |
| 173 | + // ignore exceptions thrown from listeners, also ignore interrupts that user code may cause |
| 174 | + } |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments