Skip to content

Make jvm_memory_direct_bytes_used metrics compatible with jdk8. - #3677

Merged
eolivelli merged 7 commits into
apache:masterfrom
horizonzy:fix-3252
Dec 7, 2022
Merged

Make jvm_memory_direct_bytes_used metrics compatible with jdk8.#3677
eolivelli merged 7 commits into
apache:masterfrom
horizonzy:fix-3252

Conversation

@horizonzy

Copy link
Copy Markdown
Member

Descriptions of the changes in this PR:
In #3252, it only uses poolMxBeanOp to get direct memory usage. It only works in jdk11.
But if the user still uses the old version, netty still uses NoCleanerConstructor ByteBuffer to allocate direct memory, the poolMxBeanOp didn't cover it.
We should check the netty PlatformDependent.useDirectBufferNoCleaner(), then choose the corresponding way to get direct memory usage.

@hezhangjian hezhangjian left a comment

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.

Should we add another test case or just modify it?

@horizonzy

Copy link
Copy Markdown
Member Author

Should we add another test case or just modify it?

Already change the test case to cover it.

@horizonzy

Copy link
Copy Markdown
Member Author

rerun failure checks

private static final Optional<BufferPoolMXBean> poolMxBeanOp;

static {
AtomicLong tmpDirectMemoryUsage = null;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do we need to get direct memory usage here according to PlatformDependent.useDirectBufferNoCleaner() here instead of getting both values?

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.

done

@horizonzy

Copy link
Copy Markdown
Member Author

rerun failure checks

1 similar comment
@horizonzy

Copy link
Copy Markdown
Member Author

rerun failure checks

@horizonzy

Copy link
Copy Markdown
Member Author

rerun failure checks

List<BufferPoolMXBean> platformMXBeans = ManagementFactory.getPlatformMXBeans(BufferPoolMXBean.class);
poolMxBeanOp = platformMXBeans.stream()
.filter(bufferPoolMXBean -> bufferPoolMXBean.getName().equals("direct")).findAny();
getDirectMemoryUsage = () -> poolMxBeanOp.get().getMemoryUsed();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We need to check whether poolMxBeanOp is present before get

log.warn("Failed to access netty DIRECT_MEMORY_COUNTER field {}", t.getMessage());
}
directMemoryUsage = tmpDirectMemoryUsage;
getDirectMemoryUsage = () -> directMemoryUsage.get();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We need to check whether the directMemoryUsage is null before get

*/
private static AtomicLong directMemoryUsage;
private static Optional<BufferPoolMXBean> poolMxBeanOp;
private static Supplier<Long> getDirectMemoryUsage;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can it be final?

@hangc0276

Copy link
Copy Markdown
Contributor

ping @eolivelli @dlg99 @zymap @shoothzj, please help take a look, thanks.

# Conflicts:
#	stats/bookkeeper-stats-providers/prometheus-metrics-provider/src/main/java/org/apache/bookkeeper/stats/prometheus/PrometheusMetricsProvider.java
@horizonzy

Copy link
Copy Markdown
Member Author

rerun failure checks

poolMxBeanOp = Optional.empty();
AtomicLong tmpDirectMemoryUsage = null;
try {
Field field = PlatformDependent.class.getDeclaredField("DIRECT_MEMORY_COUNTER");

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.

Looks like they have the usedDirectMemory() method to access that field. Why don't we use that?

@eolivelli eolivelli left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

@eolivelli
eolivelli merged commit 5a38080 into apache:master Dec 7, 2022
hangc0276 pushed a commit that referenced this pull request Dec 8, 2022
hangc0276 added a commit to hangc0276/bookkeeper that referenced this pull request Dec 8, 2022
hangc0276 added a commit that referenced this pull request Dec 9, 2022
Descriptions of the changes in this PR:

### Motivation
Fix cherry-pick #3677 issue
nicoloboschi pushed a commit to datastax/bookkeeper that referenced this pull request Jan 11, 2023
nicoloboschi pushed a commit to datastax/bookkeeper that referenced this pull request Jan 11, 2023
Descriptions of the changes in this PR:

### Motivation
Fix cherry-pick apache#3677 issue

(cherry picked from commit b454014)
yaalsn pushed a commit to yaalsn/bookkeeper that referenced this pull request Jan 30, 2023
zymap pushed a commit that referenced this pull request Feb 16, 2023
Ghatage pushed a commit to sijie/bookkeeper that referenced this pull request Jul 12, 2024
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.

5 participants