forked from optiopay/kafka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_test.go
274 lines (247 loc) · 6.9 KB
/
server_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
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
package kafka
import (
"bytes"
"fmt"
"net"
"strconv"
"sync"
"time"
"github.com/optiopay/kafka/proto"
)
const (
AnyRequest = -1
)
type Serializable interface {
Bytes() ([]byte, error)
}
type RequestHandler func(request Serializable) (response Serializable)
type Server struct {
Processed int
mu sync.RWMutex
ln net.Listener
clients map[int64]net.Conn
handlers map[int16]RequestHandler
}
func NewServer() *Server {
srv := &Server{
clients: make(map[int64]net.Conn),
handlers: make(map[int16]RequestHandler),
}
srv.handlers[AnyRequest] = srv.defaultRequestHandler
return srv
}
// Handle registers handler for given message kind. Handler registered with
// AnyRequest kind will be used only if there is no precise handler for the
// kind.
func (srv *Server) Handle(reqKind int16, handler RequestHandler) {
srv.mu.Lock()
srv.handlers[reqKind] = handler
srv.mu.Unlock()
}
func (srv *Server) Address() string {
return srv.ln.Addr().String()
}
func (srv *Server) HostPort() (string, int) {
host, sport, err := net.SplitHostPort(srv.ln.Addr().String())
if err != nil {
panic(fmt.Sprintf("cannot split server address: %s", err))
}
port, err := strconv.Atoi(sport)
if err != nil {
panic(fmt.Sprintf("port '%s' is not a number: %s", sport, err))
}
if host == "" {
host = "localhost"
}
return host, port
}
func (srv *Server) Start() {
srv.mu.Lock()
defer srv.mu.Unlock()
if srv.ln != nil {
panic("server already started")
}
ln, err := net.Listen("tcp4", "")
if err != nil {
panic(fmt.Sprintf("cannot start server: %s", err))
}
srv.ln = ln
go func() {
for {
client, err := ln.Accept()
if err != nil {
return
}
go srv.handleClient(client)
}
}()
}
func (srv *Server) Close() {
srv.mu.Lock()
_ = srv.ln.Close()
for _, cli := range srv.clients {
_ = cli.Close()
}
srv.clients = make(map[int64]net.Conn)
srv.mu.Unlock()
}
func (srv *Server) handleClient(c net.Conn) {
clientID := time.Now().UnixNano()
srv.mu.Lock()
srv.clients[clientID] = c
srv.mu.Unlock()
defer func() {
srv.mu.Lock()
delete(srv.clients, clientID)
srv.mu.Unlock()
}()
for {
kind, b, err := proto.ReadReq(c)
if err != nil {
return
}
srv.mu.RLock()
fn, ok := srv.handlers[kind]
if !ok {
fn, ok = srv.handlers[AnyRequest]
}
srv.mu.RUnlock()
if !ok {
panic(fmt.Sprintf("no handler for %d", kind))
}
var request Serializable
switch kind {
case proto.FetchReqKind:
request, err = proto.ReadFetchReq(bytes.NewBuffer(b))
case proto.ProduceReqKind:
request, err = proto.ReadProduceReq(bytes.NewBuffer(b))
case proto.OffsetReqKind:
request, err = proto.ReadOffsetReq(bytes.NewBuffer(b))
case proto.MetadataReqKind:
request, err = proto.ReadMetadataReq(bytes.NewBuffer(b))
case proto.ConsumerMetadataReqKind:
request, err = proto.ReadConsumerMetadataReq(bytes.NewBuffer(b))
case proto.OffsetCommitReqKind:
request, err = proto.ReadOffsetCommitReq(bytes.NewBuffer(b))
case proto.OffsetFetchReqKind:
request, err = proto.ReadOffsetFetchReq(bytes.NewBuffer(b))
case proto.APIVersionsReqKind:
request, err = proto.ReadAPIVersionsReq(bytes.NewBuffer(b))
}
if err != nil {
panic(fmt.Sprintf("could not read message %d: %s", kind, err))
}
response := fn(request)
if response != nil {
b, err := response.Bytes()
if err != nil {
panic(fmt.Sprintf("cannot serialize %T: %s", response, err))
}
if _, err := c.Write(b); err != nil {
panic(fmt.Sprintf("cannot wirte to client: %s", err))
}
}
}
}
func (srv *Server) defaultRequestHandler(request Serializable) Serializable {
srv.mu.RLock()
defer srv.mu.RUnlock()
srv.Processed++
switch req := request.(type) {
case *proto.FetchReq:
resp := &proto.FetchResp{
CorrelationID: req.GetCorrelationID(),
Topics: make([]proto.FetchRespTopic, len(req.Topics)),
}
for ti, topic := range req.Topics {
resp.Topics[ti] = proto.FetchRespTopic{
Name: topic.Name,
Partitions: make([]proto.FetchRespPartition, len(topic.Partitions)),
}
for pi, part := range topic.Partitions {
resp.Topics[ti].Partitions[pi] = proto.FetchRespPartition{
ID: part.ID,
Err: proto.ErrUnknownTopicOrPartition,
TipOffset: -1,
Messages: []*proto.Message{},
}
}
}
return resp
case *proto.ProduceReq:
resp := &proto.ProduceResp{
CorrelationID: req.GetCorrelationID(),
}
resp.Topics = make([]proto.ProduceRespTopic, len(req.Topics))
for ti, topic := range req.Topics {
resp.Topics[ti] = proto.ProduceRespTopic{
Name: topic.Name,
Partitions: make([]proto.ProduceRespPartition, len(topic.Partitions)),
}
for pi, part := range topic.Partitions {
resp.Topics[ti].Partitions[pi] = proto.ProduceRespPartition{
ID: part.ID,
Err: proto.ErrUnknownTopicOrPartition,
Offset: -1,
}
}
}
return resp
case *proto.OffsetReq:
topics := make([]proto.OffsetRespTopic, len(req.Topics))
for ti := range req.Topics {
var topic = &topics[ti]
topic.Name = req.Topics[ti].Name
topic.Partitions = make([]proto.OffsetRespPartition, len(req.Topics[ti].Partitions))
for pi := range topic.Partitions {
var part = &topic.Partitions[pi]
part.ID = req.Topics[ti].Partitions[pi].ID
part.Err = proto.ErrUnknownTopicOrPartition
}
}
return &proto.OffsetResp{
CorrelationID: req.GetCorrelationID(),
Topics: topics,
}
case *proto.MetadataReq:
host, sport, err := net.SplitHostPort(srv.ln.Addr().String())
if err != nil {
panic(fmt.Sprintf("cannot split server address: %s", err))
}
port, err := strconv.Atoi(sport)
if err != nil {
panic(fmt.Sprintf("port '%s' is not a number: %s", sport, err))
}
if host == "" {
host = "localhost"
}
return &proto.MetadataResp{
CorrelationID: req.GetCorrelationID(),
Brokers: []proto.MetadataRespBroker{
{NodeID: 1, Host: host, Port: int32(port)},
},
Topics: []proto.MetadataRespTopic{},
}
case *proto.ConsumerMetadataReq:
panic("not implemented")
case *proto.OffsetCommitReq:
panic("not implemented")
case *proto.OffsetFetchReq:
panic("not implemented")
case *proto.APIVersionsReq:
return &proto.APIVersionsResp{
CorrelationID: req.GetCorrelationID(),
APIVersions: []proto.SupportedVersion{
proto.SupportedVersion{APIKey: proto.ProduceReqKind, MinVersion: 0, MaxVersion: 0},
proto.SupportedVersion{APIKey: proto.FetchReqKind, MinVersion: 0, MaxVersion: 0},
proto.SupportedVersion{APIKey: proto.OffsetReqKind, MinVersion: 0, MaxVersion: 0},
proto.SupportedVersion{APIKey: proto.MetadataReqKind, MinVersion: 0, MaxVersion: 0},
proto.SupportedVersion{APIKey: proto.OffsetCommitReqKind, MinVersion: 0, MaxVersion: 0},
proto.SupportedVersion{APIKey: proto.OffsetFetchReqKind, MinVersion: 0, MaxVersion: 0},
proto.SupportedVersion{APIKey: proto.ConsumerMetadataReqKind, MinVersion: 0, MaxVersion: 0},
},
}
default:
panic(fmt.Sprintf("unknown message type: %T", req))
}
}