Skip to content

Commit

Permalink
Add enable-parallel-downloads flag in user agent (#2502)
Browse files Browse the repository at this point in the history
* Add enable-parallel-downloads flag in user agent

* dummy

* dummy
  • Loading branch information
sethiay authored Sep 18, 2024
1 parent 9bc632d commit 9f13ed7
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 47 deletions.
4 changes: 4 additions & 0 deletions cfg/config_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ func IsFileCacheEnabled(mountConfig *Config) bool {
return mountConfig.FileCache.MaxSizeMb != 0 && string(mountConfig.CacheDir) != ""
}

func IsParallelDownloadsEnabled(mountConfig *Config) bool {
return IsFileCacheEnabled(mountConfig) && mountConfig.FileCache.EnableParallelDownloads
}

// ListCacheTTLSecsToDuration converts TTL in seconds to time.Duration.
func ListCacheTTLSecsToDuration(secs int64) time.Duration {
err := isTTLInSecsValid(secs)
Expand Down
53 changes: 53 additions & 0 deletions cfg/config_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,59 @@ func TestIsFileCacheEnabled(t *testing.T) {

}

func TestIsParallelDownloadsEnabled(t *testing.T) {
testCases := []struct {
name string
config *Config
expectedIsParallelDownloadsEnabled bool
}{
{
name: "Config with file-cache enabled",
config: &Config{
CacheDir: "/tmp/folder/",
FileCache: FileCacheConfig{
MaxSizeMb: -1,
},
},
expectedIsParallelDownloadsEnabled: false,
},
{
name: "Empty Config.",
config: &Config{},
expectedIsParallelDownloadsEnabled: false,
},
{
name: "Config with file-cache disabled but enable parallel downloads is set.",
config: &Config{
CacheDir: "",
FileCache: FileCacheConfig{
MaxSizeMb: -1,
EnableParallelDownloads: true,
},
},
expectedIsParallelDownloadsEnabled: false,
},
{
name: "Config with file-cache and parallel downloads enabled.",
config: &Config{
CacheDir: "//tmp//folder//",
FileCache: FileCacheConfig{
MaxSizeMb: -1,
EnableParallelDownloads: true,
},
},
expectedIsParallelDownloadsEnabled: true,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.expectedIsParallelDownloadsEnabled, IsParallelDownloadsEnabled(tc.config))
})
}

}

func Test_ListCacheTtlSecsToDuration(t *testing.T) {
var testCases = []struct {
testName string
Expand Down
6 changes: 5 additions & 1 deletion cmd/legacy_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,11 @@ func getConfigForUserAgent(mountConfig *cfg.Config) string {
if mountConfig.FileCache.CacheFileForRangeRead {
isFileCacheForRangeReadEnabled = "1"
}
return fmt.Sprintf("%s:%s", isFileCacheEnabled, isFileCacheForRangeReadEnabled)
isParallelDownloadsEnabled := "0"
if cfg.IsParallelDownloadsEnabled(mountConfig) {
isParallelDownloadsEnabled = "1"
}
return fmt.Sprintf("%s:%s:%s", isFileCacheEnabled, isFileCacheForRangeReadEnabled, isParallelDownloadsEnabled)
}
func createStorageHandle(newConfig *cfg.Config, userAgent string) (storageHandle storage.StorageHandle, err error) {
storageClientConfig := storageutil.StorageClientConfig{
Expand Down
132 changes: 86 additions & 46 deletions cmd/legacy_main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,74 +69,114 @@ func (t *MainTest) TestGetUserAgentWhenMetadataImageTypeEnvVarIsSet() {

mountConfig := &cfg.Config{}
userAgent := getUserAgent("AppName", getConfigForUserAgent(mountConfig))
expectedUserAgent := strings.TrimSpace(fmt.Sprintf("gcsfuse/%s AppName (GPN:gcsfuse-DLVM) (Cfg:0:0)", common.GetVersion()))
expectedUserAgent := strings.TrimSpace(fmt.Sprintf("gcsfuse/%s AppName (GPN:gcsfuse-DLVM) (Cfg:0:0:0)", common.GetVersion()))

assert.Equal(t.T(), expectedUserAgent, userAgent)
}

func (t *MainTest) TestGetUserAgentWhenMetadataImageTypeEnvVarIsNotSet() {
mountConfig := &cfg.Config{}
userAgent := getUserAgent("AppName", getConfigForUserAgent(mountConfig))
expectedUserAgent := strings.TrimSpace(fmt.Sprintf("gcsfuse/%s (GPN:gcsfuse-AppName) (Cfg:0:0)", common.GetVersion()))
expectedUserAgent := strings.TrimSpace(fmt.Sprintf("gcsfuse/%s (GPN:gcsfuse-AppName) (Cfg:0:0:0)", common.GetVersion()))

assert.Equal(t.T(), expectedUserAgent, userAgent)
}

func (t *MainTest) TestGetUserAgentConfigWithNoFileCache() {
mountConfig := &cfg.Config{}
userAgent := getUserAgent("AppName", getConfigForUserAgent(mountConfig))
expectedUserAgent := strings.TrimSpace(fmt.Sprintf("gcsfuse/%s (GPN:gcsfuse-AppName) (Cfg:0:0)", common.GetVersion()))
expectedUserAgent := strings.TrimSpace(fmt.Sprintf("gcsfuse/%s (GPN:gcsfuse-AppName) (Cfg:0:0:0)", common.GetVersion()))
assert.Equal(t.T(), expectedUserAgent, userAgent)
}

func (t *MainTest) TestGetUserAgentConfigWithFileCacheEnabledRandomReadEnabled() {
mountConfig := &cfg.Config{
CacheDir: "//tmp//folder//",
FileCache: cfg.FileCacheConfig{
MaxSizeMb: -1,
CacheFileForRangeRead: true,
func (t *MainTest) TestGetUserAgentConfig() {
testCases := []struct {
name string
mountConfig *cfg.Config
expectedUserAgent string
}{
{
name: "Config with file cache disabled when cache dir is given but maxsize is set 0.",
mountConfig: &cfg.Config{
CacheDir: "//tmp//folder//",
FileCache: cfg.FileCacheConfig{
MaxSizeMb: 0,
},
},
expectedUserAgent: strings.TrimSpace(fmt.Sprintf("gcsfuse/%s (GPN:gcsfuse-AppName) (Cfg:0:0:0)", common.GetVersion())),
},
}
userAgent := getUserAgent("AppName", getConfigForUserAgent(mountConfig))
expectedUserAgent := strings.TrimSpace(fmt.Sprintf("gcsfuse/%s (GPN:gcsfuse-AppName) (Cfg:1:1)", common.GetVersion()))
assert.Equal(t.T(), expectedUserAgent, userAgent)
}

func (t *MainTest) TestGetUserAgentConfigWithFileCacheEnabledRandomDisabled() {
// Test File Cache Enabled but Random Read Disabled
mountConfig := &cfg.Config{
CacheDir: "//tmp//folder//",
FileCache: cfg.FileCacheConfig{
MaxSizeMb: -1,
{
name: "Config with file cache disabled where maxsize is set but cache dir is not set.",
mountConfig: &cfg.Config{
FileCache: cfg.FileCacheConfig{
MaxSizeMb: -1,
},
},
expectedUserAgent: strings.TrimSpace(fmt.Sprintf("gcsfuse/%s (GPN:gcsfuse-AppName) (Cfg:0:0:0)", common.GetVersion())),
},
}
userAgent := getUserAgent("AppName", getConfigForUserAgent(mountConfig))
expectedUserAgent := strings.TrimSpace(fmt.Sprintf("gcsfuse/%s (GPN:gcsfuse-AppName) (Cfg:1:0)", common.GetVersion()))
assert.Equal(t.T(), expectedUserAgent, userAgent)
}
func (t *MainTest) TestGetUserAgentConfigWithFileCacheSizeSetCacheDirNotSet() {
// Test File cache disabled where MaxSize is set but Cache Dir is not set.
mountConfig := &cfg.Config{
FileCache: cfg.FileCacheConfig{
MaxSizeMb: -1,
{
name: "Config with file cache enabled but random read disabled.",
mountConfig: &cfg.Config{
CacheDir: "//tmp//folder//",
FileCache: cfg.FileCacheConfig{
MaxSizeMb: -1,
},
},
expectedUserAgent: strings.TrimSpace(fmt.Sprintf("gcsfuse/%s (GPN:gcsfuse-AppName) (Cfg:1:0:0)", common.GetVersion())),
},
{
name: "Config with file cache and random read enabled.",
mountConfig: &cfg.Config{
CacheDir: "//tmp//folder//",
FileCache: cfg.FileCacheConfig{
MaxSizeMb: -1,
CacheFileForRangeRead: true,
},
},
expectedUserAgent: strings.TrimSpace(fmt.Sprintf("gcsfuse/%s (GPN:gcsfuse-AppName) (Cfg:1:1:0)", common.GetVersion())),
},
{
name: "Config with file cache disabled and enable parallel downloads set.",
mountConfig: &cfg.Config{
CacheDir: "",
FileCache: cfg.FileCacheConfig{
MaxSizeMb: -1,
EnableParallelDownloads: true,
},
},
expectedUserAgent: strings.TrimSpace(fmt.Sprintf("gcsfuse/%s (GPN:gcsfuse-AppName) (Cfg:0:0:0)", common.GetVersion())),
},
{
name: "Config with file cache and parallel downloads enabled.",
mountConfig: &cfg.Config{
CacheDir: "/cache/path",
FileCache: cfg.FileCacheConfig{
MaxSizeMb: -1,
EnableParallelDownloads: true,
},
},
expectedUserAgent: strings.TrimSpace(fmt.Sprintf("gcsfuse/%s (GPN:gcsfuse-AppName) (Cfg:1:0:1)", common.GetVersion())),
},
{
name: "Config with file cache, random reads and parallel downloads enabled.",
mountConfig: &cfg.Config{
CacheDir: "/cache/path",
FileCache: cfg.FileCacheConfig{
MaxSizeMb: -1,
CacheFileForRangeRead: true,
EnableParallelDownloads: true,
},
},
expectedUserAgent: strings.TrimSpace(fmt.Sprintf("gcsfuse/%s (GPN:gcsfuse-AppName) (Cfg:1:1:1)", common.GetVersion())),
},
}
userAgent := getUserAgent("AppName", getConfigForUserAgent(mountConfig))
expectedUserAgent := strings.TrimSpace(fmt.Sprintf("gcsfuse/%s (GPN:gcsfuse-AppName) (Cfg:0:0)", common.GetVersion()))
assert.Equal(t.T(), expectedUserAgent, userAgent)
}

func (t *MainTest) TestGetUserAgentConfigWithCacheDirSetMaxSizeDisabled() {
// Test File Cache disabled when Cache Dir is given but maxSize is set 0.
mountConfig := &cfg.Config{
CacheDir: "//tmp//folder//",
FileCache: cfg.FileCacheConfig{
MaxSizeMb: 0,
},
for _, tc := range testCases {
t.T().Run(tc.name, func(t *testing.T) {
userAgent := getUserAgent("AppName", getConfigForUserAgent(tc.mountConfig))
assert.Equal(t, tc.expectedUserAgent, userAgent)
})
}
userAgent := getUserAgent("AppName", getConfigForUserAgent(mountConfig))
expectedUserAgent := strings.TrimSpace(fmt.Sprintf("gcsfuse/%s (GPN:gcsfuse-AppName) (Cfg:0:0)", common.GetVersion()))
assert.Equal(t.T(), expectedUserAgent, userAgent)
}

func (t *MainTest) TestGetUserAgentWhenMetadataImageTypeEnvVarSetAndAppNameNotSet() {
Expand All @@ -145,7 +185,7 @@ func (t *MainTest) TestGetUserAgentWhenMetadataImageTypeEnvVarSetAndAppNameNotSe

mountConfig := &cfg.Config{}
userAgent := getUserAgent("", getConfigForUserAgent(mountConfig))
expectedUserAgent := strings.TrimSpace(fmt.Sprintf("gcsfuse/%s (GPN:gcsfuse-DLVM) (Cfg:0:0)", common.GetVersion()))
expectedUserAgent := strings.TrimSpace(fmt.Sprintf("gcsfuse/%s (GPN:gcsfuse-DLVM) (Cfg:0:0:0)", common.GetVersion()))

assert.Equal(t.T(), expectedUserAgent, userAgent)
}
Expand Down

0 comments on commit 9f13ed7

Please sign in to comment.