-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "[WOR-1560] Upgrade to TCL 1.1.1 for MDC-aware Stairway (#418)"
This reverts commit 530a866. Reason: TCL's upgrade to k8s client 20.0.1 broke its KubePodListener, which could affect the recovery of Stairway flights.
- Loading branch information
1 parent
5c2fd3d
commit f89aa96
Showing
10 changed files
with
202 additions
and
38 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
27 changes: 27 additions & 0 deletions
27
service/src/main/java/bio/terra/profile/app/common/MDCHandlingException.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package bio.terra.profile.app.common; | ||
|
||
import bio.terra.common.exception.InternalServerErrorException; | ||
import java.util.List; | ||
|
||
public class MDCHandlingException extends InternalServerErrorException { | ||
|
||
public MDCHandlingException(String message) { | ||
super(message); | ||
} | ||
|
||
public MDCHandlingException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
|
||
public MDCHandlingException(Throwable cause) { | ||
super(cause); | ||
} | ||
|
||
public MDCHandlingException(String message, List<String> causes) { | ||
super(message, causes); | ||
} | ||
|
||
public MDCHandlingException(String message, Throwable cause, List<String> causes) { | ||
super(message, cause, causes); | ||
} | ||
} |
136 changes: 136 additions & 0 deletions
136
service/src/main/java/bio/terra/profile/app/common/MdcHook.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package bio.terra.profile.app.common; | ||
|
||
import bio.terra.stairway.FlightContext; | ||
import bio.terra.stairway.FlightMap; | ||
import bio.terra.stairway.HookAction; | ||
import bio.terra.stairway.StairwayHook; | ||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.core.type.TypeReference; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import jakarta.annotation.Nullable; | ||
import java.util.Map; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.slf4j.MDC; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* A {@link StairwayHook} for propagating MDC context across steps using the input {@link | ||
* FlightMap}. | ||
* | ||
* <p>This allows steps to have the same MDC context as when their flight was created. Note that any | ||
* modifications to the MDC context within a step are not propagated to other steps. | ||
*/ | ||
@Component | ||
public class MdcHook implements StairwayHook { | ||
private static final Logger logger = LoggerFactory.getLogger(MdcHook.class); | ||
private static final String FLIGHT_LOG_FORMAT = "Operation: {}, flightClass: {}, flightId: {}"; | ||
private static final String STEP_LOG_FORMAT = | ||
"Operation: {}, flightClass: {}, flightId: {}, stepClass: {}, stepIndex: {}, direction: {}"; | ||
|
||
/** The key to use in {@link FlightMap} for storing the MDC context. */ | ||
public static final String MDC_FLIGHT_MAP_KEY = "mdcKey"; | ||
|
||
private static final TypeReference<Map<String, String>> mapType = new TypeReference<>() {}; | ||
|
||
private final ObjectMapper objectMapper; | ||
|
||
@Autowired | ||
public MdcHook(ObjectMapper objectMapper) { | ||
this.objectMapper = objectMapper; | ||
} | ||
|
||
/** | ||
* Returns a serialized version of the current MDC context. | ||
* | ||
* <p>This is meant to be used to set up the initial {@link FlightMap} when a Flight is being | ||
* created to propagate the context of the flight creation to the steps. | ||
*/ | ||
public Object getSerializedCurrentContext() { | ||
return serializeMdc(MDC.getCopyOfContextMap()); | ||
} | ||
|
||
@Override | ||
public HookAction startStep(FlightContext flightContext) { | ||
logger.info( | ||
STEP_LOG_FORMAT, | ||
"startStep", | ||
flightContext.getFlightClassName(), | ||
flightContext.getFlightId(), | ||
flightContext.getStepClassName(), | ||
flightContext.getStepIndex(), | ||
flightContext.getDirection().name()); | ||
String serializedMdc = flightContext.getInputParameters().get(MDC_FLIGHT_MAP_KEY, String.class); | ||
// Note that this destroys any previous context on this thread. | ||
MDC.setContextMap(deserializeMdc(serializedMdc)); | ||
return HookAction.CONTINUE; | ||
} | ||
|
||
@Override | ||
public HookAction endStep(FlightContext flightContext) { | ||
logger.info( | ||
STEP_LOG_FORMAT, | ||
"endStep", | ||
flightContext.getFlightClassName(), | ||
flightContext.getFlightId(), | ||
flightContext.getStepClassName(), | ||
flightContext.getStepIndex(), | ||
flightContext.getDirection().name()); | ||
MDC.clear(); | ||
return HookAction.CONTINUE; | ||
} | ||
|
||
@Override | ||
public HookAction startFlight(FlightContext flightContext) { | ||
logger.info( | ||
FLIGHT_LOG_FORMAT, | ||
"startFlight", | ||
flightContext.getFlightClassName(), | ||
flightContext.getFlightId()); | ||
return HookAction.CONTINUE; | ||
} | ||
|
||
@Override | ||
public HookAction endFlight(FlightContext flightContext) { | ||
logger.info( | ||
FLIGHT_LOG_FORMAT, | ||
"endFlight", | ||
flightContext.getFlightClassName(), | ||
flightContext.getFlightId()); | ||
return HookAction.CONTINUE; | ||
} | ||
|
||
private String serializeMdc(@Nullable Map<String, String> mdcMap) { | ||
try { | ||
return objectMapper.writeValueAsString(mdcMap); | ||
} catch (JsonProcessingException e) { | ||
throw new MDCHandlingException( | ||
"Error serializing MDC map from string: " | ||
+ (mdcMap == null ? "null" : mdcMap.toString())); | ||
} | ||
} | ||
|
||
private Map<String, String> deserializeMdc(@Nullable String serializedMdc) { | ||
if (serializedMdc == null) { | ||
return Map.of(); | ||
} | ||
Map<String, String> mdcContext; | ||
try { | ||
mdcContext = objectMapper.readValue(serializedMdc, mapType); | ||
} catch (JsonProcessingException e) { | ||
throw new MDCHandlingException("Error deserializing MDC map from string: " + serializedMdc); | ||
} | ||
if (mdcContext == null) { | ||
return Map.of(); | ||
} | ||
return mdcContext; | ||
} | ||
|
||
@Override | ||
public HookAction stateTransition(FlightContext context) throws InterruptedException { | ||
logger.info( | ||
"Flight ID {} changed status to {}.", context.getFlightId(), context.getFlightStatus()); | ||
return HookAction.CONTINUE; | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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