@@ -80,9 +80,9 @@ public class KafkaSystemAdmin implements ExtendedSystemAdmin {
8080
8181 protected final String systemName ;
8282 protected final Consumer metadataConsumer ;
83+ protected final Config config ;
8384
84- // get ZkUtils object to connect to Kafka's ZK.
85- private final Supplier <ZkUtils > getZkConnection ;
85+ protected AdminClient adminClient = null ;
8686
8787 // Custom properties to create a new coordinator stream.
8888 private final Properties coordinatorStreamProperties ;
@@ -96,52 +96,21 @@ public class KafkaSystemAdmin implements ExtendedSystemAdmin {
9696 // Kafka properties for intermediate topics creation
9797 private final Map <String , Properties > intermediateStreamProperties ;
9898
99- // adminClient is required for deleteCommittedMessages operation
100- private final AdminClient adminClient ;
101-
10299 // used for intermediate streams
103- private final boolean deleteCommittedMessages ;
100+ protected final boolean deleteCommittedMessages ;
104101
105102 private final AtomicBoolean stopped = new AtomicBoolean (false );
106103
107104 public KafkaSystemAdmin (String systemName , Config config , Consumer metadataConsumer ) {
108105 this .systemName = systemName ;
106+ this .config = config ;
109107
110108 if (metadataConsumer == null ) {
111109 throw new SamzaException (
112110 "Cannot construct KafkaSystemAdmin for system " + systemName + " with null metadataConsumer" );
113111 }
114112 this .metadataConsumer = metadataConsumer ;
115113
116- // populate brokerList from either consumer or producer configs
117- Properties props = new Properties ();
118- String brokerList = config .get (
119- String .format (KafkaConfig .CONSUMER_CONFIGS_CONFIG_KEY (), systemName , ConsumerConfig .BOOTSTRAP_SERVERS_CONFIG ));
120- if (brokerList == null ) {
121- brokerList = config .get (String .format (KafkaConfig .PRODUCER_CONFIGS_CONFIG_KEY (), systemName ,
122- ConsumerConfig .BOOTSTRAP_SERVERS_CONFIG ));
123- }
124- if (brokerList == null ) {
125- throw new SamzaException (
126- ConsumerConfig .BOOTSTRAP_SERVERS_CONFIG + " is required for systemAdmin for system " + systemName );
127- }
128- props .put (ConsumerConfig .BOOTSTRAP_SERVERS_CONFIG , brokerList );
129-
130- // kafka.admin.AdminUtils requires zkConnect
131- // this will change after we move to the new org.apache..AdminClient
132- String zkConnect =
133- config .get (String .format (KafkaConfig .CONSUMER_CONFIGS_CONFIG_KEY (), systemName , ZOOKEEPER_CONNECT ));
134- if (StringUtils .isBlank (zkConnect )) {
135- throw new SamzaException ("Missing zookeeper.connect config for admin for system " + systemName );
136- }
137- props .put (ZOOKEEPER_CONNECT , zkConnect );
138-
139- adminClient = AdminClient .create (props );
140-
141- getZkConnection = () -> {
142- return ZkUtils .apply (zkConnect , 6000 , 6000 , false );
143- };
144-
145114 KafkaConfig kafkaConfig = new KafkaConfig (config );
146115 coordinatorStreamReplicationFactor = Integer .valueOf (kafkaConfig .getCoordinatorReplicationFactor ());
147116 coordinatorStreamProperties = KafkaSystemAdminUtilsScala .getCoordinatorTopicProperties (kafkaConfig );
@@ -197,6 +166,8 @@ public void stop() {
197166 } catch (Exception e ) {
198167 LOG .warn ("metadataConsumer.close for system " + systemName + " failed with exception." , e );
199168 }
169+ }
170+ if (adminClient != null ) {
200171 try {
201172 adminClient .close ();
202173 } catch (Exception e ) {
@@ -546,14 +517,14 @@ public Integer offsetComparator(String offset1, String offset2) {
546517 public boolean createStream (StreamSpec streamSpec ) {
547518 LOG .info ("Creating Kafka topic: {} on system: {}" , streamSpec .getPhysicalName (), streamSpec .getSystemName ());
548519
549- return KafkaSystemAdminUtilsScala .createStream (toKafkaSpec (streamSpec ), getZkConnection );
520+ return KafkaSystemAdminUtilsScala .createStream (toKafkaSpec (streamSpec ), getZkConnection () );
550521 }
551522
552523 @ Override
553524 public boolean clearStream (StreamSpec streamSpec ) {
554525 LOG .info ("Creating Kafka topic: {} on system: {}" , streamSpec .getPhysicalName (), streamSpec .getSystemName ());
555526
556- KafkaSystemAdminUtilsScala .clearStream (streamSpec , getZkConnection );
527+ KafkaSystemAdminUtilsScala .clearStream (streamSpec , getZkConnection () );
557528
558529 Map <String , List <PartitionInfo >> topicsMetadata = getTopicMetadata (ImmutableSet .of (streamSpec .getPhysicalName ()));
559530 return topicsMetadata .get (streamSpec .getPhysicalName ()).isEmpty ();
@@ -630,11 +601,56 @@ Map<String, List<PartitionInfo>> getTopicMetadata(Set<String> topics) {
630601 @ Override
631602 public void deleteMessages (Map <SystemStreamPartition , String > offsets ) {
632603 if (deleteCommittedMessages ) {
604+ if (adminClient == null ) {
605+ adminClient = AdminClient .create (createAdminClientProperties ());
606+ }
633607 KafkaSystemAdminUtilsScala .deleteMessages (adminClient , offsets );
634608 deleteMessageCalled = true ;
635609 }
636610 }
637611
612+ protected Properties createAdminClientProperties () {
613+ // populate brokerList from either consumer or producer configs
614+ Properties props = new Properties ();
615+ // included SSL settings if needed
616+
617+ props .putAll (config .subset (String .format ("systems.%s.consumer." , systemName ), true ));
618+
619+ //validate brokerList
620+ String brokerList = config .get (
621+ String .format (KafkaConfig .CONSUMER_CONFIGS_CONFIG_KEY (), systemName , ConsumerConfig .BOOTSTRAP_SERVERS_CONFIG ));
622+ if (brokerList == null ) {
623+ brokerList = config .get (String .format (KafkaConfig .PRODUCER_CONFIGS_CONFIG_KEY (), systemName ,
624+ ConsumerConfig .BOOTSTRAP_SERVERS_CONFIG ));
625+ }
626+ if (brokerList == null ) {
627+ throw new SamzaException (
628+ ConsumerConfig .BOOTSTRAP_SERVERS_CONFIG + " is required for systemAdmin for system " + systemName );
629+ }
630+ props .put (ConsumerConfig .BOOTSTRAP_SERVERS_CONFIG , brokerList );
631+
632+
633+ // kafka.admin.AdminUtils requires zkConnect
634+ // this will change after we move to the new org.apache..AdminClient
635+ String zkConnect =
636+ config .get (String .format (KafkaConfig .CONSUMER_CONFIGS_CONFIG_KEY (), systemName , ZOOKEEPER_CONNECT ));
637+ if (StringUtils .isBlank (zkConnect )) {
638+ throw new SamzaException ("Missing zookeeper.connect config for admin for system " + systemName );
639+ }
640+ props .put (ZOOKEEPER_CONNECT , zkConnect );
641+
642+ return props ;
643+ }
644+
645+ private Supplier <ZkUtils > getZkConnection () {
646+ String zkConnect =
647+ config .get (String .format (KafkaConfig .CONSUMER_CONFIGS_CONFIG_KEY (), systemName , ZOOKEEPER_CONNECT ));
648+ if (StringUtils .isBlank (zkConnect )) {
649+ throw new SamzaException ("Missing zookeeper.connect config for admin for system " + systemName );
650+ }
651+ return () -> ZkUtils .apply (zkConnect , 6000 , 6000 , false );
652+ }
653+
638654 /**
639655 * Container for metadata about offsets.
640656 */
0 commit comments