1use std::{
4 collections::{BTreeMap, HashMap, HashSet},
5 time::Duration,
6};
7
8#[cfg(test)]
9use krabka_protocol::owned::metadata_response;
10use krabka_protocol::{
11 owned::{
12 alter_partition_reassignments_request::{
13 AlterPartitionReassignmentsRequest, ReassignablePartition, ReassignableTopic,
14 },
15 alter_partition_reassignments_response::AlterPartitionReassignmentsResponse,
16 create_partitions_request::{
17 CreatePartitionsAssignment, CreatePartitionsRequest, CreatePartitionsTopic,
18 },
19 create_topics_request::{
20 CreatableReplicaAssignment, CreatableTopic, CreatableTopicConfig, CreateTopicsRequest,
21 },
22 delete_records_request::{
23 DeleteRecordsPartition, DeleteRecordsRequest, DeleteRecordsTopic,
24 },
25 delete_topics_request::{DeleteTopicState, DeleteTopicsRequest},
26 describe_cluster_request::DescribeClusterRequest,
27 list_partition_reassignments_request::{
28 ListPartitionReassignmentsRequest, ListPartitionReassignmentsTopics,
29 },
30 list_partition_reassignments_response::ListPartitionReassignmentsResponse,
31 metadata_request::{MetadataRequest, MetadataRequestTopic},
32 metadata_response::MetadataResponse,
33 },
34 primitives::uuid::Uuid as ProtoUuid,
35};
36use krabka_units::{Time, convert::TimeExt as _};
37use tokio::time::Instant;
38use uuid::Uuid;
39
40use crate::{
41 AdminClient, AdminError, KafkaError, NOT_CONTROLLER, kafka_error_if,
42 retry::{ControllerRetry, RetryPolicy, call_timeout_error, is_connection_failure},
43};
44
45#[derive(Debug, Clone, Default, bon::Builder)]
47pub struct CreateTopicSpec {
48 #[builder(into)]
49 pub name: String,
50 #[builder(default = -1)]
52 pub partitions: i32,
53 #[builder(default = -1)]
55 pub replicas: i32,
56 #[builder(default)]
57 pub configs: BTreeMap<String, String>,
58 #[builder(default)]
63 pub replica_assignments: BTreeMap<i32, Vec<i32>>,
64}
65
66#[derive(Debug, Clone, PartialEq)]
67pub struct CreateTopicOutcome {
68 pub name: String,
69 pub topic_id: Option<Uuid>,
70 pub error: Option<KafkaError>,
71 pub throttle_time: Option<Time>,
75}
76
77#[derive(Debug, Clone, PartialEq)]
78pub struct DeleteTopicOutcome {
79 pub name: String,
80 pub error: Option<KafkaError>,
81 pub throttle_time: Option<Time>,
84}
85
86#[derive(Debug, Clone, Copy, PartialEq)]
90pub struct TopicMutationOptions {
91 pub timeout: Option<Time>,
95 pub retry_on_quota_violation: bool,
98}
99
100impl Default for TopicMutationOptions {
101 fn default() -> Self {
102 Self {
103 timeout: None,
104 retry_on_quota_violation: true,
105 }
106 }
107}
108
109impl TopicMutationOptions {
110 #[must_use]
112 pub fn with_timeout(timeout: Time) -> Self {
113 Self {
114 timeout: Some(timeout),
115 ..Self::default()
116 }
117 }
118
119 fn retry_policy(self, base: RetryPolicy) -> RetryPolicy {
121 match self.timeout {
122 Some(timeout) => RetryPolicy {
123 timeout: Duration::from_millis(u64::try_from(timeout.millis_i64()).unwrap_or(0)),
124 ..base
125 },
126 None => base,
127 }
128 }
129}
130
131#[derive(Debug, Clone, PartialEq, Eq)]
132pub struct DeleteRecordsOp {
133 pub topic: String,
134 pub partition: i32,
135 pub offset: i64,
136}
137
138#[derive(Debug, Clone, PartialEq, Eq)]
139pub struct DeleteRecordsOutcome {
140 pub topic: String,
141 pub partition: i32,
142 pub error_code: i16,
143 pub low_watermark: i64,
144}
145
146#[derive(Debug, Clone, Default)]
148pub struct CreatePartitionsOp {
149 pub name: String,
150 pub new_total_count: i32,
151 pub assignments: Option<Vec<Vec<i32>>>,
155}
156
157#[derive(Debug, Clone, PartialEq)]
158pub struct CreatePartitionsOutcome {
159 pub name: String,
160 pub error: Option<KafkaError>,
161 pub throttle_time: Option<Time>,
164}
165
166#[derive(Debug, Clone, Default, PartialEq, Eq)]
167pub struct TopicMetadata {
168 pub controller_id: i32,
169 pub topics: Vec<TopicMetadataEntry>,
170}
171
172#[derive(Debug, Clone, PartialEq, Eq)]
173pub struct TopicMetadataEntry {
174 pub name: String,
175 pub topic_id: Option<Uuid>,
176 pub partition_count: i32,
177 pub replication_factor: i32,
178 pub error: Option<KafkaError>,
179}
180
181#[derive(Debug, Clone, Copy, PartialEq, Eq)]
184pub enum TopicReplicationStatus {
185 InSync,
187 ReassignmentInProgress,
189 ReassignmentSubmitted,
191}
192
193#[derive(Debug, Clone, PartialEq, Eq)]
194pub struct PartitionAssignment {
195 pub topic: String,
196 pub partition: i32,
197 pub replicas: Vec<i32>,
198 pub adding_replicas: Vec<i32>,
199 pub removing_replicas: Vec<i32>,
200}
201
202#[derive(Debug, Clone, PartialEq, Eq)]
203pub struct PartitionAssignmentOutcome {
204 pub topic: String,
205 pub partition: i32,
206 pub error: Option<KafkaError>,
207}
208
209impl AdminClient {
210 pub async fn describe_partition_assignments(
218 &mut self,
219 topics: &[&str],
220 ) -> Result<Vec<PartitionAssignment>, AdminError> {
221 let response: MetadataResponse = self.conn.send(build_metadata(topics)).await?;
222 Ok(response
223 .topics
224 .into_iter()
225 .flat_map(|topic| {
226 let name = topic.name.unwrap_or_default();
227 topic
228 .partitions
229 .into_iter()
230 .map(move |partition| PartitionAssignment {
231 topic: name.clone(),
232 partition: partition.partition_index,
233 replicas: partition.replica_nodes,
234 adding_replicas: Vec::new(),
235 removing_replicas: Vec::new(),
236 })
237 })
238 .collect())
239 }
240
241 pub async fn list_partition_reassignments(
248 &mut self,
249 partitions: &BTreeMap<String, Vec<i32>>,
250 timeout: Time,
251 ) -> Result<Vec<PartitionAssignment>, AdminError> {
252 let response = self
253 .conn
254 .send(ListPartitionReassignmentsRequest {
255 timeout_ms: timeout.millis_i32(),
256 topics: (!partitions.is_empty()).then(|| {
257 partitions
258 .iter()
259 .map(|(name, indexes)| ListPartitionReassignmentsTopics {
260 name: name.clone(),
261 partition_indexes: indexes.clone(),
262 ..Default::default()
263 })
264 .collect()
265 }),
266 ..Default::default()
267 })
268 .await?;
269 broker_error(
270 "ListPartitionReassignments",
271 response.error_code,
272 response.error_message,
273 )?;
274 Ok(response
275 .topics
276 .into_iter()
277 .flat_map(|topic| {
278 topic
279 .partitions
280 .into_iter()
281 .map(move |partition| PartitionAssignment {
282 topic: topic.name.clone(),
283 partition: partition.partition_index,
284 replicas: partition.replicas,
285 adding_replicas: partition.adding_replicas,
286 removing_replicas: partition.removing_replicas,
287 })
288 })
289 .collect())
290 }
291
292 pub async fn alter_partition_assignments(
299 &mut self,
300 assignments: &BTreeMap<(String, i32), Option<Vec<i32>>>,
301 timeout: Time,
302 ) -> Result<Vec<PartitionAssignmentOutcome>, AdminError> {
303 self.alter_partition_assignments_with(
304 assignments,
305 timeout,
306 AlterPartitionReassignmentsOptions::default(),
307 )
308 .await
309 }
310
311 pub async fn alter_partition_assignments_with(
320 &mut self,
321 assignments: &BTreeMap<(String, i32), Option<Vec<i32>>>,
322 timeout: Time,
323 options: AlterPartitionReassignmentsOptions,
324 ) -> Result<Vec<PartitionAssignmentOutcome>, AdminError> {
325 let request = build_partition_assignment_request(assignments, timeout, options);
326 let min_version = if options.allow_replication_factor_change {
327 0
328 } else {
329 ALLOW_REPLICATION_FACTOR_CHANGE_MIN_VERSION
330 };
331 let mut retry = ControllerRetry::new("AlterPartitionReassignments", self.retry);
332 loop {
333 let response = retry
334 .bounded(self.conn.send_at_least(request.clone(), min_version))
335 .await?;
336 if response.error_code != NOT_CONTROLLER {
337 return parse_partition_assignment_response(response);
338 }
339 retry.after_not_controller(self).await?;
340 }
341 }
342
343 pub async fn metadata(&mut self, topics: &[&str]) -> Result<TopicMetadata, AdminError> {
349 let req = build_metadata(topics);
350 let resp = self.conn.send(req).await?;
351 Ok(parse_metadata(resp))
352 }
353
354 pub async fn reconcile_topic_replication_factor(
366 &mut self,
367 topic: &str,
368 replication_factor: i32,
369 timeout: Time,
370 ) -> Result<TopicReplicationStatus, AdminError> {
371 if self.conn.uses_controller_bootstrap() {
372 return Err(AdminError::Broker {
373 api: "ControllerEndpoint",
374 code: 115,
375 name: "UNSUPPORTED_ENDPOINT_TYPE",
376 message: Some(
377 "replication-factor reconciliation requires a broker bootstrap endpoint".into(),
378 ),
379 });
380 }
381 let mut retry = ControllerRetry::new("AlterPartitionReassignments", self.retry);
387 retry.bounded(self.refresh_controller_connection()).await?;
388 loop {
389 match retry
390 .bounded(self.reconcile_topic_replication_factor_once(
391 topic,
392 replication_factor,
393 timeout,
394 ))
395 .await
396 {
397 Err(AdminError::Broker {
398 code: NOT_CONTROLLER,
399 ..
400 }) => retry.after_not_controller(self).await?,
401 result => return result,
402 }
403 }
404 }
405
406 async fn reconcile_topic_replication_factor_once(
407 &mut self,
408 topic: &str,
409 replication_factor: i32,
410 timeout: Time,
411 ) -> Result<TopicReplicationStatus, AdminError> {
412 let ongoing: ListPartitionReassignmentsResponse = self
413 .conn
414 .send(ListPartitionReassignmentsRequest::default())
415 .await?;
416 broker_error(
417 "ListPartitionReassignments",
418 ongoing.error_code,
419 ongoing.error_message,
420 )?;
421 if ongoing.topics.iter().any(|entry| entry.name == topic) {
422 return Ok(TopicReplicationStatus::ReassignmentInProgress);
423 }
424
425 let cluster = self
429 .conn
430 .send_at_least(
431 DescribeClusterRequest {
432 include_fenced_brokers: true,
433 ..Default::default()
434 },
435 2,
436 )
437 .await?;
438 broker_error("DescribeCluster", cluster.error_code, cluster.error_message)?;
439 let eligible_brokers = cluster
440 .brokers
441 .iter()
442 .filter(|broker| !broker.is_fenced)
443 .map(|broker| broker.broker_id)
444 .collect::<Vec<_>>();
445
446 let metadata: MetadataResponse = self.conn.send(build_metadata(&[topic])).await?;
447 let Some(request) = build_replication_factor_reassignment(
448 &metadata,
449 &eligible_brokers,
450 topic,
451 replication_factor,
452 timeout,
453 )?
454 else {
455 return Ok(TopicReplicationStatus::InSync);
456 };
457 let response: AlterPartitionReassignmentsResponse = self.conn.send(request).await?;
458 broker_error(
459 "AlterPartitionReassignments",
460 response.error_code,
461 response.error_message,
462 )?;
463 if let Some(error) = response.responses.into_iter().find_map(|topic_response| {
464 topic_response
465 .partitions
466 .into_iter()
467 .find(|partition| partition.error_code != 0)
468 }) {
469 return Err(AdminError::Broker {
470 api: "AlterPartitionReassignments",
471 code: error.error_code,
472 name: crate::kafka_error_name(error.error_code),
473 message: error.error_message,
474 });
475 }
476 Ok(TopicReplicationStatus::ReassignmentSubmitted)
477 }
478
479 pub async fn create_topics(
500 &mut self,
501 specs: &[CreateTopicSpec],
502 options: TopicMutationOptions,
503 ) -> Result<Vec<CreateTopicOutcome>, AdminError> {
504 self.create_topics_with_retry(specs, options, options.retry_policy(self.retry))
505 .await
506 }
507
508 async fn create_topics_with_retry(
509 &mut self,
510 specs: &[CreateTopicSpec],
511 options: TopicMutationOptions,
512 policy: RetryPolicy,
513 ) -> Result<Vec<CreateTopicOutcome>, AdminError> {
514 let names = specs.iter().map(|spec| spec.name.clone()).collect();
515 self.mutate_topics(
516 "CreateTopics",
517 names,
518 options.retry_on_quota_violation,
519 policy,
520 |pending, timeout_ms| {
521 let specs = specs
522 .iter()
523 .filter(|spec| pending.contains(&spec.name))
524 .cloned()
525 .collect::<Vec<_>>();
526 build_create_topics(&specs, timeout_ms)
527 },
528 parse_create_topics,
529 )
530 .await
531 }
532
533 pub async fn delete_topics(
541 &mut self,
542 names: &[&str],
543 options: TopicMutationOptions,
544 ) -> Result<Vec<DeleteTopicOutcome>, AdminError> {
545 self.delete_topics_with_retry(names, options, options.retry_policy(self.retry))
546 .await
547 }
548
549 async fn delete_topics_with_retry(
550 &mut self,
551 names: &[&str],
552 options: TopicMutationOptions,
553 policy: RetryPolicy,
554 ) -> Result<Vec<DeleteTopicOutcome>, AdminError> {
555 let names = names.iter().map(|name| (*name).to_owned()).collect();
556 self.mutate_topics(
557 "DeleteTopics",
558 names,
559 options.retry_on_quota_violation,
560 policy,
561 build_delete_topics,
562 parse_delete_topics,
563 )
564 .await
565 }
566
567 pub async fn create_partitions(
575 &mut self,
576 ops: &[CreatePartitionsOp],
577 options: TopicMutationOptions,
578 ) -> Result<Vec<CreatePartitionsOutcome>, AdminError> {
579 self.create_partitions_with_retry(ops, options, options.retry_policy(self.retry))
580 .await
581 }
582
583 async fn create_partitions_with_retry(
584 &mut self,
585 ops: &[CreatePartitionsOp],
586 options: TopicMutationOptions,
587 policy: RetryPolicy,
588 ) -> Result<Vec<CreatePartitionsOutcome>, AdminError> {
589 let names = ops.iter().map(|op| op.name.clone()).collect();
590 self.mutate_topics(
591 "CreatePartitions",
592 names,
593 options.retry_on_quota_violation,
594 policy,
595 |pending, timeout_ms| {
596 let ops = ops
597 .iter()
598 .filter(|op| pending.contains(&op.name))
599 .cloned()
600 .collect::<Vec<_>>();
601 build_create_partitions(&ops, timeout_ms)
602 },
603 parse_create_partitions,
604 )
605 .await
606 }
607
608 async fn mutate_topics<R, T>(
611 &mut self,
612 api: &'static str,
613 topics: Vec<String>,
614 retry_on_quota_violation: bool,
615 policy: RetryPolicy,
616 build: impl Fn(&[String], i32) -> R,
617 parse: impl Fn(R::Response) -> Vec<T>,
618 ) -> Result<Vec<T>, AdminError>
619 where
620 R: krabka_protocol::ProtocolRequest + Clone,
621 R::Response: 'static,
622 T: TopicMutationOutcome,
623 {
624 let mut order = Vec::new();
625 for topic in topics {
626 if !order.contains(&topic) {
627 order.push(topic);
628 }
629 }
630 let mut pending = order.clone();
631 let mut done = HashMap::<String, T>::new();
632 let mut throttled = HashMap::<String, (T, Instant)>::new();
633 let mut deadline = policy.start();
634 let mut attempts = 0_u32;
635 let mut last_error = "none".to_owned();
636 while !pending.is_empty() {
637 attempts = attempts.saturating_add(1);
638 let request = build(&pending, deadline.remaining_millis());
639 let Some(response) = deadline.bounded(self.conn.send(request)).await else {
642 "the request was in flight at the deadline".clone_into(&mut last_error);
643 break;
644 };
645 let outcomes = match response {
646 Ok(response) => parse(response),
647 Err(error) if is_connection_failure(&error) => {
650 last_error = error.to_string();
651 if deadline.exhausted() {
652 break;
653 }
654 deadline.backoff().await;
655 if deadline.expired() {
656 break;
657 }
658 continue;
659 }
660 Err(error) => return Err(error),
661 };
662 let controller_bootstrap = self.conn.uses_controller_bootstrap();
663 if outcomes
664 .iter()
665 .any(|outcome| is_not_controller(outcome.error_code(), controller_bootstrap))
666 {
667 "NOT_CONTROLLER".clone_into(&mut last_error);
668 if deadline
669 .bounded(self.refresh_controller_after_not_controller())
670 .await
671 .transpose()?
672 .is_none()
673 {
674 break;
675 }
676 if deadline.exhausted() {
677 break;
678 }
679 deadline.backoff().await;
680 if deadline.expired() {
681 break;
682 }
683 continue;
684 }
685
686 let received = Instant::now();
687 let mut retry = Vec::new();
688 let mut throttle = Duration::ZERO;
689 for outcome in outcomes {
690 let topic = outcome.topic().to_owned();
691 if !pending.contains(&topic) || done.contains_key(&topic) {
692 tracing::warn!(api, topic, "server response mentioned an unknown topic");
693 continue;
694 }
695 if retry_on_quota_violation && outcome.error_code() == THROTTLING_QUOTA_EXCEEDED {
696 throttle = throttle.max(outcome.throttle_duration());
697 retry.push(topic.clone());
698 throttled.insert(topic, (outcome, received));
699 } else {
700 throttled.remove(&topic);
701 done.insert(topic, outcome);
702 }
703 }
704 for topic in &pending {
705 if !done.contains_key(topic) && !retry.contains(topic) {
706 let error = KafkaError {
707 code: UNKNOWN_SERVER_ERROR,
708 name: crate::kafka_error_name(UNKNOWN_SERVER_ERROR),
709 message: Some(format!(
710 "The controller response did not contain a result for topic {topic}"
711 )),
712 };
713 done.insert(topic.clone(), T::failed(topic, error));
714 }
715 }
716 pending = retry;
717 if pending.is_empty() {
718 break;
719 }
720 "THROTTLING_QUOTA_EXCEEDED".clone_into(&mut last_error);
721 if throttle.is_zero() {
726 deadline.backoff().await;
727 } else {
728 deadline.sleep(throttle).await;
729 }
730 if deadline.expired() {
731 break;
732 }
733 }
734 for topic in pending {
735 let outcome = match throttled.remove(&topic) {
736 Some((outcome, received)) => {
737 let remaining = outcome
738 .throttle_duration()
739 .saturating_sub(received.elapsed());
740 outcome.with_throttle_duration(remaining)
741 }
742 None => T::failed(&topic, call_timeout_error(api, attempts, &last_error)),
743 };
744 done.insert(topic, outcome);
745 }
746 Ok(order
747 .into_iter()
748 .filter_map(|topic| done.remove(&topic))
749 .collect())
750 }
751
752 pub async fn delete_records(
764 &mut self,
765 ops: &[DeleteRecordsOp],
766 timeout: Time,
767 ) -> Result<Vec<DeleteRecordsOutcome>, AdminError> {
768 if ops.is_empty() {
769 return Ok(Vec::new());
770 }
771
772 let mut outcomes = Vec::new();
773 let groups = self
774 .delete_records_leader_groups(ops, &mut outcomes)
775 .await?;
776 for (endpoint, leader_ops) in groups {
777 self.reconnect(&endpoint).await?;
778 let req = build_delete_records(&leader_ops, timeout);
779 let resp = self.conn.send(req).await?;
780 outcomes.extend(parse_delete_records(resp));
781 }
782 Ok(outcomes)
783 }
784
785 pub(crate) async fn refresh_controller_connection(&mut self) -> Result<(), AdminError> {
789 if self.conn.uses_controller_bootstrap() {
790 return self.conn.rebootstrap().await;
791 }
792 let md_resp = self.conn.send(build_metadata(&[])).await?;
793 let Some(controller_addr) = controller_endpoint(&md_resp) else {
794 if controller_requires_bootstrap_fallback(&md_resp) {
798 return self.conn.rebootstrap().await;
799 }
800 return Err(AdminError::Protocol(format!(
801 "metadata names no endpoint for controller {}",
802 md_resp.controller_id
803 )));
804 };
805 self.reconnect(&controller_addr).await
806 }
807
808 async fn delete_records_leader_groups(
809 &mut self,
810 ops: &[DeleteRecordsOp],
811 outcomes: &mut Vec<DeleteRecordsOutcome>,
812 ) -> Result<BTreeMap<String, Vec<DeleteRecordsOp>>, AdminError> {
813 let mut topic_names = ops.iter().map(|op| op.topic.as_str()).collect::<Vec<_>>();
814 topic_names.sort_unstable();
815 topic_names.dedup();
816
817 let metadata = self.conn.send(build_metadata(&topic_names)).await?;
818 let broker_endpoints = metadata
819 .brokers
820 .iter()
821 .map(|broker| {
822 let endpoint = if broker.port > 0 {
823 format!("{}:{}", broker.host, broker.port)
824 } else {
825 self.bootstrap_addrs
826 .first()
827 .cloned()
828 .unwrap_or_else(|| format!("{}:{}", broker.host, broker.port))
829 };
830 (broker.node_id, endpoint)
831 })
832 .collect::<BTreeMap<_, _>>();
833 let mut groups = BTreeMap::<String, Vec<DeleteRecordsOp>>::new();
834
835 for op in ops {
836 let Some(topic) = metadata
837 .topics
838 .iter()
839 .find(|topic| topic.name.as_deref() == Some(op.topic.as_str()))
840 else {
841 outcomes.push(delete_records_error_outcome(op, 3));
842 continue;
843 };
844
845 if topic.error_code != 0 {
846 outcomes.push(delete_records_error_outcome(op, topic.error_code));
847 continue;
848 }
849
850 let Some(partition) = topic
851 .partitions
852 .iter()
853 .find(|partition| partition.partition_index == op.partition)
854 else {
855 outcomes.push(delete_records_error_outcome(op, 3));
856 continue;
857 };
858
859 if partition.error_code != 0 {
860 outcomes.push(delete_records_error_outcome(op, partition.error_code));
861 continue;
862 }
863
864 let Some(endpoint) = broker_endpoints.get(&partition.leader_id) else {
865 return Err(AdminError::Protocol(format!(
866 "no broker endpoint for DeleteRecords leader {}",
867 partition.leader_id
868 )));
869 };
870 groups.entry(endpoint.clone()).or_default().push(op.clone());
871 }
872
873 Ok(groups)
874 }
875}
876
877const UNKNOWN_SERVER_ERROR: i16 = -1;
879const NOT_LEADER_OR_FOLLOWER: i16 = 6;
882const THROTTLING_QUOTA_EXCEEDED: i16 = 89;
884
885fn is_not_controller(code: i16, controller_bootstrap: bool) -> bool {
889 code == NOT_CONTROLLER || (controller_bootstrap && code == NOT_LEADER_OR_FOLLOWER)
890}
891
892trait TopicMutationOutcome {
894 fn topic(&self) -> &str;
896 fn error_code(&self) -> i16;
898 fn throttle_duration(&self) -> Duration;
900 #[must_use]
902 fn with_throttle_duration(self, remaining: Duration) -> Self;
903 fn failed(topic: &str, error: KafkaError) -> Self;
905}
906
907fn throttle_duration(throttle_time: Option<Time>) -> Duration {
908 throttle_time.map_or(Duration::ZERO, |time| {
909 Duration::from_millis(u64::try_from(time.millis_i64()).unwrap_or(0))
910 })
911}
912
913fn throttle_time(remaining: Duration) -> Time {
914 Time::from_millis(i64::try_from(remaining.as_millis()).unwrap_or(i64::MAX))
915}
916
917fn quota_throttle_time(code: i16, throttle_time_ms: i32) -> Option<Time> {
920 (code == THROTTLING_QUOTA_EXCEEDED).then(|| Time::from_millis(i64::from(throttle_time_ms)))
921}
922
923impl TopicMutationOutcome for CreateTopicOutcome {
924 fn topic(&self) -> &str {
925 &self.name
926 }
927 fn error_code(&self) -> i16 {
928 self.error.as_ref().map_or(0, |error| error.code)
929 }
930 fn throttle_duration(&self) -> Duration {
931 throttle_duration(self.throttle_time)
932 }
933 fn with_throttle_duration(self, remaining: Duration) -> Self {
934 Self {
935 throttle_time: Some(throttle_time(remaining)),
936 ..self
937 }
938 }
939 fn failed(topic: &str, error: KafkaError) -> Self {
940 Self {
941 name: topic.to_owned(),
942 topic_id: None,
943 error: Some(error),
944 throttle_time: None,
945 }
946 }
947}
948
949impl TopicMutationOutcome for DeleteTopicOutcome {
950 fn topic(&self) -> &str {
951 &self.name
952 }
953 fn error_code(&self) -> i16 {
954 self.error.as_ref().map_or(0, |error| error.code)
955 }
956 fn throttle_duration(&self) -> Duration {
957 throttle_duration(self.throttle_time)
958 }
959 fn with_throttle_duration(self, remaining: Duration) -> Self {
960 Self {
961 throttle_time: Some(throttle_time(remaining)),
962 ..self
963 }
964 }
965 fn failed(topic: &str, error: KafkaError) -> Self {
966 Self {
967 name: topic.to_owned(),
968 error: Some(error),
969 throttle_time: None,
970 }
971 }
972}
973
974impl TopicMutationOutcome for CreatePartitionsOutcome {
975 fn topic(&self) -> &str {
976 &self.name
977 }
978 fn error_code(&self) -> i16 {
979 self.error.as_ref().map_or(0, |error| error.code)
980 }
981 fn throttle_duration(&self) -> Duration {
982 throttle_duration(self.throttle_time)
983 }
984 fn with_throttle_duration(self, remaining: Duration) -> Self {
985 Self {
986 throttle_time: Some(throttle_time(remaining)),
987 ..self
988 }
989 }
990 fn failed(topic: &str, error: KafkaError) -> Self {
991 Self {
992 name: topic.to_owned(),
993 error: Some(error),
994 throttle_time: None,
995 }
996 }
997}
998
999#[derive(Debug, Clone, Copy, PartialEq, Eq)]
1001pub struct AlterPartitionReassignmentsOptions {
1002 pub allow_replication_factor_change: bool,
1005}
1006
1007impl Default for AlterPartitionReassignmentsOptions {
1008 fn default() -> Self {
1009 Self {
1010 allow_replication_factor_change: true,
1011 }
1012 }
1013}
1014
1015const ALLOW_REPLICATION_FACTOR_CHANGE_MIN_VERSION: i16 = 1;
1018
1019fn build_partition_assignment_request(
1020 assignments: &BTreeMap<(String, i32), Option<Vec<i32>>>,
1021 timeout: Time,
1022 options: AlterPartitionReassignmentsOptions,
1023) -> AlterPartitionReassignmentsRequest {
1024 let mut topics = BTreeMap::<String, Vec<ReassignablePartition>>::new();
1025 for ((topic, partition), replicas) in assignments {
1026 topics
1027 .entry(topic.clone())
1028 .or_default()
1029 .push(ReassignablePartition {
1030 partition_index: *partition,
1031 replicas: replicas.clone(),
1032 ..Default::default()
1033 });
1034 }
1035 AlterPartitionReassignmentsRequest {
1036 timeout_ms: timeout.millis_i32(),
1037 allow_replication_factor_change: options.allow_replication_factor_change,
1038 topics: topics
1039 .into_iter()
1040 .map(|(name, partitions)| ReassignableTopic {
1041 name,
1042 partitions,
1043 ..Default::default()
1044 })
1045 .collect(),
1046 ..Default::default()
1047 }
1048}
1049
1050fn parse_partition_assignment_response(
1051 response: AlterPartitionReassignmentsResponse,
1052) -> Result<Vec<PartitionAssignmentOutcome>, AdminError> {
1053 broker_error(
1054 "AlterPartitionReassignments",
1055 response.error_code,
1056 response.error_message,
1057 )?;
1058 Ok(response
1059 .responses
1060 .into_iter()
1061 .flat_map(|topic| {
1062 topic
1063 .partitions
1064 .into_iter()
1065 .map(move |partition| PartitionAssignmentOutcome {
1066 topic: topic.name.clone(),
1067 partition: partition.partition_index,
1068 error: kafka_error_if(partition.error_code, partition.error_message),
1069 })
1070 })
1071 .collect())
1072}
1073
1074fn build_metadata(topics: &[&str]) -> MetadataRequest {
1075 MetadataRequest {
1076 topics: if topics.is_empty() {
1077 None
1078 } else {
1079 Some(
1080 topics
1081 .iter()
1082 .map(|n| MetadataRequestTopic {
1083 topic_id: ProtoUuid::ZERO,
1084 name: Some((*n).to_string()),
1085 ..Default::default()
1086 })
1087 .collect(),
1088 )
1089 },
1090 allow_auto_topic_creation: false,
1091 include_cluster_authorized_operations: false,
1092 include_topic_authorized_operations: false,
1093 ..Default::default()
1094 }
1095}
1096
1097fn broker_error(api: &'static str, code: i16, message: Option<String>) -> Result<(), AdminError> {
1098 if code == 0 {
1099 return Ok(());
1100 }
1101 Err(AdminError::Broker {
1102 api,
1103 code,
1104 name: crate::kafka_error_name(code),
1105 message,
1106 })
1107}
1108
1109fn build_replication_factor_reassignment(
1110 metadata: &MetadataResponse,
1111 eligible_brokers: &[i32],
1112 topic_name: &str,
1113 replication_factor: i32,
1114 timeout: Time,
1115) -> Result<Option<AlterPartitionReassignmentsRequest>, AdminError> {
1116 broker_error("Metadata", metadata.error_code, None)?;
1117 let desired = usize::try_from(replication_factor).map_err(|_| {
1118 AdminError::Protocol("replication factor must be a positive integer".into())
1119 })?;
1120 if desired == 0 {
1121 return Err(AdminError::Protocol(
1122 "replication factor must be a positive integer".into(),
1123 ));
1124 }
1125
1126 let mut brokers = eligible_brokers.to_vec();
1127 brokers.sort_unstable();
1128 brokers.dedup();
1129 if desired > brokers.len() {
1130 return Err(AdminError::Broker {
1131 api: "AlterPartitionReassignments",
1132 code: 38,
1133 name: crate::kafka_error_name(38),
1134 message: Some(format!(
1135 "replication factor {replication_factor} exceeds live broker count {}",
1136 brokers.len()
1137 )),
1138 });
1139 }
1140
1141 let topic = metadata
1142 .topics
1143 .iter()
1144 .find(|topic| topic.name.as_deref() == Some(topic_name))
1145 .ok_or_else(|| AdminError::Broker {
1146 api: "Metadata",
1147 code: 3,
1148 name: crate::kafka_error_name(3),
1149 message: Some(format!("topic {topic_name:?} is absent from metadata")),
1150 })?;
1151 broker_error("Metadata", topic.error_code, None)?;
1152
1153 let available = brokers.iter().copied().collect::<HashSet<_>>();
1154 let mut partitions = Vec::new();
1155 for (rotation, partition) in topic.partitions.iter().enumerate() {
1156 broker_error("Metadata", partition.error_code, None)?;
1157 let mut selected = HashSet::with_capacity(desired);
1158 let mut target = partition
1159 .replica_nodes
1160 .iter()
1161 .copied()
1162 .filter(|broker| available.contains(broker) && selected.insert(*broker))
1163 .take(desired)
1164 .collect::<Vec<_>>();
1165 for offset in 0..brokers.len() {
1166 if target.len() == desired {
1167 break;
1168 }
1169 let broker = brokers[(rotation + offset) % brokers.len()];
1170 if selected.insert(broker) {
1171 target.push(broker);
1172 }
1173 }
1174 if target != partition.replica_nodes {
1175 partitions.push(ReassignablePartition {
1176 partition_index: partition.partition_index,
1177 replicas: Some(target),
1178 ..Default::default()
1179 });
1180 }
1181 }
1182 if partitions.is_empty() {
1183 return Ok(None);
1184 }
1185
1186 Ok(Some(AlterPartitionReassignmentsRequest {
1187 timeout_ms: timeout.millis_i32(),
1188 allow_replication_factor_change: true,
1189 topics: vec![ReassignableTopic {
1190 name: topic_name.to_string(),
1191 partitions,
1192 ..Default::default()
1193 }],
1194 ..Default::default()
1195 }))
1196}
1197
1198fn build_create_topics(specs: &[CreateTopicSpec], timeout_ms: i32) -> CreateTopicsRequest {
1199 CreateTopicsRequest {
1200 topics: specs
1201 .iter()
1202 .map(|s| CreatableTopic {
1203 name: s.name.clone(),
1204 num_partitions: s.partitions,
1205 replication_factor: i16::try_from(s.replicas).unwrap_or(i16::MAX),
1206 assignments: s
1207 .replica_assignments
1208 .iter()
1209 .map(|(partition_index, broker_ids)| CreatableReplicaAssignment {
1210 partition_index: *partition_index,
1211 broker_ids: broker_ids.clone(),
1212 ..Default::default()
1213 })
1214 .collect(),
1215 configs: s
1216 .configs
1217 .iter()
1218 .map(|(k, v)| CreatableTopicConfig {
1219 name: k.clone(),
1220 value: Some(v.clone()),
1221 ..Default::default()
1222 })
1223 .collect(),
1224 ..Default::default()
1225 })
1226 .collect(),
1227 timeout_ms,
1228 validate_only: false,
1229 ..Default::default()
1230 }
1231}
1232
1233fn build_delete_topics(names: &[String], timeout_ms: i32) -> DeleteTopicsRequest {
1237 DeleteTopicsRequest {
1238 topic_names: names.to_vec(),
1239 topics: names
1240 .iter()
1241 .map(|name| DeleteTopicState {
1242 name: Some(name.clone()),
1243 topic_id: ProtoUuid::ZERO,
1244 ..Default::default()
1245 })
1246 .collect(),
1247 timeout_ms,
1248 ..Default::default()
1249 }
1250}
1251
1252fn build_create_partitions(ops: &[CreatePartitionsOp], timeout_ms: i32) -> CreatePartitionsRequest {
1253 CreatePartitionsRequest {
1254 topics: ops
1255 .iter()
1256 .map(|op| CreatePartitionsTopic {
1257 name: op.name.clone(),
1258 count: op.new_total_count,
1259 assignments: op.assignments.as_ref().map(|assignments| {
1260 assignments
1261 .iter()
1262 .map(|broker_ids| CreatePartitionsAssignment {
1263 broker_ids: broker_ids.clone(),
1264 ..Default::default()
1265 })
1266 .collect()
1267 }),
1268 ..Default::default()
1269 })
1270 .collect(),
1271 timeout_ms,
1272 validate_only: false,
1273 ..Default::default()
1274 }
1275}
1276
1277fn build_delete_records(ops: &[DeleteRecordsOp], timeout: Time) -> DeleteRecordsRequest {
1278 let mut topics = BTreeMap::<String, Vec<DeleteRecordsPartition>>::new();
1279 for op in ops {
1280 topics
1281 .entry(op.topic.clone())
1282 .or_default()
1283 .push(DeleteRecordsPartition {
1284 partition_index: op.partition,
1285 offset: op.offset,
1286 ..Default::default()
1287 });
1288 }
1289
1290 DeleteRecordsRequest {
1291 topics: topics
1292 .into_iter()
1293 .map(|(name, partitions)| DeleteRecordsTopic {
1294 name,
1295 partitions,
1296 ..Default::default()
1297 })
1298 .collect(),
1299 timeout_ms: timeout.millis_i32(),
1300 ..Default::default()
1301 }
1302}
1303
1304fn parse_create_topics(
1305 resp: <CreateTopicsRequest as krabka_protocol::ProtocolRequest>::Response,
1306) -> Vec<CreateTopicOutcome> {
1307 let throttle_time_ms = resp.throttle_time_ms;
1308 resp.topics
1309 .into_iter()
1310 .map(|t| CreateTopicOutcome {
1311 name: t.name,
1312 topic_id: proto_uuid_to_opt(t.topic_id),
1313 throttle_time: quota_throttle_time(t.error_code, throttle_time_ms),
1314 error: kafka_error_if(t.error_code, t.error_message),
1315 })
1316 .collect()
1317}
1318
1319fn parse_delete_topics(
1320 resp: <DeleteTopicsRequest as krabka_protocol::ProtocolRequest>::Response,
1321) -> Vec<DeleteTopicOutcome> {
1322 let throttle_time_ms = resp.throttle_time_ms;
1323 resp.responses
1324 .into_iter()
1325 .map(|t| DeleteTopicOutcome {
1326 name: t.name.unwrap_or_default(),
1327 throttle_time: quota_throttle_time(t.error_code, throttle_time_ms),
1328 error: kafka_error_if(t.error_code, t.error_message),
1329 })
1330 .collect()
1331}
1332
1333fn parse_create_partitions(
1334 resp: <CreatePartitionsRequest as krabka_protocol::ProtocolRequest>::Response,
1335) -> Vec<CreatePartitionsOutcome> {
1336 let throttle_time_ms = resp.throttle_time_ms;
1337 resp.results
1338 .into_iter()
1339 .map(|t| CreatePartitionsOutcome {
1340 name: t.name,
1341 throttle_time: quota_throttle_time(t.error_code, throttle_time_ms),
1342 error: kafka_error_if(t.error_code, t.error_message),
1343 })
1344 .collect()
1345}
1346
1347fn parse_delete_records(
1348 resp: <DeleteRecordsRequest as krabka_protocol::ProtocolRequest>::Response,
1349) -> Vec<DeleteRecordsOutcome> {
1350 resp.topics
1351 .into_iter()
1352 .flat_map(|topic| {
1353 let topic_name = topic.name;
1354 topic
1355 .partitions
1356 .into_iter()
1357 .map(move |partition| DeleteRecordsOutcome {
1358 topic: topic_name.clone(),
1359 partition: partition.partition_index,
1360 error_code: partition.error_code,
1361 low_watermark: partition.low_watermark,
1362 })
1363 })
1364 .collect()
1365}
1366
1367fn delete_records_error_outcome(op: &DeleteRecordsOp, error_code: i16) -> DeleteRecordsOutcome {
1368 DeleteRecordsOutcome {
1369 topic: op.topic.clone(),
1370 partition: op.partition,
1371 error_code,
1372 low_watermark: -1,
1373 }
1374}
1375
1376fn parse_metadata(
1377 resp: <MetadataRequest as krabka_protocol::ProtocolRequest>::Response,
1378) -> TopicMetadata {
1379 let topics = resp
1380 .topics
1381 .into_iter()
1382 .map(|t| {
1383 let partition_count = i32::try_from(t.partitions.len()).unwrap_or(i32::MAX);
1384 let replication_factor = i32::from(t.partitions.first().map_or(0, |p| {
1385 i16::try_from(p.replica_nodes.len()).unwrap_or(i16::MAX)
1386 }));
1387 TopicMetadataEntry {
1388 name: t.name.unwrap_or_default(),
1389 topic_id: proto_uuid_to_opt(t.topic_id),
1390 partition_count,
1391 replication_factor,
1392 error: kafka_error_if(t.error_code, None),
1393 }
1394 })
1395 .collect();
1396 TopicMetadata {
1397 controller_id: resp.controller_id,
1398 topics,
1399 }
1400}
1401
1402fn controller_endpoint(
1403 resp: &<MetadataRequest as krabka_protocol::ProtocolRequest>::Response,
1404) -> Option<String> {
1405 let id = resp.controller_id;
1406 resp.brokers
1407 .iter()
1408 .find(|b| b.node_id == id && !b.host.is_empty() && b.port > 0)
1409 .map(|b| format!("{}:{}", b.host, b.port))
1410}
1411
1412fn controller_requires_bootstrap_fallback(
1413 resp: &<MetadataRequest as krabka_protocol::ProtocolRequest>::Response,
1414) -> bool {
1415 resp.brokers
1416 .iter()
1417 .any(|broker| broker.node_id == resp.controller_id && broker.port <= 0)
1418}
1419
1420fn proto_uuid_to_opt(u: ProtoUuid) -> Option<Uuid> {
1421 if u == ProtoUuid::ZERO {
1422 None
1423 } else {
1424 Some(Uuid::from_bytes(u.0))
1425 }
1426}
1427
1428#[cfg(test)]
1429mod tests {
1430 use std::collections::BTreeMap;
1431
1432 use krabka_protocol::{
1433 UnknownTaggedFields,
1434 owned::metadata_response::{
1435 MetadataResponseBroker, MetadataResponsePartition, MetadataResponseTopic,
1436 },
1437 };
1438
1439 use super::*;
1440 use crate::retry::KAFKA_ADMIN_RETRY;
1441
1442 #[test]
1443 fn exact_assignment_request_preserves_replica_order() {
1444 let assignments = maplit::btreemap! {
1445 ("orders".into(), 3) => Some(vec![4, 2, 1]),
1446 };
1447 let request = build_partition_assignment_request(
1448 &assignments,
1449 krabka_units::secs(30),
1450 AlterPartitionReassignmentsOptions::default(),
1451 );
1452 assert2::assert!(
1453 request
1454 == AlterPartitionReassignmentsRequest {
1455 timeout_ms: 30_000,
1456 allow_replication_factor_change: true,
1457 topics: vec![ReassignableTopic {
1458 name: "orders".into(),
1459 partitions: vec![ReassignablePartition {
1460 partition_index: 3,
1461 replicas: Some(vec![4, 2, 1]),
1462 ..Default::default()
1463 }],
1464 ..Default::default()
1465 }],
1466 ..Default::default()
1467 }
1468 );
1469 }
1470
1471 #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
1476 async fn allow_replication_factor_change_reaches_the_wire_and_needs_v1_when_false() {
1477 use krabka_client_core::{ClientError, MockBroker};
1478 use krabka_protocol::owned::{
1479 alter_partition_reassignments_request,
1480 alter_partition_reassignments_response::AlterPartitionReassignmentsResponse,
1481 api_versions_request,
1482 };
1483
1484 use crate::partition_leaders::test_support::{
1485 ApiVersionRange, api_versions, decode_request, encode_response,
1486 };
1487
1488 for (name, max_version, allow, expected) in [
1491 ("plain call at v1", 1, None, Ok(vec![(1, true)])),
1492 ("default at v1", 1, Some(true), Ok(vec![(1, true)])),
1493 (
1494 "refuse a change at v1",
1495 1,
1496 Some(false),
1497 Ok(vec![(1, false)]),
1498 ),
1499 ("default at v0", 0, Some(true), Ok(vec![(0, true)])),
1500 ("refuse a change at v0", 0, Some(false), Err(())),
1501 ] {
1502 let seen = Arc::new(Mutex::new(Vec::new()));
1503 let requests = Arc::clone(&seen);
1504 let broker = MockBroker::start(move |api_key, version, _, body| match api_key {
1505 api_versions_request::API_KEY => Some(
1506 api_versions()
1507 .apis(vec![
1508 ApiVersionRange::builder()
1509 .api_key(alter_partition_reassignments_request::API_KEY)
1510 .max_version(max_version)
1511 .build(),
1512 ])
1513 .call(),
1514 ),
1515 alter_partition_reassignments_request::API_KEY => {
1516 let request: AlterPartitionReassignmentsRequest =
1517 decode_request(body, version, true);
1518 requests
1519 .lock()
1520 .expect("requests lock")
1521 .push((version, request.allow_replication_factor_change));
1522 Some(encode_response(
1523 &AlterPartitionReassignmentsResponse::default(),
1524 version,
1525 true,
1526 ))
1527 }
1528 _ => None,
1529 })
1530 .await;
1531 let mut admin = AdminClient::connect(&[broker.addr.to_string()])
1532 .await
1533 .expect("admin connects");
1534
1535 let assignments = maplit::btreemap! {
1536 ("orders".into(), 0) => Some(vec![1, 2]),
1537 };
1538 let timeout = krabka_units::secs(5);
1539 let outcome = match allow {
1540 None => {
1541 admin
1542 .alter_partition_assignments(&assignments, timeout)
1543 .await
1544 }
1545 Some(allow) => {
1546 admin
1547 .alter_partition_assignments_with(
1548 &assignments,
1549 timeout,
1550 AlterPartitionReassignmentsOptions {
1551 allow_replication_factor_change: allow,
1552 },
1553 )
1554 .await
1555 }
1556 };
1557 let result = outcome
1558 .map(|_| seen.lock().expect("requests lock").clone())
1559 .map_err(|error| {
1560 assert2::assert!(
1561 matches!(
1562 error,
1563 AdminError::Transport(ClientError::IncompatibleVersion { .. })
1564 ),
1565 "case {name}: {error:?}"
1566 );
1567 });
1568
1569 broker.stop();
1570 assert2::assert!(result == expected, "case {name}");
1571 }
1572 }
1573
1574 fn reassignment_metadata(assignments: &[&[i32]]) -> MetadataResponse {
1575 MetadataResponse {
1576 brokers: (1..=3)
1577 .map(|node_id| MetadataResponseBroker {
1578 node_id,
1579 ..Default::default()
1580 })
1581 .collect(),
1582 topics: vec![MetadataResponseTopic {
1583 name: Some("orders".into()),
1584 partitions: assignments
1585 .iter()
1586 .enumerate()
1587 .map(|(partition_index, replicas)| MetadataResponsePartition {
1588 partition_index: i32::try_from(partition_index)
1589 .expect("test partition index fits i32"),
1590 replica_nodes: replicas.to_vec(),
1591 ..Default::default()
1592 })
1593 .collect(),
1594 ..Default::default()
1595 }],
1596 ..Default::default()
1597 }
1598 }
1599
1600 #[test]
1601 fn replication_factor_reassignment_preserves_and_rotates_replicas() {
1602 let cases = [
1603 (
1604 "increase",
1605 reassignment_metadata(&[&[1], &[2]]),
1606 2,
1607 vec![(0, vec![1, 2]), (1, vec![2, 3])],
1608 ),
1609 (
1610 "decrease",
1611 reassignment_metadata(&[&[1, 2, 3], &[2, 3, 1]]),
1612 2,
1613 vec![(0, vec![1, 2]), (1, vec![2, 3])],
1614 ),
1615 ];
1616
1617 for (case, metadata, replication_factor, expected_partitions) in cases {
1618 let actual = build_replication_factor_reassignment(
1619 &metadata,
1620 &[1, 2, 3],
1621 "orders",
1622 replication_factor,
1623 krabka_units::secs(5),
1624 )
1625 .unwrap();
1626 let expected = Some(AlterPartitionReassignmentsRequest {
1627 timeout_ms: 5_000,
1628 allow_replication_factor_change: true,
1629 topics: vec![ReassignableTopic {
1630 name: "orders".into(),
1631 partitions: expected_partitions
1632 .into_iter()
1633 .map(|(partition_index, replicas)| ReassignablePartition {
1634 partition_index,
1635 replicas: Some(replicas),
1636 ..Default::default()
1637 })
1638 .collect(),
1639 ..Default::default()
1640 }],
1641 ..Default::default()
1642 });
1643 assert2::assert!(actual == expected, "case {case}");
1644 }
1645 }
1646
1647 #[test]
1648 fn replication_factor_reassignment_rejects_factor_above_broker_count() {
1649 let error = build_replication_factor_reassignment(
1650 &reassignment_metadata(&[&[1]]),
1651 &[1, 2, 3],
1652 "orders",
1653 4,
1654 krabka_units::secs(5),
1655 )
1656 .unwrap_err();
1657
1658 assert2::assert!(matches!(
1659 error,
1660 AdminError::Broker {
1661 api: "AlterPartitionReassignments",
1662 code: 38,
1663 name: "INVALID_REPLICATION_FACTOR",
1664 ..
1665 }
1666 ));
1667 }
1668
1669 #[test]
1670 fn replication_factor_reassignment_replaces_fenced_replica() {
1671 let actual = build_replication_factor_reassignment(
1672 &reassignment_metadata(&[&[1, 2]]),
1673 &[1, 3],
1674 "orders",
1675 2,
1676 krabka_units::secs(5),
1677 )
1678 .unwrap()
1679 .expect("fenced replica requires reassignment");
1680
1681 assert2::assert!(actual.topics[0].partitions[0].replicas == Some(vec![1, 3]));
1682 }
1683
1684 #[test]
1685 fn build_create_topics_one_spec() {
1686 let req = build_create_topics(
1687 &[CreateTopicSpec::builder()
1688 .name("foo")
1689 .partitions(3)
1690 .replicas(1)
1691 .configs(maplit::btreemap! {
1692 "retention.ms".to_string() => "60000".to_string(),
1693 })
1694 .build()],
1695 5_000,
1696 );
1697 assert2::assert!(
1698 req == CreateTopicsRequest {
1699 topics: vec![CreatableTopic {
1700 name: "foo".to_string(),
1701 num_partitions: 3,
1702 replication_factor: 1,
1703 assignments: vec![],
1704 configs: vec![CreatableTopicConfig {
1705 name: "retention.ms".to_string(),
1706 value: Some("60000".to_string()),
1707 unknown_tagged_fields: UnknownTaggedFields(vec![]),
1708 }],
1709 unknown_tagged_fields: UnknownTaggedFields(vec![]),
1710 }],
1711 timeout_ms: 5_000,
1712 validate_only: false,
1713 unknown_tagged_fields: UnknownTaggedFields(vec![]),
1714 }
1715 );
1716 }
1717
1718 #[test]
1722 fn replica_assignments_reach_the_wire_as_kafka_sends_them() {
1723 let topics = build_create_topics(
1724 &[CreateTopicSpec::builder()
1725 .name("placed")
1726 .partitions(-1)
1727 .replicas(-1)
1728 .replica_assignments(maplit::btreemap! {
1729 1 => vec![3, 1],
1730 0 => vec![1, 2],
1731 })
1732 .build()],
1733 5_000,
1734 );
1735 assert2::assert!(
1736 topics.topics
1737 == vec![CreatableTopic {
1738 name: "placed".into(),
1739 num_partitions: -1,
1740 replication_factor: -1,
1741 assignments: vec![
1742 CreatableReplicaAssignment {
1743 partition_index: 0,
1744 broker_ids: vec![1, 2],
1745 ..Default::default()
1746 },
1747 CreatableReplicaAssignment {
1748 partition_index: 1,
1749 broker_ids: vec![3, 1],
1750 ..Default::default()
1751 },
1752 ],
1753 ..Default::default()
1754 }]
1755 );
1756
1757 for (name, assignments, expected) in [
1758 ("broker placement", None, None),
1759 (
1760 "explicit placement",
1761 Some(vec![vec![2, 3], vec![3, 1]]),
1762 Some(vec![
1763 CreatePartitionsAssignment {
1764 broker_ids: vec![2, 3],
1765 ..Default::default()
1766 },
1767 CreatePartitionsAssignment {
1768 broker_ids: vec![3, 1],
1769 ..Default::default()
1770 },
1771 ]),
1772 ),
1773 ] {
1774 let request = build_create_partitions(
1775 &[CreatePartitionsOp {
1776 name: "orders".into(),
1777 new_total_count: 4,
1778 assignments,
1779 }],
1780 5_000,
1781 );
1782 assert2::assert!(
1783 request.topics
1784 == vec![CreatePartitionsTopic {
1785 name: "orders".into(),
1786 count: 4,
1787 assignments: expected,
1788 ..Default::default()
1789 }],
1790 "case {name}"
1791 );
1792 }
1793 }
1794
1795 #[test]
1796 fn build_delete_records_groups_partitions_by_topic() {
1797 let req = build_delete_records(
1798 &[
1799 DeleteRecordsOp {
1800 topic: "beta".to_string(),
1801 partition: 1,
1802 offset: -1,
1803 },
1804 DeleteRecordsOp {
1805 topic: "alpha".to_string(),
1806 partition: 0,
1807 offset: 50,
1808 },
1809 DeleteRecordsOp {
1810 topic: "alpha".to_string(),
1811 partition: 2,
1812 offset: 75,
1813 },
1814 ],
1815 krabka_units::secs(5),
1816 );
1817
1818 assert2::assert!(
1819 req == DeleteRecordsRequest {
1820 topics: vec![
1821 DeleteRecordsTopic {
1822 name: "alpha".to_string(),
1823 partitions: vec![
1824 DeleteRecordsPartition {
1825 partition_index: 0,
1826 offset: 50,
1827 unknown_tagged_fields: UnknownTaggedFields(vec![]),
1828 },
1829 DeleteRecordsPartition {
1830 partition_index: 2,
1831 offset: 75,
1832 unknown_tagged_fields: UnknownTaggedFields(vec![]),
1833 },
1834 ],
1835 unknown_tagged_fields: UnknownTaggedFields(vec![]),
1836 },
1837 DeleteRecordsTopic {
1838 name: "beta".to_string(),
1839 partitions: vec![DeleteRecordsPartition {
1840 partition_index: 1,
1841 offset: -1,
1842 unknown_tagged_fields: UnknownTaggedFields(vec![]),
1843 }],
1844 unknown_tagged_fields: UnknownTaggedFields(vec![]),
1845 },
1846 ],
1847 timeout_ms: 5_000,
1848 unknown_tagged_fields: UnknownTaggedFields(vec![]),
1849 }
1850 );
1851 }
1852
1853 #[test]
1854 fn parse_delete_records_flattens_partition_errors() {
1855 use krabka_protocol::owned::delete_records_response::{
1856 DeleteRecordsPartitionResult, DeleteRecordsResponse, DeleteRecordsTopicResult,
1857 };
1858
1859 let resp = DeleteRecordsResponse {
1860 topics: vec![DeleteRecordsTopicResult {
1861 name: "wal".to_string(),
1862 partitions: vec![
1863 DeleteRecordsPartitionResult {
1864 partition_index: 0,
1865 low_watermark: 50,
1866 error_code: 0,
1867 ..Default::default()
1868 },
1869 DeleteRecordsPartitionResult {
1870 partition_index: 1,
1871 low_watermark: -1,
1872 error_code: 1,
1873 ..Default::default()
1874 },
1875 ],
1876 ..Default::default()
1877 }],
1878 ..Default::default()
1879 };
1880
1881 let outcomes = parse_delete_records(resp);
1882 assert_eq!(
1883 outcomes,
1884 vec![
1885 DeleteRecordsOutcome {
1886 topic: "wal".to_string(),
1887 partition: 0,
1888 error_code: 0,
1889 low_watermark: 50,
1890 },
1891 DeleteRecordsOutcome {
1892 topic: "wal".to_string(),
1893 partition: 1,
1894 error_code: 1,
1895 low_watermark: -1,
1896 },
1897 ]
1898 );
1899 }
1900
1901 #[test]
1913 fn not_controller_codes_match_kafka() {
1914 let cases = [
1915 ((NOT_CONTROLLER, false), true),
1916 ((NOT_CONTROLLER, true), true),
1917 ((NOT_LEADER_OR_FOLLOWER, false), false),
1918 ((NOT_LEADER_OR_FOLLOWER, true), true),
1919 ((36, true), false),
1920 ((THROTTLING_QUOTA_EXCEEDED, false), false),
1921 ((0, false), false),
1922 ];
1923 let actual = cases
1924 .map(|((code, bootstrap), _)| ((code, bootstrap), is_not_controller(code, bootstrap)));
1925 assert2::assert!(actual == cases);
1926 }
1927
1928 #[test]
1937 fn controller_endpoint_picks_broker_with_matching_node_id() {
1938 use metadata_response::{MetadataResponse, MetadataResponseBroker};
1939 let resp = MetadataResponse {
1940 controller_id: 2,
1941 brokers: vec![
1942 MetadataResponseBroker {
1943 node_id: 1,
1944 host: "h1".into(),
1945 port: 9092,
1946 rack: None,
1947 ..Default::default()
1948 },
1949 MetadataResponseBroker {
1950 node_id: 2,
1951 host: "h2".into(),
1952 port: 9093,
1953 rack: None,
1954 ..Default::default()
1955 },
1956 ],
1957 ..Default::default()
1958 };
1959 let addr = controller_endpoint(&resp);
1960 assert2::assert!(addr.as_deref() == Some("h2:9093"));
1961 }
1962
1963 #[test]
1967 fn controller_endpoint_returns_none_when_no_match() {
1968 use metadata_response::{MetadataResponse, MetadataResponseBroker};
1969 let resp = MetadataResponse {
1970 controller_id: 99,
1971 brokers: vec![MetadataResponseBroker {
1972 node_id: 1,
1973 host: "h1".into(),
1974 port: 9092,
1975 rack: None,
1976 ..Default::default()
1977 }],
1978 ..Default::default()
1979 };
1980 assert2::assert!(controller_endpoint(&resp).is_none());
1981 }
1982
1983 #[test]
1984 fn controller_endpoint_rejects_non_dialable_ephemeral_port() {
1985 use metadata_response::{MetadataResponse, MetadataResponseBroker};
1986 let resp = MetadataResponse {
1987 controller_id: 1,
1988 brokers: vec![MetadataResponseBroker {
1989 node_id: 1,
1990 host: "127.0.0.1".into(),
1991 port: 0,
1992 ..Default::default()
1993 }],
1994 ..Default::default()
1995 };
1996
1997 assert2::assert!(controller_endpoint(&resp).is_none());
1998 assert2::assert!(controller_requires_bootstrap_fallback(&resp));
1999 }
2000
2001 #[test]
2010 fn parse_metadata_carries_through_per_topic_errors() {
2011 use metadata_response::{MetadataResponse, MetadataResponseTopic};
2012 let resp = MetadataResponse {
2013 topics: vec![
2014 MetadataResponseTopic {
2015 name: Some("ok-topic".into()),
2016 error_code: 0,
2017 ..Default::default()
2018 },
2019 MetadataResponseTopic {
2020 name: Some("missing".into()),
2021 error_code: 3, ..Default::default()
2023 },
2024 ],
2025 ..Default::default()
2026 };
2027 let md = parse_metadata(resp);
2028 assert2::assert!(
2029 md == TopicMetadata {
2030 controller_id: -1,
2031 topics: vec![
2032 TopicMetadataEntry {
2033 name: "ok-topic".to_string(),
2034 topic_id: None,
2035 partition_count: 0,
2036 replication_factor: 0,
2037 error: None,
2038 },
2039 TopicMetadataEntry {
2040 name: "missing".to_string(),
2041 topic_id: None,
2042 partition_count: 0,
2043 replication_factor: 0,
2044 error: Some(KafkaError {
2045 code: 3,
2046 name: "UNKNOWN_TOPIC_OR_PARTITION",
2047 message: None,
2048 }),
2049 },
2050 ],
2051 }
2052 );
2053 }
2054
2055 #[test]
2056 fn parse_metadata_zero_uuid_becomes_none() {
2057 use metadata_response::{MetadataResponse, MetadataResponseTopic};
2058 let resp = MetadataResponse {
2059 topics: vec![MetadataResponseTopic {
2060 name: Some("foo".into()),
2061 topic_id: ProtoUuid::ZERO,
2062 ..Default::default()
2063 }],
2064 ..Default::default()
2065 };
2066 let md = parse_metadata(resp);
2067 assert2::assert!(md.topics[0].topic_id.is_none());
2068 }
2069
2070 #[test]
2071 fn parse_metadata_computes_partition_count_and_replication_factor() {
2072 use metadata_response::{
2073 MetadataResponse, MetadataResponsePartition, MetadataResponseTopic,
2074 };
2075 let part = MetadataResponsePartition {
2076 replica_nodes: vec![1, 2],
2077 ..Default::default()
2078 };
2079 let resp = MetadataResponse {
2080 topics: vec![MetadataResponseTopic {
2081 name: Some("foo".into()),
2082 partitions: vec![part.clone(), part.clone(), part],
2083 ..Default::default()
2084 }],
2085 ..Default::default()
2086 };
2087 let md = parse_metadata(resp);
2088 assert2::assert!(
2089 (
2090 md.topics[0].partition_count,
2091 md.topics[0].replication_factor
2092 ) == (3, 2)
2093 );
2094 }
2095
2096 #[test]
2099 fn parse_create_topics_per_topic_error() {
2100 use krabka_protocol::owned::create_topics_response::{
2101 CreatableTopicResult, CreateTopicsResponse,
2102 };
2103 let resp = CreateTopicsResponse {
2104 topics: vec![
2105 CreatableTopicResult {
2106 name: "ok".into(),
2107 topic_id: ProtoUuid([7; 16]),
2108 error_code: 0,
2109 error_message: None,
2110 ..Default::default()
2111 },
2112 CreatableTopicResult {
2113 name: "dup".into(),
2114 error_code: 36, error_message: Some("already there".into()),
2116 ..Default::default()
2117 },
2118 CreatableTopicResult {
2119 name: "throttled".into(),
2120 error_code: THROTTLING_QUOTA_EXCEEDED,
2121 error_message: None,
2122 ..Default::default()
2123 },
2124 ],
2125 throttle_time_ms: 250,
2126 ..Default::default()
2127 };
2128 let outcomes = parse_create_topics(resp);
2129 assert2::assert!(
2130 outcomes
2131 == vec![
2132 CreateTopicOutcome {
2133 name: "ok".to_string(),
2134 topic_id: Some(Uuid::from_bytes([7; 16])),
2136 error: None,
2137 throttle_time: None,
2138 },
2139 CreateTopicOutcome {
2140 name: "dup".to_string(),
2141 topic_id: None,
2142 error: Some(KafkaError {
2143 code: 36,
2144 name: "TOPIC_ALREADY_EXISTS",
2145 message: Some("already there".to_string()),
2146 }),
2147 throttle_time: None,
2148 },
2149 CreateTopicOutcome {
2150 name: "throttled".to_string(),
2151 topic_id: None,
2152 error: Some(KafkaError {
2153 code: THROTTLING_QUOTA_EXCEEDED,
2154 name: "THROTTLING_QUOTA_EXCEEDED",
2155 message: None,
2156 }),
2157 throttle_time: Some(Time::from_millis(250)),
2158 },
2159 ]
2160 );
2161 }
2162
2163 #[test]
2166 fn parse_delete_topics_handles_missing_name() {
2167 use krabka_protocol::owned::delete_topics_response::{
2168 DeletableTopicResult, DeleteTopicsResponse,
2169 };
2170 let resp = DeleteTopicsResponse {
2171 responses: vec![
2172 DeletableTopicResult {
2173 name: None,
2174 error_code: 0,
2175 ..Default::default()
2176 },
2177 DeletableTopicResult {
2178 name: Some("named".into()),
2179 error_code: 3,
2180 error_message: Some("nope".into()),
2181 ..Default::default()
2182 },
2183 ],
2184 ..Default::default()
2185 };
2186 let outs = parse_delete_topics(resp);
2187 assert2::assert!(
2188 outs == vec![
2189 DeleteTopicOutcome {
2190 name: String::new(),
2193 error: None,
2194 throttle_time: None,
2195 },
2196 DeleteTopicOutcome {
2197 name: "named".to_string(),
2198 error: Some(KafkaError {
2199 code: 3,
2200 name: "UNKNOWN_TOPIC_OR_PARTITION",
2201 message: Some("nope".to_string()),
2202 }),
2203 throttle_time: None,
2204 },
2205 ]
2206 );
2207 }
2208
2209 #[test]
2212 fn parse_create_partitions_per_topic_error() {
2213 use krabka_protocol::owned::create_partitions_response::{
2214 CreatePartitionsResponse, CreatePartitionsTopicResult,
2215 };
2216 let resp = CreatePartitionsResponse {
2217 results: vec![
2218 CreatePartitionsTopicResult {
2219 name: "ok".into(),
2220 error_code: 0,
2221 error_message: None,
2222 ..Default::default()
2223 },
2224 CreatePartitionsTopicResult {
2225 name: "bad".into(),
2226 error_code: 37,
2227 error_message: Some("bad count".into()),
2228 ..Default::default()
2229 },
2230 ],
2231 ..Default::default()
2232 };
2233 let outs = parse_create_partitions(resp);
2234 assert2::assert!(
2235 outs == vec![
2236 CreatePartitionsOutcome {
2237 name: "ok".to_string(),
2238 error: None,
2239 throttle_time: None,
2240 },
2241 CreatePartitionsOutcome {
2242 name: "bad".to_string(),
2243 error: Some(KafkaError {
2244 code: 37,
2245 name: "INVALID_PARTITIONS",
2246 message: Some("bad count".to_string()),
2247 }),
2248 throttle_time: None,
2249 },
2250 ]
2251 );
2252 }
2253
2254 use std::sync::{Arc, Mutex};
2257
2258 use bytes::{Buf, BytesMut};
2259 use krabka_client_core::{MockBroker, MockReply};
2260 use krabka_protocol::{
2261 Decode, Encode,
2262 owned::{
2263 api_versions_request,
2264 api_versions_response::{ApiVersion, ApiVersionsResponse},
2265 create_partitions_request,
2266 create_partitions_response::{CreatePartitionsResponse, CreatePartitionsTopicResult},
2267 create_topics_request,
2268 create_topics_response::{CreatableTopicResult, CreateTopicsResponse},
2269 delete_topics_request,
2270 delete_topics_response::{DeletableTopicResult, DeleteTopicsResponse},
2271 metadata_request,
2272 },
2273 };
2274
2275 #[derive(Clone, Copy, Debug)]
2277 enum Mutation {
2278 CreateTopics,
2279 DeleteTopics,
2280 CreatePartitions,
2281 }
2282
2283 #[derive(Clone, Debug)]
2287 struct Answer {
2288 codes: Vec<(&'static str, i16)>,
2289 throttle_time_ms: i32,
2290 silent: bool,
2292 closed: bool,
2294 }
2295
2296 fn answer(codes: &[(&'static str, i16)]) -> Answer {
2297 Answer {
2298 codes: codes.to_vec(),
2299 throttle_time_ms: 0,
2300 silent: false,
2301 closed: false,
2302 }
2303 }
2304
2305 fn throttled(codes: &[(&'static str, i16)], throttle_time_ms: i32) -> Answer {
2306 Answer {
2307 codes: codes.to_vec(),
2308 throttle_time_ms,
2309 silent: false,
2310 closed: false,
2311 }
2312 }
2313
2314 fn silent() -> Answer {
2315 Answer {
2316 codes: Vec::new(),
2317 throttle_time_ms: 0,
2318 silent: true,
2319 closed: false,
2320 }
2321 }
2322
2323 fn closed() -> Answer {
2324 Answer {
2325 codes: Vec::new(),
2326 throttle_time_ms: 0,
2327 silent: false,
2328 closed: true,
2329 }
2330 }
2331
2332 type SentRequest = (Vec<String>, i32);
2334
2335 type MutationCase = (
2338 &'static str,
2339 Vec<Answer>,
2340 Duration,
2341 TopicMutationOptions,
2342 Vec<Vec<String>>,
2343 Vec<Observed>,
2344 );
2345
2346 type Observed = (String, i16, bool);
2349
2350 fn body(response: &impl Encode, version: i16, flexible: bool) -> Vec<u8> {
2351 let mut bytes = BytesMut::new();
2352 if flexible {
2353 bytes.extend_from_slice(&[0]);
2354 }
2355 response
2356 .encode(&mut bytes, version)
2357 .expect("response encodes");
2358 bytes.to_vec()
2359 }
2360
2361 fn decode_body<R: for<'de> Decode<'de>>(mut body: &[u8], version: i16, flexible: bool) -> R {
2362 let client_id_len = body.get_i16();
2363 body.advance(usize::try_from(client_id_len).expect("client id length"));
2364 if flexible {
2365 body.advance(1);
2366 }
2367 R::decode(&mut body, version).expect("request decodes")
2368 }
2369
2370 async fn scripted_controller(
2374 script: Vec<Answer>,
2375 requests: Arc<Mutex<Vec<SentRequest>>>,
2376 ) -> MockBroker {
2377 let port = Arc::new(std::sync::atomic::AtomicU16::new(0));
2378 let handler_port = Arc::clone(&port);
2379 let mut next = 0_usize;
2380 let close = Arc::new(std::sync::atomic::AtomicBool::new(false));
2381 let handler_close = Arc::clone(&close);
2382 let mut handler = move |api_key: i16, version: i16, request: &[u8]| {
2383 let api_version = |api_key, max_version| ApiVersion {
2384 api_key,
2385 min_version: 0,
2386 max_version,
2387 ..Default::default()
2388 };
2389 let mut take = |names: Vec<String>, timeout_ms: i32| {
2390 requests
2391 .lock()
2392 .expect("requests lock")
2393 .push((names, timeout_ms));
2394 let entry = script[next.min(script.len() - 1)].clone();
2395 next += 1;
2396 handler_close.store(entry.closed, std::sync::atomic::Ordering::SeqCst);
2397 entry
2398 };
2399 match api_key {
2400 api_versions_request::API_KEY => Some(body(
2401 &ApiVersionsResponse {
2402 api_keys: vec![
2403 api_version(api_versions_request::API_KEY, 0),
2404 api_version(metadata_request::API_KEY, 12),
2405 api_version(create_topics_request::API_KEY, 7),
2406 api_version(delete_topics_request::API_KEY, 6),
2407 api_version(create_partitions_request::API_KEY, 3),
2408 ],
2409 ..Default::default()
2410 },
2411 0,
2412 false,
2413 )),
2414 metadata_request::API_KEY => Some(body(
2415 &MetadataResponse {
2416 controller_id: 1,
2417 brokers: vec![MetadataResponseBroker {
2418 node_id: 1,
2419 host: "127.0.0.1".into(),
2420 port: i32::from(handler_port.load(std::sync::atomic::Ordering::SeqCst)),
2421 ..Default::default()
2422 }],
2423 ..Default::default()
2424 },
2425 version,
2426 version >= metadata_request::FLEXIBLE_MIN,
2427 )),
2428 create_topics_request::API_KEY => {
2429 let flexible = version >= create_topics_request::FLEXIBLE_MIN;
2430 let request: CreateTopicsRequest = decode_body(request, version, flexible);
2431 let entry = take(
2432 request
2433 .topics
2434 .iter()
2435 .map(|topic| topic.name.clone())
2436 .collect(),
2437 request.timeout_ms,
2438 );
2439 if entry.silent {
2440 return None;
2441 }
2442 Some(body(
2443 &CreateTopicsResponse {
2444 throttle_time_ms: entry.throttle_time_ms,
2445 topics: entry
2446 .codes
2447 .iter()
2448 .map(|(name, code)| CreatableTopicResult {
2449 name: (*name).to_owned(),
2450 error_code: *code,
2451 ..Default::default()
2452 })
2453 .collect(),
2454 ..Default::default()
2455 },
2456 version,
2457 flexible,
2458 ))
2459 }
2460 delete_topics_request::API_KEY => {
2461 let flexible = version >= delete_topics_request::FLEXIBLE_MIN;
2462 let request: DeleteTopicsRequest = decode_body(request, version, flexible);
2463 let entry = take(
2464 request
2465 .topics
2466 .iter()
2467 .map(|topic| topic.name.clone().unwrap_or_default())
2468 .collect(),
2469 request.timeout_ms,
2470 );
2471 if entry.silent {
2472 return None;
2473 }
2474 Some(body(
2475 &DeleteTopicsResponse {
2476 throttle_time_ms: entry.throttle_time_ms,
2477 responses: entry
2478 .codes
2479 .iter()
2480 .map(|(name, code)| DeletableTopicResult {
2481 name: Some((*name).to_owned()),
2482 error_code: *code,
2483 ..Default::default()
2484 })
2485 .collect(),
2486 ..Default::default()
2487 },
2488 version,
2489 flexible,
2490 ))
2491 }
2492 create_partitions_request::API_KEY => {
2493 let flexible = version >= create_partitions_request::FLEXIBLE_MIN;
2494 let request: CreatePartitionsRequest = decode_body(request, version, flexible);
2495 let entry = take(
2496 request
2497 .topics
2498 .iter()
2499 .map(|topic| topic.name.clone())
2500 .collect(),
2501 request.timeout_ms,
2502 );
2503 if entry.silent {
2504 return None;
2505 }
2506 Some(body(
2507 &CreatePartitionsResponse {
2508 throttle_time_ms: entry.throttle_time_ms,
2509 results: entry
2510 .codes
2511 .iter()
2512 .map(|(name, code)| CreatePartitionsTopicResult {
2513 name: (*name).to_owned(),
2514 error_code: *code,
2515 ..Default::default()
2516 })
2517 .collect(),
2518 ..Default::default()
2519 },
2520 version,
2521 flexible,
2522 ))
2523 }
2524 _ => None,
2525 }
2526 };
2527 let broker = MockBroker::start_with_replies(move |api_key, version, _, request| {
2528 let reply = handler(api_key, version, request);
2529 if close.swap(false, std::sync::atomic::Ordering::SeqCst) {
2530 return MockReply::Close;
2531 }
2532 reply.map_or(MockReply::Silent, MockReply::Respond)
2533 })
2534 .await;
2535 port.store(broker.addr.port(), std::sync::atomic::Ordering::SeqCst);
2536 broker
2537 }
2538
2539 fn observe<T: TopicMutationOutcome>(outcomes: Vec<T>) -> Vec<Observed> {
2540 outcomes
2541 .into_iter()
2542 .map(|outcome| {
2543 (
2544 outcome.topic().to_owned(),
2545 outcome.error_code(),
2546 !outcome.throttle_duration().is_zero(),
2547 )
2548 })
2549 .collect()
2550 }
2551
2552 async fn run_mutation(
2553 admin: &mut AdminClient,
2554 mutation: Mutation,
2555 topics: &[&str],
2556 options: TopicMutationOptions,
2557 policy: RetryPolicy,
2558 ) -> Vec<Observed> {
2559 match mutation {
2560 Mutation::CreateTopics => {
2561 let specs = topics
2562 .iter()
2563 .map(|name| {
2564 CreateTopicSpec::builder()
2565 .name(*name)
2566 .partitions(1)
2567 .replicas(1)
2568 .build()
2569 })
2570 .collect::<Vec<_>>();
2571 observe(
2572 admin
2573 .create_topics_with_retry(&specs, options, policy)
2574 .await
2575 .expect("create_topics"),
2576 )
2577 }
2578 Mutation::DeleteTopics => observe(
2579 admin
2580 .delete_topics_with_retry(topics, options, policy)
2581 .await
2582 .expect("delete_topics"),
2583 ),
2584 Mutation::CreatePartitions => {
2585 let ops = topics
2586 .iter()
2587 .map(|name| CreatePartitionsOp {
2588 name: (*name).to_owned(),
2589 new_total_count: 2,
2590 ..Default::default()
2591 })
2592 .collect::<Vec<_>>();
2593 observe(
2594 admin
2595 .create_partitions_with_retry(&ops, options, policy)
2596 .await
2597 .expect("create_partitions"),
2598 )
2599 }
2600 }
2601 }
2602
2603 #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
2611 async fn topic_mutations_retry_not_controller_and_quota_as_kafka_does() {
2612 const LONG: Duration = Duration::from_secs(5);
2613 const NOW: Duration = Duration::from_millis(500);
2616 let ok = |name: &str| (name.to_owned(), 0, false);
2617 let failed = |name: &str, code| (name.to_owned(), code, false);
2618 let quota = |name: &str| (name.to_owned(), THROTTLING_QUOTA_EXCEEDED, true);
2619 let names = |lists: &[&[&str]]| {
2620 lists
2621 .iter()
2622 .map(|list| {
2623 list.iter()
2624 .map(|name| (*name).to_owned())
2625 .collect::<Vec<_>>()
2626 })
2627 .collect::<Vec<_>>()
2628 };
2629 let retry_quota = TopicMutationOptions::default();
2630 let no_quota_retry = TopicMutationOptions {
2631 retry_on_quota_violation: false,
2632 ..TopicMutationOptions::default()
2633 };
2634 let cases: Vec<MutationCase> = vec![
2635 (
2636 "no error",
2637 vec![answer(&[("a", 0), ("b", 0)])],
2638 LONG,
2639 retry_quota,
2640 names(&[&["a", "b"]]),
2641 vec![ok("a"), ok("b")],
2642 ),
2643 (
2644 "not controller twice, then success",
2645 vec![
2646 answer(&[("a", 41), ("b", 41)]),
2647 answer(&[("a", 41), ("b", 41)]),
2648 answer(&[("a", 0), ("b", 0)]),
2649 ],
2650 LONG,
2651 retry_quota,
2652 names(&[&["a", "b"], &["a", "b"], &["a", "b"]]),
2653 vec![ok("a"), ok("b")],
2654 ),
2655 (
2656 "not controller for one topic sends every topic again",
2657 vec![
2658 answer(&[("a", 0), ("b", 41)]),
2659 answer(&[("a", 0), ("b", 0)]),
2660 ],
2661 LONG,
2662 retry_quota,
2663 names(&[&["a", "b"], &["a", "b"]]),
2664 vec![ok("a"), ok("b")],
2665 ),
2666 (
2667 "quota for one topic sends only that topic again",
2668 vec![answer(&[("a", 0), ("b", 89)]), answer(&[("b", 0)])],
2669 LONG,
2670 retry_quota,
2671 names(&[&["a", "b"], &["b"]]),
2672 vec![ok("a"), ok("b")],
2673 ),
2674 (
2675 "quota with retry off is final",
2676 vec![throttled(&[("a", 89), ("b", 0)], 250)],
2677 LONG,
2678 no_quota_retry,
2679 names(&[&["a", "b"]]),
2680 vec![quota("a"), ok("b")],
2681 ),
2682 (
2683 "not controller past the deadline times out",
2684 vec![answer(&[("a", 41), ("b", 41)])],
2685 NOW,
2686 retry_quota,
2687 names(&[&["a", "b"]]),
2688 vec![failed("a", 7), failed("b", 7)],
2689 ),
2690 (
2691 "quota past the deadline keeps the quota error",
2692 vec![throttled(&[("a", 89), ("b", 36)], 60_000)],
2693 Duration::from_millis(100),
2694 retry_quota,
2695 names(&[&["a", "b"]]),
2696 vec![quota("a"), failed("b", 36)],
2697 ),
2698 (
2699 "a silent controller stops the call at the deadline",
2700 vec![silent()],
2701 Duration::from_millis(300),
2702 retry_quota,
2703 names(&[&["a", "b"]]),
2704 vec![failed("a", 7), failed("b", 7)],
2705 ),
2706 (
2707 "a lost connection is retried after the backoff",
2708 vec![closed(), closed(), closed(), answer(&[("a", 0), ("b", 0)])],
2709 LONG,
2710 retry_quota,
2711 names(&[&["a", "b"], &["a", "b"], &["a", "b"], &["a", "b"]]),
2712 vec![ok("a"), ok("b")],
2713 ),
2714 (
2715 "a topic with no result fails",
2716 vec![answer(&[("a", 0)])],
2717 LONG,
2718 retry_quota,
2719 names(&[&["a", "b"]]),
2720 vec![ok("a"), failed("b", -1)],
2721 ),
2722 ];
2723 for mutation in [
2724 Mutation::CreateTopics,
2725 Mutation::DeleteTopics,
2726 Mutation::CreatePartitions,
2727 ] {
2728 for (name, script, timeout, options, expected_requests, expected) in cases.clone() {
2729 let requests = Arc::new(Mutex::new(Vec::new()));
2730 let controller = scripted_controller(script, Arc::clone(&requests)).await;
2731 let mut admin = AdminClient::connect(&[controller.addr.to_string()])
2732 .await
2733 .expect("admin connects");
2734 let backoff = if timeout == LONG {
2735 Duration::from_millis(1)
2736 } else {
2737 timeout
2738 };
2739 let policy = RetryPolicy {
2740 timeout,
2741 initial_backoff: backoff,
2742 max_backoff: backoff,
2743 jitter: 0.0,
2744 max_retries: u32::MAX,
2745 };
2746
2747 let started = tokio::time::Instant::now();
2748 let observed =
2749 run_mutation(&mut admin, mutation, &["a", "b"], options, policy).await;
2750 let within_deadline = started.elapsed() < timeout + Duration::from_secs(2);
2751
2752 controller.stop();
2753 let requests = requests.lock().expect("requests lock").clone();
2754 let timeouts_within_deadline = requests.iter().all(|(_, timeout_ms)| {
2755 u128::try_from(*timeout_ms).is_ok_and(|ms| ms <= timeout.as_millis())
2756 });
2757 let sent = requests
2758 .into_iter()
2759 .map(|(names, _)| names)
2760 .collect::<Vec<_>>();
2761 assert2::assert!(
2762 (sent, observed, timeouts_within_deadline, within_deadline)
2763 == (expected_requests, expected, true, true),
2764 "{mutation:?}: {name}"
2765 );
2766 }
2767 }
2768 }
2769
2770 #[tokio::test(flavor = "multi_thread", worker_threads = 2)]
2773 async fn quota_error_at_the_deadline_carries_the_remaining_throttle_time() {
2774 let requests = Arc::new(Mutex::new(Vec::new()));
2775 let controller =
2776 scripted_controller(vec![throttled(&[("a", 89)], 60_000)], Arc::clone(&requests)).await;
2777 let mut admin = AdminClient::connect(&[controller.addr.to_string()])
2778 .await
2779 .expect("admin connects");
2780 let policy = RetryPolicy {
2781 timeout: Duration::from_millis(100),
2782 ..KAFKA_ADMIN_RETRY
2783 };
2784
2785 let outcomes = admin
2786 .delete_topics_with_retry(&["a"], TopicMutationOptions::default(), policy)
2787 .await
2788 .expect("delete_topics");
2789
2790 controller.stop();
2791 let [outcome] = outcomes.as_slice() else {
2792 panic!("one outcome expected, got {outcomes:?}");
2793 };
2794 let remaining = outcome.throttle_duration();
2795 assert2::assert!(
2796 (
2797 outcome.error.as_ref().map(|error| error.code),
2798 remaining > Duration::from_secs(59),
2799 remaining < Duration::from_mins(1),
2800 ) == (Some(THROTTLING_QUOTA_EXCEEDED), true, true)
2801 );
2802 }
2803
2804 fn assert_send<T: Send>(_: T) {}
2805
2806 #[test]
2808 fn controller_call_futures_are_send() {
2809 let _ = |admin: &mut AdminClient, specs: &[CreateTopicSpec], names: &[&str]| {
2810 assert_send(admin.create_topics(specs, TopicMutationOptions::default()));
2811 assert_send(admin.delete_topics(names, TopicMutationOptions::default()));
2812 assert_send(admin.create_partitions(&[], TopicMutationOptions::default()));
2813 assert_send(admin.unregister_broker(1));
2814 assert_send(admin.update_features(
2815 &BTreeMap::new(),
2816 crate::UpdateFeaturesOptions::builder().build(),
2817 ));
2818 assert_send(admin.alter_partition_assignments(&BTreeMap::new(), krabka_units::secs(1)));
2819 assert_send(admin.reconcile_topic_replication_factor("t", 1, krabka_units::secs(1)));
2820 };
2821 }
2822}