From f26659b53a2475774fb8c418c0dacc809441cb5b Mon Sep 17 00:00:00 2001 From: "opensearch-trigger-bot[bot]" <98922864+opensearch-trigger-bot[bot]@users.noreply.github.com> Date: Tue, 1 Aug 2023 16:08:17 -0700 Subject: [PATCH] Call listner.onFailure when lock creation failed (#435) (#443) Signed-off-by: Heemin Kim (cherry picked from commit d2e0ac046af1386dbcdc91446b35420abf4d91a7) Co-authored-by: Heemin Kim --- .../jobscheduler/spi/utils/LockService.java | 9 ++++---- .../jobscheduler/spi/utils/LockServiceIT.java | 22 +++++++++++++++++++ 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/spi/src/main/java/org/opensearch/jobscheduler/spi/utils/LockService.java b/spi/src/main/java/org/opensearch/jobscheduler/spi/utils/LockService.java index d133d7b1..19924da4 100644 --- a/spi/src/main/java/org/opensearch/jobscheduler/spi/utils/LockService.java +++ b/spi/src/main/java/org/opensearch/jobscheduler/spi/utils/LockService.java @@ -236,17 +236,16 @@ private void createLock(final LockModel tempLock, ActionListener list exception -> { if (exception instanceof VersionConflictEngineException) { logger.debug("Lock is already created. {}", exception.getMessage()); + listener.onResponse(null); + return; } - if (exception instanceof IOException) { - logger.error("IOException occurred creating lock", exception); - } - listener.onResponse(null); + listener.onFailure(exception); } ) ); } catch (IOException e) { logger.error("IOException occurred creating lock", e); - listener.onResponse(null); + listener.onFailure(e); } } diff --git a/spi/src/test/java/org/opensearch/jobscheduler/spi/utils/LockServiceIT.java b/spi/src/test/java/org/opensearch/jobscheduler/spi/utils/LockServiceIT.java index f43d976a..e52ba488 100644 --- a/spi/src/test/java/org/opensearch/jobscheduler/spi/utils/LockServiceIT.java +++ b/spi/src/test/java/org/opensearch/jobscheduler/spi/utils/LockServiceIT.java @@ -189,6 +189,28 @@ public void testSecondAcquireLockFail() throws Exception { latch.await(10L, TimeUnit.SECONDS); } + public void testAcquireLockWithLongIdFail() throws Exception { + String uniqSuffix = "_long_lock_id"; + String lockID = randomAlphaOfLengthBetween(513, 1000); + CountDownLatch latch = new CountDownLatch(1); + LockService lockService = new LockService(client(), this.clusterService); + final JobExecutionContext context = new JobExecutionContext( + Instant.now(), + new JobDocVersion(0, 0, 0), + lockService, + JOB_INDEX_NAME + uniqSuffix, + JOB_ID + uniqSuffix + ); + + lockService.acquireLockWithId(context.getJobIndexName(), LOCK_DURATION_SECONDS, lockID, ActionListener.wrap(lock -> { + fail("should throw an exception"); + }, exception -> { + assertTrue(exception.getMessage().contains("too long")); + latch.countDown(); + })); + latch.await(10L, TimeUnit.SECONDS); + } + public void testLockReleasedAndAcquired() throws Exception { String uniqSuffix = "_lock_release+acquire"; String lockID = randomAlphaOfLengthBetween(6, 15);