Skip to content
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

Merged
merged 3 commits into from
Jan 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.");
Copy link
Contributor

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

} 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
Expand Up @@ -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.");
Copy link
Contributor

Choose a reason for hiding this comment

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

  • Is there additional information in our logs that will tie this to a series of requests to make the logs useful? (thinking trace-id, etc)
  • Is there additional information we can add to this to help us correlate that this message was for X, Y, Z?

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cpate4 I kubectl-ed into an existing listener deployment (i.e. prior to this PR) and manually set LOGGING_LEVEL_ORG_BROADINSTITUTE_LISTENER to DEBUG. Upon restart of that listener, I see these lines logged at startup:

2024-01-26 18:17:36.745   INFO 1 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2024-01-26 18:17:36.941   INFO 1 --- [  restartedMain] o.b.listener.ListenerApplication         : Started ListenerApplication in 38.997 seconds (JVM running for 45.53)
2024-01-26 18:17:37.437   INFO 1 --- [  restartedMain] o.b.l.relay.http.AvailabilityListener    : LivenessState: CORRECT
2024-01-26 18:17:37.443  DEBUG 1 --- [  restartedMain] o.b.l.r.t.RelayedRequestPipeline         : Registering HTTP pipeline
2024-01-26 18:17:37.838  DEBUG 1 --- [  restartedMain] o.b.l.r.t.RelayedRequestPipeline         : Registering WebSocket upgrades pipeline
2024-01-26 18:17:37.840  DEBUG 1 --- [  restartedMain] o.b.l.r.http.ListenerConnectionHandler   : Opening connection to Azure Relay.

but I do not see them repeated for each request. Does this change your stance?

Copy link
Contributor

Choose a reason for hiding this comment

The 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! - 🤗

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Copy link
Contributor

Choose a reason for hiding this comment

The 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this liveness probe called twice?

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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"));
}
}
Loading