Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions docs/config-app.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,9 @@ For `JVM` metrics
- `cache.scheme` - set the external Cache Service protocol: `http`, `https`, etc.
- `cache.host` - set the external Cache Service destination in format `host:port`.
- `cache.path` - set the external Cache Service path, for example `/cache`.
- `cache.internal.scheme` - set the internal Cache Service protocol: `http`, `https`, etc., the internal scheme get priority over the external one when provided.
- `cache.internal.host` - set the internal Cache Service destination in format `host:port`, the internal port get priority over the external one when provided.
- `cache.internal.path` - set the internal Cache Service path, for example `/cache`, the internal path get priority over the external one when provided.
- `storage.pbc.enabled` - If set to true, this will allow storing modules’ data in third-party storage.
- `storage.pbc.path` - set the external Cache Service path for module caching, for example `/pbc-storage`.
- `cache.api-key-secured` - if set to `true`, will cause Prebid Server to add a special API key header to Prebid Cache requests.
Expand Down
28 changes: 16 additions & 12 deletions src/main/java/org/prebid/server/cache/CoreCacheService.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ public class CoreCacheService {
private static final int MAX_DATACENTER_REGION_LENGTH = 4;

private final HttpClient httpClient;
private final URL endpointUrl;
private final URL externalEndpointUrl;
private final URL internalEndpointUrl;
private final String cachedAssetUrlTemplate;
private final long expectedCacheTimeMs;
private final VastModifier vastModifier;
Expand All @@ -86,7 +87,8 @@ public class CoreCacheService {

public CoreCacheService(
HttpClient httpClient,
URL endpointUrl,
URL externalEndpointUrl,
URL internalEndpointUrl,
String cachedAssetUrlTemplate,
long expectedCacheTimeMs,
String apiKey,
Expand All @@ -101,7 +103,8 @@ public CoreCacheService(
JacksonMapper mapper) {

this.httpClient = Objects.requireNonNull(httpClient);
this.endpointUrl = Objects.requireNonNull(endpointUrl);
this.externalEndpointUrl = Objects.requireNonNull(externalEndpointUrl);
this.internalEndpointUrl = internalEndpointUrl;
this.cachedAssetUrlTemplate = Objects.requireNonNull(cachedAssetUrlTemplate);
this.expectedCacheTimeMs = expectedCacheTimeMs;
this.vastModifier = Objects.requireNonNull(vastModifier);
Expand All @@ -121,13 +124,13 @@ public CoreCacheService(
}

public String getEndpointHost() {
final String host = endpointUrl.getHost();
final int port = endpointUrl.getPort();
final String host = externalEndpointUrl.getHost();
final int port = externalEndpointUrl.getPort();
return port != -1 ? "%s:%d".formatted(host, port) : host;
}

public String getEndpointPath() {
return endpointUrl.getPath();
return externalEndpointUrl.getPath();
}

public String getCachedAssetURLTemplate() {
Expand All @@ -142,7 +145,7 @@ public String cacheVideoDebugLog(CachedDebugLog cachedDebugLog, Integer videoCac
makeDebugCacheCreative(cachedDebugLog, cacheKey, videoCacheTtl));
final BidCacheRequest bidCacheRequest = toBidCacheRequest(cachedCreatives);
httpClient.post(
endpointUrl.toString(),
ObjectUtils.firstNonNull(internalEndpointUrl, externalEndpointUrl).toString(),
cacheHeaders,
mapper.encodeToString(bidCacheRequest),
expectedCacheTimeMs);
Expand Down Expand Up @@ -179,7 +182,7 @@ private Future<BidCacheResponse> makeRequest(BidCacheRequest bidCacheRequest,

final long startTime = clock.millis();
return httpClient.post(
endpointUrl.toString(),
ObjectUtils.firstNonNull(internalEndpointUrl, externalEndpointUrl).toString(),
cacheHeaders,
mapper.encodeToString(bidCacheRequest),
remainingTimeout)
Expand Down Expand Up @@ -308,9 +311,9 @@ private Future<CacheServiceResult> doCacheOpenrtb(List<CacheBid> bids,

updateCreativeMetrics(accountId, cachedCreatives);

final String url = endpointUrl.toString();
final String url = ObjectUtils.firstNonNull(internalEndpointUrl, externalEndpointUrl).toString();
final String body = mapper.encodeToString(bidCacheRequest);
final CacheHttpRequest httpRequest = CacheHttpRequest.of(url, body);
final CacheHttpRequest httpRequest = CacheHttpRequest.of(externalEndpointUrl.toString(), body);

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

final CacheHttpResponse httpResponse = CacheHttpResponse.of(response.getStatusCode(), response.getBody());
final int responseStatusCode = response.getStatusCode();
final DebugHttpCall httpCall = makeDebugHttpCall(endpointUrl.toString(), httpRequest, httpResponse, startTime);
final DebugHttpCall httpCall = makeDebugHttpCall(
externalEndpointUrl.toString(), httpRequest, httpResponse, startTime);
final BidCacheResponse bidCacheResponse;
try {
bidCacheResponse = toBidCacheResponse(
Expand All @@ -359,7 +363,7 @@ private CacheServiceResult failResponseOpenrtb(Throwable exception,

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

final DebugHttpCall httpCall = makeDebugHttpCall(endpointUrl.toString(), request, null, startTime);
final DebugHttpCall httpCall = makeDebugHttpCall(externalEndpointUrl.toString(), request, null, startTime);
return CacheServiceResult.of(httpCall, exception, Collections.emptyMap());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.vertx.core.file.FileSystem;
import io.vertx.core.http.HttpClientOptions;
import io.vertx.core.net.JksOptions;
import lombok.Data;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.prebid.server.activity.ActivitiesConfigResolver;
Expand Down Expand Up @@ -158,14 +159,9 @@ public class ServiceConfiguration {

@Bean
CoreCacheService cacheService(
@Value("${cache.scheme}") String scheme,
@Value("${cache.host}") String host,
@Value("${cache.path}") String path,
@Value("${cache.query}") String query,
CacheConfigurationProperties cacheConfigurationProperties,
@Value("${auction.cache.expected-request-time-ms}") long expectedCacheTimeMs,
@Value("${pbc.api.key:#{null}}") String apiKey,
@Value("${cache.api-key-secured:false}") boolean apiKeySecured,
@Value("${cache.append-trace-info-to-cache-id:false}") boolean appendTraceInfoToCacheId,
@Value("${datacenter-region:#{null}}") String datacenterRegion,
VastModifier vastModifier,
EventsService eventsService,
Expand All @@ -174,14 +170,25 @@ CoreCacheService cacheService(
Clock clock,
JacksonMapper mapper) {

final String scheme = cacheConfigurationProperties.getScheme();
final String host = cacheConfigurationProperties.getHost();
final String path = cacheConfigurationProperties.getPath();
final String query = cacheConfigurationProperties.getQuery();
final CacheConfigurationProperties.InternalCacheConfigurationProperties internalProperties =
cacheConfigurationProperties.getInternal();

return new CoreCacheService(
httpClient,
CacheServiceUtil.getCacheEndpointUrl(scheme, host, path),
internalProperties == null ? null : CacheServiceUtil.getCacheEndpointUrl(
internalProperties.getScheme(),
internalProperties.getHost(),
internalProperties.getPath()),
CacheServiceUtil.getCachedAssetUrlTemplate(scheme, host, path, query),
expectedCacheTimeMs,
apiKey,
apiKeySecured,
appendTraceInfoToCacheId,
cacheConfigurationProperties.isApiKeySecured(),
cacheConfigurationProperties.isAppendTraceInfoToCacheId(),
datacenterRegion,
vastModifier,
eventsService,
Expand All @@ -191,6 +198,40 @@ CoreCacheService cacheService(
mapper);
}

@Bean
@ConfigurationProperties(prefix = "cache")
CacheConfigurationProperties cacheConfigurationProperties() {
return new CacheConfigurationProperties();
}

@Data
private static class CacheConfigurationProperties {

private String scheme;

private String host;

private String path;

private String query;

boolean apiKeySecured;

boolean appendTraceInfoToCacheId;

private InternalCacheConfigurationProperties internal;

@Data
private static class InternalCacheConfigurationProperties {

private String scheme;

private String host;

private String path;
}
}

@Bean
@ConditionalOnProperty(prefix = "cache.module", name = "enabled", havingValue = "false", matchIfMissing = true)
PbcStorageService noOpModuleCacheService() {
Expand Down
Loading
Loading