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

[Enhancement] Nested read-write lock issue in ManagedCursor #23605

Closed
2 tasks done
Denovo1998 opened this issue Nov 17, 2024 · 6 comments
Closed
2 tasks done

[Enhancement] Nested read-write lock issue in ManagedCursor #23605

Denovo1998 opened this issue Nov 17, 2024 · 6 comments
Labels
type/enhancement The enhancements for the existing features or docs. e.g. reduce memory usage of the delayed messages

Comments

@Denovo1998
Copy link
Contributor

Search before asking

  • I searched in the issues and found nothing similar.

Motivation

The following are all related to the org.apache.bookkeeper.mledger.impl.ManagedCursorImpl#isMessageDeleted:

@Override
public boolean isMessageDeleted(Position position) {
lock.readLock().lock();
try {
return position.compareTo(markDeletePosition) <= 0
|| individualDeletedMessages.contains(position.getLedgerId(), position.getEntryId());
} finally {
lock.readLock().unlock();
}
}

  1. A write lock has already been acquired, but a read lock has been acquired as well. This doesn't pose a major problem, but it does result in some unnecessary additional overhead.

    lock.writeLock().lock();
    try {
    if (log.isDebugEnabled()) {
    log.debug("[{}] [{}] Deleting individual messages at {}. Current status: {} - md-position: {}",
    ledger.getName(), name, positions, individualDeletedMessages, markDeletePosition);
    }
    for (Position pos : positions) {
    Position position = requireNonNull(pos);
    if (ledger.getLastConfirmedEntry().compareTo(position) < 0) {
    if (log.isDebugEnabled()) {
    log.debug(
    "[{}] Failed mark delete due to invalid markDelete {} is ahead of last-confirmed-entry {} "
    + "for cursor [{}]", ledger.getName(), position, ledger.getLastConfirmedEntry(), name);
    }
    callback.deleteFailed(new ManagedLedgerException("Invalid mark deleted position"), ctx);
    return;
    }
    if (isMessageDeleted(position)) {

  2. The performance impact of nested read locks can be negligible, but frequent acquisition and release of locks will slightly increase system overhead.

    lock.readLock().lock();
    try {
    positions.stream().filter(this::isMessageDeleted).forEach(alreadyAcknowledgedPositions::add);
    } finally {
    lock.readLock().unlock();
    }

Solution

Add a method that does not require a read lock.

    private boolean isMessageDeletedWithOutReadLock(Position position) {
        return position.compareTo(markDeletePosition) <= 0
                || individualDeletedMessages.contains(position.getLedgerId(), position.getEntryId());
    }

Alternatives

No response

Anything else?

I think the modification at 22246 here is for code reuse, but the nested read-write locks in these two segments may need to be modified.

If such a modification is necessary, please let me know, and I will submit a simple PR.

Are you willing to submit a PR?

  • I'm willing to submit a PR!
@Denovo1998 Denovo1998 added the type/enhancement The enhancements for the existing features or docs. e.g. reduce memory usage of the delayed messages label Nov 17, 2024
@Denovo1998
Copy link
Contributor Author

@dao-jun @lhotari PTAL.

@dao-jun
Copy link
Member

dao-jun commented Nov 18, 2024

Personally, I don't think we need to fix it, Read/Write Lock is ReentrantLock, and it just compare the lock owner of the AQS and the current thread when we required a ReadLock after required a WriteLock.

@lhotari
Copy link
Member

lhotari commented Nov 18, 2024

2. The performance impact of nested read locks can be negligible, but frequent acquisition and release of locks will slightly increase system overhead.

Makes sense. I have a large changeset to address multiple performance issues with Broker cache and one of the changes is related: lhotari@a0d0b54 . I haven't yet split this large changeset into PRs for review.

@Denovo1998
Copy link
Contributor Author

@lhotari Oh, I saw it! If you think it‘s necessary. Maybe you can solve the read-write lock nesting problem mentioned in this issue in your pr.

@lhotari
Copy link
Member

lhotari commented Nov 18, 2024

@lhotari Oh, I saw it! If you think it‘s necessary. Maybe you can solve the read-write lock nesting problem mentioned in this issue in your pr.

@Denovo1998 you can go ahead and create a PR if you wish.

@lhotari
Copy link
Member

lhotari commented Nov 19, 2024

Addressed by #23609

@lhotari lhotari closed this as completed Nov 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/enhancement The enhancements for the existing features or docs. e.g. reduce memory usage of the delayed messages
Projects
None yet
Development

No branches or pull requests

3 participants