Skip to content

Commit

Permalink
mcs: tso server compare address without scheme (#8283)
Browse files Browse the repository at this point in the history
close #8284

Signed-off-by: lhy1024 <admin@liudos.us>

Co-authored-by: Hu# <jinhao.hu@pingcap.com>
  • Loading branch information
lhy1024 and HuSharp authored Jun 13, 2024
1 parent 3e34b80 commit 9dff6e6
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pkg/keyspace/tso_keyspace_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -874,7 +874,7 @@ func (m *GroupManager) SetPriorityForKeyspaceGroup(id uint32, node string, prior
inKeyspaceGroup := false
members := make([]endpoint.KeyspaceGroupMember, 0, len(kg.Members))
for _, member := range kg.Members {
if member.Address == node {
if member.CompareAddress(node) {
inKeyspaceGroup = true
member.Priority = priority
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/storage/endpoint/tso_keyspace_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/tikv/pd/pkg/slice"
"github.com/tikv/pd/pkg/storage/kv"
"github.com/tikv/pd/pkg/utils/typeutil"
"go.etcd.io/etcd/clientv3"
)

Expand Down Expand Up @@ -80,6 +81,14 @@ type KeyspaceGroupMember struct {
Priority int `json:"priority"`
}

// CompareAddress compares the address with the given address.
// It compares the address without the scheme.
// Otherwise, it will not work when we update the scheme from http to https.
// Issue: https://github.com/tikv/pd/issues/8284
func (m *KeyspaceGroupMember) CompareAddress(addr string) bool {
return typeutil.EqualBaseURLs(m.Address, addr)
}

// SplitState defines the split state of a keyspace group.
type SplitState struct {
// SplitSource is the current keyspace group ID from which the keyspace group is split.
Expand Down
4 changes: 2 additions & 2 deletions pkg/tso/keyspace_group_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ func (s *state) getNextPrimaryToReset(
if member.Priority > maxPriority {
maxPriority = member.Priority
}
if member.Address == localAddress {
if member.CompareAddress(localAddress) {
localPriority = member.Priority
}
}
Expand Down Expand Up @@ -667,7 +667,7 @@ func (kgm *KeyspaceGroupManager) primaryPriorityCheckLoop() {

func (kgm *KeyspaceGroupManager) isAssignedToMe(group *endpoint.KeyspaceGroup) bool {
return slice.AnyOf(group.Members, func(i int) bool {
return group.Members[i].Address == kgm.tsoServiceID.ServiceAddr
return group.Members[i].CompareAddress(kgm.tsoServiceID.ServiceAddr)
})
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/tso/keyspace_group_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ func collectAssignedKeyspaceGroupIDs(re *require.Assertions, kgm *KeyspaceGroupM
re.Equal(i, int(am.kgID))
re.Equal(i, int(kg.ID))
for _, m := range kg.Members {
if m.Address == kgm.tsoServiceID.ServiceAddr {
if m.CompareAddress(kgm.tsoServiceID.ServiceAddr) {
ids = append(ids, uint32(i))
break
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/utils/typeutil/comparison.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package typeutil
import (
"math"
"sort"
"strings"
"time"
)

Expand Down Expand Up @@ -78,3 +79,11 @@ func AreStringSlicesEquivalent(a, b []string) bool {
func Float64Equal(a, b float64) bool {
return math.Abs(a-b) <= 1e-6
}

// EqualBaseURLs compares two URLs without scheme.
func EqualBaseURLs(url1, url2 string) bool {
trimScheme := func(s string) string {
return strings.TrimPrefix(strings.TrimPrefix(s, "https://"), "http://")
}
return trimScheme(url1) == trimScheme(url2)
}
8 changes: 8 additions & 0 deletions pkg/utils/typeutil/comparison_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,11 @@ func TestAreStringSlicesEquivalent(t *testing.T) {
re.False(AreStringSlicesEquivalent([]string{}, []string{"a", "b"}))
re.False(AreStringSlicesEquivalent([]string{"a", "b"}, []string{}))
}

func TestCompareURLsWithoutScheme(t *testing.T) {
re := require.New(t)
re.True(EqualBaseURLs("", ""))
re.True(EqualBaseURLs("http://127.0.0.1", "http://127.0.0.1"))
re.True(EqualBaseURLs("http://127.0.0.1", "https://127.0.0.1"))
re.True(EqualBaseURLs("127.0.0.1", "http://127.0.0.1"))
}
2 changes: 1 addition & 1 deletion server/apiv2/handlers/tso_keyspace_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ func SetPriorityForKeyspaceGroup(c *gin.Context) {
// check if node exists
members := kg.Members
if slice.NoneOf(members, func(i int) bool {
return members[i].Address == node
return members[i].CompareAddress(node)
}) {
c.AbortWithStatusJSON(http.StatusBadRequest, "tso node does not exist in the keyspace group")
}
Expand Down

0 comments on commit 9dff6e6

Please sign in to comment.