Skip to content

Commit 9defa2a

Browse files
committed
Update chunk size logic to use total cache size
1 parent c5c4c87 commit 9defa2a

File tree

1 file changed

+62
-3
lines changed

1 file changed

+62
-3
lines changed

Diff for: pkg/gce-pd-csi-driver/cache.go

+62-3
Original file line numberDiff line numberDiff line change
@@ -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.FormatFloat(maxChunkSize, 'g', -1, 64)
179+
var chunkSize string
180+
cachePvSize, err := fetchPvSize(raidedLocalSsdPath)
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...)
@@ -639,6 +647,21 @@ func watchDiskDetaches(watcher *fsnotify.Watcher, nodeName string, errorCh chan
639647
errorCh <- fmt.Errorf("disk update event errored: %v", err)
640648
// watch for events
641649
case event := <-watcher.Events:
650+
args := []string{
651+
"--updatemetadata",
652+
getVolumeGroupName(nodeName),
653+
}
654+
_, err := common.RunCommand("" /* pipedCmd */, nil /* pipedCmdArg */, "vgck", args...)
655+
if err != nil {
656+
klog.Errorf("Error updating volume group's metadata: %v", err)
657+
}
658+
args = []string{}
659+
info, _ := common.RunCommand("" /* pipedCmd */, nil /* pipedCmdArg */, "pvs", args...)
660+
args = []string{
661+
getVolumeGroupName(nodeName),
662+
}
663+
infoVG, _ := common.RunCommand("" /* pipedCmd */, nil /* pipedCmdArg */, "vgdisplay", args...)
664+
klog.Infof("Got VG %s and PV %s", infoVG, info)
642665
// In case of an event i.e. creation or deletion of any new PV, we update the VG metadata.
643666
// This might include some non-LVM changes, no harm in updating metadata multiple times.
644667
reduceVolumeGroup(getVolumeGroupName(nodeName), true)
@@ -674,3 +697,39 @@ func addRaidedLSSDToVg(vgName, lssdPath string) error {
674697
}
675698
return nil
676699
}
700+
701+
func fetchPvSize(pvName string) (string, error) {
702+
var pvSize string
703+
args := []string{
704+
"--select",
705+
"pv_name=" + pvName,
706+
"-o",
707+
"--noheadings",
708+
"pv_size",
709+
"--units=b",
710+
}
711+
info, err := common.RunCommand("" /* pipedCmd */, nil /* pipedCmdArg */, "pvs", args...)
712+
if err != nil {
713+
return "", fmt.Errorf("errored while fetching PV size %v: %s", err, info)
714+
}
715+
infoString := strings.TrimSpace(string(info))
716+
infoSlice := strings.Fields(infoString)
717+
re, err := regexp.Compile("^[0-9]+B$")
718+
if err != nil {
719+
return "", fmt.Errorf("Errored while compiling regex for PV size")
720+
}
721+
for _, i := range infoSlice {
722+
if re.MatchString(i) {
723+
pvSize = strings.TrimSuffix(i, "B")
724+
break
725+
}
726+
}
727+
if pvSize != "" {
728+
pvSizeInt, err := strconv.ParseFloat(pvSize, 64)
729+
if err != nil {
730+
return "", fmt.Errorf("Error while fetching PV size for cache")
731+
}
732+
return strconv.FormatInt(int64(pvSizeInt/KiB), 10) + "KiB", nil
733+
}
734+
return "", fmt.Errorf("Error fetching PV size for cache")
735+
}

0 commit comments

Comments
 (0)