-
Notifications
You must be signed in to change notification settings - Fork 721
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: add balancer for keyspace group #6274
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
c85cbb0
mcs: add balancer for keyspace group
lhy1024 2d186a4
address comments
lhy1024 84b3a1c
make test stable
lhy1024 56dec99
Merge branch 'master' of http://github.com/tikv/pd into balancer2
lhy1024 1abd1b5
Merge branch 'master' of http://github.com/tikv/pd into balancer2
lhy1024 1329cc9
Merge branch 'master' of http://github.com/tikv/pd into balancer2
lhy1024 ec3785e
fix test
lhy1024 5a7833f
fix test
lhy1024 dd71090
use api mode
lhy1024 87ab40a
address comments
lhy1024 1f4c117
Merge branch 'master' of http://github.com/tikv/pd into balancer2
lhy1024 33438c3
refactor test
lhy1024 60752e1
fix lint
lhy1024 21972f0
address comments
lhy1024 cf2ff20
address comments
lhy1024 162770a
Merge branch 'master' into balancer2
ti-chi-bot 4702df3
Merge branch 'master' into balancer2
ti-chi-bot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Copyright 2023 TiKV Project Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package balancer | ||
|
||
// Policy is the policy of balancer. | ||
type Policy int | ||
|
||
const ( | ||
// PolicyRoundRobin is the round robin policy. | ||
PolicyRoundRobin Policy = iota | ||
// PolicyLeast is the policy to return the least used node. | ||
// TODO: move indexed heap to pkg and use it. | ||
PolicyLeast | ||
) | ||
|
||
func (p Policy) String() string { | ||
switch p { | ||
case PolicyRoundRobin: | ||
return "round-robin" | ||
case PolicyLeast: | ||
return "least" | ||
default: | ||
return "unknown" | ||
} | ||
} | ||
|
||
// Balancer is the interface for balancer. | ||
type Balancer[T uint32 | string] interface { | ||
// Next returns next one. | ||
Next() T | ||
// Put puts one into balancer. | ||
Put(T) | ||
// Delete deletes one from balancer. | ||
Delete(T) | ||
// GetAll returns all nodes. | ||
GetAll() []T | ||
// Len returns the length of nodes. | ||
Len() int | ||
} | ||
|
||
// GenByPolicy generates a balancer by policy. | ||
func GenByPolicy[T uint32 | string](policy Policy) Balancer[T] { | ||
switch policy { | ||
case PolicyRoundRobin: | ||
return NewRoundRobin[T]() | ||
default: // only round-robin is supported now. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not return round robin directly? |
||
return NewRoundRobin[T]() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
// Copyright 2023 TiKV Project Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package balancer | ||
|
||
import ( | ||
"math/rand" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestBalancerPutAndDelete(t *testing.T) { | ||
t.Parallel() | ||
re := require.New(t) | ||
balancers := []Balancer[uint32]{ | ||
NewRoundRobin[uint32](), | ||
} | ||
for _, balancer := range balancers { | ||
re.Equal(uint32(0), balancer.Next()) | ||
// test put | ||
exists := make(map[uint32]struct{}) | ||
for i := 0; i < 100; i++ { | ||
num := rand.Uint32() | ||
balancer.Put(num) | ||
exists[num] = struct{}{} | ||
re.Equal(len(balancer.GetAll()), len(exists)) | ||
t := balancer.Next() | ||
re.Contains(exists, t) | ||
} | ||
// test delete | ||
for num := range exists { | ||
balancer.Delete(num) | ||
delete(exists, num) | ||
re.Equal(len(balancer.GetAll()), len(exists)) | ||
if len(exists) == 0 { | ||
break | ||
} | ||
t := balancer.Next() | ||
re.NotEqual(t, num) | ||
re.Contains(exists, t) | ||
} | ||
re.Equal(uint32(0), balancer.Next()) | ||
} | ||
} | ||
|
||
func TestBalancerDuplicate(t *testing.T) { | ||
t.Parallel() | ||
re := require.New(t) | ||
balancers := []Balancer[uint32]{ | ||
NewRoundRobin[uint32](), | ||
} | ||
for _, balancer := range balancers { | ||
re.Len(balancer.GetAll(), 0) | ||
// test duplicate put | ||
balancer.Put(1) | ||
re.Len(balancer.GetAll(), 1) | ||
balancer.Put(1) | ||
re.Len(balancer.GetAll(), 1) | ||
// test duplicate delete | ||
balancer.Delete(1) | ||
re.Len(balancer.GetAll(), 0) | ||
balancer.Delete(1) | ||
re.Len(balancer.GetAll(), 0) | ||
} | ||
} | ||
|
||
func TestRoundRobin(t *testing.T) { | ||
t.Parallel() | ||
re := require.New(t) | ||
balancer := NewRoundRobin[uint32]() | ||
for i := 0; i < 100; i++ { | ||
num := rand.Uint32() | ||
balancer.Put(num) | ||
} | ||
statistics := make(map[uint32]int) | ||
for i := 0; i < 1000; i++ { | ||
statistics[balancer.Next()]++ | ||
} | ||
min := 1000 | ||
max := 0 | ||
for _, v := range statistics { | ||
if v < min { | ||
min = v | ||
} | ||
if v > max { | ||
max = v | ||
} | ||
} | ||
re.LessOrEqual(max-min, 10) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
// Copyright 2023 TiKV Project Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package balancer | ||
|
||
import ( | ||
"sync" | ||
"sync/atomic" | ||
) | ||
|
||
// RoundRobin is a balancer that selects nodes in a round-robin fashion. | ||
type RoundRobin[T uint32 | string] struct { | ||
sync.RWMutex | ||
nodes []T | ||
bufferflies marked this conversation as resolved.
Show resolved
Hide resolved
|
||
exists map[T]struct{} | ||
next uint32 | ||
} | ||
|
||
// NewRoundRobin creates a balancer that selects nodes in a round-robin fashion. | ||
func NewRoundRobin[T uint32 | string]() *RoundRobin[T] { | ||
return &RoundRobin[T]{ | ||
nodes: make([]T, 0), | ||
exists: make(map[T]struct{}), | ||
} | ||
} | ||
|
||
// Next returns next address | ||
func (r *RoundRobin[T]) Next() (t T) { | ||
r.RLock() | ||
defer r.RUnlock() | ||
if len(r.nodes) == 0 { | ||
return | ||
} | ||
next := atomic.AddUint32(&r.next, 1) | ||
node := r.nodes[(int(next)-1)%len(r.nodes)] | ||
return node | ||
} | ||
|
||
// GetAll returns all nodes | ||
func (r *RoundRobin[T]) GetAll() []T { | ||
r.RLock() | ||
defer r.RUnlock() | ||
return r.nodes | ||
} | ||
|
||
// Put puts one into balancer. | ||
func (r *RoundRobin[T]) Put(node T) { | ||
r.Lock() | ||
defer r.Unlock() | ||
if _, ok := r.exists[node]; !ok { | ||
r.nodes = append(r.nodes, node) | ||
r.exists[node] = struct{}{} | ||
} | ||
} | ||
|
||
// Delete deletes one from balancer. | ||
func (r *RoundRobin[T]) Delete(node T) { | ||
r.Lock() | ||
defer r.Unlock() | ||
if _, ok := r.exists[node]; ok { | ||
for i, n := range r.nodes { | ||
if n == node { | ||
r.nodes = append(r.nodes[:i], r.nodes[i+1:]...) | ||
delete(r.exists, node) | ||
break | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Len returns the length of nodes. | ||
func (r *RoundRobin[T]) Len() int { | ||
r.RLock() | ||
defer r.RUnlock() | ||
return len(r.nodes) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
where is it used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it will do at anotherr pr
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not add it in another pr? maybe you need replace it by adding todo?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1 to remove PolicyLeast if we don't use it
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't worry. It will be added after #6268 is merged.