Skip to content

Commit 9a2fead

Browse files
Cache endpoint split for response (#3981)
1 parent e22f781 commit 9a2fead

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;
@@ -158,14 +159,9 @@ public class ServiceConfiguration {
158159

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

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

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

0 commit comments

Comments
 (0)