fix(zfspv): rounding off the volume size to Gi and Mi (#191)

ZFS does not create the zvol if volume size is not multiple of
the volblocksize. There are use cases where customer will create
a PVC with size as 5G, which will be 5 * 1000 * 1000 * 1000 bytes
and this is not the multiple of default volblocksize 8k.

In ZFS, volblocksize and recordsize must be power of 2 from 512B to 1M,
so keeping the size in the form of Gi or Mi should be
sufficient to make volsize multiple of volblocksize/recordsize.


Signed-off-by: Pawan <pawan@mayadata.io>
This commit is contained in:
Pawan Prakash Sharma 2020-08-07 20:50:13 +05:30 committed by GitHub
parent 8d3705b08b
commit b1b69ebfe7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
21 changed files with 4918 additions and 7 deletions

View file

@ -37,6 +37,14 @@ import (
zfs "github.com/openebs/zfs-localpv/pkg/zfs"
)
// size constants
const (
MB = 1000 * 1000
GB = 1000 * 1000 * 1000
Mi = 1024 * 1024
Gi = 1024 * 1024 * 1024
)
// controller is the server implementation
// for CSI Controller
type controller struct {
@ -75,10 +83,26 @@ func sendEventOrIgnore(pvcName, pvName, capacity, stgType, method string) {
}
}
// getRoundedCapacity rounds the capacity on 1024 base
func getRoundedCapacity(size int64) int64 {
/*
* volblocksize and recordsize must be power of 2 from 512B to 1M
* so keeping the size in the form of Gi or Mi should be
* sufficient to make volsize multiple of volblocksize/recordsize.
*/
if size > Gi {
return ((size + Gi - 1) / Gi) * Gi
}
// Keeping minimum allocatable size as 1Mi (1024 * 1024)
return ((size + Mi - 1) / Mi) * Mi
}
// CreateZFSVolume create new zfs volume from csi volume request
func CreateZFSVolume(req *csi.CreateVolumeRequest) (string, error) {
volName := req.GetName()
size := req.GetCapacityRange().RequiredBytes
size := getRoundedCapacity(req.GetCapacityRange().RequiredBytes)
// parameter keys may be mistyped from the CRD specification when declaring
// the storageclass, which kubectl validation will not catch. Because ZFS
@ -148,7 +172,7 @@ func CreateZFSClone(req *csi.CreateVolumeRequest, snapshot string) (string, erro
parameters := req.GetParameters()
// lower case keys, cf CreateZFSVolume()
pool := helpers.GetInsensitiveParameter(&parameters, "poolname")
size := req.GetCapacityRange().RequiredBytes
size := getRoundedCapacity(req.GetCapacityRange().RequiredBytes)
volsize := strconv.FormatInt(int64(size), 10)
snapshotID := strings.Split(snapshot, "@")
@ -208,7 +232,7 @@ func (cs *controller) CreateVolume(
parameters := req.GetParameters()
// lower case keys, cf CreateZFSVolume()
pool := helpers.GetInsensitiveParameter(&parameters, "poolname")
size := req.GetCapacityRange().RequiredBytes
size := getRoundedCapacity(req.GetCapacityRange().RequiredBytes)
contentSource := req.GetVolumeContentSource()
pvcName := helpers.GetInsensitiveParameter(&parameters, "csi.storage.k8s.io/pvc/name")
@ -325,7 +349,8 @@ func (cs *controller) ControllerExpandVolume(
req *csi.ControllerExpandVolumeRequest,
) (*csi.ControllerExpandVolumeResponse, error) {
updatedSize := req.GetCapacityRange().GetRequiredBytes()
/* round off the new size */
updatedSize := getRoundedCapacity(req.GetCapacityRange().GetRequiredBytes())
vol, err := zfs.GetZFSVolume(req.VolumeId)
if err != nil {

View file

@ -0,0 +1,45 @@
/*
Copyright 2020 The OpenEBS Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package driver
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestRoundOff(t *testing.T) {
tests := map[string]struct {
input int64
expected int64
}{
"Minimum allocatable is 1Mi": {input: 1, expected: Mi},
"roundOff to same Mi size": {input: Mi, expected: Mi},
"roundOff to nearest Mi": {input: Mi + 1, expected: Mi * 2},
"roundOff to same Gi size": {input: Gi, expected: Gi},
"roundOff to nearest Gi": {input: Gi + 1, expected: Gi * 2},
"roundOff MB size": {input: 5 * MB, expected: 5 * Mi},
"roundOff GB size": {input: 5 * GB, expected: 5 * Gi},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
assert.Equal(t, test.expected, getRoundedCapacity(test.input))
})
}
}