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
use std::error::Error;
use std::fmt;
use std::iter;
use std::time::Duration;
use rdkafka::admin::{AdminClient, AdminOptions, NewTopic};
use rdkafka::client::ClientContext;
use rdkafka::error::{KafkaError, RDKafkaErrorCode};
use ore::collections::CollectionExt;
use ore::retry::Retry;
pub async fn create_new_topic<'a, C>(
client: &'a AdminClient<C>,
admin_opts: &AdminOptions,
new_topic: &'a NewTopic<'a>,
) -> Result<(), CreateTopicError>
where
C: ClientContext,
{
create_topic_helper(client, admin_opts, new_topic, false).await
}
pub async fn ensure_topic<'a, C>(
client: &'a AdminClient<C>,
admin_opts: &AdminOptions,
new_topic: &'a NewTopic<'a>,
) -> Result<(), CreateTopicError>
where
C: ClientContext,
{
create_topic_helper(client, admin_opts, new_topic, true).await
}
async fn create_topic_helper<'a, C>(
client: &'a AdminClient<C>,
admin_opts: &AdminOptions,
new_topic: &'a NewTopic<'a>,
allow_existing: bool,
) -> Result<(), CreateTopicError>
where
C: ClientContext,
{
let res = client
.create_topics(iter::once(new_topic), &admin_opts)
.await?;
if res.len() != 1 {
return Err(CreateTopicError::TopicCountMismatch(res.len()));
}
match res.into_element() {
Ok(_) => Ok(()),
Err((_, RDKafkaErrorCode::TopicAlreadyExists)) if allow_existing => Ok(()),
Err((_, e)) => Err(CreateTopicError::Kafka(KafkaError::AdminOp(e))),
}?;
Retry::default()
.retry_async(|_| async {
let metadata = client
.inner()
.fetch_metadata(None, Some(Duration::from_secs(10)))?;
let topic = metadata
.topics()
.iter()
.find(|t| t.name() == new_topic.name)
.ok_or(CreateTopicError::MissingMetadata)?;
if topic.partitions().len() as i32 != new_topic.num_partitions {
return Err(CreateTopicError::PartitionCountMismatch {
expected: new_topic.num_partitions,
actual: topic.partitions().len() as i32,
});
}
Ok(())
})
.await
}
#[derive(Debug)]
pub enum CreateTopicError {
Kafka(KafkaError),
TopicCountMismatch(usize),
MissingMetadata,
PartitionCountMismatch {
expected: i32,
actual: i32,
},
}
impl fmt::Display for CreateTopicError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
CreateTopicError::Kafka(e) => write!(f, "{}", e),
CreateTopicError::TopicCountMismatch(n) => write!(
f,
"kafka topic creation returned {} results, but exactly one result was expected",
n
),
CreateTopicError::MissingMetadata => {
f.write_str("unable to fetch topic metadata after creation")
}
CreateTopicError::PartitionCountMismatch { expected, actual } => write!(
f,
"topic reports {} partitions, but expected {} partitions",
actual, expected
),
}
}
}
impl Error for CreateTopicError {
fn source(&self) -> Option<&(dyn Error + 'static)> {
match self {
CreateTopicError::Kafka(e) => Some(e),
CreateTopicError::TopicCountMismatch(_)
| CreateTopicError::MissingMetadata
| CreateTopicError::PartitionCountMismatch { .. } => None,
}
}
}
impl From<KafkaError> for CreateTopicError {
fn from(e: KafkaError) -> CreateTopicError {
CreateTopicError::Kafka(e)
}
}