witash commented on issue #6118:
URL: https://github.com/apache/couchdb/issues/6118#issuecomment-5867385032

   Thanks for the fix.
   
   There's two error messages in the issue description, which appear to be two 
different issues.
   
   `invalid_start_purge_seq` is fixed on main now, but I'm still able to 
reproduce `Index updates applied in the wrong order` error on main by:
   
   1. Creating a database on an instance on main or 3.5.2 with two sample 
documents
   2. Purging them
   3. Immediately afterwards, before the index has a chance to be updated, 
recreating them in the reverse order that they were purged
   4. querying the index again
   
   I think this may be what initially broke the instance from the issue 
description; it was doing heavy purging while clients were still uploading 
edits. 
   So a client re-creating purged documents by editing them before the index 
had caught up is possible, although unlikely.
   The application doing the purging waits for the index to be up to date 
between large purging batches, and it should be rare to get new edits on 
documents that have been purged on the server, so this should be a rare edge 
case, but apparently it did happen.
   
   Then, the `invalid_start_purge_seq` error occurred after trying to fix the 
wrong order error by rebuilding the index.
   
   ### Version
   
   3.5.2, and main at a24ee4393 (which includes #6096)
   
   ### Steps to Reproduce
   
   Single node:
   
   ```sh
   C=http://admin:[email protected]:5984
   H='Content-Type: application/json'
   ```
   
   1. Create a Nouveau index on a db with docs A, B and C:
      ```sh
      curl -X PUT "$C/db?q=1"
      curl -X PUT "$C/db/_design/x" -H "$H" -d 
'{"nouveau":{"idx":{"index":"function(doc){ if (doc.v) { index(\"string\", 
\"v\", doc.v); } }"}}}'
      A=$(curl -s -X PUT "$C/db/A" -H "$H" -d '{"v":"a"}' | jq -r .rev)
      B=$(curl -s -X PUT "$C/db/B" -H "$H" -d '{"v":"b"}' | jq -r .rev)
      curl -X PUT "$C/db/C" -H "$H" -d '{"v":"c"}'
      curl "$C/db/_design/x/_nouveau/idx?q=*:*"     # total_hits: 3
      ```
   2. Purge A, then purge B:
      ```sh
      curl -X POST "$C/db/_purge" -H "$H" -d "{\"A\":[\"$A\"]}"
      curl -X POST "$C/db/_purge" -H "$H" -d "{\"B\":[\"$B\"]}"
      ```
   3. Before the index is queried again, replicate B and then A back with a new 
revision (as a client pushing an edit would):
      ```sh
      curl -X POST "$C/db/_bulk_docs" -H "$H" -d 
'{"new_edits":false,"docs":[{"_id":"B","_rev":"2-bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb","_revisions":{"start":2,"ids":["bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb","'"${B#1-}"'"]},"v":"b2"}]}'
      curl -X POST "$C/db/_bulk_docs" -H "$H" -d 
'{"new_edits":false,"docs":[{"_id":"A","_rev":"2-aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","_revisions":{"start":2,"ids":["aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa","'"${A#1-}"'"]},"v":"a2"}]}'
      ```
   4. Query the index:
      ```
      $ curl "$C/db/_design/x/_nouveau/idx?q=*:*"
      {"error":"{badmatch,{error,{conflict,<<\"Index updates applied in the 
wrong order (current seq: 8, match seq: 8, attempted seq: 7)\">>}}}", ...}
      ```
      Log:
      ```
      nouveau_index_manager: db:db ddoc:_design/x index:idx failed with: 
{{badmatch,{error,{conflict,<<"Index updates applied in the wrong order 
(current seq: 8, match seq: 8, attempted seq: 
7)">>}}},[{nouveau_index_updater,purge_index,3,[{file,"src/nouveau_index_updater.erl"},{line,291}]},{nouveau_index_updater,update,1,[{file,"src/nouveau_index_updater.erl"},{line,97}]}]}
      ```
   5. (Optional) Restart CouchDB and Nouveau and query again: it still fails.
   
   If the index is queried or updated by ken between steps 2 and 3, there's no 
error and A and B are indexed normally.
   
   The same thing happens with a no-op purge: purging a live, already indexed 
doc by a revision that is no longer a leaf.
   
   ### Possible Cause
   
   In `nouveau_index_updater:purge_index`, a purge info whose doc still exists 
is re-indexed with the [doc's *current* 
seq](https://github.com/apache/couchdb/blob/801f61773f25c16bd9b140154b8163d8df0f78ff/src/nouveau/src/nouveau_index_updater.erl#L274),
 and [`index_update_seq` is advanced to 
it](https://github.com/apache/couchdb/blob/801f61773f25c16bd9b140154b8163d8df0f78ff/src/nouveau/src/nouveau_index_updater.erl#L280).
 Purge infos are processed in purge order, but re-created docs' seqs don't 
follow that order, so the second update has a lower seq than the first and 
Nouveau rejects it.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to