-
Notifications
You must be signed in to change notification settings - Fork 53
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
Simplify code using try with resources for the Reentrant lock #259
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
777fab8
Simplify code using try with resources for the Reentrant lock
davecramer 8bae555
fix tests
davecramer 2e44870
rename functions
davecramer 0bb39d8
appease checkstyle
davecramer 09e825d
we have a rule that states that line 15 must end the comment section.…
davecramer 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 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
54 changes: 54 additions & 0 deletions
54
wrapper/src/main/java/software/amazon/jdbc/util/ResourceLock.java
This file contains 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,54 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
/* | ||
* portions Copyright (c) 2022, PostgreSQL Global Development Group | ||
* See the LICENSE file in the project root for more information. | ||
*/ | ||
|
||
package software.amazon.jdbc.util; | ||
|
||
import java.util.concurrent.locks.ReentrantLock; | ||
|
||
/** | ||
* Extends a ReentrantLock for use in try-with-resources block. | ||
* | ||
* <h2>Example use</h2> | ||
* <pre>{@code | ||
* | ||
* try (ResourceLock ignore = lock.obtain()) { | ||
* // do something while holding the resource lock | ||
* } | ||
* | ||
* }</pre> | ||
*/ | ||
public final class ResourceLock extends ReentrantLock implements AutoCloseable { | ||
|
||
/** | ||
* Obtain a lock and return the ResourceLock for use in try-with-resources block. | ||
*/ | ||
public ResourceLock obtain() { | ||
lock(); | ||
return this; | ||
} | ||
|
||
/** | ||
* Unlock on exit of try-with-resources block. | ||
*/ | ||
@Override | ||
public void close() { | ||
this.unlock(); | ||
} | ||
} | ||
This file contains 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
101 changes: 101 additions & 0 deletions
101
wrapper/src/test/java/software/amazon/jdbc/util/ResourceLockTest.java
This file contains 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,101 @@ | ||||||||
package software.amazon.jdbc.util; | ||||||||
|
||||||||
import org.junit.jupiter.api.Test; | ||||||||
|
||||||||
import java.util.ArrayList; | ||||||||
import java.util.HashSet; | ||||||||
import java.util.List; | ||||||||
import java.util.Set; | ||||||||
import java.util.concurrent.Callable; | ||||||||
import java.util.concurrent.ExecutionException; | ||||||||
import java.util.concurrent.ExecutorService; | ||||||||
import java.util.concurrent.Executors; | ||||||||
import java.util.concurrent.Future; | ||||||||
import java.util.concurrent.TimeUnit; | ||||||||
import java.util.concurrent.atomic.AtomicInteger; | ||||||||
import java.util.concurrent.locks.LockSupport; | ||||||||
|
||||||||
import static org.junit.jupiter.api.Assertions.*; | ||||||||
|
||||||||
class ResourceLockTest { | ||||||||
|
||||||||
@Test | ||||||||
void testObtainClose() { | ||||||||
final ResourceLock lock = new ResourceLock(); | ||||||||
|
||||||||
assertFalse(lock.isLocked()); | ||||||||
assertFalse(lock.isHeldByCurrentThread()); | ||||||||
|
||||||||
try (ResourceLock ignore = lock.obtain()) { | ||||||||
assertTrue(lock.isLocked()); | ||||||||
assertTrue(lock.isHeldByCurrentThread()); | ||||||||
} | ||||||||
|
||||||||
assertFalse(lock.isLocked()); | ||||||||
assertFalse(lock.isHeldByCurrentThread()); | ||||||||
} | ||||||||
|
||||||||
@Test | ||||||||
void testObtainWhenMultiThreadedExpectLinearExecution() throws InterruptedException, ExecutionException { | ||||||||
CallWithResourceLock callWithResourceLock = new CallWithResourceLock(); | ||||||||
|
||||||||
int levelOfConcurrency = 5; | ||||||||
|
||||||||
ExecutorService executorService = Executors.newFixedThreadPool(levelOfConcurrency); | ||||||||
try { | ||||||||
List<Callable<CounterPair>> callables = new ArrayList<>(); | ||||||||
for (int i = 0; i < levelOfConcurrency; i++) { | ||||||||
callables.add(callWithResourceLock::invoke); | ||||||||
} | ||||||||
|
||||||||
// expect linear execution | ||||||||
List<Future<CounterPair>> results = executorService.invokeAll(callables); | ||||||||
|
||||||||
Set<Integer> preLockSet = new HashSet<>(); | ||||||||
Set<Integer> postLockSet = new HashSet<>(); | ||||||||
for (Future<CounterPair> result : results) { | ||||||||
CounterPair entry = result.get(); | ||||||||
preLockSet.add(entry.preLock); | ||||||||
postLockSet.add(entry.postLock); | ||||||||
} | ||||||||
|
||||||||
assertEquals(levelOfConcurrency, postLockSet.size()); // linear execution inside resource lock block | ||||||||
assertEquals(1, preLockSet.size()); // all threads called invoke before any finish | ||||||||
|
||||||||
} finally { | ||||||||
executorService.shutdown(); | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
static final class CallWithResourceLock { | ||||||||
|
||||||||
// wait enough time to allow concurrent threads to block on the lock | ||||||||
final long waitTime = TimeUnit.MILLISECONDS.toNanos(20); | ||||||||
final ResourceLock lock = new ResourceLock(); | ||||||||
final AtomicInteger counter = new AtomicInteger(); | ||||||||
|
||||||||
/** | ||||||||
* Invoke returning 'pre lock' and 'post lock' counter value. | ||||||||
*/ | ||||||||
CounterPair invoke() { | ||||||||
int preLock = counter.get(); | ||||||||
try (ResourceLock ignore = lock.obtain()) { | ||||||||
int postLock = counter.get(); | ||||||||
LockSupport.parkNanos(waitTime); | ||||||||
counter.incrementAndGet(); | ||||||||
return new CounterPair(preLock, postLock); | ||||||||
} | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
static final class CounterPair { | ||||||||
final int preLock; | ||||||||
final int postLock; | ||||||||
|
||||||||
CounterPair(int preLock, int postLock) { | ||||||||
this.preLock = preLock; | ||||||||
this.postLock = postLock; | ||||||||
} | ||||||||
} | ||||||||
|
||||||||
} | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
This file contains 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
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.