8000 KAFKA-17350: Improve share group describe for empty groups (#16897) · Python-Repository-Hub/kafka@f6bfa94 · GitHub
[go: up one dir, main page]

Skip to content

Commit f6bfa94

Browse files
KAFKA-17350: Improve share group describe for empty groups (apache#16897)
When you use kafka-share-groups.sh --describe for an empty group, it prints an empty table consisting of only the table header. kafka-consumer-groups.sh summarises the group status to make the output more informative and only prints the table if it contains more than zero rows. This PR applies this principle across all of the variants of describing share groups which makes the output much nicer where the output would otherwise be strangely empty. Reviewers: Manikumar Reddy <manikumar.reddy@gmail.com>
1 parent 4271565 commit f6bfa94

File tree

2 files changed

+42
-12
lines changed

2 files changed

+42
-12
lines changed

tools/src/main/java/org/apache/kafka/tools/consumer/group/ShareGroupCommand.java

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,22 @@ private void printGroupInfo(List<ShareGroupListing> groups) {
169169
}
170170
}
171171

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("\nShare 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 {
173188
String group = opts.options.valueOf(opts.groupOpt);
174189
ShareGroupDescription description = getDescribeGroup(group);
175190
if (description == null)
@@ -218,7 +233,7 @@ Map<TopicPartition, Long> getOffsets(Collection<MemberDescription> members) thro
218233

219234
private void printOffsets(ShareGroupDescription description) throws ExecutionException, InterruptedException {
220235
Map<TopicPartition, Long> offsets = getOffsets(description.members());
221-
if (offsets != null && !offsets.isEmpty()) {
236+
if (maybePrintEmptyGroupState(description.groupId(), description.state(), offsets.size())) {
222237
String fmt = printOffsetFormat(description, offsets);
223238
System.out.printf(fmt, "GROUP", "TOPIC", "PARTITION", "OFFSET");
224239

@@ -238,6 +253,8 @@ private static String printOffsetFormat(ShareGroupDescription description, Map<T
238253
}
239254

240255
private void printStates(ShareGroupDescription description) {
256+
maybePrintEmptyGroupState(description.groupId(), description.state(), 1);
257+
241258
int groupLen = Math.max(15, description.groupId().length());
242259
String coordinator = description.coordinator().host() + ":" + description.coordinator().port() + " (" + description.coordinator().idString() + ")";
243260
int coordinatorLen = Math.max(25, coordinator.length());
@@ -251,17 +268,19 @@ private void printMembers(ShareGroupDescription description) {
251268
int groupLen = Math.max(15, description.groupId().length());
252269
int maxConsumerIdLen = 15, maxHostLen = 15, maxClientIdLen = 15;
253270
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+
}
259277

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+
}
265284
}
266285
}
267286

tools/src/test/java/org/apache/kafka/tools/consumer/group/ShareGroupCommandTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@
4949
import joptsimple.OptionException;
5050

5151
import static org.junit.jupiter.api.Assertions.assertEquals;
52+
import static org.junit.jupiter.api.Assertions.assertFalse;
5253
import static org.junit.jupiter.api.Assertions.assertThrows;
54+
import static org.junit.jupiter.api.Assertions.assertTrue;
5355
import static org.mockito.ArgumentMatchers.any;
5456
import static org.mockito.Mockito.mock;
5557
import static org.mockito.Mockito.when;
@@ -129,6 +131,15 @@ public void testDescribeShareGroupsGetOffsets() throws Exception {
129131
service.close();
130132
}
131133

134+
@Test
135+
public void testPrintEmptyGroupState() {
136+
assertFalse(ShareGroupService.maybePrintEmptyGroupState("group", ShareGroupState.EMPTY, 0));
137+
assertFalse(ShareGroupService.maybePrintEmptyGroupState("group", ShareGroupState.DEAD, 0));
138+
assertFalse(ShareGroupService.maybePrintEmptyGroupState("group", ShareGroupState.STABLE, 0));
139+
assertTrue(ShareGroupService.maybePrintEmptyGroupState("group", ShareGroupState.STABLE, 1));
140+
assertTrue(ShareGroupService.maybePrintEmptyGroupState("group", ShareGroupState.UNKNOWN, 1));
141+
}
142+
132143
@Test
133144
public void testListWithUnrecognizedOption() {
134145
String bootstrapServer = "localhost:9092";

0 commit comments

Comments
 (0)
0