From d10be9455ccee71072843da100f9102d410a4406 Mon Sep 17 00:00:00 2001
From: Valery Kharseko <vharseko@3a-systems.ru>
Date: Tue, 01 Dec 2026 11:05:46 +0000
Subject: [PATCH] Prevent duplicate changelog cursors during replica publication (#1352)
---
opendj-server-legacy/src/main/java/org/opends/server/replication/server/changelog/file/FileChangelogDB.java | 45 +++++++++++-----------
opendj-server-legacy/src/test/java/org/opends/server/replication/server/changelog/file/FileChangelogDBTest.java | 70 +++++++++++++++++++++++++++++++++++
2 files changed, 92 insertions(+), 23 deletions(-)
diff --git a/opendj-server-legacy/src/main/java/org/opends/server/replication/server/changelog/file/FileChangelogDB.java b/opendj-server-legacy/src/main/java/org/opends/server/replication/server/changelog/file/FileChangelogDB.java
index ca8e5ca..11111c4 100644
--- a/opendj-server-legacy/src/main/java/org/opends/server/replication/server/changelog/file/FileChangelogDB.java
+++ b/opendj-server-legacy/src/main/java/org/opends/server/replication/server/changelog/file/FileChangelogDB.java
@@ -199,19 +199,6 @@
final Pair<FileReplicaDB, Boolean> result = getExistingOrNewReplicaDB(domainMap, serverId, baseDN, server);
if (result != null)
{
- final Boolean dbWasCreated = result.getSecond();
- if (dbWasCreated)
- { // new replicaDB => update all cursors with it
- final List<DomainDBCursor> cursors = registeredDomainCursors.get(baseDN);
- if (cursors != null && !cursors.isEmpty())
- {
- for (DomainDBCursor cursor : cursors)
- {
- cursor.addReplicaDB(serverId, null);
- }
- }
- }
-
return result;
}
}
@@ -305,7 +292,17 @@
}
final FileReplicaDB newDB = newReplicaDB(serverId, baseDN, server, cryptoSuite, replicationEnv);
- domainMap.put(serverId, newDB);
+ final List<DomainDBCursor> cursors = domainCursors(baseDN);
+ synchronized (cursors)
+ {
+ // A reader must either discover this replica in its initial scan or receive the
+ // creation announcement. Doing both can open two cursors over the same log.
+ domainMap.put(serverId, newDB);
+ for (DomainDBCursor cursor : cursors)
+ {
+ cursor.addReplicaDB(serverId, null);
+ }
+ }
return Pair.of(newDB, true);
}
finally
@@ -823,21 +820,23 @@
public DBCursor<UpdateMsg> getCursorFrom(final DN baseDN, final ServerState startState, CursorOptions options)
throws ChangelogException
{
- final DomainDBCursor cursor = newDomainDBCursor(baseDN, options);
- for (int serverId : getDomainMap(baseDN).keySet())
+ final DomainDBCursor cursor = new DomainDBCursor(baseDN, this, options);
+ final List<DomainDBCursor> cursors = domainCursors(baseDN);
+ synchronized (cursors)
{
- // get the last already sent CSN from that server to get a cursor
- final CSN lastCSN = startState != null ? startState.getCSN(serverId) : null;
- cursor.addReplicaDB(serverId, lastCSN);
+ for (int serverId : getDomainMap(baseDN).keySet())
+ {
+ final CSN lastCSN = startState != null ? startState.getCSN(serverId) : null;
+ cursor.addReplicaDB(serverId, lastCSN);
+ }
+ cursors.add(cursor);
}
return cursor;
}
- private DomainDBCursor newDomainDBCursor(final DN baseDN, final CursorOptions options)
+ private List<DomainDBCursor> domainCursors(final DN baseDN)
{
- final DomainDBCursor cursor = new DomainDBCursor(baseDN, this, options);
- putCursor(registeredDomainCursors, baseDN, cursor);
- return cursor;
+ return registeredDomainCursors.computeIfAbsent(baseDN, dn -> new CopyOnWriteArrayList<>());
}
private CSN getOfflineCSN(DN baseDN, int serverId, CSN startAfterCSN)
diff --git a/opendj-server-legacy/src/test/java/org/opends/server/replication/server/changelog/file/FileChangelogDBTest.java b/opendj-server-legacy/src/test/java/org/opends/server/replication/server/changelog/file/FileChangelogDBTest.java
index 31bfa61..df17e0a 100644
--- a/opendj-server-legacy/src/test/java/org/opends/server/replication/server/changelog/file/FileChangelogDBTest.java
+++ b/opendj-server-legacy/src/test/java/org/opends/server/replication/server/changelog/file/FileChangelogDBTest.java
@@ -25,6 +25,7 @@
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
@@ -553,6 +554,75 @@
}
}
+ @Test
+ public void cursorOpenedDuringReplicaPublicationReadsEachChangeOnce() throws Exception
+ {
+ TestCaseUtils.startServer();
+ final ReplicationServer server = configureReplicationServer(100, 5000);
+ final FileChangelogDB db = (FileChangelogDB) server.getChangelogDB();
+ final DN dn = DN.valueOf("dc=cursor-publication");
+ final CSN csn = new CSN(5000, 1, SERVER_ID);
+ final AtomicReference<DBCursor<UpdateMsg>> opened = new AtomicReference<>();
+ final FutureTask<Void> firstRead = new FutureTask<>(() ->
+ {
+ final DBCursor<UpdateMsg> cursor = db.getCursorFrom(dn, new ServerState(),
+ new CursorOptions(GREATER_THAN_OR_EQUAL_TO_KEY, AFTER_MATCHING_KEY));
+ opened.set(cursor);
+ assertTrue(cursor.next());
+ assertEquals(cursor.getRecord().getCSN(), csn);
+ return null;
+ });
+ final Thread reader = new Thread(firstRead, "cursor opened during replica publication");
+ try
+ {
+ db.getDomainToReplicaDBs().put(dn, new ConcurrentHashMap<Integer, FileReplicaDB>()
+ {
+ @Override
+ public FileReplicaDB put(Integer serverId, FileReplicaDB replica)
+ {
+ final FileReplicaDB previous = super.put(serverId, replica);
+ try
+ {
+ replica.add(new DeleteMsg(dn, csn, "uid"));
+ reader.start();
+ // Let the reader consume the change before the creator can announce it, or let
+ // the creator continue once the reader is waiting for its publication lock.
+ final ThreadMXBean threads = ManagementFactory.getThreadMXBean();
+ final long deadline = System.nanoTime() + TimeUnit.MILLISECONDS.toNanos(TIMEOUT_MS);
+ while (!firstRead.isDone())
+ {
+ final ThreadInfo info = threads.getThreadInfo(reader.getId());
+ if (info != null && info.getThreadState() == Thread.State.BLOCKED
+ && info.getLockOwnerId() == Thread.currentThread().getId())
+ {
+ break;
+ }
+ assertTrue(System.nanoTime() < deadline, "Reader did not reach replica publication");
+ Thread.sleep(1);
+ }
+ }
+ catch (Exception e)
+ {
+ throw new AssertionError(e);
+ }
+ return previous;
+ }
+ });
+ db.getOrCreateReplicaDB(dn, SERVER_ID, server);
+ firstRead.get(TIMEOUT_MS, TimeUnit.MILLISECONDS);
+ assertFalse(opened.get().next(), "The creation announcement replayed the change already read");
+ }
+ finally
+ {
+ join(reader);
+ if (opened.get() != null)
+ {
+ opened.get().close();
+ }
+ remove(server);
+ }
+ }
+
/**
* A creation which bails out on the identity check must not drop the domain map another creation
* has freshly inserted: the drop is equality based and two empty maps are equal, so an identity
--
Gitblit v1.10.0