@@ -393,6 +393,31 @@ static bool ParseListConsumerGroupsArgs(string[] commandArgs,
393
393
}
394
394
}
395
395
396
+ static Tuple < ElectionType , List < TopicPartition > > ParseElectLeadersArgs ( string [ ] args )
397
+ {
398
+ if ( ( args . Length - 1 ) % 2 != 0 )
399
+ {
400
+ Console . WriteLine ( "usage: .. <bootstrapServers> elect-leaders <electionType> <topic1> <partition1> .." ) ;
401
+ Environment . ExitCode = 1 ;
402
+ return null ;
403
+ }
404
+
405
+ var electionType = Enum . Parse < ElectionType > ( args [ 0 ] ) ;
406
+ var partitions = new List < TopicPartition > ( ) ;
407
+ if ( args . Length == 1 )
408
+ {
409
+ partitions = null ;
410
+ return Tuple . Create ( electionType , partitions ) ;
411
+ }
412
+ for ( int i = 1 ; i < args . Length ; i += 2 )
413
+ {
414
+ var topic = args [ i ] ;
415
+ var partition = Int32 . Parse ( args [ i + 1 ] ) ;
416
+ partitions . Add ( new TopicPartition ( topic , partition ) ) ;
417
+ }
418
+ return Tuple . Create ( electionType , partitions ) ;
419
+ }
420
+
396
421
static void PrintListOffsetsResultInfos ( List < ListOffsetsResultInfo > ListOffsetsResultInfos )
397
422
{
398
423
foreach ( var listOffsetsResultInfo in ListOffsetsResultInfos )
@@ -403,6 +428,20 @@ static void PrintListOffsetsResultInfos(List<ListOffsetsResultInfo> ListOffsetsR
403
428
}
404
429
}
405
430
431
+ static void PrintElectLeaderResults ( List < TopicPartitionError > topicPartitions )
432
+ {
433
+ Console . WriteLine ( $ "ElectLeaders response has { topicPartitions . Count } partition(s):") ;
434
+ foreach ( var partitionResult in topicPartitions )
435
+ {
436
+ if ( ! partitionResult . Error . IsError )
437
+ Console . WriteLine ( $ "Election successful in { partitionResult . Topic } { partitionResult . Partition } ") ;
438
+ else
439
+ Console . WriteLine ( $ "Election failed in { partitionResult . Topic } { partitionResult . Partition } : " +
440
+ $ "Code: { partitionResult . Error . Code } " +
441
+ $ ", Reason: { partitionResult . Error . Reason } ") ;
442
+ }
443
+ }
444
+
406
445
static async Task CreateAclsAsync ( string bootstrapServers , string [ ] commandArgs )
407
446
{
408
447
List < AclBinding > aclBindings ;
@@ -978,6 +1017,41 @@ static async Task ListOffsetsAsync(string bootstrapServers, string[] commandArgs
978
1017
}
979
1018
}
980
1019
}
1020
+
1021
+ static async Task ElectLeadersAsync ( string bootstrapServers , string [ ] commandArgs )
1022
+ {
1023
+ if ( commandArgs . Length < 3 && ( commandArgs . Length - 1 ) % 2 != 0 )
1024
+ {
1025
+ Console . WriteLine ( "usage: .. <bootstrapServers> elect-leaders <electionType> <topic1> <partition1> .." ) ;
1026
+ Environment . ExitCode = 1 ;
1027
+ return ;
1028
+ }
1029
+
1030
+ var req = ParseElectLeadersArgs ( commandArgs ) ;
1031
+ var electionType = req . Item1 ;
1032
+ var partitions = req . Item2 ;
1033
+ var timeout = TimeSpan . FromSeconds ( 30 ) ;
1034
+ ElectLeadersOptions options = new ElectLeadersOptions ( ) { RequestTimeout = timeout } ;
1035
+ using ( var adminClient = new AdminClientBuilder ( new AdminClientConfig { BootstrapServers = bootstrapServers } ) . Build ( ) )
1036
+ {
1037
+ try
1038
+ {
1039
+ var result = await adminClient . ElectLeadersAsync ( electionType , partitions , options ) ;
1040
+ PrintElectLeaderResults ( result . TopicPartitions ) ;
1041
+
1042
+ }
1043
+ catch ( ElectLeadersException e )
1044
+ {
1045
+ Console . WriteLine ( "One or more elect leaders operations failed." ) ;
1046
+ PrintElectLeaderResults ( e . Results . TopicPartitions ) ;
1047
+ }
1048
+ catch ( KafkaException e )
1049
+ {
1050
+ Console . WriteLine ( $ "An error occurred electing leaders: { e } ") ;
1051
+ Environment . ExitCode = 1 ;
1052
+ }
1053
+ }
1054
+ }
981
1055
static void PrintTopicDescriptions ( List < TopicDescription > topicDescriptions , bool includeAuthorizedOperations )
982
1056
{
983
1057
foreach ( var topic in topicDescriptions )
@@ -1146,7 +1220,7 @@ public static async Task Main(string[] args)
1146
1220
"list-consumer-group-offsets" , "alter-consumer-group-offsets" ,
1147
1221
"incremental-alter-configs" , "describe-user-scram-credentials" ,
1148
1222
"alter-user-scram-credentials" , "describe-topics" ,
1149
- "describe-cluster" , "list-offsets"
1223
+ "describe-cluster" , "list-offsets" , "elect-leaders"
1150
1224
} ) +
1151
1225
" .." ) ;
1152
1226
Environment . ExitCode = 1 ;
@@ -1210,6 +1284,9 @@ public static async Task Main(string[] args)
1210
1284
case "list-offsets" :
1211
1285
await ListOffsetsAsync ( bootstrapServers , commandArgs ) ;
1212
1286
break ;
1287
+ case "elect-leaders" :
1288
+ await ElectLeadersAsync ( bootstrapServers , commandArgs ) ;
1289
+ break ;
1213
1290
default :
1214
1291
Console . WriteLine ( $ "unknown command: { command } ") ;
1215
1292
break ;
0 commit comments