Error creating primary Index immediately after collection creation : Keyspace not found in CB datastore

I’m currently facing an issue when I’m trying to build a primary index right after the collection creation.
At startup, my app checks if required collection exists, and, if not, creates them.
To do so, I’m using the following code :

CollectionSpec collectionSpec = CollectionSpec.create(collectionName, databaseScope);
this.getBucket().collections().createCollection(collectionSpec);

Then right at the following line, I’m creating the primary index for my collection, with the following code

QueryResult result = this.getScope(organization).query(
String.format("CREATE PRIMARY INDEX `%s` ON `%s`", indexName, collectionName)
);

And there comes my issue, if I’m putting a sleep (regular TimeUnit.SECONDS.sleep(4);) of a couple of seconds between those lines, everything works fine. But if I’m not putting any sleep between those lines I’m getting an error message Keyspace not found in CB datastore. Which seems to indicate that my collection does not exist.

So I’d like to know if there is some proper way to ensure that the collection is fully created synchronously so that my next line of code can use it already.

Thanks for your time :slight_smile:

1 Like

Collection creation is asynchronous will not wait for propagated to all the services. It will take couple of secs.
Track via MB-46643, DOC-8971

1 Like

as @vsr1 pointed out and you figured out, it’s eventually consistent. We are working on a proper solution mid-term, but you can imagine that in a distributed systems things are not staightforward.

I think instead of a fixed sleep that will slow testing down, I’d just check for that error and then do a shorter sleep and retry the query op. This also allows to easily put it in its own method so it doesn’t clutter the other code so much.

1 Like

Thanks @vsr1 for your answer and the related issues links.

@daschl , I have no issue with that. I completely understand the underlying constraints and I can easily work around that (plus this is only at the first init). Adding a “blocking” parameter on the SDK method would be a nice touch but that is probably far from a priority.

One thing that seems weird however is that checking the collection existence through the ScopeSpec method collections() returns the “still not created” collections making it impossible to rely on anything than catching the index creation exception and trying to handle this specific error message.

Anyway thanks for your precious answers, I know my way around better now :slight_smile:

@jonathan your observation is correct. What you are observing is the state delta between the cluster manager (which handles the collection and scope creation) and the query engine being notified of it and starting to recognize it in its internal state.

1 Like