GET _index does not immediately return indexes created with POST _index
I am using the POST and DELETE _index APIs in CouchDB 2.0.0. I realize the index will be built out asynchronously, but it looks like even the index definition is not registered synchronously. Queries to GET _index will not display a created index until a couple milliseconds later (and will not remove a deleted index from the list until a couple milliseconds later). These couple milliseconds are no big deal in most use cases, but in our case the problem shows up in unit tests that validate (via GET) that a created index (via POST) did in fact get defined in CouchDB. Also if CouchDB crashed in that time window we would expect the index definition would be persisted since the client already received a 200 “created” result. The problem persists even if I do a _ensure_full_commit after the index POST but before the index GET. I am using a single node CouchDB (n=1), so no cluster complications. I would have expected that design doc (index) creation would follow the same guarantees as regular doc creation, that is, the created document should be immediately queryable after getting a 200 “created” result.
Expected Behavior
Indexes that are created via POST /{db}/_index and return a 200 "created" result should immediately be available in GET /{db}/_index queries.
Current Behavior
Index can not be retrieved via GET /{db}/_index until a few milliseconds after receiving 200 “created” result.
Possible Solution
I would have expected that design doc (index) creation would follow the same guarantees as regular doc creation, that is, the created document should be immediately queryable after getting a 200 “created” result.
Steps to Reproduce (for bugs)
2017-12-28 19:37:30.658 POST /testindexoperations/_index 2017-12-28 19:37:30.660 POST /testindexoperations/_ensure_full_commit 2017-12-28 19:37:30.661 GET /testindexoperations/_index/ <-- INDEX NOT IN LIST 2017-12-28 19:37:30.662 GET /testindexoperations/_index/ <-- INDEX NOT IN LIST 2017-12-28 19:37:30.664 GET /testindexoperations/_index/ <-- INDEX IN LIST
Context
In our case the problem shows up in unit tests that validate (via GET) that a created index (via POST) did in fact get defined in CouchDB. In the real world, if CouchDB crashed in that time window we would expect the index definition would be persisted since the client already received a 200 “created” result.
Your Environment
- Version used: 2.0.0
I have a very similar problem with unit testing right now. I think it's the same underlying issue as well.
I've configured my CouchDB as a single node instance and enbabled couch_peruser. My tests for adding a user and writing a document to the respective user database sometimes works and sometimes fails. There seems to be a delay between the 201 Created for the user and being able to write to the user's database.
Is this intentionally? What's the best practive here? Of course I could just skip the tests, but I also want to integrate this into an automated workflow where I need to be sure this works reliably.
Are we supposed to make HEAD requests until we get a 200 for the user's database?
I'm using CouchDB 2.1.1 on Ubuntu 16.04. (Erlang 18.1). I can reliably reproduce the issue by chaining the following curl commands:
curl -u admin:secret -X PUT -H "Content-Type: application/json" -d '{"name":"[email protected]","password":"abc","type":"user","roles":[]}' couchhost:5984/_users/org.couchdb.user:[email protected] \
&& curl -u admin:secret -H "Content-Type: application/json" -d '{"field":"value"}' couchhost:5984/userdb-6a6f686e646f65406d61696c2e636f6d
I can also reliably write to it afterwards using the second curl command again:
curl -u admin:secret -H "Content-Type: application/json" -d '{"field":"value"}' couchhost:5984/userdb-6a6f686e646f65406d61696c2e636f6d
6a6f686e646f65406d61696c2e636f6d being the hex representation of [email protected].
@denyeart does this also happen with q=1 databases? // please reopen with your feedback.
@codemusings yours is a separate issue that has a similar profile. peruser is an async process and you’ll have to poll for the DB existing before you can use it. Either by repeatedly requesting /dbname or by subscribing to /_global_changes.