This repository has been archived by the owner on Jan 10, 2024. It is now read-only.
forked from zendesk/ruby-kafka
-
Notifications
You must be signed in to change notification settings - Fork 1
/
offset_manager_spec.rb
296 lines (236 loc) · 8.23 KB
/
offset_manager_spec.rb
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
# frozen_string_literal: true
require 'timecop'
describe Kafka::OffsetManager do
let(:cluster) { double(:cluster) }
let(:group) { double(:group) }
let(:fetcher) { double(:fetcher) }
let(:logger) { LOGGER }
let(:offset_manager) {
Kafka::OffsetManager.new(
cluster: cluster,
group: group,
fetcher: fetcher,
logger: logger,
commit_interval: commit_interval,
commit_threshold: 0,
offset_retention_time: offset_retention_time
)
}
let(:offset_retention_time) { nil }
let(:commit_interval) { 0 }
let(:partition_assignments) { { 'greetings' => [0, 1, 2] } }
before do
allow(group).to receive(:commit_offsets)
allow(group).to receive(:assigned_to?) do |topic, partition|
(partition_assignments[topic] || []).include?(partition)
end
allow(fetcher).to receive(:seek)
end
describe "#commit_offsets" do
it "commits the processed offsets to the group" do
offset_manager.mark_as_processed("greetings", 0, 42)
offset_manager.mark_as_processed("greetings", 1, 13)
offset_manager.commit_offsets
expected_offsets = {
"greetings" => {
0 => 43,
1 => 14,
}
}
expect(group).to have_received(:commit_offsets).with(expected_offsets)
end
context "after calling #mark_as_processed with offsets from non-assigned partitions" do
it "only commits offsets from assigned partitions" do
offset_manager.mark_as_processed("greetings", 0, 42)
offset_manager.mark_as_processed("greetings", 1, 13)
offset_manager.mark_as_processed("greetings", 5, 75)
offset_manager.mark_as_processed("seasons-greetings", 3, 15)
offset_manager.commit_offsets
expected_offsets = {
"greetings" => {
0 => 43,
1 => 14,
}
}
expect(group).to have_received(:commit_offsets).with(expected_offsets)
end
end
context "after marking offsets as processed for the same partition but out of order" do
it "committs the newest offset" do
offset_manager.mark_as_processed("greetings", 0, 42)
offset_manager.mark_as_processed("greetings", 1, 579)
offset_manager.mark_as_processed("greetings", 0, 5)
offset_manager.mark_as_processed("greetings", 1, 95)
offset_manager.commit_offsets
expected_offsets = {
"greetings" => {
0 => 43,
1 => 580
}
}
expect(group).to have_received(:commit_offsets).with(expected_offsets)
end
end
end
describe "#commit_offsets_if_necessary" do
let(:fetched_offsets_response) do
Kafka::Protocol::OffsetFetchResponse.new(topics: {
"greetings" => {
0 => partition_offset_info(-1),
1 => partition_offset_info(24),
2 => partition_offset_info(4)
}
})
end
before do
allow(group).to receive(:fetch_offsets).and_return(fetched_offsets_response)
end
context "at the first commit" do
it "re-commits previously committed offsets" do
fetch_committed_offsets
offset_manager.mark_as_processed("greetings", 1, 25)
offset_manager.commit_offsets_if_necessary
expected_offsets = {
"greetings" => {
1 => 26,
2 => 4
}
}
expect(group).to have_received(:commit_offsets).with(expected_offsets)
end
end
context "commit intervals" do
let(:commit_interval) { 10 }
let(:offset_retention_time) { 300 }
let(:commits) { [] }
before do
allow(group).to receive(:commit_offsets) do |offsets|
commits << offsets
end
Timecop.freeze(Time.now)
# initial commit
fetch_committed_offsets
offset_manager.mark_as_processed("greetings", 0, 0)
offset_manager.commit_offsets_if_necessary
expect(commits.size).to eq(1)
end
after do
Timecop.return
end
context "before the commit timeout" do
before do
Timecop.travel(commit_interval - 1)
end
it "does not commit processed offsets to the group" do
expect do
offset_manager.mark_as_processed("greetings", 0, 1)
offset_manager.commit_offsets_if_necessary
end.not_to change(commits, :size)
end
end
context "after the commit timeout" do
before do
Timecop.travel(commit_interval + 1)
end
it "commits processed offsets without recommitting previously committed offsets" do
expect do
offset_manager.mark_as_processed("greetings", 0, 1)
offset_manager.commit_offsets_if_necessary
end.to change(commits, :size).by(1)
expected_offsets = {
"greetings" => { 0 => 2 }
}
expect(commits.last).to eq(expected_offsets)
end
end
context "after the recommit timeout" do
before do
Timecop.travel(offset_retention_time / 2 + 1)
end
it "commits processed offsets and recommits previously committed offsets" do
expect do
offset_manager.mark_as_processed("greetings", 0, 1)
offset_manager.commit_offsets_if_necessary
end.to change(commits, :size).by(1)
expected_offsets = {
"greetings" => {
0 => 2,
1 => 24,
2 => 4
}
}
expect(commits.last).to eq(expected_offsets)
end
end
end
def fetch_committed_offsets
offset_manager.next_offset_for("greetings", 1)
end
def partition_offset_info(offset)
Kafka::Protocol::OffsetFetchResponse::PartitionOffsetInfo.new(offset: offset, metadata: nil, error_code: 0)
end
end
describe "#next_offset_for" do
let(:fetched_offsets) { double(:fetched_offsets) }
before do
allow(group).to receive(:fetch_offsets).and_return(fetched_offsets)
end
it "returns the last committed offset" do
allow(fetched_offsets).to receive(:offset_for).with("greetings", 0) { 41 }
offset = offset_manager.next_offset_for("greetings", 0)
expect(offset).to eq 41
end
it "returns the default offset if none have been committed" do
allow(group).to receive(:assigned_partitions) { { "greetings" => [0] } }
allow(fetched_offsets).to receive(:offset_for).with("greetings", 0) { -1 }
allow(cluster).to receive(:resolve_offsets).with("greetings", [0], :latest) { { 0 => 42 } }
offset_manager.set_default_offset("greetings", :latest)
offset = offset_manager.next_offset_for("greetings", 0)
expect(offset).to eq 42
end
end
describe "#clear_offsets_excluding" do
let(:partition_assignments) { { 'x' => [0, 1] } }
it "clears offsets except for the partitions in the exclusion list" do
offset_manager.mark_as_processed("x", 0, 42)
offset_manager.mark_as_processed("x", 1, 13)
offset_manager.clear_offsets_excluding("x" => [0])
offset_manager.commit_offsets
expected_offsets = {
"x" => {
0 => 43,
}
}
expect(group).to have_received(:commit_offsets).with(expected_offsets)
end
end
describe "#seek_to" do
it "seeks to given topic-partition/offset" do
topic = "greetings"
partition = 0
seek_offset = 14
offset_manager.seek_to(topic, partition, seek_offset)
offset = offset_manager.next_offset_for(topic, partition)
expect(offset).to eq seek_offset
end
it "propagates the seek to the fetcher" do
topic = "greetings"
partition = 0
offset = 14
offset_manager.seek_to(topic, partition, offset)
expect(fetcher).to have_received(:seek).with(topic, partition, offset)
end
end
describe "#seek_to_default" do
it "seeks default offset" do
topic = "greetings"
partition = 0
offset_manager.set_default_offset(topic, :latest)
allow(group).to receive(:assigned_partitions) { { topic => [0] } }
allow(cluster).to receive(:resolve_offsets).with(topic, [0], :latest) { { 0 => 42 } }
offset_manager.seek_to_default(topic, partition)
offset = offset_manager.next_offset_for(topic, partition)
expect(offset).to eq 42
end
end
end