Skip to content

Commit c0e7057

Browse files
authored
owner: fix that get owner opValue before set this value when the etcdClient is nil (#45393)
close #45392
1 parent aca4429 commit c0e7057

File tree

3 files changed

+44
-3
lines changed

3 files changed

+44
-3
lines changed

owner/BUILD.bazel

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,14 @@ go_test(
3636
],
3737
embed = [":owner"],
3838
flaky = True,
39-
shard_count = 4,
39+
shard_count = 5,
4040
deps = [
4141
"//ddl",
4242
"//infoschema",
4343
"//kv",
4444
"//parser/terror",
4545
"//store/mockstore",
46+
"//testkit",
4647
"//testkit/testsetup",
4748
"//util",
4849
"//util/logutil",

owner/manager_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/pingcap/tidb/owner"
3030
"github.com/pingcap/tidb/parser/terror"
3131
"github.com/pingcap/tidb/store/mockstore"
32+
"github.com/pingcap/tidb/testkit"
3233
"github.com/pingcap/tidb/util/logutil"
3334
"github.com/stretchr/testify/require"
3435
clientv3 "go.etcd.io/etcd/client/v3"
@@ -176,6 +177,35 @@ func TestSetAndGetOwnerOpValue(t *testing.T) {
176177
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/owner/MockDelOwnerKey"))
177178
}
178179

180+
// TestGetOwnerOpValueBeforeSet tests get owner opValue before set this value when the etcdClient is nil.
181+
func TestGetOwnerOpValueBeforeSet(t *testing.T) {
182+
if runtime.GOOS == "windows" {
183+
t.Skip("integration.NewClusterV3 will create file contains a colon which is not allowed on Windows")
184+
}
185+
require.NoError(t, failpoint.Enable("github.com/pingcap/tidb/owner/MockNotSetOwnerOp", `return(true)`))
186+
187+
_, dom := testkit.CreateMockStoreAndDomain(t)
188+
ddl := dom.DDL()
189+
require.NoError(t, ddl.OwnerManager().CampaignOwner())
190+
isOwner := checkOwner(ddl, true)
191+
require.True(t, isOwner)
192+
193+
// test set/get owner info
194+
manager := ddl.OwnerManager()
195+
ownerID, err := manager.GetOwnerID(context.Background())
196+
require.NoError(t, err)
197+
require.Equal(t, ddl.GetID(), ownerID)
198+
op, err := owner.GetOwnerOpValue(context.Background(), nil, DDLOwnerKey, "log prefix")
199+
require.NoError(t, err)
200+
require.Equal(t, op, owner.OpNone)
201+
require.NoError(t, failpoint.Disable("github.com/pingcap/tidb/owner/MockNotSetOwnerOp"))
202+
err = manager.SetOwnerOpValue(context.Background(), owner.OpGetUpgradingState)
203+
require.NoError(t, err)
204+
op, err = owner.GetOwnerOpValue(context.Background(), nil, DDLOwnerKey, "log prefix")
205+
require.NoError(t, err)
206+
require.Equal(t, op, owner.OpGetUpgradingState)
207+
}
208+
179209
func TestCluster(t *testing.T) {
180210
if runtime.GOOS == "windows" {
181211
t.Skip("integration.NewClusterV3 will create file contains a colon which is not allowed on Windows")

owner/mock.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"time"
2222

2323
"github.com/pingcap/errors"
24+
"github.com/pingcap/failpoint"
2425
"github.com/pingcap/tidb/ddl/util"
2526
"github.com/pingcap/tidb/kv"
2627
"github.com/pingcap/tidb/util/logutil"
@@ -44,13 +45,19 @@ type mockManager struct {
4445
resignDone chan struct{}
4546
}
4647

48+
var mockOwnerOpValue atomic.Pointer[OpType]
49+
4750
// NewMockManager creates a new mock Manager.
4851
func NewMockManager(ctx context.Context, id string, store kv.Storage, ownerKey string) Manager {
4952
cancelCtx, cancelFunc := context.WithCancel(ctx)
5053
storeID := "mock_store_id"
5154
if store != nil {
5255
storeID = store.UUID()
5356
}
57+
58+
// Make sure the mockOwnerOpValue is initialized before GetOwnerOpValue in bootstrap.
59+
op := OpNone
60+
mockOwnerOpValue.Store(&op)
5461
return &mockManager{
5562
id: id,
5663
storeID: storeID,
@@ -106,9 +113,12 @@ func (m *mockManager) GetOwnerID(_ context.Context) (string, error) {
106113
return "", errors.New("no owner")
107114
}
108115

109-
var mockOwnerOpValue atomic.Pointer[OpType]
110-
111116
func (*mockManager) SetOwnerOpValue(_ context.Context, op OpType) error {
117+
failpoint.Inject("MockNotSetOwnerOp", func(val failpoint.Value) {
118+
if val.(bool) {
119+
failpoint.Return(nil)
120+
}
121+
})
112122
mockOwnerOpValue.Store(&op)
113123
return nil
114124
}

0 commit comments

Comments
 (0)