forked from IBM/sarama
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload_aware_sticky.go
More file actions
56 lines (49 loc) · 2.01 KB
/
Copy pathload_aware_sticky.go
File metadata and controls
56 lines (49 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package main
import (
"encoding/json"
"github.com/IBM/sarama"
)
// LoadSample is the per-cycle load observation that a member reports to the
// group leader as part of its JoinGroup subscription metadata. The schema is
// versioned so the leader can reject samples it does not understand.
type LoadSample struct {
Version int `json:"v"`
CPUPercent float64 `json:"cpu"`
InFlight int `json:"in_flight"`
LagMillis int64 `json:"lag_ms"`
}
// LoadObserver returns a fresh sample of the local member's current load.
// Implementations should be cheap to call: it runs on every JoinGroup cycle.
type LoadObserver func() LoadSample
// LoadAwareSticky wraps the built-in sticky balance strategy and reports a
// fresh LoadSample to the group leader on every JoinGroup. It implements
// sarama.SubscriptionUserDataProvider; assignment logic is delegated unchanged
// to NewBalanceStrategySticky, which keeps this example focused on the
// per-cycle metadata hook rather than on a custom assignor.
//
// A real load-aware assignor would also implement sarama.BalanceStrategy.Plan
// itself, decode each member's UserData on the leader, and weight the
// assignment by the reported load.
type LoadAwareSticky struct {
sarama.BalanceStrategy
observe LoadObserver
}
// NewLoadAwareSticky returns a load-aware wrapper around the sticky strategy.
// The observe callback is invoked once per JoinGroup; its return value is
// JSON-serialized into the member's subscription UserData.
func NewLoadAwareSticky(observe LoadObserver) *LoadAwareSticky {
return &LoadAwareSticky{
BalanceStrategy: sarama.NewBalanceStrategySticky(),
observe: observe,
}
}
// SubscriptionUserData satisfies sarama.SubscriptionUserDataProvider. The
// topics argument is the member's currently subscribed topic set, supplied by
// sarama immediately before the JoinGroup is sent.
func (s *LoadAwareSticky) SubscriptionUserData(_ []string) ([]byte, error) {
sample := s.observe()
if sample.Version == 0 {
sample.Version = 1
}
return json.Marshal(sample)
}