diff options
author | Jon Marius Venstad <venstad@gmail.com> | 2020-12-07 13:37:24 +0100 |
---|---|---|
committer | Jon Marius Venstad <venstad@gmail.com> | 2020-12-07 13:37:24 +0100 |
commit | bdb102917700d9f698423b5a1d62ead8a06f5b5a (patch) | |
tree | 1daacf13716d072b1a5578ae03fb35ae2e61a81f /container-di | |
parent | c7548568b93ac5552667ca34a754bf6b36b5033e (diff) |
Always deconstruct in reverse creation order, including Provider objects
Diffstat (limited to 'container-di')
6 files changed, 24 insertions, 11 deletions
diff --git a/container-di/abi-spec.json b/container-di/abi-spec.json index a9dda2ae224..02cc29cd07f 100644 --- a/container-di/abi-spec.json +++ b/container-di/abi-spec.json @@ -1,15 +1,16 @@ { "com.yahoo.container.di.componentgraph.Provider": { "superClass": "java.lang.Object", - "interfaces": [], + "interfaces": [ + "com.yahoo.component.Deconstructable" + ], "attributes": [ "public", "interface", "abstract" ], "methods": [ - "public abstract java.lang.Object get()", - "public abstract void deconstruct()" + "public abstract java.lang.Object get()" ], "fields": [] } diff --git a/container-di/src/main/java/com/yahoo/container/di/ComponentDeconstructor.java b/container-di/src/main/java/com/yahoo/container/di/ComponentDeconstructor.java index 61497cf71bc..4e3881a6fe6 100644 --- a/container-di/src/main/java/com/yahoo/container/di/ComponentDeconstructor.java +++ b/container-di/src/main/java/com/yahoo/container/di/ComponentDeconstructor.java @@ -4,6 +4,7 @@ package com.yahoo.container.di; import org.osgi.framework.Bundle; import java.util.Collection; +import java.util.List; /** * @author gjoranv @@ -11,6 +12,7 @@ import java.util.Collection; */ public interface ComponentDeconstructor { - void deconstruct(Collection<Object> components, Collection<Bundle> bundles); + /** Deconstructs the given components in order, then the given bundles. */ + void deconstruct(List<Object> components, Collection<Bundle> bundles); } diff --git a/container-di/src/main/java/com/yahoo/container/di/Container.java b/container-di/src/main/java/com/yahoo/container/di/Container.java index af580767a17..7fc2f2e55bc 100644 --- a/container-di/src/main/java/com/yahoo/container/di/Container.java +++ b/container-di/src/main/java/com/yahoo/container/di/Container.java @@ -21,6 +21,7 @@ import com.yahoo.container.di.config.SubscriberFactory; import com.yahoo.vespa.config.ConfigKey; import org.osgi.framework.Bundle; +import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashSet; @@ -159,10 +160,16 @@ public class Container { private void deconstructObsoleteComponents(ComponentGraph oldGraph, ComponentGraph newGraph, Collection<Bundle> obsoleteBundles) { - IdentityHashMap<Object, Object> oldComponents = new IdentityHashMap<>(); - oldGraph.allConstructedComponentsAndProviders().forEach(c -> oldComponents.put(c, null)); - newGraph.allConstructedComponentsAndProviders().forEach(oldComponents::remove); - componentDeconstructor.deconstruct(oldComponents.keySet(), obsoleteBundles); + Map<Object, ?> newComponents = new IdentityHashMap<>(newGraph.size()); + for (Object component : newGraph.allConstructedComponentsAndProviders()) + newComponents.put(component, null); + + List<Object> obsoleteComponents = new ArrayList<>(); + for (Object component : oldGraph.allConstructedComponentsAndProviders()) + if ( ! newComponents.containsKey(component)) + obsoleteComponents.add(component); + + componentDeconstructor.deconstruct(obsoleteComponents, obsoleteBundles); } private Set<Bundle> installApplicationBundles(Map<ConfigKey<? extends ConfigInstance>, ConfigInstance> configsIncludingBootstrapConfigs) { diff --git a/container-di/src/main/java/com/yahoo/container/di/componentgraph/Provider.java b/container-di/src/main/java/com/yahoo/container/di/componentgraph/Provider.java index a2d193654ea..3fd3195e5dd 100644 --- a/container-di/src/main/java/com/yahoo/container/di/componentgraph/Provider.java +++ b/container-di/src/main/java/com/yahoo/container/di/componentgraph/Provider.java @@ -1,6 +1,8 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.container.di.componentgraph; +import com.yahoo.component.Deconstructable; + /** * <p>Provides a component of the parameter type T. * If (and only if) dependency injection does not have a component of type T, @@ -17,9 +19,8 @@ package com.yahoo.container.di.componentgraph; * @author Tony Vaagenes * @author gjoranv */ -public interface Provider<T> { +public interface Provider<T> extends Deconstructable { T get(); - void deconstruct(); } diff --git a/container-di/src/main/java/com/yahoo/container/di/componentgraph/core/ComponentGraph.java b/container-di/src/main/java/com/yahoo/container/di/componentgraph/core/ComponentGraph.java index 256c965052a..fef2809f236 100644 --- a/container-di/src/main/java/com/yahoo/container/di/componentgraph/core/ComponentGraph.java +++ b/container-di/src/main/java/com/yahoo/container/di/componentgraph/core/ComponentGraph.java @@ -166,6 +166,7 @@ public class ComponentGraph { } } + /** All constructed components and providers of this, in reverse creation order, i.e., suited for ordered deconstruction. */ public List<Object> allConstructedComponentsAndProviders() { List<Node> orderedNodes = topologicalSort(nodes()); Collections.reverse(orderedNodes); diff --git a/container-di/src/test/java/com/yahoo/container/di/ContainerTest.java b/container-di/src/test/java/com/yahoo/container/di/ContainerTest.java index 19f277ff8fb..d39e8a36aed 100644 --- a/container-di/src/test/java/com/yahoo/container/di/ContainerTest.java +++ b/container-di/src/test/java/com/yahoo/container/di/ContainerTest.java @@ -19,6 +19,7 @@ import org.osgi.framework.Bundle; import org.osgi.framework.BundleException; import java.util.Collection; +import java.util.List; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -380,7 +381,7 @@ public class ContainerTest extends ContainerTestBase { public static class TestDeconstructor implements ComponentDeconstructor { @Override - public void deconstruct(Collection<Object> components, Collection<Bundle> bundles) { + public void deconstruct(List<Object> components, Collection<Bundle> bundles) { components.forEach(component -> { if (component instanceof DestructableComponent) { DestructableComponent vespaComponent = (DestructableComponent) component; |