add subset algorithm
parent
63583d4744
commit
f02eeef0f6
@ -1,65 +0,0 @@
|
||||
package roundrobin
|
||||
|
||||
import (
|
||||
"context"
|
||||
"math/rand"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"google.golang.org/grpc/balancer"
|
||||
"google.golang.org/grpc/balancer/base"
|
||||
"google.golang.org/grpc/resolver"
|
||||
)
|
||||
|
||||
const Name = "rr"
|
||||
|
||||
func init() {
|
||||
balancer.Register(newBuilder())
|
||||
}
|
||||
|
||||
type roundRobinPickerBuilder struct{}
|
||||
|
||||
func newBuilder() balancer.Builder {
|
||||
return base.NewBalancerBuilder(Name, new(roundRobinPickerBuilder))
|
||||
}
|
||||
|
||||
func (b *roundRobinPickerBuilder) Build(readySCs map[resolver.Address]balancer.SubConn) balancer.Picker {
|
||||
if len(readySCs) == 0 {
|
||||
return base.NewErrPicker(balancer.ErrNoSubConnAvailable)
|
||||
}
|
||||
|
||||
rand.Seed(time.Now().UnixNano())
|
||||
picker := &roundRobinPicker{
|
||||
index: rand.Int(),
|
||||
}
|
||||
|
||||
for addr, conn := range readySCs {
|
||||
picker.conns = append(picker.conns, &subConn{
|
||||
addr: addr,
|
||||
conn: conn,
|
||||
})
|
||||
}
|
||||
|
||||
return picker
|
||||
}
|
||||
|
||||
type roundRobinPicker struct {
|
||||
conns []*subConn
|
||||
index int
|
||||
lock sync.Mutex
|
||||
}
|
||||
|
||||
func (p *roundRobinPicker) Pick(ctx context.Context, info balancer.PickInfo) (
|
||||
conn balancer.SubConn, done func(balancer.DoneInfo), err error) {
|
||||
p.lock.Lock()
|
||||
defer p.lock.Unlock()
|
||||
|
||||
p.index = (p.index + 1) % len(p.conns)
|
||||
return p.conns[p.index].conn, func(info balancer.DoneInfo) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
type subConn struct {
|
||||
addr resolver.Address
|
||||
conn balancer.SubConn
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package resolver
|
||||
|
||||
import "math/rand"
|
||||
|
||||
func subset(set []string, sub int) []string {
|
||||
rand.Shuffle(len(set), func(i, j int) {
|
||||
set[i], set[j] = set[j], set[i]
|
||||
})
|
||||
if len(set) <= sub {
|
||||
return set
|
||||
} else {
|
||||
return set[:sub]
|
||||
}
|
||||
}
|
@ -0,0 +1,65 @@
|
||||
package resolver
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"zero/core/mathx"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestSubset(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
set int
|
||||
sub int
|
||||
}{
|
||||
{
|
||||
name: "more vals to subset",
|
||||
set: 100,
|
||||
sub: 36,
|
||||
},
|
||||
{
|
||||
name: "less vals to subset",
|
||||
set: 100,
|
||||
sub: 200,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range tests {
|
||||
t.Run(test.name, func(t *testing.T) {
|
||||
var vals []string
|
||||
for i := 0; i < test.set; i++ {
|
||||
vals = append(vals, strconv.Itoa(i))
|
||||
}
|
||||
|
||||
m := make(map[interface{}]int)
|
||||
for i := 0; i < 1000; i++ {
|
||||
set := subset(append([]string(nil), vals...), test.sub)
|
||||
for _, val := range set {
|
||||
m[val]++
|
||||
}
|
||||
}
|
||||
|
||||
assert.True(t, mathx.CalcEntropy(m) > 0.95)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSubsetLess(t *testing.T) {
|
||||
var vals []string
|
||||
for i := 0; i < 100; i++ {
|
||||
vals = append(vals, strconv.Itoa(i))
|
||||
}
|
||||
|
||||
m := make(map[interface{}]int)
|
||||
for i := 0; i < 1000; i++ {
|
||||
set := subset(append([]string(nil), vals...), 200)
|
||||
for _, val := range set {
|
||||
m[val]++
|
||||
}
|
||||
}
|
||||
|
||||
assert.True(t, mathx.CalcEntropy(m) > 0.95)
|
||||
}
|
Loading…
Reference in New Issue