Skip to content

Commit f69a7e8

Browse files
committed
Use NullKeyspaceID to solve the backward compatibility
Signed-off-by: JmPotato <github@ipotato.me>
1 parent d250eae commit f69a7e8

File tree

6 files changed

+45
-28
lines changed

6 files changed

+45
-28
lines changed

pkg/mcs/resourcemanager/server/manager.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ func (m *Manager) DeleteResourceGroup(name string) error {
285285
if name == reservedDefaultGroupName {
286286
return errs.ErrDeleteReservedGroup
287287
}
288-
if err := m.storage.DeleteResourceGroupSetting(constant.DefaultKeyspaceID, name); err != nil {
288+
if err := m.storage.DeleteResourceGroupSetting(constant.NullKeyspaceID, name); err != nil {
289289
return err
290290
}
291291
m.Lock()

pkg/mcs/resourcemanager/server/resource_group.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,7 @@ func (rg *ResourceGroup) IntoProtoResourceGroup() *rmpb.ResourceGroup {
234234
// TODO: persist the state of the group separately.
235235
func (rg *ResourceGroup) persistSettings(storage endpoint.ResourceGroupStorage) error {
236236
metaGroup := rg.IntoProtoResourceGroup()
237-
return storage.SaveResourceGroupSetting(constant.DefaultKeyspaceID, rg.Name, metaGroup)
237+
return storage.SaveResourceGroupSetting(constant.NullKeyspaceID, rg.Name, metaGroup)
238238
}
239239

240240
// GroupStates is the tokens set of a resource group.
@@ -302,5 +302,5 @@ func (rg *ResourceGroup) UpdateRUConsumption(c *rmpb.Consumption) {
302302
// persistStates persists the resource group tokens.
303303
func (rg *ResourceGroup) persistStates(storage endpoint.ResourceGroupStorage) error {
304304
states := rg.GetGroupStates()
305-
return storage.SaveResourceGroupStates(constant.DefaultKeyspaceID, rg.Name, states)
305+
return storage.SaveResourceGroupStates(constant.NullKeyspaceID, rg.Name, states)
306306
}

pkg/storage/endpoint/resource_group.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,19 @@ var _ ResourceGroupStorage = (*StorageEndpoint)(nil)
4040

4141
// SaveResourceGroupSetting stores a resource group to storage.
4242
func (se *StorageEndpoint) SaveResourceGroupSetting(keyspaceID uint32, name string, msg proto.Message) error {
43-
if keyspaceID == constant.DefaultKeyspaceID {
44-
return se.saveProto(keypath.ResourceGroupSettingPath(name), msg)
45-
}
4643
return se.saveProto(keypath.KeyspaceResourceGroupSettingPath(keyspaceID, name), msg)
4744
}
4845

4946
// DeleteResourceGroupSetting removes a resource group from storage.
5047
func (se *StorageEndpoint) DeleteResourceGroupSetting(keyspaceID uint32, name string) error {
51-
if keyspaceID == constant.DefaultKeyspaceID {
52-
return se.Remove(keypath.ResourceGroupSettingPath(name))
53-
}
5448
return se.Remove(keypath.KeyspaceResourceGroupSettingPath(keyspaceID, name))
5549
}
5650

5751
// LoadResourceGroupSettings loads all resource groups from storage.
5852
func (se *StorageEndpoint) LoadResourceGroupSettings(f func(keyspaceID uint32, name string, rawValue string)) error {
5953
if err := se.loadRangeByPrefix(keypath.ResourceGroupSettingPrefix(), func(key, value string) {
60-
// Using the default keyspace ID for the resource group settings loaded from the legacy path.
61-
f(constant.DefaultKeyspaceID, key, value)
54+
// Using the null keyspace ID for the resource group settings loaded from the legacy path.
55+
f(constant.NullKeyspaceID, key, value)
6256
}); err != nil {
6357
return err
6458
}
@@ -75,25 +69,19 @@ func (se *StorageEndpoint) LoadResourceGroupSettings(f func(keyspaceID uint32, n
7569

7670
// SaveResourceGroupStates stores a resource group to storage.
7771
func (se *StorageEndpoint) SaveResourceGroupStates(keyspaceID uint32, name string, obj any) error {
78-
if keyspaceID == constant.DefaultKeyspaceID {
79-
return se.saveJSON(keypath.ResourceGroupStatePath(name), obj)
80-
}
8172
return se.saveJSON(keypath.KeyspaceResourceGroupStatePath(keyspaceID, name), obj)
8273
}
8374

8475
// DeleteResourceGroupStates removes a resource group from storage.
8576
func (se *StorageEndpoint) DeleteResourceGroupStates(keyspaceID uint32, name string) error {
86-
if keyspaceID == constant.DefaultKeyspaceID {
87-
return se.Remove(keypath.ResourceGroupStatePath(name))
88-
}
8977
return se.Remove(keypath.KeyspaceResourceGroupStatePath(keyspaceID, name))
9078
}
9179

9280
// LoadResourceGroupStates loads all resource groups from storage.
9381
func (se *StorageEndpoint) LoadResourceGroupStates(f func(keyspaceID uint32, name string, rawValue string)) error {
9482
if err := se.loadRangeByPrefix(keypath.ResourceGroupStatePrefix(), func(key, value string) {
95-
// Using the default keyspace ID for the resource group states loaded from the legacy path.
96-
f(constant.DefaultKeyspaceID, key, value)
83+
// Using the null keyspace ID for the resource group states loaded from the legacy path.
84+
f(constant.NullKeyspaceID, key, value)
9785
}); err != nil {
9886
return err
9987
}

pkg/storage/storage_resource_group_test.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package storage
1616

1717
import (
18+
"math/rand"
1819
"testing"
1920

2021
"github.com/stretchr/testify/require"
@@ -29,9 +30,10 @@ func TestResourceGroupStorage(t *testing.T) {
2930
storage := NewStorageWithMemoryBackend()
3031

3132
keyspaceGroups := map[uint32][]string{
32-
constant.DefaultKeyspaceID: {"rg1", "rg2", "rg/3", "4/rg/5"},
33-
1: {"rg1", "rg2", "rg/3", "4/rg/5"},
34-
2: {"rg1", "rg2", "rg/3", "4/rg/5"},
33+
constant.NullKeyspaceID: generateRandomResourceGroupNames(100),
34+
constant.DefaultKeyspaceID: generateRandomResourceGroupNames(100),
35+
1: generateRandomResourceGroupNames(100),
36+
2: generateRandomResourceGroupNames(100),
3537
}
3638
// Test legacy and keyspace resource group settings.
3739
for keyspaceID, names := range keyspaceGroups {
@@ -77,3 +79,20 @@ func TestResourceGroupStorage(t *testing.T) {
7779
})
7880
re.NoError(err)
7981
}
82+
83+
const charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_=+[]{}|;:,.<>?/"
84+
85+
// generateRandomResourceGroupNames generates n random resource group names with letters, numbers and symbols.
86+
func generateRandomResourceGroupNames(n int) []string {
87+
names := make([]string, n)
88+
for i := 0; i < n; i++ {
89+
// Generate a random length between 1 and 100 characters.
90+
length := 1 + rand.Intn(100)
91+
name := make([]byte, length)
92+
for j := 0; j < length; j++ {
93+
name[j] = charset[rand.Intn(len(charset))]
94+
}
95+
names[i] = string(name)
96+
}
97+
return names
98+
}

pkg/utils/keypath/resource_group.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,40 +18,48 @@ import (
1818
"fmt"
1919
"strconv"
2020
"strings"
21+
22+
"github.com/tikv/pd/pkg/mcs/utils/constant"
2123
)
2224

2325
// ControllerConfigPath returns the path to save the controller config.
2426
func ControllerConfigPath() string {
2527
return controllerConfigPath
2628
}
2729

28-
// ResourceGroupSettingPath returns the path to save the legacy resource group settings.
29-
func ResourceGroupSettingPath(groupName string) string {
30+
// resourceGroupSettingPath returns the path to save the legacy resource group settings.
31+
func resourceGroupSettingPath(groupName string) string {
3032
return fmt.Sprintf(resourceGroupSettingsPathFormat, groupName)
3133
}
3234

33-
// ResourceGroupStatePath returns the path to save the legacy resource group states.
34-
func ResourceGroupStatePath(groupName string) string {
35+
// resourceGroupStatePath returns the path to save the legacy resource group states.
36+
func resourceGroupStatePath(groupName string) string {
3537
return fmt.Sprintf(resourceGroupStatesPathFormat, groupName)
3638
}
3739

3840
// ResourceGroupSettingPrefix returns the prefix of the legacy resource group settings.
3941
func ResourceGroupSettingPrefix() string {
40-
return ResourceGroupSettingPath("")
42+
return resourceGroupSettingPath("")
4143
}
4244

4345
// ResourceGroupStatePrefix returns the prefix of the legacy resource group states.
4446
func ResourceGroupStatePrefix() string {
45-
return ResourceGroupStatePath("")
47+
return resourceGroupStatePath("")
4648
}
4749

4850
// KeyspaceResourceGroupSettingPath returns the path to save the keyspace resource group settings.
4951
func KeyspaceResourceGroupSettingPath(keyspaceID uint32, groupName string) string {
52+
if keyspaceID == constant.NullKeyspaceID {
53+
return resourceGroupSettingPath(groupName)
54+
}
5055
return fmt.Sprintf(keyspaceResourceGroupSettingsPathFormat, keyspaceID, groupName)
5156
}
5257

5358
// KeyspaceResourceGroupStatePath returns the path to save the keyspace resource group states.
5459
func KeyspaceResourceGroupStatePath(keyspaceID uint32, groupName string) string {
60+
if keyspaceID == constant.NullKeyspaceID {
61+
return resourceGroupStatePath(groupName)
62+
}
5563
return fmt.Sprintf(keyspaceResourceGroupStatesPathFormat, keyspaceID, groupName)
5664
}
5765

tools/pd-ut/testdata/group.10.etcdkey

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ encryption_keys get
6969
encryption_keys watch
7070
resource_group/controller get
7171
resource_group/controller save
72+
resource_group/keyspace/settings/ get
73+
resource_group/keyspace/states/ get
7274
resource_group/settings/ get
7375
resource_group/settings/default save
7476
resource_group/states/ get

0 commit comments

Comments
 (0)