forked from segmentio/kafka-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_groupbalancer_test.go
121 lines (111 loc) · 3.11 KB
/
example_groupbalancer_test.go
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package kafka
import (
"context"
"encoding/json"
"io/ioutil"
"net/http"
"os"
"strings"
"time"
)
// ExampleNewReader_rackAffinity shows how the RackAffinityGroupBalancer can be
// used to pair up consumers with brokers in the same AWS availability zone.
// This code assumes that each brokers' rack is configured to be the name of the
// AZ in which it is running.
func ExampleNewReader_rackAffinity() {
r := NewReader(ReaderConfig{
Brokers: []string{"kafka:9092"},
GroupID: "my-group",
Topic: "my-topic",
GroupBalancers: []GroupBalancer{
RackAffinityGroupBalancer{Rack: findRack()},
RangeGroupBalancer{},
},
})
r.ReadMessage(context.Background())
r.Close()
}
// findRack is the basic rack resolver strategy for use in AWS. It supports
// * ECS with the task metadata endpoint enabled (returns the container
// instance's availability zone)
// * Linux EC2 (returns the instance's availability zone)
func findRack() string {
switch whereAmI() {
case "ecs":
return ecsAvailabilityZone()
case "ec2":
return ec2AvailabilityZone()
}
return ""
}
const ecsContainerMetadataURI = "ECS_CONTAINER_METADATA_URI"
// whereAmI determines which strategy the rack resolver should use.
func whereAmI() string {
// https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task-metadata-endpoint.html
if os.Getenv(ecsContainerMetadataURI) != "" {
return "ecs"
}
// https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/identify_ec2_instances.html
for _, path := range [...]string{
"/sys/devices/virtual/dmi/id/product_uuid",
"/sys/hypervisor/uuid",
} {
b, err := ioutil.ReadFile(path)
if err != nil {
continue
}
s := string(b)
switch {
case strings.HasPrefix(s, "EC2"), strings.HasPrefix(s, "ec2"):
return "ec2"
}
}
return "somewhere"
}
// ecsAvailabilityZone queries the task endpoint for the metadata URI that ECS
// injects into the ECS_CONTAINER_METADATA_URI variable in order to retrieve
// the availability zone where the task is running.
func ecsAvailabilityZone() string {
client := http.Client{
Timeout: time.Second,
Transport: &http.Transport{
DisableCompression: true,
DisableKeepAlives: true,
},
}
r, err := client.Get(os.Getenv(ecsContainerMetadataURI) + "/task")
if err != nil {
return ""
}
defer r.Body.Close()
var md struct {
AvailabilityZone string
}
if err := json.NewDecoder(r.Body).Decode(&md); err != nil {
return ""
}
return md.AvailabilityZone
}
// ec2AvailabilityZone queries the metadata endpoint to discover the
// availability zone where this code is running. we avoid calling this function
// unless we know we're in EC2. Otherwise, in other environments, we would need
// to wait for the request to 169.254.169.254 to timeout before proceeding.
func ec2AvailabilityZone() string {
client := http.Client{
Timeout: time.Second,
Transport: &http.Transport{
DisableCompression: true,
DisableKeepAlives: true,
},
}
r, err := client.Get("http://169.254.169.254/latest/meta-data/placement/availability-zone")
if err != nil {
return ""
}
defer r.Body.Close()
b, err := ioutil.ReadAll(r.Body)
if err != nil {
return ""
}
return string(b)
}