add subset algorithm

master
kevin 4 years ago
parent 63583d4744
commit f02eeef0f6

@ -119,7 +119,7 @@ func TestConsistent_next(t *testing.T) {
m[val]++ m[val]++
} }
entropy := mathx.CalcEntropy(m, total) entropy := mathx.CalcEntropy(m)
assert.Equal(t, size, len(m)) assert.Equal(t, size, len(m))
assert.True(t, entropy > .95) assert.True(t, entropy > .95)

@ -237,7 +237,7 @@ func TestRoundRobin_initialize(t *testing.T) {
for k, v := range m { for k, v := range m {
mi[k] = v mi[k] = v
} }
entropy := mathx.CalcEntropy(mi, total) entropy := mathx.CalcEntropy(mi)
assert.True(t, entropy > .95) assert.True(t, entropy > .95)
} }
@ -265,7 +265,7 @@ func TestRoundRobin_next(t *testing.T) {
m[val]++ m[val]++
} }
entropy := mathx.CalcEntropy(m, total) entropy := mathx.CalcEntropy(m)
assert.Equal(t, size, len(m)) assert.Equal(t, size, len(m))
assert.True(t, entropy > .95) assert.True(t, entropy > .95)

@ -47,7 +47,7 @@ func TestConsistentHash(t *testing.T) {
for k, v := range keys { for k, v := range keys {
mi[k] = v mi[k] = v
} }
entropy := mathx.CalcEntropy(mi, requestSize) entropy := mathx.CalcEntropy(mi)
assert.True(t, entropy > .95) assert.True(t, entropy > .95)
} }

@ -2,9 +2,14 @@ package mathx
import "math" import "math"
func CalcEntropy(m map[interface{}]int, total int) float64 { func CalcEntropy(m map[interface{}]int) float64 {
var entropy float64 var entropy float64
var total int
for _, v := range m {
total += v
}
for _, v := range m { for _, v := range m {
proba := float64(v) / float64(total) proba := float64(v) / float64(total)
entropy -= proba * math.Log2(proba) entropy -= proba * math.Log2(proba)

@ -13,5 +13,5 @@ func TestCalcEntropy(t *testing.T) {
for i := 0; i < total; i++ { for i := 0; i < total; i++ {
m[i] = count m[i] = count
} }
assert.True(t, CalcEntropy(m, total*count) > .99) assert.True(t, CalcEntropy(m) > .99)
} }

@ -65,7 +65,7 @@ func TestUnstable_Distribution(t *testing.T) {
for k, v := range m { for k, v := range m {
mi[k] = v mi[k] = v
} }
entropy := CalcEntropy(mi, total) entropy := CalcEntropy(mi)
assert.True(t, len(m) > 1) assert.True(t, len(m) > 1)
assert.True(t, entropy > 0.95) assert.True(t, entropy > 0.95)
} }

@ -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
}

@ -12,6 +12,7 @@ import (
const ( const (
DiscovScheme = "discov" DiscovScheme = "discov"
EndpointSep = "," EndpointSep = ","
subsetSize = 36
) )
var builder discovBuilder var builder discovBuilder
@ -36,7 +37,7 @@ func (b *discovBuilder) Build(target resolver.Target, cc resolver.ClientConn, op
update := func() { update := func() {
var addrs []resolver.Address var addrs []resolver.Address
for _, val := range sub.Values() { for _, val := range subset(sub.Values(), subsetSize) {
addrs = append(addrs, resolver.Address{ addrs = append(addrs, resolver.Address{
Addr: val, Addr: val,
}) })

@ -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…
Cancel
Save