Skip to content

Pass user context to pd.NewClientWithContext in NewClientWithOpts#1019

Merged
disksing merged 2 commits intotikv:masterfrom
healthwaite:master
Oct 20, 2023
Merged

Pass user context to pd.NewClientWithContext in NewClientWithOpts#1019
disksing merged 2 commits intotikv:masterfrom
healthwaite:master

Conversation

@healthwaite
Copy link
Contributor

@healthwaite healthwaite commented Oct 16, 2023

When creating a new rawkv client in NewClientWithOpts we neglect to pass through the user's context to the pd client. This means any deadline/timeout set by the user is not adhered to. By default the call to pd.NewClientWithContext will retry for 100 attempts with a one second sleep between each attempt. This is annoying as a user if we want to set a shorter deadline. Fix the issue by calling pd.NewClientWithContext and passing through the user's context.

Using a modified version of example/rawkv/rawkv.go which sets the context deadline to 5 seconds and attempts to dial a bogus endpoint as an example. Before the fix:

$ time go run ./examples/rawkv/rawkv.go                                                                                                                                                                                                                            
[2023/10/16 16:30:35.202 +01:00] [WARN] [pd_service_discovery.go:448] ["[pd] failed to get cluster id"] [url=http://127.0.0.1:2379] [error="[PD:client:ErrClientGetMember]error:rpc error: code = Unavailable desc = connection error: desc = \"transport: Error while dialing: dial tcp 127.0.0.1:2379: connect: connection refused\" target:127.0.0.1:2379 status:TRANSIENT_FAILURE: error:rpc error: code = Unavailable desc = connection error: desc = \"transport: Error while dialing: dial tcp 127.0.0.1:2379: connect: connection refused\" target:127.0.0.1:2379 status:TRANSIENT_FAILURE"]                                                                                                                                                                                                                                                  
[2023/10/16 16:30:36.203 +01:00] [WARN] [pd_service_discovery.go:448] ["[pd] failed to get cluster id"] [url=http://127.0.0.1:2379] [error="[PD:client:ErrClientGetMember]error:rpc error: code = Unavailable desc = connection error: desc = \"transport: Error while dialing: dial tcp 127.0.0.1:2379: connect: connection refused\" target:127.0.0.1:2379 status:TRANSIENT_FAILURE: error:rpc error: code = Unavailable desc = connection error: desc = \"transport: Error while dialing: dial tcp 127.0.0.1:2379: connect: connection refused\" target:127.0.0.1:2379 status:TRANSIENT_FAILURE"]                                                                                                                                                                                                                                                  
... snipped ...
[2023/10/16 16:32:14.202 +01:00] [WARN] [pd_service_discovery.go:448] ["[pd] failed to get cluster id"] [url=http://127.0.0.1:2379] [error="[PD:client:ErrClientGetMember]error:rpc error: code = Unavailable desc = connection error: desc = \"transport: Error while dialing: dial tcp 127.0.0.1:2379: connect: connection refused\" target:127.0.0.1:2379 status:TRANSIENT_FAILURE: error:rpc error: code = Unavailable desc = connection error: desc = \"transport: Error while dialing: dial tcp 127.0.0.1:2379: connect: connection refused\" target:127.0.0.1:2379 status:TRANSIENT_FAILURE"]
panic: [pd] failed to get cluster id

goroutine 1 [running]:
main.main()
        /home/areid/dev/client-go/examples/rawkv/rawkv.go:28 +0x565
exit status 2

real    1m40.452s
user    0m0.854s
sys     0m0.188s

After the fix:

$ time go run ./examples/rawkv/rawkv.go
[2023/10/16 16:33:06.881 +01:00] [WARN] [pd_service_discovery.go:448] ["[pd] failed to get cluster id"] [url=http://127.0.0.1:2379] [error="[PD:client:ErrClientGetMember]error:rpc error: code = Unavailable desc = connection error: desc = \"transport: Error while dialing: dial tcp 127.0.0.1:2379: connect: connection refused\" target:127.0.0.1:2379 status:TRANSIENT_FAILURE: error:rpc error: code = Unavailable desc = connection error: desc = \"transport: Error while dialing: dial tcp 127.0.0.1:2379: connect: connection refused\" target:127.0.0.1:2379 status:TRANSIENT_FAILURE"]
[2023/10/16 16:33:07.881 +01:00] [WARN] [pd_service_discovery.go:448] ["[pd] failed to get cluster id"] [url=http://127.0.0.1:2379] [error="[PD:client:ErrClientGetMember]error:rpc error: code = Unavailable desc = connection error: desc = \"transport: Error while dialing: dial tcp 127.0.0.1:2379: connect: connection refused\" target:127.0.0.1:2379 status:TRANSIENT_FAILURE: error:rpc error: code = Unavailable desc = connection error: desc = \"transport: Error while dialing: dial tcp 127.0.0.1:2379: connect: connection refused\" target:127.0.0.1:2379 status:TRANSIENT_FAILURE"]
[2023/10/16 16:33:08.882 +01:00] [WARN] [pd_service_discovery.go:448] ["[pd] failed to get cluster id"] [url=http://127.0.0.1:2379] [error="[PD:client:ErrClientGetMember]error:rpc error: code = Unavailable desc = connection error: desc = \"transport: Error while dialing: dial tcp 127.0.0.1:2379: connect: connection refused\" target:127.0.0.1:2379 status:TRANSIENT_FAILURE: error:rpc error: code = Unavailable desc = connection error: desc = \"transport: Error while dialing: dial tcp 127.0.0.1:2379: connect: connection refused\" target:127.0.0.1:2379 status:TRANSIENT_FAILURE"]
[2023/10/16 16:33:09.882 +01:00] [WARN] [pd_service_discovery.go:448] ["[pd] failed to get cluster id"] [url=http://127.0.0.1:2379] [error="[PD:client:ErrClientGetMember]error:rpc error: code = Unavailable desc = connection error: desc = \"transport: Error while dialing: dial tcp 127.0.0.1:2379: connect: connection refused\" target:127.0.0.1:2379 status:TRANSIENT_FAILURE: error:rpc error: code = Unavailable desc = connection error: desc = \"transport: Error while dialing: dial tcp 127.0.0.1:2379: connect: connection refused\" target:127.0.0.1:2379 status:TRANSIENT_FAILURE"]
[2023/10/16 16:33:10.881 +01:00] [WARN] [pd_service_discovery.go:448] ["[pd] failed to get cluster id"] [url=http://127.0.0.1:2379] [error="[PD:client:ErrClientGetMember]error:rpc error: code = Unavailable desc = connection error: desc = \"transport: Error while dialing: dial tcp 127.0.0.1:2379: connect: connection refused\" target:127.0.0.1:2379 status:TRANSIENT_FAILURE: error:rpc error: code = Unavailable desc = connection error: desc = \"transport: Error while dialing: dial tcp 127.0.0.1:2379: connect: connection refused\" target:127.0.0.1:2379 status:TRANSIENT_FAILURE"]
[2023/10/16 16:33:11.881 +01:00] [WARN] [pd_service_discovery.go:448] ["[pd] failed to get cluster id"] [url=http://127.0.0.1:2379] [error="[PD:client:ErrClientGetMember]error:rpc error: code = DeadlineExceeded desc = context deadline exceeded target:127.0.0.1:2379 status:TRANSIENT_FAILURE: error:rpc error: code = DeadlineExceeded desc = context deadline exceeded target:127.0.0.1:2379 status:TRANSIENT_FAILURE"]
panic: [pd] failed to get cluster id

goroutine 1 [running]:
main.main()
        /home/areid/dev/client-go/examples/rawkv/rawkv.go:32 +0x5bd
exit status 2

real    0m5.500s
user    0m0.839s
sys     0m0.339s

Signed-off-by: Alex Reid <areid@akamai.com>
@disksing disksing merged commit 33e722e into tikv:master Oct 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants