From 1124cfee16d494055333c206aec6ce41c7d0526a Mon Sep 17 00:00:00 2001 From: Yujin Ahn Date: Tue, 11 Feb 2025 00:43:16 +0900 Subject: [PATCH] Implement translog isLockAvailable Signed-off-by: Yujin Ahn --- .../org/opensearch/index/translog/RemoteFsTranslog.java | 8 ++++---- .../translog/RemoteFsTimestampAwareTranslogTests.java | 4 ++-- .../opensearch/index/translog/RemoteFsTranslogTests.java | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/server/src/main/java/org/opensearch/index/translog/RemoteFsTranslog.java b/server/src/main/java/org/opensearch/index/translog/RemoteFsTranslog.java index 017009cd9d25f..4157e9cf2725f 100644 --- a/server/src/main/java/org/opensearch/index/translog/RemoteFsTranslog.java +++ b/server/src/main/java/org/opensearch/index/translog/RemoteFsTranslog.java @@ -91,7 +91,7 @@ public class RemoteFsTranslog extends Translog { // Semaphore used to allow only single remote generation to happen at a time protected final Semaphore remoteGenerationDeletionPermits = new Semaphore(REMOTE_DELETION_PERMITS); - // These permits exist to allow any inflight background triggered upload. + // this lock exists to allow any inflight background triggered upload. private static final ReentrantLock reentrantLock = new ReentrantLock(); private static final ReleasableLock syncPermit = new ReleasableLock(reentrantLock); protected final AtomicBoolean pauseSync = new AtomicBoolean(false); @@ -377,7 +377,7 @@ private boolean prepareAndUpload(Long primaryTerm, Long generation) throws IOExc // 1. Using startedPrimarySupplier, we prevent the new primary to do pre-emptive syncs // 2. Using syncPermit, we prevent syncs at the desired time during primary relocation. if (startedPrimarySupplier.getAsBoolean() == false || syncPermit.isHeldByCurrentThread() || syncPermit.tryAcquire() == null) { - logger.debug("skipped uploading translog for {} {} syncPermit={}", primaryTerm, generation, availablePermits()); + logger.debug("skipped uploading translog for {} {} isLockAvailable={}", primaryTerm, generation, isLockAvailable()); // NO-OP return false; } @@ -748,8 +748,8 @@ public long getMinUnreferencedSeqNoInSegments(long minUnrefCheckpointInLastCommi } // Visible for testing - int availablePermits() { - return reentrantLock.isLocked() ? 0 : 1; + boolean isLockAvailable() { + return !reentrantLock.isLocked(); } /** diff --git a/server/src/test/java/org/opensearch/index/translog/RemoteFsTimestampAwareTranslogTests.java b/server/src/test/java/org/opensearch/index/translog/RemoteFsTimestampAwareTranslogTests.java index 78ae90936d78e..23299f76e1d82 100644 --- a/server/src/test/java/org/opensearch/index/translog/RemoteFsTimestampAwareTranslogTests.java +++ b/server/src/test/java/org/opensearch/index/translog/RemoteFsTimestampAwareTranslogTests.java @@ -544,11 +544,11 @@ public void testDrainSync() throws Exception { } }); thread1.start(); - assertBusy(() -> assertEquals(0, translog.availablePermits())); + assertBusy(() -> assertFalse(translog.isLockAvailable())); // Case 2 - During an upload, if drainSync is called, it will wait for it to acquire and available permits are 0. Releasable releasable = translog.drainSync(); assertBusy(() -> assertEquals(0, latch.getCount())); - assertEquals(0, translog.availablePermits()); + assertFalse(translog.isLockAvailable()); slowDown.setSleepSeconds(0); assertEquals(4, translog.allUploaded().size()); assertEquals(2, translog.readers.size()); diff --git a/server/src/test/java/org/opensearch/index/translog/RemoteFsTranslogTests.java b/server/src/test/java/org/opensearch/index/translog/RemoteFsTranslogTests.java index 190af714d5764..aa6f743bd79af 100644 --- a/server/src/test/java/org/opensearch/index/translog/RemoteFsTranslogTests.java +++ b/server/src/test/java/org/opensearch/index/translog/RemoteFsTranslogTests.java @@ -876,11 +876,11 @@ public void testDrainSync() throws Exception { } }); thread1.start(); - assertBusy(() -> assertEquals(0, translog.availablePermits())); + assertBusy(() -> assertFalse(translog.isLockAvailable())); // Case 2 - During an upload, if drainSync is called, it will wait for it to acquire and available permits are 0. Releasable releasable = translog.drainSync(); assertBusy(() -> assertEquals(0, latch.getCount())); - assertEquals(0, translog.availablePermits()); + assertFalse(translog.isLockAvailable()); slowDown.setSleepSeconds(0); assertEquals(6, translog.allUploaded().size()); assertEquals(2, translog.readers.size());