-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
AJ-1550: handle errors during startup #57
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,39 @@ | ||
package org.broadinstitute.listener; | ||
|
||
import org.broadinstitute.listener.relay.ListenerException; | ||
import org.broadinstitute.listener.relay.transport.RelayedRequestPipeline; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.boot.availability.AvailabilityChangeEvent; | ||
import org.springframework.boot.availability.LivenessState; | ||
import org.springframework.boot.context.event.ApplicationReadyEvent; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.event.EventListener; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class StartupHandler { | ||
|
||
private final RelayedRequestPipeline relayedRequestPipeline; | ||
private final ApplicationContext context; | ||
|
||
public StartupHandler(RelayedRequestPipeline relayedRequestPipeline) { | ||
private static final Logger logger = LoggerFactory.getLogger(StartupHandler.class); | ||
|
||
public StartupHandler(RelayedRequestPipeline relayedRequestPipeline, ApplicationContext context) { | ||
this.relayedRequestPipeline = relayedRequestPipeline; | ||
this.context = context; | ||
} | ||
|
||
@EventListener(ApplicationReadyEvent.class) | ||
public void startProcessingRequests() { | ||
this.relayedRequestPipeline.processRelayedRequests(); | ||
@SuppressWarnings("java:S1181") // intentionally catches Throwable, not Exception | ||
public void startProcessingRequests() throws ListenerException { | ||
logger.info("Starting pipeline to process relayed requests ... "); | ||
try { | ||
this.relayedRequestPipeline.processRelayedRequests(); | ||
logger.info("Relayed requests pipeline started."); | ||
} catch (Throwable t) { | ||
AvailabilityChangeEvent.publish(context, LivenessState.BROKEN); | ||
throw new ListenerException("Error during listener startup: " + t.getMessage(), t); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package org.broadinstitute.listener.relay; | ||
|
||
public class ListenerException extends Exception { | ||
|
||
public ListenerException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
public ListenerException(String message) { | ||
super(message); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -90,7 +90,7 @@ public Flux<RelayedHttpListenerContext> receiveRelayedHttpRequests() { | |
public Mono<String> openConnection() { | ||
return Mono.create( | ||
sink -> { | ||
logger.debug("Opening connection to Azure Relay."); | ||
logger.info("Opening connection to Azure Relay."); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this will run / be shown for every connection being open. this opens the opportunity to be verbose in our logs. couple of things ...
Trying to make the message as actionable as possible to help us correlate this both upstream and downstream at the INFO level, otherwise I would suggest we move it back to DEBUG There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, that is not my intent. I thought this was only called at startup, when the listener attaches to the Relay, and not for each individual request from end users. Since this PR is focused on that startup/attachment, I intended to increase logging at that point only. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cpate4 I
but I do not see them repeated for each request. Does this change your stance? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think I said it, "will run for every connection being opened", not for every request. That being said, I am slow (it's Friday) - I believe this listener is specific to a single user (i.e. meaning we are deploying one of these for each data plane set of apps, not sharing an AzureRelayListener, and not having to pull apart logs to separate out my requests from your requests) - so, yeah, should be good on the DEBUG/INFO level. That being said, happy to leave them, and if we get to a point where they are noisy, we can handle it then. Thank you for entertaining me on this! - 🤗 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry for the misunderstanding about connections vs requests! Cool, I will forge ahead with this and we can always pull it back if it's noisy, as you said. |
||
listener | ||
.openAsync() | ||
.whenComplete( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,10 +41,10 @@ public RelayedRequestPipeline( | |
} | ||
|
||
public void processRelayedRequests() { | ||
logger.debug("Registering HTTP pipeline"); | ||
logger.info("Registering HTTP pipeline"); | ||
registerHttpExecutionPipeline(Schedulers.boundedElastic()); | ||
|
||
logger.debug("Registering WebSocket upgrades pipeline"); | ||
logger.info("Registering WebSocket upgrades pipeline"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same comments re: walking through this and identifying usefulness of these logs |
||
webSocketConnectionsHandler | ||
.acceptHttpUpgradeRequests() | ||
.subscribe( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,13 +3,19 @@ | |
import static org.hamcrest.CoreMatchers.equalTo; | ||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.Mockito.doNothing; | ||
import static org.mockito.Mockito.doThrow; | ||
import static org.mockito.Mockito.when; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
|
||
import com.microsoft.azure.relay.HybridConnectionListener; | ||
import org.broadinstitute.listener.StartupHandler; | ||
import org.broadinstitute.listener.relay.ListenerException; | ||
import org.broadinstitute.listener.relay.health.HybridConnectionListenerHealth; | ||
import org.broadinstitute.listener.relay.transport.RelayedRequestPipeline; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
@@ -39,12 +45,18 @@ class AvailabilityTest { | |
@Autowired private ApplicationContext context; | ||
@Autowired private ApplicationAvailability applicationAvailability; | ||
@Autowired private HybridConnectionListenerHealth hybridConnectionListenerHealth; | ||
@Autowired private StartupHandler startupHandler; | ||
|
||
@MockBean private HybridConnectionListener hybridConnectionListener; | ||
@MockBean private RelayedRequestPipeline relayedRequestPipeline; | ||
|
||
@BeforeEach | ||
void beforeEach() { | ||
when(hybridConnectionListener.isOnline()).thenReturn(true); | ||
doNothing().when(relayedRequestPipeline).processRelayedRequests(); | ||
// set liveness and readiness to ok before each test; prevents crosstalk between tests | ||
AvailabilityChangeEvent.publish(context, LivenessState.CORRECT); | ||
AvailabilityChangeEvent.publish(context, ReadinessState.ACCEPTING_TRAFFIC); | ||
} | ||
|
||
// Reference: https://www.baeldung.com/spring-liveness-readiness-probes | ||
|
@@ -127,4 +139,36 @@ void hybridConnectionListenerHealth() throws Exception { | |
applicationAvailability.getLivenessState(), | ||
equalTo(LivenessState.CORRECT)); | ||
} | ||
|
||
/** | ||
* Verify that an error during startup sets liveness state to broken. | ||
* | ||
* @throws Exception on exception in the test | ||
*/ | ||
@Test | ||
void processRelayedRequestsError() throws Exception { | ||
// livenessState and liveness probe should both be ok, to start | ||
assertThat( | ||
"Liveness state should be CORRECT", | ||
applicationAvailability.getLivenessState(), | ||
equalTo(LivenessState.CORRECT)); | ||
ResultActions livenessResult = mvc.perform(get("/actuator/health/liveness")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is this liveness probe called twice? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. because of a copy-paste error on my part! Good catch, thank you; fixed. |
||
livenessResult.andExpect(status().isOk()).andExpect(jsonPath("$.status").value("UP")); | ||
|
||
// mock processRelayedRequests to throw an exception | ||
doThrow(new RuntimeException("intentional failure for unit test")) | ||
.when(relayedRequestPipeline) | ||
.processRelayedRequests(); | ||
// and re-run the startup handler, mimicking startup | ||
assertThrows(ListenerException.class, () -> startupHandler.startProcessingRequests()); | ||
|
||
// liveness state and liveness probe should now show failure | ||
assertThat( | ||
"Liveness state should be BROKEN", | ||
applicationAvailability.getLivenessState(), | ||
equalTo(LivenessState.BROKEN)); | ||
mvc.perform(get("/actuator/health/liveness")) | ||
.andExpect(status().isServiceUnavailable()) | ||
.andExpect(jsonPath("$.status").value("DOWN")); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See other comments re: INFO level and log-message usability in production