| | |
| | | TestCaseUtils.initializeTestBackend(true); |
| | | setConnectionTimeout(LONG_CONNECTION_TIMEOUT_MS); |
| | | final WithholdingReplicationServer rs = new WithholdingReplicationServer(); |
| | | final DSRSShutdownSync shutdownSync = shutdownSync(); |
| | | CSN previousAnnouncement = null; |
| | | Thread disabling = null; |
| | | try |
| | | { |
| | |
| | | // The only credit of the window goes to a change of our own. |
| | | assertEquals(replaceDescription("first"), ResultCode.SUCCESS); |
| | | rs.waitForUpdates(1); |
| | | final CSN firstChange = rs.receivedUpdateCSNs().get(0); |
| | | // The previous domain used the same clock tick and had a higher sequence number. |
| | | previousAnnouncement = new CSN(firstChange.getTime(), firstChange.getSeqnum() + 2, DS_ID); |
| | | final CompletableFuture<CSN> announcementAtFirstProbe = new CompletableFuture<>(); |
| | | rs.onFirstWindowProbe = () -> { |
| | | announcementAtFirstProbe.complete(offlineAnnouncementOfTheReplica()); |
| | | return null; |
| | | }; |
| | | shutdownSync.replicaOfflineMsgSent(baseDN, previousAnnouncement); |
| | | |
| | | final long beforeTheDisable = System.nanoTime(); |
| | | disabling = startThread("disable", domain::disable); |
| | | final CSN stopAnnouncement = announcementAtFirstProbe.get(10, SECONDS); |
| | | assertNotNull(stopAnnouncement, "the stop must announce going offline before waiting for credit"); |
| | | assertFalse(previousAnnouncement.equals(stopAnnouncement), "the stop must announce its own CSN"); |
| | | disabling.join(stopBoundMS()); |
| | | final long disableMS = NANOSECONDS.toMillis(System.nanoTime() - beforeTheDisable); |
| | | assertFalse(disabling.isAlive(), |
| | |
| | | assertEquals(rs.count(ReplicaOfflineMsg.class), 0, "the replica offline message was written with no credit"); |
| | | assertEquals(rs.count(StopMsg.class), 1, "the session was closed as a failed one, not ended in order"); |
| | | |
| | | /* The grace period of the announcement has run out by now, so whether the shutdown of a |
| | | * collocated replication server would wait for it says nothing: what is left announced does. |
| | | * An announcement older than the change is one of an earlier stop of the replica. */ |
| | | final CSN announced = offlineAnnouncementOfTheReplica(); |
| | | assertTrue(announced == null || !announced.isNewerThan(rs.receivedUpdateCSNs().get(0)), |
| | | "the replica offline message given up is still announced: " + announced); |
| | | final CSN remainingAnnouncement = offlineAnnouncementOfTheReplica(); |
| | | assertTrue(remainingAnnouncement == null || previousAnnouncement.equals(remainingAnnouncement), |
| | | "expected the stop to withdraw " + stopAnnouncement + ", but found " + remainingAnnouncement); |
| | | } |
| | | finally |
| | | { |
| | | try |
| | | { |
| | | cleanUp(rs, disabling); |
| | | } |
| | | finally |
| | | { |
| | | if (previousAnnouncement != null) |
| | | { |
| | | shutdownSync.replicaOfflineMsgNotSent(baseDN, previousAnnouncement); |
| | | } |
| | | setConnectionTimeout(SHORT_CONNECTION_TIMEOUT_MS); |
| | | } |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * With no connection timeout, a connection waits for its replication server for as long as it |
| | |
| | | } |
| | | } |
| | | |
| | | @Test(timeOut = 120_000) |
| | | public void failoverTriesAnotherDiscoveryResponderWhenTheFormerRsRefusesConnection() throws Exception |
| | | @DataProvider |
| | | public Object[][] discoveryResponderStartReplyTimeouts() |
| | | { |
| | | return new Object[][] { { 0 }, { 1 } }; |
| | | } |
| | | |
| | | @Test(dataProvider = "discoveryResponderStartReplyTimeouts", timeOut = 120_000) |
| | | public void failoverTriesAnotherDiscoveryResponderWhenTheFormerRsRefusesConnection(int timedOutReplies) |
| | | throws Exception |
| | | { |
| | | TestCaseUtils.initializeTestBackend(true); |
| | | final WithholdingReplicationServer former = new WithholdingReplicationServer(LARGE_WINDOW); |
| | | final WithholdingReplicationServer survivor = new WithholdingReplicationServer(LARGE_WINDOW, 4); |
| | | final AtomicInteger survivorStarts = new AtomicInteger(); |
| | | final CountDownLatch survivorRetry = new CountDownLatch(1); |
| | | final CountDownLatch finishRetry = new CountDownLatch(1); |
| | | final DelayedFailoverHandshake handshake = new DelayedFailoverHandshake(1, timedOutReplies); |
| | | try |
| | | { |
| | | survivor.differentGroup = true; |
| | |
| | | former.listenSocket.close(); |
| | | return null; |
| | | }; |
| | | survivor.beforeStartReply = () -> { |
| | | if (survivorStarts.incrementAndGet() == 2) |
| | | { |
| | | survivorRetry.countDown(); |
| | | await(finishRetry); |
| | | } |
| | | return null; |
| | | }; |
| | | survivor.delayedFailoverHandshake = handshake; |
| | | former.closeTheSession(); |
| | | |
| | | await(survivorRetry); |
| | | assertFalse(domain.hasConnectionError(), "the broker declared isolation before trying the discovery responder"); |
| | | finishRetry.countDown(); |
| | | await(handshake.nextStartReceived); |
| | | assertFalse(domain.hasConnectionError(), |
| | | "the broker declared isolation before connecting to the discovery responder"); |
| | | handshake.sendNextReply.countDown(); |
| | | newTimer().repeatUntilSuccess(() -> { |
| | | assertEquals(domain.getRsServerId(), survivor.serverId); |
| | | assertFalse(domain.hasConnectionError()); |
| | |
| | | assertEquals(replaceDescription("after failover to the discovery responder"), ResultCode.SUCCESS); |
| | | survivor.waitForUpdates(2); |
| | | assertEquals(survivor.receivedUpdateCSNs(), List.of(beforeFailover, domain.getServerState().getCSN(DS_ID))); |
| | | assertEquals(survivorStarts.get(), 2, "the survivor must receive one discovery probe and one session start"); |
| | | assertEquals(handshake.starts.get(), 2 + timedOutReplies, |
| | | "the survivor must receive one discovery probe and the expected session starts"); |
| | | } |
| | | finally |
| | | { |
| | | finishRetry.countDown(); |
| | | handshake.sendNextReply.countDown(); |
| | | try |
| | | { |
| | | survivor.shutdown(); |
| | |
| | | } |
| | | |
| | | @Test(timeOut = 120_000) |
| | | public void queuedStopSkipsTheDiscoveryResponderRetry() throws Exception |
| | | { |
| | | TestCaseUtils.initializeTestBackend(true); |
| | | setConnectionTimeout(LONG_CONNECTION_TIMEOUT_MS); |
| | | final WithholdingReplicationServer former = new WithholdingReplicationServer(LARGE_WINDOW); |
| | | final WithholdingReplicationServer responder = new WithholdingReplicationServer(LARGE_WINDOW, 4); |
| | | final DelayedFailoverHandshake handshake = new DelayedFailoverHandshake(1, 1); |
| | | Thread stopping = null; |
| | | try |
| | | { |
| | | responder.differentGroup = true; |
| | | responder.start(); |
| | | final LDAPReplicationDomain domain = startDomain(former, 100000, responder.getAddress()); |
| | | final ReplicationBroker broker = brokerOf(domain); |
| | | assertEquals(domain.getRsServerId(), former.serverId); |
| | | assertEquals(replaceDescription("elect the former replication server at failover"), ResultCode.SUCCESS); |
| | | former.waitForUpdates(1); |
| | | |
| | | responder.differentGroup = false; |
| | | former.beforeStartReply = () -> { |
| | | former.listenSocket.close(); |
| | | return null; |
| | | }; |
| | | responder.delayedFailoverHandshake = handshake; |
| | | handshake.sendNextReply.countDown(); |
| | | former.closeTheSession(); |
| | | |
| | | newTimer().repeatUntilSuccess(() -> assertEquals(handshake.starts.get(), 2, |
| | | "the responder must be withholding its first session start reply")); |
| | | final Thread stop = startThread("stop during the responder's start handshake", broker::stop); |
| | | stopping = stop; |
| | | newTimer().repeatUntilSuccess(() -> assertTrue( |
| | | stop.getState() == Thread.State.BLOCKED && isIn(stop, ReplicationBroker.class, "stop"), |
| | | "stop must be waiting for the reconnect to release its lock")); |
| | | stop.join(stopBoundMS()); |
| | | assertFalse(stop.isAlive(), "stop did not finish after the first start reply timed out"); |
| | | assertEquals(handshake.starts.get(), 2, "the broker retried the responder with a stop queued"); |
| | | } |
| | | finally |
| | | { |
| | | try |
| | | { |
| | | responder.shutdown(); |
| | | } |
| | | finally |
| | | { |
| | | cleanUp(former, stopping); |
| | | setConnectionTimeout(SHORT_CONNECTION_TIMEOUT_MS); |
| | | } |
| | | } |
| | | } |
| | | |
| | | @Test(timeOut = 120_000) |
| | | public void queuedStopSkipsTheFallbackAfterContinuedDiscovery() throws Exception |
| | | { |
| | | TestCaseUtils.initializeTestBackend(true); |
| | | final WithholdingReplicationServer former = new WithholdingReplicationServer(LARGE_WINDOW); |
| | | final WithholdingReplicationServer responder = new WithholdingReplicationServer(LARGE_WINDOW, 4); |
| | | final DelayedFailoverHandshake handshake = new DelayedFailoverHandshake(0, 0); |
| | | Thread stopping = null; |
| | | try |
| | | { |
| | | setConnectionTimeout(LONG_CONNECTION_TIMEOUT_MS); |
| | | responder.differentGroup = true; |
| | | responder.start(); |
| | | final LDAPReplicationDomain domain = startDomain(former, 100000, responder.getAddress()); |
| | | final ReplicationBroker broker = brokerOf(domain); |
| | | assertEquals(domain.getRsServerId(), former.serverId); |
| | | newTimer().repeatUntilSuccess(() -> assertNotNull(former.session.get())); |
| | | |
| | | responder.differentGroup = false; |
| | | responder.delayedFailoverHandshake = handshake; |
| | | former.beforeStartReply = () -> { |
| | | former.listenSocket.close(); |
| | | return null; |
| | | }; |
| | | former.dropTheSession(); |
| | | |
| | | await(handshake.nextStartReceived); |
| | | newTimer().repeatUntilSuccess(() -> assertTrue(Thread.getAllStackTraces().keySet().stream() |
| | | .anyMatch(thread -> waitsIn(thread, ReplicationBroker.class, "retryElectedConnection")), |
| | | "the broker must be waiting for the responder in continued discovery")); |
| | | final Thread stop = startThread("stop during continued discovery", broker::stop); |
| | | stopping = stop; |
| | | newTimer().repeatUntilSuccess(() -> assertTrue( |
| | | stop.getState() == Thread.State.BLOCKED && isIn(stop, ReplicationBroker.class, "stop"), |
| | | "stop must be waiting for the reconnect to release its lock")); |
| | | |
| | | handshake.sendNextReply.countDown(); |
| | | stop.join(stopBoundMS()); |
| | | assertFalse(stop.isAlive(), "stop did not finish after the discovery reply"); |
| | | assertEquals(handshake.starts.get(), 1, "the responder must receive only the discovery probe"); |
| | | } |
| | | finally |
| | | { |
| | | handshake.sendNextReply.countDown(); |
| | | try |
| | | { |
| | | responder.shutdown(); |
| | | } |
| | | finally |
| | | { |
| | | try |
| | | { |
| | | cleanUp(former, stopping); |
| | | } |
| | | finally |
| | | { |
| | | setConnectionTimeout(SHORT_CONNECTION_TIMEOUT_MS); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | @Test(timeOut = 120_000) |
| | | public void reconnectClosesTheSessionWhenTopologyTimesOut() throws Exception |
| | | { |
| | | TestCaseUtils.initializeTestBackend(true); |
| | |
| | | return false; |
| | | } |
| | | |
| | | private static DSRSShutdownSync shutdownSync() throws Exception |
| | | { |
| | | final Field shutdownSync = MultimasterReplication.class.getDeclaredField("dsrsShutdownSync"); |
| | | shutdownSync.setAccessible(true); |
| | | return (DSRSShutdownSync) shutdownSync.get(null); |
| | | } |
| | | |
| | | /** |
| | | * Returns the CSN of the announcement that the replica of the domain went offline which the |
| | | * shutdown of a collocated replication server would wait for the forward of, if there is one. |
| | | */ |
| | | private CSN offlineAnnouncementOfTheReplica() throws Exception |
| | | { |
| | | final Field shutdownSync = MultimasterReplication.class.getDeclaredField("dsrsShutdownSync"); |
| | | shutdownSync.setAccessible(true); |
| | | final Field announcements = DSRSShutdownSync.class.getDeclaredField("replicaOfflineMsgs"); |
| | | announcements.setAccessible(true); |
| | | final Map<?, ?> byDomain = (Map<?, ?>) announcements.get(shutdownSync.get(null)); |
| | | final Map<?, ?> byDomain = (Map<?, ?>) announcements.get(shutdownSync()); |
| | | final Map<?, ?> byReplica = (Map<?, ?>) byDomain.get(baseDN); |
| | | final Object announcement = byReplica != null ? byReplica.get(DS_ID) : null; |
| | | if (announcement == null) |
| | |
| | | /** Whether this server is away: it then sends no heartbeat, and answers no connection either. */ |
| | | private volatile boolean away; |
| | | private volatile Callable<Void> beforeStartReply; |
| | | private volatile Callable<Void> onFirstWindowProbe; |
| | | /** If set, read from the next session after StartSessionMsg instead of sending its topology. */ |
| | | private final AtomicReference<CompletableFuture<ReplicationMsg>> readInsteadOfSendingTopology = |
| | | new AtomicReference<>(); |
| | |
| | | { |
| | | lastOffline = (ReplicaOfflineMsg) msg; |
| | | } |
| | | else if (msg instanceof WindowProbeMsg) |
| | | { |
| | | final Callable<Void> onProbe = onFirstWindowProbe; |
| | | if (onProbe != null) |
| | | { |
| | | onFirstWindowProbe = null; |
| | | onProbe.call(); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | catch (Exception e) |