Skip to content

Commit

Permalink
perf(orca): reuse ObjectMapper instances (#2937)
Browse files Browse the repository at this point in the history
creating a new ObjectMapper instance for each Stage or other per-request
contexts can be wasteful; ObjectMapper is safe to reuse across threads
(after it is configured). See
spinnaker/spinnaker#4367 for more details.
  • Loading branch information
mattnworb authored and ezimanyi committed May 30, 2019
1 parent cb0c8d9 commit 8835c07
Show file tree
Hide file tree
Showing 11 changed files with 23 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import static com.netflix.spinnaker.orca.pipeline.model.Execution.ExecutionType.
*/
trait DeploymentDetailsAware {

private ObjectMapper pipelineObjectMapper = OrcaObjectMapper.newInstance()
private ObjectMapper pipelineObjectMapper = OrcaObjectMapper.getInstance()

void withImageFromPrecedingStage(
Stage stage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public Scheduler scheduler() {

@Bean
public ObjectMapper mapper() {
return OrcaObjectMapper.newInstance();
return OrcaObjectMapper.getInstance();
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
public class OrcaObjectMapper {
private OrcaObjectMapper() {}

private static final ObjectMapper INSTANCE = newInstance();

public static ObjectMapper newInstance() {
ObjectMapper instance = new ObjectMapper();
instance.registerModule(new Jdk8Module());
Expand All @@ -41,4 +43,15 @@ public static ObjectMapper newInstance() {
instance.setSerializationInclusion(NON_NULL);
return instance;
}

/**
* Return an ObjectMapper instance that can be reused. Do not change the configuration of this
* instance as it will be shared across the entire application, use {@link #newInstance()}
* instead.
*
* @return Reusable ObjectMapper instance
*/
public static ObjectMapper getInstance() {
return INSTANCE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ public <O> O mapTo(Class<O> type) {
return mapTo(null, type);
}

@JsonIgnore private final transient ObjectMapper objectMapper = OrcaObjectMapper.newInstance();
@JsonIgnore private final transient ObjectMapper objectMapper = OrcaObjectMapper.getInstance();

/**
* Maps the stage's context to a typed object at a provided pointer. Uses <a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
public class BuildDetailExtractor {

private final List<DetailExtractor> detailExtractors;
private final ObjectMapper mapper = OrcaObjectMapper.newInstance();
private final ObjectMapper mapper = OrcaObjectMapper.getInstance();

public BuildDetailExtractor() {
this.detailExtractors =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class ContextParameterProcessor {

private final Logger log = LoggerFactory.getLogger(getClass());

private static final ObjectMapper mapper = OrcaObjectMapper.newInstance();
private static final ObjectMapper mapper = OrcaObjectMapper.getInstance();

private ExpressionEvaluator expressionEvaluator;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
@Component
public class ServiceKeyExpressionFunctionProvider implements ExpressionFunctionProvider {
private static final String CREATE_SERVICE_KEY_STAGE_NAME = "createServiceKey";
private static final ObjectMapper objectMapper = OrcaObjectMapper.newInstance();
private static final ObjectMapper objectMapper = OrcaObjectMapper.getInstance();

@Nullable
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class PipelineTemplateService {

private final Renderer renderer;

private final ObjectMapper mapper = OrcaObjectMapper.newInstance();
private final ObjectMapper mapper = OrcaObjectMapper.getInstance();

@Autowired
public PipelineTemplateService(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ interface ExpressionAware {
val contextParameterProcessor: ContextParameterProcessor

companion object {
val mapper: ObjectMapper = OrcaObjectMapper.newInstance()
val mapper: ObjectMapper = OrcaObjectMapper.getInstance()
}

val log: Logger
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public class RedisExecutionRepository implements ExecutionRepository {

private final RedisClientDelegate redisClientDelegate;
private final Optional<RedisClientDelegate> previousRedisClientDelegate;
private final ObjectMapper mapper = OrcaObjectMapper.newInstance();
private final ObjectMapper mapper = OrcaObjectMapper.getInstance();
private final int chunkSize;
private final Scheduler queryAllScheduler;
private final Scheduler queryByAppScheduler;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class WebConfiguration extends WebMvcConfigurerAdapter {
}

@Bean(name = "objectMapper", autowire = Autowire.BY_TYPE) ObjectMapper orcaObjectMapper() {
OrcaObjectMapper.newInstance()
OrcaObjectMapper.getInstance()
}

@Bean
Expand Down

0 comments on commit 8835c07

Please sign in to comment.