diff options
9 files changed, 59 insertions, 51 deletions
diff --git a/container-core/src/main/java/com/yahoo/container/core/config/HandlersConfigurerDi.java b/container-core/src/main/java/com/yahoo/container/core/config/HandlersConfigurerDi.java index a7471bc1771..dd8b2605820 100644 --- a/container-core/src/main/java/com/yahoo/container/core/config/HandlersConfigurerDi.java +++ b/container-core/src/main/java/com/yahoo/container/core/config/HandlersConfigurerDi.java @@ -100,6 +100,7 @@ public class HandlersConfigurerDi { } + // TODO Vespa 8: Remove, only used for Jersey @Override public BundleClasses getBundleClasses(ComponentSpecification bundleSpec, Set<String> packagesToScan) { //Temporary hack: Using class name since ClassLoaderOsgiFramework is not available at compile time in this bundle. diff --git a/container-core/src/main/java/com/yahoo/container/core/config/testutil/HandlersConfigurerTestWrapper.java b/container-core/src/main/java/com/yahoo/container/core/config/testutil/HandlersConfigurerTestWrapper.java index 1fd90125cde..684a45aeac1 100644 --- a/container-core/src/main/java/com/yahoo/container/core/config/testutil/HandlersConfigurerTestWrapper.java +++ b/container-core/src/main/java/com/yahoo/container/core/config/testutil/HandlersConfigurerTestWrapper.java @@ -19,6 +19,7 @@ import com.yahoo.osgi.MockOsgi; import java.io.File; import java.io.IOException; +import java.util.Collection; import java.util.LinkedHashSet; import java.util.Random; import java.util.Set; @@ -102,15 +103,12 @@ public class HandlersConfigurerTestWrapper { } private ComponentDeconstructor getTestDeconstructor() { - return new ComponentDeconstructor() { - @Override - public void deconstruct(Object component) { - if (component instanceof AbstractComponent) { - AbstractComponent abstractComponent = (AbstractComponent) component; - if (abstractComponent.isDeconstructable()) - ((AbstractComponent) component).deconstruct(); + return components -> components.forEach(component -> { + if (component instanceof AbstractComponent) { + AbstractComponent abstractComponent = (AbstractComponent) component; + if (abstractComponent.isDeconstructable()) abstractComponent.deconstruct(); } - }}; + }); } public void reloadConfig() { 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 dbfb842204d..09f72c9d86d 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 @@ -1,10 +1,12 @@ // 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; +import java.util.Collection; + /** * @author gjoranv * @author Tony Vaagenes */ public interface ComponentDeconstructor { - void deconstruct(Object component); + void deconstruct(Collection<Object> components); } 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 0c534b0673c..f82fd22f40d 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 @@ -69,7 +69,7 @@ public class Container { IdentityHashMap<Object, Object> oldComponents = new IdentityHashMap<>(); oldGraph.allConstructedComponentsAndProviders().forEach(c -> oldComponents.put(c, null)); newGraph.allConstructedComponentsAndProviders().forEach(oldComponents::remove); - oldComponents.keySet().forEach(componentDeconstructor::deconstruct); + componentDeconstructor.deconstruct(oldComponents.keySet()); } public ComponentGraph getNewComponentGraph(ComponentGraph oldGraph, Injector fallbackInjector, boolean restartOnRedeploy) { @@ -184,7 +184,7 @@ public class Container { } } - public void installBundles(Map<ConfigKey<? extends ConfigInstance>, ConfigInstance> configsIncludingBootstrapConfigs) { + private void installBundles(Map<ConfigKey<? extends ConfigInstance>, ConfigInstance> configsIncludingBootstrapConfigs) { BundlesConfig bundlesConfig = getConfig(bundlesConfigKey, configsIncludingBootstrapConfigs); osgi.useBundles(bundlesConfig.bundle()); } @@ -209,7 +209,7 @@ public class Container { private void addNodes(ComponentsConfig componentsConfig, ComponentGraph graph) { for (ComponentsConfig.Components config : componentsConfig.components()) { - BundleInstantiationSpecification specification = bundleInstatiationSpecification(config); + BundleInstantiationSpecification specification = bundleInstantiationSpecification(config); Class<?> componentClass = osgi.resolveClass(specification); Node componentNode; @@ -234,7 +234,7 @@ public class Container { } } - public void shutdownConfigurer() { + void shutdownConfigurer() { configurer.shutdown(); } @@ -244,7 +244,7 @@ public class Container { } private void deconstructAllComponents(ComponentGraph graph, ComponentDeconstructor deconstructor) { - graph.allConstructedComponentsAndProviders().forEach(deconstructor::deconstruct); + deconstructor.deconstruct(graph.allConstructedComponentsAndProviders()); } public static <T extends ConfigInstance> T getConfig(ConfigKey<T> key, @@ -258,7 +258,7 @@ public class Container { return key.getConfigClass().cast(inst); } - public static BundleInstantiationSpecification bundleInstatiationSpecification(ComponentsConfig.Components config) { + private static BundleInstantiationSpecification bundleInstantiationSpecification(ComponentsConfig.Components config) { return BundleInstantiationSpecification.getFromStrings(config.id(), config.classId(), config.bundle()); } 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 f55d68ad708..d07aefef75b 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 @@ -158,14 +158,14 @@ public class ComponentGraph { // reset instances with modified dependencies for (Node node : topologicalSort(nodes())) { for (Node usedComponent : node.usedComponents()) { - if (!usedComponent.instance.isPresent()) { + if (usedComponent.instance.isEmpty()) { node.instance = Optional.empty(); } } } } - public Collection<?> allConstructedComponentsAndProviders() { + public Collection<Object> allConstructedComponentsAndProviders() { return nodes().stream().map(node -> node.constructedInstance().get()).collect(Collectors.toList()); } @@ -252,7 +252,7 @@ public class ComponentGraph { private Node lookupOrCreateGlobalComponent(Node node, Injector fallbackInjector, Class<?> clazz, Key<?> key) { Optional<Node> component = lookupGlobalComponent(key); - if (!component.isPresent()) { + if (component.isEmpty()) { Object instance; try { log.log(LogLevel.DEBUG, "Trying the fallback injector to create" + messageForNoGlobalComponent(clazz, node)); 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 3996dff2811..9c4891c7db2 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 @@ -15,6 +15,7 @@ import com.yahoo.container.di.config.RestApiContext; import org.junit.Ignore; import org.junit.Test; +import java.util.Collection; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -282,17 +283,13 @@ public class ContainerTest extends ContainerTestBase { public void providers_are_destructed() { writeBootstrapConfigs("id1", DestructableProvider.class); - ComponentDeconstructor deconstructor = new ComponentDeconstructor() { - @Override - public void deconstruct(Object component) { - if (component instanceof AbstractComponent) { - ((AbstractComponent) component).deconstruct(); - ; - } else if (component instanceof Provider) { - ((Provider<?>) component).deconstruct(); - } + ComponentDeconstructor deconstructor = components -> components.forEach(component -> { + if (component instanceof AbstractComponent) { + ((AbstractComponent) component).deconstruct(); + } else if (component instanceof Provider) { + ((Provider<?>) component).deconstruct(); } - }; + }); Container container = newContainer(dirConfigSource, deconstructor); @@ -376,11 +373,13 @@ public class ContainerTest extends ContainerTestBase { public static class TestDeconstructor implements ComponentDeconstructor { @Override - public void deconstruct(Object component) { - if (component instanceof DestructableComponent) { - DestructableComponent vespaComponent = (DestructableComponent) component; - vespaComponent.deconstruct(); - } + public void deconstruct(Collection<Object> components) { + components.forEach(component -> { + if (component instanceof DestructableComponent) { + DestructableComponent vespaComponent = (DestructableComponent) component; + vespaComponent.deconstruct(); + } + }); } } diff --git a/container-disc/src/main/java/com/yahoo/container/jdisc/component/Deconstructor.java b/container-disc/src/main/java/com/yahoo/container/jdisc/component/Deconstructor.java index 4fe7a07e281..4920051cbee 100644 --- a/container-disc/src/main/java/com/yahoo/container/jdisc/component/Deconstructor.java +++ b/container-disc/src/main/java/com/yahoo/container/jdisc/component/Deconstructor.java @@ -9,6 +9,7 @@ import com.yahoo.jdisc.SharedResource; import com.yahoo.log.LogLevel; import java.time.Duration; +import java.util.Collection; import java.util.Random; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; @@ -36,21 +37,23 @@ public class Deconstructor implements ComponentDeconstructor { @Override - public void deconstruct(Object component) { - if (component instanceof AbstractComponent) { - AbstractComponent abstractComponent = (AbstractComponent) component; - if (abstractComponent.isDeconstructable()) { - executor.schedule(new DestructComponentTask(abstractComponent), delay.getSeconds(), TimeUnit.SECONDS); + public void deconstruct(Collection<Object> components) { + for (var component : components) { + if (component instanceof AbstractComponent) { + AbstractComponent abstractComponent = (AbstractComponent) component; + if (abstractComponent.isDeconstructable()) { + executor.schedule(new DestructComponentTask(abstractComponent), delay.getSeconds(), TimeUnit.SECONDS); + } + } else if (component instanceof Provider) { + // TODO Providers should most likely be deconstructed similarily to AbstractComponent + log.info("Starting deconstruction of provider " + component); + ((Provider<?>) component).deconstruct(); + log.info("Finished deconstruction of provider " + component); + } else if (component instanceof SharedResource) { + log.info("Releasing container reference to resource " + component); + // No need to delay release, as jdisc does ref-counting + ((SharedResource) component).release(); } - } else if (component instanceof Provider) { - // TODO Providers should most likely be deconstructed similarily to AbstractComponent - log.info("Starting deconstruction of provider " + component); - ((Provider<?>)component).deconstruct(); - log.info("Finished deconstruction of provider " + component); - } else if (component instanceof SharedResource) { - log.info("Releasing container reference to resource " + component); - // No need to delay release, as jdisc does ref-counting - ((SharedResource)component).release(); } } diff --git a/container-disc/src/test/java/com/yahoo/container/jdisc/component/DeconstructorTest.java b/container-disc/src/test/java/com/yahoo/container/jdisc/component/DeconstructorTest.java index ff61127f617..395b1aa7c44 100644 --- a/container-disc/src/test/java/com/yahoo/container/jdisc/component/DeconstructorTest.java +++ b/container-disc/src/test/java/com/yahoo/container/jdisc/component/DeconstructorTest.java @@ -8,6 +8,9 @@ import com.yahoo.jdisc.SharedResource; import org.junit.Before; import org.junit.Test; +import java.util.Collections; + +import static java.util.Collections.singleton; import static org.junit.Assert.assertTrue; /** @@ -25,7 +28,7 @@ public class DeconstructorTest { public void require_abstract_component_destructed() throws InterruptedException { TestAbstractComponent abstractComponent = new TestAbstractComponent(); // Done by executor, so it takes some time even with a 0 delay. - deconstructor.deconstruct(abstractComponent); + deconstructor.deconstruct(singleton(abstractComponent)); int cnt = 0; while (! abstractComponent.destructed && (cnt++ < 12000)) { Thread.sleep(10); @@ -36,14 +39,14 @@ public class DeconstructorTest { @Test public void require_provider_destructed() { TestProvider provider = new TestProvider(); - deconstructor.deconstruct(provider); + deconstructor.deconstruct(singleton(provider)); assertTrue(provider.destructed); } @Test public void require_shared_resource_released() { TestSharedResource sharedResource = new TestSharedResource(); - deconstructor.deconstruct(sharedResource); + deconstructor.deconstruct(singleton(sharedResource)); assertTrue(sharedResource.released); } diff --git a/jdisc_core/src/main/java/com/yahoo/jdisc/application/BundleInstaller.java b/jdisc_core/src/main/java/com/yahoo/jdisc/application/BundleInstaller.java index fa14579a00c..f5325baa53a 100644 --- a/jdisc_core/src/main/java/com/yahoo/jdisc/application/BundleInstaller.java +++ b/jdisc_core/src/main/java/com/yahoo/jdisc/application/BundleInstaller.java @@ -9,6 +9,8 @@ import java.util.Arrays; import java.util.LinkedList; import java.util.List; +import static java.util.Collections.singletonList; + /** * <p>This is a utility class to help with installing, starting, stopping and uninstalling OSGi Bundles. You can choose * to inject an instance of this class, or it can be created explicitly by reference to a {@link OsgiFramework}.</p> @@ -74,7 +76,7 @@ public final class BundleInstaller { throw new BundleException("OSGi header '" + OsgiHeader.APPLICATION + "' not allowed for " + "non-application bundle " + bundle.getSymbolicName() + "."); } - osgiFramework.startBundles(Arrays.asList(bundle), false); + osgiFramework.startBundles(singletonList(bundle), false); } private void stop(Bundle bundle) throws BundleException { |