Health-Aware Resolver Pool
Problem
Design HealthAwareResolverPool, an in-memory DNS resolver that stores one current address answer per hostname, tracks the latest health observation for each address, and applies a fixed time to live.
Requirements
- Replace an answer.
publishAnswerreplaces the hostname's cached answer with the supplied distinct addresses andobservedAt. - Track latest health.
recordHealthreplaces the address's previous health observation with the supplied value. - Expire by TTL. An answer is valid through
observedAt + ttland expires whennowis greater; expired addresses are not eligible. - Return eligible addresses.
resolvereturns exactly the healthy addresses in the hostname's current valid answer. The returned array's order is not judged. - Keep reads hostname-local.
resolveruns in time, where is the size of the requested hostname's current answer, independent of addresses stored for other hostnames.
API
| Signature | Returns | Behavior |
|---|---|---|
HealthAwareResolverPool(ttl: integer) | Not applicable | Creates an empty pool with one fixed positive TTL. |
publishAnswer(hostname: string, addresses: string[], observedAt: integer) | void | Replaces one hostname's cached answer. |
recordHealth(address: string, healthy: boolean) | void | Stores the address's latest health observation. |
resolve(hostname: string, now: integer) | string[] | Returns the requested hostname's eligible address set as an array in time. |
Examples
Given HealthAwareResolverPool(10):
| Step | Operation | Result |
|---|---|---|
| 1 | publishAnswer("api.test", ["10.0.0.2", "10.0.0.1"], 5) | void |
| 2 | recordHealth("10.0.0.2", true) | void |
| 3 | recordHealth("10.0.0.1", false) | void |
| 4 | resolve("api.test", 15) | ["10.0.0.2"] |
| 5 | resolve("api.test", 16) | [] |
Constraints
ttlis between 1 and 1,000,000,000.- Every string is non-empty ASCII text with at most 253 characters.
- Each published answer contains 1 to 1,000 distinct addresses.
- Time values are between 0 and 1,000,000,000.
observedAt + ttlfits in a signed 64-bit integer.- Every hostname supplied to
resolvehas a current published answer. - Before resolving an unexpired answer, every address in that current answer has a recorded health observation.
- At most 50,000 public method calls occur per testcase.
- Inputs satisfy these constraints.
Notes
Calls are sequential. Result-array order has no meaning.