This repository was archived by the owner on Apr 3, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathkafka_server.py
More file actions
416 lines (329 loc) · 11.4 KB
/
kafka_server.py
File metadata and controls
416 lines (329 loc) · 11.4 KB
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
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
# Fluent Bit
# ==========
# Copyright (C) 2015-2026 The Fluent Bit Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import socket
import struct
import threading
logger = logging.getLogger(__name__)
API_KEY_PRODUCE = 0
API_KEY_METADATA = 3
API_KEY_API_VERSIONS = 18
BROKER_NODE_ID = 1
data_storage = {
"connections": [],
"requests": [],
"messages": [],
}
server_thread = None
server_socket = None
server_port = None
server_stop_event = threading.Event()
server_ready_event = threading.Event()
def reset_kafka_server_state():
data_storage["connections"] = []
data_storage["requests"] = []
data_storage["messages"] = []
server_stop_event.clear()
server_ready_event.clear()
def _recv_exact(sock, size):
chunks = []
remaining = size
while remaining > 0:
chunk = sock.recv(remaining)
if not chunk:
return None
chunks.append(chunk)
remaining -= len(chunk)
return b"".join(chunks)
def _read_int16(data, offset):
return struct.unpack_from(">h", data, offset)[0], offset + 2
def _read_int32(data, offset):
return struct.unpack_from(">i", data, offset)[0], offset + 4
def _read_int64(data, offset):
return struct.unpack_from(">q", data, offset)[0], offset + 8
def _read_string(data, offset):
length, offset = _read_int16(data, offset)
if length < 0:
return None, offset
end = offset + length
return data[offset:end].decode("utf-8", errors="replace"), end
def _read_bytes(data, offset):
length, offset = _read_int32(data, offset)
if length < 0:
return None, offset
end = offset + length
return data[offset:end], end
def _read_array_length(data, offset):
return _read_int32(data, offset)
def _parse_request_header(payload):
api_key, offset = _read_int16(payload, 0)
api_version, offset = _read_int16(payload, offset)
correlation_id, offset = _read_int32(payload, offset)
client_id, offset = _read_string(payload, offset)
return {
"api_key": api_key,
"api_version": api_version,
"correlation_id": correlation_id,
"client_id": client_id,
"body": payload[offset:],
}
def _encode_string(value):
encoded = value.encode("utf-8")
return struct.pack(">h", len(encoded)) + encoded
def _encode_response(correlation_id, body):
frame = struct.pack(">i", correlation_id) + body
return struct.pack(">i", len(frame)) + frame
def _encode_metadata_response(topics, host, port):
body = []
body.append(struct.pack(">i", 1))
body.append(struct.pack(">i", BROKER_NODE_ID))
body.append(_encode_string(host))
body.append(struct.pack(">i", port))
body.append(struct.pack(">i", len(topics)))
for topic in topics:
body.append(struct.pack(">h", 0))
body.append(_encode_string(topic))
body.append(struct.pack(">i", 1))
body.append(struct.pack(">h", 0))
body.append(struct.pack(">i", 0))
body.append(struct.pack(">i", BROKER_NODE_ID))
body.append(struct.pack(">i", 1))
body.append(struct.pack(">i", BROKER_NODE_ID))
body.append(struct.pack(">i", 1))
body.append(struct.pack(">i", BROKER_NODE_ID))
return b"".join(body)
def _encode_produce_response(topic, partition=0, base_offset=0):
return b"".join(
[
struct.pack(">i", 1),
_encode_string(topic),
struct.pack(">i", 1),
struct.pack(">i", partition),
struct.pack(">h", 0),
struct.pack(">q", base_offset),
]
)
def _encode_api_versions_response(api_version):
api_versions = [
(API_KEY_PRODUCE, 0, 0),
(API_KEY_METADATA, 0, 0),
(API_KEY_API_VERSIONS, 0, 3),
]
if api_version >= 3:
entries = []
for api_key, min_version, max_version in api_versions:
entries.append(
struct.pack(">h", api_key)
+ struct.pack(">h", min_version)
+ struct.pack(">h", max_version)
+ b"\x00"
)
return b"".join(
[
struct.pack(">h", 0),
bytes([len(api_versions) + 1]),
b"".join(entries),
struct.pack(">i", 0),
b"\x00",
]
)
body = [struct.pack(">h", 0), struct.pack(">i", len(api_versions))]
for api_key, min_version, max_version in api_versions:
body.append(struct.pack(">h", api_key))
body.append(struct.pack(">h", min_version))
body.append(struct.pack(">h", max_version))
if api_version >= 1:
body.append(struct.pack(">i", 0))
return b"".join(body)
def _parse_metadata_request(body):
topic_count, offset = _read_array_length(body, 0)
topics = []
for _ in range(topic_count):
topic, offset = _read_string(body, offset)
topics.append(topic)
return topics
def _parse_message_set(data):
messages = []
offset = 0
while offset < len(data):
if len(data) - offset < 12:
break
message_offset, offset = _read_int64(data, offset)
message_size, offset = _read_int32(data, offset)
end = offset + message_size
if end > len(data):
break
_, cursor = _read_int32(data, offset)
magic = data[cursor]
cursor += 1
attributes = data[cursor]
cursor += 1
key, cursor = _read_bytes(data, cursor)
value, cursor = _read_bytes(data, cursor)
if magic == 1 and cursor + 8 <= end:
cursor += 8
if attributes & 0x07:
raise ValueError("Compressed Kafka message sets are not supported by the fake broker")
messages.append(
{
"offset": message_offset,
"magic": magic,
"attributes": attributes,
"key": key,
"value": value,
}
)
offset = end
return messages
def _parse_produce_request(body):
required_acks, offset = _read_int16(body, 0)
timeout_ms, offset = _read_int32(body, offset)
topic_count, offset = _read_array_length(body, offset)
produced_topics = []
for _ in range(topic_count):
topic, offset = _read_string(body, offset)
partition_count, offset = _read_array_length(body, offset)
partitions = []
for _ in range(partition_count):
partition, offset = _read_int32(body, offset)
message_set, offset = _read_bytes(body, offset)
records = _parse_message_set(message_set or b"")
partitions.append(
{
"partition": partition,
"records": records,
}
)
produced_topics.append(
{
"topic": topic,
"partitions": partitions,
}
)
return {
"required_acks": required_acks,
"timeout_ms": timeout_ms,
"topics": produced_topics,
}
def _handle_request(sock, request, host, port):
data_storage["requests"].append(
{
"api_key": request["api_key"],
"api_version": request["api_version"],
"correlation_id": request["correlation_id"],
"client_id": request["client_id"],
}
)
if request["api_key"] == API_KEY_API_VERSIONS:
sock.sendall(
_encode_response(
request["correlation_id"],
_encode_api_versions_response(request["api_version"]),
)
)
return
if request["api_key"] == API_KEY_METADATA:
topics = _parse_metadata_request(request["body"])
sock.sendall(
_encode_response(
request["correlation_id"],
_encode_metadata_response(topics, host, port),
)
)
return
if request["api_key"] == API_KEY_PRODUCE:
produced = _parse_produce_request(request["body"])
for topic_data in produced["topics"]:
for partition_data in topic_data["partitions"]:
for record in partition_data["records"]:
data_storage["messages"].append(
{
"topic": topic_data["topic"],
"partition": partition_data["partition"],
"key": record["key"],
"value": record["value"],
"magic": record["magic"],
"attributes": record["attributes"],
"client_id": request["client_id"],
}
)
first_topic = produced["topics"][0]["topic"] if produced["topics"] else "test"
sock.sendall(
_encode_response(
request["correlation_id"],
_encode_produce_response(first_topic),
)
)
return
logger.warning("Unsupported Kafka API key %s", request["api_key"])
def _connection_loop(client, address, host, port):
data_storage["connections"].append({"address": address})
with client:
while not server_stop_event.is_set():
header = _recv_exact(client, 4)
if not header:
return
frame_size = struct.unpack(">i", header)[0]
payload = _recv_exact(client, frame_size)
if payload is None:
return
request = _parse_request_header(payload)
_handle_request(client, request, host, port)
def _server_loop(host, port):
global server_socket, server_port
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((host, port))
sock.listen(16)
sock.settimeout(0.2)
server_socket = sock
server_port = port
server_ready_event.set()
logger.info("Starting fake Kafka broker on %s:%s", host, port)
try:
while not server_stop_event.is_set():
try:
client, address = sock.accept()
except socket.timeout:
continue
except OSError:
break
worker = threading.Thread(
target=_connection_loop,
args=(client, address, host, port),
daemon=True,
)
worker.start()
finally:
try:
sock.close()
except OSError:
pass
server_socket = None
def kafka_server_run(port, host="127.0.0.1"):
global server_thread
reset_kafka_server_state()
server_thread = threading.Thread(target=_server_loop, args=(host, port), daemon=True)
server_thread.start()
server_ready_event.wait(timeout=5)
return server_thread
def kafka_server_stop():
server_stop_event.set()
if server_socket is not None:
try:
server_socket.close()
except OSError:
pass