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

scheduler: fix scheduler save config #7108

Merged
merged 6 commits into from
Sep 27, 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
2 changes: 1 addition & 1 deletion pkg/schedule/schedulers/evict_leader.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func newEvictLeaderScheduler(opController *operator.Controller, conf *evictLeade
}
}

// EvictStores returns the IDs of the evict-stores.
// EvictStoreIDs returns the IDs of the evict-stores.
func (s *evictLeaderScheduler) EvictStoreIDs() []uint64 {
return s.conf.getStores()
}
Expand Down
14 changes: 7 additions & 7 deletions pkg/schedule/schedulers/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,16 @@
return nil, errs.ErrSchedulerCreateFuncNotRegistered.FastGenByArgs(typ)
}

s, err := fn(oc, storage, dec, removeSchedulerCb...)
if err != nil {
return nil, err
}
return fn(oc, storage, dec, removeSchedulerCb...)
}

// SaveSchedulerConfig saves the config of the specified scheduler.
func SaveSchedulerConfig(storage endpoint.ConfigStorage, s Scheduler) error {
data, err := s.EncodeConfig()
if err != nil {
return nil, err
return err

Check warning on line 134 in pkg/schedule/schedulers/scheduler.go

View check run for this annotation

Codecov / codecov/patch

pkg/schedule/schedulers/scheduler.go#L134

Added line #L134 was not covered by tests
}
err = storage.SaveSchedulerConfig(s.GetName(), data)
return s, err
return storage.SaveSchedulerConfig(s.GetName(), data)
}

// FindSchedulerTypeByName finds the type of the specified name.
Expand Down
8 changes: 8 additions & 0 deletions pkg/schedule/schedulers/scheduler_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@
}

c.schedulerHandlers[name] = scheduler
if err := SaveSchedulerConfig(c.storage, scheduler); err != nil {
log.Error("can not save HTTP scheduler config", zap.String("scheduler-name", scheduler.GetName()), errs.ZapError(err))
return err

Check warning on line 143 in pkg/schedule/schedulers/scheduler_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/schedule/schedulers/scheduler_controller.go#L142-L143

Added lines #L142 - L143 were not covered by tests
}
c.cluster.GetSchedulerConfig().AddSchedulerCfg(scheduler.GetType(), args)
return nil
}
Expand Down Expand Up @@ -188,6 +192,10 @@
c.wg.Add(1)
go c.runScheduler(s)
c.schedulers[s.Scheduler.GetName()] = s
if err := SaveSchedulerConfig(c.storage, scheduler); err != nil {
log.Error("can not save scheduler config", zap.String("scheduler-name", scheduler.GetName()), errs.ZapError(err))
return err

Check warning on line 197 in pkg/schedule/schedulers/scheduler_controller.go

View check run for this annotation

Codecov / codecov/patch

pkg/schedule/schedulers/scheduler_controller.go#L196-L197

Added lines #L196 - L197 were not covered by tests
}
c.cluster.GetSchedulerConfig().AddSchedulerCfg(s.Scheduler.GetType(), args)
return nil
}
Expand Down
3 changes: 2 additions & 1 deletion server/cluster/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3111,8 +3111,9 @@ func TestPersistScheduler(t *testing.T) {
// whether the schedulers added or removed in dynamic way are recorded in opt
_, newOpt, err := newTestScheduleConfig()
re.NoError(err)
_, err = schedulers.CreateScheduler(schedulers.ShuffleRegionType, oc, storage, schedulers.ConfigJSONDecoder([]byte("null")))
shuffle, err := schedulers.CreateScheduler(schedulers.ShuffleRegionType, oc, storage, schedulers.ConfigJSONDecoder([]byte("null")))
re.NoError(err)
re.NoError(controller.AddScheduler(shuffle))
// suppose we add a new default enable scheduler
sc.DefaultSchedulers = append(sc.DefaultSchedulers, sc.SchedulerConfig{Type: "shuffle-region"})
defer func() {
Expand Down
115 changes: 115 additions & 0 deletions tests/server/cluster/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/tikv/pd/pkg/mock/mockid"
sc "github.com/tikv/pd/pkg/schedule/config"
"github.com/tikv/pd/pkg/schedule/operator"
"github.com/tikv/pd/pkg/schedule/schedulers"
"github.com/tikv/pd/pkg/storage"
"github.com/tikv/pd/pkg/syncer"
"github.com/tikv/pd/pkg/tso"
Expand All @@ -47,6 +48,7 @@ import (
"github.com/tikv/pd/server/cluster"
"github.com/tikv/pd/server/config"
"github.com/tikv/pd/tests"
"github.com/tikv/pd/tests/server/api"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
Expand Down Expand Up @@ -1275,6 +1277,119 @@ func TestStaleTermHeartbeat(t *testing.T) {
re.NoError(err)
}

func TestTransferLeaderForScheduler(t *testing.T) {
re := require.New(t)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
re.NoError(failpoint.Enable("github.com/tikv/pd/pkg/schedule/changeCoordinatorTicker", `return(true)`))
tc, err := tests.NewTestCluster(ctx, 2)
defer tc.Destroy()
re.NoError(err)
err = tc.RunInitialServers()
re.NoError(err)
tc.WaitLeader()
// start
leaderServer := tc.GetServer(tc.GetLeader())
re.NoError(leaderServer.BootstrapCluster())
rc := leaderServer.GetServer().GetRaftCluster()
re.NotNil(rc)

storesNum := 2
grpcPDClient := testutil.MustNewGrpcClient(re, leaderServer.GetAddr())
for i := 1; i <= storesNum; i++ {
store := &metapb.Store{
Id: uint64(i),
Address: "127.0.0.1:" + strconv.Itoa(i),
}
resp, err := putStore(grpcPDClient, leaderServer.GetClusterID(), store)
re.NoError(err)
re.Equal(pdpb.ErrorType_OK, resp.GetHeader().GetError().GetType())
}
// region heartbeat
id := leaderServer.GetAllocator()
putRegionWithLeader(re, rc, id, 1)

time.Sleep(time.Second)
re.True(leaderServer.GetRaftCluster().IsPrepared())
// Add evict leader scheduler
api.MustAddScheduler(re, leaderServer.GetAddr(), schedulers.EvictLeaderName, map[string]interface{}{
"store_id": 1,
})
api.MustAddScheduler(re, leaderServer.GetAddr(), schedulers.EvictLeaderName, map[string]interface{}{
"store_id": 2,
})
// Check scheduler updated.
schedulersController := rc.GetCoordinator().GetSchedulersController()
re.Len(schedulersController.GetSchedulerNames(), 6)
checkEvictLeaderSchedulerExist(re, schedulersController, true)
checkEvictLeaderStoreIDs(re, schedulersController, []uint64{1, 2})

// transfer PD leader to another PD
tc.ResignLeader()
rc.Stop()
tc.WaitLeader()
leaderServer = tc.GetServer(tc.GetLeader())
rc1 := leaderServer.GetServer().GetRaftCluster()
rc1.Start(leaderServer.GetServer())
re.NoError(err)
re.NotNil(rc1)
// region heartbeat
id = leaderServer.GetAllocator()
putRegionWithLeader(re, rc1, id, 1)
time.Sleep(time.Second)
re.True(leaderServer.GetRaftCluster().IsPrepared())
// Check scheduler updated.
schedulersController = rc1.GetCoordinator().GetSchedulersController()
re.Len(schedulersController.GetSchedulerNames(), 6)
checkEvictLeaderSchedulerExist(re, schedulersController, true)
checkEvictLeaderStoreIDs(re, schedulersController, []uint64{1, 2})

// transfer PD leader back to the previous PD
tc.ResignLeader()
rc1.Stop()
tc.WaitLeader()
leaderServer = tc.GetServer(tc.GetLeader())
rc = leaderServer.GetServer().GetRaftCluster()
rc.Start(leaderServer.GetServer())
re.NotNil(rc)
// region heartbeat
id = leaderServer.GetAllocator()
putRegionWithLeader(re, rc, id, 1)
time.Sleep(time.Second)
re.True(leaderServer.GetRaftCluster().IsPrepared())
// Check scheduler updated
schedulersController = rc.GetCoordinator().GetSchedulersController()
re.Len(schedulersController.GetSchedulerNames(), 6)
checkEvictLeaderSchedulerExist(re, schedulersController, true)
checkEvictLeaderStoreIDs(re, schedulersController, []uint64{1, 2})

re.NoError(failpoint.Disable("github.com/tikv/pd/pkg/schedule/changeCoordinatorTicker"))
}

func checkEvictLeaderSchedulerExist(re *require.Assertions, sc *schedulers.Controller, exist bool) {
testutil.Eventually(re, func() bool {
if !exist {
return sc.GetScheduler(schedulers.EvictLeaderName) == nil
}
return sc.GetScheduler(schedulers.EvictLeaderName) != nil
})
}

func checkEvictLeaderStoreIDs(re *require.Assertions, sc *schedulers.Controller, expected []uint64) {
handler, ok := sc.GetSchedulerHandlers()[schedulers.EvictLeaderName]
re.True(ok)
h, ok := handler.(interface {
EvictStoreIDs() []uint64
})
re.True(ok)
var evictStoreIDs []uint64
testutil.Eventually(re, func() bool {
evictStoreIDs = h.EvictStoreIDs()
return len(evictStoreIDs) == len(expected)
})
re.ElementsMatch(evictStoreIDs, expected)
}

func putRegionWithLeader(re *require.Assertions, rc *cluster.RaftCluster, id id.Allocator, storeID uint64) {
for i := 0; i < 3; i++ {
regionID, err := id.Alloc()
Expand Down
Loading