@@ -169,10 +169,18 @@ func setupCaching(devicePath string, req *csi.NodeStageVolumeRequest, nodeId str
169
169
klog .V (4 ).Infof ("Assuming valid data cache size and mode, resizing cache is not supported" )
170
170
} else {
171
171
cacheSize := req .GetPublishContext ()[common .ContextDataCacheSize ]
172
- chunkSize , err := fetchChunkSizeKiB (cacheSize )
172
+ maxChunkSizeStr := strconv .FormatInt (int64 (maxChunkSize / KiB ), 10 )
173
+ var chunkSize string
174
+ cachePvSize , err := fetchPvSizeGiB ()
173
175
if err != nil {
174
- klog .Errorf ("Errored to fetch cache size, verify the data-cache-size is valid: got %v, error: %q" , cacheSize , err )
175
- return mainDevicePath , err
176
+ klog .Errorf ("Errored while fetching PV size, got %v, falling back to default chunkSize of %v" , err , maxChunkSize )
177
+ chunkSize = maxChunkSizeStr
178
+ } else {
179
+ chunkSize , err = fetchChunkSizeKiB (cachePvSize )
180
+ if err != nil {
181
+ klog .Errorf ("Errored to fetch cache size, verify the data-cache-size is valid: got %v, error: %q" , chunkSize , err )
182
+ chunkSize = maxChunkSizeStr
183
+ }
176
184
}
177
185
// Check if LV exists
178
186
info , err = common .RunCommand ("" /* pipedCmd */ , nil /* pipedCmdArg */ , "lvs" , args ... )
@@ -635,7 +643,7 @@ func watchDiskDetaches(watcher *fsnotify.Watcher, nodeName string, errorCh chan
635
643
klog .Errorf ("Error updating volume group's metadata: %v" , err )
636
644
}
637
645
reduceVolumeGroup (getVolumeGroupName (nodeName ), true )
638
- klog .V (2 ).Infof ("disk attach/detach event %#v\n " , event )
646
+ klog .V (6 ).Infof ("disk attach/detach event %#v\n " , event )
639
647
}
640
648
}
641
649
}
@@ -667,3 +675,48 @@ func addRaidedLSSDToVg(vgName, lssdPath string) error {
667
675
}
668
676
return nil
669
677
}
678
+
679
+ func fetchPvSizeGiB () (string , error ) {
680
+ args := []string {
681
+ "--select" ,
682
+ "-o" ,
683
+ "--noheadings" ,
684
+ "pv_size" ,
685
+ "--units=b" ,
686
+ }
687
+ // RAIDed device is always registered with its /dev/md127 equivalent in VG so cannot check it directly based on the RAIDed LSSD path which could be /dev/md/csi-driver-data-cache
688
+ info , err := common .RunCommand ("grep" /* pipedCmd */ , []string {"/dev/md" } /* pipedCmdArg */ , "pvs" , args ... )
689
+ if err != nil {
690
+ return "" , fmt .Errorf ("errored while fetching PV size %v: %s" , err , info )
691
+ }
692
+ infoString := strings .TrimSpace (string (info ))
693
+ infoSlice := strings .Fields (infoString )
694
+ pvSize , err := fetchNumberGiB (infoSlice )
695
+ if err != nil {
696
+ return "" , fmt .Errorf ("Error fetching PV size for cache %v" , err )
697
+ }
698
+ return pvSize , nil
699
+
700
+ }
701
+
702
+ func fetchNumberGiB (infoSlice []string ) (string , error ) {
703
+ re , err := regexp .Compile ("^[0-9]+B$" )
704
+ if err != nil {
705
+ return "" , fmt .Errorf ("Failed to compile regex match %v" , err )
706
+ }
707
+ var pvSize string
708
+ for _ , i := range infoSlice {
709
+ if re .MatchString (i ) {
710
+ pvSize , err = strings .TrimSuffix (i , "B" ), nil
711
+ if err != nil {
712
+ return "" , fmt .Errorf ("Failed to extract PV size %v" , err )
713
+ }
714
+ break
715
+ }
716
+ }
717
+ pvSizeInt , err := strconv .ParseFloat (pvSize , 64 )
718
+ if err != nil {
719
+ return "" , fmt .Errorf ("Error while fetching PV size for cache %v" , err )
720
+ }
721
+ return strconv .FormatInt (int64 (math .Ceil (pvSizeInt / GiB )), 10 ) + "GiB" , nil
722
+ }
0 commit comments