From 2caf0d9564b9f4f404c6f0a5bf7abe7500e434ba Mon Sep 17 00:00:00 2001 From: EvgeniiMunin Date: Thu, 22 Aug 2024 11:27:19 +0200 Subject: [PATCH] refacto codestyle --- .../GreenbidsRealTimeDataConfiguration.java | 9 ++- .../time/data/core/ThrottlingThresholds.java | 10 --- .../GreenbidsRealTimeDataProperties.java | 5 ++ .../real/time/data/model/ModelCache.java | 9 +-- .../real/time/data/model/ThresholdCache.java | 9 +-- ...alTimeDataProcessedAuctionRequestHook.java | 18 +++-- ...meDataProcessedAuctionRequestHookTest.java | 67 +++++++------------ .../thresholds_pbuid=test-pbuid.json | 2 - .../vertx/httpclient/BasicHttpClientTest.java | 20 ++++++ 9 files changed, 78 insertions(+), 71 deletions(-) diff --git a/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/config/GreenbidsRealTimeDataConfiguration.java b/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/config/GreenbidsRealTimeDataConfiguration.java index b5a11c21f13..195e39be296 100644 --- a/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/config/GreenbidsRealTimeDataConfiguration.java +++ b/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/config/GreenbidsRealTimeDataConfiguration.java @@ -16,6 +16,7 @@ import java.util.List; import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.ReentrantLock; @ConditionalOnProperty(prefix = "hooks." + GreenbidsRealTimeDataModule.CODE, name = "enabled", havingValue = "true") @Configuration @@ -42,6 +43,8 @@ GreenbidsRealTimeDataModule greenbidsRealTimeDataModule( .expireAfterWrite(cacheExpirationMinutes, TimeUnit.MINUTES) .build(); + final ReentrantLock lock = new ReentrantLock(); + final GreenbidsRealTimeDataProperties globalProperties = GreenbidsRealTimeDataProperties.of( modelCacheWithExpiration, thresholdsCacheWithExpiration, @@ -50,7 +53,8 @@ GreenbidsRealTimeDataModule greenbidsRealTimeDataModule( gcsBucketName, cacheExpirationMinutes, onnxModelCacheKeyPrefix, - thresholdsCacheKeyPrefix + thresholdsCacheKeyPrefix, + lock ); return new GreenbidsRealTimeDataModule(List.of( @@ -62,6 +66,7 @@ GreenbidsRealTimeDataModule greenbidsRealTimeDataModule( googleCloudGreenbidsProject, gcsBucketName, onnxModelCacheKeyPrefix, - thresholdsCacheKeyPrefix))); + thresholdsCacheKeyPrefix, + lock))); } } diff --git a/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/core/ThrottlingThresholds.java b/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/core/ThrottlingThresholds.java index 05bdaa5a16a..d25aaefe25e 100644 --- a/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/core/ThrottlingThresholds.java +++ b/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/core/ThrottlingThresholds.java @@ -11,12 +11,6 @@ @Value public class ThrottlingThresholds { - @JsonProperty("featurizer") - String featurizer; - - @JsonProperty("pipeline") - String pipeline; - @JsonProperty("thresholds") List thresholds; @@ -31,14 +25,10 @@ public class ThrottlingThresholds { @JsonCreator public ThrottlingThresholds( - @JsonProperty("featurizer") String featurizer, - @JsonProperty("pipeline") String pipeline, @JsonProperty("thresholds") List thresholds, @JsonProperty("tpr") List tpr, @JsonProperty("fpr") List fpr, @JsonProperty("version") String version) { - this.featurizer = featurizer; - this.pipeline = pipeline; this.thresholds = thresholds; this.tpr = tpr; this.fpr = fpr; diff --git a/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/model/GreenbidsRealTimeDataProperties.java b/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/model/GreenbidsRealTimeDataProperties.java index 9dfa61963b9..069aeddddfa 100644 --- a/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/model/GreenbidsRealTimeDataProperties.java +++ b/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/model/GreenbidsRealTimeDataProperties.java @@ -5,6 +5,8 @@ import lombok.Value; import org.prebid.server.hooks.modules.greenbids.real.time.data.core.ThrottlingThresholds; +import java.util.concurrent.locks.Lock; + @Value(staticConstructor = "of") public class GreenbidsRealTimeDataProperties { @@ -31,4 +33,7 @@ public class GreenbidsRealTimeDataProperties { @JsonProperty(value = "thresholdsCacheKeyPrefix", required = true) String thresholdsCacheKeyPrefix; + + @JsonProperty(value = "lock", required = true) + Lock lock; } diff --git a/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/model/ModelCache.java b/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/model/ModelCache.java index d7d2643ea0c..d0d838a142d 100644 --- a/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/model/ModelCache.java +++ b/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/model/ModelCache.java @@ -22,22 +22,23 @@ public class ModelCache { Storage storage; - ReentrantLock lock; - String onnxModelCacheKeyPrefix; + ReentrantLock lock; + public ModelCache( String modelPath, Storage storage, String gcsBucketName, Cache cache, - String onnxModelCacheKeyPrefix) { + String onnxModelCacheKeyPrefix, + ReentrantLock lock) { this.gcsBucketName = gcsBucketName; this.modelPath = modelPath; this.cache = cache; this.storage = storage; - this.lock = new ReentrantLock(); this.onnxModelCacheKeyPrefix = onnxModelCacheKeyPrefix; + this.lock = lock; } public OnnxModelRunner getModelRunner(String pbuid) { diff --git a/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/model/ThresholdCache.java b/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/model/ThresholdCache.java index 3a020732ddb..832381b6ad3 100644 --- a/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/model/ThresholdCache.java +++ b/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/model/ThresholdCache.java @@ -24,26 +24,27 @@ public class ThresholdCache { Storage storage; - ReentrantLock lock; - ObjectMapper mapper; String thresholdsCacheKeyPrefix; + ReentrantLock lock; + public ThresholdCache( String thresholdPath, Storage storage, String gcsBucketName, ObjectMapper mapper, Cache cache, - String thresholdsCacheKeyPrefix) { + String thresholdsCacheKeyPrefix, + ReentrantLock lock) { this.gcsBucketName = gcsBucketName; this.thresholdPath = thresholdPath; this.cache = cache; this.storage = storage; - this.lock = new ReentrantLock(); this.mapper = mapper; this.thresholdsCacheKeyPrefix = thresholdsCacheKeyPrefix; + this.lock = lock; } public ThrottlingThresholds getThrottlingThresholds(String pbuid) { diff --git a/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/v1/GreenbidsRealTimeDataProcessedAuctionRequestHook.java b/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/v1/GreenbidsRealTimeDataProcessedAuctionRequestHook.java index 676befd4759..79891f390c8 100644 --- a/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/v1/GreenbidsRealTimeDataProcessedAuctionRequestHook.java +++ b/extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/v1/GreenbidsRealTimeDataProcessedAuctionRequestHook.java @@ -66,6 +66,7 @@ import java.util.Objects; import java.util.Optional; import java.util.UUID; +import java.util.concurrent.locks.ReentrantLock; import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.stream.StreamSupport; @@ -87,6 +88,7 @@ public class GreenbidsRealTimeDataProcessedAuctionRequestHook implements Process private final String gcsBucketName; private final String onnxModelCacheKeyPrefix; private final String thresholdsCacheKeyPrefix; + private final ReentrantLock lock; public GreenbidsRealTimeDataProcessedAuctionRequestHook( ObjectMapper mapper, @@ -96,7 +98,8 @@ public GreenbidsRealTimeDataProcessedAuctionRequestHook( String googleCloudGreenbidsProject, String gcsBucketName, String onnxModelCacheKeyPrefix, - String thresholdsCacheKeyPrefix) { + String thresholdsCacheKeyPrefix, + ReentrantLock lock) { this.mapper = Objects.requireNonNull(mapper); this.jacksonMapper = new JacksonMapper(mapper); this.modelCacheWithExpiration = modelCacheWithExpiration; @@ -106,6 +109,7 @@ public GreenbidsRealTimeDataProcessedAuctionRequestHook( this.gcsBucketName = gcsBucketName; this.onnxModelCacheKeyPrefix = onnxModelCacheKeyPrefix; this.thresholdsCacheKeyPrefix = thresholdsCacheKeyPrefix; + this.lock = lock; } @Override @@ -189,7 +193,8 @@ private OnnxModelRunner retrieveOnnxModelRunner(Partner partner, Storage storage storage, gcsBucketName, modelCacheWithExpiration, - onnxModelCacheKeyPrefix); + onnxModelCacheKeyPrefix, + lock); return modelCache.getModelRunner(partner.getPbuid()); } @@ -201,7 +206,8 @@ private Double retrieveThreshold(Partner partner, Storage storage) { gcsBucketName, mapper, thresholdsCacheWithExpiration, - thresholdsCacheKeyPrefix); + thresholdsCacheKeyPrefix, + lock); final ThrottlingThresholds throttlingThresholds = thresholdCache .getThrottlingThresholds(partner.getPbuid()); return partner.getThresholdForPartner(throttlingThresholds); @@ -211,8 +217,7 @@ private Map> runModeAndFilterBidders( OnnxModelRunner onnxModelRunner, List throttlingMessages, String[][] throttlingInferenceRows, - Double threshold - ) { + Double threshold) { final Map> impsBiddersFilterMap = new HashMap<>(); final OrtSession.Result results; try { @@ -332,8 +337,7 @@ private Map createOrtb2ImpExt( greenbidsId, impBiddersFilterMap, isExploration); return Ortb2ImpExtResult.of( explorationResult, tid); - } - )); + })); } private Tags toAnalyticsTags(List analyticsResults) { diff --git a/extra/modules/greenbids-real-time-data/src/test/java/org/prebid/server/hooks/modules/greenbids/real/time/data/v1/GreenbidsRealTimeDataProcessedAuctionRequestHookTest.java b/extra/modules/greenbids-real-time-data/src/test/java/org/prebid/server/hooks/modules/greenbids/real/time/data/v1/GreenbidsRealTimeDataProcessedAuctionRequestHookTest.java index 756f68ebf7c..8f8b02e9534 100644 --- a/extra/modules/greenbids-real-time-data/src/test/java/org/prebid/server/hooks/modules/greenbids/real/time/data/v1/GreenbidsRealTimeDataProcessedAuctionRequestHookTest.java +++ b/extra/modules/greenbids-real-time-data/src/test/java/org/prebid/server/hooks/modules/greenbids/real/time/data/v1/GreenbidsRealTimeDataProcessedAuctionRequestHookTest.java @@ -20,10 +20,8 @@ import org.prebid.server.hooks.modules.greenbids.real.time.data.core.ThrottlingThresholds; import org.prebid.server.hooks.modules.greenbids.real.time.data.model.AnalyticsResult; import org.prebid.server.hooks.modules.greenbids.real.time.data.model.ExplorationResult; -import org.prebid.server.hooks.modules.greenbids.real.time.data.model.ModelCache; import org.prebid.server.hooks.modules.greenbids.real.time.data.model.OnnxModelRunner; import org.prebid.server.hooks.modules.greenbids.real.time.data.model.Ortb2ImpExtResult; -import org.prebid.server.hooks.modules.greenbids.real.time.data.model.ThresholdCache; import org.prebid.server.hooks.modules.greenbids.real.time.data.v1.model.analytics.ActivityImpl; import org.prebid.server.hooks.modules.greenbids.real.time.data.v1.model.analytics.AppliedToImpl; import org.prebid.server.hooks.modules.greenbids.real.time.data.v1.model.analytics.ResultImpl; @@ -47,6 +45,7 @@ import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; +import java.util.concurrent.locks.ReentrantLock; import java.util.function.UnaryOperator; import static org.assertj.core.api.Assertions.assertThat; @@ -80,10 +79,6 @@ public class GreenbidsRealTimeDataProcessedAuctionRequestHookTest { private Cache thresholdsCacheWithExpiration; - private ModelCache modelCache; - - private ThresholdCache thresholdCache; - @BeforeEach public void setUp() { final ObjectMapper mapper = new ObjectMapper(); @@ -102,20 +97,8 @@ public void setUp() { GOOGLE_CLOUD_PROJECT, GCS_BUCKET_NAME, ONNX_MODEL_CACHE_KEY_PREFIX, - THRESHOLDS_CACHE_KEY_PREFIX); - modelCache = new ModelCache( - null, - null, - null, - modelCacheWithExpiration, - ONNX_MODEL_CACHE_KEY_PREFIX); - thresholdCache = new ThresholdCache( - null, - null, - null, - jacksonMapper.mapper(), - thresholdsCacheWithExpiration, - THRESHOLDS_CACHE_KEY_PREFIX); + THRESHOLDS_CACHE_KEY_PREFIX, + new ReentrantLock()); } @Test @@ -135,10 +118,10 @@ public void shouldExitEarlyWhenPartnerNotActivatedInBidRequest() throws IOExcept final AuctionInvocationContext invocationContext = givenAuctionInvocationContext(auctionContext); when(invocationContext.auctionContext()).thenReturn(auctionContext); - modelCache.getCache().cleanUp(); - thresholdCache.getCache().cleanUp(); - modelCache.getCache().put("onnxModelRunner_test-pbuid", givenOnnxModelRunner()); - thresholdCache.getCache().put("throttlingThresholds_test-pbuid", givenThrottlingThresholds()); + modelCacheWithExpiration.cleanUp(); + thresholdsCacheWithExpiration.cleanUp(); + modelCacheWithExpiration.put("onnxModelRunner_test-pbuid", givenOnnxModelRunner()); + thresholdsCacheWithExpiration.put("throttlingThresholds_test-pbuid", givenThrottlingThresholds()); // when final Future> future = target @@ -182,9 +165,9 @@ public void shouldExitEarlyWhenThresholdIsNotAvailable() throws OrtException, IO final AuctionInvocationContext invocationContext = givenAuctionInvocationContext(auctionContext); when(invocationContext.auctionContext()).thenReturn(auctionContext); - modelCache.getCache().cleanUp(); - thresholdCache.getCache().cleanUp(); - modelCache.getCache().put("onnxModelRunner_test-pbuid", givenOnnxModelRunner()); + modelCacheWithExpiration.cleanUp(); + thresholdsCacheWithExpiration.cleanUp(); + modelCacheWithExpiration.put("onnxModelRunner_test-pbuid", givenOnnxModelRunner()); // when final Future> future = target @@ -228,9 +211,9 @@ public void shouldExitEarlyWhenModelIsNotAvailable() throws IOException { final AuctionInvocationContext invocationContext = givenAuctionInvocationContext(auctionContext); when(invocationContext.auctionContext()).thenReturn(auctionContext); - modelCache.getCache().cleanUp(); - thresholdCache.getCache().cleanUp(); - thresholdCache.getCache().put("throttlingThresholds_test-pbuid", givenThrottlingThresholds()); + modelCacheWithExpiration.cleanUp(); + thresholdsCacheWithExpiration.cleanUp(); + thresholdsCacheWithExpiration.put("throttlingThresholds_test-pbuid", givenThrottlingThresholds()); // when final Future> future = target @@ -274,10 +257,10 @@ public void shouldNotFilterBiddersAndReturnAnalyticsTagWhenExploration() throws final AuctionInvocationContext invocationContext = givenAuctionInvocationContext(auctionContext); when(invocationContext.auctionContext()).thenReturn(auctionContext); - modelCache.getCache().cleanUp(); - thresholdCache.getCache().cleanUp(); - modelCache.getCache().put("onnxModelRunner_test-pbuid", givenOnnxModelRunner()); - thresholdCache.getCache().put("throttlingThresholds_test-pbuid", givenThrottlingThresholds()); + modelCacheWithExpiration.cleanUp(); + thresholdsCacheWithExpiration.cleanUp(); + modelCacheWithExpiration.put("onnxModelRunner_test-pbuid", givenOnnxModelRunner()); + thresholdsCacheWithExpiration.put("throttlingThresholds_test-pbuid", givenThrottlingThresholds()); final AnalyticsResult expectedAnalyticsResult = expectedAnalyticsResult(true, true); @@ -330,10 +313,10 @@ public void shouldFilterBiddersBasedOnModelWhenAnyFeatureNotAvailable() throws O final AuctionInvocationContext invocationContext = givenAuctionInvocationContext(auctionContext); when(invocationContext.auctionContext()).thenReturn(auctionContext); - modelCache.getCache().cleanUp(); - thresholdCache.getCache().cleanUp(); - modelCache.getCache().put("onnxModelRunner_test-pbuid", givenOnnxModelRunner()); - thresholdCache.getCache().put("throttlingThresholds_test-pbuid", givenThrottlingThresholds()); + modelCacheWithExpiration.cleanUp(); + thresholdsCacheWithExpiration.cleanUp(); + modelCacheWithExpiration.put("onnxModelRunner_test-pbuid", givenOnnxModelRunner()); + thresholdsCacheWithExpiration.put("throttlingThresholds_test-pbuid", givenThrottlingThresholds()); final BidRequest expectedBidRequest = expectedUpdatedBidRequest( request -> request, jacksonMapper, explorationRate); @@ -390,10 +373,10 @@ public void shouldFilterBiddersBasedOnModelResults() throws OrtException, IOExce final AuctionInvocationContext invocationContext = givenAuctionInvocationContext(auctionContext); when(invocationContext.auctionContext()).thenReturn(auctionContext); - modelCache.getCache().cleanUp(); - thresholdCache.getCache().cleanUp(); - modelCache.getCache().put("onnxModelRunner_test-pbuid", givenOnnxModelRunner()); - thresholdCache.getCache().put("throttlingThresholds_test-pbuid", givenThrottlingThresholds()); + modelCacheWithExpiration.cleanUp(); + thresholdsCacheWithExpiration.cleanUp(); + modelCacheWithExpiration.put("onnxModelRunner_test-pbuid", givenOnnxModelRunner()); + thresholdsCacheWithExpiration.put("throttlingThresholds_test-pbuid", givenThrottlingThresholds()); final BidRequest expectedBidRequest = expectedUpdatedBidRequest( request -> request, jacksonMapper, explorationRate); diff --git a/extra/modules/greenbids-real-time-data/src/test/resources/thresholds_pbuid=test-pbuid.json b/extra/modules/greenbids-real-time-data/src/test/resources/thresholds_pbuid=test-pbuid.json index 4f286a9c7dd..cd21f77e696 100644 --- a/extra/modules/greenbids-real-time-data/src/test/resources/thresholds_pbuid=test-pbuid.json +++ b/extra/modules/greenbids-real-time-data/src/test/resources/thresholds_pbuid=test-pbuid.json @@ -1,6 +1,4 @@ { - "featurizer": "featurizer", - "pipeline": "aaabbbccc", "thresholds": [ 0.4, 0.224, diff --git a/src/test/java/org/prebid/server/vertx/httpclient/BasicHttpClientTest.java b/src/test/java/org/prebid/server/vertx/httpclient/BasicHttpClientTest.java index 6d998df1b94..a9101f06440 100644 --- a/src/test/java/org/prebid/server/vertx/httpclient/BasicHttpClientTest.java +++ b/src/test/java/org/prebid/server/vertx/httpclient/BasicHttpClientTest.java @@ -162,6 +162,26 @@ public void requestShouldFailIfHttpRequestTimedOut(Vertx vertx, VertxTestContext })); } + @Test + public void requestShouldFailIfHttpResponseTimedOut(Vertx vertx, VertxTestContext context) { + // given + final BasicHttpClient httpClient = new BasicHttpClient(vertx, vertx.createHttpClient()); + final int serverPort = 8888; + + startServer(serverPort, 0L, 2000L); + + // when + final Future future = httpClient.get("http://localhost:" + serverPort, 1000L); + + // then + future.onComplete(context.failing(e -> { + assertThat(e) + .isInstanceOf(TimeoutException.class) + .hasMessage("Timeout period of 1000ms has been exceeded"); + context.completeNow(); + })); + } + /** * The server returns entire response or body with delay. */