From 42511e25bb2beaf731446cae4124992ef498febf Mon Sep 17 00:00:00 2001 From: Ben Gojanski Date: Tue, 10 Nov 2026 21:26:19 +0200 Subject: [PATCH 1/6] IntentIQ Identity module: server-side identity resolution Co-Authored-By: Claude Opus 5.5 --- extra/bundle/pom.xml | 5 + extra/modules/intentiq-identity/README.md | 220 +++++ extra/modules/intentiq-identity/pom.xml | 32 + .../intentiq/identity/cache/CacheKey.java | 9 + .../intentiq/identity/cache/CacheResult.java | 51 ++ .../identity/cache/CacheTtlPolicy.java | 41 + .../identity/cache/IdentityCache.java | 269 +++++++ .../identity/cache/IdentityStore.java | 16 + .../intentiq/identity/cache/KeyType.java | 14 + .../identity/cache/RedisIdentityStore.java | 55 ++ .../identity/cache/RedisStatsReporter.java | 53 ++ .../config/IntentiqIdentityConfig.java | 146 ++++ .../metric/IntentiqIdentityMetrics.java | 183 +++++ .../metric/NoopIntentiqIdentityMetrics.java | 26 + .../model/IntentiqIdentityModuleContext.java | 13 + .../model/config/CacheProperties.java | 43 + .../config/IntentiqIdentityProperties.java | 32 + .../model/config/RedisProperties.java | 13 + .../IntentiqIdentityAuctionResponseHook.java | 250 ++++++ .../identity/v1/IntentiqIdentityModule.java | 17 + ...iqIdentityProcessedAuctionRequestHook.java | 478 +++++++++++ .../identity/v1/core/ConfigResolver.java | 50 ++ .../identity/v1/core/DeviceUserAgent.java | 58 ++ .../v1/core/FirstPartyKeyExtractor.java | 130 +++ .../intentiq/identity/v1/core/IiqParam.java | 45 ++ .../identity/cache/IdentityCacheTest.java | 447 +++++++++++ .../cache/RedisIdentityStoreTest.java | 128 +++ .../cache/RedisStatsReporterTest.java | 133 ++++ .../config/IntentiqIdentityConfigTest.java | 175 ++++ .../metric/IntentiqIdentityMetricsTest.java | 165 ++++ ...tentiqIdentityAuctionResponseHookTest.java | 233 ++++++ .../v1/IntentiqIdentityModuleTest.java | 55 ++ ...entityProcessedAuctionRequestHookTest.java | 750 ++++++++++++++++++ .../identity/v1/core/DeviceUserAgentTest.java | 39 + .../v1/core/FirstPartyKeyExtractorTest.java | 146 ++++ extra/modules/pom.xml | 1 + .../configs/prebid-config-with-intentiq.yaml | 106 +++ 37 files changed, 4627 insertions(+) create mode 100644 extra/modules/intentiq-identity/README.md create mode 100644 extra/modules/intentiq-identity/pom.xml create mode 100644 extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/CacheKey.java create mode 100644 extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/CacheResult.java create mode 100644 extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/CacheTtlPolicy.java create mode 100644 extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/IdentityCache.java create mode 100644 extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/IdentityStore.java create mode 100644 extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/KeyType.java create mode 100644 extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/RedisIdentityStore.java create mode 100644 extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/RedisStatsReporter.java create mode 100644 extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/config/IntentiqIdentityConfig.java create mode 100644 extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/metric/IntentiqIdentityMetrics.java create mode 100644 extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/metric/NoopIntentiqIdentityMetrics.java create mode 100644 extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/model/IntentiqIdentityModuleContext.java create mode 100644 extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/model/config/CacheProperties.java create mode 100644 extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/model/config/IntentiqIdentityProperties.java create mode 100644 extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/model/config/RedisProperties.java create mode 100644 extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityAuctionResponseHook.java create mode 100644 extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityModule.java create mode 100644 extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityProcessedAuctionRequestHook.java create mode 100644 extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/ConfigResolver.java create mode 100644 extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/DeviceUserAgent.java create mode 100644 extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/FirstPartyKeyExtractor.java create mode 100644 extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/IiqParam.java create mode 100644 extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/cache/IdentityCacheTest.java create mode 100644 extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/cache/RedisIdentityStoreTest.java create mode 100644 extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/cache/RedisStatsReporterTest.java create mode 100644 extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/config/IntentiqIdentityConfigTest.java create mode 100644 extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/metric/IntentiqIdentityMetricsTest.java create mode 100644 extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityAuctionResponseHookTest.java create mode 100644 extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityModuleTest.java create mode 100644 extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityProcessedAuctionRequestHookTest.java create mode 100644 extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/DeviceUserAgentTest.java create mode 100644 extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/FirstPartyKeyExtractorTest.java create mode 100644 sample/configs/prebid-config-with-intentiq.yaml diff --git a/extra/bundle/pom.xml b/extra/bundle/pom.xml index f0d7d84d381..d7a842c3a3d 100644 --- a/extra/bundle/pom.xml +++ b/extra/bundle/pom.xml @@ -75,6 +75,11 @@ pb-rule-engine ${project.version} + + org.prebid.server.hooks.modules + intentiq-identity + ${project.version} + diff --git a/extra/modules/intentiq-identity/README.md b/extra/modules/intentiq-identity/README.md new file mode 100644 index 00000000000..fc7d50e5d4a --- /dev/null +++ b/extra/modules/intentiq-identity/README.md @@ -0,0 +1,220 @@ +## Overview + +The IntentIQ Identity module enriches an incoming OpenRTB request by adding resolved IDs to +`user.eids`. At the `processed-auction-request` stage it calls the IntentIQ Bid Enhancement S2S API +(`ProfilesEngineServlet`) and merges the eids from the response into `user.eids` before the request +is sent to bidders. Please contact your IntentIQ account manager to get a partner token. + +See the [S2S integration docs](https://s2s.documents.intentiq.com/) for the full API contract. + +## Setup + +### Execution Plan + +This module runs at two stages: `processed-auction-request` (enrich `user.eids`) and, optionally, +`auction-response` (report winning bids as impressions to `reports-endpoint`). Enable the module and +add the hook(s) to the execution plan: + +```yaml +hooks: + intentiq-identity: + enabled: true + host-execution-plan: > + { + "endpoints": { + "/openrtb2/auction": { + "stages": { + "processed-auction-request": { + "groups": [ + { "timeout": 100, "hook-sequence": [ + { "module-code": "intentiq-identity", + "hook-impl-code": "intentiq-identity-processed-auction-request-hook" } ] } ] + }, + "auction-response": { + "groups": [ + { "timeout": 100, "hook-sequence": [ + { "module-code": "intentiq-identity", + "hook-impl-code": "intentiq-identity-auction-response-hook" } ] } ] + } + } + } + } + } + modules: + intentiq-identity: + api-endpoint: https://be-api-s2s.intentiq.com/profiles_engine/ProfilesEngineServlet + reports-endpoint: https://reports-s2s.intentiq.com/profiles_engine/ProfilesEngineServlet + partner-id: "1234567890" + timeout: 1000 + cache-max-size: 100000 + cache: + enabled: true + ttlseconds: 43200 + max-keys: 10 + ttl-ceiling-first-party-seconds: 86400 + ttl-ceiling-third-party-seconds: 43200 + ttl-ceiling-device-seconds: 3600 + negative-ttl-seconds: 120 + in-progress-ttl-seconds: 1800 + redis: + host: localhost + port: 6379 +``` + +Use the region-specific `api-endpoint`: US `be-api-s2s.intentiq.com`, EU `be-api-s2s-gdpr.intentiq.com`, +APAC `be-api-s2s-apac.intentiq.com`. When `api-endpoint` is empty the hook is a no-op. + +### Account-level config + +Host-level config (above) provides defaults. Account-specific values can be set under the account's +`hooks.modules.intentiq-identity` config and are merged over the host defaults per request — so +`partner-id`, `timeout`, and `cache.*` can be tuned per account. + +## Module Configuration Parameters + +| Param Name | Level | Required | Type | Default | Description | +|:------------------|:--------|:---------|:--------|:---------|:---------------------------------------------------------------------| +| api-endpoint | host | yes | string | none | Bid Enhancement `ProfilesEngineServlet` URL (region-specific) | +| reports-endpoint | host | no | string | none | Impression-reporting `ProfilesEngineServlet` URL; blank disables it | +| partner-id | account | yes | string | none | Partner token from IntentIQ, sent as the `dpi` query parameter | +| timeout | account | no | long | 1000 | HTTP timeout (ms) for the identity-resolution call | +| cache.enabled | account | no | boolean | false | Use the Caffeine + Redis cache (host must configure `redis.*`) | +| cache.ttlseconds | account | no | int | 43200 | Fallback TTL (seconds) when the response omits `cttl` (12h) | +| cache.max-keys | account | no | int | 10 | Max alias keys derived per request (caps eid-stuffed requests) | +| cache.ttl-ceiling-first-party-seconds | account | no | int | 86400 | Upper bound on TTL for first-party id keys (pubcid, MAID, other eids) | +| cache.ttl-ceiling-third-party-seconds | account | no | int | 43200 | Upper bound on TTL for third-party id keys (`intentiq.com`) | +| cache.ttl-ceiling-device-seconds | account | no | int | 3600 | Upper bound on TTL for the probabilistic device-composite key | +| cache.negative-ttl-seconds | account | no | int | 120 | TTL for the negative (unresolvable id) sentinel | +| cache.in-progress-ttl-seconds | account | no | int | 1800 | TTL for the IN_PROGRESS marker that dedups concurrent resolution calls (matches the backend's 30m window) | +| cache-max-size | host | no | long | 100000 | Max entries in the local (Caffeine) layer | +| metrics-enabled | host | no | boolean | true | Record the module's `custom.*` metrics; set `false` to opt out | +| redis.host | host | cond. | string | none | Redis host (required when caching) | +| redis.port | host | cond. | int | none | Redis port (required when caching) | +| redis.password | host | no | string | none | Redis password | + +The hook sends `at/mi/pt/dpn/srvrReq` constants plus `dpi` (= `partner-id`), and — when present on +the request — `ip`, `ipv6`, `uas`, `ref` (site domain / app name), `iiquid` (an existing +`intentiq.com` eid), and `pcid`+`idtype` from `device.ifa` (`idtype 4` for MAID/AAID, `idtype 8` for +CTV with the id upper-cased; skipped when `device.lmt = 1`). The response `data.eids` are merged into +`user.eids`; on any failure the hook takes no action and the auction proceeds unchanged. + +### Caching + +When caching is enabled, resolved eids are cached in two layers: **Caffeine** (L1, in-process) backed +by an `IdentityStore` (L2, shared) — **Redis** by default. L2 failures are non-fatal: the hook falls +through to a live API call. A partner can use a non-Redis backend by supplying their own +`IdentityStore` bean (the default Redis store is `@ConditionalOnMissingBean`). + +**Multi-key (alias) caching.** Every relevant first-party id on the request becomes a namespaced +alias key, ordered by priority: `iiq:` (`intentiq.com`), `pubcid:` (`pubcid.org` / +`sharedid.org`), `maid:` (`device.ifa`; upper-cased for CTV, skipped when `device.lmt = 1`), +`:` for any other eid (e.g. `uidapi.com`), and a probabilistic `dev:` composite +as last resort. Keys are de-duplicated and capped at `cache.max-keys`. On a lookup the +highest-priority key with a live entry wins, and that entry is **back-filled** under every other key +that missed — so the alias graph grows over time and a later request carrying any of those ids hits. +Differing resolutions are never merged (only the single winning entry propagates). + +**TTL.** The response `cttl` (or `cache.ttlseconds` when omitted) always wins, but is capped per id +class by `cache.ttl-ceiling-{first-party,third-party,device}-seconds` — the cache holds the volatile +resolved eids, so ceilings are upper bounds, not the IntentIQ backend's long mapping TTLs. + +**Negative caching.** When the API resolves no eids for a request, a negative sentinel is written +under all candidate keys so unresolvable ids do not re-hit the S2S API on every request; a negative +cache hit makes the hook take no action and skip the call. The suppression window honors the response +`cttl` when present (the backend signals how long to suppress for empty/invalid traffic, capped at the +first-party ceiling), falling back to `cache.negative-ttl-seconds` when `cttl` is absent. + +**In-progress dedup.** On a full miss, before the live call an `IN_PROGRESS` marker +(`cache.in-progress-ttl-seconds`) is written under all candidate keys. A concurrent request for the +same id reads the marker and skips firing a duplicate call (it proceeds without enrichment); the +in-flight call populates the cache for subsequent requests. The marker is overwritten by the resolved +(or negative) entry when the call completes, or expires if it never does. A resolved entry is always +preferred over the marker on read. + +### Impression reporting + +When `reports-endpoint` is set and the `auction-response` hook is in the execution plan, the module +reports each winning `seatbid[].bid[]` to the IntentIQ impression API — a fire-and-forget GET to +`?at=45&rtype=1&dpi=&rdata=`. The `rdata` +carries `bidderCode` (seat), `cpm`, `currency`, `placementId` (imp id), `biddingPlatformId=4`, +`vrref` (site domain / app), `ip`, `ua`, `prebidAuctionId`, and `abTestUuid`. The `abTestUuid` is the +one returned by the resolution response — the resolution hook stashes it in the module context and +this hook reads it (omitted on a cache hit, since no fresh IIQ response was produced). With +`reports-endpoint` blank the hook is a no-op. The bid response is never modified. + +### Metrics + +The hook framework already emits per-module `call` / `success.*` / `failure` / `timeout` / +`execution-error` / `duration` for free under +`modules.module.intentiq-identity.stage..hook..…` (`` is `procauction` for the +request hook, `auctionresponse` for the response hook). The granularity is driven by what each hook +returns (`update` when enriched, `noAction` otherwise, `failed` on error). + +In addition the module emits the custom counters below (implemented in `IntentiqIdentityMetrics`). +Recording is **on by default**; set `metrics-enabled: false` (host-level) to disable it entirely. +**Per-partner** metrics are suffixed with `_` (the `partner-id`, never an internal backend id), +following the per-partner Graphite naming convention so the same per-partner Grafana templating applies; the +suffix is omitted when no partner id is configured. The `cache.*` outcome counters are additionally +broken down **by layer** (`l1` = Caffeine in-process, `l2` = Redis) and by `` — `third_party` +(`intentiq.com`), `first_party` (pubcid / MAID / other eids), or `device` (the probabilistic UA+IP +composite); on a HIT/NEGATIVE/IN_PROGRESS the keytype is the key that matched, on a full miss the +request's highest-priority candidate. + +``` +modules.module.intentiq-identity.custom.cache.l1.hit._ # positive entry served from L1 (Caffeine) +modules.module.intentiq-identity.custom.cache.l2.hit._ # positive entry served from L2 (Redis) +modules.module.intentiq-identity.custom.cache.l1.negative.hit._ # negative sentinel from L1; counted as miss, no API call +modules.module.intentiq-identity.custom.cache.l2.negative.hit._ # negative sentinel from L2; counted as miss, no API call +modules.module.intentiq-identity.custom.cache.l1.in_progress._ # in-flight marker from L1; duplicate API call skipped +modules.module.intentiq-identity.custom.cache.l2.in_progress._ # in-flight marker from L2; duplicate API call skipped +modules.module.intentiq-identity.custom.cache.miss._ # full miss (neither L1 nor L2) -> API called +modules.module.intentiq-identity.custom.api.success_ # resolution API responded and parsed OK +modules.module.intentiq-identity.custom.api.error_ # resolution API failed/timed out/unparseable +modules.module.intentiq-identity.custom.api.latency_ # resolution API call duration (timer; every call) +modules.module.intentiq-identity.custom.flow.latency_ # whole-flow latency: enrich hook -> bid release (timer; per auction) +modules.module.intentiq-identity.custom.enriched_ # eids added to user.eids (a match) +modules.module.intentiq-identity.custom.eids.none_ # resolution produced no eids (pairs with enriched for match rate) +modules.module.intentiq-identity.custom.skip.no_endpoint_ # no api-endpoint configured; resolution skipped before any API call +modules.module.intentiq-identity.custom.tc._ # one counter per enumerated termination-cause id +modules.module.intentiq-identity.custom.impression.reported_ # winning bid reported to reports-endpoint (overall) +modules.module.intentiq-identity.custom.impression.error_ # impression report call failed +``` + +Shared L1 (Caffeine) / L2 (Redis) health is process-wide, so these are emitted **globally — without +the `_` suffix** (one series each): + +``` +modules.module.intentiq-identity.custom.l1.size # current L1 entry count (gauge; vs cache-max-size) +modules.module.intentiq-identity.custom.l1.eviction # cumulative L1 evictions (gauge) +modules.module.intentiq-identity.custom.l1.get.error # L1 read threw (≈never; treated as miss) +modules.module.intentiq-identity.custom.l1.put.error # L1 write threw (≈never) +modules.module.intentiq-identity.custom.l2.get.latency # L2 GET duration (timer; every probe) +modules.module.intentiq-identity.custom.l2.put.latency # L2 PUT duration (timer; every write) +modules.module.intentiq-identity.custom.l2.get.error # L2 GET failed -> fell through to live API (fail-open) +modules.module.intentiq-identity.custom.l2.put.error # L2 PUT failed (entry still in L1, not in shared store) +modules.module.intentiq-identity.custom.l2.size # Redis DBSIZE (gauge; polled ~30s; INSTANCE-WIDE) +modules.module.intentiq-identity.custom.l2.eviction # Redis INFO evicted_keys (gauge; polled ~30s; INSTANCE-WIDE) +``` + +JVM / system health (free memory, GC) is provided by **prebid-server core**, not this module — enable +`metrics.jmx.enabled: true` and it registers `jvm.memory.*` and `jvm.gc.*` into the same registry. + +> **Prometheus / scrape gotcha:** if these are scraped via the Prometheus `/metrics` endpoint, set +> `metrics.metricType: counter` — **not** the default `flushingCounter`, which resets after each +> report (correct for Graphite/InfluxDB push, wrong for scrape) and would make the counters read as +> near-zero on every scrape. The server logs a warning when Prometheus is enabled with +> `flushingCounter`. + +## Running the demo + +1. Build the bundle: `mvn clean package --file extra/pom.xml` +2. Set `api-endpoint` and `partner-id` in `sample/configs/prebid-config-with-intentiq.yaml`. +3. Run: + `java -jar target/prebid-server-bundle.jar --spring.config.additional-location=sample/configs/prebid-config-with-intentiq.yaml` +4. POST a request to `/openrtb2/auction` and observe `user.eids` enriched in `ext.debug.resolvedrequest`. + +## Maintainer contacts + +Any suggestions or questions can be directed to the IntentIQ team. Alternatively please open a new +[issue](https://github.com/prebid/prebid-server-java/issues/new) or +[pull request](https://github.com/prebid/prebid-server-java/pulls) in this repository. diff --git a/extra/modules/intentiq-identity/pom.xml b/extra/modules/intentiq-identity/pom.xml new file mode 100644 index 00000000000..a0def6b962f --- /dev/null +++ b/extra/modules/intentiq-identity/pom.xml @@ -0,0 +1,32 @@ + + + 4.0.0 + + + org.prebid.server.hooks.modules + all-modules + 4.6.0-SNAPSHOT + + + intentiq-identity + + intentiq-identity + IntentIQ server-side identity resolution module + + + 1.6.1 + + + + + io.vertx + vertx-redis-client + + + com.github.ua-parser + uap-java + ${uap-java.version} + + + diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/CacheKey.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/CacheKey.java new file mode 100644 index 00000000000..b004f9347bf --- /dev/null +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/CacheKey.java @@ -0,0 +1,9 @@ +package org.prebid.server.hooks.modules.intentiq.identity.cache; + +/** + * A single namespaced cache key derived from a first-party identifier on the bid request, together + * with its {@link KeyType} (used to pick the TTL ceiling). A request yields an ordered list of these; + * the resolved identity is aliased across all of them. + */ +public record CacheKey(String key, KeyType type) { +} diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/CacheResult.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/CacheResult.java new file mode 100644 index 00000000000..07e50ee7373 --- /dev/null +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/CacheResult.java @@ -0,0 +1,51 @@ +package org.prebid.server.hooks.modules.intentiq.identity.cache; + +import com.iab.openrtb.request.Eid; + +import java.util.List; + +/** + * Outcome of a multi-key cache lookup: + *
    + *
  • {@link State#HIT} — a positive entry was found; {@link #eids()} carries the resolved identity.
  • + *
  • {@link State#NEGATIVE} — a negative sentinel was found (the id is known-unresolvable); skip the + * upstream call and do not enrich.
  • + *
  • {@link State#IN_PROGRESS} — a resolution call for this id is already in flight; skip the + * upstream call (do not fire a duplicate) and do not enrich.
  • + *
  • {@link State#MISS} — nothing cached; fetch from the API.
  • + *
+ */ +public record CacheResult(State state, List eids, KeyType keyType, Layer layer) { + public enum State { + HIT, + NEGATIVE, + IN_PROGRESS, + MISS + } + + /** Which cache layer served the outcome: {@code L1} (in-process Caffeine) or {@code L2} (Redis). */ + public enum Layer { + L1, + L2 + } + + // keyType is the type of the candidate key that produced the outcome (HIT/NEGATIVE/IN_PROGRESS); + // both keyType and layer are null for MISS, where no key/layer matched. + private static final CacheResult MISS = new CacheResult(State.MISS, List.of(), null, null); + + public static CacheResult hit(List eids, KeyType keyType, Layer layer) { + return new CacheResult(State.HIT, eids, keyType, layer); + } + + public static CacheResult negative(KeyType keyType, Layer layer) { + return new CacheResult(State.NEGATIVE, List.of(), keyType, layer); + } + + public static CacheResult inProgress(KeyType keyType, Layer layer) { + return new CacheResult(State.IN_PROGRESS, List.of(), keyType, layer); + } + + public static CacheResult miss() { + return MISS; + } +} diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/CacheTtlPolicy.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/CacheTtlPolicy.java new file mode 100644 index 00000000000..b1e571dd682 --- /dev/null +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/CacheTtlPolicy.java @@ -0,0 +1,41 @@ +package org.prebid.server.hooks.modules.intentiq.identity.cache; + +/** + * TTL policy for cached identity entries. The IntentIQ API {@code cttl} (or the configured default + * when absent) always wins, but is capped by a per-{@link KeyType} ceiling — we cache the volatile + * resolved eids, not the stable cookie mapping, so ceilings are upper bounds only and deliberately + * far shorter than the IntentIQ backend's mapping TTLs. Negative (unresolvable) entries and the + * in-progress marker each use a separate short TTL. + */ +public record CacheTtlPolicy(long defaultTtlMs, + long firstPartyCeilingMs, + long thirdPartyCeilingMs, + long deviceCeilingMs, + long negativeTtlMs, + long inProgressTtlMs) { + public long ceilingFor(KeyType type) { + return switch (type) { + case FIRST_PARTY -> firstPartyCeilingMs; + case THIRD_PARTY -> thirdPartyCeilingMs; + case DEVICE -> deviceCeilingMs; + }; + } + + /** + * Effective positive TTL for a key: {@code min(cttl-or-default, ceiling(type))}. + */ + public long effectiveTtlMs(KeyType type, long cttlMs) { + final long base = cttlMs > 0 ? cttlMs : defaultTtlMs; + return Math.min(base, ceilingFor(type)); + } + + /** + * Suppression TTL for a negative (unresolvable) entry. On an empty/invalid response the IntentIQ + * backend signals how long to suppress re-querying this user via {@code cttl}; honor it when present + * (bounded by the first-party ceiling as a safety cap against absurd values), else fall back to the + * configured default negative TTL. + */ + public long negativeTtlMs(long cttlMs) { + return cttlMs > 0 ? Math.min(cttlMs, firstPartyCeilingMs) : negativeTtlMs; + } +} diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/IdentityCache.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/IdentityCache.java new file mode 100644 index 00000000000..1027acc34d5 --- /dev/null +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/IdentityCache.java @@ -0,0 +1,269 @@ +package org.prebid.server.hooks.modules.intentiq.identity.cache; + +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; +import com.github.benmanes.caffeine.cache.Expiry; +import com.iab.openrtb.request.Eid; +import io.vertx.core.CompositeFuture; +import io.vertx.core.Future; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.jspecify.annotations.NonNull; +import org.prebid.server.hooks.modules.intentiq.identity.metric.IntentiqIdentityMetrics; +import org.prebid.server.json.JacksonMapper; +import org.prebid.server.log.Logger; +import org.prebid.server.log.LoggerFactory; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; + +/** + * Dual-layer, multi-key (alias) cache for resolved eids: Caffeine (L1, in-process) backed by a + * pluggable {@link IdentityStore} (L2, shared; Redis by default). + * + *

A request yields an ordered list of {@link CacheKey}s (one per first-party id present). On read, + * the highest-priority key with a live entry wins and that entry is back-filled under every other key + * that missed, so the alias graph grows over time and a later request carrying any of those ids hits + * (strategy B). On a full miss the caller fetches once and writes the entry under all keys. + * + *

Entries carry the IntentIQ {@code cttl} capped by a per-{@link KeyType} ceiling (see + * {@link CacheTtlPolicy}). Unresolvable ids are cached as a short-lived negative sentinel so they do + * not re-hit the upstream API. L2 failures are swallowed (fail-open) so the auction can fall through + * to a live call. Differing resolutions are never merged — only the single winning entry propagates. + */ +public class IdentityCache { + + private static final Logger logger = LoggerFactory.getLogger(IdentityCache.class); + + private final Cache local; + private final IdentityStore store; + private final JacksonMapper mapper; + private final CacheTtlPolicy ttlPolicy; + private final IntentiqIdentityMetrics metrics; + + public IdentityCache(long maxSize, CacheTtlPolicy ttlPolicy, IdentityStore store, JacksonMapper mapper, + IntentiqIdentityMetrics metrics) { + this.local = Caffeine.newBuilder() + .maximumSize(maxSize) + .expireAfter(new CacheEntryExpiry()) + .recordStats() + .build(); + this.ttlPolicy = Objects.requireNonNull(ttlPolicy); + this.store = Objects.requireNonNull(store); + this.mapper = Objects.requireNonNull(mapper); + this.metrics = Objects.requireNonNull(metrics); + // L1 capacity gauges: current size (vs cache-max-size) and cumulative evictions. + metrics.registerL1Gauges(local::estimatedSize, () -> local.stats().evictionCount()); + } + + public Future get(List keys) { + if (keys == null || keys.isEmpty()) { + return Future.succeededFuture(CacheResult.miss()); + } + + // L1 sweep in priority order; Caffeine evicts expired entries, so a present entry is live. + // A resolved entry always wins; an in-progress marker is a fallback that short-circuits the + // L2 probe (this instance already knows a call is in flight) without firing a duplicate. + KeyType inProgressType = null; + for (int i = 0; i < keys.size(); i++) { + final CacheEntry entry = l1Get(keys.get(i).key()); + if (entry == null) { + continue; + } + if (entry.isInProgress()) { + if (inProgressType == null) { + inProgressType = keys.get(i).type(); + } + continue; + } + backfill(keys, i, entry); + return Future.succeededFuture(toResult(entry, keys.get(i).type(), CacheResult.Layer.L1)); + } + if (inProgressType != null) { + return Future.succeededFuture(CacheResult.inProgress(inProgressType, CacheResult.Layer.L1)); + } + + // Full L1 miss: probe all keys in L2 concurrently. Prefer the highest-priority resolved entry; + // fall back to an in-progress marker only if no resolved entry is found under any key. + return l2GetAll(keys).map(entries -> { + KeyType l2InProgressType = null; + for (int i = 0; i < entries.size(); i++) { + final CacheEntry entry = entries.get(i); + if (entry == null) { + continue; + } + if (entry.isInProgress()) { + if (l2InProgressType == null) { + l1Put(keys.get(i).key(), entry); + l2InProgressType = keys.get(i).type(); + } + continue; + } + l1Put(keys.get(i).key(), entry); + backfill(keys, i, entry); + return toResult(entry, keys.get(i).type(), CacheResult.Layer.L2); + } + return l2InProgressType != null + ? CacheResult.inProgress(l2InProgressType, CacheResult.Layer.L2) + : CacheResult.miss(); + }); + } + + public void put(List keys, List eids, long cttlMs) { + for (CacheKey key : keys) { + final long ttl = ttlPolicy.effectiveTtlMs(key.type(), cttlMs); + writeBoth(key.key(), CacheEntry.of(eids, false, false, System.currentTimeMillis() + ttl), ttl); + } + } + + public void putNegative(List keys, long cttlMs) { + final long ttl = ttlPolicy.negativeTtlMs(cttlMs); + for (CacheKey key : keys) { + writeBoth(key.key(), CacheEntry.of(List.of(), true, false, System.currentTimeMillis() + ttl), ttl); + } + } + + /** + * Mark a resolution as in flight: write an IN_PROGRESS sentinel under every key with the short + * in-progress TTL, so a concurrent request for the same id reads it and skips firing a duplicate + * upstream call. Overwritten by {@link #put} / {@link #putNegative} when the call completes; if the + * call never completes the marker simply expires. + */ + public void putInProgress(List keys) { + final long ttl = ttlPolicy.inProgressTtlMs(); + for (CacheKey key : keys) { + writeBoth(key.key(), CacheEntry.of(List.of(), false, true, System.currentTimeMillis() + ttl), ttl); + } + } + + /** Propagate the winning entry under every other key that missed, capped by each key's ceiling. */ + private void backfill(List keys, int hitIndex, CacheEntry hit) { + final long remaining = hit.getExp() - System.currentTimeMillis(); + if (remaining <= 0) { + return; + } + + for (int i = 0; i < keys.size(); i++) { + if (i == hitIndex) { + continue; + } + final CacheKey key = keys.get(i); + if (l1Get(key.key()) != null) { + continue; + } + final long ttl = Math.min(remaining, ttlPolicy.ceilingFor(key.type())); + writeBoth(key.key(), CacheEntry.of(hit.getEids(), hit.isNegative(), hit.isInProgress(), + System.currentTimeMillis() + ttl), ttl); + } + } + + private void writeBoth(String key, CacheEntry entry, long ttlMs) { + l1Put(key, entry); + final long startNanos = System.nanoTime(); + store.put(key, mapper.encodeToString(entry), ttlMs) + .onComplete(ignored -> metrics.l2PutLatency(System.nanoTime() - startNanos)) + .onFailure(throwable -> { + metrics.l2PutError(); + logger.warn("IntentIQ identity cache L2 PUT failed", throwable); + }); + } + + // L1 (Caffeine) is in-process and effectively never fails, but wrap it so a pathological failure + // (e.g. expiry/weigher throwing, OOM) is counted and swallowed (fail open) rather than aborting. + private void l1Put(String key, CacheEntry entry) { + try { + local.put(key, entry); + } catch (RuntimeException e) { + metrics.l1PutError(); + logger.warn("IntentIQ identity cache L1 PUT failed", e); + } + } + + private CacheEntry l1Get(String key) { + try { + return local.getIfPresent(key); + } catch (RuntimeException e) { + metrics.l1GetError(); + logger.warn("IntentIQ identity cache L1 GET failed", e); + return null; + } + } + + private Future> l2GetAll(List keys) { + final List> futures = new ArrayList<>(); + for (CacheKey key : keys) { + final long startNanos = System.nanoTime(); + futures.add(store.get(key.key()) + .onComplete(ignored -> metrics.l2GetLatency(System.nanoTime() - startNanos)) + .map(this::decodeValid) + .otherwise(throwable -> { + // L2 read failed: we fall through to a live API call (fail open). Count it so the + // otherwise-swallowed failure is visible for alerting. + metrics.l2GetError(); + logger.warn("IntentIQ identity cache L2 GET failed", throwable); + return null; + })); + } + return Future.join(futures).map(CompositeFuture::list); + } + + private CacheEntry decodeValid(String value) { + if (value == null) { + return null; + } + final CacheEntry entry = mapper.decodeValue(value, CacheEntry.class); + if (entry == null || entry.getExp() <= System.currentTimeMillis()) { + return null; + } + return entry; + } + + private static CacheResult toResult(CacheEntry entry, KeyType keyType, CacheResult.Layer layer) { + if (entry.isInProgress()) { + return CacheResult.inProgress(keyType, layer); + } + return entry.isNegative() + ? CacheResult.negative(keyType, layer) + : CacheResult.hit(entry.getEids(), keyType, layer); + } + + @Data + @NoArgsConstructor + @AllArgsConstructor(staticName = "of") + static class CacheEntry { + + List eids; + + boolean negative; + + boolean inProgress; + + long exp; + } + + private static class CacheEntryExpiry implements Expiry { + + @Override + public long expireAfterCreate(@NonNull String key, @NonNull CacheEntry value, long currentTime) { + return remainingNanos(value); + } + + @Override + public long expireAfterUpdate(@NonNull String key, @NonNull CacheEntry value, + long currentTime, long currentDuration) { + return remainingNanos(value); + } + + @Override + public long expireAfterRead(@NonNull String key, @NonNull CacheEntry value, + long currentTime, long currentDuration) { + return currentDuration; + } + + private static long remainingNanos(CacheEntry value) { + return Math.max(0, value.getExp() - System.currentTimeMillis()) * 1_000_000L; + } + } +} diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/IdentityStore.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/IdentityStore.java new file mode 100644 index 00000000000..dd589e02629 --- /dev/null +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/IdentityStore.java @@ -0,0 +1,16 @@ +package org.prebid.server.hooks.modules.intentiq.identity.cache; + +import io.vertx.core.Future; + +/** + * Generic, backend-agnostic key/value store used as the shared (L2) layer of {@link IdentityCache}. + * The default implementation is Redis ({@link RedisIdentityStore}); a partner can provide a different + * backend by supplying another implementation. Values are opaque strings (the cache handles + * serialization), so the store stays decoupled from the eid model. + */ +public interface IdentityStore { + + Future get(String key); + + Future put(String key, String value, long ttlMs); +} diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/KeyType.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/KeyType.java new file mode 100644 index 00000000000..4a3517c2b14 --- /dev/null +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/KeyType.java @@ -0,0 +1,14 @@ +package org.prebid.server.hooks.modules.intentiq.identity.cache; + +/** + * Classifies a cache key by the kind of identifier it carries, so the TTL ceiling can be applied + * per id class (see {@link CacheTtlPolicy}). First-party ids are treated as longer-lived than + * third-party / probabilistic ones. Note {@code intentiq.com} is treated as {@link #THIRD_PARTY}, + * matching how the IntentIQ backend classifies the IntentIQ cookie id. + */ +public enum KeyType { + + FIRST_PARTY, + THIRD_PARTY, + DEVICE +} diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/RedisIdentityStore.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/RedisIdentityStore.java new file mode 100644 index 00000000000..5ee618c3ac3 --- /dev/null +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/RedisIdentityStore.java @@ -0,0 +1,55 @@ +package org.prebid.server.hooks.modules.intentiq.identity.cache; + +import io.vertx.core.Future; +import io.vertx.redis.client.RedisAPI; +import lombok.RequiredArgsConstructor; + +import java.util.List; + +/** + * Redis-backed {@link IdentityStore} (the default L2 backend). Stores values with a per-entry TTL + * via {@code SET key value PX }. + */ +@RequiredArgsConstructor +public class RedisIdentityStore implements IdentityStore { + + private static final String EVICTED_KEYS_FIELD = "evicted_keys:"; + + private final RedisAPI redis; + + @Override + public Future get(String key) { + return redis.get(key).map(response -> response != null ? response.toString() : null); + } + + @Override + public Future put(String key, String value, long ttlMs) { + return redis.set(List.of(key, value, "PX", Long.toString(ttlMs))).mapEmpty(); + } + + /** Current key count of the selected DB ({@code DBSIZE}). Instance-wide, not module-scoped. */ + public Future dbSize() { + return redis.dbsize().map(response -> response != null ? response.toLong() : 0L); + } + + /** Cumulative {@code evicted_keys} from {@code INFO stats}. Instance-wide. */ + public Future evictedKeys() { + return redis.info(List.of("stats")).map(RedisIdentityStore::parseEvictedKeys); + } + + private static Long parseEvictedKeys(io.vertx.redis.client.Response response) { + if (response == null) { + return 0L; + } + for (final String line : response.toString().split("\\r?\\n")) { + if (line.startsWith(EVICTED_KEYS_FIELD)) { + try { + return Long.parseLong(line.substring(EVICTED_KEYS_FIELD.length()).trim()); + } catch (NumberFormatException e) { + return 0L; + } + } + } + return 0L; + } +} diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/RedisStatsReporter.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/RedisStatsReporter.java new file mode 100644 index 00000000000..b03748be73f --- /dev/null +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/RedisStatsReporter.java @@ -0,0 +1,53 @@ +package org.prebid.server.hooks.modules.intentiq.identity.cache; + +import io.vertx.core.Vertx; +import org.prebid.server.hooks.modules.intentiq.identity.metric.IntentiqIdentityMetrics; +import org.prebid.server.log.Logger; +import org.prebid.server.log.LoggerFactory; + +import java.util.Objects; +import java.util.concurrent.atomic.AtomicLong; + +/** + * Periodically polls Redis (L2) {@code DBSIZE} and {@code INFO stats} {@code evicted_keys} and exposes + * them as the global {@code l2.size} / {@code l2.eviction} gauges. Redis stats are asynchronous and + * instance-wide, so (unlike Caffeine's in-process L1 counters) they can't be read inside a synchronous + * gauge — this caches the latest poll into atomics the gauges read. Both values are Redis-instance-wide, + * not module-scoped. + */ +public class RedisStatsReporter { + + private static final Logger logger = LoggerFactory.getLogger(RedisStatsReporter.class); + + private final RedisIdentityStore store; + private final Vertx vertx; + private final long pollIntervalMs; + private final AtomicLong size = new AtomicLong(); + private final AtomicLong evictions = new AtomicLong(); + + public RedisStatsReporter(RedisIdentityStore store, + Vertx vertx, + IntentiqIdentityMetrics metrics, + long pollIntervalMs) { + this.store = Objects.requireNonNull(store); + this.vertx = Objects.requireNonNull(vertx); + this.pollIntervalMs = pollIntervalMs; + metrics.registerL2Gauges(size::get, evictions::get); + } + + /** Poll once immediately, then on a fixed interval. Returns this for fluent wiring. */ + public RedisStatsReporter start() { + poll(); + vertx.setPeriodic(pollIntervalMs, id -> poll()); + return this; + } + + private void poll() { + store.dbSize() + .onSuccess(size::set) + .onFailure(t -> logger.warn("IntentIQ identity L2 DBSIZE poll failed", t)); + store.evictedKeys() + .onSuccess(evictions::set) + .onFailure(t -> logger.warn("IntentIQ identity L2 evicted_keys poll failed", t)); + } +} diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/config/IntentiqIdentityConfig.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/config/IntentiqIdentityConfig.java new file mode 100644 index 00000000000..87183d02546 --- /dev/null +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/config/IntentiqIdentityConfig.java @@ -0,0 +1,146 @@ +package org.prebid.server.hooks.modules.intentiq.identity.config; + +import com.codahale.metrics.MetricRegistry; +import io.vertx.core.Vertx; +import io.vertx.redis.client.Redis; +import io.vertx.redis.client.RedisAPI; +import io.vertx.redis.client.RedisOptions; +import org.apache.commons.lang3.StringUtils; +import org.prebid.server.hooks.modules.intentiq.identity.cache.CacheTtlPolicy; +import org.prebid.server.hooks.modules.intentiq.identity.cache.IdentityCache; +import org.prebid.server.hooks.modules.intentiq.identity.cache.IdentityStore; +import org.prebid.server.hooks.modules.intentiq.identity.cache.RedisIdentityStore; +import org.prebid.server.hooks.modules.intentiq.identity.cache.RedisStatsReporter; +import org.prebid.server.hooks.modules.intentiq.identity.metric.IntentiqIdentityMetrics; +import org.prebid.server.hooks.modules.intentiq.identity.metric.NoopIntentiqIdentityMetrics; +import org.prebid.server.hooks.modules.intentiq.identity.model.config.CacheProperties; +import org.prebid.server.hooks.modules.intentiq.identity.model.config.IntentiqIdentityProperties; +import org.prebid.server.hooks.modules.intentiq.identity.model.config.RedisProperties; +import org.prebid.server.hooks.modules.intentiq.identity.v1.IntentiqIdentityAuctionResponseHook; +import org.prebid.server.hooks.modules.intentiq.identity.v1.IntentiqIdentityModule; +import org.prebid.server.hooks.modules.intentiq.identity.v1.IntentiqIdentityProcessedAuctionRequestHook; +import org.prebid.server.hooks.modules.intentiq.identity.v1.core.ConfigResolver; +import org.prebid.server.hooks.modules.intentiq.identity.v1.core.FirstPartyKeyExtractor; +import org.prebid.server.json.JacksonMapper; +import org.prebid.server.json.JsonMerger; +import org.prebid.server.json.ObjectMapperProvider; +import org.prebid.server.vertx.httpclient.HttpClient; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.util.List; + +@ConditionalOnProperty(prefix = "hooks." + IntentiqIdentityModule.CODE, name = "enabled", havingValue = "true") +@Configuration +public class IntentiqIdentityConfig { + + // How often the L2 (Redis) size/eviction gauges are refreshed from DBSIZE / INFO. + private static final long REDIS_STATS_POLL_MS = 30_000L; + + @Bean + @ConfigurationProperties(prefix = "hooks.modules." + IntentiqIdentityModule.CODE) + IntentiqIdentityProperties intentiqIdentityProperties() { + return new IntentiqIdentityProperties(); + } + + @Bean + ConfigResolver intentiqIdentityConfigResolver(JsonMerger jsonMerger, IntentiqIdentityProperties properties) { + return new ConfigResolver(ObjectMapperProvider.mapper(), jsonMerger, properties); + } + + /** + * Default L2 store; can override caching by supplying your own {@link IdentityStore} bean. + */ + @Bean + @ConditionalOnMissingBean(IdentityStore.class) + @ConditionalOnProperty(prefix = "hooks.modules." + IntentiqIdentityModule.CODE + ".cache", + name = "enabled", havingValue = "true") + IdentityStore intentiqIdentityStore(IntentiqIdentityProperties properties, Vertx vertx) { + return new RedisIdentityStore(createRedisApi(properties.getRedis(), vertx)); + } + + @Bean + @ConditionalOnProperty(prefix = "hooks.modules." + IntentiqIdentityModule.CODE + ".cache", + name = "enabled", havingValue = "true") + IdentityCache intentiqIdentityCache(IntentiqIdentityProperties properties, + IdentityStore identityStore, + JacksonMapper mapper, + IntentiqIdentityMetrics metrics) { + final CacheProperties cache = properties.getCache(); + final CacheTtlPolicy ttlPolicy = new CacheTtlPolicy( + cache.getTtlseconds() * 1000L, + cache.getTtlCeilingFirstPartySeconds() * 1000L, + cache.getTtlCeilingThirdPartySeconds() * 1000L, + cache.getTtlCeilingDeviceSeconds() * 1000L, + cache.getNegativeTtlSeconds() * 1000L, + cache.getInProgressTtlSeconds() * 1000L); + + return new IdentityCache(properties.getCacheMaxSize(), ttlPolicy, identityStore, mapper, metrics); + } + + /** + * Polls Redis (L2) DBSIZE / evicted_keys into the {@code l2.size} / {@code l2.eviction} gauges. + * Only when the L2 store is the default Redis backend (a custom {@link IdentityStore} has no such + * stats). Returns {@code null} otherwise — Spring simply does not register the bean. + */ + @Bean + @ConditionalOnProperty(prefix = "hooks.modules." + IntentiqIdentityModule.CODE + ".cache", + name = "enabled", havingValue = "true") + RedisStatsReporter intentiqIdentityRedisStatsReporter(IdentityStore identityStore, + Vertx vertx, + IntentiqIdentityMetrics metrics) { + return identityStore instanceof RedisIdentityStore redisStore + ? new RedisStatsReporter(redisStore, vertx, metrics, REDIS_STATS_POLL_MS).start() + : null; + } + + @Bean + @ConditionalOnProperty(prefix = "hooks.modules." + IntentiqIdentityModule.CODE, + name = "metrics-enabled", havingValue = "true", matchIfMissing = true) + IntentiqIdentityMetrics intentiqIdentityMetrics(MetricRegistry metricRegistry) { + return new IntentiqIdentityMetrics(metricRegistry); + } + + @Bean + @ConditionalOnMissingBean(IntentiqIdentityMetrics.class) + IntentiqIdentityMetrics noopIntentiqIdentityMetrics() { + return new NoopIntentiqIdentityMetrics(); + } + + @Bean + IntentiqIdentityModule intentiqIdentityModule(ConfigResolver configResolver, + HttpClient httpClient, + JacksonMapper mapper, + IntentiqIdentityMetrics metrics, + IntentiqIdentityProperties properties, + @Autowired(required = false) IdentityCache identityCache) { + final FirstPartyKeyExtractor keyExtractor = + new FirstPartyKeyExtractor(properties.getCache().getMaxKeys()); + + return new IntentiqIdentityModule(List.of( + new IntentiqIdentityProcessedAuctionRequestHook( + configResolver, httpClient, mapper, identityCache, keyExtractor, metrics), + new IntentiqIdentityAuctionResponseHook(configResolver, httpClient, mapper, metrics))); + } + + private static RedisAPI createRedisApi(RedisProperties redis, Vertx vertx) { + if (redis == null || StringUtils.isBlank(redis.getHost())) { + throw new IllegalArgumentException("hooks.modules." + IntentiqIdentityModule.CODE + + ".redis.host is required when cache is enabled"); + } + if (redis.getPort() == null) { + throw new IllegalArgumentException("hooks.modules." + IntentiqIdentityModule.CODE + + ".redis.port is required when cache is enabled"); + } + + final String credentials = StringUtils.isNotBlank(redis.getPassword()) ? ":" + redis.getPassword() + "@" : ""; + final RedisOptions options = new RedisOptions() + .setConnectionString("redis://" + credentials + redis.getHost() + ":" + redis.getPort()); + + return RedisAPI.api(Redis.createClient(vertx, options)); + } +} diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/metric/IntentiqIdentityMetrics.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/metric/IntentiqIdentityMetrics.java new file mode 100644 index 00000000000..2ed890e73ff --- /dev/null +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/metric/IntentiqIdentityMetrics.java @@ -0,0 +1,183 @@ +package org.prebid.server.hooks.modules.intentiq.identity.metric; + +import com.codahale.metrics.Gauge; +import com.codahale.metrics.MetricRegistry; +import org.apache.commons.lang3.StringUtils; +import org.prebid.server.hooks.modules.intentiq.identity.v1.IntentiqIdentityModule; + +import java.util.Objects; +import java.util.concurrent.TimeUnit; +import java.util.function.LongSupplier; + +/** + * Module-specific counters for dashboards, namespaced under {@code modules.module..custom.*} + * (grouped with the framework's {@code modules.module..*} tree, see the README Metrics section). + * Call/success/failure/execution-time are already emitted by the hook framework and are not duplicated here. + * + *

Each metric is suffixed with the partner's {@code dpi} (the partner-facing data-provider id, never an + * internal backend id) as {@code _}, following the per-partner naming convention so the same + * Grafana per-partner templating applies. The suffix is omitted when no dpi is configured. + */ +public class IntentiqIdentityMetrics { + + private static final String PREFIX = "modules.module." + IntentiqIdentityModule.CODE + ".custom."; + + private final MetricRegistry metricRegistry; + + public IntentiqIdentityMetrics(MetricRegistry metricRegistry) { + this.metricRegistry = Objects.requireNonNull(metricRegistry); + } + + /** For the no-op subclass wired when {@code metrics-enabled} is false; never touches the registry. */ + protected IntentiqIdentityMetrics() { + this.metricRegistry = null; + } + + /** Positive entry served from cache; {@code layer} is {@code l1} (Caffeine) or {@code l2} (Redis). */ + public void cacheHit(String layer, String keyType, String dpi) { + inc("cache." + layer + ".hit." + keyType, dpi); + } + + /** Full miss — neither L1 nor L2 had the id; the API is called. Not layer-specific. */ + public void cacheMiss(String keyType, String dpi) { + inc("cache.miss." + keyType, dpi); + } + + public void cacheNegativeHit(String layer, String keyType, String dpi) { + inc("cache." + layer + ".negative.hit." + keyType, dpi); + } + + public void cacheInProgress(String layer, String keyType, String dpi) { + inc("cache." + layer + ".in_progress." + keyType, dpi); + } + + public void apiSuccess(String dpi) { + inc("api.success", dpi); + } + + public void apiError(String dpi) { + inc("api.error", dpi); + } + + public void enriched(String dpi) { + inc("enriched", dpi); + } + + /** The resolution produced no eids (no match); pairs with {@link #enriched} for a match rate. */ + public void eidsNone(String dpi) { + inc("eids.none", dpi); + } + + /** Resolution skipped before any API call because no api-endpoint is configured for the partner. */ + public void skipNoEndpoint(String dpi) { + inc("skip.no_endpoint", dpi); + } + + public void terminationCause(long tc, String dpi) { + if (tc >= 0 && tc < 200) { + inc("tc." + tc, dpi); + } + } + + /** Records the latency of a single identity-resolution API call, regardless of outcome. */ + public void apiLatency(long timeNanos, String dpi) { + time("api.latency", dpi, timeNanos); + } + + /** + * Wall-clock latency of the whole module flow within one auction: from the enrich hook + * (processed-auction-request) entry to the auction-response hook (bid release). Recorded once + * per auction the module participated in. + */ + public void flowLatency(long timeNanos, String dpi) { + time("flow.latency", dpi, timeNanos); + } + + public void impressionReported(String dpi) { + inc("impression.reported", dpi); + } + + public void impressionError(String dpi) { + inc("impression.error", dpi); + } + + // --- Backpressure / capacity (shared L1/L2 health) --------------------------------------------- + // Caffeine (L1) and Redis (L2) are process-wide singletons shared across all partners, so their + // saturation is not attributable to a single dpi. These are emitted WITHOUT the _ suffix + // (global), unlike the per-partner business counters above; this also keeps their cardinality at + // exactly one series each. See the README Metrics section. + + /** An L1 (Caffeine) read threw; treated as a miss for that key. Should be ~never. */ + public void l1GetError() { + inc("l1.get.error", null); + } + + /** An L1 (Caffeine) write threw; the entry did not land in L1. Should be ~never. */ + public void l1PutError() { + inc("l1.put.error", null); + } + + /** Latency of a single L2 (Redis) GET, recorded on every probe regardless of outcome. */ + public void l2GetLatency(long timeNanos) { + time("l2.get.latency", null, timeNanos); + } + + /** Latency of a single L2 (Redis) PUT, recorded on every write regardless of outcome. */ + public void l2PutLatency(long timeNanos) { + time("l2.put.latency", null, timeNanos); + } + + /** + * An L2 GET failed/timed out (connection refused, pool exhaustion, command timeout). The cache + * fails open — it falls through to a live API call — so without this counter the failure is + * invisible. Each fall-through-on-read is one increment. + */ + public void l2GetError() { + inc("l2.get.error", null); + } + + /** An L2 PUT failed/timed out; the entry still lives in L1 but did not reach the shared store. */ + public void l2PutError() { + inc("l2.put.error", null); + } + + /** + * Register the L1 (Caffeine) capacity gauges. Idempotent and global (no dpi). {@code size} + * tracks current entry count against the configured {@code cache-max-size}; {@code evictions} + * is Caffeine's cumulative eviction count (rate it in the dashboard). No-op when recording is + * disabled. ({@code load.failure} is intentionally not exposed — this cache uses manual + * {@code put} with no {@code CacheLoader}, so load failures cannot occur.) + */ + public void registerL1Gauges(LongSupplier size, LongSupplier evictions) { + gauge("l1.size", size); + gauge("l1.eviction", evictions); + } + + /** + * Register the L2 (Redis) capacity gauges, fed by a periodic poller (Redis stats can't be read + * synchronously). Global (no dpi). {@code size} is {@code DBSIZE}, {@code evictions} is the + * cumulative {@code evicted_keys} from {@code INFO stats} — both Redis-instance-wide, not + * module-scoped. No-op when recording is disabled. + */ + public void registerL2Gauges(LongSupplier size, LongSupplier evictions) { + gauge("l2.size", size); + gauge("l2.eviction", evictions); + } + + protected void inc(String name, String dpi) { + metricRegistry.counter(withDpi(name, dpi)).inc(); + } + + protected void time(String name, String dpi, long timeNanos) { + metricRegistry.timer(withDpi(name, dpi)).update(timeNanos, TimeUnit.NANOSECONDS); + } + + protected void gauge(String name, LongSupplier supplier) { + metricRegistry.gauge(withDpi(name, null), () -> (Gauge) supplier::getAsLong); + } + + private static String withDpi(String name, String dpi) { + final String full = PREFIX + name; + return StringUtils.isNotBlank(dpi) ? full + "_" + dpi : full; + } +} diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/metric/NoopIntentiqIdentityMetrics.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/metric/NoopIntentiqIdentityMetrics.java new file mode 100644 index 00000000000..f6e3a2e931a --- /dev/null +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/metric/NoopIntentiqIdentityMetrics.java @@ -0,0 +1,26 @@ +package org.prebid.server.hooks.modules.intentiq.identity.metric; + +import java.util.function.LongSupplier; + +/** + * Wired in place of {@link IntentiqIdentityMetrics} when {@code metrics-enabled} is false (see + * {@code IntentiqIdentityConfig}). Every recording path is a no-op, so callers need no flag check + * and the shared {@code MetricRegistry} is never touched. + */ +public class NoopIntentiqIdentityMetrics extends IntentiqIdentityMetrics { + + @Override + protected void inc(String name, String dpi) { + // no-op + } + + @Override + protected void time(String name, String dpi, long timeNanos) { + // no-op + } + + @Override + protected void gauge(String name, LongSupplier supplier) { + // no-op + } +} diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/model/IntentiqIdentityModuleContext.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/model/IntentiqIdentityModuleContext.java new file mode 100644 index 00000000000..2a50a46b277 --- /dev/null +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/model/IntentiqIdentityModuleContext.java @@ -0,0 +1,13 @@ +package org.prebid.server.hooks.modules.intentiq.identity.model; + +/** + * State carried from the processed-auction-request (enrich) hook to the auction-response + * (impression report) hook via {@code InvocationResult#moduleContext()} within one auction. + * + * @param startNanos {@link System#nanoTime()} captured at enrich-hook entry; lets the + * auction-response hook record the whole-flow latency (enrich -> bid release) + * @param abTestUuid IIQ A/B test id returned by the resolution response, echoed back on the report + * @param terminationCause IIQ termination cause ({@code tc}) from the resolution response, if any + */ +public record IntentiqIdentityModuleContext(long startNanos, String abTestUuid, Long terminationCause) { +} diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/model/config/CacheProperties.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/model/config/CacheProperties.java new file mode 100644 index 00000000000..a2241ffc241 --- /dev/null +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/model/config/CacheProperties.java @@ -0,0 +1,43 @@ +package org.prebid.server.hooks.modules.intentiq.identity.model.config; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +public class CacheProperties { + + private boolean enabled; + + /** Positive TTL used when the IntentIQ API omits {@code cttl}. */ + private int ttlseconds = 43_200; + + /** Max number of alias keys derived per request (guards against eid-stuffed requests). */ + @JsonProperty("max-keys") + private int maxKeys = 10; + + /** Upper bound on positive TTL for first-party id keys (pubcid, MAID, other eids). */ + @JsonProperty("ttl-ceiling-first-party-seconds") + private int ttlCeilingFirstPartySeconds = 86_400; + + /** Upper bound on positive TTL for third-party id keys (intentiq.com). */ + @JsonProperty("ttl-ceiling-third-party-seconds") + private int ttlCeilingThirdPartySeconds = 43_200; + + /** Upper bound on positive TTL for the probabilistic device-composite key. */ + @JsonProperty("ttl-ceiling-device-seconds") + private int ttlCeilingDeviceSeconds = 3_600; + + /** Short TTL for the negative (unresolvable id) sentinel. */ + @JsonProperty("negative-ttl-seconds") + private int negativeTtlSeconds = 120; + + /** + * TTL for the IN_PROGRESS marker written while a resolution call is in flight, after which the + * marker expires even if the call never completed. Defaults to 30 minutes to match the IntentIQ + * backend's in-progress window; lower it if you want a failed resolution to be retried sooner. + */ + @JsonProperty("in-progress-ttl-seconds") + private int inProgressTtlSeconds = 1800; +} diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/model/config/IntentiqIdentityProperties.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/model/config/IntentiqIdentityProperties.java new file mode 100644 index 00000000000..1fad945c41f --- /dev/null +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/model/config/IntentiqIdentityProperties.java @@ -0,0 +1,32 @@ +package org.prebid.server.hooks.modules.intentiq.identity.model.config; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +public final class IntentiqIdentityProperties { + + @JsonProperty("api-endpoint") + String apiEndpoint; + + @JsonProperty("reports-endpoint") + String reportsEndpoint; + + @JsonProperty("partner-id") + String partnerId; + + Long timeout; + + CacheProperties cache = new CacheProperties(); + + RedisProperties redis; + + @JsonProperty("cache-max-size") + long cacheMaxSize = 100_000L; + + // Module metrics are recorded by default; can opt out by setting this to false. + @JsonProperty("metrics-enabled") + boolean metricsEnabled = true; +} diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/model/config/RedisProperties.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/model/config/RedisProperties.java new file mode 100644 index 00000000000..6d2f678a01a --- /dev/null +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/model/config/RedisProperties.java @@ -0,0 +1,13 @@ +package org.prebid.server.hooks.modules.intentiq.identity.model.config; + +import lombok.Data; + +@Data +public final class RedisProperties { + + String host; + + Integer port; + + String password; +} diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityAuctionResponseHook.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityAuctionResponseHook.java new file mode 100644 index 00000000000..bdbc9154d1d --- /dev/null +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityAuctionResponseHook.java @@ -0,0 +1,250 @@ +package org.prebid.server.hooks.modules.intentiq.identity.v1; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.iab.openrtb.request.App; +import com.iab.openrtb.request.BidRequest; +import com.iab.openrtb.request.Device; +import com.iab.openrtb.request.Site; +import com.iab.openrtb.response.Bid; +import com.iab.openrtb.response.BidResponse; +import com.iab.openrtb.response.SeatBid; +import io.vertx.core.Future; +import org.apache.commons.lang3.StringUtils; +import org.prebid.server.auction.model.AuctionContext; +import org.prebid.server.hooks.execution.v1.InvocationResultImpl; +import org.prebid.server.hooks.modules.intentiq.identity.metric.IntentiqIdentityMetrics; +import org.prebid.server.hooks.modules.intentiq.identity.model.IntentiqIdentityModuleContext; +import org.prebid.server.hooks.modules.intentiq.identity.model.config.IntentiqIdentityProperties; +import org.prebid.server.hooks.modules.intentiq.identity.v1.core.ConfigResolver; +import org.prebid.server.hooks.modules.intentiq.identity.v1.core.IiqParam; +import org.prebid.server.hooks.v1.InvocationAction; +import org.prebid.server.hooks.v1.InvocationResult; +import org.prebid.server.hooks.v1.InvocationStatus; +import org.prebid.server.hooks.v1.auction.AuctionInvocationContext; +import org.prebid.server.hooks.v1.auction.AuctionResponseHook; +import org.prebid.server.hooks.v1.auction.AuctionResponsePayload; +import org.prebid.server.json.JacksonMapper; +import org.prebid.server.log.Logger; +import org.prebid.server.log.LoggerFactory; +import org.prebid.server.util.HttpUtil; +import org.prebid.server.vertx.httpclient.HttpClient; + +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; +import java.util.Objects; + +/** + * Runs at the {@code auction-response} stage and reports each winning bid to the IntentIQ S2S + * impression-reporting API (fire-and-forget GET to {@code reports-endpoint}). The {@code abTestUuid} + * produced by the resolution hook is read from the module context. The bid response is never modified. + */ +public class IntentiqIdentityAuctionResponseHook implements AuctionResponseHook { + + private static final Logger logger = LoggerFactory.getLogger(IntentiqIdentityAuctionResponseHook.class); + + public static final String CODE = "intentiq-identity-auction-response-hook"; + + private static final long DEFAULT_TIMEOUT_MS = 1000L; + private static final String BIDDING_PLATFORM_OPENRTB = "4"; + private static final String DEFAULT_CURRENCY = "USD"; + + // Identifies the request source to the IntentIQ S2S API as prebid-server-java. + private static final String SOURCE_PBJV = "pbjv"; + + private final ConfigResolver configResolver; + private final HttpClient httpClient; + private final JacksonMapper mapper; + private final IntentiqIdentityMetrics metrics; + + public IntentiqIdentityAuctionResponseHook(ConfigResolver configResolver, + HttpClient httpClient, + JacksonMapper mapper, + IntentiqIdentityMetrics metrics) { + this.configResolver = Objects.requireNonNull(configResolver); + this.httpClient = Objects.requireNonNull(httpClient); + this.mapper = Objects.requireNonNull(mapper); + this.metrics = Objects.requireNonNull(metrics); + } + + @Override + public Future> call(AuctionResponsePayload auctionResponsePayload, + AuctionInvocationContext invocationContext) { + final IntentiqIdentityProperties properties = configResolver.resolve(invocationContext.accountConfig()); + final IntentiqIdentityModuleContext context = + invocationContext.moduleContext() instanceof IntentiqIdentityModuleContext ctx ? ctx : null; + // Whole-flow latency: enrich hook entry (startNanos) -> here (bid release), recorded once + // per auction regardless of whether an impression report is configured/sent. + if (context != null) { + metrics.flowLatency(System.nanoTime() - context.startNanos(), properties.getPartnerId()); + } + + final BidResponse bidResponse = auctionResponsePayload.bidResponse(); + if (StringUtils.isBlank(properties.getReportsEndpoint()) || bidResponse == null) { + return Future.succeededFuture(noAction()); + } + + final BidRequest bidRequest = bidRequest(invocationContext); + final String abTestUuid = context != null ? context.abTestUuid() : null; + final Long terminationCause = context != null ? context.terminationCause() : null; + final String currency = bidResponse.getCur() != null ? bidResponse.getCur() : DEFAULT_CURRENCY; + + for (final SeatBid seatBid : nullSafe(bidResponse.getSeatbid())) { + for (final Bid bid : nullSafe(seatBid.getBid())) { + report(properties, bidRequest, seatBid.getSeat(), bid, currency, abTestUuid, terminationCause); + } + } + + return Future.succeededFuture(noAction()); + } + + private void report(IntentiqIdentityProperties properties, + BidRequest bidRequest, + String bidderCode, + Bid bid, + String currency, + String abTestUuid, + Long terminationCause) { + final Map rdata = new LinkedHashMap<>(); + put(rdata, RdataField.BIDDER_CODE, bidderCode); + put(rdata, RdataField.PARTNER_ID, properties.getPartnerId()); + put(rdata, RdataField.CPM, bid.getPrice()); + put(rdata, RdataField.CURRENCY, currency); + appendOriginalBid(rdata, bid); + put(rdata, RdataField.PLACEMENT_ID, bid.getImpid()); + put(rdata, RdataField.BIDDING_PLATFORM_ID, BIDDING_PLATFORM_OPENRTB); + putIfPresent(rdata, RdataField.VRREF, resolveRef(bidRequest)); + final String auctionId = bidRequest != null ? bidRequest.getId() : null; + putIfPresent(rdata, RdataField.PREBID_AUCTION_ID, auctionId); + putIfPresent(rdata, RdataField.PARTNER_AUCTION_ID, auctionId); + putIfPresent(rdata, RdataField.AB_TEST_UUID, abTestUuid); + if (terminationCause != null) { + put(rdata, RdataField.TERMINATION_CAUSE, terminationCause); + } + final Device device = bidRequest != null ? bidRequest.getDevice() : null; + if (device != null) { + putIfPresent(rdata, RdataField.IP, + StringUtils.isNotBlank(device.getIp()) ? device.getIp() : device.getIpv6()); + putIfPresent(rdata, RdataField.UA, device.getUa()); + } + + final String dpi = properties.getPartnerId(); + + final long timeout = properties.getTimeout() != null ? properties.getTimeout() : DEFAULT_TIMEOUT_MS; + httpClient.get(reportUrl(properties, rdata), HttpUtil.headers(), timeout) + .onSuccess(response -> metrics.impressionReported(dpi)) + .onFailure(throwable -> { + metrics.impressionError(dpi); + logger.warn("IntentIQ impression report failed", throwable); + }); + } + + private String reportUrl(IntentiqIdentityProperties properties, Map rdata) { + final String endpoint = properties.getReportsEndpoint(); + return endpoint + + (endpoint.contains("?") ? "&" : "?") + + IiqParam.AT.key() + "=45" + + "&" + IiqParam.RTYPE.key() + "=1" + + "&" + IiqParam.SOURCE.key() + "=" + SOURCE_PBJV + + "&" + IiqParam.DPI.key() + "=" + encodeComponent(StringUtils.defaultString(properties.getPartnerId())) + + "&" + IiqParam.RDATA.key() + "=" + encodeComponent(mapper.encodeToString(rdata)); + } + + private static String resolveRef(BidRequest bidRequest) { + if (bidRequest == null) { + return null; + } + final Site site = bidRequest.getSite(); + if (site != null) { + return StringUtils.isNotBlank(site.getDomain()) ? site.getDomain() : site.getPage(); + } + final App app = bidRequest.getApp(); + if (app != null) { + return StringUtils.isNotBlank(app.getBundle()) ? app.getBundle() : app.getName(); + } + return null; + } + + private static BidRequest bidRequest(AuctionInvocationContext invocationContext) { + final AuctionContext auctionContext = invocationContext.auctionContext(); + return auctionContext != null ? auctionContext.getBidRequest() : null; + } + + private static void appendOriginalBid(Map rdata, Bid bid) { + final ObjectNode ext = bid.getExt(); + if (ext == null) { + return; + } + final JsonNode originalCpm = ext.get("origbidcpm"); + if (originalCpm != null && originalCpm.isNumber()) { + put(rdata, RdataField.ORIGINAL_CPM, originalCpm.decimalValue()); + } + final JsonNode originalCurrency = ext.get("origbidcur"); + if (originalCurrency != null && StringUtils.isNotBlank(originalCurrency.asText())) { + put(rdata, RdataField.ORIGINAL_CURRENCY, originalCurrency.asText()); + } + } + + private static void put(Map map, RdataField field, Object value) { + map.put(field.key(), value); + } + + private static void putIfPresent(Map map, RdataField field, String value) { + if (StringUtils.isNotBlank(value)) { + map.put(field.key(), value); + } + } + + private static List nullSafe(List list) { + return list != null ? list : List.of(); + } + + private static String encodeComponent(String value) { + return URLEncoder.encode(value, StandardCharsets.UTF_8).replace("+", "%20"); + } + + private static InvocationResult noAction() { + return InvocationResultImpl.builder() + .status(InvocationStatus.success) + .action(InvocationAction.no_action) + .build(); + } + + @Override + public String code() { + return CODE; + } + + /** Field names of the impression-report {@code rdata} JSON object sent to the IntentIQ S2S API. */ + private enum RdataField { + + BIDDER_CODE("bidderCode"), + PARTNER_ID("partnerId"), + CPM("cpm"), + CURRENCY("currency"), + ORIGINAL_CPM("originalCpm"), + ORIGINAL_CURRENCY("originalCurrency"), + PLACEMENT_ID("placementId"), + BIDDING_PLATFORM_ID("biddingPlatformId"), + VRREF("vrref"), + PREBID_AUCTION_ID("prebidAuctionId"), + PARTNER_AUCTION_ID("partnerAuctionId"), + AB_TEST_UUID("abTestUuid"), + TERMINATION_CAUSE("terminationCause"), + IP("ip"), + UA("ua"); + + private final String key; + + RdataField(String key) { + this.key = key; + } + + String key() { + return key; + } + } +} diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityModule.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityModule.java new file mode 100644 index 00000000000..ca2ba2ec5b4 --- /dev/null +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityModule.java @@ -0,0 +1,17 @@ +package org.prebid.server.hooks.modules.intentiq.identity.v1; + +import org.prebid.server.hooks.v1.Hook; +import org.prebid.server.hooks.v1.InvocationContext; +import org.prebid.server.hooks.v1.Module; + +import java.util.Collection; + +public record IntentiqIdentityModule( + Collection> hooks) implements Module { + public static final String CODE = "intentiq-identity"; + + @Override + public String code() { + return CODE; + } +} diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityProcessedAuctionRequestHook.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityProcessedAuctionRequestHook.java new file mode 100644 index 00000000000..a4cb1e6edd0 --- /dev/null +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityProcessedAuctionRequestHook.java @@ -0,0 +1,478 @@ +package org.prebid.server.hooks.modules.intentiq.identity.v1; + +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonDeserializer; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.iab.openrtb.request.App; +import com.iab.openrtb.request.BidRequest; +import com.iab.openrtb.request.BrandVersion; +import com.iab.openrtb.request.Device; +import com.iab.openrtb.request.Eid; +import com.iab.openrtb.request.Regs; +import com.iab.openrtb.request.Site; +import com.iab.openrtb.request.Uid; +import com.iab.openrtb.request.User; +import com.iab.openrtb.request.UserAgent; +import io.vertx.core.Future; +import io.vertx.core.MultiMap; +import lombok.Data; +import lombok.NoArgsConstructor; +import org.apache.commons.lang3.StringUtils; +import org.prebid.server.hooks.execution.v1.InvocationResultImpl; +import org.prebid.server.hooks.execution.v1.auction.AuctionRequestPayloadImpl; +import org.prebid.server.hooks.modules.intentiq.identity.cache.CacheKey; +import org.prebid.server.hooks.modules.intentiq.identity.cache.CacheResult; +import org.prebid.server.hooks.modules.intentiq.identity.cache.IdentityCache; +import org.prebid.server.hooks.modules.intentiq.identity.cache.KeyType; +import org.prebid.server.hooks.modules.intentiq.identity.metric.IntentiqIdentityMetrics; +import org.prebid.server.hooks.modules.intentiq.identity.model.IntentiqIdentityModuleContext; +import org.prebid.server.hooks.modules.intentiq.identity.model.config.IntentiqIdentityProperties; +import org.prebid.server.hooks.modules.intentiq.identity.v1.core.ConfigResolver; +import org.prebid.server.hooks.modules.intentiq.identity.v1.core.FirstPartyKeyExtractor; +import org.prebid.server.hooks.modules.intentiq.identity.v1.core.IiqParam; +import org.prebid.server.hooks.v1.InvocationAction; +import org.prebid.server.hooks.v1.InvocationResult; +import org.prebid.server.hooks.v1.InvocationStatus; +import org.prebid.server.hooks.v1.PayloadUpdate; +import org.prebid.server.hooks.v1.auction.AuctionInvocationContext; +import org.prebid.server.hooks.v1.auction.AuctionRequestPayload; +import org.prebid.server.hooks.v1.auction.ProcessedAuctionRequestHook; +import org.prebid.server.json.JacksonMapper; +import org.prebid.server.log.Logger; +import org.prebid.server.log.LoggerFactory; +import org.prebid.server.util.HttpUtil; +import org.prebid.server.vertx.httpclient.HttpClient; + +import java.io.IOException; +import java.net.URLEncoder; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Objects; +import java.util.TreeMap; +import java.util.stream.Collectors; + +public class IntentiqIdentityProcessedAuctionRequestHook implements ProcessedAuctionRequestHook { + + private static final Logger logger = + LoggerFactory.getLogger(IntentiqIdentityProcessedAuctionRequestHook.class); + + public static final String CODE = "intentiq-identity-processed-auction-request-hook"; + + private static final String IIQ_SOURCE = "intentiq.com"; + + // Identifies the request source to the IntentIQ S2S API as prebid-server-java. + private static final String SOURCE_PBJV = "pbjv"; + + // Per the GDPR S2S guide, the TCF consent string is passed via the `gdpr-consent` request header, + // not as a query parameter. + private static final String GDPR_CONSENT_HEADER = "gdpr-consent"; + + private static final long DEFAULT_TIMEOUT_MS = 1000L; + + private final ConfigResolver configResolver; + private final HttpClient httpClient; + private final JacksonMapper mapper; + private final IdentityCache cache; + private final FirstPartyKeyExtractor keyExtractor; + private final IntentiqIdentityMetrics metrics; + + public IntentiqIdentityProcessedAuctionRequestHook(ConfigResolver configResolver, + HttpClient httpClient, + JacksonMapper mapper, + IdentityCache cache, + FirstPartyKeyExtractor keyExtractor, + IntentiqIdentityMetrics metrics) { + this.configResolver = Objects.requireNonNull(configResolver); + this.httpClient = Objects.requireNonNull(httpClient); + this.mapper = Objects.requireNonNull(mapper); + this.cache = cache; + this.keyExtractor = Objects.requireNonNull(keyExtractor); + this.metrics = Objects.requireNonNull(metrics); + } + + @Override + public Future> call(AuctionRequestPayload auctionRequestPayload, + AuctionInvocationContext invocationContext) { + final long startNanos = System.nanoTime(); + final IntentiqIdentityProperties properties = configResolver.resolve(invocationContext.accountConfig()); + final String dpi = properties.getPartnerId(); + if (StringUtils.isBlank(properties.getApiEndpoint())) { + metrics.skipNoEndpoint(dpi); + return Future.succeededFuture(noAction(new IntentiqIdentityModuleContext(startNanos, null, null))); + } + + return resolveEids(properties, auctionRequestPayload.bidRequest()) + .map(resolution -> { + final List eids = resolution.eids(); + final IntentiqIdentityModuleContext context = new IntentiqIdentityModuleContext( + startNanos, resolution.abTestUuid(), resolution.terminationCause()); + if (eids == null || eids.isEmpty()) { + metrics.eidsNone(dpi); + return noAction(context); + } + metrics.enriched(dpi); + return update(payload -> enrichUserEids(payload, eids), context); + }) + .otherwise(throwable -> { + logger.warn("IntentIQ identity resolution failed, proceeding without enrichment", throwable); + metrics.apiError(dpi); + return noAction(new IntentiqIdentityModuleContext(startNanos, null, null)); + }); + } + + private Future resolveEids(IntentiqIdentityProperties properties, BidRequest bidRequest) { + final boolean cacheEnabled = cache != null && properties.getCache().isEnabled(); + final List keys = cacheEnabled ? keyExtractor.candidateKeys(bidRequest) : List.of(); + if (keys.isEmpty()) { + return fetch(properties, bidRequest) + .map(response -> new Resolution( + extractEids(response), response.getAbTestUuid(), response.getTc())); + } + + final String dpi = properties.getPartnerId(); + // Cache counters are broken down by key type: the type of the key that actually matched for + // HIT/NEGATIVE/IN_PROGRESS, and the request's primary (highest-priority) candidate type for a + // full MISS, where no key matched. + final String primaryType = keyTypeToken(keys.getFirst().type()); + return cache.get(keys).compose(result -> switch (result.state()) { + case HIT -> { + metrics.cacheHit(layerToken(result.layer()), keyTypeToken(result.keyType()), dpi); + yield Future.succeededFuture(new Resolution(result.eids(), null, null)); + } + case NEGATIVE -> { + // A negative sentinel is a cached miss (id is known-unresolvable): no identity to + // serve, so it counts toward cache.miss, not cache.hit. The negative-specific counter + // distinguishes it from a true miss (no API call is made here) and is tagged by layer. + final String type = keyTypeToken(result.keyType()); + metrics.cacheMiss(type, dpi); + metrics.cacheNegativeHit(layerToken(result.layer()), type, dpi); + yield Future.succeededFuture(new Resolution(null, null, null)); + } + case IN_PROGRESS -> { + // A resolution call for this id is already in flight; skip firing a duplicate and + // proceed without enrichment (the in-flight call will populate the cache). + metrics.cacheInProgress(layerToken(result.layer()), keyTypeToken(result.keyType()), dpi); + yield Future.succeededFuture(new Resolution(null, null, null)); + } + case MISS -> { + metrics.cacheMiss(primaryType, dpi); + cache.putInProgress(keys); + yield fetchAndCache(properties, bidRequest, keys); + } + }); + } + + // Short, stable, lowercase metric token for a cache key type (e.g. THIRD_PARTY -> "third_party"). + private static String keyTypeToken(KeyType type) { + return type != null ? type.name().toLowerCase(Locale.ROOT) : "unknown"; + } + + // Cache layer that served the outcome: "l1" (Caffeine) or "l2" (Redis). + private static String layerToken(CacheResult.Layer layer) { + return layer != null ? layer.name().toLowerCase(Locale.ROOT) : "unknown"; + } + + private Future fetchAndCache(IntentiqIdentityProperties properties, + BidRequest bidRequest, + List keys) { + return fetch(properties, bidRequest).map(response -> { + final List eids = extractEids(response); + if (eids != null && !eids.isEmpty()) { + cache.put(keys, eids, response.getCttl() != null ? response.getCttl() : 0L); + } else { + cache.putNegative(keys, response.getCttl() != null ? response.getCttl() : 0L); + } + return new Resolution(eids, response.getAbTestUuid(), response.getTc()); + }); + } + + private Future fetch(IntentiqIdentityProperties properties, BidRequest bidRequest) { + final long timeout = properties.getTimeout() != null ? properties.getTimeout() : DEFAULT_TIMEOUT_MS; + final long startNanos = System.nanoTime(); + return httpClient.get(resolveUrl(properties, bidRequest), resolveHeaders(bidRequest), timeout) + .map(response -> { + final IiqResponse parsed = mapper.decodeValue(response.getBody(), IiqResponse.class); + metrics.apiSuccess(properties.getPartnerId()); + if (parsed != null && parsed.getTc() != null) { + metrics.terminationCause(parsed.getTc(), properties.getPartnerId()); + } + return parsed; + }) + .onComplete(ignored -> metrics.apiLatency(System.nanoTime() - startNanos, properties.getPartnerId())); + } + + private static List extractEids(IiqResponse response) { + return response != null && response.getData() != null ? response.getData().getEids() : null; + } + + private String resolveUrl(IntentiqIdentityProperties properties, BidRequest bidRequest) { + final String apiEndpoint = properties.getApiEndpoint(); + final StringBuilder url = new StringBuilder(apiEndpoint); + url.append(apiEndpoint.contains("?") ? '&' : '?').append(IiqParam.AT.key()).append("=39"); + appendIfPresent(url, IiqParam.MI, "10"); + appendIfPresent(url, IiqParam.DPI, properties.getPartnerId()); + appendIfPresent(url, IiqParam.PT, "17"); + appendIfPresent(url, IiqParam.DPN, "1"); + appendIfPresent(url, IiqParam.SRVR_REQ, "true"); + appendIfPresent(url, IiqParam.SOURCE, SOURCE_PBJV); + + final Device device = bidRequest.getDevice(); + if (device != null) { + appendIfPresent(url, IiqParam.IP, device.getIp()); + appendIfPresent(url, IiqParam.IPV6, device.getIpv6()); + appendIfPresent(url, IiqParam.UAS, device.getUa()); + appendIfPresent(url, IiqParam.UH, buildUaHints(device.getSua())); + appendDeviceId(url, device); + } + appendIfPresent(url, IiqParam.REF, resolveRef(bidRequest)); + appendIfPresent(url, IiqParam.IIQUID, resolveIiqUid(bidRequest.getUser())); + appendConsent(url, bidRequest); + + return url.toString(); + } + + private static void appendConsent(StringBuilder url, BidRequest bidRequest) { + final Regs regs = bidRequest.getRegs(); + if (regs != null) { + appendIfPresent(url, IiqParam.GDPR, resolveGdpr(regs)); + appendIfPresent(url, IiqParam.US_PRIVACY, resolveUsPrivacy(regs)); + appendIfPresent(url, IiqParam.GPP, regs.getGpp()); + // Forwarded ahead of backend support so the GPP section ids are available if the backend adds gpp_sid. + appendIfPresent(url, IiqParam.GPP_SID, StringUtils.join(regs.getGppSid(), ',')); + } + } + + private static MultiMap resolveHeaders(BidRequest bidRequest) { + final MultiMap headers = HttpUtil.headers(); + HttpUtil.addHeaderIfValueIsNotEmpty(headers, GDPR_CONSENT_HEADER, resolveConsent(bidRequest.getUser())); + return headers; + } + + private static String resolveGdpr(Regs regs) { + final Integer gdpr = regs.getGdpr() != null ? regs.getGdpr() + : regs.getExt() != null ? regs.getExt().getGdpr() : null; + return gdpr != null ? String.valueOf(gdpr) : null; + } + + private static String resolveUsPrivacy(Regs regs) { + if (StringUtils.isNotBlank(regs.getUsPrivacy())) { + return regs.getUsPrivacy(); + } + return regs.getExt() != null ? regs.getExt().getUsPrivacy() : null; + } + + private static String resolveConsent(User user) { + if (user == null) { + return null; + } + if (StringUtils.isNotBlank(user.getConsent())) { + return user.getConsent(); + } + return user.getExt() != null ? user.getExt().getConsent() : null; + } + + private static void appendIfPresent(StringBuilder url, IiqParam param, String value) { + if (StringUtils.isNotBlank(value)) { + url.append('&').append(param.key()).append('=').append(encodeComponent(value)); + } + } + + // Builds the `uh` UA client-hints JSON from OpenRTB device.sua, matching the IntentIQ backend's + // UA-CH hint format: numeric-keyed (0-8) UA-CH values, brands sorted, major vs full version. + private String buildUaHints(UserAgent sua) { + // The IntentIQ backend consumes hints only for high-entropy client hints (sua.source == 2). + if (sua == null || !Integer.valueOf(2).equals(sua.getSource())) { + return null; + } + final Map hints = new LinkedHashMap<>(); + appendBrowserHints(hints, sua.getBrowsers()); + if (sua.getMobile() != null) { + hints.put("1", "?" + sua.getMobile()); + } + appendPlatformHints(hints, sua.getPlatform()); + putQuoted(hints, "3", sua.getArchitecture()); + putQuoted(hints, "4", sua.getBitness()); + putQuoted(hints, "5", sua.getModel()); + return hints.isEmpty() ? null : mapper.encodeToString(hints); + } + + private static void appendBrowserHints(Map hints, List browsers) { + if (browsers == null) { + return; + } + // Sorted by brand to match the IntentIQ backend's hint formatting (deterministic hint string). + final TreeMap majorByBrand = new TreeMap<>(); + final TreeMap fullByBrand = new TreeMap<>(); + for (BrandVersion browser : browsers) { + if (browser == null || StringUtils.isBlank(browser.getBrand()) + || browser.getVersion() == null || browser.getVersion().isEmpty()) { + continue; + } + final String fullVersion = String.join(".", browser.getVersion()); + final int dot = fullVersion.indexOf('.'); + majorByBrand.put(browser.getBrand(), dot > 0 ? fullVersion.substring(0, dot) : fullVersion); + fullByBrand.put(browser.getBrand(), fullVersion); + } + putBrandList(hints, "0", majorByBrand); + putBrandList(hints, "8", fullByBrand); + } + + private static void putBrandList(Map hints, String key, TreeMap brandToVersion) { + if (brandToVersion.isEmpty()) { + return; + } + hints.put(key, brandToVersion.entrySet().stream() + .map(entry -> quote(entry.getKey()) + ";v=" + quote(entry.getValue())) + .collect(Collectors.joining(", "))); + } + + private static void appendPlatformHints(Map hints, BrandVersion platform) { + if (platform == null || StringUtils.isBlank(platform.getBrand())) { + return; + } + hints.put("2", quote(platform.getBrand())); + if (platform.getVersion() != null && !platform.getVersion().isEmpty()) { + hints.put("6", quote(String.join(".", platform.getVersion()))); + } + } + + private static void putQuoted(Map hints, String key, String value) { + if (StringUtils.isNotBlank(value)) { + hints.put(key, quote(value)); + } + } + + private static String quote(String value) { + return "\"" + value + "\""; + } + + private static void appendDeviceId(StringBuilder url, Device device) { + final String ifa = device.getIfa(); + if (StringUtils.isBlank(ifa) || Integer.valueOf(1).equals(device.getLmt())) { + return; + } + + final Integer deviceType = device.getDevicetype(); + final boolean ctv = deviceType != null && (deviceType == 3 || deviceType == 7); + // CTV ids (idtype 8) must be uppercase; MAID/AAID (idtype 4) is case-insensitive. + final String pcid = ctv ? ifa.toUpperCase(Locale.ROOT) : ifa; + url.append('&').append(IiqParam.PCID.key()).append('=').append(encodeComponent(pcid)) + .append('&').append(IiqParam.IDTYPE.key()).append('=').append(ctv ? 8 : 4); + } + + private static String resolveRef(BidRequest bidRequest) { + final Site site = bidRequest.getSite(); + if (site != null) { + return StringUtils.isNotBlank(site.getDomain()) ? site.getDomain() : site.getPage(); + } + final App app = bidRequest.getApp(); + if (app != null) { + return StringUtils.isNotBlank(app.getBundle()) ? app.getBundle() : app.getName(); + } + return null; + } + + private static String resolveIiqUid(User user) { + if (user == null || user.getEids() == null) { + return null; + } + return user.getEids().stream() + .filter(eid -> eid != null && IIQ_SOURCE.equals(eid.getSource())) + .map(Eid::getUids) + .filter(Objects::nonNull) + .flatMap(List::stream) + .filter(Objects::nonNull) + .map(Uid::getId) + .filter(StringUtils::isNotBlank) + .findFirst() + .orElse(null); + } + + private static String encodeComponent(String value) { + return URLEncoder.encode(value, StandardCharsets.UTF_8).replace("+", "%20"); + } + + private static AuctionRequestPayload enrichUserEids(AuctionRequestPayload payload, List resolvedEids) { + final BidRequest bidRequest = payload.bidRequest(); + final User existingUser = bidRequest.getUser() != null ? bidRequest.getUser() : User.builder().build(); + + final List mergedEids = new ArrayList<>(); + if (existingUser.getEids() != null) { + mergedEids.addAll(existingUser.getEids()); + } + mergedEids.addAll(resolvedEids); + + final User enrichedUser = existingUser.toBuilder().eids(mergedEids).build(); + return AuctionRequestPayloadImpl.of(bidRequest.toBuilder().user(enrichedUser).build()); + } + + private static InvocationResult update(PayloadUpdate payloadUpdate, + IntentiqIdentityModuleContext context) { + return InvocationResultImpl.builder() + .status(InvocationStatus.success) + .action(InvocationAction.update) + .payloadUpdate(payloadUpdate) + .moduleContext(context) + .build(); + } + + private static InvocationResult noAction(IntentiqIdentityModuleContext context) { + return InvocationResultImpl.builder() + .status(InvocationStatus.success) + .action(InvocationAction.no_action) + .moduleContext(context) + .build(); + } + + @Override + public String code() { + return CODE; + } + + private record Resolution(List eids, String abTestUuid, Long terminationCause) { + } + + @Data + @NoArgsConstructor + static class IiqResponse { + + @JsonDeserialize(using = LenientIiqDataDeserializer.class) + IiqData data; + + Long cttl; + + String abTestUuid; + + Long tc; + } + + @Data + @NoArgsConstructor + static class IiqData { + + List eids; + } + + /** + * IntentIQ returns {@code data} as an object on a hit but as an empty string ({@code ""}) on an + * empty or invalid response. Tolerate the non-object form by treating it as absent rather than + * failing the whole parse (which would mask a valid empty response as an API error). + */ + static class LenientIiqDataDeserializer extends JsonDeserializer { + + @Override + public IiqData deserialize(JsonParser parser, DeserializationContext context) throws IOException { + final JsonNode node = parser.readValueAsTree(); + if (node == null || !node.isObject()) { + return null; + } + return parser.getCodec().treeToValue(node, IiqData.class); + } + } +} diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/ConfigResolver.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/ConfigResolver.java new file mode 100644 index 00000000000..c3b4fa95214 --- /dev/null +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/ConfigResolver.java @@ -0,0 +1,50 @@ +package org.prebid.server.hooks.modules.intentiq.identity.v1.core; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import org.prebid.server.hooks.modules.intentiq.identity.model.config.IntentiqIdentityProperties; +import org.prebid.server.json.JsonMerger; + +import java.util.Objects; +import java.util.Optional; + +/** + * Resolves effective module properties for a request by merging the account-level config + * (from {@code AuctionInvocationContext.accountConfig()}) over the host-level (global) properties. + * Mirrors the optable-targeting module's resolver. + */ +public class ConfigResolver { + + private final ObjectMapper mapper; + private final JsonMerger jsonMerger; + private final IntentiqIdentityProperties globalProperties; + private final JsonNode globalPropertiesNode; + + public ConfigResolver(ObjectMapper mapper, JsonMerger jsonMerger, IntentiqIdentityProperties globalProperties) { + this.mapper = Objects.requireNonNull(mapper); + this.jsonMerger = Objects.requireNonNull(jsonMerger); + this.globalProperties = Objects.requireNonNull(globalProperties); + this.globalPropertiesNode = Objects.requireNonNull(mapper.valueToTree(globalProperties)); + } + + public IntentiqIdentityProperties resolve(ObjectNode accountConfig) { + // With host-level-only config (no account override) accountConfig is null; JsonMergePatch + // rejects a null patch ("input cannot be null"), so short-circuit to the global properties. + if (accountConfig == null || accountConfig.isEmpty()) { + return globalProperties; + } + final JsonNode merged = jsonMerger.merge(accountConfig, globalPropertiesNode); + return parse(merged).orElse(globalProperties); + } + + private Optional parse(JsonNode node) { + try { + return Optional.ofNullable(node) + .filter(it -> !it.isEmpty()) + .map(it -> mapper.convertValue(it, IntentiqIdentityProperties.class)); + } catch (IllegalArgumentException e) { + return Optional.empty(); + } + } +} diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/DeviceUserAgent.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/DeviceUserAgent.java new file mode 100644 index 00000000000..73c0ed7d61a --- /dev/null +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/DeviceUserAgent.java @@ -0,0 +1,58 @@ +package org.prebid.server.hooks.modules.intentiq.identity.v1.core; + +import org.apache.commons.lang3.StringUtils; +import ua_parser.Client; +import ua_parser.Parser; + +import java.util.Optional; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +/** + * Normalizes a raw User-Agent string into a compact, deterministic cache-key segment. + * + *

The IntentIQ caching guidance asks integrators to key on normalized UA fields + * (OS family/version, browser family/major, device family) rather than the full raw UA string, + * which would fragment the cache. The resulting segment looks like {@code iOS17_MobileSafari17_iPhone}. + * + *

Parsing is deterministic, so the same device always yields the same segment — which is all the + * module's self-contained cache requires. (Rendering engine and app-vs-browser, shown in some guide + * samples, are not exposed by this parser and are intentionally omitted.) + */ +public final class DeviceUserAgent { + + private static final Parser UA_PARSER = new Parser(); + private static final String UNKNOWN = "Other"; + + private DeviceUserAgent() { + } + + /** + * Returns the normalized UA segment, or an empty string when {@code ua} is blank or yields no + * recognizable fields. + */ + public static String normalize(String ua) { + if (StringUtils.isBlank(ua)) { + return StringUtils.EMPTY; + } + + final Client client = UA_PARSER.parse(ua); + final String os = client.os != null ? token(client.os.family, client.os.major) : null; + final String browser = client.userAgent != null + ? token(client.userAgent.family, client.userAgent.major) + : null; + final String device = client.device != null ? token(client.device.family, null) : null; + + return Stream.of(os, browser, device) + .filter(StringUtils::isNotBlank) + .collect(Collectors.joining("_")); + } + + private static String token(String family, String version) { + if (StringUtils.isBlank(family) || UNKNOWN.equals(family)) { + return null; + } + final String value = family + StringUtils.defaultString(version); + return Optional.of(value.replaceAll("\\s+", "")).filter(StringUtils::isNotBlank).orElse(null); + } +} diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/FirstPartyKeyExtractor.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/FirstPartyKeyExtractor.java new file mode 100644 index 00000000000..412ee620069 --- /dev/null +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/FirstPartyKeyExtractor.java @@ -0,0 +1,130 @@ +package org.prebid.server.hooks.modules.intentiq.identity.v1.core; + +import com.iab.openrtb.request.BidRequest; +import com.iab.openrtb.request.Device; +import com.iab.openrtb.request.Eid; +import com.iab.openrtb.request.Uid; +import com.iab.openrtb.request.User; +import org.apache.commons.lang3.StringUtils; +import org.prebid.server.hooks.modules.intentiq.identity.cache.CacheKey; +import org.prebid.server.hooks.modules.intentiq.identity.cache.KeyType; + +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Locale; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +/** + * Derives the ordered list of alias cache keys for a bid request. Each relevant first-party id present + * becomes one namespaced key; the resolved identity is aliased across all of them so a later request + * carrying any of those ids hits the cache. + * + *

Priority (highest first): IIQ eid, shared id / pubcid, device MAID, any other eid source, then a + * probabilistic device composite as last resort. Keys are lower-cased, de-duplicated (first occurrence + * wins) and capped at {@code maxKeys}. {@code device.lmt == 1} suppresses the MAID key; CTV ifas are + * upper-cased to match the resolution request's {@code idtype 8} handling. + */ +public class FirstPartyKeyExtractor { + + private static final String IIQ_SOURCE = "intentiq.com"; + private static final Set SHARED_SOURCES = Set.of("pubcid.org", "sharedid.org"); + + private final int maxKeys; + + public FirstPartyKeyExtractor(int maxKeys) { + this.maxKeys = maxKeys; + } + + public List candidateKeys(BidRequest bidRequest) { + final List keys = new ArrayList<>(); + final User user = bidRequest.getUser(); + final List eids = user != null ? user.getEids() : null; + final Device device = bidRequest.getDevice(); + + addEidKeys(keys, eids, "iiq", KeyType.THIRD_PARTY, IIQ_SOURCE::equals); + addEidKeys(keys, eids, "pubcid", KeyType.FIRST_PARTY, SHARED_SOURCES::contains); + addMaidKey(keys, device); + addOtherEidKeys(keys, eids); + addDeviceComposite(keys, device); + + return dedupAndCap(keys); + } + + private static void addEidKeys(List keys, + List eids, + String namespace, + KeyType type, + java.util.function.Predicate sourceMatch) { + if (eids == null) { + return; + } + eids.stream() + .filter(eid -> eid != null && eid.getSource() != null && sourceMatch.test(eid.getSource())) + .map(Eid::getUids) + .filter(Objects::nonNull) + .flatMap(List::stream) + .filter(Objects::nonNull) + .map(Uid::getId) + .filter(StringUtils::isNotBlank) + .forEach(id -> keys.add(new CacheKey(namespace + ":" + id, type))); + } + + private static void addMaidKey(List keys, Device device) { + if (device == null || StringUtils.isBlank(device.getIfa()) || Integer.valueOf(1).equals(device.getLmt())) { + return; + } + final Integer deviceType = device.getDevicetype(); + final boolean ctv = deviceType != null && (deviceType == 3 || deviceType == 7); + final String ifa = ctv ? device.getIfa().toUpperCase(Locale.ROOT) : device.getIfa(); + keys.add(new CacheKey("maid:" + ifa, KeyType.FIRST_PARTY)); + } + + private static void addOtherEidKeys(List keys, List eids) { + if (eids == null) { + return; + } + for (Eid eid : eids) { + if (eid == null || eid.getSource() == null || eid.getUids() == null) { + continue; + } + final String source = eid.getSource(); + if (IIQ_SOURCE.equals(source) || SHARED_SOURCES.contains(source)) { + continue; + } + final String namespace = source.toLowerCase(Locale.ROOT); + for (Uid uid : eid.getUids()) { + if (uid != null && StringUtils.isNotBlank(uid.getId())) { + keys.add(new CacheKey(namespace + ":" + uid.getId(), KeyType.FIRST_PARTY)); + } + } + } + } + + private static void addDeviceComposite(List keys, Device device) { + if (device == null) { + return; + } + final String ip = StringUtils.isNotBlank(device.getIp()) ? device.getIp() : device.getIpv6(); + // Normalized UA fields (not the raw UA string) to avoid fragmenting the cache per request. + final String ua = DeviceUserAgent.normalize(device.getUa()); + final String composite = Stream.of(device.getIfa(), StringUtils.trimToNull(ua), ip) + .filter(StringUtils::isNotBlank) + .collect(Collectors.joining("_")); + if (!composite.isEmpty()) { + keys.add(new CacheKey("dev:" + composite, KeyType.DEVICE)); + } + } + + private List dedupAndCap(List keys) { + final Map unique = new LinkedHashMap<>(); + for (CacheKey key : keys) { + unique.putIfAbsent(key.key(), key); + } + return unique.values().stream().limit(maxKeys).collect(Collectors.toList()); + } +} diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/IiqParam.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/IiqParam.java new file mode 100644 index 00000000000..1315368f985 --- /dev/null +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/IiqParam.java @@ -0,0 +1,45 @@ +package org.prebid.server.hooks.modules.intentiq.identity.v1.core; + +/** + * Query-parameter names of the IntentIQ S2S HTTP API, shared by both the identity-resolution + * request and the impression-report request. The enum constant carries the on-the-wire key; + * the value is request-specific and supplied at the call site (e.g. {@code at} is {@code 39} for the + * resolution request and {@code 45} for the impression report). + */ +public enum IiqParam { + + // Common / identity-resolution request + AT("at"), + MI("mi"), + DPI("dpi"), + PT("pt"), + DPN("dpn"), + SRVR_REQ("srvrReq"), + SOURCE("source"), + IP("ip"), + IPV6("ipv6"), + UAS("uas"), + UH("uh"), + PCID("pcid"), + IDTYPE("idtype"), + REF("ref"), + IIQUID("iiquid"), + GDPR("gdpr"), + US_PRIVACY("us_privacy"), + GPP("gpp"), + GPP_SID("gpp_sid"), + + // Impression-report request + RTYPE("rtype"), + RDATA("rdata"); + + private final String key; + + IiqParam(String key) { + this.key = key; + } + + public String key() { + return key; + } +} diff --git a/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/cache/IdentityCacheTest.java b/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/cache/IdentityCacheTest.java new file mode 100644 index 00000000000..5774bbaf14c --- /dev/null +++ b/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/cache/IdentityCacheTest.java @@ -0,0 +1,447 @@ +package org.prebid.server.hooks.modules.intentiq.identity.cache; + +import com.codahale.metrics.MetricRegistry; +import com.iab.openrtb.request.Eid; +import com.iab.openrtb.request.Uid; +import io.vertx.core.Future; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.prebid.server.hooks.modules.intentiq.identity.metric.IntentiqIdentityMetrics; +import org.prebid.server.json.JacksonMapper; +import org.prebid.server.json.ObjectMapperProvider; + +import java.util.List; + +import static java.util.Collections.singletonList; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class IdentityCacheTest { + + private static final JacksonMapper MAPPER = new JacksonMapper(ObjectMapperProvider.mapper()); + + // defaultTtl 12h, firstParty ceiling 24h, thirdParty ceiling 12h, device ceiling 1h, negative 2min, + // in-progress 60s + private static final CacheTtlPolicy POLICY = + new CacheTtlPolicy(43_200_000L, 86_400_000L, 43_200_000L, 3_600_000L, 120_000L, 60_000L); + + private static final CacheKey IIQ = new CacheKey("iiq:uid-1", KeyType.THIRD_PARTY); + private static final CacheKey PUBCID = new CacheKey("pubcid:p1", KeyType.FIRST_PARTY); + private static final CacheKey DEV = new CacheKey("dev:ifa_ua_ip", KeyType.DEVICE); + + @Mock + private IdentityStore store; + + private IdentityCache target; + private MetricRegistry metricRegistry; + + private final List eids = singletonList(Eid.builder() + .source("intentiq.com") + .uids(singletonList(Uid.builder().id("uid-1").build())) + .build()); + + @BeforeEach + public void setUp() { + metricRegistry = new MetricRegistry(); + target = new IdentityCache(100L, POLICY, store, MAPPER, new IntentiqIdentityMetrics(metricRegistry)); + } + + @Test + public void getShouldReturnFromLocalCacheWithoutQueryingStoreAfterPut() { + // given + when(store.put(any(), any(), anyLong())).thenReturn(Future.succeededFuture()); + target.put(List.of(IIQ), eids, 1000L); + + // when + final CacheResult result = target.get(List.of(IIQ)).result(); + + // then + assertThat(result.state()).isEqualTo(CacheResult.State.HIT); + assertThat(result.eids()).isEqualTo(eids); + verify(store, never()).get(any()); + } + + @Test + public void putShouldRefreshExpiryWhenSameKeyWrittenAgain() { + // given + when(store.put(any(), any(), anyLong())).thenReturn(Future.succeededFuture()); + target.put(List.of(IIQ), eids, 1000L); + + // when — a second write to the same key updates the existing L1 entry (exercises expireAfterUpdate) + target.put(List.of(IIQ), eids, 5000L); + + // then + final CacheResult result = target.get(List.of(IIQ)).result(); + assertThat(result.state()).isEqualTo(CacheResult.State.HIT); + assertThat(result.eids()).isEqualTo(eids); + } + + @Test + public void putShouldWriteToStoreWithEffectiveTtlFromCttl() { + // given + when(store.put(any(), any(), anyLong())).thenReturn(Future.succeededFuture()); + + // when — cttl below every ceiling is used verbatim + target.put(List.of(IIQ), eids, 1000L); + + // then + verify(store).put(eq("iiq:uid-1"), any(), eq(1000L)); + } + + @Test + public void putShouldUseDefaultTtlWhenCttlIsNotPositive() { + // given + when(store.put(any(), any(), anyLong())).thenReturn(Future.succeededFuture()); + + // when — third-party ceiling (12h) equals default, so default applies + target.put(List.of(IIQ), eids, 0L); + + // then + verify(store).put(eq("iiq:uid-1"), any(), eq(43_200_000L)); + } + + @Test + public void putShouldCapCttlAtPerTypeCeiling() { + // given + when(store.put(any(), any(), anyLong())).thenReturn(Future.succeededFuture()); + + // when — huge cttl on a device key is capped at the 1h device ceiling + target.put(List.of(DEV), eids, 999_999_999L); + + // then + verify(store).put(eq("dev:ifa_ua_ip"), any(), eq(3_600_000L)); + } + + @Test + public void putShouldWriteEntryUnderEveryKey() { + // given + when(store.put(any(), any(), anyLong())).thenReturn(Future.succeededFuture()); + + // when + target.put(List.of(IIQ, PUBCID), eids, 1000L); + + // then + verify(store).put(eq("iiq:uid-1"), any(), eq(1000L)); + verify(store).put(eq("pubcid:p1"), any(), eq(1000L)); + } + + @Test + public void getShouldReturnMissOnFullMiss() { + // given + when(store.get(any())).thenReturn(Future.succeededFuture(null)); + + // when + final CacheResult result = target.get(List.of(IIQ)).result(); + + // then + assertThat(result.state()).isEqualTo(CacheResult.State.MISS); + verify(store).get("iiq:uid-1"); + } + + @Test + public void getShouldReturnMissForEmptyKeys() { + // when + final CacheResult result = target.get(List.of()).result(); + + // then + assertThat(result.state()).isEqualTo(CacheResult.State.MISS); + verify(store, never()).get(any()); + } + + @Test + public void getShouldFallOpenToMissWhenStoreFails() { + // given + when(store.get(any())).thenReturn(Future.failedFuture(new RuntimeException("down"))); + + // when + final Future future = target.get(List.of(IIQ)); + + // then + assertThat(future.succeeded()).isTrue(); + assertThat(future.result().state()).isEqualTo(CacheResult.State.MISS); + } + + @Test + public void getShouldReturnHitFromStoreWhenPresentAndNotExpired() { + // given + when(store.get("iiq:uid-1")) + .thenReturn(Future.succeededFuture(positiveEntry(System.currentTimeMillis() + 60_000L))); + + // when + final CacheResult result = target.get(List.of(IIQ)).result(); + + // then + assertThat(result.state()).isEqualTo(CacheResult.State.HIT); + assertThat(result.eids()).isEqualTo(eids); + } + + @Test + public void getShouldReturnMissWhenStoreEntryIsExpired() { + // given + when(store.get(any())).thenReturn(Future.succeededFuture(positiveEntry(System.currentTimeMillis() - 1L))); + + // when + final CacheResult result = target.get(List.of(IIQ)).result(); + + // then + assertThat(result.state()).isEqualTo(CacheResult.State.MISS); + } + + @Test + public void getShouldPromoteStoreHitToLocalCacheAndNotQueryStoreAgain() { + // given + when(store.get("iiq:uid-1")) + .thenReturn(Future.succeededFuture(positiveEntry(System.currentTimeMillis() + 60_000L))); + target.get(List.of(IIQ)).result(); + + // when — second lookup + final CacheResult result = target.get(List.of(IIQ)).result(); + + // then + assertThat(result.eids()).isEqualTo(eids); + verify(store).get("iiq:uid-1"); + } + + @Test + public void getShouldReturnHighestPriorityHitAndBackfillMissedKeys() { + // given — first (highest-priority) key misses, second hits + lenient().when(store.put(any(), any(), anyLong())).thenReturn(Future.succeededFuture()); + when(store.get("iiq:uid-1")).thenReturn(Future.succeededFuture(null)); + when(store.get("pubcid:p1")) + .thenReturn(Future.succeededFuture(positiveEntry(System.currentTimeMillis() + 60_000L))); + + // when + final CacheResult result = target.get(List.of(IIQ, PUBCID)).result(); + + // then — the hit is returned and the missed higher-priority key is back-filled + assertThat(result.state()).isEqualTo(CacheResult.State.HIT); + assertThat(result.eids()).isEqualTo(eids); + verify(store).put(eq("iiq:uid-1"), any(), anyLong()); + } + + @Test + public void getShouldBackfillFromLocalHitUnderOtherKeys() { + // given — IIQ already in local cache, PUBCID not present anywhere + when(store.put(any(), any(), anyLong())).thenReturn(Future.succeededFuture()); + target.put(List.of(IIQ), eids, 60_000L); + + // when — lookup with both keys hits IIQ in L1 and back-fills PUBCID + final CacheResult result = target.get(List.of(IIQ, PUBCID)).result(); + + // then + assertThat(result.state()).isEqualTo(CacheResult.State.HIT); + verify(store).put(eq("pubcid:p1"), any(), anyLong()); + } + + @Test + public void putNegativeShouldYieldNegativeResultWithoutQueryingStore() { + // given + when(store.put(any(), any(), anyLong())).thenReturn(Future.succeededFuture()); + target.putNegative(List.of(IIQ), 0L); + + // when + final CacheResult result = target.get(List.of(IIQ)).result(); + + // then + assertThat(result.state()).isEqualTo(CacheResult.State.NEGATIVE); + verify(store, never()).get(any()); + } + + @Test + public void putNegativeShouldWriteSentinelUnderAllKeysWithDefaultTtlWhenCttlAbsent() { + // given + when(store.put(any(), any(), anyLong())).thenReturn(Future.succeededFuture()); + + // when + target.putNegative(List.of(IIQ, PUBCID), 0L); + + // then + verify(store).put(eq("iiq:uid-1"), any(), eq(120_000L)); + verify(store).put(eq("pubcid:p1"), any(), eq(120_000L)); + } + + @Test + public void putNegativeShouldHonorResponseCttlAsSuppressionTtl() { + // given — the BE signals the suppression window via cttl on an empty/invalid response + when(store.put(any(), any(), anyLong())).thenReturn(Future.succeededFuture()); + + // when + target.putNegative(List.of(IIQ), 600_000L); + + // then + verify(store).put(eq("iiq:uid-1"), any(), eq(600_000L)); + } + + @Test + public void putNegativeShouldCapResponseCttlAtFirstPartyCeiling() { + // given + when(store.put(any(), any(), anyLong())).thenReturn(Future.succeededFuture()); + + // when — an absurd cttl is bounded by the first-party ceiling (24h) + target.putNegative(List.of(IIQ), 999_999_999_999L); + + // then + verify(store).put(eq("iiq:uid-1"), any(), eq(86_400_000L)); + } + + @Test + public void getShouldReturnNegativeFromStoreSentinel() { + // given + when(store.get("iiq:uid-1")) + .thenReturn(Future.succeededFuture(negativeEntry(System.currentTimeMillis() + 60_000L))); + + // when + final CacheResult result = target.get(List.of(IIQ)).result(); + + // then + assertThat(result.state()).isEqualTo(CacheResult.State.NEGATIVE); + } + + @Test + public void putShouldStoreLocallyEvenWhenStorePutFails() { + // given + when(store.put(any(), any(), anyLong())).thenReturn(Future.failedFuture(new RuntimeException("down"))); + + // when + target.put(List.of(IIQ), eids, 1000L); + + // then — local layer still serves the value without touching the store + assertThat(target.get(List.of(IIQ)).result().eids()).isEqualTo(eids); + verify(store, never()).get(any()); + } + + @Test + public void putInProgressShouldWriteSentinelUnderAllKeysWithInProgressTtl() { + // given + when(store.put(any(), any(), anyLong())).thenReturn(Future.succeededFuture()); + + // when + target.putInProgress(List.of(IIQ, PUBCID)); + + // then — marker written under every key with the in-progress TTL (60s) + verify(store).put(eq("iiq:uid-1"), any(), eq(60_000L)); + verify(store).put(eq("pubcid:p1"), any(), eq(60_000L)); + } + + @Test + public void getShouldReturnInProgressAfterPutInProgress() { + // given + when(store.put(any(), any(), anyLong())).thenReturn(Future.succeededFuture()); + target.putInProgress(List.of(IIQ)); + + // when + final CacheResult result = target.get(List.of(IIQ)).result(); + + // then — served from L1 without touching the store + assertThat(result.state()).isEqualTo(CacheResult.State.IN_PROGRESS); + assertThat(result.eids()).isEmpty(); + verify(store, never()).get(any()); + } + + @Test + public void getShouldReturnInProgressFromStoreSentinel() { + // given + when(store.get("iiq:uid-1")) + .thenReturn(Future.succeededFuture(inProgressEntry(System.currentTimeMillis() + 60_000L))); + + // when + final CacheResult result = target.get(List.of(IIQ)).result(); + + // then + assertThat(result.state()).isEqualTo(CacheResult.State.IN_PROGRESS); + } + + @Test + public void getShouldPreferResolvedHitOverInProgressMarker() { + // given — highest-priority key is in progress, a lower-priority key already resolved + lenient().when(store.put(any(), any(), anyLong())).thenReturn(Future.succeededFuture()); + when(store.get("iiq:uid-1")) + .thenReturn(Future.succeededFuture(inProgressEntry(System.currentTimeMillis() + 60_000L))); + when(store.get("pubcid:p1")) + .thenReturn(Future.succeededFuture(positiveEntry(System.currentTimeMillis() + 60_000L))); + + // when + final CacheResult result = target.get(List.of(IIQ, PUBCID)).result(); + + // then — the resolved entry wins over the in-progress marker + assertThat(result.state()).isEqualTo(CacheResult.State.HIT); + assertThat(result.eids()).isEqualTo(eids); + } + + @Test + public void getShouldRecordStoreGetErrorAndLatencyWhenL2GetFails() { + // given + when(store.get(any())).thenReturn(Future.failedFuture(new RuntimeException("down"))); + + // when + target.get(List.of(IIQ)).result(); + + // then — the otherwise-swallowed L2 failure is counted, and latency is recorded regardless + assertThat(counter("l2.get.error")).isEqualTo(1); + assertThat(timerCount("l2.get.latency")).isEqualTo(1); + } + + @Test + public void putShouldRecordStorePutErrorAndLatencyWhenL2PutFails() { + // given + when(store.put(any(), any(), anyLong())).thenReturn(Future.failedFuture(new RuntimeException("down"))); + + // when + target.put(List.of(IIQ), eids, 1000L); + + // then + assertThat(counter("l2.put.error")).isEqualTo(1); + assertThat(timerCount("l2.put.latency")).isEqualTo(1); + } + + @Test + public void shouldExposeL1SizeGaugeReflectingCachedEntries() { + // given + when(store.put(any(), any(), anyLong())).thenReturn(Future.succeededFuture()); + + // when + target.put(List.of(IIQ), eids, 60_000L); + + // then — the L1 size gauge is registered and reflects the written entry + assertThat(gauge("l1.size")).isEqualTo(1L); + assertThat(metricRegistry.getGauges()).containsKey(metricName("l1.eviction")); + } + + private long counter(String name) { + return metricRegistry.counter(metricName(name)).getCount(); + } + + private long timerCount(String name) { + return metricRegistry.timer(metricName(name)).getCount(); + } + + private Object gauge(String name) { + return metricRegistry.getGauges().get(metricName(name)).getValue(); + } + + private static String metricName(String name) { + return "modules.module.intentiq-identity.custom." + name; + } + + private String positiveEntry(long exp) { + return MAPPER.encodeToString(IdentityCache.CacheEntry.of(eids, false, false, exp)); + } + + private String negativeEntry(long exp) { + return MAPPER.encodeToString(IdentityCache.CacheEntry.of(List.of(), true, false, exp)); + } + + private String inProgressEntry(long exp) { + return MAPPER.encodeToString(IdentityCache.CacheEntry.of(List.of(), false, true, exp)); + } +} diff --git a/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/cache/RedisIdentityStoreTest.java b/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/cache/RedisIdentityStoreTest.java new file mode 100644 index 00000000000..0f65d37afae --- /dev/null +++ b/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/cache/RedisIdentityStoreTest.java @@ -0,0 +1,128 @@ +package org.prebid.server.hooks.modules.intentiq.identity.cache; + +import io.vertx.core.Future; +import io.vertx.redis.client.RedisAPI; +import io.vertx.redis.client.Response; +import io.vertx.redis.client.impl.types.SimpleStringType; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class RedisIdentityStoreTest { + + @Mock + private RedisAPI redis; + + @Captor + private ArgumentCaptor> setArgsCaptor; + + private RedisIdentityStore target; + + @BeforeEach + public void setUp() { + target = new RedisIdentityStore(redis); + } + + @Test + public void putShouldIssueSetWithPxTtl() { + // given + when(redis.set(any())).thenReturn(Future.succeededFuture()); + + // when + target.put("key", "value", 1000L); + + // then + verify(redis).set(setArgsCaptor.capture()); + assertThat(setArgsCaptor.getValue()).containsExactly("key", "value", "PX", "1000"); + } + + @Test + public void getShouldReturnResponseAsString() { + // given + when(redis.get("key")).thenReturn(Future.succeededFuture(SimpleStringType.create("value"))); + + // when and then + assertThat(target.get("key").result()).isEqualTo("value"); + } + + @Test + public void getShouldReturnNullWhenKeyAbsent() { + // given + when(redis.get("key")).thenReturn(Future.succeededFuture(null)); + + // when and then + assertThat(target.get("key").result()).isNull(); + } + + @Test + public void dbSizeShouldReturnResponseAsLong() { + // given + final Response response = mock(Response.class); + when(response.toLong()).thenReturn(42L); + when(redis.dbsize()).thenReturn(Future.succeededFuture(response)); + + // when and then + assertThat(target.dbSize().result()).isEqualTo(42L); + } + + @Test + public void dbSizeShouldReturnZeroWhenResponseNull() { + // given + when(redis.dbsize()).thenReturn(Future.succeededFuture(null)); + + // when and then + assertThat(target.dbSize().result()).isZero(); + } + + @Test + public void evictedKeysShouldParseValueFromInfoStats() { + // given + when(redis.info(List.of("stats"))).thenReturn(Future.succeededFuture( + SimpleStringType.create("# Stats\r\nexpired_keys:5\r\nevicted_keys:17\r\nkeyspace_hits:1\r\n"))); + + // when and then + assertThat(target.evictedKeys().result()).isEqualTo(17L); + } + + @Test + public void evictedKeysShouldReturnZeroWhenResponseNull() { + // given + when(redis.info(List.of("stats"))).thenReturn(Future.succeededFuture(null)); + + // when and then + assertThat(target.evictedKeys().result()).isZero(); + } + + @Test + public void evictedKeysShouldReturnZeroWhenFieldMissing() { + // given + when(redis.info(List.of("stats"))).thenReturn(Future.succeededFuture( + SimpleStringType.create("# Stats\r\nexpired_keys:5\r\nkeyspace_hits:1\r\n"))); + + // when and then + assertThat(target.evictedKeys().result()).isZero(); + } + + @Test + public void evictedKeysShouldReturnZeroWhenValueNotANumber() { + // given + when(redis.info(List.of("stats"))).thenReturn(Future.succeededFuture( + SimpleStringType.create("evicted_keys:not-a-number\r\n"))); + + // when and then + assertThat(target.evictedKeys().result()).isZero(); + } +} diff --git a/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/cache/RedisStatsReporterTest.java b/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/cache/RedisStatsReporterTest.java new file mode 100644 index 00000000000..37e69ca5857 --- /dev/null +++ b/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/cache/RedisStatsReporterTest.java @@ -0,0 +1,133 @@ +package org.prebid.server.hooks.modules.intentiq.identity.cache; + +import io.vertx.core.Future; +import io.vertx.core.Handler; +import io.vertx.core.Vertx; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.prebid.server.hooks.modules.intentiq.identity.metric.IntentiqIdentityMetrics; + +import java.util.function.LongSupplier; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatNullPointerException; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.lenient; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class RedisStatsReporterTest { + + @Mock + private RedisIdentityStore store; + + @Mock + private Vertx vertx; + + @Mock + private IntentiqIdentityMetrics metrics; + + @Captor + private ArgumentCaptor sizeSupplierCaptor; + + @Captor + private ArgumentCaptor evictionsSupplierCaptor; + + @Captor + private ArgumentCaptor> periodicHandlerCaptor; + + @Test + public void constructorShouldThrowWhenStoreIsNull() { + // when and then + assertThatNullPointerException() + .isThrownBy(() -> new RedisStatsReporter(null, vertx, metrics, 1000L)); + } + + @Test + public void constructorShouldThrowWhenVertxIsNull() { + // when and then + assertThatNullPointerException() + .isThrownBy(() -> new RedisStatsReporter(store, null, metrics, 1000L)); + } + + @Test + public void constructorShouldRegisterGaugesThatReadTheLatestPolledValues() { + // given + when(store.dbSize()).thenReturn(Future.succeededFuture(7L)); + when(store.evictedKeys()).thenReturn(Future.succeededFuture(3L)); + + // when + new RedisStatsReporter(store, vertx, metrics, 1000L).start(); + + // then + verify(metrics).registerL2Gauges(sizeSupplierCaptor.capture(), evictionsSupplierCaptor.capture()); + assertThat(sizeSupplierCaptor.getValue().getAsLong()).isEqualTo(7L); + assertThat(evictionsSupplierCaptor.getValue().getAsLong()).isEqualTo(3L); + } + + @Test + public void startShouldPollImmediatelyAndScheduleAPeriodicPoll() { + // given + when(store.dbSize()).thenReturn(Future.succeededFuture(1L)); + when(store.evictedKeys()).thenReturn(Future.succeededFuture(1L)); + + // when + new RedisStatsReporter(store, vertx, metrics, 1000L).start(); + + // then + verify(store).dbSize(); + verify(store).evictedKeys(); + verify(vertx).setPeriodic(eq(1000L), any()); + } + + @Test + public void startShouldReturnSelfForFluentWiring() { + // given + when(store.dbSize()).thenReturn(Future.succeededFuture(0L)); + when(store.evictedKeys()).thenReturn(Future.succeededFuture(0L)); + final RedisStatsReporter target = new RedisStatsReporter(store, vertx, metrics, 1000L); + + // when and then + assertThat(target.start()).isSameAs(target); + } + + @Test + public void periodicTickShouldPollAgain() { + // given + when(store.dbSize()).thenReturn(Future.succeededFuture(1L)); + when(store.evictedKeys()).thenReturn(Future.succeededFuture(1L)); + when(vertx.setPeriodic(eq(1000L), periodicHandlerCaptor.capture())).thenReturn(1L); + + new RedisStatsReporter(store, vertx, metrics, 1000L).start(); + + // when + periodicHandlerCaptor.getValue().handle(1L); + + // then + verify(store, times(2)).dbSize(); + verify(store, times(2)).evictedKeys(); + } + + @Test + public void pollShouldLeaveGaugesAtZeroWhenBothProbesFail() { + // given + lenient().when(vertx.setPeriodic(eq(1000L), any())).thenReturn(1L); + when(store.dbSize()).thenReturn(Future.failedFuture(new RuntimeException("dbsize down"))); + when(store.evictedKeys()).thenReturn(Future.failedFuture(new RuntimeException("info down"))); + + // when + new RedisStatsReporter(store, vertx, metrics, 1000L).start(); + + // then + verify(metrics).registerL2Gauges(sizeSupplierCaptor.capture(), evictionsSupplierCaptor.capture()); + assertThat(sizeSupplierCaptor.getValue().getAsLong()).isZero(); + assertThat(evictionsSupplierCaptor.getValue().getAsLong()).isZero(); + } +} diff --git a/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/config/IntentiqIdentityConfigTest.java b/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/config/IntentiqIdentityConfigTest.java new file mode 100644 index 00000000000..60c37592e77 --- /dev/null +++ b/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/config/IntentiqIdentityConfigTest.java @@ -0,0 +1,175 @@ +package org.prebid.server.hooks.modules.intentiq.identity.config; + +import com.codahale.metrics.MetricRegistry; +import io.vertx.core.Future; +import io.vertx.core.Vertx; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.prebid.server.hooks.modules.intentiq.identity.cache.IdentityCache; +import org.prebid.server.hooks.modules.intentiq.identity.cache.IdentityStore; +import org.prebid.server.hooks.modules.intentiq.identity.cache.RedisIdentityStore; +import org.prebid.server.hooks.modules.intentiq.identity.cache.RedisStatsReporter; +import org.prebid.server.hooks.modules.intentiq.identity.metric.IntentiqIdentityMetrics; +import org.prebid.server.hooks.modules.intentiq.identity.metric.NoopIntentiqIdentityMetrics; +import org.prebid.server.hooks.modules.intentiq.identity.model.config.IntentiqIdentityProperties; +import org.prebid.server.hooks.modules.intentiq.identity.model.config.RedisProperties; +import org.prebid.server.hooks.modules.intentiq.identity.v1.IntentiqIdentityModule; +import org.prebid.server.hooks.modules.intentiq.identity.v1.core.ConfigResolver; +import org.prebid.server.json.JacksonMapper; +import org.prebid.server.json.JsonMerger; +import org.prebid.server.json.ObjectMapperProvider; +import org.prebid.server.vertx.httpclient.HttpClient; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +public class IntentiqIdentityConfigTest { + + private static final JacksonMapper MAPPER = new JacksonMapper(ObjectMapperProvider.mapper()); + + @Mock + private HttpClient httpClient; + + @Mock + private Vertx vertx; + + private final IntentiqIdentityConfig target = new IntentiqIdentityConfig(); + + @Test + public void propertiesBeanShouldBeCreated() { + assertThat(target.intentiqIdentityProperties()).isInstanceOf(IntentiqIdentityProperties.class); + } + + @Test + public void configResolverBeanShouldBeCreated() { + assertThat(target.intentiqIdentityConfigResolver(new JsonMerger(MAPPER), new IntentiqIdentityProperties())) + .isInstanceOf(ConfigResolver.class); + } + + @Test + public void moduleBeanShouldExposeProcessedAuctionRequestHookUnderModuleCode() { + // given + final ConfigResolver configResolver = + new ConfigResolver(MAPPER.mapper(), new JsonMerger(MAPPER), new IntentiqIdentityProperties()); + + // when + final IntentiqIdentityModule module = target.intentiqIdentityModule( + configResolver, + httpClient, + MAPPER, + new IntentiqIdentityMetrics(new MetricRegistry()), + new IntentiqIdentityProperties(), + mock(IdentityCache.class)); + + // then + assertThat(module.code()).isEqualTo(IntentiqIdentityModule.CODE); + assertThat(module.hooks()).hasSize(2); + } + + @Test + public void metricsBeanShouldBeCreated() { + assertThat(target.intentiqIdentityMetrics(new MetricRegistry())) + .isInstanceOf(IntentiqIdentityMetrics.class); + } + + @Test + public void noopMetricsBeanShouldBeCreated() { + assertThat(target.noopIntentiqIdentityMetrics()) + .isInstanceOf(NoopIntentiqIdentityMetrics.class); + } + + @Test + public void identityStoreBeanShouldFailWhenRedisHostMissing() { + // given + final IntentiqIdentityProperties properties = new IntentiqIdentityProperties(); + properties.setRedis(new RedisProperties()); + + // when and then + assertThatThrownBy(() -> target.intentiqIdentityStore(properties, vertx)) + .isInstanceOf(IllegalArgumentException.class); + } + + @Test + public void identityStoreBeanShouldFailWhenRedisPortMissing() { + // given + final RedisProperties redis = new RedisProperties(); + redis.setHost("localhost"); + final IntentiqIdentityProperties properties = new IntentiqIdentityProperties(); + properties.setRedis(redis); + + // when and then + assertThatThrownBy(() -> target.intentiqIdentityStore(properties, vertx)) + .isInstanceOf(IllegalArgumentException.class); + } + + @Test + public void redisStatsReporterBeanShouldReturnReporterWhenStoreIsRedis() { + // given + final RedisIdentityStore store = mock(RedisIdentityStore.class); + when(store.dbSize()).thenReturn(Future.succeededFuture(0L)); + when(store.evictedKeys()).thenReturn(Future.succeededFuture(0L)); + + // when + final RedisStatsReporter reporter = target.intentiqIdentityRedisStatsReporter( + store, vertx, new IntentiqIdentityMetrics(new MetricRegistry())); + + // then + assertThat(reporter).isNotNull(); + } + + @Test + public void redisStatsReporterBeanShouldReturnNullWhenStoreIsNotRedis() { + // when and then — a custom (non-Redis) IdentityStore has no DBSIZE/INFO stats to poll + assertThat(target.intentiqIdentityRedisStatsReporter( + mock(IdentityStore.class), vertx, new IntentiqIdentityMetrics(new MetricRegistry()))) + .isNull(); + } + + @Test + public void identityStoreBeanShouldBeBuiltWhenRedisPasswordConfigured() { + // given — a real Vertx is required because the Redis client casts it to VertxInternal + final Vertx realVertx = Vertx.vertx(); + final RedisProperties redis = new RedisProperties(); + redis.setHost("redis.internal"); + redis.setPort(6390); + redis.setPassword("s3cret"); + final IntentiqIdentityProperties properties = new IntentiqIdentityProperties(); + properties.setRedis(redis); + + try { + // when and then + assertThat(target.intentiqIdentityStore(properties, realVertx)).isNotNull(); + } finally { + realVertx.close(); + } + } + + @Test + public void identityStoreAndCacheBeansShouldBeBuiltWhenRedisConfigured() { + // given — a real Vertx is required because the Redis client casts it to VertxInternal + final Vertx realVertx = Vertx.vertx(); + final RedisProperties redis = new RedisProperties(); + redis.setHost("redis.internal"); + redis.setPort(6390); + final IntentiqIdentityProperties properties = new IntentiqIdentityProperties(); + properties.setRedis(redis); + + try { + // when + final IdentityStore store = target.intentiqIdentityStore(properties, realVertx); + final IdentityCache cache = target.intentiqIdentityCache( + properties, store, MAPPER, new IntentiqIdentityMetrics(new MetricRegistry())); + + // then + assertThat(store).isNotNull(); + assertThat(cache).isNotNull(); + } finally { + realVertx.close(); + } + } +} diff --git a/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/metric/IntentiqIdentityMetricsTest.java b/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/metric/IntentiqIdentityMetricsTest.java new file mode 100644 index 00000000000..ebbeaad774e --- /dev/null +++ b/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/metric/IntentiqIdentityMetricsTest.java @@ -0,0 +1,165 @@ +package org.prebid.server.hooks.modules.intentiq.identity.metric; + +import com.codahale.metrics.MetricRegistry; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class IntentiqIdentityMetricsTest { + + private static final String DPI = "15769"; + + private MetricRegistry metricRegistry; + private IntentiqIdentityMetrics target; + + @BeforeEach + public void setUp() { + metricRegistry = new MetricRegistry(); + target = new IntentiqIdentityMetrics(metricRegistry); + } + + @Test + public void shouldIncrementNamespacedCountersSuffixedWithDpi() { + // when + target.cacheHit("l1", "third_party", DPI); + target.cacheHit("l2", "third_party", DPI); + target.cacheNegativeHit("l2", "device", DPI); + target.cacheInProgress("l1", "device", DPI); + target.cacheMiss("third_party", DPI); + target.cacheMiss("device", DPI); + target.apiSuccess(DPI); + target.apiError(DPI); + target.enriched(DPI); + target.eidsNone(DPI); + target.skipNoEndpoint(DPI); + target.impressionReported(DPI); + target.impressionError(DPI); + + // then + assertThat(count("cache.l1.hit.third_party_" + DPI)).isEqualTo(1); + assertThat(count("cache.l2.hit.third_party_" + DPI)).isEqualTo(1); + assertThat(count("cache.l2.negative.hit.device_" + DPI)).isEqualTo(1); + assertThat(count("cache.l1.in_progress.device_" + DPI)).isEqualTo(1); + assertThat(count("cache.miss.third_party_" + DPI)).isEqualTo(1); + assertThat(count("cache.miss.device_" + DPI)).isEqualTo(1); + assertThat(count("api.success_" + DPI)).isEqualTo(1); + assertThat(count("api.error_" + DPI)).isEqualTo(1); + assertThat(count("enriched_" + DPI)).isEqualTo(1); + assertThat(count("eids.none_" + DPI)).isEqualTo(1); + assertThat(count("skip.no_endpoint_" + DPI)).isEqualTo(1); + assertThat(count("impression.reported_" + DPI)).isEqualTo(1); + assertThat(count("impression.error_" + DPI)).isEqualTo(1); + } + + @Test + public void shouldRecordNothingWhenDisabled() { + // given + final IntentiqIdentityMetrics disabled = new NoopIntentiqIdentityMetrics(); + + // when + disabled.cacheHit("l1", "third_party", DPI); + disabled.terminationCause(20, DPI); + disabled.apiLatency(1_000_000L, DPI); + + // then + assertThat(metricRegistry.getMetrics()).isEmpty(); + } + + @Test + public void shouldOmitDpiSuffixWhenDpiBlank() { + // when + target.enriched(null); + target.enriched(""); + + // then + assertThat(count("enriched")).isEqualTo(2); + } + + @Test + public void shouldIncrementTerminationCauseCounterByRawCauseId() { + // when + target.terminationCause(20, DPI); + target.terminationCause(20, DPI); + target.terminationCause(36, DPI); + + // then + assertThat(count("tc.20_" + DPI)).isEqualTo(2); + assertThat(count("tc.36_" + DPI)).isEqualTo(1); + } + + @Test + public void shouldDropOutOfRangeTerminationCauses() { + // when — out-of-range values emit no counter at all + target.terminationCause(3333, DPI); + target.terminationCause(120088, DPI); + target.terminationCause(49, DPI); + + // then + assertThat(count("tc.3333_" + DPI)).isZero(); + assertThat(count("tc.120088_" + DPI)).isZero(); + assertThat(count("tc.49_" + DPI)).isEqualTo(1); + } + + @Test + public void shouldRecordApiLatencyTimer() { + // when + target.apiLatency(1_000_000L, DPI); + target.apiLatency(3_000_000L, DPI); + + // then + assertThat(metricRegistry.timer("modules.module.intentiq-identity.custom.api.latency_" + DPI).getCount()) + .isEqualTo(2); + } + + @Test + public void shouldRecordL1L2HealthMetricsGloballyWithoutDpiSuffix() { + // when — shared L1/L2 health metrics carry no _ segment + target.l1GetError(); + target.l1PutError(); + target.l2GetError(); + target.l2PutError(); + target.l2PutError(); + target.l2GetLatency(1_000_000L); + target.l2PutLatency(2_000_000L); + target.registerL1Gauges(() -> 7L, () -> 3L); + target.registerL2Gauges(() -> 100L, () -> 9L); + + // then + assertThat(count("l1.get.error")).isEqualTo(1); + assertThat(count("l1.put.error")).isEqualTo(1); + assertThat(count("l2.get.error")).isEqualTo(1); + assertThat(count("l2.put.error")).isEqualTo(2); + assertThat(metricRegistry.timer("modules.module.intentiq-identity.custom.l2.get.latency").getCount()) + .isEqualTo(1); + assertThat(metricRegistry.timer("modules.module.intentiq-identity.custom.l2.put.latency").getCount()) + .isEqualTo(1); + assertThat(gauge("l1.size")).isEqualTo(7L); + assertThat(gauge("l1.eviction")).isEqualTo(3L); + assertThat(gauge("l2.size")).isEqualTo(100L); + assertThat(gauge("l2.eviction")).isEqualTo(9L); + } + + @Test + public void shouldRecordNoHealthMetricsWhenDisabled() { + // given + final IntentiqIdentityMetrics disabled = new NoopIntentiqIdentityMetrics(); + + // when + disabled.l2GetError(); + disabled.l1PutError(); + disabled.l2GetLatency(1_000_000L); + disabled.registerL1Gauges(() -> 7L, () -> 3L); + + // then + assertThat(metricRegistry.getMetrics()).isEmpty(); + } + + private Object gauge(String name) { + return metricRegistry.getGauges().get("modules.module.intentiq-identity.custom." + name).getValue(); + } + + private long count(String name) { + return metricRegistry.counter("modules.module.intentiq-identity.custom." + name).getCount(); + } +} diff --git a/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityAuctionResponseHookTest.java b/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityAuctionResponseHookTest.java new file mode 100644 index 00000000000..31b85ce4833 --- /dev/null +++ b/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityAuctionResponseHookTest.java @@ -0,0 +1,233 @@ +package org.prebid.server.hooks.modules.intentiq.identity.v1; + +import com.codahale.metrics.MetricRegistry; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.iab.openrtb.request.BidRequest; +import com.iab.openrtb.request.Device; +import com.iab.openrtb.request.Site; +import com.iab.openrtb.response.Bid; +import com.iab.openrtb.response.BidResponse; +import com.iab.openrtb.response.SeatBid; +import io.vertx.core.Future; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.mockito.junit.jupiter.MockitoSettings; +import org.mockito.quality.Strictness; +import org.prebid.server.auction.model.AuctionContext; +import org.prebid.server.hooks.execution.v1.auction.AuctionResponsePayloadImpl; +import org.prebid.server.hooks.modules.intentiq.identity.metric.IntentiqIdentityMetrics; +import org.prebid.server.hooks.modules.intentiq.identity.model.IntentiqIdentityModuleContext; +import org.prebid.server.hooks.modules.intentiq.identity.model.config.IntentiqIdentityProperties; +import org.prebid.server.hooks.modules.intentiq.identity.v1.core.ConfigResolver; +import org.prebid.server.hooks.v1.InvocationAction; +import org.prebid.server.hooks.v1.auction.AuctionInvocationContext; +import org.prebid.server.hooks.v1.auction.AuctionResponsePayload; +import org.prebid.server.json.JacksonMapper; +import org.prebid.server.json.JsonMerger; +import org.prebid.server.json.ObjectMapperProvider; +import org.prebid.server.vertx.httpclient.HttpClient; +import org.prebid.server.vertx.httpclient.model.HttpClientResponse; + +import java.math.BigDecimal; +import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; +import java.util.List; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoInteractions; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +@MockitoSettings(strictness = Strictness.LENIENT) +public class IntentiqIdentityAuctionResponseHookTest { + + private static final JacksonMapper MAPPER = new JacksonMapper(ObjectMapperProvider.mapper()); + private static final JsonMerger MERGER = new JsonMerger(MAPPER); + private static final String REPORTS = "https://reports.example.com/report"; + + @Mock + private HttpClient httpClient; + + @Mock + private AuctionInvocationContext invocationContext; + + @Mock + private AuctionContext auctionContext; + + @Captor + private ArgumentCaptor urlCaptor; + + private final MetricRegistry metricRegistry = new MetricRegistry(); + private final IntentiqIdentityMetrics metrics = new IntentiqIdentityMetrics(metricRegistry); + + @BeforeEach + public void setUp() { + when(invocationContext.accountConfig()).thenReturn(MAPPER.mapper().createObjectNode()); + when(invocationContext.auctionContext()).thenReturn(auctionContext); + when(invocationContext.moduleContext()).thenReturn(null); + when(httpClient.get(urlCaptor.capture(), any(), anyLong())) + .thenReturn(Future.succeededFuture(HttpClientResponse.of(200, null, "{}"))); + } + + private IntentiqIdentityAuctionResponseHook target(String reportsEndpoint, String partnerId) { + final IntentiqIdentityProperties properties = new IntentiqIdentityProperties(); + properties.setReportsEndpoint(reportsEndpoint); + properties.setPartnerId(partnerId); + properties.setTimeout(1000L); + final ConfigResolver configResolver = new ConfigResolver(MAPPER.mapper(), MERGER, properties); + return new IntentiqIdentityAuctionResponseHook(configResolver, httpClient, MAPPER, metrics); + } + + private AuctionResponsePayload payload(BidResponse bidResponse) { + return AuctionResponsePayloadImpl.of(bidResponse); + } + + private void givenRequest(BidRequest bidRequest) { + when(auctionContext.getBidRequest()).thenReturn(bidRequest); + } + + private static BidResponse response(String seat, Bid... bids) { + return BidResponse.builder() + .cur("USD") + .seatbid(List.of(SeatBid.builder().seat(seat).bid(List.of(bids)).build())) + .build(); + } + + private static String rdataJson(String url) { + final String marker = "rdata="; + final String encoded = url.substring(url.indexOf(marker) + marker.length()); + return URLDecoder.decode(encoded, StandardCharsets.UTF_8); + } + + @Test + public void callShouldReportWinningBidWithRdataAndConstants() { + // given + givenRequest(BidRequest.builder() + .site(Site.builder().domain("test.com").build()) + .device(Device.builder().ip("1.2.3.4").ua("UA").build()) + .build()); + when(invocationContext.moduleContext()) + .thenReturn(new IntentiqIdentityModuleContext(System.nanoTime(), "ab-123", null)); + final Bid bid = Bid.builder().impid("imp1").price(BigDecimal.valueOf(1.18)).build(); + + // when + target(REPORTS, "999").call(payload(response("pubmatic", bid)), invocationContext); + + // then + final String url = urlCaptor.getValue(); + assertThat(url).startsWith(REPORTS + "?at=45&rtype=1&source=pbjv&dpi=999&rdata="); + assertThat(url).contains("pubmatic").contains("imp1").contains("ab-123").contains("test.com"); + assertThat(metricRegistry.counter("modules.module.intentiq-identity.custom.impression.reported_999") + .getCount()) + .isEqualTo(1); + } + + @Test + public void callShouldReportEachWinningBid() { + // given + givenRequest(BidRequest.builder().build()); + final Bid bid1 = Bid.builder().impid("a").price(BigDecimal.ONE).build(); + final Bid bid2 = Bid.builder().impid("b").price(BigDecimal.TEN).build(); + + // when + target(REPORTS, "999").call(payload(response("seat", bid1, bid2)), invocationContext); + + // then + verify(httpClient, times(2)).get(any(), any(), anyLong()); + } + + @Test + public void callShouldOmitAbTestUuidWhenAbsent() { + // given + givenRequest(BidRequest.builder().build()); + final Bid bid = Bid.builder().impid("a").price(BigDecimal.ONE).build(); + + // when + target(REPORTS, "999").call(payload(response("seat", bid)), invocationContext); + + // then + assertThat(urlCaptor.getValue()).doesNotContain("abTestUuid"); + } + + @Test + public void callShouldIncludeOriginalCpmAndCurrencyFromBidExt() { + // given + givenRequest(BidRequest.builder().build()); + final ObjectNode ext = MAPPER.mapper().createObjectNode(); + ext.put("origbidcpm", new BigDecimal("2.50")); + ext.put("origbidcur", "EUR"); + final Bid bid = Bid.builder().impid("a").price(BigDecimal.valueOf(1.18)).ext(ext).build(); + + // when + target(REPORTS, "999").call(payload(response("seat", bid)), invocationContext); + + // then + final String rdata = rdataJson(urlCaptor.getValue()); + assertThat(rdata).contains("\"originalCpm\":2.50").contains("\"originalCurrency\":\"EUR\""); + } + + @Test + public void callShouldIncludePartnerAuctionIdFromRequestId() { + // given + givenRequest(BidRequest.builder().id("auction-77").build()); + final Bid bid = Bid.builder().impid("a").price(BigDecimal.ONE).build(); + + // when + target(REPORTS, "999").call(payload(response("seat", bid)), invocationContext); + + // then + assertThat(rdataJson(urlCaptor.getValue())).contains("\"partnerAuctionId\":\"auction-77\""); + } + + @Test + public void callShouldIncludeTerminationCauseFromModuleContext() { + // given + givenRequest(BidRequest.builder().build()); + when(invocationContext.moduleContext()) + .thenReturn(new IntentiqIdentityModuleContext(System.nanoTime(), "ab-1", 7L)); + final Bid bid = Bid.builder().impid("a").price(BigDecimal.ONE).build(); + + // when + target(REPORTS, "999").call(payload(response("seat", bid)), invocationContext); + + // then + assertThat(rdataJson(urlCaptor.getValue())).contains("\"terminationCause\":7"); + } + + @Test + public void callShouldOmitTerminationCauseWhenModuleContextHasNone() { + // given + givenRequest(BidRequest.builder().build()); + when(invocationContext.moduleContext()) + .thenReturn(new IntentiqIdentityModuleContext(System.nanoTime(), "ab-1", null)); + final Bid bid = Bid.builder().impid("a").price(BigDecimal.ONE).build(); + + // when + target(REPORTS, "999").call(payload(response("seat", bid)), invocationContext); + + // then + assertThat(rdataJson(urlCaptor.getValue())).doesNotContain("terminationCause"); + } + + @Test + public void callShouldReturnNoActionAndSkipReportingWhenReportsEndpointBlank() { + // when + final var result = target(null, "999") + .call(payload(response("seat", Bid.builder().impid("a").price(BigDecimal.ONE).build())), + invocationContext) + .result(); + + // then + assertThat(result.action()).isEqualTo(InvocationAction.no_action); + verifyNoInteractions(httpClient); + } +} diff --git a/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityModuleTest.java b/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityModuleTest.java new file mode 100644 index 00000000000..81bde2b0a1d --- /dev/null +++ b/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityModuleTest.java @@ -0,0 +1,55 @@ +package org.prebid.server.hooks.modules.intentiq.identity.v1; + +import com.codahale.metrics.MetricRegistry; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.prebid.server.hooks.modules.intentiq.identity.metric.IntentiqIdentityMetrics; +import org.prebid.server.hooks.modules.intentiq.identity.model.config.IntentiqIdentityProperties; +import org.prebid.server.hooks.modules.intentiq.identity.v1.core.ConfigResolver; +import org.prebid.server.hooks.modules.intentiq.identity.v1.core.FirstPartyKeyExtractor; +import org.prebid.server.hooks.v1.Hook; +import org.prebid.server.hooks.v1.InvocationContext; +import org.prebid.server.json.JacksonMapper; +import org.prebid.server.json.JsonMerger; +import org.prebid.server.json.ObjectMapperProvider; +import org.prebid.server.vertx.httpclient.HttpClient; + +import java.util.List; + +import static java.util.Collections.emptyList; +import static org.assertj.core.api.Assertions.assertThat; + +@ExtendWith(MockitoExtension.class) +public class IntentiqIdentityModuleTest { + + private static final JacksonMapper MAPPER = new JacksonMapper(ObjectMapperProvider.mapper()); + + @Mock + private HttpClient httpClient; + + @Test + public void codeShouldReturnExpectedValue() { + // given + final IntentiqIdentityModule target = new IntentiqIdentityModule(emptyList()); + + // when and then + assertThat(target.code()).isEqualTo("intentiq-identity"); + } + + @Test + public void hooksShouldReturnProvidedHooks() { + // given + final ConfigResolver configResolver = + new ConfigResolver(MAPPER.mapper(), new JsonMerger(MAPPER), new IntentiqIdentityProperties()); + final List> hooks = List.of( + new IntentiqIdentityProcessedAuctionRequestHook( + configResolver, httpClient, MAPPER, null, + new FirstPartyKeyExtractor(10), new IntentiqIdentityMetrics(new MetricRegistry()))); + final IntentiqIdentityModule target = new IntentiqIdentityModule(hooks); + + // when and then + assertThat(target.hooks()).isEqualTo(hooks); + } +} diff --git a/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityProcessedAuctionRequestHookTest.java b/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityProcessedAuctionRequestHookTest.java new file mode 100644 index 00000000000..241fcb36258 --- /dev/null +++ b/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityProcessedAuctionRequestHookTest.java @@ -0,0 +1,750 @@ +package org.prebid.server.hooks.modules.intentiq.identity.v1; + +import com.codahale.metrics.MetricRegistry; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.ObjectNode; +import com.iab.openrtb.request.BidRequest; +import com.iab.openrtb.request.BrandVersion; +import com.iab.openrtb.request.Device; +import com.iab.openrtb.request.Eid; +import com.iab.openrtb.request.Regs; +import com.iab.openrtb.request.Site; +import com.iab.openrtb.request.Uid; +import com.iab.openrtb.request.User; +import com.iab.openrtb.request.UserAgent; +import org.prebid.server.proto.openrtb.ext.request.ExtRegs; +import org.prebid.server.proto.openrtb.ext.request.ExtUser; +import io.vertx.core.Future; +import io.vertx.core.MultiMap; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Captor; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; +import org.mockito.junit.jupiter.MockitoSettings; +import org.mockito.quality.Strictness; +import org.prebid.server.hooks.execution.v1.auction.AuctionRequestPayloadImpl; +import org.prebid.server.hooks.modules.intentiq.identity.cache.CacheResult; +import org.prebid.server.hooks.modules.intentiq.identity.cache.IdentityCache; +import org.prebid.server.hooks.modules.intentiq.identity.cache.KeyType; +import org.prebid.server.hooks.modules.intentiq.identity.metric.IntentiqIdentityMetrics; +import org.prebid.server.hooks.modules.intentiq.identity.model.IntentiqIdentityModuleContext; +import org.prebid.server.hooks.modules.intentiq.identity.model.config.IntentiqIdentityProperties; +import org.prebid.server.hooks.modules.intentiq.identity.v1.core.ConfigResolver; +import org.prebid.server.hooks.modules.intentiq.identity.v1.core.FirstPartyKeyExtractor; +import org.prebid.server.hooks.v1.InvocationAction; +import org.prebid.server.hooks.v1.InvocationResult; +import org.prebid.server.hooks.v1.InvocationStatus; +import org.prebid.server.hooks.v1.PayloadUpdate; +import org.prebid.server.hooks.v1.auction.AuctionInvocationContext; +import org.prebid.server.hooks.v1.auction.AuctionRequestPayload; +import org.prebid.server.json.JacksonMapper; +import org.prebid.server.json.JsonMerger; +import org.prebid.server.json.ObjectMapperProvider; +import org.prebid.server.vertx.httpclient.HttpClient; +import org.prebid.server.vertx.httpclient.model.HttpClientResponse; + +import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; +import java.util.List; + +import static java.util.Collections.singletonList; +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoInteractions; +import static org.mockito.Mockito.when; + +@ExtendWith(MockitoExtension.class) +@MockitoSettings(strictness = Strictness.LENIENT) +public class IntentiqIdentityProcessedAuctionRequestHookTest { + + private static final JacksonMapper MAPPER = new JacksonMapper(ObjectMapperProvider.mapper()); + private static final JsonMerger MERGER = new JsonMerger(MAPPER); + private static final String API_ENDPOINT = "https://dev.example.com/resolve"; + private static final String EIDS_BODY = + "{\"data\":{\"eids\":[{\"source\":\"intentiq.com\",\"uids\":[{\"id\":\"abc\",\"atype\":1}]}]}}"; + private static final String EMPTY_BODY = "{\"data\":{\"eids\":[]}}"; + + @Mock + private HttpClient httpClient; + + @Mock + private AuctionInvocationContext invocationContext; + + @Captor + private ArgumentCaptor urlCaptor; + + @Captor + private ArgumentCaptor headersCaptor; + + private final MetricRegistry metricRegistry = new MetricRegistry(); + private final IntentiqIdentityMetrics metrics = new IntentiqIdentityMetrics(metricRegistry); + + @BeforeEach + public void setUp() { + when(invocationContext.accountConfig()).thenReturn(MAPPER.mapper().createObjectNode()); + } + + // Counter-asserting tests all run with partner id "123"; metrics are suffixed with the dpi. + private long counter(String name) { + return metricRegistry.counter("modules.module.intentiq-identity.custom." + name + "_123").getCount(); + } + + private IntentiqIdentityProcessedAuctionRequestHook target(String apiEndpoint, String partnerId) { + return target(apiEndpoint, partnerId, null); + } + + private IntentiqIdentityProcessedAuctionRequestHook target(String apiEndpoint, String partnerId, + IdentityCache cache) { + final IntentiqIdentityProperties properties = new IntentiqIdentityProperties(); + properties.setApiEndpoint(apiEndpoint); + properties.setPartnerId(partnerId); + properties.setTimeout(1000L); + if (cache != null) { + properties.getCache().setEnabled(true); + } + final ConfigResolver configResolver = new ConfigResolver(MAPPER.mapper(), MERGER, properties); + return new IntentiqIdentityProcessedAuctionRequestHook( + configResolver, httpClient, MAPPER, cache, new FirstPartyKeyExtractor(10), metrics); + } + + @Test + public void codeShouldReturnExpectedValue() { + assertThat(target(API_ENDPOINT, "123").code()) + .isEqualTo("intentiq-identity-processed-auction-request-hook"); + } + + @Test + public void callShouldEnrichUserEidsWhenApiReturnsEids() { + // given + givenResponse(EIDS_BODY); + final AuctionRequestPayload payload = AuctionRequestPayloadImpl.of(BidRequest.builder().build()); + + // when + final InvocationResult result = target(API_ENDPOINT, "123") + .call(payload, invocationContext).result(); + + // then + assertThat(result.status()).isEqualTo(InvocationStatus.success); + assertThat(result.action()).isEqualTo(InvocationAction.update); + final Eid enriched = applyUpdate(result, payload).getUser().getEids().getFirst(); + assertThat(enriched.getSource()).isEqualTo("intentiq.com"); + assertThat(enriched.getUids().getFirst().getId()).isEqualTo("abc"); + } + + @Test + public void callShouldFallBackToGlobalPropertiesWhenAccountConfigIsNull() { + // given — host-level-only config: no account override, so accountConfig() is null in production + // (JsonMergePatch rejects a null patch, which previously failed the whole hook invocation). + givenResponse(EIDS_BODY); + when(invocationContext.accountConfig()).thenReturn(null); + final AuctionRequestPayload payload = AuctionRequestPayloadImpl.of(BidRequest.builder().build()); + + // when + final InvocationResult result = target(API_ENDPOINT, "123") + .call(payload, invocationContext).result(); + + // then — resolver falls back to global properties and the hook still enriches + assertThat(result.status()).isEqualTo(InvocationStatus.success); + assertThat(result.action()).isEqualTo(InvocationAction.update); + assertThat(applyUpdate(result, payload).getUser().getEids().getFirst().getSource()) + .isEqualTo("intentiq.com"); + } + + @Test + public void callShouldAppendResolvedEidsAfterExistingUserEids() { + // given + givenResponse(EIDS_BODY); + final Eid existing = Eid.builder() + .source("pubcid.org") + .uids(singletonList(Uid.builder().id("existing-uid").build())) + .build(); + final BidRequest bidRequest = BidRequest.builder() + .user(User.builder().eids(singletonList(existing)).build()) + .build(); + final AuctionRequestPayload payload = AuctionRequestPayloadImpl.of(bidRequest); + + // when + final InvocationResult result = target(API_ENDPOINT, "123") + .call(payload, invocationContext).result(); + + // then + assertThat(applyUpdate(result, payload).getUser().getEids()) + .extracting(Eid::getSource) + .containsExactly("pubcid.org", "intentiq.com"); + } + + @Test + public void callShouldSendGetWithConstantsPartnerAndSrvrReq() { + // given + givenCapturedResponse(); + + // when + target(API_ENDPOINT, "partner-42").call( + AuctionRequestPayloadImpl.of(BidRequest.builder().build()), invocationContext); + + // then + assertThat(urlCaptor.getValue()) + .isEqualTo(API_ENDPOINT + "?at=39&mi=10&dpi=partner-42&pt=17&dpn=1&srvrReq=true&source=pbjv"); + } + + @Test + public void callShouldApplyAccountLevelPartnerIdOverride() { + // given + givenCapturedResponse(); + final ObjectNode account = MAPPER.mapper().createObjectNode().put("partner-id", "acct-9"); + when(invocationContext.accountConfig()).thenReturn(account); + + // when + target(API_ENDPOINT, "global-1").call( + AuctionRequestPayloadImpl.of(BidRequest.builder().build()), invocationContext); + + // then — account-level partner-id wins over the global value + assertThat(urlCaptor.getValue()).contains("&dpi=acct-9"); + } + + @Test + public void callShouldAppendDeviceIpIpv6AndUserAgent() { + // given + givenCapturedResponse(); + final BidRequest bidRequest = BidRequest.builder() + .device(Device.builder().ip("125.253.50.47").ipv6("2001:db8::1").ua("Mozilla/5.0 (iPhone)").build()) + .build(); + + // when + target(API_ENDPOINT, "383342646").call(AuctionRequestPayloadImpl.of(bidRequest), invocationContext); + + // then + assertThat(urlCaptor.getValue()) + .isEqualTo(API_ENDPOINT + "?at=39&mi=10&dpi=383342646&pt=17&dpn=1&srvrReq=true&source=pbjv" + + "&ip=125.253.50.47&ipv6=2001%3Adb8%3A%3A1&uas=Mozilla%2F5.0%20%28iPhone%29"); + } + + @Test + public void callShouldSendDeviceIfaAsPcidWithIdtype4ForMobile() { + // given + givenCapturedResponse(); + final BidRequest bidRequest = BidRequest.builder() + .device(Device.builder().ifa("maid-AbC").devicetype(1).build()) + .build(); + + // when + target(API_ENDPOINT, "123").call(AuctionRequestPayloadImpl.of(bidRequest), invocationContext); + + // then + assertThat(urlCaptor.getValue()).contains("&pcid=maid-AbC&idtype=4"); + } + + @Test + public void callShouldUppercasePcidAndSendIdtype8ForCtv() { + // given + givenCapturedResponse(); + final BidRequest bidRequest = BidRequest.builder() + .device(Device.builder().ifa("rida-abc").devicetype(3).build()) + .build(); + + // when + target(API_ENDPOINT, "123").call(AuctionRequestPayloadImpl.of(bidRequest), invocationContext); + + // then + assertThat(urlCaptor.getValue()).contains("&pcid=RIDA-ABC&idtype=8"); + } + + @Test + public void callShouldNotSendPcidWhenLimitAdTracking() { + // given + givenCapturedResponse(); + final BidRequest bidRequest = BidRequest.builder() + .device(Device.builder().ifa("maid-1").lmt(1).build()) + .build(); + + // when + target(API_ENDPOINT, "123").call(AuctionRequestPayloadImpl.of(bidRequest), invocationContext); + + // then + assertThat(urlCaptor.getValue()).doesNotContain("pcid").doesNotContain("idtype"); + } + + @Test + public void callShouldSendSiteDomainAsRef() { + // given + givenCapturedResponse(); + final BidRequest bidRequest = BidRequest.builder().site(Site.builder().domain("example.com").build()).build(); + + // when + target(API_ENDPOINT, "123").call(AuctionRequestPayloadImpl.of(bidRequest), invocationContext); + + // then + assertThat(urlCaptor.getValue()).contains("&ref=example.com"); + } + + @Test + public void callShouldSendExistingIiqUniversalIdAsIiquid() { + // given + givenCapturedResponse(); + final BidRequest bidRequest = BidRequest.builder() + .user(User.builder().eids(singletonList(Eid.builder() + .source("intentiq.com") + .uids(singletonList(Uid.builder().id("IIQ-UID-1").build())) + .build())).build()) + .build(); + + // when + target(API_ENDPOINT, "123").call(AuctionRequestPayloadImpl.of(bidRequest), invocationContext); + + // then + assertThat(urlCaptor.getValue()).contains("&iiquid=IIQ-UID-1"); + } + + @Test + public void callShouldAppendGdprUsPrivacyAndGppAsQueryParamsFromTopLevelFields() { + // given + givenCapturedResponse(); + final BidRequest bidRequest = BidRequest.builder() + .regs(Regs.builder().gdpr(1).usPrivacy("1YNN").gpp("DBABMA~CONSENT").gppSid(List.of(2, 6)).build()) + .user(User.builder().consent("CO-TCF-STRING").build()) + .build(); + + // when + target(API_ENDPOINT, "123").call(AuctionRequestPayloadImpl.of(bidRequest), invocationContext); + + // then — consent goes in the gdpr-consent header (GDPR guide), not the query string + assertThat(urlCaptor.getValue()) + .contains("&gdpr=1") + .contains("&us_privacy=1YNN") + .contains("&gpp=DBABMA%7ECONSENT") + .contains("&gpp_sid=2%2C6") + .doesNotContain("gdpr_consent"); + assertThat(headersCaptor.getValue().get("gdpr-consent")).isEqualTo("CO-TCF-STRING"); + } + + @Test + public void callShouldSendConsentInHeaderFromUserExtFallback() { + // given + givenCapturedResponse(); + final BidRequest bidRequest = BidRequest.builder() + .regs(Regs.builder().ext(ExtRegs.of(1, "1NYN", null, null)).build()) + .user(User.builder().ext(ExtUser.builder().consent("EXT-TCF-STRING").build()).build()) + .build(); + + // when + target(API_ENDPOINT, "123").call(AuctionRequestPayloadImpl.of(bidRequest), invocationContext); + + // then + assertThat(urlCaptor.getValue()) + .contains("&gdpr=1") + .contains("&us_privacy=1NYN") + .doesNotContain("gdpr_consent"); + assertThat(headersCaptor.getValue().get("gdpr-consent")).isEqualTo("EXT-TCF-STRING"); + } + + @Test + public void callShouldNotAppendConsentParamsOrHeaderWhenAbsent() { + // given + givenCapturedResponse(); + + // when + target(API_ENDPOINT, "123").call( + AuctionRequestPayloadImpl.of(BidRequest.builder().build()), invocationContext); + + // then + assertThat(urlCaptor.getValue()) + .doesNotContain("gdpr") + .doesNotContain("us_privacy") + .doesNotContain("gpp"); + assertThat(headersCaptor.getValue().get("gdpr-consent")).isNull(); + } + + @Test + public void callShouldAppendUaHintsFromDeviceSuaInUhParam() throws Exception { + // given + givenCapturedResponse(); + final UserAgent sua = UserAgent.builder() + .source(2) + .browsers(List.of( + new BrandVersion("Chromium", List.of("108", "0", "5359", "125"), null), + new BrandVersion("Google Chrome", List.of("108", "0", "5359", "125"), null), + new BrandVersion("Not?A_Brand", List.of("8", "0", "0", "0"), null))) + .platform(new BrandVersion("Windows", List.of("15", "0", "0"), null)) + .mobile(0) + .architecture("x86") + .bitness("64") + .build(); + final BidRequest bidRequest = BidRequest.builder().device(Device.builder().sua(sua).build()).build(); + + // when + target(API_ENDPOINT, "123").call(AuctionRequestPayloadImpl.of(bidRequest), invocationContext); + + // then — uh is the numeric-keyed UA-CH JSON the IntentIQ backend expects (brands sorted, major vs full version) + final JsonNode uh = MAPPER.mapper().readTree(extractParam(urlCaptor.getValue(), "uh")); + assertThat(uh.get("0").asText()) + .isEqualTo("\"Chromium\";v=\"108\", \"Google Chrome\";v=\"108\", \"Not?A_Brand\";v=\"8\""); + assertThat(uh.get("8").asText()).isEqualTo("\"Chromium\";v=\"108.0.5359.125\", " + + "\"Google Chrome\";v=\"108.0.5359.125\", \"Not?A_Brand\";v=\"8.0.0.0\""); + assertThat(uh.get("1").asText()).isEqualTo("?0"); + assertThat(uh.get("2").asText()).isEqualTo("\"Windows\""); + assertThat(uh.get("3").asText()).isEqualTo("\"x86\""); + assertThat(uh.get("4").asText()).isEqualTo("\"64\""); + assertThat(uh.get("6").asText()).isEqualTo("\"15.0.0\""); + assertThat(uh.has("5")).isFalse(); + assertThat(uh.has("7")).isFalse(); + } + + @Test + public void callShouldNotAppendUhWhenSuaSourceIsNotHighEntropy() { + // given — the IntentIQ backend only consumes hints when sua.source == 2 (high-entropy) + givenCapturedResponse(); + final UserAgent sua = UserAgent.builder() + .source(1) + .browsers(List.of(new BrandVersion("Chrome", List.of("120"), null))) + .build(); + final BidRequest bidRequest = BidRequest.builder().device(Device.builder().sua(sua).build()).build(); + + // when + target(API_ENDPOINT, "123").call(AuctionRequestPayloadImpl.of(bidRequest), invocationContext); + + // then + assertThat(urlCaptor.getValue()).doesNotContain("uh="); + } + + @Test + public void callShouldCarryTerminationCauseFromResponseInModuleContext() { + // given + givenResponse("{\"data\":{\"eids\":[{\"source\":\"intentiq.com\",\"uids\":[{\"id\":\"abc\"}]}]},\"tc\":5}"); + + // when + final InvocationResult result = target(API_ENDPOINT, "123") + .call(AuctionRequestPayloadImpl.of(BidRequest.builder().build()), invocationContext).result(); + + // then + final IntentiqIdentityModuleContext ctx = (IntentiqIdentityModuleContext) result.moduleContext(); + assertThat(ctx.abTestUuid()).isNull(); + assertThat(ctx.terminationCause()).isEqualTo(5L); + } + + @Test + public void callShouldTreatEmptyStringDataAsValidEmptyResponseNotApiError() { + // given — GDPR/invalid BE responses return data as an empty string (""), not an object + givenResponse("{\"adt\":4,\"ct\":2,\"data\":\"\",\"cttl\":600000,\"tc\":36}"); + + // when + final InvocationResult result = target(API_ENDPOINT, "123") + .call(AuctionRequestPayloadImpl.of(BidRequest.builder().build()), invocationContext).result(); + + // then — parsed cleanly as an empty result, not counted as an API error + assertThat(result.action()).isEqualTo(InvocationAction.no_action); + assertThat(counter("api.success")).isEqualTo(1); + assertThat(counter("api.error")).isZero(); + assertThat(counter("eids.none")).isEqualTo(1); + assertThat(counter("tc.36")).isEqualTo(1); + } + + @Test + public void callShouldPassResponseCttlToNegativeCacheOnMiss() { + // given — BE supplies the suppression window via cttl on an empty response + final IdentityCache cache = mock(IdentityCache.class); + when(cache.get(any())).thenReturn(Future.succeededFuture(CacheResult.miss())); + givenResponse("{\"data\":{\"eids\":[]},\"cttl\":600000}"); + final AuctionRequestPayload payload = AuctionRequestPayloadImpl.of( + BidRequest.builder().device(Device.builder().ifa("MAID-1").build()).build()); + + // when + target(API_ENDPOINT, "123", cache).call(payload, invocationContext).result(); + + // then + verify(cache).putNegative(any(), eq(600_000L)); + } + + @Test + public void callShouldReturnNoActionWhenApiReturnsNoEids() { + // given + givenResponse(EMPTY_BODY); + + // when + final InvocationResult result = target(API_ENDPOINT, "123") + .call(AuctionRequestPayloadImpl.of(BidRequest.builder().build()), invocationContext).result(); + + // then + assertThat(result.action()).isEqualTo(InvocationAction.no_action); + assertThat(result.payloadUpdate()).isNull(); + } + + @Test + public void callShouldReturnNoActionWhenHttpCallFails() { + // given + when(httpClient.get(any(), any(), anyLong())) + .thenReturn(Future.failedFuture(new RuntimeException("boom"))); + + // when + final Future> future = target(API_ENDPOINT, "123") + .call(AuctionRequestPayloadImpl.of(BidRequest.builder().build()), invocationContext); + + // then + assertThat(future.succeeded()).isTrue(); + assertThat(future.result().action()).isEqualTo(InvocationAction.no_action); + } + + @Test + public void callShouldReturnNoActionAndSkipHttpWhenApiEndpointBlank() { + // when + final InvocationResult result = target(null, "123") + .call(AuctionRequestPayloadImpl.of(BidRequest.builder().build()), invocationContext).result(); + + // then + assertThat(result.action()).isEqualTo(InvocationAction.no_action); + verifyNoInteractions(httpClient); + } + + @Test + public void callShouldUseCachedEidsAndSkipHttpOnCacheHit() { + // given + final IdentityCache cache = mock(IdentityCache.class); + final Eid cached = Eid.builder() + .source("intentiq.com") + .uids(singletonList(Uid.builder().id("cached-uid").build())) + .build(); + when(cache.get(any())) + .thenReturn(Future.succeededFuture( + CacheResult.hit(singletonList(cached), KeyType.FIRST_PARTY, CacheResult.Layer.L1))); + final AuctionRequestPayload payload = AuctionRequestPayloadImpl.of( + BidRequest.builder().device(Device.builder().ifa("MAID-1").build()).build()); + + // when + final InvocationResult result = + target(API_ENDPOINT, "123", cache).call(payload, invocationContext).result(); + + // then + assertThat(result.action()).isEqualTo(InvocationAction.update); + assertThat(applyUpdate(result, payload).getUser().getEids()).containsExactly(cached); + verifyNoInteractions(httpClient); + } + + @Test + public void callShouldFetchAndPopulateCacheOnMiss() { + // given + final IdentityCache cache = mock(IdentityCache.class); + when(cache.get(any())).thenReturn(Future.succeededFuture(CacheResult.miss())); + givenResponse(EIDS_BODY); + final AuctionRequestPayload payload = AuctionRequestPayloadImpl.of( + BidRequest.builder().device(Device.builder().ifa("MAID-1").build()).build()); + + // when + final InvocationResult result = + target(API_ENDPOINT, "123", cache).call(payload, invocationContext).result(); + + // then + assertThat(result.action()).isEqualTo(InvocationAction.update); + verify(cache).put(any(), any(), anyLong()); + } + + @Test + public void callShouldWriteNegativeCacheEntryWhenApiReturnsNoEidsOnMiss() { + // given + final IdentityCache cache = mock(IdentityCache.class); + when(cache.get(any())).thenReturn(Future.succeededFuture(CacheResult.miss())); + givenResponse(EMPTY_BODY); + final AuctionRequestPayload payload = AuctionRequestPayloadImpl.of( + BidRequest.builder().device(Device.builder().ifa("MAID-1").build()).build()); + + // when + final InvocationResult result = + target(API_ENDPOINT, "123", cache).call(payload, invocationContext).result(); + + // then — EMPTY_BODY carries no cttl, so the negative entry uses the default suppression TTL + assertThat(result.action()).isEqualTo(InvocationAction.no_action); + verify(cache).putNegative(any(), eq(0L)); + } + + @Test + public void callShouldReturnNoActionAndSkipHttpOnNegativeCacheHit() { + // given + final IdentityCache cache = mock(IdentityCache.class); + when(cache.get(any())).thenReturn(Future.succeededFuture( + CacheResult.negative(KeyType.FIRST_PARTY, CacheResult.Layer.L2))); + final AuctionRequestPayload payload = AuctionRequestPayloadImpl.of( + BidRequest.builder().device(Device.builder().ifa("MAID-1").build()).build()); + + // when + final InvocationResult result = + target(API_ENDPOINT, "123", cache).call(payload, invocationContext).result(); + + // then + assertThat(result.action()).isEqualTo(InvocationAction.no_action); + assertThat(counter("cache.l2.negative.hit.first_party")).isEqualTo(1); + assertThat(counter("cache.miss.first_party")).isEqualTo(1); + assertThat(counter("cache.l1.hit.first_party")).isZero(); + verifyNoInteractions(httpClient); + } + + @Test + public void callShouldIncrementApiSuccessAndEnrichedOnEnrichment() { + // given + givenResponse(EIDS_BODY); + + // when + target(API_ENDPOINT, "123").call( + AuctionRequestPayloadImpl.of(BidRequest.builder().build()), invocationContext).result(); + + // then + assertThat(counter("api.success")).isEqualTo(1); + assertThat(counter("enriched")).isEqualTo(1); + } + + @Test + public void callShouldRecordRawTerminationCauseFromResponse() { + // given + givenResponse("{\"data\":{\"eids\":[]},\"tc\":20}"); + + // when + target(API_ENDPOINT, "123").call( + AuctionRequestPayloadImpl.of(BidRequest.builder().build()), invocationContext).result(); + + // then + assertThat(counter("tc.20")).isEqualTo(1); + } + + @Test + public void callShouldDropOutOfRangeTerminationCause() { + // given — an out-of-range tc emits no counter + givenResponse("{\"data\":{\"eids\":[]},\"tc\":120088}"); + + // when + target(API_ENDPOINT, "123").call( + AuctionRequestPayloadImpl.of(BidRequest.builder().build()), invocationContext).result(); + + // then + assertThat(counter("tc.120088")).isZero(); + } + + @Test + public void callShouldIncrementApiErrorWhenHttpFails() { + // given + when(httpClient.get(any(), any(), anyLong())) + .thenReturn(Future.failedFuture(new RuntimeException("boom"))); + + // when + target(API_ENDPOINT, "123").call( + AuctionRequestPayloadImpl.of(BidRequest.builder().build()), invocationContext).result(); + + // then + assertThat(counter("api.error")).isEqualTo(1); + } + + @Test + public void callShouldIncrementCacheHitOnCacheHit() { + // given + final IdentityCache cache = mock(IdentityCache.class); + when(cache.get(any())).thenReturn(Future.succeededFuture(CacheResult.hit(singletonList(Eid.builder() + .source("intentiq.com") + .uids(singletonList(Uid.builder().id("u").build())) + .build()), KeyType.FIRST_PARTY, CacheResult.Layer.L1))); + final AuctionRequestPayload payload = AuctionRequestPayloadImpl.of( + BidRequest.builder().device(Device.builder().ifa("MAID-1").build()).build()); + + // when + target(API_ENDPOINT, "123", cache).call(payload, invocationContext).result(); + + // then + assertThat(counter("cache.l1.hit.first_party")).isEqualTo(1); + assertThat(counter("cache.miss.first_party")).isZero(); + } + + @Test + public void callShouldIncrementCacheMissOnCacheMiss() { + // given + final IdentityCache cache = mock(IdentityCache.class); + when(cache.get(any())).thenReturn(Future.succeededFuture(CacheResult.miss())); + givenResponse(EIDS_BODY); + final AuctionRequestPayload payload = AuctionRequestPayloadImpl.of( + BidRequest.builder().device(Device.builder().ifa("MAID-1").build()).build()); + + // when + target(API_ENDPOINT, "123", cache).call(payload, invocationContext).result(); + + // then + assertThat(counter("cache.miss.first_party")).isEqualTo(1); + assertThat(counter("cache.l1.hit.first_party")).isZero(); + } + + @Test + public void callShouldMarkInProgressBeforeFetchingOnCacheMiss() { + // given + final IdentityCache cache = mock(IdentityCache.class); + when(cache.get(any())).thenReturn(Future.succeededFuture(CacheResult.miss())); + givenResponse(EIDS_BODY); + final AuctionRequestPayload payload = AuctionRequestPayloadImpl.of( + BidRequest.builder().device(Device.builder().ifa("MAID-1").build()).build()); + + // when + target(API_ENDPOINT, "123", cache).call(payload, invocationContext).result(); + + // then — marker written so concurrent requests don't fire a duplicate, then the call is made + verify(cache).putInProgress(any()); + verify(httpClient).get(any(), any(), anyLong()); + } + + @Test + public void callShouldReturnNoActionAndSkipHttpOnInProgressCacheResult() { + // given + final IdentityCache cache = mock(IdentityCache.class); + when(cache.get(any())).thenReturn(Future.succeededFuture( + CacheResult.inProgress(KeyType.FIRST_PARTY, CacheResult.Layer.L1))); + final AuctionRequestPayload payload = AuctionRequestPayloadImpl.of( + BidRequest.builder().device(Device.builder().ifa("MAID-1").build()).build()); + + // when + final InvocationResult result = + target(API_ENDPOINT, "123", cache).call(payload, invocationContext).result(); + + // then — a resolution is already in flight: do not fire a duplicate, do not enrich + assertThat(result.action()).isEqualTo(InvocationAction.no_action); + verifyNoInteractions(httpClient); + verify(cache, never()).putInProgress(any()); + } + + @Test + public void callShouldIncrementCacheInProgressOnInProgressResult() { + // given + final IdentityCache cache = mock(IdentityCache.class); + when(cache.get(any())).thenReturn(Future.succeededFuture( + CacheResult.inProgress(KeyType.FIRST_PARTY, CacheResult.Layer.L1))); + final AuctionRequestPayload payload = AuctionRequestPayloadImpl.of( + BidRequest.builder().device(Device.builder().ifa("MAID-1").build()).build()); + + // when + target(API_ENDPOINT, "123", cache).call(payload, invocationContext).result(); + + // then + assertThat(counter("cache.l1.in_progress.first_party")).isEqualTo(1); + } + + private void givenResponse(String body) { + when(httpClient.get(any(), any(), anyLong())) + .thenReturn(Future.succeededFuture(HttpClientResponse.of(200, null, body))); + } + + private void givenCapturedResponse() { + when(httpClient.get(urlCaptor.capture(), headersCaptor.capture(), anyLong())) + .thenReturn(Future.succeededFuture(HttpClientResponse.of(200, null, EMPTY_BODY))); + } + + private static BidRequest applyUpdate(InvocationResult result, + AuctionRequestPayload payload) { + final PayloadUpdate update = result.payloadUpdate(); + return update.apply(payload).bidRequest(); + } + + private static String extractParam(String url, String key) { + final int start = url.indexOf("&" + key + "=") + key.length() + 2; + final int end = url.indexOf('&', start); + final String value = url.substring(start, end < 0 ? url.length() : end); + return URLDecoder.decode(value, StandardCharsets.UTF_8); + } +} diff --git a/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/DeviceUserAgentTest.java b/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/DeviceUserAgentTest.java new file mode 100644 index 00000000000..d9db6af098f --- /dev/null +++ b/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/DeviceUserAgentTest.java @@ -0,0 +1,39 @@ +package org.prebid.server.hooks.modules.intentiq.identity.v1.core; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class DeviceUserAgentTest { + + @Test + public void shouldReturnEmptyForBlankUa() { + assertThat(DeviceUserAgent.normalize(null)).isEmpty(); + assertThat(DeviceUserAgent.normalize(" ")).isEmpty(); + } + + @Test + public void shouldReturnEmptyForUnrecognizedUa() { + assertThat(DeviceUserAgent.normalize("UA")).isEmpty(); + } + + @Test + public void shouldNormalizeIphoneSafari() { + final String ua = "Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) " + + "AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1"; + + assertThat(DeviceUserAgent.normalize(ua)) + .contains("iOS17") + .contains("iPhone") + .doesNotContain(" "); + } + + @Test + public void shouldBeDeterministic() { + final String ua = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " + + "(KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"; + + assertThat(DeviceUserAgent.normalize(ua)).isEqualTo(DeviceUserAgent.normalize(ua)); + assertThat(DeviceUserAgent.normalize(ua)).contains("Chrome120"); + } +} diff --git a/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/FirstPartyKeyExtractorTest.java b/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/FirstPartyKeyExtractorTest.java new file mode 100644 index 00000000000..de2e2e88749 --- /dev/null +++ b/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/FirstPartyKeyExtractorTest.java @@ -0,0 +1,146 @@ +package org.prebid.server.hooks.modules.intentiq.identity.v1.core; + +import com.iab.openrtb.request.BidRequest; +import com.iab.openrtb.request.Device; +import com.iab.openrtb.request.Eid; +import com.iab.openrtb.request.Uid; +import com.iab.openrtb.request.User; +import org.junit.jupiter.api.Test; +import org.prebid.server.hooks.modules.intentiq.identity.cache.CacheKey; +import org.prebid.server.hooks.modules.intentiq.identity.cache.KeyType; + +import java.util.List; + +import static java.util.Collections.singletonList; +import static org.assertj.core.api.Assertions.assertThat; + +public class FirstPartyKeyExtractorTest { + + private final FirstPartyKeyExtractor target = new FirstPartyKeyExtractor(10); + + @Test + public void shouldReturnEmptyWhenNoIdentifiersPresent() { + assertThat(target.candidateKeys(BidRequest.builder().build())).isEmpty(); + } + + @Test + public void shouldOrderKeysByPriorityAndTagTypes() { + // given + final BidRequest bidRequest = BidRequest.builder() + .user(User.builder().eids(List.of( + eid("uidapi.com", "uid2-1"), + eid("pubcid.org", "pub-1"), + eid("intentiq.com", "iiq-1"))) + .build()) + .device(Device.builder().ifa("ifa-1").ua("UA").ip("1.2.3.4").build()) + .build(); + + // when + final List keys = target.candidateKeys(bidRequest); + + // then — iiq, pubcid, maid, other eid, device composite (unrecognized UA contributes nothing) + assertThat(keys).containsExactly( + new CacheKey("iiq:iiq-1", KeyType.THIRD_PARTY), + new CacheKey("pubcid:pub-1", KeyType.FIRST_PARTY), + new CacheKey("maid:ifa-1", KeyType.FIRST_PARTY), + new CacheKey("uidapi.com:uid2-1", KeyType.FIRST_PARTY), + new CacheKey("dev:ifa-1_1.2.3.4", KeyType.DEVICE)); + } + + @Test + public void shouldNormalizeUserAgentInDeviceCompositeRatherThanUseRawString() { + // given — a real iPhone Safari UA + final String rawUa = "Mozilla/5.0 (iPhone; CPU iPhone OS 17_0 like Mac OS X) " + + "AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.0 Mobile/15E148 Safari/604.1"; + final BidRequest bidRequest = BidRequest.builder() + .device(Device.builder().ua(rawUa).ip("1.2.3.4").build()) + .build(); + + // when + final CacheKey deviceKey = target.candidateKeys(bidRequest).stream() + .filter(key -> key.type() == KeyType.DEVICE) + .findFirst() + .orElseThrow(); + + // then — normalized OS/browser/device tokens, never the raw UA string + assertThat(deviceKey.key()) + .startsWith("dev:") + .endsWith("_1.2.3.4") + .contains("iOS") + .doesNotContain(rawUa) + .doesNotContain(" "); + } + + @Test + public void shouldTreatSharedidOrgAsPubcid() { + // given + final BidRequest bidRequest = BidRequest.builder() + .user(User.builder().eids(singletonList(eid("sharedid.org", "s-1"))).build()) + .build(); + + // when / then + assertThat(target.candidateKeys(bidRequest)) + .containsExactly(new CacheKey("pubcid:s-1", KeyType.FIRST_PARTY)); + } + + @Test + public void shouldSkipMaidKeyWhenLimitAdTracking() { + // given + final BidRequest bidRequest = BidRequest.builder() + .device(Device.builder().ifa("ifa-1").lmt(1).build()) + .build(); + + // when / then — only the device composite remains, no maid: key + assertThat(target.candidateKeys(bidRequest)) + .extracting(CacheKey::key) + .doesNotContain("maid:ifa-1"); + } + + @Test + public void shouldUppercaseMaidForCtv() { + // given + final BidRequest bidRequest = BidRequest.builder() + .device(Device.builder().ifa("rida-abc").devicetype(3).build()) + .build(); + + // when / then + assertThat(target.candidateKeys(bidRequest)) + .extracting(CacheKey::key) + .contains("maid:RIDA-ABC"); + } + + @Test + public void shouldDeduplicateRepeatedKeys() { + // given — same pubcid id twice + final BidRequest bidRequest = BidRequest.builder() + .user(User.builder().eids(List.of(eid("pubcid.org", "dup"), eid("pubcid.org", "dup"))).build()) + .build(); + + // when / then + assertThat(target.candidateKeys(bidRequest)) + .containsExactly(new CacheKey("pubcid:dup", KeyType.FIRST_PARTY)); + } + + @Test + public void shouldCapNumberOfKeys() { + // given — extractor capped at 2 + final FirstPartyKeyExtractor capped = new FirstPartyKeyExtractor(2); + final BidRequest bidRequest = BidRequest.builder() + .user(User.builder().eids(List.of( + eid("intentiq.com", "iiq-1"), + eid("pubcid.org", "pub-1"), + eid("uidapi.com", "uid2-1"))) + .build()) + .build(); + + // when / then — only the two highest-priority keys survive + assertThat(capped.candidateKeys(bidRequest)) + .containsExactly( + new CacheKey("iiq:iiq-1", KeyType.THIRD_PARTY), + new CacheKey("pubcid:pub-1", KeyType.FIRST_PARTY)); + } + + private static Eid eid(String source, String id) { + return Eid.builder().source(source).uids(singletonList(Uid.builder().id(id).build())).build(); + } +} diff --git a/extra/modules/pom.xml b/extra/modules/pom.xml index ccdff21cdec..f9c60f74b9b 100644 --- a/extra/modules/pom.xml +++ b/extra/modules/pom.xml @@ -29,6 +29,7 @@ optable-targeting wurfl-devicedetection live-intent-omni-channel-identity + intentiq-identity diff --git a/sample/configs/prebid-config-with-intentiq.yaml b/sample/configs/prebid-config-with-intentiq.yaml new file mode 100644 index 00000000000..f35a0f39e53 --- /dev/null +++ b/sample/configs/prebid-config-with-intentiq.yaml @@ -0,0 +1,106 @@ +status-response: "ok" +adapters: + generic: + enabled: true + endpoint: http://localhost:9099/bidder + appnexus: + enabled: true + ix: + enabled: true + openx: + enabled: true + pubmatic: + enabled: true + rubicon: + enabled: true +metrics: + prefix: prebid + # JVM/system health (free memory, GC) is collected at the deployment level (JMX/process exporter), + # not by this module — keeps the shippable module free of server-level concerns. + prometheus: + enabled: true + port: 9090 + namespace: prebid + custom-labels-enabled: false +cache: + scheme: http + host: localhost + path: /cache + query: uuid= +settings: + enforce-valid-account: false + generate-storedrequest-bidrequest-id: true + filesystem: + settings-filename: sample/configs/sample-app-settings.yaml + stored-requests-dir: sample + stored-imps-dir: sample + stored-responses-dir: sample/stored + categories-dir: +gdpr: + default-value: 1 + vendorlist: + v2: + cache-dir: /var/tmp/vendor2 + v3: + cache-dir: /var/tmp/vendor3 +admin-endpoints: + logging-changelevel: + enabled: true + path: /logging/changelevel + on-application-port: true + protected: false +hooks: + intentiq-identity: + enabled: true + host-execution-plan: > + { + "endpoints": { + "/openrtb2/auction": { + "stages": { + "processed-auction-request": { + "groups": [ + { + "timeout": 6000, + "hook-sequence": [ + { + "module-code": "intentiq-identity", + "hook-impl-code": "intentiq-identity-processed-auction-request-hook" + } + ] + } + ] + }, + "auction-response": { + "groups": [ + { + "timeout": 6000, + "hook-sequence": [ + { + "module-code": "intentiq-identity", + "hook-impl-code": "intentiq-identity-auction-response-hook" + } + ] + } + ] + } + } + } + } + } + modules: + intentiq-identity: + api-endpoint: http://127.0.0.1:9098/profiles_engine/ProfilesEngineServlet + reports-endpoint: http://127.0.0.1:9098/profiles_engine/ProfilesEngineServlet + partner-id: "383342646" + timeout: 6000 + cache-max-size: 100000 + cache: + enabled: true + ttlseconds: 43200 + # dev: keep the in-progress marker short so a timed-out VR retries soon instead of + # poisoning the device key for 30 min (default 1800s) when the VPN is slow/flapping. + in-progress-ttl-seconds: 30 + redis: + host: 127.0.0.1 + port: 6379 + password: From 02afc1b0fa48eb69596e46c9c9ad4df9ea9585f3 Mon Sep 17 00:00:00 2001 From: Ben Gojanski Date: Thu, 12 Nov 2026 23:33:55 +0200 Subject: [PATCH 2/6] IntentIQ Identity: production-readiness fixes - Parse abTestUuid from the API response (the snake_case mapper read it as ab_test_uuid, so it was always null). - Keep abTestUuid and tc in cache entries, so reports of cache-served auctions carry them. - Treat a non-2xx API answer as an error instead of caching "no ids". - Remove the in-progress marker when a call fails, so the next request retries; default its TTL to 30s. - Bound L2 reads with cache.l2-timeout-ms, so a hung Redis falls through to the API. - Redis: configurable pool size and wait queue (defaults 32 / 1024), TLS, username, password set as an option. - Report only the highest bid per impression, with its own reports-timeout; count non-2xx reports as errors. - Do not add a resolved eid whose source the request already carries. - Ignore an all-zero device.ifa for pcid and cache keys. - Skip the call when partner-id is blank, or when gdpr=1 without a consent string. - Sample failure logs at 1% without stack traces. - Sample config boots on PBS 4.5 (gdpr.vendorlist.live.startup-cache-dir). Co-Authored-By: Claude Opus 5.5 --- extra/modules/intentiq-identity/README.md | 48 ++++- .../intentiq/identity/cache/CacheResult.java | 18 +- .../identity/cache/IdentityCache.java | 60 ++++-- .../identity/cache/IdentityStore.java | 2 + .../identity/cache/RedisIdentityStore.java | 5 + .../config/IntentiqIdentityConfig.java | 16 +- .../metric/IntentiqIdentityMetrics.java | 10 + .../model/config/CacheProperties.java | 15 +- .../config/IntentiqIdentityProperties.java | 4 + .../model/config/RedisProperties.java | 14 ++ .../IntentiqIdentityAuctionResponseHook.java | 50 ++++- ...iqIdentityProcessedAuctionRequestHook.java | 86 ++++++-- .../v1/core/FirstPartyKeyExtractor.java | 14 +- .../identity/cache/IdentityCacheTest.java | 117 +++++++++-- ...tentiqIdentityAuctionResponseHookTest.java | 67 ++++++ ...entityProcessedAuctionRequestHookTest.java | 190 +++++++++++++++++- .../v1/core/FirstPartyKeyExtractorTest.java | 17 ++ .../configs/prebid-config-with-intentiq.yaml | 5 +- 18 files changed, 647 insertions(+), 91 deletions(-) diff --git a/extra/modules/intentiq-identity/README.md b/extra/modules/intentiq-identity/README.md index fc7d50e5d4a..c03e5370df8 100644 --- a/extra/modules/intentiq-identity/README.md +++ b/extra/modules/intentiq-identity/README.md @@ -12,7 +12,7 @@ See the [S2S integration docs](https://s2s.documents.intentiq.com/) for the full ### Execution Plan This module runs at two stages: `processed-auction-request` (enrich `user.eids`) and, optionally, -`auction-response` (report winning bids as impressions to `reports-endpoint`). Enable the module and +`auction-response` (report the winning bid of each impression to `reports-endpoint`). Enable the module and add the hook(s) to the execution plan: ```yaml @@ -46,6 +46,7 @@ hooks: reports-endpoint: https://reports-s2s.intentiq.com/profiles_engine/ProfilesEngineServlet partner-id: "1234567890" timeout: 1000 + reports-timeout: 1000 cache-max-size: 100000 cache: enabled: true @@ -55,14 +56,22 @@ hooks: ttl-ceiling-third-party-seconds: 43200 ttl-ceiling-device-seconds: 3600 negative-ttl-seconds: 120 - in-progress-ttl-seconds: 1800 + in-progress-ttl-seconds: 30 + l2-timeout-ms: 30 redis: host: localhost port: 6379 + max-pool-size: 32 + max-pool-waiting: 1024 ``` Use the region-specific `api-endpoint`: US `be-api-s2s.intentiq.com`, EU `be-api-s2s-gdpr.intentiq.com`, -APAC `be-api-s2s-apac.intentiq.com`. When `api-endpoint` is empty the hook is a no-op. +APAC `be-api-s2s-apac.intentiq.com`, and the matching `reports-endpoint`: US `reports-s2s.intentiq.com`, +EU `reports-s2s-gdpr.intentiq.com`. When `api-endpoint` is empty the hook is a no-op. + +The `processed-auction-request` group timeout bounds how long the auction waits for IntentIQ. Set it +above the real round trip from the PBS host to the API (and `timeout` at or below it); a call that +outlives the group timeout is dropped and the auction proceeds without enrichment. ### Account-level config @@ -78,6 +87,7 @@ Host-level config (above) provides defaults. Account-specific values can be set | reports-endpoint | host | no | string | none | Impression-reporting `ProfilesEngineServlet` URL; blank disables it | | partner-id | account | yes | string | none | Partner token from IntentIQ, sent as the `dpi` query parameter | | timeout | account | no | long | 1000 | HTTP timeout (ms) for the identity-resolution call | +| reports-timeout | account | no | long | 1000 | HTTP timeout (ms) for the fire-and-forget impression report | | cache.enabled | account | no | boolean | false | Use the Caffeine + Redis cache (host must configure `redis.*`) | | cache.ttlseconds | account | no | int | 43200 | Fallback TTL (seconds) when the response omits `cttl` (12h) | | cache.max-keys | account | no | int | 10 | Max alias keys derived per request (caps eid-stuffed requests) | @@ -85,18 +95,29 @@ Host-level config (above) provides defaults. Account-specific values can be set | cache.ttl-ceiling-third-party-seconds | account | no | int | 43200 | Upper bound on TTL for third-party id keys (`intentiq.com`) | | cache.ttl-ceiling-device-seconds | account | no | int | 3600 | Upper bound on TTL for the probabilistic device-composite key | | cache.negative-ttl-seconds | account | no | int | 120 | TTL for the negative (unresolvable id) sentinel | -| cache.in-progress-ttl-seconds | account | no | int | 1800 | TTL for the IN_PROGRESS marker that dedups concurrent resolution calls (matches the backend's 30m window) | +| cache.in-progress-ttl-seconds | account | no | int | 30 | TTL for the IN_PROGRESS marker that dedups concurrent resolution calls; a failed call removes it at once | +| cache.l2-timeout-ms | host | no | long | 30 | Max wait (ms) for an L2 read; a slower or hung store counts as a miss | | cache-max-size | host | no | long | 100000 | Max entries in the local (Caffeine) layer | | metrics-enabled | host | no | boolean | true | Record the module's `custom.*` metrics; set `false` to opt out | | redis.host | host | cond. | string | none | Redis host (required when caching) | | redis.port | host | cond. | int | none | Redis port (required when caching) | -| redis.password | host | no | string | none | Redis password | +| redis.username | host | no | string | none | Redis ACL user | +| redis.password | host | no | string | none | Redis password (any characters; not part of a URL) | +| redis.ssl | host | no | boolean | false | Connect over TLS | +| redis.max-pool-size | host | no | int | 32 | Max pooled Redis connections | +| redis.max-pool-waiting | host | no | int | 1024 | Max commands queued for a free connection before failing fast | The hook sends `at/mi/pt/dpn/srvrReq` constants plus `dpi` (= `partner-id`), and — when present on the request — `ip`, `ipv6`, `uas`, `ref` (site domain / app name), `iiquid` (an existing `intentiq.com` eid), and `pcid`+`idtype` from `device.ifa` (`idtype 4` for MAID/AAID, `idtype 8` for CTV with the id upper-cased; skipped when `device.lmt = 1`). The response `data.eids` are merged into -`user.eids`; on any failure the hook takes no action and the auction proceeds unchanged. +`user.eids`, except a source the request already carries (OpenRTB allows one eid per source, so the +publisher's own eid is kept). On any failure, including a non-2xx answer, the hook takes no action and +the auction proceeds unchanged. + +The call is skipped (no action) when the account has no `partner-id`, or when GDPR applies +(`regs.gdpr = 1`) and the request carries no consent string (`user.consent`): IntentIQ rejects such a +request. The consent string is sent in the `gdpr-consent` header. ### Caching @@ -128,18 +149,21 @@ first-party ceiling), falling back to `cache.negative-ttl-seconds` when `cttl` i (`cache.in-progress-ttl-seconds`) is written under all candidate keys. A concurrent request for the same id reads the marker and skips firing a duplicate call (it proceeds without enrichment); the in-flight call populates the cache for subsequent requests. The marker is overwritten by the resolved -(or negative) entry when the call completes, or expires if it never does. A resolved entry is always +(or negative) entry when the call completes, removed when the call fails (so the next request +retries), or expires if the call never completes. A resolved entry is always preferred over the marker on read. ### Impression reporting When `reports-endpoint` is set and the `auction-response` hook is in the execution plan, the module -reports each winning `seatbid[].bid[]` to the IntentIQ impression API — a fire-and-forget GET to +reports the winning bid of each impression (the highest-priced bid per `impid` across all seats) to +the IntentIQ impression API — a fire-and-forget GET to `?at=45&rtype=1&dpi=&rdata=`. The `rdata` carries `bidderCode` (seat), `cpm`, `currency`, `placementId` (imp id), `biddingPlatformId=4`, `vrref` (site domain / app), `ip`, `ua`, `prebidAuctionId`, and `abTestUuid`. The `abTestUuid` is the one returned by the resolution response — the resolution hook stashes it in the module context and -this hook reads it (omitted on a cache hit, since no fresh IIQ response was produced). With +this hook reads it. On a cache hit it is the `abTestUuid` (and `terminationCause`) of the response +that filled the cache entry. With `reports-endpoint` blank the hook is a no-op. The bid response is never modified. ### Metrics @@ -169,15 +193,17 @@ modules.module.intentiq-identity.custom.cache.l1.in_progress._ # modules.module.intentiq-identity.custom.cache.l2.in_progress._ # in-flight marker from L2; duplicate API call skipped modules.module.intentiq-identity.custom.cache.miss._ # full miss (neither L1 nor L2) -> API called modules.module.intentiq-identity.custom.api.success_ # resolution API responded and parsed OK -modules.module.intentiq-identity.custom.api.error_ # resolution API failed/timed out/unparseable +modules.module.intentiq-identity.custom.api.error_ # resolution API failed/timed out/non-2xx/unparseable modules.module.intentiq-identity.custom.api.latency_ # resolution API call duration (timer; every call) modules.module.intentiq-identity.custom.flow.latency_ # whole-flow latency: enrich hook -> bid release (timer; per auction) modules.module.intentiq-identity.custom.enriched_ # eids added to user.eids (a match) modules.module.intentiq-identity.custom.eids.none_ # resolution produced no eids (pairs with enriched for match rate) modules.module.intentiq-identity.custom.skip.no_endpoint_ # no api-endpoint configured; resolution skipped before any API call +modules.module.intentiq-identity.custom.skip.no_partner # no partner-id configured; resolution skipped +modules.module.intentiq-identity.custom.skip.no_consent_ # regs.gdpr=1 without a consent string; resolution skipped modules.module.intentiq-identity.custom.tc._ # one counter per enumerated termination-cause id modules.module.intentiq-identity.custom.impression.reported_ # winning bid reported to reports-endpoint (overall) -modules.module.intentiq-identity.custom.impression.error_ # impression report call failed +modules.module.intentiq-identity.custom.impression.error_ # impression report call failed or answered non-2xx ``` Shared L1 (Caffeine) / L2 (Redis) health is process-wide, so these are emitted **globally — without diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/CacheResult.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/CacheResult.java index 07e50ee7373..2f5165b64fd 100644 --- a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/CacheResult.java +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/CacheResult.java @@ -15,7 +15,8 @@ *

  • {@link State#MISS} — nothing cached; fetch from the API.
  • * */ -public record CacheResult(State state, List eids, KeyType keyType, Layer layer) { +public record CacheResult(State state, List eids, String abTestUuid, Long terminationCause, + KeyType keyType, Layer layer) { public enum State { HIT, NEGATIVE, @@ -31,18 +32,21 @@ public enum Layer { // keyType is the type of the candidate key that produced the outcome (HIT/NEGATIVE/IN_PROGRESS); // both keyType and layer are null for MISS, where no key/layer matched. - private static final CacheResult MISS = new CacheResult(State.MISS, List.of(), null, null); + // abTestUuid / terminationCause are the values of the IntentIQ response that produced the cached + // entry, so the impression report of a cache-served auction still carries the A/B group and tc. + private static final CacheResult MISS = new CacheResult(State.MISS, List.of(), null, null, null, null); - public static CacheResult hit(List eids, KeyType keyType, Layer layer) { - return new CacheResult(State.HIT, eids, keyType, layer); + public static CacheResult hit(List eids, String abTestUuid, Long terminationCause, + KeyType keyType, Layer layer) { + return new CacheResult(State.HIT, eids, abTestUuid, terminationCause, keyType, layer); } - public static CacheResult negative(KeyType keyType, Layer layer) { - return new CacheResult(State.NEGATIVE, List.of(), keyType, layer); + public static CacheResult negative(String abTestUuid, Long terminationCause, KeyType keyType, Layer layer) { + return new CacheResult(State.NEGATIVE, List.of(), abTestUuid, terminationCause, keyType, layer); } public static CacheResult inProgress(KeyType keyType, Layer layer) { - return new CacheResult(State.IN_PROGRESS, List.of(), keyType, layer); + return new CacheResult(State.IN_PROGRESS, List.of(), null, null, keyType, layer); } public static CacheResult miss() { diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/IdentityCache.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/IdentityCache.java index 1027acc34d5..cb7a6581131 100644 --- a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/IdentityCache.java +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/IdentityCache.java @@ -12,12 +12,14 @@ import org.jspecify.annotations.NonNull; import org.prebid.server.hooks.modules.intentiq.identity.metric.IntentiqIdentityMetrics; import org.prebid.server.json.JacksonMapper; +import org.prebid.server.log.ConditionalLogger; import org.prebid.server.log.Logger; import org.prebid.server.log.LoggerFactory; import java.util.ArrayList; import java.util.List; import java.util.Objects; +import java.util.concurrent.TimeUnit; /** * Dual-layer, multi-key (alias) cache for resolved eids: Caffeine (L1, in-process) backed by a @@ -31,20 +33,24 @@ *

    Entries carry the IntentIQ {@code cttl} capped by a per-{@link KeyType} ceiling (see * {@link CacheTtlPolicy}). Unresolvable ids are cached as a short-lived negative sentinel so they do * not re-hit the upstream API. L2 failures are swallowed (fail-open) so the auction can fall through - * to a live call. Differing resolutions are never merged — only the single winning entry propagates. + * to a live call, and every L2 read is bounded by {@code l2TimeoutMs} so a hung store cannot hold the + * auction. Differing resolutions are never merged — only the single winning entry propagates. */ public class IdentityCache { private static final Logger logger = LoggerFactory.getLogger(IdentityCache.class); + private static final ConditionalLogger conditionalLogger = new ConditionalLogger(logger); + private static final double LOG_SAMPLING_RATE = 0.01; private final Cache local; private final IdentityStore store; private final JacksonMapper mapper; private final CacheTtlPolicy ttlPolicy; private final IntentiqIdentityMetrics metrics; + private final long l2TimeoutMs; public IdentityCache(long maxSize, CacheTtlPolicy ttlPolicy, IdentityStore store, JacksonMapper mapper, - IntentiqIdentityMetrics metrics) { + IntentiqIdentityMetrics metrics, long l2TimeoutMs) { this.local = Caffeine.newBuilder() .maximumSize(maxSize) .expireAfter(new CacheEntryExpiry()) @@ -54,6 +60,7 @@ public IdentityCache(long maxSize, CacheTtlPolicy ttlPolicy, IdentityStore store this.store = Objects.requireNonNull(store); this.mapper = Objects.requireNonNull(mapper); this.metrics = Objects.requireNonNull(metrics); + this.l2TimeoutMs = l2TimeoutMs; // L1 capacity gauges: current size (vs cache-max-size) and cumulative evictions. metrics.registerL1Gauges(local::estimatedSize, () -> local.stats().evictionCount()); } @@ -111,17 +118,19 @@ public Future get(List keys) { }); } - public void put(List keys, List eids, long cttlMs) { + public void put(List keys, List eids, String abTestUuid, Long terminationCause, long cttlMs) { for (CacheKey key : keys) { final long ttl = ttlPolicy.effectiveTtlMs(key.type(), cttlMs); - writeBoth(key.key(), CacheEntry.of(eids, false, false, System.currentTimeMillis() + ttl), ttl); + writeBoth(key.key(), CacheEntry.of(eids, false, false, System.currentTimeMillis() + ttl, + abTestUuid, terminationCause), ttl); } } - public void putNegative(List keys, long cttlMs) { + public void putNegative(List keys, String abTestUuid, Long terminationCause, long cttlMs) { final long ttl = ttlPolicy.negativeTtlMs(cttlMs); for (CacheKey key : keys) { - writeBoth(key.key(), CacheEntry.of(List.of(), true, false, System.currentTimeMillis() + ttl), ttl); + writeBoth(key.key(), CacheEntry.of(List.of(), true, false, System.currentTimeMillis() + ttl, + abTestUuid, terminationCause), ttl); } } @@ -134,7 +143,27 @@ public void putNegative(List keys, long cttlMs) { public void putInProgress(List keys) { final long ttl = ttlPolicy.inProgressTtlMs(); for (CacheKey key : keys) { - writeBoth(key.key(), CacheEntry.of(List.of(), false, true, System.currentTimeMillis() + ttl), ttl); + writeBoth(key.key(), CacheEntry.of(List.of(), false, true, System.currentTimeMillis() + ttl, + null, null), ttl); + } + } + + /** + * Remove the IN_PROGRESS markers of a resolution call that failed (timeout, transport error, + * non-2xx, unparsable body), so the next request for the same user retries instead of being + * skipped until the marker expires. + */ + public void clearInProgress(List keys) { + for (CacheKey key : keys) { + final CacheEntry entry = l1Get(key.key()); + if (entry != null && entry.isInProgress()) { + local.invalidate(key.key()); + } + store.delete(key.key()).onFailure(throwable -> { + metrics.l2PutError(); + conditionalLogger.warn("IntentIQ identity cache L2 DELETE failed: " + throwable.getMessage(), + LOG_SAMPLING_RATE); + }); } } @@ -155,7 +184,7 @@ private void backfill(List keys, int hitIndex, CacheEntry hit) { } final long ttl = Math.min(remaining, ttlPolicy.ceilingFor(key.type())); writeBoth(key.key(), CacheEntry.of(hit.getEids(), hit.isNegative(), hit.isInProgress(), - System.currentTimeMillis() + ttl), ttl); + System.currentTimeMillis() + ttl, hit.getAbTestUuid(), hit.getTc()), ttl); } } @@ -166,7 +195,8 @@ private void writeBoth(String key, CacheEntry entry, long ttlMs) { .onComplete(ignored -> metrics.l2PutLatency(System.nanoTime() - startNanos)) .onFailure(throwable -> { metrics.l2PutError(); - logger.warn("IntentIQ identity cache L2 PUT failed", throwable); + conditionalLogger.warn("IntentIQ identity cache L2 PUT failed: " + throwable.getMessage(), + LOG_SAMPLING_RATE); }); } @@ -196,13 +226,15 @@ private Future> l2GetAll(List keys) { for (CacheKey key : keys) { final long startNanos = System.nanoTime(); futures.add(store.get(key.key()) + .timeout(l2TimeoutMs, TimeUnit.MILLISECONDS) .onComplete(ignored -> metrics.l2GetLatency(System.nanoTime() - startNanos)) .map(this::decodeValid) .otherwise(throwable -> { // L2 read failed: we fall through to a live API call (fail open). Count it so the // otherwise-swallowed failure is visible for alerting. metrics.l2GetError(); - logger.warn("IntentIQ identity cache L2 GET failed", throwable); + conditionalLogger.warn("IntentIQ identity cache L2 GET failed: " + throwable.getMessage(), + LOG_SAMPLING_RATE); return null; })); } @@ -225,8 +257,8 @@ private static CacheResult toResult(CacheEntry entry, KeyType keyType, CacheResu return CacheResult.inProgress(keyType, layer); } return entry.isNegative() - ? CacheResult.negative(keyType, layer) - : CacheResult.hit(entry.getEids(), keyType, layer); + ? CacheResult.negative(entry.getAbTestUuid(), entry.getTc(), keyType, layer) + : CacheResult.hit(entry.getEids(), entry.getAbTestUuid(), entry.getTc(), keyType, layer); } @Data @@ -241,6 +273,10 @@ static class CacheEntry { boolean inProgress; long exp; + + String abTestUuid; + + Long tc; } private static class CacheEntryExpiry implements Expiry { diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/IdentityStore.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/IdentityStore.java index dd589e02629..3031e59bc35 100644 --- a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/IdentityStore.java +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/IdentityStore.java @@ -13,4 +13,6 @@ public interface IdentityStore { Future get(String key); Future put(String key, String value, long ttlMs); + + Future delete(String key); } diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/RedisIdentityStore.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/RedisIdentityStore.java index 5ee618c3ac3..452c1feb3ce 100644 --- a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/RedisIdentityStore.java +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/RedisIdentityStore.java @@ -27,6 +27,11 @@ public Future put(String key, String value, long ttlMs) { return redis.set(List.of(key, value, "PX", Long.toString(ttlMs))).mapEmpty(); } + @Override + public Future delete(String key) { + return redis.del(List.of(key)).mapEmpty(); + } + /** Current key count of the selected DB ({@code DBSIZE}). Instance-wide, not module-scoped. */ public Future dbSize() { return redis.dbsize().map(response -> response != null ? response.toLong() : 0L); diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/config/IntentiqIdentityConfig.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/config/IntentiqIdentityConfig.java index 87183d02546..3812f208f9c 100644 --- a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/config/IntentiqIdentityConfig.java +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/config/IntentiqIdentityConfig.java @@ -79,7 +79,8 @@ IdentityCache intentiqIdentityCache(IntentiqIdentityProperties properties, cache.getNegativeTtlSeconds() * 1000L, cache.getInProgressTtlSeconds() * 1000L); - return new IdentityCache(properties.getCacheMaxSize(), ttlPolicy, identityStore, mapper, metrics); + return new IdentityCache( + properties.getCacheMaxSize(), ttlPolicy, identityStore, mapper, metrics, cache.getL2TimeoutMs()); } /** @@ -137,9 +138,18 @@ private static RedisAPI createRedisApi(RedisProperties redis, Vertx vertx) { + ".redis.port is required when cache is enabled"); } - final String credentials = StringUtils.isNotBlank(redis.getPassword()) ? ":" + redis.getPassword() + "@" : ""; + // Credentials are set as options, not in the connection string, so a password with URL-reserved + // characters (@, :, /, %) is not mangled. final RedisOptions options = new RedisOptions() - .setConnectionString("redis://" + credentials + redis.getHost() + ":" + redis.getPort()); + .setConnectionString((redis.isSsl() ? "rediss://" : "redis://") + redis.getHost() + ":" + redis.getPort()) + .setMaxPoolSize(redis.getMaxPoolSize()) + .setMaxPoolWaiting(redis.getMaxPoolWaiting()); + if (StringUtils.isNotBlank(redis.getUsername())) { + options.setUser(redis.getUsername()); + } + if (StringUtils.isNotBlank(redis.getPassword())) { + options.setPassword(redis.getPassword()); + } return RedisAPI.api(Redis.createClient(vertx, options)); } diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/metric/IntentiqIdentityMetrics.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/metric/IntentiqIdentityMetrics.java index 2ed890e73ff..15ac3f8a0a2 100644 --- a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/metric/IntentiqIdentityMetrics.java +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/metric/IntentiqIdentityMetrics.java @@ -73,6 +73,16 @@ public void skipNoEndpoint(String dpi) { inc("skip.no_endpoint", dpi); } + /** Resolution skipped because the account has no partner-id (dpi) configured. */ + public void skipNoPartner(String dpi) { + inc("skip.no_partner", dpi); + } + + /** Resolution skipped because GDPR applies (regs.gdpr=1) and the request carries no consent string. */ + public void skipNoConsent(String dpi) { + inc("skip.no_consent", dpi); + } + public void terminationCause(long tc, String dpi) { if (tc >= 0 && tc < 200) { inc("tc." + tc, dpi); diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/model/config/CacheProperties.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/model/config/CacheProperties.java index a2241ffc241..59393e1a85d 100644 --- a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/model/config/CacheProperties.java +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/model/config/CacheProperties.java @@ -34,10 +34,17 @@ public class CacheProperties { private int negativeTtlSeconds = 120; /** - * TTL for the IN_PROGRESS marker written while a resolution call is in flight, after which the - * marker expires even if the call never completed. Defaults to 30 minutes to match the IntentIQ - * backend's in-progress window; lower it if you want a failed resolution to be retried sooner. + * TTL for the IN_PROGRESS marker written while a resolution call is in flight. A failed call + * removes its marker at once; this TTL only bounds a marker whose call never completed (e.g. the + * server stopped mid-call), so it needs to outlive the API timeout and little more. */ @JsonProperty("in-progress-ttl-seconds") - private int inProgressTtlSeconds = 1800; + private int inProgressTtlSeconds = 30; + + /** + * Upper bound on an L2 (shared store) read. A slower or hung store is treated as a miss and the + * request falls through to a live API call instead of waiting for the hook timeout. + */ + @JsonProperty("l2-timeout-ms") + private long l2TimeoutMs = 30; } diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/model/config/IntentiqIdentityProperties.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/model/config/IntentiqIdentityProperties.java index 1fad945c41f..066c7ed7b56 100644 --- a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/model/config/IntentiqIdentityProperties.java +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/model/config/IntentiqIdentityProperties.java @@ -19,6 +19,10 @@ public final class IntentiqIdentityProperties { Long timeout; + /** Timeout of the fire-and-forget impression report; it does not hold the auction. */ + @JsonProperty("reports-timeout") + Long reportsTimeout; + CacheProperties cache = new CacheProperties(); RedisProperties redis; diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/model/config/RedisProperties.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/model/config/RedisProperties.java index 6d2f678a01a..ac89726f174 100644 --- a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/model/config/RedisProperties.java +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/model/config/RedisProperties.java @@ -1,5 +1,6 @@ package org.prebid.server.hooks.modules.intentiq.identity.model.config; +import com.fasterxml.jackson.annotation.JsonProperty; import lombok.Data; @Data @@ -9,5 +10,18 @@ public final class RedisProperties { Integer port; + String username; + String password; + + /** Connect over TLS (e.g. ElastiCache / Memorystore with in-transit encryption). */ + boolean ssl; + + /** Max pooled connections. The Vert.x default (6) exhausts at a few hundred auctions per second. */ + @JsonProperty("max-pool-size") + int maxPoolSize = 32; + + /** Max commands queued for a free connection before a command fails fast. */ + @JsonProperty("max-pool-waiting") + int maxPoolWaiting = 1024; } diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityAuctionResponseHook.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityAuctionResponseHook.java index bdbc9154d1d..b77eefabc2e 100644 --- a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityAuctionResponseHook.java +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityAuctionResponseHook.java @@ -25,6 +25,7 @@ import org.prebid.server.hooks.v1.auction.AuctionResponseHook; import org.prebid.server.hooks.v1.auction.AuctionResponsePayload; import org.prebid.server.json.JacksonMapper; +import org.prebid.server.log.ConditionalLogger; import org.prebid.server.log.Logger; import org.prebid.server.log.LoggerFactory; import org.prebid.server.util.HttpUtil; @@ -38,13 +39,16 @@ import java.util.Objects; /** - * Runs at the {@code auction-response} stage and reports each winning bid to the IntentIQ S2S - * impression-reporting API (fire-and-forget GET to {@code reports-endpoint}). The {@code abTestUuid} + * Runs at the {@code auction-response} stage and reports the winning bid of each impression (the + * highest price across all seats) to the IntentIQ S2S impression-reporting API (fire-and-forget GET + * to {@code reports-endpoint}). The {@code abTestUuid} * produced by the resolution hook is read from the module context. The bid response is never modified. */ public class IntentiqIdentityAuctionResponseHook implements AuctionResponseHook { private static final Logger logger = LoggerFactory.getLogger(IntentiqIdentityAuctionResponseHook.class); + private static final ConditionalLogger conditionalLogger = new ConditionalLogger(logger); + private static final double LOG_SAMPLING_RATE = 0.01; public static final String CODE = "intentiq-identity-auction-response-hook"; @@ -92,13 +96,34 @@ public Future> call(AuctionResponsePayl final Long terminationCause = context != null ? context.terminationCause() : null; final String currency = bidResponse.getCur() != null ? bidResponse.getCur() : DEFAULT_CURRENCY; + for (final SeatBidRef winner : winningBids(bidResponse)) { + report(properties, bidRequest, winner.seat(), winner.bid(), currency, abTestUuid, terminationCause); + } + + return Future.succeededFuture(noAction()); + } + + // The response holds every seat's bid for an impression, but only one of them can render: report the + // highest-priced bid per impid (first seen wins a tie), so impressions and revenue are not overcounted. + private static List winningBids(BidResponse bidResponse) { + final Map winnerByImp = new LinkedHashMap<>(); for (final SeatBid seatBid : nullSafe(bidResponse.getSeatbid())) { for (final Bid bid : nullSafe(seatBid.getBid())) { - report(properties, bidRequest, seatBid.getSeat(), bid, currency, abTestUuid, terminationCause); + if (bid == null || bid.getPrice() == null) { + continue; + } + winnerByImp.merge(StringUtils.defaultString(bid.getImpid()), new SeatBidRef(seatBid.getSeat(), bid), + IntentiqIdentityAuctionResponseHook::higherPrice); } } + return List.copyOf(winnerByImp.values()); + } - return Future.succeededFuture(noAction()); + private static SeatBidRef higherPrice(SeatBidRef current, SeatBidRef candidate) { + return candidate.bid().getPrice().compareTo(current.bid().getPrice()) > 0 ? candidate : current; + } + + private record SeatBidRef(String seat, Bid bid) { } private void report(IntentiqIdentityProperties properties, @@ -133,12 +158,23 @@ private void report(IntentiqIdentityProperties properties, final String dpi = properties.getPartnerId(); - final long timeout = properties.getTimeout() != null ? properties.getTimeout() : DEFAULT_TIMEOUT_MS; + final long timeout = properties.getReportsTimeout() != null + ? properties.getReportsTimeout() + : DEFAULT_TIMEOUT_MS; httpClient.get(reportUrl(properties, rdata), HttpUtil.headers(), timeout) - .onSuccess(response -> metrics.impressionReported(dpi)) + .onSuccess(response -> { + if (response.getStatusCode() >= 200 && response.getStatusCode() < 300) { + metrics.impressionReported(dpi); + } else { + metrics.impressionError(dpi); + conditionalLogger.warn("IntentIQ impression report answered HTTP " + + response.getStatusCode(), LOG_SAMPLING_RATE); + } + }) .onFailure(throwable -> { metrics.impressionError(dpi); - logger.warn("IntentIQ impression report failed", throwable); + conditionalLogger.warn("IntentIQ impression report failed: " + throwable.getMessage(), + LOG_SAMPLING_RATE); }); } diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityProcessedAuctionRequestHook.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityProcessedAuctionRequestHook.java index a4cb1e6edd0..ba9d88aae12 100644 --- a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityProcessedAuctionRequestHook.java +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityProcessedAuctionRequestHook.java @@ -1,5 +1,6 @@ package org.prebid.server.hooks.modules.intentiq.identity.v1; +import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JsonDeserializer; @@ -40,6 +41,7 @@ import org.prebid.server.hooks.v1.auction.AuctionRequestPayload; import org.prebid.server.hooks.v1.auction.ProcessedAuctionRequestHook; import org.prebid.server.json.JacksonMapper; +import org.prebid.server.log.ConditionalLogger; import org.prebid.server.log.Logger; import org.prebid.server.log.LoggerFactory; import org.prebid.server.util.HttpUtil; @@ -49,11 +51,13 @@ import java.net.URLEncoder; import java.nio.charset.StandardCharsets; import java.util.ArrayList; +import java.util.HashSet; import java.util.LinkedHashMap; import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Objects; +import java.util.Set; import java.util.TreeMap; import java.util.stream.Collectors; @@ -61,6 +65,8 @@ public class IntentiqIdentityProcessedAuctionRequestHook implements ProcessedAuc private static final Logger logger = LoggerFactory.getLogger(IntentiqIdentityProcessedAuctionRequestHook.class); + private static final ConditionalLogger conditionalLogger = new ConditionalLogger(logger); + private static final double LOG_SAMPLING_RATE = 0.01; public static final String CODE = "intentiq-identity-processed-auction-request-hook"; @@ -106,8 +112,17 @@ public Future> call(AuctionRequestPayloa metrics.skipNoEndpoint(dpi); return Future.succeededFuture(noAction(new IntentiqIdentityModuleContext(startNanos, null, null))); } + if (StringUtils.isBlank(dpi)) { + metrics.skipNoPartner(dpi); + return Future.succeededFuture(noAction(new IntentiqIdentityModuleContext(startNanos, null, null))); + } + final BidRequest bidRequest = auctionRequestPayload.bidRequest(); + if (isGdprWithoutConsent(bidRequest)) { + metrics.skipNoConsent(dpi); + return Future.succeededFuture(noAction(new IntentiqIdentityModuleContext(startNanos, null, null))); + } - return resolveEids(properties, auctionRequestPayload.bidRequest()) + return resolveEids(properties, bidRequest) .map(resolution -> { final List eids = resolution.eids(); final IntentiqIdentityModuleContext context = new IntentiqIdentityModuleContext( @@ -120,7 +135,8 @@ public Future> call(AuctionRequestPayloa return update(payload -> enrichUserEids(payload, eids), context); }) .otherwise(throwable -> { - logger.warn("IntentIQ identity resolution failed, proceeding without enrichment", throwable); + conditionalLogger.warn("IntentIQ identity resolution failed, proceeding without enrichment: " + + throwable.getMessage(), LOG_SAMPLING_RATE); metrics.apiError(dpi); return noAction(new IntentiqIdentityModuleContext(startNanos, null, null)); }); @@ -143,7 +159,8 @@ private Future resolveEids(IntentiqIdentityProperties properties, Bi return cache.get(keys).compose(result -> switch (result.state()) { case HIT -> { metrics.cacheHit(layerToken(result.layer()), keyTypeToken(result.keyType()), dpi); - yield Future.succeededFuture(new Resolution(result.eids(), null, null)); + yield Future.succeededFuture( + new Resolution(result.eids(), result.abTestUuid(), result.terminationCause())); } case NEGATIVE -> { // A negative sentinel is a cached miss (id is known-unresolvable): no identity to @@ -152,7 +169,8 @@ private Future resolveEids(IntentiqIdentityProperties properties, Bi final String type = keyTypeToken(result.keyType()); metrics.cacheMiss(type, dpi); metrics.cacheNegativeHit(layerToken(result.layer()), type, dpi); - yield Future.succeededFuture(new Resolution(null, null, null)); + yield Future.succeededFuture( + new Resolution(null, result.abTestUuid(), result.terminationCause())); } case IN_PROGRESS -> { // A resolution call for this id is already in flight; skip firing a duplicate and @@ -181,15 +199,19 @@ private static String layerToken(CacheResult.Layer layer) { private Future fetchAndCache(IntentiqIdentityProperties properties, BidRequest bidRequest, List keys) { - return fetch(properties, bidRequest).map(response -> { - final List eids = extractEids(response); - if (eids != null && !eids.isEmpty()) { - cache.put(keys, eids, response.getCttl() != null ? response.getCttl() : 0L); - } else { - cache.putNegative(keys, response.getCttl() != null ? response.getCttl() : 0L); - } - return new Resolution(eids, response.getAbTestUuid(), response.getTc()); - }); + return fetch(properties, bidRequest) + .map(response -> { + final List eids = extractEids(response); + final long cttl = response.getCttl() != null ? response.getCttl() : 0L; + if (eids != null && !eids.isEmpty()) { + cache.put(keys, eids, response.getAbTestUuid(), response.getTc(), cttl); + } else { + cache.putNegative(keys, response.getAbTestUuid(), response.getTc(), cttl); + } + return new Resolution(eids, response.getAbTestUuid(), response.getTc()); + }) + // A failed call caches nothing: drop its in-progress marker so the next request retries. + .onFailure(ignored -> cache.clearInProgress(keys)); } private Future fetch(IntentiqIdentityProperties properties, BidRequest bidRequest) { @@ -197,9 +219,16 @@ private Future fetch(IntentiqIdentityProperties properties, BidRequ final long startNanos = System.nanoTime(); return httpClient.get(resolveUrl(properties, bidRequest), resolveHeaders(bidRequest), timeout) .map(response -> { + final int status = response.getStatusCode(); + if (status < 200 || status >= 300) { + throw new IllegalStateException("IntentIQ API answered HTTP " + status); + } final IiqResponse parsed = mapper.decodeValue(response.getBody(), IiqResponse.class); + if (parsed == null) { + throw new IllegalStateException("IntentIQ API answered an empty body"); + } metrics.apiSuccess(properties.getPartnerId()); - if (parsed != null && parsed.getTc() != null) { + if (parsed.getTc() != null) { metrics.terminationCause(parsed.getTc(), properties.getPartnerId()); } return parsed; @@ -237,6 +266,14 @@ private String resolveUrl(IntentiqIdentityProperties properties, BidRequest bidR return url.toString(); } + // Under GDPR the IntentIQ API rejects a request without a TCF consent string, so do not send one. + private static boolean isGdprWithoutConsent(BidRequest bidRequest) { + final Regs regs = bidRequest.getRegs(); + return regs != null + && "1".equals(resolveGdpr(regs)) + && StringUtils.isBlank(resolveConsent(bidRequest.getUser())); + } + private static void appendConsent(StringBuilder url, BidRequest bidRequest) { final Regs regs = bidRequest.getRegs(); if (regs != null) { @@ -354,7 +391,8 @@ private static String quote(String value) { private static void appendDeviceId(StringBuilder url, Device device) { final String ifa = device.getIfa(); - if (StringUtils.isBlank(ifa) || Integer.valueOf(1).equals(device.getLmt())) { + if (StringUtils.isBlank(ifa) || Integer.valueOf(1).equals(device.getLmt()) + || FirstPartyKeyExtractor.isZeroIfa(ifa)) { return; } @@ -402,11 +440,23 @@ private static AuctionRequestPayload enrichUserEids(AuctionRequestPayload payloa final BidRequest bidRequest = payload.bidRequest(); final User existingUser = bidRequest.getUser() != null ? bidRequest.getUser() : User.builder().build(); + // An eid source the request already carries is kept as sent: OpenRTB allows one eid per source, + // so a resolved eid of the same source (e.g. the page's own intentiq.com id) is not added again. final List mergedEids = new ArrayList<>(); + final Set sources = new HashSet<>(); if (existingUser.getEids() != null) { - mergedEids.addAll(existingUser.getEids()); + for (Eid eid : existingUser.getEids()) { + mergedEids.add(eid); + if (eid != null && eid.getSource() != null) { + sources.add(eid.getSource()); + } + } + } + for (Eid eid : resolvedEids) { + if (eid != null && (eid.getSource() == null || sources.add(eid.getSource()))) { + mergedEids.add(eid); + } } - mergedEids.addAll(resolvedEids); final User enrichedUser = existingUser.toBuilder().eids(mergedEids).build(); return AuctionRequestPayloadImpl.of(bidRequest.toBuilder().user(enrichedUser).build()); @@ -447,6 +497,8 @@ static class IiqResponse { Long cttl; + // The PBS mapper is snake_case; IntentIQ sends this field camelCase. + @JsonProperty("abTestUuid") String abTestUuid; Long tc; diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/FirstPartyKeyExtractor.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/FirstPartyKeyExtractor.java index 412ee620069..77a611332ff 100644 --- a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/FirstPartyKeyExtractor.java +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/FirstPartyKeyExtractor.java @@ -74,8 +74,17 @@ private static void addEidKeys(List keys, .forEach(id -> keys.add(new CacheKey(namespace + ":" + id, type))); } + /** + * An all-zero ifa (iOS without ATT consent, Android with the ad id deleted) is a placeholder, not a + * device id: every such device sends the same value, so it must never be sent or used as a key. + */ + public static boolean isZeroIfa(String ifa) { + return ifa != null && !ifa.isEmpty() && ifa.chars().allMatch(c -> c == '0' || c == '-'); + } + private static void addMaidKey(List keys, Device device) { - if (device == null || StringUtils.isBlank(device.getIfa()) || Integer.valueOf(1).equals(device.getLmt())) { + if (device == null || StringUtils.isBlank(device.getIfa()) || Integer.valueOf(1).equals(device.getLmt()) + || isZeroIfa(device.getIfa())) { return; } final Integer deviceType = device.getDevicetype(); @@ -112,7 +121,8 @@ private static void addDeviceComposite(List keys, Device device) { final String ip = StringUtils.isNotBlank(device.getIp()) ? device.getIp() : device.getIpv6(); // Normalized UA fields (not the raw UA string) to avoid fragmenting the cache per request. final String ua = DeviceUserAgent.normalize(device.getUa()); - final String composite = Stream.of(device.getIfa(), StringUtils.trimToNull(ua), ip) + final String ifa = isZeroIfa(device.getIfa()) ? null : device.getIfa(); + final String composite = Stream.of(ifa, StringUtils.trimToNull(ua), ip) .filter(StringUtils::isNotBlank) .collect(Collectors.joining("_")); if (!composite.isEmpty()) { diff --git a/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/cache/IdentityCacheTest.java b/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/cache/IdentityCacheTest.java index 5774bbaf14c..b018b79056d 100644 --- a/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/cache/IdentityCacheTest.java +++ b/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/cache/IdentityCacheTest.java @@ -4,6 +4,7 @@ import com.iab.openrtb.request.Eid; import com.iab.openrtb.request.Uid; import io.vertx.core.Future; +import io.vertx.core.Promise; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -14,6 +15,7 @@ import org.prebid.server.json.ObjectMapperProvider; import java.util.List; +import java.util.concurrent.TimeUnit; import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; @@ -53,14 +55,14 @@ public class IdentityCacheTest { @BeforeEach public void setUp() { metricRegistry = new MetricRegistry(); - target = new IdentityCache(100L, POLICY, store, MAPPER, new IntentiqIdentityMetrics(metricRegistry)); + target = new IdentityCache(100L, POLICY, store, MAPPER, new IntentiqIdentityMetrics(metricRegistry), 30L); } @Test public void getShouldReturnFromLocalCacheWithoutQueryingStoreAfterPut() { // given when(store.put(any(), any(), anyLong())).thenReturn(Future.succeededFuture()); - target.put(List.of(IIQ), eids, 1000L); + target.put(List.of(IIQ), eids, null, null, 1000L); // when final CacheResult result = target.get(List.of(IIQ)).result(); @@ -75,10 +77,10 @@ public void getShouldReturnFromLocalCacheWithoutQueryingStoreAfterPut() { public void putShouldRefreshExpiryWhenSameKeyWrittenAgain() { // given when(store.put(any(), any(), anyLong())).thenReturn(Future.succeededFuture()); - target.put(List.of(IIQ), eids, 1000L); + target.put(List.of(IIQ), eids, null, null, 1000L); // when — a second write to the same key updates the existing L1 entry (exercises expireAfterUpdate) - target.put(List.of(IIQ), eids, 5000L); + target.put(List.of(IIQ), eids, null, null, 5000L); // then final CacheResult result = target.get(List.of(IIQ)).result(); @@ -92,7 +94,7 @@ public void putShouldWriteToStoreWithEffectiveTtlFromCttl() { when(store.put(any(), any(), anyLong())).thenReturn(Future.succeededFuture()); // when — cttl below every ceiling is used verbatim - target.put(List.of(IIQ), eids, 1000L); + target.put(List.of(IIQ), eids, null, null, 1000L); // then verify(store).put(eq("iiq:uid-1"), any(), eq(1000L)); @@ -104,7 +106,7 @@ public void putShouldUseDefaultTtlWhenCttlIsNotPositive() { when(store.put(any(), any(), anyLong())).thenReturn(Future.succeededFuture()); // when — third-party ceiling (12h) equals default, so default applies - target.put(List.of(IIQ), eids, 0L); + target.put(List.of(IIQ), eids, null, null, 0L); // then verify(store).put(eq("iiq:uid-1"), any(), eq(43_200_000L)); @@ -116,7 +118,7 @@ public void putShouldCapCttlAtPerTypeCeiling() { when(store.put(any(), any(), anyLong())).thenReturn(Future.succeededFuture()); // when — huge cttl on a device key is capped at the 1h device ceiling - target.put(List.of(DEV), eids, 999_999_999L); + target.put(List.of(DEV), eids, null, null, 999_999_999L); // then verify(store).put(eq("dev:ifa_ua_ip"), any(), eq(3_600_000L)); @@ -128,7 +130,7 @@ public void putShouldWriteEntryUnderEveryKey() { when(store.put(any(), any(), anyLong())).thenReturn(Future.succeededFuture()); // when - target.put(List.of(IIQ, PUBCID), eids, 1000L); + target.put(List.of(IIQ, PUBCID), eids, null, null, 1000L); // then verify(store).put(eq("iiq:uid-1"), any(), eq(1000L)); @@ -233,7 +235,7 @@ public void getShouldReturnHighestPriorityHitAndBackfillMissedKeys() { public void getShouldBackfillFromLocalHitUnderOtherKeys() { // given — IIQ already in local cache, PUBCID not present anywhere when(store.put(any(), any(), anyLong())).thenReturn(Future.succeededFuture()); - target.put(List.of(IIQ), eids, 60_000L); + target.put(List.of(IIQ), eids, null, null, 60_000L); // when — lookup with both keys hits IIQ in L1 and back-fills PUBCID final CacheResult result = target.get(List.of(IIQ, PUBCID)).result(); @@ -247,7 +249,7 @@ public void getShouldBackfillFromLocalHitUnderOtherKeys() { public void putNegativeShouldYieldNegativeResultWithoutQueryingStore() { // given when(store.put(any(), any(), anyLong())).thenReturn(Future.succeededFuture()); - target.putNegative(List.of(IIQ), 0L); + target.putNegative(List.of(IIQ), null, null, 0L); // when final CacheResult result = target.get(List.of(IIQ)).result(); @@ -263,7 +265,7 @@ public void putNegativeShouldWriteSentinelUnderAllKeysWithDefaultTtlWhenCttlAbse when(store.put(any(), any(), anyLong())).thenReturn(Future.succeededFuture()); // when - target.putNegative(List.of(IIQ, PUBCID), 0L); + target.putNegative(List.of(IIQ, PUBCID), null, null, 0L); // then verify(store).put(eq("iiq:uid-1"), any(), eq(120_000L)); @@ -276,7 +278,7 @@ public void putNegativeShouldHonorResponseCttlAsSuppressionTtl() { when(store.put(any(), any(), anyLong())).thenReturn(Future.succeededFuture()); // when - target.putNegative(List.of(IIQ), 600_000L); + target.putNegative(List.of(IIQ), null, null, 600_000L); // then verify(store).put(eq("iiq:uid-1"), any(), eq(600_000L)); @@ -288,7 +290,7 @@ public void putNegativeShouldCapResponseCttlAtFirstPartyCeiling() { when(store.put(any(), any(), anyLong())).thenReturn(Future.succeededFuture()); // when — an absurd cttl is bounded by the first-party ceiling (24h) - target.putNegative(List.of(IIQ), 999_999_999_999L); + target.putNegative(List.of(IIQ), null, null, 999_999_999_999L); // then verify(store).put(eq("iiq:uid-1"), any(), eq(86_400_000L)); @@ -313,7 +315,7 @@ public void putShouldStoreLocallyEvenWhenStorePutFails() { when(store.put(any(), any(), anyLong())).thenReturn(Future.failedFuture(new RuntimeException("down"))); // when - target.put(List.of(IIQ), eids, 1000L); + target.put(List.of(IIQ), eids, null, null, 1000L); // then — local layer still serves the value without touching the store assertThat(target.get(List.of(IIQ)).result().eids()).isEqualTo(eids); @@ -397,7 +399,7 @@ public void putShouldRecordStorePutErrorAndLatencyWhenL2PutFails() { when(store.put(any(), any(), anyLong())).thenReturn(Future.failedFuture(new RuntimeException("down"))); // when - target.put(List.of(IIQ), eids, 1000L); + target.put(List.of(IIQ), eids, null, null, 1000L); // then assertThat(counter("l2.put.error")).isEqualTo(1); @@ -410,13 +412,90 @@ public void shouldExposeL1SizeGaugeReflectingCachedEntries() { when(store.put(any(), any(), anyLong())).thenReturn(Future.succeededFuture()); // when - target.put(List.of(IIQ), eids, 60_000L); + target.put(List.of(IIQ), eids, null, null, 60_000L); // then — the L1 size gauge is registered and reflects the written entry assertThat(gauge("l1.size")).isEqualTo(1L); assertThat(metricRegistry.getGauges()).containsKey(metricName("l1.eviction")); } + @Test + public void hitShouldCarryAbTestUuidAndTerminationCauseOfTheCachedResponse() { + // given + when(store.put(any(), any(), anyLong())).thenReturn(Future.succeededFuture()); + target.put(List.of(IIQ), eids, "S2S_ab", 0L, 60_000L); + + // when + final CacheResult result = target.get(List.of(IIQ)).result(); + + // then + assertThat(result.state()).isEqualTo(CacheResult.State.HIT); + assertThat(result.abTestUuid()).isEqualTo("S2S_ab"); + assertThat(result.terminationCause()).isEqualTo(0L); + } + + @Test + public void storeHitShouldCarryAbTestUuidAndTerminationCause() { + // given + lenient().when(store.put(any(), any(), anyLong())).thenReturn(Future.succeededFuture()); + when(store.get("iiq:uid-1")).thenReturn(Future.succeededFuture(MAPPER.encodeToString( + IdentityCache.CacheEntry.of(List.of(), true, false, System.currentTimeMillis() + 60_000L, + "S2S_neg", 41L)))); + + // when + final CacheResult result = target.get(List.of(IIQ)).result(); + + // then + assertThat(result.state()).isEqualTo(CacheResult.State.NEGATIVE); + assertThat(result.abTestUuid()).isEqualTo("S2S_neg"); + assertThat(result.terminationCause()).isEqualTo(41L); + } + + @Test + public void clearInProgressShouldDropMarkerFromBothLayers() { + // given + when(store.put(any(), any(), anyLong())).thenReturn(Future.succeededFuture()); + when(store.delete(any())).thenReturn(Future.succeededFuture()); + when(store.get(any())).thenReturn(Future.succeededFuture(null)); + target.putInProgress(List.of(IIQ, PUBCID)); + + // when + target.clearInProgress(List.of(IIQ, PUBCID)); + + // then — the next lookup is a miss, so the request retries the API + assertThat(target.get(List.of(IIQ, PUBCID)).result().state()).isEqualTo(CacheResult.State.MISS); + verify(store).delete("iiq:uid-1"); + verify(store).delete("pubcid:p1"); + } + + @Test + public void clearInProgressShouldKeepResolvedLocalEntry() { + // given — another request resolved the id meanwhile + when(store.put(any(), any(), anyLong())).thenReturn(Future.succeededFuture()); + when(store.delete(any())).thenReturn(Future.succeededFuture()); + target.put(List.of(IIQ), eids, null, null, 60_000L); + + // when + target.clearInProgress(List.of(IIQ)); + + // then + assertThat(target.get(List.of(IIQ)).result().state()).isEqualTo(CacheResult.State.HIT); + } + + @Test + public void getShouldTreatHungStoreAsMissAfterL2Timeout() throws Exception { + // given — the store never answers + when(store.get(any())).thenReturn(Promise.promise().future()); + + // when + final CacheResult result = target.get(List.of(IIQ)).toCompletionStage().toCompletableFuture() + .get(2, TimeUnit.SECONDS); + + // then + assertThat(result.state()).isEqualTo(CacheResult.State.MISS); + assertThat(counter("l2.get.error")).isEqualTo(1); + } + private long counter(String name) { return metricRegistry.counter(metricName(name)).getCount(); } @@ -434,14 +513,14 @@ private static String metricName(String name) { } private String positiveEntry(long exp) { - return MAPPER.encodeToString(IdentityCache.CacheEntry.of(eids, false, false, exp)); + return MAPPER.encodeToString(IdentityCache.CacheEntry.of(eids, false, false, exp, null, null)); } private String negativeEntry(long exp) { - return MAPPER.encodeToString(IdentityCache.CacheEntry.of(List.of(), true, false, exp)); + return MAPPER.encodeToString(IdentityCache.CacheEntry.of(List.of(), true, false, exp, null, null)); } private String inProgressEntry(long exp) { - return MAPPER.encodeToString(IdentityCache.CacheEntry.of(List.of(), false, true, exp)); + return MAPPER.encodeToString(IdentityCache.CacheEntry.of(List.of(), false, true, exp, null, null)); } } diff --git a/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityAuctionResponseHookTest.java b/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityAuctionResponseHookTest.java index 31b85ce4833..f51abdb613d 100644 --- a/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityAuctionResponseHookTest.java +++ b/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityAuctionResponseHookTest.java @@ -41,6 +41,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoInteractions; @@ -218,6 +219,72 @@ public void callShouldOmitTerminationCauseWhenModuleContextHasNone() { assertThat(rdataJson(urlCaptor.getValue())).doesNotContain("terminationCause"); } + @Test + public void callShouldReportOnlyHighestBidPerImpressionAcrossSeats() { + // given — two bidders bid on imp "a", one on imp "b" + givenRequest(BidRequest.builder().build()); + final BidResponse bidResponse = BidResponse.builder() + .cur("USD") + .seatbid(List.of( + SeatBid.builder().seat("low").bid(List.of( + Bid.builder().impid("a").price(BigDecimal.ONE).build(), + Bid.builder().impid("b").price(BigDecimal.valueOf(3)).build())).build(), + SeatBid.builder().seat("high").bid(List.of( + Bid.builder().impid("a").price(BigDecimal.TEN).build())).build())) + .build(); + + // when + target(REPORTS, "999").call(payload(bidResponse), invocationContext); + + // then — one report per impression: imp a by the higher bidder, imp b by its only bidder + verify(httpClient, times(2)).get(any(), any(), anyLong()); + assertThat(urlCaptor.getAllValues()).extracting(IntentiqIdentityAuctionResponseHookTest::rdataJson) + .anySatisfy(rdata -> assertThat(rdata).contains("\"bidderCode\":\"high\"") + .contains("\"placementId\":\"a\"")) + .anySatisfy(rdata -> assertThat(rdata).contains("\"bidderCode\":\"low\"") + .contains("\"placementId\":\"b\"")) + .noneSatisfy(rdata -> assertThat(rdata).contains("\"bidderCode\":\"low\"") + .contains("\"placementId\":\"a\"")); + } + + @Test + public void callShouldUseReportsTimeoutRatherThanEnrichmentTimeout() { + // given + givenRequest(BidRequest.builder().build()); + final IntentiqIdentityProperties properties = new IntentiqIdentityProperties(); + properties.setReportsEndpoint(REPORTS); + properties.setPartnerId("999"); + properties.setTimeout(80L); + properties.setReportsTimeout(1500L); + final IntentiqIdentityAuctionResponseHook hook = new IntentiqIdentityAuctionResponseHook( + new ConfigResolver(MAPPER.mapper(), MERGER, properties), httpClient, MAPPER, metrics); + + // when + hook.call(payload(response("seat", Bid.builder().impid("a").price(BigDecimal.ONE).build())), + invocationContext); + + // then + verify(httpClient).get(any(), any(), eq(1500L)); + } + + @Test + public void callShouldCountNon2xxReportAsError() { + // given + givenRequest(BidRequest.builder().build()); + when(httpClient.get(any(), any(), anyLong())) + .thenReturn(Future.succeededFuture(HttpClientResponse.of(500, null, ""))); + + // when + target(REPORTS, "999").call(payload(response("seat", Bid.builder().impid("a").price(BigDecimal.ONE) + .build())), invocationContext); + + // then + assertThat(metricRegistry.counter("modules.module.intentiq-identity.custom.impression.error_999") + .getCount()).isEqualTo(1); + assertThat(metricRegistry.counter("modules.module.intentiq-identity.custom.impression.reported_999") + .getCount()).isZero(); + } + @Test public void callShouldReturnNoActionAndSkipReportingWhenReportsEndpointBlank() { // when diff --git a/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityProcessedAuctionRequestHookTest.java b/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityProcessedAuctionRequestHookTest.java index 241fcb36258..34c2012564c 100644 --- a/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityProcessedAuctionRequestHookTest.java +++ b/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityProcessedAuctionRequestHookTest.java @@ -459,7 +459,7 @@ public void callShouldPassResponseCttlToNegativeCacheOnMiss() { target(API_ENDPOINT, "123", cache).call(payload, invocationContext).result(); // then - verify(cache).putNegative(any(), eq(600_000L)); + verify(cache).putNegative(any(), any(), any(), eq(600_000L)); } @Test @@ -512,7 +512,7 @@ public void callShouldUseCachedEidsAndSkipHttpOnCacheHit() { .build(); when(cache.get(any())) .thenReturn(Future.succeededFuture( - CacheResult.hit(singletonList(cached), KeyType.FIRST_PARTY, CacheResult.Layer.L1))); + CacheResult.hit(singletonList(cached), null, null, KeyType.FIRST_PARTY, CacheResult.Layer.L1))); final AuctionRequestPayload payload = AuctionRequestPayloadImpl.of( BidRequest.builder().device(Device.builder().ifa("MAID-1").build()).build()); @@ -541,7 +541,7 @@ public void callShouldFetchAndPopulateCacheOnMiss() { // then assertThat(result.action()).isEqualTo(InvocationAction.update); - verify(cache).put(any(), any(), anyLong()); + verify(cache).put(any(), any(), any(), any(), anyLong()); } @Test @@ -559,7 +559,7 @@ public void callShouldWriteNegativeCacheEntryWhenApiReturnsNoEidsOnMiss() { // then — EMPTY_BODY carries no cttl, so the negative entry uses the default suppression TTL assertThat(result.action()).isEqualTo(InvocationAction.no_action); - verify(cache).putNegative(any(), eq(0L)); + verify(cache).putNegative(any(), any(), any(), eq(0L)); } @Test @@ -567,7 +567,7 @@ public void callShouldReturnNoActionAndSkipHttpOnNegativeCacheHit() { // given final IdentityCache cache = mock(IdentityCache.class); when(cache.get(any())).thenReturn(Future.succeededFuture( - CacheResult.negative(KeyType.FIRST_PARTY, CacheResult.Layer.L2))); + CacheResult.negative(null, null, KeyType.FIRST_PARTY, CacheResult.Layer.L2))); final AuctionRequestPayload payload = AuctionRequestPayloadImpl.of( BidRequest.builder().device(Device.builder().ifa("MAID-1").build()).build()); @@ -644,7 +644,7 @@ public void callShouldIncrementCacheHitOnCacheHit() { when(cache.get(any())).thenReturn(Future.succeededFuture(CacheResult.hit(singletonList(Eid.builder() .source("intentiq.com") .uids(singletonList(Uid.builder().id("u").build())) - .build()), KeyType.FIRST_PARTY, CacheResult.Layer.L1))); + .build()), null, null, KeyType.FIRST_PARTY, CacheResult.Layer.L1))); final AuctionRequestPayload payload = AuctionRequestPayloadImpl.of( BidRequest.builder().device(Device.builder().ifa("MAID-1").build()).build()); @@ -725,6 +725,184 @@ public void callShouldIncrementCacheInProgressOnInProgressResult() { assertThat(counter("cache.l1.in_progress.first_party")).isEqualTo(1); } + @Test + public void callShouldNotSendAllZeroIfaAsPcid() { + // given + givenCapturedResponse(); + final BidRequest bidRequest = BidRequest.builder() + .device(Device.builder().ifa("00000000-0000-0000-0000-000000000000").lmt(0).build()) + .build(); + + // when + target(API_ENDPOINT, "123").call(AuctionRequestPayloadImpl.of(bidRequest), invocationContext); + + // then + assertThat(urlCaptor.getValue()).doesNotContain("pcid").doesNotContain("idtype"); + } + + @Test + public void callShouldCarryCachedAbTestUuidAndTerminationCauseOnCacheHit() { + // given + final IdentityCache cache = mock(IdentityCache.class); + when(cache.get(any())).thenReturn(Future.succeededFuture(CacheResult.hit(singletonList(Eid.builder() + .source("intentiq.com") + .uids(singletonList(Uid.builder().id("u").build())) + .build()), "S2S_cached", 0L, KeyType.FIRST_PARTY, CacheResult.Layer.L2))); + final AuctionRequestPayload payload = AuctionRequestPayloadImpl.of( + BidRequest.builder().device(Device.builder().ifa("MAID-1").build()).build()); + + // when + final InvocationResult result = + target(API_ENDPOINT, "123", cache).call(payload, invocationContext).result(); + + // then — the impression report of a cache-served auction keeps the A/B group and tc + final IntentiqIdentityModuleContext context = (IntentiqIdentityModuleContext) result.moduleContext(); + assertThat(context.abTestUuid()).isEqualTo("S2S_cached"); + assertThat(context.terminationCause()).isEqualTo(0L); + } + + @Test + public void callShouldCarryCachedAbTestUuidAndTerminationCauseOnNegativeHit() { + // given + final IdentityCache cache = mock(IdentityCache.class); + when(cache.get(any())).thenReturn(Future.succeededFuture( + CacheResult.negative("S2S_neg", 41L, KeyType.FIRST_PARTY, CacheResult.Layer.L1))); + final AuctionRequestPayload payload = AuctionRequestPayloadImpl.of( + BidRequest.builder().device(Device.builder().ifa("MAID-1").build()).build()); + + // when + final InvocationResult result = + target(API_ENDPOINT, "123", cache).call(payload, invocationContext).result(); + + // then + final IntentiqIdentityModuleContext context = (IntentiqIdentityModuleContext) result.moduleContext(); + assertThat(context.abTestUuid()).isEqualTo("S2S_neg"); + assertThat(context.terminationCause()).isEqualTo(41L); + } + + @Test + public void callShouldCacheAbTestUuidAndTerminationCauseOnMiss() { + // given + final IdentityCache cache = mock(IdentityCache.class); + when(cache.get(any())).thenReturn(Future.succeededFuture(CacheResult.miss())); + givenResponse("{\"data\":{\"eids\":[{\"source\":\"intentiq.com\",\"uids\":[{\"id\":\"abc\"}]}]}," + + "\"abTestUuid\":\"S2S_fresh\",\"tc\":0,\"cttl\":5000}"); + final AuctionRequestPayload payload = AuctionRequestPayloadImpl.of( + BidRequest.builder().device(Device.builder().ifa("MAID-1").build()).build()); + + // when + target(API_ENDPOINT, "123", cache).call(payload, invocationContext).result(); + + // then + verify(cache).put(any(), any(), eq("S2S_fresh"), eq(0L), eq(5000L)); + } + + @Test + public void callShouldTreatNon2xxAsApiErrorAndCacheNothing() { + // given — an overloaded API answering 503 with a JSON body + final IdentityCache cache = mock(IdentityCache.class); + when(cache.get(any())).thenReturn(Future.succeededFuture(CacheResult.miss())); + when(httpClient.get(any(), any(), anyLong())) + .thenReturn(Future.succeededFuture(HttpClientResponse.of(503, null, EMPTY_BODY))); + final AuctionRequestPayload payload = AuctionRequestPayloadImpl.of( + BidRequest.builder().device(Device.builder().ifa("MAID-1").build()).build()); + + // when + final InvocationResult result = + target(API_ENDPOINT, "123", cache).call(payload, invocationContext).result(); + + // then + assertThat(result.action()).isEqualTo(InvocationAction.no_action); + assertThat(counter("api.error")).isEqualTo(1); + assertThat(counter("api.success")).isZero(); + verify(cache, never()).putNegative(any(), any(), any(), anyLong()); + verify(cache, never()).put(any(), any(), any(), any(), anyLong()); + verify(cache).clearInProgress(any()); + } + + @Test + public void callShouldClearInProgressMarkerWhenHttpCallFailsOnMiss() { + // given + final IdentityCache cache = mock(IdentityCache.class); + when(cache.get(any())).thenReturn(Future.succeededFuture(CacheResult.miss())); + when(httpClient.get(any(), any(), anyLong())) + .thenReturn(Future.failedFuture(new RuntimeException("timeout"))); + final AuctionRequestPayload payload = AuctionRequestPayloadImpl.of( + BidRequest.builder().device(Device.builder().ifa("MAID-1").build()).build()); + + // when + target(API_ENDPOINT, "123", cache).call(payload, invocationContext).result(); + + // then — the next request for this user retries instead of waiting out the marker + verify(cache).putInProgress(any()); + verify(cache).clearInProgress(any()); + } + + @Test + public void callShouldSkipHttpWhenPartnerIdBlank() { + // when + final InvocationResult result = target(API_ENDPOINT, " ") + .call(AuctionRequestPayloadImpl.of(BidRequest.builder().build()), invocationContext).result(); + + // then + assertThat(result.action()).isEqualTo(InvocationAction.no_action); + verifyNoInteractions(httpClient); + } + + @Test + public void callShouldSkipHttpWhenGdprAppliesWithoutConsent() { + // given + final BidRequest bidRequest = BidRequest.builder() + .regs(Regs.builder().gdpr(1).build()) + .user(User.builder().build()) + .build(); + + // when + final InvocationResult result = target(API_ENDPOINT, "123") + .call(AuctionRequestPayloadImpl.of(bidRequest), invocationContext).result(); + + // then + assertThat(result.action()).isEqualTo(InvocationAction.no_action); + assertThat(counter("skip.no_consent")).isEqualTo(1); + verifyNoInteractions(httpClient); + } + + @Test + public void callShouldCallApiWhenGdprDoesNotApplyWithoutConsent() { + // given + givenCapturedResponse(); + final BidRequest bidRequest = BidRequest.builder().regs(Regs.builder().gdpr(0).build()).build(); + + // when + target(API_ENDPOINT, "123").call(AuctionRequestPayloadImpl.of(bidRequest), invocationContext); + + // then + assertThat(urlCaptor.getValue()).contains("&gdpr=0"); + } + + @Test + public void callShouldNotDuplicateEidSourceTheRequestAlreadyCarries() { + // given — the page already sends its own intentiq.com id + givenResponse("{\"data\":{\"eids\":[{\"source\":\"intentiq.com\",\"uids\":[{\"id\":\"resolved\"}]}," + + "{\"source\":\"liveramp.com\",\"uids\":[{\"id\":\"lr\"}]}]}}"); + final Eid pageEid = Eid.builder() + .source("intentiq.com") + .uids(singletonList(Uid.builder().id("page-iiq").build())) + .build(); + final AuctionRequestPayload payload = AuctionRequestPayloadImpl.of(BidRequest.builder() + .user(User.builder().eids(singletonList(pageEid)).build()) + .build()); + + // when + final InvocationResult result = target(API_ENDPOINT, "123") + .call(payload, invocationContext).result(); + + // then — one eid per source: the page's intentiq.com id is kept, liveramp.com is added + final List eids = applyUpdate(result, payload).getUser().getEids(); + assertThat(eids).extracting(Eid::getSource).containsExactly("intentiq.com", "liveramp.com"); + assertThat(eids.getFirst().getUids().getFirst().getId()).isEqualTo("page-iiq"); + } + private void givenResponse(String body) { when(httpClient.get(any(), any(), anyLong())) .thenReturn(Future.succeededFuture(HttpClientResponse.of(200, null, body))); diff --git a/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/FirstPartyKeyExtractorTest.java b/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/FirstPartyKeyExtractorTest.java index de2e2e88749..293fad72791 100644 --- a/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/FirstPartyKeyExtractorTest.java +++ b/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/FirstPartyKeyExtractorTest.java @@ -143,4 +143,21 @@ public void shouldCapNumberOfKeys() { private static Eid eid(String source, String id) { return Eid.builder().source(source).uids(singletonList(Uid.builder().id(id).build())).build(); } + + @Test + public void candidateKeysShouldNotKeyOnAllZeroIfa() { + // given — every ATT-denied iOS device sends this value, often with lmt=0 + final BidRequest bidRequest = BidRequest.builder() + .device(Device.builder().ifa("00000000-0000-0000-0000-000000000000").lmt(0) + .ua("UA").ip("1.2.3.4").build()) + .build(); + + // when + final List keys = new FirstPartyKeyExtractor(10).candidateKeys(bidRequest); + + // then + assertThat(keys).extracting(CacheKey::key) + .noneMatch(key -> key.startsWith("maid:")) + .noneMatch(key -> key.contains("00000000")); + } } diff --git a/sample/configs/prebid-config-with-intentiq.yaml b/sample/configs/prebid-config-with-intentiq.yaml index f35a0f39e53..cdf044e2ba0 100644 --- a/sample/configs/prebid-config-with-intentiq.yaml +++ b/sample/configs/prebid-config-with-intentiq.yaml @@ -43,6 +43,8 @@ gdpr: cache-dir: /var/tmp/vendor2 v3: cache-dir: /var/tmp/vendor3 + live: + startup-cache-dir: /var/tmp/vendor3 admin-endpoints: logging-changelevel: enabled: true @@ -97,9 +99,6 @@ hooks: cache: enabled: true ttlseconds: 43200 - # dev: keep the in-progress marker short so a timed-out VR retries soon instead of - # poisoning the device key for 30 min (default 1800s) when the VPN is slow/flapping. - in-progress-ttl-seconds: 30 redis: host: 127.0.0.1 port: 6379 From d21080ceb21bb71ffe8f8bb5953b32a25afa9986 Mon Sep 17 00:00:00 2001 From: Ben Gojanski Date: Fri, 13 Nov 2026 00:36:28 +0200 Subject: [PATCH 3/6] IntentIQ Identity: cache scoping, privacy gates, in-flight sharing - Scope cache keys by partner-id and a digest of the request's privacy signals (gdpr, consent, us_privacy, gpp): no cross-partner sharing, and a changed consent or opt-out is resolved again. - Use the IP/UA device composite only when the request has no stronger id, and leave an lmt=1 ifa out of it. - Skip the impression report without a partner-id, or when GDPR applies without consent. - Concurrent requests for the same user on one instance share one cache lookup and API call, so they are enriched and A/B-attributed too. - Cache the normalized user agent (Caffeine) instead of parsing it on every auction. - README: cache settings other than cache.enabled are host-level. Co-Authored-By: Claude Opus 5.5 --- extra/modules/intentiq-identity/README.md | 35 ++++-- .../metric/IntentiqIdentityMetrics.java | 5 + .../IntentiqIdentityAuctionResponseHook.java | 7 ++ ...iqIdentityProcessedAuctionRequestHook.java | 85 +++++++------ .../identity/v1/core/DeviceUserAgent.java | 10 ++ .../v1/core/FirstPartyKeyExtractor.java | 27 +++-- .../identity/v1/core/PrivacySignals.java | 90 ++++++++++++++ ...tentiqIdentityAuctionResponseHookTest.java | 47 ++++++++ ...entityProcessedAuctionRequestHookTest.java | 70 +++++++++++ .../v1/core/FirstPartyKeyExtractorTest.java | 114 ++++++++++++++---- 10 files changed, 409 insertions(+), 81 deletions(-) create mode 100644 extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/PrivacySignals.java diff --git a/extra/modules/intentiq-identity/README.md b/extra/modules/intentiq-identity/README.md index c03e5370df8..88519db2e3b 100644 --- a/extra/modules/intentiq-identity/README.md +++ b/extra/modules/intentiq-identity/README.md @@ -77,7 +77,9 @@ outlives the group timeout is dropped and the auction proceeds without enrichmen Host-level config (above) provides defaults. Account-specific values can be set under the account's `hooks.modules.intentiq-identity` config and are merged over the host defaults per request — so -`partner-id`, `timeout`, and `cache.*` can be tuned per account. +`partner-id`, `timeout`, `reports-timeout` and `cache.enabled` can be set per account. The cache itself +(and every other `cache.*` setting) is built once from the host config: an account can switch caching off +with `cache.enabled: false`, but caching on an account needs it enabled on the host. ## Module Configuration Parameters @@ -88,14 +90,14 @@ Host-level config (above) provides defaults. Account-specific values can be set | partner-id | account | yes | string | none | Partner token from IntentIQ, sent as the `dpi` query parameter | | timeout | account | no | long | 1000 | HTTP timeout (ms) for the identity-resolution call | | reports-timeout | account | no | long | 1000 | HTTP timeout (ms) for the fire-and-forget impression report | -| cache.enabled | account | no | boolean | false | Use the Caffeine + Redis cache (host must configure `redis.*`) | -| cache.ttlseconds | account | no | int | 43200 | Fallback TTL (seconds) when the response omits `cttl` (12h) | -| cache.max-keys | account | no | int | 10 | Max alias keys derived per request (caps eid-stuffed requests) | -| cache.ttl-ceiling-first-party-seconds | account | no | int | 86400 | Upper bound on TTL for first-party id keys (pubcid, MAID, other eids) | -| cache.ttl-ceiling-third-party-seconds | account | no | int | 43200 | Upper bound on TTL for third-party id keys (`intentiq.com`) | -| cache.ttl-ceiling-device-seconds | account | no | int | 3600 | Upper bound on TTL for the probabilistic device-composite key | -| cache.negative-ttl-seconds | account | no | int | 120 | TTL for the negative (unresolvable id) sentinel | -| cache.in-progress-ttl-seconds | account | no | int | 30 | TTL for the IN_PROGRESS marker that dedups concurrent resolution calls; a failed call removes it at once | +| cache.enabled | both | no | boolean | false | Use the Caffeine + Redis cache (must be on at host level, with `redis.*`); an account can turn it off | +| cache.ttlseconds | host | no | int | 43200 | Fallback TTL (seconds) when the response omits `cttl` (12h) | +| cache.max-keys | host | no | int | 10 | Max alias keys derived per request (caps eid-stuffed requests) | +| cache.ttl-ceiling-first-party-seconds | host | no | int | 86400 | Upper bound on TTL for first-party id keys (pubcid, MAID, other eids) | +| cache.ttl-ceiling-third-party-seconds | host | no | int | 43200 | Upper bound on TTL for third-party id keys (`intentiq.com`) | +| cache.ttl-ceiling-device-seconds | host | no | int | 3600 | Upper bound on TTL for the probabilistic device-composite key | +| cache.negative-ttl-seconds | host | no | int | 120 | TTL for the negative (unresolvable id) sentinel | +| cache.in-progress-ttl-seconds | host | no | int | 30 | TTL for the IN_PROGRESS marker that dedups concurrent resolution calls; a failed call removes it at once | | cache.l2-timeout-ms | host | no | long | 30 | Max wait (ms) for an L2 read; a slower or hung store counts as a miss | | cache-max-size | host | no | long | 100000 | Max entries in the local (Caffeine) layer | | metrics-enabled | host | no | boolean | true | Record the module's `custom.*` metrics; set `false` to opt out | @@ -129,8 +131,12 @@ through to a live API call. A partner can use a non-Redis backend by supplying t **Multi-key (alias) caching.** Every relevant first-party id on the request becomes a namespaced alias key, ordered by priority: `iiq:` (`intentiq.com`), `pubcid:` (`pubcid.org` / `sharedid.org`), `maid:` (`device.ifa`; upper-cased for CTV, skipped when `device.lmt = 1`), -`:` for any other eid (e.g. `uidapi.com`), and a probabilistic `dev:` composite -as last resort. Keys are de-duplicated and capped at `cache.max-keys`. On a lookup the +`:` for any other eid (e.g. `uidapi.com`). Only when the request carries none of these is a +probabilistic `dev:` composite used (normalized UA; the ifa is left out under `lmt = 1`), so +users who share an IP are never given each other's identity. Every key is scoped by the partner id and a +digest of the request's privacy signals (`regs.gdpr`, consent, `us_privacy`, `gpp`): one partner's +resolution is never served to another, and a user whose consent or opt-out changed is resolved again. +Keys are de-duplicated and capped at `cache.max-keys`. On a lookup the highest-priority key with a live entry wins, and that entry is **back-filled** under every other key that missed — so the alias graph grows over time and a later request carrying any of those ids hits. Differing resolutions are never merged (only the single winning entry propagates). @@ -147,7 +153,8 @@ first-party ceiling), falling back to `cache.negative-ttl-seconds` when `cttl` i **In-progress dedup.** On a full miss, before the live call an `IN_PROGRESS` marker (`cache.in-progress-ttl-seconds`) is written under all candidate keys. A concurrent request for the -same id reads the marker and skips firing a duplicate call (it proceeds without enrichment); the +same id on the same PBS instance waits for the in-flight call and uses its result (eids and A/B +attribution); one on another instance reads the marker from Redis and proceeds without enrichment. The in-flight call populates the cache for subsequent requests. The marker is overwritten by the resolved (or negative) entry when the call completes, removed when the call fails (so the next request retries), or expires if the call never completes. A resolved entry is always @@ -164,7 +171,8 @@ carries `bidderCode` (seat), `cpm`, `currency`, `placementId` (imp id), `bidding one returned by the resolution response — the resolution hook stashes it in the module context and this hook reads it. On a cache hit it is the `abTestUuid` (and `terminationCause`) of the response that filled the cache entry. With -`reports-endpoint` blank the hook is a no-op. The bid response is never modified. +`reports-endpoint` blank the hook is a no-op. Nothing is reported when the account has no `partner-id`, +or when GDPR applies without a consent string (the report carries the user's IP and UA). The bid response is never modified. ### Metrics @@ -191,6 +199,7 @@ modules.module.intentiq-identity.custom.cache.l1.negative.hit._ # modules.module.intentiq-identity.custom.cache.l2.negative.hit._ # negative sentinel from L2; counted as miss, no API call modules.module.intentiq-identity.custom.cache.l1.in_progress._ # in-flight marker from L1; duplicate API call skipped modules.module.intentiq-identity.custom.cache.l2.in_progress._ # in-flight marker from L2; duplicate API call skipped +modules.module.intentiq-identity.custom.cache.in_flight.joined_ # request waited for the same user's call in flight on this instance modules.module.intentiq-identity.custom.cache.miss._ # full miss (neither L1 nor L2) -> API called modules.module.intentiq-identity.custom.api.success_ # resolution API responded and parsed OK modules.module.intentiq-identity.custom.api.error_ # resolution API failed/timed out/non-2xx/unparseable diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/metric/IntentiqIdentityMetrics.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/metric/IntentiqIdentityMetrics.java index 15ac3f8a0a2..998309aec99 100644 --- a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/metric/IntentiqIdentityMetrics.java +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/metric/IntentiqIdentityMetrics.java @@ -51,6 +51,11 @@ public void cacheInProgress(String layer, String keyType, String dpi) { inc("cache." + layer + ".in_progress." + keyType, dpi); } + /** A request joined a resolution call already in flight on this instance for the same user. */ + public void inFlightJoined(String dpi) { + inc("cache.in_flight.joined", dpi); + } + public void apiSuccess(String dpi) { inc("api.success", dpi); } diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityAuctionResponseHook.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityAuctionResponseHook.java index b77eefabc2e..d7285819846 100644 --- a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityAuctionResponseHook.java +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityAuctionResponseHook.java @@ -18,6 +18,7 @@ import org.prebid.server.hooks.modules.intentiq.identity.model.config.IntentiqIdentityProperties; import org.prebid.server.hooks.modules.intentiq.identity.v1.core.ConfigResolver; import org.prebid.server.hooks.modules.intentiq.identity.v1.core.IiqParam; +import org.prebid.server.hooks.modules.intentiq.identity.v1.core.PrivacySignals; import org.prebid.server.hooks.v1.InvocationAction; import org.prebid.server.hooks.v1.InvocationResult; import org.prebid.server.hooks.v1.InvocationStatus; @@ -92,6 +93,12 @@ public Future> call(AuctionResponsePayl } final BidRequest bidRequest = bidRequest(invocationContext); + // Same gates as the resolution call: the report carries the user's IP and UA. + if (StringUtils.isBlank(properties.getPartnerId()) + || (bidRequest != null && PrivacySignals.isGdprWithoutConsent(bidRequest))) { + return Future.succeededFuture(noAction()); + } + final String abTestUuid = context != null ? context.abTestUuid() : null; final Long terminationCause = context != null ? context.terminationCause() : null; final String currency = bidResponse.getCur() != null ? bidResponse.getCur() : DEFAULT_CURRENCY; diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityProcessedAuctionRequestHook.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityProcessedAuctionRequestHook.java index ba9d88aae12..833b9302a4d 100644 --- a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityProcessedAuctionRequestHook.java +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityProcessedAuctionRequestHook.java @@ -18,6 +18,7 @@ import com.iab.openrtb.request.UserAgent; import io.vertx.core.Future; import io.vertx.core.MultiMap; +import io.vertx.core.Promise; import lombok.Data; import lombok.NoArgsConstructor; import org.apache.commons.lang3.StringUtils; @@ -33,6 +34,7 @@ import org.prebid.server.hooks.modules.intentiq.identity.v1.core.ConfigResolver; import org.prebid.server.hooks.modules.intentiq.identity.v1.core.FirstPartyKeyExtractor; import org.prebid.server.hooks.modules.intentiq.identity.v1.core.IiqParam; +import org.prebid.server.hooks.modules.intentiq.identity.v1.core.PrivacySignals; import org.prebid.server.hooks.v1.InvocationAction; import org.prebid.server.hooks.v1.InvocationResult; import org.prebid.server.hooks.v1.InvocationStatus; @@ -59,6 +61,7 @@ import java.util.Objects; import java.util.Set; import java.util.TreeMap; +import java.util.concurrent.ConcurrentHashMap; import java.util.stream.Collectors; public class IntentiqIdentityProcessedAuctionRequestHook implements ProcessedAuctionRequestHook { @@ -87,6 +90,9 @@ public class IntentiqIdentityProcessedAuctionRequestHook implements ProcessedAuc private final IdentityCache cache; private final FirstPartyKeyExtractor keyExtractor; private final IntentiqIdentityMetrics metrics; + // Resolution calls in flight on this instance, by cache key: a concurrent request for the same user + // waits for the call instead of going without enrichment and A/B attribution. + private final Map> inFlightByKey = new ConcurrentHashMap<>(); public IntentiqIdentityProcessedAuctionRequestHook(ConfigResolver configResolver, HttpClient httpClient, @@ -117,7 +123,7 @@ public Future> call(AuctionRequestPayloa return Future.succeededFuture(noAction(new IntentiqIdentityModuleContext(startNanos, null, null))); } final BidRequest bidRequest = auctionRequestPayload.bidRequest(); - if (isGdprWithoutConsent(bidRequest)) { + if (PrivacySignals.isGdprWithoutConsent(bidRequest)) { metrics.skipNoConsent(dpi); return Future.succeededFuture(noAction(new IntentiqIdentityModuleContext(startNanos, null, null))); } @@ -144,7 +150,8 @@ public Future> call(AuctionRequestPayloa private Future resolveEids(IntentiqIdentityProperties properties, BidRequest bidRequest) { final boolean cacheEnabled = cache != null && properties.getCache().isEnabled(); - final List keys = cacheEnabled ? keyExtractor.candidateKeys(bidRequest) : List.of(); + final List keys = cacheEnabled ? keyExtractor.candidateKeys(bidRequest, properties.getPartnerId()) + : List.of(); if (keys.isEmpty()) { return fetch(properties, bidRequest) .map(response -> new Resolution( @@ -156,6 +163,41 @@ private Future resolveEids(IntentiqIdentityProperties properties, Bi // HIT/NEGATIVE/IN_PROGRESS, and the request's primary (highest-priority) candidate type for a // full MISS, where no key matched. final String primaryType = keyTypeToken(keys.getFirst().type()); + // Claim the user's keys before the cache lookup, so a concurrent request on this instance shares this + // lookup (and the API call, if any) instead of racing it. + final Promise owned = Promise.promise(); + final Future inFlight = claimOrJoin(keys, owned.future()); + if (inFlight != null) { + metrics.inFlightJoined(dpi); + // The failure of the shared call is counted once, by the request that made it. + return inFlight.recover(ignored -> Future.succeededFuture(new Resolution(null, null, null))); + } + + owned.future().onComplete(ignored -> keys.forEach(key -> inFlightByKey.remove(key.key(), owned.future()))); + lookupAndResolve(properties, bidRequest, keys, dpi, primaryType).onComplete(owned); + return owned.future(); + } + + // Returns the resolution another request already claimed for one of these keys, or null after claiming + // all of them for the given resolution. + private Future claimOrJoin(List keys, Future resolution) { + for (int i = 0; i < keys.size(); i++) { + final Future existing = inFlightByKey.putIfAbsent(keys.get(i).key(), resolution); + if (existing != null) { + for (int j = 0; j < i; j++) { + inFlightByKey.remove(keys.get(j).key(), resolution); + } + return existing; + } + } + return null; + } + + private Future lookupAndResolve(IntentiqIdentityProperties properties, + BidRequest bidRequest, + List keys, + String dpi, + String primaryType) { return cache.get(keys).compose(result -> switch (result.state()) { case HIT -> { metrics.cacheHit(layerToken(result.layer()), keyTypeToken(result.keyType()), dpi); @@ -266,20 +308,12 @@ private String resolveUrl(IntentiqIdentityProperties properties, BidRequest bidR return url.toString(); } - // Under GDPR the IntentIQ API rejects a request without a TCF consent string, so do not send one. - private static boolean isGdprWithoutConsent(BidRequest bidRequest) { - final Regs regs = bidRequest.getRegs(); - return regs != null - && "1".equals(resolveGdpr(regs)) - && StringUtils.isBlank(resolveConsent(bidRequest.getUser())); - } - private static void appendConsent(StringBuilder url, BidRequest bidRequest) { final Regs regs = bidRequest.getRegs(); if (regs != null) { - appendIfPresent(url, IiqParam.GDPR, resolveGdpr(regs)); - appendIfPresent(url, IiqParam.US_PRIVACY, resolveUsPrivacy(regs)); - appendIfPresent(url, IiqParam.GPP, regs.getGpp()); + appendIfPresent(url, IiqParam.GDPR, PrivacySignals.gdpr(bidRequest)); + appendIfPresent(url, IiqParam.US_PRIVACY, PrivacySignals.usPrivacy(bidRequest)); + appendIfPresent(url, IiqParam.GPP, PrivacySignals.gpp(bidRequest)); // Forwarded ahead of backend support so the GPP section ids are available if the backend adds gpp_sid. appendIfPresent(url, IiqParam.GPP_SID, StringUtils.join(regs.getGppSid(), ',')); } @@ -287,33 +321,10 @@ private static void appendConsent(StringBuilder url, BidRequest bidRequest) { private static MultiMap resolveHeaders(BidRequest bidRequest) { final MultiMap headers = HttpUtil.headers(); - HttpUtil.addHeaderIfValueIsNotEmpty(headers, GDPR_CONSENT_HEADER, resolveConsent(bidRequest.getUser())); + HttpUtil.addHeaderIfValueIsNotEmpty(headers, GDPR_CONSENT_HEADER, PrivacySignals.consent(bidRequest)); return headers; } - private static String resolveGdpr(Regs regs) { - final Integer gdpr = regs.getGdpr() != null ? regs.getGdpr() - : regs.getExt() != null ? regs.getExt().getGdpr() : null; - return gdpr != null ? String.valueOf(gdpr) : null; - } - - private static String resolveUsPrivacy(Regs regs) { - if (StringUtils.isNotBlank(regs.getUsPrivacy())) { - return regs.getUsPrivacy(); - } - return regs.getExt() != null ? regs.getExt().getUsPrivacy() : null; - } - - private static String resolveConsent(User user) { - if (user == null) { - return null; - } - if (StringUtils.isNotBlank(user.getConsent())) { - return user.getConsent(); - } - return user.getExt() != null ? user.getExt().getConsent() : null; - } - private static void appendIfPresent(StringBuilder url, IiqParam param, String value) { if (StringUtils.isNotBlank(value)) { url.append('&').append(param.key()).append('=').append(encodeComponent(value)); diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/DeviceUserAgent.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/DeviceUserAgent.java index 73c0ed7d61a..fc119cf599d 100644 --- a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/DeviceUserAgent.java +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/DeviceUserAgent.java @@ -1,6 +1,8 @@ package org.prebid.server.hooks.modules.intentiq.identity.v1.core; import org.apache.commons.lang3.StringUtils; +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; import ua_parser.Client; import ua_parser.Parser; @@ -22,6 +24,10 @@ public final class DeviceUserAgent { private static final Parser UA_PARSER = new Parser(); + // Parsing runs hundreds of regexes; real traffic repeats a small set of UA strings. + private static final Cache NORMALIZED_BY_UA = Caffeine.newBuilder() + .maximumSize(10_000) + .build(); private static final String UNKNOWN = "Other"; private DeviceUserAgent() { @@ -36,6 +42,10 @@ public static String normalize(String ua) { return StringUtils.EMPTY; } + return NORMALIZED_BY_UA.get(ua, DeviceUserAgent::parse); + } + + private static String parse(String ua) { final Client client = UA_PARSER.parse(ua); final String os = client.os != null ? token(client.os.family, client.os.major) : null; final String browser = client.userAgent != null diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/FirstPartyKeyExtractor.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/FirstPartyKeyExtractor.java index 77a611332ff..ef918fab536 100644 --- a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/FirstPartyKeyExtractor.java +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/FirstPartyKeyExtractor.java @@ -24,10 +24,14 @@ * becomes one namespaced key; the resolved identity is aliased across all of them so a later request * carrying any of those ids hits the cache. * - *

    Priority (highest first): IIQ eid, shared id / pubcid, device MAID, any other eid source, then a - * probabilistic device composite as last resort. Keys are lower-cased, de-duplicated (first occurrence - * wins) and capped at {@code maxKeys}. {@code device.lmt == 1} suppresses the MAID key; CTV ifas are - * upper-cased to match the resolution request's {@code idtype 8} handling. + *

    Priority (highest first): IIQ eid, shared id / pubcid, device MAID, any other eid source. Only when + * the request carries none of these is a probabilistic device composite (ifa / normalized UA / IP) used, so a + * resolution is never shared between users through a common IP. Keys are de-duplicated (first occurrence + * wins) and capped at {@code maxKeys}. {@code device.lmt == 1} and an all-zero ifa suppress the ifa; CTV + * ifas are upper-cased to match the resolution request's {@code idtype 8} handling. + * + *

    Every key is scoped by the partner id and the request's privacy-signal fingerprint, so one partner's + * resolution is never served to another, and a user whose consent or opt-out changed is resolved again. */ public class FirstPartyKeyExtractor { @@ -40,7 +44,7 @@ public FirstPartyKeyExtractor(int maxKeys) { this.maxKeys = maxKeys; } - public List candidateKeys(BidRequest bidRequest) { + public List candidateKeys(BidRequest bidRequest, String partnerId) { final List keys = new ArrayList<>(); final User user = bidRequest.getUser(); final List eids = user != null ? user.getEids() : null; @@ -50,9 +54,14 @@ public List candidateKeys(BidRequest bidRequest) { addEidKeys(keys, eids, "pubcid", KeyType.FIRST_PARTY, SHARED_SOURCES::contains); addMaidKey(keys, device); addOtherEidKeys(keys, eids); - addDeviceComposite(keys, device); + if (keys.isEmpty()) { + addDeviceComposite(keys, device); + } - return dedupAndCap(keys); + final String scope = StringUtils.defaultString(partnerId) + ":" + PrivacySignals.fingerprint(bidRequest) + ":"; + return dedupAndCap(keys).stream() + .map(key -> new CacheKey(scope + key.key(), key.type())) + .collect(Collectors.toList()); } private static void addEidKeys(List keys, @@ -121,7 +130,9 @@ private static void addDeviceComposite(List keys, Device device) { final String ip = StringUtils.isNotBlank(device.getIp()) ? device.getIp() : device.getIpv6(); // Normalized UA fields (not the raw UA string) to avoid fragmenting the cache per request. final String ua = DeviceUserAgent.normalize(device.getUa()); - final String ifa = isZeroIfa(device.getIfa()) ? null : device.getIfa(); + final String ifa = isZeroIfa(device.getIfa()) || Integer.valueOf(1).equals(device.getLmt()) + ? null + : device.getIfa(); final String composite = Stream.of(ifa, StringUtils.trimToNull(ua), ip) .filter(StringUtils::isNotBlank) .collect(Collectors.joining("_")); diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/PrivacySignals.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/PrivacySignals.java new file mode 100644 index 00000000000..b0514cad2f1 --- /dev/null +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/PrivacySignals.java @@ -0,0 +1,90 @@ +package org.prebid.server.hooks.modules.intentiq.identity.v1.core; + +import com.iab.openrtb.request.BidRequest; +import com.iab.openrtb.request.Regs; +import com.iab.openrtb.request.User; +import org.apache.commons.lang3.StringUtils; + +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.util.HexFormat; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +/** + * Reads the privacy signals of a bid request (GDPR applicability, TCF consent, US privacy, GPP), from the + * OpenRTB 2.6 fields first and the 2.5 {@code ext} fields as fallback. + */ +public final class PrivacySignals { + + private static final String NO_SIGNALS = "-"; + private static final int FINGERPRINT_HEX_LENGTH = 16; + + private PrivacySignals() { + } + + public static String gdpr(BidRequest bidRequest) { + final Regs regs = bidRequest.getRegs(); + if (regs == null) { + return null; + } + final Integer gdpr = regs.getGdpr() != null ? regs.getGdpr() + : regs.getExt() != null ? regs.getExt().getGdpr() : null; + return gdpr != null ? String.valueOf(gdpr) : null; + } + + public static String usPrivacy(BidRequest bidRequest) { + final Regs regs = bidRequest.getRegs(); + if (regs == null) { + return null; + } + if (StringUtils.isNotBlank(regs.getUsPrivacy())) { + return regs.getUsPrivacy(); + } + return regs.getExt() != null ? regs.getExt().getUsPrivacy() : null; + } + + public static String gpp(BidRequest bidRequest) { + return bidRequest.getRegs() != null ? bidRequest.getRegs().getGpp() : null; + } + + public static String consent(BidRequest bidRequest) { + final User user = bidRequest.getUser(); + if (user == null) { + return null; + } + if (StringUtils.isNotBlank(user.getConsent())) { + return user.getConsent(); + } + return user.getExt() != null ? user.getExt().getConsent() : null; + } + + /** GDPR applies ({@code regs.gdpr = 1}) and the request carries no TCF consent string. */ + public static boolean isGdprWithoutConsent(BidRequest bidRequest) { + return "1".equals(gdpr(bidRequest)) && StringUtils.isBlank(consent(bidRequest)); + } + + /** + * A short, stable digest of all privacy signals of the request. Requests whose signals differ (e.g. a + * user who changed their consent or opted out) get different fingerprints. + */ + public static String fingerprint(BidRequest bidRequest) { + final String signals = Stream.of(gdpr(bidRequest), consent(bidRequest), usPrivacy(bidRequest), + gpp(bidRequest)) + .map(StringUtils::defaultString) + .collect(Collectors.joining("|")); + if (StringUtils.isBlank(signals.replace("|", ""))) { + return NO_SIGNALS; + } + return HexFormat.of().formatHex(sha256(signals)).substring(0, FINGERPRINT_HEX_LENGTH); + } + + private static byte[] sha256(String value) { + try { + return MessageDigest.getInstance("SHA-256").digest(value.getBytes(StandardCharsets.UTF_8)); + } catch (NoSuchAlgorithmException e) { + throw new IllegalStateException("SHA-256 is not available", e); + } + } +} diff --git a/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityAuctionResponseHookTest.java b/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityAuctionResponseHookTest.java index f51abdb613d..c6cd73ca550 100644 --- a/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityAuctionResponseHookTest.java +++ b/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityAuctionResponseHookTest.java @@ -4,7 +4,9 @@ import com.fasterxml.jackson.databind.node.ObjectNode; import com.iab.openrtb.request.BidRequest; import com.iab.openrtb.request.Device; +import com.iab.openrtb.request.Regs; import com.iab.openrtb.request.Site; +import com.iab.openrtb.request.User; import com.iab.openrtb.response.Bid; import com.iab.openrtb.response.BidResponse; import com.iab.openrtb.response.SeatBid; @@ -285,6 +287,51 @@ public void callShouldCountNon2xxReportAsError() { .getCount()).isZero(); } + @Test + public void callShouldNotReportWhenGdprAppliesWithoutConsent() { + // given + givenRequest(BidRequest.builder() + .regs(Regs.builder().gdpr(1).build()) + .device(Device.builder().ip("1.2.3.4").ua("UA").build()) + .build()); + + // when + target(REPORTS, "999").call(payload(response("seat", Bid.builder().impid("a").price(BigDecimal.ONE) + .build())), invocationContext); + + // then + verifyNoInteractions(httpClient); + } + + @Test + public void callShouldReportWhenGdprAppliesWithConsent() { + // given + givenRequest(BidRequest.builder() + .regs(Regs.builder().gdpr(1).build()) + .user(User.builder().consent("TCF").build()) + .build()); + + // when + target(REPORTS, "999").call(payload(response("seat", Bid.builder().impid("a").price(BigDecimal.ONE) + .build())), invocationContext); + + // then + verify(httpClient).get(any(), any(), anyLong()); + } + + @Test + public void callShouldNotReportWithoutPartnerId() { + // given + givenRequest(BidRequest.builder().build()); + + // when + target(REPORTS, "").call(payload(response("seat", Bid.builder().impid("a").price(BigDecimal.ONE) + .build())), invocationContext); + + // then + verifyNoInteractions(httpClient); + } + @Test public void callShouldReturnNoActionAndSkipReportingWhenReportsEndpointBlank() { // when diff --git a/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityProcessedAuctionRequestHookTest.java b/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityProcessedAuctionRequestHookTest.java index 34c2012564c..afaf37ef14a 100644 --- a/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityProcessedAuctionRequestHookTest.java +++ b/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityProcessedAuctionRequestHookTest.java @@ -16,6 +16,7 @@ import org.prebid.server.proto.openrtb.ext.request.ExtUser; import io.vertx.core.Future; import io.vertx.core.MultiMap; +import io.vertx.core.Promise; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -57,6 +58,7 @@ import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.when; @@ -903,6 +905,74 @@ public void callShouldNotDuplicateEidSourceTheRequestAlreadyCarries() { assertThat(eids.getFirst().getUids().getFirst().getId()).isEqualTo("page-iiq"); } + @Test + public void callShouldJoinResolutionAlreadyInFlightForTheSameUser() { + // given — two auctions for the same user while the first one's API call is still open + final IdentityCache cache = mock(IdentityCache.class); + when(cache.get(any())).thenReturn(Future.succeededFuture(CacheResult.miss())); + final Promise apiResponse = Promise.promise(); + when(httpClient.get(any(), any(), anyLong())).thenReturn(apiResponse.future()); + final IntentiqIdentityProcessedAuctionRequestHook hook = target(API_ENDPOINT, "123", cache); + final AuctionRequestPayload payload = AuctionRequestPayloadImpl.of( + BidRequest.builder().device(Device.builder().ifa("MAID-1").build()).build()); + + // when + final Future> first = hook.call(payload, invocationContext); + final Future> second = hook.call(payload, invocationContext); + apiResponse.complete(HttpClientResponse.of(200, null, "{\"data\":{\"eids\":[{\"source\":\"intentiq.com\"," + + "\"uids\":[{\"id\":\"abc\"}]}]},\"abTestUuid\":\"S2S_ab\",\"tc\":0}")); + + // then — one API call; both auctions enriched and attributed to the A/B group + verify(httpClient, times(1)).get(any(), any(), anyLong()); + assertThat(first.result().action()).isEqualTo(InvocationAction.update); + assertThat(second.result().action()).isEqualTo(InvocationAction.update); + assertThat(((IntentiqIdentityModuleContext) second.result().moduleContext()).abTestUuid()) + .isEqualTo("S2S_ab"); + assertThat(counter("cache.in_flight.joined")).isEqualTo(1); + } + + @Test + public void callShouldCallApiAgainOnceTheInFlightCallCompleted() { + // given + final IdentityCache cache = mock(IdentityCache.class); + when(cache.get(any())).thenReturn(Future.succeededFuture(CacheResult.miss())); + givenResponse(EIDS_BODY); + final IntentiqIdentityProcessedAuctionRequestHook hook = target(API_ENDPOINT, "123", cache); + final AuctionRequestPayload payload = AuctionRequestPayloadImpl.of( + BidRequest.builder().device(Device.builder().ifa("MAID-1").build()).build()); + + // when + hook.call(payload, invocationContext).result(); + hook.call(payload, invocationContext).result(); + + // then — nothing stays registered as in flight + verify(httpClient, times(2)).get(any(), any(), anyLong()); + assertThat(counter("cache.in_flight.joined")).isZero(); + } + + @Test + public void callShouldJoinWhileTheCacheLookupIsStillOpen() { + // given — the second auction arrives before the first one's cache lookup has answered + final IdentityCache cache = mock(IdentityCache.class); + final Promise lookup = Promise.promise(); + when(cache.get(any())).thenReturn(lookup.future()); + givenResponse(EIDS_BODY); + final IntentiqIdentityProcessedAuctionRequestHook hook = target(API_ENDPOINT, "123", cache); + final AuctionRequestPayload payload = AuctionRequestPayloadImpl.of( + BidRequest.builder().device(Device.builder().ifa("MAID-1").build()).build()); + + // when + final Future> first = hook.call(payload, invocationContext); + final Future> second = hook.call(payload, invocationContext); + lookup.complete(CacheResult.miss()); + + // then — one lookup, one API call, both enriched + verify(cache, times(1)).get(any()); + verify(httpClient, times(1)).get(any(), any(), anyLong()); + assertThat(first.result().action()).isEqualTo(InvocationAction.update); + assertThat(second.result().action()).isEqualTo(InvocationAction.update); + } + private void givenResponse(String body) { when(httpClient.get(any(), any(), anyLong())) .thenReturn(Future.succeededFuture(HttpClientResponse.of(200, null, body))); diff --git a/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/FirstPartyKeyExtractorTest.java b/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/FirstPartyKeyExtractorTest.java index 293fad72791..2de01a28d15 100644 --- a/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/FirstPartyKeyExtractorTest.java +++ b/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/FirstPartyKeyExtractorTest.java @@ -3,6 +3,7 @@ import com.iab.openrtb.request.BidRequest; import com.iab.openrtb.request.Device; import com.iab.openrtb.request.Eid; +import com.iab.openrtb.request.Regs; import com.iab.openrtb.request.Uid; import com.iab.openrtb.request.User; import org.junit.jupiter.api.Test; @@ -16,11 +17,14 @@ public class FirstPartyKeyExtractorTest { + // Partner "dpi", request with no privacy signals. + private static final String SCOPE = "dpi:-:"; + private final FirstPartyKeyExtractor target = new FirstPartyKeyExtractor(10); @Test public void shouldReturnEmptyWhenNoIdentifiersPresent() { - assertThat(target.candidateKeys(BidRequest.builder().build())).isEmpty(); + assertThat(target.candidateKeys(BidRequest.builder().build(), "dpi")).isEmpty(); } @Test @@ -36,15 +40,14 @@ public void shouldOrderKeysByPriorityAndTagTypes() { .build(); // when - final List keys = target.candidateKeys(bidRequest); + final List keys = target.candidateKeys(bidRequest, "dpi"); - // then — iiq, pubcid, maid, other eid, device composite (unrecognized UA contributes nothing) + // then — iiq, pubcid, maid, other eid; no device composite while a stronger id is present assertThat(keys).containsExactly( - new CacheKey("iiq:iiq-1", KeyType.THIRD_PARTY), - new CacheKey("pubcid:pub-1", KeyType.FIRST_PARTY), - new CacheKey("maid:ifa-1", KeyType.FIRST_PARTY), - new CacheKey("uidapi.com:uid2-1", KeyType.FIRST_PARTY), - new CacheKey("dev:ifa-1_1.2.3.4", KeyType.DEVICE)); + new CacheKey(SCOPE + "iiq:iiq-1", KeyType.THIRD_PARTY), + new CacheKey(SCOPE + "pubcid:pub-1", KeyType.FIRST_PARTY), + new CacheKey(SCOPE + "maid:ifa-1", KeyType.FIRST_PARTY), + new CacheKey(SCOPE + "uidapi.com:uid2-1", KeyType.FIRST_PARTY)); } @Test @@ -57,14 +60,14 @@ public void shouldNormalizeUserAgentInDeviceCompositeRatherThanUseRawString() { .build(); // when - final CacheKey deviceKey = target.candidateKeys(bidRequest).stream() + final CacheKey deviceKey = target.candidateKeys(bidRequest, "dpi").stream() .filter(key -> key.type() == KeyType.DEVICE) .findFirst() .orElseThrow(); // then — normalized OS/browser/device tokens, never the raw UA string assertThat(deviceKey.key()) - .startsWith("dev:") + .startsWith(SCOPE + "dev:") .endsWith("_1.2.3.4") .contains("iOS") .doesNotContain(rawUa) @@ -79,8 +82,8 @@ public void shouldTreatSharedidOrgAsPubcid() { .build(); // when / then - assertThat(target.candidateKeys(bidRequest)) - .containsExactly(new CacheKey("pubcid:s-1", KeyType.FIRST_PARTY)); + assertThat(target.candidateKeys(bidRequest, "dpi")) + .containsExactly(new CacheKey(SCOPE + "pubcid:s-1", KeyType.FIRST_PARTY)); } @Test @@ -91,9 +94,9 @@ public void shouldSkipMaidKeyWhenLimitAdTracking() { .build(); // when / then — only the device composite remains, no maid: key - assertThat(target.candidateKeys(bidRequest)) + assertThat(target.candidateKeys(bidRequest, "dpi")) .extracting(CacheKey::key) - .doesNotContain("maid:ifa-1"); + .doesNotContain(SCOPE + "maid:ifa-1"); } @Test @@ -104,9 +107,9 @@ public void shouldUppercaseMaidForCtv() { .build(); // when / then - assertThat(target.candidateKeys(bidRequest)) + assertThat(target.candidateKeys(bidRequest, "dpi")) .extracting(CacheKey::key) - .contains("maid:RIDA-ABC"); + .contains(SCOPE + "maid:RIDA-ABC"); } @Test @@ -117,8 +120,8 @@ public void shouldDeduplicateRepeatedKeys() { .build(); // when / then - assertThat(target.candidateKeys(bidRequest)) - .containsExactly(new CacheKey("pubcid:dup", KeyType.FIRST_PARTY)); + assertThat(target.candidateKeys(bidRequest, "dpi")) + .containsExactly(new CacheKey(SCOPE + "pubcid:dup", KeyType.FIRST_PARTY)); } @Test @@ -134,10 +137,10 @@ public void shouldCapNumberOfKeys() { .build(); // when / then — only the two highest-priority keys survive - assertThat(capped.candidateKeys(bidRequest)) + assertThat(capped.candidateKeys(bidRequest, "dpi")) .containsExactly( - new CacheKey("iiq:iiq-1", KeyType.THIRD_PARTY), - new CacheKey("pubcid:pub-1", KeyType.FIRST_PARTY)); + new CacheKey(SCOPE + "iiq:iiq-1", KeyType.THIRD_PARTY), + new CacheKey(SCOPE + "pubcid:pub-1", KeyType.FIRST_PARTY)); } private static Eid eid(String source, String id) { @@ -153,11 +156,76 @@ public void candidateKeysShouldNotKeyOnAllZeroIfa() { .build(); // when - final List keys = new FirstPartyKeyExtractor(10).candidateKeys(bidRequest); + final List keys = new FirstPartyKeyExtractor(10).candidateKeys(bidRequest, "dpi"); // then assertThat(keys).extracting(CacheKey::key) - .noneMatch(key -> key.startsWith("maid:")) + .noneMatch(key -> key.contains(":maid:")) .noneMatch(key -> key.contains("00000000")); } + + @Test + public void shouldUseDeviceCompositeOnlyWithoutAStrongerId() { + // given + final BidRequest bidRequest = BidRequest.builder() + .device(Device.builder().ifa("ifa-1").ua("UA").ip("1.2.3.4").build()) + .build(); + final BidRequest anonymous = BidRequest.builder() + .device(Device.builder().ua("UA").ip("1.2.3.4").build()) + .build(); + + // when / then + assertThat(target.candidateKeys(bidRequest, "dpi")).extracting(CacheKey::type) + .containsExactly(KeyType.FIRST_PARTY); + assertThat(target.candidateKeys(anonymous, "dpi")) + .containsExactly(new CacheKey(SCOPE + "dev:1.2.3.4", KeyType.DEVICE)); + } + + @Test + public void shouldLeaveLimitAdTrackingIfaOutOfDeviceComposite() { + // given + final BidRequest bidRequest = BidRequest.builder() + .device(Device.builder().ifa("ifa-1").lmt(1).ip("1.2.3.4").build()) + .build(); + + // when / then + assertThat(target.candidateKeys(bidRequest, "dpi")) + .containsExactly(new CacheKey(SCOPE + "dev:1.2.3.4", KeyType.DEVICE)); + } + + @Test + public void shouldScopeKeysByPartner() { + // given + final BidRequest bidRequest = BidRequest.builder() + .user(User.builder().eids(singletonList(eid("pubcid.org", "p"))).build()) + .build(); + + // when / then + assertThat(target.candidateKeys(bidRequest, "111").getFirst().key()) + .isNotEqualTo(target.candidateKeys(bidRequest, "222").getFirst().key()); + } + + @Test + public void shouldScopeKeysByPrivacySignals() { + // given — the same user before and after opting out + final User user = User.builder().eids(singletonList(eid("pubcid.org", "p"))).build(); + final BidRequest before = BidRequest.builder().user(user) + .regs(Regs.builder().usPrivacy("1YNN").build()).build(); + final BidRequest after = BidRequest.builder().user(user) + .regs(Regs.builder().usPrivacy("1YYN").build()).build(); + final BidRequest consentChanged = BidRequest.builder() + .user(user.toBuilder().consent("CONSENT-B").build()) + .regs(Regs.builder().gdpr(1).build()).build(); + final BidRequest consentBefore = BidRequest.builder() + .user(user.toBuilder().consent("CONSENT-A").build()) + .regs(Regs.builder().gdpr(1).build()).build(); + + // when / then + assertThat(target.candidateKeys(before, "dpi").getFirst().key()) + .isNotEqualTo(target.candidateKeys(after, "dpi").getFirst().key()); + assertThat(target.candidateKeys(consentBefore, "dpi").getFirst().key()) + .isNotEqualTo(target.candidateKeys(consentChanged, "dpi").getFirst().key()); + assertThat(target.candidateKeys(before, "dpi").getFirst().key()) + .isEqualTo(target.candidateKeys(before, "dpi").getFirst().key()); + } } From 54de3122a54c7bd2d967cf746f1e57065594e738 Mon Sep 17 00:00:00 2001 From: Ben Gojanski Date: Fri, 13 Nov 2026 20:35:14 +0200 Subject: [PATCH 4/6] IntentIQ Identity: Redis TLS and cluster mode With redis.ssl the Vert.x 5 client refused every connection: it needs a hostname verification algorithm, and none was set. TLS now verifies the host name (redis.verify-hostname: false turns it off), and redis.ca-cert-path trusts a private CA for the Redis connection only. redis.cluster connects in cluster mode (ElastiCache Serverless, Valkey or Redis cluster mode enabled). Co-Authored-By: Claude Opus 5.5 --- extra/modules/intentiq-identity/README.md | 5 ++++- .../config/IntentiqIdentityConfig.java | 15 +++++++++++++ .../model/config/RedisProperties.java | 11 ++++++++++ .../config/IntentiqIdentityConfigTest.java | 22 +++++++++++++++++++ 4 files changed, 52 insertions(+), 1 deletion(-) diff --git a/extra/modules/intentiq-identity/README.md b/extra/modules/intentiq-identity/README.md index 88519db2e3b..682fd3a9aca 100644 --- a/extra/modules/intentiq-identity/README.md +++ b/extra/modules/intentiq-identity/README.md @@ -101,11 +101,14 @@ with `cache.enabled: false`, but caching on an account needs it enabled on the h | cache.l2-timeout-ms | host | no | long | 30 | Max wait (ms) for an L2 read; a slower or hung store counts as a miss | | cache-max-size | host | no | long | 100000 | Max entries in the local (Caffeine) layer | | metrics-enabled | host | no | boolean | true | Record the module's `custom.*` metrics; set `false` to opt out | -| redis.host | host | cond. | string | none | Redis host (required when caching) | +| redis.host | host | cond. | string | none | Redis or Valkey host (required when caching) | | redis.port | host | cond. | int | none | Redis port (required when caching) | | redis.username | host | no | string | none | Redis ACL user | | redis.password | host | no | string | none | Redis password (any characters; not part of a URL) | | redis.ssl | host | no | boolean | false | Connect over TLS | +| redis.verify-hostname | host | no | boolean | true | With `ssl`: check that the server certificate matches `redis.host` | +| redis.ca-cert-path | host | no | string | none | With `ssl`: PEM file of a private CA that signed the server certificate (applies to Redis only) | +| redis.cluster | host | no | boolean | false | Redis / Valkey cluster mode (e.g. ElastiCache Serverless or cluster mode enabled) | | redis.max-pool-size | host | no | int | 32 | Max pooled Redis connections | | redis.max-pool-waiting | host | no | int | 1024 | Max commands queued for a free connection before failing fast | diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/config/IntentiqIdentityConfig.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/config/IntentiqIdentityConfig.java index 3812f208f9c..4fa3550e03d 100644 --- a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/config/IntentiqIdentityConfig.java +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/config/IntentiqIdentityConfig.java @@ -2,8 +2,11 @@ import com.codahale.metrics.MetricRegistry; import io.vertx.core.Vertx; +import io.vertx.core.net.NetClientOptions; +import io.vertx.core.net.PemTrustOptions; import io.vertx.redis.client.Redis; import io.vertx.redis.client.RedisAPI; +import io.vertx.redis.client.RedisClientType; import io.vertx.redis.client.RedisOptions; import org.apache.commons.lang3.StringUtils; import org.prebid.server.hooks.modules.intentiq.identity.cache.CacheTtlPolicy; @@ -41,6 +44,8 @@ public class IntentiqIdentityConfig { // How often the L2 (Redis) size/eviction gauges are refreshed from DBSIZE / INFO. private static final long REDIS_STATS_POLL_MS = 30_000L; + private static final String HOSTNAME_VERIFICATION_HTTPS = "HTTPS"; + @Bean @ConfigurationProperties(prefix = "hooks.modules." + IntentiqIdentityModule.CODE) IntentiqIdentityProperties intentiqIdentityProperties() { @@ -141,9 +146,19 @@ private static RedisAPI createRedisApi(RedisProperties redis, Vertx vertx) { // Credentials are set as options, not in the connection string, so a password with URL-reserved // characters (@, :, /, %) is not mangled. final RedisOptions options = new RedisOptions() + .setType(redis.isCluster() ? RedisClientType.CLUSTER : RedisClientType.STANDALONE) .setConnectionString((redis.isSsl() ? "rediss://" : "redis://") + redis.getHost() + ":" + redis.getPort()) .setMaxPoolSize(redis.getMaxPoolSize()) .setMaxPoolWaiting(redis.getMaxPoolWaiting()); + if (redis.isSsl()) { + // Vert.x refuses a TLS connection unless a hostname verification algorithm is set; an empty one + // turns the check off. + final NetClientOptions netClientOptions = options.getNetClientOptions() + .setHostnameVerificationAlgorithm(redis.isVerifyHostname() ? HOSTNAME_VERIFICATION_HTTPS : ""); + if (StringUtils.isNotBlank(redis.getCaCertPath())) { + netClientOptions.setTrustOptions(new PemTrustOptions().addCertPath(redis.getCaCertPath())); + } + } if (StringUtils.isNotBlank(redis.getUsername())) { options.setUser(redis.getUsername()); } diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/model/config/RedisProperties.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/model/config/RedisProperties.java index ac89726f174..a3c60d22b87 100644 --- a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/model/config/RedisProperties.java +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/model/config/RedisProperties.java @@ -17,6 +17,17 @@ public final class RedisProperties { /** Connect over TLS (e.g. ElastiCache / Memorystore with in-transit encryption). */ boolean ssl; + /** With {@code ssl}: check that the server certificate matches {@code host}. */ + @JsonProperty("verify-hostname") + boolean verifyHostname = true; + + /** With {@code ssl}: PEM file of the CA that signed the server certificate, for a private CA. */ + @JsonProperty("ca-cert-path") + String caCertPath; + + /** Redis / Valkey cluster mode (e.g. ElastiCache Serverless or cluster mode enabled). */ + boolean cluster; + /** Max pooled connections. The Vert.x default (6) exhausts at a few hundred auctions per second. */ @JsonProperty("max-pool-size") int maxPoolSize = 32; diff --git a/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/config/IntentiqIdentityConfigTest.java b/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/config/IntentiqIdentityConfigTest.java index 60c37592e77..a96d7fba1a0 100644 --- a/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/config/IntentiqIdentityConfigTest.java +++ b/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/config/IntentiqIdentityConfigTest.java @@ -149,6 +149,28 @@ public void identityStoreBeanShouldBeBuiltWhenRedisPasswordConfigured() { } } + @Test + public void identityStoreBeanShouldBeBuiltWhenRedisTlsClusterConfigured() { + // given — a real Vertx is required because the Redis client casts it to VertxInternal + final Vertx realVertx = Vertx.vertx(); + final RedisProperties redis = new RedisProperties(); + redis.setHost("clustercfg.redis.internal"); + redis.setPort(6379); + redis.setSsl(true); + redis.setVerifyHostname(false); + redis.setCaCertPath("/etc/ssl/redis-ca.pem"); + redis.setCluster(true); + final IntentiqIdentityProperties properties = new IntentiqIdentityProperties(); + properties.setRedis(redis); + + try { + // when and then + assertThat(target.intentiqIdentityStore(properties, realVertx)).isNotNull(); + } finally { + realVertx.close(); + } + } + @Test public void identityStoreAndCacheBeansShouldBeBuiltWhenRedisConfigured() { // given — a real Vertx is required because the Redis client casts it to VertxInternal From 78e5ec14081024885edf9a60c81f765d7af467b9 Mon Sep 17 00:00:00 2001 From: Ben Gojanski Date: Mon, 16 Nov 2026 22:06:15 +0200 Subject: [PATCH 5/6] IntentIQ Identity: skip COPPA requests, per-request debug trace Requests with regs.coppa=1 are neither enriched nor reported (counted as skip.coppa). With ext.prebid.trace: verbose, each hook returns a debug message saying what it did: where the ids came from (API, cache layer and key type, or a call already in flight) or why it skipped, and how long it took. README: the auction waits for the processed-auction-request group timeout, while the call itself may run up to `timeout`. A call that outlives the group timeout finishes in the background and fills the cache for the user's next request. The example now waits 200 ms with a 1000 ms call timeout. Co-Authored-By: Claude Opus 5.5 --- extra/modules/intentiq-identity/README.md | 36 ++++++-- .../metric/IntentiqIdentityMetrics.java | 5 ++ .../IntentiqIdentityAuctionResponseHook.java | 30 +++++-- ...iqIdentityProcessedAuctionRequestHook.java | 84 +++++++++++++++---- .../intentiq/identity/v1/core/HookTrace.java | 29 +++++++ .../identity/v1/core/PrivacySignals.java | 6 ++ ...tentiqIdentityAuctionResponseHookTest.java | 16 ++++ ...entityProcessedAuctionRequestHookTest.java | 72 +++++++++++++++- 8 files changed, 243 insertions(+), 35 deletions(-) create mode 100644 extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/HookTrace.java diff --git a/extra/modules/intentiq-identity/README.md b/extra/modules/intentiq-identity/README.md index 682fd3a9aca..e3310bc2548 100644 --- a/extra/modules/intentiq-identity/README.md +++ b/extra/modules/intentiq-identity/README.md @@ -26,13 +26,13 @@ hooks: "stages": { "processed-auction-request": { "groups": [ - { "timeout": 100, "hook-sequence": [ + { "timeout": 200, "hook-sequence": [ { "module-code": "intentiq-identity", "hook-impl-code": "intentiq-identity-processed-auction-request-hook" } ] } ] }, "auction-response": { "groups": [ - { "timeout": 100, "hook-sequence": [ + { "timeout": 50, "hook-sequence": [ { "module-code": "intentiq-identity", "hook-impl-code": "intentiq-identity-auction-response-hook" } ] } ] } @@ -69,9 +69,11 @@ Use the region-specific `api-endpoint`: US `be-api-s2s.intentiq.com`, EU `be-api APAC `be-api-s2s-apac.intentiq.com`, and the matching `reports-endpoint`: US `reports-s2s.intentiq.com`, EU `reports-s2s-gdpr.intentiq.com`. When `api-endpoint` is empty the hook is a no-op. -The `processed-auction-request` group timeout bounds how long the auction waits for IntentIQ. Set it -above the real round trip from the PBS host to the API (and `timeout` at or below it); a call that -outlives the group timeout is dropped and the auction proceeds without enrichment. +The `processed-auction-request` group timeout is how long the auction waits for IntentIQ. `timeout` is +how long the call itself may run. Keep `timeout` above the group timeout: a call that outlives the group +timeout keeps running in the background, and with caching on its result fills the cache, so the next +request of that user is enriched from the cache. With `timeout` at or below the group timeout, a slow call +is dropped and the auction proceeds without enrichment. ### Account-level config @@ -120,9 +122,9 @@ CTV with the id upper-cased; skipped when `device.lmt = 1`). The response `data. publisher's own eid is kept). On any failure, including a non-2xx answer, the hook takes no action and the auction proceeds unchanged. -The call is skipped (no action) when the account has no `partner-id`, or when GDPR applies -(`regs.gdpr = 1`) and the request carries no consent string (`user.consent`): IntentIQ rejects such a -request. The consent string is sent in the `gdpr-consent` header. +The call is skipped (no action) when the account has no `partner-id`, when the request is subject to +COPPA (`regs.coppa = 1`), or when GDPR applies (`regs.gdpr = 1`) and the request carries no consent +string (`user.consent`): IntentIQ rejects such a request. The consent string is sent in the `gdpr-consent` header. ### Caching @@ -175,7 +177,22 @@ one returned by the resolution response — the resolution hook stashes it in th this hook reads it. On a cache hit it is the `abTestUuid` (and `terminationCause`) of the response that filled the cache entry. With `reports-endpoint` blank the hook is a no-op. Nothing is reported when the account has no `partner-id`, -or when GDPR applies without a consent string (the report carries the user's IP and UA). The bid response is never modified. +when the request is subject to COPPA, or when GDPR applies without a consent string (the report carries the user's IP and UA). The bid response is never modified. + +### Debug trace + +Send a request with `ext.prebid.trace: "verbose"` to see what the module did, per hook, in +`ext.prebid.modules.trace` of the response (`debugmessages`): + +``` +[enrich] 1 eid(s): intentiq.com; source: cache l1 hit (device key); tc=0, abTestUuid=ab-1; took 1 ms +[enrich] skipped: regs.coppa=1; no API call, user.eids unchanged +[report] sent 1 winning bid(s) to reports-endpoint (abTestUuid=ab-1) +``` + +The source is `IntentIQ API`, a cache hit, negative entry or in-progress marker (with layer and key type), +or the same user's call in flight on this instance. A hook that did not finish in time shows +`status: timeout` and no message. ### Metrics @@ -213,6 +230,7 @@ modules.module.intentiq-identity.custom.eids.none_ # res modules.module.intentiq-identity.custom.skip.no_endpoint_ # no api-endpoint configured; resolution skipped before any API call modules.module.intentiq-identity.custom.skip.no_partner # no partner-id configured; resolution skipped modules.module.intentiq-identity.custom.skip.no_consent_ # regs.gdpr=1 without a consent string; resolution skipped +modules.module.intentiq-identity.custom.skip.coppa_ # regs.coppa=1; resolution and reporting skipped modules.module.intentiq-identity.custom.tc._ # one counter per enumerated termination-cause id modules.module.intentiq-identity.custom.impression.reported_ # winning bid reported to reports-endpoint (overall) modules.module.intentiq-identity.custom.impression.error_ # impression report call failed or answered non-2xx diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/metric/IntentiqIdentityMetrics.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/metric/IntentiqIdentityMetrics.java index 998309aec99..e317b2443e1 100644 --- a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/metric/IntentiqIdentityMetrics.java +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/metric/IntentiqIdentityMetrics.java @@ -88,6 +88,11 @@ public void skipNoConsent(String dpi) { inc("skip.no_consent", dpi); } + /** Resolution and reporting skipped because the request is subject to COPPA (regs.coppa=1). */ + public void skipCoppa(String dpi) { + inc("skip.coppa", dpi); + } + public void terminationCause(long tc, String dpi) { if (tc >= 0 && tc < 200) { inc("tc." + tc, dpi); diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityAuctionResponseHook.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityAuctionResponseHook.java index d7285819846..393e5102c5e 100644 --- a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityAuctionResponseHook.java +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityAuctionResponseHook.java @@ -17,6 +17,7 @@ import org.prebid.server.hooks.modules.intentiq.identity.model.IntentiqIdentityModuleContext; import org.prebid.server.hooks.modules.intentiq.identity.model.config.IntentiqIdentityProperties; import org.prebid.server.hooks.modules.intentiq.identity.v1.core.ConfigResolver; +import org.prebid.server.hooks.modules.intentiq.identity.v1.core.HookTrace; import org.prebid.server.hooks.modules.intentiq.identity.v1.core.IiqParam; import org.prebid.server.hooks.modules.intentiq.identity.v1.core.PrivacySignals; import org.prebid.server.hooks.v1.InvocationAction; @@ -78,6 +79,7 @@ public IntentiqIdentityAuctionResponseHook(ConfigResolver configResolver, @Override public Future> call(AuctionResponsePayload auctionResponsePayload, AuctionInvocationContext invocationContext) { + final boolean trace = HookTrace.isEnabled(invocationContext); final IntentiqIdentityProperties properties = configResolver.resolve(invocationContext.accountConfig()); final IntentiqIdentityModuleContext context = invocationContext.moduleContext() instanceof IntentiqIdentityModuleContext ctx ? ctx : null; @@ -89,25 +91,38 @@ public Future> call(AuctionResponsePayl final BidResponse bidResponse = auctionResponsePayload.bidResponse(); if (StringUtils.isBlank(properties.getReportsEndpoint()) || bidResponse == null) { - return Future.succeededFuture(noAction()); + return skipped(trace, "no reports-endpoint configured or no bid response"); } final BidRequest bidRequest = bidRequest(invocationContext); // Same gates as the resolution call: the report carries the user's IP and UA. - if (StringUtils.isBlank(properties.getPartnerId()) - || (bidRequest != null && PrivacySignals.isGdprWithoutConsent(bidRequest))) { - return Future.succeededFuture(noAction()); + if (StringUtils.isBlank(properties.getPartnerId())) { + return skipped(trace, "no partner-id configured"); + } + if (bidRequest != null && PrivacySignals.isCoppa(bidRequest)) { + metrics.skipCoppa(properties.getPartnerId()); + return skipped(trace, "regs.coppa=1"); + } + if (bidRequest != null && PrivacySignals.isGdprWithoutConsent(bidRequest)) { + return skipped(trace, "regs.gdpr=1 without a consent string"); } final String abTestUuid = context != null ? context.abTestUuid() : null; final Long terminationCause = context != null ? context.terminationCause() : null; final String currency = bidResponse.getCur() != null ? bidResponse.getCur() : DEFAULT_CURRENCY; - for (final SeatBidRef winner : winningBids(bidResponse)) { + final List winners = winningBids(bidResponse); + for (final SeatBidRef winner : winners) { report(properties, bidRequest, winner.seat(), winner.bid(), currency, abTestUuid, terminationCause); } - return Future.succeededFuture(noAction()); + return Future.succeededFuture(noAction(HookTrace.messages(trace, + "[report] sent " + winners.size() + " winning bid(s) to reports-endpoint (abTestUuid=" + + abTestUuid + ")"))); + } + + private static Future> skipped(boolean trace, String reason) { + return Future.succeededFuture(noAction(HookTrace.messages(trace, "[report] skipped: " + reason))); } // The response holds every seat's bid for an impression, but only one of them can render: report the @@ -249,10 +264,11 @@ private static String encodeComponent(String value) { return URLEncoder.encode(value, StandardCharsets.UTF_8).replace("+", "%20"); } - private static InvocationResult noAction() { + private static InvocationResult noAction(List debugMessages) { return InvocationResultImpl.builder() .status(InvocationStatus.success) .action(InvocationAction.no_action) + .debugMessages(debugMessages) .build(); } diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityProcessedAuctionRequestHook.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityProcessedAuctionRequestHook.java index 833b9302a4d..699dedc1839 100644 --- a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityProcessedAuctionRequestHook.java +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityProcessedAuctionRequestHook.java @@ -33,6 +33,7 @@ import org.prebid.server.hooks.modules.intentiq.identity.model.config.IntentiqIdentityProperties; import org.prebid.server.hooks.modules.intentiq.identity.v1.core.ConfigResolver; import org.prebid.server.hooks.modules.intentiq.identity.v1.core.FirstPartyKeyExtractor; +import org.prebid.server.hooks.modules.intentiq.identity.v1.core.HookTrace; import org.prebid.server.hooks.modules.intentiq.identity.v1.core.IiqParam; import org.prebid.server.hooks.modules.intentiq.identity.v1.core.PrivacySignals; import org.prebid.server.hooks.v1.InvocationAction; @@ -62,6 +63,7 @@ import java.util.Set; import java.util.TreeMap; import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; public class IntentiqIdentityProcessedAuctionRequestHook implements ProcessedAuctionRequestHook { @@ -112,20 +114,25 @@ public IntentiqIdentityProcessedAuctionRequestHook(ConfigResolver configResolver public Future> call(AuctionRequestPayload auctionRequestPayload, AuctionInvocationContext invocationContext) { final long startNanos = System.nanoTime(); + final boolean trace = HookTrace.isEnabled(invocationContext); final IntentiqIdentityProperties properties = configResolver.resolve(invocationContext.accountConfig()); final String dpi = properties.getPartnerId(); if (StringUtils.isBlank(properties.getApiEndpoint())) { metrics.skipNoEndpoint(dpi); - return Future.succeededFuture(noAction(new IntentiqIdentityModuleContext(startNanos, null, null))); + return skipped(startNanos, trace, "no api-endpoint configured"); } if (StringUtils.isBlank(dpi)) { metrics.skipNoPartner(dpi); - return Future.succeededFuture(noAction(new IntentiqIdentityModuleContext(startNanos, null, null))); + return skipped(startNanos, trace, "no partner-id configured"); } final BidRequest bidRequest = auctionRequestPayload.bidRequest(); + if (PrivacySignals.isCoppa(bidRequest)) { + metrics.skipCoppa(dpi); + return skipped(startNanos, trace, "regs.coppa=1"); + } if (PrivacySignals.isGdprWithoutConsent(bidRequest)) { metrics.skipNoConsent(dpi); - return Future.succeededFuture(noAction(new IntentiqIdentityModuleContext(startNanos, null, null))); + return skipped(startNanos, trace, "regs.gdpr=1 without a consent string"); } return resolveEids(properties, bidRequest) @@ -135,19 +142,44 @@ public Future> call(AuctionRequestPayloa startNanos, resolution.abTestUuid(), resolution.terminationCause()); if (eids == null || eids.isEmpty()) { metrics.eidsNone(dpi); - return noAction(context); + return noAction(context, HookTrace.messages(trace, resolvedMessage(resolution, startNanos))); } metrics.enriched(dpi); - return update(payload -> enrichUserEids(payload, eids), context); + return update(payload -> enrichUserEids(payload, eids), context, + HookTrace.messages(trace, resolvedMessage(resolution, startNanos))); }) .otherwise(throwable -> { conditionalLogger.warn("IntentIQ identity resolution failed, proceeding without enrichment: " + throwable.getMessage(), LOG_SAMPLING_RATE); metrics.apiError(dpi); - return noAction(new IntentiqIdentityModuleContext(startNanos, null, null)); + return noAction(new IntentiqIdentityModuleContext(startNanos, null, null), + HookTrace.messages(trace, "[enrich] failed: " + throwable.getMessage() + + "; auction continues without IntentIQ ids; took " + millisSince(startNanos) + + " ms")); }); } + private static Future> skipped(long startNanos, + boolean trace, + String reason) { + return Future.succeededFuture(noAction(new IntentiqIdentityModuleContext(startNanos, null, null), + HookTrace.messages(trace, "[enrich] skipped: " + reason + "; no API call, user.eids unchanged"))); + } + + private static String resolvedMessage(Resolution resolution, long startNanos) { + final List eids = resolution.eids(); + final String result = eids == null || eids.isEmpty() + ? "no ids" + : eids.size() + " eid(s): " + eids.stream().map(Eid::getSource).collect(Collectors.joining(", ")); + return "[enrich] " + result + "; source: " + resolution.source() + + "; tc=" + resolution.terminationCause() + ", abTestUuid=" + resolution.abTestUuid() + + "; took " + millisSince(startNanos) + " ms"; + } + + private static long millisSince(long startNanos) { + return TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startNanos); + } + private Future resolveEids(IntentiqIdentityProperties properties, BidRequest bidRequest) { final boolean cacheEnabled = cache != null && properties.getCache().isEnabled(); final List keys = cacheEnabled ? keyExtractor.candidateKeys(bidRequest, properties.getPartnerId()) @@ -155,7 +187,7 @@ private Future resolveEids(IntentiqIdentityProperties properties, Bi if (keys.isEmpty()) { return fetch(properties, bidRequest) .map(response -> new Resolution( - extractEids(response), response.getAbTestUuid(), response.getTc())); + extractEids(response), response.getAbTestUuid(), response.getTc(), "IntentIQ API")); } final String dpi = properties.getPartnerId(); @@ -170,7 +202,11 @@ private Future resolveEids(IntentiqIdentityProperties properties, Bi if (inFlight != null) { metrics.inFlightJoined(dpi); // The failure of the shared call is counted once, by the request that made it. - return inFlight.recover(ignored -> Future.succeededFuture(new Resolution(null, null, null))); + return inFlight + .map(resolution -> resolution.withSource( + "the same user's call in flight on this instance (" + resolution.source() + ")")) + .recover(ignored -> Future.succeededFuture(new Resolution(null, null, null, + "the same user's call in flight on this instance, which failed"))); } owned.future().onComplete(ignored -> keys.forEach(key -> inFlightByKey.remove(key.key(), owned.future()))); @@ -201,8 +237,9 @@ private Future lookupAndResolve(IntentiqIdentityProperties propertie return cache.get(keys).compose(result -> switch (result.state()) { case HIT -> { metrics.cacheHit(layerToken(result.layer()), keyTypeToken(result.keyType()), dpi); - yield Future.succeededFuture( - new Resolution(result.eids(), result.abTestUuid(), result.terminationCause())); + yield Future.succeededFuture(new Resolution(result.eids(), result.abTestUuid(), + result.terminationCause(), "cache " + layerToken(result.layer()) + " hit (" + + keyTypeToken(result.keyType()) + " key)")); } case NEGATIVE -> { // A negative sentinel is a cached miss (id is known-unresolvable): no identity to @@ -211,14 +248,17 @@ private Future lookupAndResolve(IntentiqIdentityProperties propertie final String type = keyTypeToken(result.keyType()); metrics.cacheMiss(type, dpi); metrics.cacheNegativeHit(layerToken(result.layer()), type, dpi); - yield Future.succeededFuture( - new Resolution(null, result.abTestUuid(), result.terminationCause())); + yield Future.succeededFuture(new Resolution(null, result.abTestUuid(), result.terminationCause(), + "cache " + layerToken(result.layer()) + " negative entry (" + type + + " key), no API call")); } case IN_PROGRESS -> { // A resolution call for this id is already in flight; skip firing a duplicate and // proceed without enrichment (the in-flight call will populate the cache). metrics.cacheInProgress(layerToken(result.layer()), keyTypeToken(result.keyType()), dpi); - yield Future.succeededFuture(new Resolution(null, null, null)); + yield Future.succeededFuture(new Resolution(null, null, null, + "cache " + layerToken(result.layer()) + " in-progress marker (" + + keyTypeToken(result.keyType()) + " key): another PBS instance is calling the API")); } case MISS -> { metrics.cacheMiss(primaryType, dpi); @@ -250,7 +290,8 @@ private Future fetchAndCache(IntentiqIdentityProperties properties, } else { cache.putNegative(keys, response.getAbTestUuid(), response.getTc(), cttl); } - return new Resolution(eids, response.getAbTestUuid(), response.getTc()); + return new Resolution(eids, response.getAbTestUuid(), response.getTc(), + "IntentIQ API (cache miss)"); }) // A failed call caches nothing: drop its in-progress marker so the next request retries. .onFailure(ignored -> cache.clearInProgress(keys)); @@ -474,20 +515,24 @@ private static AuctionRequestPayload enrichUserEids(AuctionRequestPayload payloa } private static InvocationResult update(PayloadUpdate payloadUpdate, - IntentiqIdentityModuleContext context) { + IntentiqIdentityModuleContext context, + List debugMessages) { return InvocationResultImpl.builder() .status(InvocationStatus.success) .action(InvocationAction.update) .payloadUpdate(payloadUpdate) .moduleContext(context) + .debugMessages(debugMessages) .build(); } - private static InvocationResult noAction(IntentiqIdentityModuleContext context) { + private static InvocationResult noAction(IntentiqIdentityModuleContext context, + List debugMessages) { return InvocationResultImpl.builder() .status(InvocationStatus.success) .action(InvocationAction.no_action) .moduleContext(context) + .debugMessages(debugMessages) .build(); } @@ -496,7 +541,12 @@ public String code() { return CODE; } - private record Resolution(List eids, String abTestUuid, Long terminationCause) { + // source: where the result came from, for the debug trace. + private record Resolution(List eids, String abTestUuid, Long terminationCause, String source) { + + Resolution withSource(String newSource) { + return new Resolution(eids, abTestUuid, terminationCause, newSource); + } } @Data diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/HookTrace.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/HookTrace.java new file mode 100644 index 00000000000..51692002641 --- /dev/null +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/HookTrace.java @@ -0,0 +1,29 @@ +package org.prebid.server.hooks.modules.intentiq.identity.v1.core; + +import org.prebid.server.auction.model.AuctionContext; +import org.prebid.server.hooks.v1.auction.AuctionInvocationContext; +import org.prebid.server.proto.openrtb.ext.request.TraceLevel; + +import java.util.List; + +/** + * Per-request debug messages of the module. PBS shows a hook's debug messages in + * {@code ext.prebid.modules.trace} only when the request asks for {@code ext.prebid.trace: verbose}, so the + * messages are built only then. + */ +public final class HookTrace { + + private HookTrace() { + } + + public static boolean isEnabled(AuctionInvocationContext invocationContext) { + final AuctionContext auctionContext = invocationContext != null ? invocationContext.auctionContext() : null; + return auctionContext != null + && auctionContext.getDebugContext() != null + && auctionContext.getDebugContext().getTraceLevel() == TraceLevel.verbose; + } + + public static List messages(boolean enabled, String message) { + return enabled ? List.of(message) : null; + } +} diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/PrivacySignals.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/PrivacySignals.java index b0514cad2f1..a9af28983f7 100644 --- a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/PrivacySignals.java +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/v1/core/PrivacySignals.java @@ -65,6 +65,12 @@ public static boolean isGdprWithoutConsent(BidRequest bidRequest) { return "1".equals(gdpr(bidRequest)) && StringUtils.isBlank(consent(bidRequest)); } + /** The request is subject to COPPA ({@code regs.coppa = 1}). */ + public static boolean isCoppa(BidRequest bidRequest) { + final Regs regs = bidRequest.getRegs(); + return regs != null && Integer.valueOf(1).equals(regs.getCoppa()); + } + /** * A short, stable digest of all privacy signals of the request. Requests whose signals differ (e.g. a * user who changed their consent or opted out) get different fingerprints. diff --git a/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityAuctionResponseHookTest.java b/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityAuctionResponseHookTest.java index c6cd73ca550..44cb51fb83c 100644 --- a/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityAuctionResponseHookTest.java +++ b/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityAuctionResponseHookTest.java @@ -303,6 +303,22 @@ public void callShouldNotReportWhenGdprAppliesWithoutConsent() { verifyNoInteractions(httpClient); } + @Test + public void callShouldNotReportWhenCoppaApplies() { + // given + givenRequest(BidRequest.builder() + .regs(Regs.builder().coppa(1).build()) + .device(Device.builder().ip("1.2.3.4").ua("UA").build()) + .build()); + + // when + target(REPORTS, "999").call(payload(response("seat", Bid.builder().impid("a").price(BigDecimal.ONE) + .build())), invocationContext); + + // then + verifyNoInteractions(httpClient); + } + @Test public void callShouldReportWhenGdprAppliesWithConsent() { // given diff --git a/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityProcessedAuctionRequestHookTest.java b/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityProcessedAuctionRequestHookTest.java index afaf37ef14a..a1c727984d6 100644 --- a/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityProcessedAuctionRequestHookTest.java +++ b/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/v1/IntentiqIdentityProcessedAuctionRequestHookTest.java @@ -12,8 +12,6 @@ import com.iab.openrtb.request.Uid; import com.iab.openrtb.request.User; import com.iab.openrtb.request.UserAgent; -import org.prebid.server.proto.openrtb.ext.request.ExtRegs; -import org.prebid.server.proto.openrtb.ext.request.ExtUser; import io.vertx.core.Future; import io.vertx.core.MultiMap; import io.vertx.core.Promise; @@ -26,6 +24,8 @@ import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoSettings; import org.mockito.quality.Strictness; +import org.prebid.server.auction.model.AuctionContext; +import org.prebid.server.auction.model.debug.DebugContext; import org.prebid.server.hooks.execution.v1.auction.AuctionRequestPayloadImpl; import org.prebid.server.hooks.modules.intentiq.identity.cache.CacheResult; import org.prebid.server.hooks.modules.intentiq.identity.cache.IdentityCache; @@ -44,6 +44,9 @@ import org.prebid.server.json.JacksonMapper; import org.prebid.server.json.JsonMerger; import org.prebid.server.json.ObjectMapperProvider; +import org.prebid.server.proto.openrtb.ext.request.ExtRegs; +import org.prebid.server.proto.openrtb.ext.request.ExtUser; +import org.prebid.server.proto.openrtb.ext.request.TraceLevel; import org.prebid.server.vertx.httpclient.HttpClient; import org.prebid.server.vertx.httpclient.model.HttpClientResponse; @@ -869,6 +872,71 @@ public void callShouldSkipHttpWhenGdprAppliesWithoutConsent() { verifyNoInteractions(httpClient); } + @Test + public void callShouldSkipHttpWhenCoppaApplies() { + // given + final BidRequest bidRequest = BidRequest.builder().regs(Regs.builder().coppa(1).build()).build(); + + // when + final InvocationResult result = target(API_ENDPOINT, "123") + .call(AuctionRequestPayloadImpl.of(bidRequest), invocationContext).result(); + + // then + assertThat(result.action()).isEqualTo(InvocationAction.no_action); + assertThat(counter("skip.coppa")).isEqualTo(1); + verifyNoInteractions(httpClient); + } + + @Test + public void callShouldReturnDebugMessagesWhenVerboseTraceRequested() { + // given + givenResponse(EIDS_BODY); + givenTraceLevel(TraceLevel.verbose); + + // when + final InvocationResult result = target(API_ENDPOINT, "123") + .call(AuctionRequestPayloadImpl.of(BidRequest.builder().build()), invocationContext).result(); + + // then + assertThat(result.debugMessages()).singleElement().asString() + .startsWith("[enrich] 1 eid(s): intentiq.com; source: IntentIQ API"); + } + + @Test + public void callShouldReturnSkipReasonAsDebugMessageWhenVerboseTraceRequested() { + // given + givenTraceLevel(TraceLevel.verbose); + final BidRequest bidRequest = BidRequest.builder().regs(Regs.builder().coppa(1).build()).build(); + + // when + final InvocationResult result = target(API_ENDPOINT, "123") + .call(AuctionRequestPayloadImpl.of(bidRequest), invocationContext).result(); + + // then + assertThat(result.debugMessages()) + .containsExactly("[enrich] skipped: regs.coppa=1; no API call, user.eids unchanged"); + } + + @Test + public void callShouldNotReturnDebugMessagesWithoutVerboseTrace() { + // given + givenResponse(EIDS_BODY); + givenTraceLevel(TraceLevel.basic); + + // when + final InvocationResult result = target(API_ENDPOINT, "123") + .call(AuctionRequestPayloadImpl.of(BidRequest.builder().build()), invocationContext).result(); + + // then + assertThat(result.debugMessages()).isNull(); + } + + private void givenTraceLevel(TraceLevel traceLevel) { + when(invocationContext.auctionContext()).thenReturn(AuctionContext.builder() + .debugContext(DebugContext.of(true, false, traceLevel)) + .build()); + } + @Test public void callShouldCallApiWhenGdprDoesNotApplyWithoutConsent() { // given From df66466dcebbe64ecb90bdd1f9fbfa3888230bce Mon Sep 17 00:00:00 2001 From: Ben Gojanski Date: Mon, 16 Nov 2026 22:16:04 +0200 Subject: [PATCH 6/6] IntentIQ Identity: Aerospike as the shared cache store cache.provider: aerospike stores the shared (L2) cache in Aerospike (namespace.set, value in bin "v", per-record TTL) through the client's async API, completing on the caller's Vert.x context. PBS starts while Aerospike is down; the client reconnects and the cache fails open. Redis (or Valkey) stays the default provider. Co-Authored-By: Claude Opus 5.5 --- extra/modules/intentiq-identity/README.md | 32 +++- extra/modules/intentiq-identity/pom.xml | 6 + .../cache/AerospikeIdentityStore.java | 153 +++++++++++++++++ .../config/IntentiqIdentityConfig.java | 48 +++++- .../model/config/AerospikeProperties.java | 37 +++++ .../model/config/CacheProperties.java | 3 + .../config/IntentiqIdentityProperties.java | 2 + .../cache/AerospikeIdentityStoreTest.java | 155 ++++++++++++++++++ .../config/IntentiqIdentityConfigTest.java | 29 ++++ 9 files changed, 461 insertions(+), 4 deletions(-) create mode 100644 extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/AerospikeIdentityStore.java create mode 100644 extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/model/config/AerospikeProperties.java create mode 100644 extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/cache/AerospikeIdentityStoreTest.java diff --git a/extra/modules/intentiq-identity/README.md b/extra/modules/intentiq-identity/README.md index e3310bc2548..feb310dcbb4 100644 --- a/extra/modules/intentiq-identity/README.md +++ b/extra/modules/intentiq-identity/README.md @@ -113,6 +113,17 @@ with `cache.enabled: false`, but caching on an account needs it enabled on the h | redis.cluster | host | no | boolean | false | Redis / Valkey cluster mode (e.g. ElastiCache Serverless or cluster mode enabled) | | redis.max-pool-size | host | no | int | 32 | Max pooled Redis connections | | redis.max-pool-waiting | host | no | int | 1024 | Max commands queued for a free connection before failing fast | +| cache.provider | host | no | string | redis | Shared (L2) store: `redis` (also Valkey) or `aerospike` | +| aerospike.host | host | cond. | string | none | Aerospike seed host (required with `cache.provider: aerospike`) | +| aerospike.port | host | no | int | 3000 | Aerospike port | +| aerospike.namespace | host | cond. | string | none | Namespace (required); must allow TTLs, see below | +| aerospike.set | host | cond. | string | none | Set (required) | +| aerospike.user | host | no | string | none | User, when security is enabled (Enterprise Edition) | +| aerospike.password | host | no | string | none | Password | +| aerospike.timeout-ms | host | no | int | 100 | Total timeout of one read, write or delete | +| aerospike.connect-timeout-ms | host | no | int | 1000 | Timeout of the initial cluster connection | +| aerospike.max-connections-per-node | host | no | int | 100 | Max async connections per node | +| aerospike.event-loop-threads | host | no | int | 2 | Threads of the Aerospike client's event loop | The hook sends `at/mi/pt/dpn/srvrReq` constants plus `dpi` (= `partner-id`), and — when present on the request — `ip`, `ipv6`, `uas`, `ref` (site domain / app name), `iiquid` (an existing @@ -129,9 +140,24 @@ string (`user.consent`): IntentIQ rejects such a request. The consent string is ### Caching When caching is enabled, resolved eids are cached in two layers: **Caffeine** (L1, in-process) backed -by an `IdentityStore` (L2, shared) — **Redis** by default. L2 failures are non-fatal: the hook falls -through to a live API call. A partner can use a non-Redis backend by supplying their own -`IdentityStore` bean (the default Redis store is `@ConditionalOnMissingBean`). +by an `IdentityStore` (L2, shared): **Redis** (or Valkey) by default, or **Aerospike** with +`cache.provider: aerospike`. L2 failures are non-fatal: the hook falls through to a live API call, and PBS +starts even when the store is down. A partner can use another backend by supplying their own +`IdentityStore` bean (the built-in store is `@ConditionalOnMissingBean`). + +```yaml + cache: + enabled: true + provider: aerospike + aerospike: + host: aerospike.internal + namespace: cache + set: iiq +``` + +Aerospike entries are written with a TTL, so the namespace must have `nsup-period` above 0 (or +`allow-ttl-without-nsup true`). Otherwise Aerospike rejects every write: the module still works, but only +from the local layer. **Multi-key (alias) caching.** Every relevant first-party id on the request becomes a namespaced alias key, ordered by priority: `iiq:` (`intentiq.com`), `pubcid:` (`pubcid.org` / diff --git a/extra/modules/intentiq-identity/pom.xml b/extra/modules/intentiq-identity/pom.xml index a0def6b962f..4cfcfba959c 100644 --- a/extra/modules/intentiq-identity/pom.xml +++ b/extra/modules/intentiq-identity/pom.xml @@ -16,6 +16,7 @@ 1.6.1 + 10.5.0 @@ -28,5 +29,10 @@ uap-java ${uap-java.version} + + com.aerospike + aerospike-client-jdk21 + ${aerospike-client.version} + diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/AerospikeIdentityStore.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/AerospikeIdentityStore.java new file mode 100644 index 00000000000..303fdfb92dc --- /dev/null +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/cache/AerospikeIdentityStore.java @@ -0,0 +1,153 @@ +package org.prebid.server.hooks.modules.intentiq.identity.cache; + +import com.aerospike.client.AerospikeException; +import com.aerospike.client.Bin; +import com.aerospike.client.IAerospikeClient; +import com.aerospike.client.Key; +import com.aerospike.client.Record; +import com.aerospike.client.async.EventLoops; +import com.aerospike.client.listener.DeleteListener; +import com.aerospike.client.listener.RecordListener; +import com.aerospike.client.listener.WriteListener; +import com.aerospike.client.policy.Policy; +import com.aerospike.client.policy.WritePolicy; +import io.vertx.core.Context; +import io.vertx.core.Future; +import io.vertx.core.Promise; +import io.vertx.core.Vertx; + +import java.util.Objects; + +/** + * Aerospike-backed {@link IdentityStore}: one record per key in {@code namespace.set}, the value in bin + * {@code v}, with a per-record TTL. Uses the client's async API, so no thread waits on Aerospike. + * + *

    The namespace must allow TTLs ({@code nsup-period} above 0, or {@code allow-ttl-without-nsup}), or + * Aerospike rejects every write; the cache then fails open and works from the local layer only. + */ +public class AerospikeIdentityStore implements IdentityStore, AutoCloseable { + + private static final String VALUE_BIN = "v"; + + private final IAerospikeClient client; + private final EventLoops eventLoops; + private final String namespace; + private final String set; + private final Policy readPolicy; + private final WritePolicy deletePolicy; + private final int timeoutMs; + + public AerospikeIdentityStore(IAerospikeClient client, + EventLoops eventLoops, + String namespace, + String set, + int timeoutMs) { + this.client = Objects.requireNonNull(client); + this.eventLoops = Objects.requireNonNull(eventLoops); + this.namespace = Objects.requireNonNull(namespace); + this.set = Objects.requireNonNull(set); + this.timeoutMs = timeoutMs; + this.readPolicy = new Policy(); + this.readPolicy.setTimeout(timeoutMs); + this.deletePolicy = writePolicy(timeoutMs, 0); + } + + @Override + public Future get(String key) { + final Context context = Vertx.currentContext(); + final Promise promise = Promise.promise(); + try { + client.get(eventLoops.next(), new RecordListener() { + @Override + public void onSuccess(Key aerospikeKey, Record record) { + final Object value = record != null ? record.getValue(VALUE_BIN) : null; + complete(context, () -> promise.complete(value != null ? value.toString() : null)); + } + + @Override + public void onFailure(AerospikeException exception) { + complete(context, () -> promise.fail(exception)); + } + }, readPolicy, key(key), VALUE_BIN); + } catch (AerospikeException e) { + promise.fail(e); + } + return promise.future(); + } + + @Override + public Future put(String key, String value, long ttlMs) { + final Context context = Vertx.currentContext(); + final Promise promise = Promise.promise(); + try { + client.put(eventLoops.next(), new WriteListener() { + @Override + public void onSuccess(Key aerospikeKey) { + complete(context, promise::complete); + } + + @Override + public void onFailure(AerospikeException exception) { + complete(context, () -> promise.fail(exception)); + } + }, writePolicy(timeoutMs, expirationSeconds(ttlMs)), key(key), new Bin(VALUE_BIN, value)); + } catch (AerospikeException e) { + promise.fail(e); + } + return promise.future(); + } + + @Override + public Future delete(String key) { + final Context context = Vertx.currentContext(); + final Promise promise = Promise.promise(); + try { + client.delete(eventLoops.next(), new DeleteListener() { + @Override + public void onSuccess(Key aerospikeKey, boolean existed) { + complete(context, promise::complete); + } + + @Override + public void onFailure(AerospikeException exception) { + complete(context, () -> promise.fail(exception)); + } + }, deletePolicy, key(key)); + } catch (AerospikeException e) { + promise.fail(e); + } + return promise.future(); + } + + @Override + public void close() { + client.close(); + eventLoops.close(); + } + + private Key key(String key) { + return new Key(namespace, set, key); + } + + private static WritePolicy writePolicy(int timeoutMs, int expirationSeconds) { + final WritePolicy policy = new WritePolicy(); + policy.setTimeout(timeoutMs); + policy.expiration = expirationSeconds; + return policy; + } + + // Aerospike TTLs are whole seconds; round up so an entry never expires before the cache meant it to. + static int expirationSeconds(long ttlMs) { + final long seconds = (ttlMs + 999) / 1000; + return (int) Math.max(1, Math.min(Integer.MAX_VALUE, seconds)); + } + + // Completes the caller's future on the caller's Vert.x context, not on the Aerospike event loop thread. + private static void complete(Context context, Runnable completion) { + if (context != null) { + context.runOnContext(ignored -> completion.run()); + } else { + completion.run(); + } + } +} diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/config/IntentiqIdentityConfig.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/config/IntentiqIdentityConfig.java index 4fa3550e03d..e6fc74d3005 100644 --- a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/config/IntentiqIdentityConfig.java +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/config/IntentiqIdentityConfig.java @@ -1,5 +1,11 @@ package org.prebid.server.hooks.modules.intentiq.identity.config; +import com.aerospike.client.AerospikeClient; +import com.aerospike.client.Host; +import com.aerospike.client.async.EventLoops; +import com.aerospike.client.async.EventPolicy; +import com.aerospike.client.async.NioEventLoops; +import com.aerospike.client.policy.ClientPolicy; import com.codahale.metrics.MetricRegistry; import io.vertx.core.Vertx; import io.vertx.core.net.NetClientOptions; @@ -9,6 +15,7 @@ import io.vertx.redis.client.RedisClientType; import io.vertx.redis.client.RedisOptions; import org.apache.commons.lang3.StringUtils; +import org.prebid.server.hooks.modules.intentiq.identity.cache.AerospikeIdentityStore; import org.prebid.server.hooks.modules.intentiq.identity.cache.CacheTtlPolicy; import org.prebid.server.hooks.modules.intentiq.identity.cache.IdentityCache; import org.prebid.server.hooks.modules.intentiq.identity.cache.IdentityStore; @@ -16,6 +23,7 @@ import org.prebid.server.hooks.modules.intentiq.identity.cache.RedisStatsReporter; import org.prebid.server.hooks.modules.intentiq.identity.metric.IntentiqIdentityMetrics; import org.prebid.server.hooks.modules.intentiq.identity.metric.NoopIntentiqIdentityMetrics; +import org.prebid.server.hooks.modules.intentiq.identity.model.config.AerospikeProperties; import org.prebid.server.hooks.modules.intentiq.identity.model.config.CacheProperties; import org.prebid.server.hooks.modules.intentiq.identity.model.config.IntentiqIdentityProperties; import org.prebid.server.hooks.modules.intentiq.identity.model.config.RedisProperties; @@ -46,6 +54,9 @@ public class IntentiqIdentityConfig { private static final String HOSTNAME_VERIFICATION_HTTPS = "HTTPS"; + private static final String PROVIDER_REDIS = "redis"; + private static final String PROVIDER_AEROSPIKE = "aerospike"; + @Bean @ConfigurationProperties(prefix = "hooks.modules." + IntentiqIdentityModule.CODE) IntentiqIdentityProperties intentiqIdentityProperties() { @@ -65,7 +76,15 @@ ConfigResolver intentiqIdentityConfigResolver(JsonMerger jsonMerger, IntentiqIde @ConditionalOnProperty(prefix = "hooks.modules." + IntentiqIdentityModule.CODE + ".cache", name = "enabled", havingValue = "true") IdentityStore intentiqIdentityStore(IntentiqIdentityProperties properties, Vertx vertx) { - return new RedisIdentityStore(createRedisApi(properties.getRedis(), vertx)); + final String provider = properties.getCache().getProvider(); + if (StringUtils.isBlank(provider) || PROVIDER_REDIS.equalsIgnoreCase(provider)) { + return new RedisIdentityStore(createRedisApi(properties.getRedis(), vertx)); + } + if (PROVIDER_AEROSPIKE.equalsIgnoreCase(provider)) { + return createAerospikeStore(properties.getAerospike()); + } + throw new IllegalArgumentException("hooks.modules." + IntentiqIdentityModule.CODE + + ".cache.provider must be redis or aerospike, not: " + provider); } @Bean @@ -133,6 +152,33 @@ IntentiqIdentityModule intentiqIdentityModule(ConfigResolver configResolver, new IntentiqIdentityAuctionResponseHook(configResolver, httpClient, mapper, metrics))); } + private static AerospikeIdentityStore createAerospikeStore(AerospikeProperties aerospike) { + if (aerospike == null || StringUtils.isAnyBlank(aerospike.getHost(), aerospike.getNamespace(), + aerospike.getSet())) { + throw new IllegalArgumentException("hooks.modules." + IntentiqIdentityModule.CODE + + ".aerospike.host, namespace and set are required when cache.provider is aerospike"); + } + + final EventLoops eventLoops = new NioEventLoops(new EventPolicy(), aerospike.getEventLoopThreads()); + final ClientPolicy policy = new ClientPolicy(); + policy.eventLoops = eventLoops; + // PBS starts even when Aerospike is down; the client keeps reconnecting and the cache fails open. + policy.failIfNotConnected = false; + policy.timeout = aerospike.getConnectTimeoutMs(); + policy.asyncMaxConnsPerNode = aerospike.getMaxConnectionsPerNode(); + if (StringUtils.isNotBlank(aerospike.getUser())) { + policy.user = aerospike.getUser(); + policy.password = aerospike.getPassword(); + } + + return new AerospikeIdentityStore( + new AerospikeClient(policy, new Host(aerospike.getHost(), aerospike.getPort())), + eventLoops, + aerospike.getNamespace(), + aerospike.getSet(), + aerospike.getTimeoutMs()); + } + private static RedisAPI createRedisApi(RedisProperties redis, Vertx vertx) { if (redis == null || StringUtils.isBlank(redis.getHost())) { throw new IllegalArgumentException("hooks.modules." + IntentiqIdentityModule.CODE diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/model/config/AerospikeProperties.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/model/config/AerospikeProperties.java new file mode 100644 index 00000000000..2d9b4ed60a8 --- /dev/null +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/model/config/AerospikeProperties.java @@ -0,0 +1,37 @@ +package org.prebid.server.hooks.modules.intentiq.identity.model.config; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Data; + +@Data +public final class AerospikeProperties { + + String host; + + Integer port = 3000; + + String namespace; + + String set; + + /** Enterprise Edition security user, if security is enabled. */ + String user; + + String password; + + /** Timeout of the initial cluster connection. */ + @JsonProperty("connect-timeout-ms") + int connectTimeoutMs = 1_000; + + /** Total timeout of one read, write or delete. */ + @JsonProperty("timeout-ms") + int timeoutMs = 100; + + /** Max async connections per cluster node. */ + @JsonProperty("max-connections-per-node") + int maxConnectionsPerNode = 100; + + /** Threads of the client's event loop. */ + @JsonProperty("event-loop-threads") + int eventLoopThreads = 2; +} diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/model/config/CacheProperties.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/model/config/CacheProperties.java index 59393e1a85d..4229afdf66a 100644 --- a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/model/config/CacheProperties.java +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/model/config/CacheProperties.java @@ -10,6 +10,9 @@ public class CacheProperties { private boolean enabled; + /** Shared (L2) store: {@code redis} (also Valkey) or {@code aerospike}. */ + private String provider = "redis"; + /** Positive TTL used when the IntentIQ API omits {@code cttl}. */ private int ttlseconds = 43_200; diff --git a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/model/config/IntentiqIdentityProperties.java b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/model/config/IntentiqIdentityProperties.java index 066c7ed7b56..01066359f64 100644 --- a/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/model/config/IntentiqIdentityProperties.java +++ b/extra/modules/intentiq-identity/src/main/java/org/prebid/server/hooks/modules/intentiq/identity/model/config/IntentiqIdentityProperties.java @@ -27,6 +27,8 @@ public final class IntentiqIdentityProperties { RedisProperties redis; + AerospikeProperties aerospike; + @JsonProperty("cache-max-size") long cacheMaxSize = 100_000L; diff --git a/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/cache/AerospikeIdentityStoreTest.java b/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/cache/AerospikeIdentityStoreTest.java new file mode 100644 index 00000000000..8793693a21c --- /dev/null +++ b/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/cache/AerospikeIdentityStoreTest.java @@ -0,0 +1,155 @@ +package org.prebid.server.hooks.modules.intentiq.identity.cache; + +import com.aerospike.client.AerospikeException; +import com.aerospike.client.Bin; +import com.aerospike.client.IAerospikeClient; +import com.aerospike.client.Key; +import com.aerospike.client.Record; +import com.aerospike.client.ResultCode; +import com.aerospike.client.async.EventLoops; +import com.aerospike.client.listener.DeleteListener; +import com.aerospike.client.listener.RecordListener; +import com.aerospike.client.listener.WriteListener; +import com.aerospike.client.policy.Policy; +import com.aerospike.client.policy.WritePolicy; +import io.vertx.core.Future; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentCaptor; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.Map; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.verify; + +@ExtendWith(MockitoExtension.class) +public class AerospikeIdentityStoreTest { + + @Mock + private IAerospikeClient client; + + @Mock + private EventLoops eventLoops; + + private AerospikeIdentityStore target; + + @BeforeEach + public void setUp() { + target = new AerospikeIdentityStore(client, eventLoops, "ns", "iiq", 100); + } + + @Test + public void getShouldReturnStoredValue() { + // given + doAnswer(invocation -> { + invocation.getArgument(1).onSuccess(invocation.getArgument(3), + new Record(Map.of("v", "cached"), 1, 100)); + return null; + }).when(client).get(any(), any(RecordListener.class), any(Policy.class), any(Key.class), eq("v")); + + // when + final Future result = target.get("key"); + + // then + assertThat(result.result()).isEqualTo("cached"); + final ArgumentCaptor key = ArgumentCaptor.forClass(Key.class); + verify(client).get(any(), any(RecordListener.class), any(Policy.class), key.capture(), eq("v")); + assertThat(key.getValue().namespace).isEqualTo("ns"); + assertThat(key.getValue().setName).isEqualTo("iiq"); + assertThat(key.getValue().userKey.toString()).isEqualTo("key"); + } + + @Test + public void getShouldReturnNullWhenRecordIsMissing() { + // given + doAnswer(invocation -> { + invocation.getArgument(1).onSuccess(invocation.getArgument(3), null); + return null; + }).when(client).get(any(), any(RecordListener.class), any(Policy.class), any(Key.class), eq("v")); + + // when and then + final Future result = target.get("key"); + assertThat(result.succeeded()).isTrue(); + assertThat(result.result()).isNull(); + } + + @Test + public void getShouldFailWhenAerospikeFails() { + // given + doAnswer(invocation -> { + invocation.getArgument(1).onFailure(new AerospikeException(ResultCode.TIMEOUT)); + return null; + }).when(client).get(any(), any(RecordListener.class), any(Policy.class), any(Key.class), eq("v")); + + // when and then + assertThat(target.get("key").failed()).isTrue(); + } + + @Test + public void putShouldWriteValueWithTtlRoundedUpToSeconds() { + // given + doAnswer(invocation -> { + invocation.getArgument(1).onSuccess(invocation.getArgument(3)); + return null; + }).when(client).put(any(), any(WriteListener.class), any(WritePolicy.class), any(Key.class), any(Bin.class)); + + // when + final Future result = target.put("key", "value", 1_500L); + + // then + assertThat(result.succeeded()).isTrue(); + final ArgumentCaptor policy = ArgumentCaptor.forClass(WritePolicy.class); + final ArgumentCaptor bin = ArgumentCaptor.forClass(Bin.class); + verify(client).put(any(), any(WriteListener.class), policy.capture(), any(Key.class), bin.capture()); + assertThat(policy.getValue().expiration).isEqualTo(2); + assertThat(bin.getValue().name).isEqualTo("v"); + assertThat(bin.getValue().value.toString()).isEqualTo("value"); + } + + @Test + public void putShouldFailWhenAerospikeRejectsTheWrite() { + // given + doAnswer(invocation -> { + invocation.getArgument(1).onFailure(new AerospikeException(ResultCode.FAIL_FORBIDDEN)); + return null; + }).when(client).put(any(), any(WriteListener.class), any(WritePolicy.class), any(Key.class), any(Bin.class)); + + // when and then + assertThat(target.put("key", "value", 1_000L).failed()).isTrue(); + } + + @Test + public void deleteShouldSucceedWhetherOrNotTheRecordExisted() { + // given + doAnswer(invocation -> { + invocation.getArgument(1).onSuccess(invocation.getArgument(3), false); + return null; + }).when(client).delete(any(), any(DeleteListener.class), any(WritePolicy.class), any(Key.class)); + + // when and then + assertThat(target.delete("key").succeeded()).isTrue(); + } + + @Test + public void expirationSecondsShouldNeverBeBelowOneSecond() { + assertThat(AerospikeIdentityStore.expirationSeconds(1L)).isEqualTo(1); + assertThat(AerospikeIdentityStore.expirationSeconds(0L)).isEqualTo(1); + assertThat(AerospikeIdentityStore.expirationSeconds(60_000L)).isEqualTo(60); + } + + @Test + public void closeShouldCloseClientAndEventLoops() { + // when + target.close(); + + // then + verify(client).close(); + verify(eventLoops).close(); + } +} diff --git a/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/config/IntentiqIdentityConfigTest.java b/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/config/IntentiqIdentityConfigTest.java index a96d7fba1a0..d3e359e06fb 100644 --- a/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/config/IntentiqIdentityConfigTest.java +++ b/extra/modules/intentiq-identity/src/test/java/org/prebid/server/hooks/modules/intentiq/identity/config/IntentiqIdentityConfigTest.java @@ -13,6 +13,7 @@ import org.prebid.server.hooks.modules.intentiq.identity.cache.RedisStatsReporter; import org.prebid.server.hooks.modules.intentiq.identity.metric.IntentiqIdentityMetrics; import org.prebid.server.hooks.modules.intentiq.identity.metric.NoopIntentiqIdentityMetrics; +import org.prebid.server.hooks.modules.intentiq.identity.model.config.AerospikeProperties; import org.prebid.server.hooks.modules.intentiq.identity.model.config.IntentiqIdentityProperties; import org.prebid.server.hooks.modules.intentiq.identity.model.config.RedisProperties; import org.prebid.server.hooks.modules.intentiq.identity.v1.IntentiqIdentityModule; @@ -94,6 +95,34 @@ public void identityStoreBeanShouldFailWhenRedisHostMissing() { .isInstanceOf(IllegalArgumentException.class); } + @Test + public void identityStoreBeanShouldFailWhenAerospikeNamespaceMissing() { + // given + final AerospikeProperties aerospike = new AerospikeProperties(); + aerospike.setHost("localhost"); + aerospike.setSet("iiq"); + final IntentiqIdentityProperties properties = new IntentiqIdentityProperties(); + properties.getCache().setProvider("aerospike"); + properties.setAerospike(aerospike); + + // when and then + assertThatThrownBy(() -> target.intentiqIdentityStore(properties, vertx)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("namespace"); + } + + @Test + public void identityStoreBeanShouldFailWhenCacheProviderUnknown() { + // given + final IntentiqIdentityProperties properties = new IntentiqIdentityProperties(); + properties.getCache().setProvider("memcached"); + + // when and then + assertThatThrownBy(() -> target.intentiqIdentityStore(properties, vertx)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessageContaining("memcached"); + } + @Test public void identityStoreBeanShouldFailWhenRedisPortMissing() { // given