-
Notifications
You must be signed in to change notification settings - Fork 775
mcs: skip transferring primary to itself #10970
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
Merged
ti-chi-bot
merged 5 commits into
tikv:master
from
lhy1024:lhy/fix-tso-self-transfer-primary
Jul 6, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fbbec7e
mcs: skip transferring primary to itself
lhy1024 5196294
test: fix transfer primary test cleanup
lhy1024 2699206
mcs: simplify primary candidate selection
lhy1024 edeccc0
mcs: match old primary by name or address
lhy1024 209fd43
test: refactor transfer primary cases
lhy1024 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 hidden or 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 hidden or 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,199 @@ | ||
| // Copyright 2026 TiKV Project 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. | ||
|
|
||
| package utils_test | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/require" | ||
| clientv3 "go.etcd.io/etcd/client/v3" | ||
| "go.uber.org/goleak" | ||
|
|
||
| "github.com/tikv/pd/pkg/election" | ||
| "github.com/tikv/pd/pkg/mcs/discovery" | ||
| mcsutils "github.com/tikv/pd/pkg/mcs/utils" | ||
| "github.com/tikv/pd/pkg/mcs/utils/constant" | ||
| "github.com/tikv/pd/pkg/utils/etcdutil" | ||
| "github.com/tikv/pd/pkg/utils/keypath" | ||
| "github.com/tikv/pd/pkg/utils/testutil" | ||
| ) | ||
|
|
||
| func TestMain(m *testing.M) { | ||
| goleak.VerifyTestMain(m, testutil.LeakOptions...) | ||
| } | ||
|
|
||
| const ( | ||
| testPrimaryName = "tso-primary" | ||
| testPrimaryAddr = "http://127.0.0.1:10001" | ||
| testSecondaryName = "tso-secondary" | ||
| testSecondaryAddr = "http://127.0.0.1:10002" | ||
| ) | ||
|
|
||
| func TestTransferPrimary(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| oldPrimary string | ||
| newPrimary string | ||
| expectedPrimary string | ||
| keepExpectedPrimary bool | ||
| }{ | ||
| { | ||
| name: "self transfer by name keeps expected primary lease", | ||
| oldPrimary: testPrimaryName, | ||
| newPrimary: testPrimaryName, | ||
| expectedPrimary: testPrimaryAddr, | ||
| keepExpectedPrimary: true, | ||
| }, | ||
| { | ||
| name: "self transfer by address keeps expected primary lease", | ||
| oldPrimary: testPrimaryName, | ||
| newPrimary: testPrimaryAddr, | ||
| expectedPrimary: testPrimaryAddr, | ||
| keepExpectedPrimary: true, | ||
| }, | ||
| { | ||
| name: "random transfer excludes old primary by name", | ||
| oldPrimary: testPrimaryName, | ||
| expectedPrimary: testSecondaryAddr, | ||
| }, | ||
| { | ||
| name: "random transfer excludes old primary by address", | ||
| oldPrimary: testPrimaryAddr, | ||
| expectedPrimary: testSecondaryAddr, | ||
| }, | ||
| { | ||
| name: "explicit transfer selects target by name", | ||
| oldPrimary: testPrimaryName, | ||
| newPrimary: testSecondaryName, | ||
| expectedPrimary: testSecondaryAddr, | ||
| }, | ||
| { | ||
| name: "explicit transfer selects target by address", | ||
| oldPrimary: testPrimaryName, | ||
| newPrimary: testSecondaryAddr, | ||
| expectedPrimary: testSecondaryAddr, | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| env := newTransferPrimaryTestEnv(t) | ||
| before := env.getExpectedPrimary(t) | ||
| re := require.New(t) | ||
| re.Equal(testPrimaryAddr, before.value) | ||
| re.Equal(int64(env.lease.GetID()), before.lease) | ||
|
|
||
| err := mcsutils.TransferPrimary(env.client, env.lease, constant.TSOServiceName, | ||
| tt.oldPrimary, tt.newPrimary, 0, map[string]bool{ | ||
| testPrimaryAddr: true, | ||
| testSecondaryAddr: true, | ||
| }) | ||
| re.NoError(err) | ||
|
|
||
| after := env.getExpectedPrimary(t) | ||
| re.Equal(tt.expectedPrimary, after.value) | ||
| if tt.keepExpectedPrimary { | ||
| re.Equal(before.modRevision, after.modRevision) | ||
| re.Equal(before.lease, after.lease) | ||
| return | ||
| } | ||
| re.NotEqual(before.modRevision, after.modRevision) | ||
| re.NotEqual(before.lease, after.lease) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| type transferPrimaryTestEnv struct { | ||
| ctx context.Context | ||
| client *clientv3.Client | ||
| lease *election.Lease | ||
| expectedPrimaryPath string | ||
| } | ||
|
|
||
| type expectedPrimary struct { | ||
| value string | ||
| modRevision int64 | ||
| lease int64 | ||
| } | ||
|
|
||
| func newTransferPrimaryTestEnv(t *testing.T) *transferPrimaryTestEnv { | ||
| t.Helper() | ||
| re := require.New(t) | ||
| _, client, clean := etcdutil.NewTestEtcdCluster(t, 1, nil) | ||
| t.Cleanup(clean) | ||
|
|
||
| keypath.SetClusterID(uint64(time.Now().UnixNano())) | ||
| t.Cleanup(keypath.ResetClusterID) | ||
|
|
||
| ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second) | ||
| t.Cleanup(cancel) | ||
|
|
||
| putRegistryEntry(t, ctx, client, testPrimaryName, testPrimaryAddr) | ||
| putRegistryEntry(t, ctx, client, testSecondaryName, testSecondaryAddr) | ||
|
|
||
| lease := election.NewLease(client, "tso expected primary 00000") | ||
| re.NoError(lease.Grant(constant.DefaultLease)) | ||
| t.Cleanup(func() { | ||
| _ = lease.Close() | ||
| }) | ||
|
|
||
| expectedPrimaryPath := keypath.ExpectedPrimaryPath(&keypath.MsParam{ | ||
| ServiceName: constant.TSOServiceName, | ||
| GroupID: 0, | ||
| }) | ||
| _, err := client.Put(ctx, expectedPrimaryPath, testPrimaryAddr, clientv3.WithLease(lease.GetID())) | ||
| re.NoError(err) | ||
|
|
||
| return &transferPrimaryTestEnv{ | ||
| ctx: ctx, | ||
| client: client, | ||
| lease: lease, | ||
| expectedPrimaryPath: expectedPrimaryPath, | ||
| } | ||
| } | ||
|
|
||
| func (env *transferPrimaryTestEnv) getExpectedPrimary(t *testing.T) expectedPrimary { | ||
| t.Helper() | ||
| re := require.New(t) | ||
| resp, err := env.client.Get(env.ctx, env.expectedPrimaryPath) | ||
| re.NoError(err) | ||
| re.Len(resp.Kvs, 1) | ||
| kv := resp.Kvs[0] | ||
| return expectedPrimary{ | ||
| value: string(kv.Value), | ||
| modRevision: kv.ModRevision, | ||
| lease: kv.Lease, | ||
| } | ||
| } | ||
|
|
||
| func putRegistryEntry( | ||
| t *testing.T, | ||
| ctx context.Context, | ||
| client *clientv3.Client, | ||
| name string, | ||
| serviceAddr string, | ||
| ) { | ||
| t.Helper() | ||
| entry := discovery.ServiceRegistryEntry{ | ||
| Name: name, | ||
| ServiceAddr: serviceAddr, | ||
| } | ||
| serializedValue, err := entry.Serialize() | ||
| require.NoError(t, err) | ||
| _, err = client.Put(ctx, keypath.RegistryPath(constant.TSOServiceName, serviceAddr), serializedValue) | ||
| require.NoError(t, err) | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.