rbd: add constants for pool stat options and wrapper for rbd_pool_stats_get()

Added pool stat options constants.
Also implemented GetAllPoolStats() function to introduce
the functionality of rbd_pool_stats_option_add_uint64() and rbd_pool_stats_get()

Signed-off-by: Mudit Agarwal <muagarwa@redhat.com>
This commit is contained in:
Mudit Agarwal 2020-07-06 19:10:10 +05:30 committed by John Mulligan
parent fec47ac9a9
commit 2bb1b9bffc
2 changed files with 167 additions and 0 deletions

View File

@ -126,3 +126,92 @@ func (poolstats *poolStats) destroy() {
poolstats.stats = nil
}
}
// PoolStatOption represents a group of configurable pool stat options.
type PoolStatOption C.rbd_pool_stat_option_t
const (
// PoolStatOptionImages is the representation of
// RBD_POOL_STAT_OPTION_IMAGES from librbd.
PoolStatOptionImages = PoolStatOption(C.RBD_POOL_STAT_OPTION_IMAGES)
// PoolStatOptionImageProvisionedBytes is the representation of
// RBD_POOL_STAT_OPTION_IMAGE_PROVISIONED_BYTES from librbd.
PoolStatOptionImageProvisionedBytes = PoolStatOption(C.RBD_POOL_STAT_OPTION_IMAGE_PROVISIONED_BYTES)
// PoolStatOptionImageMaxProvisionedBytes is the representation of
// RBD_POOL_STAT_OPTION_IMAGE_MAX_PROVISIONED_BYTES from librbd.
PoolStatOptionImageMaxProvisionedBytes = PoolStatOption(C.RBD_POOL_STAT_OPTION_IMAGE_MAX_PROVISIONED_BYTES)
// PoolStatOptionImageSnapshots is the representation of
// RBD_POOL_STAT_OPTION_IMAGE_SNAPSHOTS from librbd.
PoolStatOptionImageSnapshots = PoolStatOption(C.RBD_POOL_STAT_OPTION_IMAGE_SNAPSHOTS)
// PoolStatOptionTrashImages is the representation of
// RBD_POOL_STAT_OPTION_TRASH_IMAGES from librbd.
PoolStatOptionTrashImages = PoolStatOption(C.RBD_POOL_STAT_OPTION_TRASH_IMAGES)
// PoolStatOptionTrashProvisionedBytes is the representation of
// RBD_POOL_STAT_OPTION_TRASH_PROVISIONED_BYTES from librbd.
PoolStatOptionTrashProvisionedBytes = PoolStatOption(C.RBD_POOL_STAT_OPTION_TRASH_PROVISIONED_BYTES)
// PoolStatOptionTrashMaxProvisionedBytes is the representation of
// RBD_POOL_STAT_OPTION_TRASH_MAX_PROVISIONED_BYTES from librbd.
PoolStatOptionTrashMaxProvisionedBytes = PoolStatOption(C.RBD_POOL_STAT_OPTION_TRASH_MAX_PROVISIONED_BYTES)
// PoolStatOptionTrashSnapshots is the representation of
// RBD_POOL_STAT_OPTION_TRASH_SNAPSHOTS from librbd.
PoolStatOptionTrashSnapshots = PoolStatOption(C.RBD_POOL_STAT_OPTION_TRASH_SNAPSHOTS)
)
// addPoolStatOption adds the given PoolStatOption to PoolStats.
//
// Implements:
// int rbd_pool_stats_option_add_uint64(rbd_pool_stats_t stats, int stat_option, uint64_t* stat_val)
func (poolstats *poolStats) addPoolStatOption(option PoolStatOption, val *uint64) error {
ret := C.rbd_pool_stats_option_add_uint64(
poolstats.stats,
C.int(option),
(*C.uint64_t)(val))
return getError(ret)
}
// GetAllPoolStats returns a map of all PoolStatOption(s) to their respective values.
//
// Implements:
// int rbd_pool_stats_get(rados_ioctx_t io, rbd_pool_stats_t stats);
func GetAllPoolStats(ioctx *rados.IOContext) (map[PoolStatOption]uint64, error) {
var omap = make(map[PoolStatOption]uint64)
if ioctx == nil {
return omap, ErrNoIOContext
}
poolstats := poolStatsCreate()
defer func() {
poolstats.destroy()
}()
var keys [8]PoolStatOption
keys[0] = PoolStatOptionImages
keys[1] = PoolStatOptionImageProvisionedBytes
keys[2] = PoolStatOptionImageMaxProvisionedBytes
keys[3] = PoolStatOptionImageSnapshots
keys[4] = PoolStatOptionTrashImages
keys[5] = PoolStatOptionTrashProvisionedBytes
keys[6] = PoolStatOptionTrashMaxProvisionedBytes
keys[7] = PoolStatOptionTrashSnapshots
ovalArray := make([]uint64, len(keys))
// add option with the address where the respective value would be stored.
for i, key := range keys {
err := poolstats.addPoolStatOption(key, &ovalArray[i])
if err != nil {
return omap, err
}
}
ret := C.rbd_pool_stats_get(cephIoctx(ioctx), poolstats.stats)
if ret < 0 {
return omap, getError(ret)
}
for j, key := range keys {
omap[key] = ovalArray[j]
}
return omap, nil
}

View File

@ -5,6 +5,7 @@ package rbd
import (
"math/rand"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
@ -131,3 +132,80 @@ func TestPoolInit(t *testing.T) {
assert.NoError(t, err)
})
}
func TestGetAllPoolStat(t *testing.T) {
conn := radosConnect(t)
poolName := GetUUID()
err := conn.MakePool(poolName)
assert.NoError(t, err)
ioctx, err := conn.OpenIOContext(poolName)
assert.NoError(t, err)
assert.NotNil(t, ioctx)
defer func() {
ioctx.Destroy()
conn.DeletePool(poolName)
conn.Shutdown()
}()
poolstats := poolStatsCreate()
defer func() {
poolstats.destroy()
}()
var imageName string
size := uint64(2 << 20)
var expectedSize uint64
for idx := 0; idx < 3; idx++ {
imageName = GetUUID()
image, err := Create(ioctx, imageName, size, testImageOrder)
assert.NoError(t, err)
defer func() {
assert.NoError(t, image.Remove())
}()
expectedSize += size
}
imageName = GetUUID()
_, err = Create(ioctx, imageName, size, testImageOrder)
assert.NoError(t, err)
expectedSize += size
img, err := OpenImage(ioctx, imageName, NoSnapshot)
assert.NoError(t, err)
mySnap, err := img.CreateSnapshot("mySnap")
assert.NoError(t, err)
t.Run("NullIOContext", func(t *testing.T) {
_, err = GetAllPoolStats(nil)
assert.Error(t, err)
})
t.Run("CheckPoolStatOption", func(t *testing.T) {
err := img.Resize(size)
assert.NoError(t, err)
assert.NoError(t, mySnap.Remove())
assert.NoError(t, img.Close())
err = img.Trash(time.Hour)
assert.NoError(t, err)
defer func() {
trashList, err := GetTrashList(ioctx)
assert.NoError(t, err)
assert.NoError(t, TrashRemove(ioctx, trashList[0].Id, true))
}()
omap, err := GetAllPoolStats(ioctx)
assert.NoError(t, err)
assert.Equal(t, uint64(3), omap[PoolStatOptionImages])
assert.Equal(t, (expectedSize - size), omap[PoolStatOptionImageProvisionedBytes])
assert.Equal(t, expectedSize-size, omap[PoolStatOptionImageMaxProvisionedBytes])
assert.Equal(t, uint64(0), omap[PoolStatOptionImageSnapshots])
assert.Equal(t, uint64(0), omap[PoolStatOptionTrashSnapshots])
assert.Equal(t, uint64(1), omap[PoolStatOptionTrashImages])
assert.Equal(t, size, omap[PoolStatOptionTrashProvisionedBytes])
assert.Equal(t, size, omap[PoolStatOptionTrashMaxProvisionedBytes])
})
}