@@ -169,7 +169,22 @@ private void printGroupInfo(List<ShareGroupListing> groups) {
169
169
}
170
170
}
171
171
172
- private void describeGroups () throws ExecutionException , InterruptedException {
172
+ /**
173
+ * Prints a summary of the state for situations where the group is empty or dead.
174
+ *
175
+ * @return Whether the group detail should be printed
176
+ */
177
+ public static boolean maybePrintEmptyGroupState (String group , ShareGroupState state , int numRows ) {
178
+ if (state == ShareGroupState .DEAD ) {
179
+ printError ("Share group '" + group + "' does not exist." , Optional .empty ());
180
+ } else if (state == ShareGroupState .EMPTY ) {
181
+ System .err .println ("\n Share group '" + group + "' has no active members." );
182
+ }
183
+
184
+ return !state .equals (ShareGroupState .DEAD ) && numRows > 0 ;
185
+ }
186
+
187
+ public void describeGroups () throws ExecutionException , InterruptedException {
173
188
String group = opts .options .valueOf (opts .groupOpt );
174
189
ShareGroupDescription description = getDescribeGroup (group );
175
190
if (description == null )
@@ -218,7 +233,7 @@ Map<TopicPartition, Long> getOffsets(Collection<MemberDescription> members) thro
218
233
219
234
private void printOffsets (ShareGroupDescription description ) throws ExecutionException , InterruptedException {
220
235
Map <TopicPartition , Long > offsets = getOffsets (description .members ());
221
- if (offsets != null && ! offsets .isEmpty ( )) {
236
+ if (maybePrintEmptyGroupState ( description . groupId (), description . state (), offsets .size () )) {
222
237
String fmt = printOffsetFormat (description , offsets );
223
238
System .out .printf (fmt , "GROUP" , "TOPIC" , "PARTITION" , "OFFSET" );
224
239
@@ -238,6 +253,8 @@ private static String printOffsetFormat(ShareGroupDescription description, Map<T
238
253
}
239
254
240
255
private void printStates (ShareGroupDescription description ) {
256
+ maybePrintEmptyGroupState (description .groupId (), description .state (), 1 );
257
+
241
258
int groupLen = Math .max (15 , description .groupId ().length ());
242
259
String coordinator = description .coordinator ().host () + ":" + description .coordinator ().port () + " (" + description .coordinator ().idString () + ")" ;
243
260
int coordinatorLen = Math .max (25 , coordinator .length ());
@@ -251,17 +268,19 @@ private void printMembers(ShareGroupDescription description) {
251
268
int groupLen = Math .max (15 , description .groupId ().length ());
252
269
int maxConsumerIdLen = 15 , maxHostLen = 15 , maxClientIdLen = 15 ;
253
270
Collection <MemberDescription > members = description .members ();
254
- for (MemberDescription member : members ) {
255
- maxConsumerIdLen = Math .max (maxConsumerIdLen , member .consumerId ().length ());
256
- maxHostLen = Math .max (maxHostLen , member .host ().length ());
257
- maxClientIdLen = Math .max (maxClientIdLen , member .clientId ().length ());
258
- }
271
+ if (maybePrintEmptyGroupState (description .groupId (), description .state (), description .members ().size ())) {
272
+ for (MemberDescription member : members ) {
273
+ maxConsumerIdLen = Math .max (maxConsumerIdLen , member .consumerId ().length ());
274
+ maxHostLen = Math .max (maxHostLen , member .host ().length ());
275
+ maxClientIdLen = Math .max (maxClientIdLen , member .clientId ().length ());
276
+ }
259
277
260
- String fmt = "%" + -groupLen + "s %" + -maxConsumerIdLen + "s %" + -maxHostLen + "s %" + -maxClientIdLen + "s %s\n " ;
261
- System .out .printf (fmt , "GROUP" , "CONSUMER-ID" , "HOST" , "CLIENT-ID" , "ASSIGNMENT" );
262
- for (MemberDescription member : members ) {
263
- System .out .printf (fmt , description .groupId (), member .consumerId (), member .host (), member .clientId (),
264
- member .assignment ().topicPartitions ().stream ().map (part -> part .topic () + ":" + part .partition ()).collect (Collectors .joining ("," )));
278
+ String fmt = "%" + -groupLen + "s %" + -maxConsumerIdLen + "s %" + -maxHostLen + "s %" + -maxClientIdLen + "s %s\n " ;
279
+ System .out .printf (fmt , "GROUP" , "CONSUMER-ID" , "HOST" , "CLIENT-ID" , "ASSIGNMENT" );
280
+ for (MemberDescription member : members ) {
281
+ System .out .printf (fmt , description .groupId (), member .consumerId (), member .host (), member .clientId (),
282
+ member .assignment ().topicPartitions ().stream ().map (part -> part .topic () + ":" + part .partition ()).collect (Collectors .joining ("," )));
283
+ }
265
284
}
266
285
}
267
286
0 commit comments