From 466415a0f01fc73994e08c54ff516f22f5d07c0b Mon Sep 17 00:00:00 2001 From: Pierre Lecerf Date: Fri, 20 Nov 2026 19:58:18 +0100 Subject: [PATCH 1/6] feat(engine): swappable core runtime --- .../engine/module/WombatCoreConfig.java | 62 +-------- .../engine/module/WombatModuleConfig.java | 8 ++ .../engine/module/runtime/CoreDeployment.java | 52 +++++++ .../module/runtime/CoreDeploymentFactory.java | 102 ++++++++++++++ .../engine/module/runtime/CoreRuntime.java | 122 +++++++++++++++++ .../module/runtime/RuntimeAssetEvaluator.java | 96 +++++++++++++ .../module/runtime/RuntimeAssetMonitor.java | 48 +++++++ .../wombat/engine/monitor/MonitorConfig.java | 10 +- .../engine/module/WombatCoreConfigTest.java | 21 ++- .../engine/module/WombatModuleConfigTest.java | 12 ++ .../runtime/CoreDeploymentFactoryTest.java | 99 ++++++++++++++ .../module/runtime/CoreRuntimeTest.java | 127 +++++++++++++++++ .../module/runtime/RuntimeProxiesTest.java | 129 ++++++++++++++++++ 13 files changed, 824 insertions(+), 64 deletions(-) create mode 100644 src/main/java/tech/illuin/wombat/engine/module/runtime/CoreDeployment.java create mode 100644 src/main/java/tech/illuin/wombat/engine/module/runtime/CoreDeploymentFactory.java create mode 100644 src/main/java/tech/illuin/wombat/engine/module/runtime/CoreRuntime.java create mode 100644 src/main/java/tech/illuin/wombat/engine/module/runtime/RuntimeAssetEvaluator.java create mode 100644 src/main/java/tech/illuin/wombat/engine/module/runtime/RuntimeAssetMonitor.java create mode 100644 src/test/java/tech/illuin/wombat/engine/module/runtime/CoreDeploymentFactoryTest.java create mode 100644 src/test/java/tech/illuin/wombat/engine/module/runtime/CoreRuntimeTest.java create mode 100644 src/test/java/tech/illuin/wombat/engine/module/runtime/RuntimeProxiesTest.java diff --git a/src/main/java/tech/illuin/wombat/engine/module/WombatCoreConfig.java b/src/main/java/tech/illuin/wombat/engine/module/WombatCoreConfig.java index 578b3ac..6af78df 100644 --- a/src/main/java/tech/illuin/wombat/engine/module/WombatCoreConfig.java +++ b/src/main/java/tech/illuin/wombat/engine/module/WombatCoreConfig.java @@ -1,72 +1,24 @@ package tech.illuin.wombat.engine.module; -import io.quarkus.arc.All; import jakarta.enterprise.context.ApplicationScoped; -import jakarta.enterprise.inject.Disposes; import jakarta.inject.Singleton; -import tech.illuin.wombat.core.WombatCore; -import tech.illuin.wombat.core.context.WombatContextProvider; import tech.illuin.wombat.core.evaluation.AssetEvaluator; -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.commons.WombatProperties; -import tech.illuin.wombat.core.source.persistence.WombatMetricPersister; -import tech.illuin.wombat.engine.evaluation.CachedAssetEvaluator; -import tech.illuin.wombat.engine.evaluation.CachedAssetEvaluatorProperties; -import tech.illuin.wombat.engine.module.WombatModuleConfig.DefaultHandlers; -import tech.illuin.wombat.engine.module.registry.ModuleMetadata; -import tech.illuin.wombat.engine.module.registry.ModuleRegistry; - -import java.util.List; +import tech.illuin.wombat.engine.module.runtime.CoreRuntime; +import tech.illuin.wombat.engine.module.runtime.RuntimeAssetEvaluator; /** - * Assembles the SDK entry point out of the beans the app provides: the context to resolve, the secrets modules read - * credentials from, the impact-resolvers the modules draw on, the persister sourced metrics are written through, and - * the modules {@link WombatModuleConfig} decided to wire. + * Exposes the evaluator of the running core. The core itself is assembled by + * {@link tech.illuin.wombat.engine.module.runtime.CoreDeploymentFactory}, and swapped on redeploy by {@link CoreRuntime}. */ @ApplicationScoped public class WombatCoreConfig { - @Singleton - public WombatCore provideWombatCore( - WombatContextProvider contextProvider, - SecretResolver secrets, - WombatMetricPersister persister, - List modules, - ModuleRegistry moduleRegistry, - @All List defaultHandlers - ) { - List activeModules = modules.stream() - .filter(m -> moduleRegistry.getModule(m.type()).map(ModuleMetadata::enabled).orElse(false)) - .toList(); - - // Default handlers are stateless singletons, the same instance serves every asset of their family. - return new WombatCore(contextProvider, new WombatServices(secrets), persister, activeModules, defaults -> defaultHandlers.forEach( - dh -> defaults.register( - dh.family(), - (services, asset) -> dh.activityResolver(), - (services, asset) -> dh.impactResolver(), - (services, asset) -> dh.costResolver()) - ) - ); - } - /** - * Resolvers are built per asset on first use, so a calculator keeps serving assets added later as long as their - * module is already wired; a brand-new module needs a fresh one. + * Delegates to the current deployment on every call, so that its holders follow redeploys. */ @Singleton - public AssetEvaluator provideImpactCalculator(WombatCore core, WombatProperties wombat) - { - CachedAssetEvaluatorProperties cacheProperties = wombat.evaluator().cache(); - if (cacheProperties.enabled()) - return new CachedAssetEvaluator(core.createEvaluator(), cacheProperties); - return core.createEvaluator(); - } - - public void closeWombatCore(@Disposes WombatCore core) + public AssetEvaluator provideAssetEvaluator(CoreRuntime runtime) { - core.close(); + return new RuntimeAssetEvaluator(runtime); } } diff --git a/src/main/java/tech/illuin/wombat/engine/module/WombatModuleConfig.java b/src/main/java/tech/illuin/wombat/engine/module/WombatModuleConfig.java index 94b6608..17d8cc1 100644 --- a/src/main/java/tech/illuin/wombat/engine/module/WombatModuleConfig.java +++ b/src/main/java/tech/illuin/wombat/engine/module/WombatModuleConfig.java @@ -108,6 +108,14 @@ public static List provideWombatModules(ExtensionLoader extensionL return modules; } + /** + * Every core deployment shares these instances, so they are closed here, once, rather than with a deployment. + */ + public static void closeWombatModules(@Disposes List modules) + { + modules.forEach(WombatModule::close); + } + @Produces @Singleton public static ModuleRegistry provideModuleRegistry(List modules, WombatProperties wombat) { diff --git a/src/main/java/tech/illuin/wombat/engine/module/runtime/CoreDeployment.java b/src/main/java/tech/illuin/wombat/engine/module/runtime/CoreDeployment.java new file mode 100644 index 0000000..1299000 --- /dev/null +++ b/src/main/java/tech/illuin/wombat/engine/module/runtime/CoreDeployment.java @@ -0,0 +1,52 @@ +package tech.illuin.wombat.engine.module.runtime; + +import tech.illuin.wombat.core.asset.type.AssetType; +import tech.illuin.wombat.core.evaluation.AssetEvaluator; +import tech.illuin.wombat.core.module.WombatModule; +import tech.illuin.wombat.core.source.AssetMonitor; + +import java.time.Instant; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * The monitor and evaluator built out of one set of modules, swapped as a whole by {@link CoreRuntime}. + * + * @param modules the module instances wired in, shared with the other deployments + * @param missing the requested asset-types that no module detected on this instance provides + * @param evaluator the evaluator the app serves, cached when the evaluator cache is enabled + * @param uncachedEvaluator the evaluator underneath the cache, or {@code evaluator} itself when the cache is disabled + */ +public record CoreDeployment( + List modules, + Set missing, + AssetMonitor monitor, + AssetEvaluator evaluator, + AssetEvaluator uncachedEvaluator, + Instant deployedAt +) { + public CoreDeployment + { + modules = List.copyOf(modules); + missing = Set.copyOf(missing); + } + + public Set types() + { + return this.modules.stream() + .map(WombatModule::type) + .collect(Collectors.toUnmodifiableSet()); + } + + /** + * Closes the monitor and the evaluator, which own the components and executors; never the modules, which the other + * deployments share. Both wait for the work already submitted to their executor. + */ + void close() + { + this.monitor.close(); + /* A cached evaluator closes its delegate, the uncached one */ + this.evaluator.close(); + } +} diff --git a/src/main/java/tech/illuin/wombat/engine/module/runtime/CoreDeploymentFactory.java b/src/main/java/tech/illuin/wombat/engine/module/runtime/CoreDeploymentFactory.java new file mode 100644 index 0000000..560f82b --- /dev/null +++ b/src/main/java/tech/illuin/wombat/engine/module/runtime/CoreDeploymentFactory.java @@ -0,0 +1,102 @@ +package tech.illuin.wombat.engine.module.runtime; + +import io.quarkus.arc.All; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.inject.Inject; +import tech.illuin.wombat.commons.WombatProperties; +import tech.illuin.wombat.core.WombatCore; +import tech.illuin.wombat.core.context.WombatContextProvider; +import tech.illuin.wombat.core.evaluation.AssetEvaluator; +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; +import tech.illuin.wombat.engine.evaluation.CachedAssetEvaluator; +import tech.illuin.wombat.engine.evaluation.CachedAssetEvaluatorProperties; +import tech.illuin.wombat.engine.module.WombatModuleConfig.DefaultHandlers; + +import java.time.Instant; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +/** + * Builds a {@link CoreDeployment} out of the beans the app provides: the context to resolve, the secrets modules read + * credentials from, the impact-resolvers the modules draw on, the persister sourced metrics are written through, and + * the requested modules among those detected on this instance. + */ +@ApplicationScoped +public class CoreDeploymentFactory +{ + private final WombatContextProvider contextProvider; + private final SecretResolver secrets; + private final WombatMetricPersister persister; + private final List modules; + private final List defaultHandlers; + private final CachedAssetEvaluatorProperties cacheProperties; + + @Inject + public CoreDeploymentFactory( + WombatContextProvider contextProvider, + SecretResolver secrets, + WombatMetricPersister persister, + List modules, + @All List defaultHandlers, + WombatProperties wombat + ) { + this(contextProvider, secrets, persister, modules, defaultHandlers, wombat.evaluator().cache()); + } + + CoreDeploymentFactory( + WombatContextProvider contextProvider, + SecretResolver secrets, + WombatMetricPersister persister, + List modules, + List defaultHandlers, + CachedAssetEvaluatorProperties cacheProperties + ) { + this.contextProvider = contextProvider; + this.secrets = secrets; + this.persister = persister; + this.modules = modules; + this.defaultHandlers = defaultHandlers; + this.cacheProperties = cacheProperties; + } + + /** + * @param requested the names of the asset-types to deploy; those no detected module provides are reported as missing + */ + public CoreDeployment create(Set requested) + { + List deployed = this.modules.stream() + .filter(module -> requested.contains(module.type().name())) + .toList(); + + Set missing = new HashSet<>(requested); + deployed.forEach(module -> missing.remove(module.type().name())); + + /* The core holds nothing but the modules, which are closed by CoreRuntime: it is not kept, nor closed. */ + WombatCore core = this.createCore(deployed); + AssetMonitor monitor = core.createMonitor(); + AssetEvaluator uncached = core.createEvaluator(); + AssetEvaluator evaluator = this.cacheProperties.enabled() + ? new CachedAssetEvaluator(uncached, this.cacheProperties) + : uncached; + + return new CoreDeployment(deployed, missing, monitor, evaluator, uncached, Instant.now()); + } + + private WombatCore createCore(List deployed) + { + // Default handlers are stateless singletons, the same instance serves every asset of their family. + return new WombatCore(this.contextProvider, new WombatServices(this.secrets), this.persister, deployed, defaults -> this.defaultHandlers.forEach( + dh -> defaults.register( + dh.family(), + (services, asset) -> dh.activityResolver(), + (services, asset) -> dh.impactResolver(), + (services, asset) -> dh.costResolver()) + ) + ); + } +} diff --git a/src/main/java/tech/illuin/wombat/engine/module/runtime/CoreRuntime.java b/src/main/java/tech/illuin/wombat/engine/module/runtime/CoreRuntime.java new file mode 100644 index 0000000..2adc679 --- /dev/null +++ b/src/main/java/tech/illuin/wombat/engine/module/runtime/CoreRuntime.java @@ -0,0 +1,122 @@ +package tech.illuin.wombat.engine.module.runtime; + +import io.quarkus.runtime.StartupEvent; +import jakarta.annotation.PreDestroy; +import jakarta.annotation.Priority; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.event.Observes; +import jakarta.inject.Inject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import tech.illuin.wombat.core.asset.type.AssetType; +import tech.illuin.wombat.engine.module.registry.ModuleMetadata; +import tech.illuin.wombat.engine.module.registry.ModuleRegistry; + +import java.time.Duration; +import java.util.HashSet; +import java.util.Set; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.Executor; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; + +/** + * Holds the running {@link CoreDeployment}, and swaps it for a new one on redeploy. + *

+ * The app's {@code AssetMonitor} and {@code AssetEvaluator} beans read {@link #current()} on every call, see + * {@link RuntimeAssetMonitor} and {@link RuntimeAssetEvaluator}, so they follow a swap at once. The replaced deployment + * is closed after a grace delay, leaving the calls that already hold it time to complete. + */ +@ApplicationScoped +public class CoreRuntime +{ + private final CoreDeploymentFactory factory; + private final ModuleRegistry registry; + private final Executor retiredCloser; + private volatile CoreDeployment current; + + /* After the environments are reconciled, and before the monitor is scheduled */ + static final int STARTUP_PRIORITY_DEPLOY = 2100; + private static final Duration CLOSE_GRACE_DELAY = Duration.ofSeconds(60); + private static final Logger logger = LoggerFactory.getLogger(CoreRuntime.class); + + @Inject + public CoreRuntime(CoreDeploymentFactory factory, ModuleRegistry registry) + { + this(factory, registry, CompletableFuture.delayedExecutor(CLOSE_GRACE_DELAY.toSeconds(), TimeUnit.SECONDS)); + } + + CoreRuntime(CoreDeploymentFactory factory, ModuleRegistry registry, Executor retiredCloser) + { + this.factory = factory; + this.registry = registry; + this.retiredCloser = retiredCloser; + } + + void onStart(@Observes @Priority(STARTUP_PRIORITY_DEPLOY) StartupEvent event) + { + this.deploy(this.registry.listModules().stream() + .filter(ModuleMetadata::enabled) + .map(module -> module.type().name()) + .collect(Collectors.toSet()) + ); + } + + /** + * @throws IllegalStateException before the first deployment + */ + public CoreDeployment current() + { + CoreDeployment deployment = this.current; + if (deployment == null) + throw new IllegalStateException("The wombat core is not deployed yet"); + return deployment; + } + + /** + * Builds a deployment of the requested asset-types and swaps it in. If the build fails, the running deployment is + * kept and the exception is thrown. Deployments are applied one at a time. + */ + public synchronized CoreDeployment deploy(Set requested) + { + CoreDeployment next = this.factory.create(requested); + CoreDeployment previous = this.current; + this.current = next; + + if (previous != null) + this.retiredCloser.execute(previous::close); + this.logChanges(previous, next); + return next; + } + + /* Synchronized with deploy, so that a deployment swapped in during shutdown is the one closed */ + @PreDestroy + synchronized void close() + { + CoreDeployment deployment = this.current; + if (deployment != null) + deployment.close(); + } + + private void logChanges(CoreDeployment previous, CoreDeployment next) + { + Set before = previous == null ? Set.of() : names(previous.types()); + Set after = names(next.types()); + + Set added = new HashSet<>(after); + added.removeAll(before); + Set removed = new HashSet<>(before); + removed.removeAll(after); + + logger.info("Deployed wombat core with {} module(s), added {}, removed {}", after.size(), added, removed); + if (!next.missing().isEmpty()) + logger.warn("Requested asset-types with no module on this instance: {}", next.missing()); + } + + private static Set names(Set types) + { + return types.stream() + .map(AssetType::name) + .collect(Collectors.toSet()); + } +} diff --git a/src/main/java/tech/illuin/wombat/engine/module/runtime/RuntimeAssetEvaluator.java b/src/main/java/tech/illuin/wombat/engine/module/runtime/RuntimeAssetEvaluator.java new file mode 100644 index 0000000..ab9bb3d --- /dev/null +++ b/src/main/java/tech/illuin/wombat/engine/module/runtime/RuntimeAssetEvaluator.java @@ -0,0 +1,96 @@ +package tech.illuin.wombat.engine.module.runtime; + +import tech.illuin.wombat.core.activity.WombatActivityException; +import tech.illuin.wombat.core.activity.WombatActivityResolver; +import tech.illuin.wombat.core.activity.commons.ActivityData; +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.type.AssetType; +import tech.illuin.wombat.core.evaluation.AssetEvaluation; +import tech.illuin.wombat.core.evaluation.AssetEvaluator; +import tech.illuin.wombat.core.evaluation.WombatEvaluationException; +import tech.illuin.wombat.core.evaluation.WombatEvaluationResolver; +import tech.illuin.wombat.core.module.ProcessorFactory; + +import java.util.List; +import java.util.Optional; + +/** + * The app's {@link AssetEvaluator}: each call runs on the evaluator of the current {@link CoreDeployment}. A subclass + * because the core evaluator is a class; every public method is overridden, so the parent's own state is never used. + */ +public class RuntimeAssetEvaluator extends AssetEvaluator +{ + private final CoreRuntime runtime; + + public RuntimeAssetEvaluator(CoreRuntime runtime) + { + super(null, null); + this.runtime = runtime; + } + + @Override + public List evaluate(TimeRange range, AssetFilter filter, Class evalType) throws WombatEvaluationException + { + return this.runtime.current().evaluator().evaluate(range, filter, evalType); + } + + @Override + public List evaluate(TimeRange range, AssetFilter filter) throws WombatEvaluationException + { + return this.runtime.current().evaluator().evaluate(range, filter); + } + + @Override + public void refresh(AssetIdentity identity) + { + this.runtime.current().evaluator().refresh(identity); + } + + @Override + public Optional computeActivity(Asset asset, TimeRange range, AssetFilter filter) throws WombatActivityException + { + return this.runtime.current().evaluator().computeActivity(asset, range, filter); + } + + @Override + public Optional computeImpact(Asset asset, ActivityData activity) throws WombatEvaluationException + { + return this.runtime.current().evaluator().computeImpact(asset, activity); + } + + @Override + public Optional computeCost(Asset asset, ActivityData activity) throws WombatEvaluationException + { + return this.runtime.current().evaluator().computeCost(asset, activity); + } + + /** Resolvers are registered by {@link CoreDeploymentFactory}, from the deployed modules. */ + @Override + public AssetEvaluator registerActivityResolver(AssetType type, ProcessorFactory factory) + { + throw new UnsupportedOperationException("Resolvers come from the deployed modules"); + } + + @Override + public AssetEvaluator registerCostResolver(AssetType type, ProcessorFactory factory) + { + throw new UnsupportedOperationException("Resolvers come from the deployed modules"); + } + + @Override + public AssetEvaluator registerImpactResolver(AssetType type, ProcessorFactory factory) + { + throw new UnsupportedOperationException("Resolvers come from the deployed modules"); + } + + /** Only closes the parent's unused executor: the deployments are closed by {@link CoreRuntime}. */ + @Override + public void close() + { + super.close(); + } +} diff --git a/src/main/java/tech/illuin/wombat/engine/module/runtime/RuntimeAssetMonitor.java b/src/main/java/tech/illuin/wombat/engine/module/runtime/RuntimeAssetMonitor.java new file mode 100644 index 0000000..f2edef5 --- /dev/null +++ b/src/main/java/tech/illuin/wombat/engine/module/runtime/RuntimeAssetMonitor.java @@ -0,0 +1,48 @@ +package tech.illuin.wombat.engine.module.runtime; + +import tech.illuin.wombat.core.asset.AssetIdentity; +import tech.illuin.wombat.core.asset.type.AssetType; +import tech.illuin.wombat.core.module.ProcessorFactory; +import tech.illuin.wombat.core.source.AssetMonitor; +import tech.illuin.wombat.core.source.WombatSource; + +/** + * The app's {@link AssetMonitor}: each call runs on the monitor of the current {@link CoreDeployment}. A subclass + * because the core monitor is a class; every public method is overridden, so the parent's own state is never used. + */ +public class RuntimeAssetMonitor extends AssetMonitor +{ + private final CoreRuntime runtime; + + public RuntimeAssetMonitor(CoreRuntime runtime) + { + super(null, null, null); + this.runtime = runtime; + } + + @Override + public void trigger() + { + this.runtime.current().monitor().trigger(); + } + + @Override + public void refresh(AssetIdentity identity) + { + this.runtime.current().monitor().refresh(identity); + } + + /** Sources are registered by {@link CoreDeploymentFactory}, from the deployed modules. */ + @Override + public AssetMonitor register(AssetType type, ProcessorFactory factory) + { + throw new UnsupportedOperationException("Sources come from the deployed modules"); + } + + /** Only closes the parent's unused executor: the deployments are closed by {@link CoreRuntime}. */ + @Override + public void close() + { + super.close(); + } +} diff --git a/src/main/java/tech/illuin/wombat/engine/monitor/MonitorConfig.java b/src/main/java/tech/illuin/wombat/engine/monitor/MonitorConfig.java index fc65712..a835874 100644 --- a/src/main/java/tech/illuin/wombat/engine/monitor/MonitorConfig.java +++ b/src/main/java/tech/illuin/wombat/engine/monitor/MonitorConfig.java @@ -7,8 +7,9 @@ import jakarta.validation.ConstraintViolationException; import jakarta.validation.Validator; import tech.illuin.wombat.commons.WombatProperties; -import tech.illuin.wombat.core.WombatCore; import tech.illuin.wombat.core.source.AssetMonitor; +import tech.illuin.wombat.engine.module.runtime.CoreRuntime; +import tech.illuin.wombat.engine.module.runtime.RuntimeAssetMonitor; import java.io.IOException; import java.io.InputStream; @@ -36,10 +37,13 @@ public MonitoredEnvironments provideMonitoredEnvironments(WombatProperties womba } } + /** + * Delegates to the current deployment on every call, so that its holders follow redeploys. + */ @Singleton - public AssetMonitor provideMonitor(WombatCore core) + public AssetMonitor provideMonitor(CoreRuntime runtime) { - return core.createMonitor(); + return new RuntimeAssetMonitor(runtime); } @Singleton diff --git a/src/test/java/tech/illuin/wombat/engine/module/WombatCoreConfigTest.java b/src/test/java/tech/illuin/wombat/engine/module/WombatCoreConfigTest.java index bdc2d8d..956bcb0 100644 --- a/src/test/java/tech/illuin/wombat/engine/module/WombatCoreConfigTest.java +++ b/src/test/java/tech/illuin/wombat/engine/module/WombatCoreConfigTest.java @@ -6,7 +6,6 @@ import jakarta.inject.Inject; import jakarta.transaction.Transactional; import org.junit.jupiter.api.Test; -import tech.illuin.wombat.core.WombatCore; import tech.illuin.wombat.core.asset.type.ServiceFamily; import tech.illuin.wombat.core.evaluation.AssetEvaluator; import tech.illuin.wombat.core.module.WombatModule; @@ -14,6 +13,9 @@ import tech.illuin.wombat.core.source.AssetMonitor; import tech.illuin.wombat.core.source.persistence.WombatMetricPersister; import tech.illuin.wombat.engine.evaluation.CachedAssetEvaluator; +import tech.illuin.wombat.engine.module.runtime.CoreRuntime; +import tech.illuin.wombat.engine.module.runtime.RuntimeAssetEvaluator; +import tech.illuin.wombat.engine.module.runtime.RuntimeAssetMonitor; import tech.illuin.wombat.module.kubernetes_api.KubernetesAPIModule; import tech.illuin.wombat.engine.module.library.kubernetes_simulated.KubernetesSimulatedModule; import tech.illuin.wombat.module.kubernetes_static.KubernetesStaticModule; @@ -44,7 +46,7 @@ @QuarkusTest class WombatCoreConfigTest { - @Inject WombatCore core; + @Inject CoreRuntime runtime; @Inject WombatMetricPersister persister; @Inject SecretResolver secretResolver; @Inject Instance calculator; @@ -55,7 +57,7 @@ class WombatCoreConfigTest @Test void theContextFreeEntryPointsAreWired() { - assertNotNull(this.core); + assertNotNull(this.runtime); assertNotNull(this.persister); assertNotNull(this.secretResolver); } @@ -88,13 +90,20 @@ void everyModuleIsWiredWhenNoToggleIsSet() assertTrue(this.modules.stream().anyMatch(LLMVertexAIModule.class::isInstance)); } + @Test + void theRuntimeIsDeployedWithEveryEnabledModuleAtStartup() + { + assertEquals(this.modules.size(), this.runtime.current().modules().size()); + assertTrue(this.runtime.current().missing().isEmpty()); + } + @Test @Transactional void theCalculatorResolvesAgainstThePersistedContext() { AssetEvaluator evaluator = this.calculator.get(); - assertNotNull(evaluator); - assertInstanceOf(CachedAssetEvaluator.class, evaluator); + assertInstanceOf(RuntimeAssetEvaluator.class, evaluator); + assertInstanceOf(CachedAssetEvaluator.class, this.runtime.current().evaluator()); } @Test @@ -104,6 +113,6 @@ void theMonitorResolvesAgainstThePersistedContext() // Deliberately not triggered: sampling dials the real clusters in the monitored-environments file, which // would make this a live integration test. That the monitor gets a source per sourceable asset — rather // than being a bare instance that samples nothing — is covered by WombatCoreTest in core. - assertNotNull(this.monitor.get()); + assertInstanceOf(RuntimeAssetMonitor.class, this.monitor.get()); } } diff --git a/src/test/java/tech/illuin/wombat/engine/module/WombatModuleConfigTest.java b/src/test/java/tech/illuin/wombat/engine/module/WombatModuleConfigTest.java index a52a34d..e6ebfb0 100644 --- a/src/test/java/tech/illuin/wombat/engine/module/WombatModuleConfigTest.java +++ b/src/test/java/tech/illuin/wombat/engine/module/WombatModuleConfigTest.java @@ -176,6 +176,18 @@ void configureQuarkusObjectMapper_ignoresUnknownProperties() throws Exception private record SampleRecord(String firstName, String homeAddress) {} + @Test + void closeWombatModules_closesEveryDetectedModule() + { + WombatModule deployed = mock(WombatModule.class); + WombatModule disabled = mock(WombatModule.class); + + closeWombatModules(List.of(deployed, disabled)); + + verify(deployed).close(); + verify(disabled).close(); + } + @Test void provideDefaults_createsHandlersForFamilies() { diff --git a/src/test/java/tech/illuin/wombat/engine/module/runtime/CoreDeploymentFactoryTest.java b/src/test/java/tech/illuin/wombat/engine/module/runtime/CoreDeploymentFactoryTest.java new file mode 100644 index 0000000..663c72f --- /dev/null +++ b/src/test/java/tech/illuin/wombat/engine/module/runtime/CoreDeploymentFactoryTest.java @@ -0,0 +1,99 @@ +package tech.illuin.wombat.engine.module.runtime; + +import org.junit.jupiter.api.Test; +import tech.illuin.wombat.core.context.ResolvedContext; +import tech.illuin.wombat.core.module.WombatModule; +import tech.illuin.wombat.core.secret.SecretResolver; +import tech.illuin.wombat.engine.evaluation.CachedAssetEvaluator; +import tech.illuin.wombat.engine.evaluation.CachedAssetEvaluatorProperties; +import tech.illuin.wombat.engine.module.library.kubernetes_simulated.KubernetesSimulatedModule; +import tech.illuin.wombat.engine.module.library.llm_simulated.LLMSimulatedModule; + +import java.time.Duration; +import java.util.List; +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertNotSame; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class CoreDeploymentFactoryTest +{ + private static final String K8S = KubernetesSimulatedModule.TYPE.name(); + private static final String LLM = LLMSimulatedModule.TYPE.name(); + + @Test + void onlyTheRequestedModulesAreDeployed() + { + CoreDeployment deployment = factory(true).create(Set.of(LLM)); + + assertEquals(Set.of(LLMSimulatedModule.TYPE), deployment.types()); + assertTrue(deployment.missing().isEmpty()); + deployment.close(); + } + + @Test + void aRequestedTypeWithNoDetectedModuleIsReportedMissing() + { + CoreDeployment deployment = factory(true).create(Set.of(K8S, "acme.unknown")); + + assertEquals(Set.of(KubernetesSimulatedModule.TYPE), deployment.types()); + assertEquals(Set.of("acme.unknown"), deployment.missing()); + deployment.close(); + } + + @Test + void theCachedEvaluatorWrapsTheUncachedOneWhenTheCacheIsEnabled() + { + CoreDeployment deployment = factory(true).create(Set.of(K8S, LLM)); + + assertInstanceOf(CachedAssetEvaluator.class, deployment.evaluator()); + assertNotSame(deployment.evaluator(), deployment.uncachedEvaluator()); + deployment.close(); + } + + @Test + void bothEvaluatorsAreTheSameWhenTheCacheIsDisabled() + { + CoreDeployment deployment = factory(false).create(Set.of(K8S, LLM)); + + assertSame(deployment.evaluator(), deployment.uncachedEvaluator()); + deployment.close(); + } + + @Test + void eachDeploymentGetsItsOwnMonitorAndEvaluatorButSharesTheModules() + { + CoreDeploymentFactory factory = factory(true); + CoreDeployment first = factory.create(Set.of(K8S)); + CoreDeployment second = factory.create(Set.of(K8S)); + + assertNotSame(first.monitor(), second.monitor()); + assertNotSame(first.evaluator(), second.evaluator()); + assertSame(first.modules().getFirst(), second.modules().getFirst()); + first.close(); + second.close(); + } + + private static CoreDeploymentFactory factory(boolean cache) + { + List modules = List.of(new KubernetesSimulatedModule(), new LLMSimulatedModule()); + return new CoreDeploymentFactory( + () -> new ResolvedContext(List.of()), + SecretResolver.standard(), + (identity, type, metrics) -> {}, + modules, + List.of(), + new CacheProperties(cache, 1000L, Duration.ofMinutes(10), List.of()) + ); + } + + private record CacheProperties( + boolean enabled, + long maximumSize, + Duration expireAfterWrite, + List activityResolverCaches + ) implements CachedAssetEvaluatorProperties {} +} diff --git a/src/test/java/tech/illuin/wombat/engine/module/runtime/CoreRuntimeTest.java b/src/test/java/tech/illuin/wombat/engine/module/runtime/CoreRuntimeTest.java new file mode 100644 index 0000000..15bc419 --- /dev/null +++ b/src/test/java/tech/illuin/wombat/engine/module/runtime/CoreRuntimeTest.java @@ -0,0 +1,127 @@ +package tech.illuin.wombat.engine.module.runtime; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import tech.illuin.wombat.core.evaluation.AssetEvaluator; +import tech.illuin.wombat.core.module.WombatModule; +import tech.illuin.wombat.core.source.AssetMonitor; +import tech.illuin.wombat.engine.module.library.kubernetes_simulated.KubernetesSimulatedModule; +import tech.illuin.wombat.engine.module.library.llm_simulated.LLMSimulatedModule; +import tech.illuin.wombat.engine.module.registry.InMemoryModuleRegistry; + +import java.time.Instant; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +class CoreRuntimeTest +{ + private CoreDeploymentFactory factory; + private InMemoryModuleRegistry registry; + private List pendingCloses; + private CoreRuntime runtime; + + @BeforeEach + void setUp() + { + this.factory = mock(CoreDeploymentFactory.class); + this.registry = new InMemoryModuleRegistry(); + this.pendingCloses = new ArrayList<>(); + /* Holds the delayed closes, so that tests decide when the grace delay is over */ + this.runtime = new CoreRuntime(this.factory, this.registry, this.pendingCloses::add); + } + + @Test + void currentBeforeTheFirstDeploymentThrows() + { + assertThrows(IllegalStateException.class, this.runtime::current); + } + + @Test + void startupDeploysTheEnabledModulesOfTheRegistry() + { + this.registry.registerModule(new KubernetesSimulatedModule(), true); + this.registry.registerModule(new LLMSimulatedModule(), false); + when(this.factory.create(any())).thenReturn(deployment()); + + this.runtime.onStart(null); + + verify(this.factory).create(Set.of(KubernetesSimulatedModule.TYPE.name())); + } + + @Test + void aDeploySwapsInTheNewDeploymentAtOnce() + { + CoreDeployment second = deployment(); + when(this.factory.create(any())).thenReturn(deployment(), second); + + this.runtime.deploy(Set.of()); + this.runtime.deploy(Set.of()); + + assertSame(second, this.runtime.current()); + } + + @Test + void thePreviousDeploymentIsClosedAfterTheGraceDelay() + { + CoreDeployment first = deployment(); + CoreDeployment second = deployment(); + when(this.factory.create(any())).thenReturn(first, second); + this.runtime.deploy(Set.of()); + this.runtime.deploy(Set.of()); + + verify(first.monitor(), never()).close(); + verify(first.evaluator(), never()).close(); + + this.pendingCloses.forEach(Runnable::run); + verify(first.monitor()).close(); + verify(first.evaluator()).close(); + verify(second.monitor(), never()).close(); + verify(second.evaluator(), never()).close(); + } + + @Test + void aFailedBuildKeepsTheRunningDeployment() + { + CoreDeployment first = deployment(); + when(this.factory.create(any())).thenReturn(first).thenThrow(new IllegalStateException("broken module")); + + this.runtime.deploy(Set.of()); + assertThrows(IllegalStateException.class, () -> this.runtime.deploy(Set.of())); + + assertSame(first, this.runtime.current()); + assertTrue(this.pendingCloses.isEmpty()); + } + + @Test + void closingClosesTheCurrentDeploymentButNotItsModules() + { + WombatModule module = mock(WombatModule.class); + when(module.type()).thenReturn(KubernetesSimulatedModule.TYPE); + CoreDeployment deployment = deployment(module); + when(this.factory.create(any())).thenReturn(deployment); + this.runtime.deploy(Set.of()); + + this.runtime.close(); + + verify(deployment.monitor()).close(); + verify(deployment.evaluator()).close(); + verify(module, never()).close(); + } + + /* Monitor and evaluator are mocks, so that closing can be verified without a context */ + private static CoreDeployment deployment(WombatModule... modules) + { + AssetEvaluator evaluator = mock(AssetEvaluator.class); + return new CoreDeployment(List.of(modules), Set.of(), mock(AssetMonitor.class), evaluator, evaluator, Instant.now()); + } +} diff --git a/src/test/java/tech/illuin/wombat/engine/module/runtime/RuntimeProxiesTest.java b/src/test/java/tech/illuin/wombat/engine/module/runtime/RuntimeProxiesTest.java new file mode 100644 index 0000000..f72d385 --- /dev/null +++ b/src/test/java/tech/illuin/wombat/engine/module/runtime/RuntimeProxiesTest.java @@ -0,0 +1,129 @@ +package tech.illuin.wombat.engine.module.runtime; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import tech.illuin.wombat.core.activity.commons.AssetFilter; +import tech.illuin.wombat.core.activity.commons.TimeRange; +import tech.illuin.wombat.core.asset.AssetIdentity; +import tech.illuin.wombat.core.evaluation.AssetEvaluation; +import tech.illuin.wombat.core.evaluation.AssetEvaluator; +import tech.illuin.wombat.core.evaluation.WombatEvaluationException; +import tech.illuin.wombat.core.source.AssetMonitor; +import tech.illuin.wombat.engine.module.registry.InMemoryModuleRegistry; + +import java.lang.reflect.Method; +import java.lang.reflect.Modifier; +import java.time.Instant; +import java.util.Arrays; +import java.util.List; +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +class RuntimeProxiesTest +{ + private CoreDeploymentFactory factory; + private CoreRuntime runtime; + + @BeforeEach + void setUp() + { + this.factory = mock(CoreDeploymentFactory.class); + this.runtime = new CoreRuntime(this.factory, new InMemoryModuleRegistry(), Runnable::run); + } + + /* A public method left to the parent would silently run on its empty pools: a core bump adding one must fail here */ + @Test + void theEvaluatorOverridesEveryPublicMethodOfTheCoreEvaluator() + { + assertOverridesEveryPublicMethod(AssetEvaluator.class, RuntimeAssetEvaluator.class); + } + + @Test + void theMonitorOverridesEveryPublicMethodOfTheCoreMonitor() + { + assertOverridesEveryPublicMethod(AssetMonitor.class, RuntimeAssetMonitor.class); + } + + @Test + void theEvaluatorFollowsTheCurrentDeployment() throws WombatEvaluationException + { + CoreDeployment first = deployment(); + CoreDeployment second = deployment(); + when(this.factory.create(any())).thenReturn(first, second); + TimeRange range = mock(TimeRange.class); + AssetFilter filter = mock(AssetFilter.class); + List evaluations = List.of(mock(AssetEvaluation.class)); + when(second.evaluator().evaluate(range, filter)).thenReturn(evaluations); + + RuntimeAssetEvaluator evaluator = new RuntimeAssetEvaluator(this.runtime); + this.runtime.deploy(Set.of()); + evaluator.evaluate(range, filter); + verify(first.evaluator()).evaluate(range, filter); + + this.runtime.deploy(Set.of()); + assertSame(evaluations, evaluator.evaluate(range, filter)); + } + + @Test + void theMonitorFollowsTheCurrentDeployment() + { + CoreDeployment first = deployment(); + CoreDeployment second = deployment(); + when(this.factory.create(any())).thenReturn(first, second); + AssetIdentity identity = new AssetIdentity("asset", "env", "acme.type"); + + RuntimeAssetMonitor monitor = new RuntimeAssetMonitor(this.runtime); + this.runtime.deploy(Set.of()); + monitor.trigger(); + verify(first.monitor()).trigger(); + + this.runtime.deploy(Set.of()); + monitor.trigger(); + monitor.refresh(identity); + verify(second.monitor()).trigger(); + verify(second.monitor()).refresh(identity); + } + + @Test + void registeringOnTheProxiesIsRefused() + { + assertThrows(UnsupportedOperationException.class, () -> new RuntimeAssetEvaluator(this.runtime).registerActivityResolver(null, null)); + assertThrows(UnsupportedOperationException.class, () -> new RuntimeAssetMonitor(this.runtime).register(null, null)); + } + + private static void assertOverridesEveryPublicMethod(Class core, Class proxy) + { + List missing = Arrays.stream(core.getMethods()) + .filter(method -> !Modifier.isStatic(method.getModifiers())) + .filter(method -> method.getDeclaringClass() != Object.class) + .filter(method -> !isDeclaredBy(proxy, method)) + .map(Method::toGenericString) + .toList(); + + assertEquals(List.of(), missing, proxy.getSimpleName() + " must override every public method of " + core.getSimpleName()); + } + + private static boolean isDeclaredBy(Class type, Method method) + { + try { + type.getDeclaredMethod(method.getName(), method.getParameterTypes()); + return true; + } + catch (NoSuchMethodException e) { + return false; + } + } + + private static CoreDeployment deployment() + { + AssetEvaluator evaluator = mock(AssetEvaluator.class); + return new CoreDeployment(List.of(), Set.of(), mock(AssetMonitor.class), evaluator, evaluator, Instant.now()); + } +} From 350f91cc56b87b844fbf961f515df927529911ad Mon Sep 17 00:00:00 2001 From: Pierre Lecerf Date: Fri, 20 Nov 2026 20:47:33 +0100 Subject: [PATCH 2/6] feat(engine): database-backed module registry --- doc/access-control.md | 4 + .../wombat/endpoint/MetricController.java | 2 +- .../wombat/endpoint/ModuleController.java | 91 ++++++++- .../engine/context/asset/AssetValidator.java | 1 + .../asset/persistence/AssetRepository.java | 13 ++ .../wombat/engine/module/ModuleInfo.java | 30 +++ .../wombat/engine/module/ModuleService.java | 96 +++++++++ .../engine/module/WombatModuleConfig.java | 35 +--- .../extension/WombatModuleProperties.java | 39 +--- .../module/persistence/ModuleEntity.java | 89 +++++++++ .../module/persistence/ModuleRepository.java | 52 +++++ .../module/registry/DbModuleRegistry.java | 177 +++++++++++++++++ .../registry/InMemoryModuleRegistry.java | 32 ++- .../registry/ModuleConflictException.java | 12 ++ .../module/registry/ModuleMetadata.java | 39 +++- .../module/registry/ModuleRegistry.java | 21 ++ .../registry/UnknownModuleException.java | 12 ++ .../schema/DefaultSchemaRegistry.java | 2 + .../illuin/wombat/security/Permission.java | 6 + .../tech/illuin/wombat/security/Role.java | 1 + .../illuin/wombat/security/UiPagePolicy.java | 2 + src/main/resources/application.yaml | 7 +- .../postgresql/V16__module_settings.sql | 34 ++++ .../migration/sqlite/V25__module_settings.sql | 34 ++++ src/main/webui/src/domain/access.ts | 3 + src/main/webui/src/services/api/modules.ts | 6 +- .../endpoint/DisabledModuleProfile.java | 18 -- .../MetricControllerDisabledModuleTest.java | 20 +- .../wombat/endpoint/ModuleControllerTest.java | 163 ++++++++++++++- .../WorkerControllerDisabledModuleTest.java | 10 +- .../asset/persistence/AssetConverterTest.java | 3 +- .../engine/module/ModuleServiceTest.java | 146 ++++++++++++++ .../engine/module/WombatModuleConfigTest.java | 70 +------ .../DynamicModuleIntegrationTest.java | 11 +- .../persistence/ModuleRepositoryTest.java | 74 +++++++ .../module/registry/DbModuleRegistryTest.java | 185 ++++++++++++++++++ .../registry/InMemoryModuleRegistryTest.java | 29 ++- .../MonitoredEnvironmentsTemplateTest.java | 9 +- .../backend/postgres/PostgresSchemaTest.java | 2 +- .../tech/illuin/wombat/security/RoleTest.java | 6 +- .../wombat/security/UiPagePolicyTest.java | 2 + .../wombat/ui/AuthenticationEnabledTest.java | 2 +- src/test/resources/application.yaml | 6 - 43 files changed, 1393 insertions(+), 203 deletions(-) create mode 100644 src/main/java/tech/illuin/wombat/engine/module/ModuleInfo.java create mode 100644 src/main/java/tech/illuin/wombat/engine/module/ModuleService.java create mode 100644 src/main/java/tech/illuin/wombat/engine/module/persistence/ModuleEntity.java create mode 100644 src/main/java/tech/illuin/wombat/engine/module/persistence/ModuleRepository.java create mode 100644 src/main/java/tech/illuin/wombat/engine/module/registry/DbModuleRegistry.java create mode 100644 src/main/java/tech/illuin/wombat/engine/module/registry/ModuleConflictException.java create mode 100644 src/main/java/tech/illuin/wombat/engine/module/registry/UnknownModuleException.java create mode 100644 src/main/resources/db/migration/postgresql/V16__module_settings.sql create mode 100644 src/main/resources/db/migration/sqlite/V25__module_settings.sql delete mode 100644 src/test/java/tech/illuin/wombat/endpoint/DisabledModuleProfile.java create mode 100644 src/test/java/tech/illuin/wombat/engine/module/ModuleServiceTest.java create mode 100644 src/test/java/tech/illuin/wombat/engine/module/persistence/ModuleRepositoryTest.java create mode 100644 src/test/java/tech/illuin/wombat/engine/module/registry/DbModuleRegistryTest.java diff --git a/doc/access-control.md b/doc/access-control.md index 9fd9b3e..5c6a245 100644 --- a/doc/access-control.md +++ b/doc/access-control.md @@ -55,6 +55,8 @@ Endpoints check permissions, and each role grants a fixed set of them: | `ui.users.read` | ✓ | | | | `ui.environments.read` | ✓ | ✓ | ✓ | | `ui.environments.manage` | ✓ | | | +| `ui.modules.read` | ✓ | ✓ | ✓ | +| `ui.modules.manage` | ✓ | | | | `api.impact.read` | ✓ | ✓ | ✓ | | `api.simulation.read` | ✓ | ✓ | ✓ | | `api.environment.read` | ✓ | ✓ | ✓ | @@ -62,6 +64,8 @@ Endpoints check permissions, and each role grants a fixed set of them: | `api.asset.read` | ✓ | ✓ | ✓ | | `api.asset.write` | ✓ | | | | `api.metric.write` | ✓ | | | +| `api.module.read` | ✓ | ✓ | ✓ | +| `api.module.manage` | ✓ | | | | `api.apikey.manage` | ✓ | ✓ | ✓ | | `api.apikey.admin` | ✓ | | ✓ | | `api.user.manage` | ✓ | | | diff --git a/src/main/java/tech/illuin/wombat/endpoint/MetricController.java b/src/main/java/tech/illuin/wombat/endpoint/MetricController.java index 4c3767c..86973d9 100644 --- a/src/main/java/tech/illuin/wombat/endpoint/MetricController.java +++ b/src/main/java/tech/illuin/wombat/endpoint/MetricController.java @@ -116,7 +116,7 @@ public Response push( private boolean hasActiveModule(AssetIdentity asset) { return this.modules.getModule(asset.assetType()) - .filter(ModuleMetadata::enabled) + .filter(ModuleMetadata::active) .isPresent(); } diff --git a/src/main/java/tech/illuin/wombat/endpoint/ModuleController.java b/src/main/java/tech/illuin/wombat/endpoint/ModuleController.java index 5badea9..8009412 100644 --- a/src/main/java/tech/illuin/wombat/endpoint/ModuleController.java +++ b/src/main/java/tech/illuin/wombat/endpoint/ModuleController.java @@ -1,7 +1,11 @@ package tech.illuin.wombat.endpoint; +import com.fasterxml.jackson.annotation.JsonProperty; import io.quarkus.resteasy.reactive.server.EndpointDisabled; import io.quarkus.security.PermissionsAllowed; +import io.quarkus.security.identity.SecurityIdentity; +import jakarta.validation.Valid; +import jakarta.validation.constraints.NotNull; import jakarta.ws.rs.*; import jakarta.ws.rs.core.MediaType; import org.eclipse.microprofile.openapi.annotations.enums.SchemaType; @@ -11,8 +15,11 @@ import org.eclipse.microprofile.openapi.annotations.responses.APIResponse; import tech.illuin.wombat.endpoint.response.Response; import tech.illuin.wombat.endpoint.response.Status; -import tech.illuin.wombat.engine.module.registry.ModuleMetadata; +import tech.illuin.wombat.engine.module.ModuleInfo; +import tech.illuin.wombat.engine.module.ModuleService; +import tech.illuin.wombat.engine.module.registry.ModuleConflictException; import tech.illuin.wombat.engine.module.registry.ModuleRegistry; +import tech.illuin.wombat.engine.module.registry.UnknownModuleException; import tech.illuin.wombat.engine.module.registry.schema.AssetSchemaRegistry; import tech.illuin.wombat.feature.Feature; import tech.illuin.wombat.security.Permission; @@ -20,18 +27,24 @@ import java.util.List; +import static jakarta.ws.rs.core.Response.Status.CONFLICT; + @Path("api/modules") @EndpointDisabled(name = Feature.Keys.API_READ, stringValue = "false") @PermissionsAllowed(Permission.Names.API_MODULE_READ) public class ModuleController { + private final ModuleService service; private final ModuleRegistry registry; private final AssetSchemaRegistry schemaRegistry; + private final SecurityIdentity identity; - public ModuleController(ModuleRegistry registry, AssetSchemaRegistry schemaRegistry) + public ModuleController(ModuleService service, ModuleRegistry registry, AssetSchemaRegistry schemaRegistry, SecurityIdentity identity) { + this.service = service; this.registry = registry; this.schemaRegistry = schemaRegistry; + this.identity = identity; } @GET @@ -43,14 +56,14 @@ public ModuleController(ModuleRegistry registry, AssetSchemaRegistry schemaRegis schema = @Schema( properties = { @SchemaProperty(name = "status", implementation = Status.class), - @SchemaProperty(name = "payload", type = SchemaType.ARRAY, implementation = ModuleMetadata.class) + @SchemaProperty(name = "payload", type = SchemaType.ARRAY, implementation = ModuleInfo.class) } ) ) ) - public Response> list() + public Response> list() { - return Response.success(this.registry.listModules()); + return Response.success(this.service.list()); } @GET @@ -63,16 +76,20 @@ public Response> list() schema = @Schema( properties = { @SchemaProperty(name = "status", implementation = Status.class), - @SchemaProperty(name = "payload", type = SchemaType.OBJECT, implementation = ModuleMetadata.class) + @SchemaProperty(name = "payload", type = SchemaType.OBJECT, implementation = ModuleInfo.class) } ) ) ) - public Response find(@PathParam("id") String id) + @APIResponse(responseCode = "404", description = "Unknown module") + public Response find(@PathParam("id") String id) { - return this.registry.getModule(id) - .map(Response::success) - .orElseThrow(() -> new NotFoundException("No module could be found for id " + id)); + try { + return Response.success(this.service.find(id)); + } + catch (UnknownModuleException e) { + throw new NotFoundException(e.getMessage()); + } } @GET @@ -97,4 +114,58 @@ public Response getSchema(@PathParam("id") String id) .map(Response::success) .orElseThrow(() -> new NotFoundException("No module could be found for id " + id)); } + + @PUT + @Path("/{id}") + @PermissionsAllowed(Permission.Names.API_MODULE_MANAGE) + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + @APIResponse(responseCode = "200", content = @Content(mediaType = MediaType.APPLICATION_JSON, schema = @Schema(properties = { + @SchemaProperty(name = "status", implementation = Status.class), + @SchemaProperty(name = "payload", implementation = ModuleInfo.class) + }))) + @APIResponse(responseCode = "400", description = "Invalid payload") + @APIResponse(responseCode = "404", description = "Unknown module") + @APIResponse(responseCode = "409", description = "The module is missing") + public Response update(@PathParam("id") String id, @NotNull @Valid ModuleUpdateRequest request) + { + try { + return Response.success(this.service.setEnabled(id, request.enabled(), this.user())); + } + catch (UnknownModuleException e) { + throw new NotFoundException(e.getMessage()); + } + catch (ModuleConflictException e) { + throw new ClientErrorException(e.getMessage(), CONFLICT); + } + } + + @DELETE + @Path("/{id}") + @PermissionsAllowed(Permission.Names.API_MODULE_MANAGE) + @APIResponse(responseCode = "204", description = "Deleted") + @APIResponse(responseCode = "404", description = "Unknown module") + @APIResponse(responseCode = "409", description = "The module is detected, only a missing one can be deleted") + public void delete(@PathParam("id") String id) + { + try { + this.service.remove(id); + } + catch (UnknownModuleException e) { + throw new NotFoundException(e.getMessage()); + } + catch (ModuleConflictException e) { + throw new ClientErrorException(e.getMessage(), CONFLICT); + } + } + + /* Null when authentication is disabled, every caller is then anonymous */ + private String user() + { + return this.identity.isAnonymous() ? null : this.identity.getPrincipal().getName(); + } + + public record ModuleUpdateRequest( + @NotNull @JsonProperty("enabled") @Schema(required = true) Boolean enabled + ) {} } diff --git a/src/main/java/tech/illuin/wombat/engine/context/asset/AssetValidator.java b/src/main/java/tech/illuin/wombat/engine/context/asset/AssetValidator.java index 87b8535..63d4443 100644 --- a/src/main/java/tech/illuin/wombat/engine/context/asset/AssetValidator.java +++ b/src/main/java/tech/illuin/wombat/engine/context/asset/AssetValidator.java @@ -31,6 +31,7 @@ public void validate(Asset asset) throws InvalidAssetException throw new InvalidAssetException("Unknown asset type " + unrecognized.rawType() + " for asset " + asset.identity().id()); ModuleMetadata module = this.modules.getModule(asset.type()) + .filter(candidate -> !candidate.missing()) .orElseThrow(() -> new InvalidAssetException("No module for asset type " + asset.type().name() + " of asset " + asset.identity().id())); if (!module.enabled()) logger.debug("Asset {} is declared with asset type {} whose module is disabled, its metrics will be skipped", asset.identity().id(), asset.type().name()); diff --git a/src/main/java/tech/illuin/wombat/engine/context/asset/persistence/AssetRepository.java b/src/main/java/tech/illuin/wombat/engine/context/asset/persistence/AssetRepository.java index 444f14e..e81eb5e 100644 --- a/src/main/java/tech/illuin/wombat/engine/context/asset/persistence/AssetRepository.java +++ b/src/main/java/tech/illuin/wombat/engine/context/asset/persistence/AssetRepository.java @@ -4,7 +4,9 @@ import tech.illuin.wombat.persistence.backend.api.repository.Repository; import java.util.List; +import java.util.Map; import java.util.Optional; +import java.util.stream.Collectors; /** * Visible assets are those not soft-deleted, as the API and the reconciler see them; active ones are also not @@ -42,6 +44,17 @@ public long countVisibleByEnvironment(String environmentId) .getSingleResult(); } + /** + * @return the number of visible assets per asset-type name; types without any are absent + */ + public Map countVisibleByType() + { + return getEntityManager() + .createQuery("SELECT a.type, COUNT(a) FROM AssetEntity a WHERE a.deletedAt IS NULL GROUP BY a.type", Object[].class) + .getResultStream() + .collect(Collectors.toMap(row -> (String) row[0], row -> (Long) row[1])); + } + public List findVisible() { return getEntityManager() diff --git a/src/main/java/tech/illuin/wombat/engine/module/ModuleInfo.java b/src/main/java/tech/illuin/wombat/engine/module/ModuleInfo.java new file mode 100644 index 0000000..e8cee44 --- /dev/null +++ b/src/main/java/tech/illuin/wombat/engine/module/ModuleInfo.java @@ -0,0 +1,30 @@ +package tech.illuin.wombat.engine.module; + +import tech.illuin.wombat.core.asset.Asset; +import tech.illuin.wombat.core.asset.type.AssetType; +import tech.illuin.wombat.engine.module.registry.ModuleMetadata.Source; + +/** + * A module as the API serves it: its registry state ({@link tech.illuin.wombat.engine.module.registry.ModuleMetadata}), + * and whether the running core holds it. + * + * @param type as the module advertised it when last detected, for a missing one + * @param assetClass null when the module is missing + * @param enabled the registry state, which the asset pickers follow at once + * @param deployed whether the core running on the serving instance holds the module + * @param pending whether the registry state differs from the deployed one, until a redeploy + * @param missing whether the module has a registry row but is no longer detected, e.g. its extension jar was removed + * @param source null when the module is missing + * @param assetCount the number of assets of this type, soft-deleted ones aside + */ +public record ModuleInfo( + AssetType type, + Class assetClass, + boolean enabled, + boolean creatable, + boolean deployed, + boolean pending, + boolean missing, + Source source, + long assetCount +) {} diff --git a/src/main/java/tech/illuin/wombat/engine/module/ModuleService.java b/src/main/java/tech/illuin/wombat/engine/module/ModuleService.java new file mode 100644 index 0000000..672bb0d --- /dev/null +++ b/src/main/java/tech/illuin/wombat/engine/module/ModuleService.java @@ -0,0 +1,96 @@ +package tech.illuin.wombat.engine.module; + +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.transaction.Transactional; +import tech.illuin.wombat.core.asset.type.AssetType; +import tech.illuin.wombat.engine.context.asset.persistence.AssetRepository; +import tech.illuin.wombat.engine.module.registry.ModuleMetadata; +import tech.illuin.wombat.engine.module.registry.ModuleRegistry; +import tech.illuin.wombat.engine.module.registry.UnknownModuleException; +import tech.illuin.wombat.engine.module.runtime.CoreRuntime; + +import java.util.Comparator; +import java.util.List; +import java.util.Map; +import java.util.Set; + +/** + * The modules administration view, for {@code ModuleController} only: the rest of the app reads the modules from the + * {@link ModuleRegistry}. Adds to the registry's state what only the running core knows, and the asset counts. + */ +@ApplicationScoped +public class ModuleService +{ + private final ModuleRegistry registry; + private final AssetRepository assets; + private final CoreRuntime runtime; + + /* Detected modules first, then missing ones, each by name */ + private static final Comparator ORDER = Comparator.comparing(ModuleInfo::missing) + .thenComparing(module -> module.type().name()); + + public ModuleService(ModuleRegistry registry, AssetRepository assets, CoreRuntime runtime) + { + this.registry = registry; + this.assets = assets; + this.runtime = runtime; + } + + @Transactional + public List list() + { + Map counts = this.assets.countVisibleByType(); + Set deployed = this.runtime.current().types(); + return this.registry.listModules().stream() + .map(module -> info(module, deployed, counts)) + .sorted(ORDER) + .toList(); + } + + /** + * @throws UnknownModuleException if the module is not registered + */ + @Transactional + public ModuleInfo find(String name) + { + return this.registry.getModule(name) + .map(module -> info(module, this.runtime.current().types(), this.assets.countVisibleByType())) + .orElseThrow(() -> new UnknownModuleException(name)); + } + + /** + * See {@link ModuleRegistry#setEnabled}: the asset pickers follow at once, the core only on redeploy. + */ + @Transactional + public ModuleInfo setEnabled(String name, boolean enabled, String user) + { + this.registry.setEnabled(name, enabled, user); + return this.find(name); + } + + /** + * See {@link ModuleRegistry#remove}. + */ + @Transactional + public void remove(String name) + { + this.registry.remove(name); + } + + private static ModuleInfo info(ModuleMetadata module, Set deployedTypes, Map counts) + { + boolean deployed = deployedTypes.contains(module.type()); + return new ModuleInfo( + module.type(), + module.assetClass(), + module.enabled(), + module.creatable(), + deployed, + /* A redeploy cannot bring a missing module in. Against the local core for now, against the latest deployment once redeploys are versioned */ + !module.missing() && module.enabled() != deployed, + module.missing(), + module.source(), + counts.getOrDefault(module.type().name(), 0L) + ); + } +} diff --git a/src/main/java/tech/illuin/wombat/engine/module/WombatModuleConfig.java b/src/main/java/tech/illuin/wombat/engine/module/WombatModuleConfig.java index 17d8cc1..92bfa08 100644 --- a/src/main/java/tech/illuin/wombat/engine/module/WombatModuleConfig.java +++ b/src/main/java/tech/illuin/wombat/engine/module/WombatModuleConfig.java @@ -37,8 +37,6 @@ import tech.illuin.wombat.engine.module.extension.CoreExtensionLoader; import tech.illuin.wombat.engine.module.extension.DynamicExtensionLoader; import tech.illuin.wombat.engine.module.extension.ExtensionLoader; -import tech.illuin.wombat.engine.module.extension.WombatModuleProperties; -import tech.illuin.wombat.engine.module.registry.InMemoryModuleRegistry; import tech.illuin.wombat.engine.module.registry.ModuleRegistry; import tech.illuin.wombat.engine.module.registry.schema.AssetSchemaRegistry; import tech.illuin.wombat.engine.module.registry.schema.DefaultSchemaRegistry; @@ -116,32 +114,13 @@ public static void closeWombatModules(@Disposes List modules) modules.forEach(WombatModule::close); } - @Produces @Singleton - public static ModuleRegistry provideModuleRegistry(List modules, WombatProperties wombat) - { - WombatModuleProperties properties = wombat.module(); - /* This will soon become DB-backed and handle all detected modules; for now we use the conf-based switches for core modules only */ - InMemoryModuleRegistry registry = new InMemoryModuleRegistry(); - modules.forEach(module -> { - boolean enabled = switch (module.type().name()) { - case "tech.illuin.wombat-module.kubernetes-api" -> properties.kubernetesApi().enabled(); - case "tech.illuin.wombat-module.llm-prometheus" -> properties.llmPrometheus().enabled(); - case "tech.illuin.wombat-module.llm-anthropic" -> properties.llmAnthropic().enabled(); - case "tech.illuin.wombat-module.llm-vertexai" -> properties.llmVertexAi().enabled(); - default -> true; - }; - registry.registerModule(module, enabled); - }); - return registry; - } - @Produces @Singleton public static AssetSchemaRegistry provideAssetSchemaRegistry(ModuleRegistry moduleRegistry, ObjectMapper mapper) { return new DefaultSchemaRegistry(moduleRegistry, mapper); } - public void configureQuarkusObjectMapper(@Observes StartupEvent event, ObjectMapper mapper, ModuleRegistry modules) + public void configureQuarkusObjectMapper(@Observes StartupEvent event, ObjectMapper mapper, List modules) { registerSubTypes(modules, mapper); } @@ -149,7 +128,7 @@ public void configureQuarkusObjectMapper(@Observes StartupEvent event, ObjectMap @Produces @Singleton @Typed(YAMLMapper.class) - public YAMLMapper provideYAMLMapper(ModuleRegistry modules) + public YAMLMapper provideYAMLMapper(List modules) { YAMLMapper mapper = YAMLMapper.builder() .findAndAddModules() @@ -162,7 +141,7 @@ public YAMLMapper provideYAMLMapper(ModuleRegistry modules) @Produces @Singleton @Typed(JsonMapper.class) - public JsonMapper provideJsonMapper(ModuleRegistry modules) + public JsonMapper provideJsonMapper(List modules) { JsonMapper mapper = JsonMapper.builder().findAndAddModules().build(); registerSubTypes(modules, mapper); @@ -170,14 +149,16 @@ public JsonMapper provideJsonMapper(ModuleRegistry modules) } /** - * Disabled modules are registered too, so their assets are still read as such rather than as unrecognized ones. + * Every detected module is registered, disabled ones too, so their assets are still read as such rather than as + * unrecognized ones. Taken from the detected modules rather than the registry, which reads the database: the YAML + * mapper is needed at startup, before the migrations run. */ - private static void registerSubTypes(ModuleRegistry modules, O mapper) + private static void registerSubTypes(List modules, O mapper) { mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); mapper.disable(SerializationFeature.FAIL_ON_UNWRAPPED_TYPE_IDENTIFIERS); mapper.addHandler(new UnrecognizedAssetProblemHandler()); - modules.listModules().forEach(module -> mapper.registerSubtypes(new NamedType( + modules.forEach(module -> mapper.registerSubtypes(new NamedType( module.assetClass(), module.type().name() ))); } diff --git a/src/main/java/tech/illuin/wombat/engine/module/extension/WombatModuleProperties.java b/src/main/java/tech/illuin/wombat/engine/module/extension/WombatModuleProperties.java index 5ef822f..9fcd24b 100644 --- a/src/main/java/tech/illuin/wombat/engine/module/extension/WombatModuleProperties.java +++ b/src/main/java/tech/illuin/wombat/engine/module/extension/WombatModuleProperties.java @@ -5,47 +5,14 @@ import java.nio.file.Path; +/** + * Whether a module is enabled is no longer configured here: it is the registry's state, kept in the database. + */ public interface WombatModuleProperties { - @WithName("kubernetes-api") - KubernetesApiProperties kubernetesApi(); - - @WithName("llm-prometheus") - LlmPrometheusProperties llmPrometheus(); - - @WithName("llm-anthropic") - LlmAnthropicProperties llmAnthropic(); - - @WithName("llm-vertex-ai") - LlmVertexAIProperties llmVertexAi(); - @WithName("extension") ExtensionProperties extension(); - interface KubernetesApiProperties - { - @WithDefault("true") - boolean enabled(); - } - - interface LlmPrometheusProperties - { - @WithDefault("true") - boolean enabled(); - } - - interface LlmAnthropicProperties - { - @WithDefault("true") - boolean enabled(); - } - - interface LlmVertexAIProperties - { - @WithDefault("true") - boolean enabled(); - } - interface ExtensionProperties { @WithName("path") diff --git a/src/main/java/tech/illuin/wombat/engine/module/persistence/ModuleEntity.java b/src/main/java/tech/illuin/wombat/engine/module/persistence/ModuleEntity.java new file mode 100644 index 0000000..c139bd9 --- /dev/null +++ b/src/main/java/tech/illuin/wombat/engine/module/persistence/ModuleEntity.java @@ -0,0 +1,89 @@ +package tech.illuin.wombat.engine.module.persistence; + +import jakarta.persistence.Column; +import jakarta.persistence.Convert; +import jakarta.persistence.Entity; +import jakarta.persistence.EnumType; +import jakarta.persistence.Enumerated; +import jakarta.persistence.Id; +import jakarta.persistence.PrePersist; +import jakarta.persistence.PreUpdate; +import jakarta.persistence.Table; +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.engine.context.environment.persistence.EnvironmentEntity; +import tech.illuin.wombat.persistence.model.BooleanFlagConverter; + +import java.time.Instant; + +/** + * The registry state of a module: whether it is enabled, and the asset-type it advertised when last detected. A row is + * registered for every module an instance detects, and is kept once its module is no longer detected, so that the + * module shows as missing and keeps its state. + */ +@Entity +@Table(name = "modules") +public class ModuleEntity +{ + /** The asset-type name, e.g. {@code tech.illuin.wombat-module.kubernetes-api}. */ + @Id @Column(name = "type_name", nullable = false) + public String typeName; + + @Column(nullable = false) + public String namespace; + + @Column(name = "local_name", nullable = false) + public String localName; + + @Enumerated(EnumType.STRING) + @Column(nullable = false, columnDefinition = "TEXT") + public ActivityRegime regime; + + @Enumerated(EnumType.STRING) + @Column(nullable = false, columnDefinition = "TEXT") + public ServiceFamily family; + + @Convert(converter = BooleanFlagConverter.class) + @Column(nullable = false, columnDefinition = "INTEGER") + public boolean enabled; + + @Column(name = "updated_at", nullable = false, columnDefinition = "INTEGER") + public Instant updatedAt; + + /** Who last toggled the module; null for a registration, or when authentication is disabled. */ + @Column(name = "updated_by") + public String updatedBy; + + public static ModuleEntity registered(AssetType type) + { + ModuleEntity entity = new ModuleEntity(); + entity.typeName = type.name(); + entity.enabled = true; + entity.advertise(type); + return entity; + } + + public AssetType type() + { + return new AssetType(this.namespace, this.localName, this.regime, this.family); + } + + /** + * Records the asset-type the detected module advertises, which a later version of its jar may change. + */ + public void advertise(AssetType type) + { + this.namespace = type.namespace(); + this.localName = type.localName(); + this.regime = type.regime(); + this.family = type.family(); + } + + @PrePersist + @PreUpdate + void onWrite() + { + this.updatedAt = EnvironmentEntity.now(); + } +} diff --git a/src/main/java/tech/illuin/wombat/engine/module/persistence/ModuleRepository.java b/src/main/java/tech/illuin/wombat/engine/module/persistence/ModuleRepository.java new file mode 100644 index 0000000..138c950 --- /dev/null +++ b/src/main/java/tech/illuin/wombat/engine/module/persistence/ModuleRepository.java @@ -0,0 +1,52 @@ +package tech.illuin.wombat.engine.module.persistence; + +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.transaction.Transactional; +import tech.illuin.wombat.core.asset.type.AssetType; +import tech.illuin.wombat.persistence.backend.api.repository.Repository; + +import java.util.Collection; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import java.util.function.Function; +import java.util.stream.Collectors; + +@ApplicationScoped +public class ModuleRepository extends Repository +{ + public ModuleRepository() + { + super(ModuleEntity.class); + } + + /** + * Inserts an enabled row for each of {@code types} that has none. The existing rows keep their state, and take the + * asset-type as advertised now. + * + * @return the names of the types registered + * @throws jakarta.persistence.PersistenceException if another instance registered one of them in the meantime + */ + @Transactional + public Set register(Collection types) + { + Map existing = this.listAll().stream() + .collect(Collectors.toMap(entity -> entity.typeName, Function.identity())); + + Set registered = new HashSet<>(); + for (AssetType type : types) + { + ModuleEntity entity = existing.get(type.name()); + if (entity == null) + { + this.entityManager.persist(ModuleEntity.registered(type)); + registered.add(type.name()); + } + else if (!entity.type().equals(type)) + entity.advertise(type); + } + /* Flushed here, so that a concurrent registration surfaces as a PersistenceException rather than at commit */ + this.entityManager.flush(); + return registered; + } +} diff --git a/src/main/java/tech/illuin/wombat/engine/module/registry/DbModuleRegistry.java b/src/main/java/tech/illuin/wombat/engine/module/registry/DbModuleRegistry.java new file mode 100644 index 0000000..82f2a37 --- /dev/null +++ b/src/main/java/tech/illuin/wombat/engine/module/registry/DbModuleRegistry.java @@ -0,0 +1,177 @@ +package tech.illuin.wombat.engine.module.registry; + +import io.quarkus.runtime.StartupEvent; +import jakarta.annotation.Priority; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.event.Observes; +import jakarta.inject.Inject; +import jakarta.persistence.PersistenceException; +import jakarta.transaction.Transactional; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import tech.illuin.wombat.commons.WombatProperties; +import tech.illuin.wombat.core.asset.type.AssetType; +import tech.illuin.wombat.core.module.WombatModule; +import tech.illuin.wombat.engine.module.persistence.ModuleEntity; +import tech.illuin.wombat.engine.module.persistence.ModuleRepository; +import tech.illuin.wombat.engine.module.registry.ModuleMetadata.Source; + +import java.net.URISyntaxException; +import java.nio.file.FileSystemNotFoundException; +import java.nio.file.Path; +import java.security.CodeSource; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.function.Function; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +/** + * The modules registered in the {@code modules} table, shared by every instance: a toggle is seen by all of them at + * once. Every module this instance detects is registered at startup; a registered module no instance detects any more + * is missing, and keeps its state should it come back. + */ +@ApplicationScoped +public class DbModuleRegistry implements ModuleRegistry +{ + private final Map detected; + private final ModuleRepository repository; + private final Path extensionPath; + + /* After the persistence setup, and before the environments are reconciled and the core deployed */ + static final int STARTUP_PRIORITY_REGISTER = 1900; + private static final Logger logger = LoggerFactory.getLogger(DbModuleRegistry.class); + + @Inject + public DbModuleRegistry(List modules, ModuleRepository repository, WombatProperties wombat) + { + this(modules, repository, wombat.module().extension().path()); + } + + DbModuleRegistry(List modules, ModuleRepository repository, Path extensionPath) + { + this.detected = modules.stream().collect(Collectors.toMap(module -> module.type().name(), Function.identity())); + this.repository = repository; + this.extensionPath = extensionPath.toAbsolutePath().normalize(); + } + + /** + * Registers the detected modules that have no row yet, enabled; the existing rows keep their state. + */ + void onStart(@Observes @Priority(STARTUP_PRIORITY_REGISTER) StartupEvent event) + { + List types = this.detected.values().stream() + .map(WombatModule::type) + .toList(); + try { + Set registered = this.repository.register(types); + if (!registered.isEmpty()) + logger.info("Registered {} new module(s): {}", registered.size(), registered); + } + catch (PersistenceException e) { + /* Most likely another instance registering the same modules at once: a module with no row is enabled anyway */ + logger.warn("Could not register the detected modules, they are enabled until registered: {}", e.getMessage(), e); + } + } + + /** + * @return the detected modules, then the missing ones + */ + @Override + @Transactional + public List listModules() + { + Map rows = this.repository.listAll().stream() + .collect(Collectors.toMap(entity -> entity.typeName, Function.identity())); + + Stream detected = this.detected.values().stream() + .map(module -> this.detected(module, rows.get(module.type().name()))); + Stream missing = rows.values().stream() + .filter(entity -> !this.detected.containsKey(entity.typeName)) + .map(DbModuleRegistry::missing); + return Stream.concat(detected, missing).toList(); + } + + @Override + @Transactional + public Optional getModule(String module) + { + ModuleEntity entity = this.repository.findById(module); + WombatModule detected = this.detected.get(module); + if (detected != null) + return Optional.of(this.detected(detected, entity)); + return Optional.ofNullable(entity).map(DbModuleRegistry::missing); + } + + @Override + @Transactional + public ModuleMetadata setEnabled(String module, boolean enabled, String user) + { + ModuleEntity entity = this.repository.findById(module); + WombatModule detected = this.detected.get(module); + if (detected == null) + { + if (entity == null) + throw new UnknownModuleException(module); + throw new ModuleConflictException("Module " + module + " is not detected, it cannot be toggled"); + } + + /* Registered at startup, unless that failed */ + if (entity == null) + { + entity = ModuleEntity.registered(detected.type()); + this.repository.persist(entity); + } + entity.enabled = enabled; + entity.updatedBy = user; + return this.detected(detected, entity); + } + + @Override + @Transactional + public void remove(String module) + { + ModuleEntity entity = this.repository.findByIdOptional(module) + .orElseThrow(() -> new UnknownModuleException(module)); + if (this.detected.containsKey(module)) + throw new ModuleConflictException("Module " + module + " is detected, only a missing module can be removed"); + + this.repository.delete(entity); + } + + private ModuleMetadata detected(WombatModule module, ModuleEntity entity) + { + return new ModuleMetadata( + module.type(), + module.assetClass(), + entity == null || entity.enabled, + false, + this.isExtension(module.getClass()) ? Source.extension : Source.core + ); + } + + private static ModuleMetadata missing(ModuleEntity entity) + { + return new ModuleMetadata(entity.type(), null, entity.enabled, true, null); + } + + /** + * @return whether the class was loaded from a jar of the extension folder + */ + private boolean isExtension(Class type) + { + CodeSource source = type.getProtectionDomain().getCodeSource(); + if (source == null || source.getLocation() == null) + return false; + try { + Path location = Path.of(source.getLocation().toURI()).toAbsolutePath().normalize(); + return this.extensionPath.equals(location.getParent()); + } + catch (URISyntaxException | IllegalArgumentException | FileSystemNotFoundException e) { + /* Not a file, so not a jar of the extension folder */ + return false; + } + } +} diff --git a/src/main/java/tech/illuin/wombat/engine/module/registry/InMemoryModuleRegistry.java b/src/main/java/tech/illuin/wombat/engine/module/registry/InMemoryModuleRegistry.java index 7f57c03..c9d1265 100644 --- a/src/main/java/tech/illuin/wombat/engine/module/registry/InMemoryModuleRegistry.java +++ b/src/main/java/tech/illuin/wombat/engine/module/registry/InMemoryModuleRegistry.java @@ -2,27 +2,26 @@ import tech.illuin.wombat.core.module.WombatModule; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.concurrent.ConcurrentHashMap; +/** + * A registry of the modules registered on it, none of them missing; its state is lost with it. + */ public class InMemoryModuleRegistry implements ModuleRegistry { private final Map modules; public InMemoryModuleRegistry() { - this.modules = new HashMap<>(); + this.modules = new ConcurrentHashMap<>(); } public void registerModule(WombatModule module, boolean enabled) { - this.modules.put(module.type().name(), new ModuleMetadata( - module.type(), - module.assetClass(), - enabled - )); + this.modules.put(module.type().name(), ModuleMetadata.core(module.type(), module.assetClass(), enabled)); } @Override @@ -36,4 +35,23 @@ public Optional getModule(String module) { return Optional.ofNullable(this.modules.get(module)); } + + @Override + public ModuleMetadata setEnabled(String module, boolean enabled, String user) + { + ModuleMetadata current = this.getModule(module) + .orElseThrow(() -> new UnknownModuleException(module)); + ModuleMetadata updated = ModuleMetadata.core(current.type(), current.assetClass(), enabled); + this.modules.put(module, updated); + return updated; + } + + /** No module is missing here, so none can be removed. */ + @Override + public void remove(String module) + { + if (!this.modules.containsKey(module)) + throw new UnknownModuleException(module); + throw new ModuleConflictException("Module " + module + " is detected, only a missing module can be removed"); + } } diff --git a/src/main/java/tech/illuin/wombat/engine/module/registry/ModuleConflictException.java b/src/main/java/tech/illuin/wombat/engine/module/registry/ModuleConflictException.java new file mode 100644 index 0000000..213017d --- /dev/null +++ b/src/main/java/tech/illuin/wombat/engine/module/registry/ModuleConflictException.java @@ -0,0 +1,12 @@ +package tech.illuin.wombat.engine.module.registry; + +/** + * A missing module toggled, or a detected module deleted. + */ +public class ModuleConflictException extends RuntimeException +{ + public ModuleConflictException(String message) + { + super(message); + } +} diff --git a/src/main/java/tech/illuin/wombat/engine/module/registry/ModuleMetadata.java b/src/main/java/tech/illuin/wombat/engine/module/registry/ModuleMetadata.java index 9526be9..d680868 100644 --- a/src/main/java/tech/illuin/wombat/engine/module/registry/ModuleMetadata.java +++ b/src/main/java/tech/illuin/wombat/engine/module/registry/ModuleMetadata.java @@ -5,18 +5,51 @@ import tech.illuin.wombat.core.asset.SimulatedAsset; import tech.illuin.wombat.core.asset.type.AssetType; +/** + * A registered module, as the registry knows it. + * + * @param type for a missing module, as it advertised it when last detected + * @param assetClass null when the module is missing + * @param enabled the registry state, shared by every instance + * @param missing whether the module is registered but no longer detected, e.g. its extension jar was removed + * @param source whether it comes from the extension folder; null when the module is missing + */ public record ModuleMetadata( AssetType type, Class assetClass, - boolean enabled + boolean enabled, + boolean missing, + Source source ) { + /** + * A module detected on this instance, not from the extension folder. + */ + public static ModuleMetadata core(AssetType type, Class assetClass, boolean enabled) + { + return new ModuleMetadata(type, assetClass, enabled, false, Source.core); + } + + /** + * @return whether its assets can be processed: the module is enabled, and detected + */ + public boolean active() + { + return this.enabled && !this.missing; + } + /** * @return whether assets of this type can be declared in an environment; simulated assets only exist within a - * simulation request + * simulation request, and a missing module takes no asset */ @JsonProperty("creatable") public boolean creatable() { - return !SimulatedAsset.class.isAssignableFrom(this.assetClass); + return !this.missing && !SimulatedAsset.class.isAssignableFrom(this.assetClass); + } + + public enum Source + { + core, + extension, } } diff --git a/src/main/java/tech/illuin/wombat/engine/module/registry/ModuleRegistry.java b/src/main/java/tech/illuin/wombat/engine/module/registry/ModuleRegistry.java index 79380b9..d805980 100644 --- a/src/main/java/tech/illuin/wombat/engine/module/registry/ModuleRegistry.java +++ b/src/main/java/tech/illuin/wombat/engine/module/registry/ModuleRegistry.java @@ -7,12 +7,33 @@ public interface ModuleRegistry { + /** + * @return every registered module, missing ones included + */ List listModules(); + /** + * @return the registered module, which may be missing: see {@link ModuleMetadata#active()} + */ Optional getModule(String module); default Optional getModule(AssetType type) { return this.getModule(type.name()); } + + /** + * @param user who toggles the module, null when authentication is disabled + * @throws UnknownModuleException if the module is not registered + * @throws ModuleConflictException if the module is missing + */ + ModuleMetadata setEnabled(String module, boolean enabled, String user); + + /** + * Forgets a missing module. Should it be detected again, it is registered afresh, enabled. + * + * @throws UnknownModuleException if the module is not registered + * @throws ModuleConflictException if the module is detected + */ + void remove(String module); } diff --git a/src/main/java/tech/illuin/wombat/engine/module/registry/UnknownModuleException.java b/src/main/java/tech/illuin/wombat/engine/module/registry/UnknownModuleException.java new file mode 100644 index 0000000..e104843 --- /dev/null +++ b/src/main/java/tech/illuin/wombat/engine/module/registry/UnknownModuleException.java @@ -0,0 +1,12 @@ +package tech.illuin.wombat.engine.module.registry; + +/** + * A module neither detected nor registered. + */ +public class UnknownModuleException extends RuntimeException +{ + public UnknownModuleException(String name) + { + super("No module could be found for id " + name); + } +} diff --git a/src/main/java/tech/illuin/wombat/engine/module/registry/schema/DefaultSchemaRegistry.java b/src/main/java/tech/illuin/wombat/engine/module/registry/schema/DefaultSchemaRegistry.java index c197015..b99aca4 100644 --- a/src/main/java/tech/illuin/wombat/engine/module/registry/schema/DefaultSchemaRegistry.java +++ b/src/main/java/tech/illuin/wombat/engine/module/registry/schema/DefaultSchemaRegistry.java @@ -61,6 +61,8 @@ public DefaultSchemaRegistry(ModuleRegistry moduleRegistry, ObjectMapper objectM public Optional getSchema(AssetType type) { JsonNode node = this.cache.computeIfAbsent(type, k -> this.moduleRegistry.getModule(k) + /* A missing module has no class to describe */ + .filter(module -> !module.missing()) .map(mm -> withoutIdentity(this.schemaGenerator.generateSchema(mm.assetClass()))) .orElse(null) ); diff --git a/src/main/java/tech/illuin/wombat/security/Permission.java b/src/main/java/tech/illuin/wombat/security/Permission.java index 1c2ce41..b9fc08c 100644 --- a/src/main/java/tech/illuin/wombat/security/Permission.java +++ b/src/main/java/tech/illuin/wombat/security/Permission.java @@ -10,6 +10,8 @@ public enum Permission UI_USERS_READ(Names.UI_USERS_READ, false), UI_ENVIRONMENTS_READ(Names.UI_ENVIRONMENTS_READ, false), UI_ENVIRONMENTS_MANAGE(Names.UI_ENVIRONMENTS_MANAGE, false), + UI_MODULES_READ(Names.UI_MODULES_READ, false), + UI_MODULES_MANAGE(Names.UI_MODULES_MANAGE, false), API_IMPACT_READ(Names.API_IMPACT_READ, true), API_SIMULATION_READ(Names.API_SIMULATION_READ, true), API_ENVIRONMENT_READ(Names.API_ENVIRONMENT_READ, true), @@ -18,6 +20,7 @@ public enum Permission API_ASSET_WRITE(Names.API_ASSET_WRITE, true), API_METRIC_WRITE(Names.API_METRIC_WRITE, true), API_MODULE_READ(Names.API_MODULE_READ, true), + API_MODULE_MANAGE(Names.API_MODULE_MANAGE, true), API_APIKEY_MANAGE(Names.API_APIKEY_MANAGE, false), API_APIKEY_ADMIN(Names.API_APIKEY_ADMIN, false), API_USER_MANAGE(Names.API_USER_MANAGE, false), @@ -59,9 +62,12 @@ public static final class Names public static final String UI_USERS_READ = "ui.users.read"; public static final String UI_ENVIRONMENTS_READ = "ui.environments.read"; public static final String UI_ENVIRONMENTS_MANAGE = "ui.environments.manage"; + public static final String UI_MODULES_READ = "ui.modules.read"; + public static final String UI_MODULES_MANAGE = "ui.modules.manage"; public static final String API_IMPACT_READ = "api.impact.read"; public static final String API_SIMULATION_READ = "api.simulation.read"; public static final String API_MODULE_READ = "api.module.read"; + public static final String API_MODULE_MANAGE = "api.module.manage"; public static final String API_ENVIRONMENT_READ = "api.environment.read"; public static final String API_ENVIRONMENT_WRITE = "api.environment.write"; public static final String API_ASSET_READ = "api.asset.read"; diff --git a/src/main/java/tech/illuin/wombat/security/Role.java b/src/main/java/tech/illuin/wombat/security/Role.java index c44641f..94b8d1c 100644 --- a/src/main/java/tech/illuin/wombat/security/Role.java +++ b/src/main/java/tech/illuin/wombat/security/Role.java @@ -57,6 +57,7 @@ private static final class Base UI_DASHBOARD_READ, UI_APIKEYS_MANAGE, UI_ENVIRONMENTS_READ, + UI_MODULES_READ, API_APIKEY_MANAGE, API_IMPACT_READ, API_SIMULATION_READ, diff --git a/src/main/java/tech/illuin/wombat/security/UiPagePolicy.java b/src/main/java/tech/illuin/wombat/security/UiPagePolicy.java index e3a9503..94d2881 100644 --- a/src/main/java/tech/illuin/wombat/security/UiPagePolicy.java +++ b/src/main/java/tech/illuin/wombat/security/UiPagePolicy.java @@ -49,6 +49,8 @@ static Permission requiredFor(String path) return Permission.UI_ENVIRONMENTS_MANAGE; if (path.equals("/environments") || path.startsWith("/environments/")) return Permission.UI_ENVIRONMENTS_READ; + if (path.equals("/modules") || path.startsWith("/modules/")) + return Permission.UI_MODULES_READ; return Permission.UI_DASHBOARD_READ; } } diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index 795972c..a42014c 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -132,12 +132,7 @@ wombat: expire-after-write: 10m module: - kubernetes-api: - enabled: true - llm-prometheus: - enabled: true - llm-anthropic: - enabled: true + # Modules are enabled or disabled from Settings > Modules, no longer here extension: path: "extensions" diff --git a/src/main/resources/db/migration/postgresql/V16__module_settings.sql b/src/main/resources/db/migration/postgresql/V16__module_settings.sql new file mode 100644 index 0000000..93a1736 --- /dev/null +++ b/src/main/resources/db/migration/postgresql/V16__module_settings.sql @@ -0,0 +1,34 @@ +CREATE TABLE modules +( + type_name TEXT PRIMARY KEY, + namespace TEXT NOT NULL, + local_name TEXT NOT NULL, + regime TEXT NOT NULL, + family TEXT NOT NULL, + enabled BIGINT NOT NULL, + updated_at TIMESTAMP WITH TIME ZONE NOT NULL, + updated_by TEXT +); + +CREATE TABLE core_deployments +( + version BIGINT PRIMARY KEY, + id TEXT NOT NULL, + requested_at TIMESTAMP WITH TIME ZONE NOT NULL, + requested_by TEXT, + enabled_modules TEXT NOT NULL +); + +CREATE UNIQUE INDEX idx_core_deployments_id ON core_deployments (id); + +CREATE TABLE core_instances +( + instance_id TEXT PRIMARY KEY, + deployed_version BIGINT NOT NULL, + deployed_at TIMESTAMP WITH TIME ZONE NOT NULL, + status TEXT NOT NULL, + error TEXT, + detected_modules TEXT NOT NULL, + missing_modules TEXT NOT NULL, + heartbeat_at TIMESTAMP WITH TIME ZONE NOT NULL +); diff --git a/src/main/resources/db/migration/sqlite/V25__module_settings.sql b/src/main/resources/db/migration/sqlite/V25__module_settings.sql new file mode 100644 index 0000000..d85cea7 --- /dev/null +++ b/src/main/resources/db/migration/sqlite/V25__module_settings.sql @@ -0,0 +1,34 @@ +CREATE TABLE modules +( + type_name TEXT PRIMARY KEY, + namespace TEXT NOT NULL, + local_name TEXT NOT NULL, + regime TEXT NOT NULL, + family TEXT NOT NULL, + enabled INTEGER NOT NULL, + updated_at INTEGER NOT NULL, + updated_by TEXT +); + +CREATE TABLE core_deployments +( + version INTEGER PRIMARY KEY, + id TEXT NOT NULL, + requested_at INTEGER NOT NULL, + requested_by TEXT, + enabled_modules TEXT NOT NULL +); + +CREATE UNIQUE INDEX idx_core_deployments_id ON core_deployments (id); + +CREATE TABLE core_instances +( + instance_id TEXT PRIMARY KEY, + deployed_version INTEGER NOT NULL, + deployed_at INTEGER NOT NULL, + status TEXT NOT NULL, + error TEXT, + detected_modules TEXT NOT NULL, + missing_modules TEXT NOT NULL, + heartbeat_at INTEGER NOT NULL +); diff --git a/src/main/webui/src/domain/access.ts b/src/main/webui/src/domain/access.ts index 89820ad..b246281 100644 --- a/src/main/webui/src/domain/access.ts +++ b/src/main/webui/src/domain/access.ts @@ -4,6 +4,8 @@ export type Permission = | 'ui.users.read' | 'ui.environments.read' | 'ui.environments.manage' + | 'ui.modules.read' + | 'ui.modules.manage' | 'api.impact.read' | 'api.simulation.read' | 'api.environment.read' @@ -12,6 +14,7 @@ export type Permission = | 'api.asset.write' | 'api.metric.write' | 'api.module.read' + | 'api.module.manage' | 'api.apikey.manage' | 'api.apikey.admin' | 'api.user.manage' diff --git a/src/main/webui/src/services/api/modules.ts b/src/main/webui/src/services/api/modules.ts index c861085..0262f87 100644 --- a/src/main/webui/src/services/api/modules.ts +++ b/src/main/webui/src/services/api/modules.ts @@ -3,8 +3,10 @@ import { request } from '@/services/api/client' export type { JsonSchema, ModuleMetadata } from '@/domain/module' -export function fetchModules(): Promise { - return request('/api/modules') +/** The asset types this server detects. Missing modules (registered, but no longer detected) take no asset, so they are left out. */ +export async function fetchModules(): Promise { + const modules = await request<(ModuleMetadata & { missing?: boolean })[]>('/api/modules') + return modules.filter(module => !module.missing) } /* A type's schema only changes with the server's version: fetch it once per page load */ diff --git a/src/test/java/tech/illuin/wombat/endpoint/DisabledModuleProfile.java b/src/test/java/tech/illuin/wombat/endpoint/DisabledModuleProfile.java deleted file mode 100644 index 53bd093..0000000 --- a/src/test/java/tech/illuin/wombat/endpoint/DisabledModuleProfile.java +++ /dev/null @@ -1,18 +0,0 @@ -package tech.illuin.wombat.endpoint; - -import io.quarkus.test.junit.QuarkusTestProfile; - -import java.util.Map; - -/** An aggregator which disabled the kubernetes-api module. */ -public class DisabledModuleProfile implements QuarkusTestProfile -{ - @Override - public Map getConfigOverrides() - { - return Map.of( - "wombat.mode", "aggregator", - "wombat.module.kubernetes-api.enabled", "false" - ); - } -} diff --git a/src/test/java/tech/illuin/wombat/endpoint/MetricControllerDisabledModuleTest.java b/src/test/java/tech/illuin/wombat/endpoint/MetricControllerDisabledModuleTest.java index 6ec7ef8..aa08855 100644 --- a/src/test/java/tech/illuin/wombat/endpoint/MetricControllerDisabledModuleTest.java +++ b/src/test/java/tech/illuin/wombat/endpoint/MetricControllerDisabledModuleTest.java @@ -5,19 +5,25 @@ import io.quarkus.test.junit.TestProfile; import io.restassured.http.ContentType; import jakarta.inject.Inject; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import tech.illuin.wombat.engine.impact.kubernetes.KubernetesMetricRepository; import tech.illuin.wombat.engine.impact.llm.LLMModelMetricRepository; +import tech.illuin.wombat.engine.module.registry.ModuleRegistry; +import tech.illuin.wombat.module.kubernetes_api.KubernetesAPIModule; import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.is; import static org.junit.jupiter.api.Assertions.assertEquals; -/** The metrics of an asset whose module the aggregator disabled are skipped, while the others of the request are saved. */ +/** + * The metrics of an asset whose module the aggregator disabled are skipped, while the others of the request are saved. + * Ingestion follows the registry: the module is disabled, not redeployed. + */ @QuarkusTest -@TestProfile(DisabledModuleProfile.class) +@TestProfile(AggregatorProfile.class) class MetricControllerDisabledModuleTest { @Inject @@ -26,15 +32,25 @@ class MetricControllerDisabledModuleTest @Inject LLMModelMetricRepository llm; + @Inject + ModuleRegistry modules; + @BeforeEach void setUp() { + this.modules.setEnabled(KubernetesAPIModule.TYPE.name(), false, null); QuarkusTransaction.requiringNew().run(() -> { this.kubernetes.deleteByAssetId("east.pushed-cluster"); this.llm.deleteByAssetId("east.pushed-llm"); }); } + @AfterEach + void tearDown() + { + this.modules.setEnabled(KubernetesAPIModule.TYPE.name(), true, null); + } + @Test void push_skipsTheMetricsOfADisabledModule_reportsThemInTheStatus_andSavesTheOthers() { diff --git a/src/test/java/tech/illuin/wombat/endpoint/ModuleControllerTest.java b/src/test/java/tech/illuin/wombat/endpoint/ModuleControllerTest.java index 2e826ed..8822229 100644 --- a/src/test/java/tech/illuin/wombat/endpoint/ModuleControllerTest.java +++ b/src/test/java/tech/illuin/wombat/endpoint/ModuleControllerTest.java @@ -1,20 +1,41 @@ package tech.illuin.wombat.endpoint; +import io.quarkus.narayana.jta.QuarkusTransaction; import io.quarkus.test.junit.QuarkusTest; import io.quarkus.test.junit.TestProfile; import io.quarkus.test.security.TestSecurity; import io.quarkus.test.security.oidc.Claim; import io.quarkus.test.security.oidc.OidcSecurity; +import io.restassured.http.ContentType; +import jakarta.inject.Inject; import jakarta.ws.rs.NotFoundException; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.Test; +import io.quarkus.security.identity.SecurityIdentity; +import tech.illuin.wombat.engine.module.ModuleService; +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.engine.module.library.kubernetes_simulated.KubernetesSimulatedModule; import tech.illuin.wombat.engine.module.registry.ModuleMetadata; import tech.illuin.wombat.engine.module.registry.ModuleRegistry; +import tech.illuin.wombat.engine.module.persistence.ModuleEntity; +import tech.illuin.wombat.engine.module.persistence.ModuleRepository; import tech.illuin.wombat.engine.module.registry.schema.AssetSchemaRegistry; +import tech.illuin.wombat.security.Caller; +import tech.illuin.wombat.security.Permission; import tech.illuin.wombat.security.PermissionAugmentor; +import tech.illuin.wombat.security.Role; +import tech.illuin.wombat.security.apikey.ApiKeyAuthenticationMechanism; +import tech.illuin.wombat.security.apikey.ApiKeyService; import tech.illuin.wombat.ui.AuthenticationEnabledTest; +import java.time.Duration; +import java.time.Instant; +import java.util.EnumSet; +import java.util.List; import java.util.Optional; +import java.util.UUID; import static io.restassured.RestAssured.given; import static org.hamcrest.Matchers.hasItem; @@ -30,6 +51,23 @@ class ModuleControllerTest { private static final String MODULE_ID = KubernetesSimulatedModule.TYPE.name(); + private static final AssetType MISSING_TYPE = AssetType.of("acme", "removed-extension", ActivityRegime.MEASURED, ServiceFamily.LLM); + private static final String MISSING_ID = MISSING_TYPE.name(); + + @Inject + ModuleRepository repository; + + @Inject + ApiKeyService apiKeys; + + @AfterEach + void restoreTheRegistry() + { + QuarkusTransaction.requiringNew().run(() -> { + this.repository.findById(MODULE_ID).enabled = true; + this.repository.deleteById(MISSING_ID); + }); + } @Test @TestSecurity(user = "module-reader", roles = "wombat-users", augmentors = PermissionAugmentor.class) @@ -44,6 +82,121 @@ void list_returnsModuleMetadata() .body("payload.find { it.type.name == '" + MODULE_ID + "' }.enabled", is(true)); } + @Test + @TestSecurity(user = "module-reader", roles = "wombat-users", augmentors = PermissionAugmentor.class) + void list_tellsTheRegistryStateFromTheDeployedOne() + { + given() + .when().get("/api/modules") + .then() + .statusCode(200) + .body("payload.find { it.type.name == '" + MODULE_ID + "' }.deployed", is(true)) + .body("payload.find { it.type.name == '" + MODULE_ID + "' }.pending", is(false)) + .body("payload.find { it.type.name == '" + MODULE_ID + "' }.missing", is(false)) + .body("payload.find { it.type.name == '" + MODULE_ID + "' }.source", is("core")) + .body("payload.find { it.type.name == '" + MODULE_ID + "' }.assetCount", notNullValue()); + } + + @Test + @TestSecurity(user = "boss", roles = "wombat-admins", augmentors = PermissionAugmentor.class) + void update_changesTheRegistryAtOnce_andLeavesTheCoreUntilARedeploy() + { + given().contentType(ContentType.JSON).body("{\"enabled\": false}") + .when().put("/api/modules/" + MODULE_ID) + .then() + .statusCode(200) + .body("payload.enabled", is(false)) + .body("payload.deployed", is(true)) + .body("payload.pending", is(true)); + + given() + .when().get("/api/modules/" + MODULE_ID) + .then() + .statusCode(200) + .body("payload.enabled", is(false)) + .body("payload.pending", is(true)); + + assertEquals("boss", QuarkusTransaction.requiringNew().call(() -> this.repository.findById(MODULE_ID).updatedBy)); + } + + @Test + @TestSecurity(user = "boss", roles = "wombat-admins", augmentors = PermissionAugmentor.class) + void update_rejectsAPayloadWithoutTheFlag() + { + given().contentType(ContentType.JSON).body("{}") + .when().put("/api/modules/" + MODULE_ID) + .then() + .statusCode(400); + } + + @Test + @TestSecurity(user = "boss", roles = "wombat-admins", augmentors = PermissionAugmentor.class) + void aMissingModule_isListed_cannotBeToggled_andCanBeDeleted() + { + QuarkusTransaction.requiringNew().run(() -> this.repository.persist(ModuleEntity.registered(MISSING_TYPE))); + + given() + .when().get("/api/modules") + .then() + .statusCode(200) + .body("payload.find { it.type.name == '" + MISSING_ID + "' }.missing", is(true)) + .body("payload.find { it.type.name == '" + MISSING_ID + "' }.type.family", is("LLM")); + given().contentType(ContentType.JSON).body("{\"enabled\": false}") + .when().put("/api/modules/" + MISSING_ID) + .then() + .statusCode(409); + given().when().get("/api/modules/" + MISSING_ID + "/schema").then().statusCode(404); + + given().when().delete("/api/modules/" + MISSING_ID).then().statusCode(204); + given().when().get("/api/modules/" + MISSING_ID).then().statusCode(404); + } + + @Test + @TestSecurity(user = "boss", roles = "wombat-admins", augmentors = PermissionAugmentor.class) + void aDetectedModule_cannotBeDeleted() + { + given().when().delete("/api/modules/" + MODULE_ID).then().statusCode(409); + } + + @Test + @TestSecurity(user = "boss", roles = "wombat-admins", augmentors = PermissionAugmentor.class) + void writesOnAnUnknownModule_returnNotFound() + { + given().contentType(ContentType.JSON).body("{\"enabled\": false}") + .when().put("/api/modules/does-not-exist") + .then() + .statusCode(404); + given().when().delete("/api/modules/does-not-exist").then().statusCode(404); + } + + @Test + @TestSecurity(user = "module-reader", roles = "wombat-users", augmentors = PermissionAugmentor.class) + void writesAreForbiddenToUsers() + { + given().contentType(ContentType.JSON).body("{\"enabled\": false}") + .when().put("/api/modules/" + MODULE_ID) + .then() + .statusCode(403); + given().when().delete("/api/modules/" + MODULE_ID).then().statusCode(403); + } + + @Test + void aKey_togglesModulesOnlyWithTheManagePermission() + { + String manager = this.issue(Permission.Names.API_MODULE_MANAGE, Permission.Names.API_MODULE_READ); + String reader = this.issue(Permission.Names.API_MODULE_READ); + + given().header(ApiKeyAuthenticationMechanism.HEADER, reader).contentType(ContentType.JSON).body("{\"enabled\": false}") + .when().put("/api/modules/" + MODULE_ID) + .then() + .statusCode(403); + given().header(ApiKeyAuthenticationMechanism.HEADER, manager).contentType(ContentType.JSON).body("{\"enabled\": false}") + .when().put("/api/modules/" + MODULE_ID) + .then() + .statusCode(200) + .body("payload.enabled", is(false)); + } + @Test @TestSecurity(user = "module-reader", roles = "wombat-users", augmentors = PermissionAugmentor.class) void find_returnsModuleMetadata() @@ -107,12 +260,18 @@ void getSchema_forKnownModuleWithoutSchema_throwsNotFound() KubernetesSimulatedModule module = new KubernetesSimulatedModule(); ModuleRegistry registry = mock(ModuleRegistry.class); AssetSchemaRegistry schemaRegistry = mock(AssetSchemaRegistry.class); - when(registry.getModule(MODULE_ID)).thenReturn(Optional.of(new ModuleMetadata(module.type(), module.assetClass(), true))); + when(registry.getModule(MODULE_ID)).thenReturn(Optional.of(ModuleMetadata.core(module.type(), module.assetClass(), true))); when(schemaRegistry.getSchema(module.type())).thenReturn(Optional.empty()); - ModuleController controller = new ModuleController(registry, schemaRegistry); + ModuleController controller = new ModuleController(mock(ModuleService.class), registry, schemaRegistry, mock(SecurityIdentity.class)); NotFoundException error = assertThrows(NotFoundException.class, () -> controller.getSchema(MODULE_ID)); assertEquals("No module could be found for id " + MODULE_ID, error.getMessage()); } + + private String issue(String... permissions) + { + Caller admin = new Caller("https://idp", UUID.randomUUID().toString(), "admin", "admin@example.com", EnumSet.of(Role.ADMIN), Role.ADMIN.permissions()); + return this.apiKeys.create(admin, "modules", List.of(permissions), Instant.now().plus(Duration.ofDays(1))).token(); + } } \ No newline at end of file diff --git a/src/test/java/tech/illuin/wombat/endpoint/WorkerControllerDisabledModuleTest.java b/src/test/java/tech/illuin/wombat/endpoint/WorkerControllerDisabledModuleTest.java index 5d6c7f6..155ea19 100644 --- a/src/test/java/tech/illuin/wombat/endpoint/WorkerControllerDisabledModuleTest.java +++ b/src/test/java/tech/illuin/wombat/endpoint/WorkerControllerDisabledModuleTest.java @@ -6,6 +6,7 @@ import io.restassured.http.ContentType; import jakarta.inject.Inject; import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import tech.illuin.wombat.core.activity.commons.Country; import tech.illuin.wombat.core.asset.AssetIdentity; @@ -31,7 +32,7 @@ /** An aggregator which disabled the kubernetes-api module: its assets are still declared, their metrics are skipped when pushed. */ @QuarkusTest -@TestProfile(DisabledModuleProfile.class) +@TestProfile(AggregatorProfile.class) class WorkerControllerDisabledModuleTest { private static final Country FRANCE = Country.ofCode("FRA").orElseThrow(); @@ -46,10 +47,17 @@ class WorkerControllerDisabledModuleTest @Inject ModuleRegistry modules; + @BeforeEach + void setUp() + { + this.modules.setEnabled(KubernetesAPIModule.TYPE.name(), false, null); + } + @AfterEach void tearDown() throws Exception { this.declare(new MonitoredEnvironments(Map.of())); + this.modules.setEnabled(KubernetesAPIModule.TYPE.name(), true, null); } @Test diff --git a/src/test/java/tech/illuin/wombat/engine/context/asset/persistence/AssetConverterTest.java b/src/test/java/tech/illuin/wombat/engine/context/asset/persistence/AssetConverterTest.java index faa8928..4b94c7d 100644 --- a/src/test/java/tech/illuin/wombat/engine/context/asset/persistence/AssetConverterTest.java +++ b/src/test/java/tech/illuin/wombat/engine/context/asset/persistence/AssetConverterTest.java @@ -13,7 +13,6 @@ import tech.illuin.wombat.core.asset.profile.ServerProvider; import tech.illuin.wombat.core.module.WombatModule; import tech.illuin.wombat.engine.module.WombatModuleConfig; -import tech.illuin.wombat.engine.module.registry.ModuleRegistries; import tech.illuin.wombat.module.kubernetes_api.KubernetesAPIAsset; import tech.illuin.wombat.module.kubernetes_api.KubernetesAPIModule; import tech.illuin.wombat.module.kubernetes_api.KubernetesAPIServerProfile; @@ -124,7 +123,7 @@ private Asset roundTrip(Asset asset) private static AssetConverter converter() { AssetConverter converter = new AssetConverter(); - converter.mapper = new WombatModuleConfig().provideJsonMapper(ModuleRegistries.enabled(modules())); + converter.mapper = new WombatModuleConfig().provideJsonMapper(modules()); return converter; } diff --git a/src/test/java/tech/illuin/wombat/engine/module/ModuleServiceTest.java b/src/test/java/tech/illuin/wombat/engine/module/ModuleServiceTest.java new file mode 100644 index 0000000..d5d8e99 --- /dev/null +++ b/src/test/java/tech/illuin/wombat/engine/module/ModuleServiceTest.java @@ -0,0 +1,146 @@ +package tech.illuin.wombat.engine.module; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +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.evaluation.AssetEvaluator; +import tech.illuin.wombat.core.module.WombatModule; +import tech.illuin.wombat.core.source.AssetMonitor; +import tech.illuin.wombat.engine.context.asset.persistence.AssetRepository; +import tech.illuin.wombat.engine.module.library.kubernetes_simulated.KubernetesSimulatedModule; +import tech.illuin.wombat.engine.module.library.llm_simulated.LLMSimulatedModule; +import tech.illuin.wombat.engine.module.registry.ModuleMetadata; +import tech.illuin.wombat.engine.module.registry.ModuleRegistry; +import tech.illuin.wombat.engine.module.registry.UnknownModuleException; +import tech.illuin.wombat.engine.module.runtime.CoreDeployment; +import tech.illuin.wombat.engine.module.runtime.CoreRuntime; + +import java.time.Instant; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +class ModuleServiceTest +{ + private static final KubernetesSimulatedModule K8S_MODULE = new KubernetesSimulatedModule(); + private static final LLMSimulatedModule LLM_MODULE = new LLMSimulatedModule(); + private static final String K8S = K8S_MODULE.type().name(); + private static final String LLM = LLM_MODULE.type().name(); + private static final AssetType REMOVED = AssetType.of("acme", "removed", ActivityRegime.MEASURED, ServiceFamily.LLM); + + private ModuleRegistry registry; + private AssetRepository assets; + private CoreRuntime runtime; + private ModuleService service; + + @BeforeEach + void setUp() + { + this.registry = mock(ModuleRegistry.class); + this.assets = mock(AssetRepository.class); + this.runtime = mock(CoreRuntime.class); + this.service = new ModuleService(this.registry, this.assets, this.runtime); + + when(this.assets.countVisibleByType()).thenReturn(Map.of(K8S, 3L)); + when(this.registry.getModule(any(String.class))).thenReturn(Optional.empty()); + } + + @Test + void aModuleDisabledButStillDeployedIsPending() + { + this.registered(detected(K8S_MODULE, true), detected(LLM_MODULE, false)); + this.deployed(K8S_MODULE, LLM_MODULE); + + Map modules = byName(this.service.list()); + + assertTrue(modules.get(K8S).deployed()); + assertFalse(modules.get(K8S).pending()); + assertEquals(3, modules.get(K8S).assetCount()); + + assertFalse(modules.get(LLM).enabled()); + assertTrue(modules.get(LLM).deployed()); + assertTrue(modules.get(LLM).pending(), "disabled in the registry, still in the core until a redeploy"); + assertEquals(0, modules.get(LLM).assetCount()); + } + + @Test + void aMissingModuleIsNeverPending_sinceARedeployCannotBringItIn() + { + this.registered(missing(REMOVED)); + this.deployed(); + + ModuleInfo module = this.service.list().getFirst(); + + assertTrue(module.missing()); + assertFalse(module.deployed()); + assertFalse(module.pending()); + } + + @Test + void list_putsTheDetectedModulesFirst_thenTheMissingOnes() + { + this.registered(missing(REMOVED), detected(LLM_MODULE, true), detected(K8S_MODULE, true)); + this.deployed(); + + List names = this.service.list().stream().map(module -> module.type().name()).toList(); + + assertEquals(List.of(K8S, LLM, REMOVED.name()), names); + } + + @Test + void setEnabled_goesThroughTheRegistry() + { + when(this.registry.getModule(K8S)).thenReturn(Optional.of(detected(K8S_MODULE, false))); + this.deployed(K8S_MODULE); + + ModuleInfo module = this.service.setEnabled(K8S, false, "alice"); + + verify(this.registry).setEnabled(K8S, false, "alice"); + assertTrue(module.pending()); + } + + @Test + void find_onAnUnknownModuleIsNotFound() + { + assertThrows(UnknownModuleException.class, () -> this.service.find("acme.unknown")); + } + + private void registered(ModuleMetadata... modules) + { + when(this.registry.listModules()).thenReturn(List.of(modules)); + } + + private void deployed(WombatModule... modules) + { + AssetEvaluator evaluator = mock(AssetEvaluator.class); + when(this.runtime.current()).thenReturn(new CoreDeployment(List.of(modules), Set.of(), mock(AssetMonitor.class), evaluator, evaluator, Instant.now())); + } + + private static ModuleMetadata detected(WombatModule module, boolean enabled) + { + return ModuleMetadata.core(module.type(), module.assetClass(), enabled); + } + + private static ModuleMetadata missing(AssetType type) + { + return new ModuleMetadata(type, null, true, true, null); + } + + private static Map byName(List modules) + { + return modules.stream().collect(Collectors.toMap(module -> module.type().name(), module -> module)); + } +} diff --git a/src/test/java/tech/illuin/wombat/engine/module/WombatModuleConfigTest.java b/src/test/java/tech/illuin/wombat/engine/module/WombatModuleConfigTest.java index e6ebfb0..d8d6811 100644 --- a/src/test/java/tech/illuin/wombat/engine/module/WombatModuleConfigTest.java +++ b/src/test/java/tech/illuin/wombat/engine/module/WombatModuleConfigTest.java @@ -14,29 +14,20 @@ import tech.illuin.wombat.commons.WombatProperties; import tech.illuin.wombat.engine.connector.boavizta.BoaviztaProperties; import tech.illuin.wombat.engine.connector.ecologits.EcologitsProperties; -import tech.illuin.wombat.engine.module.library.kubernetes_simulated.KubernetesSimulatedModule; -import tech.illuin.wombat.engine.module.library.llm_simulated.LLMSimulatedModule; import tech.illuin.wombat.engine.module.extension.CompositeExtensionLoader; import tech.illuin.wombat.engine.module.extension.ExtensionLoader; import tech.illuin.wombat.engine.module.extension.WombatModuleProperties; -import tech.illuin.wombat.engine.module.registry.ModuleMetadata; -import tech.illuin.wombat.engine.module.registry.ModuleRegistries; -import tech.illuin.wombat.engine.module.registry.ModuleRegistry; import tech.illuin.wombat.persistence.BackendMode; import tech.illuin.wombat.persistence.PersistenceBackendProperties; import tech.illuin.wombat.persistence.backend.push.WorkerProperties; -import tech.illuin.wombat.module.kubernetes_static.KubernetesStaticModule; -import tech.illuin.wombat.module.llm_anthropic.LLMAnthropicModule; import tech.illuin.wombat.module.kubernetes_api.KubernetesAPIModule; import tech.illuin.wombat.module.llm_prometheus.LLMPrometheusModule; import tech.illuin.wombat.module.llm_static.LLMStaticModule; -import tech.illuin.wombat.module.llm_vertexai.LLMVertexAIModule; import java.nio.file.Path; import java.util.List; import java.util.Optional; -import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -46,8 +37,7 @@ import static tech.illuin.wombat.engine.module.WombatModuleConfig.*; /** - * Exercises the producers directly rather than through CDI: the toggles are plain logic over - * {@link WombatModuleProperties}, and the collaborators are never touched during instantiation. + * Exercises the producers directly rather than through CDI: the collaborators are never touched during instantiation. */ class WombatModuleConfigTest { @@ -57,7 +47,7 @@ class WombatModuleConfigTest @Test void provideWombatExtensionLoader_returnsCompositeExtensionLoader() { - WombatProperties wombat = wombatProperties(true, true, true, true); + WombatProperties wombat = wombatProperties(); ExtensionLoader loader = provideWombatExtensionLoader(wombat); assertInstanceOf(CompositeExtensionLoader.class, loader); @@ -76,38 +66,6 @@ void provideWombatModules_loadsModulesFromExtensionLoader() verify(loader).load(); } - @Test - void provideModuleRegistry_usesConfigurationForEnabledMetadata() - { - List modules = List.of( - new KubernetesAPIModule(), - new LLMPrometheusModule(), - new LLMAnthropicModule(), - new LLMVertexAIModule(), - new KubernetesSimulatedModule(), - new LLMSimulatedModule(), - new KubernetesStaticModule(), - new LLMStaticModule() - ); - - assertAll( - () -> assertEnabled(provideModuleRegistry(modules, wombatProperties(false, true, true, true)), new KubernetesAPIModule(), false), - () -> assertEnabled(provideModuleRegistry(modules, wombatProperties(true, false, true, true)), new LLMPrometheusModule(), false), - () -> assertEnabled(provideModuleRegistry(modules, wombatProperties(true, true, false, true)), new LLMAnthropicModule(), false), - () -> assertEnabled(provideModuleRegistry(modules, wombatProperties(true, true, true, false)), new LLMVertexAIModule(), false), - () -> assertEnabled(provideModuleRegistry(modules, wombatProperties(false, false, false, false)), new KubernetesSimulatedModule(), true), - () -> assertEnabled(provideModuleRegistry(modules, wombatProperties(false, false, false, false)), new LLMSimulatedModule(), true), - () -> assertEnabled(provideModuleRegistry(modules, wombatProperties(false, false, false, false)), new KubernetesStaticModule(), true), - () -> assertEnabled(provideModuleRegistry(modules, wombatProperties(false, false, false, false)), new LLMStaticModule(), true) - ); - } - - private static void assertEnabled(ModuleRegistry registry, WombatModule module, boolean expected) - { - ModuleMetadata metadata = registry.getModule(module.type().name()).orElseThrow(); - assertEquals(expected, metadata.enabled(), "enabled metadata for " + metadata.type().name()); - } - @Test void provideMappers_registerSubtypesForGivenModules() { @@ -117,17 +75,17 @@ void provideMappers_registerSubtypesForGivenModules() new LLMStaticModule() ); - JsonMapper jsonMapper = this.config.provideJsonMapper(ModuleRegistries.enabled(modules)); + JsonMapper jsonMapper = this.config.provideJsonMapper(modules); assertNotNull(jsonMapper); - YAMLMapper yamlMapper = this.config.provideYAMLMapper(ModuleRegistries.enabled(modules)); + YAMLMapper yamlMapper = this.config.provideYAMLMapper(modules); assertNotNull(yamlMapper); } @Test void yamlMapper_deserializesKebabCaseAndIgnoresUnknownProperties() throws Exception { - YAMLMapper yamlMapper = this.config.provideYAMLMapper(ModuleRegistries.enabled(List.of())); + YAMLMapper yamlMapper = this.config.provideYAMLMapper(List.of()); String yaml = """ first-name: Alice home-address: 123 Main St @@ -142,7 +100,7 @@ void yamlMapper_deserializesKebabCaseAndIgnoresUnknownProperties() throws Except @Test void jsonMapper_deserializesCamelCaseAndIgnoresUnknownProperties() throws Exception { - JsonMapper jsonMapper = this.config.provideJsonMapper(ModuleRegistries.enabled(List.of())); + JsonMapper jsonMapper = this.config.provideJsonMapper(List.of()); String json = """ { "firstName": "Bob", @@ -160,7 +118,7 @@ void jsonMapper_deserializesCamelCaseAndIgnoresUnknownProperties() throws Except void configureQuarkusObjectMapper_ignoresUnknownProperties() throws Exception { ObjectMapper mapper = new ObjectMapper(); - this.config.configureQuarkusObjectMapper(null, mapper, ModuleRegistries.enabled(List.of())); + this.config.configureQuarkusObjectMapper(null, mapper, List.of()); String json = """ { "firstName": "Charlie", @@ -210,7 +168,7 @@ void provideDefaults_createsHandlersForFamilies() assertNotNull(llmHandlers.costResolver()); } - private static WombatProperties wombatProperties(boolean kubernetesApi, boolean llmPrometheus, boolean llmAnthropic, boolean vertexAi) + private static WombatProperties wombatProperties() { return new WombatProperties() { @@ -240,18 +198,6 @@ public WombatModuleProperties module() { return new WombatModuleProperties() { - @Override - public WombatModuleProperties.KubernetesApiProperties kubernetesApi() { return () -> kubernetesApi; } - - @Override - public WombatModuleProperties.LlmPrometheusProperties llmPrometheus() { return () -> llmPrometheus; } - - @Override - public WombatModuleProperties.LlmAnthropicProperties llmAnthropic() { return () -> llmAnthropic; } - - @Override - public WombatModuleProperties.LlmVertexAIProperties llmVertexAi() { return () -> vertexAi; } - @Override public WombatModuleProperties.ExtensionProperties extension() { return () -> Path.of("extensions"); } }; diff --git a/src/test/java/tech/illuin/wombat/engine/module/extension/DynamicModuleIntegrationTest.java b/src/test/java/tech/illuin/wombat/engine/module/extension/DynamicModuleIntegrationTest.java index 7b9fe27..d9df913 100644 --- a/src/test/java/tech/illuin/wombat/engine/module/extension/DynamicModuleIntegrationTest.java +++ b/src/test/java/tech/illuin/wombat/engine/module/extension/DynamicModuleIntegrationTest.java @@ -24,7 +24,6 @@ import tech.illuin.wombat.core.source.Monitorable; import tech.illuin.wombat.core.source.WombatSource; import tech.illuin.wombat.engine.module.WombatModuleConfig; -import tech.illuin.wombat.engine.module.registry.ModuleRegistries; import tech.illuin.wombat.engine.monitor.MonitoredEnvironments; import java.io.FileOutputStream; @@ -59,7 +58,7 @@ void dynamicModule_e2e_discovery_jackson_persistence_core(@TempDir Path tempDir) // 1. YAML Deserialization via WombatModuleConfig WombatModuleConfig moduleConfig = new WombatModuleConfig(); - YAMLMapper yamlMapper = moduleConfig.provideYAMLMapper(ModuleRegistries.enabled(modules)); + YAMLMapper yamlMapper = moduleConfig.provideYAMLMapper(modules); String yaml = """ environments: @@ -92,7 +91,7 @@ void dynamicModule_e2e_discovery_jackson_persistence_core(@TempDir Path tempDir) assertEquals("tech.custom.extension.custom-source", entity.type); assertEquals(customAsset, entity.toProperties()); - JsonMapper jsonMapper = moduleConfig.provideJsonMapper(ModuleRegistries.enabled(modules)); + JsonMapper jsonMapper = moduleConfig.provideJsonMapper(modules); AssetConverter converter = new AssetConverter(jsonMapper); String dbJson = converter.convertToDatabaseColumn(customAsset); assertTrue(dbJson.contains("\"type\":\"tech.custom.extension.custom-source\"")); @@ -127,7 +126,7 @@ void dynamicModule_whenDropped_databaseRowGracefullyIgnored(@TempDir Path tempDi { List modules = loader.load(); WombatModuleConfig moduleConfig = new WombatModuleConfig(); - JsonMapper jsonMapper = moduleConfig.provideJsonMapper(ModuleRegistries.enabled(modules)); + JsonMapper jsonMapper = moduleConfig.provideJsonMapper(modules); AssetConverter converter = new AssetConverter(jsonMapper); CustomDynamicAsset customAsset = new CustomDynamicAsset( @@ -139,7 +138,7 @@ void dynamicModule_whenDropped_databaseRowGracefullyIgnored(@TempDir Path tempDi // Now simulate the JAR being removed/dropped: standard modules only WombatModuleConfig standardConfig = new WombatModuleConfig(); - JsonMapper standardJsonMapper = standardConfig.provideJsonMapper(ModuleRegistries.enabled(List.of())); + JsonMapper standardJsonMapper = standardConfig.provideJsonMapper(List.of()); AssetConverter standardConverter = new AssetConverter(standardJsonMapper); Asset parsedAsset = standardConverter.convertToEntityAttribute(dbJson); @@ -151,7 +150,7 @@ void dynamicModule_whenDropped_databaseRowGracefullyIgnored(@TempDir Path tempDi assertEquals(dbJson, standardConverter.convertToDatabaseColumn(unrecognized)); // YAML recovery verification for dropped module - YAMLMapper standardYamlMapper = standardConfig.provideYAMLMapper(ModuleRegistries.enabled(List.of())); + YAMLMapper standardYamlMapper = standardConfig.provideYAMLMapper(List.of()); String yaml = """ environments: dynamic-env: diff --git a/src/test/java/tech/illuin/wombat/engine/module/persistence/ModuleRepositoryTest.java b/src/test/java/tech/illuin/wombat/engine/module/persistence/ModuleRepositoryTest.java new file mode 100644 index 0000000..8c62527 --- /dev/null +++ b/src/test/java/tech/illuin/wombat/engine/module/persistence/ModuleRepositoryTest.java @@ -0,0 +1,74 @@ +package tech.illuin.wombat.engine.module.persistence; + +import io.quarkus.narayana.jta.QuarkusTransaction; +import io.quarkus.test.junit.QuarkusTest; +import jakarta.inject.Inject; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; +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.engine.module.library.kubernetes_simulated.KubernetesSimulatedModule; + +import java.util.List; +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@QuarkusTest +class ModuleRepositoryTest +{ + private static final String DETECTED = KubernetesSimulatedModule.TYPE.name(); + private static final AssetType NEW_TYPE = AssetType.of("acme", "new-extension", ActivityRegime.MEASURED, ServiceFamily.LLM); + private static final String NEW = NEW_TYPE.name(); + + @Inject + ModuleRepository repository; + + @AfterEach + void tearDown() + { + QuarkusTransaction.requiringNew().run(() -> { + ModuleEntity detected = this.repository.findById(DETECTED); + detected.enabled = true; + detected.advertise(KubernetesSimulatedModule.TYPE); + this.repository.deleteById(NEW); + }); + } + + @Test + void everyDetectedModuleIsRegisteredAtStartup() + { + assertTrue(this.repository.findById(DETECTED).enabled); + } + + @Test + void register_insertsTheNewModules_andLeavesTheStateOfTheExistingOnes() + { + QuarkusTransaction.requiringNew().run(() -> this.repository.findById(DETECTED).enabled = false); + + Set registered = this.repository.register(List.of(KubernetesSimulatedModule.TYPE, NEW_TYPE)); + + assertEquals(Set.of(NEW), registered); + assertFalse(this.repository.findById(DETECTED).enabled); + ModuleEntity created = this.repository.findById(NEW); + assertTrue(created.enabled); + assertNull(created.updatedBy); + assertEquals(NEW_TYPE, created.type()); + } + + @Test + void register_updatesTheAdvertisedTypeOfAnExistingModule() + { + AssetType changed = AssetType.of(KubernetesSimulatedModule.TYPE.namespace(), KubernetesSimulatedModule.TYPE.localName(), ActivityRegime.MEASURED, ServiceFamily.KUBERNETES_CONTAINER); + + this.repository.register(List.of(changed)); + assertEquals(ActivityRegime.MEASURED, this.repository.findById(DETECTED).regime); + + this.repository.register(List.of(KubernetesSimulatedModule.TYPE)); + assertEquals(KubernetesSimulatedModule.TYPE, this.repository.findById(DETECTED).type()); + } +} diff --git a/src/test/java/tech/illuin/wombat/engine/module/registry/DbModuleRegistryTest.java b/src/test/java/tech/illuin/wombat/engine/module/registry/DbModuleRegistryTest.java new file mode 100644 index 0000000..d8d1969 --- /dev/null +++ b/src/test/java/tech/illuin/wombat/engine/module/registry/DbModuleRegistryTest.java @@ -0,0 +1,185 @@ +package tech.illuin.wombat.engine.module.registry; + +import jakarta.persistence.PersistenceException; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +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.engine.module.library.kubernetes_simulated.KubernetesSimulatedModule; +import tech.illuin.wombat.engine.module.library.llm_simulated.LLMSimulatedModule; +import tech.illuin.wombat.engine.module.persistence.ModuleEntity; +import tech.illuin.wombat.engine.module.persistence.ModuleRepository; + +import java.nio.file.Path; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; + +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.argThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +class DbModuleRegistryTest +{ + private static final String K8S = KubernetesSimulatedModule.TYPE.name(); + private static final String LLM = LLMSimulatedModule.TYPE.name(); + private static final AssetType REMOVED = AssetType.of("acme", "removed", ActivityRegime.MEASURED, ServiceFamily.LLM); + + private ModuleRepository repository; + private DbModuleRegistry registry; + + @BeforeEach + void setUp() + { + this.repository = mock(ModuleRepository.class); + this.registry = new DbModuleRegistry(List.of(new KubernetesSimulatedModule(), new LLMSimulatedModule()), this.repository, Path.of("extensions")); + when(this.repository.findByIdOptional(any())).thenReturn(Optional.empty()); + } + + @Test + void startupRegistersEveryDetectedModule() + { + when(this.repository.register(any())).thenReturn(Set.of()); + + this.registry.onStart(null); + + verify(this.repository).register(argThat(types -> Set.copyOf(types).equals(Set.of(KubernetesSimulatedModule.TYPE, LLMSimulatedModule.TYPE)))); + } + + @Test + void aFailedRegistrationDoesNotFailTheStartup() + { + when(this.repository.register(any())).thenThrow(new PersistenceException("unique constraint violated")); + + assertDoesNotThrow(() -> this.registry.onStart(null)); + } + + @Test + void detectedModulesAreEnabledAsTheirRowSays_orEnabledWithoutRow() + { + when(this.repository.listAll()).thenReturn(List.of(row(KubernetesSimulatedModule.TYPE, false))); + + Map enabled = this.registry.listModules().stream() + .collect(Collectors.toMap(module -> module.type().name(), ModuleMetadata::enabled)); + + assertEquals(Map.of(K8S, false, LLM, true), enabled); + } + + @Test + void aRegisteredModuleNoLongerDetectedIsListedMissing_withItsAdvertisedType() + { + when(this.repository.listAll()).thenReturn(List.of(row(REMOVED, false))); + + List modules = this.registry.listModules(); + + assertEquals(3, modules.size()); + ModuleMetadata missing = modules.getLast(); + assertEquals(REMOVED, missing.type()); + assertTrue(missing.missing()); + assertFalse(missing.enabled()); + assertFalse(missing.active()); + assertFalse(missing.creatable()); + assertNull(missing.assetClass()); + assertNull(missing.source()); + } + + @Test + void getModule_returnsDetectedAndMissingModules() + { + when(this.repository.findById(K8S)).thenReturn(row(KubernetesSimulatedModule.TYPE, false)); + when(this.repository.findById(REMOVED.name())).thenReturn(row(REMOVED, true)); + + assertFalse(this.registry.getModule(K8S).orElseThrow().enabled()); + assertTrue(this.registry.getModule(LLM).orElseThrow().active()); + assertTrue(this.registry.getModule(REMOVED.name()).orElseThrow().missing()); + assertTrue(this.registry.getModule("acme.unknown").isEmpty()); + } + + @Test + void aModuleFromTheClasspathIsACoreOne() + { + ModuleMetadata module = this.registry.getModule(K8S).orElseThrow(); + + assertEquals(ModuleMetadata.Source.core, module.source()); + } + + @Test + void setEnabled_updatesTheRowAndWhoChangedIt() + { + ModuleEntity row = row(KubernetesSimulatedModule.TYPE, true); + when(this.repository.findById(K8S)).thenReturn(row); + + ModuleMetadata module = this.registry.setEnabled(K8S, false, "alice"); + + assertFalse(module.enabled()); + assertFalse(row.enabled); + assertEquals("alice", row.updatedBy); + } + + @Test + void setEnabled_registersADetectedModuleWithoutRow() + { + this.registry.setEnabled(K8S, false, null); + + verify(this.repository).persist(any(ModuleEntity.class)); + } + + @Test + void setEnabled_onAMissingModuleIsAConflict() + { + when(this.repository.findById(REMOVED.name())).thenReturn(row(REMOVED, true)); + + assertThrows(ModuleConflictException.class, () -> this.registry.setEnabled(REMOVED.name(), false, null)); + } + + @Test + void setEnabled_onAnUnknownModuleIsNotFound() + { + assertThrows(UnknownModuleException.class, () -> this.registry.setEnabled("acme.unknown", false, null)); + } + + @Test + void remove_deletesTheRowOfAMissingModule() + { + ModuleEntity row = row(REMOVED, true); + when(this.repository.findByIdOptional(REMOVED.name())).thenReturn(Optional.of(row)); + + this.registry.remove(REMOVED.name()); + + verify(this.repository).delete(row); + } + + @Test + void remove_onADetectedModuleIsAConflict() + { + when(this.repository.findByIdOptional(K8S)).thenReturn(Optional.of(row(KubernetesSimulatedModule.TYPE, true))); + + assertThrows(ModuleConflictException.class, () -> this.registry.remove(K8S)); + verify(this.repository, never()).delete(any()); + } + + @Test + void remove_onAnUnknownModuleIsNotFound() + { + assertThrows(UnknownModuleException.class, () -> this.registry.remove("acme.unknown")); + } + + private static ModuleEntity row(AssetType type, boolean enabled) + { + ModuleEntity entity = ModuleEntity.registered(type); + entity.enabled = enabled; + return entity; + } +} diff --git a/src/test/java/tech/illuin/wombat/engine/module/registry/InMemoryModuleRegistryTest.java b/src/test/java/tech/illuin/wombat/engine/module/registry/InMemoryModuleRegistryTest.java index f82d7ba..aafa5f7 100644 --- a/src/test/java/tech/illuin/wombat/engine/module/registry/InMemoryModuleRegistryTest.java +++ b/src/test/java/tech/illuin/wombat/engine/module/registry/InMemoryModuleRegistryTest.java @@ -7,6 +7,8 @@ import java.util.Set; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; class InMemoryModuleRegistryTest @@ -19,7 +21,7 @@ void registerModule_listsAndFindsModuleMetadata() registry.registerModule(module, false); - ModuleMetadata expected = new ModuleMetadata(module.type(), module.assetClass(), false); + ModuleMetadata expected = ModuleMetadata.core(module.type(), module.assetClass(), false); assertEquals(Set.of(expected), Set.copyOf(registry.listModules())); assertEquals(expected, registry.getModule(module.type().name()).orElseThrow()); assertEquals(expected, registry.getModule(module.type()).orElseThrow()); @@ -47,4 +49,27 @@ void unregisteredTypeIsAbsent() assertTrue(registry.getModule(new LLMSimulatedModule().type().name()).isEmpty()); assertTrue(registry.listModules().isEmpty()); } -} \ No newline at end of file + + @Test + void setEnabled_togglesARegisteredModule() + { + KubernetesSimulatedModule module = new KubernetesSimulatedModule(); + InMemoryModuleRegistry registry = new InMemoryModuleRegistry(); + registry.registerModule(module, true); + + assertFalse(registry.setEnabled(module.type().name(), false, null).enabled()); + assertFalse(registry.getModule(module.type()).orElseThrow().enabled()); + assertThrows(UnknownModuleException.class, () -> registry.setEnabled("unknown", false, null)); + } + + @Test + void remove_refusesEveryModule_sinceNoneIsMissing() + { + KubernetesSimulatedModule module = new KubernetesSimulatedModule(); + InMemoryModuleRegistry registry = new InMemoryModuleRegistry(); + registry.registerModule(module, true); + + assertThrows(ModuleConflictException.class, () -> registry.remove(module.type().name())); + assertThrows(UnknownModuleException.class, () -> registry.remove("unknown")); + } +} diff --git a/src/test/java/tech/illuin/wombat/engine/monitor/MonitoredEnvironmentsTemplateTest.java b/src/test/java/tech/illuin/wombat/engine/monitor/MonitoredEnvironmentsTemplateTest.java index 1e616c1..130eb31 100644 --- a/src/test/java/tech/illuin/wombat/engine/monitor/MonitoredEnvironmentsTemplateTest.java +++ b/src/test/java/tech/illuin/wombat/engine/monitor/MonitoredEnvironmentsTemplateTest.java @@ -17,7 +17,6 @@ import tech.illuin.wombat.engine.evaluation.CachedAssetEvaluatorProperties; import tech.illuin.wombat.engine.module.WombatModuleConfig; import tech.illuin.wombat.engine.module.extension.WombatModuleProperties; -import tech.illuin.wombat.engine.module.registry.ModuleRegistries; import tech.illuin.wombat.module.kubernetes_api.KubernetesAPIAsset; import tech.illuin.wombat.module.kubernetes_api.KubernetesAPIModule; import tech.illuin.wombat.module.llm_prometheus.LLMPrometheusAsset; @@ -115,11 +114,11 @@ private static MonitoredEnvironments load() */ private static YAMLMapper mapper() { - return new WombatModuleConfig().provideYAMLMapper(ModuleRegistries.enabled(List.of( + return new WombatModuleConfig().provideYAMLMapper(List.of( new KubernetesAPIModule(), new LLMPrometheusModule(), new LLMStaticModule() - ))); + )); } private static Asset byType(List assets, AssetType type) @@ -164,10 +163,6 @@ private static WombatProperties wombatProperties(String environmentsFile) { return new WombatModuleProperties() { - @Override public WombatModuleProperties.KubernetesApiProperties kubernetesApi() { return () -> true; } - @Override public WombatModuleProperties.LlmPrometheusProperties llmPrometheus() { return () -> true; } - @Override public WombatModuleProperties.LlmAnthropicProperties llmAnthropic() { return () -> true; } - @Override public WombatModuleProperties.LlmVertexAIProperties llmVertexAi() { return () -> true; } @Override public WombatModuleProperties.ExtensionProperties extension() { return () -> Path.of("extensions"); } }; } diff --git a/src/test/java/tech/illuin/wombat/persistence/backend/postgres/PostgresSchemaTest.java b/src/test/java/tech/illuin/wombat/persistence/backend/postgres/PostgresSchemaTest.java index ebaf41a..8eb5020 100644 --- a/src/test/java/tech/illuin/wombat/persistence/backend/postgres/PostgresSchemaTest.java +++ b/src/test/java/tech/illuin/wombat/persistence/backend/postgres/PostgresSchemaTest.java @@ -32,7 +32,7 @@ class PostgresSchemaTest { private static final Set EXPECTED_TABLES = Set.of( "server_metrics", "model_metrics", "environments", "assets", "asset_config_history", - "users", "api_keys", "role_grants", "secrets" + "users", "api_keys", "role_grants", "secrets", "modules", "core_deployments", "core_instances" ); private static String jdbcUrl; diff --git a/src/test/java/tech/illuin/wombat/security/RoleTest.java b/src/test/java/tech/illuin/wombat/security/RoleTest.java index 2f3334a..8c41067 100644 --- a/src/test/java/tech/illuin/wombat/security/RoleTest.java +++ b/src/test/java/tech/illuin/wombat/security/RoleTest.java @@ -23,6 +23,9 @@ void admin_holdsEveryPermission() assertFalse(Role.USER.permissions().contains(Permission.API_SECRET_READ)); assertFalse(Role.USER.permissions().contains(Permission.UI_ENVIRONMENTS_MANAGE)); assertFalse(Permission.API_SECRET_READ.grantableToApiKeys()); + assertFalse(Role.USER.permissions().contains(Permission.API_MODULE_MANAGE)); + assertFalse(Role.USER.permissions().contains(Permission.UI_MODULES_MANAGE)); + assertTrue(Permission.API_MODULE_MANAGE.grantableToApiKeys()); } @Test @@ -30,7 +33,8 @@ void users_readTheReportAndManageTheirOwnKeys() { assertEquals( EnumSet.of( - Permission.UI_DASHBOARD_READ, Permission.UI_APIKEYS_MANAGE, Permission.UI_ENVIRONMENTS_READ, Permission.API_APIKEY_MANAGE, + Permission.UI_DASHBOARD_READ, Permission.UI_APIKEYS_MANAGE, Permission.UI_ENVIRONMENTS_READ, Permission.UI_MODULES_READ, + Permission.API_APIKEY_MANAGE, Permission.API_IMPACT_READ, Permission.API_SIMULATION_READ, Permission.API_ENVIRONMENT_READ, Permission.API_ASSET_READ, Permission.API_MODULE_READ ), diff --git a/src/test/java/tech/illuin/wombat/security/UiPagePolicyTest.java b/src/test/java/tech/illuin/wombat/security/UiPagePolicyTest.java index 27a55cb..e3c2181 100644 --- a/src/test/java/tech/illuin/wombat/security/UiPagePolicyTest.java +++ b/src/test/java/tech/illuin/wombat/security/UiPagePolicyTest.java @@ -29,6 +29,8 @@ void requiredFor_mapsSpaPathsToTheirPermissions() assertEquals(tech.illuin.wombat.security.Permission.UI_ENVIRONMENTS_READ, UiPagePolicy.requiredFor("/environments")); assertEquals(tech.illuin.wombat.security.Permission.UI_ENVIRONMENTS_READ, UiPagePolicy.requiredFor("/environments/preprod")); assertEquals(tech.illuin.wombat.security.Permission.UI_ENVIRONMENTS_MANAGE, UiPagePolicy.requiredFor("/environments/new")); + assertEquals(tech.illuin.wombat.security.Permission.UI_MODULES_READ, UiPagePolicy.requiredFor("/modules")); + assertEquals(tech.illuin.wombat.security.Permission.UI_MODULES_READ, UiPagePolicy.requiredFor("/modules/tech.illuin.wombat-module.llm-static")); } @Test diff --git a/src/test/java/tech/illuin/wombat/ui/AuthenticationEnabledTest.java b/src/test/java/tech/illuin/wombat/ui/AuthenticationEnabledTest.java index 552f1eb..2955df4 100644 --- a/src/test/java/tech/illuin/wombat/ui/AuthenticationEnabledTest.java +++ b/src/test/java/tech/illuin/wombat/ui/AuthenticationEnabledTest.java @@ -149,7 +149,7 @@ void admin_managesApiKeys() .when().get("/api/keys/settings") .then() .statusCode(200) - .body("payload.grantablePermissions", containsInAnyOrder("api.impact.read", "api.simulation.read", "api.environment.read", "api.module.read", "api.environment.write", "api.asset.read", "api.asset.write", "api.metric.write")) + .body("payload.grantablePermissions", containsInAnyOrder("api.impact.read", "api.simulation.read", "api.environment.read", "api.module.read", "api.module.manage", "api.environment.write", "api.asset.read", "api.asset.write", "api.metric.write")) .body("payload.maxLifetimeDays", is(365)) .body("payload.allowNeverExpiring", is(false)); } diff --git a/src/test/resources/application.yaml b/src/test/resources/application.yaml index 8810e68..eb63b80 100644 --- a/src/test/resources/application.yaml +++ b/src/test/resources/application.yaml @@ -59,11 +59,5 @@ wombat: expire-after-write: 10m module: - kubernetes-api: - enabled: true - llm-prometheus: - enabled: true - llm-anthropic: - enabled: true extension: path: "/target/extensions" From 35c2a0711a812c9c756313156e138c8f579bfec1 Mon Sep 17 00:00:00 2001 From: Pierre Lecerf Date: Fri, 20 Nov 2026 22:05:13 +0100 Subject: [PATCH 3/6] feat(engine): versioned core redeploys across instances --- .../wombat/endpoint/ModuleController.java | 39 +++ .../engine/context/asset/AssetValidator.java | 2 +- .../wombat/engine/module/DeploymentInfo.java | 48 ++++ .../wombat/engine/module/ModuleInfo.java | 9 +- .../wombat/engine/module/ModuleService.java | 87 +++++- .../extension/WombatModuleProperties.java | 23 +- .../persistence/CoreDeploymentEntity.java | 67 +++++ .../persistence/CoreDeploymentRepository.java | 47 ++++ .../persistence/CoreInstanceEntity.java | 55 ++++ .../persistence/CoreInstanceRepository.java | 41 +++ .../persistence/TypeNameSetConverter.java | 33 +++ .../module/registry/DbModuleRegistry.java | 55 ++-- .../module/registry/ModuleMetadata.java | 24 +- .../schema/DefaultSchemaRegistry.java | 5 +- .../engine/module/runtime/CoreDeployment.java | 4 + .../module/runtime/CoreDeploymentFactory.java | 6 +- .../module/runtime/CoreDeploymentService.java | 258 ++++++++++++++++++ .../engine/module/runtime/CoreRuntime.java | 54 ++-- .../runtime/DeploymentConflictException.java | 12 + src/main/resources/application.yaml | 7 +- .../wombat/endpoint/ModuleControllerTest.java | 75 ++++- .../engine/module/ModuleServiceTest.java | 47 +++- .../engine/module/WombatModuleConfigTest.java | 1 + .../persistence/CoreDeploymentEntityTest.java | 42 +++ .../CoreDeploymentRepositoryTest.java | 78 ++++++ .../module/registry/DbModuleRegistryTest.java | 48 +++- .../runtime/CoreDeploymentFactoryTest.java | 12 +- .../runtime/CoreDeploymentServiceTest.java | 232 ++++++++++++++++ .../module/runtime/CoreRuntimeTest.java | 71 ++--- .../module/runtime/RuntimeProxiesTest.java | 19 +- .../MonitoredEnvironmentsTemplateTest.java | 1 + 31 files changed, 1368 insertions(+), 134 deletions(-) create mode 100644 src/main/java/tech/illuin/wombat/engine/module/DeploymentInfo.java create mode 100644 src/main/java/tech/illuin/wombat/engine/module/persistence/CoreDeploymentEntity.java create mode 100644 src/main/java/tech/illuin/wombat/engine/module/persistence/CoreDeploymentRepository.java create mode 100644 src/main/java/tech/illuin/wombat/engine/module/persistence/CoreInstanceEntity.java create mode 100644 src/main/java/tech/illuin/wombat/engine/module/persistence/CoreInstanceRepository.java create mode 100644 src/main/java/tech/illuin/wombat/engine/module/persistence/TypeNameSetConverter.java create mode 100644 src/main/java/tech/illuin/wombat/engine/module/runtime/CoreDeploymentService.java create mode 100644 src/main/java/tech/illuin/wombat/engine/module/runtime/DeploymentConflictException.java create mode 100644 src/test/java/tech/illuin/wombat/engine/module/persistence/CoreDeploymentEntityTest.java create mode 100644 src/test/java/tech/illuin/wombat/engine/module/persistence/CoreDeploymentRepositoryTest.java create mode 100644 src/test/java/tech/illuin/wombat/engine/module/runtime/CoreDeploymentServiceTest.java diff --git a/src/main/java/tech/illuin/wombat/endpoint/ModuleController.java b/src/main/java/tech/illuin/wombat/endpoint/ModuleController.java index 8009412..ae0abfd 100644 --- a/src/main/java/tech/illuin/wombat/endpoint/ModuleController.java +++ b/src/main/java/tech/illuin/wombat/endpoint/ModuleController.java @@ -13,13 +13,16 @@ import org.eclipse.microprofile.openapi.annotations.media.Schema; import org.eclipse.microprofile.openapi.annotations.media.SchemaProperty; import org.eclipse.microprofile.openapi.annotations.responses.APIResponse; +import org.jboss.resteasy.reactive.ResponseStatus; import tech.illuin.wombat.endpoint.response.Response; import tech.illuin.wombat.endpoint.response.Status; +import tech.illuin.wombat.engine.module.DeploymentInfo; import tech.illuin.wombat.engine.module.ModuleInfo; import tech.illuin.wombat.engine.module.ModuleService; import tech.illuin.wombat.engine.module.registry.ModuleConflictException; import tech.illuin.wombat.engine.module.registry.ModuleRegistry; import tech.illuin.wombat.engine.module.registry.UnknownModuleException; +import tech.illuin.wombat.engine.module.runtime.DeploymentConflictException; import tech.illuin.wombat.engine.module.registry.schema.AssetSchemaRegistry; import tech.illuin.wombat.feature.Feature; import tech.illuin.wombat.security.Permission; @@ -159,6 +162,42 @@ public void delete(@PathParam("id") String id) } } + @GET + @Path("/deployment") + @Produces(MediaType.APPLICATION_JSON) + @APIResponse(responseCode = "200", content = @Content(mediaType = MediaType.APPLICATION_JSON, schema = @Schema(properties = { + @SchemaProperty(name = "status", implementation = Status.class), + @SchemaProperty(name = "payload", implementation = DeploymentInfo.class) + }))) + public Response deployment() + { + return Response.success(this.service.deployment()); + } + + /** + * Records a deployment of the modules enabled now, and deploys it on this instance; the others follow on their next + * poll. A build failing here is reported in the payload, the running core being kept. + */ + @POST + @Path("/deployment") + @PermissionsAllowed(Permission.Names.API_MODULE_MANAGE) + @Produces(MediaType.APPLICATION_JSON) + @ResponseStatus(201) + @APIResponse(responseCode = "201", content = @Content(mediaType = MediaType.APPLICATION_JSON, schema = @Schema(properties = { + @SchemaProperty(name = "status", implementation = Status.class), + @SchemaProperty(name = "payload", implementation = DeploymentInfo.Redeployed.class) + }))) + @APIResponse(responseCode = "409", description = "Another redeploy was requested at the same time") + public Response redeploy() + { + try { + return Response.success(this.service.redeploy(this.user())); + } + catch (DeploymentConflictException e) { + throw new ClientErrorException(e.getMessage(), CONFLICT); + } + } + /* Null when authentication is disabled, every caller is then anonymous */ private String user() { diff --git a/src/main/java/tech/illuin/wombat/engine/context/asset/AssetValidator.java b/src/main/java/tech/illuin/wombat/engine/context/asset/AssetValidator.java index 63d4443..f515f6f 100644 --- a/src/main/java/tech/illuin/wombat/engine/context/asset/AssetValidator.java +++ b/src/main/java/tech/illuin/wombat/engine/context/asset/AssetValidator.java @@ -31,7 +31,7 @@ public void validate(Asset asset) throws InvalidAssetException throw new InvalidAssetException("Unknown asset type " + unrecognized.rawType() + " for asset " + asset.identity().id()); ModuleMetadata module = this.modules.getModule(asset.type()) - .filter(candidate -> !candidate.missing()) + .filter(ModuleMetadata::detectedHere) .orElseThrow(() -> new InvalidAssetException("No module for asset type " + asset.type().name() + " of asset " + asset.identity().id())); if (!module.enabled()) logger.debug("Asset {} is declared with asset type {} whose module is disabled, its metrics will be skipped", asset.identity().id(), asset.type().name()); diff --git a/src/main/java/tech/illuin/wombat/engine/module/DeploymentInfo.java b/src/main/java/tech/illuin/wombat/engine/module/DeploymentInfo.java new file mode 100644 index 0000000..60cada6 --- /dev/null +++ b/src/main/java/tech/illuin/wombat/engine/module/DeploymentInfo.java @@ -0,0 +1,48 @@ +package tech.illuin.wombat.engine.module; + +import tech.illuin.wombat.engine.module.persistence.CoreInstanceEntity; +import tech.illuin.wombat.engine.module.runtime.CoreDeploymentService; + +import java.time.Instant; +import java.util.List; +import java.util.Set; + +/** + * The core deployment as the API serves it: the latest one requested, which instances run it, and the toggles it does + * not hold yet. + * + * @param pending the modules whose registry state the latest deployment does not hold yet + * @param pollIntervalSeconds how often instances poll for the latest deployment + */ +public record DeploymentInfo(Latest latest, List instances, List pending, long pollIntervalSeconds) +{ + /** + * @param id the full hash: clients shorten it to 7 characters, like a commit id + * @param modules the modules enabled when it was requested + */ + public record Latest(long version, String id, Instant requestedAt, String requestedBy, Set modules) {} + + /** + * @param deployedId null if the deployment the instance runs is unknown here + * @param current whether this is the instance serving the request + */ + public record Instance( + String instanceId, + long deployedVersion, + String deployedId, + Instant deployedAt, + CoreInstanceEntity.Status status, + String error, + Set detectedModules, + Set missingModules, + boolean current + ) {} + + public record PendingModule(String type, boolean enabled) {} + + /** + * A redeploy recorded, and the outcome of deploying it on the instance which recorded it; the others follow on + * their next poll. + */ + public record Redeployed(long version, String id, CoreDeploymentService.Result local) {} +} diff --git a/src/main/java/tech/illuin/wombat/engine/module/ModuleInfo.java b/src/main/java/tech/illuin/wombat/engine/module/ModuleInfo.java index e8cee44..6ba3ef6 100644 --- a/src/main/java/tech/illuin/wombat/engine/module/ModuleInfo.java +++ b/src/main/java/tech/illuin/wombat/engine/module/ModuleInfo.java @@ -9,12 +9,13 @@ * and whether the running core holds it. * * @param type as the module advertised it when last detected, for a missing one - * @param assetClass null when the module is missing + * @param assetClass null when the serving instance does not detect the module * @param enabled the registry state, which the asset pickers follow at once * @param deployed whether the core running on the serving instance holds the module - * @param pending whether the registry state differs from the deployed one, until a redeploy - * @param missing whether the module has a registry row but is no longer detected, e.g. its extension jar was removed - * @param source null when the module is missing + * @param pending whether the latest deployment does not hold the registry state yet, until a redeploy; never for a + * missing module, which a redeploy cannot bring in + * @param missing whether no live instance detects the module any more, e.g. its extension jar was removed + * @param source null when the serving instance does not detect the module * @param assetCount the number of assets of this type, soft-deleted ones aside */ public record ModuleInfo( diff --git a/src/main/java/tech/illuin/wombat/engine/module/ModuleService.java b/src/main/java/tech/illuin/wombat/engine/module/ModuleService.java index 672bb0d..489f643 100644 --- a/src/main/java/tech/illuin/wombat/engine/module/ModuleService.java +++ b/src/main/java/tech/illuin/wombat/engine/module/ModuleService.java @@ -4,9 +4,13 @@ import jakarta.transaction.Transactional; import tech.illuin.wombat.core.asset.type.AssetType; import tech.illuin.wombat.engine.context.asset.persistence.AssetRepository; +import tech.illuin.wombat.engine.module.persistence.CoreDeploymentEntity; +import tech.illuin.wombat.engine.module.persistence.CoreInstanceEntity; import tech.illuin.wombat.engine.module.registry.ModuleMetadata; import tech.illuin.wombat.engine.module.registry.ModuleRegistry; import tech.illuin.wombat.engine.module.registry.UnknownModuleException; +import tech.illuin.wombat.engine.module.runtime.CoreDeploymentService; +import tech.illuin.wombat.engine.module.runtime.CoreDeploymentService.Redeploy; import tech.illuin.wombat.engine.module.runtime.CoreRuntime; import java.util.Comparator; @@ -16,7 +20,7 @@ /** * The modules administration view, for {@code ModuleController} only: the rest of the app reads the modules from the - * {@link ModuleRegistry}. Adds to the registry's state what only the running core knows, and the asset counts. + * {@link ModuleRegistry}. Adds to the registry's state what the core deployments know, and the asset counts. */ @ApplicationScoped public class ModuleService @@ -24,16 +28,17 @@ public class ModuleService private final ModuleRegistry registry; private final AssetRepository assets; private final CoreRuntime runtime; + private final CoreDeploymentService deployments; - /* Detected modules first, then missing ones, each by name */ private static final Comparator ORDER = Comparator.comparing(ModuleInfo::missing) .thenComparing(module -> module.type().name()); - public ModuleService(ModuleRegistry registry, AssetRepository assets, CoreRuntime runtime) + public ModuleService(ModuleRegistry registry, AssetRepository assets, CoreRuntime runtime, CoreDeploymentService deployments) { this.registry = registry; this.assets = assets; this.runtime = runtime; + this.deployments = deployments; } @Transactional @@ -41,8 +46,9 @@ public List list() { Map counts = this.assets.countVisibleByType(); Set deployed = this.runtime.current().types(); + Set latest = this.latest().enabledModules; return this.registry.listModules().stream() - .map(module -> info(module, deployed, counts)) + .map(module -> info(module, deployed, latest, counts)) .sorted(ORDER) .toList(); } @@ -54,7 +60,7 @@ public List list() public ModuleInfo find(String name) { return this.registry.getModule(name) - .map(module -> info(module, this.runtime.current().types(), this.assets.countVisibleByType())) + .map(module -> info(module, this.runtime.current().types(), this.latest().enabledModules, this.assets.countVisibleByType())) .orElseThrow(() -> new UnknownModuleException(name)); } @@ -68,26 +74,81 @@ public ModuleInfo setEnabled(String name, boolean enabled, String user) return this.find(name); } - /** - * See {@link ModuleRegistry#remove}. - */ @Transactional public void remove(String name) { this.registry.remove(name); } - private static ModuleInfo info(ModuleMetadata module, Set deployedTypes, Map counts) + @Transactional + public DeploymentInfo deployment() + { + CoreDeploymentEntity latest = this.latest(); + List instances = this.deployments.liveInstances().stream() + .map(this::instance) + .toList(); + List pending = this.registry.listModules().stream() + .filter(module -> isPending(module, latest.enabledModules)) + .map(module -> new DeploymentInfo.PendingModule(module.type().name(), module.enabled())) + .sorted(Comparator.comparing(DeploymentInfo.PendingModule::type)) + .toList(); + + return new DeploymentInfo( + new DeploymentInfo.Latest(latest.version, latest.id, latest.requestedAt, latest.requestedBy, latest.enabledModules), + instances, + pending, + this.deployments.pollInterval().toSeconds() + ); + } + + /** + * See {@link CoreDeploymentService#redeploy}. + * + * @throws tech.illuin.wombat.engine.module.runtime.DeploymentConflictException if another instance requested a + * redeploy at the same time + */ + public DeploymentInfo.Redeployed redeploy(String user) + { + Redeploy redeploy = this.deployments.redeploy(user); + return new DeploymentInfo.Redeployed(redeploy.deployment().version, redeploy.deployment().id, redeploy.local()); + } + + private CoreDeploymentEntity latest() + { + return this.deployments.latest() + .orElseThrow(() -> new IllegalStateException("No core deployment is recorded, the startup records one")); + } + + private DeploymentInfo.Instance instance(CoreInstanceEntity entity) + { + return new DeploymentInfo.Instance( + entity.instanceId, + entity.deployedVersion, + this.deployments.find(entity.deployedVersion).map(deployment -> deployment.id).orElse(null), + entity.deployedAt, + entity.status, + entity.error, + entity.detectedModules, + entity.missingModules, + entity.instanceId.equals(this.deployments.instanceId()) + ); + } + + /* A redeploy cannot bring a missing module in */ + private static boolean isPending(ModuleMetadata module, Set latest) + { + return !module.missing() && module.enabled() != latest.contains(module.type().name()); + } + + private static ModuleInfo info(ModuleMetadata module, Set deployedTypes, Set latest, Map counts) { - boolean deployed = deployedTypes.contains(module.type()); return new ModuleInfo( module.type(), module.assetClass(), module.enabled(), module.creatable(), - deployed, - /* A redeploy cannot bring a missing module in. Against the local core for now, against the latest deployment once redeploys are versioned */ - !module.missing() && module.enabled() != deployed, + deployedTypes.contains(module.type()), + isPending(module, latest), module.missing(), module.source(), counts.getOrDefault(module.type().name(), 0L) diff --git a/src/main/java/tech/illuin/wombat/engine/module/extension/WombatModuleProperties.java b/src/main/java/tech/illuin/wombat/engine/module/extension/WombatModuleProperties.java index 9fcd24b..ecd1d4d 100644 --- a/src/main/java/tech/illuin/wombat/engine/module/extension/WombatModuleProperties.java +++ b/src/main/java/tech/illuin/wombat/engine/module/extension/WombatModuleProperties.java @@ -4,19 +4,36 @@ import io.smallrye.config.WithName; import java.nio.file.Path; +import java.time.Duration; -/** - * Whether a module is enabled is no longer configured here: it is the registry's state, kept in the database. - */ public interface WombatModuleProperties { @WithName("extension") ExtensionProperties extension(); + @WithName("deployment") + DeploymentProperties deployment(); + interface ExtensionProperties { @WithName("path") @WithDefault("extensions") Path path(); } + + interface DeploymentProperties + { + /** How often an instance checks for a newer core deployment, and writes its heartbeat. */ + @WithName("poll-interval") + @WithDefault("30s") + Duration pollInterval(); + + /** + * Identifies this instance among the replicas: the {@code HOSTNAME} variable (the pod name in Kubernetes), else + * {@code wombat}. Stable across restarts, so that a restarted instance takes its heartbeat back. + */ + @WithName("instance-id") + @WithDefault("${HOSTNAME:wombat}") + String instanceId(); + } } diff --git a/src/main/java/tech/illuin/wombat/engine/module/persistence/CoreDeploymentEntity.java b/src/main/java/tech/illuin/wombat/engine/module/persistence/CoreDeploymentEntity.java new file mode 100644 index 0000000..9839f84 --- /dev/null +++ b/src/main/java/tech/illuin/wombat/engine/module/persistence/CoreDeploymentEntity.java @@ -0,0 +1,67 @@ +package tech.illuin.wombat.engine.module.persistence; + +import jakarta.persistence.Column; +import jakarta.persistence.Convert; +import jakarta.persistence.Entity; +import jakarta.persistence.Id; +import jakarta.persistence.Table; + +import java.nio.charset.StandardCharsets; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; +import java.time.Instant; +import java.util.HexFormat; +import java.util.Set; +import java.util.TreeSet; + +/** + * A core redeploy: the modules every instance builds its core of, as they were enabled when it was requested. Instances + * converge on the latest version. + */ +@Entity +@Table(name = "core_deployments") +public class CoreDeploymentEntity +{ + /** Orders the deployments: a redeploy takes the next one. */ + @Id @Column(nullable = false) + public Long version; + + /** Hex SHA-256 of the version, request time and modules; shortened to 7 characters for display, like a commit id. */ + @Column(nullable = false, unique = true) + public String id; + + @Column(name = "requested_at", nullable = false, columnDefinition = "INTEGER") + public Instant requestedAt; + + /** Null for the deployment of the first startup, or when authentication is disabled. */ + @Column(name = "requested_by") + public String requestedBy; + + @Convert(converter = TypeNameSetConverter.class) + @Column(name = "enabled_modules", nullable = false) + public Set enabledModules; + + public static CoreDeploymentEntity of(long version, Instant requestedAt, String requestedBy, Set enabledModules) + { + CoreDeploymentEntity entity = new CoreDeploymentEntity(); + entity.version = version; + entity.requestedAt = requestedAt; + entity.requestedBy = requestedBy; + entity.enabledModules = new TreeSet<>(enabledModules); + entity.id = id(version, requestedAt, entity.enabledModules); + return entity; + } + + /* The version is part of it, so that two deployments of the same modules still get different ids */ + static String id(long version, Instant requestedAt, Set enabledModules) + { + String content = version + "\n" + requestedAt.toEpochMilli() + "\n" + String.join(",", new TreeSet<>(enabledModules)); + try { + byte[] hash = MessageDigest.getInstance("SHA-256").digest(content.getBytes(StandardCharsets.UTF_8)); + return HexFormat.of().formatHex(hash); + } + catch (NoSuchAlgorithmException e) { + throw new IllegalStateException("SHA-256 is not available", e); + } + } +} diff --git a/src/main/java/tech/illuin/wombat/engine/module/persistence/CoreDeploymentRepository.java b/src/main/java/tech/illuin/wombat/engine/module/persistence/CoreDeploymentRepository.java new file mode 100644 index 0000000..40466cc --- /dev/null +++ b/src/main/java/tech/illuin/wombat/engine/module/persistence/CoreDeploymentRepository.java @@ -0,0 +1,47 @@ +package tech.illuin.wombat.engine.module.persistence; + +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.transaction.Transactional; +import tech.illuin.wombat.engine.context.environment.persistence.EnvironmentEntity; +import tech.illuin.wombat.persistence.backend.api.repository.Repository; + +import java.util.Optional; +import java.util.Set; + +@ApplicationScoped +public class CoreDeploymentRepository extends Repository +{ + public CoreDeploymentRepository() + { + super(CoreDeploymentEntity.class); + } + + @Transactional + public Optional findLatest() + { + return getEntityManager() + .createQuery("FROM CoreDeploymentEntity d ORDER BY d.version DESC", CoreDeploymentEntity.class) + .setMaxResults(1) + .getResultStream() + .findFirst(); + } + + /** + * Records a deployment of {@code enabledModules} under the next version. + * + * @throws jakarta.persistence.PersistenceException if another instance recorded that version in the meantime + */ + @Transactional + public CoreDeploymentEntity insertNext(Set enabledModules, String requestedBy) + { + long latest = getEntityManager() + .createQuery("SELECT COALESCE(MAX(d.version), 0) FROM CoreDeploymentEntity d", Long.class) + .getSingleResult(); + + CoreDeploymentEntity entity = CoreDeploymentEntity.of(latest + 1, EnvironmentEntity.now(), requestedBy, enabledModules); + this.entityManager.persist(entity); + /* Flushed here, so that a concurrent insert surfaces as a PersistenceException rather than at commit */ + this.entityManager.flush(); + return entity; + } +} diff --git a/src/main/java/tech/illuin/wombat/engine/module/persistence/CoreInstanceEntity.java b/src/main/java/tech/illuin/wombat/engine/module/persistence/CoreInstanceEntity.java new file mode 100644 index 0000000..35e9cb1 --- /dev/null +++ b/src/main/java/tech/illuin/wombat/engine/module/persistence/CoreInstanceEntity.java @@ -0,0 +1,55 @@ +package tech.illuin.wombat.engine.module.persistence; + +import jakarta.persistence.Column; +import jakarta.persistence.Convert; +import jakarta.persistence.Entity; +import jakarta.persistence.EnumType; +import jakarta.persistence.Enumerated; +import jakarta.persistence.Id; +import jakarta.persistence.Table; + +import java.time.Instant; +import java.util.Set; + +/** + * The heartbeat of an instance: the core deployment it runs, and the modules it detects. + */ +@Entity +@Table(name = "core_instances") +public class CoreInstanceEntity +{ + @Id @Column(name = "instance_id", nullable = false) + public String instanceId; + + @Column(name = "deployed_version", nullable = false) + public long deployedVersion; + + @Column(name = "deployed_at", nullable = false, columnDefinition = "INTEGER") + public Instant deployedAt; + + /** Whether the latest deployment failed to build here; the instance then keeps running its deployed version. */ + @Enumerated(EnumType.STRING) + @Column(nullable = false, columnDefinition = "TEXT") + public Status status; + + @Column + public String error; + + @Convert(converter = TypeNameSetConverter.class) + @Column(name = "detected_modules", nullable = false) + public Set detectedModules; + + /** The asset-types its deployment requested, but no module detected here provides. */ + @Convert(converter = TypeNameSetConverter.class) + @Column(name = "missing_modules", nullable = false) + public Set missingModules; + + @Column(name = "heartbeat_at", nullable = false, columnDefinition = "INTEGER") + public Instant heartbeatAt; + + public enum Status + { + ok, + failed, + } +} diff --git a/src/main/java/tech/illuin/wombat/engine/module/persistence/CoreInstanceRepository.java b/src/main/java/tech/illuin/wombat/engine/module/persistence/CoreInstanceRepository.java new file mode 100644 index 0000000..c0af5d6 --- /dev/null +++ b/src/main/java/tech/illuin/wombat/engine/module/persistence/CoreInstanceRepository.java @@ -0,0 +1,41 @@ +package tech.illuin.wombat.engine.module.persistence; + +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.transaction.Transactional; +import tech.illuin.wombat.persistence.backend.api.repository.Repository; + +import java.time.Duration; +import java.time.Instant; +import java.util.List; + +@ApplicationScoped +public class CoreInstanceRepository extends Repository +{ + /* An instance which missed this many heartbeats is considered gone */ + private static final int MISSED_HEARTBEATS = 3; + + public CoreInstanceRepository() + { + super(CoreInstanceEntity.class); + } + + @Transactional + public void upsert(CoreInstanceEntity entity) + { + this.entityManager.merge(entity); + } + + /** + * @param pollInterval how often instances write their heartbeat + * @return the instances which wrote a heartbeat lately, ordered by id + */ + @Transactional + public List findLive(Duration pollInterval) + { + Instant since = Instant.now().minus(pollInterval.multipliedBy(MISSED_HEARTBEATS)); + return getEntityManager() + .createQuery("FROM CoreInstanceEntity i WHERE i.heartbeatAt >= :since ORDER BY i.instanceId", CoreInstanceEntity.class) + .setParameter("since", since) + .getResultList(); + } +} diff --git a/src/main/java/tech/illuin/wombat/engine/module/persistence/TypeNameSetConverter.java b/src/main/java/tech/illuin/wombat/engine/module/persistence/TypeNameSetConverter.java new file mode 100644 index 0000000..3d90950 --- /dev/null +++ b/src/main/java/tech/illuin/wombat/engine/module/persistence/TypeNameSetConverter.java @@ -0,0 +1,33 @@ +package tech.illuin.wombat.engine.module.persistence; + +import jakarta.persistence.AttributeConverter; +import jakarta.persistence.Converter; + +import java.util.Arrays; +import java.util.Set; +import java.util.TreeSet; +import java.util.stream.Collectors; + +/** + * Stores asset-type names comma-separated, sorted; a name holds no comma. + */ +@Converter +public class TypeNameSetConverter implements AttributeConverter, String> +{ + @Override + public String convertToDatabaseColumn(Set names) + { + return names.stream() + .sorted() + .collect(Collectors.joining(",")); + } + + @Override + public Set convertToEntityAttribute(String column) + { + if (column == null || column.isBlank()) + return new TreeSet<>(); + return Arrays.stream(column.split(",")) + .collect(Collectors.toCollection(TreeSet::new)); + } +} diff --git a/src/main/java/tech/illuin/wombat/engine/module/registry/DbModuleRegistry.java b/src/main/java/tech/illuin/wombat/engine/module/registry/DbModuleRegistry.java index 82f2a37..be9f751 100644 --- a/src/main/java/tech/illuin/wombat/engine/module/registry/DbModuleRegistry.java +++ b/src/main/java/tech/illuin/wombat/engine/module/registry/DbModuleRegistry.java @@ -12,6 +12,7 @@ import tech.illuin.wombat.commons.WombatProperties; import tech.illuin.wombat.core.asset.type.AssetType; import tech.illuin.wombat.core.module.WombatModule; +import tech.illuin.wombat.engine.module.persistence.CoreInstanceRepository; import tech.illuin.wombat.engine.module.persistence.ModuleEntity; import tech.illuin.wombat.engine.module.persistence.ModuleRepository; import tech.illuin.wombat.engine.module.registry.ModuleMetadata.Source; @@ -20,6 +21,7 @@ import java.nio.file.FileSystemNotFoundException; import java.nio.file.Path; import java.security.CodeSource; +import java.time.Duration; import java.util.List; import java.util.Map; import java.util.Optional; @@ -30,31 +32,35 @@ /** * The modules registered in the {@code modules} table, shared by every instance: a toggle is seen by all of them at - * once. Every module this instance detects is registered at startup; a registered module no instance detects any more - * is missing, and keeps its state should it come back. + * once. Every module this instance detects is registered at startup; a registered module no live instance detects any + * more (see the heartbeats of {@link CoreInstanceRepository}) is missing, and keeps its state should it come back. */ @ApplicationScoped public class DbModuleRegistry implements ModuleRegistry { private final Map detected; private final ModuleRepository repository; + private final CoreInstanceRepository instances; private final Path extensionPath; + private final Duration pollInterval; /* After the persistence setup, and before the environments are reconciled and the core deployed */ static final int STARTUP_PRIORITY_REGISTER = 1900; private static final Logger logger = LoggerFactory.getLogger(DbModuleRegistry.class); @Inject - public DbModuleRegistry(List modules, ModuleRepository repository, WombatProperties wombat) + public DbModuleRegistry(List modules, ModuleRepository repository, CoreInstanceRepository instances, WombatProperties wombat) { - this(modules, repository, wombat.module().extension().path()); + this(modules, repository, instances, wombat.module().extension().path(), wombat.module().deployment().pollInterval()); } - DbModuleRegistry(List modules, ModuleRepository repository, Path extensionPath) + DbModuleRegistry(List modules, ModuleRepository repository, CoreInstanceRepository instances, Path extensionPath, Duration pollInterval) { this.detected = modules.stream().collect(Collectors.toMap(module -> module.type().name(), Function.identity())); this.repository = repository; + this.instances = instances; this.extensionPath = extensionPath.toAbsolutePath().normalize(); + this.pollInterval = pollInterval; } /** @@ -77,7 +83,7 @@ void onStart(@Observes @Priority(STARTUP_PRIORITY_REGISTER) StartupEvent event) } /** - * @return the detected modules, then the missing ones + * @return the modules detected here, then those registered but not detected here */ @Override @Transactional @@ -85,13 +91,14 @@ public List listModules() { Map rows = this.repository.listAll().stream() .collect(Collectors.toMap(entity -> entity.typeName, Function.identity())); + Set detectedElsewhere = this.detectedByLiveInstances(); Stream detected = this.detected.values().stream() .map(module -> this.detected(module, rows.get(module.type().name()))); - Stream missing = rows.values().stream() + Stream others = rows.values().stream() .filter(entity -> !this.detected.containsKey(entity.typeName)) - .map(DbModuleRegistry::missing); - return Stream.concat(detected, missing).toList(); + .map(entity -> notDetectedHere(entity, !detectedElsewhere.contains(entity.typeName))); + return Stream.concat(detected, others).toList(); } @Override @@ -102,7 +109,7 @@ public Optional getModule(String module) WombatModule detected = this.detected.get(module); if (detected != null) return Optional.of(this.detected(detected, entity)); - return Optional.ofNullable(entity).map(DbModuleRegistry::missing); + return Optional.ofNullable(entity).map(this::notDetectedHere); } @Override @@ -111,12 +118,10 @@ public ModuleMetadata setEnabled(String module, boolean enabled, String user) { ModuleEntity entity = this.repository.findById(module); WombatModule detected = this.detected.get(module); - if (detected == null) - { - if (entity == null) - throw new UnknownModuleException(module); + if (detected == null && entity == null) + throw new UnknownModuleException(module); + if (detected == null && this.notDetectedHere(entity).missing()) throw new ModuleConflictException("Module " + module + " is not detected, it cannot be toggled"); - } /* Registered at startup, unless that failed */ if (entity == null) @@ -126,7 +131,7 @@ public ModuleMetadata setEnabled(String module, boolean enabled, String user) } entity.enabled = enabled; entity.updatedBy = user; - return this.detected(detected, entity); + return detected != null ? this.detected(detected, entity) : this.notDetectedHere(entity); } @Override @@ -135,7 +140,7 @@ public void remove(String module) { ModuleEntity entity = this.repository.findByIdOptional(module) .orElseThrow(() -> new UnknownModuleException(module)); - if (this.detected.containsKey(module)) + if (this.detected.containsKey(module) || !this.notDetectedHere(entity).missing()) throw new ModuleConflictException("Module " + module + " is detected, only a missing module can be removed"); this.repository.delete(entity); @@ -152,9 +157,21 @@ private ModuleMetadata detected(WombatModule module, ModuleEntity entity) ); } - private static ModuleMetadata missing(ModuleEntity entity) + private ModuleMetadata notDetectedHere(ModuleEntity entity) + { + return notDetectedHere(entity, !this.detectedByLiveInstances().contains(entity.typeName)); + } + + private static ModuleMetadata notDetectedHere(ModuleEntity entity, boolean missing) + { + return new ModuleMetadata(entity.type(), null, entity.enabled, missing, null); + } + + private Set detectedByLiveInstances() { - return new ModuleMetadata(entity.type(), null, entity.enabled, true, null); + return this.instances.findLive(this.pollInterval).stream() + .flatMap(instance -> instance.detectedModules.stream()) + .collect(Collectors.toSet()); } /** diff --git a/src/main/java/tech/illuin/wombat/engine/module/registry/ModuleMetadata.java b/src/main/java/tech/illuin/wombat/engine/module/registry/ModuleMetadata.java index d680868..81c9ba1 100644 --- a/src/main/java/tech/illuin/wombat/engine/module/registry/ModuleMetadata.java +++ b/src/main/java/tech/illuin/wombat/engine/module/registry/ModuleMetadata.java @@ -8,11 +8,11 @@ /** * A registered module, as the registry knows it. * - * @param type for a missing module, as it advertised it when last detected - * @param assetClass null when the module is missing + * @param type for a module not detected here, as it advertised it when last detected + * @param assetClass null when this instance does not detect the module * @param enabled the registry state, shared by every instance - * @param missing whether the module is registered but no longer detected, e.g. its extension jar was removed - * @param source whether it comes from the extension folder; null when the module is missing + * @param missing whether no live instance detects the module any more, e.g. its extension jar was removed + * @param source whether it comes from the extension folder; null when this instance does not detect the module */ public record ModuleMetadata( AssetType type, @@ -30,21 +30,29 @@ public static ModuleMetadata core(AssetType type, Class assetCl } /** - * @return whether its assets can be processed: the module is enabled, and detected + * @return whether this instance detects the module, and so holds its classes + */ + public boolean detectedHere() + { + return this.assetClass != null; + } + + /** + * @return whether its assets can be processed here: the module is enabled, and detected here */ public boolean active() { - return this.enabled && !this.missing; + return this.enabled && this.detectedHere(); } /** * @return whether assets of this type can be declared in an environment; simulated assets only exist within a - * simulation request, and a missing module takes no asset + * simulation request, and a module not detected here takes no asset */ @JsonProperty("creatable") public boolean creatable() { - return !this.missing && !SimulatedAsset.class.isAssignableFrom(this.assetClass); + return this.detectedHere() && !SimulatedAsset.class.isAssignableFrom(this.assetClass); } public enum Source diff --git a/src/main/java/tech/illuin/wombat/engine/module/registry/schema/DefaultSchemaRegistry.java b/src/main/java/tech/illuin/wombat/engine/module/registry/schema/DefaultSchemaRegistry.java index b99aca4..4c9896f 100644 --- a/src/main/java/tech/illuin/wombat/engine/module/registry/schema/DefaultSchemaRegistry.java +++ b/src/main/java/tech/illuin/wombat/engine/module/registry/schema/DefaultSchemaRegistry.java @@ -17,6 +17,7 @@ import tech.illuin.wombat.core.asset.type.AssetType; import tech.illuin.wombat.core.secret.SecretKey; import tech.illuin.wombat.core.source.Period; +import tech.illuin.wombat.engine.module.registry.ModuleMetadata; import tech.illuin.wombat.engine.module.registry.ModuleRegistry; import java.time.ZoneId; @@ -61,8 +62,8 @@ public DefaultSchemaRegistry(ModuleRegistry moduleRegistry, ObjectMapper objectM public Optional getSchema(AssetType type) { JsonNode node = this.cache.computeIfAbsent(type, k -> this.moduleRegistry.getModule(k) - /* A missing module has no class to describe */ - .filter(module -> !module.missing()) + /* A module not detected here has no class to describe */ + .filter(ModuleMetadata::detectedHere) .map(mm -> withoutIdentity(this.schemaGenerator.generateSchema(mm.assetClass()))) .orElse(null) ); diff --git a/src/main/java/tech/illuin/wombat/engine/module/runtime/CoreDeployment.java b/src/main/java/tech/illuin/wombat/engine/module/runtime/CoreDeployment.java index 1299000..b46f37b 100644 --- a/src/main/java/tech/illuin/wombat/engine/module/runtime/CoreDeployment.java +++ b/src/main/java/tech/illuin/wombat/engine/module/runtime/CoreDeployment.java @@ -13,12 +13,16 @@ /** * The monitor and evaluator built out of one set of modules, swapped as a whole by {@link CoreRuntime}. * + * @param version the deployment's place in the sequence every instance converges on + * @param id the deployment's hash, shown shortened like a commit id * @param modules the module instances wired in, shared with the other deployments * @param missing the requested asset-types that no module detected on this instance provides * @param evaluator the evaluator the app serves, cached when the evaluator cache is enabled * @param uncachedEvaluator the evaluator underneath the cache, or {@code evaluator} itself when the cache is disabled */ public record CoreDeployment( + long version, + String id, List modules, Set missing, AssetMonitor monitor, diff --git a/src/main/java/tech/illuin/wombat/engine/module/runtime/CoreDeploymentFactory.java b/src/main/java/tech/illuin/wombat/engine/module/runtime/CoreDeploymentFactory.java index 560f82b..6803fb0 100644 --- a/src/main/java/tech/illuin/wombat/engine/module/runtime/CoreDeploymentFactory.java +++ b/src/main/java/tech/illuin/wombat/engine/module/runtime/CoreDeploymentFactory.java @@ -67,7 +67,7 @@ public CoreDeploymentFactory( /** * @param requested the names of the asset-types to deploy; those no detected module provides are reported as missing */ - public CoreDeployment create(Set requested) + public CoreDeployment create(long version, String id, Set requested) { List deployed = this.modules.stream() .filter(module -> requested.contains(module.type().name())) @@ -76,7 +76,7 @@ public CoreDeployment create(Set requested) Set missing = new HashSet<>(requested); deployed.forEach(module -> missing.remove(module.type().name())); - /* The core holds nothing but the modules, which are closed by CoreRuntime: it is not kept, nor closed. */ + /* The core holds nothing but the modules, which every deployment shares: it is neither kept nor closed */ WombatCore core = this.createCore(deployed); AssetMonitor monitor = core.createMonitor(); AssetEvaluator uncached = core.createEvaluator(); @@ -84,7 +84,7 @@ public CoreDeployment create(Set requested) ? new CachedAssetEvaluator(uncached, this.cacheProperties) : uncached; - return new CoreDeployment(deployed, missing, monitor, evaluator, uncached, Instant.now()); + return new CoreDeployment(version, id, deployed, missing, monitor, evaluator, uncached, Instant.now()); } private WombatCore createCore(List deployed) diff --git a/src/main/java/tech/illuin/wombat/engine/module/runtime/CoreDeploymentService.java b/src/main/java/tech/illuin/wombat/engine/module/runtime/CoreDeploymentService.java new file mode 100644 index 0000000..93bf5f3 --- /dev/null +++ b/src/main/java/tech/illuin/wombat/engine/module/runtime/CoreDeploymentService.java @@ -0,0 +1,258 @@ +package tech.illuin.wombat.engine.module.runtime; + +import io.quarkus.runtime.StartupEvent; +import io.quarkus.scheduler.Scheduled; +import jakarta.annotation.Priority; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.event.Observes; +import jakarta.inject.Inject; +import jakarta.persistence.PersistenceException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import tech.illuin.wombat.commons.NodeMode; +import tech.illuin.wombat.commons.WombatProperties; +import tech.illuin.wombat.core.module.WombatModule; +import tech.illuin.wombat.engine.context.environment.persistence.EnvironmentEntity; +import tech.illuin.wombat.engine.module.persistence.CoreDeploymentEntity; +import tech.illuin.wombat.engine.module.persistence.CoreDeploymentRepository; +import tech.illuin.wombat.engine.module.persistence.CoreInstanceEntity; +import tech.illuin.wombat.engine.module.persistence.CoreInstanceRepository; +import tech.illuin.wombat.engine.module.registry.ModuleMetadata; +import tech.illuin.wombat.engine.module.registry.ModuleRegistry; + +import java.time.Duration; +import java.util.HashSet; +import java.util.List; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; + +/** + * Decides which deployment the {@link CoreRuntime} runs, the database being the only channel between instances. A + * redeploy records the modules enabled in the registry under the next version; every instance deploys the latest + * version, at startup and on each poll, and writes a heartbeat telling the deployment it runs. + */ +@ApplicationScoped +public class CoreDeploymentService +{ + private final CoreRuntime runtime; + private final ModuleRegistry registry; + private final CoreDeploymentRepository deployments; + private final CoreInstanceRepository instances; + private final Set detected; + private final String instanceId; + private final Duration pollInterval; + private final boolean heartbeat; + /* The latest version that failed to build here: the poll does not retry it, a newer one is needed */ + private volatile Failure failure; + + /* After the modules are registered and the environments reconciled, before the monitor is scheduled */ + static final int STARTUP_PRIORITY_DEPLOY = 2100; + private static final Logger logger = LoggerFactory.getLogger(CoreDeploymentService.class); + + @Inject + public CoreDeploymentService( + CoreRuntime runtime, + ModuleRegistry registry, + CoreDeploymentRepository deployments, + CoreInstanceRepository instances, + List modules, + WombatProperties wombat + ) { + this( + runtime, + registry, + deployments, + instances, + modules, + wombat.module().deployment().instanceId(), + wombat.module().deployment().pollInterval(), + /* A worker has no API nor UI to show its heartbeat, nor replicas to converge with */ + wombat.mode() != NodeMode.worker + ); + } + + CoreDeploymentService( + CoreRuntime runtime, + ModuleRegistry registry, + CoreDeploymentRepository deployments, + CoreInstanceRepository instances, + List modules, + String instanceId, + Duration pollInterval, + boolean heartbeat + ) { + this.runtime = runtime; + this.registry = registry; + this.deployments = deployments; + this.instances = instances; + this.detected = modules.stream().map(module -> module.type().name()).collect(Collectors.toSet()); + this.instanceId = instanceId; + this.pollInterval = pollInterval; + this.heartbeat = heartbeat; + } + + /** + * Deploys the latest deployment; the first instance to start records one out of the registry. A failure here fails + * the startup: there is no core to keep running. + */ + void onStart(@Observes @Priority(STARTUP_PRIORITY_DEPLOY) StartupEvent event) + { + CoreDeploymentEntity latest = this.deployments.findLatest().orElseGet(this::bootstrap); + this.runtime.deploy(latest.version, latest.id, latest.enabledModules); + this.writeHeartbeat(); + } + + /** + * Records a deployment of the modules enabled in the registry, then deploys it here; the other instances follow on + * their next poll. + * + * @param user who requested it, null when authentication is disabled + * @throws DeploymentConflictException if another instance recorded a redeploy at the same time + */ + public Redeploy redeploy(String user) + { + try { + CoreDeploymentEntity next = this.deployments.insertNext(this.enabledModules(), user); + return new Redeploy(next, this.deployHere(next)); + } + catch (PersistenceException e) { + throw new DeploymentConflictException("Another redeploy was just requested, the instances converge on it", e); + } + } + + @Scheduled( + identity = "wombat-core-deployment-sync", + every = "${wombat.module.deployment.poll-interval:30s}", + delayed = "${wombat.module.deployment.poll-interval:30s}", + concurrentExecution = Scheduled.ConcurrentExecution.SKIP + ) + void sync() + { + if (!this.heartbeat) + return; + Failure failed = this.failure; + this.deployments.findLatest() + .filter(latest -> latest.version > this.runtime.current().version()) + .filter(latest -> failed == null || latest.version > failed.version()) + .ifPresent(this::deployHere); + this.writeHeartbeat(); + } + + public Optional latest() + { + return this.deployments.findLatest(); + } + + public Optional find(long version) + { + return this.deployments.findByIdOptional(version); + } + + public List liveInstances() + { + return this.instances.findLive(this.pollInterval); + } + + public String instanceId() + { + return this.instanceId; + } + + public Duration pollInterval() + { + return this.pollInterval; + } + + /** + * The first instance to start records the modules the registry enables; another one starting at the same time may + * win that race, its deployment is then the one taken. + */ + private CoreDeploymentEntity bootstrap() + { + try { + return this.deployments.insertNext(this.enabledModules(), null); + } + catch (PersistenceException e) { + return this.deployments.findLatest().orElseThrow(() -> e); + } + } + + /* A build failure is reported rather than thrown: the running deployment is kept, and the heartbeat tells it */ + @SuppressWarnings("IllegalCatch") + private Result deployHere(CoreDeploymentEntity deployment) + { + Set before = CoreRuntime.names(this.runtime.current().types()); + try { + Set after = CoreRuntime.names(this.runtime.deploy(deployment.version, deployment.id, deployment.enabledModules).types()); + this.failure = null; + return Result.ok(difference(after, before), difference(before, after)); + } + catch (RuntimeException e) { + logger.error("Could not deploy wombat core {} (#{}), keeping the running one: {}", CoreRuntime.shortId(deployment.id), deployment.version, e.getMessage(), e); + this.failure = new Failure(deployment.version, e.getMessage()); + return Result.failed(e.getMessage()); + } + finally { + this.writeHeartbeat(); + } + } + + private void writeHeartbeat() + { + if (!this.heartbeat) + return; + CoreDeployment current = this.runtime.current(); + Failure failed = this.failure; + + CoreInstanceEntity entity = new CoreInstanceEntity(); + entity.instanceId = this.instanceId; + entity.deployedVersion = current.version(); + entity.deployedAt = current.deployedAt(); + entity.status = failed == null ? CoreInstanceEntity.Status.ok : CoreInstanceEntity.Status.failed; + entity.error = failed == null ? null : failed.error(); + entity.detectedModules = this.detected; + entity.missingModules = current.missing(); + entity.heartbeatAt = EnvironmentEntity.now(); + this.instances.upsert(entity); + } + + private Set enabledModules() + { + return this.registry.listModules().stream() + .filter(ModuleMetadata::enabled) + .map(module -> module.type().name()) + .collect(Collectors.toSet()); + } + + private static Set difference(Set from, Set without) + { + Set difference = new HashSet<>(from); + difference.removeAll(without); + return difference; + } + + private record Failure(long version, String error) {} + + /** + * A redeploy recorded, and the outcome of deploying it on the instance which recorded it. + */ + public record Redeploy(CoreDeploymentEntity deployment, Result local) {} + + /** + * @param added the asset-types the new core holds, and the previous one did not + * @param removed the asset-types the previous core held, and the new one does not + */ + public record Result(CoreInstanceEntity.Status status, String error, Set added, Set removed) + { + static Result ok(Set added, Set removed) + { + return new Result(CoreInstanceEntity.Status.ok, null, added, removed); + } + + static Result failed(String error) + { + return new Result(CoreInstanceEntity.Status.failed, error, Set.of(), Set.of()); + } + } +} diff --git a/src/main/java/tech/illuin/wombat/engine/module/runtime/CoreRuntime.java b/src/main/java/tech/illuin/wombat/engine/module/runtime/CoreRuntime.java index 2adc679..5ed4335 100644 --- a/src/main/java/tech/illuin/wombat/engine/module/runtime/CoreRuntime.java +++ b/src/main/java/tech/illuin/wombat/engine/module/runtime/CoreRuntime.java @@ -1,16 +1,11 @@ package tech.illuin.wombat.engine.module.runtime; -import io.quarkus.runtime.StartupEvent; import jakarta.annotation.PreDestroy; -import jakarta.annotation.Priority; import jakarta.enterprise.context.ApplicationScoped; -import jakarta.enterprise.event.Observes; import jakarta.inject.Inject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import tech.illuin.wombat.core.asset.type.AssetType; -import tech.illuin.wombat.engine.module.registry.ModuleMetadata; -import tech.illuin.wombat.engine.module.registry.ModuleRegistry; import java.time.Duration; import java.util.HashSet; @@ -21,7 +16,8 @@ import java.util.stream.Collectors; /** - * Holds the running {@link CoreDeployment}, and swaps it for a new one on redeploy. + * Holds the running {@link CoreDeployment}, and swaps it for a new one on redeploy. Which deployment to run is decided + * by {@link CoreDeploymentService}. *

* The app's {@code AssetMonitor} and {@code AssetEvaluator} beans read {@link #current()} on every call, see * {@link RuntimeAssetMonitor} and {@link RuntimeAssetEvaluator}, so they follow a swap at once. The replaced deployment @@ -31,37 +27,24 @@ public class CoreRuntime { private final CoreDeploymentFactory factory; - private final ModuleRegistry registry; private final Executor retiredCloser; private volatile CoreDeployment current; - /* After the environments are reconciled, and before the monitor is scheduled */ - static final int STARTUP_PRIORITY_DEPLOY = 2100; private static final Duration CLOSE_GRACE_DELAY = Duration.ofSeconds(60); private static final Logger logger = LoggerFactory.getLogger(CoreRuntime.class); @Inject - public CoreRuntime(CoreDeploymentFactory factory, ModuleRegistry registry) + public CoreRuntime(CoreDeploymentFactory factory) { - this(factory, registry, CompletableFuture.delayedExecutor(CLOSE_GRACE_DELAY.toSeconds(), TimeUnit.SECONDS)); + this(factory, CompletableFuture.delayedExecutor(CLOSE_GRACE_DELAY.toSeconds(), TimeUnit.SECONDS)); } - CoreRuntime(CoreDeploymentFactory factory, ModuleRegistry registry, Executor retiredCloser) + CoreRuntime(CoreDeploymentFactory factory, Executor retiredCloser) { this.factory = factory; - this.registry = registry; this.retiredCloser = retiredCloser; } - void onStart(@Observes @Priority(STARTUP_PRIORITY_DEPLOY) StartupEvent event) - { - this.deploy(this.registry.listModules().stream() - .filter(ModuleMetadata::enabled) - .map(module -> module.type().name()) - .collect(Collectors.toSet()) - ); - } - /** * @throws IllegalStateException before the first deployment */ @@ -75,12 +58,21 @@ public CoreDeployment current() /** * Builds a deployment of the requested asset-types and swaps it in. If the build fails, the running deployment is - * kept and the exception is thrown. Deployments are applied one at a time. + * kept and the exception is thrown. Deployments are applied one at a time, and only forward: a version not newer + * than the running one is ignored. + * + * @return the running deployment */ - public synchronized CoreDeployment deploy(Set requested) + public synchronized CoreDeployment deploy(long version, String id, Set requested) { - CoreDeployment next = this.factory.create(requested); CoreDeployment previous = this.current; + if (previous != null && version <= previous.version()) + { + logger.debug("Already running core deployment {}, ignoring deployment {}", previous.version(), version); + return previous; + } + + CoreDeployment next = this.factory.create(version, id, requested); this.current = next; if (previous != null) @@ -108,15 +100,23 @@ private void logChanges(CoreDeployment previous, CoreDeployment next) Set removed = new HashSet<>(before); removed.removeAll(after); - logger.info("Deployed wombat core with {} module(s), added {}, removed {}", after.size(), added, removed); + logger.info("Deployed wombat core {} (#{}) with {} module(s), added {}, removed {}", shortId(next.id()), next.version(), after.size(), added, removed); if (!next.missing().isEmpty()) logger.warn("Requested asset-types with no module on this instance: {}", next.missing()); } - private static Set names(Set types) + static Set names(Set types) { return types.stream() .map(AssetType::name) .collect(Collectors.toSet()); } + + /** + * @return the first 7 characters of a deployment id, like a short commit id + */ + public static String shortId(String id) + { + return id.length() > 7 ? id.substring(0, 7) : id; + } } diff --git a/src/main/java/tech/illuin/wombat/engine/module/runtime/DeploymentConflictException.java b/src/main/java/tech/illuin/wombat/engine/module/runtime/DeploymentConflictException.java new file mode 100644 index 0000000..3f5126c --- /dev/null +++ b/src/main/java/tech/illuin/wombat/engine/module/runtime/DeploymentConflictException.java @@ -0,0 +1,12 @@ +package tech.illuin.wombat.engine.module.runtime; + +/** + * A redeploy requested at the same time as another one, by another instance. + */ +public class DeploymentConflictException extends RuntimeException +{ + public DeploymentConflictException(String message, Throwable cause) + { + super(message, cause); + } +} diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml index a42014c..134fe2f 100644 --- a/src/main/resources/application.yaml +++ b/src/main/resources/application.yaml @@ -132,9 +132,14 @@ wombat: expire-after-write: 10m module: - # Modules are enabled or disabled from Settings > Modules, no longer here + # Modules are enabled or disabled from Settings > Modules extension: path: "extensions" + deployment: + # How often an instance deploys the latest core redeploy, and writes its heartbeat; an instance missing 3 is gone + poll-interval: 30s + # Identifies this instance among the replicas: the pod name in Kubernetes, "wombat" when HOSTNAME is unset + instance-id: ${HOSTNAME:wombat} ## Secret Resolution Configuration # Write mode defaults to "disabled". diff --git a/src/test/java/tech/illuin/wombat/endpoint/ModuleControllerTest.java b/src/test/java/tech/illuin/wombat/endpoint/ModuleControllerTest.java index 8822229..085b940 100644 --- a/src/test/java/tech/illuin/wombat/endpoint/ModuleControllerTest.java +++ b/src/test/java/tech/illuin/wombat/endpoint/ModuleControllerTest.java @@ -21,6 +21,8 @@ import tech.illuin.wombat.engine.module.registry.ModuleRegistry; import tech.illuin.wombat.engine.module.persistence.ModuleEntity; import tech.illuin.wombat.engine.module.persistence.ModuleRepository; +import tech.illuin.wombat.engine.module.runtime.CoreDeploymentService; +import tech.illuin.wombat.engine.module.runtime.CoreRuntime; import tech.illuin.wombat.engine.module.registry.schema.AssetSchemaRegistry; import tech.illuin.wombat.security.Caller; import tech.illuin.wombat.security.Permission; @@ -38,6 +40,8 @@ import java.util.UUID; import static io.restassured.RestAssured.given; +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.notNullValue; @@ -60,13 +64,22 @@ class ModuleControllerTest @Inject ApiKeyService apiKeys; + @Inject + CoreRuntime runtime; + + @Inject + CoreDeploymentService deployments; + + /* The registry and the core are shared by the tests of this profile: a test changing them restores them */ @AfterEach - void restoreTheRegistry() + void restoreTheRegistryAndTheCore() { QuarkusTransaction.requiringNew().run(() -> { this.repository.findById(MODULE_ID).enabled = true; this.repository.deleteById(MISSING_ID); }); + if (!this.runtime.current().types().contains(KubernetesSimulatedModule.TYPE)) + this.deployments.redeploy(null); } @Test @@ -180,6 +193,66 @@ void writesAreForbiddenToUsers() given().when().delete("/api/modules/" + MODULE_ID).then().statusCode(403); } + @Test + @TestSecurity(user = "module-reader", roles = "wombat-users", augmentors = PermissionAugmentor.class) + void deployment_tellsTheLatestDeployment_andTheInstancesRunningIt() + { + given() + .when().get("/api/modules/deployment") + .then() + .statusCode(200) + .body("payload.latest.version", notNullValue()) + .body("payload.latest.id.length()", is(64)) + .body("payload.latest.modules", hasItem(MODULE_ID)) + .body("payload.instances.find { it.current }.status", is("ok")) + .body("payload.instances.find { it.current }.detectedModules", hasItem(MODULE_ID)) + .body("payload.pending", empty()) + .body("payload.pollIntervalSeconds", is(30)); + } + + @Test + @TestSecurity(user = "boss", roles = "wombat-admins", augmentors = PermissionAugmentor.class) + void aToggleIsPendingUntilARedeploy_whichEveryInstanceThenRuns() + { + long before = this.runtime.current().version(); + given().contentType(ContentType.JSON).body("{\"enabled\": false}").when().put("/api/modules/" + MODULE_ID).then().statusCode(200); + given() + .when().get("/api/modules/deployment") + .then() + .statusCode(200) + .body("payload.pending.type", contains(MODULE_ID)); + + given() + .when().post("/api/modules/deployment") + .then() + .statusCode(201) + .body("payload.version", is((int) before + 1)) + .body("payload.id.length()", is(64)) + .body("payload.local.status", is("ok")) + .body("payload.local.removed", contains(MODULE_ID)); + + given() + .when().get("/api/modules/" + MODULE_ID) + .then() + .statusCode(200) + .body("payload.enabled", is(false)) + .body("payload.deployed", is(false)) + .body("payload.pending", is(false)); + given() + .when().get("/api/modules/deployment") + .then() + .statusCode(200) + .body("payload.pending", empty()) + .body("payload.instances.find { it.current }.deployedVersion", is((int) before + 1)); + } + + @Test + @TestSecurity(user = "module-reader", roles = "wombat-users", augmentors = PermissionAugmentor.class) + void aRedeployIsForbiddenToUsers() + { + given().when().post("/api/modules/deployment").then().statusCode(403); + } + @Test void aKey_togglesModulesOnlyWithTheManagePermission() { diff --git a/src/test/java/tech/illuin/wombat/engine/module/ModuleServiceTest.java b/src/test/java/tech/illuin/wombat/engine/module/ModuleServiceTest.java index d5d8e99..a5678e0 100644 --- a/src/test/java/tech/illuin/wombat/engine/module/ModuleServiceTest.java +++ b/src/test/java/tech/illuin/wombat/engine/module/ModuleServiceTest.java @@ -11,12 +11,15 @@ import tech.illuin.wombat.engine.context.asset.persistence.AssetRepository; import tech.illuin.wombat.engine.module.library.kubernetes_simulated.KubernetesSimulatedModule; import tech.illuin.wombat.engine.module.library.llm_simulated.LLMSimulatedModule; +import tech.illuin.wombat.engine.module.persistence.CoreDeploymentEntity; import tech.illuin.wombat.engine.module.registry.ModuleMetadata; import tech.illuin.wombat.engine.module.registry.ModuleRegistry; import tech.illuin.wombat.engine.module.registry.UnknownModuleException; import tech.illuin.wombat.engine.module.runtime.CoreDeployment; +import tech.illuin.wombat.engine.module.runtime.CoreDeploymentService; import tech.illuin.wombat.engine.module.runtime.CoreRuntime; +import java.time.Duration; import java.time.Instant; import java.util.List; import java.util.Map; @@ -44,6 +47,7 @@ class ModuleServiceTest private ModuleRegistry registry; private AssetRepository assets; private CoreRuntime runtime; + private CoreDeploymentService deployments; private ModuleService service; @BeforeEach @@ -52,14 +56,16 @@ void setUp() this.registry = mock(ModuleRegistry.class); this.assets = mock(AssetRepository.class); this.runtime = mock(CoreRuntime.class); - this.service = new ModuleService(this.registry, this.assets, this.runtime); + this.deployments = mock(CoreDeploymentService.class); + this.service = new ModuleService(this.registry, this.assets, this.runtime, this.deployments); + this.latest(K8S, LLM); when(this.assets.countVisibleByType()).thenReturn(Map.of(K8S, 3L)); when(this.registry.getModule(any(String.class))).thenReturn(Optional.empty()); } @Test - void aModuleDisabledButStillDeployedIsPending() + void aModuleDisabledSinceTheLatestDeploymentIsPending() { this.registered(detected(K8S_MODULE, true), detected(LLM_MODULE, false)); this.deployed(K8S_MODULE, LLM_MODULE); @@ -72,7 +78,7 @@ void aModuleDisabledButStillDeployedIsPending() assertFalse(modules.get(LLM).enabled()); assertTrue(modules.get(LLM).deployed()); - assertTrue(modules.get(LLM).pending(), "disabled in the registry, still in the core until a redeploy"); + assertTrue(modules.get(LLM).pending(), "disabled in the registry, still in the latest deployment until a redeploy"); assertEquals(0, modules.get(LLM).assetCount()); } @@ -89,6 +95,19 @@ void aMissingModuleIsNeverPending_sinceARedeployCannotBringItIn() assertFalse(module.pending()); } + @Test + void pendingComparesWithTheLatestDeployment_notWithTheLocalCore() + { + this.registered(detected(K8S_MODULE, true)); + this.latest(K8S); + this.deployed(); + + ModuleInfo module = this.service.list().getFirst(); + + assertFalse(module.deployed(), "this instance lags behind"); + assertFalse(module.pending(), "the latest deployment holds it: no redeploy is needed"); + } + @Test void list_putsTheDetectedModulesFirst_thenTheMissingOnes() { @@ -118,6 +137,26 @@ void find_onAnUnknownModuleIsNotFound() assertThrows(UnknownModuleException.class, () -> this.service.find("acme.unknown")); } + @Test + void deployment_listsThePendingModules() + { + this.registered(detected(K8S_MODULE, false), detected(LLM_MODULE, true), missing(REMOVED)); + this.latest(K8S); + when(this.deployments.liveInstances()).thenReturn(List.of()); + when(this.deployments.pollInterval()).thenReturn(Duration.ofSeconds(30)); + + DeploymentInfo deployment = this.service.deployment(); + + assertEquals(List.of(new DeploymentInfo.PendingModule(K8S, false), new DeploymentInfo.PendingModule(LLM, true)), deployment.pending()); + assertEquals(1, deployment.latest().version()); + assertEquals(30, deployment.pollIntervalSeconds()); + } + + private void latest(String... enabled) + { + when(this.deployments.latest()).thenReturn(Optional.of(CoreDeploymentEntity.of(1, Instant.now(), null, Set.of(enabled)))); + } + private void registered(ModuleMetadata... modules) { when(this.registry.listModules()).thenReturn(List.of(modules)); @@ -126,7 +165,7 @@ private void registered(ModuleMetadata... modules) private void deployed(WombatModule... modules) { AssetEvaluator evaluator = mock(AssetEvaluator.class); - when(this.runtime.current()).thenReturn(new CoreDeployment(List.of(modules), Set.of(), mock(AssetMonitor.class), evaluator, evaluator, Instant.now())); + when(this.runtime.current()).thenReturn(new CoreDeployment(1, "id", List.of(modules), Set.of(), mock(AssetMonitor.class), evaluator, evaluator, Instant.now())); } private static ModuleMetadata detected(WombatModule module, boolean enabled) diff --git a/src/test/java/tech/illuin/wombat/engine/module/WombatModuleConfigTest.java b/src/test/java/tech/illuin/wombat/engine/module/WombatModuleConfigTest.java index d8d6811..1a05c4a 100644 --- a/src/test/java/tech/illuin/wombat/engine/module/WombatModuleConfigTest.java +++ b/src/test/java/tech/illuin/wombat/engine/module/WombatModuleConfigTest.java @@ -200,6 +200,7 @@ public WombatModuleProperties module() { @Override public WombatModuleProperties.ExtensionProperties extension() { return () -> Path.of("extensions"); } + @Override public WombatModuleProperties.DeploymentProperties deployment() { return null; } }; } diff --git a/src/test/java/tech/illuin/wombat/engine/module/persistence/CoreDeploymentEntityTest.java b/src/test/java/tech/illuin/wombat/engine/module/persistence/CoreDeploymentEntityTest.java new file mode 100644 index 0000000..7e620b9 --- /dev/null +++ b/src/test/java/tech/illuin/wombat/engine/module/persistence/CoreDeploymentEntityTest.java @@ -0,0 +1,42 @@ +package tech.illuin.wombat.engine.module.persistence; + +import org.junit.jupiter.api.Test; + +import java.time.Instant; +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class CoreDeploymentEntityTest +{ + private static final Instant REQUESTED_AT = Instant.parse("2026-11-20T14:02:00Z"); + + @Test + void theIdIsAHexSha256() + { + String id = CoreDeploymentEntity.of(7, REQUESTED_AT, "alice", Set.of("a.b")).id; + + assertEquals(64, id.length()); + assertTrue(id.matches("[0-9a-f]+"), id); + } + + @Test + void theSameInputsGiveTheSameId_whateverTheModuleOrder() + { + assertEquals( + CoreDeploymentEntity.of(7, REQUESTED_AT, "alice", Set.of("a.b", "c.d")).id, + CoreDeploymentEntity.of(7, REQUESTED_AT, "bob", Set.of("c.d", "a.b")).id + ); + } + + @Test + void twoDeploymentsOfTheSameModulesGetDifferentIds() + { + assertNotEquals( + CoreDeploymentEntity.of(7, REQUESTED_AT, null, Set.of("a.b")).id, + CoreDeploymentEntity.of(8, REQUESTED_AT, null, Set.of("a.b")).id + ); + } +} diff --git a/src/test/java/tech/illuin/wombat/engine/module/persistence/CoreDeploymentRepositoryTest.java b/src/test/java/tech/illuin/wombat/engine/module/persistence/CoreDeploymentRepositoryTest.java new file mode 100644 index 0000000..6648c9f --- /dev/null +++ b/src/test/java/tech/illuin/wombat/engine/module/persistence/CoreDeploymentRepositoryTest.java @@ -0,0 +1,78 @@ +package tech.illuin.wombat.engine.module.persistence; + +import io.quarkus.narayana.jta.QuarkusTransaction; +import io.quarkus.test.junit.QuarkusTest; +import jakarta.inject.Inject; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.Test; + +import java.time.Duration; +import java.time.Instant; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +@QuarkusTest +class CoreDeploymentRepositoryTest +{ + @Inject + CoreDeploymentRepository deployments; + + @Inject + CoreInstanceRepository instances; + + private final List inserted = new ArrayList<>(); + + @AfterEach + void tearDown() + { + QuarkusTransaction.requiringNew().run(() -> { + this.inserted.forEach(this.deployments::deleteById); + this.instances.deleteById("stale"); + }); + } + + @Test + void theStartupRecordedAFirstDeployment() + { + assertTrue(this.deployments.findLatest().isPresent()); + } + + @Test + void insertNext_takesTheNextVersion_andKeepsTheModules() + { + long latest = this.deployments.findLatest().orElseThrow().version; + + CoreDeploymentEntity next = this.deployments.insertNext(Set.of("a.b", "c.d"), "alice"); + this.inserted.add(next.version); + + assertEquals(latest + 1, next.version); + CoreDeploymentEntity read = this.deployments.findLatest().orElseThrow(); + assertEquals(next.id, read.id); + assertEquals(Set.of("a.b", "c.d"), read.enabledModules); + assertEquals("alice", read.requestedBy); + } + + @Test + void findLive_leavesOutTheInstancesWhichMissedThreeHeartbeats() + { + CoreInstanceEntity stale = new CoreInstanceEntity(); + stale.instanceId = "stale"; + stale.deployedVersion = 1; + stale.deployedAt = Instant.now().minus(Duration.ofHours(1)); + stale.status = CoreInstanceEntity.Status.ok; + stale.detectedModules = Set.of("a.b"); + stale.missingModules = Set.of(); + stale.heartbeatAt = Instant.now().minus(Duration.ofSeconds(91)); + this.instances.upsert(stale); + + List live = this.instances.findLive(Duration.ofSeconds(30)); + + assertFalse(live.stream().anyMatch(instance -> instance.instanceId.equals("stale"))); + assertFalse(live.isEmpty(), "this instance wrote its heartbeat at startup"); + } +} diff --git a/src/test/java/tech/illuin/wombat/engine/module/registry/DbModuleRegistryTest.java b/src/test/java/tech/illuin/wombat/engine/module/registry/DbModuleRegistryTest.java index d8d1969..83d8b0c 100644 --- a/src/test/java/tech/illuin/wombat/engine/module/registry/DbModuleRegistryTest.java +++ b/src/test/java/tech/illuin/wombat/engine/module/registry/DbModuleRegistryTest.java @@ -8,10 +8,13 @@ import tech.illuin.wombat.core.asset.type.ServiceFamily; import tech.illuin.wombat.engine.module.library.kubernetes_simulated.KubernetesSimulatedModule; import tech.illuin.wombat.engine.module.library.llm_simulated.LLMSimulatedModule; +import tech.illuin.wombat.engine.module.persistence.CoreInstanceEntity; +import tech.illuin.wombat.engine.module.persistence.CoreInstanceRepository; import tech.illuin.wombat.engine.module.persistence.ModuleEntity; import tech.illuin.wombat.engine.module.persistence.ModuleRepository; import java.nio.file.Path; +import java.time.Duration; import java.util.List; import java.util.Map; import java.util.Optional; @@ -38,14 +41,23 @@ class DbModuleRegistryTest private static final AssetType REMOVED = AssetType.of("acme", "removed", ActivityRegime.MEASURED, ServiceFamily.LLM); private ModuleRepository repository; + private CoreInstanceRepository instances; private DbModuleRegistry registry; @BeforeEach void setUp() { this.repository = mock(ModuleRepository.class); - this.registry = new DbModuleRegistry(List.of(new KubernetesSimulatedModule(), new LLMSimulatedModule()), this.repository, Path.of("extensions")); + this.instances = mock(CoreInstanceRepository.class); + this.registry = new DbModuleRegistry( + List.of(new KubernetesSimulatedModule(), new LLMSimulatedModule()), + this.repository, + this.instances, + Path.of("extensions"), + Duration.ofSeconds(30) + ); when(this.repository.findByIdOptional(any())).thenReturn(Optional.empty()); + when(this.instances.findLive(any())).thenReturn(List.of()); } @Test @@ -95,6 +107,32 @@ void aRegisteredModuleNoLongerDetectedIsListedMissing_withItsAdvertisedType() assertNull(missing.source()); } + @Test + void aModuleDetectedByAnotherLiveInstanceOnly_isNotMissing_butNotActiveHere() + { + when(this.repository.listAll()).thenReturn(List.of(row(REMOVED, true))); + when(this.instances.findLive(any())).thenReturn(List.of(instance(REMOVED.name()))); + + ModuleMetadata elsewhere = this.registry.listModules().getLast(); + + assertFalse(elsewhere.missing()); + assertFalse(elsewhere.detectedHere()); + assertFalse(elsewhere.active()); + assertFalse(elsewhere.creatable()); + } + + @Test + void aModuleDetectedByAnotherLiveInstance_canBeToggled_butNotRemoved() + { + ModuleEntity row = row(REMOVED, true); + when(this.repository.findById(REMOVED.name())).thenReturn(row); + when(this.repository.findByIdOptional(REMOVED.name())).thenReturn(Optional.of(row)); + when(this.instances.findLive(any())).thenReturn(List.of(instance(REMOVED.name()))); + + assertFalse(this.registry.setEnabled(REMOVED.name(), false, null).enabled()); + assertThrows(ModuleConflictException.class, () -> this.registry.remove(REMOVED.name())); + } + @Test void getModule_returnsDetectedAndMissingModules() { @@ -176,6 +214,14 @@ void remove_onAnUnknownModuleIsNotFound() assertThrows(UnknownModuleException.class, () -> this.registry.remove("acme.unknown")); } + private static CoreInstanceEntity instance(String... detected) + { + CoreInstanceEntity instance = new CoreInstanceEntity(); + instance.instanceId = "other"; + instance.detectedModules = Set.of(detected); + return instance; + } + private static ModuleEntity row(AssetType type, boolean enabled) { ModuleEntity entity = ModuleEntity.registered(type); diff --git a/src/test/java/tech/illuin/wombat/engine/module/runtime/CoreDeploymentFactoryTest.java b/src/test/java/tech/illuin/wombat/engine/module/runtime/CoreDeploymentFactoryTest.java index 663c72f..b3bde1d 100644 --- a/src/test/java/tech/illuin/wombat/engine/module/runtime/CoreDeploymentFactoryTest.java +++ b/src/test/java/tech/illuin/wombat/engine/module/runtime/CoreDeploymentFactoryTest.java @@ -27,7 +27,7 @@ class CoreDeploymentFactoryTest @Test void onlyTheRequestedModulesAreDeployed() { - CoreDeployment deployment = factory(true).create(Set.of(LLM)); + CoreDeployment deployment = factory(true).create(1, "id", Set.of(LLM)); assertEquals(Set.of(LLMSimulatedModule.TYPE), deployment.types()); assertTrue(deployment.missing().isEmpty()); @@ -37,7 +37,7 @@ void onlyTheRequestedModulesAreDeployed() @Test void aRequestedTypeWithNoDetectedModuleIsReportedMissing() { - CoreDeployment deployment = factory(true).create(Set.of(K8S, "acme.unknown")); + CoreDeployment deployment = factory(true).create(1, "id", Set.of(K8S, "acme.unknown")); assertEquals(Set.of(KubernetesSimulatedModule.TYPE), deployment.types()); assertEquals(Set.of("acme.unknown"), deployment.missing()); @@ -47,7 +47,7 @@ void aRequestedTypeWithNoDetectedModuleIsReportedMissing() @Test void theCachedEvaluatorWrapsTheUncachedOneWhenTheCacheIsEnabled() { - CoreDeployment deployment = factory(true).create(Set.of(K8S, LLM)); + CoreDeployment deployment = factory(true).create(1, "id", Set.of(K8S, LLM)); assertInstanceOf(CachedAssetEvaluator.class, deployment.evaluator()); assertNotSame(deployment.evaluator(), deployment.uncachedEvaluator()); @@ -57,7 +57,7 @@ void theCachedEvaluatorWrapsTheUncachedOneWhenTheCacheIsEnabled() @Test void bothEvaluatorsAreTheSameWhenTheCacheIsDisabled() { - CoreDeployment deployment = factory(false).create(Set.of(K8S, LLM)); + CoreDeployment deployment = factory(false).create(1, "id", Set.of(K8S, LLM)); assertSame(deployment.evaluator(), deployment.uncachedEvaluator()); deployment.close(); @@ -67,8 +67,8 @@ void bothEvaluatorsAreTheSameWhenTheCacheIsDisabled() void eachDeploymentGetsItsOwnMonitorAndEvaluatorButSharesTheModules() { CoreDeploymentFactory factory = factory(true); - CoreDeployment first = factory.create(Set.of(K8S)); - CoreDeployment second = factory.create(Set.of(K8S)); + CoreDeployment first = factory.create(1, "id", Set.of(K8S)); + CoreDeployment second = factory.create(1, "id", Set.of(K8S)); assertNotSame(first.monitor(), second.monitor()); assertNotSame(first.evaluator(), second.evaluator()); diff --git a/src/test/java/tech/illuin/wombat/engine/module/runtime/CoreDeploymentServiceTest.java b/src/test/java/tech/illuin/wombat/engine/module/runtime/CoreDeploymentServiceTest.java new file mode 100644 index 0000000..dc2c779 --- /dev/null +++ b/src/test/java/tech/illuin/wombat/engine/module/runtime/CoreDeploymentServiceTest.java @@ -0,0 +1,232 @@ +package tech.illuin.wombat.engine.module.runtime; + +import jakarta.persistence.PersistenceException; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; +import tech.illuin.wombat.core.evaluation.AssetEvaluator; +import tech.illuin.wombat.core.module.WombatModule; +import tech.illuin.wombat.core.source.AssetMonitor; +import tech.illuin.wombat.engine.module.library.kubernetes_simulated.KubernetesSimulatedModule; +import tech.illuin.wombat.engine.module.library.llm_simulated.LLMSimulatedModule; +import tech.illuin.wombat.engine.module.persistence.CoreDeploymentEntity; +import tech.illuin.wombat.engine.module.persistence.CoreDeploymentRepository; +import tech.illuin.wombat.engine.module.persistence.CoreInstanceEntity; +import tech.illuin.wombat.engine.module.persistence.CoreInstanceRepository; +import tech.illuin.wombat.engine.module.registry.InMemoryModuleRegistry; + +import java.time.Duration; +import java.time.Instant; +import java.util.List; +import java.util.Optional; +import java.util.Set; +import java.util.stream.Collectors; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.ArgumentMatchers.isNull; +import static org.mockito.Mockito.atLeastOnce; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +class CoreDeploymentServiceTest +{ + private static final String K8S = KubernetesSimulatedModule.TYPE.name(); + private static final String LLM = LLMSimulatedModule.TYPE.name(); + + private CoreDeploymentFactory factory; + private CoreRuntime runtime; + private InMemoryModuleRegistry registry; + private CoreDeploymentRepository deployments; + private CoreInstanceRepository instances; + + @BeforeEach + void setUp() + { + this.factory = mock(CoreDeploymentFactory.class); + /* Builds a deployment holding the requested modules, under the requested version */ + when(this.factory.create(anyLong(), anyString(), any())).thenAnswer(call -> deployment(call.getArgument(0), call.getArgument(2))); + this.runtime = new CoreRuntime(this.factory, Runnable::run); + this.registry = new InMemoryModuleRegistry(); + this.registry.registerModule(new KubernetesSimulatedModule(), true); + this.registry.registerModule(new LLMSimulatedModule(), false); + this.deployments = mock(CoreDeploymentRepository.class); + this.instances = mock(CoreInstanceRepository.class); + } + + @Test + void startupDeploysTheLatestDeployment_notTheRegistry() + { + when(this.deployments.findLatest()).thenReturn(Optional.of(recorded(4, K8S, LLM))); + + this.service(true).onStart(null); + + assertEquals(4, this.runtime.current().version()); + assertEquals(Set.of(K8S, LLM), CoreRuntime.names(this.runtime.current().types()), "the registry disables LLM, but no redeploy applied it"); + } + + @Test + void theFirstStartupRecordsTheModulesTheRegistryEnables() + { + when(this.deployments.findLatest()).thenReturn(Optional.empty()); + when(this.deployments.insertNext(Set.of(K8S), null)).thenReturn(recorded(1, K8S)); + + this.service(true).onStart(null); + + assertEquals(1, this.runtime.current().version()); + verify(this.deployments).insertNext(Set.of(K8S), null); + } + + @Test + void aStartupRacingAnotherTakesTheDeploymentItRecorded() + { + when(this.deployments.findLatest()).thenReturn(Optional.empty(), Optional.of(recorded(1, K8S, LLM))); + when(this.deployments.insertNext(any(), isNull())).thenThrow(new PersistenceException("duplicate version")); + + this.service(true).onStart(null); + + assertEquals(Set.of(K8S, LLM), CoreRuntime.names(this.runtime.current().types())); + } + + @Test + void aRedeployRecordsTheEnabledModulesAndDeploysThemHere() + { + CoreDeploymentService service = this.started(recorded(1, K8S, LLM)); + when(this.deployments.insertNext(Set.of(K8S), "alice")).thenReturn(recorded(2, K8S)); + + CoreDeploymentService.Redeploy redeploy = service.redeploy("alice"); + + assertEquals(2, this.runtime.current().version()); + assertEquals(CoreInstanceEntity.Status.ok, redeploy.local().status()); + assertEquals(Set.of(), redeploy.local().added()); + assertEquals(Set.of(LLM), redeploy.local().removed()); + } + + @Test + void aRedeployRecordedAtTheSameTimeAsAnotherIsAConflict() + { + CoreDeploymentService service = this.started(recorded(1, K8S)); + when(this.deployments.insertNext(any(), any())).thenThrow(new PersistenceException("duplicate version")); + + assertThrows(DeploymentConflictException.class, () -> service.redeploy("alice")); + } + + @Test + void aFailedBuildIsReported_keepsTheRunningCore_andShowsInTheHeartbeat() + { + CoreDeploymentService service = this.started(recorded(1, K8S)); + when(this.deployments.insertNext(any(), any())).thenReturn(recorded(2, K8S)); + doThrow(new IllegalStateException("broken module")).when(this.factory).create(eq(2L), anyString(), any()); + + CoreDeploymentService.Redeploy redeploy = service.redeploy("alice"); + + assertEquals(CoreInstanceEntity.Status.failed, redeploy.local().status()); + assertEquals("broken module", redeploy.local().error()); + assertEquals(1, this.runtime.current().version()); + CoreInstanceEntity heartbeat = this.lastHeartbeat(); + assertEquals(CoreInstanceEntity.Status.failed, heartbeat.status); + assertEquals(1, heartbeat.deployedVersion); + } + + @Test + void aPollDeploysTheLatestDeploymentWhenBehind() + { + CoreDeploymentService service = this.started(recorded(1, K8S)); + when(this.deployments.findLatest()).thenReturn(Optional.of(recorded(3, K8S, LLM))); + + service.sync(); + + assertEquals(3, this.runtime.current().version()); + assertEquals(3, this.lastHeartbeat().deployedVersion); + } + + @Test + void aPollDoesNotRetryAFailedVersion_butANewerOne() + { + CoreDeploymentService service = this.started(recorded(1, K8S)); + doThrow(new IllegalStateException("broken module")).when(this.factory).create(eq(2L), anyString(), any()); + when(this.deployments.findLatest()).thenReturn(Optional.of(recorded(2, K8S))); + + service.sync(); + service.sync(); + verify(this.factory, times(1)).create(eq(2L), anyString(), any()); + + when(this.deployments.findLatest()).thenReturn(Optional.of(recorded(3, K8S))); + service.sync(); + assertEquals(3, this.runtime.current().version()); + assertEquals(CoreInstanceEntity.Status.ok, this.lastHeartbeat().status); + assertNull(this.lastHeartbeat().error); + } + + @Test + void theHeartbeatTellsTheDeployedVersion_theDetectedModules_andTheMissingOnes() + { + this.started(recorded(1, K8S, "acme.removed")); + + CoreInstanceEntity heartbeat = this.lastHeartbeat(); + + assertEquals("instance-a", heartbeat.instanceId); + assertEquals(1, heartbeat.deployedVersion); + assertEquals(Set.of(K8S, LLM), heartbeat.detectedModules); + assertEquals(Set.of("acme.removed"), heartbeat.missingModules); + } + + @Test + void aWorkerDeploys_butNeitherPollsNorWritesAHeartbeat() + { + when(this.deployments.findLatest()).thenReturn(Optional.of(recorded(1, K8S))); + CoreDeploymentService service = this.service(false); + + service.onStart(null); + service.sync(); + + assertEquals(1, this.runtime.current().version()); + verify(this.instances, never()).upsert(any()); + verify(this.deployments, times(1)).findLatest(); + } + + private CoreDeploymentService started(CoreDeploymentEntity latest) + { + when(this.deployments.findLatest()).thenReturn(Optional.of(latest)); + CoreDeploymentService service = this.service(true); + service.onStart(null); + return service; + } + + private CoreDeploymentService service(boolean heartbeat) + { + List modules = List.of(new KubernetesSimulatedModule(), new LLMSimulatedModule()); + return new CoreDeploymentService(this.runtime, this.registry, this.deployments, this.instances, modules, "instance-a", Duration.ofSeconds(30), heartbeat); + } + + private CoreInstanceEntity lastHeartbeat() + { + ArgumentCaptor heartbeats = ArgumentCaptor.forClass(CoreInstanceEntity.class); + verify(this.instances, atLeastOnce()).upsert(heartbeats.capture()); + return heartbeats.getValue(); + } + + private static CoreDeploymentEntity recorded(long version, String... modules) + { + return CoreDeploymentEntity.of(version, Instant.now(), null, Set.of(modules)); + } + + /* Holds the requested modules among those of this test; the others are reported missing */ + private static CoreDeployment deployment(long version, Set requested) + { + List known = List.of(new KubernetesSimulatedModule(), new LLMSimulatedModule()); + List deployed = known.stream().filter(module -> requested.contains(module.type().name())).toList(); + Set missing = requested.stream().filter(name -> deployed.stream().noneMatch(module -> module.type().name().equals(name))).collect(Collectors.toSet()); + AssetEvaluator evaluator = mock(AssetEvaluator.class); + return new CoreDeployment(version, "id-" + version, deployed, missing, mock(AssetMonitor.class), evaluator, evaluator, Instant.now()); + } +} diff --git a/src/test/java/tech/illuin/wombat/engine/module/runtime/CoreRuntimeTest.java b/src/test/java/tech/illuin/wombat/engine/module/runtime/CoreRuntimeTest.java index 15bc419..848faa2 100644 --- a/src/test/java/tech/illuin/wombat/engine/module/runtime/CoreRuntimeTest.java +++ b/src/test/java/tech/illuin/wombat/engine/module/runtime/CoreRuntimeTest.java @@ -6,27 +6,28 @@ import tech.illuin.wombat.core.module.WombatModule; import tech.illuin.wombat.core.source.AssetMonitor; import tech.illuin.wombat.engine.module.library.kubernetes_simulated.KubernetesSimulatedModule; -import tech.illuin.wombat.engine.module.library.llm_simulated.LLMSimulatedModule; -import tech.illuin.wombat.engine.module.registry.InMemoryModuleRegistry; import java.time.Instant; import java.util.ArrayList; import java.util.List; import java.util.Set; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; +import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; class CoreRuntimeTest { private CoreDeploymentFactory factory; - private InMemoryModuleRegistry registry; private List pendingCloses; private CoreRuntime runtime; @@ -34,10 +35,9 @@ class CoreRuntimeTest void setUp() { this.factory = mock(CoreDeploymentFactory.class); - this.registry = new InMemoryModuleRegistry(); this.pendingCloses = new ArrayList<>(); /* Holds the delayed closes, so that tests decide when the grace delay is over */ - this.runtime = new CoreRuntime(this.factory, this.registry, this.pendingCloses::add); + this.runtime = new CoreRuntime(this.factory, this.pendingCloses::add); } @Test @@ -47,37 +47,37 @@ void currentBeforeTheFirstDeploymentThrows() } @Test - void startupDeploysTheEnabledModulesOfTheRegistry() + void aDeploySwapsInTheNewDeploymentAtOnce() { - this.registry.registerModule(new KubernetesSimulatedModule(), true); - this.registry.registerModule(new LLMSimulatedModule(), false); - when(this.factory.create(any())).thenReturn(deployment()); + CoreDeployment second = deployment(2); + when(this.factory.create(anyLong(), anyString(), any())).thenReturn(deployment(1), second); - this.runtime.onStart(null); + this.runtime.deploy(1, "one", Set.of()); + this.runtime.deploy(2, "two", Set.of()); - verify(this.factory).create(Set.of(KubernetesSimulatedModule.TYPE.name())); + assertSame(second, this.runtime.current()); } @Test - void aDeploySwapsInTheNewDeploymentAtOnce() + void aVersionNotNewerThanTheRunningOneIsIgnored() { - CoreDeployment second = deployment(); - when(this.factory.create(any())).thenReturn(deployment(), second); + CoreDeployment second = deployment(2); + when(this.factory.create(anyLong(), anyString(), any())).thenReturn(second); + this.runtime.deploy(2, "two", Set.of()); - this.runtime.deploy(Set.of()); - this.runtime.deploy(Set.of()); - - assertSame(second, this.runtime.current()); + assertSame(second, this.runtime.deploy(1, "one", Set.of())); + assertSame(second, this.runtime.deploy(2, "two", Set.of())); + verify(this.factory, times(1)).create(anyLong(), anyString(), any()); } @Test void thePreviousDeploymentIsClosedAfterTheGraceDelay() { - CoreDeployment first = deployment(); - CoreDeployment second = deployment(); - when(this.factory.create(any())).thenReturn(first, second); - this.runtime.deploy(Set.of()); - this.runtime.deploy(Set.of()); + CoreDeployment first = deployment(1); + CoreDeployment second = deployment(2); + when(this.factory.create(anyLong(), anyString(), any())).thenReturn(first, second); + this.runtime.deploy(1, "one", Set.of()); + this.runtime.deploy(2, "two", Set.of()); verify(first.monitor(), never()).close(); verify(first.evaluator(), never()).close(); @@ -92,11 +92,11 @@ void thePreviousDeploymentIsClosedAfterTheGraceDelay() @Test void aFailedBuildKeepsTheRunningDeployment() { - CoreDeployment first = deployment(); - when(this.factory.create(any())).thenReturn(first).thenThrow(new IllegalStateException("broken module")); + CoreDeployment first = deployment(1); + when(this.factory.create(anyLong(), anyString(), any())).thenReturn(first).thenThrow(new IllegalStateException("broken module")); - this.runtime.deploy(Set.of()); - assertThrows(IllegalStateException.class, () -> this.runtime.deploy(Set.of())); + this.runtime.deploy(1, "one", Set.of()); + assertThrows(IllegalStateException.class, () -> this.runtime.deploy(2, "two", Set.of())); assertSame(first, this.runtime.current()); assertTrue(this.pendingCloses.isEmpty()); @@ -107,9 +107,9 @@ void closingClosesTheCurrentDeploymentButNotItsModules() { WombatModule module = mock(WombatModule.class); when(module.type()).thenReturn(KubernetesSimulatedModule.TYPE); - CoreDeployment deployment = deployment(module); - when(this.factory.create(any())).thenReturn(deployment); - this.runtime.deploy(Set.of()); + CoreDeployment deployment = deployment(1, module); + when(this.factory.create(anyLong(), anyString(), any())).thenReturn(deployment); + this.runtime.deploy(1, "one", Set.of()); this.runtime.close(); @@ -118,10 +118,17 @@ void closingClosesTheCurrentDeploymentButNotItsModules() verify(module, never()).close(); } + @Test + void aShortIdIsTheFirstSevenCharacters() + { + assertEquals("a3f9c21", CoreRuntime.shortId("a3f9c21e8b0d")); + assertEquals("abc", CoreRuntime.shortId("abc")); + } + /* Monitor and evaluator are mocks, so that closing can be verified without a context */ - private static CoreDeployment deployment(WombatModule... modules) + static CoreDeployment deployment(long version, WombatModule... modules) { AssetEvaluator evaluator = mock(AssetEvaluator.class); - return new CoreDeployment(List.of(modules), Set.of(), mock(AssetMonitor.class), evaluator, evaluator, Instant.now()); + return new CoreDeployment(version, "id-" + version, List.of(modules), Set.of(), mock(AssetMonitor.class), evaluator, evaluator, Instant.now()); } } diff --git a/src/test/java/tech/illuin/wombat/engine/module/runtime/RuntimeProxiesTest.java b/src/test/java/tech/illuin/wombat/engine/module/runtime/RuntimeProxiesTest.java index f72d385..0b66aae 100644 --- a/src/test/java/tech/illuin/wombat/engine/module/runtime/RuntimeProxiesTest.java +++ b/src/test/java/tech/illuin/wombat/engine/module/runtime/RuntimeProxiesTest.java @@ -9,7 +9,6 @@ import tech.illuin.wombat.core.evaluation.AssetEvaluator; import tech.illuin.wombat.core.evaluation.WombatEvaluationException; import tech.illuin.wombat.core.source.AssetMonitor; -import tech.illuin.wombat.engine.module.registry.InMemoryModuleRegistry; import java.lang.reflect.Method; import java.lang.reflect.Modifier; @@ -22,6 +21,8 @@ import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyLong; +import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -35,7 +36,7 @@ class RuntimeProxiesTest void setUp() { this.factory = mock(CoreDeploymentFactory.class); - this.runtime = new CoreRuntime(this.factory, new InMemoryModuleRegistry(), Runnable::run); + this.runtime = new CoreRuntime(this.factory, Runnable::run); } /* A public method left to the parent would silently run on its empty pools: a core bump adding one must fail here */ @@ -56,18 +57,18 @@ void theEvaluatorFollowsTheCurrentDeployment() throws WombatEvaluationException { CoreDeployment first = deployment(); CoreDeployment second = deployment(); - when(this.factory.create(any())).thenReturn(first, second); + when(this.factory.create(anyLong(), anyString(), any())).thenReturn(first, second); TimeRange range = mock(TimeRange.class); AssetFilter filter = mock(AssetFilter.class); List evaluations = List.of(mock(AssetEvaluation.class)); when(second.evaluator().evaluate(range, filter)).thenReturn(evaluations); RuntimeAssetEvaluator evaluator = new RuntimeAssetEvaluator(this.runtime); - this.runtime.deploy(Set.of()); + this.runtime.deploy(1, "id-1", Set.of()); evaluator.evaluate(range, filter); verify(first.evaluator()).evaluate(range, filter); - this.runtime.deploy(Set.of()); + this.runtime.deploy(2, "id-2", Set.of()); assertSame(evaluations, evaluator.evaluate(range, filter)); } @@ -76,15 +77,15 @@ void theMonitorFollowsTheCurrentDeployment() { CoreDeployment first = deployment(); CoreDeployment second = deployment(); - when(this.factory.create(any())).thenReturn(first, second); + when(this.factory.create(anyLong(), anyString(), any())).thenReturn(first, second); AssetIdentity identity = new AssetIdentity("asset", "env", "acme.type"); RuntimeAssetMonitor monitor = new RuntimeAssetMonitor(this.runtime); - this.runtime.deploy(Set.of()); + this.runtime.deploy(1, "id-1", Set.of()); monitor.trigger(); verify(first.monitor()).trigger(); - this.runtime.deploy(Set.of()); + this.runtime.deploy(2, "id-2", Set.of()); monitor.trigger(); monitor.refresh(identity); verify(second.monitor()).trigger(); @@ -124,6 +125,6 @@ private static boolean isDeclaredBy(Class type, Method method) private static CoreDeployment deployment() { AssetEvaluator evaluator = mock(AssetEvaluator.class); - return new CoreDeployment(List.of(), Set.of(), mock(AssetMonitor.class), evaluator, evaluator, Instant.now()); + return new CoreDeployment(0, "id", List.of(), Set.of(), mock(AssetMonitor.class), evaluator, evaluator, Instant.now()); } } diff --git a/src/test/java/tech/illuin/wombat/engine/monitor/MonitoredEnvironmentsTemplateTest.java b/src/test/java/tech/illuin/wombat/engine/monitor/MonitoredEnvironmentsTemplateTest.java index 130eb31..731cf64 100644 --- a/src/test/java/tech/illuin/wombat/engine/monitor/MonitoredEnvironmentsTemplateTest.java +++ b/src/test/java/tech/illuin/wombat/engine/monitor/MonitoredEnvironmentsTemplateTest.java @@ -164,6 +164,7 @@ private static WombatProperties wombatProperties(String environmentsFile) return new WombatModuleProperties() { @Override public WombatModuleProperties.ExtensionProperties extension() { return () -> Path.of("extensions"); } + @Override public WombatModuleProperties.DeploymentProperties deployment() { return null; } }; } From dcf2072d8974cb513b5ab897dd40615db522ec0c Mon Sep 17 00:00:00 2001 From: Pierre Lecerf Date: Fri, 20 Nov 2026 22:25:45 +0100 Subject: [PATCH 4/6] feat(engine): modules list and detail pages --- .../webui/src/components/RegimePill.spec.ts | 28 +++ src/main/webui/src/components/RegimePill.vue | 30 +++ .../webui/src/components/SourcePill.spec.ts | 21 ++ src/main/webui/src/components/SourcePill.vue | 30 +++ src/main/webui/src/domain/module.ts | 53 ++++- src/main/webui/src/layout/HeaderNav.vue | 10 +- .../webui/src/layout/styles/components.css | 7 + .../webui/src/layout/styles/environments.css | 1 - src/main/webui/src/layout/styles/modules.css | 32 +++ src/main/webui/src/main.ts | 1 + .../environments/EnvironmentPage.spec.ts | 8 +- .../components/AssetEditorModal.vue | 9 +- .../environments/components/AssetGrid.vue | 10 +- .../impact/components/KubernetesPanel.vue | 4 +- .../src/pages/impact/components/LlmPanel.vue | 4 +- .../src/pages/modules/ModulePage.spec.ts | 115 +++++++++++ .../webui/src/pages/modules/ModulePage.vue | 182 ++++++++++++++++++ .../src/pages/modules/ModulesPage.spec.ts | 135 +++++++++++++ .../webui/src/pages/modules/ModulesPage.vue | 174 +++++++++++++++++ .../pages/modules/components/CoreStatus.vue | 50 +++++ .../modules/components/RedeployDialog.vue | 116 +++++++++++ .../pages/modules/model/deployment.spec.ts | 79 ++++++++ .../src/pages/modules/model/deployment.ts | 59 ++++++ .../pages/modules/model/moduleList.spec.ts | 43 +++++ .../src/pages/modules/model/moduleList.ts | 36 ++++ src/main/webui/src/router.ts | 14 ++ src/main/webui/src/services/api/modules.ts | 59 +++++- .../src/services/auth/permissions.spec.ts | 1 + .../webui/src/services/auth/permissions.ts | 8 +- 29 files changed, 1290 insertions(+), 29 deletions(-) create mode 100644 src/main/webui/src/components/RegimePill.spec.ts create mode 100644 src/main/webui/src/components/RegimePill.vue create mode 100644 src/main/webui/src/components/SourcePill.spec.ts create mode 100644 src/main/webui/src/components/SourcePill.vue create mode 100644 src/main/webui/src/layout/styles/modules.css create mode 100644 src/main/webui/src/pages/modules/ModulePage.spec.ts create mode 100644 src/main/webui/src/pages/modules/ModulePage.vue create mode 100644 src/main/webui/src/pages/modules/ModulesPage.spec.ts create mode 100644 src/main/webui/src/pages/modules/ModulesPage.vue create mode 100644 src/main/webui/src/pages/modules/components/CoreStatus.vue create mode 100644 src/main/webui/src/pages/modules/components/RedeployDialog.vue create mode 100644 src/main/webui/src/pages/modules/model/deployment.spec.ts create mode 100644 src/main/webui/src/pages/modules/model/deployment.ts create mode 100644 src/main/webui/src/pages/modules/model/moduleList.spec.ts create mode 100644 src/main/webui/src/pages/modules/model/moduleList.ts diff --git a/src/main/webui/src/components/RegimePill.spec.ts b/src/main/webui/src/components/RegimePill.spec.ts new file mode 100644 index 0000000..cc99f9b --- /dev/null +++ b/src/main/webui/src/components/RegimePill.spec.ts @@ -0,0 +1,28 @@ +import { describe, expect, it } from 'vitest' +import { mount } from '@vue/test-utils' +import RegimePill from './RegimePill.vue' + +describe('RegimePill', () => { + it('shows a measured regime with a ruler', () => { + const pill = mount(RegimePill, { props: { regime: 'MEASURED' } }) + + expect(pill.text()).toBe('Measured') + expect(pill.classes()).toContain('measured') + expect(pill.findAll('svg path')).toHaveLength(5) + }) + + it('shows a modeled regime with geometric shapes', () => { + const pill = mount(RegimePill, { props: { regime: 'MODELED' } }) + + expect(pill.text()).toBe('Modeled') + expect(pill.find('svg rect').exists()).toBe(true) + expect(pill.find('svg circle').exists()).toBe(true) + }) + + it('shows an unknown regime without icon', () => { + const pill = mount(RegimePill, { props: { regime: 'UNKNOWN' } }) + + expect(pill.text()).toBe('Unknown') + expect(pill.find('svg').exists()).toBe(false) + }) +}) diff --git a/src/main/webui/src/components/RegimePill.vue b/src/main/webui/src/components/RegimePill.vue new file mode 100644 index 0000000..248859a --- /dev/null +++ b/src/main/webui/src/components/RegimePill.vue @@ -0,0 +1,30 @@ + + + diff --git a/src/main/webui/src/components/SourcePill.spec.ts b/src/main/webui/src/components/SourcePill.spec.ts new file mode 100644 index 0000000..5d712b1 --- /dev/null +++ b/src/main/webui/src/components/SourcePill.spec.ts @@ -0,0 +1,21 @@ +import { describe, expect, it } from 'vitest' +import { mount } from '@vue/test-utils' +import SourcePill from './SourcePill.vue' + +describe('SourcePill', () => { + it('shows a core module with a package', () => { + const pill = mount(SourcePill, { props: { source: 'core' } }) + + expect(pill.text()).toBe('Core') + expect(pill.classes()).toEqual(['icon-pill', 'core']) + expect(pill.findAll('svg path')).toHaveLength(4) + }) + + it('shows an extension module with a puzzle piece', () => { + const pill = mount(SourcePill, { props: { source: 'extension' } }) + + expect(pill.text()).toBe('Extension') + expect(pill.attributes('title')).toBe('Loaded from a jar of the extension folder') + expect(pill.findAll('svg path')).toHaveLength(1) + }) +}) diff --git a/src/main/webui/src/components/SourcePill.vue b/src/main/webui/src/components/SourcePill.vue new file mode 100644 index 0000000..af3ed78 --- /dev/null +++ b/src/main/webui/src/components/SourcePill.vue @@ -0,0 +1,30 @@ + + + diff --git a/src/main/webui/src/domain/module.ts b/src/main/webui/src/domain/module.ts index ce45249..7c84f1c 100644 --- a/src/main/webui/src/domain/module.ts +++ b/src/main/webui/src/domain/module.ts @@ -3,12 +3,63 @@ import type { AssetType } from '@/domain/environment' /** GET /api/modules: an asset type the server knows. */ export interface ModuleMetadata { type: AssetType - assetClass: string + /** Null when the serving instance does not detect the module. */ + assetClass: string | null /** Disabled modules accept assets, but their metrics are skipped. */ enabled: boolean /** Simulated types only exist within simulation requests. */ creatable: boolean } +export type ModuleSource = 'core' | 'extension' + +/** A module as Settings > Modules shows it: its registry state, and whether the running core holds it. */ +export interface ModuleInfo extends ModuleMetadata { + /** Whether the core running on the serving instance holds it. */ + deployed: boolean + /** Whether the latest core deployment does not hold its registry state yet. */ + pending: boolean + /** Whether no instance detects it any more, e.g. its extension jar was removed. */ + missing: boolean + source: ModuleSource | null + assetCount: number +} + +export type CoreInstanceStatus = 'ok' | 'failed' + +export interface CoreInstance { + instanceId: string + deployedVersion: number + deployedId: string | null + deployedAt: string + /** Failed when the latest deployment did not build there: the instance keeps running its deployed version. */ + status: CoreInstanceStatus + error: string | null + detectedModules: string[] + missingModules: string[] + /** Whether it is the instance which served the request. */ + current: boolean +} + +/** GET /api/modules/deployment */ +export interface CoreDeploymentStatus { + latest: { version: number; id: string; requestedAt: string; requestedBy: string | null; modules: string[] } + instances: CoreInstance[] + pending: { type: string; enabled: boolean }[] + pollIntervalSeconds: number +} + +/** POST /api/modules/deployment: the redeploy, and how it went on the instance which recorded it. */ +export interface Redeployed { + version: number + id: string + local: { status: CoreInstanceStatus; error: string | null; added: string[]; removed: string[] } +} + +/** The first 7 characters of a deployment id, like a short commit id. */ +export function shortDeploymentId(id: string): string { + return id.slice(0, 7) +} + /** A JSON schema (draft 2020-12), as served by GET /api/modules/{id}/schema. */ export type JsonSchema = Record diff --git a/src/main/webui/src/layout/HeaderNav.vue b/src/main/webui/src/layout/HeaderNav.vue index b682a09..46fef45 100644 --- a/src/main/webui/src/layout/HeaderNav.vue +++ b/src/main/webui/src/layout/HeaderNav.vue @@ -12,7 +12,7 @@ const links = computed(() => settingsLinks(appInfo.value?.user)) const onReport = computed(() => route.name === 'home' || route.name === 'impact') const onEnvironments = computed(() => typeof route.name === 'string' && route.name.startsWith('environment')) const showEnvironments = computed(() => can(appInfo.value?.user, 'ui.environments.read')) -const inSettings = computed(() => links.value.some(link => link.to === route.path)) +const inSettings = computed(() => links.value.some(link => route.path === link.to || route.path.startsWith(`${link.to}/`))) const { open, root } = useDropdown() @@ -38,12 +38,18 @@ const { open, root } = useDropdown()

diff --git a/src/main/webui/src/pages/environments/components/AssetGrid.vue b/src/main/webui/src/pages/environments/components/AssetGrid.vue index 0f94472..57e9b89 100644 --- a/src/main/webui/src/pages/environments/components/AssetGrid.vue +++ b/src/main/webui/src/pages/environments/components/AssetGrid.vue @@ -1,8 +1,8 @@ @@ -44,7 +44,7 @@ function regimeLabel(asset: AssetInfo): string | null { {{ asset.id }} - {{ regimeLabel(asset) }} + Disabled {{ typeLabel(asset) }} diff --git a/src/main/webui/src/pages/impact/components/KubernetesPanel.vue b/src/main/webui/src/pages/impact/components/KubernetesPanel.vue index f0d7bde..4358360 100644 --- a/src/main/webui/src/pages/impact/components/KubernetesPanel.vue +++ b/src/main/webui/src/pages/impact/components/KubernetesPanel.vue @@ -1,8 +1,8 @@ + + diff --git a/src/main/webui/src/pages/modules/ModulesPage.spec.ts b/src/main/webui/src/pages/modules/ModulesPage.spec.ts new file mode 100644 index 0000000..8df1533 --- /dev/null +++ b/src/main/webui/src/pages/modules/ModulesPage.spec.ts @@ -0,0 +1,135 @@ +import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' +import { flushPromises, mount, type VueWrapper } from '@vue/test-utils' +import { createMemoryHistory, createRouter } from 'vue-router' +import type { CoreDeploymentStatus, ModuleInfo } from '@/domain/module' +import { deleteModule, fetchDeployment, fetchModuleInfos, redeployCore } from '@/services/api/modules' +import ModulesPage from './ModulesPage.vue' + +const session = vi.hoisted(() => ({ setPermissions: (_permissions: string[]): void => {} })) + +vi.mock('@/services/auth/session', async () => { + const { readonly, ref } = await import('vue') + const appInfo = ref(null) + session.setPermissions = permissions => { + appInfo.value = { auth: { enabled: true }, user: { authenticated: true, name: 'someone', roles: [], permissions }, ui: { maxDateRange: { millis: 0, label: '' } } } + } + return { useAppInfo: () => ({ appInfo: readonly(appInfo) }), loadAppInfo: vi.fn() } +}) +vi.mock('@/services/api/modules', () => ({ + fetchModuleInfos: vi.fn(), fetchDeployment: vi.fn(), deleteModule: vi.fn(), redeployCore: vi.fn(), +})) + +const READER = ['ui.modules.read'] +const MANAGER = ['ui.modules.read', 'ui.modules.manage', 'api.module.manage'] + +function module(localName: string, overrides: Partial = {}): ModuleInfo { + return { + type: { namespace: 'tech.illuin', localName, name: `tech.illuin.${localName}`, regime: 'MEASURED', family: 'LLM' }, + assetClass: 'Asset', enabled: true, creatable: true, deployed: true, pending: false, missing: false, source: 'core', assetCount: 2, + ...overrides, + } +} + +const anthropic = module('llm-anthropic') +const vertex = module('llm-vertexai', { enabled: false, pending: true }) +const removed = module('acme-removed', { missing: true, assetClass: null, source: null, deployed: false, assetCount: 0 }) + +function deployment(version: number, overrides: Partial = {}): CoreDeploymentStatus { + return { + latest: { version, id: `a3f9c21e8b0d${version}`, requestedAt: '2026-11-20T14:02:00Z', requestedBy: 'alice', modules: [] }, + instances: [{ + instanceId: 'wombat', deployedVersion: version, deployedId: `a3f9c21e8b0d${version}`, deployedAt: '2026-11-20T14:02:00Z', + status: 'ok', error: null, detectedModules: [], missingModules: [], current: true, + }], + pending: [{ type: vertex.type.name, enabled: false }], + pollIntervalSeconds: 30, + ...overrides, + } +} + +let wrapper: VueWrapper + +async function render(permissions: string[]): Promise { + session.setPermissions(permissions) + const router = createRouter({ + history: createMemoryHistory(), + routes: [ + { path: '/modules', name: 'modules', component: ModulesPage }, + { path: '/modules/:type', name: 'module', component: { template: '
' } }, + ], + }) + await router.push('/modules') + wrapper = mount(ModulesPage, { global: { plugins: [router] }, attachTo: document.body }) + await flushPromises() +} + +beforeEach(() => { + vi.mocked(fetchModuleInfos).mockResolvedValue([removed, vertex, anthropic]) + vi.mocked(fetchDeployment).mockResolvedValue(deployment(7)) +}) + +afterEach(() => { + wrapper.unmount() + document.body.innerHTML = '' + vi.clearAllMocks() +}) + +describe('ModulesPage', () => { + it('lists the modules with their states, missing ones last', async () => { + await render(READER) + + const rows = wrapper.findAll('.module-row') + expect(rows.map(row => row.find('.tech-name').text())).toEqual([anthropic.type.name, vertex.type.name, removed.type.name]) + expect(rows[1].findAll('.module-state').map(state => state.text())).toEqual(['Disabled', 'Not deployed']) + expect(rows[2].classes()).toContain('inactive') + expect(rows[2].find('.module-state').text()).toBe('Missing') + }) + + it('shows the running core by its short id, and the changes not deployed', async () => { + await render(READER) + + expect(wrapper.find('.core-id').text()).toBe('a3f9c21') + expect(wrapper.find('.core-status-line').text()).toContain('by alice') + expect(wrapper.find('.module-pending-banner').text()).toBe( + '1 change not deployed: a core redeployment is required for monitoring and evaluations to take it into account.', + ) + }) + + it('leaves redeploying and deleting to managers', async () => { + await render(READER) + + expect(wrapper.find('#redeployCore').exists()).toBe(false) + expect(wrapper.find('.module-delete').exists()).toBe(false) + }) + + it('deletes a missing module once its name is typed', async () => { + vi.mocked(deleteModule).mockResolvedValue() + await render(MANAGER) + + await wrapper.find('.module-delete').trigger('click') + const input = document.querySelector('.confirm-delete-input')! + input.value = removed.type.name + input.dispatchEvent(new Event('input')) + await flushPromises() + document.querySelector('.confirm-delete-submit')!.click() + await flushPromises() + + expect(deleteModule).toHaveBeenCalledWith(removed.type.name) + expect(fetchModuleInfos).toHaveBeenCalledTimes(2) + }) + + it('redeploys, then waits until every instance runs the new core', async () => { + vi.mocked(redeployCore).mockResolvedValue({ version: 8, id: 'b71c0de99', local: { status: 'ok', error: null, added: [], removed: [vertex.type.name] } }) + await render(MANAGER) + + await wrapper.find('#redeployCore').trigger('click') + expect(document.querySelector('.redeploy-changes')!.textContent).toContain(vertex.type.name) + + vi.mocked(fetchDeployment).mockResolvedValue(deployment(8, { pending: [] })) + document.querySelector('.redeploy-dialog .btn-apply')!.click() + await flushPromises() + + expect(redeployCore).toHaveBeenCalled() + expect(document.querySelector('.redeploy-dialog')!.textContent).toContain('Every instance runs core b71c0de') + }) +}) diff --git a/src/main/webui/src/pages/modules/ModulesPage.vue b/src/main/webui/src/pages/modules/ModulesPage.vue new file mode 100644 index 0000000..0d51c21 --- /dev/null +++ b/src/main/webui/src/pages/modules/ModulesPage.vue @@ -0,0 +1,174 @@ + + + diff --git a/src/main/webui/src/pages/modules/components/CoreStatus.vue b/src/main/webui/src/pages/modules/components/CoreStatus.vue new file mode 100644 index 0000000..2179c1d --- /dev/null +++ b/src/main/webui/src/pages/modules/components/CoreStatus.vue @@ -0,0 +1,50 @@ + + + diff --git a/src/main/webui/src/pages/modules/components/RedeployDialog.vue b/src/main/webui/src/pages/modules/components/RedeployDialog.vue new file mode 100644 index 0000000..034d5e2 --- /dev/null +++ b/src/main/webui/src/pages/modules/components/RedeployDialog.vue @@ -0,0 +1,116 @@ + + + diff --git a/src/main/webui/src/pages/modules/model/deployment.spec.ts b/src/main/webui/src/pages/modules/model/deployment.spec.ts new file mode 100644 index 0000000..8a97d07 --- /dev/null +++ b/src/main/webui/src/pages/modules/model/deployment.spec.ts @@ -0,0 +1,79 @@ +import { describe, expect, it, vi } from 'vitest' +import type { CoreDeploymentStatus, CoreInstance } from '@/domain/module' +import { awaitConvergence, convergence, convergenceTimeoutMs, divergentInstances } from './deployment' + +function instance(instanceId: string, deployedVersion: number, overrides: Partial = {}): CoreInstance { + return { + instanceId, deployedVersion, deployedId: `id-${deployedVersion}`, deployedAt: '2026-11-20T14:02:00Z', + status: 'ok', error: null, detectedModules: ['a', 'b'], missingModules: [], current: false, + ...overrides, + } +} + +function status(...instances: CoreInstance[]): CoreDeploymentStatus { + return { + latest: { version: 7, id: 'id-7', requestedAt: '2026-11-20T14:02:00Z', requestedBy: null, modules: [] }, + instances, pending: [], pollIntervalSeconds: 30, + } +} + +describe('convergence', () => { + it('is done once every instance runs the version', () => { + expect(convergence(status(instance('a', 7), instance('b', 8)), 7)).toMatchObject({ state: 'done', converged: 2, total: 2 }) + }) + + it('waits for the instances still behind', () => { + expect(convergence(status(instance('a', 7), instance('b', 6)), 7)).toMatchObject({ state: 'waiting', converged: 1, total: 2 }) + }) + + it('fails when an instance could not build the version', () => { + const broken = instance('b', 6, { status: 'failed', error: 'broken module' }) + expect(convergence(status(instance('a', 7), broken), 7)).toMatchObject({ state: 'failed', failed: [broken] }) + }) +}) + +describe('awaitConvergence', () => { + it('polls until every instance runs the version', async () => { + const fetchStatus = vi.fn<() => Promise>() + .mockResolvedValueOnce(status(instance('a', 7), instance('b', 6))) + .mockResolvedValueOnce(status(instance('a', 7), instance('b', 7))) + const progress = vi.fn() + + const result = await awaitConvergence(fetchStatus, 7, { sleep: async () => {}, onProgress: progress }) + + expect(result.state).toBe('done') + expect(fetchStatus).toHaveBeenCalledTimes(2) + expect(progress).toHaveBeenNthCalledWith(1, expect.objectContaining({ converged: 1, total: 2 })) + }) + + it('gives up past the timeout', async () => { + let clock = 0 + const fetchStatus = vi.fn(async () => status(instance('a', 6))) + + const result = await awaitConvergence(fetchStatus, 7, { + now: () => clock, + sleep: async ms => { clock += ms }, + pollMs: 10_000, + }) + + expect(result.state).toBe('timeout') + expect(clock).toBeGreaterThanOrEqual(convergenceTimeoutMs(30)) + }) + + it('stops polling once aborted', async () => { + const controller = new AbortController() + const fetchStatus = vi.fn(async () => status(instance('a', 6))) + + const result = await awaitConvergence(fetchStatus, 7, { sleep: async () => controller.abort(), signal: controller.signal }) + + expect(result.state).toBe('waiting') + expect(fetchStatus).toHaveBeenCalledTimes(2) + }) +}) + +describe('divergentInstances', () => { + it('lists the instances detecting other modules than the current one', () => { + const other = instance('b', 7, { detectedModules: ['a'] }) + expect(divergentInstances([instance('a', 7, { current: true }), instance('c', 7, { detectedModules: ['b', 'a'] }), other])).toEqual([other]) + }) +}) diff --git a/src/main/webui/src/pages/modules/model/deployment.ts b/src/main/webui/src/pages/modules/model/deployment.ts new file mode 100644 index 0000000..a4b075a --- /dev/null +++ b/src/main/webui/src/pages/modules/model/deployment.ts @@ -0,0 +1,59 @@ +import type { CoreDeploymentStatus, CoreInstance } from '@/domain/module' + +export type ConvergenceState = 'waiting' | 'done' | 'failed' | 'timeout' + +export interface Convergence { + state: ConvergenceState + /** How many live instances run the version, or a newer one. */ + converged: number + total: number + /** The instances on which the version failed to build. */ + failed: CoreInstance[] +} + +/** How far the live instances are from running `version`. */ +export function convergence(status: CoreDeploymentStatus, version: number): Convergence { + const converged = status.instances.filter(instance => instance.deployedVersion >= version).length + const failed = status.instances.filter(instance => instance.status === 'failed' && instance.deployedVersion < version) + const state: ConvergenceState = failed.length > 0 ? 'failed' : converged === status.instances.length ? 'done' : 'waiting' + return { state, converged, total: status.instances.length, failed } +} + +/** Every instance polls once per interval: past two of them, and some slack, a lagging one is stuck. */ +export function convergenceTimeoutMs(pollIntervalSeconds: number): number { + return (2 * pollIntervalSeconds + 10) * 1000 +} + +export interface ConvergenceOptions { + pollMs?: number + sleep?: (ms: number) => Promise + now?: () => number + onProgress?: (progress: Convergence) => void + /** Stops polling: the last progress is returned as it is. */ + signal?: AbortSignal +} + +/** Polls the deployment status until every live instance runs `version`, one failed to build it, or time runs out. */ +export async function awaitConvergence( + fetchStatus: () => Promise, + version: number, + { pollMs = 2000, sleep = ms => new Promise(resolve => setTimeout(resolve, ms)), now = Date.now, onProgress, signal }: ConvergenceOptions = {}, +): Promise { + const start = now() + for (;;) { + const status = await fetchStatus() + const progress = convergence(status, version) + onProgress?.(progress) + if (progress.state !== 'waiting' || signal?.aborted) return progress + if (now() - start >= convergenceTimeoutMs(status.pollIntervalSeconds)) return { ...progress, state: 'timeout' } + await sleep(pollMs) + } +} + +/** The instances which detect other modules than the one serving the page: their extension folders differ. */ +export function divergentInstances(instances: readonly CoreInstance[]): CoreInstance[] { + const reference = instances.find(instance => instance.current) + if (!reference) return [] + const expected = [...reference.detectedModules].sort().join(',') + return instances.filter(instance => [...instance.detectedModules].sort().join(',') !== expected) +} diff --git a/src/main/webui/src/pages/modules/model/moduleList.spec.ts b/src/main/webui/src/pages/modules/model/moduleList.spec.ts new file mode 100644 index 0000000..0c98706 --- /dev/null +++ b/src/main/webui/src/pages/modules/model/moduleList.spec.ts @@ -0,0 +1,43 @@ +import { describe, expect, it } from 'vitest' +import type { ModuleInfo } from '@/domain/module' +import { filterModules, moduleStates, sortModules } from './moduleList' + +function module(localName: string, family: ModuleInfo['type']['family'], overrides: Partial = {}): ModuleInfo { + return { + type: { namespace: 'tech.illuin', localName, name: `tech.illuin.${localName}`, regime: 'MEASURED', family }, + assetClass: 'Asset', enabled: true, creatable: true, deployed: true, pending: false, missing: false, source: 'core', assetCount: 0, + ...overrides, + } +} + +const anthropic = module('llm-anthropic', 'LLM') +const kubernetes = module('kubernetes-api', 'KUBERNETES_CONTAINER', { enabled: false, pending: true }) +const removed = module('acme-removed', 'LLM', { missing: true, assetClass: null, source: null, deployed: false }) + +describe('moduleStates', () => { + it('tells the registry state, and whether it awaits a redeploy', () => { + expect(moduleStates(anthropic)).toEqual(['enabled']) + expect(moduleStates(kubernetes)).toEqual(['disabled', 'pending']) + }) + + it('only tells a missing module is missing', () => { + expect(moduleStates(removed)).toEqual(['missing']) + }) +}) + +describe('filterModules', () => { + it('matches the query against the full type name', () => { + expect(filterModules([anthropic, kubernetes], 'KUBERNETES', [])).toEqual([kubernetes]) + expect(filterModules([anthropic, kubernetes], 'tech.illuin.llm', [])).toEqual([anthropic]) + }) + + it('keeps the modules with one of the states', () => { + expect(filterModules([anthropic, kubernetes, removed], '', ['pending', 'missing'])).toEqual([kubernetes, removed]) + }) +}) + +describe('sortModules', () => { + it('lists by family then name, missing modules last', () => { + expect(sortModules([removed, anthropic, kubernetes])).toEqual([kubernetes, anthropic, removed]) + }) +}) diff --git a/src/main/webui/src/pages/modules/model/moduleList.ts b/src/main/webui/src/pages/modules/model/moduleList.ts new file mode 100644 index 0000000..d48f38d --- /dev/null +++ b/src/main/webui/src/pages/modules/model/moduleList.ts @@ -0,0 +1,36 @@ +import { FAMILY_ORDER } from '@/domain/assetTypeDisplay' +import type { ModuleInfo } from '@/domain/module' + +export type ModuleState = 'enabled' | 'disabled' | 'pending' | 'missing' + +export const STATE_LABELS: Record = { + enabled: 'Enabled', + disabled: 'Disabled', + pending: 'Not deployed', + missing: 'Missing', +} + +export function moduleStates(module: ModuleInfo): ModuleState[] { + if (module.missing) return ['missing'] + const states: ModuleState[] = [module.enabled ? 'enabled' : 'disabled'] + if (module.pending) states.push('pending') + return states +} + +/** The modules whose name contains the query, and which have one of the states, if any. */ +export function filterModules(modules: readonly ModuleInfo[], query: string, states: readonly ModuleState[]): ModuleInfo[] { + const needle = query.trim().toLowerCase() + return modules.filter(module => { + if (states.length > 0 && !moduleStates(module).some(state => states.includes(state))) + return false + return !needle || module.type.name.toLowerCase().includes(needle) + }) +} + +/** Missing modules last; the others by family, then name. */ +export function sortModules(modules: readonly ModuleInfo[]): ModuleInfo[] { + return [...modules].sort((a, b) => + Number(a.missing) - Number(b.missing) + || FAMILY_ORDER.indexOf(a.type.family) - FAMILY_ORDER.indexOf(b.type.family) + || a.type.localName.localeCompare(b.type.localName)) +} diff --git a/src/main/webui/src/router.ts b/src/main/webui/src/router.ts index 92df680..c01c8bf 100644 --- a/src/main/webui/src/router.ts +++ b/src/main/webui/src/router.ts @@ -11,6 +11,8 @@ import ForbiddenView from '@/pages/system/ForbiddenPage.vue' import UsersView from '@/pages/users/UsersPage.vue' import EnvironmentsView from '@/pages/environments/EnvironmentsPage.vue' import EnvironmentView from '@/pages/environments/EnvironmentPage.vue' +import ModulesView from '@/pages/modules/ModulesPage.vue' +import ModuleView from '@/pages/modules/ModulePage.vue' declare module 'vue-router' { interface RouteMeta { @@ -52,6 +54,18 @@ export const routes: RouteRecordRaw[] = [ component: EnvironmentView, meta: {permission: 'ui.environments.read'}, }, + { + path: '/modules', + name: 'modules', + component: ModulesView, + meta: {permission: 'ui.modules.read'}, + }, + { + path: '/modules/:type', + name: 'module', + component: ModuleView, + meta: {permission: 'ui.modules.read'}, + }, { path: '/users', name: 'users', diff --git a/src/main/webui/src/services/api/modules.ts b/src/main/webui/src/services/api/modules.ts index 0262f87..ee25d86 100644 --- a/src/main/webui/src/services/api/modules.ts +++ b/src/main/webui/src/services/api/modules.ts @@ -1,12 +1,57 @@ -import type { JsonSchema, ModuleMetadata } from '@/domain/module' -import { request } from '@/services/api/client' +import type { CoreDeploymentStatus, JsonSchema, ModuleInfo, ModuleMetadata, Redeployed } from '@/domain/module' +import { request, type StatusMessages } from '@/services/api/client' -export type { JsonSchema, ModuleMetadata } from '@/domain/module' +export type { CoreDeploymentStatus, JsonSchema, ModuleInfo, ModuleMetadata, Redeployed } from '@/domain/module' -/** The asset types this server detects. Missing modules (registered, but no longer detected) take no asset, so they are left out. */ +const MODULE_MESSAGES: StatusMessages = { + 403: 'You are not allowed to manage modules.', + 404: 'This module is unknown to the server.', +} + +function modulePath(type: string): string { + return `/api/modules/${encodeURIComponent(type)}` +} + +/** Every registered module, missing ones included. */ +export function fetchModuleInfos(): Promise { + return request('/api/modules') +} + +/** The asset types this server knows. Missing modules take no asset, so they are left out. */ export async function fetchModules(): Promise { - const modules = await request<(ModuleMetadata & { missing?: boolean })[]>('/api/modules') - return modules.filter(module => !module.missing) + return (await fetchModuleInfos()).filter(module => !module.missing) +} + +export function fetchModule(type: string): Promise { + return request(modulePath(type), { messages: MODULE_MESSAGES }) +} + +/** The asset pickers follow at once; the core only follows on redeploy. */ +export function setModuleEnabled(type: string, enabled: boolean): Promise { + return request(modulePath(type), { + method: 'PUT', + body: { enabled }, + messages: { ...MODULE_MESSAGES, 409: 'No instance detects this module any more: it cannot be toggled.' }, + }) +} + +/** Forgets a missing module. */ +export function deleteModule(type: string): Promise { + return request(modulePath(type), { + method: 'DELETE', + messages: { ...MODULE_MESSAGES, 409: 'An instance detects this module again: only a missing module can be deleted.' }, + }) +} + +export function fetchDeployment(): Promise { + return request('/api/modules/deployment') +} + +export function redeployCore(): Promise { + return request('/api/modules/deployment', { + method: 'POST', + messages: { ...MODULE_MESSAGES, 409: 'Another redeploy was just requested: the instances converge on it.' }, + }) } /* A type's schema only changes with the server's version: fetch it once per page load */ @@ -15,7 +60,7 @@ const schemas = new Map>() export function fetchModuleSchema(type: string): Promise { let schema = schemas.get(type) if (!schema) { - schema = request(`/api/modules/${encodeURIComponent(type)}/schema`, { messages: { 404: 'This asset type is unknown to the server.' } }) + schema = request(`${modulePath(type)}/schema`, { messages: { 404: 'This asset type is unknown to the server.' } }) schema.catch(() => schemas.delete(type)) schemas.set(type, schema) } diff --git a/src/main/webui/src/services/auth/permissions.spec.ts b/src/main/webui/src/services/auth/permissions.spec.ts index a5197cf..3c44697 100644 --- a/src/main/webui/src/services/auth/permissions.spec.ts +++ b/src/main/webui/src/services/auth/permissions.spec.ts @@ -34,6 +34,7 @@ describe('settings menu links', () => { expect(settingsLinks({ permissions: ['ui.dashboard.read'] })).toEqual([]) expect(settingsLinks({ permissions: ['ui.apikeys.manage'] }).map(link => link.label)).toEqual(['API keys']) expect(settingsLinks({ permissions: ['ui.users.read', 'ui.apikeys.manage'] }).map(link => link.to)).toEqual(['/api-keys', '/users']) + expect(settingsLinks({ permissions: ['ui.users.read', 'ui.apikeys.manage', 'ui.modules.read'] }).map(link => link.label)).toEqual(['Modules', 'API keys', 'Users']) expect(settingsLinks(null)).toEqual([]) }) }) diff --git a/src/main/webui/src/services/auth/permissions.ts b/src/main/webui/src/services/auth/permissions.ts index 0137c5e..6c31039 100644 --- a/src/main/webui/src/services/auth/permissions.ts +++ b/src/main/webui/src/services/auth/permissions.ts @@ -10,14 +10,16 @@ export function can(user: PermissionHolder | null | undefined, permission: Permi } export interface MenuLink { - label: 'Users' | 'API keys' + label: 'Users' | 'API keys' | 'Modules' to: string permission: Permission + icon: 'key' | 'users' | 'modules' } const SETTINGS_LINKS: MenuLink[] = [ - { label: 'API keys', to: '/api-keys', permission: 'ui.apikeys.manage' }, - { label: 'Users', to: '/users', permission: 'ui.users.read' }, + { label: 'Modules', to: '/modules', permission: 'ui.modules.read', icon: 'modules' }, + { label: 'API keys', to: '/api-keys', permission: 'ui.apikeys.manage', icon: 'key' }, + { label: 'Users', to: '/users', permission: 'ui.users.read', icon: 'users' }, ] /** The links of the header Settings menu the user may follow. */ From d8e9192aeb50107e68b514e981ef25be3d6cd296 Mon Sep 17 00:00:00 2001 From: Pierre Lecerf Date: Fri, 20 Nov 2026 22:33:59 +0100 Subject: [PATCH 5/6] feat(engine): asset schema outline on the module page --- src/main/webui/src/components/RegimePill.vue | 2 +- src/main/webui/src/layout/styles/impact.css | 2 +- src/main/webui/src/layout/styles/modules.css | 20 +++ .../src/pages/modules/ModulePage.spec.ts | 29 +++- .../webui/src/pages/modules/ModulePage.vue | 41 +++++- .../modules/components/SchemaOutline.spec.ts | 55 +++++++ .../modules/components/SchemaOutline.vue | 53 +++++++ .../pages/modules/model/schemaOutline.spec.ts | 129 +++++++++++++++++ .../src/pages/modules/model/schemaOutline.ts | 137 ++++++++++++++++++ 9 files changed, 462 insertions(+), 6 deletions(-) create mode 100644 src/main/webui/src/pages/modules/components/SchemaOutline.spec.ts create mode 100644 src/main/webui/src/pages/modules/components/SchemaOutline.vue create mode 100644 src/main/webui/src/pages/modules/model/schemaOutline.spec.ts create mode 100644 src/main/webui/src/pages/modules/model/schemaOutline.ts diff --git a/src/main/webui/src/components/RegimePill.vue b/src/main/webui/src/components/RegimePill.vue index 248859a..f43f55b 100644 --- a/src/main/webui/src/components/RegimePill.vue +++ b/src/main/webui/src/components/RegimePill.vue @@ -5,7 +5,7 @@ import type { ActivityRegime } from '@/domain/environment' defineProps<{ regime: ActivityRegime }>() const TITLES: Record = { - MEASURED: 'Its activity is measured on the running system', + MEASURED: 'Its activity is measured on a running system', MODELED: 'Its activity is modeled from a declared profile', UNKNOWN: 'How its activity is known is not told', } diff --git a/src/main/webui/src/layout/styles/impact.css b/src/main/webui/src/layout/styles/impact.css index f377ba2..86490f0 100644 --- a/src/main/webui/src/layout/styles/impact.css +++ b/src/main/webui/src/layout/styles/impact.css @@ -181,7 +181,7 @@ body { background: var(--bg); color: var(--text); font-family: var(--sans); font .stat-row .kpi-value.yellow { color: var(--accent-yellow); } .stat-row .kpi-value.blue { color: var(--accent-blue); } .stat-row .kpi-value.purple { color: var(--accent-purple); } -.asset-breakdown { margin-top: 14px; display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 14px; align-content: start; flex: 1 1 0; min-height: 0; overflow-y: auto; } +.asset-breakdown { margin-top: 14px; display: grid; grid-template-columns: repeat(auto-fill, minmax(270px, 1fr)); gap: 14px; align-content: start; flex: 1 1 0; min-height: 0; overflow-y: auto; } .asset-block { position: relative; border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; background: var(--surface-alt, transparent); } .asset-head { display: flex; flex-direction: column; align-items: flex-start; gap: 8px; cursor: pointer; user-select: none; padding-right: 24px; } .asset-head:hover .asset-name { color: var(--accent-yellow); } diff --git a/src/main/webui/src/layout/styles/modules.css b/src/main/webui/src/layout/styles/modules.css index ebd1edc..f943025 100644 --- a/src/main/webui/src/layout/styles/modules.css +++ b/src/main/webui/src/layout/styles/modules.css @@ -30,3 +30,23 @@ .redeploy-dialog { display: flex; flex-direction: column; gap: 12px; } .redeploy-changes { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; font-size: 13px; } + +.module-schema-body { padding: 18px 26px; } +.schema-outline { list-style: none; margin: 0; padding: 0; display: grid; gap: 14px; } +.schema-outline .schema-outline { margin-top: 10px; padding-left: 16px; border-left: 2px solid var(--border); } +.schema-property-head { display: flex; align-items: center; flex-wrap: wrap; gap: 6px 8px; } +.schema-label { font-size: 13px; font-weight: 600; color: var(--text); } +.schema-type { font-size: 12px; color: var(--accent-blue); } +.schema-required { font-size: 11px; font-weight: 600; color: var(--accent-orange); } +.schema-constraint, .schema-default { font-size: 11px; color: var(--text-muted); } +.schema-constraint + .schema-constraint::before, .schema-type + .schema-constraint::before { content: '· '; } +.schema-description { margin: 4px 0 0; font-size: 12px; color: var(--text-muted); line-height: 1.5; max-width: 720px; } +.schema-values { display: flex; flex-wrap: wrap; align-items: center; gap: 4px; margin-top: 6px; } +.schema-more { display: contents; } +.schema-more summary, .schema-nested > summary, .schema-variant > summary { cursor: pointer; font-size: 11px; color: var(--text-dim); } +.schema-nested { margin-top: 6px; } +.schema-variants { margin-top: 8px; display: grid; gap: 6px; } +.schema-variants-label { font-size: 11px; font-weight: 600; color: var(--text-dim); } +.schema-variant > summary { display: inline-flex; align-items: center; gap: 8px; } +.schema-variant .schema-description { margin-left: 16px; } +.schema-raw { margin: 0; max-height: 480px; overflow: auto; background: var(--surface2); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 12px 14px; font-size: 12px; line-height: 1.5; } diff --git a/src/main/webui/src/pages/modules/ModulePage.spec.ts b/src/main/webui/src/pages/modules/ModulePage.spec.ts index bb142b0..faa75d4 100644 --- a/src/main/webui/src/pages/modules/ModulePage.spec.ts +++ b/src/main/webui/src/pages/modules/ModulePage.spec.ts @@ -2,7 +2,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest' import { flushPromises, mount, type VueWrapper } from '@vue/test-utils' import { createMemoryHistory, createRouter } from 'vue-router' import type { CoreDeploymentStatus, ModuleInfo } from '@/domain/module' -import { fetchDeployment, fetchModule, setModuleEnabled } from '@/services/api/modules' +import { fetchDeployment, fetchModule, fetchModuleSchema, setModuleEnabled } from '@/services/api/modules' import ModulePage from './ModulePage.vue' const session = vi.hoisted(() => ({ setPermissions: (_permissions: string[]): void => {} })) @@ -16,7 +16,7 @@ vi.mock('@/services/auth/session', async () => { return { useAppInfo: () => ({ appInfo: readonly(appInfo) }), loadAppInfo: vi.fn() } }) vi.mock('@/services/api/modules', () => ({ - fetchModule: vi.fn(), fetchDeployment: vi.fn(), setModuleEnabled: vi.fn(), deleteModule: vi.fn(), + fetchModule: vi.fn(), fetchDeployment: vi.fn(), setModuleEnabled: vi.fn(), deleteModule: vi.fn(), fetchModuleSchema: vi.fn(), })) const READER = ['ui.modules.read'] @@ -49,8 +49,15 @@ async function render(permissions: string[], module: ModuleInfo = anthropic): Pr await flushPromises() } +const schema = { + type: 'object', + required: ['adminKey'], + properties: { adminKey: { type: 'string', format: 'secret-key', description: 'Secret holding an Admin API key' } }, +} + beforeEach(() => { vi.mocked(fetchDeployment).mockResolvedValue(deployment) + vi.mocked(fetchModuleSchema).mockResolvedValue(schema) }) afterEach(() => { @@ -105,6 +112,24 @@ describe('ModulePage', () => { expect(setModuleEnabled).toHaveBeenCalledWith(anthropic.type.name, true) }) + it('outlines the asset properties, or shows the raw schema', async () => { + await render(READER) + + expect(fetchModuleSchema).toHaveBeenCalledWith(anthropic.type.name) + expect(wrapper.find('.schema-property .schema-label').text()).toBe('Admin key') + expect(wrapper.find('.schema-type').text()).toBe('secret reference') + + await wrapper.find('.module-schema .btn-link-inline').trigger('click') + expect(JSON.parse(wrapper.find('.schema-raw').text())).toEqual(schema) + }) + + it('does not ask for the schema of a module this instance does not detect', async () => { + await render(READER, { ...anthropic, assetClass: null, source: null, deployed: false }) + + expect(fetchModuleSchema).not.toHaveBeenCalled() + expect(wrapper.find('.module-schema').text()).toContain('This instance does not detect the module') + }) + it('offers to delete a missing module instead of toggling it', async () => { await render(MANAGER, { ...anthropic, missing: true, assetClass: null, source: null, deployed: false }) diff --git a/src/main/webui/src/pages/modules/ModulePage.vue b/src/main/webui/src/pages/modules/ModulePage.vue index 96eac31..03f715d 100644 --- a/src/main/webui/src/pages/modules/ModulePage.vue +++ b/src/main/webui/src/pages/modules/ModulePage.vue @@ -1,9 +1,9 @@ + + diff --git a/src/main/webui/src/pages/modules/model/schemaOutline.spec.ts b/src/main/webui/src/pages/modules/model/schemaOutline.spec.ts new file mode 100644 index 0000000..39dfedc --- /dev/null +++ b/src/main/webui/src/pages/modules/model/schemaOutline.spec.ts @@ -0,0 +1,129 @@ +import { describe, expect, it } from 'vitest' +import { humanize, schemaOutline, type OutlineProperty } from './schemaOutline' + +/* Trimmed from the schemas the server serves for llm-anthropic, llm-static and kubernetes-api */ +const schema = { + $schema: 'https://json-schema.org/draft/2020-12/schema', + type: 'object', + required: ['adminKey', 'profile'], + properties: { + apiUrl: { type: 'string', description: 'Base URL of the Anthropic API', default: 'https://api.anthropic.com' }, + adminKey: { type: 'string', format: 'secret-key', pattern: '^[A-Za-z0-9_-]+$', description: 'Secret holding an Admin API key' }, + readTimeout: { type: ['string', 'null'], format: 'duration' }, + period: { type: 'string', pattern: '^\\d+\\s*[smhSMH]$' }, + profile: { + type: 'object', + required: ['models'], + properties: { + location: { type: 'string', default: 'USA', oneOf: [{ const: 'FRA', title: 'France' }, { const: 'USA', title: 'United States' }] }, + outputTokensPerRequest: { type: 'integer', default: 500, exclusiveMinimum: 0 }, + models: { + type: 'array', + minItems: 1, + items: { + type: 'object', + required: ['provider'], + properties: { + provider: { type: 'string', enum: ['anthropic', 'mistralai'] }, + scenarios: { + type: 'array', + items: { + default: { type: 'constant' }, + oneOf: [ + { + type: 'object', title: 'Constant', description: 'Constant request volume', + properties: { requestPerYear: { type: 'integer', minimum: 0 }, applicability: { $ref: '#/$defs/ApplicabilityFilter' }, type: { const: 'constant', default: 'constant' } }, + }, + { + type: 'object', title: 'Batch activity', + properties: { triggerTime: { type: 'string', format: 'time' }, type: { const: 'batch_activity', default: 'batch_activity' } }, + }, + ], + }, + }, + }, + }, + }, + }, + }, + }, + $defs: { + ApplicabilityFilter: { + type: 'object', + description: 'Days and months a scenario applies to', + properties: { daysOfWeek: { type: 'array', items: { type: 'string', enum: ['MONDAY', 'SUNDAY'] } } }, + }, + }, +} + +function find(properties: OutlineProperty[], name: string): OutlineProperty { + const found = properties.find(property => property.name === name) + if (!found) throw new Error(`No property ${name} in ${properties.map(property => property.name)}`) + return found +} + +const outline = schemaOutline(schema) + +describe('schemaOutline', () => { + it('lists the properties in the order of the schema, with labels and whether they are required', () => { + expect(outline.map(property => property.name)).toEqual(['apiUrl', 'adminKey', 'readTimeout', 'period', 'profile']) + expect(find(outline, 'apiUrl')).toMatchObject({ label: 'Api url', type: 'string', required: false, defaultValue: 'https://api.anthropic.com' }) + expect(find(outline, 'adminKey').required).toBe(true) + }) + + it('tells a secret reference by its type, without the pattern of every secret key', () => { + expect(find(outline, 'adminKey')).toMatchObject({ type: 'secret reference', constraints: [] }) + }) + + it('tells a nullable type, a format and a pattern', () => { + expect(find(outline, 'readTimeout')).toMatchObject({ type: 'string (duration)', constraints: ['may be null'] }) + expect(find(outline, 'period').constraints).toEqual(['matches ^\\d+\\s*[smhSMH]$']) + }) + + it('lists the values of a oneOf of constants with their titles, and of an enum', () => { + const profile = find(outline, 'profile') + expect(find(profile.properties, 'location')).toMatchObject({ + defaultValue: 'USA', + values: [{ value: 'FRA', title: 'France' }, { value: 'USA', title: 'United States' }], + variants: [], + }) + const provider = find(find(profile.properties, 'models').properties, 'provider') + expect(provider.values).toEqual([{ value: 'anthropic' }, { value: 'mistralai' }]) + }) + + it('tells bounds, and formats non-string defaults', () => { + expect(find(find(outline, 'profile').properties, 'outputTokensPerRequest')).toMatchObject({ type: 'integer', defaultValue: '500', constraints: ['> 0'] }) + }) + + it('describes a list by its items', () => { + const models = find(find(outline, 'profile').properties, 'models') + expect(models).toMatchObject({ type: 'list of objects', required: true, constraints: ['at least 1 item'] }) + expect(models.properties.map(property => property.name)).toEqual(['provider', 'scenarios']) + }) + + it('describes the alternatives of a union by title, with their discriminator apart', () => { + const scenarios = find(find(find(outline, 'profile').properties, 'models').properties, 'scenarios') + expect(scenarios.type).toBe('list of alternatives') + expect(scenarios.defaultValue).toBeUndefined() + expect(scenarios.variants.map(variant => [variant.title, variant.discriminator])).toEqual([ + ['Constant', { name: 'type', value: 'constant' }], + ['Batch activity', { name: 'type', value: 'batch_activity' }], + ]) + expect(scenarios.variants[0].properties.map(property => property.name)).toEqual(['requestPerYear', 'applicability']) + expect(find(scenarios.variants[1].properties, 'triggerTime').type).toBe('string (time)') + }) + + it('resolves the definitions a property refers to', () => { + const scenarios = find(find(find(outline, 'profile').properties, 'models').properties, 'scenarios') + const applicability = find(scenarios.variants[0].properties, 'applicability') + expect(applicability).toMatchObject({ type: 'object', description: 'Days and months a scenario applies to' }) + expect(find(applicability.properties, 'daysOfWeek')).toMatchObject({ type: 'list of strings', values: [{ value: 'MONDAY' }, { value: 'SUNDAY' }] }) + }) +}) + +describe('humanize', () => { + it('turns a camel-case or snake-case name into words', () => { + expect(humanize('outputTokensPerRequest')).toBe('Output tokens per request') + expect(humanize('batch_activity')).toBe('Batch activity') + }) +}) diff --git a/src/main/webui/src/pages/modules/model/schemaOutline.ts b/src/main/webui/src/pages/modules/model/schemaOutline.ts new file mode 100644 index 0000000..6f53606 --- /dev/null +++ b/src/main/webui/src/pages/modules/model/schemaOutline.ts @@ -0,0 +1,137 @@ +import { inlineDefinitions } from '@/components/asset-forms/model/schema' +import type { JsonSchema } from '@/domain/module' + +type Schema = Record + +export interface OutlineValue { + value: string + title?: string +} + +/** One alternative of a `oneOf` union: an object told apart by its discriminator's value. */ +export interface OutlineVariant { + title: string + discriminator?: { name: string; value: string } + description?: string + properties: OutlineProperty[] +} + +export interface OutlineProperty { + name: string + label: string + /** e.g. `string`, `integer`, `list of objects`, `secret reference` */ + type: string + required: boolean + description?: string + defaultValue?: string + /** e.g. `≥ 0`, `not empty`, `at least 1 item`, `matches ^\d+$` */ + constraints: string[] + /** The values an enum, or a `oneOf` of constants, allows */ + values: OutlineValue[] + /** The properties of an object, or of the items of a list */ + properties: OutlineProperty[] + /** The alternatives of a union, or of the items of a list */ + variants: OutlineVariant[] +} + +const SECRET_KEY_FORMAT = 'secret-key' + +/** The properties of an asset type's schema, described for reading rather than for a form. */ +export function schemaOutline(schema: JsonSchema): OutlineProperty[] { + return properties(inlineDefinitions(schema)) +} + +/** e.g. `Output tokens per request` for `outputTokensPerRequest` */ +export function humanize(name: string): string { + const words = name.replace(/([a-z0-9])([A-Z])/g, '$1 $2').replace(/[_-]/g, ' ').toLowerCase().trim() + return words.charAt(0).toUpperCase() + words.slice(1) +} + +function properties(node: Schema): OutlineProperty[] { + const required = new Set((node.required ?? []) as string[]) + return Object.entries((node.properties ?? {}) as Record) + .map(([name, property]) => describe(name, property, required.has(name))) +} + +function describe(name: string, node: Schema, required: boolean): OutlineProperty { + const items = isSchema(node.items) ? node.items : null + const shape = items ?? node + return { + name, + label: typeof node.title === 'string' ? node.title : humanize(name), + type: typeLabel(node, items), + required, + description: typeof node.description === 'string' ? node.description : undefined, + defaultValue: defaultLabel(node.default), + constraints: [...constraints(node), ...(items ? constraints(items) : [])], + values: values(shape), + properties: properties(shape), + variants: variants(shape), + } +} + +function typeLabel(node: Schema, items: Schema | null): string { + if (items) + return `list of ${plural(typeLabel(items, isSchema(items.items) ? items.items : null))}` + if (node.format === SECRET_KEY_FORMAT) return 'secret reference' + if (variants(node).length > 0) return 'one of' + const types = (Array.isArray(node.type) ? node.type : [node.type]).filter(type => typeof type === 'string' && type !== 'null') as string[] + const type = types.join(' or ') || 'any' + return typeof node.format === 'string' ? `${type} (${node.format})` : type +} + +/** e.g. `strings (time)` for `string (time)`, `secret references` for `secret reference` */ +function plural(type: string): string { + if (type === 'one of') return 'alternatives' + const [noun, format] = type.split(' (') + return `${noun}s${format ? ` (${format}` : ''}` +} + +function constraints(node: Schema): string[] { + const found: string[] = [] + if (Array.isArray(node.type) && node.type.includes('null')) found.push('may be null') + if (typeof node.minimum === 'number') found.push(`≥ ${node.minimum}`) + if (typeof node.exclusiveMinimum === 'number') found.push(`> ${node.exclusiveMinimum}`) + if (typeof node.maximum === 'number') found.push(`≤ ${node.maximum}`) + if (typeof node.exclusiveMaximum === 'number') found.push(`< ${node.exclusiveMaximum}`) + if (node.minLength === 1) found.push('not empty') + else if (typeof node.minLength === 'number') found.push(`at least ${node.minLength} characters`) + if (typeof node.maxLength === 'number') found.push(`at most ${node.maxLength} characters`) + if (typeof node.minItems === 'number') found.push(`at least ${node.minItems} item${node.minItems === 1 ? '' : 's'}`) + if (typeof node.maxItems === 'number') found.push(`at most ${node.maxItems} item${node.maxItems === 1 ? '' : 's'}`) + /* A secret reference is told by its type: its pattern is the one of every secret key */ + if (typeof node.pattern === 'string' && node.format !== SECRET_KEY_FORMAT) found.push(`matches ${node.pattern}`) + return found +} + +function values(node: Schema): OutlineValue[] { + if (Array.isArray(node.enum)) + return node.enum.map(value => ({ value: String(value) })) + if (Array.isArray(node.oneOf) && node.oneOf.every(option => isSchema(option) && 'const' in option)) + return (node.oneOf as Schema[]).map(option => ({ value: String(option.const), title: typeof option.title === 'string' ? option.title : undefined })) + return [] +} + +function variants(node: Schema): OutlineVariant[] { + if (!Array.isArray(node.oneOf) || node.oneOf.some(option => !isSchema(option) || !isSchema(option.properties))) + return [] + return (node.oneOf as Schema[]).map((option, index) => { + const discriminator = Object.entries(option.properties as Record).find(([, property]) => 'const' in property) + return { + title: typeof option.title === 'string' ? option.title : `Alternative ${index + 1}`, + discriminator: discriminator && { name: discriminator[0], value: String(discriminator[1].const) }, + description: typeof option.description === 'string' ? option.description : undefined, + properties: properties(option).filter(property => property.name !== discriminator?.[0]), + } + }) +} + +/* An object default, e.g. the first alternative of a union, says nothing a reader needs */ +function defaultLabel(value: unknown): string | undefined { + if (value === undefined || value === null || (typeof value === 'object' && !Array.isArray(value))) return undefined + return typeof value === 'string' ? value : JSON.stringify(value) +} + +function isSchema(value: unknown): value is Schema { + return value !== null && typeof value === 'object' && !Array.isArray(value) +} From a0d68766d9be411f02f47187a48e5fe8aeac8e76 Mon Sep 17 00:00:00 2001 From: Pierre Lecerf Date: Fri, 20 Nov 2026 22:55:56 +0100 Subject: [PATCH 6/6] docs(engine): modules and core deployments --- README.md | 9 +- doc/distributed.md | 16 ++- doc/modules.md | 123 ++++++++++++++++++ .../wombat/engine/module/DeploymentInfo.java | 10 +- .../wombat/engine/module/ModuleService.java | 8 +- .../persistence/CoreInstanceEntity.java | 4 +- .../module/runtime/CoreDeploymentService.java | 20 +-- .../wombat/endpoint/ModuleControllerTest.java | 18 +++ .../CoreDeploymentRepositoryTest.java | 2 +- .../runtime/CoreDeploymentServiceTest.java | 8 +- 10 files changed, 187 insertions(+), 31 deletions(-) create mode 100644 doc/modules.md diff --git a/README.md b/README.md index e51bd96..3867e41 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,7 @@ Wombat exposes HTTP endpoints, it aims at the same feature coverage in both UI a API hubs include: * `api/impact` is the equivalent of the dashboard but in JSON form, it makes it possible to query active environments and get impact estimations for their services * `api/simulation` is for getting estimates over entirely manufactured asset descriptions and service activity summary -* `api/modules` is for getting information about registered `WombatModules` and the asset types they provide +* `api/modules` is for listing and managing registered `WombatModules` and their asset types: see [modules](doc/modules.md) * `api/environments` is for getting information about active environments * `api/metrics` is for pushing sampled metrics from worker instances to an aggregator, see [distributed setup](doc/distributed.md) * `api/secrets` provides admin-only management of the optional encrypted database secret store; see [secret management](doc/secret-management.md) @@ -219,6 +219,7 @@ More information on each of these modules can be found [in the `wombat-core` doc * [Secret management](doc/secret-management.md): how to provide necessary credentials to Wombat connectors * [Metric compaction](doc/compaction.md): how sourced metrics are folded before being served, and the effects of tuning the compaction window * [Distributed setup](doc/distributed.md): the standalone, aggregator and worker modes, for workers sampling remote environments and pushing their metrics to an aggregator +* [Modules and core deployments](doc/modules.md): enabling modules from Settings > Modules, redeploying the core, and how replicas converge on it ## How to run @@ -259,8 +260,10 @@ When running in dev-mode, you'll have access to the Quarkus dev-ui at `/q/dev-ui Wombat allows user to provide custom modules built with `wombat-core` (see [repository](https://github.com/illuin-tech/wombat-core)) to extend its functionality. -Custom modules are loaded **_at startup_** from every JAR found in `module.extension.path` -(defaults to `extensions`, relative to the working directory). Override it with `-Dmodule.extension.path=...` or `MODULE_EXTENSION_PATH=...`. +Custom modules are loaded **_at startup_** from every JAR found in `wombat.module.extension.path` +(defaults to `extensions`, relative to the working directory). Override it with `-Dwombat.module.extension.path=...` or `WOMBAT_MODULE_EXTENSION_PATH=...`. +A JAR added later is only loaded after a restart. Once loaded, a module is enabled; it is turned off, and the core +redeployed, from **Settings > Modules**: see [modules](doc/modules.md). When starting up the app with a custom extension, you should see logs like these: diff --git a/doc/distributed.md b/doc/distributed.md index 54fb03a..a64f57e 100644 --- a/doc/distributed.md +++ b/doc/distributed.md @@ -70,6 +70,9 @@ skipped and reported back to the worker in the response status, while the other malformed request (e.g. an unknown metric family, a missing asset, a missing or reversed window) is still rejected with `400`, and nothing from it is saved. +A module turned off on the aggregator, from **Settings > Modules**, applies to the next push: no core redeploy is needed +for the metrics of its assets to be skipped. See [modules](modules.md). + ### Declared environments Each asset belongs to the declaration it comes from: the aggregator's own `monitor.environments-file`, or one worker. @@ -117,8 +120,17 @@ removes it from the aggregator at the worker's next startup. No metric is written to the worker's database, so a worker always runs on an in-memory SQLite database, whatever its `persistence.backend.mode` setting. -Each window is sent in at most one request per metric family. A push that fails (aggregator unreachable, rejected key) -is retried a few times on network errors, then logged and dropped: the worker does not buffer windows it could not send. +A worker enables every module it detects and currently cannot turn them off: it will only declare to the aggregator the assets it samples. The worker deploys its own core at startup, and does not take part in the [core deployments](modules.md#replicas) of the aggregator. + +Each window is sent in at most one request per metric family. A push that fails (aggregator unreachable, rejected key) is retried a few times on network errors, then logged and dropped: the worker does not buffer windows it could not send. + +## Replicas + +Instances sharing one database share their modules' state, and converge on the same [core deployment](modules.md#replicas) within a poll interval of a redeploy. + +Sampling orchestration is not shared between instances, though: so each `standalone` or `worker` instance samples every asset it is given. + +> ⚠️ In the near future Wombat will gain an orchestration layer for accommodating multi-tenant standalone/aggregator instances ; for now this is only partially supported. If this is something you need please reach out to us. ## Running it locally diff --git a/doc/modules.md b/doc/modules.md new file mode 100644 index 0000000..e2e8933 --- /dev/null +++ b/doc/modules.md @@ -0,0 +1,123 @@ +# Modules and core deployments + +A **module** provides an asset type: how its assets are described, sampled and evaluated. Wombat ships with core +modules, and loads [custom ones](../README.md#custom-extensions) from the jars of its extension folder. The **core** is +what monitors and evaluates assets: it is built out of the enabled modules, and rebuilt on each **redeploy**. + +Modules are managed from **Settings > Modules**, or through `api/modules`. + +```mermaid +sequenceDiagram + actor Admin + participant A as Instance A + participant DB as Database + participant B as Instance B + Admin->>DB: toggle a module (modules) + Note over A,B: asset pickers follow at once, the cores do not + Admin->>A: Redeploy core + A->>DB: record the enabled modules as version 8 (core_deployments) + A->>A: build core 8, swap it in + A->>DB: heartbeat: runs 8 (core_instances) + A-->>Admin: core a3f9c21 deployed here + loop every poll interval + B->>DB: latest version? + B->>B: build core 8, swap it in + B->>DB: heartbeat: runs 8 + end +``` + +## The registry + +Each instance detects its modules once, at startup: the core modules, and those of the jars found in +`wombat.module.extension.path`. A jar added or removed later is only seen after a restart. + +Every detected module is **registered** in the database, enabled, along with the asset type it advertises. Its state is then the registry's, shared by every instance: + +| State | Meaning | +|--------------|--------------------------------------------------------------------------------------------------------------------------| +| **Enabled** | Offered in the asset pickers, and held by the next core deployed. | +| **Disabled** | Hidden from the asset pickers, and left out of the next core deployed. Its assets are kept, and still accepted. | +| **Missing** | Registered, but no live instance detects it any more, e.g. its jar was removed. It keeps its state, but is unusable. | + +A missing module comes back as it was when its jar does. It can also be deleted, in which case Wombat will forget its state: should its jar come back, it is registered once again, and enabled. A detected module cannot be deleted. + +A module detected by another instance, but not by the one serving a page, is not considered missing: that instance only lacks its classes, so it cannot show its schema, nor validate its assets. + +## What a module toggle changes, and when + +| Where | When | +|--------------------------------------------------------------------|-------------------------------------------------------------------------| +| The asset pickers of the environment pages | At once | +| The metrics workers push to an aggregator | At once: those of a disabled module are skipped | +| Assets declared by a worker, or created through the API | Never: they are accepted whatever the module's state | +| Monitoring (sampling) and evaluation (Impact / Cost estimation) | After a core redeploy, on every instance | + +Only a redeploy applies toggles to the core, **even across restarts**: a restarted instance runs the latest +deployment, not the registry. Settings > Modules tells the changes not deployed yet, and which core each instance runs. + +## Core deployments + +### Redeploying + +**Redeploy core** (`POST /api/modules/deployment`) records the modules enabled at that moment under the next version, +then deploys it on the instance which received the request. + +Deploying builds a new core and swaps it in: the calls already running finish on the previous core, which is closed a +minute later. + +A redeploy resets the evaluation caches, and starts sampling afresh, as a restart does: a sampling run in progress during the swap may overlap the first run of the new core. + +A redeploy with no change still rebuilds every core, which resets the caches and retries the instances that failed. + +### Replicas + +The database is the only channel between instances. Each instance: +* deploys the latest version at startup; the first one to start records version 1 out of the registry; +* every `wombat.module.deployment.poll-interval`, deploys the latest version if it runs an older one, and writes a + **heartbeat**: the version it runs, whether the latest one failed to build there, and the modules it detects. + +Replicas thus converge on a redeploy within one poll interval. An instance which missed three heartbeats is considered gone: it is no longer listed, nor counted to tell whether a module is missing. Versions only go forward: an instance never deploys a version older than the one it runs. + +Instances detecting different modules, because their extension folders differ, are flagged on the Modules page. A +deployment requesting a module an instance does not detect is still deployed there, without that module, which the +heartbeat reports. + +### Failures + +A core that fails to build is not swapped in: the instance keeps running its previous core, and its heartbeat reports +the failure and associated error, shown on the Modules page. +The poll does not retry a version that failed: fix the cause, then redeploy. Should the build fail at startup, the instance will not start at all, since there is no previous core to keep running. + +Two redeploys requested at the same time on two instances record the same version once: the second gets `409`, and +every instance converges on the first. + +### Workers + +A [worker](distributed.md#workers) has no UI nor API, and its database is in memory: it enables every module it detects, +deploys its core at startup, and neither polls nor writes a heartbeat. It only declares the assets it samples. To skip +the metrics of a module, disable it on the aggregator. + +## Configuration + +| Setting | Environment variable | Default | Description | +|------------------------------------------|------------------------------------------|----------------------|---------------------------------------------------------------| +| `wombat.module.extension.path` | `WOMBAT_MODULE_EXTENSION_PATH` | `extensions` | Folder whose jars are loaded as modules, at startup | +| `wombat.module.deployment.poll-interval` | `WOMBAT_MODULE_DEPLOYMENT_POLL_INTERVAL` | `30s` | How often an instance deploys the latest core, and heartbeats | +| `wombat.module.deployment.instance-id` | `WOMBAT_MODULE_DEPLOYMENT_INSTANCE_ID` | `${HOSTNAME:wombat}` | Identifies an instance among the replicas | + +The instance id is the pod name in Kubernetes, and `wombat` when `HOSTNAME` is unset: a restarted instance takes its +heartbeat back. Under plain Docker, `HOSTNAME` is the container id, which changes when the container is recreated; set the instance id to keep it. + +## API and permissions + +| Route | Permission | Does | +|---------------------------------|---------------------|---------------------------------------------------------------------------| +| `GET /api/modules`, `GET /{id}` | `api.module.read` | The modules, their state, whether the core serving the request holds them | +| `GET /api/modules/{id}/schema` | `api.module.read` | The JSON schema of the module's assets | +| `PUT /api/modules/{id}` | `api.module.manage` | Enables or disables a module (`{"enabled": false}`); `409` if missing | +| `DELETE /api/modules/{id}` | `api.module.manage` | Deletes a missing module; `409` if detected | +| `GET /api/modules/deployment` | `api.module.read` | The latest deployment, the live instances, the changes not deployed | +| `POST /api/modules/deployment` | `api.module.manage` | Redeploys the core; `409` if another redeploy was requested at once | + +`api.module.manage` is held by admins, and can be granted to API keys. The Modules pages need `ui.modules.read`, held by every role; toggling, redeploying and deleting from them also need `ui.modules.manage`, held by admins. See +[access control](access-control.md). diff --git a/src/main/java/tech/illuin/wombat/engine/module/DeploymentInfo.java b/src/main/java/tech/illuin/wombat/engine/module/DeploymentInfo.java index 60cada6..928d78d 100644 --- a/src/main/java/tech/illuin/wombat/engine/module/DeploymentInfo.java +++ b/src/main/java/tech/illuin/wombat/engine/module/DeploymentInfo.java @@ -14,24 +14,24 @@ * @param pending the modules whose registry state the latest deployment does not hold yet * @param pollIntervalSeconds how often instances poll for the latest deployment */ -public record DeploymentInfo(Latest latest, List instances, List pending, long pollIntervalSeconds) +public record DeploymentInfo(LatestDeployment latest, List instances, List pending, long pollIntervalSeconds) { /** * @param id the full hash: clients shorten it to 7 characters, like a commit id * @param modules the modules enabled when it was requested */ - public record Latest(long version, String id, Instant requestedAt, String requestedBy, Set modules) {} + public record LatestDeployment(long version, String id, Instant requestedAt, String requestedBy, Set modules) {} /** * @param deployedId null if the deployment the instance runs is unknown here * @param current whether this is the instance serving the request */ - public record Instance( + public record CoreInstance( String instanceId, long deployedVersion, String deployedId, Instant deployedAt, - CoreInstanceEntity.Status status, + CoreInstanceEntity.InstanceStatus status, String error, Set detectedModules, Set missingModules, @@ -44,5 +44,5 @@ public record PendingModule(String type, boolean enabled) {} * A redeploy recorded, and the outcome of deploying it on the instance which recorded it; the others follow on * their next poll. */ - public record Redeployed(long version, String id, CoreDeploymentService.Result local) {} + public record Redeployed(long version, String id, CoreDeploymentService.DeployResult local) {} } diff --git a/src/main/java/tech/illuin/wombat/engine/module/ModuleService.java b/src/main/java/tech/illuin/wombat/engine/module/ModuleService.java index 489f643..0c0db2c 100644 --- a/src/main/java/tech/illuin/wombat/engine/module/ModuleService.java +++ b/src/main/java/tech/illuin/wombat/engine/module/ModuleService.java @@ -84,7 +84,7 @@ public void remove(String name) public DeploymentInfo deployment() { CoreDeploymentEntity latest = this.latest(); - List instances = this.deployments.liveInstances().stream() + List instances = this.deployments.liveInstances().stream() .map(this::instance) .toList(); List pending = this.registry.listModules().stream() @@ -94,7 +94,7 @@ public DeploymentInfo deployment() .toList(); return new DeploymentInfo( - new DeploymentInfo.Latest(latest.version, latest.id, latest.requestedAt, latest.requestedBy, latest.enabledModules), + new DeploymentInfo.LatestDeployment(latest.version, latest.id, latest.requestedAt, latest.requestedBy, latest.enabledModules), instances, pending, this.deployments.pollInterval().toSeconds() @@ -119,9 +119,9 @@ private CoreDeploymentEntity latest() .orElseThrow(() -> new IllegalStateException("No core deployment is recorded, the startup records one")); } - private DeploymentInfo.Instance instance(CoreInstanceEntity entity) + private DeploymentInfo.CoreInstance instance(CoreInstanceEntity entity) { - return new DeploymentInfo.Instance( + return new DeploymentInfo.CoreInstance( entity.instanceId, entity.deployedVersion, this.deployments.find(entity.deployedVersion).map(deployment -> deployment.id).orElse(null), diff --git a/src/main/java/tech/illuin/wombat/engine/module/persistence/CoreInstanceEntity.java b/src/main/java/tech/illuin/wombat/engine/module/persistence/CoreInstanceEntity.java index 35e9cb1..2f0532a 100644 --- a/src/main/java/tech/illuin/wombat/engine/module/persistence/CoreInstanceEntity.java +++ b/src/main/java/tech/illuin/wombat/engine/module/persistence/CoreInstanceEntity.java @@ -30,7 +30,7 @@ public class CoreInstanceEntity /** Whether the latest deployment failed to build here; the instance then keeps running its deployed version. */ @Enumerated(EnumType.STRING) @Column(nullable = false, columnDefinition = "TEXT") - public Status status; + public InstanceStatus status; @Column public String error; @@ -47,7 +47,7 @@ public class CoreInstanceEntity @Column(name = "heartbeat_at", nullable = false, columnDefinition = "INTEGER") public Instant heartbeatAt; - public enum Status + public enum InstanceStatus { ok, failed, diff --git a/src/main/java/tech/illuin/wombat/engine/module/runtime/CoreDeploymentService.java b/src/main/java/tech/illuin/wombat/engine/module/runtime/CoreDeploymentService.java index 93bf5f3..0573be8 100644 --- a/src/main/java/tech/illuin/wombat/engine/module/runtime/CoreDeploymentService.java +++ b/src/main/java/tech/illuin/wombat/engine/module/runtime/CoreDeploymentService.java @@ -180,18 +180,18 @@ private CoreDeploymentEntity bootstrap() /* A build failure is reported rather than thrown: the running deployment is kept, and the heartbeat tells it */ @SuppressWarnings("IllegalCatch") - private Result deployHere(CoreDeploymentEntity deployment) + private DeployResult deployHere(CoreDeploymentEntity deployment) { Set before = CoreRuntime.names(this.runtime.current().types()); try { Set after = CoreRuntime.names(this.runtime.deploy(deployment.version, deployment.id, deployment.enabledModules).types()); this.failure = null; - return Result.ok(difference(after, before), difference(before, after)); + return DeployResult.ok(difference(after, before), difference(before, after)); } catch (RuntimeException e) { logger.error("Could not deploy wombat core {} (#{}), keeping the running one: {}", CoreRuntime.shortId(deployment.id), deployment.version, e.getMessage(), e); this.failure = new Failure(deployment.version, e.getMessage()); - return Result.failed(e.getMessage()); + return DeployResult.failed(e.getMessage()); } finally { this.writeHeartbeat(); @@ -209,7 +209,7 @@ private void writeHeartbeat() entity.instanceId = this.instanceId; entity.deployedVersion = current.version(); entity.deployedAt = current.deployedAt(); - entity.status = failed == null ? CoreInstanceEntity.Status.ok : CoreInstanceEntity.Status.failed; + entity.status = failed == null ? CoreInstanceEntity.InstanceStatus.ok : CoreInstanceEntity.InstanceStatus.failed; entity.error = failed == null ? null : failed.error(); entity.detectedModules = this.detected; entity.missingModules = current.missing(); @@ -237,22 +237,22 @@ private record Failure(long version, String error) {} /** * A redeploy recorded, and the outcome of deploying it on the instance which recorded it. */ - public record Redeploy(CoreDeploymentEntity deployment, Result local) {} + public record Redeploy(CoreDeploymentEntity deployment, DeployResult local) {} /** * @param added the asset-types the new core holds, and the previous one did not * @param removed the asset-types the previous core held, and the new one does not */ - public record Result(CoreInstanceEntity.Status status, String error, Set added, Set removed) + public record DeployResult(CoreInstanceEntity.InstanceStatus status, String error, Set added, Set removed) { - static Result ok(Set added, Set removed) + static DeployResult ok(Set added, Set removed) { - return new Result(CoreInstanceEntity.Status.ok, null, added, removed); + return new DeployResult(CoreInstanceEntity.InstanceStatus.ok, null, added, removed); } - static Result failed(String error) + static DeployResult failed(String error) { - return new Result(CoreInstanceEntity.Status.failed, error, Set.of(), Set.of()); + return new DeployResult(CoreInstanceEntity.InstanceStatus.failed, error, Set.of(), Set.of()); } } } diff --git a/src/test/java/tech/illuin/wombat/endpoint/ModuleControllerTest.java b/src/test/java/tech/illuin/wombat/endpoint/ModuleControllerTest.java index 085b940..f68a79b 100644 --- a/src/test/java/tech/illuin/wombat/endpoint/ModuleControllerTest.java +++ b/src/test/java/tech/illuin/wombat/endpoint/ModuleControllerTest.java @@ -43,6 +43,7 @@ import static org.hamcrest.Matchers.contains; import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.hasItems; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.notNullValue; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -253,6 +254,23 @@ void aRedeployIsForbiddenToUsers() given().when().post("/api/modules/deployment").then().statusCode(403); } + @Test + @TestSecurity(user = "module-reader", roles = "wombat-users", augmentors = PermissionAugmentor.class) + void openApi_describesTheModuleRoutesAndTheirPayloads() + { + given().queryParam("format", "json") + .when().get("/api/openapi") + .then() + .statusCode(200) + .body("paths.'/api/modules/{id}'.keySet()", hasItems("get", "put", "delete")) + .body("paths.'/api/modules/deployment'.keySet()", hasItems("get", "post")) + .body("paths.'/api/modules/deployment'.post.responses.keySet()", hasItems("201", "409")) + .body("components.schemas.ModuleInfo.properties.keySet()", hasItems("type", "enabled", "deployed", "pending", "missing", "source", "assetCount")) + .body("components.schemas.DeploymentInfo.properties.keySet()", hasItems("latest", "instances", "pending", "pollIntervalSeconds")) + .body("components.schemas.Redeployed.properties.keySet()", hasItems("version", "id", "local")) + .body("components.schemas.InstanceStatus.enum", contains("ok", "failed")); + } + @Test void aKey_togglesModulesOnlyWithTheManagePermission() { diff --git a/src/test/java/tech/illuin/wombat/engine/module/persistence/CoreDeploymentRepositoryTest.java b/src/test/java/tech/illuin/wombat/engine/module/persistence/CoreDeploymentRepositoryTest.java index 6648c9f..abb15fb 100644 --- a/src/test/java/tech/illuin/wombat/engine/module/persistence/CoreDeploymentRepositoryTest.java +++ b/src/test/java/tech/illuin/wombat/engine/module/persistence/CoreDeploymentRepositoryTest.java @@ -64,7 +64,7 @@ void findLive_leavesOutTheInstancesWhichMissedThreeHeartbeats() stale.instanceId = "stale"; stale.deployedVersion = 1; stale.deployedAt = Instant.now().minus(Duration.ofHours(1)); - stale.status = CoreInstanceEntity.Status.ok; + stale.status = CoreInstanceEntity.InstanceStatus.ok; stale.detectedModules = Set.of("a.b"); stale.missingModules = Set.of(); stale.heartbeatAt = Instant.now().minus(Duration.ofSeconds(91)); diff --git a/src/test/java/tech/illuin/wombat/engine/module/runtime/CoreDeploymentServiceTest.java b/src/test/java/tech/illuin/wombat/engine/module/runtime/CoreDeploymentServiceTest.java index dc2c779..f5863ab 100644 --- a/src/test/java/tech/illuin/wombat/engine/module/runtime/CoreDeploymentServiceTest.java +++ b/src/test/java/tech/illuin/wombat/engine/module/runtime/CoreDeploymentServiceTest.java @@ -106,7 +106,7 @@ void aRedeployRecordsTheEnabledModulesAndDeploysThemHere() CoreDeploymentService.Redeploy redeploy = service.redeploy("alice"); assertEquals(2, this.runtime.current().version()); - assertEquals(CoreInstanceEntity.Status.ok, redeploy.local().status()); + assertEquals(CoreInstanceEntity.InstanceStatus.ok, redeploy.local().status()); assertEquals(Set.of(), redeploy.local().added()); assertEquals(Set.of(LLM), redeploy.local().removed()); } @@ -129,11 +129,11 @@ void aFailedBuildIsReported_keepsTheRunningCore_andShowsInTheHeartbeat() CoreDeploymentService.Redeploy redeploy = service.redeploy("alice"); - assertEquals(CoreInstanceEntity.Status.failed, redeploy.local().status()); + assertEquals(CoreInstanceEntity.InstanceStatus.failed, redeploy.local().status()); assertEquals("broken module", redeploy.local().error()); assertEquals(1, this.runtime.current().version()); CoreInstanceEntity heartbeat = this.lastHeartbeat(); - assertEquals(CoreInstanceEntity.Status.failed, heartbeat.status); + assertEquals(CoreInstanceEntity.InstanceStatus.failed, heartbeat.status); assertEquals(1, heartbeat.deployedVersion); } @@ -163,7 +163,7 @@ void aPollDoesNotRetryAFailedVersion_butANewerOne() when(this.deployments.findLatest()).thenReturn(Optional.of(recorded(3, K8S))); service.sync(); assertEquals(3, this.runtime.current().version()); - assertEquals(CoreInstanceEntity.Status.ok, this.lastHeartbeat().status); + assertEquals(CoreInstanceEntity.InstanceStatus.ok, this.lastHeartbeat().status); assertNull(this.lastHeartbeat().error); }