HHH-17347 Support for JDK which do not support JFR events

This commit is contained in:
Sanne Grinovero 2023-11-13 08:32:47 +00:00 committed by Steve Ebersole
parent 16eecea9d0
commit 1b2be49045
4 changed files with 28 additions and 17 deletions

View File

@ -18,7 +18,7 @@ org.gradle.caching=true
# but it won't find it and will fail. # 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; # 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 # 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. # 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 # 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 org.gradle.java.installations.auto-download=false

View File

@ -16,12 +16,9 @@ dependencies {
testImplementation testLibs.jfrUnit testImplementation testLibs.jfrUnit
} }
test {
// JfrUnit requires JDJ 17 //Testing JFR events require JDK > 16
compileTestJava.onlyIf{ javaLauncher = javaToolchains.launcherFor {
jdkVersions.test.release.asInt() >= 17 && jdkVersions.explicit languageVersion = JavaLanguageVersion.of(17)
}
} }
test.onlyIf {
jdkVersions.test.release.asInt() >= 17 && jdkVersions.explicit
}

View File

@ -19,27 +19,34 @@ import org.hibernate.persister.collection.CollectionPersister;
import org.hibernate.persister.entity.EntityPersister; import org.hibernate.persister.entity.EntityPersister;
import org.hibernate.stat.internal.StatsHelper; import org.hibernate.stat.internal.StatsHelper;
import jdk.jfr.EventType;
import static java.util.concurrent.TimeUnit.NANOSECONDS; import static java.util.concurrent.TimeUnit.NANOSECONDS;
@AllowNonPortable @AllowNonPortable
public class JfrEventManager implements EventManager { public class JfrEventManager implements EventManager {
private static final EventType eventType = EventType.getEventType( SessionOpenEvent.class );
@Override @Override
public SessionOpenEvent beginSessionOpenEvent() { public SessionOpenEvent beginSessionOpenEvent() {
final SessionOpenEvent sessionOpenEvent = new SessionOpenEvent(); if ( eventType.isEnabled() ) {
if ( sessionOpenEvent.isEnabled() ) { final SessionOpenEvent sessionOpenEvent = new SessionOpenEvent();
sessionOpenEvent.begin(); sessionOpenEvent.begin();
return sessionOpenEvent;
}
else {
return null;
} }
return sessionOpenEvent;
} }
@Override @Override
public void completeSessionOpenEvent( public void completeSessionOpenEvent(
HibernateEvent event, HibernateEvent event,
SharedSessionContractImplementor session) { SharedSessionContractImplementor session) {
final SessionOpenEvent sessionOpenEvent = (SessionOpenEvent) event; if ( event != null ) {
if ( sessionOpenEvent.isEnabled() ) { final SessionOpenEvent sessionOpenEvent = (SessionOpenEvent) event;
sessionOpenEvent.end(); sessionOpenEvent.end();
if ( sessionOpenEvent.shouldCommit() ) { if ( sessionOpenEvent.shouldCommit() ) {
sessionOpenEvent.sessionIdentifier = getSessionIdentifier( session ); sessionOpenEvent.sessionIdentifier = getSessionIdentifier( session );

View File

@ -30,15 +30,19 @@ import static org.assertj.core.api.Assertions.assertThat;
@JfrEventTest @JfrEventTest
@DomainModel @DomainModel
@SessionFactory @SessionFactory
public class SessionEventTests { public class SessionEventTests {
public JfrEvents jfrEvents = new JfrEvents(); public JfrEvents jfrEvents = new JfrEvents();
@Test @Test
@EnableEvent(SessionOpenEvent.NAME) @EnableEvent(SessionOpenEvent.NAME)
@EnableEvent(SessionClosedEvent.NAME) @EnableEvent(SessionClosedEvent.NAME)
public void testSessionOpenEvent(SessionFactoryScope scope) { public void testSessionOpenEvent(SessionFactoryScope scope) {
jfrEvents.reset();
final String openedSessionIdentifier = scope.fromSession( (session) -> { final String openedSessionIdentifier = scope.fromSession( (session) -> {
final List<RecordedEvent> events = jfrEvents.events().filter( recordedEvent -> recordedEvent.hasField("sessionIdentifier" ) ).toList(); final List<RecordedEvent> events = jfrEvents.events().filter( recordedEvent -> {
String eventName = recordedEvent.getEventType().getName();
return eventName.equals( SessionOpenEvent.NAME );
} ).toList();
assertThat( events ).hasSize( 1 ); assertThat( events ).hasSize( 1 );
final RecordedEvent event = events.get( 0 ); final RecordedEvent event = events.get( 0 );
assertThat( event.getEventType().getName() ).isEqualTo( SessionOpenEvent.NAME ); assertThat( event.getEventType().getName() ).isEqualTo( SessionOpenEvent.NAME );
@ -49,7 +53,10 @@ public class SessionEventTests {
return event.getString( "sessionIdentifier" ); return event.getString( "sessionIdentifier" );
} ); } );
final List<RecordedEvent> events = jfrEvents.events().filter( recordedEvent -> recordedEvent.hasField("sessionIdentifier" ) ).toList(); final List<RecordedEvent> events = jfrEvents.events().filter( recordedEvent -> {
String eventName = recordedEvent.getEventType().getName();
return eventName.equals( SessionClosedEvent.NAME );
} ).toList();
assertThat( events ).hasSize( 1 ); assertThat( events ).hasSize( 1 );
final RecordedEvent event = events.get( 0 ); final RecordedEvent event = events.get( 0 );
assertThat( event.getEventType().getName() ).isEqualTo( SessionClosedEvent.NAME ); assertThat( event.getEventType().getName() ).isEqualTo( SessionClosedEvent.NAME );