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

Fix runtime-metrics-java17 tests on openj9 #8499

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,15 @@ public final class RuntimeMetrics implements Closeable {

private final AtomicBoolean isClosed = new AtomicBoolean();
private final OpenTelemetry openTelemetry;
private final List<RecordedEventHandler> recordedEventHandlers;
private final RecordingStream recordingStream;
private final CountDownLatch startUpLatch = new CountDownLatch(1);
private final List<AutoCloseable> observables = new ArrayList<>();
private final JfrRuntimeMetrics jfrRuntimeMetrics;

@SuppressWarnings("CatchingUnchecked")
RuntimeMetrics(
OpenTelemetry openTelemetry, Predicate<JfrFeature> featurePredicate, boolean disableJmx) {
this.openTelemetry = openTelemetry;
this.recordedEventHandlers = HandlerRegistry.getHandlers(openTelemetry, featurePredicate);
try {
recordingStream = new RecordingStream();
recordedEventHandlers.forEach(
handler -> {
EventSettings eventSettings = recordingStream.enable(handler.getEventName());
handler.getPollingDuration().ifPresent(eventSettings::withPeriod);
handler.getThreshold().ifPresent(eventSettings::withThreshold);
recordingStream.onEvent(handler.getEventName(), handler);
});
recordingStream.onMetadata(event -> startUpLatch.countDown());
Thread daemonRunner = new Thread(() -> recordingStream.start());
daemonRunner.setDaemon(true);
daemonRunner.start();
jfrRuntimeMetrics = JfrRuntimeMetrics.build(openTelemetry, featurePredicate);

// Set up metrics gathered by JMX
if (!disableJmx) {
Expand Down Expand Up @@ -98,18 +84,8 @@ OpenTelemetry getOpenTelemetry() {
}

// Visible for testing
List<RecordedEventHandler> getRecordedEventHandlers() {
return recordedEventHandlers;
}

// Visible for testing
RecordingStream getRecordingStream() {
return recordingStream;
}

// Visible for testing
CountDownLatch getStartUpLatch() {
return startUpLatch;
JfrRuntimeMetrics getJfrRuntimeMetrics() {
return jfrRuntimeMetrics;
}

/** Stop recording JFR events. */
Expand All @@ -119,9 +95,70 @@ public void close() {
logger.log(Level.WARNING, "RuntimeMetrics is already closed");
return;
}
recordingStream.close();
recordedEventHandlers.forEach(RecordedEventHandler::close);
if (jfrRuntimeMetrics != null) {
jfrRuntimeMetrics.close();
}

JmxRuntimeMetricsUtil.closeObservers(observables);
}

static class JfrRuntimeMetrics implements Closeable {
private final List<RecordedEventHandler> recordedEventHandlers;
private final RecordingStream recordingStream;
private final CountDownLatch startUpLatch = new CountDownLatch(1);

private JfrRuntimeMetrics(OpenTelemetry openTelemetry, Predicate<JfrFeature> featurePredicate) {
this.recordedEventHandlers = HandlerRegistry.getHandlers(openTelemetry, featurePredicate);
recordingStream = new RecordingStream();
recordedEventHandlers.forEach(
handler -> {
EventSettings eventSettings = recordingStream.enable(handler.getEventName());
handler.getPollingDuration().ifPresent(eventSettings::withPeriod);
handler.getThreshold().ifPresent(eventSettings::withThreshold);
recordingStream.onEvent(handler.getEventName(), handler);
});
recordingStream.onMetadata(event -> startUpLatch.countDown());
Thread daemonRunner = new Thread(() -> recordingStream.start());
daemonRunner.setDaemon(true);
daemonRunner.start();
}

static JfrRuntimeMetrics build(
OpenTelemetry openTelemetry, Predicate<JfrFeature> featurePredicate) {
if (!hasJfrRecordingStream()) {
return null;
}
return new JfrRuntimeMetrics(openTelemetry, featurePredicate);
}

@Override
public void close() {
recordingStream.close();
recordedEventHandlers.forEach(RecordedEventHandler::close);
}

// Visible for testing
List<RecordedEventHandler> getRecordedEventHandlers() {
return recordedEventHandlers;
}

// Visible for testing
RecordingStream getRecordingStream() {
return recordingStream;
}

// Visible for testing
CountDownLatch getStartUpLatch() {
return startUpLatch;
}

private static boolean hasJfrRecordingStream() {
try {
Class.forName("jdk.jfr.consumer.RecordingStream");
return true;
} catch (ClassNotFoundException e) {
return false;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ public void beforeEach(ExtensionContext context) throws InterruptedException {
RuntimeMetricsBuilder builder = RuntimeMetrics.builder(sdk);
builderConsumer.accept(builder);
runtimeMetrics = builder.build();
runtimeMetrics.getStartUpLatch().await(30, TimeUnit.SECONDS);
RuntimeMetrics.JfrRuntimeMetrics jfrRuntimeMetrics = runtimeMetrics.getJfrRuntimeMetrics();
if (jfrRuntimeMetrics != null) {
jfrRuntimeMetrics.getStartUpLatch().await(30, TimeUnit.SECONDS);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void build() {
var openTelemetry = OpenTelemetry.noop();
try (var jfrTelemetry = new RuntimeMetricsBuilder(openTelemetry).build()) {
assertThat(jfrTelemetry.getOpenTelemetry()).isSameAs(openTelemetry);
assertThat(jfrTelemetry.getRecordedEventHandlers())
assertThat(jfrTelemetry.getJfrRuntimeMetrics().getRecordedEventHandlers())
.hasSizeGreaterThan(0)
.allSatisfy(handler -> assertThat(handler.getFeature().isDefaultEnabled()).isTrue());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ void create_AllDisabled() {
void builder() {
try (var jfrTelemetry = RuntimeMetrics.builder(sdk).build()) {
assertThat(jfrTelemetry.getOpenTelemetry()).isSameAs(sdk);
assertThat(jfrTelemetry.getRecordedEventHandlers())
assertThat(jfrTelemetry.getJfrRuntimeMetrics().getRecordedEventHandlers())
.hasSizeGreaterThan(0)
.allSatisfy(handler -> assertThat(handler.getFeature().isDefaultEnabled()).isTrue());
}
Expand All @@ -82,7 +82,10 @@ void close() {
try (RuntimeMetrics jfrTelemetry = RuntimeMetrics.builder(sdk).build()) {
// Track whether RecordingStream has been closed
AtomicBoolean recordingStreamClosed = new AtomicBoolean(false);
jfrTelemetry.getRecordingStream().onClose(() -> recordingStreamClosed.set(true));
jfrTelemetry
.getJfrRuntimeMetrics()
.getRecordingStream()
.onClose(() -> recordingStreamClosed.set(true));

assertThat(reader.collectAllMetrics()).isNotEmpty();

Expand Down