-
Notifications
You must be signed in to change notification settings - Fork 9.8k
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
rafthttp: use http.Request.WithContext instead of Cancel #7255
rafthttp: use http.Request.WithContext instead of Cancel #7255
Conversation
@xiang90 @heyitsanthony PTAL. Don't think the jenkins-proxy-ci is failing due to this change. Have already tested e2e, rafthttptest and integration in my local machine. Thanks! |
rafthttp/pipeline.go
Outdated
"github.com/coreos/etcd/pkg/pbutil" | ||
"github.com/coreos/etcd/pkg/types" | ||
"github.com/coreos/etcd/raft" | ||
"github.com/coreos/etcd/raft/raftpb" | ||
|
||
"golang.org/x/net/context" |
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.
use https://golang.org/pkg/context/ instead?
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
rafthttp/pipeline.go
Outdated
// always be stopped. So we use reportCriticalError to report it to errorc. | ||
if err == errMemberRemoved { | ||
reportCriticalError(err, p.errorc) | ||
go 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.
why put this into a go routine? what's wrong with the previous 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.
@xiang90 i have coded a select statement after this so that after cancel the program exits. Without this the test case was failing on using request.WithCancel. Let me try again.
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.
@xiang90 i tested it with old code, it will not work. The test case fails. So this code is required for it to work properly in this 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.
Why the old work works? But now it does not? I think we should figure it out. The only change how to set the cancel(), right? It should not affect the code after it I assume.
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.
@xiang90 I had already checked the code earlier of go package. With old code it checks for cancel. i mean its cancel func(depricated) But with the new implementation it needs the check for ctx.Done. The above code ensures the proper closure of request before exiting. Without this code, it doesn't.
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 do not think I understand what you said.
Let me clarify it.
now, we:
- create context with cancel at line 122
- pass in the created context to req at line 123
Now if the cancel fun get called, the request should be canceled.
- create a go routine to listen on stop. if server is stopped, we can cancel.
So if server is stopped, go routine should be triggered on stop channel, and call cancel.
After it calls cancel, the http request should be canceled.
From your test, it fails to cancel the http request. There is something goes wrong.
You "fix" that by making the http request nonblocking. That is not what we want. We want the request to be canceled once the server is stopped. Basically we need to make sure the go routine created at 134 exits.
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.
@xiang90 ok will check it out.
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.
@xiang90 i went through the code and i found that the issue is with the test case. Earlier the request cancel was being handled by us manually, hence the roundTripperBlocker used for test case is checking for req.Cancel which used to come earlier from our manual implementation. Now that is not the case and since the roundTripperBlocker has no implementation for ctx.Done hence it was failing.
Will correct the same and update the PR. Thanks!
rafthttp/snapshot_sender.go
Outdated
@@ -26,6 +26,8 @@ import ( | |||
"github.com/coreos/etcd/pkg/types" | |||
"github.com/coreos/etcd/raft" | |||
"github.com/coreos/etcd/snap" | |||
|
|||
"golang.org/x/net/context" |
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.
sure.
rafthttp/stream.go
Outdated
@@ -410,6 +412,7 @@ func (cr *streamReader) stop() { | |||
} | |||
|
|||
func (cr *streamReader) dial(t streamType) (io.ReadCloser, error) { | |||
|
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.
remove this extra empty line
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
Ignore proxy-ci for now. |
@sinsharat looks like a new failure. I tried to reproduce with |
@xiang90 @heyitsanthony all changes done. PTAL. |
@xiang90 @heyitsanthony All fixed. PTAL. |
cancel := httputil.RequestCanceler(req) | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
req = req.WithContext(ctx) | ||
defer cancel() |
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.
revert this change ( defer cancel() )? or change L128 of rafthttp/pipeline.go to make them consistent?
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.
@xiang90 i had to do this change because the govet in CI was giving a possible context leak error with old cancel placed there since it expects cancel to be called mandatorily before exiting. What do you want me to do for this?
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.
can you also fix other places? like L128 of rafthttp/pipeline.go
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.
@xiang90 for that we will need to change the design a bit since we cannot use defer directly there. For this reason govmt is not giving the error there. Using defer will not cause the code to work properly. So we will need to move the go routine code to i.e. L124 to L128 outside go routine and the code from L132 to L156 inside the go routine. Is that fine with you? Also this will make the code very similary to snapshot sender.go
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.
ok. we can deal with that later.
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.
ok
looks great. there are a few minor issues. we need to continue to clean up other pkgs if there is any. Thanks @sinsharat! |
@xiang90 do you want other packages to be corrected in the same PR? |
@sinsharat No. We can do them in other prs :P |
@xiang90 Ok. Let me know if anything else required to be done here. Will start with other packages once this gets merged. |
LGTM |
@sinsharat Actually can you squash two commits into one? |
@xiang90 its squashed into one. |
Thank you! |
@@ -16,13 +16,13 @@ package rafthttp | |||
|
|||
import ( | |||
"bytes" | |||
"context" |
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.
should we import "golang.org/x/net/context"
instead of context
? @xiang90 @heyitsanthony .
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.
@fanminshi we will use context in 3.2 release. we do not need the compatibility anymore.
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.
got it. Also have we refractor our code to use context
yet?
#6953