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

rafthttp: use http.Request.WithContext instead of Cancel #7255

Merged
merged 1 commit into from
Feb 1, 2017
Merged

rafthttp: use http.Request.WithContext instead of Cancel #7255

merged 1 commit into from
Feb 1, 2017

Conversation

sinsharat
Copy link
Contributor

@sinsharat
Copy link
Contributor Author

sinsharat commented Jan 31, 2017

@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.
Proof attched:
image

Thanks!

"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"
Copy link
Contributor

Choose a reason for hiding this comment

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

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

// always be stopped. So we use reportCriticalError to report it to errorc.
if err == errMemberRemoved {
reportCriticalError(err, p.errorc)
go func() {
Copy link
Contributor

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?

Copy link
Contributor Author

@sinsharat sinsharat Jan 31, 2017

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.

Copy link
Contributor Author

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.
image

Copy link
Contributor

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.

Copy link
Contributor Author

@sinsharat sinsharat Jan 31, 2017

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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!

@@ -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"
Copy link
Contributor

Choose a reason for hiding this comment

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

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.

@@ -410,6 +412,7 @@ func (cr *streamReader) stop() {
}

func (cr *streamReader) dial(t streamType) (io.ReadCloser, error) {

Copy link
Contributor

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

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

@xiang90
Copy link
Contributor

xiang90 commented Jan 31, 2017

Ignore proxy-ci for now.

@heyitsanthony
Copy link
Contributor

@sinsharat looks like a new failure. I tried to reproduce with go test -run=RejectInsecure -v -tags cluster_proxy from your brach but it seems OK...

@sinsharat
Copy link
Contributor Author

@xiang90 @heyitsanthony all changes done. PTAL.
Thanks!

@sinsharat
Copy link
Contributor Author

@xiang90 @heyitsanthony All fixed. PTAL.
Thanks!

cancel := httputil.RequestCanceler(req)
ctx, cancel := context.WithCancel(context.Background())
req = req.WithContext(ctx)
defer cancel()
Copy link
Contributor

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?

Copy link
Contributor Author

@sinsharat sinsharat Feb 1, 2017

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?

Copy link
Contributor

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

Copy link
Contributor Author

@sinsharat sinsharat Feb 1, 2017

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

Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

@xiang90
Copy link
Contributor

xiang90 commented Feb 1, 2017

looks great. there are a few minor issues. we need to continue to clean up other pkgs if there is any. Thanks @sinsharat!

@sinsharat
Copy link
Contributor Author

@xiang90 do you want other packages to be corrected in the same PR?

@xiang90
Copy link
Contributor

xiang90 commented Feb 1, 2017

@sinsharat No. We can do them in other prs :P

@xiang90 xiang90 closed this Feb 1, 2017
@xiang90 xiang90 reopened this Feb 1, 2017
@sinsharat
Copy link
Contributor Author

sinsharat commented Feb 1, 2017

@xiang90 Ok. Let me know if anything else required to be done here. Will start with other packages once this gets merged.
Thank you!

@xiang90
Copy link
Contributor

xiang90 commented Feb 1, 2017

LGTM

@xiang90
Copy link
Contributor

xiang90 commented Feb 1, 2017

@sinsharat Actually can you squash two commits into one?

@sinsharat
Copy link
Contributor Author

@xiang90 its squashed into one.
Thanks!

@xiang90 xiang90 merged commit 42e7d4d into etcd-io:master Feb 1, 2017
@xiang90
Copy link
Contributor

xiang90 commented Feb 1, 2017

Thank you!

@@ -16,13 +16,13 @@ package rafthttp

import (
"bytes"
"context"
Copy link
Member

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 .

Copy link
Contributor

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.

Copy link
Member

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?

@sinsharat sinsharat deleted the use_requestWithContext_for_cancel branch February 2, 2017 12:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants