Fix JdbcLockRegistry ttl (#3080)
Signed-off-by: vasilchev <vasil.ilchev@bosch.com>
This commit is contained in:
@@ -9,6 +9,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.eclipse.hawkbit.repository.jpa;
|
package org.eclipse.hawkbit.repository.jpa;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.concurrent.locks.Lock;
|
import java.util.concurrent.locks.Lock;
|
||||||
@@ -211,8 +212,10 @@ public class JpaRepositoryConfiguration {
|
|||||||
@Bean
|
@Bean
|
||||||
@ConditionalOnMissingBean
|
@ConditionalOnMissingBean
|
||||||
@SuppressWarnings("java:S1452") // it could be any LockRegistry<? extends Lock>
|
@SuppressWarnings("java:S1452") // it could be any LockRegistry<? extends Lock>
|
||||||
public LockRegistry<? extends Lock> lockRegistry(final Optional<LockRepository> lockRepository) {
|
public LockRegistry<? extends Lock> lockRegistry(final Optional<LockRepository> lockRepository, final LockProperties lockProperties) {
|
||||||
return lockRepository.<LockRegistry<? extends Lock>> map(JdbcLockRegistry::new).orElseGet(DefaultLockRegistry::new);
|
return lockRepository.<LockRegistry<? extends Lock>> map(repo ->
|
||||||
|
new JdbcLockRegistry(repo, Duration.ofMillis(lockProperties.getTtl()))
|
||||||
|
).orElseGet(DefaultLockRegistry::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
|||||||
@@ -9,6 +9,7 @@
|
|||||||
*/
|
*/
|
||||||
package org.eclipse.hawkbit.repository.jpa.cluster;
|
package org.eclipse.hawkbit.repository.jpa.cluster;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
import java.time.Instant;
|
import java.time.Instant;
|
||||||
import java.time.temporal.ChronoUnit;
|
import java.time.temporal.ChronoUnit;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -95,17 +96,16 @@ public class DistributedLockRepository extends DefaultLockRepository {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Spring Integration 7.0 calls acquire(String, Duration) directly; the deprecated acquire(String) is no longer invoked
|
||||||
|
// by JdbcLockRegistry. Override the new method to populate lockToRefreshTime for the refresh mechanism.
|
||||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||||
@Override
|
@Override
|
||||||
public boolean acquire(final String lock) {
|
public boolean acquire(final String lock, final Duration ttl) {
|
||||||
try {
|
try {
|
||||||
// real acquisition (made by super.acquire) is made in a new transaction
|
// run in a new transaction so we know the committed result before updating lockToRefreshTime
|
||||||
// because we need to know real (after transaction commit) result Ïto know if it is really successful.
|
|
||||||
// otherwise the super.acquire will return result before been committed and could be false positive
|
|
||||||
final boolean acquired = DeploymentHelper.runInNewTransaction(
|
final boolean acquired = DeploymentHelper.runInNewTransaction(
|
||||||
txManager, "lock-acquire", Isolation.READ_COMMITTED.value(), status -> super.acquire(lock));
|
txManager, "lock-acquire", Isolation.READ_COMMITTED.value(), status -> super.acquire(lock, ttl));
|
||||||
if (acquired) {
|
if (acquired) {
|
||||||
// update next refresh time
|
|
||||||
refreshAfterMillis.ifPresent(afterMillis -> lockToRefreshTime.put(lock, Instant.now().plus(afterMillis, ChronoUnit.MILLIS)));
|
refreshAfterMillis.ifPresent(afterMillis -> lockToRefreshTime.put(lock, Instant.now().plus(afterMillis, ChronoUnit.MILLIS)));
|
||||||
}
|
}
|
||||||
return acquired;
|
return acquired;
|
||||||
|
|||||||
Reference in New Issue
Block a user