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

Fix wrong resource group name for some requests #788

Merged
merged 2 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
64 changes: 64 additions & 0 deletions integration_tests/resource_group_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package tikv_test

import (
"context"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/tikv/client-go/v2/tikv"
"github.com/tikv/client-go/v2/tikvrpc"
)

var _ tikv.Client = &resourceGroupNameMockClient{}

type resourceGroupNameMockClient struct {
tikv.Client

t *testing.T
expectedTag string
requestCount int
}

func (c *resourceGroupNameMockClient) SendRequest(ctx context.Context, addr string, req *tikvrpc.Request, timeout time.Duration) (*tikvrpc.Response, error) {
if req.GetResourceControlContext().GetResourceGroupName() == c.expectedTag {
c.requestCount++
}
return c.Client.SendRequest(ctx, addr, req, timeout)
}

func TestResourceGroupName(t *testing.T) {
testTag := "test"
/* Get */
store := NewTestStore(t)
client := &resourceGroupNameMockClient{t: t, Client: store.GetTiKVClient(), expectedTag: testTag}
store.SetTiKVClient(client)
txn, err := store.Begin()
assert.NoError(t, err)
txn.SetResourceGroupName(testTag)
_, _ = txn.Get(context.Background(), []byte{})
assert.Equal(t, 1, client.requestCount)
assert.NoError(t, store.Close())

/* BatchGet */
store = NewTestStore(t)
client = &resourceGroupNameMockClient{t: t, Client: store.GetTiKVClient(), expectedTag: testTag}
store.SetTiKVClient(client)
txn, err = store.Begin()
assert.NoError(t, err)
txn.SetResourceGroupName(testTag)
_, _ = txn.BatchGet(context.Background(), [][]byte{[]byte("k")})
assert.Equal(t, 1, client.requestCount)
assert.NoError(t, store.Close())

/* Scan */
store = NewTestStore(t)
client = &resourceGroupNameMockClient{t: t, Client: store.GetTiKVClient(), expectedTag: testTag}
store.SetTiKVClient(client)
txn, err = store.Begin()
assert.NoError(t, err)
txn.SetResourceGroupName(testTag)
_, _ = txn.Iter([]byte("abc"), []byte("def"))
assert.Equal(t, 1, client.requestCount)
assert.NoError(t, store.Close())
}
3 changes: 1 addition & 2 deletions txnkv/txnsnapshot/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ import (
"github.com/tikv/client-go/v2/tikvrpc"
"github.com/tikv/client-go/v2/tikvrpc/interceptor"
"github.com/tikv/client-go/v2/txnkv/txnlock"
"github.com/tikv/client-go/v2/util"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -248,7 +247,7 @@ func (s *Scanner) getData(bo *retry.Backoffer) error {
IsolationLevel: s.snapshot.isolationLevel.ToPB(),
RequestSource: s.snapshot.GetRequestSource(),
ResourceControlContext: &kvrpcpb.ResourceControlContext{
ResourceGroupName: util.ResourceGroupNameFromCtx(bo.GetCtx()),
ResourceGroupName: s.snapshot.mu.resourceGroupName,
},
BusyThresholdMs: uint32(s.snapshot.mu.busyThreshold.Milliseconds()),
})
Expand Down
4 changes: 2 additions & 2 deletions txnkv/txnsnapshot/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ func (s *KVSnapshot) batchGetSingleRegion(bo *retry.Backoffer, batch batchKeys,
IsolationLevel: s.isolationLevel.ToPB(),
RequestSource: s.GetRequestSource(),
ResourceControlContext: &kvrpcpb.ResourceControlContext{
ResourceGroupName: util.ResourceGroupNameFromCtx(bo.GetCtx()),
ResourceGroupName: s.mu.resourceGroupName,
},
BusyThresholdMs: uint32(s.mu.busyThreshold.Milliseconds()),
})
Expand Down Expand Up @@ -614,7 +614,7 @@ func (s *KVSnapshot) get(ctx context.Context, bo *retry.Backoffer, k []byte) ([]
IsolationLevel: s.isolationLevel.ToPB(),
RequestSource: s.GetRequestSource(),
ResourceControlContext: &kvrpcpb.ResourceControlContext{
ResourceGroupName: util.ResourceGroupNameFromCtx(bo.GetCtx()),
ResourceGroupName: s.mu.resourceGroupName,
},
BusyThresholdMs: uint32(s.mu.busyThreshold.Milliseconds()),
})
Expand Down