@@ -3,6 +3,7 @@ package gceGCEDriver
3
3
import (
4
4
"context"
5
5
"fmt"
6
+ "math"
6
7
"regexp"
7
8
"strconv"
8
9
"strings"
@@ -16,10 +17,13 @@ import (
16
17
)
17
18
18
19
const (
19
- cacheSuffix = "csi-fast"
20
- mainLvSuffix = "csi-main"
21
- raidedLocalSsdName = "csi-driver-data-cache"
22
- raidMode = "0"
20
+ cacheSuffix = "csi-fast"
21
+ mainLvSuffix = "csi-main"
22
+ raidedLocalSsdName = "csi-driver-data-cache"
23
+ raidMode = "0"
24
+ maxAllowedChunks int64 = 1000000 // This is the max allowed chunks for LVM
25
+ GiB int64 = 1024 * 1024 * 1024
26
+ KiB int64 = 1024
23
27
)
24
28
25
29
func fetchRAIDedLocalSsdPath () (string , error ) {
@@ -159,21 +163,30 @@ func setupCaching(devicePath string, req *csi.NodeStageVolumeRequest, nodeId str
159
163
// Validate that cache is setup for required size
160
164
klog .V (4 ).Infof ("Assuming valid data cache size and mode, resizing cache is not supported" )
161
165
} else {
162
- fastCacheSize := req .GetPublishContext ()[common .ContextDataCacheSize ]
163
- chunkSize := "960" // Cannot use default chunk size(64KiB) as it errors on maxChunksAllowed. Unit - KiB
164
- args = []string {
165
- "--yes" ,
166
- "-n" ,
167
- cacheLvName ,
168
- "-L" ,
169
- // ConvertGiStringToInt64 converts the input size to GiB so default to "g" for cache size - LVM g|G is GiB.
170
- fastCacheSize + "g" ,
171
- volumeGroupName ,
172
- raidedLocalSsdPath ,
173
- }
174
- info , err = common .RunCommand ("" /* pipedCmd */ , nil /* pipedCmdArg */ , "lvcreate" , args ... )
166
+ cacheSize := req .GetPublishContext ()[common .ContextDataCacheSize ]
167
+ chunkSize , err := fetchChunkSize (cacheSize )
175
168
if err != nil {
176
- return mainDevicePath , fmt .Errorf ("Errored while creating cache %w: %s" , err , info )
169
+ klog .Errorf ("Errored to fetch cache size, verify the data-cache-size is valid: got %v, error: %q" , cacheSize , err )
170
+ return mainDevicePath , err
171
+ }
172
+ // Check if LV exists
173
+ info , err = common .RunCommand ("" /* pipedCmd */ , nil /* pipedCmdArg */ , "lvs" , args ... )
174
+ lvExists := strings .Contains (string (info ), cacheLvName )
175
+ if ! lvExists {
176
+ args = []string {
177
+ "--yes" ,
178
+ "-n" ,
179
+ cacheLvName ,
180
+ "-L" ,
181
+ // ConvertGiStringToInt64 converts the input size to GiB so default to "g" for cache size - LVM g|G is GiB.
182
+ cacheSize + "g" ,
183
+ volumeGroupName ,
184
+ raidedLocalSsdPath ,
185
+ }
186
+ info , err = common .RunCommand ("" /* pipedCmd */ , nil /* pipedCmdArg */ , "lvcreate" , args ... )
187
+ if err != nil {
188
+ return mainDevicePath , fmt .Errorf ("Errored while creating cache %w: %s" , err , info )
189
+ }
177
190
}
178
191
179
192
// Once caching is setup, link the PD to cache
@@ -188,7 +201,7 @@ func setupCaching(devicePath string, req *csi.NodeStageVolumeRequest, nodeId str
188
201
req .GetPublishContext ()[common .ContextDataCacheMode ],
189
202
volumeGroupName + "/" + mainLvName ,
190
203
"--chunksize" ,
191
- string ( chunkSize ) ,
204
+ chunkSize ,
192
205
"--force" ,
193
206
"-y" ,
194
207
}
@@ -497,3 +510,18 @@ func isCachingSetup(mainLvName string) (error, bool) {
497
510
}
498
511
return nil , false
499
512
}
513
+
514
+ func fetchChunkSize (cacheSize string ) (string , error ) {
515
+ var chunkSize float64
516
+ var maxChunkSize int64 = 1 * GiB // Max allowed chunk size as per LVM documentation
517
+ var minChunkSize int64 = 320 * KiB // This is randomly selected, we need a multiple of 32KiB, the default size would be too small for caching https://man7.org/linux/man-pages/man8/lvcreate.8.html (--chunksize)
518
+ cacheSizeInt , err := common .ConvertGiStringToInt64 (cacheSize )
519
+ if err != nil {
520
+ return "0" , err
521
+ }
522
+ // Chunksize should be divisible by 32Kib so we need (chunksize/32*1024)*32*1024
523
+ chunkSize = float64 (cacheSizeInt ) / float64 (maxAllowedChunks )
524
+ chunkSize = math .Ceil (chunkSize / float64 (32 * KiB )) * float64 (32 * KiB )
525
+ chunkSize = math .Min (math .Max (chunkSize , float64 (minChunkSize )), float64 (maxChunkSize ))
526
+ return strconv .FormatInt (int64 (chunkSize ), 10 ), nil
527
+ }
0 commit comments