Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(models): Use the partition leader from partition info #1388

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 19 additions & 9 deletions src/main/java/org/akhq/models/Partition.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
@Getter
@NoArgsConstructor
public class Partition {
private Node.Partition leader;
private int id;
private String topic;
private List<Node.Partition> nodes;
Expand All @@ -27,27 +28,36 @@ public Partition(String topic, TopicPartitionInfo partitionInfo, List<LogDir> lo
this.firstOffset = offsets.getFirstOffset();
this.lastOffset = offsets.getLastOffset();
this.nodes = new ArrayList<>();

for (org.apache.kafka.common.Node replica : partitionInfo.replicas()) {
nodes.add(new Node.Partition(
Node.Partition partition = new Node.Partition(
replica,
partitionInfo.leader().id() == replica.id(),
partitionInfo.isr().stream().anyMatch(node -> node.id() == replica.id())
));
);

this.nodes.add(partition);
if (partition.isLeader()) {
this.leader = partition;
}
}

if (this.leader == null) {
org.apache.kafka.common.Node leader = partitionInfo.leader();
this.leader = new Node.Partition(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I null check the leader here?

leader,
true,
partitionInfo.isr().stream().anyMatch(node -> node.id() == leader.id())
);
}
}

public Node.Partition getLeader() {
return nodes
.stream()
.filter(Node.Partition::isLeader)
.findFirst()
.orElseThrow(() -> new NoSuchElementException("Leader not found"));
return this.leader;
}

public long getLogDirSize() {
return this.getLogDir().stream()
.filter(logDir -> logDir.getBrokerId() == this.getLeader().getId())
.filter(logDir -> this.leader != null && logDir.getBrokerId() == this.leader.getId())
.map(LogDir::getSize)
.reduce(0L, Long::sum);
}
Expand Down