|
| 1 | +package org.prebid.server.hooks.modules.rule.engine.core.config; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.JsonNode; |
| 4 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 5 | +import com.github.benmanes.caffeine.cache.Caffeine; |
| 6 | +import io.vertx.core.Future; |
| 7 | +import io.vertx.core.Vertx; |
| 8 | +import org.apache.commons.lang3.ObjectUtils; |
| 9 | +import org.prebid.server.execution.retry.RetryPolicy; |
| 10 | +import org.prebid.server.execution.retry.Retryable; |
| 11 | +import org.prebid.server.hooks.modules.rule.engine.core.rules.PerStageRule; |
| 12 | +import org.prebid.server.log.Logger; |
| 13 | +import org.prebid.server.log.LoggerFactory; |
| 14 | + |
| 15 | +import java.time.Clock; |
| 16 | +import java.time.Duration; |
| 17 | +import java.time.Instant; |
| 18 | +import java.time.format.DateTimeParseException; |
| 19 | +import java.util.Map; |
| 20 | +import java.util.Objects; |
| 21 | +import java.util.Optional; |
| 22 | +import java.util.concurrent.TimeUnit; |
| 23 | + |
| 24 | +public class RuleParser { |
| 25 | + |
| 26 | + private static final Logger logger = LoggerFactory.getLogger(RuleParser.class); |
| 27 | + |
| 28 | + private final AccountConfigParser parser; |
| 29 | + private final Vertx vertx; |
| 30 | + private final Clock clock; |
| 31 | + |
| 32 | + private final RetryPolicy retryPolicy; |
| 33 | + |
| 34 | + private final Map<String, ParsingAttempt> accountIdToParsingAttempt; |
| 35 | + private final Map<String, PerStageRule> accountIdToRules; |
| 36 | + |
| 37 | + public RuleParser(long cacheExpireAfterMinutes, |
| 38 | + long cacheMaxSize, |
| 39 | + RetryPolicy retryPolicy, |
| 40 | + AccountConfigParser parser, |
| 41 | + Vertx vertx, |
| 42 | + Clock clock) { |
| 43 | + |
| 44 | + this.parser = Objects.requireNonNull(parser); |
| 45 | + this.vertx = Objects.requireNonNull(vertx); |
| 46 | + this.clock = Objects.requireNonNull(clock); |
| 47 | + this.retryPolicy = Objects.requireNonNull(retryPolicy); |
| 48 | + |
| 49 | + this.accountIdToParsingAttempt = Caffeine.newBuilder() |
| 50 | + .expireAfterAccess(cacheExpireAfterMinutes, TimeUnit.MINUTES) |
| 51 | + .maximumSize(cacheMaxSize) |
| 52 | + .<String, ParsingAttempt>build() |
| 53 | + .asMap(); |
| 54 | + |
| 55 | + this.accountIdToRules = Caffeine.newBuilder() |
| 56 | + .expireAfterAccess(cacheExpireAfterMinutes, TimeUnit.MINUTES) |
| 57 | + .maximumSize(cacheMaxSize) |
| 58 | + .<String, PerStageRule>build() |
| 59 | + .asMap(); |
| 60 | + } |
| 61 | + |
| 62 | + public Future<PerStageRule> parseForAccount(String accountId, ObjectNode config) { |
| 63 | + final PerStageRule cachedRule = accountIdToRules.get(accountId); |
| 64 | + |
| 65 | + if (cachedRule != null && cachedRule.timestamp().compareTo(getConfigTimestamp(config)) >= 0) { |
| 66 | + return Future.succeededFuture(cachedRule); |
| 67 | + } |
| 68 | + |
| 69 | + parseConfig(accountId, config); |
| 70 | + return Future.succeededFuture(ObjectUtils.defaultIfNull(cachedRule, PerStageRule.noOp())); |
| 71 | + } |
| 72 | + |
| 73 | + private Instant getConfigTimestamp(ObjectNode config) { |
| 74 | + try { |
| 75 | + return Optional.of(config) |
| 76 | + .map(node -> node.get("timestamp")) |
| 77 | + .filter(JsonNode::isTextual) |
| 78 | + .map(JsonNode::asText) |
| 79 | + .map(Instant::parse) |
| 80 | + .orElse(Instant.EPOCH); |
| 81 | + } catch (DateTimeParseException exception) { |
| 82 | + return Instant.EPOCH; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private void parseConfig(String accountId, ObjectNode config) { |
| 87 | + final Instant now = clock.instant(); |
| 88 | + final ParsingAttempt attempt = accountIdToParsingAttempt.compute( |
| 89 | + accountId, (ignored, previousAttempt) -> tryRegisteringNewAttempt(previousAttempt, now)); |
| 90 | + |
| 91 | + // reference equality used on purpose - if references are equal - then we should parse |
| 92 | + if (attempt.timestamp() == now) { |
| 93 | + logger.info("Parsing rule for account {}", accountId); |
| 94 | + vertx.executeBlocking(() -> parser.parse(config)) |
| 95 | + .onSuccess(result -> succeedParsingAttempt(accountId, result)) |
| 96 | + .onFailure(error -> failParsingAttempt(accountId, attempt, error)); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private ParsingAttempt tryRegisteringNewAttempt(ParsingAttempt previousAttempt, Instant currentAttemptStart) { |
| 101 | + if (previousAttempt == null) { |
| 102 | + return new ParsingAttempt.InProgress(currentAttemptStart, retryPolicy); |
| 103 | + } |
| 104 | + |
| 105 | + if (previousAttempt instanceof ParsingAttempt.InProgress) { |
| 106 | + return previousAttempt; |
| 107 | + } |
| 108 | + |
| 109 | + if (previousAttempt.retryPolicy() instanceof Retryable previousAttemptRetryPolicy) { |
| 110 | + final Instant previouslyDecidedToRetryAfter = previousAttempt.timestamp().plus( |
| 111 | + Duration.ofMillis(previousAttemptRetryPolicy.delay())); |
| 112 | + |
| 113 | + return previouslyDecidedToRetryAfter.isBefore(currentAttemptStart) |
| 114 | + ? new ParsingAttempt.InProgress(currentAttemptStart, previousAttemptRetryPolicy.next()) |
| 115 | + : previousAttempt; |
| 116 | + } |
| 117 | + |
| 118 | + return previousAttempt; |
| 119 | + } |
| 120 | + |
| 121 | + private void succeedParsingAttempt(String accountId, PerStageRule result) { |
| 122 | + accountIdToRules.put(accountId, result); |
| 123 | + accountIdToParsingAttempt.remove(accountId); |
| 124 | + |
| 125 | + logger.debug("Successfully parsed rule-engine config for account {}", accountId); |
| 126 | + } |
| 127 | + |
| 128 | + private void failParsingAttempt(String accountId, ParsingAttempt attempt, Throwable cause) { |
| 129 | + accountIdToParsingAttempt.put(accountId, ((ParsingAttempt.InProgress) attempt).failed()); |
| 130 | + |
| 131 | + logger.error( |
| 132 | + "Failed to parse rule-engine config for account %s: %s".formatted(accountId, cause.getMessage()), |
| 133 | + cause); |
| 134 | + } |
| 135 | + |
| 136 | + private sealed interface ParsingAttempt { |
| 137 | + |
| 138 | + Instant timestamp(); |
| 139 | + |
| 140 | + RetryPolicy retryPolicy(); |
| 141 | + |
| 142 | + record Failed(Instant timestamp, RetryPolicy retryPolicy) implements ParsingAttempt { |
| 143 | + } |
| 144 | + |
| 145 | + record InProgress(Instant timestamp, RetryPolicy retryPolicy) implements ParsingAttempt { |
| 146 | + |
| 147 | + public Failed failed() { |
| 148 | + return new Failed(timestamp, retryPolicy); |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments