Skip to content

Commit

Permalink
mcs: tso server compare address without scheme
Browse files Browse the repository at this point in the history
Signed-off-by: lhy1024 <[email protected]>
  • Loading branch information
lhy1024 committed Jun 13, 2024
1 parent 14c68f6 commit d7f8baf
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 5 deletions.
3 changes: 2 additions & 1 deletion pkg/keyspace/tso_keyspace_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/tikv/pd/pkg/utils/etcdutil"
"github.com/tikv/pd/pkg/utils/logutil"
"github.com/tikv/pd/pkg/utils/syncutil"
"github.com/tikv/pd/pkg/utils/typeutil"
"go.etcd.io/etcd/clientv3"
"go.etcd.io/etcd/mvcc/mvccpb"
"go.uber.org/zap"
Expand Down Expand Up @@ -874,7 +875,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 typeutil.CompareURLsWithoutScheme(member.Address, node) {
inKeyspaceGroup = true
member.Priority = priority
}
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 typeutil.CompareURLsWithoutScheme(member.Address, 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 typeutil.CompareURLsWithoutScheme(group.Members[i].Address, 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 typeutil.CompareURLsWithoutScheme(m.Address, kgm.tsoServiceID.ServiceAddr) {
ids = append(ids, uint32(i))
break
}
Expand Down
15 changes: 15 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,17 @@ func AreStringSlicesEquivalent(a, b []string) bool {
func Float64Equal(a, b float64) bool {
return math.Abs(a-b) <= 1e-6
}

// CompareURLsWithoutScheme compares two URLs without scheme.
func CompareURLsWithoutScheme(url1, url2 string) bool {
trimScheme := func(url string) string {
if strings.HasPrefix(url, "http://") {
return strings.TrimPrefix(url, "http://")
} else if strings.HasPrefix(url, "https://") {
return strings.TrimPrefix(url, "https://")
}
return url
}

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(CompareURLsWithoutScheme("", ""))
re.True(CompareURLsWithoutScheme("http://127.0.0.1", "http://127.0.0.1"))
re.True(CompareURLsWithoutScheme("http://127.0.0.1", "https://127.0.0.1"))
re.True(CompareURLsWithoutScheme("127.0.0.1", "http://127.0.0.1"))
}
3 changes: 2 additions & 1 deletion server/apiv2/handlers/tso_keyspace_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/tikv/pd/pkg/slice"
"github.com/tikv/pd/pkg/storage/endpoint"
"github.com/tikv/pd/pkg/utils/syncutil"
"github.com/tikv/pd/pkg/utils/typeutil"
"github.com/tikv/pd/server"
"github.com/tikv/pd/server/apiv2/middlewares"
)
Expand Down Expand Up @@ -507,7 +508,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 typeutil.CompareURLsWithoutScheme(members[i].Address, node)
}) {
c.AbortWithStatusJSON(http.StatusBadRequest, "tso node does not exist in the keyspace group")
}
Expand Down

0 comments on commit d7f8baf

Please sign in to comment.