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
refactor(orc8r): Replace multi.go with hashicorp/go-multierror module #12627
Conversation
Thanks for opening a PR! 💯
Howto
More infoPlease take a moment to read through the Magma project's
If this is your first Magma PR, also consider reading
|
feg/gateway/services/session_proxy/servicers/multiple_session_controller.go
Outdated
Show resolved
Hide resolved
feg/gateway/services/session_proxy/servicers/session_controller.go
Outdated
Show resolved
Hide resolved
feg/gateway/services/session_proxy/servicers/session_controller.go
Outdated
Show resolved
Hide resolved
The insync-check is failing, you probably need some more go mod clean-up. |
The pr title prefix is fairly generic and does not match best practices, maybe better would be |
orc8r/cloud/go/services/orchestrator/servicers/protected/indexer_servicer.go
Outdated
Show resolved
Hide resolved
orc8r/cloud/go/services/orchestrator/servicers/protected/indexer_servicer.go
Outdated
Show resolved
Hide resolved
orc8r/cloud/go/services/orchestrator/servicers/protected/indexer_servicer.go
Outdated
Show resolved
Hide resolved
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.
LGTM
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.
Why are we removing our own implementation of errors if we already have it?
Why aren't we removing hashicorp instead to avoid that module not being supported in the future? (same that happened with errors module)
Note we also try to maintain the errors in one single line. So printing them in multiple lines is something we should avoid. Otherwise debugging and parsing becomes difficult. (for example you can not use grep properly anymore and you need to look for previous lines to see the full error)
if len(sessionIDToIMSI) != 0 { | ||
err := directoryd.MapSessionIDsToIMSIs(ctx, networkID, sessionIDToIMSI) | ||
multiError = multiError.AddFmt(err, "failed to update directoryd mapping of session IDs to IMSIs %+v", sessionIDToIMSI) | ||
if err != 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.
i know this has been mentioned below, but just as a placeholder: remove all ifs since Append
will ignore nils
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 contains formatting, the errors. Errorf
result will still not be nil
, even if err
was nil
.
But this 3 line block could be extracted to a function, since they just differ in a few words.
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.
Introduced a wrapper in this file.
@@ -25,6 +25,8 @@ import ( | |||
"github.com/aeden/traceroute" | |||
"github.com/emakeev/snowflake" | |||
"github.com/golang/glog" | |||
"github.com/hashicorp/go-multierror" | |||
"github.com/pkg/errors" |
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.
why are we introducing an unsupported package 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.
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.
@uri200 The package is removed in the final commit of this PR, so this is not part of the changes. (The PR isn't nicely tailored for commit by commit review, sorry.)
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.
The ticket #12632 was created as a result of realizing that the unsupported package was used here. I think this is for consistence but I agree that this PR should maybe also fix (parts of) #12632.
Could you please elaborate? We introduce 12627 which we are not sure, we need and then we create #12632 to fix issues that #12627 will create?
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.
Oh, sorry, that was not formulated clear enough.
#12632 was created while working on #12627. But it will also be solved here for the touched code. So no new instances of #12632 will be introduced and the code commented here is actually fixed in a later commit, as @alexzurbonsen explained.
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.
Cleaned up the commit history. Now all changes that replace merrors by hasicorp/go-multierror are in one commit.
I expect that the chances that this package is better maintained than our own library function are pretty high. It is also well tested and due to its wide usage it is also probably already known to many contributors.
That is a good point! |
eac469a
to
2e70a50
Compare
Let's wait for @emakeev , he may have some context on multi error code. |
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.
Could you please explain what is the purpose of this change?
The usage pattern seems to be nearly identical,
the implementation already exists & in use and hasn't been problematic so far.
Is there any reason, we want to spend time of developers & reviewers to fix something which is not broken while there are so many real improvements Magma can benefit from?
If you just need to change the format of the errors' string (which also has to be justified, BTW), it could be accomplished by a small 4 lines PR vs this 400 lines PR and keep flexibility to modify the format later anytime you need/want, just change func (me *Multi) Error() string to:
func (me *Multi) Error() string {
if me == nil {
return "<nil>"
}
switch len(me.errors) {
case 0:
return ""
case 1:
return me.errors[0].Error()
default:
var b bytes.Buffer
fmt.Fprintf(&b, "%d errors occurred:\n", len(me.errors))
fmtStr := "\t* %v\n"
for _, e := range me.errors {
fmt.Fprintf(&b, fmtStr, e)
}
b.Write([]byte("\n"))
return b.String()
}
}
--- a/orc8r/lib/go/merrors/multi.go
+++ b/orc8r/lib/go/merrors/multi.go
@@ -41,12 +41,12 @@ func (me *Multi) Error() string {
return me.errors[0].Error()
default:
var b bytes.Buffer
- fmtStr := "errors: [%d: %v"
- for i, e := range me.errors {
- fmt.Fprintf(&b, fmtStr, i, e)
- fmtStr = "; %d: %v"
+ fmt.Fprintf(&b, "%d errors occurred:\n", len(me.errors))
+ fmtStr := "\t* %v\n"
+ for _, e := range me.errors {
+ fmt.Fprintf(&b, fmtStr, e)
}
- b.Write([]byte("]"))
+ b.Write([]byte("\n"))
return b.String()
}
}
or something like this...
note that it also applies to the rest of Magma codebase (merror is maintained as well as the rest of Magma code), the original implementation is simple, controlled by us, allows us to format multi errors ANY way we want or need, now and in the future and it relies only on std Go packages. |
Hey @emakeev, Thank you for your comments. There are right now two implementations of multierrors used in Magma:
We saw an opportunity to clean up the source code and decided to go for the more commonly used one. The hashicorp library is also widely known, better documented and might rely less on tribal knowledge. Migrating to that also preserves the possibility to add arbitrary Regarding the formatting change: |
Sure, it seems to be reasonable to settle on a single implementation & since the work is already done, I don't see an issue w it. I'll leave it to @uri200 to decide if the change to multi line format is acceptable for FeG. |
you need to fix the insych issue |
remove grpc_to_http.go because it is unused Signed-off-by: Alexander zur Bonsen <alexander.zur.bonsen@tngtech.com> deleted multi_error_test.go Signed-off-by: Marco Pfirrmann <marco.pfirrmann@tngtech.com> remove multi.go Signed-off-by: Alexander zur Bonsen <alexander.zur.bonsen@tngtech.com>
0cecb84
to
200c71d
Compare
Author: Marco Pfirrmann <marco.pfirrmann@tngtech.com> Signed-off-by: Marco Pfirrmann <marco.pfirrmann@tngtech.com> changed merrors.Multi to multierrors.Error Signed-off-by: Marco Pfirrmann <marco.pfirrmann@tngtech.com> certifier.go: changed merrors package to hashicorp/multierrors Signed-off-by: Marco Pfirrmann <marco.pfirrmann@tngtech.com> introduce missing null checks Signed-off-by: Alexander zur Bonsen <alexander.zur.bonsen@tngtech.com> renaming multiError variables for better readability Signed-off-by: Marco Pfirrmann <marco.pfirrmann@tngtech.com> multierror.Append is assigned to the errs variable Signed-off-by: Marco Pfirrmann <marco.pfirrmann@tngtech.com> remove pkg/errors package from this PR and use std lib Signed-off-by: Alexander zur Bonsen <alexander.zur.bonsen@tngtech.com> include nil check in wrapper in indexer_servicer.go signed-off-by: Marco Pfirrmann <marco.pfirrmann@tngtech.com>
Signed-off-by: Alexander zur Bonsen <alexander.zur.bonsen@tngtech.com>
Summary
This PR modifies Magma's multi error management to use the
hashicorp/go-multierror
module.Formatting of multierrors changes due to the format used in the hashicorp module.
The new output looks like this:
whereas the former output was
where
error
is a string that contains the error message, and in many cases additional parts with further details abouth the error.hashicorp/go-multierror
is already used in this repo, often in conjunction with theErrorf
function fromgithub.com/pkg/errors
. Since it is not longer maintained we use the standard libfmt.Errorf
in this PR. We also opened an issue to remove thepkg/errors
from magma #12632.Since now unused, we delete
multi.go
and respective tests. Plusgrpc_to_http.go
, which was already unused before.Test Plan
./build.py -c
./build.py --all
Additional Information
Done in pairing with @mpfirrmann.