spring-data-mongodb
spring-data-mongodb copied to clipboard
Expose ability to set CreateIndexCommitQuorum in DefaultIndexOperations
Currently spring mongo doesn't support specifying commit quorum for index builds in the library.
Commit Quorum has been there since mongodb 4.4. To be able to set commit quorum one has to add a complete implementation of IndexOperations.
It would be great if DefaultIndexOperations provided support for setting commit quorum and/or provided a hook for it
Thanks for creating the issue. The driver supports this via MongoCollection#createIndexes that accepts an IndexModel using the current IndexOptions containing name, etc. plus a CreateIndexOptions parameter that holds the quorum.
Instead of collection.createIndex(mappedKeys, indexOptions) the IndexOperations would need to do something like:
IndexModel indexModel = new IndexModel(mappedKeys, indexOptions);
collection.createIndexes(List.of(indexModel), new CreateIndexOptions().commitQuorum(MAJORITY));
Thank you for the prompt response.
I agree the mongo driver does support it but was hoping for support from spring-data-mongodb when using MongoTemplate.
This comes up when using MongoTemplate and MongoMappingContext with autoIndexCreation=true
The DefaultIndexOperations.java used by MongoPersistentEntityIndexCreator doesn't provide a way to set it up.
We have been patching it for past few versions on our side to give us a hook
public String ensureIndex(final IndexDefinition indexDefinition) {
return Objects.requireNonNull(execute(collection -> {
MongoPersistentEntity<?> entity = lookupPersistentEntity(type, collectionName);
IndexOptions indexOptions = IndexConverters.indexDefinitionToIndexOptionsConverter().convert(indexDefinition);
indexOptions = addPartialFilterIfPresent(indexOptions, indexDefinition.getIndexOptions(), entity);
indexOptions = addDefaultCollationIfRequired(indexOptions, entity);
Document mappedKeys = mapper.getMappedSort(indexDefinition.getIndexKeys(), entity);
List<IndexModel> indexModels = Collections.singletonList(new IndexModel(mappedKeys, indexOptions));
return collection.createIndexes(indexModels, getCreateIndexOptions()).get(0);
}));
}
public CreateIndexOptions getCreateIndexOptions() {
return new CreateIndexOptions();
}
It would be great if this is considered as a valid use case and implemented, if feasible, in future releases