mirror of
https://github.com/ceph/ceph-csi.git
synced 2025-06-13 10:33:35 +00:00
Fix volsize for cephfs and rbd
Signed-off-by: Madhu Rajanna <madhupr007@gmail.com>
This commit is contained in:
committed by
mergify[bot]
parent
9287948991
commit
7274bd09e5
@ -18,6 +18,7 @@ package util
|
||||
|
||||
import (
|
||||
"context"
|
||||
"math"
|
||||
"os"
|
||||
"path"
|
||||
"strings"
|
||||
@ -36,12 +37,43 @@ import (
|
||||
const (
|
||||
// MiB - MebiByte size
|
||||
MiB = 1024 * 1024
|
||||
GiB = MiB * 1024
|
||||
)
|
||||
|
||||
// RoundUpToMiB rounds up given quantity upto chunks of MiB
|
||||
func RoundUpToMiB(size int64) int64 {
|
||||
// RoundOffVolSize rounds up given quantity upto chunks of MiB/GiB
|
||||
func RoundOffVolSize(size int64) int64 {
|
||||
requestBytes := size
|
||||
return roundUpSize(requestBytes, MiB)
|
||||
if requestBytes < GiB {
|
||||
return roundUpSize(requestBytes, MiB)
|
||||
}
|
||||
size = roundUpSize(requestBytes, GiB)
|
||||
// convert size back to MiB for rbd CLI
|
||||
return size * GiB / MiB
|
||||
}
|
||||
|
||||
func roundUpSize(volumeSizeBytes, allocationUnitBytes int64) int64 {
|
||||
roundedUp := volumeSizeBytes / allocationUnitBytes
|
||||
if volumeSizeBytes%allocationUnitBytes > 0 {
|
||||
roundedUp++
|
||||
}
|
||||
return roundedUp
|
||||
}
|
||||
|
||||
// RoundOffBytes converts roundoff the size
|
||||
// 1.1Mib will be round off to 2Mib same for GiB
|
||||
// size less than will be round off to 1MiB
|
||||
func RoundOffBytes(bytes int64) int64 {
|
||||
var num int64
|
||||
floatBytes := float64(bytes)
|
||||
// round off the value if its in decimal
|
||||
if floatBytes < GiB {
|
||||
num = int64(math.Ceil(floatBytes / MiB))
|
||||
num *= MiB
|
||||
} else {
|
||||
num = int64(math.Ceil(floatBytes / GiB))
|
||||
num *= GiB
|
||||
}
|
||||
return num
|
||||
}
|
||||
|
||||
// variables which will be set during the build time
|
||||
@ -82,14 +114,6 @@ type Config struct {
|
||||
|
||||
}
|
||||
|
||||
func roundUpSize(volumeSizeBytes, allocationUnitBytes int64) int64 {
|
||||
roundedUp := volumeSizeBytes / allocationUnitBytes
|
||||
if volumeSizeBytes%allocationUnitBytes > 0 {
|
||||
roundedUp++
|
||||
}
|
||||
return roundedUp
|
||||
}
|
||||
|
||||
// CreatePersistanceStorage creates storage path and initializes new cache
|
||||
func CreatePersistanceStorage(sPath, metaDataStore, pluginPath string) (CachePersister, error) {
|
||||
var err error
|
||||
|
144
pkg/util/util_test.go
Normal file
144
pkg/util/util_test.go
Normal file
@ -0,0 +1,144 @@
|
||||
/*
|
||||
Copyright 2019 The Ceph-CSI 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 util
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRoundOffBytes(t *testing.T) {
|
||||
type args struct {
|
||||
bytes int64
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want int64
|
||||
}{
|
||||
{
|
||||
"1MiB conversions",
|
||||
args{
|
||||
bytes: 1048576,
|
||||
},
|
||||
1048576,
|
||||
},
|
||||
{
|
||||
"1000kiB conversion",
|
||||
args{
|
||||
bytes: 1000,
|
||||
},
|
||||
1048576, // equal to 1MiB
|
||||
},
|
||||
{
|
||||
"1.5Mib conversion",
|
||||
args{
|
||||
bytes: 1572864,
|
||||
},
|
||||
2097152, // equal to 2MiB
|
||||
},
|
||||
{
|
||||
"1.1MiB conversion",
|
||||
args{
|
||||
bytes: 1153434,
|
||||
},
|
||||
2097152, // equal to 2MiB
|
||||
},
|
||||
{
|
||||
"1.5GiB conversion",
|
||||
args{
|
||||
bytes: 1610612736,
|
||||
},
|
||||
2147483648, // equal to 2GiB
|
||||
},
|
||||
{
|
||||
"1.1GiB conversion",
|
||||
args{
|
||||
bytes: 1181116007,
|
||||
},
|
||||
2147483648, // equal to 2GiB
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
ts := tt
|
||||
t.Run(ts.name, func(t *testing.T) {
|
||||
if got := RoundOffBytes(ts.args.bytes); got != ts.want {
|
||||
t.Errorf("RoundOffBytes() = %v, want %v", got, ts.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRoundOffVolSize(t *testing.T) {
|
||||
type args struct {
|
||||
size int64
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want int64
|
||||
}{
|
||||
{
|
||||
"1MiB conversions",
|
||||
args{
|
||||
size: 1048576,
|
||||
},
|
||||
1, // MiB
|
||||
},
|
||||
{
|
||||
"1000kiB conversion",
|
||||
args{
|
||||
size: 1000,
|
||||
},
|
||||
1, // MiB
|
||||
},
|
||||
{
|
||||
"1.5Mib conversion",
|
||||
args{
|
||||
size: 1572864,
|
||||
},
|
||||
2, // MiB
|
||||
},
|
||||
{
|
||||
"1.1MiB conversion",
|
||||
args{
|
||||
size: 1153434,
|
||||
},
|
||||
2, // MiB
|
||||
},
|
||||
{
|
||||
"1.5GiB conversion",
|
||||
args{
|
||||
size: 1610612736,
|
||||
},
|
||||
2048, // MiB
|
||||
},
|
||||
{
|
||||
"1.1GiB conversion",
|
||||
args{
|
||||
size: 1181116007,
|
||||
},
|
||||
2048, // MiB
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
ts := tt
|
||||
t.Run(ts.name, func(t *testing.T) {
|
||||
if got := RoundOffVolSize(ts.args.size); got != ts.want {
|
||||
t.Errorf("RoundOffVolSize() = %v, want %v", got, ts.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user