karmada-io / karmada

Open, Multi-Cloud, Multi-Cluster Kubernetes Orchestration
https://karmada.io
Apache License 2.0
4.44k stars 880 forks source link

only generate json patch for status filed when patchBindingStatus #5234

Closed chaosi-zju closed 2 months ago

chaosi-zju commented 2 months ago

What type of PR is this?

/kind cleanup

What this PR does / why we need it:

only generate json patch for status filed when patchBindingStatus

Which issue(s) this PR fixes:

Fixes #5203

Special notes for your reviewer:

Test report of Benchmark:

1) test code:

benchmark_test.go ```go var now = metav1.Now() var bindSpec = workv1alpha2.ResourceBindingSpec{ PropagateDeps: true, ReplicaRequirements: &workv1alpha2.ReplicaRequirements{ ResourceRequest: util.EmptyResource().ResourceList(), }, Replicas: 10, Placement: &policyv1alpha1.Placement{ ReplicaScheduling: &policyv1alpha1.ReplicaSchedulingStrategy{ ReplicaSchedulingType: policyv1alpha1.ReplicaSchedulingTypeDivided, ReplicaDivisionPreference: policyv1alpha1.ReplicaDivisionPreferenceAggregated, }, }, GracefulEvictionTasks: nil, RequiredBy: []workv1alpha2.BindingSnapshot{ { Clusters: []workv1alpha2.TargetCluster{ { Name: "member3", Replicas: 2, }, }, }, }, SchedulerName: "default", Resource: workv1alpha2.ObjectReference{ APIVersion: "v1", Kind: "Pod", Namespace: "default", Name: "pod", }, Failover: &policyv1alpha1.FailoverBehavior{ Application: &policyv1alpha1.ApplicationFailoverBehavior{ DecisionConditions: policyv1alpha1.DecisionConditions{ TolerationSeconds: pointer.Int32(1), }, }, }, Clusters: []workv1alpha2.TargetCluster{ { Name: "member1", Replicas: 1, }, { Name: "member1", Replicas: 2, }, }, ConflictResolution: workv1alpha2.ResourceConflictResolutionOverwrite, RescheduleTriggeredAt: &now, } var successCondition = util.NewCondition(workv1alpha2.Scheduled, workv1alpha2.BindingReasonSuccess, successfulSchedulingMessage, metav1.ConditionTrue) var rb = &workv1alpha2.ResourceBinding{ ObjectMeta: metav1.ObjectMeta{Name: "rb-4", Namespace: "default"}, Spec: bindSpec, Status: workv1alpha2.ResourceBindingStatus{}, } var updateRB = &workv1alpha2.ResourceBinding{ ObjectMeta: metav1.ObjectMeta{Name: "rb-4", Namespace: "default"}, Spec: bindSpec, Status: workv1alpha2.ResourceBindingStatus{Conditions: []metav1.Condition{successCondition}}, } func BenchmarkPatchBindingStatusOld(b *testing.B) { for i := 0; i < b.N; i++ { patchBytes, err := helper.GenMergePatch(rb, updateRB) if err != nil { b.Fatal(err) } if len(patchBytes) == 0 { b.Fatal(err) } } } func BenchmarkPatchBindingStatus(b *testing.B) { for i := 0; i < b.N; i++ { patchBytes, err := helper.GenMergePatch(rb.Status, updateRB.Status) if err != nil { b.Fatal(err) } if len(patchBytes) == 0 { b.Fatal(err) } patchBytes = append(patchBytes, '}') patchBytes = append([]byte("{\"status\":"), patchBytes...) } } ```

2) test result

goos: windows
goarch: amd64
pkg: github.com/karmada-io/karmada/pkg/scheduler
cpu: Intel(R) Xeon(R) Gold 6278C CPU @ 2.60GHz
BenchmarkPatchBindingStatusOld
BenchmarkPatchBindingStatusOld-4           19429             64816 ns/op
PASS
goos: windows
goarch: amd64
pkg: github.com/karmada-io/karmada/pkg/scheduler
cpu: Intel(R) Xeon(R) Gold 6278C CPU @ 2.60GHz
BenchmarkPatchBindingStatus
BenchmarkPatchBindingStatus-4             125234              9259 ns/op
PASS

So, here is about a 6x performance increase.

Does this PR introduce a user-facing change?:

chaosi-zju commented 2 months ago

/cc @XiShanYongYe-Chang

codecov-commenter commented 2 months ago

:warning: Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

Attention: Patch coverage is 18.18182% with 9 lines in your changes missing coverage. Please review.

Project coverage is 28.23%. Comparing base (bc1c96e) to head (fbb7a5e). Report is 34 commits behind head on master.

Files Patch % Lines
pkg/util/helper/patch.go 0.00% 9 Missing :warning:

:exclamation: Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## master #5234 +/- ## ========================================== - Coverage 28.26% 28.23% -0.03% ========================================== Files 632 632 Lines 43732 43762 +30 ========================================== - Hits 12360 12358 -2 - Misses 30470 30503 +33 + Partials 902 901 -1 ``` | [Flag](https://app.codecov.io/gh/karmada-io/karmada/pull/5234/flags?src=pr&el=flags&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=karmada-io) | Coverage Δ | | |---|---|---| | [unittests](https://app.codecov.io/gh/karmada-io/karmada/pull/5234/flags?src=pr&el=flag&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=karmada-io) | `28.23% <18.18%> (-0.03%)` | :arrow_down: | Flags with carried forward coverage won't be shown. [Click here](https://docs.codecov.io/docs/carryforward-flags?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=karmada-io#carryforward-flags-in-the-pull-request-comment) to find out more.

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

XiShanYongYe-Chang commented 2 months ago

/assign

chaosi-zju commented 2 months ago

I added a Benchmark Test report in PR description~

XiShanYongYe-Chang commented 2 months ago

Hi @LivingCcj, can you help take a look?

LivingCcj commented 2 months ago

Have a great optimization effect, Hope it will bring good benefits to the community!

chaosi-zju commented 2 months ago

cc @XiShanYongYe-Chang

XiShanYongYe-Chang commented 2 months ago

Thanks~ /cc @whitewindmills @chaunceyjiang

chaosi-zju commented 2 months ago

I would prefer to extract those codes into a public function.

done, could you check again?

whitewindmills commented 2 months ago

are those flaky tests known?

chaosi-zju commented 2 months ago

are those flaky tests known?

Sorry, previously in patchScheduleResultForResourceBinding, I additionally want to use helper.GenFieldMergePatch("spec", crb.Spec, updateCRB.Spec) to avoid comparisons of the status field. But, when patching ScheduleResult, not only spec field changed, but also metadata field changed, just like metadata.annotation.lastAppliedPlacement. So, my previous code introduced a bug.

Now, I this pr, I'm not plan to modify patchScheduleResultForResourceBinding.

whitewindmills commented 2 months ago

get it. /lgtm /approve

karmada-bot commented 2 months ago

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: whitewindmills

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

The pull request process is described here

Needs approval from an approver in each of these files: - ~~[pkg/scheduler/OWNERS](https://github.com/karmada-io/karmada/blob/master/pkg/scheduler/OWNERS)~~ [whitewindmills] - ~~[pkg/util/OWNERS](https://github.com/karmada-io/karmada/blob/master/pkg/util/OWNERS)~~ [whitewindmills] Approvers can indicate their approval by writing `/approve` in a comment Approvers can cancel approval by writing `/approve cancel` in a comment