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() {
if ( eventType.isEnabled() ) {
final SessionOpenEvent sessionOpenEvent = new SessionOpenEvent(); final SessionOpenEvent sessionOpenEvent = new SessionOpenEvent();
if ( sessionOpenEvent.isEnabled() ) {
sessionOpenEvent.begin(); sessionOpenEvent.begin();
}
return sessionOpenEvent; return sessionOpenEvent;
} }
else {
return null;
}
}
@Override @Override
public void completeSessionOpenEvent( public void completeSessionOpenEvent(
HibernateEvent event, HibernateEvent event,
SharedSessionContractImplementor session) { SharedSessionContractImplementor session) {
if ( event != null ) {
final SessionOpenEvent sessionOpenEvent = (SessionOpenEvent) event; final SessionOpenEvent sessionOpenEvent = (SessionOpenEvent) event;
if ( sessionOpenEvent.isEnabled() ) {
sessionOpenEvent.end(); sessionOpenEvent.end();
if ( sessionOpenEvent.shouldCommit() ) { if ( sessionOpenEvent.shouldCommit() ) {
sessionOpenEvent.sessionIdentifier = getSessionIdentifier( session ); sessionOpenEvent.sessionIdentifier = getSessionIdentifier( session );

View File

@ -37,8 +37,12 @@ public class SessionEventTests {
@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 );