forked from ceph/ceph-csi
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
util: added read affinity related functions and unit testcases
This commit adds util functions related to read affinity and unit testcases for the same. Signed-off-by: Praveen M <[email protected]>
- Loading branch information
1 parent
a93f3e2
commit 9cec05f
Showing
2 changed files
with
144 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
Copyright 2023 The Ceph-CSI Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package util | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// ConstructReadAffinityMapOption constructs a read affinity map option based on the provided crushLocationMap. | ||
// It appends crush location labels in the format | ||
// "read_from_replica=localize,crush_location=label1:value1|label2:value2|...". | ||
func ConstructReadAffinityMapOption(crushLocationMap map[string]string) string { | ||
if len(crushLocationMap) == 0 { | ||
return "" | ||
} | ||
|
||
var b strings.Builder | ||
b.WriteString("read_from_replica=localize,crush_location=") | ||
first := true | ||
for key, val := range crushLocationMap { | ||
if first { | ||
b.WriteString(fmt.Sprintf("%s:%s", key, val)) | ||
first = false | ||
} else { | ||
b.WriteString(fmt.Sprintf("|%s:%s", key, val)) | ||
} | ||
} | ||
|
||
return b.String() | ||
} | ||
|
||
// GetReadAffinityMapOptions retrieves the readAffinityMapOptions from the CSI config file if it exists. | ||
// If not, it falls back to returning the `cliReadAffinityMapOptions` from the command line. | ||
// If neither of these options is available, it returns an empty string. | ||
func GetReadAffinityMapOptions( | ||
clusterID, cliReadAffinityMapOptions string, nodeLabels map[string]string, | ||
) (string, error) { | ||
var ( | ||
err error | ||
configReadAffinityEnabled bool | ||
configCrushLocationLabels string | ||
) | ||
|
||
configReadAffinityEnabled, configCrushLocationLabels, err = GetCrushLocationLabels(CsiConfigFile, clusterID) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
if !configReadAffinityEnabled { | ||
return "", nil | ||
} | ||
|
||
if configCrushLocationLabels == "" { | ||
return cliReadAffinityMapOptions, nil | ||
} | ||
|
||
crushLocationMap := GetCrushLocationMap(configCrushLocationLabels, nodeLabels) | ||
readAffinityMapOptions := ConstructReadAffinityMapOption(crushLocationMap) | ||
|
||
return readAffinityMapOptions, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/* | ||
Copyright 2023 The Ceph-CSI Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package util | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestReadAffinity_ConstructReadAffinityMapOption(t *testing.T) { | ||
t.Parallel() | ||
tests := []struct { | ||
name string | ||
crushLocationmap map[string]string | ||
wantAny []string | ||
}{ | ||
{ | ||
name: "nil crushLocationmap", | ||
crushLocationmap: nil, | ||
wantAny: []string{""}, | ||
}, | ||
{ | ||
name: "empty crushLocationmap", | ||
crushLocationmap: map[string]string{}, | ||
wantAny: []string{""}, | ||
}, | ||
{ | ||
name: "single entry in crushLocationmap", | ||
crushLocationmap: map[string]string{ | ||
"region": "east", | ||
}, | ||
wantAny: []string{"read_from_replica=localize,crush_location=region:east"}, | ||
}, | ||
{ | ||
name: "multiple entries in crushLocationmap", | ||
crushLocationmap: map[string]string{ | ||
"region": "east", | ||
"zone": "east-1", | ||
}, | ||
wantAny: []string{ | ||
"read_from_replica=localize,crush_location=region:east|zone:east-1", | ||
"read_from_replica=localize,crush_location=zone:east-1|region:east", | ||
}, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
currentTT := tt | ||
t.Run(tt.name, func(t *testing.T) { | ||
t.Parallel() | ||
assert.Contains(t, currentTT.wantAny, ConstructReadAffinityMapOption(currentTT.crushLocationmap)) | ||
}) | ||
} | ||
} |