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 | class ConsumerSettings(BaseModel):
"""Data.Rentgen consumer-specific settings.
These options are passed directly to
`AIOKafkaConsumer <https://aiokafka.readthedocs.io/en/stable/api.html#aiokafka.AIOKafkaConsumer>`_.
Examples
--------
.. code-block:: bash
DATA_RENTGEN__CONSUMER__TOPICS_LIST=["input.runs"]
DATA_RENTGEN__CONSUMER__MALFOMED_TOPIC="input.runs:malformed"
DATA_RENTGEN__CONSUMER__GROUP_ID=data-rentgen
DATA_RENTGEN__CONSUMER__FETCH_MAX_WAIT_MS=5000
DATA_RENTGEN__CONSUMER__MAX_PARTITION_FETCH_BYTES=5MiB
"""
topics_list: list[str] = Field(
default=["input.runs"],
description="List of Kafka topics to subscribe. Mutually exclusive with :obj:`~topics_pattern`.",
)
topics_pattern: str | None = Field(
default=None,
description="Regex pattern of topics to subscribe. Mutually exclusive with :obj:`~topics_list`.",
)
@model_validator(mode="after")
def _check_topics(self):
if not self.topics_list and not self.topics_pattern:
msg = "input should contain either 'topics_list' or 'topics_pattern' field, both are empty"
raise ValueError(msg)
if self.topics_list and self.topics_pattern:
msg = "input should contain either 'topics_list' or 'topics_pattern' field, both are set"
raise ValueError(msg)
return self
group_id: str | None = Field(
default="data-rentgen",
description=textwrap.dedent(
"""
Name of the consumer group to join for dynamic partition assignment (if enabled),
and to use for fetching and committing offsets.
If ``None``, auto-partition assignment (via group coordinator) and offset commits are disabled.
""",
),
)
# Defaults are copied from FastStream: https://github.com/airtai/faststream/blob/0.5.33/faststream/kafka/fastapi/fastapi.py#L618
# But only options, related to consumer
max_records: int | None = Field(
default=None,
description=textwrap.dedent(
"""
Number of messages to consume as one batch.
``None`` means no limit applied.
""",
),
)
fetch_max_bytes: ByteSize = Field(
default=ByteSize(50 * 1024 * 1024),
description="The maximum amount of data the server should return for a fetch request.",
)
fetch_min_bytes: ByteSize = Field(
default=ByteSize(1),
description=textwrap.dedent(
"""
Minimum amount of data the server should
return for a fetch request, otherwise wait up to
:obj:`~fetch_max_wait_ms` for more data to accumulate.
""",
),
)
fetch_max_wait_ms: int = Field(
default=500,
description=textwrap.dedent(
"""
The maximum amount of time in milliseconds
the server will block before answering the fetch request if
there isn't sufficient data to immediately satisfy the
requirement given by :obj:`~fetch_min_bytes`.
""",
),
)
max_partition_fetch_bytes: ByteSize = Field(
default=ByteSize(1024 * 1024),
description=textwrap.dedent(
"""
The maximum amount of data
per-partition the server will return. The maximum total memory
used for a request ``= #partitions * max_partition_fetch_bytes``.
This size must be at least as large as the maximum message size
the server allows or else it is possible for the producer to
send messages larger than the consumer can fetch. If that
happens, the consumer can get stuck trying to fetch a large
message on a certain partition.
""",
),
)
batch_timeout_ms: int = Field(
default=200,
description=textwrap.dedent(
"""
Milliseconds spent waiting if data is not available in the buffer.
If 0, returns immediately with any records that are available currently in the buffer,
else returns empty.
""",
),
)
auto_offset_reset: Literal["latest", "earliest", "none"] = Field(
default="latest",
description=textwrap.dedent(
""""
A policy for resetting offsets on ``OffsetOutOfRangeError`` errors:
* ``earliest`` will move to the oldest available message
* ``latest`` will move to the most recent
* ``none`` will raise an exception so you can handle this case
""",
),
)
max_poll_interval_ms: int = Field(
default=5 * 60 * 1000,
description=textwrap.dedent(
"""
Maximum allowed time between calls to consume messages in batches.
If this interval is exceeded the consumer is considered failed and the group will
rebalance in order to reassign the partitions to another consumer
group member.
If API methods block waiting for messages, that time
does not count against this timeout.
""",
),
)
session_timeout_ms: int = Field(
default=10 * 1000,
description=textwrap.dedent(
"""
Client group session and failure detection
timeout. The consumer sends periodic heartbeats
(``heartbeat.interval.ms``) to indicate its liveness to the broker.
If no hearts are received by the broker for a group member within
the session timeout, the broker will remove the consumer from the
group and trigger a rebalance.
The allowed range is configured with the **broker** configuration properties
``group.min.session.timeout.ms`` and ``group.max.session.timeout.ms``.
""",
),
)
heartbeat_interval_ms: int = Field(
default=3 * 1000,
description=textwrap.dedent(
"""
The expected time in milliseconds
between heartbeats to the consumer coordinator when using
Kafka's group management feature. Heartbeats are used to ensure
that the consumer's session stays active and to facilitate
rebalancing when new consumers join or leave the group.
The value must be set lower than :obj:`~session_timeout_ms`, but typically
should be set no higher than 1/3 of that value. It can be
adjusted even lower to control the expected time for normal
rebalances.
""",
),
)
consumer_timeout_ms: int = Field(
default=200,
description=textwrap.dedent(
"""
Maximum wait timeout for background fetching routine.
Mostly defines how fast the system will see rebalance and
request new data for new partitions.
""",
),
)
isolation_level: Literal["read_uncommitted", "read_committed"] = Field(
default="read_uncommitted",
description=textwrap.dedent(
"""
Controls how to read messages written
transactionally.
* ``read_committed`` - batch consumer will only return
transactional messages which have been committed.
* ``read_uncommitted`` (the default) - batch consumer will
return all messages, even transactional messages which have been
aborted.
Non-transactional messages will be returned unconditionally in
either mode.
Messages will always be returned in offset order. Hence, in
``read_committed`` mode, batch consumer will only return
messages up to the last stable offset (LSO), which is the one less
than the offset of the first open transaction. In particular any
messages appearing after messages belonging to ongoing transactions
will be withheld until the relevant transaction has been completed.
As a result, ``read_committed`` consumers will not be able to read up
to the high watermark when there are in flight transactions.
Further, when in ``read_committed`` the seek_to_end method will
return the LSO.
""",
),
)
|