Skip to content

[Performance] Optimize allocations in ManagedLedgerImpl.getEarlierReadPositionForActiveCursors()/ManagedCursorContainer.iterator() #9958

Closed

Description

@lhotari

Problem

In profiling, I can see that a lot of allocations in TLAB are made for this kind of stack trace:

Iterator java.util.concurrent.ConcurrentSkipListMap.entryIterator()
Iterator java.util.concurrent.ConcurrentSkipListMap$EntrySet.iterator()
Iterator org.apache.bookkeeper.mledger.impl.ManagedCursorContainer.iterator()
PositionImpl org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl.getEarlierReadPositionForActiveCursors()
void org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl.doCacheEviction(long)
void org.apache.bookkeeper.mledger.impl.ManagedLedgerFactoryImpl.lambda$doCacheEviction$5(long
void org.apache.bookkeeper.mledger.impl.ManagedLedgerFactoryImpl$$Lambda$83.1547081970.accept(Object)
void java.util.concurrent.ConcurrentHashMap$ValuesView.forEach(Consumer)
void org.apache.bookkeeper.mledger.impl.ManagedLedgerFactoryImpl.doCacheEviction()
void org.apache.bookkeeper.mledger.impl.ManagedLedgerFactoryImpl.cacheEvictionTask()
void org.apache.bookkeeper.mledger.impl.ManagedLedgerFactoryImpl$$Lambda$81.1649498695.run()
void java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor$Worker)
void java.util.concurrent.ThreadPoolExecutor$Worker.run()
void io.netty.util.concurrent.FastThreadLocalRunnable.run()
void java.lang.Thread.run()

(Broker version is Pulsar 2.7.0)

Expected behavior

Calls on the hot paths shouldn't create a lot of allocations.

Activity

  1. added
    type/enhancement
    on Mar 18, 2021
  2. lhotari commented on Mar 18, 2021

    @lhotari
    MemberAuthor

    @merlimat would you mind taking a look at this issue? In the stack trace it shows that ConcurrentSkipListMap is used in ManagedCursorContainer. Would there be a benefit to switch over to ConcurrentOpenHashMap?
    I understood from our earlier discussions that you have optimized it for minimal allocations.

  3. lhotari commented on Mar 19, 2021

    @lhotari
    MemberAuthor

    @merlimat This performance problem has also been reported on Pulsar Slack in this discussion: https://apache-pulsar.slack.com/archives/C5Z4T36F7/p1614293057103600 .

    It seems that adjusting managedLedgerCacheEvictionFrequency configuration value is one way to workaround the performance issue. The description of this configuration setting is "Configure the cache eviction frequency for the managed ledger cache (evictions/sec)" , default value is 100.0 .

    Why does the cache eviction have to run 100 times per second by default?

  4. merlimat commented on Mar 29, 2021

    @merlimat
    Contributor

    The main reason is to make sure the data stays cached for least possible amount of time.

    The data itself is not the main problem (it's already stored in pooled direct memory and there's a max size to the cache).

    The problem typically comes from the tracking objects that are stored in the cache (eg: the pointers to the data). The current implementation is based on the ConcurrentSkipListMap which stores the objects and Nodes to hold them. All these objects, if they're cached for significant amount of time will be candidate to get pushed down to the old-gen when the young-gen is compacted, and will have to get compacted there later, causing longer gc pauses.

    This was true for G1GC, although it might not be exactly the same with ZGC (hence: the threshold and tradeoff considerations will vary).

  5. codelipenghui commented on Mar 4, 2022

    @codelipenghui
    Contributor

    The issue had no activity for 30 days, mark with Stale label.

  6. lhotari commented on Jun 7, 2022

    @lhotari
    MemberAuthor

    Will be addressed by "PIP-174: Provide new implementation for broker dispatch cache" , #15954 / #15955

  7. added 3 commits that reference this issue on Aug 23, 2022
    c1220cb
    690d4d0
    8402e07
  8. added a commit that references this issue on Aug 31, 2022
    856ef15
  9. added a commit that references this issue on Oct 31, 2022
    bd27df4
  10. added a commit that references this issue on Nov 17, 2022
    3ab0b2d
  11. added a commit that references this issue on Dec 7, 2022
    aa1f660
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    lifecycle/staletype/enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions