The Index not available after CouchBase docker image start up

Hi All,
In my project, The Unit Test need the dependency of Couchbase instance, so I create a docker image and start it up with the UT.

I have create an Index when build the docker image:
CREATE INDEX class_index ON flowgatetest(_class) USING GSI WITH {\"defer_build\":false}

The issue is, When the UT run and call the couchbase, it failed frequently by saying:

No index available on keyspace flowgatetest that matches your query. Use CREATE INDEX or CREATE PRIMARY INDEX to create an index, or check that your expected index is online.

I must sleep several minutes to wait the Index online, and sometimes even wait a long time the index is still not available.

So how can I fix this issue, what should I do to make the Index online immediately , Thanks a lot for your help

Hi @yongchunc !

Are you loading data into the database and then creating the index? If so, it might make more sense to do it the other way around so that the index gets “built” on an empty dataset and then keeps itself up-to-date as the data comes in.

I’d also suggest you have a look at our integration with TestContainers. It may not directly solve your issue here, but it will be a more standardized way of implementing your tests.

Hi @perry, thanks for your reply.

Are you loading data into the database and then creating the index?

The answer is No, Actually, i create the index in the init script, after that Unit Test insert or delete data in the database.

This is my init script:

couchbase-cli cluster-init --cluster-username=xxx --cluster-password=pwd --cluster-ramsize=1024 --cluster-index-ramsize=512 --cluster-fts-ramsize=256 --index-storage-setting=default --services=data,index,query
couchbase-cli user-manage -c localhost -u xxx -p pwd --set --rbac-username=xxx --rbac-password=pwd --roles=bucket_full_access[*] --auth-domain=local
couchbase-cli bucket-create -c localhost -u xxx -p pwd --bucket bucket-name --bucket-type couchbase --bucket-ramsize 512 --bucket-replica 1 --wait
cbq -u xxx -p pwd --script="CREATE INDEX \`class_index\` ON \`bucket-name\`(\`_class\`) USING GSI WITH {\"defer_build\":false}"

So I don’t understand why this erro happens.

Understood thank you. Also just to confirm, the query you’re running matches that index correct?

I’m surprised that it’s taking several minutes to come online, but if there are very few CPU/disk resources available then I suppose it could take a little bit of time. Another approach might be to use cbq to query for the state of the index and then you’ll know exactly when it is online: Getting System Information | Couchbase Docs

You may also want to consider using this Docker setup which is a more comprehensive way of setting up a test system: GitHub - brantburnett/couchbasefakeit: Docker scripts for a base Couchbase Server image for testing/development, with support for fakeit for data generation

thanks to @btburnett3