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

UnmarshalKey implemented using pure reflection #28821

Merged
merged 7 commits into from
Sep 6, 2024

Conversation

dustmop
Copy link
Contributor

@dustmop dustmop commented Aug 27, 2024

What does this PR do?

This version of UnmarshalKey doesn't rely upon the config having an internal map[string]interface{} to store data. Instead, it is implemented using reflection only and demonstrates the a node-based view of the config's data model.

Motivation

Preparation for the new config model

Additional Notes

Possible Drawbacks / Trade-offs

Describe how to test/QA your changes

Functionality is covered by unit tests

This version of UnmarshalKey doesn't rely upon the config having an internal map[string]interface{} to store data. Instead, it is implemented using reflection only and demonstrates the a node-based view of the config's data model.
@dustmop dustmop added changelog/no-changelog team/agent-shared-components qa/done Skip QA week as QA was done before merge and regressions are covered by tests labels Aug 27, 2024
@dustmop dustmop added this to the 7.58.0 milestone Aug 27, 2024
@dustmop dustmop requested a review from a team as a code owner August 27, 2024 19:58
@pr-commenter
Copy link

pr-commenter bot commented Aug 27, 2024

Test changes on VM

Use this command from test-infra-definitions to manually test this PR changes on a VM:

inv create-vm --pipeline-id=43752364 --os-family=ubuntu

Note: This applies to commit 29acfc7

@pr-commenter
Copy link

pr-commenter bot commented Aug 27, 2024

Regression Detector

Regression Detector Results

Run ID: d9cea281-468c-4ab3-bb1c-eb959235b595 Metrics dashboard Target profiles

Baseline: 0cce24d
Comparison: ce51c6c

Performance changes are noted in the perf column of each table:

  • ✅ = significantly better comparison variant performance
  • ❌ = significantly worse comparison variant performance
  • ➖ = no significant change in performance

No significant changes in experiment optimization goals

Confidence level: 90.00%
Effect size tolerance: |Δ mean %| ≥ 5.00%

There were no significant changes in experiment optimization goals at this confidence level and effect size tolerance.

Fine details of change detection per experiment

perf experiment goal Δ mean % Δ mean % CI trials links
file_tree memory utilization +1.85 [+1.76, +1.95] 1 Logs
basic_py_check % cpu utilization +1.19 [-1.84, +4.21] 1 Logs
uds_dogstatsd_to_api_cpu % cpu utilization +0.73 [-0.13, +1.58] 1 Logs
pycheck_lots_of_tags % cpu utilization +0.22 [-2.22, +2.67] 1 Logs
idle memory utilization +0.20 [+0.16, +0.24] 1 Logs
tcp_dd_logs_filter_exclude ingress throughput +0.00 [-0.01, +0.01] 1 Logs
uds_dogstatsd_to_api ingress throughput -0.00 [-0.00, +0.00] 1 Logs
otel_to_otel_logs ingress throughput -0.45 [-1.26, +0.35] 1 Logs
tcp_syslog_to_blackhole ingress throughput -1.79 [-14.62, +11.04] 1 Logs

Bounds Checks

perf experiment bounds_check_name replicates_passed
idle memory_usage 1/10

Explanation

A regression test is an A/B test of target performance in a repeatable rig, where "performance" is measured as "comparison variant minus baseline variant" for an optimization goal (e.g., ingress throughput). Due to intrinsic variability in measuring that goal, we can only estimate its mean value for each experiment; we report uncertainty in that value as a 90.00% confidence interval denoted "Δ mean % CI".

For each experiment, we decide whether a change in performance is a "regression" -- a change worth investigating further -- if all of the following criteria are true:

  1. Its estimated |Δ mean %| ≥ 5.00%, indicating the change is big enough to merit a closer look.

  2. Its 90.00% confidence interval "Δ mean % CI" does not contain zero, indicating that if our statistical model is accurate, there is at least a 90.00% chance there is a difference in performance between baseline and comparison variants.

  3. Its configuration does not mark it "erratic".

Comment on lines 82 to 93
if v.Kind() == reflect.Struct {
return &nodeimpl{typ: structNodeType, val: v}
}
if v.Kind() == reflect.Map {
return &nodeimpl{typ: mapNodeType, val: v}
}
if v.Kind() == reflect.Slice {
return &nodeimpl{typ: listNodeType, val: v}
}
if isScalar(v) {
return &nodeimpl{typ: scalarNodeType, val: v}
}
Copy link
Member

Choose a reason for hiding this comment

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

Nitpick suggestion

Suggested change
if v.Kind() == reflect.Struct {
return &nodeimpl{typ: structNodeType, val: v}
}
if v.Kind() == reflect.Map {
return &nodeimpl{typ: mapNodeType, val: v}
}
if v.Kind() == reflect.Slice {
return &nodeimpl{typ: listNodeType, val: v}
}
if isScalar(v) {
return &nodeimpl{typ: scalarNodeType, val: v}
}
nodeType := invalidNodeType
if v.Kind() == reflect.Struct {
nodeType = structNodeType
}
if v.Kind() == reflect.Map {
nodeType = mapNodeType
}
if v.Kind() == reflect.Slice {
nodeType = listNodeType
}
if isScalar(v) {
nodeType = scalarNodeType
}
if nodeType == invalidNodeType {
return nil, fmt.Errorf("could not create node from: %v of type %T and kind %v", v, v, v.Kind())
}
return &nodeimpl{typ: nodeType, val: v}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Now this method returns a different implementation based upon the value's kind.

if isScalar(v) {
return &nodeimpl{typ: scalarNodeType, val: v}
}
panic(fmt.Errorf("could not create node from: %v of type %T and kind %v", v, v, v.Kind()))
Copy link
Member

Choose a reason for hiding this comment

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

Do we want to panic? Should we return (node, error) and handle the case of invalid node type?

Copy link
Member

Choose a reason for hiding this comment

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

I agree, maybe returning an error would be better.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed all panics from the file, returning errors everywhere now.

Comment on lines 28 to 36
if outValue.Kind() == reflect.Map {
return copyMap(outValue, source)
}
if outValue.Kind() == reflect.Struct {
return copyStruct(outValue, source)
}
if outValue.Kind() == reflect.Slice {
return copyList(outValue, source.AsList())
}
Copy link
Member

@GustavoCaso GustavoCaso Aug 29, 2024

Choose a reason for hiding this comment

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

Nitpick: Could we use a switch statement here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, good call, done.

}
return keys
}
return nil
Copy link
Member

Choose a reason for hiding this comment

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

Is return nil the same same as a empty slice of strings?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was using nil to mean "no children" before, but now this function returns an error instead in that case.

}
scalar := child.AsScalar()
if scalar != nil {
mval, _ := scalar.GetString()
Copy link
Member

Choose a reason for hiding this comment

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

If understand correctly, GetString would return an empty string if leaf can not get the string value. Do we want to store empty string as map values?

Copy link
Member

Choose a reason for hiding this comment

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

Do we need this conversion to string ? Why not use the original scalar type ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed this call to GetString so that we check the error return. This call isn't quite a conversion: the var scalar represents a leaf node, and this call is getting the string value of that leaf node.

Comment on lines 194 to 203
// GetInt returns the scalar as a int, or an error otherwise
func (n *leafimpl) GetInt() (int, error) {
switch n.val.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return int(n.val.Int()), nil
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return int(n.val.Uint()), nil
}
return 0, conversionError(n.val, "int")
}
Copy link
Member

Choose a reason for hiding this comment

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

YAML supports floats. Do we care about floats for our use case?

Copy link
Member

Choose a reason for hiding this comment

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

We seem to use float values on our configuration template pkg/config/config_template.yaml

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added float support to the various places that needed it.

}
elemType := target.Type()
elemType = elemType.Elem()
results := reflect.MakeSlice(reflect.SliceOf(elemType), source.Size(), source.Size())
Copy link
Member

Choose a reason for hiding this comment

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

We are accessing souorce.Size() three times, should hosting the size value on a variable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, done, now numElems is used for this value.

Copy link
Member

@GustavoCaso GustavoCaso left a comment

Choose a reason for hiding this comment

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

Awesome work. The code is very easy to follow 🎉

Left a few questions 😄

pkg/config/structure/unmarshal.go Outdated Show resolved Hide resolved
if isScalar(v) {
return &nodeimpl{typ: scalarNodeType, val: v}
}
panic(fmt.Errorf("could not create node from: %v of type %T and kind %v", v, v, v.Kind()))
Copy link
Member

Choose a reason for hiding this comment

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

I agree, maybe returning an error would be better.

pkg/config/structure/unmarshal.go Outdated Show resolved Hide resolved
pkg/config/structure/unmarshal.go Outdated Show resolved Hide resolved
pkg/config/structure/unmarshal.go Outdated Show resolved Hide resolved
pkg/config/structure/unmarshal.go Outdated Show resolved Hide resolved
}
scalar := child.AsScalar()
if scalar != nil {
mval, _ := scalar.GetString()
Copy link
Member

Choose a reason for hiding this comment

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

Do we need this conversion to string ? Why not use the original scalar type ?

pkg/config/structure/unmarshal.go Outdated Show resolved Hide resolved
pkg/config/structure/unmarshal.go Outdated Show resolved Hide resolved
pkg/config/structure/unmarshal.go Outdated Show resolved Hide resolved
Instead of methods like AsList and AsScalar, rely on interface type checking. Each specific type of node uses its own implementation.
Support struct tag specifiers like omitempty.
Introduce errNotFound for GetChild when nodes aren't found
Copy link
Member

@hush-hush hush-hush left a comment

Choose a reason for hiding this comment

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

I added a couple of quesiton/nit-picks, but it looks good overall !0

pkg/config/structure/unmarshal.go Outdated Show resolved Hide resolved
mtype := reflect.MapOf(ktype, vtype)
results := reflect.MakeMap(mtype)

mapKeys, err := source.ChildrenKeys()
Copy link
Member

Choose a reason for hiding this comment

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

This would mean we could Unmarshal a struct into a map, no ? Should we prevent this and check that the source is also a map ?

Similar question for the other copy* func

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this is okay? It matches how the standard Unmarshal family of functions work. We'll keep an eye on it in future PRs, as this new function is used in more places, to see if we should decide to disable this behavior.

@dustmop
Copy link
Contributor Author

dustmop commented Sep 6, 2024

/merge

@dd-devflow
Copy link

dd-devflow bot commented Sep 6, 2024

🚂 MergeQueue: waiting for PR to be ready

This merge request is not mergeable yet, because of pending checks/missing approvals. It will be added to the queue as soon as checks pass and/or get approvals.
Note: if you pushed new commits since the last approval, you may need additional approval.
You can remove it from the waiting list with /remove command.

Use /merge -c to cancel this operation!

@dd-devflow
Copy link

dd-devflow bot commented Sep 6, 2024

🚂 MergeQueue: pull request added to the queue

The median merge time in main is 23m.

Use /merge -c to cancel this operation!

@dd-mergequeue dd-mergequeue bot merged commit 2a712cb into main Sep 6, 2024
197 of 217 checks passed
@dd-mergequeue dd-mergequeue bot deleted the dustin.long/unmarshal-reflect branch September 6, 2024 17:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
changelog/no-changelog qa/done Skip QA week as QA was done before merge and regressions are covered by tests team/agent-shared-components
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants