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

pdctl, api, schedule: pdctl supports scatter region. #1028

Merged
merged 3 commits into from
Apr 17, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
29 changes: 29 additions & 0 deletions pdctl/command/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func NewAddOperatorCommand() *cobra.Command {
c.AddCommand(NewRemovePeerCommand())
c.AddCommand(NewMergeRegionCommand())
c.AddCommand(NewSplitRegionCommand())
c.AddCommand(NewScatterRegionCommand())
return c
}

Expand Down Expand Up @@ -286,6 +287,34 @@ func splitRegionCommandFunc(cmd *cobra.Command, args []string) {
postJSON(cmd, operatorsPrefix, input)
}

// NewScatterRegionCommand returns a command to scatter a region.
func NewScatterRegionCommand() *cobra.Command {
c := &cobra.Command{
Use: "scatter-region <region_id>",
Short: "scatter a region",
Run: scatterRegionCommandFunc,
}
return c
}

func scatterRegionCommandFunc(cmd *cobra.Command, args []string) {
if len(args) != 1 {
fmt.Println(cmd.UsageString())
return
}

ids, err := parseUint64s(args)
if err != nil {
fmt.Println(err)
return
}

input := make(map[string]interface{})
input["name"] = cmd.Name()
input["region_id"] = ids[0]
postJSON(cmd, operatorsPrefix, input)
}

// NewRemoveOperatorCommand returns a command to remove operators.
func NewRemoveOperatorCommand() *cobra.Command {
c := &cobra.Command{
Expand Down
10 changes: 10 additions & 0 deletions server/api/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,16 @@ func (h *operatorHandler) Post(w http.ResponseWriter, r *http.Request) {
h.r.JSON(w, http.StatusInternalServerError, err.Error())
return
}
case "scatter-region":
regionID, ok := input["region_id"].(float64)
if !ok {
h.r.JSON(w, http.StatusBadRequest, "missing region id")
return
}
if err := h.AddScatterRegionOperator(uint64(regionID)); err != nil {
h.r.JSON(w, http.StatusInternalServerError, err.Error())
return
}
default:
h.r.JSON(w, http.StatusBadRequest, "unknown operator")
return
Expand Down
22 changes: 22 additions & 0 deletions server/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,28 @@ func (h *Handler) AddSplitRegionOperator(regionID uint64) error {
return nil
}

// AddScatterRegionOperator adds an operator to scatter a region.
func (h *Handler) AddScatterRegionOperator(regionID uint64) error {
c, err := h.getCoordinator()
if err != nil {
return errors.Trace(err)
}

region := c.cluster.GetRegion(regionID)
if region == nil {
return ErrRegionNotFound(regionID)
}

op := c.regionScatterer.Scatter(region)
if op == nil {
return nil
}
if ok := c.addOperator(op); !ok {
return errors.Trace(errAddOperator)
}
return nil
}

// GetDownPeerRegions gets the region with down peer.
func (h *Handler) GetDownPeerRegions() ([]*core.RegionInfo, error) {
c := h.s.GetRaftCluster()
Expand Down
4 changes: 3 additions & 1 deletion server/schedule/region_scatterer.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func (r *RegionScatterer) scatterRegion(region *core.RegionInfo) *Operator {
steps := make([]OperatorStep, 0, len(region.GetPeers()))

stores := r.collectAvailableStores(region)
var kind OperatorKind
for _, peer := range region.GetPeers() {
if len(stores) == 0 {
// Reset selected stores if we have no available stores.
Expand All @@ -124,12 +125,13 @@ func (r *RegionScatterer) scatterRegion(region *core.RegionInfo) *Operator {
peer.GetStoreId(), newPeer.GetStoreId(), newPeer.GetId())
steps = append(steps, op.steps...)
steps = append(steps, TransferLeader{ToStore: newPeer.GetStoreId()})
kind |= op.Kind()
}

if len(steps) == 0 {
return nil
}
return NewOperator("scatter-region", region.GetId(), OpAdmin, steps...)
return NewOperator("scatter-region", region.GetId(), kind, steps...)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the effect of this change, will it limit the schedule rate?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep. To avoid the cluster being too busy.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But we don't want to limit it when using in TiDB Lightning, any way to satisfy both sides?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lightning will not be affected. With this patch, scatter-region operators will occupy the limits of balance schedulers.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see, the ScatterRegion API bypasses the rate limiter.

}

func (r *RegionScatterer) selectPeerToReplace(stores map[uint64]*core.StoreInfo, region *core.RegionInfo, oldPeer *metapb.Peer) *metapb.Peer {
Expand Down