-
Notifications
You must be signed in to change notification settings - Fork 222
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
pd http: support api to get store min resolved ts #793
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
1648fc9
update
HuSharp 447cb63
update goimports
HuSharp 33cb18a
update goimports
HuSharp 88ad95c
address comment and add test for real pd
HuSharp b2d9fe6
address comment
HuSharp 075017b
address comment
HuSharp 9d4d857
update comment
HuSharp 9be61ab
Merge branch 'master' into add_store_min_resolved_ts
HuSharp 08178c1
address comment
HuSharp 6e4bfa5
update log output
HuSharp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
// Copyright 2023 TiKV Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
// NOTE: The code in this file is based on code from the | ||
// TiDB project, licensed under the Apache License v 2.0 | ||
// | ||
// https://github.com/pingcap/tidb/tree/cc5e161ac06827589c4966674597c137cc9e809c/store/tikv/tests/prewrite_test.go | ||
// | ||
|
||
// Copyright 2023 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package tikv_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"strings" | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
|
||
"github.com/pingcap/failpoint" | ||
"github.com/pingcap/kvproto/pkg/kvrpcpb" | ||
"github.com/stretchr/testify/suite" | ||
"github.com/tikv/client-go/v2/oracle" | ||
"github.com/tikv/client-go/v2/tikv" | ||
"github.com/tikv/client-go/v2/tikvrpc" | ||
"github.com/tikv/client-go/v2/util" | ||
pd "github.com/tikv/pd/client" | ||
) | ||
|
||
func TestPDAPI(t *testing.T) { | ||
if !*withTiKV { | ||
t.Skip("skipping TestPDAPI because with-tikv is not enabled") | ||
} | ||
suite.Run(t, new(apiTestSuite)) | ||
} | ||
|
||
type apiTestSuite struct { | ||
suite.Suite | ||
store *tikv.KVStore | ||
} | ||
|
||
func (s *apiTestSuite) SetupTest() { | ||
addrs := strings.Split(*pdAddrs, ",") | ||
pdClient, err := pd.NewClient(addrs, pd.SecurityOption{}) | ||
s.Require().Nil(err) | ||
rpcClient := tikv.NewRPCClient() | ||
// Set PD HTTP client. | ||
store, err := tikv.NewTestTiKVStore(rpcClient, pdClient, nil, nil, 0, tikv.WithPDHTTPClient(nil, addrs)) | ||
s.store = store | ||
storeID := uint64(1) | ||
s.store.GetRegionCache().SetRegionCacheStore(storeID, s.storeAddr(storeID), s.storeAddr(storeID), tikvrpc.TiKV, 1, nil) | ||
} | ||
|
||
func (s *apiTestSuite) storeAddr(id uint64) string { | ||
return fmt.Sprintf("store%d", id) | ||
} | ||
|
||
type storeSafeTsMockClient struct { | ||
tikv.Client | ||
requestCount int32 | ||
} | ||
|
||
func (c *storeSafeTsMockClient) SendRequest(ctx context.Context, addr string, req *tikvrpc.Request, timeout time.Duration) (*tikvrpc.Response, error) { | ||
if req.Type != tikvrpc.CmdStoreSafeTS { | ||
return c.Client.SendRequest(ctx, addr, req, timeout) | ||
} | ||
atomic.AddInt32(&c.requestCount, 1) | ||
resp := &tikvrpc.Response{} | ||
resp.Resp = &kvrpcpb.StoreSafeTSResponse{SafeTs: 150} | ||
return resp, nil | ||
} | ||
|
||
func (c *storeSafeTsMockClient) Close() error { | ||
return c.Client.Close() | ||
} | ||
|
||
func (c *storeSafeTsMockClient) CloseAddr(addr string) error { | ||
return c.Client.CloseAddr(addr) | ||
} | ||
|
||
func (s *apiTestSuite) TestGetStoreMinResolvedTS() { | ||
util.EnableFailpoints() | ||
// Try to get the minimum resolved timestamp of the store from PD. | ||
require := s.Require() | ||
require.Nil(failpoint.Enable("tikvclient/InjectMinResolvedTS", `return(100)`)) | ||
mockClient := storeSafeTsMockClient{ | ||
Client: s.store.GetTiKVClient(), | ||
} | ||
s.store.SetTiKVClient(&mockClient) | ||
var retryCount int | ||
for s.store.GetMinSafeTS(oracle.GlobalTxnScope) != 100 { | ||
time.Sleep(2 * time.Second) | ||
if retryCount > 5 { | ||
break | ||
} | ||
retryCount++ | ||
} | ||
require.Equal(atomic.LoadInt32(&mockClient.requestCount), int32(0)) | ||
require.Equal(uint64(100), s.store.GetMinSafeTS(oracle.GlobalTxnScope)) | ||
require.Nil(failpoint.Disable("tikvclient/InjectMinResolvedTS")) | ||
|
||
// Try to get the minimum resolved timestamp of the store from TiKV. | ||
require.Nil(failpoint.Enable("tikvclient/InjectMinResolvedTS", `return(0)`)) | ||
defer func() { | ||
s.Require().Nil(failpoint.Disable("tikvclient/InjectMinResolvedTS")) | ||
}() | ||
retryCount = 0 | ||
for s.store.GetMinSafeTS(oracle.GlobalTxnScope) != 150 { | ||
time.Sleep(2 * time.Second) | ||
if retryCount > 5 { | ||
break | ||
} | ||
retryCount++ | ||
} | ||
require.GreaterOrEqual(atomic.LoadInt32(&mockClient.requestCount), int32(1)) | ||
require.Equal(uint64(150), s.store.GetMinSafeTS(oracle.GlobalTxnScope)) | ||
} | ||
|
||
func (s *apiTestSuite) TearDownTest() { | ||
if s.store != nil { | ||
s.Require().Nil(s.store.Close()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back 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.
We could add a debug log to output
safeTS
anderr
when it's not nil.