-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Conversation
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.
Test changes on VMUse 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 |
Regression DetectorRegression Detector ResultsRun ID: d9cea281-468c-4ab3-bb1c-eb959235b595 Metrics dashboard Target profiles Baseline: 0cce24d Performance changes are noted in the perf column of each table:
No significant changes in experiment optimization goalsConfidence level: 90.00% There were no significant changes in experiment optimization goals at this confidence level and effect size tolerance.
|
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:
-
Its estimated |Δ mean %| ≥ 5.00%, indicating the change is big enough to merit a closer look.
-
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.
-
Its configuration does not mark it "erratic".
pkg/config/structure/unmarshal.go
Outdated
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} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nitpick suggestion
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} |
There was a problem hiding this comment.
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.
pkg/config/structure/unmarshal.go
Outdated
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())) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
pkg/config/structure/unmarshal.go
Outdated
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()) | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, good call, done.
pkg/config/structure/unmarshal.go
Outdated
} | ||
return keys | ||
} | ||
return nil |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
pkg/config/structure/unmarshal.go
Outdated
} | ||
scalar := child.AsScalar() | ||
if scalar != nil { | ||
mval, _ := scalar.GetString() |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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.
pkg/config/structure/unmarshal.go
Outdated
// 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") | ||
} |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
pkg/config/structure/unmarshal.go
Outdated
} | ||
elemType := target.Type() | ||
elemType = elemType.Elem() | ||
results := reflect.MakeSlice(reflect.SliceOf(elemType), source.Size(), source.Size()) |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this 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
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())) |
There was a problem hiding this comment.
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
} | ||
scalar := child.AsScalar() | ||
if scalar != nil { | ||
mval, _ := scalar.GetString() |
There was a problem hiding this comment.
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 ?
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
There was a problem hiding this 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
mtype := reflect.MapOf(ktype, vtype) | ||
results := reflect.MakeMap(mtype) | ||
|
||
mapKeys, err := source.ChildrenKeys() |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
/merge |
🚂 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. Use |
🚂 MergeQueue: pull request added to the queue The median merge time in Use |
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