@ -188,31 +188,46 @@ public class TbKafkaAdmin implements TbQueueAdmin {
}
public boolean isTopicEmpty ( String topic ) {
return areAllTopicsEmpty ( Set . of ( topic ) ) ;
}
public boolean areAllTopicsEmpty ( Set < String > topics ) {
try {
if ( ! getTopics ( ) . contains ( topic ) ) {
List < String > existingTopics = getTopics ( ) . stream ( ) . filter ( topics : : contains ) . toList ( ) ;
if ( existingTopics . isEmpty ( ) ) {
return true ;
}
TopicDescription topicDescription = settings . getAdminClient ( ) . describeTopics ( Collections . singletonList ( topic ) ) . topicNameValues ( ) . get ( topic ) . get ( ) ;
List < TopicPartition > partitions = topicDescription . partitions ( ) . stream ( ) . map ( partitionInfo - > new TopicPartition ( topic , partitionInfo . partition ( ) ) ) . toList ( ) ;
Map < TopicPartition , ListOffsetsResult . ListOffsetsResultInfo > beginningOffsets = settings . getAdminClient ( ) . listOffsets ( partitions . stream ( )
List < TopicPartition > allPartitions = settings . getAdminClient ( ) . describeTopics ( existingTopics ) . topicNameValues ( ) . entrySet ( ) . stream ( )
. flatMap ( entry - > {
String topic = entry . getKey ( ) ;
TopicDescription topicDescription ;
try {
topicDescription = entry . getValue ( ) . get ( ) ;
} catch ( InterruptedException | ExecutionException e ) {
throw new RuntimeException ( e ) ;
}
return topicDescription . partitions ( ) . stream ( ) . map ( partitionInfo - > new TopicPartition ( topic , partitionInfo . partition ( ) ) ) ;
} )
. toList ( ) ;
Map < TopicPartition , ListOffsetsResult . ListOffsetsResultInfo > beginningOffsets = settings . getAdminClient ( ) . listOffsets ( allPartitions . stream ( )
. collect ( Collectors . toMap ( partition - > partition , partition - > OffsetSpec . earliest ( ) ) ) ) . all ( ) . get ( ) ;
Map < TopicPartition , ListOffsetsResult . ListOffsetsResultInfo > endOffsets = settings . getAdminClient ( ) . listOffsets ( partitions . stream ( )
Map < TopicPartition , ListOffsetsResult . ListOffsetsResultInfo > endOffsets = settings . getAdminClient ( ) . listOffsets ( allPartitions . stream ( )
. collect ( Collectors . toMap ( partition - > partition , partition - > OffsetSpec . latest ( ) ) ) ) . all ( ) . get ( ) ;
for ( TopicPartition partition : partitions ) {
for ( TopicPartition partition : allP artitions) {
long beginningOffset = beginningOffsets . get ( partition ) . offset ( ) ;
long endOffset = endOffsets . get ( partition ) . offset ( ) ;
if ( beginningOffset ! = endOffset ) {
log . debug ( "Partition [{}] of topic [{}] is not empty. Returning false." , partition . partition ( ) , topic ) ;
log . debug ( "Partition [{}] of topic [{}] is not empty. Returning false." , partition . partition ( ) , partition . topic ( ) ) ;
return false ;
}
}
return true ;
} catch ( InterruptedException | ExecutionException e ) {
log . error ( "Failed to check if topic [{}] is empty." , topic , e ) ;
log . error ( "Failed to check if topics [{}] empty." , topics , e ) ;
return false ;
}
}