-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fail lock acquisition if service already holds one. #24782
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
patrickmann
merged 6 commits into
master
from
refactor/fail-lock-acquisition-if-factory-holds-one
Mar 12, 2026
+123
−10
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3738500
Fail lock acquisition if service already holds one.
dennisoelkers b79ee69
Merge branch 'master' into refactor/fail-lock-acquisition-if-factory-…
dennisoelkers 6f1eb13
Add test coverage for IllegalStateException on duplicate lock acquisi…
Copilot b8ec0d3
Merge branch 'master' into refactor/fail-lock-acquisition-if-factory-…
dennisoelkers dee1f05
Merge branch 'master' into refactor/fail-lock-acquisition-if-factory-…
dennisoelkers 89add0b
Pulling up check for pre-existing lock, using idiomatic optional hand…
dennisoelkers File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
graylog2-server/src/test/java/org/graylog2/cluster/lock/RefreshingLockServiceTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| /* | ||
| * Copyright (C) 2020 Graylog, Inc. | ||
| * | ||
| * This program is free software: you can redistribute it and/or modify | ||
| * it under the terms of the Server Side Public License, version 1, | ||
| * as published by MongoDB, Inc. | ||
| * | ||
| * This program is distributed in the hope that it will be useful, | ||
| * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| * Server Side Public License for more details. | ||
| * | ||
| * You should have received a copy of the Server Side Public License | ||
| * along with this program. If not, see | ||
| * <http://www.mongodb.com/licensing/server-side-public-license>. | ||
| */ | ||
| package org.graylog2.cluster.lock; | ||
|
|
||
| import org.graylog2.shared.SuppressForbidden; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.mockito.Mock; | ||
| import org.mockito.junit.jupiter.MockitoExtension; | ||
|
|
||
| import java.time.Duration; | ||
| import java.time.ZoneOffset; | ||
| import java.time.ZonedDateTime; | ||
| import java.util.Optional; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.ScheduledExecutorService; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
| import static org.mockito.ArgumentMatchers.anyString; | ||
| import static org.mockito.ArgumentMatchers.eq; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| @ExtendWith(MockitoExtension.class) | ||
| public class RefreshingLockServiceTest { | ||
|
|
||
| @Mock | ||
| private LockService lockService; | ||
|
|
||
| private ScheduledExecutorService scheduler; | ||
|
|
||
| private RefreshingLockService refreshingLockService; | ||
|
|
||
| private final Lock firstLock = Lock.builder() | ||
| .resource("first-resource") | ||
| .lockedBy("node-123") | ||
| .createdAt(ZonedDateTime.now(ZoneOffset.UTC)) | ||
| .updatedAt(ZonedDateTime.now(ZoneOffset.UTC)) | ||
| .build(); | ||
|
|
||
| private final Lock secondLock = Lock.builder() | ||
| .resource("second-resource") | ||
| .lockedBy("node-123") | ||
| .createdAt(ZonedDateTime.now(ZoneOffset.UTC)) | ||
| .updatedAt(ZonedDateTime.now(ZoneOffset.UTC)) | ||
| .build(); | ||
|
|
||
| @BeforeEach | ||
| @SuppressForbidden("Using Executors.newSingleThreadScheduledExecutor() is okay in tests") | ||
| void setUp() { | ||
| scheduler = Executors.newSingleThreadScheduledExecutor(); | ||
| refreshingLockService = new RefreshingLockService( | ||
| lockService, | ||
| scheduler, | ||
| Duration.ofMinutes(5) | ||
| ); | ||
| } | ||
|
|
||
| @AfterEach | ||
| void tearDown() { | ||
| if (scheduler != null) { | ||
| scheduler.shutdownNow(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| void throwsIllegalStateExceptionWhenAcquiringLockWhileAlreadyHoldingOne() throws AlreadyLockedException { | ||
| // Mock the lock service to return locks | ||
| when(lockService.lock(eq("first-resource"), anyString())) | ||
| .thenReturn(Optional.of(firstLock)); | ||
| when(lockService.lock(eq("second-resource"), anyString())) | ||
| .thenReturn(Optional.of(secondLock)); | ||
|
|
||
| // Acquire first lock successfully | ||
| refreshingLockService.acquireAndKeepLock("first-resource", "context-1"); | ||
|
|
||
| // Attempt to acquire second lock while still holding the first | ||
| assertThatThrownBy(() -> refreshingLockService.acquireAndKeepLock("second-resource", "context-2")) | ||
| .isInstanceOf(IllegalStateException.class) | ||
| .hasMessageContaining("Unable to acquire new lock, already holding lock that would get lost"); | ||
| } | ||
|
|
||
| @Test | ||
| void throwsIllegalStateExceptionWhenAcquiringLockWithMaxConcurrencyWhileAlreadyHoldingOne() throws AlreadyLockedException { | ||
| // Mock the lock service to return locks | ||
| when(lockService.lock(eq("first-resource"), eq(1))) | ||
| .thenReturn(Optional.of(firstLock)); | ||
| when(lockService.lock(eq("second-resource"), eq(1))) | ||
| .thenReturn(Optional.of(secondLock)); | ||
|
|
||
| // Acquire first lock successfully | ||
| refreshingLockService.acquireAndKeepLock("first-resource", 1); | ||
|
|
||
| // Attempt to acquire second lock while still holding the first | ||
| assertThatThrownBy(() -> refreshingLockService.acquireAndKeepLock("second-resource", 1)) | ||
| .isInstanceOf(IllegalStateException.class) | ||
| .hasMessageContaining("Unable to acquire new lock, already holding lock that would get lost"); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new lock validation logic should be covered by a test case that verifies the
IllegalStateExceptionis thrown when attempting to schedule a lock while already holding one.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot: Please implement a test like that.