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.
# 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

View File

@ -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
}

View File

@ -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 );

View File

@ -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<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 );
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<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 );
final RecordedEvent event = events.get( 0 );
assertThat( event.getEventType().getName() ).isEqualTo( SessionClosedEvent.NAME );