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

[server] ReadWriteLock on LeaderFollowerState #1251

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

KaiSernLim
Copy link
Contributor

@KaiSernLim KaiSernLim commented Oct 17, 2024

Background

TODO:

Changes

  • Added a ReadWriteLock to the LeaderFollowerState in PartitionConsumptionState
  • In produceToStoreBufferServiceOrKafka() on the consumer's code path, the read lock must be acquired for the duration of the message's processing in order for the condition of shouldProcessMessage() to hold. Also applies to produceToStoreBufferServiceOrKafkaInBatch().
    • Another thread trying to modify LeaderFollowerState as part of a state transition would need to wait for this consumer thread to finish processing the message and release the read lock.
  • Added a unit test testShouldProcessRecord() which simulates the scenario where the consumer thread processes a batch of polled messages while another thread modifies the leader-follower state in the PCS. It's specifically testing a follower to leader transition and verifying that the leader-follower state in the PCS can't be modified while the consumer thread is processing messages.

Correctness

TODO:

Performance Impact

  • Performance is bottlenecked by produceToStoreBufferServiceOrKafka(), because that is the only location where the lock is held for a long period of time. If a writer is waiting on the write lock, it must wait for all readers to release their locks. All future readers will also need to wait until the writer is done because the writer has priority, so they are also bottlenecked by produceToStoreBufferServiceOrKafka().
  • Readers should not affect the performance of other readers. Therefore, there should be no performance impact until a state transition occurs, and the state needs to be overwritten.
  • Since the writer simply needs to set an enum value, the critical section should be instantaneous once it manages to acquire the lock, and any slowdown while locking the writer lock should be minimal.

How was this PR tested?

CI

Does this PR introduce any user-facing changes?

  • No. You can skip the rest of this section.

@KaiSernLim KaiSernLim self-assigned this Oct 18, 2024

LeaderFollowerState() {
this.state = LeaderFollowerStateType.STANDBY;
this.rwLock = new ReentrantReadWriteLock(true); // TODO: would it be better if this was non-fair? I think no
Copy link
Contributor

Choose a reason for hiding this comment

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

What does this true mean?

Copy link
Contributor Author

@KaiSernLim KaiSernLim Oct 28, 2024

Choose a reason for hiding this comment

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

The parameter is for whether the lock is in fair-mode or not (docs link).

Basically, fair-mode will prioritize write locks trying to acquire the lock to ensure that the write lock doesn't get starved. More specifically, it'll respect approximate arrival-order policy whereas non-fair will have no order of entry guarantees.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants