diff --git a/cmd/pd-server/main.go b/cmd/pd-server/main.go index 484f81cf558..84c71716e8d 100644 --- a/cmd/pd-server/main.go +++ b/cmd/pd-server/main.go @@ -78,6 +78,7 @@ func main() { } func createServerWrapper(args []string) (context.Context, context.CancelFunc, basicsvr.Server) { + schedulers.Register() cfg := config.NewConfig() err := cfg.Parse(args) @@ -135,7 +136,6 @@ func createServerWrapper(args []string) (context.Context, context.CancelFunc, ba if err != nil { log.Fatal("create server failed", errs.ZapError(err)) } - schedulers.Register() return ctx, cancel, svr } diff --git a/server/cluster/cluster_worker_test.go b/server/cluster/cluster_worker_test.go index 1d4fc993174..795b0bfd2e2 100644 --- a/server/cluster/cluster_worker_test.go +++ b/server/cluster/cluster_worker_test.go @@ -23,7 +23,6 @@ import ( "github.com/stretchr/testify/require" "github.com/tikv/pd/pkg/core" "github.com/tikv/pd/pkg/mock/mockid" - _ "github.com/tikv/pd/pkg/utils/testutil" "github.com/tikv/pd/server/storage" ) diff --git a/server/schedulers/init.go b/server/schedulers/init.go index d1508f5ce57..eeed22eff12 100644 --- a/server/schedulers/init.go +++ b/server/schedulers/init.go @@ -17,6 +17,7 @@ package schedulers import ( "strconv" "strings" + "sync" "github.com/tikv/pd/pkg/core" "github.com/tikv/pd/pkg/errs" @@ -24,8 +25,16 @@ import ( "github.com/tikv/pd/server/schedule" ) +var registerOnce sync.Once + // Register registers schedulers. func Register() { + registerOnce.Do(func() { + schedulersRegister() + }) +} + +func schedulersRegister() { // balance leader schedule.RegisterSliceDecoderBuilder(BalanceLeaderType, func(args []string) schedule.ConfigDecoder { return func(v interface{}) error { diff --git a/tests/dashboard/race_test.go b/tests/dashboard/race_test.go index 69596531dbb..ca1713c87fb 100644 --- a/tests/dashboard/race_test.go +++ b/tests/dashboard/race_test.go @@ -23,10 +23,11 @@ import ( "github.com/tikv/pd/pkg/dashboard" "github.com/tikv/pd/tests" - _ "github.com/tikv/pd/pkg/utils/testutil" + "github.com/tikv/pd/server/schedulers" ) func TestCancelDuringStarting(t *testing.T) { + schedulers.Register() prepareTestConfig() defer resetTestConfig() ctx, cancel := context.WithCancel(context.Background()) diff --git a/tests/dashboard/service_test.go b/tests/dashboard/service_test.go index f75e047d8f1..be59d2794f5 100644 --- a/tests/dashboard/service_test.go +++ b/tests/dashboard/service_test.go @@ -28,6 +28,7 @@ import ( "github.com/tikv/pd/pkg/dashboard" "github.com/tikv/pd/pkg/utils/testutil" "github.com/tikv/pd/server/config" + "github.com/tikv/pd/server/schedulers" "github.com/tikv/pd/tests" "github.com/tikv/pd/tests/pdctl" pdctlCmd "github.com/tikv/pd/tools/pd-ctl/pdctl" @@ -49,6 +50,7 @@ func TestDashboardTestSuite(t *testing.T) { } func (suite *dashboardTestSuite) SetupSuite() { + schedulers.Register() dashboard.SetCheckInterval(10 * time.Millisecond) suite.ctx, suite.cancel = context.WithCancel(context.Background()) suite.httpClient = &http.Client{ diff --git a/tests/server/server_test.go b/tests/server/server_test.go index 3b85cd3cf0d..0f9c338b8c4 100644 --- a/tests/server/server_test.go +++ b/tests/server/server_test.go @@ -22,15 +22,18 @@ import ( "github.com/tikv/pd/pkg/utils/tempurl" "github.com/tikv/pd/pkg/utils/testutil" "github.com/tikv/pd/server/config" + "github.com/tikv/pd/server/schedulers" "github.com/tikv/pd/tests" "go.uber.org/goleak" ) func TestMain(m *testing.M) { + schedulers.Register() goleak.VerifyTestMain(m, testutil.LeakOptions...) } func TestUpdateAdvertiseUrls(t *testing.T) { + schedulers.Register() re := require.New(t) ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -72,6 +75,7 @@ func TestUpdateAdvertiseUrls(t *testing.T) { } func TestClusterID(t *testing.T) { + schedulers.Register() re := require.New(t) ctx, cancel := context.WithCancel(context.Background()) defer cancel() @@ -108,6 +112,7 @@ func TestClusterID(t *testing.T) { } func TestLeader(t *testing.T) { + schedulers.Register() re := require.New(t) ctx, cancel := context.WithCancel(context.Background()) defer cancel() diff --git a/tools/pd-simulator/main.go b/tools/pd-simulator/main.go index 179184e62d4..3b5afc906e3 100644 --- a/tools/pd-simulator/main.go +++ b/tools/pd-simulator/main.go @@ -31,6 +31,7 @@ import ( "github.com/tikv/pd/server" "github.com/tikv/pd/server/api" "github.com/tikv/pd/server/config" + "github.com/tikv/pd/server/schedulers" "github.com/tikv/pd/server/statistics" "github.com/tikv/pd/tools/pd-analysis/analysis" "github.com/tikv/pd/tools/pd-simulator/simulator" @@ -55,6 +56,7 @@ var ( ) func main() { + schedulers.Register() // wait PD start. Otherwise it will happen error when getting cluster ID. time.Sleep(3 * time.Second) // ignore some undefined flag