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

introduce RPC interceptor mechanism #389

Merged
merged 19 commits into from
Dec 18, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 9 additions & 13 deletions integration_tests/interceptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,11 @@ func TestInterceptor(t *testing.T) {
assert.NoError(t, err)
err = txn.Commit(context.Background())
assert.NoError(t, err)
if len(manager.ExecLog()) > 0 {
assert.Equal(t, 2, manager.BeginCount())
assert.Equal(t, 2, manager.EndCount())
assert.Len(t, manager.ExecLog(), 2)
assert.Equal(t, "INTERCEPTOR-1", manager.ExecLog()[0])
assert.Equal(t, "INTERCEPTOR-1", manager.ExecLog()[1])
}
assert.Equal(t, 2, manager.BeginCount())
assert.Equal(t, 2, manager.EndCount())
assert.Len(t, manager.ExecLog(), 2)
assert.Equal(t, "INTERCEPTOR-1", manager.ExecLog()[0])
assert.Equal(t, "INTERCEPTOR-1", manager.ExecLog()[1])
manager.Reset()

txn, err = store.Begin()
Expand All @@ -51,11 +49,9 @@ func TestInterceptor(t *testing.T) {
value, err := txn.Get(context.Background(), []byte("KEY-1"))
assert.NoError(t, err)
assert.Equal(t, []byte("VALUE-1"), value)
if len(manager.ExecLog()) > 0 {
assert.Equal(t, 1, manager.BeginCount())
assert.Equal(t, 1, manager.EndCount())
assert.Len(t, manager.ExecLog(), 1)
assert.Equal(t, "INTERCEPTOR-2", manager.ExecLog()[0])
}
assert.Equal(t, 1, manager.BeginCount())
assert.Equal(t, 1, manager.EndCount())
assert.Len(t, manager.ExecLog(), 1)
assert.Equal(t, "INTERCEPTOR-2", manager.ExecLog()[0])
manager.Reset()
}
12 changes: 0 additions & 12 deletions internal/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ import (
"github.com/tikv/client-go/v2/internal/logutil"
"github.com/tikv/client-go/v2/metrics"
"github.com/tikv/client-go/v2/tikvrpc"
"github.com/tikv/client-go/v2/tikvrpc/interceptor"
"github.com/tikv/client-go/v2/util"
"google.golang.org/grpc"
"google.golang.org/grpc/backoff"
Expand Down Expand Up @@ -369,18 +368,7 @@ func (c *RPCClient) updateTiKVSendReqHistogram(req *tikvrpc.Request, start time.
}

// SendRequest sends a Request to server and receives Response.
// If tikvrpc.Interceptor has been set in ctx, it will be used to wrap RPC action.
func (c *RPCClient) SendRequest(ctx context.Context, addr string, req *tikvrpc.Request, timeout time.Duration) (*tikvrpc.Response, error) {
if it := interceptor.GetRPCInterceptorFromCtx(ctx); it != nil {
return it(func(target string, req *tikvrpc.Request) (*tikvrpc.Response, error) {
return c.sendRequest(ctx, target, req, timeout)
})(addr, req)
}
return c.sendRequest(ctx, addr, req, timeout)
}

// sendRequest sends a Request to server and receives Response.
func (c *RPCClient) sendRequest(ctx context.Context, addr string, req *tikvrpc.Request, timeout time.Duration) (*tikvrpc.Response, error) {
if span := opentracing.SpanFromContext(ctx); span != nil && span.Tracer() != nil {
span1 := span.Tracer().StartSpan(fmt.Sprintf("rpcClient.SendRequest, region ID: %d, type: %s", req.RegionId, req.Type), opentracing.ChildOf(span.Context()))
defer span1.Finish()
Expand Down
10 changes: 10 additions & 0 deletions internal/client/client_collapse.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (

"github.com/pkg/errors"
"github.com/tikv/client-go/v2/tikvrpc"
"github.com/tikv/client-go/v2/tikvrpc/interceptor"
"golang.org/x/sync/singleflight"
)

Expand All @@ -65,6 +66,15 @@ func (r reqCollapse) Close() error {
}

func (r reqCollapse) SendRequest(ctx context.Context, addr string, req *tikvrpc.Request, timeout time.Duration) (*tikvrpc.Response, error) {
if it := interceptor.GetRPCInterceptorFromCtx(ctx); it != nil {
mornyx marked this conversation as resolved.
Show resolved Hide resolved
return it(func(target string, req *tikvrpc.Request) (*tikvrpc.Response, error) {
return r.sendRequest(ctx, target, req, timeout)
})(addr, req)
}
return r.sendRequest(ctx, addr, req, timeout)
}

func (r reqCollapse) sendRequest(ctx context.Context, addr string, req *tikvrpc.Request, timeout time.Duration) (*tikvrpc.Response, error) {
if r.Client == nil {
panic("client should not be nil")
}
Expand Down