| | |
| | | import static java.util.concurrent.TimeUnit.*; |
| | | |
| | | import static org.forgerock.opendj.ldap.ModificationType.*; |
| | | import static org.opends.messages.ReplicationMessages.WARN_HEARTBEAT_FAILURE; |
| | | import static org.opends.messages.ReplicationMessages.WARN_REPLICATION_SERVER_BADLY_DISCONNECTED; |
| | | import static org.opends.messages.ReplicationMessages.WARN_TIMEOUT_CONNECTING_TO_RS; |
| | | import static org.opends.server.TestCaseUtils.*; |
| | |
| | | import java.util.concurrent.atomic.AtomicBoolean; |
| | | import java.util.concurrent.atomic.AtomicInteger; |
| | | import java.util.concurrent.atomic.AtomicReference; |
| | | import java.util.function.BooleanSupplier; |
| | | |
| | | import org.forgerock.opendj.ldap.DN; |
| | | import org.forgerock.opendj.ldap.ResultCode; |
| | |
| | | private static final int LARGE_MESSAGE_SIZE = 256 * 1024; |
| | | /** How many of those messages a case writes at most before it reports that no write blocks. */ |
| | | private static final int MAX_LARGE_MESSAGES = 400; |
| | | /** |
| | | * The heartbeat interval of the case of a replication server which is gone: long enough for its |
| | | * writes to fill the socket buffers before the heartbeat monitor gives up on the session. |
| | | */ |
| | | /** Heartbeat interval negotiated by the domain in the lost-server test. */ |
| | | private static final long HEARTBEAT_INTERVAL_MS = 4000; |
| | | /** A value which makes a change of the description one of those messages. */ |
| | | private static final String LARGE_VALUE = "x".repeat(LARGE_MESSAGE_SIZE); |
| | |
| | | final LDAPReplicationDomain domain = startDomain(rs); |
| | | TestCaseUtils.ERROR_TEXT_WRITER.clear(); |
| | | rs.stopReading(); |
| | | writer = startLargeChanges(keepWriting, changes, failed); |
| | | writer = startLargeChanges(keepWriting::get, changes, failed); |
| | | waitForAWriteToBlock(writer, changes); |
| | | keepWriting.set(false); |
| | | |
| | |
| | | { |
| | | TestCaseUtils.initializeTestBackend(true); |
| | | final WithholdingReplicationServer rs = new WithholdingReplicationServer(LARGE_WINDOW); |
| | | final AtomicBoolean keepWriting = new AtomicBoolean(true); |
| | | final AtomicInteger changes = new AtomicInteger(); |
| | | final AtomicReference<ResultCode> failed = new AtomicReference<>(); |
| | | Thread writer = null; |
| | | try |
| | | { |
| | | final LDAPReplicationDomain domain = startDomain(rs, HEARTBEAT_INTERVAL_MS); |
| | | final ReplicationBroker broker = brokerOf(domain); |
| | | final Session original = broker.getSession(); |
| | | // Give the replication server a CSN from which it can request recovery on reconnect. |
| | | assertEquals(replaceDescription("before the heartbeat timeout"), ResultCode.SUCCESS); |
| | | rs.waitForUpdates(1); |
| | | TestCaseUtils.ERROR_TEXT_WRITER.clear(); |
| | | rs.goAway(); |
| | | final long away = System.nanoTime(); |
| | | writer = startLargeChanges(keepWriting, changes, failed); |
| | | rs.stopReading(); |
| | | // A stalled write may still fit in the TCP buffers later. Keep writing until this session closes. |
| | | writer = startLargeChanges(() -> !original.closeInitiated(), changes, failed); |
| | | waitForAWriteToBlock(writer, changes); |
| | | keepWriting.set(false); |
| | | final long blocked = System.nanoTime(); |
| | | |
| | | // The monitor gives up two or three intervals after the last heartbeat. |
| | | rs.giveCredit(LARGE_WINDOW); |
| | | newTimer().repeatUntilSuccess(() -> assertTrue(broker.getCurrentSendWindow() > LARGE_WINDOW, |
| | | "the directory server did not receive the credit on the original session")); |
| | | rs.goAway(); |
| | | writer.join(4 * HEARTBEAT_INTERVAL_MS + 10000); |
| | | final long released = System.nanoTime(); |
| | | assertFalse(writer.isAlive(), "the write still blocks once the heartbeats of the replication server stopped"); |
| | | assertTrue(NANOSECONDS.toMillis(released - away) >= 2 * HEARTBEAT_INTERVAL_MS, "the write of change " |
| | | + changes.get() + ", blocked " + NANOSECONDS.toMillis(blocked - away) + " ms after the replication server went away, ended " |
| | | + NANOSECONDS.toMillis(released - away) + " ms after it: before the heartbeat monitor could give the session up"); |
| | | assertTrue(original.closeInitiated(), "the writer exhausted its changes while the original session was open"); |
| | | assertNull(failed.get(), "a local change failed"); |
| | | final String timeout = WARN_HEARTBEAT_FAILURE.get(DS_ID, rs.serverId, |
| | | original.getReadableRemoteAddress(), baseDN.toString()).toString(); |
| | | newTimer().repeatUntilSuccess(() -> assertTrue(TestCaseUtils.ERROR_TEXT_WRITER.getMessages().stream() |
| | | .anyMatch(record -> record.contains(timeout)), "missing heartbeat timeout: " + timeout)); |
| | | |
| | | rs.resumeReading(); |
| | | waitForConnection(domain); |
| | | // Discard any bytes queued on the old socket. Only the reconnected session can deliver the change. |
| | | final CSN last = domain.getServerState().getCSN(DS_ID); |
| | | assertNotNull(last, "the domain has no change of its own in its state"); |
| | | assertFalse(rs.receivedUpdateCSNs().contains(last), "the old session already delivered the blocked change"); |
| | | rs.dropTheSession(); |
| | | rs.resumeReading(); |
| | | waitForConnection(domain); |
| | | newTimer().repeatUntilSuccess(() -> assertTrue(rs.receivedUpdateCSNs().contains(last), |
| | | "the change cut short, " + last + ", did not reach the replication server: it has " |
| | | + rs.receivedUpdateCSNs())); |
| | |
| | | final CSN beforeFailover = former.receivedUpdateCSNs().get(1); |
| | | |
| | | survivor.differentGroup = false; |
| | | former.beforeStartReply = () -> { |
| | | // The accepted socket can answer discovery after the listener stops accepting connections. |
| | | former.listenSocket.close(); |
| | | return null; |
| | | }; |
| | | former.closeListenerAfterNextAccept = true; |
| | | survivor.delayedFailoverHandshake = handshake; |
| | | former.closeTheSession(); |
| | | |
| | |
| | | former.waitForUpdates(1); |
| | | |
| | | responder.differentGroup = false; |
| | | former.beforeStartReply = () -> { |
| | | former.listenSocket.close(); |
| | | return null; |
| | | }; |
| | | former.closeListenerAfterNextAccept = true; |
| | | responder.delayedFailoverHandshake = handshake; |
| | | handshake.sendNextReply.countDown(); |
| | | former.closeTheSession(); |
| | |
| | | |
| | | responder.differentGroup = false; |
| | | responder.delayedFailoverHandshake = handshake; |
| | | former.beforeStartReply = () -> { |
| | | former.listenSocket.close(); |
| | | return null; |
| | | }; |
| | | former.closeListenerAfterNextAccept = true; |
| | | former.dropTheSession(); |
| | | |
| | | await(handshake.nextStartReceived); |
| | |
| | | final LDAPReplicationDomain domain = startDomain(oldRS, 100000, nextRS.getAddress()); |
| | | assertEquals(domain.getRsServerId(), oldRS.serverId); |
| | | nextRS.differentGroup = false; |
| | | // Allow TLS to finish before exercising the broker's shorter start-reply timeout. |
| | | nextRS.tlsHandshakeTimeoutMS = 8_000; |
| | | nextRS.delayedFailoverHandshake = handshake; |
| | | if (formerRs == FormerRsDuringFailover.OFFLINE) |
| | | { |
| | |
| | | nextRS.waitForUpdates(1); |
| | | } |
| | | } |
| | | catch (AssertionError e) |
| | | { |
| | | final IOException tlsFailure = nextRS.firstTlsHandshakeFailure.get(); |
| | | if (tlsFailure != null) |
| | | { |
| | | e.addSuppressed(tlsFailure); |
| | | } |
| | | throw e; |
| | | } |
| | | finally |
| | | { |
| | | handshake.sendNextReply.countDown(); |
| | |
| | | * Starts a thread which makes changes of {@link #LARGE_MESSAGE_SIZE} until it is told to stop, |
| | | * counting them, and keeping the first result which is no success. |
| | | */ |
| | | private Thread startLargeChanges(final AtomicBoolean keepWriting, final AtomicInteger changes, |
| | | private Thread startLargeChanges(final BooleanSupplier keepWriting, final AtomicInteger changes, |
| | | final AtomicReference<ResultCode> failed) |
| | | { |
| | | return startThread("writer", () -> { |
| | | while (keepWriting.get() && changes.get() < MAX_LARGE_MESSAGES) |
| | | while (keepWriting.getAsBoolean() && changes.get() < MAX_LARGE_MESSAGES) |
| | | { |
| | | final ResultCode result = replaceDescription(changes.get() + LARGE_VALUE); |
| | | if (result != ResultCode.SUCCESS) |
| | |
| | | private boolean reading = true; |
| | | /** Whether this server is away: it then sends no heartbeat, and answers no connection either. */ |
| | | private volatile boolean away; |
| | | /** Accept the discovery socket, then refuse the session connection. */ |
| | | private volatile boolean closeListenerAfterNextAccept; |
| | | 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<>(); |
| | | private volatile DelayedFailoverHandshake delayedFailoverHandshake; |
| | | /** A positive timeout applies only to TLS negotiation on this server. */ |
| | | private volatile int tlsHandshakeTimeoutMS; |
| | | private final AtomicReference<IOException> firstTlsHandshakeFailure = new AtomicReference<>(); |
| | | |
| | | WithholdingReplicationServer() throws IOException |
| | | { |
| | |
| | | try |
| | | { |
| | | socket = listenSocket.accept(); |
| | | if (closeListenerAfterNextAccept) |
| | | { |
| | | listenSocket.close(); |
| | | } |
| | | } |
| | | catch (IOException e) |
| | | { |
| | |
| | | private void serve(Socket socket) |
| | | { |
| | | Session s = null; |
| | | int tlsTimeoutMS = 0; |
| | | try |
| | | { |
| | | // A server which is away answers no connection: the connection waits for it to come back. |
| | | waitToBeBack(); |
| | | s = new ReplSessionSecurity().createServerSession(socket, MultimasterReplication.getConnectionTimeoutMS()); |
| | | final int readTimeoutMS = MultimasterReplication.getConnectionTimeoutMS(); |
| | | tlsTimeoutMS = tlsHandshakeTimeoutMS; |
| | | s = new ReplSessionSecurity().createServerSessionOrThrow(socket, |
| | | tlsTimeoutMS > 0 ? tlsTimeoutMS : readTimeoutMS); |
| | | sessions.add(s); |
| | | if (tlsTimeoutMS > 0) |
| | | { |
| | | s.setSoTimeout(readTimeoutMS); |
| | | } |
| | | final ServerStartMsg start = (ServerStartMsg) s.receive(); |
| | | final Callable<Void> hook = beforeStartReply; |
| | | if (hook != null) |
| | |
| | | } |
| | | catch (Exception e) |
| | | { |
| | | // The domain closed the session, or the test closed this server. |
| | | if (s == null && tlsTimeoutMS > 0) |
| | | { |
| | | firstTlsHandshakeFailure.compareAndSet(null, new IOException("TLS handshake on replication server " |
| | | + getAddress() + " with peer " + socket.getRemoteSocketAddress(), e)); |
| | | } |
| | | // An established session may end when the domain or the test closes it. |
| | | } |
| | | finally |
| | | { |