Skip to content
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

fix: allow to set changelog-lock-wait-time-in-minutes to 0 #375

Merged
merged 1 commit into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,17 @@ public void waitForLock() throws LockException {
boolean locked = false;

final long timeToGiveUp = getClock().instant().plusSeconds(getChangeLogLockWaitTime() * 60).toEpochMilli();
locked = acquireLock();
while (!locked && (getClock().instant().toEpochMilli() < timeToGiveUp)) {
locked = acquireLock();
if (!locked) {
getLogger().info("Waiting for changelog lock....");
try {
//noinspection BusyWait
Thread.sleep(getChangeLogLockRecheckTime() * 1000);
} catch (InterruptedException e) {
// Restore thread interrupt status
Thread.currentThread().interrupt();
}
getLogger().info("Waiting for changelog lock....");
try {
//noinspection BusyWait
Thread.sleep(getChangeLogLockRecheckTime() * 1000);
} catch (InterruptedException e) {
// Restore thread interrupt status
Thread.currentThread().interrupt();
}
locked = acquireLock();
}

if (!locked) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import liquibase.ext.mongodb.statement.DropCollectionStatement;
import liquibase.ext.mongodb.statement.FindAllStatement;
import liquibase.lockservice.DatabaseChangeLogLock;
import liquibase.lockservice.LockService;
import liquibase.lockservice.LockServiceFactory;
import liquibase.nosql.executor.NoSqlExecutor;
import lombok.SneakyThrows;
Expand Down Expand Up @@ -311,14 +312,77 @@ void waitForLock() {

verify(executorMock, times(1)).queryForLong(any(CountCollectionByNameStatement.class));
verify(executorMock, times(1)).execute(any(AdjustChangeLogLockCollectionStatement.class));
verify(executorMock, times(2)).queryForObject(any(SelectChangeLogLockStatement.class), eq(Document.class));
verify(executorMock, times(3)).queryForObject(any(SelectChangeLogLockStatement.class), eq(Document.class));
verify(executorMock, times(1)).queryForList(any(FindAllStatement.class), eq(Document.class));
verifyNoMoreInteractions(executorMock);

assertThat(lockService.getHasDatabaseChangeLogLockTable()).isTrue();
assertThat(lockService.hasChangeLogLock()).isFalse();
}

@SneakyThrows
@Test
void waitForLockWithNoWaitAndNoLockInDatabase() {
Scope.getCurrentScope().getSingleton(ExecutorService.class).setExecutor(EXECUTOR_NAME, database, executorMock);
lockService.setDatabase(database);

// Repository already exists
doReturn(1L).when(executorMock).queryForLong(any(CountCollectionByNameStatement.class));
// acquire the lock
doReturn(1).when(executorMock).update(any(ReplaceChangeLogLockStatement.class));

assertThat(lockService.getHasDatabaseChangeLogLockTable()).isNull();
assertThat(lockService.hasChangeLogLock()).isFalse();

lockService.setChangeLogLockWaitTime(0L);
lockService.waitForLock();

verify(executorMock, times(1)).queryForLong(any(CountCollectionByNameStatement.class));
verify(executorMock, times(1)).execute(any(AdjustChangeLogLockCollectionStatement.class));
verify(executorMock, times(1)).queryForObject(any(SelectChangeLogLockStatement.class), eq(Document.class));
verify(executorMock, times(0)).queryForList(any(FindAllStatement.class), eq(Document.class));

verifyNoMoreInteractions(executorMock);

assertThat(lockService.getHasDatabaseChangeLogLockTable()).isTrue();
assertThat(lockService.hasChangeLogLock()).isTrue();
}

@SneakyThrows
@Test
void waitForLockWithNoWaitAndLockInDatabase() {
final MongoChangeLogLock lockedLock = new MongoChangeLogLock(1, new Date(), "lockedByMock", true);

Scope.getCurrentScope().getSingleton(ExecutorService.class).setExecutor(EXECUTOR_NAME, database, executorMock);
lockService.setDatabase(database);

doReturn(lockService.getConverter().toDocument(lockedLock))
.when(executorMock).queryForObject(any(SelectChangeLogLockStatement.class), eq(Document.class));
doReturn(Collections.singletonList((Object) lockService.getConverter().toDocument(lockedLock)))
.when(executorMock).queryForList(any(FindAllStatement.class), eq(Document.class));

// Repository already exists
doReturn(1L).when(executorMock).queryForLong(any(CountCollectionByNameStatement.class));

assertThat(lockService.getHasDatabaseChangeLogLockTable()).isNull();
assertThat(lockService.hasChangeLogLock()).isFalse();

lockService.setChangeLogLockWaitTime(0L);
assertThatExceptionOfType(LockException.class).isThrownBy(lockService::waitForLock)
.withMessageStartingWith("Could not acquire change log lock. Currently locked by lockedByMock since");


verify(executorMock, times(1)).queryForLong(any(CountCollectionByNameStatement.class));
verify(executorMock, times(1)).execute(any(AdjustChangeLogLockCollectionStatement.class));
verify(executorMock, times(1)).queryForObject(any(SelectChangeLogLockStatement.class), eq(Document.class));
//verify(executorMock, times(0)).queryForList(any(FindAllStatement.class), eq(Document.class));

verifyNoMoreInteractions(executorMock);

assertThat(lockService.getHasDatabaseChangeLogLockTable()).isTrue();
assertThat(lockService.hasChangeLogLock()).isFalse();
}

@SneakyThrows
@Test
void acquireLock() {
Expand Down
Loading