mirror of
https://github.com/gluster/glusterd2.git
synced 2026-02-05 12:45:38 +01:00
Refactor brick status to use as a generic function
Changed the brick status function to use generic strcture instead of client api strcture. Signed-off-by: Mohammed Rafi KC <rkavunga@redhat.com>
This commit is contained in:
@@ -3,6 +3,7 @@ package brick
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gluster/glusterd2/pkg/utils"
|
||||
"github.com/pborman/uuid"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
@@ -30,6 +31,18 @@ type Brickinfo struct {
|
||||
Decommissioned bool
|
||||
}
|
||||
|
||||
//Brickstatus gives status of brick
|
||||
type Brickstatus struct {
|
||||
Info Brickinfo
|
||||
Online bool
|
||||
Pid int
|
||||
Port int
|
||||
FS string
|
||||
MountOpts string
|
||||
Device string
|
||||
Size utils.SizeInfo
|
||||
}
|
||||
|
||||
func (b *Brickinfo) String() string {
|
||||
return b.NodeID.String() + ":" + b.Path
|
||||
}
|
||||
|
||||
@@ -2,19 +2,14 @@ package volumecommands
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
"github.com/gluster/glusterd2/glusterd2/brick"
|
||||
"github.com/gluster/glusterd2/glusterd2/daemon"
|
||||
"github.com/gluster/glusterd2/glusterd2/gdctx"
|
||||
"github.com/gluster/glusterd2/glusterd2/pmap"
|
||||
restutils "github.com/gluster/glusterd2/glusterd2/servers/rest/utils"
|
||||
"github.com/gluster/glusterd2/glusterd2/transaction"
|
||||
"github.com/gluster/glusterd2/glusterd2/volume"
|
||||
"github.com/gluster/glusterd2/pkg/api"
|
||||
"github.com/gluster/glusterd2/pkg/errors"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
@@ -22,8 +17,28 @@ const (
|
||||
brickStatusTxnKey string = "brickstatuses"
|
||||
)
|
||||
|
||||
func checkBricksStatus(ctx transaction.TxnCtx) error {
|
||||
func registerBricksStatusStepFuncs() {
|
||||
transaction.RegisterStepFunc(bricksStatus, "bricks-status.Check")
|
||||
}
|
||||
func createBrickStatusRspAPI(brickStatuses []brick.Brickstatus) []*api.BrickStatus {
|
||||
var brickStatusesRsp []*api.BrickStatus
|
||||
for _, status := range brickStatuses {
|
||||
s := &api.BrickStatus{
|
||||
Info: createBrickInfo(&status.Info),
|
||||
Online: status.Online,
|
||||
Pid: status.Pid,
|
||||
Port: status.Port,
|
||||
FS: status.FS,
|
||||
MountOpts: status.MountOpts,
|
||||
Device: status.Device,
|
||||
Size: createSizeInfo(&status.Size),
|
||||
}
|
||||
brickStatusesRsp = append(brickStatusesRsp, s)
|
||||
}
|
||||
return brickStatusesRsp
|
||||
}
|
||||
|
||||
func bricksStatus(ctx transaction.TxnCtx) error {
|
||||
var volname string
|
||||
if err := ctx.Get("volname", &volname); err != nil {
|
||||
ctx.Logger().WithError(err).Error("Failed to get key from transaction context.")
|
||||
@@ -35,61 +50,18 @@ func checkBricksStatus(ctx transaction.TxnCtx) error {
|
||||
ctx.Logger().WithError(err).Error("Failed to get volume information from store.")
|
||||
return err
|
||||
}
|
||||
|
||||
mtabEntries, err := getMounts()
|
||||
brickStatuses, err := volume.CheckBricksStatus(vol)
|
||||
if err != nil {
|
||||
ctx.Logger().WithError(err).Error("Failed to read /etc/mtab file.")
|
||||
ctx.Logger().WithError(err).Error("Failed to get brick status information from store.")
|
||||
return err
|
||||
}
|
||||
|
||||
var brickStatuses []*api.BrickStatus
|
||||
for _, binfo := range vol.GetLocalBricks() {
|
||||
brickDaemon, err := brick.NewGlusterfsd(binfo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
s := &api.BrickStatus{
|
||||
Info: createBrickInfo(&binfo),
|
||||
}
|
||||
|
||||
if pidOnFile, err := daemon.ReadPidFromFile(brickDaemon.PidFile()); err == nil {
|
||||
if _, err := daemon.GetProcess(pidOnFile); err == nil {
|
||||
s.Online = true
|
||||
s.Pid = pidOnFile
|
||||
s.Port = pmap.RegistrySearch(binfo.Path, pmap.GfPmapPortBrickserver)
|
||||
}
|
||||
}
|
||||
|
||||
var fstat syscall.Statfs_t
|
||||
if err := syscall.Statfs(binfo.Path, &fstat); err != nil {
|
||||
ctx.Logger().WithError(err).WithField("path",
|
||||
binfo.Path).Error("syscall.Statfs() failed")
|
||||
} else {
|
||||
s.Size = *(createSizeInfo(&fstat))
|
||||
}
|
||||
|
||||
for _, m := range mtabEntries {
|
||||
if strings.HasPrefix(binfo.Path, m.mntDir) {
|
||||
s.MountOpts = m.mntOpts
|
||||
s.Device = m.fsName
|
||||
s.FS = m.mntType
|
||||
}
|
||||
}
|
||||
|
||||
brickStatuses = append(brickStatuses, s)
|
||||
}
|
||||
|
||||
brickStatusesRsp := createBrickStatusRspAPI(brickStatuses)
|
||||
// Store the results in transaction context. This will be consumed by
|
||||
// the node that initiated the transaction.
|
||||
ctx.SetNodeResult(gdctx.MyUUID, brickStatusTxnKey, brickStatuses)
|
||||
ctx.SetNodeResult(gdctx.MyUUID, brickStatusTxnKey, brickStatusesRsp)
|
||||
return nil
|
||||
}
|
||||
|
||||
func registerBricksStatusStepFuncs() {
|
||||
transaction.RegisterStepFunc(checkBricksStatus, "bricks-status.Check")
|
||||
}
|
||||
|
||||
func volumeBricksStatusHandler(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
ctx := r.Context()
|
||||
|
||||
@@ -4,8 +4,17 @@ import (
|
||||
"github.com/gluster/glusterd2/glusterd2/brick"
|
||||
"github.com/gluster/glusterd2/glusterd2/volume"
|
||||
"github.com/gluster/glusterd2/pkg/api"
|
||||
"github.com/gluster/glusterd2/pkg/utils"
|
||||
)
|
||||
|
||||
func createSizeInfo(size *utils.SizeInfo) api.SizeInfo {
|
||||
return api.SizeInfo{
|
||||
Used: size.Used,
|
||||
Free: size.Free,
|
||||
Capacity: size.Capacity,
|
||||
}
|
||||
}
|
||||
|
||||
func createBrickInfo(b *brick.Brickinfo) api.BrickInfo {
|
||||
return api.BrickInfo{
|
||||
ID: b.ID,
|
||||
|
||||
@@ -23,12 +23,13 @@ func volumeStatusHandler(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
s, err := volumeUsage(v.Name)
|
||||
s, err := volume.UsageInfo(v.Name)
|
||||
if err != nil {
|
||||
logger.WithError(err).WithField("volume", v.Name).Error("Failed to get volume size info")
|
||||
}
|
||||
size := createSizeInfo(s)
|
||||
|
||||
resp := createVolumeStatusResp(v, s)
|
||||
resp := createVolumeStatusResp(v, &size)
|
||||
restutils.SendHTTPResponse(ctx, w, http.StatusOK, resp)
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package volumecommands
|
||||
package volume
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
@@ -12,8 +12,7 @@ import (
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
"github.com/gluster/glusterd2/pkg/api"
|
||||
|
||||
"github.com/gluster/glusterd2/pkg/utils"
|
||||
config "github.com/spf13/viper"
|
||||
)
|
||||
|
||||
@@ -50,8 +49,8 @@ func mountVolume(name string, mountpoint string) error {
|
||||
return cmd.Wait() // glusterfs daemonizes itself
|
||||
}
|
||||
|
||||
func createSizeInfo(fstat *syscall.Statfs_t) *api.SizeInfo {
|
||||
var s api.SizeInfo
|
||||
func createSizeInfo(fstat *syscall.Statfs_t) *utils.SizeInfo {
|
||||
var s utils.SizeInfo
|
||||
if fstat != nil {
|
||||
s.Capacity = fstat.Blocks * uint64(fstat.Bsize)
|
||||
s.Free = fstat.Bfree * uint64(fstat.Bsize)
|
||||
@@ -60,7 +59,8 @@ func createSizeInfo(fstat *syscall.Statfs_t) *api.SizeInfo {
|
||||
return &s
|
||||
}
|
||||
|
||||
func volumeUsage(volname string) (*api.SizeInfo, error) {
|
||||
//UsageInfo gives the size information of a gluster volume
|
||||
func UsageInfo(volname string) (*utils.SizeInfo, error) {
|
||||
|
||||
tempDir, err := ioutil.TempDir(config.GetString("rundir"), "gd2mount")
|
||||
if err != nil {
|
||||
@@ -1,6 +1,12 @@
|
||||
package volume
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
"github.com/gluster/glusterd2/glusterd2/brick"
|
||||
"github.com/gluster/glusterd2/glusterd2/daemon"
|
||||
"github.com/gluster/glusterd2/glusterd2/pmap"
|
||||
"github.com/gluster/glusterd2/pkg/errors"
|
||||
|
||||
"github.com/pborman/uuid"
|
||||
@@ -45,3 +51,53 @@ func IsQuotaEnabled(v *Volinfo) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
//CheckBricksStatus will give detailed information about brick
|
||||
func CheckBricksStatus(volinfo *Volinfo) ([]brick.Brickstatus, error) {
|
||||
|
||||
var brickStatuses []brick.Brickstatus
|
||||
mtabEntries, err := getMounts()
|
||||
if err != nil {
|
||||
log.WithError(err).Error("Failed to read /etc/mtab file.")
|
||||
return brickStatuses, err
|
||||
}
|
||||
|
||||
for _, binfo := range volinfo.GetLocalBricks() {
|
||||
brickDaemon, err := brick.NewGlusterfsd(binfo)
|
||||
if err != nil {
|
||||
return brickStatuses, err
|
||||
}
|
||||
|
||||
s := brick.Brickstatus{
|
||||
Info: binfo,
|
||||
}
|
||||
|
||||
if pidOnFile, err := daemon.ReadPidFromFile(brickDaemon.PidFile()); err == nil {
|
||||
if _, err := daemon.GetProcess(pidOnFile); err == nil {
|
||||
s.Online = true
|
||||
s.Pid = pidOnFile
|
||||
s.Port = pmap.RegistrySearch(binfo.Path, pmap.GfPmapPortBrickserver)
|
||||
}
|
||||
}
|
||||
|
||||
var fstat syscall.Statfs_t
|
||||
if err := syscall.Statfs(binfo.Path, &fstat); err != nil {
|
||||
log.WithError(err).WithField("path",
|
||||
binfo.Path).Error("syscall.Statfs() failed")
|
||||
} else {
|
||||
s.Size = *(createSizeInfo(&fstat))
|
||||
}
|
||||
|
||||
for _, m := range mtabEntries {
|
||||
if strings.HasPrefix(binfo.Path, m.mntDir) {
|
||||
s.MountOpts = m.mntOpts
|
||||
s.Device = m.fsName
|
||||
s.FS = m.mntType
|
||||
}
|
||||
}
|
||||
|
||||
brickStatuses = append(brickStatuses, s)
|
||||
}
|
||||
|
||||
return brickStatuses, nil
|
||||
}
|
||||
|
||||
@@ -94,8 +94,6 @@ func registerAllValidations() error {
|
||||
if err := registerValidation("bit-rot", validateBitrot); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := registerValidation("quota", validateQuota); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
err := registerValidation("quota", validateQuota)
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -8,3 +8,10 @@ import "reflect"
|
||||
func GetTypeString(i interface{}) string {
|
||||
return reflect.TypeOf(i).Elem().String()
|
||||
}
|
||||
|
||||
// SizeInfo represents sizing information.
|
||||
type SizeInfo struct {
|
||||
Capacity uint64
|
||||
Used uint64
|
||||
Free uint64
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user