Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mcs: fix tso server address compare #8289

Merged
merged 4 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/keyspace/tso_keyspace_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -876,7 +876,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.CompareAddress(node) {
if member.IsAddressEquivalent(node) {
inKeyspaceGroup = true
member.Priority = priority
}
Expand Down
3 changes: 1 addition & 2 deletions pkg/mcs/tso/server/grpc_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"io"
"net/http"
"strconv"
"strings"
"time"

"github.com/pingcap/errors"
Expand Down Expand Up @@ -164,7 +163,7 @@ func (s *Service) FindGroupByKeyspaceID(
Address: member.Address,
// TODO: watch the keyspace groups' primary serving address changes
// to get the latest primary serving addresses of all keyspace groups.
IsPrimary: strings.EqualFold(member.Address, am.GetLeaderAddr()),
IsPrimary: member.IsAddressEquivalent(am.GetLeaderAddr()),
})
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/storage/endpoint/tso_keyspace_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,11 @@ type KeyspaceGroupMember struct {
Priority int `json:"priority"`
}

// CompareAddress compares the address with the given address.
// IsAddressEquivalent 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 {
func (m *KeyspaceGroupMember) IsAddressEquivalent(addr string) bool {
return typeutil.EqualBaseURLs(m.Address, addr)
}

Expand Down
8 changes: 4 additions & 4 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.CompareAddress(localAddress) {
if member.IsAddressEquivalent(localAddress) {
localPriority = member.Priority
}
}
Expand Down Expand Up @@ -625,7 +625,7 @@ func (kgm *KeyspaceGroupManager) primaryPriorityCheckLoop() {
if member != nil {
aliveTSONodes := make(map[string]struct{})
kgm.tsoNodes.Range(func(key, _ any) bool {
aliveTSONodes[key.(string)] = struct{}{}
aliveTSONodes[typeutil.TrimScheme(key.(string))] = struct{}{}
return true
})
if len(aliveTSONodes) == 0 {
Expand All @@ -638,7 +638,7 @@ func (kgm *KeyspaceGroupManager) primaryPriorityCheckLoop() {
if member.Priority <= localPriority {
continue
}
if _, ok := aliveTSONodes[member.Address]; ok {
if _, ok := aliveTSONodes[typeutil.TrimScheme(member.Address)]; ok {
resetLeader = true
break
}
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].CompareAddress(kgm.tsoServiceID.ServiceAddr)
return group.Members[i].IsAddressEquivalent(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.CompareAddress(kgm.tsoServiceID.ServiceAddr) {
if m.IsAddressEquivalent(kgm.tsoServiceID.ServiceAddr) {
ids = append(ids, uint32(i))
break
}
Expand Down
10 changes: 6 additions & 4 deletions pkg/utils/typeutil/comparison.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,10 @@ func Float64Equal(a, b float64) bool {

// 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)
return TrimScheme(url1) == TrimScheme(url2)
}

// TrimScheme trims the scheme from the URL.
func TrimScheme(s string) string {
return strings.TrimPrefix(strings.TrimPrefix(s, "https://"), "http://")
}
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 @@ -515,7 +515,7 @@ func SetPriorityForKeyspaceGroup(c *gin.Context) {
// check if node exists
members := kg.Members
if slice.NoneOf(members, func(i int) bool {
return members[i].CompareAddress(node)
return members[i].IsAddressEquivalent(node)
}) {
c.AbortWithStatusJSON(http.StatusBadRequest, "tso node does not exist in the keyspace group")
}
Expand Down
Loading