1
- use std:: time:: Duration ;
1
+ use std:: { collections :: HashMap , time:: Duration } ;
2
2
3
3
use rdkafka:: {
4
4
admin:: { AdminClient , AdminOptions , NewTopic , TopicReplication } ,
@@ -7,7 +7,7 @@ use rdkafka::{
7
7
error:: KafkaError ,
8
8
types:: RDKafkaErrorCode ,
9
9
} ;
10
- use talos_common_utils :: env_var_with_defaults ;
10
+
11
11
use talos_rdkafka_utils:: kafka_config:: KafkaConfig ;
12
12
use thiserror:: Error as ThisError ;
13
13
@@ -24,26 +24,45 @@ pub enum KafkaDeployError {
24
24
KafkaError ( #[ from] KafkaError ) ,
25
25
}
26
26
27
- pub async fn create_topic ( ) -> Result < KafkaDeployStatus , KafkaDeployError > {
28
- let kafka_config = KafkaConfig :: from_env ( None ) ;
29
- println ! ( "kafka configs received from env... {kafka_config:#?}" ) ;
27
+ #[ derive( Debug , Clone ) ]
28
+ pub struct CreateTopicConfigs < ' a > {
29
+ /// topic to create.
30
+ pub topic : String ,
31
+ /// Topic specific configs.
32
+ ///
33
+ /// see: https://docs.confluent.io/platform/current/installation/configuration/topic-configs.html
34
+ pub config : HashMap < & ' a str , & ' a str > ,
35
+ /// Replication count for partitions in topic. Defaults to 3.
36
+ pub replication_factor : Option < i32 > ,
37
+ /// Number of paritions for the topic. Defaults to 1.
38
+ pub num_partitions : Option < i32 > ,
39
+ }
40
+
41
+ const DEFAULT_REPLICATION_FACTOR : i32 = 3 ;
42
+ const DEFAULT_NUM_PARTITIONS : i32 = 3 ;
43
+
44
+ pub async fn create_topic ( kafka_config : & KafkaConfig , topic_configs : CreateTopicConfigs < ' _ > ) -> Result < KafkaDeployStatus , KafkaDeployError > {
45
+ println ! ( "kafka brokers = {:?} and usename = {}" , kafka_config. brokers, kafka_config. username) ;
46
+ println ! ( "topic configs received from env = {topic_configs:#?}" ) ;
30
47
let consumer: StreamConsumer = kafka_config. build_consumer_config ( ) . create ( ) ?;
31
48
32
- let kafka_certification_topic = kafka_config. topic . to_string ( ) ;
33
- let timeout = Duration :: from_secs ( 1 ) ;
49
+ let timeout = Duration :: from_secs ( 5 ) ;
34
50
let metadata = consumer
35
- . fetch_metadata ( Some ( & kafka_certification_topic ) , timeout)
51
+ . fetch_metadata ( Some ( & topic_configs . topic ) , timeout)
36
52
. expect ( "Fetching topic metadata failed" ) ;
37
53
38
54
if !metadata. topics ( ) . is_empty ( ) && !metadata. topics ( ) [ 0 ] . partitions ( ) . is_empty ( ) {
39
55
Ok ( KafkaDeployStatus :: TopicExists )
40
56
} else {
41
57
println ! ( "Topic does not exist, creating..." ) ;
58
+
59
+ let config: Vec < ( & str , & str ) > = topic_configs. config . into_iter ( ) . collect ( ) ;
60
+
42
61
let topic = NewTopic {
43
- name : & kafka_certification_topic ,
44
- num_partitions : env_var_with_defaults ! ( "KAFKA_CREATE_TOPIC_PARTITIONS" , i32 , 1 ) ,
45
- replication : TopicReplication :: Fixed ( 1 ) ,
46
- config : vec ! [ ( "message.timestamp.type" , "LogAppendTime" ) ] ,
62
+ name : & topic_configs . topic ,
63
+ num_partitions : topic_configs . num_partitions . unwrap_or ( DEFAULT_NUM_PARTITIONS ) ,
64
+ replication : TopicReplication :: Fixed ( topic_configs . replication_factor . unwrap_or ( DEFAULT_REPLICATION_FACTOR ) ) ,
65
+ config,
47
66
} ;
48
67
49
68
let opts = AdminOptions :: new ( ) . operation_timeout ( Some ( timeout) ) ;
@@ -52,9 +71,7 @@ pub async fn create_topic() -> Result<KafkaDeployStatus, KafkaDeployError> {
52
71
53
72
let results = admin. create_topics ( & [ topic] , & opts) . await ?;
54
73
55
- results[ 0 ]
56
- . as_ref ( )
57
- . map_err ( |e| KafkaDeployError :: TopicCreation ( kafka_certification_topic, e. 1 ) ) ?;
74
+ results[ 0 ] . as_ref ( ) . map_err ( |e| KafkaDeployError :: TopicCreation ( topic_configs. topic , e. 1 ) ) ?;
58
75
59
76
Ok ( KafkaDeployStatus :: TopicCreated )
60
77
}
0 commit comments