Skip to content

[#2663] Add lockAcquireRetryInterval to LeaseDatabaseLocker - #2664

Open
colinwerner wants to merge 3 commits into
apache:mainfrom
colinwerner:lease_locker_retry_interval
Open

colinwerner wants to merge 3 commits into
apache:mainfrom
colinwerner:lease_locker_retry_interval

Conversation

@colinwerner

Copy link
Copy Markdown
Contributor

Closes #2663

LeaseDatabaseLocker uses lockAcquireSleepInterval both as the lease duration and as the wait between acquire attempts. A lease that is long enough to be safe also makes a waiting broker poll slowly, so a lease that has expired or been released is picked up up to a full lease duration late.

This adds an optional lockAcquireRetryInterval (<lease-database-locker lockAcquireRetryInterval="..."/>):

  • it sets only the wait between acquire attempts in doStart(); lockAcquireSleepInterval remains the lease duration in doStart() and keepAlive();
  • unset or <= 0 (the default) uses lockAcquireSleepInterval, so existing configurations are unchanged;
  • logs a warning if it is longer than the lease;
  • the retry debug log reports the interval in use.

The attribute appears in the generated activemq.xsd under lease-database-locker.

Commits:

  1. Re-enable LeaseDatabaseLockerTest. The class was @Ignored during the jakarta migration (AMQ-9239), so new tests there would not run. The Derby tests passed as-is. The three time-offset tests failed only because the mocked Statements.getCurrentDateTime() returned null, which Mockito's anyString() does not match. Stubbing it fixes them.
  2. Add lockAcquireRetryInterval.
  3. Report the keep-alive period as renew in toString(). toString() printed lockAcquireSleepInterval for both duration and renew, but the lease is renewed every lockKeepAlivePeriod. It's included here because this PR is about separating the locker's timings, and toString() is where they are read when tuning failover. With this change it reports all three correctly (duration, renew, retry). It's a separate commit so it can be dropped if preferred.

Tests (LeaseDatabaseLockerTest, embedded Derby):

  • testReleasedLeaseAcquiredWithinLockAcquireRetryInterval: with a 10 s lease and a 250 ms retry interval, a waiting locker acquires a released lease in well under 2.5 s (about 50 ms locally; about 9.8 s before this change), and the acquired lease still runs for the full lease duration.
  • testDefaultLockAcquireRetryIntervalIsLockAcquireSleepInterval: with the property unset, a released lease is picked up on the next lockAcquireSleepInterval poll, as before.
  • testToStringReportsLeaseTimings: toString() reports the lease duration, the keep-alive period and the retry interval.
mvn -pl activemq-jdbc-store,activemq-unit-tests test -Dtest=LeaseDatabaseLockerTest -Dsurefire.failIfNoSpecifiedTests=false

Note: on current main, activemq-broker test-compile fails in ManagementContextSslTest (SslContext is abstract), which is unrelated to this change. I ran the tests above against the other modules built with -DskipTests.

Docs: the JDBC master/slave / pluggable storage lockers page in activemq-website should list the new attribute; I can open a follow-up PR there.

Generated with Claude Code.

colin and others added 3 commits October 14, 2026 13:00
The class was ignored during the jakarta migration (AMQ-9239) with a
note about Mockito/byte-buddy module opens. The Derby-based tests pass
as-is. The three time-offset tests failed because the mocked
Statements returned null for getCurrentDateTime(), and anyString() does
not match null, so prepareStatement() returned null. Stub the statement
text so the mock matches.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
LeaseDatabaseLocker used lockAcquireSleepInterval both as the lease
duration and as the wait between acquire attempts. The lease has to
outlive the slowest renewal the master can make, so a safe lease also
makes a waiting broker poll slowly: failover after a clean stop could
take up to lockAcquireSleepInterval even though the lease was released
immediately, and up to twice that after a crash.

Add an optional lockAcquireRetryInterval that sets only the wait between
acquire attempts. lockAcquireSleepInterval stays the lease duration. The
default (unset) uses lockAcquireSleepInterval, so existing configurations
behave as before. Warn if the retry interval is longer than the lease.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
…ocker.toString()

toString() printed lockAcquireSleepInterval for both duration and renew.
The lease is renewed every lockKeepAlivePeriod of the lockable service,
so report that instead. Along with the new retry value this gives the
three timings that decide failover behaviour.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@Override
public String toString() {
return "LeaseDatabaseLocker owner:" + leaseHolderId + ",duration:" + lockAcquireSleepInterval + ",renew:" + lockAcquireSleepInterval;
return "LeaseDatabaseLocker owner:" + leaseHolderId + ",duration:" + lockAcquireSleepInterval + ",renew:" + (lockable != null ? lockable.getLockKeepAlivePeriod() : -1)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This toString() change isn't required for lockAcquireRetryInterval. It's included because renew previously printed lockAcquireSleepInterval (the same value as duration), while the lease is actually renewed every lockKeepAlivePeriod of the lockable service. Since this PR separates the locker's timings, it seemed worth making the one place that reports them accurate: duration, renew and retry are the three values that determine failover time. lockable is null until the locker is configured, so renew prints -1 in that case. It's a separate commit, so I'm happy to drop it or move it to its own PR.

@colinwerner

Copy link
Copy Markdown
Contributor Author

@jbonofre do you know who might be best to review this PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LeaseDatabaseLocker: allow the lease acquire retry interval to be set separately from the lease duration

1 participant