-
Notifications
You must be signed in to change notification settings - Fork 1.1k
GH-3272: Support lease renewal for distributed locks #3317
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Copyright 2020 the original author or authors. | ||
* | ||
* 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 | ||
* | ||
* https://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. | ||
*/ | ||
|
||
package org.springframework.integration.support.locks; | ||
|
||
/** | ||
* A {@link LockRegistry} implementing this interface supports the renewal of the time to live of a lock | ||
* | ||
* @author Alexandre Strubel | ||
* | ||
* @since 5.4 | ||
*/ | ||
public interface RenewableLockRegistry extends LockRegistry { | ||
|
||
/** | ||
* Renew the time to live of the lock is associated with the parameter object. | ||
* The lock must be held by the current thread | ||
* @param lockKey The object with which the lock is associated. | ||
*/ | ||
void renewLock(Object lockKey); | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* Copyright 2016-2019 the original author or authors. | ||
* Copyright 2016-2020 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
|
@@ -27,6 +27,7 @@ | |
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.runner.RunWith; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
@@ -40,6 +41,7 @@ | |
/** | ||
* @author Dave Syer | ||
* @author Artem Bilan | ||
* @author Stefan Vassilev | ||
* | ||
* @since 4.3 | ||
*/ | ||
|
@@ -168,7 +170,7 @@ public void testTwoThreadsSecondFailsToGetLock() throws Exception { | |
lock1.unlock(); | ||
Object ise = result.get(10, TimeUnit.SECONDS); | ||
assertThat(ise).isInstanceOf(IllegalMonitorStateException.class); | ||
assertThat(((Exception) ise).getMessage()).contains("You do not own"); | ||
assertThat(((Exception) ise).getMessage()).contains("own"); | ||
} | ||
|
||
@Test | ||
|
@@ -263,7 +265,25 @@ public void testTwoThreadsWrongOneUnlocks() throws Exception { | |
lock.unlock(); | ||
Object imse = result.get(10, TimeUnit.SECONDS); | ||
assertThat(imse).isInstanceOf(IllegalMonitorStateException.class); | ||
assertThat(((Exception) imse).getMessage()).contains("You do not own"); | ||
assertThat(((Exception) imse).getMessage()).contains("own"); | ||
} | ||
|
||
@Test | ||
public void testLockRenew() { | ||
final Lock lock = this.registry.obtain("foo"); | ||
|
||
assertThat(lock.tryLock()).isTrue(); | ||
try { | ||
registry.renewLock("foo"); | ||
} finally { | ||
lock.unlock(); | ||
} | ||
} | ||
|
||
@Test | ||
public void testLockRenewLockNotOwned() { | ||
this.registry.obtain("foo"); | ||
|
||
Assertions.assertThrows(IllegalMonitorStateException.class, () -> registry.renewLock("foo")); | ||
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. We prefer AssertJ assertions instead: |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1085,6 +1085,11 @@ If so, you can specify the `id` to be associated with the `DefaultLockRepository | |
Starting with version 5.1.8, the `JdbcLockRegistry` can be configured with the `idleBetweenTries` - a `Duration` to sleep between lock record insert/update executions. | ||
By default it is `100` milliseconds and in some environments non-leaders pollute connections with data source too often. | ||
|
||
Starting with version 5.4, the `RenewableLockRegistry` interface has been introduced and added to `JdbcLockRegistry`. | ||
The `renewLock()` method must be called during locked process in case of the locked process would be longer than time to live of the lock. | ||
So the time to live can be highly reduce and deployments can retake a lost lock quickly. | ||
NB. The lock renewal can be done only if the lock is held by the current thread, so the locked process has to be executed in another thread. | ||
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. I'm not sure what is this 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. NB (latin Nota Bene - "note well") is commonly used in real (British) English - it is not common in the USA. Use AsciiDoctor 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. Oh, right! Didn't think through. This sentence is really Thank you, Gary! OK. So, I'm pulling this locally and fixing the latest simple concerns on merge. |
||
|
||
[[jdbc-metadata-store]] | ||
=== JDBC Metadata Store | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.