← Back to Pull requests

tofieldset: Add tests to show it already allow duplicates #249

Merged
apelisse merged 2 files (43 lines) on Oct 06, 2023 20:00 UTC
View on GitHub
apelisse apelisse commented on Sep 27, 2023 22:19 UTC
Author

Continuation of #247.

This uses the pattern that I've generally used to solve the problem, which is to not try to be too smart about duplicate entries, and just record the path element for the whole thing, rather than try to track all the fields within. It's not super relevant because no one should ever be able to apply duplicates, so they wouldn't really have conflicts with sub-fields anyway, we've decided that they should have a conflict with the idea of "duplicates" (de-duplicating is a conflict).

/assign @alexzielenski

Diff: 43 lines (+40 / -3) across 2 files Reviewer: apelisse Review duration: 12820m
All Vouch review confidence checks passed
Residual risk 0.07 = Change Risk (0.41) × (1 − Review Confidence (0.83)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.41 · 43 lines diff across 2 files
0.41
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.89 · 5 substantive comment(s)
0.89
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
83%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.226
Self Review self_review
0.140
Real PR Conversation Timeline
27 items
k8s-ci-robot k8s-ci-robot commented on Sep 27, 2023 22:19 UTC
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: <a href="https://github.com/kubernetes-sigs/structured-merge-diff/pull/249#" title="Author self-approved">apelisse</a>

The full list of commands accepted by this bot can be found here.

The pull request process is described here

<details >
Needs approval from an approver in each of these files:

  • ~~OWNERS~~ [apelisse]
  • Approvers can indicate their approval by writing /approve in a comment
    Approvers can cancel approval by writing /approve cancel in a comment
    </details>
    <!-- META={"approvers":[]} -->

    apelisse apelisse commented on code on Sep 27, 2023 22:40 UTC
    Code Review
    typed/toset_test.go:1
    @@ -244,6 +249,16 @@ var fieldsetCases = []fieldsetTestCase{{
     			_P("list", _KBF("key", "b", "id", 1), "key"),
     			_P("list", _KBF("key", "b", "id", 1), "id"),
     		)},
    +		{`{"list":[{"key":"a","id":1,"nv":2},{"key":"a","id":2,"nv":3},{"key":"b","id":1},{"key":"a","id":2,"bv":true}]}`, _NS(
    +			_P("list", _KBF("key", "a", "id", 1)),
    +			_P("list", _KBF("key", "a", "id", 1), "key"),
    +			_P("list", _KBF("key", "a", "id", 1), "id"),
    +			_P("list", _KBF("key", "a", "id", 1), "nv"),
    +			_P("list", _KBF("key", "a", "id", 2)),

    cc @jpbetz
    This code is mostly irrelevant because we only use ToFieldSet on objects that are applied, and we should never allow apply to have duplicates (now, that being said, I'm curious if this whole change even makes sense?). I still wanted to illustrate that when someone will update with duplicates items, we will just give them ownership of the associative key and that's it, as a way to indicate that they own that key multiple times. WDYT?

    alexzielenski alexzielenski commented on code on Sep 27, 2023 22:43 UTC
    Code Review
    typed/tofieldset.go:1
    @@ -94,9 +94,31 @@ func (v *toFieldSetWalker) doScalar(t *schema.Scalar) ValidationErrors {
     }
     
     func (v *toFieldSetWalker) visitListItems(t *schema.List, list value.List) (errs ValidationErrors) {
    +	// Keeps track of the PEs we've seen
    +	seen := fieldpath.MakePathElementSet(list.Length())
    +	// Keeps tracks of the PEs we've counted as duplicates
    +	duplicates := fieldpath.MakePathElementSet(list.Length())

    I would've expected the implementation to use just one set and one loop; if seen.Has(pe) { continue } Is this doing something other than skipping over the duplicates?

    apelisse apelisse commented on code on Sep 27, 2023 22:49 UTC
    Code Review
    typed/tofieldset.go:1
    @@ -94,9 +94,31 @@ func (v *toFieldSetWalker) doScalar(t *schema.Scalar) ValidationErrors {
     }
     
     func (v *toFieldSetWalker) visitListItems(t *schema.List, list value.List) (errs ValidationErrors) {
    +	// Keeps track of the PEs we've seen
    +	seen := fieldpath.MakePathElementSet(list.Length())
    +	// Keeps tracks of the PEs we've counted as duplicates
    +	duplicates := fieldpath.MakePathElementSet(list.Length())

    we're trying to add duplicates exactly once to the set. I need to know if I've already seen them to know if they are duplicates, and I need to know if I've already added them to the set. But maybe I can use the v.set actually. Or even add it multiple times since I'm adding to a set.

    apelisse apelisse commented on Sep 27, 2023 22:51 UTC
    Author

    /hold

    I'm trying a new test with pruning fields that are removed by appliers and this doesn't work, looking into this.

    alexzielenski alexzielenski commented on code on Sep 27, 2023 22:53 UTC
    Code Review
    typed/tofieldset.go:1
    @@ -94,9 +94,31 @@ func (v *toFieldSetWalker) doScalar(t *schema.Scalar) ValidationErrors {
     }
     
     func (v *toFieldSetWalker) visitListItems(t *schema.List, list value.List) (errs ValidationErrors) {
    +	// Keeps track of the PEs we've seen
    +	seen := fieldpath.MakePathElementSet(list.Length())
    +	// Keeps tracks of the PEs we've counted as duplicates
    +	duplicates := fieldpath.MakePathElementSet(list.Length())

    I guess Im proposing the following pseudocode:

    seen := NewSet()
    for item in list {
        if seen.Has(item) { continue }
        seen.Insert(item)
        listWithoutDuplicates.append(item)
    }
    

    Doesn't this do as you described?

    apelisse apelisse commented on code on Sep 27, 2023 22:54 UTC
    Code Review
    typed/tofieldset.go:1
    @@ -94,9 +94,31 @@ func (v *toFieldSetWalker) doScalar(t *schema.Scalar) ValidationErrors {
     }
     
     func (v *toFieldSetWalker) visitListItems(t *schema.List, list value.List) (errs ValidationErrors) {
    +	// Keeps track of the PEs we've seen
    +	seen := fieldpath.MakePathElementSet(list.Length())
    +	// Keeps tracks of the PEs we've counted as duplicates
    +	duplicates := fieldpath.MakePathElementSet(list.Length())

    Yeah, I think that makes sense, thanks.

    apelisse apelisse commented on code on Oct 04, 2023 21:49 UTC
    Code Review
    typed/toset_test.go:1
    @@ -244,6 +249,16 @@ var fieldsetCases = []fieldsetTestCase{{
     			_P("list", _KBF("key", "b", "id", 1), "key"),
     			_P("list", _KBF("key", "b", "id", 1), "id"),
     		)},
    +		{`{"list":[{"key":"a","id":1,"nv":2},{"key":"a","id":2,"nv":3},{"key":"b","id":1},{"key":"a","id":2,"bv":true}]}`, _NS(
    +			_P("list", _KBF("key", "a", "id", 1)),
    +			_P("list", _KBF("key", "a", "id", 1), "key"),
    +			_P("list", _KBF("key", "a", "id", 1), "id"),
    +			_P("list", _KBF("key", "a", "id", 1), "nv"),
    +			_P("list", _KBF("key", "a", "id", 2)),

    One drawback of this, but I don't know if we can solve it anyway, is that if someone applies one item, and then it gets updated into two items, that item won't be pruned when it's removed from the applied configuration, meaning it can no longer be removed with server-side apply. I think that's reasonable.

    apelisse apelisse commented on Oct 05, 2023 23:29 UTC
    Author

    /hold cancel

    Thought about this further, I think it's fine, unless I forgot the use-case I had in mind ...

    alexzielenski alexzielenski commented on code on Oct 06, 2023 16:11 UTC
    Code Review
    typed/toset_test.go:1
    @@ -244,6 +249,16 @@ var fieldsetCases = []fieldsetTestCase{{
     			_P("list", _KBF("key", "b", "id", 1), "key"),
     			_P("list", _KBF("key", "b", "id", 1), "id"),
     		)},
    +		{`{"list":[{"key":"a","id":1,"nv":2},{"key":"a","id":2,"nv":3},{"key":"b","id":1},{"key":"a","id":2,"bv":true}]}`, _NS(
    +			_P("list", _KBF("key", "a", "id", 1)),
    +			_P("list", _KBF("key", "a", "id", 1), "key"),
    +			_P("list", _KBF("key", "a", "id", 1), "id"),
    +			_P("list", _KBF("key", "a", "id", 1), "nv"),
    +			_P("list", _KBF("key", "a", "id", 2)),

    This code is mostly irrelevant because we only use ToFieldSet on objects that are applied, and we should never allow apply to have duplicates (now, that being said, I'm curious if this whole change even makes sense?)

    If apply can never have duplicates why are we making SMD ToFieldSet tolerate duplicates? How do Updates get the list of fields to own if not via ToFieldSet?

    alexzielenski alexzielenski commented on code on Oct 06, 2023 16:12 UTC
    Code Review
    typed/toset_test.go:1
    @@ -244,6 +249,16 @@ var fieldsetCases = []fieldsetTestCase{{
     			_P("list", _KBF("key", "b", "id", 1), "key"),
     			_P("list", _KBF("key", "b", "id", 1), "id"),
     		)},
    +		{`{"list":[{"key":"a","id":1,"nv":2},{"key":"a","id":2,"nv":3},{"key":"b","id":1},{"key":"a","id":2,"bv":true}]}`, _NS(
    +			_P("list", _KBF("key", "a", "id", 1)),
    +			_P("list", _KBF("key", "a", "id", 1), "key"),
    +			_P("list", _KBF("key", "a", "id", 1), "id"),
    +			_P("list", _KBF("key", "a", "id", 1), "nv"),
    +			_P("list", _KBF("key", "a", "id", 2)),

    if someone applies one item, and then it gets updated into two items, that item won't be pruned when it's removed from the applied configuration, meaning it can no longer be removed with server-side apply.

    Why wouldn't it be pruned?

    alexzielenski alexzielenski commented on code on Oct 06, 2023 16:12 UTC
    Code Review
    typed/tofieldset.go:1
    @@ -94,9 +94,31 @@ func (v *toFieldSetWalker) doScalar(t *schema.Scalar) ValidationErrors {
     }
     
     func (v *toFieldSetWalker) visitListItems(t *schema.List, list value.List) (errs ValidationErrors) {
    +	// Keeps track of the PEs we've seen
    +	seen := fieldpath.MakePathElementSet(list.Length())
    +	// Keeps tracks of the PEs we've counted as duplicates
    +	duplicates := fieldpath.MakePathElementSet(list.Length())

    can mark this as resolved, since after discussion offline it is not what you want

    alexzielenski alexzielenski commented on Oct 06, 2023 16:14 UTC
    Reviewer

    Just to clarify, this change makes SMD give ownership of the entire object if there are duplicates in a list map?

    I'm having a hard time tracking the "before" behavior and the "after" behavior.

    apelisse apelisse commented on code on Oct 06, 2023 16:19 UTC
    Code Review
    typed/toset_test.go:1
    @@ -244,6 +249,16 @@ var fieldsetCases = []fieldsetTestCase{{
     			_P("list", _KBF("key", "b", "id", 1), "key"),
     			_P("list", _KBF("key", "b", "id", 1), "id"),
     		)},
    +		{`{"list":[{"key":"a","id":1,"nv":2},{"key":"a","id":2,"nv":3},{"key":"b","id":1},{"key":"a","id":2,"bv":true}]}`, _NS(
    +			_P("list", _KBF("key", "a", "id", 1)),
    +			_P("list", _KBF("key", "a", "id", 1), "key"),
    +			_P("list", _KBF("key", "a", "id", 1), "id"),
    +			_P("list", _KBF("key", "a", "id", 1), "nv"),
    +			_P("list", _KBF("key", "a", "id", 2)),

    If apply can never have duplicates why are we making SMD ToFieldSet tolerate duplicates?

    Context, we're trying to solve #234

    For built-ins, the listType=map is used a little loosely, and we don't have a great way to address that, except for accepting some duplicates. We still can't apply duplicates, but right now you can't apply an objects that has these duplicates.

    How do Updates get the list of fields to own if not via ToFieldSet?

    By using "Compare", they own the fields that they changed, since we don't know exactly what they intended to own.

    apelisse apelisse commented on code on Oct 06, 2023 16:20 UTC
    Code Review
    typed/toset_test.go:1
    @@ -244,6 +249,16 @@ var fieldsetCases = []fieldsetTestCase{{
     			_P("list", _KBF("key", "b", "id", 1), "key"),
     			_P("list", _KBF("key", "b", "id", 1), "id"),
     		)},
    +		{`{"list":[{"key":"a","id":1,"nv":2},{"key":"a","id":2,"nv":3},{"key":"b","id":1},{"key":"a","id":2,"bv":true}]}`, _NS(
    +			_P("list", _KBF("key", "a", "id", 1)),
    +			_P("list", _KBF("key", "a", "id", 1), "key"),
    +			_P("list", _KBF("key", "a", "id", 1), "id"),
    +			_P("list", _KBF("key", "a", "id", 1), "nv"),
    +			_P("list", _KBF("key", "a", "id", 2)),

    Because when you update to multiple items, it steals the ownership of the whole item and we don't prune things that you don't have ownership over.

    alexzielenski alexzielenski commented on code on Oct 06, 2023 16:23 UTC
    Code Review
    typed/toset_test.go:1
    @@ -244,6 +249,16 @@ var fieldsetCases = []fieldsetTestCase{{
     			_P("list", _KBF("key", "b", "id", 1), "key"),
     			_P("list", _KBF("key", "b", "id", 1), "id"),
     		)},
    +		{`{"list":[{"key":"a","id":1,"nv":2},{"key":"a","id":2,"nv":3},{"key":"b","id":1},{"key":"a","id":2,"bv":true}]}`, _NS(
    +			_P("list", _KBF("key", "a", "id", 1)),
    +			_P("list", _KBF("key", "a", "id", 1), "key"),
    +			_P("list", _KBF("key", "a", "id", 1), "id"),
    +			_P("list", _KBF("key", "a", "id", 1), "nv"),
    +			_P("list", _KBF("key", "a", "id", 2)),

    We still can't apply duplicates, but right now you can't apply an objects that has these duplicates.

    Is ToFieldSet invoked on the final merged object, or the partial patch? If it is invoked on the partial patch aren't there no duplicates and this PR has no effect?

    alexzielenski alexzielenski commented on code on Oct 06, 2023 16:28 UTC
    Code Review
    typed/toset_test.go:1
    @@ -244,6 +249,16 @@ var fieldsetCases = []fieldsetTestCase{{
     			_P("list", _KBF("key", "b", "id", 1), "key"),
     			_P("list", _KBF("key", "b", "id", 1), "id"),
     		)},
    +		{`{"list":[{"key":"a","id":1,"nv":2},{"key":"a","id":2,"nv":3},{"key":"b","id":1},{"key":"a","id":2,"bv":true}]}`, _NS(
    +			_P("list", _KBF("key", "a", "id", 1)),
    +			_P("list", _KBF("key", "a", "id", 1), "key"),
    +			_P("list", _KBF("key", "a", "id", 1), "id"),
    +			_P("list", _KBF("key", "a", "id", 1), "nv"),
    +			_P("list", _KBF("key", "a", "id", 2)),

    it steals the ownership of the whole item and we don't prune things that you don't have ownership over.

    It sounds like you do own it if it steals ownership of the whole item? Do you only have ownership of one copy? Can you share example of the behavior for this. I would expect that you have ownerhsip of the whole duplicated field, and when you submit an UPDATE or PATCH that does not include it, then no one has ownership, and it is pruned.

    apelisse apelisse commented on code on Oct 06, 2023 16:28 UTC
    Code Review
    typed/toset_test.go:1
    @@ -244,6 +249,16 @@ var fieldsetCases = []fieldsetTestCase{{
     			_P("list", _KBF("key", "b", "id", 1), "key"),
     			_P("list", _KBF("key", "b", "id", 1), "id"),
     		)},
    +		{`{"list":[{"key":"a","id":1,"nv":2},{"key":"a","id":2,"nv":3},{"key":"b","id":1},{"key":"a","id":2,"bv":true}]}`, _NS(
    +			_P("list", _KBF("key", "a", "id", 1)),
    +			_P("list", _KBF("key", "a", "id", 1), "key"),
    +			_P("list", _KBF("key", "a", "id", 1), "id"),
    +			_P("list", _KBF("key", "a", "id", 1), "nv"),
    +			_P("list", _KBF("key", "a", "id", 2)),

    Correct, that's more or less what I said in the PR body:

    It's not super relevant because no one should ever be able to apply duplicates

    But:
    1. This is for consistency, if we agree that this is how ownership of things that are duplicates, we should probably apply it everywhere, even if it's not relevant here.
    2. This is the first time we use that new pattern in the code so I want to make sure we're fine with it.

    apelisse apelisse commented on code on Oct 06, 2023 16:30 UTC
    Code Review
    typed/toset_test.go:1
    @@ -244,6 +249,16 @@ var fieldsetCases = []fieldsetTestCase{{
     			_P("list", _KBF("key", "b", "id", 1), "key"),
     			_P("list", _KBF("key", "b", "id", 1), "id"),
     		)},
    +		{`{"list":[{"key":"a","id":1,"nv":2},{"key":"a","id":2,"nv":3},{"key":"b","id":1},{"key":"a","id":2,"bv":true}]}`, _NS(
    +			_P("list", _KBF("key", "a", "id", 1)),
    +			_P("list", _KBF("key", "a", "id", 1), "key"),
    +			_P("list", _KBF("key", "a", "id", 1), "id"),
    +			_P("list", _KBF("key", "a", "id", 1), "nv"),
    +			_P("list", _KBF("key", "a", "id", 2)),

    Using sets, since it's a little simpler:
    Apply, "applier": [1]
    Update, "updater": [1, 1] # Updater steals ownership of 1
    Apply, "applier": [] # 1 is not pruned because it's owned by updater.
    Final object: [1, 1]

    alexzielenski alexzielenski commented on code on Oct 06, 2023 16:40 UTC
    Code Review
    typed/toset_test.go:1
    @@ -244,6 +249,16 @@ var fieldsetCases = []fieldsetTestCase{{
     			_P("list", _KBF("key", "b", "id", 1), "key"),
     			_P("list", _KBF("key", "b", "id", 1), "id"),
     		)},
    +		{`{"list":[{"key":"a","id":1,"nv":2},{"key":"a","id":2,"nv":3},{"key":"b","id":1},{"key":"a","id":2,"bv":true}]}`, _NS(
    +			_P("list", _KBF("key", "a", "id", 1)),
    +			_P("list", _KBF("key", "a", "id", 1), "key"),
    +			_P("list", _KBF("key", "a", "id", 1), "id"),
    +			_P("list", _KBF("key", "a", "id", 1), "nv"),
    +			_P("list", _KBF("key", "a", "id", 2)),

    That seems like correct behavior to me. What if it was a map typed list with duplicate keys but unequal objects?

    Apply, "applier": [ObjWithKeyA]
    Update, "updater": [ObjWithKeyAButSlighlyDifferent, ObjWithKeyAButAlsoDifferent] # Should be three copies of 'A'?
    Apply, "applier": [] # is A pruned?
    Whats the final object

    apelisse apelisse commented on code on Oct 06, 2023 17:57 UTC
    Code Review
    typed/toset_test.go:1
    @@ -244,6 +249,16 @@ var fieldsetCases = []fieldsetTestCase{{
     			_P("list", _KBF("key", "b", "id", 1), "key"),
     			_P("list", _KBF("key", "b", "id", 1), "id"),
     		)},
    +		{`{"list":[{"key":"a","id":1,"nv":2},{"key":"a","id":2,"nv":3},{"key":"b","id":1},{"key":"a","id":2,"bv":true}]}`, _NS(
    +			_P("list", _KBF("key", "a", "id", 1)),
    +			_P("list", _KBF("key", "a", "id", 1), "key"),
    +			_P("list", _KBF("key", "a", "id", 1), "id"),
    +			_P("list", _KBF("key", "a", "id", 1), "nv"),
    +			_P("list", _KBF("key", "a", "id", 2)),

    Very good point.

    So first of all, Update replaces the whole list, so if you do

    Update, "updater": [ObjWithKeyAButSlighlyDifferent, ObjWithKeyAButAlsoDifferent] # Should be three copies of 'A'?

    you end-up with that exact object.

    Second, the way this is meant to work, is that if you have duplicate, you own the key and the key only, so here's what happens in your scenario:

    Apply, "applier": [ObjWithKeyA] -> applier owns ObjWithKeyA
    Update, "updater": [ObjWithKeyAButSlighlyDifferent, ObjWithKeyAButAlsoDifferent] -> duplicates, updater owns just "KeyA" (none of the fields)
    Apply, "applier": [] # is A pruned? no
    Whats the final object
    [ObjWithKeyAButSlighlyDifferent, ObjWithKeyAButAlsoDifferent] with KeyA owned by updater.

    alexzielenski alexzielenski commented on code on Oct 06, 2023 17:59 UTC
    Code Review
    typed/toset_test.go:1
    @@ -244,6 +249,16 @@ var fieldsetCases = []fieldsetTestCase{{
     			_P("list", _KBF("key", "b", "id", 1), "key"),
     			_P("list", _KBF("key", "b", "id", 1), "id"),
     		)},
    +		{`{"list":[{"key":"a","id":1,"nv":2},{"key":"a","id":2,"nv":3},{"key":"b","id":1},{"key":"a","id":2,"bv":true}]}`, _NS(
    +			_P("list", _KBF("key", "a", "id", 1)),
    +			_P("list", _KBF("key", "a", "id", 1), "key"),
    +			_P("list", _KBF("key", "a", "id", 1), "id"),
    +			_P("list", _KBF("key", "a", "id", 1), "nv"),
    +			_P("list", _KBF("key", "a", "id", 2)),

    i think thats reasonable

    alexzielenski alexzielenski commented on Oct 06, 2023 18:47 UTC
    Contributor

    LGTM from me. I think you mentioned you wanted @jpbetz input so will defer tag

    k8s-ci-robot k8s-ci-robot commented on Oct 06, 2023 19:55 UTC
    Contributor

    @alexzielenski: Closed this PR.

    <details>

    In response to this:

    >/close

    Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.
    </details>

    k8s-ci-robot k8s-ci-robot commented on Oct 06, 2023 19:55 UTC
    Contributor

    @alexzielenski: Reopened this PR.

    <details>

    In response to this:

    >/reopen

    Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.
    </details>

    alexzielenski alexzielenski commented on Oct 06, 2023 19:55 UTC
    Contributor

    whoops. brain misfire

    /lgtm

    Reviewers
    Assignees
    apelisse apelisse
    Labels
    risk: low confidence: 83.1% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 41.1% (MEDIUM) · Higher = larger blast radius
    41.1%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 83.1% · Lower = rushed or shallow review
    83.1%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 6.9% · Safe to merge
    6.9%