I have a question related to concurrent invocation of createIndex() method by multiple clients for the same unique index (java mongodb-driver-sync 4.5.0)
Q1: Is it guaranteed that this method always blocks until the index build is complete? It looks like it’s blocking but it’s not mentioned in the java driver documentation (similar story with corresponding mongosh command).
Q2: If method is invoked while index build was already triggered and it’s in progress – is it guaranteed that this method always blocks in this scenario as well?
I performed following test:
- Local docker Mongo 5.0.5 Community standalone (so latency between java app and oms is very low)
- There is a collection with populated data 4 million documents
- I’m planning to create index with unique=true that takes about 10 seconds for this collection.
- There are 30 concurrent MongoClients (java mongodb-driver-sync 4.5.0) and each of them invokes createIndex operation for the same unique index.
- It was observed that behaviour is indeterministic and varies from run to run but at least couple of createIndex() finished successfully almost immediately whereas majority od clients is blocked for ~10 seconds as expected
- If number of clients is lower - like 5 then it’s hard to reproduce this issue.
- The same test but with small delay (like 500ms) after first createIndex() causes that remaining 29 clients always blocks for ~10 seconds.
- The same test but using MongoDB instance @Atlas blocks all clients – maybe is related to additional network overhead that hides the timing/synchronisation issue?
What do you think - is it a bug in client/server or the answer for Q2 is no - it’s not guaranteed?