@@ -175,10 +175,18 @@ func setupCaching(devicePath string, req *csi.NodeStageVolumeRequest, nodeId str
175175 klog .V (4 ).Infof ("Assuming valid data cache size and mode, resizing cache is not supported" )
176176 } else {
177177 cacheSize := req .GetPublishContext ()[common .ContextDataCacheSize ]
178- chunkSize , err := fetchChunkSizeKiB (cacheSize )
178+ maxChunkSizeStr := strconv .FormatInt (int64 (maxChunkSize / KiB ), 10 )
179+ var chunkSize string
180+ cachePvSize , err := fetchPvSizeGiB ()
179181 if err != nil {
180- klog .Errorf ("Errored to fetch cache size, verify the data-cache-size is valid: got %v, error: %q" , cacheSize , err )
181- return mainDevicePath , err
182+ klog .Errorf ("Errored while fetching PV size, got %v, falling back to default chunkSize of %v" , err , maxChunkSize )
183+ chunkSize = maxChunkSizeStr
184+ } else {
185+ chunkSize , err = fetchChunkSizeKiB (cachePvSize )
186+ if err != nil {
187+ klog .Errorf ("Errored to fetch cache size, verify the data-cache-size is valid: got %v, error: %q" , chunkSize , err )
188+ chunkSize = maxChunkSizeStr
189+ }
182190 }
183191 // Check if LV exists
184192 info , err = common .RunCommand ("" /* pipedCmd */ , nil /* pipedCmdArg */ , "lvs" , args ... )
@@ -650,7 +658,7 @@ func watchDiskDetaches(watcher *fsnotify.Watcher, nodeName string, errorCh chan
650658 klog .Errorf ("Error updating volume group's metadata: %v" , err )
651659 }
652660 reduceVolumeGroup (getVolumeGroupName (nodeName ), true )
653- klog .V (2 ).Infof ("disk attach/detach event %#v\n " , event )
661+ klog .V (6 ).Infof ("disk attach/detach event %#v\n " , event )
654662 }
655663 }
656664}
@@ -682,3 +690,48 @@ func addRaidedLSSDToVg(vgName, lssdPath string) error {
682690 }
683691 return nil
684692}
693+
694+ func fetchPvSizeGiB () (string , error ) {
695+ args := []string {
696+ "--select" ,
697+ "-o" ,
698+ "--noheadings" ,
699+ "pv_size" ,
700+ "--units=b" ,
701+ }
702+ // 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
703+ info , err := common .RunCommand ("grep" /* pipedCmd */ , []string {"/dev/md" } /* pipedCmdArg */ , "pvs" , args ... )
704+ if err != nil {
705+ return "" , fmt .Errorf ("errored while fetching PV size %v: %s" , err , info )
706+ }
707+ infoString := strings .TrimSpace (string (info ))
708+ infoSlice := strings .Fields (infoString )
709+ pvSize , err := fetchNumberGiB (infoSlice )
710+ if err != nil {
711+ return "" , fmt .Errorf ("Error fetching PV size for cache %v" , err )
712+ }
713+ return pvSize , nil
714+
715+ }
716+
717+ func fetchNumberGiB (infoSlice []string ) (string , error ) {
718+ re , err := regexp .Compile ("^[0-9]+B$" )
719+ if err != nil {
720+ return "" , fmt .Errorf ("Failed to compile regex match %v" , err )
721+ }
722+ var pvSize string
723+ for _ , i := range infoSlice {
724+ if re .MatchString (i ) {
725+ pvSize , err = strings .TrimSuffix (i , "B" ), nil
726+ if err != nil {
727+ return "" , fmt .Errorf ("Failed to extract PV size %v" , err )
728+ }
729+ break
730+ }
731+ }
732+ pvSizeInt , err := strconv .ParseFloat (pvSize , 64 )
733+ if err != nil {
734+ return "" , fmt .Errorf ("Error while fetching PV size for cache %v" , err )
735+ }
736+ return strconv .FormatInt (int64 (math .Ceil (pvSizeInt / GiB )), 10 ) + "GiB" , nil
737+ }
0 commit comments