| | |
| | | import java.util.concurrent.ThreadPoolExecutor; |
| | | import java.util.concurrent.TimeUnit; |
| | | import java.util.concurrent.atomic.AtomicBoolean; |
| | | import java.util.concurrent.atomic.AtomicInteger; |
| | | import java.util.concurrent.atomic.AtomicReference; |
| | | |
| | | import net.jcip.annotations.GuardedBy; |
| | |
| | | } |
| | | |
| | | /** |
| | | * A stop of the broker, from the moment {@link #stop()} begins until {@link #start()} clears it. |
| | | * A registered stop of the broker, kept until {@link #start()} clears it. |
| | | * <p> |
| | | * What a publish gives up for the stop is marked on the stop, and not on the broker: a publish |
| | | * which found the stop past its deadline may mark it only after start() cleared it, and then the |
| | |
| | | /** A backend whose state could not be restored must never reconnect through this broker. */ |
| | | private volatile boolean permanentlyStopped; |
| | | /** |
| | | * The stop in progress: set by {@link #stop()} before anything else it does, and cleared to |
| | | * {@code null} by {@link #start()}. While it is set, a publish which finds no window credit by |
| | | * its deadline gives its message up instead of waiting on. |
| | | * The stop in progress, registered under {@link #startStopLock} and cleared to {@code null} |
| | | * by {@link #start()}. While it is set, a publish which finds no window credit by its deadline |
| | | * gives its message up instead of waiting on. |
| | | * <p> |
| | | * stop() publishes the replica offline message before it sets {@link #shutdown}, the flag which |
| | | * ends the wait for credit, and that message needs credit too, and the lock of the pending |
| | |
| | | */ |
| | | private volatile StopInProgress stopping; |
| | | private final Object startStopLock = new Object(); |
| | | /** Stop requests waiting for the lock are visible to the reconnect that holds it. */ |
| | | private final AtomicInteger pendingStopRequests = new AtomicInteger(); |
| | | private volatile ReplicationDomainCfg config; |
| | | /** String reported under CSN=monitor when there is no connected RS. */ |
| | | static final HostPort NO_CONNECTED_SERVER = new HostPort(null, 0); |
| | |
| | | } |
| | | computeNewTopology(toRSInfos(rsInfos)); |
| | | |
| | | final boolean canRetry = previousRS != null && stopping == null && !permanentlyStopped; |
| | | final boolean canRetry = previousRS != null && canRetryFailover(); |
| | | final boolean hasReplies = !rsInfos.isEmpty(); |
| | | String electedURL = null; |
| | | if (hasReplies) |
| | |
| | | discovery.close(); |
| | | } |
| | | ConnectedRS electedRS = performPhaseOneHandshake(electedURL, true, canRetry); |
| | | if (electedRS.rsInfo == null && canRetry && stopping == null && !permanentlyStopped) |
| | | if (electedRS.rsInfo == null && canRetry && canRetryFailover()) |
| | | { |
| | | electedRS = retryElectedConnection(discovery, rsInfos, electedURL, previousRS, electedRS); |
| | | } |
| | |
| | | final String timedOutRS = electedFormerRS && responder == null ? discovery.timedOutServer(previousRS) : null; |
| | | discovery.close(); |
| | | |
| | | if (stopping != null || permanentlyStopped) |
| | | if (!canRetryFailover()) |
| | | { |
| | | return failure; |
| | | } |
| | |
| | | return failure; |
| | | } |
| | | |
| | | private boolean canRetryFailover() |
| | | { |
| | | return pendingStopRequests.get() == 0 && stopping == null && !permanentlyStopped; |
| | | } |
| | | |
| | | /** Opens the selected RS session using the fresh state returned by its start reply. */ |
| | | private void finishElectedConnection(ConnectedRS electedRS, Map<Integer, ReplicationServerInfo> rsInfos) |
| | | { |
| | |
| | | |
| | | private ConnectedRS connectToDiscoveryResponder(ReplicationServerInfo responder) |
| | | { |
| | | final ConnectedRS replacement = performPhaseOneHandshake(responder.getServerURL(), true, false); |
| | | final String url = responder.getServerURL(); |
| | | ConnectedRS replacement = performPhaseOneHandshake(url, true, true); |
| | | if (replacement == ConnectedRS.START_REPLY_TIMED_OUT && canRetryFailover()) |
| | | { |
| | | replacement = performPhaseOneHandshake(url, true, false); |
| | | } |
| | | // The generation may have changed since discovery. |
| | | if (replacement.rsInfo != null && !hasCompatibleGeneration(replacement.rsInfo)) |
| | | { |
| | |
| | | } |
| | | final StopInProgress stop; |
| | | final boolean newStop; |
| | | synchronized (startStopLock) { |
| | | if (shutdown) { |
| | | pendingStopRequests.incrementAndGet(); |
| | | synchronized (startStopLock) |
| | | { |
| | | pendingStopRequests.decrementAndGet(); |
| | | if (shutdown) |
| | | { |
| | | return; |
| | | } |
| | | // A stop which overlaps one in progress keeps its deadlines and what it gave up. |