← Back to Pull requests

typed: Allow duplicates when we merge #253

Merged
apelisse merged 2 files (112 lines) on Oct 20, 2023 02:38 UTC
View on GitHub
apelisse apelisse commented on Oct 13, 2023 02:57 UTC
Author

The goal is to ignore duplicates if they are not included in the partial object, and to replace all of the occurences if they are in the partial object.

/assign @liggitt
/cc @alexzielenski

Diff: 112 lines (+90 / -22) across 2 files Reviewer: apelisse Review duration: 10061m
All Vouch review confidence checks passed
Residual risk 0.10 = Change Risk (0.47) × (1 − Review Confidence (0.79)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.47 · 112 lines diff across 2 files
0.47
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%
79%
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.283
Self Review self_review
0.140
Real PR Conversation Timeline
23 items
k8s-ci-robot k8s-ci-robot commented on Oct 13, 2023 02:57 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/253#" 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 Oct 13, 2023 03:03 UTC
    Code Review
    typed/merge_test.go:1
    @@ -423,6 +467,18 @@ var mergeCases = []mergeTestCase{{
     		`{"atomicList":["a","a","a"]}`,
     		`{"atomicList":["a","a"]}`,
     		`{"atomicList":["a","a"]}`,
    +	}, {
    +		`{"list":[{"key":"a","id":1,"bv":true},{"key":"b","id":2},{"key":"a","id":1,"bv":false,"nv":2}]}`,
    +		`{"list":[{"key":"a","id":1,"nv":3}, {"key":"c","id":3},{"key":"b","id":2}]}`,
    +		`{"list":[{"key":"a","id":1,"bv":true,"nv":3},{"key":"c","id":3},{"key":"b","id":2}]}`,

    So this is merging the new item with the first occurence, I'm currently thinking we should replace altogether.

    alexzielenski alexzielenski commented on code on Oct 16, 2023 22:33 UTC
    Code Review
    typed/merge.go:1
    @@ -222,29 +228,33 @@ func (w *mergingWalker) visitListItems(t *schema.List, lhs, rhs value.List) (err
     				}
     				continue
     			}
    -			if _, ok := observedRHS.Get(pe); ok && nextShared != nil && !nextShared.Equals(lhsOrder[lI]) {
    +			if _, ok := observedRHS.Get(pe); ok && nextShared != nil && !nextShared.Equals(lhsPEs[lI]) {
     				// shared item, but not the one we want in this round
     				lI++
     				continue
     			}
     		}
     		if lI < lLen {
    -			pe := lhsOrder[lI]
    +			pe := lhsPEs[lI]
     			if _, ok := observedRHS.Get(pe); !ok {
     				// take LHS item
    -				lChild, _ := observedLHS.Get(pe)
    +				lChild := lhs.AtUsing(w.allocator, lI)

    This seems equivalent, since we are looking LHS first. Just curious why the change? Is it a noop?

    alexzielenski alexzielenski commented on code on Oct 16, 2023 22:34 UTC
    Code Review
    typed/merge.go:1
    @@ -290,11 +300,9 @@ func (w *mergingWalker) indexListPathElements(t *schema.List, list value.List) (
     			// this element.
     			continue
     		}
    -		if _, found := observed.Get(pe); found {
    -			errs = append(errs, errorf("duplicate entries for key %v", pe.String())...)
    -			continue
    +		if _, found := observed.Get(pe); !found {
    +			observed.Insert(pe, child)

    Is this existence check necessary? Is observed a set?

    alexzielenski alexzielenski commented on code on Oct 16, 2023 22:39 UTC
    Code Review
    typed/merge.go:1
    @@ -222,29 +228,33 @@ func (w *mergingWalker) visitListItems(t *schema.List, lhs, rhs value.List) (err
     				}
     				continue
     			}
    -			if _, ok := observedRHS.Get(pe); ok && nextShared != nil && !nextShared.Equals(lhsOrder[lI]) {
    +			if _, ok := observedRHS.Get(pe); ok && nextShared != nil && !nextShared.Equals(lhsPEs[lI]) {
     				// shared item, but not the one we want in this round
     				lI++
     				continue
     			}
     		}
     		if lI < lLen {
    -			pe := lhsOrder[lI]
    +			pe := lhsPEs[lI]
     			if _, ok := observedRHS.Get(pe); !ok {
     				// take LHS item
    -				lChild, _ := observedLHS.Get(pe)
    +				lChild := lhs.AtUsing(w.allocator, lI)
     				mergeOut, errs := w.mergeListItem(t, pe, lChild, nil)
     				errs = append(errs, errs...)
     				if mergeOut != nil {
     					out = append(out, *mergeOut)
     				}
     				lI++
     				continue
    +			} else if _, ok := mergedRHS.Get(pe); ok {
    +				// we've already merged it with RHS, we don't want to duplicate it, skip it.

    I'm not sure why this case is necessary. It's hard to track the invariants of observedRHS vs mergedRHS without going very deep into the code.

    alexzielenski alexzielenski commented on code on Oct 16, 2023 22:42 UTC
    Code Review
    typed/merge.go:1
    @@ -180,14 +180,18 @@ func (w *mergingWalker) visitListItems(t *schema.List, lhs, rhs value.List) (err
     	}
     	out := make([]interface{}, 0, outLen)
     
    -	rhsOrder, observedRHS, rhsErrs := w.indexListPathElements(t, rhs)
    +	rhsPEs, observedRHS, rhsErrs := w.indexListPathElements(t, rhs)

    Are the PE's no longer ordered?

    alexzielenski alexzielenski commented on code on Oct 16, 2023 22:45 UTC
    Code Review
    typed/merge_test.go:1
    @@ -423,6 +467,18 @@ var mergeCases = []mergeTestCase{{
     		`{"atomicList":["a","a","a"]}`,
     		`{"atomicList":["a","a"]}`,
     		`{"atomicList":["a","a"]}`,
    +	}, {
    +		`{"list":[{"key":"a","id":1,"bv":true},{"key":"b","id":2},{"key":"a","id":1,"bv":false,"nv":2}]}`,
    +		`{"list":[{"key":"a","id":1,"nv":3}, {"key":"c","id":3},{"key":"b","id":2}]}`,
    +		`{"list":[{"key":"a","id":1,"bv":true,"nv":3},{"key":"c","id":3},{"key":"b","id":2}]}`,

    This case surprised me. From our prior conversations I would not have expected the merge with one of the duplicates. It doesn't seem like as the author of such a resource I'd be very conscious of which of the duplicates I am merging with.

    Is it possible to have two instances of the same value but they are owned by different field managers? In that case I almost would expect "MY" version of the duplicate to be merged; but don't think that'd be worth the effort.

    alexzielenski alexzielenski commented on Oct 16, 2023 22:51 UTC
    Reviewer

    Except for one tests make a lot of sense and very straightforward.

    TBH I could not gain deep sense of the code changes without obtaining degree in SMD. This code could benefit a lot from some more inline comments about the invariants of this algorithm.

    apelisse apelisse commented on code on Oct 17, 2023 22:39 UTC
    Code Review
    typed/merge.go:1
    @@ -180,14 +180,18 @@ func (w *mergingWalker) visitListItems(t *schema.List, lhs, rhs value.List) (err
     	}
     	out := make([]interface{}, 0, outLen)
     
    -	rhsOrder, observedRHS, rhsErrs := w.indexListPathElements(t, rhs)
    +	rhsPEs, observedRHS, rhsErrs := w.indexListPathElements(t, rhs)

    They are still, but "order" didn't really tell me what they were. I could revert or make it rhsOrderedPEs or something.

    apelisse apelisse commented on code on Oct 17, 2023 22:40 UTC
    Code Review
    typed/merge.go:1
    @@ -222,29 +228,33 @@ func (w *mergingWalker) visitListItems(t *schema.List, lhs, rhs value.List) (err
     				}
     				continue
     			}
    -			if _, ok := observedRHS.Get(pe); ok && nextShared != nil && !nextShared.Equals(lhsOrder[lI]) {
    +			if _, ok := observedRHS.Get(pe); ok && nextShared != nil && !nextShared.Equals(lhsPEs[lI]) {
     				// shared item, but not the one we want in this round
     				lI++
     				continue
     			}
     		}
     		if lI < lLen {
    -			pe := lhsOrder[lI]
    +			pe := lhsPEs[lI]
     			if _, ok := observedRHS.Get(pe); !ok {
     				// take LHS item
    -				lChild, _ := observedLHS.Get(pe)
    +				lChild := lhs.AtUsing(w.allocator, lI)

    Actually, lhs can have multiple times the same PE, so we want to make sure we get the right one (I don't think it's equivalent).

    apelisse apelisse commented on code on Oct 17, 2023 22:44 UTC
    Code Review
    typed/merge.go:1
    @@ -290,11 +300,9 @@ func (w *mergingWalker) indexListPathElements(t *schema.List, list value.List) (
     			// this element.
     			continue
     		}
    -		if _, found := observed.Get(pe); found {
    -			errs = append(errs, errorf("duplicate entries for key %v", pe.String())...)
    -			continue
    +		if _, found := observed.Get(pe); !found {
    +			observed.Insert(pe, child)

    observed is a map from pathelement to the actual value.

    the value that ends-up being in the PE is the one we're going to use to merge I think, so if we were to override here, I think we would end-up merging with the last item in the list rather than the first. But since we've decided we might not want to do that, I will probably deal with that differently.

    apelisse apelisse commented on code on Oct 17, 2023 22:45 UTC
    Code Review
    typed/merge_test.go:1
    @@ -423,6 +467,18 @@ var mergeCases = []mergeTestCase{{
     		`{"atomicList":["a","a","a"]}`,
     		`{"atomicList":["a","a"]}`,
     		`{"atomicList":["a","a"]}`,
    +	}, {
    +		`{"list":[{"key":"a","id":1,"bv":true},{"key":"b","id":2},{"key":"a","id":1,"bv":false,"nv":2}]}`,
    +		`{"list":[{"key":"a","id":1,"nv":3}, {"key":"c","id":3},{"key":"b","id":2}]}`,
    +		`{"list":[{"key":"a","id":1,"bv":true,"nv":3},{"key":"c","id":3},{"key":"b","id":2}]}`,

    Is it possible to have two instances of the same value but they are owned by different field managers?

    No.

    This case surprised me. From our prior conversations I would not have expected the merge with one of the duplicates. It doesn't seem like as the author of such a resource I'd be very conscious of which of the duplicates I am merging with.

    yeah, I think we just need to replace rather than merge, which means I'll need to know if they are duplicated and replace altogether, I'll update the algorithm.

    liggitt liggitt commented on code on Oct 18, 2023 20:20 UTC
    Code Review
    typed/merge.go:1
    @@ -180,14 +180,18 @@ func (w *mergingWalker) visitListItems(t *schema.List, lhs, rhs value.List) (err
     	}

    AI: look for issue reported where someone couldn't update because of managed fields

    edit: this was a note to self unrelated to this PR, was https://github.com/kubernetes/kubernetes/issues/90904

    liggitt liggitt commented on code on Oct 18, 2023 20:36 UTC
    Code Review
    typed/merge.go:1
    @@ -199,12 +203,14 @@ func (w *mergingWalker) visitListItems(t *schema.List, lhs, rhs value.List) (err
     		sharedOrder = sharedOrder[1:]
     	}
     
    -	lLen, rLen = len(lhsOrder), len(rhsOrder)
    +	mergedRHS := fieldpath.MakePathElementMap(len(rhsPEs))
    +	lLen, rLen = len(lhsPEs), len(rhsPEs)
     	for lI, rI := 0, 0; lI < lLen || rI < rLen; {
     		if lI < lLen && rI < rLen {
    -			pe := lhsOrder[lI]
    -			if pe.Equals(rhsOrder[rI]) {
    +			pe := lhsPEs[lI]
    +			if pe.Equals(rhsPEs[rI]) {
     				// merge LHS & RHS items
    +				mergedRHS.Insert(pe, struct{}{})
     				lChild, _ := observedLHS.Get(pe)

    comment that lChild will be nil if the LHS had duplicates for pe

    liggitt liggitt commented on code on Oct 18, 2023 20:38 UTC
    Code Review
    typed/merge.go:1
    @@ -222,29 +228,33 @@ func (w *mergingWalker) visitListItems(t *schema.List, lhs, rhs value.List) (err
     				}
     				continue
     			}
    -			if _, ok := observedRHS.Get(pe); ok && nextShared != nil && !nextShared.Equals(lhsOrder[lI]) {
    +			if _, ok := observedRHS.Get(pe); ok && nextShared != nil && !nextShared.Equals(lhsPEs[lI]) {
     				// shared item, but not the one we want in this round
     				lI++
     				continue
     			}
     		}
     		if lI < lLen {
    -			pe := lhsOrder[lI]
    +			pe := lhsPEs[lI]
     			if _, ok := observedRHS.Get(pe); !ok {
     				// take LHS item

    comment that we use lhs.AtUsing instead of observedLHS because observedLHS can contain nil if the lhs had duplicates

    liggitt liggitt commented on code on Oct 18, 2023 20:48 UTC
    Code Review
    typed/merge.go:1
    @@ -222,29 +228,33 @@ func (w *mergingWalker) visitListItems(t *schema.List, lhs, rhs value.List) (err
     				}
     				continue
     			}
    -			if _, ok := observedRHS.Get(pe); ok && nextShared != nil && !nextShared.Equals(lhsOrder[lI]) {
    +			if _, ok := observedRHS.Get(pe); ok && nextShared != nil && !nextShared.Equals(lhsPEs[lI]) {
     				// shared item, but not the one we want in this round
     				lI++
     				continue
     			}
     		}
     		if lI < lLen {
    -			pe := lhsOrder[lI]
    +			pe := lhsPEs[lI]
     			if _, ok := observedRHS.Get(pe); !ok {
     				// take LHS item
    -				lChild, _ := observedLHS.Get(pe)
    +				lChild := lhs.AtUsing(w.allocator, lI)
     				mergeOut, errs := w.mergeListItem(t, pe, lChild, nil)
     				errs = append(errs, errs...)
     				if mergeOut != nil {
     					out = append(out, *mergeOut)
     				}
     				lI++
     				continue
    +			} else if _, ok := mergedRHS.Get(pe); ok {
    +				// we've already merged it with RHS, we don't want to duplicate it, skip it.
    +				lI++
     			}
     		}
     		if rI < rLen {
     			// Take the RHS item, merge with matching LHS item if possible
    -			pe := rhsOrder[rI]
    +			pe := rhsPEs[rI]
    +			mergedRHS.Insert(pe, struct{}{})
     			lChild, _ := observedLHS.Get(pe) // may be nil

    			lChild, _ := observedLHS.Get(pe) // may be nil if not present or LHS duplicated this item
    

    liggitt liggitt commented on code on Oct 18, 2023 20:51 UTC
    Code Review
    typed/merge.go:1
    @@ -291,10 +301,10 @@ func (w *mergingWalker) indexListPathElements(t *schema.List, list value.List) (
     			continue
     		}
     		if _, found := observed.Get(pe); found {
    -			errs = append(errs, errorf("duplicate entries for key %v", pe.String())...)
    -			continue
    +			observed.Insert(pe, value.NewValueInterface(nil))

    is it possible to only tolerate duplicates making the LHS and keep returning the duplicate error making the RHS?

    liggitt liggitt commented on Oct 18, 2023 20:52 UTC
    Reviewer

    tests look good, the change was shockingly small.

    mostly requests for clearer comments, one request to keep the duplicate-error-returning bit the same when constructing observedRHS

    apelisse apelisse commented on code on Oct 19, 2023 22:43 UTC
    Code Review
    typed/merge.go:1
    @@ -222,29 +228,33 @@ func (w *mergingWalker) visitListItems(t *schema.List, lhs, rhs value.List) (err
     				}
     				continue
     			}
    -			if _, ok := observedRHS.Get(pe); ok && nextShared != nil && !nextShared.Equals(lhsOrder[lI]) {
    +			if _, ok := observedRHS.Get(pe); ok && nextShared != nil && !nextShared.Equals(lhsPEs[lI]) {
     				// shared item, but not the one we want in this round
     				lI++
     				continue
     			}
     		}
     		if lI < lLen {
    -			pe := lhsOrder[lI]
    +			pe := lhsPEs[lI]
     			if _, ok := observedRHS.Get(pe); !ok {
     				// take LHS item
    -				lChild, _ := observedLHS.Get(pe)
    +				lChild := lhs.AtUsing(w.allocator, lI)

    Added a comment for that.

    apelisse apelisse commented on Oct 19, 2023 22:44 UTC
    Author

    PTAL, I think I've addressed all the commits. Also split the rename of rhsOrder and lhsOrder in a seperate commit to keep the meat of the change isolated.

    liggitt liggitt commented on Oct 20, 2023 02:33 UTC
    Contributor

    /lgtm

    liggitt liggitt commented on Oct 20, 2023 14:37 UTC
    Contributor

    Thanks, open the k/k PR to update master and ping me for review.

    I suspect we'll want to backport this, not sure if that means picking the fix to other smd branches or not.

    Reviewers
    Assignees
    apelisse apelisse
    Labels
    risk: low confidence: 79.3% 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: 46.7% (MEDIUM) · Higher = larger blast radius
    46.7%
    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: 79.3% · Lower = rushed or shallow review
    79.3%
    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: 9.7% · Safe to merge
    9.7%