From f8e60b28a8f4ea1cf4a10bf8cd15d0cc77b3a0d7 Mon Sep 17 00:00:00 2001 From: ShuNing Date: Sun, 29 Mar 2020 01:44:30 +0800 Subject: [PATCH 1/3] scheduelrs: add load expectations for hot scheudler (#2297) * scheduelrs: add load expectations for hot scheudler Signed-off-by: nolouch --- pkg/mock/mockcluster/mockcluster.go | 32 ++++++++ server/schedulers/hot_region.go | 48 ++++++++++- server/schedulers/hot_region_config.go | 14 ++++ server/schedulers/hot_test.go | 46 +++++------ server/schedulers/utils.go | 4 + .../pdctl/command/completion_command.go | 81 +++++++++++++++++++ tools/pd-ctl/pdctl/ctl.go | 1 + 7 files changed, 194 insertions(+), 32 deletions(-) create mode 100644 tools/pd-ctl/pdctl/command/completion_command.go diff --git a/pkg/mock/mockcluster/mockcluster.go b/pkg/mock/mockcluster/mockcluster.go index 08739da942a..aa58cba8c6e 100644 --- a/pkg/mock/mockcluster/mockcluster.go +++ b/pkg/mock/mockcluster/mockcluster.go @@ -393,11 +393,40 @@ func (mc *Cluster) UpdateStorageRatio(storeID uint64, usedRatio, availableRatio mc.PutStore(newStore) } +// UpdateStorageWrittenStats updates store written bytes. +func (mc *Cluster) UpdateStorageWrittenStats(storeID, bytesWritten, keysWritten uint64) { + store := mc.GetStore(storeID) + newStats := proto.Clone(store.GetStoreStats()).(*pdpb.StoreStats) + newStats.BytesWritten = bytesWritten + newStats.KeysWritten = keysWritten + now := time.Now().Second() + interval := &pdpb.TimeInterval{StartTimestamp: uint64(now - statistics.StoreHeartBeatReportInterval), EndTimestamp: uint64(now)} + newStats.Interval = interval + newStore := store.Clone(core.SetStoreStats(newStats)) + mc.Set(storeID, newStats) + mc.PutStore(newStore) +} + +// UpdateStorageReadStats updates store written bytes. +func (mc *Cluster) UpdateStorageReadStats(storeID, bytesWritten, keysWritten uint64) { + store := mc.GetStore(storeID) + newStats := proto.Clone(store.GetStoreStats()).(*pdpb.StoreStats) + newStats.BytesRead = bytesWritten + newStats.KeysRead = keysWritten + now := time.Now().Second() + interval := &pdpb.TimeInterval{StartTimestamp: uint64(now - statistics.StoreHeartBeatReportInterval), EndTimestamp: uint64(now)} + newStats.Interval = interval + newStore := store.Clone(core.SetStoreStats(newStats)) + mc.Set(storeID, newStats) + mc.PutStore(newStore) +} + // UpdateStorageWrittenBytes updates store written bytes. func (mc *Cluster) UpdateStorageWrittenBytes(storeID uint64, bytesWritten uint64) { store := mc.GetStore(storeID) newStats := proto.Clone(store.GetStoreStats()).(*pdpb.StoreStats) newStats.BytesWritten = bytesWritten + newStats.KeysWritten = bytesWritten / 100 now := time.Now().Second() interval := &pdpb.TimeInterval{StartTimestamp: uint64(now - statistics.StoreHeartBeatReportInterval), EndTimestamp: uint64(now)} newStats.Interval = interval @@ -411,6 +440,7 @@ func (mc *Cluster) UpdateStorageReadBytes(storeID uint64, bytesRead uint64) { store := mc.GetStore(storeID) newStats := proto.Clone(store.GetStoreStats()).(*pdpb.StoreStats) newStats.BytesRead = bytesRead + newStats.KeysRead = bytesRead / 100 now := time.Now().Second() interval := &pdpb.TimeInterval{StartTimestamp: uint64(now - statistics.StoreHeartBeatReportInterval), EndTimestamp: uint64(now)} newStats.Interval = interval @@ -424,6 +454,7 @@ func (mc *Cluster) UpdateStorageWrittenKeys(storeID uint64, keysWritten uint64) store := mc.GetStore(storeID) newStats := proto.Clone(store.GetStoreStats()).(*pdpb.StoreStats) newStats.KeysWritten = keysWritten + newStats.BytesWritten = keysWritten * 100 now := time.Now().Second() interval := &pdpb.TimeInterval{StartTimestamp: uint64(now - statistics.StoreHeartBeatReportInterval), EndTimestamp: uint64(now)} newStats.Interval = interval @@ -437,6 +468,7 @@ func (mc *Cluster) UpdateStorageReadKeys(storeID uint64, keysRead uint64) { store := mc.GetStore(storeID) newStats := proto.Clone(store.GetStoreStats()).(*pdpb.StoreStats) newStats.KeysRead = keysRead + newStats.BytesRead = keysRead * 100 now := time.Now().Second() interval := &pdpb.TimeInterval{StartTimestamp: uint64(now - statistics.StoreHeartBeatReportInterval), EndTimestamp: uint64(now)} newStats.Interval = interval diff --git a/server/schedulers/hot_region.go b/server/schedulers/hot_region.go index 78fae785b0b..feb471db9ca 100644 --- a/server/schedulers/hot_region.go +++ b/server/schedulers/hot_region.go @@ -264,6 +264,9 @@ func summaryStoresLoad( kind core.ResourceKind, ) map[uint64]*storeLoadDetail { loadDetail := make(map[uint64]*storeLoadDetail, len(storeByteRate)) + allByteSum := 0.0 + allKeySum := 0.0 + allCount := 0.0 // Stores without byte rate statistics is not available to schedule. for id, byteRate := range storeByteRate { @@ -295,6 +298,9 @@ func summaryStoresLoad( hotPeerSummary.WithLabelValues(ty, fmt.Sprintf("%v", id)).Set(keySum) } } + allByteSum += byteRate + allKeySum += keyRate + allCount += float64(len(hotPeers)) // Build store load prediction from current load and pending influence. stLoadPred := (&storeLoad{ @@ -309,6 +315,29 @@ func summaryStoresLoad( HotPeers: hotPeers, } } + storeLen := float64(len(storeByteRate)) + + for id, detail := range loadDetail { + byteExp := allByteSum / storeLen + keyExp := allKeySum / storeLen + countExp := allCount / storeLen + detail.LoadPred.Future.ExpByteRate = byteExp + detail.LoadPred.Future.ExpKeyRate = keyExp + detail.LoadPred.Future.ExpCount = countExp + // Debug + { + ty := "exp-byte-rate-" + rwTy.String() + "-" + kind.String() + hotPeerSummary.WithLabelValues(ty, fmt.Sprintf("%v", id)).Set(byteExp) + } + { + ty := "exp-key-rate-" + rwTy.String() + "-" + kind.String() + hotPeerSummary.WithLabelValues(ty, fmt.Sprintf("%v", id)).Set(keyExp) + } + { + ty := "exp-count-rate-" + rwTy.String() + "-" + kind.String() + hotPeerSummary.WithLabelValues(ty, fmt.Sprintf("%v", id)).Set(countExp) + } + } return loadDetail } @@ -553,7 +582,12 @@ func (bs *balanceSolver) filterSrcStores() map[uint64]*storeLoadDetail { if len(detail.HotPeers) == 0 { continue } - ret[id] = detail + if detail.LoadPred.min().ByteRate > bs.sche.conf.GetToleranceRatio()*detail.LoadPred.Future.ExpByteRate && + detail.LoadPred.min().KeyRate > bs.sche.conf.GetToleranceRatio()*detail.LoadPred.Future.ExpKeyRate { + ret[id] = detail + balanceHotRegionCounter.WithLabelValues("src-store-succ", strconv.FormatUint(id, 10)).Inc() + } + balanceHotRegionCounter.WithLabelValues("src-store-failed", strconv.FormatUint(id, 10)).Inc() } return ret } @@ -716,6 +750,13 @@ func (bs *balanceSolver) filterDstStores() map[uint64]*storeLoadDetail { ret := make(map[uint64]*storeLoadDetail, len(candidates)) for _, store := range candidates { if filter.Target(bs.cluster, store, filters) { + detail := bs.stLoadDetail[store.GetID()] + if detail.LoadPred.max().ByteRate*bs.sche.conf.GetToleranceRatio() < detail.LoadPred.Future.ExpByteRate && + detail.LoadPred.max().KeyRate*bs.sche.conf.GetToleranceRatio() < detail.LoadPred.Future.ExpKeyRate { + ret[store.GetID()] = bs.stLoadDetail[store.GetID()] + balanceHotRegionCounter.WithLabelValues("dst-store-succ", strconv.FormatUint(store.GetID(), 10)).Inc() + } + balanceHotRegionCounter.WithLabelValues("dst-store-fail", strconv.FormatUint(store.GetID(), 10)).Inc() ret[store.GetID()] = bs.stLoadDetail[store.GetID()] } } @@ -731,9 +772,8 @@ func (bs *balanceSolver) calcProgressiveRank() { rank := int64(0) if bs.rwTy == write && bs.opTy == transferLeader { // In this condition, CPU usage is the matter. - // Only consider about count and key rate. - if srcLd.Count > dstLd.Count && - srcLd.KeyRate >= dstLd.KeyRate+peer.GetKeyRate() { + // Only consider about key rate. + if srcLd.KeyRate >= dstLd.KeyRate+peer.GetKeyRate() { rank = -1 } } else { diff --git a/server/schedulers/hot_region_config.go b/server/schedulers/hot_region_config.go index 2db258ed44e..2c72e8046d8 100644 --- a/server/schedulers/hot_region_config.go +++ b/server/schedulers/hot_region_config.go @@ -42,6 +42,7 @@ func initHotRegionScheduleConfig() *hotRegionSchedulerConfig { GreatDecRatio: 0.95, MinorDecRatio: 0.99, MaxPeerNum: 1000, + ToleranceRatio: 1.02, // Tolerate 2% difference } } @@ -61,6 +62,7 @@ type hotRegionSchedulerConfig struct { CountRankStepRatio float64 `json:"count-rank-step-ratio"` GreatDecRatio float64 `json:"great-dec-ratio"` MinorDecRatio float64 `json:"minor-dec-ratio"` + ToleranceRatio float64 `json:"tolerance-ratio"` } func (conf *hotRegionSchedulerConfig) EncodeConfig() ([]byte, error) { @@ -81,6 +83,18 @@ func (conf *hotRegionSchedulerConfig) GetMaxPeerNumber() int { return conf.MaxPeerNum } +func (conf *hotRegionSchedulerConfig) GetToleranceRatio() float64 { + conf.RLock() + defer conf.RUnlock() + return conf.ToleranceRatio +} + +func (conf *hotRegionSchedulerConfig) SetToleranceRatio(tol float64) { + conf.Lock() + defer conf.Unlock() + conf.ToleranceRatio = tol +} + func (conf *hotRegionSchedulerConfig) GetByteRankStepRatio() float64 { conf.RLock() defer conf.RUnlock() diff --git a/server/schedulers/hot_test.go b/server/schedulers/hot_test.go index 5e5e9adff4b..a3999da5b47 100644 --- a/server/schedulers/hot_test.go +++ b/server/schedulers/hot_test.go @@ -298,6 +298,7 @@ func (s *testHotWriteRegionSchedulerSuite) TestWithKeyRate(c *C) { opt := mockoption.NewScheduleOptions() hb, err := schedule.CreateScheduler(HotWriteRegionType, schedule.NewOperatorController(ctx, nil, nil), core.NewStorage(kv.NewMemoryKV()), nil) c.Assert(err, IsNil) + hb.(*hotScheduler).conf.SetToleranceRatio(1) opt.HotRegionCacheHitsThreshold = 0 tc := mockcluster.NewCluster(opt) @@ -307,17 +308,11 @@ func (s *testHotWriteRegionSchedulerSuite) TestWithKeyRate(c *C) { tc.AddRegionStore(4, 20) tc.AddRegionStore(5, 20) - tc.UpdateStorageWrittenBytes(1, 10.5*MB*statistics.StoreHeartBeatReportInterval) - tc.UpdateStorageWrittenBytes(2, 9.5*MB*statistics.StoreHeartBeatReportInterval) - tc.UpdateStorageWrittenBytes(3, 9.5*MB*statistics.StoreHeartBeatReportInterval) - tc.UpdateStorageWrittenBytes(4, 9*MB*statistics.StoreHeartBeatReportInterval) - tc.UpdateStorageWrittenBytes(5, 8.9*MB*statistics.StoreHeartBeatReportInterval) - - tc.UpdateStorageWrittenKeys(1, 10*MB*statistics.StoreHeartBeatReportInterval) - tc.UpdateStorageWrittenKeys(2, 9.5*MB*statistics.StoreHeartBeatReportInterval) - tc.UpdateStorageWrittenKeys(3, 9.8*MB*statistics.StoreHeartBeatReportInterval) - tc.UpdateStorageWrittenKeys(4, 9*MB*statistics.StoreHeartBeatReportInterval) - tc.UpdateStorageWrittenKeys(5, 9.2*MB*statistics.StoreHeartBeatReportInterval) + tc.UpdateStorageWrittenStats(1, 10.5*MB*statistics.StoreHeartBeatReportInterval, 10.2*MB*statistics.StoreHeartBeatReportInterval) + tc.UpdateStorageWrittenStats(2, 9.5*MB*statistics.StoreHeartBeatReportInterval, 9.5*MB*statistics.StoreHeartBeatReportInterval) + tc.UpdateStorageWrittenStats(3, 9.5*MB*statistics.StoreHeartBeatReportInterval, 9.8*MB*statistics.StoreHeartBeatReportInterval) + tc.UpdateStorageWrittenStats(4, 9*MB*statistics.StoreHeartBeatReportInterval, 9*MB*statistics.StoreHeartBeatReportInterval) + tc.UpdateStorageWrittenStats(5, 8.9*MB*statistics.StoreHeartBeatReportInterval, 9.2*MB*statistics.StoreHeartBeatReportInterval) addRegionInfo(tc, write, []testRegionInfo{ {1, []uint64{2, 1, 3}, 0.5 * MB, 0.5 * MB}, @@ -331,19 +326,19 @@ func (s *testHotWriteRegionSchedulerSuite) TestWithKeyRate(c *C) { // byteDecRatio <= 0.95 && keyDecRatio <= 0.95 testutil.CheckTransferPeer(c, op, operator.OpHotRegion, 1, 4) // store byte rate (min, max): (10, 10.5) | 9.5 | 9.5 | (9, 9.5) | 8.9 - // store key rate (min, max): (9.5, 10) | 9.5 | 9.8 | (9, 9.5) | 9.2 + // store key rate (min, max): (9.7, 10.2) | 9.5 | 9.8 | (9, 9.5) | 9.2 op = hb.Schedule(tc)[0] // byteDecRatio <= 0.99 && keyDecRatio <= 0.95 testutil.CheckTransferPeer(c, op, operator.OpHotRegion, 3, 5) // store byte rate (min, max): (10, 10.5) | 9.5 | (9.45, 9.5) | (9, 9.5) | (8.9, 8.95) - // store key rate (min, max): (9.5, 10) | 9.5 | (9.7, 9.8) | (9, 9.5) | (9.2, 9.3) + // store key rate (min, max): (9.7, 10.2) | 9.5 | (9.7, 9.8) | (9, 9.5) | (9.2, 9.3) op = hb.Schedule(tc)[0] // byteDecRatio <= 0.95 testutil.CheckTransferPeer(c, op, operator.OpHotRegion, 1, 5) // store byte rate (min, max): (9.5, 10.5) | 9.5 | (9.45, 9.5) | (9, 9.5) | (8.9, 9.45) - // store key rate (min, max): (9, 10) | 9.5 | (9.7, 9.8) | (9, 9.5) | (9.2, 9.8) + // store key rate (min, max): (9.2, 10.2) | 9.5 | (9.7, 9.8) | (9, 9.5) | (9.2, 9.8) } } @@ -586,6 +581,7 @@ func (s *testHotReadRegionSchedulerSuite) TestWithKeyRate(c *C) { opt := mockoption.NewScheduleOptions() hb, err := schedule.CreateScheduler(HotReadRegionType, schedule.NewOperatorController(ctx, nil, nil), core.NewStorage(kv.NewMemoryKV()), nil) c.Assert(err, IsNil) + hb.(*hotScheduler).conf.SetToleranceRatio(1) opt.HotRegionCacheHitsThreshold = 0 tc := mockcluster.NewCluster(opt) @@ -595,17 +591,11 @@ func (s *testHotReadRegionSchedulerSuite) TestWithKeyRate(c *C) { tc.AddRegionStore(4, 20) tc.AddRegionStore(5, 20) - tc.UpdateStorageReadBytes(1, 10.5*MB*statistics.StoreHeartBeatReportInterval) - tc.UpdateStorageReadBytes(2, 9.5*MB*statistics.StoreHeartBeatReportInterval) - tc.UpdateStorageReadBytes(3, 9.5*MB*statistics.StoreHeartBeatReportInterval) - tc.UpdateStorageReadBytes(4, 9*MB*statistics.StoreHeartBeatReportInterval) - tc.UpdateStorageReadBytes(5, 8.9*MB*statistics.StoreHeartBeatReportInterval) - - tc.UpdateStorageReadKeys(1, 10*MB*statistics.StoreHeartBeatReportInterval) - tc.UpdateStorageReadKeys(2, 9.5*MB*statistics.StoreHeartBeatReportInterval) - tc.UpdateStorageReadKeys(3, 9.8*MB*statistics.StoreHeartBeatReportInterval) - tc.UpdateStorageReadKeys(4, 9*MB*statistics.StoreHeartBeatReportInterval) - tc.UpdateStorageReadKeys(5, 9.2*MB*statistics.StoreHeartBeatReportInterval) + tc.UpdateStorageReadStats(1, 10.5*MB*statistics.StoreHeartBeatReportInterval, 10.2*MB*statistics.StoreHeartBeatReportInterval) + tc.UpdateStorageReadStats(2, 9.5*MB*statistics.StoreHeartBeatReportInterval, 9.5*MB*statistics.StoreHeartBeatReportInterval) + tc.UpdateStorageReadStats(3, 9.5*MB*statistics.StoreHeartBeatReportInterval, 9.8*MB*statistics.StoreHeartBeatReportInterval) + tc.UpdateStorageReadStats(4, 9*MB*statistics.StoreHeartBeatReportInterval, 9*MB*statistics.StoreHeartBeatReportInterval) + tc.UpdateStorageReadStats(5, 8.9*MB*statistics.StoreHeartBeatReportInterval, 9.2*MB*statistics.StoreHeartBeatReportInterval) addRegionInfo(tc, read, []testRegionInfo{ {1, []uint64{1, 2, 4}, 0.5 * MB, 0.5 * MB}, @@ -619,19 +609,19 @@ func (s *testHotReadRegionSchedulerSuite) TestWithKeyRate(c *C) { // byteDecRatio <= 0.95 && keyDecRatio <= 0.95 testutil.CheckTransferLeader(c, op, operator.OpHotRegion, 1, 4) // store byte rate (min, max): (10, 10.5) | 9.5 | 9.5 | (9, 9.5) | 8.9 - // store key rate (min, max): (9.5, 10) | 9.5 | 9.8 | (9, 9.5) | 9.2 + // store key rate (min, max): (9.7, 10.2) | 9.5 | 9.8 | (9, 9.5) | 9.2 op = hb.Schedule(tc)[0] // byteDecRatio <= 0.99 && keyDecRatio <= 0.95 testutil.CheckTransferLeader(c, op, operator.OpHotRegion, 3, 5) // store byte rate (min, max): (10, 10.5) | 9.5 | (9.45, 9.5) | (9, 9.5) | (8.9, 8.95) - // store key rate (min, max): (9.5, 10) | 9.5 | (9.7, 9.8) | (9, 9.5) | (9.2, 9.3) + // store key rate (min, max): (9.7, 10.2) | 9.5 | (9.7, 9.8) | (9, 9.5) | (9.2, 9.3) op = hb.Schedule(tc)[0] // byteDecRatio <= 0.95 testutil.CheckTransferPeerWithLeaderTransfer(c, op, operator.OpHotRegion, 1, 5) // store byte rate (min, max): (9.5, 10.5) | 9.5 | (9.45, 9.5) | (9, 9.5) | (8.9, 9.45) - // store key rate (min, max): (9, 10) | 9.5 | (9.7, 9.8) | (9, 9.5) | (9.2, 9.8) + // store key rate (min, max): (9.2, 10.2) | 9.5 | (9.7, 9.8) | (9, 9.5) | (9.2, 9.8) } } diff --git a/server/schedulers/utils.go b/server/schedulers/utils.go index 0baabb52ffc..4b33cb7f208 100644 --- a/server/schedulers/utils.go +++ b/server/schedulers/utils.go @@ -208,6 +208,10 @@ type storeLoad struct { ByteRate float64 KeyRate float64 Count float64 + + ExpByteRate float64 + ExpKeyRate float64 + ExpCount float64 } func (load *storeLoad) ToLoadPred(infl Influence) *storeLoadPred { diff --git a/tools/pd-ctl/pdctl/command/completion_command.go b/tools/pd-ctl/pdctl/command/completion_command.go new file mode 100644 index 00000000000..ffd0fc835ff --- /dev/null +++ b/tools/pd-ctl/pdctl/command/completion_command.go @@ -0,0 +1,81 @@ +// Copyright 2017 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, +// See the License for the specific language governing permissions and +// limitations under the License. + +package command + +import ( + "os" + + "github.com/spf13/cobra" +) + +const ( + completionLongDesc = ` +Output shell completion code for the specified shell (bash). +The shell code must be evaluated to provide interactive +completion of pd-ctl commands. This can be done by sourcing it from +the .bash_profile. +` + + completionExample = ` + # Installing bash completion on macOS using homebrew + ## If running Bash 3.2 included with macOS + brew install bash-completion + ## or, if running Bash 4.1+ + brew install bash-completion@2 + ## If tkctl is installed via homebrew, this should start working immediately. + ## If you've installed via other means, you may need add the completion to your completion directory + tkctl completion bash > $(brew --prefix)/etc/bash_completion.d/tkctl + + + # Installing bash completion on Linux + ## If bash-completion is not installed on Linux, please install the 'bash-completion' package + ## via your distribution's package manager. + ## Load the pd-ctl completion code for bash into the current shell + source <(pd-ctl completion bash) + ## Write bash completion code to a file and source if from .bash_profile + pd-ctl completion bash > ~/completion.bash.inc + printf " + # pd-ctl shell completion + source '$HOME/completion.bash.inc' + " >> $HOME/.bash_profile + source $HOME/.bash_profile +` +) + +// NewCompletionCommand return a completion subcommand of root command +func NewCompletionCommand() *cobra.Command { + cmd := &cobra.Command{ + Use: "completion SHELL", + DisableFlagsInUseLine: true, + Short: "Output shell completion code for the specified shell (bash)", + Long: completionLongDesc, + Example: completionExample, + Run: func(cmd *cobra.Command, args []string) { + if len(args) == 1 && args[0] != "bash" { + cmd.Printf("Unsupported shell type %s.\n", args[0]) + cmd.Usage() + return + } + if len(args) > 1 { + cmd.Println("Too many arguments. Expected only the shell type.") + cmd.Usage() + return + } + cmd.Root().GenBashCompletion(os.Stdout) + }, + ValidArgs: []string{"bash"}, + } + + return cmd +} diff --git a/tools/pd-ctl/pdctl/ctl.go b/tools/pd-ctl/pdctl/ctl.go index 19f31146cd6..1f06437bf62 100644 --- a/tools/pd-ctl/pdctl/ctl.go +++ b/tools/pd-ctl/pdctl/ctl.go @@ -86,6 +86,7 @@ func getBasicCmd() *cobra.Command { command.NewLogCommand(), command.NewPluginCommand(), command.NewComponentCommand(), + command.NewCompletionCommand(), ) rootCmd.Flags().ParseErrorsWhitelist.UnknownFlags = true From bb58bf243c06b01444fdcc6a7502aceaedf265ba Mon Sep 17 00:00:00 2001 From: Qiannan Lyu Date: Sun, 29 Mar 2020 16:53:38 +1300 Subject: [PATCH 2/3] auto-completion Signed-off-by: Qiannan Lyu --- tools/pd-ctl/pdctl/command/completion_command.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/tools/pd-ctl/pdctl/command/completion_command.go b/tools/pd-ctl/pdctl/command/completion_command.go index ffd0fc835ff..debf8a0fd22 100644 --- a/tools/pd-ctl/pdctl/command/completion_command.go +++ b/tools/pd-ctl/pdctl/command/completion_command.go @@ -33,9 +33,8 @@ the .bash_profile. brew install bash-completion ## or, if running Bash 4.1+ brew install bash-completion@2 - ## If tkctl is installed via homebrew, this should start working immediately. ## If you've installed via other means, you may need add the completion to your completion directory - tkctl completion bash > $(brew --prefix)/etc/bash_completion.d/tkctl + pd-ctl completion bash > $(brew --prefix)/etc/bash_completion.d/pd-ctl # Installing bash completion on Linux @@ -62,13 +61,12 @@ func NewCompletionCommand() *cobra.Command { Long: completionLongDesc, Example: completionExample, Run: func(cmd *cobra.Command, args []string) { - if len(args) == 1 && args[0] != "bash" { - cmd.Printf("Unsupported shell type %s.\n", args[0]) + if len(args) != 1 { cmd.Usage() return } - if len(args) > 1 { - cmd.Println("Too many arguments. Expected only the shell type.") + if args[0] != "bash" { + cmd.Printf("Unsupported shell type %s. \n", args[0]) cmd.Usage() return } From 34d1a4fee0a01fb30386003fb5686b7b34ba2570 Mon Sep 17 00:00:00 2001 From: Qiannan Lyu Date: Sun, 29 Mar 2020 17:52:49 +1300 Subject: [PATCH 3/3] add unit-test Signed-off-by: Qiannan Lyu --- tests/pdctl/completion/completion_test.go | 43 ++++ tests/pdctl/helper.go | 1 + .../pdctl/command/completion_command.go | 230 ++++++++++++++++-- 3 files changed, 259 insertions(+), 15 deletions(-) create mode 100644 tests/pdctl/completion/completion_test.go diff --git a/tests/pdctl/completion/completion_test.go b/tests/pdctl/completion/completion_test.go new file mode 100644 index 00000000000..d69fb9f4411 --- /dev/null +++ b/tests/pdctl/completion/completion_test.go @@ -0,0 +1,43 @@ +// Copyright 2020 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, +// See the License for the specific language governing permissions and +// limitations under the License. + +package completion_test + +import ( + "testing" + + . "github.com/pingcap/check" + "github.com/pingcap/pd/v4/tests/pdctl" +) + +func Test(t *testing.T) { + TestingT(t) +} + +var _ = Suite(&completionTestSuite{}) + +type completionTestSuite struct{} + +func (s *completionTestSuite) TestCompletion(c *C) { + cmd := pdctl.InitCommand() + + // completion command + args := []string{"completion", "bash"} + _, _, err := pdctl.ExecuteCommandC(cmd, args...) + c.Assert(err, IsNil) + + // completion command + args = []string{"completion", "zsh"} + _, _, err = pdctl.ExecuteCommandC(cmd, args...) + c.Assert(err, IsNil) +} diff --git a/tests/pdctl/helper.go b/tests/pdctl/helper.go index 760d446d178..601d327537f 100644 --- a/tests/pdctl/helper.go +++ b/tests/pdctl/helper.go @@ -63,6 +63,7 @@ func InitCommand() *cobra.Command { command.NewLogCommand(), command.NewPluginCommand(), command.NewComponentCommand(), + command.NewCompletionCommand(), ) return rootCmd } diff --git a/tools/pd-ctl/pdctl/command/completion_command.go b/tools/pd-ctl/pdctl/command/completion_command.go index debf8a0fd22..1482c1079d3 100644 --- a/tools/pd-ctl/pdctl/command/completion_command.go +++ b/tools/pd-ctl/pdctl/command/completion_command.go @@ -1,4 +1,4 @@ -// Copyright 2017 PingCAP, Inc. +// Copyright 2020 PingCAP, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -14,6 +14,8 @@ package command import ( + "bytes" + "io" "os" "github.com/spf13/cobra" @@ -21,10 +23,12 @@ import ( const ( completionLongDesc = ` -Output shell completion code for the specified shell (bash). +Output shell completion code for the specified shell (bash or zsh). The shell code must be evaluated to provide interactive completion of pd-ctl commands. This can be done by sourcing it from the .bash_profile. + +Note for zsh users: [1] zsh completions are only supported in versions of zsh >= 5.2 ` completionExample = ` @@ -49,31 +53,227 @@ the .bash_profile. source '$HOME/completion.bash.inc' " >> $HOME/.bash_profile source $HOME/.bash_profile + + # Load the pd-ctl completion code for zsh[1] into the current shell + source <(pd-ctl completion zsh) + # Set the pd-ctl completion code for zsh[1] to autoload on startup + pd-ctl completion zsh > "${fpath[1]}/_pd-ctl" ` ) +var ( + completionShells = map[string]func(out io.Writer, cmd *cobra.Command) error{ + "bash": runCompletionBash, + "zsh": runCompletionZsh, + } +) + // NewCompletionCommand return a completion subcommand of root command func NewCompletionCommand() *cobra.Command { + shells := []string{} + for s := range completionShells { + shells = append(shells, s) + } + cmd := &cobra.Command{ Use: "completion SHELL", DisableFlagsInUseLine: true, Short: "Output shell completion code for the specified shell (bash)", Long: completionLongDesc, Example: completionExample, - Run: func(cmd *cobra.Command, args []string) { - if len(args) != 1 { - cmd.Usage() - return - } - if args[0] != "bash" { - cmd.Printf("Unsupported shell type %s. \n", args[0]) - cmd.Usage() - return - } - cmd.Root().GenBashCompletion(os.Stdout) - }, - ValidArgs: []string{"bash"}, + Run: RunCompletion, + ValidArgs: shells, } return cmd } + +// RunCompletion wrapped the bash and zsh completion scripts +func RunCompletion(cmd *cobra.Command, args []string) { + if len(args) == 0 { + cmd.Println("Shell not specified.") + cmd.Usage() + return + } + if len(args) > 1 { + cmd.Println("Too many arguments. Expected only the shell type.") + cmd.Usage() + return + } + run, found := completionShells[args[0]] + if !found { + cmd.Printf("Unsupported shell type %q.\n", args[0]) + cmd.Usage() + return + } + + run(os.Stdout, cmd.Root()) +} + +func runCompletionBash(out io.Writer, cmd *cobra.Command) error { + return cmd.GenBashCompletion(out) +} + +func runCompletionZsh(out io.Writer, cmd *cobra.Command) error { + zshHead := "#compdef pd-ctl\n" + + out.Write([]byte(zshHead)) + + zshInitialization := ` +__pd-ctl_bash_source() { + alias shopt=':' + alias _expand=_bash_expand + alias _complete=_bash_comp + emulate -L sh + setopt kshglob noshglob braceexpand + + source "$@" +} + +__pd-ctl_type() { + # -t is not supported by zsh + if [ "$1" == "-t" ]; then + shift + + # fake Bash 4 to disable "complete -o nospace". Instead + # "compopt +-o nospace" is used in the code to toggle trailing + # spaces. We don't support that, but leave trailing spaces on + # all the time + if [ "$1" = "__pd-ctl_compopt" ]; then + echo builtin + return 0 + fi + fi + type "$@" +} + +__pd-ctl_compgen() { + local completions w + completions=( $(compgen "$@") ) || return $? + + # filter by given word as prefix + while [[ "$1" = -* && "$1" != -- ]]; do + shift + shift + done + if [[ "$1" == -- ]]; then + shift + fi + for w in "${completions[@]}"; do + if [[ "${w}" = "$1"* ]]; then + echo "${w}" + fi + done +} + +__pd-ctl_compopt() { + true # don't do anything. Not supported by bashcompinit in zsh +} + +__pd-ctl_ltrim_colon_completions() +{ + if [[ "$1" == *:* && "$COMP_WORDBREAKS" == *:* ]]; then + # Remove colon-word prefix from COMPREPLY items + local colon_word=${1%${1##*:}} + local i=${#COMPREPLY[*]} + while [[ $((--i)) -ge 0 ]]; do + COMPREPLY[$i]=${COMPREPLY[$i]#"$colon_word"} + done + fi +} + +__pd-ctl_get_comp_words_by_ref() { + cur="${COMP_WORDS[COMP_CWORD]}" + prev="${COMP_WORDS[${COMP_CWORD}-1]}" + words=("${COMP_WORDS[@]}") + cword=("${COMP_CWORD[@]}") +} + +__pd-ctl_filedir() { + local RET OLD_IFS w qw + + __pd-ctl_debug "_filedir $@ cur=$cur" + if [[ "$1" = \~* ]]; then + # somehow does not work. Maybe, zsh does not call this at all + eval echo "$1" + return 0 + fi + + OLD_IFS="$IFS" + IFS=$'\n' + if [ "$1" = "-d" ]; then + shift + RET=( $(compgen -d) ) + else + RET=( $(compgen -f) ) + fi + IFS="$OLD_IFS" + + IFS="," __pd-ctl_debug "RET=${RET[@]} len=${#RET[@]}" + + for w in ${RET[@]}; do + if [[ ! "${w}" = "${cur}"* ]]; then + continue + fi + if eval "[[ \"\${w}\" = *.$1 || -d \"\${w}\" ]]"; then + qw="$(__pd-ctl_quote "${w}")" + if [ -d "${w}" ]; then + COMPREPLY+=("${qw}/") + else + COMPREPLY+=("${qw}") + fi + fi + done +} + +__pd-ctl_quote() { + if [[ $1 == \'* || $1 == \"* ]]; then + # Leave out first character + printf %q "${1:1}" + else + printf %q "$1" + fi +} + +autoload -U +X bashcompinit && bashcompinit + +# use word boundary patterns for BSD or GNU sed +LWORD='[[:<:]]' +RWORD='[[:>:]]' +if sed --help 2>&1 | grep -q GNU; then + LWORD='\<' + RWORD='\>' +fi + +__pd-ctl_convert_bash_to_zsh() { + sed \ + -e 's/declare -F/whence -w/' \ + -e 's/_get_comp_words_by_ref "\$@"/_get_comp_words_by_ref "\$*"/' \ + -e 's/local \([a-zA-Z0-9_]*\)=/local \1; \1=/' \ + -e 's/flags+=("\(--.*\)=")/flags+=("\1"); two_word_flags+=("\1")/' \ + -e 's/must_have_one_flag+=("\(--.*\)=")/must_have_one_flag+=("\1")/' \ + -e "s/${LWORD}_filedir${RWORD}/__pd-ctl_filedir/g" \ + -e "s/${LWORD}_get_comp_words_by_ref${RWORD}/__pd-ctl_get_comp_words_by_ref/g" \ + -e "s/${LWORD}__ltrim_colon_completions${RWORD}/__pd-ctl_ltrim_colon_completions/g" \ + -e "s/${LWORD}compgen${RWORD}/__pd-ctl_compgen/g" \ + -e "s/${LWORD}compopt${RWORD}/__pd-ctl_compopt/g" \ + -e "s/${LWORD}declare${RWORD}/builtin declare/g" \ + -e "s/\\\$(type${RWORD}/\$(__pd-ctl_type/g" \ + <<'BASH_COMPLETION_EOF' +` + out.Write([]byte(zshInitialization)) + + buf := new(bytes.Buffer) + cmd.GenBashCompletion(buf) + out.Write(buf.Bytes()) + + zshTail := ` +BASH_COMPLETION_EOF +} + +__pd-ctl_bash_source <(__pd-ctl_convert_bash_to_zsh) +_complete pd-ctl 2>/dev/null +` + out.Write([]byte(zshTail)) + return nil +}