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

Split host-level execution plan into two parts #1282

Merged
merged 1 commit into from
May 24, 2021
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
Expand Up @@ -13,6 +13,7 @@
import org.prebid.server.execution.Timeout;
import org.prebid.server.execution.TimeoutFactory;
import org.prebid.server.hooks.execution.model.EndpointExecutionPlan;
import org.prebid.server.hooks.execution.model.ExecutionGroup;
import org.prebid.server.hooks.execution.model.ExecutionPlan;
import org.prebid.server.hooks.execution.model.HookExecutionContext;
import org.prebid.server.hooks.execution.model.HookId;
Expand Down Expand Up @@ -42,41 +43,49 @@
import org.prebid.server.settings.model.AccountHooksConfiguration;

import java.time.Clock;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class HookStageExecutor {

private final ExecutionPlan executionPlan;
private final ExecutionPlan hostExecutionPlan;
private final ExecutionPlan defaultAccountExecutionPlan;
private final HookCatalog hookCatalog;
private final TimeoutFactory timeoutFactory;
private final Vertx vertx;
private final Clock clock;

private HookStageExecutor(ExecutionPlan executionPlan,
private HookStageExecutor(ExecutionPlan hostExecutionPlan,
ExecutionPlan defaultAccountExecutionPlan,
HookCatalog hookCatalog,
TimeoutFactory timeoutFactory,
Vertx vertx,
Clock clock) {

this.executionPlan = executionPlan;
this.hostExecutionPlan = hostExecutionPlan;
this.defaultAccountExecutionPlan = defaultAccountExecutionPlan;
this.hookCatalog = hookCatalog;
this.timeoutFactory = timeoutFactory;
this.vertx = vertx;
this.clock = clock;
}

public static HookStageExecutor create(String executionPlan,
public static HookStageExecutor create(String hostExecutionPlan,
String defaultAccountExecutionPlan,
HookCatalog hookCatalog,
TimeoutFactory timeoutFactory,
Vertx vertx,
Clock clock,
JacksonMapper mapper) {

return new HookStageExecutor(
parseExecutionPlan(executionPlan, Objects.requireNonNull(mapper)),
parseExecutionPlan(hostExecutionPlan, Objects.requireNonNull(mapper)),
parseExecutionPlan(defaultAccountExecutionPlan, mapper),
Objects.requireNonNull(hookCatalog),
Objects.requireNonNull(timeoutFactory),
Objects.requireNonNull(vertx),
Expand Down Expand Up @@ -224,7 +233,7 @@ private <PAYLOAD, CONTEXT extends InvocationContext> StageExecutor<PAYLOAD, CONT

return this.<PAYLOAD, CONTEXT>stageExecutor()
.withStage(stage)
.withExecutionPlan(planFor(account, endpoint, stage))
.withExecutionPlan(planForStage(account, endpoint, stage))
.withHookExecutionContext(context);
}

Expand All @@ -241,14 +250,34 @@ private static ExecutionPlan parseExecutionPlan(String executionPlan, JacksonMap
}

private StageExecutionPlan planForEntrypointStage(Endpoint endpoint) {
return planFor(executionPlan, endpoint, Stage.entrypoint);
return effectiveStagePlanFrom(ExecutionPlan.empty(), endpoint, Stage.entrypoint);
}

private StageExecutionPlan planFor(Account account, Endpoint endpoint, Stage stage) {
return planFor(effectiveExecutionPlanFor(account), endpoint, stage);
private StageExecutionPlan planForStage(Account account, Endpoint endpoint, Stage stage) {
return effectiveStagePlanFrom(effectiveExecutionPlanFor(account), endpoint, stage);
}

private static StageExecutionPlan planFor(ExecutionPlan executionPlan, Endpoint endpoint, Stage stage) {
private StageExecutionPlan effectiveStagePlanFrom(
ExecutionPlan accountExecutionPlan, Endpoint endpoint, Stage stage) {

final StageExecutionPlan hostStageExecutionPlan = stagePlanFrom(hostExecutionPlan, endpoint, stage);
final StageExecutionPlan accountStageExecutionPlan = stagePlanFrom(accountExecutionPlan, endpoint, stage);

if (hostStageExecutionPlan.isEmpty()) {
return accountStageExecutionPlan;
} else if (accountStageExecutionPlan.isEmpty()) {
return hostStageExecutionPlan;
}

final List<ExecutionGroup> combinedGroups = Stream.of(hostStageExecutionPlan, accountStageExecutionPlan)
.map(StageExecutionPlan::getGroups)
.flatMap(Collection::stream)
.collect(Collectors.toList());

return StageExecutionPlan.of(combinedGroups);
}

private static StageExecutionPlan stagePlanFrom(ExecutionPlan executionPlan, Endpoint endpoint, Stage stage) {
return executionPlan
.getEndpoints()
.getOrDefault(endpoint, EndpointExecutionPlan.empty())
Expand All @@ -261,7 +290,7 @@ private ExecutionPlan effectiveExecutionPlanFor(Account account) {
final ExecutionPlan accountExecutionPlan =
hooksAccountConfig != null ? hooksAccountConfig.getExecutionPlan() : null;

return accountExecutionPlan != null ? accountExecutionPlan : executionPlan;
return accountExecutionPlan != null ? accountExecutionPlan : defaultAccountExecutionPlan;
}

private InvocationContextProvider<InvocationContext> invocationContextProvider(Endpoint endpoint) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Value(staticConstructor = "of")
public class StageExecutionPlan {
Expand All @@ -15,4 +16,8 @@ public class StageExecutionPlan {
public static StageExecutionPlan empty() {
return EMPTY;
}

public boolean isEmpty() {
return Objects.equals(this, EMPTY);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,13 @@ HookStageExecutor hookStageExecutor(HooksConfigurationProperties hooksConfigurat
JacksonMapper mapper) {

return HookStageExecutor.create(
hooksConfiguration.getExecutionPlan(), hookCatalog, timeoutFactory, vertx, clock, mapper);
hooksConfiguration.getHostExecutionPlan(),
hooksConfiguration.getDefaultAccountExecutionPlan(),
hookCatalog,
timeoutFactory,
vertx,
clock,
mapper);
}

@Bean
Expand All @@ -47,6 +53,8 @@ HooksConfigurationProperties hooksConfigurationProperties() {
@NoArgsConstructor
private static class HooksConfigurationProperties {

String executionPlan;
String hostExecutionPlan;

String defaultAccountExecutionPlan;
}
}
Loading