-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
249 lines (226 loc) · 6.25 KB
/
main.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package main
import (
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"net/http"
"os"
"os/signal"
"strconv"
"sync"
"time"
"github.com/twmb/franz-go/pkg/kgo"
"github.com/twmb/franz-go/pkg/kmsg"
)
func main() {
var broker, listenAddr, group string
flag.StringVar(&broker, "b", "localhost:65363", "Kafka broker")
flag.StringVar(&listenAddr, "l", "localhost:8080", "HTTP listen port")
flag.StringVar(&group, "g", "go-kafka-state", "The Kafka ConsumerGroup")
flag.Parse()
// Connect to the Redpanda broker and consume the user topic
seeds := []string{broker}
cl, err := kgo.NewClient(
kgo.SeedBrokers(seeds...),
kgo.ConsumeTopics("user"),
kgo.ConsumerGroup(group),
kgo.Balancers(NewRangeBalancer(listenAddr)),
kgo.AdjustFetchOffsetsFn(func(ctx context.Context, m map[string]map[int32]kgo.Offset) (map[string]map[int32]kgo.Offset, error) {
for k, v := range m {
for i := range v {
m[k][i] = kgo.NewOffset().At(-2).WithEpoch(-1)
}
}
return m, nil
}),
kgo.OnPartitionsAssigned(func(ctx context.Context, c *kgo.Client, m map[string][]int32) {
fmt.Println("OnPartitionsAssigned")
}),
)
if err != nil {
panic(err)
}
defer cl.Close()
users := NewUserStore()
// Start serving HTTP requests
httpShutdown := serveHttp(listenAddr, users, cl, group)
// Run our consume loop in a separate Go routine
ctx := context.Background()
go consume(ctx, cl, users)
// Shutdown gracefully
sigs := make(chan os.Signal, 2)
signal.Notify(sigs, os.Interrupt)
<-sigs
fmt.Println("received interrupt signal; closing client")
done := make(chan struct{})
go func() {
defer close(done)
cl.Close()
ctx, cancel := context.WithTimeout(ctx, time.Second*2)
defer cancel()
httpShutdown(ctx)
}()
select {
case <-sigs:
fmt.Println("received second interrupt signal; quitting without waiting for graceful close")
case <-done:
}
}
func consume(ctx context.Context, cl *kgo.Client, users *UserStore) {
for {
fetches := cl.PollFetches(ctx)
fetches.EachPartition(func(p kgo.FetchTopicPartition) {
for _, record := range p.Records {
fmt.Printf("%s (p=%d): %s\n", string(record.Key), record.Partition, string(record.Value))
// Handle tombstones and continue with next record
if len(record.Value) == 0 {
fmt.Printf("got tombstone for: %s\n", string(record.Key))
users.Delete(string(record.Key))
continue
}
// Update state
u := User{}
err := json.Unmarshal(record.Value, &u)
if err != nil {
panic(err)
}
users.Set(string(record.Key), u)
}
})
}
}
func fetchGroup(ctx context.Context, group string, kClient *kgo.Client) map[string]string {
members := map[string]string{}
resp := kClient.RequestSharded(ctx, &kmsg.DescribeGroupsRequest{Groups: []string{group}})
for _, m := range resp[0].Resp.(*kmsg.DescribeGroupsResponse).Groups[0].Members {
metadata := kmsg.ConsumerMemberMetadata{}
metadata.ReadFrom(m.ProtocolMetadata)
assign := kmsg.ConsumerMemberAssignment{}
assign.ReadFrom(m.MemberAssignment)
for _, owned := range assign.Topics {
for _, p := range owned.Partitions {
members[owned.Topic+"-"+strconv.Itoa(int(p))] = string(metadata.UserData)
}
}
}
return members
}
func serveHttp(addr string, users *UserStore, kClient *kgo.Client, group string) func(ctx context.Context) error {
partitioner := kgo.StickyKeyPartitioner(nil)
mux := http.NewServeMux()
mux.HandleFunc("/groupinfo", func(w http.ResponseWriter, r *http.Request) {
m := fetchGroup(r.Context(), group, kClient)
b, _ := json.Marshal(m)
w.Write(b)
return
})
mux.HandleFunc("/user", func(w http.ResponseWriter, r *http.Request) {
email := r.URL.Query().Get("email")
fmt.Printf("http: %s /user?email=%s\n", r.Method, email)
switch r.Method {
case http.MethodGet:
table := fetchGroup(r.Context(), group, kClient)
p := partitioner.ForTopic("user").Partition(&kgo.Record{Key: []byte(email)}, len(table))
if a, ok := table["user-"+strconv.Itoa(p)]; ok {
// if this instance is not assigned the partition we forward the request
if a != addr {
resp, err := http.Get("http://" + a + r.URL.String())
if err != nil {
fmt.Printf("failed to chain call to instance %s: %s", a, err)
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
fmt.Printf("chain call: url=%s\n", resp.Request.URL)
io.Copy(w, resp.Body)
return
}
} else {
http.Error(w, "partition not found in routing table", http.StatusInternalServerError)
return
}
u, ok := users.Get(email)
if !ok {
http.NotFound(w, r)
return
}
b, err := json.Marshal(u)
if err != nil {
panic(err)
}
_, err = w.Write(b)
if err != nil {
panic(err)
}
return
case http.MethodPut:
u := User{}
b, err := io.ReadAll(r.Body)
if err != nil {
panic(err)
}
if err := json.Unmarshal(b, &u); err != nil {
panic(err)
}
if email == "" {
email = u.Email
}
v, err := json.Marshal(u)
if err != nil {
panic(err)
}
res := kClient.ProduceSync(r.Context(), &kgo.Record{Key: []byte(email), Value: v, Topic: "user"})
if err := res.FirstErr(); err != nil {
http.Error(w, "failed to update user", http.StatusInternalServerError)
}
w.WriteHeader(http.StatusOK)
return
case http.MethodDelete:
res := kClient.ProduceSync(r.Context(), &kgo.Record{Key: []byte(email), Value: []byte{}, Topic: "user"})
if err := res.FirstErr(); err != nil {
http.Error(w, "failed to update user", http.StatusInternalServerError)
}
w.WriteHeader(http.StatusOK)
return
}
})
s := http.Server{
Addr: addr,
Handler: mux,
}
go func() {
if err := s.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
panic(err)
}
}()
return s.Shutdown
}
type User struct {
Name string `json:"name"`
Email string `json:"email"`
}
type UserStore struct {
l sync.RWMutex
u map[string]User
}
func NewUserStore() *UserStore {
return &UserStore{u: map[string]User{}}
}
func (u *UserStore) Get(email string) (User, bool) {
u.l.RLock()
defer u.l.RUnlock()
user, ok := u.u[email]
return user, ok
}
func (u *UserStore) Set(email string, user User) {
u.l.Lock()
defer u.l.Unlock()
u.u[email] = user
}
func (u *UserStore) Delete(email string) {
u.l.Lock()
defer u.l.Unlock()
delete(u.u, email)
}