Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hide shard-sync and purge documents from _local_docs #4432

Merged
merged 4 commits into from
Feb 24, 2023

Conversation

ckruse
Copy link

@ckruse ckruse commented Feb 19, 2023

It hides the shard-sync and purge documents from the response unless a ?include_system=true is given.

Overview

Currently the system docs are included in _local_docs. This has shown to confuse users, and they try to delete them, which might fail and/or lead to problems.

Testing recommendations

curl -X PUT http:https://admin:[email protected]:15984/test-db
curl -X PUT http:https://admin:[email protected]:15984/test-db/doc -d '{"a":1}'
curl -X POST http:https://admin:[email protected]:15984/test-db/_purge -d '{"doc":["1-23202479633c2b380f79507a776743d5"]}' -Hcontent-type:application/json

sleep 5

# this shouldn't include any system documents
curl http:https://admin:[email protected]:15984/test-db/_local_docs

# this should include the system documents
curl http:https://admin:[email protected]:15984/test-db/_local_docs?include_system=true

Related Issues or Pull Requests

Checklist

I think most of this is not applicable. Regarding tests, I'm not sure if there should be added tests for this. It would mean a sleep in a test, wouldn't it?

  • Code is written and works correctly
  • Changes are covered by tests
  • Any new configurable parameters are documented in rel/overlay/etc/default.ini
  • Documentation changes were made in the src/docs folder
  • Documentation changes were backported (separated PR) to affected branches

@ckruse ckruse force-pushed the feature/hide-system-docs-in-_local branch from 13ba443 to 94f78bb Compare February 19, 2023 08:56
Copy link
Contributor

@nickva nickva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution @ckruse! That looks much simpler and more elegant than expected.

One worry I had was that there is a tiny chance some users might actually have _local/purge-something... docs. But it seems we can filter those out by inspecting the doc bodies, I suggested a way to do it, what do you think about it?

It would be nice to also cover these changes by a test. Testing is tricky because we don't really have multi-node tests. One approach we could take is to just start with a single node test and insert a few local docs that look like those system docs and see how they are filtered out.

We could copy-paste and modify an existing chttpd test like: https://github.com/apache/couchdb/blob/main/src/chttpd/test/eunit/chttpd_revs_diff_tests.erl

Then insert a few docs like :

 {
   "_id": "_local/shard-sync-sUYbrDRqi_Mfo8dD6-OGow-At83GdaY-DBmWWzFL89R1g",
   "_rev": "0-1",
   "seq": 1,
   "target_uuid": "4f3eb3181db904d62176fb81626f84d0"
    }
{
 "_id": "_local/purge-mrview-0e22fbf4544d9ee72b82a82161d4bbc6",
 "_rev": "0-1",
  "ddoc_id": "_design/ddoc1",
   "purge_seq": 0,
   "signature": [1,2,3], 
   "type": "mrview",
   "updated_on": 1676995502
 }

And some regular and non-system local docs and see how include_system= behaves.

src/couch_mrview/src/couch_mrview.erl Outdated Show resolved Hide resolved
@ckruse
Copy link
Author

ckruse commented Feb 21, 2023

In my tests there is no <<"signature">> field in purge docs. But besides that, I like your approach! 👍 what do you think of this variant?

All other fields don't seem to be particularly suitable to me:

[{<<"type">>,<<"internal_replication">>},{<<"updated_on">>,1677008682},{<<"purge_seq">>,12},{<<"source">>,<<"[email protected]">>},{<<"target">>,<<"[email protected]">>},{<<"range">>,undefined}]

Regarding tests: will add them tomorrow according to your suggestions. 👍 Thank you very much!

@nickva
Copy link
Contributor

nickva commented Feb 21, 2023

In my tests there is no <<"signature">> field in purge docs. But besides that, I like your approach! 👍 what do you think of this variant?

Looks great, I hadn't considered internal replication purge checkpoints. "purge_seq" should be enough to avoid accidents, I think.

Also you named the function better than I did. Your version is more descriptive.

Copy link
Member

@rnewson rnewson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I understand the intention but I worry about how incomplete it is. Those local docs can still be fetched, updated, deleted, if you know the ids. Just scrubbing them from the /dbname/_local_docs response is a partial fix.

The PR is careful to do its best to confirm that the docs are of the type we want to skip, by looking into the doc properties. This reminded me that actually nothing stops a user with sufficient privilege from writing a doc of these two forms (not that it would be wise to do so).

It's regrettable we used local docs for truly internal things that users shouldn't see, the fix might require us to address that properly (another btree for 'sysdocs', say) or to accept the messy status quo.

Needs more thought, but it's a good start and thank you for not just raising the issue but having a good stab at fixing it.

src/couch_mrview/src/couch_mrview.erl Show resolved Hide resolved
@ckruse
Copy link
Author

ckruse commented Feb 22, 2023

Currently I am still working on the tests (I don‘t yet understand why my documents don‘t get created), so I am afraid that creating another btree is beyond my abilities. Sorry.

But if you are still interested (I am not sure based on this conversation) I will finish the tests and fix the change requests to make the PR mergable.

@nickva
Copy link
Contributor

nickva commented Feb 22, 2023

Those local docs can still be fetched, updated, deleted, if you know the ids. Just scrubbing them from the /dbname/_local_docs response is a partial fix.

Not really, it's a random chance if reading and writting those would work. They are below the cluster level so doc id hashing isn't guaranteed to land it on a right shard

% http put $DB/db'?q=8'
% http post $DB/db/_bulk_docs docs:='[{}, {}, {}]'
% http $DB/db/_local_docs
      ....
       {
            "id": "_local/shard-sync-lX9QKOM429Elg1zgQVjY2A-k77wY4rMfBIYkHRoBveQXA",
            "key": "_local/shard-sync-lX9QKOM429Elg1zgQVjY2A-k77wY4rMfBIYkHRoBveQXA",
            "value": {
                "rev": "0-1"
            }
        }
     ...
}


% http $DB/db/_local/shard-sync-lX9QKOM429Elg1zgQVjY2A-k77wY4rMfBIYkHRoBveQXA
{
    "error": "not_found",
    "reason": "missing"
}

This reminded me that actually nothing stops a user with sufficient privilege from writing a doc of these two forms (not that it would be wise to do so).

That's plausible a user who can write to the db can randomly destroy or those checkpoint and mess up mem3 replicator or view purges. I think we're just trying to hide the noise from users not necessarily prevent them from manipulating these documents.

@rnewson
Copy link
Member

rnewson commented Feb 23, 2023

@ckruse Please continue. I agree with Nick that it's worthwhile to suppress these items (by default) from casual viewing in the /$db/_local_docs response even if its only a partial solution to the internal use of local documents versus their intended purpose.

The suggestion about a new btree was for the wider team, I agree it would be quite a lot to expect someone new to our codebase to deliver. Should we do that, and change the internal uses to write there instead of into the local_btree, we could then come back and revert this work easily enough (as the items to be suppressed would no longer exist).

This change improves the current situation, I have no objections.

@nickva
Copy link
Contributor

nickva commented Feb 23, 2023

@rnewson good point, these checkpoints and internal docs would ideally live in a separate btree

@ckruse don't hesitate to reach out for help with tests, they can be a bit daunting to figure out. Feel free to drop by our Slack (https://couchdb.apache.org/#slack) #dev channel. My username there vatamane.

Christian Kruse added 2 commits February 24, 2023 22:15
It hides the shard-sync and purge documents from the response unless a `?include_system=true` is given.

Fixes apache#3930
@ckruse ckruse force-pushed the feature/hide-system-docs-in-_local branch from 14188cd to cf8c0b7 Compare February 24, 2023 21:19
@ckruse
Copy link
Author

ckruse commented Feb 24, 2023

Ok, I guess this PR is ready for review…?

@ckruse ckruse marked this pull request as ready for review February 24, 2023 21:20
@nickva
Copy link
Contributor

nickva commented Feb 24, 2023

@ckruse almost there! Thanks for writting the test. Let's move it to a separate test module and then I think it will be good to go. Make a new module chttpd_local_docs_tests.erl and move just your suite and needed helper functions to it.

@ckruse ckruse force-pushed the feature/hide-system-docs-in-_local branch from cf8c0b7 to bece90d Compare February 24, 2023 21:33
@ckruse
Copy link
Author

ckruse commented Feb 24, 2023

Ok, makes sense. Done :-)

Copy link
Contributor

@nickva nickva left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1

Thank you for your contribution @ckruse

@nickva nickva merged commit ac600d1 into apache:main Feb 24, 2023
@ckruse ckruse deleted the feature/hide-system-docs-in-_local branch February 24, 2023 22:08
@ckruse
Copy link
Author

ckruse commented Feb 24, 2023

🥳 thanks for merging! And thanks for your support!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants