Skip to content

Commit 22e3e39

Browse files
authored
round up to GIB as a part of resize (#289)
1 parent 6b12e79 commit 22e3e39

File tree

3 files changed

+42
-29
lines changed

3 files changed

+42
-29
lines changed

pkg/spdk/controllerserver.go

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ func getBoolParameter(params map[string]string, key string) bool {
477477
return exists && (valueStr == "true" || valueStr == "True")
478478
}
479479

480-
func prepareCreateVolumeReq(ctx context.Context, req *csi.CreateVolumeRequest, sizeMiB int64) (*util.CreateLVolData, error) {
480+
func prepareCreateVolumeReq(ctx context.Context, req *csi.CreateVolumeRequest, capacityBytes int64) (*util.CreateLVolData, error) {
481481
params := req.GetParameters()
482482

483483
distrNdcs, err := getIntParameter(params, "distr_ndcs", 1)
@@ -547,7 +547,7 @@ func prepareCreateVolumeReq(ctx context.Context, req *csi.CreateVolumeRequest, s
547547

548548
createVolReq := util.CreateLVolData{
549549
LvolName: req.GetName(),
550-
Size: fmt.Sprintf("%dM", sizeMiB),
550+
Size: strconv.FormatInt(capacityBytes, 10),
551551
LvsName: params["pool_name"],
552552
Fabric: params["fabric"],
553553
MaxRWIOPS: params["qos_rw_iops"],
@@ -588,10 +588,9 @@ func (cs *controllerServer) createVolume(ctx context.Context, req *csi.CreateVol
588588
size = 1024 * 1024 * 1024
589589
}
590590

591-
sizeGiB := util.ToGiB(size)
592-
sizeMiB := sizeGiB * 1024
591+
capacityBytes := util.AlignToGiBBytes(size)
593592
vol := csi.Volume{
594-
CapacityBytes: sizeMiB * 1024 * 1024,
593+
CapacityBytes: capacityBytes,
595594
VolumeContext: req.GetParameters(),
596595
ContentSource: req.GetVolumeContentSource(),
597596
}
@@ -604,7 +603,7 @@ func (cs *controllerServer) createVolume(ctx context.Context, req *csi.CreateVol
604603
}
605604

606605
if req.GetVolumeContentSource() != nil {
607-
clonedVolume, clonedErr := cs.handleVolumeContentSource(req, poolName, &vol, sizeMiB)
606+
clonedVolume, clonedErr := cs.handleVolumeContentSource(req, poolName, &vol, capacityBytes)
608607
if clonedErr != nil {
609608
return nil, clonedErr
610609
}
@@ -613,7 +612,7 @@ func (cs *controllerServer) createVolume(ctx context.Context, req *csi.CreateVol
613612
}
614613
}
615614

616-
createVolReq, err := prepareCreateVolumeReq(ctx, req, sizeMiB)
615+
createVolReq, err := prepareCreateVolumeReq(ctx, req, capacityBytes)
617616
if err != nil {
618617
return nil, err
619618
}
@@ -708,6 +707,10 @@ func (cs *controllerServer) unpublishVolume(volumeID string) error {
708707
func (cs *controllerServer) ControllerExpandVolume(_ context.Context, req *csi.ControllerExpandVolumeRequest) (*csi.ControllerExpandVolumeResponse, error) {
709708
volumeID := req.GetVolumeId()
710709
updatedSize := req.GetCapacityRange().GetRequiredBytes()
710+
711+
// Simplyblock backends are GiB aligned, so we round up to GiB.
712+
capacityBytes := util.AlignToGiBBytes(updatedSize)
713+
711714
spdkVol, err := getSPDKVol(volumeID)
712715
if err != nil {
713716
return nil, err
@@ -718,13 +721,13 @@ func (cs *controllerServer) ControllerExpandVolume(_ context.Context, req *csi.C
718721
return nil, err
719722
}
720723

721-
_, err = sbclient.ResizeVolume(spdkVol.lvolID, updatedSize)
724+
_, err = sbclient.ResizeVolume(spdkVol.lvolID, capacityBytes)
722725
if err != nil {
723726
klog.Errorf("failed to resize lvol, LVolID: %s err: %v", spdkVol.lvolID, err)
724727
return nil, err
725728
}
726729
return &csi.ControllerExpandVolumeResponse{
727-
CapacityBytes: updatedSize,
730+
CapacityBytes: capacityBytes,
728731
NodeExpansionRequired: true,
729732
}, nil
730733
}
@@ -885,19 +888,19 @@ func newControllerServer(d *csicommon.CSIDriver) (*controllerServer, error) {
885888
return &server, nil
886889
}
887890

888-
func (cs *controllerServer) handleVolumeContentSource(req *csi.CreateVolumeRequest, poolName string, vol *csi.Volume, sizeMiB int64) (*csi.Volume, error) {
891+
func (cs *controllerServer) handleVolumeContentSource(req *csi.CreateVolumeRequest, poolName string, vol *csi.Volume, sizeBytes int64) (*csi.Volume, error) {
889892
volumeSource := req.GetVolumeContentSource()
890893
switch volumeSource.GetType().(type) {
891894
case *csi.VolumeContentSource_Snapshot:
892-
return cs.handleSnapshotSource(volumeSource.GetSnapshot(), req, poolName, vol, sizeMiB)
895+
return cs.handleSnapshotSource(volumeSource.GetSnapshot(), req, poolName, vol, sizeBytes)
893896
case *csi.VolumeContentSource_Volume:
894-
return cs.handleVolumeSource(volumeSource.GetVolume(), req, poolName, vol, sizeMiB)
897+
return cs.handleVolumeSource(volumeSource.GetVolume(), req, poolName, vol, sizeBytes)
895898
default:
896899
return nil, status.Errorf(codes.InvalidArgument, "%v not a proper volume source", volumeSource)
897900
}
898901
}
899902

900-
func (cs *controllerServer) handleSnapshotSource(snapshot *csi.VolumeContentSource_SnapshotSource, req *csi.CreateVolumeRequest, poolName string, vol *csi.Volume, sizeMiB int64) (*csi.Volume, error) {
903+
func (cs *controllerServer) handleSnapshotSource(snapshot *csi.VolumeContentSource_SnapshotSource, req *csi.CreateVolumeRequest, poolName string, vol *csi.Volume, sizeBytes int64) (*csi.Volume, error) {
901904
if snapshot == nil {
902905
return nil, nil
903906
}
@@ -917,7 +920,8 @@ func (cs *controllerServer) handleSnapshotSource(snapshot *csi.VolumeContentSour
917920
snapshotName := req.GetName()
918921
params := req.GetParameters()
919922
pvcName, _ := params[CSIStorageNameKey]
920-
newSize := fmt.Sprintf("%dM", sizeMiB)
923+
// Use raw bytes to avoid decimal/binary unit ambiguity in clone sizing.
924+
newSize := strconv.FormatInt(sizeBytes, 10)
921925
volumeID, err := sbclient.CloneSnapshot(sbSnapshot.snapshotID, snapshotName, newSize, pvcName)
922926
if err != nil {
923927
klog.Errorf("error creating simplyBlock volume: %v", err)
@@ -929,7 +933,7 @@ func (cs *controllerServer) handleSnapshotSource(snapshot *csi.VolumeContentSour
929933
return vol, nil
930934
}
931935

932-
func (cs *controllerServer) handleVolumeSource(srcVolume *csi.VolumeContentSource_VolumeSource, req *csi.CreateVolumeRequest, poolName string, vol *csi.Volume, sizeMiB int64) (*csi.Volume, error) {
936+
func (cs *controllerServer) handleVolumeSource(srcVolume *csi.VolumeContentSource_VolumeSource, req *csi.CreateVolumeRequest, poolName string, vol *csi.Volume, sizeBytes int64) (*csi.Volume, error) {
933937
if srcVolume == nil {
934938
return nil, nil
935939
}
@@ -959,7 +963,8 @@ func (cs *controllerServer) handleVolumeSource(srcVolume *csi.VolumeContentSourc
959963
klog.Errorf("failed to create snapshot, srcVolumeID: %s snapshotName: %s err: %v", srcVolumeID, snapshotName, err)
960964
return nil, status.Error(codes.Internal, err.Error())
961965
}
962-
newSize := fmt.Sprintf("%dM", sizeMiB)
966+
// Use raw bytes to avoid decimal/binary unit ambiguity in clone sizing.
967+
newSize := strconv.FormatInt(sizeBytes, 10)
963968
klog.Infof("CloneSnapshot : snapshotName=%s", snapshotName)
964969
snapshot, err := getSnapshot(snapshotID)
965970
if err != nil {

pkg/util/jsonrpc.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -335,16 +335,11 @@ func (client *RPCClient) resizeVolume(lvolID string, size int64) (bool, error) {
335335
LvolID: lvolID,
336336
NewSize: size,
337337
}
338-
var result bool
339-
out, err := client.CallSBCLI("PUT", "/lvol/resize/"+lvolID, &params)
338+
_, err := client.CallSBCLI("PUT", "/lvol/resize/"+lvolID, &params)
340339
if err != nil {
341340
return false, err
342341
}
343-
result, ok := out.(bool)
344-
if !ok {
345-
return false, fmt.Errorf("failed to convert the response to bool type. Interface: %v", out)
346-
}
347-
return result, nil
342+
return true, nil
348343
}
349344

350345
// cloneSnapshot clones a snapshot
@@ -477,6 +472,7 @@ try_request:
477472
defer resp.Body.Close()
478473

479474
var response struct {
475+
Status *bool `json:"status"`
480476
Result any `json:"result"`
481477
Results any `json:"results"`
482478
Error string `json:"error"`
@@ -488,6 +484,10 @@ try_request:
488484
method, resp.StatusCode, decodeErr)
489485
}
490486

487+
if response.Status != nil && !*response.Status {
488+
return nil, fmt.Errorf("%s: %s", method, response.Error)
489+
}
490+
491491
// --- Backward compatibility fallback ---
492492
if resp.StatusCode == http.StatusNotFound || resp.StatusCode == http.StatusBadRequest {
493493
if requestURL != oldURL {

pkg/util/util.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ const volumeContextFileName = "volume-context.json"
3737
// file name in which XPU context is stashed.
3838
const xpuContextFileName = "xpu-context.json"
3939

40+
const (
41+
MIB = int64(1024 * 1024)
42+
GIB = MIB * 1024
43+
)
44+
4045
func ParseJSONFile(fileName string, result interface{}) error {
4146
file, err := os.Open(fileName)
4247
if err != nil {
@@ -52,16 +57,19 @@ func ParseJSONFile(fileName string, result interface{}) error {
5257
return json.Unmarshal(bytes, result)
5358
}
5459

55-
// round up bytes to megabytes
60+
// ToMiB rounds up bytes to megabytes
5661
func ToMiB(bytes int64) int64 {
57-
const mi = 1024 * 1024
58-
return (bytes + mi - 1) / mi
62+
return (bytes + MIB - 1) / MIB
5963
}
6064

61-
// round up bytes to gigabytes
65+
// ToGiB rounds up bytes to gigabytes
6266
func ToGiB(bytes int64) int64 {
63-
const gi = int64(1024 * 1024 * 1024)
64-
return (bytes + gi - 1) / gi
67+
return (bytes + GIB - 1) / GIB
68+
}
69+
70+
// AlignToGiBBytes rounds bytes up to the next GiB boundary and returns bytes.
71+
func AlignToGiBBytes(bytes int64) int64 {
72+
return ToGiB(bytes) * GIB
6573
}
6674

6775
// ${env:-def}

0 commit comments

Comments
 (0)