Skip to content

use netty PlatformDependent.estimateMaxDirectMemory#15238

Merged
hezhangjian merged 7 commits intoapache:masterfrom
hezhangjian:use-netty-estimate-memory
May 15, 2022
Merged

use netty PlatformDependent.estimateMaxDirectMemory#15238
hezhangjian merged 7 commits intoapache:masterfrom
hezhangjian:use-netty-estimate-memory

Conversation

@hezhangjian
Copy link
Copy Markdown
Member

Motivation

  • PlatformDependent.maxDirectMemory() can be inaccurate if io.netty.maxDirectMemory are setted
  • Bookkeeper's DirectMemoryUtils is not worked within some jdk releases.
    In netty 4.1.75, they introduced a new method PlatformDependent.estimateMaxDirectMemory to help users get maxDirectMemory. Since netty's this method works well on many jdk releases, use this to replace below two.
    PS: DirectMemoryUtils has been removed from bookkeeper in Use netty maxDirectMemory instead of DirectMemoryUtils bookkeeper#2989

Modifications

  • use PlatformDependent.estimateMaxDirectMemory instead

@hezhangjian hezhangjian added the doc-not-needed Your PR changes do not impact docs label Apr 20, 2022
@hezhangjian hezhangjian self-assigned this Apr 20, 2022
@hezhangjian hezhangjian force-pushed the use-netty-estimate-memory branch 2 times, most recently from 33739d3 to d135685 Compare April 20, 2022 15:14
@HQebupt
Copy link
Copy Markdown
Contributor

HQebupt commented Apr 20, 2022

LGTM :)

@hezhangjian
Copy link
Copy Markdown
Member Author

/pulsarbot run-failure-checks

Copy link
Copy Markdown
Member

@michaeljmarshall michaeljmarshall left a comment

Choose a reason for hiding this comment

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

@shoothzj - I am not familiar with direct memory computation, but given the javadoc that I share in a comment, I'd like to make sure that we don't introduce a performance regression with this PR. Let me know what you think, thanks!

@Override
public double get() {
return io.netty.util.internal.PlatformDependent.maxDirectMemory();
return io.netty.util.internal.PlatformDependent.estimateMaxDirectMemory();
Copy link
Copy Markdown
Member

@michaeljmarshall michaeljmarshall Apr 21, 2022

Choose a reason for hiding this comment

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

It looks like the javadoc for this method recommends using #maxDirectMemory():

    /**
     * Compute an estimate of the maximum amount of direct memory available to this JVM.
     * <p>
     * The computation is not cached, so you probably want to use {@link #maxDirectMemory()} instead.
     * <p>
     * This will produce debug log output when called.
     *
     * @return The estimated max direct memory, in bytes.
     */
    public static long estimateMaxDirectMemory()

https://github.com/netty/netty/blob/0d653f08cd108a3ba6cf76d9fc052b8141ea3196/common/src/main/java/io/netty/util/internal/PlatformDependent.java#L1154-L1163

I am concerned that the cost of this alternative method. Are you able to provide documentation on how this method is better? Alternatively, is there a reason we can't cache the result in this class and the other references that will be called many times?

@hezhangjian
Copy link
Copy Markdown
Member Author

@michaeljmarshall My bad
The main reason I want to replace is that maxDirectMemory() can be changed by system property io.netty.maxDirectMemory, which will may lead to wrong result.
To avoid performance regression, I think we can put it in a common class, and others use the result.
What do you think?

@hezhangjian hezhangjian force-pushed the use-netty-estimate-memory branch from d135685 to 50bf474 Compare May 8, 2022 08:08
@michaeljmarshall
Copy link
Copy Markdown
Member

@michaeljmarshall My bad The main reason I want to replace is that maxDirectMemory() can be changed by system property io.netty.maxDirectMemory, which will may lead to wrong result. To avoid performance regression, I think we can put it in a common class, and others use the result. What do you think?

Is it possible that it is a feature, not a bug, that we expose io.netty.maxDirectMemory to end users? If a user configures io.netty.maxDirectMemory poorly, I wouldn't consider that a performance regression.

@hezhangjian
Copy link
Copy Markdown
Member Author

@michaeljmarshall There are two scenes using this method

  • metrics, using this method to produce maxDirectMemory metrics, estimateMaxDirectMemory is more proper.
  • Serverside config maxMessagePublishBufferSizeInMB. This param's default value is 1/2 of direct memory. io.netty.maxDirectMemory isn't on our docs. They can reconfigure it by maxMessagePublishBufferSizeInMB.

Comment thread pulsar-common/src/main/java/org/apache/pulsar/common/util/DirectMemoryUtils.java Outdated
@hezhangjian hezhangjian force-pushed the use-netty-estimate-memory branch from d57baf2 to ec48c99 Compare May 10, 2022 12:25
@hezhangjian
Copy link
Copy Markdown
Member Author

@lhotari PTAL again. @codelipenghui @eolivelli @merlimat PTAL

@coderzc
Copy link
Copy Markdown
Member

coderzc commented May 10, 2022

LGTM

// Collect JVM direct memory
systemResourceUsage.setDirectMemory(new ResourceUsage((double) (getJvmDirectMemoryUsed() / MIBI),
(double) (io.netty.util.internal.PlatformDependent.maxDirectMemory() / MIBI)));
(double) (io.netty.util.internal.PlatformDependent.estimateMaxDirectMemory() / MIBI)));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is it intentional to keep on using PlatformDependent class here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@lhotari Sorry. It's a miss. It have been fixed.

@hezhangjian hezhangjian force-pushed the use-netty-estimate-memory branch from 599d2ca to 83f764e Compare May 11, 2022 00:03
Copy link
Copy Markdown
Member

@michaeljmarshall michaeljmarshall left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks for answering my questions @shoothzj.

@hezhangjian hezhangjian force-pushed the use-netty-estimate-memory branch 4 times, most recently from 9c52638 to 30aa202 Compare May 11, 2022 10:33
@hezhangjian hezhangjian force-pushed the use-netty-estimate-memory branch from 43a832c to e935722 Compare May 11, 2022 13:13
@hezhangjian hezhangjian force-pushed the use-netty-estimate-memory branch from e935722 to b84bb7d Compare May 11, 2022 15:21
Copy link
Copy Markdown
Contributor

@eolivelli eolivelli left a comment

Choose a reason for hiding this comment

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

LGTM

@hezhangjian hezhangjian force-pushed the use-netty-estimate-memory branch 12 times, most recently from 3230097 to 98517de Compare May 14, 2022 16:06
@hezhangjian hezhangjian force-pushed the use-netty-estimate-memory branch from 98517de to 7be5743 Compare May 15, 2022 01:13
@hezhangjian hezhangjian force-pushed the use-netty-estimate-memory branch from 1430c89 to c383c79 Compare May 15, 2022 03:40
@hezhangjian
Copy link
Copy Markdown
Member Author

/pulsarbot rerun-failure-checks

@hezhangjian hezhangjian merged commit 9d5d110 into apache:master May 15, 2022
@hezhangjian hezhangjian deleted the use-netty-estimate-memory branch May 15, 2022 12:30
gaozhangmin pushed a commit to gaozhangmin/pulsar that referenced this pull request Apr 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

doc-not-needed Your PR changes do not impact docs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants