Skip to content
This repository has been archived by the owner on Mar 26, 2020. It is now read-only.

Rebalance missing validation and code refactor #986

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
10 changes: 5 additions & 5 deletions doc/endpoints.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

76 changes: 76 additions & 0 deletions glustercli/cmd/rebalance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package cmd

import (
"fmt"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

const (
helpRebalanceCmd = "Gluster Rebalance"
helpRebalanceStartCmd = "Start rebalance operation for gluster volume"
helpRebalanceStatusCmd = "Status of rebalance operation"
helpRebalanceStopCmd = "Stop rebalance operation"
)

var (
flagRebalanceStartCmdForce bool
flagRebalanceStartCmdFixLayout bool
)

var rebalanceCmd = &cobra.Command{
Use: "volume rebalance",
Copy link
Member

Choose a reason for hiding this comment

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

where ever its required place add Long command to give more information about this CLI command to users

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok

Short: helpRebalanceCmd,
}

func init() {

// Rebalance Start
rebalanceStartCmd.Flags().BoolVar(&flagRebalanceStartCmdForce, "force", false, "Force")
Copy link
Contributor

Choose a reason for hiding this comment

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

The description of the flags can be a little bit more elaborate.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For now I am following the trend set by volumestart, will update both of them with more description in the next PR

rebalanceStartCmd.Flags().BoolVar(&flagRebalanceStartCmdFixLayout, "fixlayout", false, "FixLayout")
rebalanceCmd.AddCommand(rebalanceStartCmd)

RootCmd.AddCommand(rebalanceCmd)
}

var rebalanceStartCmd = &cobra.Command{
Use: "<VOLNAME> start [flags]",
Short: helpRebalanceStartCmd,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
volname := cmd.Flags().Args()[0]
var err error
if flagRebalanceStartCmdForce {
err = client.RebalanceStart(volname, "force")
} else if flagRebalanceStartCmdFixLayout {
err = client.RebalanceStart(volname, "fix-layout")
} else {
err = client.RebalanceStart(volname, "")
}
if err != nil {
if verbose {
log.WithError(err).WithField("volume", volname).Error("rebalance start failed")
}
failure("rebalance start failed", err, 1)
}
fmt.Printf("Rebalance for %s started successfully\n", volname)
},
}

var rebalanceStopCmd = &cobra.Command{
Use: "<VOLNAME> stop",
Short: helpRebalanceStopCmd,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
volname := cmd.Flags().Args()[0]
err := client.RebalanceStop(volname)
if err != nil {
if verbose {
log.WithError(err).WithField("volume", volname).Error("Rebalance stop failed")
}
failure("rebalance start failed", err, 1)
}
fmt.Printf("Rebalance for %s stopped successfully\n", volname)
},
}
23 changes: 5 additions & 18 deletions glustercli/cmd/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,7 @@ var volumeStartCmd = &cobra.Command{
err := client.VolumeStart(volname, flagStartCmdForce)
if err != nil {
if verbose {
log.WithFields(log.Fields{
"volume": volname,
"error": err.Error(),
}).Error("volume start failed")
log.WithError(err).WithField("volume", volname).Error("volume start failed")
}
failure("volume start failed", err, 1)
}
Expand All @@ -203,10 +200,7 @@ var volumeStopCmd = &cobra.Command{
err := client.VolumeStop(volname)
if err != nil {
if verbose {
log.WithFields(log.Fields{
"volume": volname,
"error": err.Error(),
}).Error("volume stop failed")
log.WithError(err).WithField("volume", volname).Error("volume stop failed")
}
failure("Volume stop failed", err, 1)
}
Expand All @@ -223,10 +217,7 @@ var volumeDeleteCmd = &cobra.Command{
err := client.VolumeDelete(volname)
if err != nil {
if verbose {
log.WithFields(log.Fields{
"volume": volname,
"error": err.Error(),
}).Error("volume deletion failed")
log.WithError(err).WithField("volume", volname).Error("volume deletion failed")
}
failure("Volume deletion failed", err, 1)
}
Expand Down Expand Up @@ -380,9 +371,7 @@ var volumeInfoCmd = &cobra.Command{
err := volumeInfoHandler2(cmd, true)
if err != nil {
if verbose {
log.WithFields(log.Fields{
"error": err.Error(),
}).Error("error getting volumes list")
log.WithError(err).Error("error getting volumes list")
}
failure("Error getting Volumes list", err, 1)
}
Expand All @@ -397,9 +386,7 @@ var volumeListCmd = &cobra.Command{
err := volumeInfoHandler2(cmd, false)
if err != nil {
if verbose {
log.WithFields(log.Fields{
"error": err.Error(),
}).Error("error getting volumes list")
log.WithError(err).Error("error getting volumes list")
}
failure("Error getting Volumes list", err, 1)
}
Expand Down
23 changes: 23 additions & 0 deletions pkg/restclient/rebalance.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package restclient

import (
"fmt"
"net/http"

rebalanceapi "github.com/gluster/glusterd2/plugins/rebalance/api"
)

// RebalanceStart starts rebalance process for given volume
func (c *Client) RebalanceStart(volname string, option string) error {
req := rebalanceapi.StartReq{
Option: option,
}
url := fmt.Sprintf("/v1/volumes/%s/rebalance/start", volname)
Copy link
Member

@Madhu-1 Madhu-1 Jul 12, 2018

Choose a reason for hiding this comment

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

missing import fmt package

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

return c.post(url, req, http.StatusOK, nil)
Copy link
Member

Choose a reason for hiding this comment

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

same here, where are you importing HTTP package?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

}

// RebalanceStop stops rebalance process for given volume
func (c *Client) RebalanceStop(volname string) error {
url := fmt.Sprintf("/v1/volumes/%s/rebalance/stop", volname)
return c.post(url, nil, http.StatusOK, nil)
}
1 change: 1 addition & 0 deletions plugins/rebalance/api/req_resp.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type RebalInfo struct {
// RebalStatus represents the rebalance status response
type RebalStatus struct {
Volname string `json:"volume"`
State Status `json:"state"`
RebalanceID uuid.UUID `json:"rebalance-id"`
Nodes []RebalNodeStatus `json:"nodes-status"`
}
Expand Down
2 changes: 2 additions & 0 deletions plugins/rebalance/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
var (
// ErrVolNotDistribute : Cannot run rebalance on a non distribute volume
ErrVolNotDistribute = errors.New("not a distribute volume")
// ErrRebalanceAlreadyStarted : Rebalance already started on the volume
ErrRebalanceAlreadyStarted = errors.New("rebalance already started")
// ErrRebalanceNotStarted : Rebalance not started on the volume
ErrRebalanceNotStarted = errors.New("rebalance not started")
// ErrRebalanceInvalidOption : Invalid option provided to the rebalance start command
Expand Down
22 changes: 10 additions & 12 deletions plugins/rebalance/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,20 @@ func (p *Plugin) RestRoutes() route.Routes {
Pattern: "/volumes/{volname}/rebalance/start",
Version: 1,
RequestType: utils.GetTypeString((*rebalanceapi.StartReq)(nil)),
// ResponseType: utils.GetTypeString((*rebalanceapi.RebalInfo)(nil)),
HandlerFunc: rebalanceStartHandler},
route.Route{
Name: "RebalanceStop",
Method: "POST",
Pattern: "/volumes/{volname}/rebalance/stop",
Version: 1,
// ResponseType: utils.GetTypeString((*rebalanceapi.RebalInfo)(nil)),
Name: "RebalanceStop",
Method: "POST",
Pattern: "/volumes/{volname}/rebalance/stop",
Version: 1,
HandlerFunc: rebalanceStopHandler},
route.Route{
Name: "RebalanceStatus",
Method: "GET",
Pattern: "/volumes/{volname}/rebalance",
Version: 1,
// ResponseType: utils.GetTypeString((*rebalanceapi.RebalInfo)(nil)),
HandlerFunc: rebalanceStatusHandler},
Name: "RebalanceStatus",
Method: "GET",
Pattern: "/volumes/{volname}/rebalance",
Version: 1,
ResponseType: utils.GetTypeString((*rebalanceapi.RebalStatus)(nil)),
HandlerFunc: rebalanceStatusHandler},
}
}

Expand Down
Loading