Skip to content

Commit

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

Signed-off-by: lhy1024 <[email protected]>

Co-authored-by: ti-chi-bot[bot] <108142056+ti-chi-bot[bot]@users.noreply.github.com>
Co-authored-by: lhy1024 <[email protected]>
  • Loading branch information
3 people committed Aug 1, 2024
1 parent ae905fa commit a20565e
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 9 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 @@ -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.Address == 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
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"`
}

// 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) IsAddressEquivalent(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
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.Address == 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].Address == 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.Address == kgm.tsoServiceID.ServiceAddr {
if m.IsAddressEquivalent(kgm.tsoServiceID.ServiceAddr) {
ids = append(ids, uint32(i))
break
}
Expand Down
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 @@ -520,7 +520,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].IsAddressEquivalent(node)
}) {
c.AbortWithStatusJSON(http.StatusBadRequest, "tso node does not exist in the keyspace group")
}
Expand Down

0 comments on commit a20565e

Please sign in to comment.