-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
373 lines (322 loc) · 8.05 KB
/
server.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
package kafkatest
import (
"encoding/binary"
"fmt"
"io"
"net"
"strconv"
"strings"
"sync"
"time"
)
// Provides an incomplete Kafka Server implementation.
// Records messages sent via the ProduceRequest and responds to MetadataRequests as the only broker.
type Server struct {
Addr net.Addr
mu sync.Mutex
wg sync.WaitGroup
closed bool
closing chan struct{}
messages []Message
errors []error
brokerMessage []byte
nodeID int32
partitionMessage []byte
}
func NewServer() (*Server, error) {
s := &Server{
closing: make(chan struct{}),
nodeID: 1,
}
l, err := net.Listen("tcp", ":0")
if err != nil {
return nil, err
}
s.Addr = l.Addr()
// Prepare static message bytes
s.prepareBrokerMsg()
s.preparePartitionMsg()
// start server
s.wg.Add(1)
go func() {
defer s.wg.Done()
s.run(l)
}()
return s, nil
}
func (s *Server) prepareBrokerMsg() {
host, port, _ := net.SplitHostPort(s.Addr.String())
s.brokerMessage = make([]byte, 0, 4+2+len(host)+4)
s.brokerMessage = writeInt32(s.brokerMessage, s.nodeID)
s.brokerMessage = writeStr(s.brokerMessage, host)
portN, _ := strconv.Atoi(port)
s.brokerMessage = writeInt32(s.brokerMessage, int32(portN))
s.brokerMessage = writeInt16(s.brokerMessage, -1)
}
func (s *Server) preparePartitionMsg() {
s.partitionMessage = make([]byte, 0, 2+4+4+4+4)
// Write error code
s.partitionMessage = writeInt16(s.partitionMessage, 0)
// Write partition ID
s.partitionMessage = writeInt32(s.partitionMessage, 1)
// Write leader ID
s.partitionMessage = writeInt32(s.partitionMessage, s.nodeID)
// Write 0 len replicas
s.partitionMessage = writeArrayHeader(s.partitionMessage, 0)
// Write 0 len Isr
s.partitionMessage = writeArrayHeader(s.partitionMessage, 0)
}
func (s *Server) Close() {
s.mu.Lock()
defer s.mu.Unlock()
if s.closed {
return
}
s.closed = true
close(s.closing)
s.wg.Wait()
}
func (s *Server) Messages() ([]Message, error) {
if len(s.errors) > 0 {
return nil, multiError(s.errors)
}
return s.messages, nil
}
func (s *Server) run(l net.Listener) {
defer l.Close()
accepts := make(chan net.Conn)
s.wg.Add(1)
go func() {
defer s.wg.Done()
for {
c, err := l.Accept()
if err != nil {
return
}
select {
case accepts <- c:
case <-s.closing:
return
}
}
}()
for {
select {
case c := <-accepts:
s.wg.Add(1)
go func() {
defer s.wg.Done()
defer c.Close()
for {
if err := s.handle(c); err != nil {
if err == io.EOF {
return
}
s.errors = append(s.errors, err)
}
}
}()
case <-s.closing:
l.Close()
return
}
}
}
func (s *Server) handle(c net.Conn) error {
var size int32
err := binary.Read(c, binary.BigEndian, &size)
if err != nil {
return err
}
buf := make([]byte, int(size))
io.ReadFull(c, buf)
if err != nil {
return err
}
// ApiKey indicated the type of request
apiKey := int16(binary.BigEndian.Uint16(buf[:2]))
_, n := readStr(buf[8:])
request := buf[8+n:]
// Prepare response
response := make([]byte, 8, 1024)
// Leave first 4 bytes for the size.
// Copy correlationID.
copy(response[4:], buf[4:8])
switch apiKey {
case 0: // ProduceRequest
topic, partition, offset := s.readProduceRequest(request)
// Prepare success response
response = writeArrayHeader(response, 1)
response = writeStr(response, topic)
response = writeArrayHeader(response, 1)
response = writeInt32(response, partition)
response = writeInt16(response, 0) // Error Code
response = writeInt64(response, offset)
response = writeInt32(response, 0) // ThrottleTime
case 3: // Metadata
topics, _ := readStrList(request)
// Write broker message
response = writeArray(response, [][]byte{s.brokerMessage})
// Write controller id
response = writeInt32(response, 0)
// Write topic metadata
response = writeArrayHeader(response, int32(len(topics)))
for _, t := range topics {
// Write Error Code
response = writeInt16(response, 0)
// Write topic name
response = writeStr(response, t)
// Write is_internal
response = writeBool(response, false)
// Write partition
response = writeArray(response, [][]byte{s.partitionMessage})
}
case 18:
// Hard code the api versions we are implementing to ensure
// the client knows which ones we plan on implementing.
response = writeInt16(response, 0) // Error Code
// Write api versions
response = writeArrayHeader(response, 2)
// Write produce request version
response = writeInt16(response, 0)
response = writeInt16(response, 2)
response = writeInt16(response, 2)
// Write metadata request version
response = writeInt16(response, 3)
response = writeInt16(response, 1)
response = writeInt16(response, 1)
default:
return fmt.Errorf("unsupported apiKey %d", apiKey)
}
// Set response size
responseSize := len(response) - 4
binary.BigEndian.PutUint32(response[:4], uint32(responseSize))
_, err = c.Write(response)
return err
}
// readProduceRequest, assume only a single message exists
func (s *Server) readProduceRequest(request []byte) (topic string, partition int32, offset int64) {
pos := 2 + 4 + 4 // skip RequiredAcks and Timeout and array len
// Read topic name
topic, n := readStr(request[pos:])
pos += n
pos += 4 // skip array len
partition = readInt32(request[pos:])
pos += 4
pos += 4 // skip set size
offset = readInt64(request[pos:])
pos += 8
pos += 4 + 4 + 1 + 1 // skip size, crc, magic, attributes
msecs := readInt64(request[pos:])
pos += 8
key, n := readByteArray(request[pos:])
pos += n
message, n := readByteArray(request[pos:])
pos += n
s.saveMessage(Message{
Topic: topic,
Partition: partition,
Offset: offset,
Key: string(key),
Message: string(message),
Time: time.Unix(msecs/1000, msecs%1000*1000000).UTC(),
})
return
}
func (s *Server) saveMessage(m Message) {
s.mu.Lock()
defer s.mu.Unlock()
s.messages = append(s.messages, m)
}
func readList(buf []byte, f func([]byte) int) int {
pos := 4
count := int(int32(binary.BigEndian.Uint32(buf[:pos])))
for i := 0; i < count; i++ {
pos += f(buf[pos:])
}
return pos
}
func readStrList(buf []byte) ([]string, int) {
var strs []string
l := readList(buf, func(data []byte) int {
s, n := readStr(data)
strs = append(strs, s)
return n
})
return strs, l
}
func readStr(buf []byte) (string, int) {
n := int(int16(binary.BigEndian.Uint16(buf[:2])))
return string(buf[2 : 2+n]), n + 2
}
func readByteArray(buf []byte) ([]byte, int) {
n := int(int32(binary.BigEndian.Uint32(buf[:4])))
return buf[4 : 4+n], n + 4
}
func readInt16(buf []byte) int16 {
return int16(binary.BigEndian.Uint16(buf[:2]))
}
func readInt32(buf []byte) int32 {
return int32(binary.BigEndian.Uint32(buf[:4]))
}
func readInt64(buf []byte) int64 {
return int64(binary.BigEndian.Uint64(buf[:8]))
}
func writeStr(dst []byte, s string) []byte {
dst = writeInt16(dst, len(s))
return append(dst, []byte(s)...)
}
func writeInt16(dst []byte, n int) []byte {
l := len(dst)
dst = append(dst, []byte{0, 0}...)
binary.BigEndian.PutUint16(dst[l:l+2], uint16(n))
return dst
}
func writeBool(dst []byte, b bool) []byte {
v := int8(0)
if b {
v = 1
}
return writeInt8(dst, v)
}
func writeInt8(dst []byte, n int8) []byte {
return append(dst, byte(n))
}
func writeInt32(dst []byte, n int32) []byte {
l := len(dst)
dst = append(dst, []byte{0, 0, 0, 0}...)
binary.BigEndian.PutUint32(dst[l:l+4], uint32(n))
return dst
}
func writeInt64(dst []byte, n int64) []byte {
l := len(dst)
dst = append(dst, []byte{0, 0, 0, 0, 0, 0, 0, 0}...)
binary.BigEndian.PutUint64(dst[l:l+8], uint64(n))
return dst
}
func writeArrayHeader(dst []byte, n int32) []byte {
return writeInt32(dst, n)
}
func writeArray(dst []byte, data [][]byte) []byte {
dst = writeArrayHeader(dst, int32(len(data)))
for _, d := range data {
dst = append(dst, d...)
}
return dst
}
type multiError []error
func (e multiError) Error() string {
errs := make([]string, len(e))
for i := range e {
errs[i] = e[i].Error()
}
return strings.Join(errs, "\n")
}
type Message struct {
Topic string
Partition int32
Offset int64
Key string
Message string
Time time.Time
}