Skip to content

Commit

Permalink
pdctl, api, schedule: pdctl supports scatter region. (tikv#1028)
Browse files Browse the repository at this point in the history
  • Loading branch information
disksing committed May 7, 2018
1 parent 2eacc5a commit 23bc328
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 1 deletion.
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...)
}

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

0 comments on commit 23bc328

Please sign in to comment.