Skip to content

Commit ed2a342

Browse files
committed
update cache logic to calculate chunk size based on toatl cache
1 parent c5c4c87 commit ed2a342

File tree

1 file changed

+47
-3
lines changed

1 file changed

+47
-3
lines changed

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

+47-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...)
@@ -674,3 +682,39 @@ func addRaidedLSSDToVg(vgName, lssdPath string) error {
674682
}
675683
return nil
676684
}
685+
686+
func fetchPvSize(pvName string) (string, error) {
687+
var pvSize string
688+
args := []string{
689+
"--select",
690+
"pv_name=" + pvName,
691+
"-o",
692+
"--noheadings",
693+
"pv_size",
694+
"--units=b",
695+
}
696+
info, err := common.RunCommand("" /* pipedCmd */, nil /* pipedCmdArg */, "pvs", args...)
697+
if err != nil {
698+
return "", fmt.Errorf("errored while fetching PV size %v: %s", err, info)
699+
}
700+
infoString := strings.TrimSpace(string(info))
701+
infoSlice := strings.Fields(infoString)
702+
re, err := regexp.Compile("^[0-9]+B$")
703+
if err != nil {
704+
return "", fmt.Errorf("Errored while compiling regex for PV size")
705+
}
706+
for _, i := range infoSlice {
707+
if re.MatchString(i) {
708+
pvSize = strings.TrimSuffix(i, "B")
709+
break
710+
}
711+
}
712+
if pvSize != "" {
713+
pvSizeInt, err := strconv.ParseFloat(pvSize, 64)
714+
if err != nil {
715+
return "", fmt.Errorf("Error while fetching PV size for cache")
716+
}
717+
return strconv.FormatInt(int64(pvSizeInt/KiB), 10) + "KiB", nil
718+
}
719+
return "", fmt.Errorf("Error fetching PV size for cache")
720+
}

0 commit comments

Comments
 (0)