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
417
418
419
420
421
422
423
424
425
426
427
428
429
//! Partition offsets: list, delete records, end offset per leader epoch.
use std::collections::{BTreeMap, HashMap};
use crate::consumer::TopicPartition;
use crate::error::{ErrorCode, KrafkaError, ProtocolErrorKind, Result};
use crate::protocol::{
ApiKey, DeleteRecordsPartition, DeleteRecordsRequest, DeleteRecordsResponse,
DeleteRecordsTopic, ListOffsetsRequest, ListOffsetsRequestPartition, ListOffsetsRequestTopic,
ListOffsetsResponse, OffsetForLeaderEpochPartition, OffsetForLeaderEpochRequest,
OffsetForLeaderEpochResponse, OffsetForLeaderEpochTopic, versions,
};
use super::driver::{Mode, Target, answer, exchange, negotiate};
use super::{AdminClient, validate_topics};
/// Which offset [`AdminClient::list_offsets`] looks up.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum OffsetSpec {
/// The earliest offset (log start).
Earliest,
/// The end offset (high watermark, or last stable offset with
/// `read_committed`).
Latest,
/// The first offset whose timestamp is at or after these milliseconds
/// since the Unix epoch.
Timestamp(i64),
/// The offset of the record with the largest timestamp (KIP-734,
/// `ListOffsets` v7+).
MaxTimestamp,
/// The earliest offset still in local storage (KIP-405, `ListOffsets`
/// v8+).
EarliestLocal,
/// The last offset copied to remote storage (KIP-1005, `ListOffsets` v9+).
LatestTiered,
/// The earliest offset not yet copied to remote storage (KIP-1023,
/// `ListOffsets` v11+).
EarliestPendingUpload,
}
impl OffsetSpec {
/// The wire `timestamp` field for this spec.
fn as_timestamp(self) -> i64 {
match self {
OffsetSpec::Earliest => -2,
OffsetSpec::Latest => -1,
OffsetSpec::MaxTimestamp => -3,
OffsetSpec::EarliestLocal => -4,
OffsetSpec::LatestTiered => -5,
OffsetSpec::EarliestPendingUpload => -6,
OffsetSpec::Timestamp(ts) => ts,
}
}
/// Lowest `ListOffsets` version that understands this spec. A broker
/// below it would read the negative sentinel as a timestamp and answer
/// with the log start, so such a partition fails instead.
fn min_api_version(self) -> i16 {
match self {
OffsetSpec::Earliest | OffsetSpec::Latest | OffsetSpec::Timestamp(_) => {
versions::LIST_OFFSETS_MIN
}
OffsetSpec::MaxTimestamp => 7,
OffsetSpec::EarliestLocal => 8,
OffsetSpec::LatestTiered => 9,
OffsetSpec::EarliestPendingUpload => 11,
}
}
}
/// An offset from [`AdminClient::list_offsets`].
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ListedOffset {
/// The offset.
pub offset: i64,
/// The timestamp of the record at `offset`, when the spec has one.
pub timestamp: Option<i64>,
/// The leader epoch of the record at `offset`, when known.
pub leader_epoch: Option<i32>,
}
/// The end of a leader epoch, from [`AdminClient::offset_for_leader_epoch`].
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct EpochEndOffset {
/// The epoch the end offset belongs to: the requested one, or the largest
/// epoch below it when the log was truncated.
pub leader_epoch: i32,
/// The end offset of that epoch.
pub end_offset: i64,
}
admin_options! {
/// Options for [`AdminClient::list_offsets`].
ListOffsetsOptions {
/// Report `Latest` as the last stable offset, below any open
/// transaction.
read_committed: bool,
}
}
admin_options! {
/// Options for [`AdminClient::delete_records`].
DeleteRecordsOptions {}
}
admin_options! {
/// Options for [`AdminClient::offset_for_leader_epoch`].
OffsetForLeaderEpochOptions {}
}
/// Group partition keys by topic, keeping each key's value.
fn by_topic<V: Copy>(
keys: &[TopicPartition],
value: impl Fn(&TopicPartition) -> V,
) -> BTreeMap<String, Vec<(i32, V)>> {
let mut topics: BTreeMap<String, Vec<(i32, V)>> = BTreeMap::new();
for tp in keys {
topics
.entry(tp.topic.clone())
.or_default()
.push((tp.partition, value(tp)));
}
topics
}
impl AdminClient {
/// Look up offsets per partition at each partition's leader.
///
/// Returns a result per partition: one partition with no reachable leader
/// does not fail the others.
///
/// ```rust,no_run
/// # use krafka::admin::{AdminClient, ListOffsetsOptions, OffsetSpec, TopicPartition};
/// # async fn example(admin: &AdminClient) -> Result<(), krafka::error::KrafkaError> {
/// let ends = admin
/// .list_offsets(
/// (0..3).map(|p| (TopicPartition::new("orders", p), OffsetSpec::Latest)),
/// ListOffsetsOptions::default(),
/// )
/// .await?;
/// # let _ = ends;
/// # Ok(())
/// # }
/// ```
///
/// # Errors
///
/// The call fails for a closed client or an invalid topic name.
pub async fn list_offsets(
&self,
specs: impl IntoIterator<Item = (TopicPartition, OffsetSpec)>,
options: ListOffsetsOptions,
) -> Result<HashMap<TopicPartition, Result<ListedOffset>>> {
let specs: HashMap<TopicPartition, OffsetSpec> = specs.into_iter().collect();
validate_topics(specs.keys().map(|tp| tp.topic.as_str()))?;
let call = self.call("ListOffsets", Mode::Read, options.timeout)?;
let isolation_level = i8::from(options.read_committed);
let specs_ref = &specs;
Ok(call
.fan_out(
specs.keys().cloned().collect(),
|tp| Target::Leader(tp.clone()),
|conn, partitions| async move {
let version = negotiate(
&conn,
ApiKey::ListOffsets,
versions::LIST_OFFSETS_MIN,
versions::LIST_OFFSETS_MAX,
)?;
let (supported, unsupported): (Vec<TopicPartition>, Vec<TopicPartition>) =
partitions
.into_iter()
.partition(|tp| version >= specs_ref[tp].min_api_version());
let mut results: Vec<(TopicPartition, Result<ListedOffset>)> = unsupported
.into_iter()
.map(|tp| {
let error = KrafkaError::protocol_kind(
ProtocolErrorKind::UnknownApiVersion,
format!(
"{:?} needs ListOffsets v{}, the broker negotiated v{version}",
specs_ref[&tp],
specs_ref[&tp].min_api_version()
),
);
(tp, Err(error))
})
.collect();
if supported.is_empty() {
return Ok(results);
}
let metadata = &self.metadata;
let request = ListOffsetsRequest {
replica_id: -1,
isolation_level,
topics: by_topic(&supported, |tp| {
(
specs_ref[tp].as_timestamp(),
metadata.leader_epoch(&tp.topic, tp.partition).unwrap_or(-1),
)
})
.into_iter()
.map(|(name, partitions)| ListOffsetsRequestTopic {
name,
partitions: partitions
.into_iter()
.map(|(partition_index, (timestamp, current_leader_epoch))| {
ListOffsetsRequestPartition {
partition_index,
current_leader_epoch,
timestamp,
}
})
.collect(),
})
.collect(),
timeout_ms: None,
};
let response: ListOffsetsResponse =
exchange(&conn, ApiKey::ListOffsets, version, &request).await?;
for topic in response.topics {
for p in topic.partitions {
let result = answer(p.error_code, None).and_then(|()| {
if p.offset < 0 {
Err(KrafkaError::broker(
ErrorCode::OffsetNotAvailable,
"the broker reported no offset for this spec",
))
} else {
Ok(ListedOffset {
offset: p.offset,
timestamp: (p.timestamp >= 0).then_some(p.timestamp),
leader_epoch: (p.leader_epoch >= 0)
.then_some(p.leader_epoch),
})
}
});
results.push((
TopicPartition::new(topic.name.clone(), p.partition_index),
result,
));
}
}
Ok(results)
},
)
.await)
}
/// Delete the records below the given offsets, at each partition's leader.
///
/// Returns the new log start offset (low watermark) per partition.
///
/// # Errors
///
/// The call fails for a closed client or an invalid topic name.
pub async fn delete_records(
&self,
before: impl IntoIterator<Item = (TopicPartition, i64)>,
options: DeleteRecordsOptions,
) -> Result<HashMap<TopicPartition, Result<i64>>> {
let before: HashMap<TopicPartition, i64> = before.into_iter().collect();
validate_topics(before.keys().map(|tp| tp.topic.as_str()))?;
let call = self.call("DeleteRecords", Mode::Write, options.timeout)?;
let before_ref = &before;
let call_ref = &call;
Ok(call
.fan_out(
before.keys().cloned().collect(),
|tp| Target::Leader(tp.clone()),
|conn, partitions| async move {
let request = DeleteRecordsRequest {
topics: by_topic(&partitions, |tp| before_ref[tp])
.into_iter()
.map(|(name, partitions)| DeleteRecordsTopic {
name,
partitions: partitions
.into_iter()
.map(|(partition_index, offset)| DeleteRecordsPartition {
partition_index,
offset,
})
.collect(),
})
.collect(),
timeout_ms: call_ref.remaining_ms(),
};
let version = negotiate(
&conn,
ApiKey::DeleteRecords,
versions::DELETE_RECORDS_MIN,
versions::DELETE_RECORDS_MAX,
)?;
let response: DeleteRecordsResponse =
exchange(&conn, ApiKey::DeleteRecords, version, &request).await?;
Ok(response
.topics
.into_iter()
.flat_map(|t| {
let name = t.name;
t.partitions.into_iter().map(move |p| {
(
TopicPartition::new(name.clone(), p.partition_index),
answer(p.error_code, None).map(|()| p.low_watermark),
)
})
})
.collect())
},
)
.await)
}
/// Find where each given leader epoch ends, at each partition's leader —
/// how a consumer detects log truncation after a leader change.
///
/// # Errors
///
/// The call fails for a closed client or an invalid topic name.
pub async fn offset_for_leader_epoch(
&self,
epochs: impl IntoIterator<Item = (TopicPartition, i32)>,
options: OffsetForLeaderEpochOptions,
) -> Result<HashMap<TopicPartition, Result<EpochEndOffset>>> {
let epochs: HashMap<TopicPartition, i32> = epochs.into_iter().collect();
validate_topics(epochs.keys().map(|tp| tp.topic.as_str()))?;
let call = self.call("OffsetForLeaderEpoch", Mode::Read, options.timeout)?;
let epochs_ref = &epochs;
Ok(call
.fan_out(
epochs.keys().cloned().collect(),
|tp| Target::Leader(tp.clone()),
|conn, partitions| async move {
let request = OffsetForLeaderEpochRequest {
replica_id: -1,
topics: by_topic(&partitions, |tp| epochs_ref[tp])
.into_iter()
.map(|(topic, partitions)| OffsetForLeaderEpochTopic {
topic,
partitions: partitions
.into_iter()
.map(|(partition, leader_epoch)| {
OffsetForLeaderEpochPartition {
partition,
current_leader_epoch: -1,
leader_epoch,
}
})
.collect(),
})
.collect(),
};
let version = negotiate(
&conn,
ApiKey::OffsetForLeaderEpoch,
versions::OFFSET_FOR_LEADER_EPOCH_MIN,
versions::OFFSET_FOR_LEADER_EPOCH_MAX,
)?;
let response: OffsetForLeaderEpochResponse =
exchange(&conn, ApiKey::OffsetForLeaderEpoch, version, &request).await?;
Ok(response
.topics
.into_iter()
.flat_map(|t| {
let name = t.topic;
t.partitions.into_iter().map(move |p| {
(
TopicPartition::new(name.clone(), p.partition),
answer(p.error_code, None).map(|()| EpochEndOffset {
leader_epoch: p.leader_epoch,
end_offset: p.end_offset,
}),
)
})
})
.collect())
},
)
.await)
}
}
#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
mod tests {
use super::*;
#[test]
fn offset_spec_sentinels_match_the_protocol_and_carry_their_minimum_version() {
assert_eq!(OffsetSpec::Earliest.as_timestamp(), -2);
assert_eq!(OffsetSpec::Latest.as_timestamp(), -1);
assert_eq!(OffsetSpec::MaxTimestamp.as_timestamp(), -3);
assert_eq!(OffsetSpec::EarliestLocal.as_timestamp(), -4);
assert_eq!(OffsetSpec::LatestTiered.as_timestamp(), -5);
assert_eq!(OffsetSpec::EarliestPendingUpload.as_timestamp(), -6);
assert_eq!(OffsetSpec::Timestamp(1_700).as_timestamp(), 1_700);
assert_eq!(OffsetSpec::MaxTimestamp.min_api_version(), 7);
assert_eq!(OffsetSpec::EarliestLocal.min_api_version(), 8);
assert_eq!(OffsetSpec::LatestTiered.min_api_version(), 9);
assert_eq!(OffsetSpec::EarliestPendingUpload.min_api_version(), 11);
for spec in [
OffsetSpec::Earliest,
OffsetSpec::Latest,
OffsetSpec::Timestamp(0),
OffsetSpec::MaxTimestamp,
OffsetSpec::EarliestLocal,
OffsetSpec::LatestTiered,
OffsetSpec::EarliestPendingUpload,
] {
assert!(spec.min_api_version() <= versions::LIST_OFFSETS_MAX);
}
}
#[test]
fn partitions_group_by_topic_with_their_values() {
let keys = [
TopicPartition::new("b", 0),
TopicPartition::new("a", 1),
TopicPartition::new("a", 0),
];
let grouped = by_topic(&keys, |tp| tp.partition * 10);
assert_eq!(grouped["a"].len(), 2);
assert_eq!(grouped["b"], vec![(0, 0)]);
}
}