diff --git a/README.md b/README.md index 6c89b9e..b800e0a 100644 --- a/README.md +++ b/README.md @@ -231,12 +231,23 @@ LLMMetricResolver llmMetricResolver = new MicrometerLLMMetricResolver(meterRegis #### Now, assemble the `WombatCore` +The `WombatCore` is also handed `WombatServices`, the system components made available to modules. For now this is the `SecretResolver` modules use to read the credentials their assets reference by variable name (an API key, a password, etc.). `SecretResolver.standard()` reads them from the process environment, which is also what the `WombatCore` falls back to when no `WombatServices` is given. + +[Default components](#default-components) are registered per family as factories, `null` meaning there is no default for that part of the pipeline. Assets whose type has neither a module component nor a default for a given part are not evaluated for that part, and an error is logged when the evaluator is created. + ```java /* tech.illuin.wombat.core.connector.boavizta.connector.BoaviztaClient boaviztaClient */ /* tech.illuin.wombat.core.connector.ecologits.connector.EcologitsClient ecologitsClient */ +/* Default components are stateless, a single instance can serve every asset */ +WombatActivityResolver k8sActivity = new KubernetesActivityResolver(k8sMetricResolver); +WombatActivityResolver llmActivity = new LLMActivityResolver(llmMetricResolver); +WombatEvaluationResolver boavizta = new BoaviztaEvaluationResolver(boaviztaClient); +WombatEvaluationResolver ecologits = new EcologitsEvaluationResolver(ecologitsClient); + try (WombatCore core = new WombatCore( contextProvider, + new WombatServices(SecretResolver.standard()), metricPersister, List.of( new KubernetesAPIModule(), @@ -244,11 +255,11 @@ try (WombatCore core = new WombatCore( new LLMStaticModule() ), defaults -> { - defaults.register(KUBERNETES_CONTAINER, new KubernetesActivityResolver(k8sMetricResolver), new BoaviztaEvaluationResolver(boaviztaClient), null); - defaults.register(LLM, new LLMActivityResolver(llmMetricResolver), new EcologitsEvaluationResolver(ecologitsClient), null); + defaults.register(KUBERNETES_CONTAINER, (services, asset) -> k8sActivity, (services, asset) -> boavizta, null); + defaults.register(LLM, (services, asset) -> llmActivity, (services, asset) -> ecologits, null); } )) { - /* The evaluator is stateless, you should reuse it throughout your app ; e.g. make it injectable if applicable */ + /* The evaluator keeps the resolvers it built for each asset, you should reuse it throughout your app ; e.g. make it injectable if applicable */ AssetEvaluator evaluator = core.createEvaluator(); Instant now = Instant.now(); @@ -257,9 +268,11 @@ try (WombatCore core = new WombatCore( } ``` +Both the `AssetMonitor` and the `AssetEvaluator` read the context on every call: assets added to it are picked up without rebuilding anything, and the components of removed assets are discarded. When an asset's settings change, call `refresh(asset.identity())` on both so that its components are built again (see [component lifecycle](#component-lifecycle)). + ### Custom Module -Custom modules enable tracking domain-specific assets , integrating custom activity modeling or even custom impact/cost resolving. To create a custom module, define an `Asset` with its profile, implement `WombatModule`, and provide the relevant resolvers (such as a `WombatActivityResolver`). +Custom modules enable tracking domain-specific assets , integrating custom activity modeling or even custom impact/cost resolving. To create a custom module, define an `Asset` with its profile, implement `WombatModule`, and provide factories for the relevant components (such as a `WombatActivityResolver`). Here is an example of a custom module providing modeled activity data for LLM traffic: @@ -277,9 +290,9 @@ public class MySimulationModule implements WombatModule public Class assetClass() { return MySimulationAsset.class; } @Override - public Optional createActivityResolver(WombatContext ctx) + public Optional> activityResolver() { - return Optional.of(new MySimulationActivityResolver()); + return Optional.of((services, asset) -> new MySimulationActivityResolver()); } } @@ -351,6 +364,16 @@ public class MySimulationActivityResolver implements WombatActivityResolver Once defined, you can register this module in your `WombatCore` as seen [above](#custom-core) or in a Wombat app as documented [here](https://github.com/illuin-tech/wombat#custom-extensions). +#### Component lifecycle + +A module never builds its components upfront: each method of `WombatModule` returns a `ProcessorFactory`, which the `AssetMonitor` and `AssetEvaluator` invoke the first time they need a component for it. + +* a component is built once per asset identity (`AssetIdentity`) and then kept +* the component is handed the asset as it currently is on every call, so its settings should be read from there rather than captured by the factory ; anything built by the factory remains as it was built until the asset is refreshed with `refresh(asset.identity())`, or leaves the context +* if the component implements `AutoCloseable`, it is closed when its asset is refreshed or leaves the context, and when the `AssetMonitor` or `AssetEvaluator` is closed +* a factory may return the same instance for every asset (as the default components above do), such an instance must then not implement `AutoCloseable` since it would be closed along with the first asset to go ; release its resources from `WombatModule.close()` instead +* a factory failing leaves nothing behind: its exception propagates to the call that needed the component, and building it is attempted again the next time it is needed + ## How to build Building the project requires a Java 21+ JDK, in order to compile it: diff --git a/core/src/main/java/tech/illuin/wombat/core/WombatCore.java b/core/src/main/java/tech/illuin/wombat/core/WombatCore.java index 0b682d8..81507f2 100644 --- a/core/src/main/java/tech/illuin/wombat/core/WombatCore.java +++ b/core/src/main/java/tech/illuin/wombat/core/WombatCore.java @@ -4,11 +4,13 @@ import org.slf4j.LoggerFactory; import tech.illuin.wombat.core.activity.WombatActivityResolver; import tech.illuin.wombat.core.asset.type.ServiceFamily; -import tech.illuin.wombat.core.context.WombatContext; import tech.illuin.wombat.core.context.WombatContextProvider; import tech.illuin.wombat.core.evaluation.WombatEvaluationResolver; import tech.illuin.wombat.core.evaluation.AssetEvaluator; +import tech.illuin.wombat.core.module.ProcessorFactory; import tech.illuin.wombat.core.module.WombatModule; +import tech.illuin.wombat.core.module.WombatServices; +import tech.illuin.wombat.core.secret.SecretResolver; import tech.illuin.wombat.core.source.AssetMonitor; import tech.illuin.wombat.core.source.persistence.WombatMetricPersister; @@ -21,18 +23,31 @@ public final class WombatCore implements AutoCloseable { private final Map modules; private final WombatContextProvider contextProvider; + private final WombatServices services; private final WombatMetricPersister persister; private final CoreDefaults defaults; private static final Logger logger = LoggerFactory.getLogger(WombatCore.class); + /** Resolves secrets from the process environment, which is where a deployment supplies them. */ public WombatCore( WombatContextProvider contextProvider, WombatMetricPersister persister, Collection modules, Consumer defaultsAdjuster + ) { + this(contextProvider, new WombatServices(SecretResolver.standard()), persister, modules, defaultsAdjuster); + } + + public WombatCore( + WombatContextProvider contextProvider, + WombatServices services, + WombatMetricPersister persister, + Collection modules, + Consumer defaultsAdjuster ) { this.contextProvider = contextProvider; + this.services = services; this.persister = persister; this.modules = new HashMap<>(); this.defaults = new CoreDefaults(); @@ -59,44 +74,40 @@ public AssetMonitor createMonitor() public AssetMonitor createMonitor(ExecutorService executorService) { logger.info("Creating wombat source-monitor out of {} registered modules", this.modules.size()); - AssetMonitor monitor = new AssetMonitor(this.contextProvider, this.persister, executorService); - WombatContext context = this.contextProvider.provide(); - - context.assets().stream() - .filter(asset -> this.modules.containsKey(asset.type().name())) - .forEach(asset -> { - WombatModule module = this.modules.get(asset.type().name()); - WombatContext scoped = context.scope(asset.type()); - - module.createSource(scoped).ifPresentOrElse( - src -> monitor.register(asset.type(), src), - () -> logger.debug("Skipping monitor registration for asset-type {}", asset.type().name()) - ); - }); + AssetMonitor monitor = new AssetMonitor(this.contextProvider, this.services, this.persister, executorService); + + for (WombatModule module : this.modules.values()) + { + module.source().ifPresentOrElse( + factory -> monitor.register(module.type(), factory), + () -> logger.debug("Skipping monitor registration for asset-type {}", module.type().name()) + ); + } return monitor; } public AssetEvaluator createEvaluator() { logger.info("Creating wombat impact-calculator out of {} registered modules", this.modules.size()); - AssetEvaluator calculator = new AssetEvaluator(this.contextProvider); - WombatContext context = this.contextProvider.provide(); + AssetEvaluator calculator = new AssetEvaluator(this.contextProvider, this.services); for (WombatModule module : this.modules.values()) { ServiceFamily family = module.type().family(); - - WombatActivityResolver activityResolver = module.createActivityResolver(context).or(() -> this.defaults.getActivityResolver(family)).orElseThrow(); - calculator.registerActivityResolver(module.type(), activityResolver); - logger.debug("Registered wombat activity-resolver {} for asset-type {}", activityResolver.getClass().getSimpleName(), module.type().name()); - - WombatEvaluationResolver impactResolver = module.createImpactResolver(context).or(() -> this.defaults.getImpactResolver(family)).orElseThrow(); - calculator.registerImpactResolver(module.type(), impactResolver); - logger.debug("Registered wombat impact-resolver {} for asset-type {}", impactResolver.getClass().getSimpleName(), module.type().name()); - - WombatEvaluationResolver costResolver = module.createCostResolver(context).or(() -> this.defaults.getCostResolver(family)).orElseThrow(); - calculator.registerCostResolver(module.type(), costResolver); - logger.debug("Registered wombat cost-resolver {} for asset-type {}", costResolver.getClass().getSimpleName(), module.type().name()); + String typeName = module.type().name(); + + module.activityResolver().or(() -> this.defaults.getActivityResolver(family)).ifPresentOrElse( + factory -> calculator.registerActivityResolver(module.type(), factory), + () -> logger.error("No activity-resolver available for asset-type {}, its assets will not be evaluated", typeName) + ); + module.impactResolver().or(() -> this.defaults.getImpactResolver(family)).ifPresentOrElse( + factory -> calculator.registerImpactResolver(module.type(), factory), + () -> logger.error("No impact-resolver available for asset-type {}, impacts of its assets will not be evaluated", typeName) + ); + module.costResolver().or(() -> this.defaults.getCostResolver(family)).ifPresentOrElse( + factory -> calculator.registerCostResolver(module.type(), factory), + () -> logger.error("No cost-resolver available for asset-type {}, costs of its assets will not be evaluated", typeName) + ); } return calculator; @@ -111,9 +122,9 @@ public void close() public static final class CoreDefaults { - private final Map defaultActivityResolvers; - private final Map defaultImpactResolvers; - private final Map defaultCostResolvers; + private final Map> defaultActivityResolvers; + private final Map> defaultImpactResolvers; + private final Map> defaultCostResolvers; private CoreDefaults() { @@ -124,9 +135,9 @@ private CoreDefaults() public CoreDefaults register( ServiceFamily family, - WombatActivityResolver activityResolver, - WombatEvaluationResolver impactResolver, - WombatEvaluationResolver costResolver + ProcessorFactory activityResolver, + ProcessorFactory impactResolver, + ProcessorFactory costResolver ) { this.defaultActivityResolvers.put(family, activityResolver); this.defaultImpactResolvers.put(family, impactResolver); @@ -134,17 +145,17 @@ public CoreDefaults register( return this; } - public Optional getActivityResolver(ServiceFamily family) + public Optional> getActivityResolver(ServiceFamily family) { return Optional.ofNullable(this.defaultActivityResolvers.get(family)); } - public Optional getImpactResolver(ServiceFamily family) + public Optional> getImpactResolver(ServiceFamily family) { return Optional.ofNullable(this.defaultImpactResolvers.get(family)); } - public Optional getCostResolver(ServiceFamily family) + public Optional> getCostResolver(ServiceFamily family) { return Optional.ofNullable(this.defaultCostResolvers.get(family)); } diff --git a/core/src/main/java/tech/illuin/wombat/core/context/ResolvedContext.java b/core/src/main/java/tech/illuin/wombat/core/context/ResolvedContext.java index 57b8d9e..bc5faed 100644 --- a/core/src/main/java/tech/illuin/wombat/core/context/ResolvedContext.java +++ b/core/src/main/java/tech/illuin/wombat/core/context/ResolvedContext.java @@ -2,31 +2,21 @@ import tech.illuin.wombat.core.asset.Asset; import tech.illuin.wombat.core.asset.Environment; -import tech.illuin.wombat.core.secret.EnvironmentSecretResolver; -import tech.illuin.wombat.core.secret.SecretResolver; import java.util.List; public final class ResolvedContext implements WombatContext { private final List environments; - private final SecretResolver secrets; - public ResolvedContext(List environments, SecretResolver secrets) - { - this.environments = environments; - this.secrets = secrets; - } - - /** Resolves secrets from the process environment, which is where a deployment supplies them. */ public ResolvedContext(List environments) { - this(environments, new EnvironmentSecretResolver()); + this.environments = environments; } public ResolvedContext(Environment... environments) { - this(List.of(environments), new EnvironmentSecretResolver()); + this(List.of(environments)); } @Override @@ -42,10 +32,4 @@ public List assets() .flatMap(e -> e.assets().stream()) .toList(); } - - @Override - public SecretResolver secrets() - { - return this.secrets; - } } diff --git a/core/src/main/java/tech/illuin/wombat/core/context/WombatContext.java b/core/src/main/java/tech/illuin/wombat/core/context/WombatContext.java index 5375f08..d8a3771 100644 --- a/core/src/main/java/tech/illuin/wombat/core/context/WombatContext.java +++ b/core/src/main/java/tech/illuin/wombat/core/context/WombatContext.java @@ -1,9 +1,7 @@ package tech.illuin.wombat.core.context; import tech.illuin.wombat.core.asset.Asset; -import tech.illuin.wombat.core.asset.type.AssetType; import tech.illuin.wombat.core.asset.Environment; -import tech.illuin.wombat.core.secret.SecretResolver; import java.util.List; @@ -12,28 +10,4 @@ public interface WombatContext List environments(); List assets(); - - /** - * Resolves the credentials the context's assets reference by variable name. - *

- * Part of the context rather than a per-module dependency: an asset and the secrets it needs are resolved - * together, so a module builds its clients from the context alone. - */ - SecretResolver secrets(); - - default WombatContext scope(AssetType type) - { - return new ResolvedContext( - this.environments().stream() - .map(e -> { - List filtered = e.assets().stream() - .filter(p -> p.type().equals(type)) - .toList(); - return new Environment(e.id(), filtered); - }) - .filter(e -> !e.assets().isEmpty()) - .toList(), - this.secrets() - ); - } } diff --git a/core/src/main/java/tech/illuin/wombat/core/evaluation/AssetEvaluator.java b/core/src/main/java/tech/illuin/wombat/core/evaluation/AssetEvaluator.java index 4aea31a..b97075c 100644 --- a/core/src/main/java/tech/illuin/wombat/core/evaluation/AssetEvaluator.java +++ b/core/src/main/java/tech/illuin/wombat/core/evaluation/AssetEvaluator.java @@ -8,55 +8,61 @@ import tech.illuin.wombat.core.activity.commons.AssetFilter; import tech.illuin.wombat.core.activity.commons.TimeRange; import tech.illuin.wombat.core.asset.Asset; +import tech.illuin.wombat.core.asset.AssetIdentity; import tech.illuin.wombat.core.asset.type.AssetType; import tech.illuin.wombat.core.asset.Environment; +import tech.illuin.wombat.core.context.WombatContext; import tech.illuin.wombat.core.context.WombatContextProvider; +import tech.illuin.wombat.core.module.ProcessorFactory; +import tech.illuin.wombat.core.module.ProcessorPool; +import tech.illuin.wombat.core.module.WombatServices; import java.util.*; import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentLinkedQueue; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; +import java.util.stream.Collectors; public class AssetEvaluator implements AutoCloseable { private final WombatContextProvider contextProvider; - private final Map activityResolvers; - private final Map impactResolvers; - private final Map costResolvers; + private final ProcessorPool activityResolvers; + private final ProcessorPool impactResolvers; + private final ProcessorPool costResolvers; private final ExecutorService executorService; private static final Logger logger = LoggerFactory.getLogger(AssetEvaluator.class); - public AssetEvaluator(WombatContextProvider contextProvider) + public AssetEvaluator(WombatContextProvider contextProvider, WombatServices services) { - this(contextProvider, Executors.newVirtualThreadPerTaskExecutor()); + this(contextProvider, services, Executors.newVirtualThreadPerTaskExecutor()); } - public AssetEvaluator(WombatContextProvider contextProvider, ExecutorService executorService) + public AssetEvaluator(WombatContextProvider contextProvider, WombatServices services, ExecutorService executorService) { - this.activityResolvers = new HashMap<>(); - this.impactResolvers = new HashMap<>(); - this.costResolvers = new HashMap<>(); + this.activityResolvers = new ProcessorPool<>("activity-resolver", services); + this.impactResolvers = new ProcessorPool<>("impact-resolver", services); + this.costResolvers = new ProcessorPool<>("cost-resolver", services); this.contextProvider = contextProvider; this.executorService = executorService; } - public AssetEvaluator registerActivityResolver(AssetType type, WombatActivityResolver resolver) + public AssetEvaluator registerActivityResolver(AssetType type, ProcessorFactory factory) { - this.activityResolvers.put(type, resolver); + this.activityResolvers.register(type, factory); return this; } - public AssetEvaluator registerCostResolver(AssetType type, WombatEvaluationResolver resolver) + public AssetEvaluator registerCostResolver(AssetType type, ProcessorFactory factory) { - this.costResolvers.put(type, resolver); + this.costResolvers.register(type, factory); return this; } - public AssetEvaluator registerImpactResolver(AssetType type, WombatEvaluationResolver resolver) + public AssetEvaluator registerImpactResolver(AssetType type, ProcessorFactory factory) { - this.impactResolvers.put(type, resolver); + this.impactResolvers.register(type, factory); return this; } @@ -70,12 +76,18 @@ public List evaluate(TimeRange range, AssetFilter public List evaluate(TimeRange range, AssetFilter filter) throws WombatEvaluationException { + WombatContext context = this.contextProvider.provide(); + this.retain(context.assets().stream() + .map(Asset::identity) + .collect(Collectors.toSet()) + ); + Queue outputs = new ConcurrentLinkedQueue<>(); - for (Environment environment : this.contextProvider.provide().environments()) + for (Environment environment : context.environments()) { List> futures = environment.assets().stream() .filter(asset -> filter == null || filter.accepts(environment.id(), asset.identity().id())) - .map(asset -> CompletableFuture.runAsync(() -> this.runEvaluation(asset, range, filter, outputs))) + .map(asset -> CompletableFuture.runAsync(() -> this.runEvaluation(asset, range, filter, outputs), this.executorService)) .toList(); CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).join(); @@ -83,6 +95,26 @@ public List evaluate(TimeRange range, AssetFilter filter) throw return List.copyOf(outputs); } + /** + * Discards the resolvers of the asset identified by {@code identity}, so they are built again on the next call. + *

+ * Meant to be called when an asset's settings changed: its resolvers are otherwise kept for as long as the asset is + * in the context. + */ + public void refresh(AssetIdentity identity) + { + this.activityResolvers.invalidate(identity); + this.impactResolvers.invalidate(identity); + this.costResolvers.invalidate(identity); + } + + private void retain(Set identities) + { + this.activityResolvers.retain(identities); + this.impactResolvers.retain(identities); + this.costResolvers.retain(identities); + } + private void runEvaluation(Asset asset, TimeRange range, AssetFilter filter, Queue outputs) { try { @@ -103,54 +135,57 @@ private void runEvaluation(Asset asset, TimeRange range, AssetFilter filter, Que public Optional computeActivity(Asset asset, TimeRange range, AssetFilter filter) throws WombatActivityException { - WombatActivityResolver resolver = this.activityResolvers.get(asset.type()); + Optional resolver = this.activityResolvers.get(asset); - if (resolver == null || !resolver.accept(asset)) + if (resolver.isEmpty() || !resolver.get().accept(asset)) { logger.debug("Skipping asset {}: no activity resolver accepts asset-type {}", asset.identity(), asset.type()); return Optional.empty(); } - return resolver.resolve(asset, range, filter); + return resolver.get().resolve(asset, range, filter); } public Optional computeImpact(Asset asset, ActivityData activity) throws WombatEvaluationException { - WombatEvaluationResolver resolver = this.impactResolvers.get(asset.type()); + Optional resolver = this.impactResolvers.get(asset); - if (resolver == null) + if (resolver.isEmpty()) { logger.debug("Skipping impact of asset {}: no impact resolver registered for asset-type {}", asset.identity().id(), asset.type()); return Optional.empty(); } - if (!resolver.accept(asset)) + if (!resolver.get().accept(asset)) { logger.warn( "Skipping impact of asset {}: impact resolver {} rejects asset-type {} with profile {}", asset.identity().id(), - resolver.getClass().getSimpleName(), + resolver.get().getClass().getSimpleName(), asset.type().name(), asset.profile().getClass().getName() ); return Optional.empty(); } - return Optional.of(resolver.resolve(asset, activity)); + return Optional.of(resolver.get().resolve(asset, activity)); } public Optional computeCost(Asset asset, ActivityData activity) throws WombatEvaluationException { - WombatEvaluationResolver resolver = this.costResolvers.get(asset.type()); + Optional resolver = this.costResolvers.get(asset); - if (resolver == null || !resolver.accept(asset)) + if (resolver.isEmpty() || !resolver.get().accept(asset)) { logger.debug("Skipping cost of asset {}: no cost resolver accepts asset-type {}", asset.identity().id(), asset.type()); return Optional.empty(); } - return Optional.of(resolver.resolve(asset, activity)); + return Optional.of(resolver.get().resolve(asset, activity)); } @Override public void close() { this.executorService.close(); + this.activityResolvers.close(); + this.impactResolvers.close(); + this.costResolvers.close(); } } diff --git a/core/src/main/java/tech/illuin/wombat/core/module/ProcessorFactory.java b/core/src/main/java/tech/illuin/wombat/core/module/ProcessorFactory.java new file mode 100644 index 0000000..1db0e83 --- /dev/null +++ b/core/src/main/java/tech/illuin/wombat/core/module/ProcessorFactory.java @@ -0,0 +1,19 @@ +package tech.illuin.wombat.core.module; + +import tech.illuin.wombat.core.asset.Asset; + +@FunctionalInterface +public interface ProcessorFactory

+{ + /** + * Builds the processor serving the asset identified by {@code asset.identity()}. + *

+ * Called lazily, once per identity: the processor then receives the current asset on every call, so only what + * cannot be read at runtime should be derived from {@code asset} here. + *

+ * If the returned processor implements {@link AutoCloseable}, it is closed when its asset leaves the catalog, is + * refreshed, or when its owner is closed. A processor shared across assets must therefore not implement + * {@link AutoCloseable}; release its resources from {@link WombatModule#close()} instead. + */ + P create(WombatServices services, Asset asset); +} diff --git a/core/src/main/java/tech/illuin/wombat/core/module/ProcessorPool.java b/core/src/main/java/tech/illuin/wombat/core/module/ProcessorPool.java new file mode 100644 index 0000000..e0895e2 --- /dev/null +++ b/core/src/main/java/tech/illuin/wombat/core/module/ProcessorPool.java @@ -0,0 +1,120 @@ +package tech.illuin.wombat.core.module; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import tech.illuin.wombat.core.asset.Asset; +import tech.illuin.wombat.core.asset.AssetIdentity; +import tech.illuin.wombat.core.asset.type.AssetType; + +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Optional; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; + +/** + * Builds processors of a given kind from the factories registered per asset-type, and keeps one per asset identity. + */ +public final class ProcessorPool

implements AutoCloseable +{ + private final String kind; + private final WombatServices services; + private final Map> factories; + private final Map processors; + + private static final Logger logger = LoggerFactory.getLogger(ProcessorPool.class); + + /** + * @param kind the kind of processor being pooled (e.g. "source", "activity-resolver"), only used for logging + */ + public ProcessorPool(String kind, WombatServices services) + { + this.kind = kind; + this.services = services; + this.factories = new ConcurrentHashMap<>(); + this.processors = new ConcurrentHashMap<>(); + } + + public ProcessorPool

register(AssetType type, ProcessorFactory

factory) + { + this.factories.put(type.name(), factory); + logger.debug("Registered wombat {} factory for asset-type {}", this.kind, type.name()); + return this; + } + + public boolean supports(AssetType type) + { + return this.factories.containsKey(type.name()); + } + + /** + * Returns the processor serving {@code asset}, built on first use. + *

+ * A failing factory propagates its exception and nothing is kept, so building is attempted again on the next call. + * + * @return the processor serving {@code asset}, empty when no factory is registered for its asset-type + */ + public Optional

get(Asset asset) + { + ProcessorFactory

factory = this.factories.get(asset.type().name()); + if (factory == null) + return Optional.empty(); + + return Optional.of(this.processors.computeIfAbsent(asset.identity(), id -> this.create(factory, asset))); + } + + /** + * Discards the processors of every asset not in {@code identities}. + */ + public void retain(Set identities) + { + List evicted = this.processors.keySet().stream() + .filter(id -> !identities.contains(id)) + .toList(); + + for (AssetIdentity identity : evicted) + this.invalidate(identity); + } + + /** + * Discards the processor of {@code identity}, if any, so the next call builds a new one. + */ + public void invalidate(AssetIdentity identity) + { + // Removed before being closed, so that no caller can obtain a processor that is being closed. + P processor = this.processors.remove(identity); + if (processor != null) + this.release(identity, processor); + } + + @Override + public void close() + { + for (AssetIdentity identity : List.copyOf(this.processors.keySet())) + this.invalidate(identity); + } + + private P create(ProcessorFactory

factory, Asset asset) + { + P processor = Objects.requireNonNull(factory.create(this.services, asset), "factory returned a null " + this.kind); + logger.debug("Created wombat {} {} for asset {}", this.kind, processor.getClass().getSimpleName(), asset.identity().id()); + return processor; + } + + /* AutoCloseable.close() declares Exception: a processor failing to close must not prevent discarding the others. */ + @SuppressWarnings("checkstyle:IllegalCatch") + private void release(AssetIdentity identity, P processor) + { + logger.debug("Discarding wombat {} {} for asset {}", this.kind, processor.getClass().getSimpleName(), identity.id()); + if (!(processor instanceof AutoCloseable closeable)) + return; + + try { + closeable.close(); + } + catch (Exception e) { + logger.error("Could not close wombat {} for asset {}: {}", this.kind, identity.id(), e.getMessage(), e); + } + } +} diff --git a/core/src/main/java/tech/illuin/wombat/core/module/WombatModule.java b/core/src/main/java/tech/illuin/wombat/core/module/WombatModule.java index 0bd805a..4b7b509 100644 --- a/core/src/main/java/tech/illuin/wombat/core/module/WombatModule.java +++ b/core/src/main/java/tech/illuin/wombat/core/module/WombatModule.java @@ -3,12 +3,17 @@ import tech.illuin.wombat.core.activity.WombatActivityResolver; import tech.illuin.wombat.core.asset.Asset; import tech.illuin.wombat.core.asset.type.AssetType; -import tech.illuin.wombat.core.context.WombatContext; import tech.illuin.wombat.core.evaluation.WombatEvaluationResolver; import tech.illuin.wombat.core.source.WombatSource; import java.util.Optional; +/** + * Declares how assets of a given {@link AssetType} are sourced and evaluated. + *

+ * Each feature is provided as a {@link ProcessorFactory}, invoked once per asset; an empty factory means the module + * does not support that feature, in which case the defaults registered for the asset-type's family apply. + */ public interface WombatModule { AssetType type(); @@ -17,22 +22,22 @@ public interface WombatModule default void close() {} - default Optional createSource(WombatContext context) + default Optional> source() { return Optional.empty(); } - default Optional createActivityResolver(WombatContext context) + default Optional> activityResolver() { return Optional.empty(); } - default Optional createImpactResolver(WombatContext context) + default Optional> impactResolver() { return Optional.empty(); } - default Optional createCostResolver(WombatContext context) + default Optional> costResolver() { return Optional.empty(); } diff --git a/core/src/main/java/tech/illuin/wombat/core/module/WombatServices.java b/core/src/main/java/tech/illuin/wombat/core/module/WombatServices.java new file mode 100644 index 0000000..61003e4 --- /dev/null +++ b/core/src/main/java/tech/illuin/wombat/core/module/WombatServices.java @@ -0,0 +1,19 @@ +package tech.illuin.wombat.core.module; + +import tech.illuin.wombat.core.secret.SecretResolver; + +import java.util.Objects; + +/** + * System components handed to every {@link ProcessorFactory}, whichever asset it builds a processor for. + *

+ * Grouped in a single parameter so that a new service can be introduced without breaking existing factories. + */ +public record WombatServices( + SecretResolver secrets +) { + public WombatServices + { + Objects.requireNonNull(secrets, "secrets cannot be null"); + } +} diff --git a/core/src/main/java/tech/illuin/wombat/core/source/AssetMonitor.java b/core/src/main/java/tech/illuin/wombat/core/source/AssetMonitor.java index eb116d6..9199eea 100644 --- a/core/src/main/java/tech/illuin/wombat/core/source/AssetMonitor.java +++ b/core/src/main/java/tech/illuin/wombat/core/source/AssetMonitor.java @@ -6,7 +6,11 @@ import tech.illuin.wombat.core.asset.AssetIdentity; import tech.illuin.wombat.core.asset.Environment; import tech.illuin.wombat.core.asset.type.AssetType; +import tech.illuin.wombat.core.context.WombatContext; import tech.illuin.wombat.core.context.WombatContextProvider; +import tech.illuin.wombat.core.module.ProcessorFactory; +import tech.illuin.wombat.core.module.ProcessorPool; +import tech.illuin.wombat.core.module.WombatServices; import tech.illuin.wombat.core.source.data.MetricData; import tech.illuin.wombat.core.source.persistence.WombatMetricPersister; import tech.illuin.wombat.core.source.persistence.WombatPersistenceException; @@ -14,12 +18,15 @@ import java.time.Instant; import java.util.List; import java.util.Map; +import java.util.Optional; +import java.util.Set; import java.util.concurrent.*; +import java.util.stream.Collectors; public class AssetMonitor implements AutoCloseable { private final WombatContextProvider contextProvider; - private final Map sources; + private final ProcessorPool sources; private final WombatMetricPersister persister; private final ExecutorService executorService; private final Map semaphores; @@ -27,14 +34,14 @@ public class AssetMonitor implements AutoCloseable private static final Logger logger = LoggerFactory.getLogger(AssetMonitor.class); - public AssetMonitor(WombatContextProvider contextProvider, WombatMetricPersister persister) + public AssetMonitor(WombatContextProvider contextProvider, WombatServices services, WombatMetricPersister persister) { - this(contextProvider, persister, Executors.newVirtualThreadPerTaskExecutor()); + this(contextProvider, services, persister, Executors.newVirtualThreadPerTaskExecutor()); } - public AssetMonitor(WombatContextProvider contextProvider, WombatMetricPersister persister, ExecutorService executorService) + public AssetMonitor(WombatContextProvider contextProvider, WombatServices services, WombatMetricPersister persister, ExecutorService executorService) { - this.sources = new ConcurrentHashMap<>(); + this.sources = new ProcessorPool<>("source", services); this.contextProvider = contextProvider; this.persister = persister; this.executorService = executorService; @@ -42,31 +49,33 @@ public AssetMonitor(WombatContextProvider contextProvider, WombatMetricPersister this.lastActivation = new ConcurrentHashMap<>(); } - public AssetMonitor register(AssetType type, WombatSource source) + public AssetMonitor register(AssetType type, ProcessorFactory factory) { - if (this.sources.containsKey(type)) + if (this.sources.supports(type)) { - logger.trace("Source for {} was already registered with type {}", type.name(), this.sources.get(type).getClass().getSimpleName()); + logger.trace("Source for {} was already registered", type.name()); return this; } - this.sources.put(type, source); - - logger.debug("Registered wombat source {} for asset-type {}", source.getClass().getSimpleName(), type.name()); + this.sources.register(type, factory); return this; } public void trigger() { Instant heartbeat = Instant.now(); + WombatContext context = this.contextProvider.provide(); + + this.prune(context.assets().stream() + .map(Asset::identity) + .collect(Collectors.toSet()) + ); - for (Environment environment : this.contextProvider.provide().environments()) + for (Environment environment : context.environments()) { environment.assets().stream() .filter(asset -> asset instanceof Monitorable) - .filter(asset -> this.sources.containsKey(asset.type())) - .filter(asset -> this.sources.get(asset.type()).accept(asset)) + .filter(asset -> this.sources.supports(asset.type())) .forEach(asset -> { - WombatSource source = this.sources.get(asset.type()); Instant lastActivated = this.lastActivation.computeIfAbsent(asset.identity(), id -> Instant.MIN); Monitorable monitorable = (Monitorable) asset; @@ -78,15 +87,14 @@ public void trigger() } this.lastActivation.put(asset.identity(), heartbeat); - Semaphore semaphore = this.semaphores.computeIfAbsent(asset.identity(), id -> new Semaphore(1, true)); + Semaphore semaphore = this.semaphore(asset.identity()); this.executorService.execute(() -> { semaphore.acquireUninterruptibly(); try { this.runSource( heartbeat, lastActivated != Instant.MIN ? lastActivated : null, - asset, - source + asset ); } finally { @@ -97,11 +105,36 @@ public void trigger() } } - private void runSource(Instant heartbeat, Instant lastActivation, Asset asset, WombatSource source) + /** + * Discards the source of the asset identified by {@code identity}, so it is built again on the next trigger. + *

+ * Meant to be called when an asset's settings changed: its source is otherwise kept for as long as the asset is in + * the context. Waits for an ongoing run of that asset to complete before discarding its source. + */ + public void refresh(AssetIdentity identity) { + Semaphore semaphore = this.semaphore(identity); + semaphore.acquireUninterruptibly(); + try { + this.sources.invalidate(identity); + } + finally { + semaphore.release(); + } + } + + private void runSource(Instant heartbeat, Instant lastActivation, Asset asset) + { + Optional source = this.sources.get(asset); + if (source.isEmpty() || !source.get().accept(asset)) + { + logger.trace("Skipping asset {}: no source accepts asset-type {}", asset.identity().id(), asset.type().name()); + return; + } + try { logger.debug("Monitor triggered for asset {}", asset.identity().id()); - List data = source.source(heartbeat, lastActivation, asset); + List data = source.get().source(heartbeat, lastActivation, asset); this.persister.persist(asset.identity(), asset.type(), data); } catch (WombatSourceException | WombatPersistenceException e) { @@ -109,9 +142,36 @@ private void runSource(Instant heartbeat, Instant lastActivation, Asset asset, W } } + /** + * Forgets everything kept for assets that are no longer in the context. + */ + private void prune(Set identities) + { + this.lastActivation.keySet().retainAll(identities); + + List removed = this.semaphores.keySet().stream() + .filter(id -> !identities.contains(id)) + .toList(); + + // Discarded from the executor, so that a run still in progress for a removed asset does not hold up the trigger. + for (AssetIdentity identity : removed) + { + this.executorService.execute(() -> { + this.refresh(identity); + this.semaphores.remove(identity); + }); + } + } + + private Semaphore semaphore(AssetIdentity identity) + { + return this.semaphores.computeIfAbsent(identity, id -> new Semaphore(1, true)); + } + @Override public void close() { this.executorService.close(); + this.sources.close(); } } diff --git a/core/src/test/java/tech/illuin/wombat/core/WombatCoreTest.java b/core/src/test/java/tech/illuin/wombat/core/WombatCoreTest.java index 0f19f12..8a1c8ec 100644 --- a/core/src/test/java/tech/illuin/wombat/core/WombatCoreTest.java +++ b/core/src/test/java/tech/illuin/wombat/core/WombatCoreTest.java @@ -11,7 +11,6 @@ import tech.illuin.wombat.core.asset.type.ServiceFamily; import tech.illuin.wombat.core.asset.profile.AssetProfile; import tech.illuin.wombat.core.context.ResolvedContext; -import tech.illuin.wombat.core.context.WombatContext; import tech.illuin.wombat.core.context.WombatContextProvider; import tech.illuin.wombat.core.activity.commons.ActivityData; import tech.illuin.wombat.core.activity.commons.AssetFilter; @@ -21,6 +20,7 @@ import tech.illuin.wombat.core.evaluation.AssetEvaluator; import tech.illuin.wombat.core.evaluation.impact.commons.AssetImpact; import tech.illuin.wombat.core.activity.commons.TimeRange; +import tech.illuin.wombat.core.module.ProcessorFactory; import tech.illuin.wombat.core.module.WombatModule; import tech.illuin.wombat.core.source.Monitorable; import tech.illuin.wombat.core.source.Period; @@ -34,6 +34,7 @@ import java.util.List; import java.util.Optional; import java.util.Set; +import java.util.concurrent.CopyOnWriteArrayList; import java.util.function.Consumer; import static java.util.Collections.emptyList; @@ -80,6 +81,22 @@ void createMonitor_skipsAssetsWhoseTypeHasNoModule() assertEquals(List.of("a1"), module.source.sampled); } + @Test + void createMonitor_sourcesAssetsAddedAfterItsCreation() + { + SourcingModule module = new SourcingModule(); + List assets = new CopyOnWriteArrayList<>(); + WombatContextProvider context = () -> new ResolvedContext(List.of(new Environment("env", List.copyOf(assets)))); + + // Neither the asset nor its type are known when the monitor is created. + try (WombatCore core = core(context, module); AssetMonitor monitor = core.createMonitor()) { + assets.add(sourceable("a1")); + monitor.trigger(); + } + + assertEquals(List.of("a1"), module.source.sampled); + } + @Test void createCalculator_resolvesOnlyAssetsServedByAnImpactResolver() throws Exception { @@ -89,7 +106,7 @@ void createCalculator_resolvesOnlyAssetsServedByAnImpactResolver() throws Except // asset's own module as the only thing able to resolve an impact. Consumer defaults = registry -> registry.register( ServiceFamily.LLM, - (asset, range, filter) -> Optional.of(new AssetActivity( + (services, resolved) -> (asset, range, filter) -> Optional.of(new AssetActivity( asset.identity(), List.of(new LLMActivityData( ActivityRegime.MODELED, @@ -102,8 +119,8 @@ void createCalculator_resolvesOnlyAssetsServedByAnImpactResolver() throws Except 0 )) )), - SERVES_NOTHING, - SERVES_NOTHING + (services, asset) -> SERVES_NOTHING, + (services, asset) -> SERVES_NOTHING ); // The sourceable asset's module provides no impact-resolver, so only the modeled one is calculated. @@ -116,6 +133,16 @@ void createCalculator_resolvesOnlyAssetsServedByAnImpactResolver() throws Except } } + @Test + void createCalculator_skipsAssetTypesWithoutAnyResolver() throws Exception + { + // No defaults are registered, and the module provides no activity-resolver: there is nothing to evaluate with. + try (WombatCore core = core(context(modeled("static-1")), new ResolvingModule()) ; AssetEvaluator evaluator = assertDoesNotThrow(() -> core.createEvaluator())) + { + assertEquals(List.of(), evaluator.evaluate(RANGE, AssetFilter.none())); + } + } + @Test void register_rejectsASecondModuleForTheSameAssetType() { @@ -152,8 +179,7 @@ private static WombatCore core(WombatContextProvider context, Consumer assetClass() } @Override - public Optional createSource(WombatContext context) + public Optional> source() { - return Optional.of(this.source); + return Optional.of((services, asset) -> this.source); } @Override @@ -283,9 +309,9 @@ public Class assetClass() } @Override - public Optional createImpactResolver(WombatContext ctx) + public Optional> impactResolver() { - return Optional.of((resolved, data) -> new AssetImpact( + return Optional.of((services, asset) -> (resolved, data) -> new AssetImpact( resolved.identity(), null, emptyList(), diff --git a/core/src/test/java/tech/illuin/wombat/core/evaluation/AssetEvaluatorTest.java b/core/src/test/java/tech/illuin/wombat/core/evaluation/AssetEvaluatorTest.java new file mode 100644 index 0000000..dbcf543 --- /dev/null +++ b/core/src/test/java/tech/illuin/wombat/core/evaluation/AssetEvaluatorTest.java @@ -0,0 +1,164 @@ +package tech.illuin.wombat.core.evaluation; + +import org.junit.jupiter.api.Test; +import tech.illuin.wombat.core.activity.WombatActivityResolver; +import tech.illuin.wombat.core.activity.commons.AssetActivity; +import tech.illuin.wombat.core.activity.commons.AssetFilter; +import tech.illuin.wombat.core.activity.commons.TimeRange; +import tech.illuin.wombat.core.asset.Asset; +import tech.illuin.wombat.core.asset.AssetIdentity; +import tech.illuin.wombat.core.asset.Environment; +import tech.illuin.wombat.core.asset.profile.AssetProfile; +import tech.illuin.wombat.core.asset.type.ActivityRegime; +import tech.illuin.wombat.core.asset.type.AssetType; +import tech.illuin.wombat.core.asset.type.ServiceFamily; +import tech.illuin.wombat.core.context.ResolvedContext; +import tech.illuin.wombat.core.context.WombatContextProvider; +import tech.illuin.wombat.core.module.WombatServices; +import tech.illuin.wombat.core.secret.SecretResolver; + +import java.time.Instant; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Optional; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.atomic.AtomicInteger; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class AssetEvaluatorTest +{ + private static final AssetType TYPE = AssetType.of("tech.illuin", "wombat-module", "test", ActivityRegime.MODELED, ServiceFamily.LLM); + private static final WombatServices SERVICES = new WombatServices(SecretResolver.standard()); + private static final TimeRange RANGE = new TimeRange(Instant.ofEpochMilli(0), Instant.ofEpochMilli(1_000)); + + @Test + void evaluate_resolvesAssetsAddedAfterItsCreation() throws WombatEvaluationException + { + List assets = new CopyOnWriteArrayList<>(); + List created = new CopyOnWriteArrayList<>(); + + try (AssetEvaluator evaluator = evaluator(assets, created)) + { + evaluator.evaluate(RANGE, AssetFilter.none()); + assets.add(new TestAsset("a1")); + evaluator.evaluate(RANGE, AssetFilter.none()); + } + + assertEquals(1, created.size()); + assertEquals(List.of("a1"), created.getFirst().resolved); + } + + @Test + void evaluate_closesTheResolversOfRemovedAssets() throws WombatEvaluationException + { + List assets = new CopyOnWriteArrayList<>(List.of(new TestAsset("a1"), new TestAsset("a2"))); + List created = new CopyOnWriteArrayList<>(); + + try (AssetEvaluator evaluator = evaluator(assets, created)) + { + evaluator.evaluate(RANGE, AssetFilter.none()); + assets.removeIf(asset -> asset.identity().id().equals("a1")); + evaluator.evaluate(RANGE, AssetFilter.none()); + + RecordingResolver a1 = created.stream().filter(r -> r.resolved.contains("a1")).findFirst().orElseThrow(); + RecordingResolver a2 = created.stream().filter(r -> r.resolved.contains("a2")).findFirst().orElseThrow(); + assertEquals(1, a1.closed.get()); + assertEquals(0, a2.closed.get()); + } + } + + @Test + void refresh_rebuildsTheResolversOfAnAsset() throws WombatEvaluationException + { + TestAsset asset = new TestAsset("a1"); + List created = new CopyOnWriteArrayList<>(); + + try (AssetEvaluator evaluator = evaluator(List.of(asset), created)) + { + evaluator.evaluate(RANGE, AssetFilter.none()); + evaluator.refresh(asset.identity()); + evaluator.evaluate(RANGE, AssetFilter.none()); + + assertEquals(2, created.size()); + assertEquals(1, created.getFirst().closed.get()); + assertEquals(0, created.getLast().closed.get()); + } + } + + @Test + void evaluate_runsOnTheConfiguredExecutor() throws WombatEvaluationException + { + List threads = new CopyOnWriteArrayList<>(); + ExecutorService executor = Executors.newSingleThreadExecutor(runnable -> new Thread(runnable, "evaluator-test")); + + try (AssetEvaluator evaluator = new AssetEvaluator(contextOf(List.of(new TestAsset("a1"))), SERVICES, executor)) + { + evaluator.registerActivityResolver(TYPE, (services, asset) -> (resolved, range, filter) -> { + threads.add(Thread.currentThread().getName()); + return Optional.empty(); + }); + evaluator.evaluate(RANGE, AssetFilter.none()); + } + + assertEquals(List.of("evaluator-test"), threads); + } + + private static AssetEvaluator evaluator(List assets, List created) + { + return new AssetEvaluator(contextOf(assets), SERVICES).registerActivityResolver(TYPE, (services, asset) -> { + RecordingResolver resolver = new RecordingResolver(); + created.add(resolver); + return resolver; + }); + } + + private static WombatContextProvider contextOf(List assets) + { + return () -> new ResolvedContext(new Environment("env", List.copyOf(assets))); + } + + /** Resolves no activity: only which assets it was handed, and whether it was closed, matter here. */ + private static final class RecordingResolver implements WombatActivityResolver, AutoCloseable + { + private final List resolved = Collections.synchronizedList(new ArrayList<>()); + private final AtomicInteger closed = new AtomicInteger(); + + @Override + public Optional resolve(Asset asset, TimeRange range, AssetFilter filter) + { + this.resolved.add(asset.identity().id()); + return Optional.empty(); + } + + @Override + public void close() + { + this.closed.incrementAndGet(); + } + } + + private record TestAsset(String assetId) implements Asset + { + @Override + public AssetIdentity identity() + { + return AssetIdentity.of(this.assetId, "env", this.assetId, this.type()); + } + + @Override + public AssetType type() + { + return TYPE; + } + + @Override + public AssetProfile profile() + { + return null; + } + } +} diff --git a/core/src/test/java/tech/illuin/wombat/core/module/ProcessorPoolTest.java b/core/src/test/java/tech/illuin/wombat/core/module/ProcessorPoolTest.java new file mode 100644 index 0000000..04d4f71 --- /dev/null +++ b/core/src/test/java/tech/illuin/wombat/core/module/ProcessorPoolTest.java @@ -0,0 +1,254 @@ +package tech.illuin.wombat.core.module; + +import org.junit.jupiter.api.Test; +import tech.illuin.wombat.core.asset.Asset; +import tech.illuin.wombat.core.asset.AssetIdentity; +import tech.illuin.wombat.core.asset.profile.AssetProfile; +import tech.illuin.wombat.core.asset.type.ActivityRegime; +import tech.illuin.wombat.core.asset.type.AssetType; +import tech.illuin.wombat.core.asset.type.ServiceFamily; +import tech.illuin.wombat.core.secret.SecretResolver; + +import java.util.Set; +import java.util.concurrent.atomic.AtomicInteger; + +import static org.junit.jupiter.api.Assertions.*; + +class ProcessorPoolTest +{ + private static final AssetType TYPE = AssetType.of("tech.illuin", "wombat-module", "test", ActivityRegime.MEASURED, ServiceFamily.LLM); + private static final AssetType TYPE_UNSUPPORTED = AssetType.of("tech.illuin", "wombat-module", "unsupported", ActivityRegime.MODELED, ServiceFamily.LLM); + private static final WombatServices SERVICES = new WombatServices(SecretResolver.standard()); + + @Test + void get_buildsOnFirstUseAndReusesForTheSameIdentity() + { + CountingFactory factory = new CountingFactory(); + try (ProcessorPool pool = pool(factory)) + { + TestProcessor first = pool.get(asset("a1", "v1")).orElseThrow(); + // Same identity, other settings: the processor is kept, it reads the current asset at runtime. + TestProcessor second = pool.get(asset("a1", "v2")).orElseThrow(); + + assertSame(first, second); + assertEquals(1, factory.created.get()); + } + } + + @Test + void get_buildsOneProcessorPerIdentity() + { + CountingFactory factory = new CountingFactory(); + try (ProcessorPool pool = pool(factory)) + { + TestProcessor a1 = pool.get(asset("a1", "v1")).orElseThrow(); + TestProcessor a2 = pool.get(asset("a2", "v1")).orElseThrow(); + + assertNotSame(a1, a2); + assertEquals(2, factory.created.get()); + } + } + + @Test + void get_handsTheServicesAndAssetToTheFactory() + { + Asset asset = asset("a1", "v1"); + try (ProcessorPool pool = pool((services, a) -> { + assertSame(SERVICES, services); + assertSame(asset, a); + return new TestProcessor(); + })) + { + assertTrue(pool.get(asset).isPresent()); + } + } + + @Test + void get_isEmptyWhenNoFactoryIsRegisteredForTheAssetType() + { + try (ProcessorPool pool = pool(new CountingFactory())) + { + assertTrue(pool.supports(TYPE)); + assertFalse(pool.supports(TYPE_UNSUPPORTED)); + assertTrue(pool.get(new TestAsset("a1", TYPE_UNSUPPORTED, "v1")).isEmpty()); + } + } + + @Test + void get_propagatesFactoryFailuresAndRetriesOnTheNextCall() + { + AtomicInteger attempts = new AtomicInteger(); + try (ProcessorPool pool = pool((services, asset) -> { + if (attempts.incrementAndGet() == 1) + throw new IllegalStateException("misconfigured"); + return new TestProcessor(); + })) + { + assertThrows(IllegalStateException.class, () -> pool.get(asset("a1", "v1"))); + assertTrue(pool.get(asset("a1", "v1")).isPresent()); + assertEquals(2, attempts.get()); + } + } + + @Test + void get_rejectsAFactoryReturningNull() + { + try (ProcessorPool pool = pool((services, asset) -> null)) + { + assertThrows(NullPointerException.class, () -> pool.get(asset("a1", "v1"))); + } + } + + @Test + void retain_closesTheProcessorsOfAssetsNoLongerRetained() + { + CountingFactory factory = new CountingFactory(true); + try (ProcessorPool pool = pool(factory)) + { + Asset a1 = asset("a1", "v1"); + Asset a2 = asset("a2", "v1"); + ClosingProcessor p1 = (ClosingProcessor) pool.get(a1).orElseThrow(); + ClosingProcessor p2 = (ClosingProcessor) pool.get(a2).orElseThrow(); + + pool.retain(Set.of(a2.identity())); + + assertEquals(1, p1.closed.get()); + assertEquals(0, p2.closed.get()); + assertSame(p2, pool.get(a2).orElseThrow()); + assertNotSame(p1, pool.get(a1).orElseThrow()); + } + } + + @Test + void retain_discardsProcessorsThatAreNotCloseable() + { + CountingFactory factory = new CountingFactory(); + try (ProcessorPool pool = pool(factory)) + { + TestProcessor first = pool.get(asset("a1", "v1")).orElseThrow(); + + pool.retain(Set.of()); + + assertNotSame(first, pool.get(asset("a1", "v1")).orElseThrow()); + assertEquals(2, factory.created.get()); + } + } + + @Test + void invalidate_closesTheProcessorAndRebuildsItOnTheNextCall() + { + CountingFactory factory = new CountingFactory(true); + try (ProcessorPool pool = pool(factory)) + { + Asset asset = asset("a1", "v1"); + ClosingProcessor first = (ClosingProcessor) pool.get(asset).orElseThrow(); + + pool.invalidate(asset.identity()); + + assertEquals(1, first.closed.get()); + assertNotSame(first, pool.get(asset).orElseThrow()); + assertEquals(2, factory.created.get()); + } + } + + @Test + void invalidate_doesNothingWhenNoProcessorWasBuilt() + { + CountingFactory factory = new CountingFactory(true); + try (ProcessorPool pool = pool(factory)) + { + assertDoesNotThrow(() -> pool.invalidate(asset("a1", "v1").identity())); + assertEquals(0, factory.created.get()); + } + } + + @Test + void close_closesEveryProcessorEvenWhenOneFailsToClose() + { + ProcessorPool pool = pool((services, asset) -> asset.identity().id().equals("failing") + ? new FailingProcessor() + : new ClosingProcessor() + ); + ClosingProcessor a1 = (ClosingProcessor) pool.get(asset("a1", "v1")).orElseThrow(); + pool.get(asset("failing", "v1")).orElseThrow(); + ClosingProcessor a2 = (ClosingProcessor) pool.get(asset("a2", "v1")).orElseThrow(); + + assertDoesNotThrow(pool::close); + + assertEquals(1, a1.closed.get()); + assertEquals(1, a2.closed.get()); + } + + private static ProcessorPool pool(ProcessorFactory factory) + { + return new ProcessorPool("test-processor", SERVICES).register(TYPE, factory); + } + + private static Asset asset(String id, String setting) + { + return new TestAsset(id, TYPE, setting); + } + + /** {@code setting} stands for any property of the asset that is not part of its identity. */ + private record TestAsset(String assetId, AssetType type, String setting) implements Asset + { + @Override + public AssetIdentity identity() + { + return AssetIdentity.of(this.assetId, "env", this.assetId, this.type); + } + + @Override + public AssetProfile profile() + { + return PROFILE; + } + } + + private static final AssetProfile PROFILE = new AssetProfile() {}; + + private static final class CountingFactory implements ProcessorFactory + { + private final boolean closeable; + private final AtomicInteger created = new AtomicInteger(); + + CountingFactory() + { + this(false); + } + + CountingFactory(boolean closeable) + { + this.closeable = closeable; + } + + @Override + public TestProcessor create(WombatServices services, Asset asset) + { + this.created.incrementAndGet(); + return this.closeable ? new ClosingProcessor() : new TestProcessor(); + } + } + + private static class TestProcessor implements AssetProcessor {} + + private static class ClosingProcessor extends TestProcessor implements AutoCloseable + { + private final AtomicInteger closed = new AtomicInteger(); + + @Override + public void close() + { + this.closed.incrementAndGet(); + } + } + + private static final class FailingProcessor extends TestProcessor implements AutoCloseable + { + @Override + public void close() throws Exception + { + throw new Exception("close failed"); + } + } +} diff --git a/core/src/test/java/tech/illuin/wombat/core/source/AssetMonitorTest.java b/core/src/test/java/tech/illuin/wombat/core/source/AssetMonitorTest.java index 20d66b7..00a15c8 100644 --- a/core/src/test/java/tech/illuin/wombat/core/source/AssetMonitorTest.java +++ b/core/src/test/java/tech/illuin/wombat/core/source/AssetMonitorTest.java @@ -10,6 +10,8 @@ import tech.illuin.wombat.core.asset.profile.AssetProfile; import tech.illuin.wombat.core.context.ResolvedContext; import tech.illuin.wombat.core.context.WombatContextProvider; +import tech.illuin.wombat.core.module.WombatServices; +import tech.illuin.wombat.core.secret.SecretResolver; import tech.illuin.wombat.core.source.data.MetricData; import tech.illuin.wombat.core.source.persistence.WombatMetricPersister; @@ -17,18 +19,24 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.CountDownLatch; +import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicInteger; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertTrue; class AssetMonitorTest { private static final AssetType TYPE_LLM = AssetType.of("tech.illuin", "wombat-module", "llm-prometheus", ActivityRegime.MEASURED, ServiceFamily.LLM); private static final AssetType TYPE_K8S = AssetType.of("tech.illuin", "wombat-module", "kubernetes-api", ActivityRegime.MEASURED, ServiceFamily.KUBERNETES_CONTAINER); + private static final WombatServices SERVICES = new WombatServices(SecretResolver.standard()); public static void main(String[] args) throws Exception { @@ -51,9 +59,9 @@ void sameSourceTasksAreRunSimultaneously() throws InterruptedException TestAsset a3 = new TestAsset("a3", TYPE_LLM, Period.ONE_SECOND); TestAsset a4 = new TestAsset("a4", TYPE_LLM, Period.ONE_SECOND); - try (AssetMonitor monitor = new AssetMonitor(contextOf(a1, a2, a3, a4), noopPersister())) + try (AssetMonitor monitor = new AssetMonitor(contextOf(a1, a2, a3, a4), SERVICES, noopPersister())) { - monitor.register(a1.type(), source); + monitor.register(a1.type(), (services, asset) -> source); Thread.sleep(1050); monitor.trigger(); } @@ -106,10 +114,10 @@ void differentSourcesRunConcurrently() TestAsset a1 = new TestAsset("a1", TYPE_LLM, Period.ONE_SECOND); TestAsset a2 = new TestAsset("a2", TYPE_K8S, Period.ONE_SECOND); - try (AssetMonitor monitor = new AssetMonitor(contextOf(a1, a2), noopPersister())) + try (AssetMonitor monitor = new AssetMonitor(contextOf(a1, a2), SERVICES, noopPersister())) { - monitor.register(a1.type(), sourceA); - monitor.register(a2.type(), sourceB); + monitor.register(a1.type(), (services, asset) -> sourceA); + monitor.register(a2.type(), (services, asset) -> sourceB); monitor.trigger(); } @@ -135,9 +143,9 @@ void errorInOneTaskDoesNotHaltSubsequentQueuedTasks() TestAsset errorAsset = new TestAsset("error-asset", TYPE_LLM, Period.ONE_SECOND); TestAsset nextAsset = new TestAsset("next-asset", TYPE_LLM, Period.ONE_SECOND); - try (AssetMonitor monitor = new AssetMonitor(contextOf(errorAsset, nextAsset), noopPersister())) + try (AssetMonitor monitor = new AssetMonitor(contextOf(errorAsset, nextAsset), SERVICES, noopPersister())) { - monitor.register(errorAsset.type(), source); + monitor.register(errorAsset.type(), (services, asset) -> source); monitor.trigger(); } @@ -145,6 +153,83 @@ void errorInOneTaskDoesNotHaltSubsequentQueuedTasks() assertEquals(List.of("next-asset"), processed); } + @Test + void removedAssetsHaveTheirSourceClosed() throws InterruptedException + { + List assets = new CopyOnWriteArrayList<>(List.of(new SettingAsset("a1", "v1"))); + RecordingSource source = new RecordingSource(); + + try (AssetMonitor monitor = new AssetMonitor(contextOf(assets), SERVICES, noopPersister())) + { + monitor.register(TYPE_LLM, (services, asset) -> source); + monitor.trigger(); + assertNotNull(source.sampled.poll(2, TimeUnit.SECONDS)); + + assets.clear(); + monitor.trigger(); + assertTrue(source.closed.await(2, TimeUnit.SECONDS)); + } + } + + @Test + void assetsWhoseSettingsChangeKeepTheirSource() throws InterruptedException + { + List assets = new CopyOnWriteArrayList<>(List.of(new SettingAsset("a1", "v1"))); + AtomicInteger created = new AtomicInteger(); + RecordingSource source = new RecordingSource(); + + try (AssetMonitor monitor = new AssetMonitor(contextOf(assets), SERVICES, noopPersister())) + { + monitor.register(TYPE_LLM, (services, asset) -> { + created.incrementAndGet(); + return source; + }); + monitor.trigger(); + assertNotNull(source.sampled.poll(2, TimeUnit.SECONDS)); + + SettingAsset changed = new SettingAsset("a1", "v2"); + assets.set(0, changed); + monitor.trigger(); + + // The source is kept, but it is handed the asset as it currently is. + assertSame(changed, source.sampled.poll(2, TimeUnit.SECONDS)); + assertEquals(1, created.get()); + } + } + + @Test + void refreshedAssetsHaveTheirSourceRebuilt() throws InterruptedException + { + SettingAsset a1 = new SettingAsset("a1", "v1"); + BlockingQueue created = new LinkedBlockingQueue<>(); + + try (AssetMonitor monitor = new AssetMonitor(contextOf(a1), SERVICES, noopPersister())) + { + monitor.register(TYPE_LLM, (services, asset) -> { + RecordingSource source = new RecordingSource(); + created.add(source); + return source; + }); + monitor.trigger(); + RecordingSource first = created.poll(2, TimeUnit.SECONDS); + assertNotNull(first); + assertNotNull(first.sampled.poll(2, TimeUnit.SECONDS)); + + monitor.refresh(a1.identity()); + assertTrue(first.closed.await(0, TimeUnit.SECONDS)); + + monitor.trigger(); + RecordingSource second = created.poll(2, TimeUnit.SECONDS); + assertNotNull(second); + assertNotNull(second.sampled.poll(2, TimeUnit.SECONDS)); + } + } + + private static WombatContextProvider contextOf(List assets) + { + return () -> new ResolvedContext(new Environment("env", List.copyOf(assets))); + } + private static WombatContextProvider contextOf(Asset... assets) { return () -> new ResolvedContext(new Environment("env", List.of(assets))); @@ -186,6 +271,53 @@ public List source(Instant heartbeat, Instant lastActivation, Asset } } + private static final class RecordingSource implements WombatSource, AutoCloseable + { + private final BlockingQueue sampled = new LinkedBlockingQueue<>(); + private final CountDownLatch closed = new CountDownLatch(1); + + @Override + public List source(Instant heartbeat, Instant lastActivation, Asset asset) + { + this.sampled.add(asset); + return Collections.emptyList(); + } + + @Override + public void close() + { + this.closed.countDown(); + } + } + + /** Triggered on every heartbeat; {@code setting} stands for any property of the asset that is not part of its identity. */ + private record SettingAsset(String assetId, String setting) implements Asset, Monitorable + { + @Override + public AssetIdentity identity() + { + return AssetIdentity.of(this.assetId, "env", this.assetId, this.type()); + } + + @Override + public AssetType type() + { + return TYPE_LLM; + } + + @Override + public AssetProfile profile() + { + return null; + } + + @Override + public Period period() + { + return Period.ZERO; + } + } + private record TestAsset(String assetId, AssetType type, Period period) implements Asset, Monitorable { @Override diff --git a/module/src/main/java/tech/illuin/wombat/module/kubernetes_api/KubernetesAPIModule.java b/module/src/main/java/tech/illuin/wombat/module/kubernetes_api/KubernetesAPIModule.java index 58ceb4e..cea0917 100644 --- a/module/src/main/java/tech/illuin/wombat/module/kubernetes_api/KubernetesAPIModule.java +++ b/module/src/main/java/tech/illuin/wombat/module/kubernetes_api/KubernetesAPIModule.java @@ -4,10 +4,10 @@ import tech.illuin.wombat.core.asset.Asset; import tech.illuin.wombat.core.asset.type.AssetType; import tech.illuin.wombat.core.asset.type.ServiceFamily; -import tech.illuin.wombat.core.context.WombatContext; +import tech.illuin.wombat.core.module.ProcessorFactory; import tech.illuin.wombat.core.module.WombatModule; import tech.illuin.wombat.core.source.WombatSource; -import tech.illuin.wombat.module.kubernetes_api.connector.KubernetesMultiClusterClientConfig; +import tech.illuin.wombat.module.kubernetes_api.connector.KubernetesClientConfig; import tech.illuin.wombat.module.kubernetes_api.source.KubernetesAPISource; import java.util.Optional; @@ -29,8 +29,10 @@ public Class assetClass() } @Override - public Optional createSource(WombatContext context) + public Optional> source() { - return Optional.of(new KubernetesAPISource(KubernetesMultiClusterClientConfig.create(context))); + return Optional.of((services, asset) -> new KubernetesAPISource( + KubernetesClientConfig.create((KubernetesAPIAsset) asset) + )); } } diff --git a/module/src/main/java/tech/illuin/wombat/module/kubernetes_api/connector/KubernetesMultiClusterClientConfig.java b/module/src/main/java/tech/illuin/wombat/module/kubernetes_api/connector/KubernetesClientConfig.java similarity index 73% rename from module/src/main/java/tech/illuin/wombat/module/kubernetes_api/connector/KubernetesMultiClusterClientConfig.java rename to module/src/main/java/tech/illuin/wombat/module/kubernetes_api/connector/KubernetesClientConfig.java index 2dbb410..230cd62 100644 --- a/module/src/main/java/tech/illuin/wombat/module/kubernetes_api/connector/KubernetesMultiClusterClientConfig.java +++ b/module/src/main/java/tech/illuin/wombat/module/kubernetes_api/connector/KubernetesClientConfig.java @@ -5,23 +5,12 @@ import io.fabric8.kubernetes.client.KubernetesClient; import io.fabric8.kubernetes.client.KubernetesClientBuilder; import tech.illuin.wombat.module.kubernetes_api.KubernetesAPIAsset; -import tech.illuin.wombat.core.context.WombatContext; import java.io.File; -public class KubernetesMultiClusterClientConfig +public class KubernetesClientConfig { - public static KubernetesMultiClusterClient create(WombatContext context) - { - KubernetesMultiClusterClient multi = new KubernetesMultiClusterClient(); - context.assets().stream() - .filter(KubernetesAPIAsset.class::isInstance) - .map(KubernetesAPIAsset.class::cast) - .forEach(cluster -> multi.register(cluster.identity().id(), createClient(cluster))); - return multi; - } - - private static KubernetesClient createClient(KubernetesAPIAsset properties) + public static KubernetesClient create(KubernetesAPIAsset properties) { Config config = Config.fromKubeconfig( properties.context().orElse(null), diff --git a/module/src/main/java/tech/illuin/wombat/module/kubernetes_api/connector/KubernetesMultiClusterClient.java b/module/src/main/java/tech/illuin/wombat/module/kubernetes_api/connector/KubernetesMultiClusterClient.java deleted file mode 100644 index baada34..0000000 --- a/module/src/main/java/tech/illuin/wombat/module/kubernetes_api/connector/KubernetesMultiClusterClient.java +++ /dev/null @@ -1,28 +0,0 @@ -package tech.illuin.wombat.module.kubernetes_api.connector; - - -import io.fabric8.kubernetes.client.KubernetesClient; - -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; - -public class KubernetesMultiClusterClient -{ - private final Map clients; - - public KubernetesMultiClusterClient() - { - this.clients = new HashMap<>(); - } - - public Optional get(String id) - { - return Optional.ofNullable(this.clients.get(id)); - } - - public void register(String id, KubernetesClient client) - { - this.clients.put(id, client); - } -} diff --git a/module/src/main/java/tech/illuin/wombat/module/kubernetes_api/source/KubernetesAPISource.java b/module/src/main/java/tech/illuin/wombat/module/kubernetes_api/source/KubernetesAPISource.java index 4f9246d..c00008a 100644 --- a/module/src/main/java/tech/illuin/wombat/module/kubernetes_api/source/KubernetesAPISource.java +++ b/module/src/main/java/tech/illuin/wombat/module/kubernetes_api/source/KubernetesAPISource.java @@ -3,6 +3,7 @@ import io.fabric8.kubernetes.api.model.Quantity; import io.fabric8.kubernetes.api.model.metrics.v1beta1.ContainerMetrics; import io.fabric8.kubernetes.api.model.metrics.v1beta1.PodMetrics; +import io.fabric8.kubernetes.client.KubernetesClient; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import tech.illuin.wombat.core.asset.Asset; @@ -10,22 +11,21 @@ import tech.illuin.wombat.core.source.data.KubernetesData; import tech.illuin.wombat.core.source.data.MetricData; import tech.illuin.wombat.module.kubernetes_api.KubernetesAPIAsset; -import tech.illuin.wombat.module.kubernetes_api.connector.KubernetesMultiClusterClient; import java.time.Instant; import java.util.ArrayList; import java.util.List; import java.util.Map; -public class KubernetesAPISource implements WombatSource +public class KubernetesAPISource implements WombatSource, AutoCloseable { - private final KubernetesMultiClusterClient multiClusterApi; + private final KubernetesClient client; private static final Logger logger = LoggerFactory.getLogger(KubernetesAPISource.class); - public KubernetesAPISource(KubernetesMultiClusterClient multiClusterApi) + public KubernetesAPISource(KubernetesClient client) { - this.multiClusterApi = multiClusterApi; + this.client = client; } @Override @@ -39,11 +39,9 @@ public List source(Instant heartbeat, Instant lastActivation, Asset { KubernetesAPIAsset kubernetesAsset = (KubernetesAPIAsset) asset; - String clusterId = asset.identity().id(); logger.trace("Persisting pods usages for kubernetes config {}", asset.identity().id()); - List podMetricsList = this.multiClusterApi.get(asset.identity().id()) - .orElseThrow(() -> new IllegalStateException("No client registered for cluster " + clusterId)) + List podMetricsList = this.client .top() .pods() .inNamespace(kubernetesAsset.namespace()) @@ -53,6 +51,12 @@ public List source(Instant heartbeat, Instant lastActivation, Asset return this.outputToTarget(podMetricsList, kubernetesAsset); } + @Override + public void close() + { + this.client.close(); + } + private List outputToTarget(List podMetricsList, KubernetesAPIAsset properties) { String clusterId = properties.identity().id(); diff --git a/module/src/main/java/tech/illuin/wombat/module/kubernetes_static/KubernetesStaticModule.java b/module/src/main/java/tech/illuin/wombat/module/kubernetes_static/KubernetesStaticModule.java index 2c1e9e1..bca99cf 100644 --- a/module/src/main/java/tech/illuin/wombat/module/kubernetes_static/KubernetesStaticModule.java +++ b/module/src/main/java/tech/illuin/wombat/module/kubernetes_static/KubernetesStaticModule.java @@ -5,7 +5,7 @@ import tech.illuin.wombat.core.asset.type.ActivityRegime; import tech.illuin.wombat.core.asset.type.AssetType; import tech.illuin.wombat.core.asset.type.ServiceFamily; -import tech.illuin.wombat.core.context.WombatContext; +import tech.illuin.wombat.core.module.ProcessorFactory; import tech.illuin.wombat.core.module.WombatModule; import tech.illuin.wombat.module.kubernetes_static.activity.KubernetesStaticActivityResolver; @@ -28,8 +28,8 @@ public Class assetClass() } @Override - public Optional createActivityResolver(WombatContext context) + public Optional> activityResolver() { - return Optional.of(new KubernetesStaticActivityResolver()); + return Optional.of((services, asset) -> new KubernetesStaticActivityResolver()); } } diff --git a/module/src/main/java/tech/illuin/wombat/module/llm_anthropic/LLMAnthropicModule.java b/module/src/main/java/tech/illuin/wombat/module/llm_anthropic/LLMAnthropicModule.java index cba4277..8efa342 100644 --- a/module/src/main/java/tech/illuin/wombat/module/llm_anthropic/LLMAnthropicModule.java +++ b/module/src/main/java/tech/illuin/wombat/module/llm_anthropic/LLMAnthropicModule.java @@ -5,10 +5,10 @@ import tech.illuin.wombat.core.asset.type.ActivityRegime; import tech.illuin.wombat.core.asset.type.AssetType; import tech.illuin.wombat.core.asset.type.ServiceFamily; -import tech.illuin.wombat.core.context.WombatContext; +import tech.illuin.wombat.core.module.ProcessorFactory; import tech.illuin.wombat.core.module.WombatModule; import tech.illuin.wombat.module.llm_anthropic.activity.LLMAnthropicActivityResolver; -import tech.illuin.wombat.module.llm_anthropic.connector.AnthropicMultiClientConfig; +import tech.illuin.wombat.module.llm_anthropic.connector.AnthropicClientConfig; import java.util.Optional; @@ -29,8 +29,10 @@ public Class assetClass() } @Override - public Optional createActivityResolver(WombatContext context) + public Optional> activityResolver() { - return Optional.of(new LLMAnthropicActivityResolver(AnthropicMultiClientConfig.create(context))); + return Optional.of((services, asset) -> new LLMAnthropicActivityResolver( + AnthropicClientConfig.create((LLMAnthropicAsset) asset, services.secrets()) + )); } } diff --git a/module/src/main/java/tech/illuin/wombat/module/llm_anthropic/activity/LLMAnthropicActivityResolver.java b/module/src/main/java/tech/illuin/wombat/module/llm_anthropic/activity/LLMAnthropicActivityResolver.java index 757d7cd..b1e430a 100644 --- a/module/src/main/java/tech/illuin/wombat/module/llm_anthropic/activity/LLMAnthropicActivityResolver.java +++ b/module/src/main/java/tech/illuin/wombat/module/llm_anthropic/activity/LLMAnthropicActivityResolver.java @@ -17,7 +17,6 @@ import tech.illuin.wombat.module.llm_anthropic.LLMAnthropicAsset; import tech.illuin.wombat.module.llm_anthropic.LLMAnthropicProfile; import tech.illuin.wombat.module.llm_anthropic.connector.AnthropicAdminClient; -import tech.illuin.wombat.module.llm_anthropic.connector.AnthropicMultiClient; import tech.illuin.wombat.module.llm_anthropic.connector.model.AnthropicUsageReport; import java.time.Duration; @@ -28,11 +27,11 @@ public class LLMAnthropicActivityResolver implements WombatActivityResolver { - private final AnthropicMultiClient client; + private final AnthropicAdminClient client; private static final Logger logger = LoggerFactory.getLogger(LLMAnthropicActivityResolver.class); - public LLMAnthropicActivityResolver(AnthropicMultiClient client) + public LLMAnthropicActivityResolver(AnthropicAdminClient client) { this.client = client; } @@ -97,12 +96,7 @@ private static Optional resolveSlot(LLMAnthropicProfile profile, T private Map> usage(LLMAnthropicAsset asset, TimeRange range, BucketWidth width) throws WombatActivityException { - String assetId = asset.identity().id(); - AnthropicAdminClient adminClient = this.client.get(assetId).orElseThrow( - () -> new IllegalStateException("No Anthropic client registered for asset " + assetId) - ); - - return fetch(adminClient, asset.profile(), range.start(), range.end(), width, assetId); + return fetch(this.client, asset.profile(), range.start(), range.end(), width, asset.identity().id()); } private static Map> fetch( diff --git a/module/src/main/java/tech/illuin/wombat/module/llm_anthropic/connector/AnthropicMultiClientConfig.java b/module/src/main/java/tech/illuin/wombat/module/llm_anthropic/connector/AnthropicClientConfig.java similarity index 64% rename from module/src/main/java/tech/illuin/wombat/module/llm_anthropic/connector/AnthropicMultiClientConfig.java rename to module/src/main/java/tech/illuin/wombat/module/llm_anthropic/connector/AnthropicClientConfig.java index 3638455..b63b4a7 100644 --- a/module/src/main/java/tech/illuin/wombat/module/llm_anthropic/connector/AnthropicMultiClientConfig.java +++ b/module/src/main/java/tech/illuin/wombat/module/llm_anthropic/connector/AnthropicClientConfig.java @@ -3,7 +3,6 @@ import feign.Client; import feign.Feign; import feign.jackson.JacksonDecoder; -import tech.illuin.wombat.core.context.WombatContext; import tech.illuin.wombat.core.secret.SecretResolver; import tech.illuin.wombat.module.llm_anthropic.LLMAnthropicAsset; @@ -11,29 +10,18 @@ import java.net.Proxy; import java.net.URI; -public class AnthropicMultiClientConfig +public class AnthropicClientConfig { - public static AnthropicMultiClient create(WombatContext context) + public static AnthropicAdminClient create(LLMAnthropicAsset properties, SecretResolver secrets) { - return create(context, null); + return create(properties, secrets, null); } /** * @param client the HTTP client to use, {@code null} for Feign's default (or a proxied one when the asset sets a * proxy-url); exposed so tests can capture requests without a network */ - public static AnthropicMultiClient create(WombatContext context, Client client) - { - SecretResolver secrets = context.secrets(); - AnthropicMultiClient multi = new AnthropicMultiClient(); - context.assets().stream() - .filter(LLMAnthropicAsset.class::isInstance) - .map(LLMAnthropicAsset.class::cast) - .forEach(asset -> multi.register(asset.identity().id(), createClient(asset, secrets, client))); - return multi; - } - - private static AnthropicAdminClient createClient(LLMAnthropicAsset properties, SecretResolver secrets, Client client) + public static AnthropicAdminClient create(LLMAnthropicAsset properties, SecretResolver secrets, Client client) { Feign.Builder builder = Feign.builder() .decoder(new JacksonDecoder()) diff --git a/module/src/main/java/tech/illuin/wombat/module/llm_anthropic/connector/AnthropicMultiClient.java b/module/src/main/java/tech/illuin/wombat/module/llm_anthropic/connector/AnthropicMultiClient.java deleted file mode 100644 index 82c8243..0000000 --- a/module/src/main/java/tech/illuin/wombat/module/llm_anthropic/connector/AnthropicMultiClient.java +++ /dev/null @@ -1,25 +0,0 @@ -package tech.illuin.wombat.module.llm_anthropic.connector; - -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; - -public class AnthropicMultiClient -{ - private final Map clients; - - public AnthropicMultiClient() - { - this.clients = new HashMap<>(); - } - - public Optional get(String id) - { - return Optional.ofNullable(this.clients.get(id)); - } - - public void register(String id, AnthropicAdminClient client) - { - this.clients.put(id, client); - } -} diff --git a/module/src/main/java/tech/illuin/wombat/module/llm_prometheus/LLMPrometheusModule.java b/module/src/main/java/tech/illuin/wombat/module/llm_prometheus/LLMPrometheusModule.java index db1f21b..ffbe609 100644 --- a/module/src/main/java/tech/illuin/wombat/module/llm_prometheus/LLMPrometheusModule.java +++ b/module/src/main/java/tech/illuin/wombat/module/llm_prometheus/LLMPrometheusModule.java @@ -4,10 +4,10 @@ import tech.illuin.wombat.core.asset.Asset; import tech.illuin.wombat.core.asset.type.AssetType; import tech.illuin.wombat.core.asset.type.ServiceFamily; -import tech.illuin.wombat.core.context.WombatContext; +import tech.illuin.wombat.core.module.ProcessorFactory; import tech.illuin.wombat.core.module.WombatModule; import tech.illuin.wombat.core.source.WombatSource; -import tech.illuin.wombat.module.llm_prometheus.connector.PrometheusMultiClientConfig; +import tech.illuin.wombat.module.llm_prometheus.connector.PrometheusClientConfig; import tech.illuin.wombat.module.llm_prometheus.source.LLMPrometheusSource; import java.util.Optional; @@ -29,8 +29,10 @@ public Class assetClass() } @Override - public Optional createSource(WombatContext context) + public Optional> source() { - return Optional.of(new LLMPrometheusSource(PrometheusMultiClientConfig.create(context))); + return Optional.of((services, asset) -> new LLMPrometheusSource( + PrometheusClientConfig.create((LLMPrometheusAsset) asset, services.secrets()) + )); } } diff --git a/module/src/main/java/tech/illuin/wombat/module/llm_prometheus/connector/PrometheusMultiClientConfig.java b/module/src/main/java/tech/illuin/wombat/module/llm_prometheus/connector/PrometheusClientConfig.java similarity index 68% rename from module/src/main/java/tech/illuin/wombat/module/llm_prometheus/connector/PrometheusMultiClientConfig.java rename to module/src/main/java/tech/illuin/wombat/module/llm_prometheus/connector/PrometheusClientConfig.java index 89d6c25..6066c8b 100644 --- a/module/src/main/java/tech/illuin/wombat/module/llm_prometheus/connector/PrometheusMultiClientConfig.java +++ b/module/src/main/java/tech/illuin/wombat/module/llm_prometheus/connector/PrometheusClientConfig.java @@ -3,7 +3,6 @@ import feign.Client; import feign.Feign; import feign.jackson.JacksonDecoder; -import tech.illuin.wombat.core.context.WombatContext; import tech.illuin.wombat.core.secret.SecretResolver; import tech.illuin.wombat.module.llm_prometheus.LLMPrometheusAsset; @@ -11,20 +10,9 @@ import java.net.Proxy; import java.net.URI; -public class PrometheusMultiClientConfig +public class PrometheusClientConfig { - public static PrometheusMultiClient create(WombatContext context) - { - SecretResolver secrets = context.secrets(); - PrometheusMultiClient multi = new PrometheusMultiClient(); - context.assets().stream() - .filter(LLMPrometheusAsset.class::isInstance) - .map(LLMPrometheusAsset.class::cast) - .forEach(asset -> multi.register(asset.identity().id(), createClient(asset, secrets))); - return multi; - } - - private static PrometheusClient createClient(LLMPrometheusAsset properties, SecretResolver secrets) + public static PrometheusClient create(LLMPrometheusAsset properties, SecretResolver secrets) { Feign.Builder builder = Feign.builder() .decoder(new JacksonDecoder()); diff --git a/module/src/main/java/tech/illuin/wombat/module/llm_prometheus/connector/PrometheusMultiClient.java b/module/src/main/java/tech/illuin/wombat/module/llm_prometheus/connector/PrometheusMultiClient.java deleted file mode 100644 index 371b972..0000000 --- a/module/src/main/java/tech/illuin/wombat/module/llm_prometheus/connector/PrometheusMultiClient.java +++ /dev/null @@ -1,25 +0,0 @@ -package tech.illuin.wombat.module.llm_prometheus.connector; - -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; - -public class PrometheusMultiClient -{ - private final Map clients; - - public PrometheusMultiClient() - { - this.clients = new HashMap<>(); - } - - public Optional get(String id) - { - return Optional.ofNullable(this.clients.get(id)); - } - - public void register(String id, PrometheusClient client) - { - this.clients.put(id, client); - } -} diff --git a/module/src/main/java/tech/illuin/wombat/module/llm_prometheus/source/LLMPrometheusSource.java b/module/src/main/java/tech/illuin/wombat/module/llm_prometheus/source/LLMPrometheusSource.java index feae8f4..4221d27 100644 --- a/module/src/main/java/tech/illuin/wombat/module/llm_prometheus/source/LLMPrometheusSource.java +++ b/module/src/main/java/tech/illuin/wombat/module/llm_prometheus/source/LLMPrometheusSource.java @@ -4,7 +4,6 @@ import org.slf4j.LoggerFactory; import tech.illuin.wombat.module.llm_prometheus.LLMPrometheusAsset; import tech.illuin.wombat.module.llm_prometheus.connector.PrometheusClient; -import tech.illuin.wombat.module.llm_prometheus.connector.PrometheusMultiClient; import tech.illuin.wombat.module.llm_prometheus.connector.model.PrometheusQueryResponse; import tech.illuin.wombat.core.asset.Asset; import tech.illuin.wombat.core.source.WombatSource; @@ -19,11 +18,11 @@ public class LLMPrometheusSource implements WombatSource { - private final PrometheusMultiClient client; + private final PrometheusClient client; private static final Logger logger = LoggerFactory.getLogger(LLMPrometheusSource.class); - public LLMPrometheusSource(PrometheusMultiClient client) + public LLMPrometheusSource(PrometheusClient client) { this.client = client; } @@ -40,13 +39,10 @@ public List source(Instant heartbeat, Instant lastActivation, Asset LLMPrometheusAsset prometheusAsset = (LLMPrometheusAsset) asset; String assetId = asset.identity().id(); - PrometheusClient prometheusClient = this.client.get(assetId) - .orElseThrow(() -> new IllegalStateException("No Prometheus client registered for asset " + assetId)); - String query = prometheusAsset.profile().dynamicProfile().query(); logger.debug("Sampling output tokens for LLM asset {} with query '{}'", assetId, query); - PrometheusQueryResponse response = prometheusClient.query(query); + PrometheusQueryResponse response = this.client.query(query); Optional data = Optional.ofNullable(response) .map(res -> res.firstValue().orElse(0.0d)) .map(value -> Math.max(0L, Math.round(value))) diff --git a/module/src/main/java/tech/illuin/wombat/module/llm_static/LLMStaticModule.java b/module/src/main/java/tech/illuin/wombat/module/llm_static/LLMStaticModule.java index 780091b..53fe396 100644 --- a/module/src/main/java/tech/illuin/wombat/module/llm_static/LLMStaticModule.java +++ b/module/src/main/java/tech/illuin/wombat/module/llm_static/LLMStaticModule.java @@ -5,7 +5,7 @@ import tech.illuin.wombat.core.asset.Asset; import tech.illuin.wombat.core.asset.type.AssetType; import tech.illuin.wombat.core.asset.type.ServiceFamily; -import tech.illuin.wombat.core.context.WombatContext; +import tech.illuin.wombat.core.module.ProcessorFactory; import tech.illuin.wombat.core.module.WombatModule; import tech.illuin.wombat.module.llm_static.activity.LLMStaticActivityResolver; @@ -28,8 +28,8 @@ public Class assetClass() } @Override - public Optional createActivityResolver(WombatContext context) + public Optional> activityResolver() { - return Optional.of(new LLMStaticActivityResolver()); + return Optional.of((services, asset) -> new LLMStaticActivityResolver()); } } diff --git a/module/src/main/java/tech/illuin/wombat/module/llm_vertexai/LLMVertexAIModule.java b/module/src/main/java/tech/illuin/wombat/module/llm_vertexai/LLMVertexAIModule.java index 424950e..1c1831f 100644 --- a/module/src/main/java/tech/illuin/wombat/module/llm_vertexai/LLMVertexAIModule.java +++ b/module/src/main/java/tech/illuin/wombat/module/llm_vertexai/LLMVertexAIModule.java @@ -5,10 +5,10 @@ import tech.illuin.wombat.core.asset.type.ActivityRegime; import tech.illuin.wombat.core.asset.type.AssetType; import tech.illuin.wombat.core.asset.type.ServiceFamily; -import tech.illuin.wombat.core.context.WombatContext; +import tech.illuin.wombat.core.module.ProcessorFactory; import tech.illuin.wombat.core.module.WombatModule; import tech.illuin.wombat.module.llm_vertexai.activity.LLMVertexAIActivityResolver; -import tech.illuin.wombat.module.llm_vertexai.connector.GoogleMultiClientConfig; +import tech.illuin.wombat.module.llm_vertexai.connector.GoogleClientConfig; import java.util.Optional; @@ -29,8 +29,10 @@ public Class assetClass() } @Override - public Optional createActivityResolver(WombatContext context) + public Optional> activityResolver() { - return Optional.of(new LLMVertexAIActivityResolver(GoogleMultiClientConfig.create(context))); + return Optional.of((services, asset) -> new LLMVertexAIActivityResolver( + GoogleClientConfig.create((LLMVertexAIAsset) asset, services.secrets()) + )); } } diff --git a/module/src/main/java/tech/illuin/wombat/module/llm_vertexai/activity/LLMVertexAIActivityResolver.java b/module/src/main/java/tech/illuin/wombat/module/llm_vertexai/activity/LLMVertexAIActivityResolver.java index 58d34ea..4e7ac3a 100644 --- a/module/src/main/java/tech/illuin/wombat/module/llm_vertexai/activity/LLMVertexAIActivityResolver.java +++ b/module/src/main/java/tech/illuin/wombat/module/llm_vertexai/activity/LLMVertexAIActivityResolver.java @@ -13,7 +13,6 @@ import tech.illuin.wombat.module.llm_vertexai.LLMVertexAIAsset; import tech.illuin.wombat.module.llm_vertexai.LLMVertexAIProfile; import tech.illuin.wombat.module.llm_vertexai.connector.GoogleClient; -import tech.illuin.wombat.module.llm_vertexai.connector.GoogleMultiClient; import tech.illuin.wombat.module.llm_vertexai.connector.GoogleRegion; import tech.illuin.wombat.module.llm_vertexai.connector.model.GoogleQueryResponse; @@ -23,11 +22,11 @@ public class LLMVertexAIActivityResolver implements WombatActivityResolver { - private final GoogleMultiClient client; + private final GoogleClient client; private static final Logger logger = LoggerFactory.getLogger(LLMVertexAIActivityResolver.class); - public LLMVertexAIActivityResolver(GoogleMultiClient client) + public LLMVertexAIActivityResolver(GoogleClient client) { this.client = client; } @@ -81,9 +80,6 @@ private static void requireBounded(Asset asset, TimeRange range) throws WombatAc private Map usage(LLMVertexAIAsset asset, TimeRange range, Set models) throws WombatActivityException { String assetId = asset.identity().id(); - GoogleClient googleClient = this.client.get(assetId) - .orElseThrow(() -> new IllegalStateException("No Google client registered for asset " + assetId)); - String filter = buildFilter(models); String period = range.resolution().toSeconds() + "s"; @@ -93,7 +89,7 @@ private Map usage(LLMVertexAIAsset asset, TimeRange range, S String pageToken = null; try { do { - GoogleQueryResponse response = googleClient.query( + GoogleQueryResponse response = this.client.query( asset.resourceName(), filter, range.start().toString(), diff --git a/module/src/main/java/tech/illuin/wombat/module/llm_vertexai/connector/GoogleMultiClientConfig.java b/module/src/main/java/tech/illuin/wombat/module/llm_vertexai/connector/GoogleClientConfig.java similarity index 68% rename from module/src/main/java/tech/illuin/wombat/module/llm_vertexai/connector/GoogleMultiClientConfig.java rename to module/src/main/java/tech/illuin/wombat/module/llm_vertexai/connector/GoogleClientConfig.java index 1b0e858..e28fbb1 100644 --- a/module/src/main/java/tech/illuin/wombat/module/llm_vertexai/connector/GoogleMultiClientConfig.java +++ b/module/src/main/java/tech/illuin/wombat/module/llm_vertexai/connector/GoogleClientConfig.java @@ -3,7 +3,6 @@ import feign.Feign; import feign.Client; import feign.jackson.JacksonDecoder; -import tech.illuin.wombat.core.context.WombatContext; import tech.illuin.wombat.core.secret.SecretResolver; import tech.illuin.wombat.module.llm_vertexai.LLMVertexAIAsset; @@ -11,25 +10,14 @@ import java.net.Proxy; import java.net.URI; -public class GoogleMultiClientConfig +public class GoogleClientConfig { - public static GoogleMultiClient create(WombatContext context) + public static GoogleClient create(LLMVertexAIAsset properties, SecretResolver secrets) { - return create(context, null); + return create(properties, secrets, null); } - public static GoogleMultiClient create(WombatContext context, Client client) - { - SecretResolver secrets = context.secrets(); - GoogleMultiClient multi = new GoogleMultiClient(); - context.assets().stream() - .filter(LLMVertexAIAsset.class::isInstance) - .map(LLMVertexAIAsset.class::cast) - .forEach(asset -> multi.register(asset.identity().id(), createClient(asset, secrets, client))); - return multi; - } - - private static GoogleClient createClient(LLMVertexAIAsset properties, SecretResolver secrets, Client client) + public static GoogleClient create(LLMVertexAIAsset properties, SecretResolver secrets, Client client) { String serviceAccountJson = properties.usesServiceAccountKey() ? secrets.require(properties.credentialsKey()) diff --git a/module/src/main/java/tech/illuin/wombat/module/llm_vertexai/connector/GoogleMultiClient.java b/module/src/main/java/tech/illuin/wombat/module/llm_vertexai/connector/GoogleMultiClient.java deleted file mode 100644 index de59b1f..0000000 --- a/module/src/main/java/tech/illuin/wombat/module/llm_vertexai/connector/GoogleMultiClient.java +++ /dev/null @@ -1,25 +0,0 @@ -package tech.illuin.wombat.module.llm_vertexai.connector; - -import java.util.HashMap; -import java.util.Map; -import java.util.Optional; - -public class GoogleMultiClient -{ - private final Map clients; - - public GoogleMultiClient() - { - this.clients = new HashMap<>(); - } - - public Optional get(String id) - { - return Optional.ofNullable(this.clients.get(id)); - } - - public void register(String id, GoogleClient client) - { - this.clients.put(id, client); - } -} diff --git a/module/src/test/java/tech/illuin/wombat/module/kubernetes_api/KubernetesAPIModuleTest.java b/module/src/test/java/tech/illuin/wombat/module/kubernetes_api/KubernetesAPIModuleTest.java index 9205478..4a43444 100644 --- a/module/src/test/java/tech/illuin/wombat/module/kubernetes_api/KubernetesAPIModuleTest.java +++ b/module/src/test/java/tech/illuin/wombat/module/kubernetes_api/KubernetesAPIModuleTest.java @@ -1,25 +1,78 @@ package tech.illuin.wombat.module.kubernetes_api; +import io.fabric8.kubernetes.client.KubernetesClient; import org.junit.jupiter.api.Test; -import tech.illuin.wombat.core.context.ResolvedContext; +import org.junit.jupiter.api.io.TempDir; +import tech.illuin.wombat.core.asset.AssetIdentity; +import tech.illuin.wombat.core.module.ProcessorFactory; +import tech.illuin.wombat.core.module.WombatServices; +import tech.illuin.wombat.core.secret.SecretResolver; +import tech.illuin.wombat.core.source.Period; import tech.illuin.wombat.core.source.WombatSource; import tech.illuin.wombat.module.kubernetes_api.source.KubernetesAPISource; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.Optional; import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; public class KubernetesAPIModuleTest { + private static final String KUBECONFIG = """ + apiVersion: v1 + kind: Config + clusters: + - name: test + cluster: + server: https://127.0.0.1:6443 + contexts: + - name: test + context: + cluster: test + user: test + current-context: test + users: + - name: test + user: + token: test-token + """; + private final KubernetesAPIModule module = new KubernetesAPIModule(); @Test - void providesASource() + void providesASource(@TempDir Path directory) throws IOException + { + // Building the client reads the kubeconfig, but does not connect to the cluster. + Path kubeconfig = Files.writeString(directory.resolve("kubeconfig"), KUBECONFIG); + + Optional> factory = this.module.source(); + assertTrue(factory.isPresent()); + + WombatSource source = factory.get().create(new WombatServices(SecretResolver.standard()), asset(kubeconfig)); + assertInstanceOf(KubernetesAPISource.class, source); + ((KubernetesAPISource) source).close(); + } + + @Test + void closingTheSourceClosesItsClient() + { + KubernetesClient client = mock(KubernetesClient.class); + + new KubernetesAPISource(client).close(); + + verify(client).close(); + } + + private static KubernetesAPIAsset asset(Path kubeconfig) { - // An empty context builds no cluster client, so no kubeconfig is read. - Optional source = this.module.createSource(new ResolvedContext()); - assertTrue(source.isPresent()); - assertInstanceOf(KubernetesAPISource.class, source.get()); + return new KubernetesAPIAsset( + AssetIdentity.of("k", "env", "K", KubernetesAPIModule.TYPE), + kubeconfig.toString(), "default", Optional.empty(), Optional.empty(), Period.ZERO, null + ); } } diff --git a/module/src/test/java/tech/illuin/wombat/module/kubernetes_api/connector/KubernetesMultiClusterApiTest.java b/module/src/test/java/tech/illuin/wombat/module/kubernetes_api/connector/KubernetesMultiClusterApiTest.java deleted file mode 100644 index 8552df9..0000000 --- a/module/src/test/java/tech/illuin/wombat/module/kubernetes_api/connector/KubernetesMultiClusterApiTest.java +++ /dev/null @@ -1,46 +0,0 @@ -package tech.illuin.wombat.module.kubernetes_api.connector; - -import io.fabric8.kubernetes.client.KubernetesClient; -import org.junit.jupiter.api.Test; -import org.mockito.Mockito; - -import java.util.Optional; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; - -public class KubernetesMultiClusterApiTest -{ - @Test - void register_andGet_returnsRegisteredClient() - { - KubernetesMultiClusterClient api = new KubernetesMultiClusterClient(); - KubernetesClient client = Mockito.mock(KubernetesClient.class); - - api.register("c1", client); - - Optional result = api.get("c1"); - assertTrue(result.isPresent()); - assertEquals(client, result.get()); - } - - @Test - void get_unknownId_returnsEmpty() - { - KubernetesMultiClusterClient api = new KubernetesMultiClusterClient(); - assertTrue(api.get("missing").isEmpty()); - } - - @Test - void register_overwritesPreviousClientForSameId() - { - KubernetesMultiClusterClient api = new KubernetesMultiClusterClient(); - KubernetesClient first = Mockito.mock(KubernetesClient.class); - KubernetesClient second = Mockito.mock(KubernetesClient.class); - - api.register("c1", first); - api.register("c1", second); - - assertEquals(second, api.get("c1").orElseThrow()); - } -} diff --git a/module/src/test/java/tech/illuin/wombat/module/kubernetes_static/KubernetesStaticModuleTest.java b/module/src/test/java/tech/illuin/wombat/module/kubernetes_static/KubernetesStaticModuleTest.java index 637a4b2..5ea6f26 100644 --- a/module/src/test/java/tech/illuin/wombat/module/kubernetes_static/KubernetesStaticModuleTest.java +++ b/module/src/test/java/tech/illuin/wombat/module/kubernetes_static/KubernetesStaticModuleTest.java @@ -22,7 +22,7 @@ void declaresTheStaticKubernetesAssetType() @Test void providesAStaticActivityResolver() { - Optional resolver = this.module.createActivityResolver(null); + Optional resolver = this.module.activityResolver().map(factory -> factory.create(null, null)); assertTrue(resolver.isPresent()); assertInstanceOf(KubernetesStaticActivityResolver.class, resolver.get()); } @@ -30,6 +30,6 @@ void providesAStaticActivityResolver() @Test void leavesTheImpactToTheFamilyDefault() { - assertTrue(this.module.createImpactResolver(null).isEmpty()); + assertTrue(this.module.impactResolver().isEmpty()); } } diff --git a/module/src/test/java/tech/illuin/wombat/module/llm_anthropic/LLMAnthropicModuleTest.java b/module/src/test/java/tech/illuin/wombat/module/llm_anthropic/LLMAnthropicModuleTest.java index 9f2d90d..c3ee0a3 100644 --- a/module/src/test/java/tech/illuin/wombat/module/llm_anthropic/LLMAnthropicModuleTest.java +++ b/module/src/test/java/tech/illuin/wombat/module/llm_anthropic/LLMAnthropicModuleTest.java @@ -3,13 +3,12 @@ import org.junit.jupiter.api.Test; import tech.illuin.wombat.core.activity.WombatActivityResolver; import tech.illuin.wombat.core.asset.AssetIdentity; -import tech.illuin.wombat.core.asset.Environment; -import tech.illuin.wombat.core.context.ResolvedContext; +import tech.illuin.wombat.core.module.ProcessorFactory; +import tech.illuin.wombat.core.module.WombatServices; import tech.illuin.wombat.core.secret.EnvironmentSecretResolver; import tech.illuin.wombat.core.secret.MissingSecretException; import tech.illuin.wombat.module.llm_anthropic.activity.LLMAnthropicActivityResolver; -import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; @@ -31,21 +30,18 @@ void declaresTheAnthropicLLMAssetType() @Test void providesAnActivityResolverAndNoSource() { - // An empty context registers no Anthropic client, so nothing is dialled. - Optional resolver = this.module.createActivityResolver(new ResolvedContext()); - assertTrue(resolver.isPresent()); - assertInstanceOf(LLMAnthropicActivityResolver.class, resolver.get()); - assertTrue(this.module.createSource(new ResolvedContext()).isEmpty()); + // Building the client does not dial Anthropic. + Optional> factory = this.module.activityResolver(); + assertTrue(factory.isPresent()); + assertInstanceOf(LLMAnthropicActivityResolver.class, factory.get().create(services(Map.of("ANTHROPIC_ADMIN_KEY", "k")), asset())); + assertTrue(this.module.source().isEmpty()); } @Test void creatingTheResolverFailsWhenTheAdminKeyIsUnset() { - ResolvedContext context = new ResolvedContext( - List.of(new Environment("env", List.of(asset()))), - new EnvironmentSecretResolver(Map.of()::get) - ); - MissingSecretException e = assertThrows(MissingSecretException.class, () -> this.module.createActivityResolver(context)); + ProcessorFactory factory = this.module.activityResolver().orElseThrow(); + MissingSecretException e = assertThrows(MissingSecretException.class, () -> factory.create(services(Map.of()), asset())); assertEquals("ANTHROPIC_ADMIN_KEY", e.key()); } @@ -62,6 +58,11 @@ void defaultsToThePublicApiUrl() assertEquals(LLMAnthropicAsset.DEFAULT_API_URL, asset().resolvedApiUrl()); } + private static WombatServices services(Map environment) + { + return new WombatServices(new EnvironmentSecretResolver(environment::get)); + } + private static LLMAnthropicAsset asset() { return new LLMAnthropicAsset( diff --git a/module/src/test/java/tech/illuin/wombat/module/llm_anthropic/activity/LLMAnthropicActivityResolverTest.java b/module/src/test/java/tech/illuin/wombat/module/llm_anthropic/activity/LLMAnthropicActivityResolverTest.java index 6f33442..8b89bfe 100644 --- a/module/src/test/java/tech/illuin/wombat/module/llm_anthropic/activity/LLMAnthropicActivityResolverTest.java +++ b/module/src/test/java/tech/illuin/wombat/module/llm_anthropic/activity/LLMAnthropicActivityResolverTest.java @@ -12,7 +12,6 @@ import tech.illuin.wombat.module.llm_anthropic.LLMAnthropicAsset; import tech.illuin.wombat.module.llm_anthropic.LLMAnthropicProfile; import tech.illuin.wombat.module.llm_anthropic.connector.AnthropicAdminClient; -import tech.illuin.wombat.module.llm_anthropic.connector.AnthropicMultiClient; import tech.illuin.wombat.module.llm_anthropic.connector.model.AnthropicUsageReport; import tech.illuin.wombat.module.llm_anthropic.connector.model.AnthropicUsageReport.Bucket; import tech.illuin.wombat.module.llm_anthropic.connector.model.AnthropicUsageReport.Result; @@ -44,7 +43,7 @@ class LLMAnthropicActivityResolverTest AssetIdentity.of("claude", "prod", "Claude", LLMAnthropicModule.TYPE), null, null, "ANTHROPIC_ADMIN_KEY", new LLMAnthropicProfile(FRANCE, 500, List.of("claude-opus-5-5"), null, null) ); - private final LLMAnthropicActivityResolver resolver = new LLMAnthropicActivityResolver(multi(this.client)); + private final LLMAnthropicActivityResolver resolver = new LLMAnthropicActivityResolver(this.client); @Test void sumsOutputTokensPerModelAcrossBucketsAndPages() throws WombatActivityException @@ -138,11 +137,4 @@ private void stubEmpty() { when(this.client.usageReport(any(), any(), any(), anyInt(), any(), any(), any(), any())).thenReturn(new AnthropicUsageReport(List.of(), false, null)); } - - private static AnthropicMultiClient multi(AnthropicAdminClient client) - { - AnthropicMultiClient multi = new AnthropicMultiClient(); - multi.register("claude", client); - return multi; - } } diff --git a/module/src/test/java/tech/illuin/wombat/module/llm_anthropic/connector/AnthropicMultiClientConfigTest.java b/module/src/test/java/tech/illuin/wombat/module/llm_anthropic/connector/AnthropicClientConfigTest.java similarity index 88% rename from module/src/test/java/tech/illuin/wombat/module/llm_anthropic/connector/AnthropicMultiClientConfigTest.java rename to module/src/test/java/tech/illuin/wombat/module/llm_anthropic/connector/AnthropicClientConfigTest.java index 440dae7..af43b87 100644 --- a/module/src/test/java/tech/illuin/wombat/module/llm_anthropic/connector/AnthropicMultiClientConfigTest.java +++ b/module/src/test/java/tech/illuin/wombat/module/llm_anthropic/connector/AnthropicClientConfigTest.java @@ -5,8 +5,6 @@ import org.junit.jupiter.api.Test; import tech.illuin.wombat.module.llm_anthropic.LLMAnthropicModule; import tech.illuin.wombat.core.asset.AssetIdentity; -import tech.illuin.wombat.core.asset.Environment; -import tech.illuin.wombat.core.context.ResolvedContext; import tech.illuin.wombat.core.secret.EnvironmentSecretResolver; import tech.illuin.wombat.module.llm_anthropic.LLMAnthropicAsset; import tech.illuin.wombat.module.llm_anthropic.LLMAnthropicProfile; @@ -20,7 +18,7 @@ import static org.junit.jupiter.api.Assertions.*; import static tech.illuin.wombat.core.activity.commons.Country.FRANCE; -class AnthropicMultiClientConfigTest +class AnthropicClientConfigTest { private static final String BODY = """ { @@ -60,11 +58,8 @@ void sendsAdminHeadersAndExpandsArrayParameters() AssetIdentity.of("claude", "prod", "Claude", LLMAnthropicModule.TYPE), null, null, "ANTHROPIC_ADMIN_KEY", new LLMAnthropicProfile(FRANCE, 500, null, null, null) ); - ResolvedContext context = new ResolvedContext( - List.of(new Environment("prod", List.of(asset))), - new EnvironmentSecretResolver(Map.of("ANTHROPIC_ADMIN_KEY", "sk-ant-admin-test")::get) - ); - AnthropicAdminClient client = AnthropicMultiClientConfig.create(context, http).get("claude").orElseThrow(); + EnvironmentSecretResolver secrets = new EnvironmentSecretResolver(Map.of("ANTHROPIC_ADMIN_KEY", "sk-ant-admin-test")::get); + AnthropicAdminClient client = AnthropicClientConfig.create(asset, secrets, http); AnthropicUsageReport report = client.usageReport( "2026-10-01T00:00:00Z", "2026-10-01T01:00:00Z", "1m", 1440, diff --git a/module/src/test/java/tech/illuin/wombat/module/llm_prometheus/LLMPrometheusModuleTest.java b/module/src/test/java/tech/illuin/wombat/module/llm_prometheus/LLMPrometheusModuleTest.java index fa9ee84..97be7d3 100644 --- a/module/src/test/java/tech/illuin/wombat/module/llm_prometheus/LLMPrometheusModuleTest.java +++ b/module/src/test/java/tech/illuin/wombat/module/llm_prometheus/LLMPrometheusModuleTest.java @@ -2,16 +2,15 @@ import org.junit.jupiter.api.Test; import tech.illuin.wombat.core.asset.AssetIdentity; -import tech.illuin.wombat.core.asset.Environment; import tech.illuin.wombat.core.asset.profile.LLMProvider; -import tech.illuin.wombat.core.context.ResolvedContext; +import tech.illuin.wombat.core.module.ProcessorFactory; +import tech.illuin.wombat.core.module.WombatServices; import tech.illuin.wombat.core.secret.EnvironmentSecretResolver; import tech.illuin.wombat.core.secret.MissingSecretException; import tech.illuin.wombat.core.source.Period; import tech.illuin.wombat.core.source.WombatSource; import tech.illuin.wombat.module.llm_prometheus.source.LLMPrometheusSource; -import java.util.List; import java.util.Map; import java.util.Optional; @@ -35,28 +34,34 @@ void declaresThePrometheusLLMAssetType() @Test void providesAPrometheusSource() { - // An empty context registers no Prometheus client, so nothing is dialled. - Optional source = this.module.createSource(new ResolvedContext()); - assertTrue(source.isPresent()); - assertInstanceOf(LLMPrometheusSource.class, source.get()); + // Building the client does not dial Prometheus. + Optional> factory = this.module.source(); + assertTrue(factory.isPresent()); + assertInstanceOf(LLMPrometheusSource.class, factory.get().create(services(Map.of()), asset(null, null))); } @Test void sourcingABasicAuthAssetFailsWhenItsVariableIsUnset() { - // Proves the module builds its clients from the context's resolver: the credential is read from the + // Proves the module builds its clients from the services' resolver: the credential is read from the // environment at source creation, never from the asset the reconciler persisted. - LLMPrometheusAsset asset = new LLMPrometheusAsset( + ProcessorFactory factory = this.module.source().orElseThrow(); + MissingSecretException e = assertThrows(MissingSecretException.class, () -> factory.create(services(Map.of()), asset("user", "PROM_PASSWORD"))); + + assertEquals("PROM_PASSWORD", e.key()); + } + + private static WombatServices services(Map environment) + { + return new WombatServices(new EnvironmentSecretResolver(environment::get)); + } + + private static LLMPrometheusAsset asset(String username, String passwordEnv) + { + return new LLMPrometheusAsset( AssetIdentity.of("p", "env", "P", LLMPrometheusModule.TYPE), - "http://prometheus", null, "user", "PROM_PASSWORD", Period.ZERO, + "http://prometheus", null, username, passwordEnv, Period.ZERO, new LLMPrometheusProfile(LLMProvider.mistralai, "m", FRANCE, new LLMPrometheusProfile.DynamicProfile("q")) ); - ResolvedContext context = new ResolvedContext( - List.of(new Environment("env", List.of(asset))), - new EnvironmentSecretResolver(Map.of()::get) - ); - MissingSecretException e = assertThrows(MissingSecretException.class, () -> this.module.createSource(context)); - - assertEquals("PROM_PASSWORD", e.key()); } } diff --git a/module/src/test/java/tech/illuin/wombat/module/llm_prometheus/connector/PrometheusMultiClientConfigTest.java b/module/src/test/java/tech/illuin/wombat/module/llm_prometheus/connector/PrometheusClientConfigTest.java similarity index 55% rename from module/src/test/java/tech/illuin/wombat/module/llm_prometheus/connector/PrometheusMultiClientConfigTest.java rename to module/src/test/java/tech/illuin/wombat/module/llm_prometheus/connector/PrometheusClientConfigTest.java index 6d2d70d..98a0221 100644 --- a/module/src/test/java/tech/illuin/wombat/module/llm_prometheus/connector/PrometheusMultiClientConfigTest.java +++ b/module/src/test/java/tech/illuin/wombat/module/llm_prometheus/connector/PrometheusClientConfigTest.java @@ -3,61 +3,51 @@ import org.junit.jupiter.api.Test; import tech.illuin.wombat.module.llm_prometheus.LLMPrometheusModule; import tech.illuin.wombat.core.asset.AssetIdentity; -import tech.illuin.wombat.core.asset.Environment; import tech.illuin.wombat.core.asset.profile.LLMProvider; -import tech.illuin.wombat.core.context.ResolvedContext; -import tech.illuin.wombat.core.context.WombatContext; import tech.illuin.wombat.core.secret.EnvironmentSecretResolver; import tech.illuin.wombat.core.secret.MissingSecretException; +import tech.illuin.wombat.core.secret.SecretResolver; import tech.illuin.wombat.core.source.Period; import tech.illuin.wombat.module.llm_prometheus.LLMPrometheusAsset; import tech.illuin.wombat.module.llm_prometheus.LLMPrometheusProfile; -import java.util.List; import java.util.Map; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; import static tech.illuin.wombat.core.activity.commons.Country.FRANCE; -public class PrometheusMultiClientConfigTest +public class PrometheusClientConfigTest { @Test - void buildsAClientPerAssetWithoutAuthentication() + void buildsAClientWithoutAuthentication() { - PrometheusMultiClient multi = PrometheusMultiClientConfig.create( - context(Map.of(), asset("p", null, null)) - ); - assertTrue(multi.get("p").isPresent()); + PrometheusClient client = PrometheusClientConfig.create(asset("p", null, null), secrets(Map.of())); + assertNotNull(client); } @Test void readsTheBasicAuthPasswordFromTheDeclaredVariable() { - PrometheusMultiClient multi = PrometheusMultiClientConfig.create( - context(Map.of("PROM_PASSWORD", "s3cret"), asset("p", "user", "PROM_PASSWORD")) - ); - assertTrue(multi.get("p").isPresent()); + PrometheusClient client = PrometheusClientConfig.create(asset("p", "user", "PROM_PASSWORD"), secrets(Map.of("PROM_PASSWORD", "s3cret"))); + assertNotNull(client); } @Test void failsRatherThanAuthenticatingWithABlankPassword() { - // create() runs at source creation, so an unset variable stops the module wiring rather than producing a - // client that would 401 on every scrape. - WombatContext context = context(Map.of(), asset("p", "user", "PROM_PASSWORD")); - MissingSecretException e = assertThrows(MissingSecretException.class, () -> PrometheusMultiClientConfig.create(context)); + // create() runs at source creation, so an unset variable fails the source rather than producing a client + // that would 401 on every scrape. + SecretResolver secrets = secrets(Map.of()); + MissingSecretException e = assertThrows(MissingSecretException.class, () -> PrometheusClientConfig.create(asset("p", "user", "PROM_PASSWORD"), secrets)); assertEquals("PROM_PASSWORD", e.key()); } - private static WombatContext context(Map environment, LLMPrometheusAsset asset) + private static SecretResolver secrets(Map environment) { - return new ResolvedContext( - List.of(new Environment("env", List.of(asset))), - new EnvironmentSecretResolver(environment::get) - ); + return new EnvironmentSecretResolver(environment::get); } private static LLMPrometheusAsset asset(String id, String username, String passwordEnv) diff --git a/module/src/test/java/tech/illuin/wombat/module/llm_prometheus/source/LLMPrometheusSourceTest.java b/module/src/test/java/tech/illuin/wombat/module/llm_prometheus/source/LLMPrometheusSourceTest.java index df82387..c1ef958 100644 --- a/module/src/test/java/tech/illuin/wombat/module/llm_prometheus/source/LLMPrometheusSourceTest.java +++ b/module/src/test/java/tech/illuin/wombat/module/llm_prometheus/source/LLMPrometheusSourceTest.java @@ -12,7 +12,6 @@ import tech.illuin.wombat.module.llm_prometheus.LLMPrometheusAsset; import tech.illuin.wombat.module.llm_prometheus.LLMPrometheusProfile; import tech.illuin.wombat.module.llm_prometheus.connector.PrometheusClient; -import tech.illuin.wombat.module.llm_prometheus.connector.PrometheusMultiClient; import tech.illuin.wombat.module.llm_prometheus.connector.model.PrometheusQueryResponse; import java.time.Instant; @@ -50,9 +49,7 @@ void sample_emitsLLMDataWithProviderModelAndLocation() when(client.query("sum(vllm:num_tokens_total)")).thenReturn(response); - PrometheusMultiClient multiClient = new PrometheusMultiClient(); - multiClient.register("llm-prom", client); - LLMPrometheusSource source = new LLMPrometheusSource(multiClient); + LLMPrometheusSource source = new LLMPrometheusSource(client); List dataList = source.source(Instant.now(), null, asset); assertEquals(1, dataList.size()); diff --git a/module/src/test/java/tech/illuin/wombat/module/llm_static/LLMStaticModuleTest.java b/module/src/test/java/tech/illuin/wombat/module/llm_static/LLMStaticModuleTest.java index 92aa597..0a6378f 100644 --- a/module/src/test/java/tech/illuin/wombat/module/llm_static/LLMStaticModuleTest.java +++ b/module/src/test/java/tech/illuin/wombat/module/llm_static/LLMStaticModuleTest.java @@ -22,7 +22,7 @@ void declaresTheStaticLLMAssetType() @Test void providesAStaticActivityResolver() { - Optional resolver = this.module.createActivityResolver(null); + Optional resolver = this.module.activityResolver().map(factory -> factory.create(null, null)); assertTrue(resolver.isPresent()); assertInstanceOf(LLMStaticActivityResolver.class, resolver.get()); } @@ -34,6 +34,6 @@ void providesAStaticActivityResolver() @Test void leavesTheImpactToTheFamilyDefault() { - assertTrue(this.module.createImpactResolver(null).isEmpty()); + assertTrue(this.module.impactResolver().isEmpty()); } } diff --git a/module/src/test/java/tech/illuin/wombat/module/llm_vertexai/LLMVertexAIModuleTest.java b/module/src/test/java/tech/illuin/wombat/module/llm_vertexai/LLMVertexAIModuleTest.java index 63246ff..6845e28 100644 --- a/module/src/test/java/tech/illuin/wombat/module/llm_vertexai/LLMVertexAIModuleTest.java +++ b/module/src/test/java/tech/illuin/wombat/module/llm_vertexai/LLMVertexAIModuleTest.java @@ -3,8 +3,8 @@ import org.junit.jupiter.api.Test; import tech.illuin.wombat.core.activity.WombatActivityResolver; import tech.illuin.wombat.core.asset.AssetIdentity; -import tech.illuin.wombat.core.asset.Environment; -import tech.illuin.wombat.core.context.ResolvedContext; +import tech.illuin.wombat.core.module.ProcessorFactory; +import tech.illuin.wombat.core.module.WombatServices; import tech.illuin.wombat.core.secret.EnvironmentSecretResolver; import tech.illuin.wombat.core.secret.MissingSecretException; import tech.illuin.wombat.module.llm_vertexai.activity.LLMVertexAIActivityResolver; @@ -32,40 +32,36 @@ void declaresTheGoogleLLMAssetType() @Test void providesAGoogleActivityResolverAndNoSource() { - // An empty context registers no Google client, so nothing is dialled. - Optional resolver = this.module.createActivityResolver(new ResolvedContext()); - assertTrue(resolver.isPresent()); - assertInstanceOf(LLMVertexAIActivityResolver.class, resolver.get()); - assertTrue(this.module.createSource(new ResolvedContext()).isEmpty()); + Optional> factory = this.module.activityResolver(); + assertTrue(factory.isPresent()); + assertTrue(this.module.source().isEmpty()); } @Test void resolvingAnAssetWithoutCredentialsKeyDoesNotRequireGoogleCredentialsAtStartup() { - // Application Default Credentials are resolved lazily on the first request, so wiring must succeed even on - // a machine (CI, a laptop) that has none. - ResolvedContext context = context(Map.of(), asset(null)); + // Application Default Credentials are resolved lazily on the first request, so building the resolver must + // succeed even on a machine (CI, a laptop) that has none. + ProcessorFactory factory = this.module.activityResolver().orElseThrow(); - assertTrue(this.module.createActivityResolver(context).isPresent()); + assertInstanceOf(LLMVertexAIActivityResolver.class, factory.create(services(Map.of()), asset(null))); } @Test void resolvingAServiceAccountAssetFailsWhenItsVariableIsUnset() { - // Proves the module builds its clients from the context's resolver: the credential is read from the + // Proves the module builds its clients from the services' resolver: the credential is read from the // environment at resolver creation, never from the asset the reconciler persisted. - ResolvedContext context = context(Map.of(), asset("GOOGLE_SA_KEY")); + ProcessorFactory factory = this.module.activityResolver().orElseThrow(); - MissingSecretException e = assertThrows(MissingSecretException.class, () -> this.module.createActivityResolver(context)); + MissingSecretException e = assertThrows(MissingSecretException.class, () -> factory.create(services(Map.of()), asset("GOOGLE_SA_KEY"))); assertEquals("GOOGLE_SA_KEY", e.key()); } - private static ResolvedContext context(Map environment, LLMVertexAIAsset asset) + private static WombatServices services(Map environment) { - return new ResolvedContext( - List.of(new Environment("env", List.of(asset))), - new EnvironmentSecretResolver(environment::get)); + return new WombatServices(new EnvironmentSecretResolver(environment::get)); } private static LLMVertexAIAsset asset(String credentialsKey) diff --git a/module/src/test/java/tech/illuin/wombat/module/llm_vertexai/activity/LLMVertexAIActivityResolverTest.java b/module/src/test/java/tech/illuin/wombat/module/llm_vertexai/activity/LLMVertexAIActivityResolverTest.java index 10250e9..2764ab9 100644 --- a/module/src/test/java/tech/illuin/wombat/module/llm_vertexai/activity/LLMVertexAIActivityResolverTest.java +++ b/module/src/test/java/tech/illuin/wombat/module/llm_vertexai/activity/LLMVertexAIActivityResolverTest.java @@ -19,7 +19,6 @@ import tech.illuin.wombat.module.llm_vertexai.LLMVertexAIAsset; import tech.illuin.wombat.module.llm_vertexai.LLMVertexAIProfile; import tech.illuin.wombat.module.llm_vertexai.connector.GoogleClient; -import tech.illuin.wombat.module.llm_vertexai.connector.GoogleMultiClient; import tech.illuin.wombat.module.llm_vertexai.connector.model.GoogleQueryResponse; import java.nio.charset.StandardCharsets; @@ -49,7 +48,7 @@ class LLMVertexAIActivityResolverTest AssetIdentity.of("llm-google", "prod", "Google LLM", LLMVertexAIModule.TYPE), "https://monitoring.googleapis.com", PROJECT, null, new LLMVertexAIProfile(500, List.of(MODEL)) ); - private final LLMVertexAIActivityResolver resolver = new LLMVertexAIActivityResolver(multi(this.client)); + private final LLMVertexAIActivityResolver resolver = new LLMVertexAIActivityResolver(this.client); @Test void queriesTheWholeRangeOnceAlignedOnItsResolution() throws Exception @@ -177,16 +176,6 @@ void wrapsApiFailures() assertTrue(e.getMessage().contains("llm-google"), e.getMessage()); } - @Test - void failsWhenNoClientIsRegisteredForTheAsset() - { - LLMVertexAIActivityResolver resolver = new LLMVertexAIActivityResolver(new GoogleMultiClient()); - - IllegalStateException e = assertThrows(IllegalStateException.class, () -> resolver.resolve(this.asset, THREE_HOURS, null)); - - assertTrue(e.getMessage().contains("llm-google"), e.getMessage()); - } - @Test void acceptsOnlyVertexAIAssets() { @@ -292,11 +281,4 @@ private static GoogleQueryResponse response(String json) throws Exception { return MAPPER.readValue(json, GoogleQueryResponse.class); } - - private static GoogleMultiClient multi(GoogleClient client) - { - GoogleMultiClient multi = new GoogleMultiClient(); - multi.register("llm-google", client); - return multi; - } } diff --git a/module/src/test/java/tech/illuin/wombat/module/llm_vertexai/connector/GoogleMultiClientConfigTest.java b/module/src/test/java/tech/illuin/wombat/module/llm_vertexai/connector/GoogleClientConfigTest.java similarity index 77% rename from module/src/test/java/tech/illuin/wombat/module/llm_vertexai/connector/GoogleMultiClientConfigTest.java rename to module/src/test/java/tech/illuin/wombat/module/llm_vertexai/connector/GoogleClientConfigTest.java index 5e213da..976bd74 100644 --- a/module/src/test/java/tech/illuin/wombat/module/llm_vertexai/connector/GoogleMultiClientConfigTest.java +++ b/module/src/test/java/tech/illuin/wombat/module/llm_vertexai/connector/GoogleClientConfigTest.java @@ -5,11 +5,9 @@ import org.junit.jupiter.api.Test; import tech.illuin.wombat.module.llm_vertexai.LLMVertexAIModule; import tech.illuin.wombat.core.asset.AssetIdentity; -import tech.illuin.wombat.core.asset.Environment; -import tech.illuin.wombat.core.context.ResolvedContext; -import tech.illuin.wombat.core.context.WombatContext; import tech.illuin.wombat.core.secret.EnvironmentSecretResolver; import tech.illuin.wombat.core.secret.MissingSecretException; +import tech.illuin.wombat.core.secret.SecretResolver; import tech.illuin.wombat.module.llm_vertexai.LLMVertexAIAsset; import tech.illuin.wombat.module.llm_vertexai.LLMVertexAIProfile; @@ -22,36 +20,35 @@ import java.util.Map; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -public class GoogleMultiClientConfigTest +public class GoogleClientConfigTest { @Test - void buildsAClientPerAssetRelyingOnApplicationDefaultCredentials() + void buildsAClientRelyingOnApplicationDefaultCredentials() { - GoogleMultiClient multi = GoogleMultiClientConfig.create( - context(Map.of(), asset("g", null))); + GoogleClient client = GoogleClientConfig.create(asset("g", null), secrets(Map.of())); - assertTrue(multi.get("g").isPresent()); + assertNotNull(client); } @Test void buildsAClientFromAServiceAccountKeyReadFromTheDeclaredVariable() throws Exception { - GoogleMultiClient multi = GoogleMultiClientConfig.create( - context(Map.of("GOOGLE_SA_KEY", serviceAccountJson()), asset("g", "GOOGLE_SA_KEY"))); + GoogleClient client = GoogleClientConfig.create(asset("g", "GOOGLE_SA_KEY"), secrets(Map.of("GOOGLE_SA_KEY", serviceAccountJson()))); - assertTrue(multi.get("g").isPresent()); + assertNotNull(client); } @Test void failsWhenTheServiceAccountVariableIsUnset() { - WombatContext context = context(Map.of(), asset("g", "GOOGLE_SA_KEY")); + SecretResolver secrets = secrets(Map.of()); MissingSecretException e = assertThrows(MissingSecretException.class, - () -> GoogleMultiClientConfig.create(context)); + () -> GoogleClientConfig.create(asset("g", "GOOGLE_SA_KEY"), secrets)); assertEquals("GOOGLE_SA_KEY", e.key()); } @@ -59,9 +56,9 @@ void failsWhenTheServiceAccountVariableIsUnset() @Test void failsOnAMalformedServiceAccountKey() { - WombatContext context = context(Map.of("GOOGLE_SA_KEY", "not json"), asset("g", "GOOGLE_SA_KEY")); + SecretResolver secrets = secrets(Map.of("GOOGLE_SA_KEY", "not json")); - assertThrows(IllegalStateException.class, () -> GoogleMultiClientConfig.create(context)); + assertThrows(IllegalStateException.class, () -> GoogleClientConfig.create(asset("g", "GOOGLE_SA_KEY"), secrets)); } @Test @@ -89,8 +86,7 @@ void routesTokenRefreshAndMonitoringRequestsThroughTheProxy() throws Exception LLMVertexAIAsset base = asset("g", "GOOGLE_SA_KEY"); LLMVertexAIAsset proxied = new LLMVertexAIAsset(base.identity(), "http://monitoring.invalid", base.resourceName(), base.credentialsKey(), "http://127.0.0.1:" + proxy.getAddress().getPort(), base.profile()); - GoogleClient client = GoogleMultiClientConfig.create(context(Map.of("GOOGLE_SA_KEY", mapper.writeValueAsString(json)), proxied)) - .get("g").orElseThrow(); + GoogleClient client = GoogleClientConfig.create(proxied, secrets(Map.of("GOOGLE_SA_KEY", mapper.writeValueAsString(json)))); assertEquals(List.of(), client.query("projects/my-project", "f", "start", "end", "3600s", null).timeSeries()); assertTrue(requests.stream().anyMatch(request -> request.startsWith("POST http://oauth.invalid/token")), requests.toString()); @@ -108,15 +104,13 @@ void rejectsInvalidProxyUrls() for (String url : List.of("http://localhost", "https://localhost:8080", "http://user:pass@localhost:8080", "http://localhost:8080/path")) { LLMVertexAIAsset invalid = new LLMVertexAIAsset(base.identity(), base.googleUrl(), base.resourceName(), null, url, base.profile()); - assertThrows(IllegalStateException.class, () -> GoogleMultiClientConfig.create(context(Map.of(), invalid)), url); + assertThrows(IllegalStateException.class, () -> GoogleClientConfig.create(invalid, secrets(Map.of())), url); } } - private static WombatContext context(Map environment, LLMVertexAIAsset asset) + private static SecretResolver secrets(Map environment) { - return new ResolvedContext( - List.of(new Environment("env", List.of(asset))), - new EnvironmentSecretResolver(environment::get)); + return new EnvironmentSecretResolver(environment::get); } private static LLMVertexAIAsset asset(String id, String credentialsKey)