Skip to content

Commit

Permalink
fix(zfspv): rounding off the volume size to Gi and Mi
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
pawanpraka1 committed Aug 7, 2020
1 parent cd4a0de commit 479fe46
Show file tree
Hide file tree
Showing 21 changed files with 4,915 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ env:
- MINIKUBE_HOME=$HOME
- CHANGE_MINIKUBE_NONE_USER=true
- KUBECONFIG=$HOME/.kube/config
- OPENEBS_NAMESPACE=openebs
- NodeID=$HOSTNAME
services:
- docker
language: go
Expand Down
1 change: 1 addition & 0 deletions changelogs/unreleased/191-pawanpraka1
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
rounding off the volume size to Gi and Mi
3 changes: 0 additions & 3 deletions ci/ci-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,6 @@

set -e

export OPENEBS_NAMESPACE="openebs"
export NodeID=$HOSTNAME

ZFS_OPERATOR=deploy/zfs-operator.yaml
SNAP_CLASS=deploy/sample/zfssnapclass.yaml

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ require (
github.com/pkg/errors v0.8.1
github.com/spf13/cobra v0.0.5
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.5.1
golang.org/x/crypto v0.0.0-20190829043050-9756ffdc2472 // indirect
golang.org/x/net v0.0.0-20191004110552-13f9640d40b9
golang.org/x/sys v0.0.0-20190902133755-9109b7679e13
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,8 @@ github.com/stretchr/testify v0.0.0-20151208002404-e3a8ff8ce365/go.mod h1:a8OnRci
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
github.com/syndtr/gocapability v0.0.0-20160928074757-e7cb7fa329f4/go.mod h1:hkRG7XYTFWNJGYcbNJQlaLq0fg1yr4J4t/NcTQtrfww=
github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
Expand Down
31 changes: 27 additions & 4 deletions pkg/driver/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ import (
zfs "github.com/openebs/zfs-localpv/pkg/zfs"
)

// size constants
const (
Mi = 1024 * 1024
Gi = 1024 * 1024 * 1024
)

// controller is the server implementation
// for CSI Controller
type controller struct {
Expand Down Expand Up @@ -75,10 +81,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
Expand Down Expand Up @@ -148,7 +170,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, "@")
Expand Down Expand Up @@ -208,7 +230,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")

Expand Down Expand Up @@ -325,7 +347,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 {
Expand Down
44 changes: 44 additions & 0 deletions pkg/driver/controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
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},
"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},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
assert.Equal(t, test.expected, getRoundedCapacity(test.input))
})
}
}
27 changes: 27 additions & 0 deletions vendor/github.com/pmezard/go-difflib/LICENSE

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

Loading

0 comments on commit 479fe46

Please sign in to comment.