| | |
| | | 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; |
| | |
| | | } |
| | | } |
| | | |
| | | @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 |