#126 - add initialDelay to SSE scheduler to give server's handshake processing time#189
Open
xsalefter wants to merge 1 commit intokillbill:masterfrom
Open
#126 - add initialDelay to SSE scheduler to give server's handshake processing time#189xsalefter wants to merge 1 commit intokillbill:masterfrom
xsalefter wants to merge 1 commit intokillbill:masterfrom
Conversation
…ng time LogsSseHandler scheduled its drain loop with an initial delay of zero, so the background thread could publish events while the servlet container was still completing the SSE handshake. In that window the channel remains in its blocking phase (asyncStarted could be `true`, but the response isn’t committed yet), and the container raises exception (IllegalStateException: api=BLOCKED in Jetty) when we write.
Contributor
Author
|
Now this video show what happened if we set the
platform-126-SSE-with-initial-delay.webm |
Contributor
Author
|
Add compile dependency to jetty (for debugging purpose) raise another OSGi issue. So I have to use reflection to get Jetty classes. Also, using debugger and set breakpoint is not working as well (I never get the expected exception). Probably because the server already have in correct state when the line of code hit breakpoint. private void debug(final Sse sse) {
if (!(sse instanceof ServletSse)) {
logger.info(">>>>> SSE instance is not ServletSse: {}", sse.getClass().getName());
return;
}
try {
final ServletSse servletSse = (ServletSse) sse;
final Field reqField = ServletSse.class.getDeclaredField("req");
reqField.setAccessible(true);
HttpServletRequest rawRequest = (HttpServletRequest) reqField.get(servletSse);
while (rawRequest instanceof HttpServletRequestWrapper) {
rawRequest = (HttpServletRequest) ((HttpServletRequestWrapper) rawRequest).getRequest();
}
final Object state = extractChannelState(rawRequest);
if (state == null) {
logger.info(">>>>> Jetty HttpChannelState unavailable for {} (loader {})",
rawRequest.getClass().getName(),
rawRequest.getClass().getClassLoader());
return;
}
logChannelState(state);
} catch (final ReflectiveOperationException e) {
logger.warn(">>>>> Failed to inspect Jetty request state", e);
} catch (final Exception e) {
logger.warn(">>>>> Unexpected failure while inspecting Jetty request state", e);
}
}
private Object extractChannelState(final HttpServletRequest rawRequest) throws ReflectiveOperationException {
// First, try to interact with the server's Jetty classes via reflection to avoid class loader mismatches.
final Class<?> requestClass = rawRequest.getClass();
try {
final java.lang.reflect.Method method = requestClass.getMethod("getHttpChannelState");
return method.invoke(rawRequest);
} catch (final NoSuchMethodException ignored) {
// fall back to Jetty API if the method isn't present
}
// If the container request exposes Jetty 10 APIs, use them directly.
try {
final Class<?> jettyRequestClass = Class.forName("org.eclipse.jetty.server.Request", false, requestClass.getClassLoader());
if (jettyRequestClass.isInstance(rawRequest)) {
final java.lang.reflect.Method getChannelState = jettyRequestClass.getMethod("getHttpChannelState");
return getChannelState.invoke(rawRequest);
}
} catch (final ClassNotFoundException cnfe) {
// Jetty classes are not present in this class loader
logger.error(">>>>> Jetty Request class not found in class loader: ", cnfe);
}
return null;
}
private void logChannelState(final Object state) throws ReflectiveOperationException {
final Class<?> stateClass = state.getClass();
final java.lang.reflect.Method getState = stateClass.getMethod("getState");
final java.lang.reflect.Method isAsyncStarted = stateClass.getMethod("isAsyncStarted");
final java.lang.reflect.Method isResponseCommitted = stateClass.getMethod("isResponseCommitted");
logger.info(">>>>> jettyState={}, asyncStarted={}, responseCommitted={}",
getState.invoke(state),
isAsyncStarted.invoke(state),
isResponseCommitted.invoke(state));
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Original ticket : #126 . This PR also follow-up for this PR in Kill Bill repository : killbill/killbill#2169
LogsSseHandlerscheduled its drain loop withinitialDelay=0, so the background thread could publish events while the servlet container was still completing the SSE handshake. In that window the channel remains in its blocking phase (asyncStarted could betrue, but the response isn’t committed yet), and the container throw exception (IllegalStateException: api=BLOCKED in Jetty) when we write.Video below shows what exactly happened:
curlcalls to SSE endpointjettyState=HANDLING .... responseCommitted=falsemeaning the server still processing the handshake/request, and response not committed yet.platform-126-SSE-without-initial-delay.webm
To be continued in comment ....