Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Default Bids Cache TTL #3543

Merged
merged 5 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
1 change: 1 addition & 0 deletions docs/config-app.md
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ See [metrics documentation](metrics.md) for complete list of metrics submitted a
for particular publisher account. Overrides `cache.banner-ttl-seconds` property.
- `cache.account.<ACCOUNT>.video-ttl-seconds` - how long (in seconds) video creative will be available in Cache Service
for particular publisher account. Overrides `cache.video-ttl-seconds` property.
- `cache.default-ttl-seconds.{banner, video, audio, native}` - a default value how long (in seconds) a creative of the specific type will be available in Cache Service

## Application settings (account configuration, stored ad unit configurations, stored requests)
Preconfigured application settings can be obtained from multiple data sources consequently:
Expand Down
54 changes: 35 additions & 19 deletions src/main/java/org/prebid/server/auction/BidResponseCreator.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
import org.prebid.server.settings.model.AccountEventsConfig;
import org.prebid.server.settings.model.AccountTargetingConfig;
import org.prebid.server.settings.model.VideoStoredDataResult;
import org.prebid.server.spring.config.model.CacheDefaultTtlProperties;
import org.prebid.server.util.StreamUtil;
import org.prebid.server.vast.VastModifier;

Expand Down Expand Up @@ -139,6 +140,7 @@ public class BidResponseCreator {
private final Clock clock;
private final JacksonMapper mapper;
private final CacheTtl mediaTypeCacheTtl;
private final CacheDefaultTtlProperties cacheDefaultProperties;

private final String cacheHost;
private final String cachePath;
Expand All @@ -156,7 +158,8 @@ public BidResponseCreator(CoreCacheService coreCacheService,
int truncateAttrChars,
Clock clock,
JacksonMapper mapper,
CacheTtl mediaTypeCacheTtl) {
CacheTtl mediaTypeCacheTtl,
CacheDefaultTtlProperties cacheDefaultProperties) {

this.coreCacheService = Objects.requireNonNull(coreCacheService);
this.bidderCatalog = Objects.requireNonNull(bidderCatalog);
Expand All @@ -171,6 +174,7 @@ public BidResponseCreator(CoreCacheService coreCacheService,
this.clock = Objects.requireNonNull(clock);
this.mapper = Objects.requireNonNull(mapper);
this.mediaTypeCacheTtl = Objects.requireNonNull(mediaTypeCacheTtl);
this.cacheDefaultProperties = Objects.requireNonNull(cacheDefaultProperties);

cacheHost = Objects.requireNonNull(coreCacheService.getEndpointHost());
cachePath = Objects.requireNonNull(coreCacheService.getEndpointPath());
Expand Down Expand Up @@ -436,8 +440,8 @@ private BidInfo toBidInfo(Bid bid,
.bidType(type)
.bidder(bidder)
.correspondingImp(correspondingImp)
.ttl(resolveBannerTtl(bid, correspondingImp, cacheInfo, account))
.videoTtl(type == BidType.video ? resolveVideoTtl(bid, correspondingImp, cacheInfo, account) : null)
.ttl(resolveTtl(bid, type, correspondingImp, cacheInfo, account))
.vastTtl(type == BidType.video ? resolveVastTtl(bid, correspondingImp, cacheInfo, account) : null)
.category(categoryMappingResult.getCategory(bid))
.satisfiedPriority(categoryMappingResult.isBidSatisfiesPriority(bid))
.build();
Expand All @@ -457,31 +461,43 @@ private static Optional<Imp> correspondingImp(String impId, List<Imp> imps) {
.findFirst();
}

private Integer resolveBannerTtl(Bid bid, Imp imp, BidRequestCacheInfo cacheInfo, Account account) {
final AccountAuctionConfig accountAuctionConfig = account.getAuction();
private Integer resolveTtl(Bid bid, BidType type, Imp imp, BidRequestCacheInfo cacheInfo, Account account) {
final Integer bidTtl = bid.getExp();
final Integer impTtl = imp != null ? imp.getExp() : null;
final Integer requestTtl = cacheInfo.getCacheBidsTtl();

return ObjectUtils.firstNonNull(
bidTtl,
impTtl,
cacheInfo.getCacheBidsTtl(),
accountAuctionConfig != null ? accountAuctionConfig.getBannerCacheTtl() : null,
mediaTypeCacheTtl.getBannerCacheTtl());
final AccountAuctionConfig accountAuctionConfig = account.getAuction();
final Integer accountTtl = accountAuctionConfig != null ? switch (type) {
case banner -> accountAuctionConfig.getBannerCacheTtl();
case video -> accountAuctionConfig.getVideoCacheTtl();
case audio, xNative -> null;
} : null;

final Integer mediaTypeTtl = switch (type) {
case banner -> mediaTypeCacheTtl.getBannerCacheTtl();
case video -> mediaTypeCacheTtl.getVideoCacheTtl();
case audio, xNative -> null;
};

final Integer defaultTtl = switch (type) {
case banner -> cacheDefaultProperties.getBannerTtl();
case video -> cacheDefaultProperties.getVideoTtl();
case audio -> cacheDefaultProperties.getAudioTtl();
case xNative -> cacheDefaultProperties.getNativeTtl();
};

return ObjectUtils.firstNonNull(bidTtl, impTtl, requestTtl, accountTtl, mediaTypeTtl, defaultTtl);
}

private Integer resolveVideoTtl(Bid bid, Imp imp, BidRequestCacheInfo cacheInfo, Account account) {
private Integer resolveVastTtl(Bid bid, Imp imp, BidRequestCacheInfo cacheInfo, Account account) {
final AccountAuctionConfig accountAuctionConfig = account.getAuction();
final Integer bidTtl = bid.getExp();
final Integer impTtl = imp != null ? imp.getExp() : null;

return ObjectUtils.firstNonNull(
bidTtl,
impTtl,
bid.getExp(),
imp != null ? imp.getExp() : null,
cacheInfo.getCacheVideoBidsTtl(),
accountAuctionConfig != null ? accountAuctionConfig.getVideoCacheTtl() : null,
mediaTypeCacheTtl.getVideoCacheTtl());
mediaTypeCacheTtl.getVideoCacheTtl(),
cacheDefaultProperties.getVideoTtl());
}

private Future<List<BidderResponse>> invokeProcessedBidderResponseHooks(List<BidderResponse> bidderResponses,
Expand Down Expand Up @@ -1369,7 +1385,7 @@ private Bid toBid(BidInfo bidInfo,

final Integer ttl = Optional.ofNullable(cacheInfo)
.map(info -> ObjectUtils.max(cacheInfo.getTtl(), cacheInfo.getVideoTtl()))
.orElseGet(() -> ObjectUtils.max(bidInfo.getTtl(), bidInfo.getVideoTtl()));
.orElseGet(() -> ObjectUtils.max(bidInfo.getTtl(), bidInfo.getVastTtl()));

return bid.toBuilder()
.ext(updatedBidExt)
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/prebid/server/auction/model/BidInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class BidInfo {

Integer ttl;

Integer videoTtl;
Integer vastTtl;

public String getBidId() {
final ObjectNode extNode = bid != null ? bid.getExt() : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ private List<CacheBid> getCacheBids(List<BidInfo> bidInfos) {
private List<CacheBid> getVideoCacheBids(List<BidInfo> bidInfos) {
return bidInfos.stream()
.filter(bidInfo -> Objects.equals(bidInfo.getBidType(), BidType.video))
.map(bidInfo -> CacheBid.of(bidInfo, bidInfo.getVideoTtl()))
.map(bidInfo -> CacheBid.of(bidInfo, bidInfo.getVastTtl()))
.toList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
import org.prebid.server.privacy.gdpr.TcfDefinerService;
import org.prebid.server.settings.ApplicationSettings;
import org.prebid.server.settings.model.BidValidationEnforcement;
import org.prebid.server.spring.config.model.CacheDefaultTtlProperties;
import org.prebid.server.spring.config.model.ExternalConversionProperties;
import org.prebid.server.spring.config.model.HttpClientCircuitBreakerProperties;
import org.prebid.server.spring.config.model.HttpClientProperties;
Expand Down Expand Up @@ -789,6 +790,16 @@ BidderErrorNotifier bidderErrorNotifier(
metrics);
}

@Bean
CacheDefaultTtlProperties cacheDefaultTtlProperties(
@Value("${cache.default-ttl-seconds.banner:300}") Integer bannerTtl,
@Value("${cache.default-ttl-seconds.video:1500}") Integer videoTtl,
@Value("${cache.default-ttl-seconds.audio:1500}") Integer audioTtl,
@Value("${cache.default-ttl-seconds.native:300}") Integer nativeTtl) {

return CacheDefaultTtlProperties.of(bannerTtl, videoTtl, audioTtl, nativeTtl);
}

@Bean
BidResponseCreator bidResponseCreator(
CoreCacheService coreCacheService,
Expand All @@ -804,7 +815,8 @@ BidResponseCreator bidResponseCreator(
Clock clock,
JacksonMapper mapper,
@Value("${cache.banner-ttl-seconds:#{null}}") Integer bannerCacheTtl,
@Value("${cache.video-ttl-seconds:#{null}}") Integer videoCacheTtl) {
@Value("${cache.video-ttl-seconds:#{null}}") Integer videoCacheTtl,
CacheDefaultTtlProperties cacheDefaultTtlProperties) {

return new BidResponseCreator(
coreCacheService,
Expand All @@ -819,7 +831,8 @@ BidResponseCreator bidResponseCreator(
truncateAttrChars,
clock,
mapper,
CacheTtl.of(bannerCacheTtl, videoCacheTtl));
CacheTtl.of(bannerCacheTtl, videoCacheTtl),
cacheDefaultTtlProperties);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.prebid.server.spring.config.model;

import lombok.Value;

@Value(staticConstructor = "of")
public class CacheDefaultTtlProperties {

Integer bannerTtl;

Integer videoTtl;

Integer audioTtl;

Integer nativeTtl;
}
Loading
Loading