From 1b2be490454048c4ac420c421196206aa005dc91 Mon Sep 17 00:00:00 2001 From: Sanne Grinovero Date: Mon, 13 Nov 2023 08:32:47 +0000 Subject: [PATCH] HHH-17347 Support for JDK which do not support JFR events --- gradle.properties | 2 +- hibernate-jfr/hibernate-jfr.gradle | 13 +++++-------- .../event/jfr/internal/JfrEventManager.java | 17 ++++++++++++----- .../hibernate/event/jfr/SessionEventTests.java | 13 ++++++++++--- 4 files changed, 28 insertions(+), 17 deletions(-) diff --git a/gradle.properties b/gradle.properties index 134b7bf890..8dfa7ab652 100644 --- a/gradle.properties +++ b/gradle.properties @@ -18,7 +18,7 @@ org.gradle.caching=true # but it won't find it and will fail. # It's just a JRE, so it's perfectly normal that the JDK is not present; # the JRE is under /usr/lib/jvm/java-1.8.0-openjdk-1.8.0.265.b01-1.fc32.x86_64/jre -org.gradle.java.installations.auto-detect=false +org.gradle.java.installations.auto-detect=true # We can't rely on Gradle's auto-download of JDKs as it doesn't support EA releases. # See https://github.com/gradle/gradle/blob/fc7ea24f3c525d8d12a4346eb0f15976a6be9414/subprojects/platform-jvm/src/main/java/org/gradle/jvm/toolchain/install/internal/AdoptOpenJdkRemoteBinary.java#L114 org.gradle.java.installations.auto-download=false diff --git a/hibernate-jfr/hibernate-jfr.gradle b/hibernate-jfr/hibernate-jfr.gradle index 80af21e991..cb8bc21129 100644 --- a/hibernate-jfr/hibernate-jfr.gradle +++ b/hibernate-jfr/hibernate-jfr.gradle @@ -16,12 +16,9 @@ dependencies { testImplementation testLibs.jfrUnit } - -// JfrUnit requires JDJ 17 -compileTestJava.onlyIf{ - jdkVersions.test.release.asInt() >= 17 && jdkVersions.explicit +test { + //Testing JFR events require JDK > 16 + javaLauncher = javaToolchains.launcherFor { + languageVersion = JavaLanguageVersion.of(17) + } } - -test.onlyIf { - jdkVersions.test.release.asInt() >= 17 && jdkVersions.explicit -} \ No newline at end of file diff --git a/hibernate-jfr/src/main/java/org/hibernate/event/jfr/internal/JfrEventManager.java b/hibernate-jfr/src/main/java/org/hibernate/event/jfr/internal/JfrEventManager.java index 9cc24bcb76..a5dc3eecfc 100644 --- a/hibernate-jfr/src/main/java/org/hibernate/event/jfr/internal/JfrEventManager.java +++ b/hibernate-jfr/src/main/java/org/hibernate/event/jfr/internal/JfrEventManager.java @@ -19,27 +19,34 @@ import org.hibernate.persister.collection.CollectionPersister; import org.hibernate.persister.entity.EntityPersister; import org.hibernate.stat.internal.StatsHelper; +import jdk.jfr.EventType; + import static java.util.concurrent.TimeUnit.NANOSECONDS; @AllowNonPortable public class JfrEventManager implements EventManager { + private static final EventType eventType = EventType.getEventType( SessionOpenEvent.class ); + @Override public SessionOpenEvent beginSessionOpenEvent() { - final SessionOpenEvent sessionOpenEvent = new SessionOpenEvent(); - if ( sessionOpenEvent.isEnabled() ) { + if ( eventType.isEnabled() ) { + final SessionOpenEvent sessionOpenEvent = new SessionOpenEvent(); sessionOpenEvent.begin(); + return sessionOpenEvent; + } + else { + return null; } - return sessionOpenEvent; } @Override public void completeSessionOpenEvent( HibernateEvent event, SharedSessionContractImplementor session) { - final SessionOpenEvent sessionOpenEvent = (SessionOpenEvent) event; - if ( sessionOpenEvent.isEnabled() ) { + if ( event != null ) { + final SessionOpenEvent sessionOpenEvent = (SessionOpenEvent) event; sessionOpenEvent.end(); if ( sessionOpenEvent.shouldCommit() ) { sessionOpenEvent.sessionIdentifier = getSessionIdentifier( session ); diff --git a/hibernate-jfr/src/test/java/org/hibernate/event/jfr/SessionEventTests.java b/hibernate-jfr/src/test/java/org/hibernate/event/jfr/SessionEventTests.java index 3ea5583aef..e16a688bde 100644 --- a/hibernate-jfr/src/test/java/org/hibernate/event/jfr/SessionEventTests.java +++ b/hibernate-jfr/src/test/java/org/hibernate/event/jfr/SessionEventTests.java @@ -30,15 +30,19 @@ import static org.assertj.core.api.Assertions.assertThat; @JfrEventTest @DomainModel @SessionFactory -public class SessionEventTests { +public class SessionEventTests { public JfrEvents jfrEvents = new JfrEvents(); @Test @EnableEvent(SessionOpenEvent.NAME) @EnableEvent(SessionClosedEvent.NAME) public void testSessionOpenEvent(SessionFactoryScope scope) { + jfrEvents.reset(); final String openedSessionIdentifier = scope.fromSession( (session) -> { - final List events = jfrEvents.events().filter( recordedEvent -> recordedEvent.hasField("sessionIdentifier" ) ).toList(); + final List events = jfrEvents.events().filter( recordedEvent -> { + String eventName = recordedEvent.getEventType().getName(); + return eventName.equals( SessionOpenEvent.NAME ); + } ).toList(); assertThat( events ).hasSize( 1 ); final RecordedEvent event = events.get( 0 ); assertThat( event.getEventType().getName() ).isEqualTo( SessionOpenEvent.NAME ); @@ -49,7 +53,10 @@ public class SessionEventTests { return event.getString( "sessionIdentifier" ); } ); - final List events = jfrEvents.events().filter( recordedEvent -> recordedEvent.hasField("sessionIdentifier" ) ).toList(); + final List events = jfrEvents.events().filter( recordedEvent -> { + String eventName = recordedEvent.getEventType().getName(); + return eventName.equals( SessionClosedEvent.NAME ); + } ).toList(); assertThat( events ).hasSize( 1 ); final RecordedEvent event = events.get( 0 ); assertThat( event.getEventType().getName() ).isEqualTo( SessionClosedEvent.NAME );