Skip to content

Commit a0612b9

Browse files
AntoxaAntoxicRitesh Ghodrao
authored andcommitted
Cache endpoint split for response (prebid#3981)
1 parent e30c1bc commit a0612b9

5 files changed

Lines changed: 344 additions & 21 deletions

File tree

docs/config-app.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,9 @@ For `JVM` metrics
284284
- `cache.scheme` - set the external Cache Service protocol: `http`, `https`, etc.
285285
- `cache.host` - set the external Cache Service destination in format `host:port`.
286286
- `cache.path` - set the external Cache Service path, for example `/cache`.
287+
- `cache.internal.scheme` - set the internal Cache Service protocol: `http`, `https`, etc., the internal scheme get priority over the external one when provided.
288+
- `cache.internal.host` - set the internal Cache Service destination in format `host:port`, the internal port get priority over the external one when provided.
289+
- `cache.internal.path` - set the internal Cache Service path, for example `/cache`, the internal path get priority over the external one when provided.
287290
- `storage.pbc.enabled` - If set to true, this will allow storing modules’ data in third-party storage.
288291
- `storage.pbc.path` - set the external Cache Service path for module caching, for example `/pbc-storage`.
289292
- `cache.api-key-secured` - if set to `true`, will cause Prebid Server to add a special API key header to Prebid Cache requests.

src/main/java/org/prebid/server/cache/CoreCacheService.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ public class CoreCacheService {
6868
private static final int MAX_DATACENTER_REGION_LENGTH = 4;
6969

7070
private final HttpClient httpClient;
71-
private final URL endpointUrl;
71+
private final URL externalEndpointUrl;
72+
private final URL internalEndpointUrl;
7273
private final String cachedAssetUrlTemplate;
7374
private final long expectedCacheTimeMs;
7475
private final VastModifier vastModifier;
@@ -86,7 +87,8 @@ public class CoreCacheService {
8687

8788
public CoreCacheService(
8889
HttpClient httpClient,
89-
URL endpointUrl,
90+
URL externalEndpointUrl,
91+
URL internalEndpointUrl,
9092
String cachedAssetUrlTemplate,
9193
long expectedCacheTimeMs,
9294
String apiKey,
@@ -101,7 +103,8 @@ public CoreCacheService(
101103
JacksonMapper mapper) {
102104

103105
this.httpClient = Objects.requireNonNull(httpClient);
104-
this.endpointUrl = Objects.requireNonNull(endpointUrl);
106+
this.externalEndpointUrl = Objects.requireNonNull(externalEndpointUrl);
107+
this.internalEndpointUrl = internalEndpointUrl;
105108
this.cachedAssetUrlTemplate = Objects.requireNonNull(cachedAssetUrlTemplate);
106109
this.expectedCacheTimeMs = expectedCacheTimeMs;
107110
this.vastModifier = Objects.requireNonNull(vastModifier);
@@ -121,13 +124,13 @@ public CoreCacheService(
121124
}
122125

123126
public String getEndpointHost() {
124-
final String host = endpointUrl.getHost();
125-
final int port = endpointUrl.getPort();
127+
final String host = externalEndpointUrl.getHost();
128+
final int port = externalEndpointUrl.getPort();
126129
return port != -1 ? "%s:%d".formatted(host, port) : host;
127130
}
128131

129132
public String getEndpointPath() {
130-
return endpointUrl.getPath();
133+
return externalEndpointUrl.getPath();
131134
}
132135

133136
public String getCachedAssetURLTemplate() {
@@ -142,7 +145,7 @@ public String cacheVideoDebugLog(CachedDebugLog cachedDebugLog, Integer videoCac
142145
makeDebugCacheCreative(cachedDebugLog, cacheKey, videoCacheTtl));
143146
final BidCacheRequest bidCacheRequest = toBidCacheRequest(cachedCreatives);
144147
httpClient.post(
145-
endpointUrl.toString(),
148+
ObjectUtils.firstNonNull(internalEndpointUrl, externalEndpointUrl).toString(),
146149
cacheHeaders,
147150
mapper.encodeToString(bidCacheRequest),
148151
expectedCacheTimeMs);
@@ -179,7 +182,7 @@ private Future<BidCacheResponse> makeRequest(BidCacheRequest bidCacheRequest,
179182

180183
final long startTime = clock.millis();
181184
return httpClient.post(
182-
endpointUrl.toString(),
185+
ObjectUtils.firstNonNull(internalEndpointUrl, externalEndpointUrl).toString(),
183186
cacheHeaders,
184187
mapper.encodeToString(bidCacheRequest),
185188
remainingTimeout)
@@ -308,9 +311,9 @@ private Future<CacheServiceResult> doCacheOpenrtb(List<CacheBid> bids,
308311

309312
updateCreativeMetrics(accountId, cachedCreatives);
310313

311-
final String url = endpointUrl.toString();
314+
final String url = ObjectUtils.firstNonNull(internalEndpointUrl, externalEndpointUrl).toString();
312315
final String body = mapper.encodeToString(bidCacheRequest);
313-
final CacheHttpRequest httpRequest = CacheHttpRequest.of(url, body);
316+
final CacheHttpRequest httpRequest = CacheHttpRequest.of(externalEndpointUrl.toString(), body);
314317

315318
final long startTime = clock.millis();
316319
return httpClient.post(url, cacheHeaders, body, remainingTimeout)
@@ -336,7 +339,8 @@ private CacheServiceResult processResponseOpenrtb(HttpClientResponse response,
336339

337340
final CacheHttpResponse httpResponse = CacheHttpResponse.of(response.getStatusCode(), response.getBody());
338341
final int responseStatusCode = response.getStatusCode();
339-
final DebugHttpCall httpCall = makeDebugHttpCall(endpointUrl.toString(), httpRequest, httpResponse, startTime);
342+
final DebugHttpCall httpCall = makeDebugHttpCall(
343+
externalEndpointUrl.toString(), httpRequest, httpResponse, startTime);
340344
final BidCacheResponse bidCacheResponse;
341345
try {
342346
bidCacheResponse = toBidCacheResponse(
@@ -359,7 +363,7 @@ private CacheServiceResult failResponseOpenrtb(Throwable exception,
359363

360364
metrics.updateCacheRequestFailedTime(accountId, clock.millis() - startTime);
361365

362-
final DebugHttpCall httpCall = makeDebugHttpCall(endpointUrl.toString(), request, null, startTime);
366+
final DebugHttpCall httpCall = makeDebugHttpCall(externalEndpointUrl.toString(), request, null, startTime);
363367
return CacheServiceResult.of(httpCall, exception, Collections.emptyMap());
364368
}
365369

src/main/java/org/prebid/server/spring/config/ServiceConfiguration.java

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import io.vertx.core.file.FileSystem;
77
import io.vertx.core.http.HttpClientOptions;
88
import io.vertx.core.net.JksOptions;
9+
import lombok.Data;
910
import org.apache.commons.lang3.ObjectUtils;
1011
import org.apache.commons.lang3.StringUtils;
1112
import org.prebid.server.activity.ActivitiesConfigResolver;
@@ -159,14 +160,9 @@ public class ServiceConfiguration {
159160

160161
@Bean
161162
CoreCacheService cacheService(
162-
@Value("${cache.scheme}") String scheme,
163-
@Value("${cache.host}") String host,
164-
@Value("${cache.path}") String path,
165-
@Value("${cache.query}") String query,
163+
CacheConfigurationProperties cacheConfigurationProperties,
166164
@Value("${auction.cache.expected-request-time-ms}") long expectedCacheTimeMs,
167165
@Value("${pbc.api.key:#{null}}") String apiKey,
168-
@Value("${cache.api-key-secured:false}") boolean apiKeySecured,
169-
@Value("${cache.append-trace-info-to-cache-id:false}") boolean appendTraceInfoToCacheId,
170166
@Value("${datacenter-region:#{null}}") String datacenterRegion,
171167
VastModifier vastModifier,
172168
EventsService eventsService,
@@ -175,14 +171,25 @@ CoreCacheService cacheService(
175171
Clock clock,
176172
JacksonMapper mapper) {
177173

174+
final String scheme = cacheConfigurationProperties.getScheme();
175+
final String host = cacheConfigurationProperties.getHost();
176+
final String path = cacheConfigurationProperties.getPath();
177+
final String query = cacheConfigurationProperties.getQuery();
178+
final CacheConfigurationProperties.InternalCacheConfigurationProperties internalProperties =
179+
cacheConfigurationProperties.getInternal();
180+
178181
return new CoreCacheService(
179182
httpClient,
180183
CacheServiceUtil.getCacheEndpointUrl(scheme, host, path),
184+
internalProperties == null ? null : CacheServiceUtil.getCacheEndpointUrl(
185+
internalProperties.getScheme(),
186+
internalProperties.getHost(),
187+
internalProperties.getPath()),
181188
CacheServiceUtil.getCachedAssetUrlTemplate(scheme, host, path, query),
182189
expectedCacheTimeMs,
183190
apiKey,
184-
apiKeySecured,
185-
appendTraceInfoToCacheId,
191+
cacheConfigurationProperties.isApiKeySecured(),
192+
cacheConfigurationProperties.isAppendTraceInfoToCacheId(),
186193
datacenterRegion,
187194
vastModifier,
188195
eventsService,
@@ -192,6 +199,40 @@ CoreCacheService cacheService(
192199
mapper);
193200
}
194201

202+
@Bean
203+
@ConfigurationProperties(prefix = "cache")
204+
CacheConfigurationProperties cacheConfigurationProperties() {
205+
return new CacheConfigurationProperties();
206+
}
207+
208+
@Data
209+
private static class CacheConfigurationProperties {
210+
211+
private String scheme;
212+
213+
private String host;
214+
215+
private String path;
216+
217+
private String query;
218+
219+
boolean apiKeySecured;
220+
221+
boolean appendTraceInfoToCacheId;
222+
223+
private InternalCacheConfigurationProperties internal;
224+
225+
@Data
226+
private static class InternalCacheConfigurationProperties {
227+
228+
private String scheme;
229+
230+
private String host;
231+
232+
private String path;
233+
}
234+
}
235+
195236
@Bean
196237
@ConditionalOnProperty(prefix = "cache.module", name = "enabled", havingValue = "false", matchIfMissing = true)
197238
PbcStorageService noOpModuleCacheService() {

0 commit comments

Comments
 (0)