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 ReplaceChangeLogLockStatement #582

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

raffaeleflorio
Copy link

@raffaeleflorio raffaeleflorio commented Dec 1, 2024

The current update method of ReplaceChangeLogLockStatement is not safe. It allows multiple owner (identified by the lockedBy field) to acquire the lock simultaneously. And it also allows the release by a different owner. There isn't any fence in the current implementation:

final Optional<Document> changeLogLock = Optional.ofNullable(
database.getMongoDatabase()
.getCollection(collectionName)
.findOneAndReplace(Filters.eq(MongoChangeLogLock.Fields.id,
entry.getId()), inputDocument,
new FindOneAndReplaceOptions().upsert(upsert).returnDocument(ReturnDocument.AFTER))
);
return changeLogLock.map(e -> 1).orElse(0);

Indeed, by running the following code with the latest liquibase-mongodb version there will be also multiple exceptions:

public final class Main {
    public static void main(final String[] args) throws Exception {
        for (var i = 0; i < 16; i++) {
            Executors.newSingleThreadExecutor().execute(() ->
                    {
                        try (
                                var db = DatabaseFactory.getInstance().openDatabase(
                                        "mongodb://localhost:27017/test?directConnection=true",
                                        null,
                                        null,
                                        null,
                                        null
                                );
                                var liquibase = new Liquibase(
                                        "db/changelog.xml",
                                        new ClassLoaderResourceAccessor(),
                                        db
                                )
                        ) {
                            liquibase.update("");
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                    }
            );
        }
        Thread.sleep(10000);
    }
}

The proposed implementation blocks multiple acquisition by taking advantage of the Mongo DuplicateKey error. Specifically, an owner will try to acquire only an unlocked lock. And we have multiple scenarios here:

  1. It finds the unlocked lock and it acquires the lock by replacing the Mongo document
  2. It doesn't find the lock because this is the first acquisition request ever. So, it acquires the lock by inserting the Mongo document
  3. It doesn't find the lock because it's already locked. In this case, it will try in any case to insert the document in Mongo but this time it will fail to acquire because the DuplicateKey error

The same approach is used to allow the lock release only by his owner.

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.

1 participant