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: tso server compare address without scheme #8283

Merged
merged 3 commits into from
Jun 13, 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 @@ -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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not directly use EqualBaseURLs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid to add comments anywhere, so wrap it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Avoid to add comments anywhere, so wrap it.

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