Skip to content

Commit

Permalink
Implement NodeGetVolumeStats RPC
Browse files Browse the repository at this point in the history
Exposing node capacity statistics facilitates features like alerting on
high disk usage.
  • Loading branch information
Timo Reimann committed Nov 2, 2019
1 parent ef4506e commit 17343cf
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 17 deletions.
27 changes: 23 additions & 4 deletions driver/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ func TestDriverSuite(t *testing.T) {
nodeId: strconv.Itoa(nodeID),
doTag: doTag,
region: "nyc3",
mounter: &fakeMounter{},
log: logrus.New().WithField("test_enabed", true),
mounter: &fakeMounter{
mounted: map[string]string{},
},
log: logrus.New().WithField("test_enabed", true),

storage: &fakeStorageDriver{
volumes: volumes,
Expand Down Expand Up @@ -360,25 +362,42 @@ func (f *fakeSnapshotsDriver) Delete(context.Context, string) (*godo.Response, e
panic("not implemented")
}

type fakeMounter struct{}
type fakeMounter struct {
mounted map[string]string
}

func (f *fakeMounter) Format(source string, fsType string) error {
return nil
}

func (f *fakeMounter) Mount(source string, target string, fsType string, options ...string) error {
f.mounted[target] = source
return nil
}

func (f *fakeMounter) Unmount(target string) error {
delete(f.mounted, target)
return nil
}

func (f *fakeMounter) IsFormatted(source string) (bool, error) {
return true, nil
}
func (f *fakeMounter) IsMounted(target string) (bool, error) {
return true, nil
_, ok := f.mounted[target]
return ok, nil
}

func (f *fakeMounter) GetStatistics(volumePath string) (volumeStatistics, error) {
return volumeStatistics{
availableBytes: 3 * giB,
totalBytes: 10 * giB,
usedBytes: 7 * giB,

availableInodes: 3000,
totalInodes: 10000,
usedInodes: 7000,
}, nil
}

func godoResponse() *godo.Response {
Expand Down
33 changes: 33 additions & 0 deletions driver/mounter.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"strings"

"github.com/sirupsen/logrus"
"golang.org/x/sys/unix"
)

type findmntResponse struct {
Expand All @@ -38,7 +39,14 @@ type fileSystem struct {
Options string `json:"options"`
}

type volumeStatistics struct {
availableBytes, totalBytes, usedBytes int64
availableInodes, totalInodes, usedInodes int64
}

// Mounter is responsible for formatting and mounting volumes
// TODO(timoreimann): find a more suitable name since the interface encompasses
// more than just mounting functionality by now.
type Mounter interface {
// Format formats the source with the given filesystem type
Format(source, fsType string) error
Expand All @@ -57,6 +65,10 @@ type Mounter interface {
// propagated). It returns true if it's mounted. An error is returned in
// case of system errors or if it's mounted incorrectly.
IsMounted(target string) (bool, error)

// GetStatistics returns capacity-related volume statistics for the given
// volume path.
GetStatistics(volumePath string) (volumeStatistics, error)
}

// TODO(arslan): this is Linux only for now. Refactor this into a package with
Expand Down Expand Up @@ -272,3 +284,24 @@ func (m *mounter) IsMounted(target string) (bool, error) {

return targetFound, nil
}

func (m *mounter) GetStatistics(volumePath string) (volumeStatistics, error) {
var statfs unix.Statfs_t
// See http://man7.org/linux/man-pages/man2/statfs.2.html for details.
err := unix.Statfs(volumePath, &statfs)
if err != nil {
return volumeStatistics{}, err
}

volStats := volumeStatistics{
availableBytes: int64(statfs.Bavail) * int64(statfs.Bsize),
totalBytes: int64(statfs.Blocks) * int64(statfs.Bsize),
usedBytes: (int64(statfs.Blocks) - int64(statfs.Bfree)) * int64(statfs.Bsize),

availableInodes: int64(statfs.Ffree),
totalInodes: int64(statfs.Files),
usedInodes: int64(statfs.Files) - int64(statfs.Ffree),
}

return volStats, nil
}
77 changes: 65 additions & 12 deletions driver/node.go

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

2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ require (
golang.org/x/net v0.0.0-20180406214816-61147c48b25b // indirect
golang.org/x/oauth2 v0.0.0-20180402223937-921ae394b943
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f // indirect
golang.org/x/sys v0.0.0-20180406135729-3b87a42e500a // indirect
golang.org/x/sys v0.0.0-20180406135729-3b87a42e500a
golang.org/x/text v0.3.0 // indirect
golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2 // indirect
google.golang.org/appengine v1.0.0 // indirect
Expand Down

0 comments on commit 17343cf

Please sign in to comment.