forked from twmb/franz-go
/
01_fetch.go
216 lines (198 loc) · 4.77 KB
/
01_fetch.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
package kfake
import (
"sync"
"time"
"github.com/burningass23/franz-go/pkg/kerr"
"github.com/burningass23/franz-go/pkg/kmsg"
)
// Behavior:
//
// * If topic does not exist, we hang
// * Topic created while waiting is not returned in final response
// * If any partition is on a different broker, we return immediately
// * Out of range fetch causes early return
// * Raw bytes of batch counts against wait bytes
func init() { regKey(1, 4, 13) }
func (c *Cluster) handleFetch(creq clientReq, w *watchFetch) (kmsg.Response, error) {
var (
req = creq.kreq.(*kmsg.FetchRequest)
resp = req.ResponseKind().(*kmsg.FetchResponse)
)
if err := checkReqVersion(req.Key(), req.Version); err != nil {
return nil, err
}
var (
nbytes int
returnEarly bool
needp tps[int]
)
if w == nil {
out:
for i, rt := range req.Topics {
if req.Version >= 13 {
rt.Topic = c.data.id2t[rt.TopicID]
req.Topics[i].Topic = rt.Topic
}
t, ok := c.data.tps.gett(rt.Topic)
if !ok {
continue
}
for _, rp := range rt.Partitions {
pd, ok := t[rp.Partition]
if !ok || pd.createdAt.After(creq.at) {
continue
}
if pd.leader != creq.cc.b {
returnEarly = true // NotLeaderForPartition
break out
}
i, ok, atEnd := pd.searchOffset(rp.FetchOffset)
if atEnd {
continue
}
if !ok {
returnEarly = true // OffsetOutOfRange
break out
}
pbytes := 0
for _, b := range pd.batches[i:] {
nbytes += b.nbytes
pbytes += b.nbytes
if pbytes >= int(rp.PartitionMaxBytes) {
returnEarly = true
break out
}
}
needp.set(rt.Topic, rp.Partition, int(rp.PartitionMaxBytes)-pbytes)
}
}
}
wait := time.Duration(req.MaxWaitMillis) * time.Millisecond
deadline := creq.at.Add(wait)
if w == nil && !returnEarly && nbytes < int(req.MinBytes) && time.Now().Before(deadline) {
w := &watchFetch{
need: int(req.MinBytes) - nbytes,
needp: needp,
deadline: deadline,
creq: creq,
}
w.cb = func() {
select {
case c.watchFetchCh <- w:
case <-c.die:
}
}
for _, rt := range req.Topics {
t, ok := c.data.tps.gett(rt.Topic)
if !ok {
continue
}
for _, rp := range rt.Partitions {
pd, ok := t[rp.Partition]
if !ok || pd.createdAt.After(creq.at) {
continue
}
pd.watch[w] = struct{}{}
w.in = append(w.in, pd)
}
}
w.t = time.AfterFunc(wait, w.cb)
return nil, nil
}
id2t := make(map[uuid]string)
tidx := make(map[string]int)
donet := func(t string, id uuid, errCode int16) *kmsg.FetchResponseTopic {
if i, ok := tidx[t]; ok {
return &resp.Topics[i]
}
id2t[id] = t
tidx[t] = len(resp.Topics)
st := kmsg.NewFetchResponseTopic()
st.Topic = t
st.TopicID = id
resp.Topics = append(resp.Topics, st)
return &resp.Topics[len(resp.Topics)-1]
}
donep := func(t string, id uuid, p int32, errCode int16) *kmsg.FetchResponseTopicPartition {
sp := kmsg.NewFetchResponseTopicPartition()
sp.Partition = p
sp.ErrorCode = errCode
st := donet(t, id, 0)
st.Partitions = append(st.Partitions, sp)
return &st.Partitions[len(st.Partitions)-1]
}
var batchesAdded int
full:
for _, rt := range req.Topics {
for _, rp := range rt.Partitions {
pd, ok := c.data.tps.getp(rt.Topic, rp.Partition)
if !ok {
if req.Version >= 13 {
donep(rt.Topic, rt.TopicID, rp.Partition, kerr.UnknownTopicID.Code)
} else {
donep(rt.Topic, rt.TopicID, rp.Partition, kerr.UnknownTopicOrPartition.Code)
}
continue
}
if pd.leader != creq.cc.b {
donep(rt.Topic, rt.TopicID, rp.Partition, kerr.NotLeaderForPartition.Code)
continue
}
sp := donep(rt.Topic, rt.TopicID, rp.Partition, 0)
sp.HighWatermark = pd.highWatermark
sp.LastStableOffset = pd.lastStableOffset
sp.LogStartOffset = pd.logStartOffset
i, ok, atEnd := pd.searchOffset(rp.FetchOffset)
if atEnd {
continue
}
if !ok {
sp.ErrorCode = kerr.OffsetOutOfRange.Code
continue
}
var pbytes int
for _, b := range pd.batches[i:] {
if nbytes = nbytes + b.nbytes; nbytes > int(req.MaxBytes) && batchesAdded > 1 {
break full
}
if pbytes = pbytes + b.nbytes; pbytes > int(rp.PartitionMaxBytes) && batchesAdded > 1 {
break
}
batchesAdded++
sp.RecordBatches = b.AppendTo(sp.RecordBatches)
}
}
}
return resp, nil
}
type watchFetch struct {
need int
needp tps[int]
deadline time.Time
creq clientReq
in []*partData
cb func()
t *time.Timer
once sync.Once
cleaned bool
}
func (w *watchFetch) push(nbytes int) {
w.need -= nbytes
if w.need <= 0 {
w.once.Do(func() {
go w.cb()
})
}
}
func (w *watchFetch) deleted() {
w.once.Do(func() {
go w.cb()
})
}
func (w *watchFetch) cleanup(c *Cluster) {
w.cleaned = true
for _, in := range w.in {
delete(in.watch, w)
}
w.t.Stop()
}