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

Cache picker only in taggingBalancer #3

Merged
merged 2 commits into from
Nov 2, 2023
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
9 changes: 0 additions & 9 deletions tagging.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,13 @@ func (b *taggingBalancer) Rebalance(change discovery.Change) {
if !change.Result.Cacheable {
return
}
if next, ok := b.next.(loadbalance.Rebalancer); ok {
next.Delete(change)
}
b.pickerCache.Store(change.Result.CacheKey, b.createPicker(change.Result))
}

func (b *taggingBalancer) Delete(change discovery.Change) {
if !change.Result.Cacheable {
return
}
if next, ok := b.next.(loadbalance.Rebalancer); ok {
next.Delete(change)
}
b.pickerCache.Delete(change.Result.CacheKey)
}

Expand All @@ -93,10 +87,7 @@ func (b *taggingBalancer) createPicker(e discovery.Result) loadbalance.Picker {

pickers := make(map[string]loadbalance.Picker, len(instances))
for t, instances := range instances {
// a projection of raw discovery.Result has same cache option
p := b.next.GetPicker(discovery.Result{
Cacheable: e.Cacheable,
CacheKey: e.CacheKey,
Instances: instances,
})
pickers[t] = p
Expand Down
43 changes: 23 additions & 20 deletions tagging_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@ import (
"github.com/cloudwego/kitex/pkg/loadbalance"
)

type mockBalancer struct {
rebalanced bool
deleted bool
}
type mockBalancer struct{}

type mockPicker struct {
result discovery.Result
Expand All @@ -39,14 +36,6 @@ func (m *mockBalancer) GetPicker(result discovery.Result) loadbalance.Picker {
return &mockPicker{result: result}
}

func (m *mockBalancer) Rebalance(change discovery.Change) {
m.rebalanced = true
}

func (m *mockBalancer) Delete(change discovery.Change) {
m.deleted = true
}

func (m *mockPicker) Next(ctx context.Context, request interface{}) discovery.Instance {
return m.result.Instances[0]
}
Expand Down Expand Up @@ -98,6 +87,26 @@ func TestTaggingBalancer_GetPicker(t *testing.T) {
},
},
},
{
cacheable: true,
cacheKey: "multi instances",
instances: []discovery.Instance{
discovery.NewInstance("tcp", "addr1", 10, map[string]string{"foo": "bar1"}),
discovery.NewInstance("tcp", "addr2", 20, map[string]string{"foo": "bar2"}),
discovery.NewInstance("tcp", "addr3", 30, map[string]string{"foo": "bar3"}),
discovery.NewInstance("tcp", "addr4", 30, map[string]string{"foo": ""}),
discovery.NewInstance("tcp", "addr5", 30, nil),
},
tagInstances: map[string][]discovery.Instance{
"bar1": {discovery.NewInstance("tcp", "addr1", 10, map[string]string{"foo": "bar1"})},
"bar2": {discovery.NewInstance("tcp", "addr2", 20, map[string]string{"foo": "bar2"})},
"bar3": {discovery.NewInstance("tcp", "addr3", 30, map[string]string{"foo": "bar3"})},
"": {
discovery.NewInstance("tcp", "addr4", 30, map[string]string{"foo": ""}),
discovery.NewInstance("tcp", "addr5", 30, nil),
},
},
},
}

lb := New("foo", func(ctx context.Context, request interface{}) string {
Expand All @@ -120,8 +129,8 @@ func TestTaggingBalancer_GetPicker(t *testing.T) {
assert.IsType(t, &mockPicker{}, p)

pp := p.(*mockPicker)
assert.Equal(t, tt.cacheable, pp.result.Cacheable)
assert.Equal(t, tt.cacheKey, pp.result.CacheKey)
assert.Zero(t, pp.result.Cacheable)
assert.Zero(t, pp.result.CacheKey)
assert.EqualValues(t, v, pp.result.Instances)
}
}
Expand Down Expand Up @@ -160,7 +169,6 @@ func TestTaggingBalancer_Rebalance(t *testing.T) {
Instances: []discovery.Instance{discovery.NewInstance("tcp", "addr2", 20, map[string]string{"foo": "bar"})},
},
})
assert.True(t, lb.(*taggingBalancer).next.(*mockBalancer).deleted)

p2 := lb.GetPicker(discovery.Result{
Cacheable: true,
Expand All @@ -173,13 +181,9 @@ func TestTaggingBalancer_Rebalance(t *testing.T) {
mp1 := p1.(*taggingPicker).tagPickers["bar"].(*mockPicker)
mp2 := p2.(*taggingPicker).tagPickers["bar"].(*mockPicker)
assert.Equal(t, mp1.result, discovery.Result{
Cacheable: true,
CacheKey: "rebalance",
Instances: []discovery.Instance{discovery.NewInstance("tcp", "addr1", 10, map[string]string{"foo": "bar"})},
})
assert.Equal(t, mp2.result, discovery.Result{
Cacheable: true,
CacheKey: "rebalance",
Instances: []discovery.Instance{discovery.NewInstance("tcp", "addr2", 20, map[string]string{"foo": "bar"})},
})
}
Expand All @@ -205,7 +209,6 @@ func TestTaggingBalancer_Delete(t *testing.T) {
CacheKey: "delete",
},
})
assert.True(t, lb.(*taggingBalancer).next.(*mockBalancer).deleted)

pp, ok = lb.(*taggingBalancer).pickerCache.Load("delete")
assert.False(t, ok)
Expand Down
Loading