Skip to content

Commit 2caf0d9

Browse files
author
EvgeniiMunin
committed
refacto codestyle
1 parent c719bec commit 2caf0d9

File tree

9 files changed

+78
-71
lines changed

9 files changed

+78
-71
lines changed

extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/config/GreenbidsRealTimeDataConfiguration.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import java.util.List;
1818
import java.util.concurrent.TimeUnit;
19+
import java.util.concurrent.locks.ReentrantLock;
1920

2021
@ConditionalOnProperty(prefix = "hooks." + GreenbidsRealTimeDataModule.CODE, name = "enabled", havingValue = "true")
2122
@Configuration
@@ -42,6 +43,8 @@ GreenbidsRealTimeDataModule greenbidsRealTimeDataModule(
4243
.expireAfterWrite(cacheExpirationMinutes, TimeUnit.MINUTES)
4344
.build();
4445

46+
final ReentrantLock lock = new ReentrantLock();
47+
4548
final GreenbidsRealTimeDataProperties globalProperties = GreenbidsRealTimeDataProperties.of(
4649
modelCacheWithExpiration,
4750
thresholdsCacheWithExpiration,
@@ -50,7 +53,8 @@ GreenbidsRealTimeDataModule greenbidsRealTimeDataModule(
5053
gcsBucketName,
5154
cacheExpirationMinutes,
5255
onnxModelCacheKeyPrefix,
53-
thresholdsCacheKeyPrefix
56+
thresholdsCacheKeyPrefix,
57+
lock
5458
);
5559

5660
return new GreenbidsRealTimeDataModule(List.of(
@@ -62,6 +66,7 @@ GreenbidsRealTimeDataModule greenbidsRealTimeDataModule(
6266
googleCloudGreenbidsProject,
6367
gcsBucketName,
6468
onnxModelCacheKeyPrefix,
65-
thresholdsCacheKeyPrefix)));
69+
thresholdsCacheKeyPrefix,
70+
lock)));
6671
}
6772
}

extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/core/ThrottlingThresholds.java

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@
1111
@Value
1212
public class ThrottlingThresholds {
1313

14-
@JsonProperty("featurizer")
15-
String featurizer;
16-
17-
@JsonProperty("pipeline")
18-
String pipeline;
19-
2014
@JsonProperty("thresholds")
2115
List<Double> thresholds;
2216

@@ -31,14 +25,10 @@ public class ThrottlingThresholds {
3125

3226
@JsonCreator
3327
public ThrottlingThresholds(
34-
@JsonProperty("featurizer") String featurizer,
35-
@JsonProperty("pipeline") String pipeline,
3628
@JsonProperty("thresholds") List<Double> thresholds,
3729
@JsonProperty("tpr") List<Double> tpr,
3830
@JsonProperty("fpr") List<Double> fpr,
3931
@JsonProperty("version") String version) {
40-
this.featurizer = featurizer;
41-
this.pipeline = pipeline;
4232
this.thresholds = thresholds;
4333
this.tpr = tpr;
4434
this.fpr = fpr;

extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/model/GreenbidsRealTimeDataProperties.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import lombok.Value;
66
import org.prebid.server.hooks.modules.greenbids.real.time.data.core.ThrottlingThresholds;
77

8+
import java.util.concurrent.locks.Lock;
9+
810
@Value(staticConstructor = "of")
911
public class GreenbidsRealTimeDataProperties {
1012

@@ -31,4 +33,7 @@ public class GreenbidsRealTimeDataProperties {
3133

3234
@JsonProperty(value = "thresholdsCacheKeyPrefix", required = true)
3335
String thresholdsCacheKeyPrefix;
36+
37+
@JsonProperty(value = "lock", required = true)
38+
Lock lock;
3439
}

extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/model/ModelCache.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,23 @@ public class ModelCache {
2222

2323
Storage storage;
2424

25-
ReentrantLock lock;
26-
2725
String onnxModelCacheKeyPrefix;
2826

27+
ReentrantLock lock;
28+
2929
public ModelCache(
3030
String modelPath,
3131
Storage storage,
3232
String gcsBucketName,
3333
Cache<String, OnnxModelRunner> cache,
34-
String onnxModelCacheKeyPrefix) {
34+
String onnxModelCacheKeyPrefix,
35+
ReentrantLock lock) {
3536
this.gcsBucketName = gcsBucketName;
3637
this.modelPath = modelPath;
3738
this.cache = cache;
3839
this.storage = storage;
39-
this.lock = new ReentrantLock();
4040
this.onnxModelCacheKeyPrefix = onnxModelCacheKeyPrefix;
41+
this.lock = lock;
4142
}
4243

4344
public OnnxModelRunner getModelRunner(String pbuid) {

extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/model/ThresholdCache.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,27 @@ public class ThresholdCache {
2424

2525
Storage storage;
2626

27-
ReentrantLock lock;
28-
2927
ObjectMapper mapper;
3028

3129
String thresholdsCacheKeyPrefix;
3230

31+
ReentrantLock lock;
32+
3333
public ThresholdCache(
3434
String thresholdPath,
3535
Storage storage,
3636
String gcsBucketName,
3737
ObjectMapper mapper,
3838
Cache<String, ThrottlingThresholds> cache,
39-
String thresholdsCacheKeyPrefix) {
39+
String thresholdsCacheKeyPrefix,
40+
ReentrantLock lock) {
4041
this.gcsBucketName = gcsBucketName;
4142
this.thresholdPath = thresholdPath;
4243
this.cache = cache;
4344
this.storage = storage;
44-
this.lock = new ReentrantLock();
4545
this.mapper = mapper;
4646
this.thresholdsCacheKeyPrefix = thresholdsCacheKeyPrefix;
47+
this.lock = lock;
4748
}
4849

4950
public ThrottlingThresholds getThrottlingThresholds(String pbuid) {

extra/modules/greenbids-real-time-data/src/main/java/org/prebid/server/hooks/modules/greenbids/real/time/data/v1/GreenbidsRealTimeDataProcessedAuctionRequestHook.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
import java.util.Objects;
6767
import java.util.Optional;
6868
import java.util.UUID;
69+
import java.util.concurrent.locks.ReentrantLock;
6970
import java.util.stream.Collectors;
7071
import java.util.stream.IntStream;
7172
import java.util.stream.StreamSupport;
@@ -87,6 +88,7 @@ public class GreenbidsRealTimeDataProcessedAuctionRequestHook implements Process
8788
private final String gcsBucketName;
8889
private final String onnxModelCacheKeyPrefix;
8990
private final String thresholdsCacheKeyPrefix;
91+
private final ReentrantLock lock;
9092

9193
public GreenbidsRealTimeDataProcessedAuctionRequestHook(
9294
ObjectMapper mapper,
@@ -96,7 +98,8 @@ public GreenbidsRealTimeDataProcessedAuctionRequestHook(
9698
String googleCloudGreenbidsProject,
9799
String gcsBucketName,
98100
String onnxModelCacheKeyPrefix,
99-
String thresholdsCacheKeyPrefix) {
101+
String thresholdsCacheKeyPrefix,
102+
ReentrantLock lock) {
100103
this.mapper = Objects.requireNonNull(mapper);
101104
this.jacksonMapper = new JacksonMapper(mapper);
102105
this.modelCacheWithExpiration = modelCacheWithExpiration;
@@ -106,6 +109,7 @@ public GreenbidsRealTimeDataProcessedAuctionRequestHook(
106109
this.gcsBucketName = gcsBucketName;
107110
this.onnxModelCacheKeyPrefix = onnxModelCacheKeyPrefix;
108111
this.thresholdsCacheKeyPrefix = thresholdsCacheKeyPrefix;
112+
this.lock = lock;
109113
}
110114

111115
@Override
@@ -189,7 +193,8 @@ private OnnxModelRunner retrieveOnnxModelRunner(Partner partner, Storage storage
189193
storage,
190194
gcsBucketName,
191195
modelCacheWithExpiration,
192-
onnxModelCacheKeyPrefix);
196+
onnxModelCacheKeyPrefix,
197+
lock);
193198
return modelCache.getModelRunner(partner.getPbuid());
194199
}
195200

@@ -201,7 +206,8 @@ private Double retrieveThreshold(Partner partner, Storage storage) {
201206
gcsBucketName,
202207
mapper,
203208
thresholdsCacheWithExpiration,
204-
thresholdsCacheKeyPrefix);
209+
thresholdsCacheKeyPrefix,
210+
lock);
205211
final ThrottlingThresholds throttlingThresholds = thresholdCache
206212
.getThrottlingThresholds(partner.getPbuid());
207213
return partner.getThresholdForPartner(throttlingThresholds);
@@ -211,8 +217,7 @@ private Map<String, Map<String, Boolean>> runModeAndFilterBidders(
211217
OnnxModelRunner onnxModelRunner,
212218
List<ThrottlingMessage> throttlingMessages,
213219
String[][] throttlingInferenceRows,
214-
Double threshold
215-
) {
220+
Double threshold) {
216221
final Map<String, Map<String, Boolean>> impsBiddersFilterMap = new HashMap<>();
217222
final OrtSession.Result results;
218223
try {
@@ -332,8 +337,7 @@ private Map<String, Ortb2ImpExtResult> createOrtb2ImpExt(
332337
greenbidsId, impBiddersFilterMap, isExploration);
333338
return Ortb2ImpExtResult.of(
334339
explorationResult, tid);
335-
}
336-
));
340+
}));
337341
}
338342

339343
private Tags toAnalyticsTags(List<AnalyticsResult> analyticsResults) {

extra/modules/greenbids-real-time-data/src/test/java/org/prebid/server/hooks/modules/greenbids/real/time/data/v1/GreenbidsRealTimeDataProcessedAuctionRequestHookTest.java

Lines changed: 25 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@
2020
import org.prebid.server.hooks.modules.greenbids.real.time.data.core.ThrottlingThresholds;
2121
import org.prebid.server.hooks.modules.greenbids.real.time.data.model.AnalyticsResult;
2222
import org.prebid.server.hooks.modules.greenbids.real.time.data.model.ExplorationResult;
23-
import org.prebid.server.hooks.modules.greenbids.real.time.data.model.ModelCache;
2423
import org.prebid.server.hooks.modules.greenbids.real.time.data.model.OnnxModelRunner;
2524
import org.prebid.server.hooks.modules.greenbids.real.time.data.model.Ortb2ImpExtResult;
26-
import org.prebid.server.hooks.modules.greenbids.real.time.data.model.ThresholdCache;
2725
import org.prebid.server.hooks.modules.greenbids.real.time.data.v1.model.analytics.ActivityImpl;
2826
import org.prebid.server.hooks.modules.greenbids.real.time.data.v1.model.analytics.AppliedToImpl;
2927
import org.prebid.server.hooks.modules.greenbids.real.time.data.v1.model.analytics.ResultImpl;
@@ -47,6 +45,7 @@
4745
import java.util.List;
4846
import java.util.Map;
4947
import java.util.concurrent.TimeUnit;
48+
import java.util.concurrent.locks.ReentrantLock;
5049
import java.util.function.UnaryOperator;
5150

5251
import static org.assertj.core.api.Assertions.assertThat;
@@ -80,10 +79,6 @@ public class GreenbidsRealTimeDataProcessedAuctionRequestHookTest {
8079

8180
private Cache<String, ThrottlingThresholds> thresholdsCacheWithExpiration;
8281

83-
private ModelCache modelCache;
84-
85-
private ThresholdCache thresholdCache;
86-
8782
@BeforeEach
8883
public void setUp() {
8984
final ObjectMapper mapper = new ObjectMapper();
@@ -102,20 +97,8 @@ public void setUp() {
10297
GOOGLE_CLOUD_PROJECT,
10398
GCS_BUCKET_NAME,
10499
ONNX_MODEL_CACHE_KEY_PREFIX,
105-
THRESHOLDS_CACHE_KEY_PREFIX);
106-
modelCache = new ModelCache(
107-
null,
108-
null,
109-
null,
110-
modelCacheWithExpiration,
111-
ONNX_MODEL_CACHE_KEY_PREFIX);
112-
thresholdCache = new ThresholdCache(
113-
null,
114-
null,
115-
null,
116-
jacksonMapper.mapper(),
117-
thresholdsCacheWithExpiration,
118-
THRESHOLDS_CACHE_KEY_PREFIX);
100+
THRESHOLDS_CACHE_KEY_PREFIX,
101+
new ReentrantLock());
119102
}
120103

121104
@Test
@@ -135,10 +118,10 @@ public void shouldExitEarlyWhenPartnerNotActivatedInBidRequest() throws IOExcept
135118
final AuctionInvocationContext invocationContext = givenAuctionInvocationContext(auctionContext);
136119
when(invocationContext.auctionContext()).thenReturn(auctionContext);
137120

138-
modelCache.getCache().cleanUp();
139-
thresholdCache.getCache().cleanUp();
140-
modelCache.getCache().put("onnxModelRunner_test-pbuid", givenOnnxModelRunner());
141-
thresholdCache.getCache().put("throttlingThresholds_test-pbuid", givenThrottlingThresholds());
121+
modelCacheWithExpiration.cleanUp();
122+
thresholdsCacheWithExpiration.cleanUp();
123+
modelCacheWithExpiration.put("onnxModelRunner_test-pbuid", givenOnnxModelRunner());
124+
thresholdsCacheWithExpiration.put("throttlingThresholds_test-pbuid", givenThrottlingThresholds());
142125

143126
// when
144127
final Future<InvocationResult<AuctionRequestPayload>> future = target
@@ -182,9 +165,9 @@ public void shouldExitEarlyWhenThresholdIsNotAvailable() throws OrtException, IO
182165
final AuctionInvocationContext invocationContext = givenAuctionInvocationContext(auctionContext);
183166
when(invocationContext.auctionContext()).thenReturn(auctionContext);
184167

185-
modelCache.getCache().cleanUp();
186-
thresholdCache.getCache().cleanUp();
187-
modelCache.getCache().put("onnxModelRunner_test-pbuid", givenOnnxModelRunner());
168+
modelCacheWithExpiration.cleanUp();
169+
thresholdsCacheWithExpiration.cleanUp();
170+
modelCacheWithExpiration.put("onnxModelRunner_test-pbuid", givenOnnxModelRunner());
188171

189172
// when
190173
final Future<InvocationResult<AuctionRequestPayload>> future = target
@@ -228,9 +211,9 @@ public void shouldExitEarlyWhenModelIsNotAvailable() throws IOException {
228211
final AuctionInvocationContext invocationContext = givenAuctionInvocationContext(auctionContext);
229212
when(invocationContext.auctionContext()).thenReturn(auctionContext);
230213

231-
modelCache.getCache().cleanUp();
232-
thresholdCache.getCache().cleanUp();
233-
thresholdCache.getCache().put("throttlingThresholds_test-pbuid", givenThrottlingThresholds());
214+
modelCacheWithExpiration.cleanUp();
215+
thresholdsCacheWithExpiration.cleanUp();
216+
thresholdsCacheWithExpiration.put("throttlingThresholds_test-pbuid", givenThrottlingThresholds());
234217

235218
// when
236219
final Future<InvocationResult<AuctionRequestPayload>> future = target
@@ -274,10 +257,10 @@ public void shouldNotFilterBiddersAndReturnAnalyticsTagWhenExploration() throws
274257
final AuctionInvocationContext invocationContext = givenAuctionInvocationContext(auctionContext);
275258
when(invocationContext.auctionContext()).thenReturn(auctionContext);
276259

277-
modelCache.getCache().cleanUp();
278-
thresholdCache.getCache().cleanUp();
279-
modelCache.getCache().put("onnxModelRunner_test-pbuid", givenOnnxModelRunner());
280-
thresholdCache.getCache().put("throttlingThresholds_test-pbuid", givenThrottlingThresholds());
260+
modelCacheWithExpiration.cleanUp();
261+
thresholdsCacheWithExpiration.cleanUp();
262+
modelCacheWithExpiration.put("onnxModelRunner_test-pbuid", givenOnnxModelRunner());
263+
thresholdsCacheWithExpiration.put("throttlingThresholds_test-pbuid", givenThrottlingThresholds());
281264

282265
final AnalyticsResult expectedAnalyticsResult = expectedAnalyticsResult(true, true);
283266

@@ -330,10 +313,10 @@ public void shouldFilterBiddersBasedOnModelWhenAnyFeatureNotAvailable() throws O
330313
final AuctionInvocationContext invocationContext = givenAuctionInvocationContext(auctionContext);
331314
when(invocationContext.auctionContext()).thenReturn(auctionContext);
332315

333-
modelCache.getCache().cleanUp();
334-
thresholdCache.getCache().cleanUp();
335-
modelCache.getCache().put("onnxModelRunner_test-pbuid", givenOnnxModelRunner());
336-
thresholdCache.getCache().put("throttlingThresholds_test-pbuid", givenThrottlingThresholds());
316+
modelCacheWithExpiration.cleanUp();
317+
thresholdsCacheWithExpiration.cleanUp();
318+
modelCacheWithExpiration.put("onnxModelRunner_test-pbuid", givenOnnxModelRunner());
319+
thresholdsCacheWithExpiration.put("throttlingThresholds_test-pbuid", givenThrottlingThresholds());
337320

338321
final BidRequest expectedBidRequest = expectedUpdatedBidRequest(
339322
request -> request, jacksonMapper, explorationRate);
@@ -390,10 +373,10 @@ public void shouldFilterBiddersBasedOnModelResults() throws OrtException, IOExce
390373
final AuctionInvocationContext invocationContext = givenAuctionInvocationContext(auctionContext);
391374
when(invocationContext.auctionContext()).thenReturn(auctionContext);
392375

393-
modelCache.getCache().cleanUp();
394-
thresholdCache.getCache().cleanUp();
395-
modelCache.getCache().put("onnxModelRunner_test-pbuid", givenOnnxModelRunner());
396-
thresholdCache.getCache().put("throttlingThresholds_test-pbuid", givenThrottlingThresholds());
376+
modelCacheWithExpiration.cleanUp();
377+
thresholdsCacheWithExpiration.cleanUp();
378+
modelCacheWithExpiration.put("onnxModelRunner_test-pbuid", givenOnnxModelRunner());
379+
thresholdsCacheWithExpiration.put("throttlingThresholds_test-pbuid", givenThrottlingThresholds());
397380

398381
final BidRequest expectedBidRequest = expectedUpdatedBidRequest(
399382
request -> request, jacksonMapper, explorationRate);

extra/modules/greenbids-real-time-data/src/test/resources/thresholds_pbuid=test-pbuid.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
{
2-
"featurizer": "featurizer",
3-
"pipeline": "aaabbbccc",
42
"thresholds": [
53
0.4,
64
0.224,

src/test/java/org/prebid/server/vertx/httpclient/BasicHttpClientTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,26 @@ public void requestShouldFailIfHttpRequestTimedOut(Vertx vertx, VertxTestContext
162162
}));
163163
}
164164

165+
@Test
166+
public void requestShouldFailIfHttpResponseTimedOut(Vertx vertx, VertxTestContext context) {
167+
// given
168+
final BasicHttpClient httpClient = new BasicHttpClient(vertx, vertx.createHttpClient());
169+
final int serverPort = 8888;
170+
171+
startServer(serverPort, 0L, 2000L);
172+
173+
// when
174+
final Future<?> future = httpClient.get("http://localhost:" + serverPort, 1000L);
175+
176+
// then
177+
future.onComplete(context.failing(e -> {
178+
assertThat(e)
179+
.isInstanceOf(TimeoutException.class)
180+
.hasMessage("Timeout period of 1000ms has been exceeded");
181+
context.completeNow();
182+
}));
183+
}
184+
165185
/**
166186
* The server returns entire response or body with delay.
167187
*/

0 commit comments

Comments
 (0)