Skip to content

Conversation

sjhajharia
Copy link
Contributor

@sjhajharia sjhajharia commented Aug 13, 2025

This PR aims at cleaning up the raft module further by getting rid of
some extra code which can be replaced by record

Reviewers: Chia-Ping Tsai [email protected]

@github-actions github-actions bot added triage PRs from the community kraft labels Aug 13, 2025
@sjhajharia
Copy link
Contributor Author

Adding @chia7712 as he has been kindly helping review a whole lot of PRs in this domain.
TIA!

Copy link

A label of 'needs-attention' was automatically added to this PR in order to raise the
attention of the committers. Once this issue has been triaged, the triage label
should be removed to prevent this automation from happening again.

Copy link
Member

@chia7712 chia7712 left a comment

Choose a reason for hiding this comment

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

@sjhajharia thanks for this patch

public class LeaderAndEpoch {
private final OptionalInt leaderId;
private final int epoch;
public record LeaderAndEpoch(OptionalInt leaderId, int epoch) {
public static final LeaderAndEpoch UNKNOWN = new LeaderAndEpoch(OptionalInt.empty(), 0);

public LeaderAndEpoch(OptionalInt leaderId, int epoch) {
Copy link
Member

Choose a reason for hiding this comment

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

this could be simplified.

    public LeaderAndEpoch {
        Objects.requireNonNull(leaderId);
    }

Also, could you please add unit test for it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Make sense. I have added LeaderAndEpochTest for the same.

@@ -28,7 +28,7 @@
* Tracks the votes cast by voters in an election held by a Nominee.
*/
public class EpochElection {
Copy link
Member

Choose a reason for hiding this comment

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

it could be a record class, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think we can do so here. Right now EpochElection holds mutable state voterStates and the methods like recordVote(...) mutate it (voterState.setState(...)). As far as I know, record only supports final members.

Copy link
Member

Choose a reason for hiding this comment

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

mutable state voterStates

voterStates is changed to a "final" member in this PR, so EpochElection could be a record class now.

public record EpochElection(Map<Integer, VoterState> voterStates) {
    public EpochElection(Set<ReplicaKey> voterStates) {
        this(voterStates.stream().collect(Collectors.toMap(ReplicaKey::id, VoterState::new)));
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes makes sense. I have fixed the same. Pls re-review when possible.

@sjhajharia sjhajharia requested a review from chia7712 August 21, 2025 17:50
@github-actions github-actions bot removed triage PRs from the community needs-attention labels Aug 22, 2025
Copy link
Member

@chia7712 chia7712 left a comment

Choose a reason for hiding this comment

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

@sjhajharia thanks for your updates

public class EpochElection {
private Map<Integer, VoterState> voterStates;

public record EpochElection(Map<Integer, VoterState> voterStates) {
Copy link
Member

Choose a reason for hiding this comment

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

Could you please make voterStates immutable?

Copy link
Member

Choose a reason for hiding this comment

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

Also, the toString could be removed

Copy link
Member

Choose a reason for hiding this comment

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

the voterIds could return voterStates.keySet() instead if voterStates is immutable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I removed the toString()
For the other comment on making voterStates immutable, I am not sure if that can be done. The public constructor EpochElection is what is used by multiple classes and it takes Set<ReplicaKey> voters and generates the needed Map with VoterState inner class.

@sjhajharia sjhajharia requested a review from chia7712 September 6, 2025 13:27
@chia7712 chia7712 merged commit 52dfe1e into apache:trunk Sep 6, 2025
24 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants