mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Valery Kharseko
49 mins ago 70f1d20362f4a89f425ad1bfa51ae7be5f9e3eb1
Fix assured replication tests with interleaved topology messages (#1344)

## Summary

- Let the fake replication server read past topology messages and the
expected generation ID warning while waiting for an ACK.
- Match the ACK to the update by CSN and preserve the five-second read
budget in cases that expect no ACK.
- Send a topology message before the update in every
`testSafeDataFromRS` case to exercise both the ACK and timeout paths
deterministically.
- Wait for SAFE_READ monitoring to record the ACK outcome before
checking counters in `testSafeReadOneRSComplex` and
`testSafeReadModeReply`.

## Verification

- Before the reader fix, all 24 cases reproduced the
`ClassCastException` seen in [Ubuntu / Java 17
CI](https://github.com/OpenIdentityPlatform/OpenDJ/actions/runs/46069438883/job/140742690311).
- On macOS with Java 11, all 360 `AssuredReplicationServerTest` cases
and all 14 `AssuredReplicationPluginTest` cases passed. This includes
all 24 `testSafeDataFromRS` cases.
2 files modified
74 ■■■■■ changed files
opendj-server-legacy/src/test/java/org/opends/server/replication/plugin/AssuredReplicationPluginTest.java 2 ●●●●● patch | view | raw | blame | history
opendj-server-legacy/src/test/java/org/opends/server/replication/server/AssuredReplicationServerTest.java 72 ●●●●● patch | view | raw | blame | history
opendj-server-legacy/src/test/java/org/opends/server/replication/plugin/AssuredReplicationPluginTest.java
@@ -1169,6 +1169,8 @@
      // Check for monitoring data
      DN baseDN = DN.valueOf(SAFE_READ_DN);
      assertMonitorAttrValueEventually(baseDN, "assured-sr-received-updates-acked", 1,
          "The DS should record the ACK it sent");
      new MonitorAssertions(baseDN)
        .assertValue("assured-sr-received-updates", 1)
        .assertValue("assured-sr-received-updates-acked", 1)
opendj-server-legacy/src/test/java/org/opends/server/replication/server/AssuredReplicationServerTest.java
@@ -40,6 +40,7 @@
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.assertj.core.api.SoftAssertions;
@@ -71,6 +72,7 @@
import org.opends.server.replication.protocol.UpdateMsg;
import org.opends.server.replication.service.ReplicationDomain;
import org.opends.server.types.DirectoryException;
import org.opends.server.util.TestTimer;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
@@ -793,35 +795,45 @@
    }
    /**
     * Make the RS send an assured message and return the ack message it
     * receives from the RS.
     * Sends an update and waits for its acknowledgement or for the read deadline to expire.
     */
    public AckMsg sendNewFakeUpdate(boolean expectAck) throws Exception
    {
        if (!expectAck)
        {
          session.setSoTimeout(NO_ACK_READ_TIMEOUT);
        }
        // Create a new delete update message (the simplest to create)
        DeleteMsg delMsg = new DeleteMsg(baseDN, gen.newCSN(),
        UUID.randomUUID().toString());
        // Send del message in assured mode
      DeleteMsg delMsg = new DeleteMsg(baseDN, gen.newCSN(), UUID.randomUUID().toString());
        delMsg.setAssured(isAssured);
        delMsg.setAssuredMode(assuredMode);
        delMsg.setSafeDataLevel(safeDataLevel);
        session.publish(delMsg);
        // Read and return matching ack
        ReplicationMsg replMsg = session.receive();
        if (replMsg instanceof ErrorMsg)
      int timeoutMillis = expectAck ? MultimasterReplication.getConnectionTimeoutMS() : NO_ACK_READ_TIMEOUT;
      long deadline = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(timeoutMillis);
      boolean allowGenerationIdError = generationId != DEFAULT_GENID;
      while (true)
        {
        // Support for connection done with bad gen id : we receive an error
          // message that we must throw away before reading our ack.
          replMsg = session.receive();
        long remainingNanos = deadline - System.nanoTime();
        if (remainingNanos <= 0)
        {
          throw new SocketTimeoutException("Timed out waiting for ACK of update " + delMsg.getCSN());
        }
        return (AckMsg)replMsg;
        // Round up because a socket timeout of zero would allow an indefinite read.
        session.setSoTimeout((int) (TimeUnit.NANOSECONDS.toMillis(remainingNanos - 1) + 1));
        ReplicationMsg reply = session.receive();
        if (reply instanceof TopologyMsg)
        {
          continue;
        }
        if (allowGenerationIdError && reply instanceof ErrorMsg
            && ReplicationServerDomain.isGenerationIdWarningMsgID(((ErrorMsg) reply).getMsgID()))
        {
          allowGenerationIdError = false;
          continue;
        }
        assertThat(reply).as("response to update %s", delMsg.getCSN()).isInstanceOf(AckMsg.class);
        AckMsg ack = (AckMsg) reply;
        assertThat(ack.getCSN()).as("acknowledged CSN").isEqualTo(delMsg.getCSN());
        return ack;
      }
    }
    /**
@@ -2209,6 +2221,18 @@
      fakeRs1 = createFakeReplicationServer(FRS1_ID, fakeRsGid, fakeRsGenId,
        sendInAssured, AssuredMode.SAFE_DATA_MODE, sdLevel, SENDER_RS_SCENARIO);
      // Queue a topology notification before the update, so it is read before any ACK.
      ReplicationServerDomain domain = rs1.getReplicationServerDomain(DN.valueOf(TEST_ROOT_DN_STRING));
      ReplicationServerHandler sender = new TestTimer.Builder()
          .maxSleep(MultimasterReplication.getConnectionTimeoutMS(), TimeUnit.MILLISECONDS)
          .sleepTimes(10, TimeUnit.MILLISECONDS)
          .toTimer().repeatUntilSuccess(() -> {
            ReplicationServerHandler handler = domain.getConnectedRSs().get(FRS1_ID);
            assertNotNull(handler, "The fake sender has not registered with the replication server");
            return handler;
          });
      sender.sendTopoInfo(domain.createTopologyMsgForRS());
      /*
       * Send an assured update using configured assured parameters
       */
@@ -2719,8 +2743,6 @@
        assertBetweenInclusive(sendUpdateTime, SMALL_TIMEOUT, LONG_TIMEOUT);
      }
      sleepWhileUpdatePropagates(500);
      // Check monitoring values in DS 1
      final SafeReadAssertions srAssertsRD1 = fakeRd1.newSafeReadAssertions().sentUpdates(1);
      if ((otherFakeDsGid == DEFAULT_GID && otherFakeDsGenId == DEFAULT_GENID
@@ -2739,7 +2761,13 @@
               .wrongStatusUpdates(shouldSeeWrongStatus ? 1 : 0)
               .replayErrorUpdates(shouldSeeReplayError ? 1 : 0);
      addExpectedErrors(srAssertsRD1, shouldSeeDsIdInError, shouldSeeRsIdInError, shouldSeeDsRsIdInError);
      srAssertsRD1.runAsserts();
      // The ACK can unblock the sender before its monitoring counters are updated.
      new TestTimer.Builder()
          .maxSleep(5, TimeUnit.SECONDS)
          .sleepTimes(50, TimeUnit.MILLISECONDS)
          .toTimer().repeatUntilSuccess(srAssertsRD1::runAsserts);
      sleepWhileUpdatePropagates(500);
      // Check monitoring values in DS 2
      checkDSReceivedAndAcked(fakeRDs[2], 1);