diff options
author | Jon Marius Venstad <jonmv@users.noreply.github.com> | 2020-12-08 01:16:50 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-08 01:16:50 +0100 |
commit | ed28b553908170648a2a5467d478d7830057a5b8 (patch) | |
tree | 3945720b9de8d56e84e49810c80356fa19771617 /container-disc | |
parent | 82d1a71afff67b0004291be4091d8a00d0dfa79e (diff) |
Revert "Revert "Revert "Always deconstruct in reverse creation order, including Provider objects"""
Diffstat (limited to 'container-disc')
-rw-r--r-- | container-disc/src/main/java/com/yahoo/container/jdisc/component/Deconstructor.java | 22 | ||||
-rw-r--r-- | container-disc/src/test/java/com/yahoo/container/jdisc/component/DeconstructorTest.java | 25 |
2 files changed, 23 insertions, 24 deletions
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 0bb60c8aca8..e60e8d407cd 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 @@ -2,13 +2,10 @@ package com.yahoo.container.jdisc.component; import com.yahoo.component.AbstractComponent; -import com.yahoo.component.Deconstructable; import com.yahoo.concurrent.ThreadFactoryFactory; import com.yahoo.container.di.ComponentDeconstructor; import com.yahoo.container.di.componentgraph.Provider; import com.yahoo.jdisc.SharedResource; - -import java.util.List; import java.util.logging.Level; import org.osgi.framework.Bundle; import org.osgi.framework.BundleException; @@ -35,7 +32,7 @@ public class Deconstructor implements ComponentDeconstructor { private static final Logger log = Logger.getLogger(Deconstructor.class.getName()); - final ScheduledExecutorService executor = + private final ScheduledExecutorService executor = Executors.newScheduledThreadPool(1, ThreadFactoryFactory.getThreadFactory("component-deconstructor")); private final Duration delay; @@ -45,8 +42,8 @@ public class Deconstructor implements ComponentDeconstructor { } @Override - public void deconstruct(List<Object> components, Collection<Bundle> bundles) { - Collection<Deconstructable> destructibleComponents = new ArrayList<>(); + public void deconstruct(Collection<Object> components, Collection<Bundle> bundles) { + Collection<AbstractComponent> destructibleComponents = new ArrayList<>(); for (var component : components) { if (component instanceof AbstractComponent) { AbstractComponent abstractComponent = (AbstractComponent) component; @@ -54,7 +51,10 @@ public class Deconstructor implements ComponentDeconstructor { destructibleComponents.add(abstractComponent); } } else if (component instanceof Provider) { - destructibleComponents.add((Deconstructable) component); + // TODO Providers should most likely be deconstructed similarly to AbstractComponent + log.log(FINE, () -> "Starting deconstruction of provider " + component); + ((Provider<?>) component).deconstruct(); + log.log(FINE, () -> "Finished deconstruction of provider " + component); } else if (component instanceof SharedResource) { log.log(FINE, () -> "Releasing container reference to resource " + component); // No need to delay release, as jdisc does ref-counting @@ -69,10 +69,10 @@ public class Deconstructor implements ComponentDeconstructor { private static class DestructComponentTask implements Runnable { private final Random random = new Random(System.nanoTime()); - private final Collection<Deconstructable> components; + private final Collection<AbstractComponent> components; private final Collection<Bundle> bundles; - DestructComponentTask(Collection<Deconstructable> components, Collection<Bundle> bundles) { + DestructComponentTask(Collection<AbstractComponent> components, Collection<Bundle> bundles) { this.components = components; this.bundles = bundles; } @@ -89,10 +89,10 @@ public class Deconstructor implements ComponentDeconstructor { @Override public void run() { for (var component : components) { - log.log(FINE, () -> "Starting deconstruction of " + component); + log.log(FINE, () -> "Starting deconstruction of component " + component); try { component.deconstruct(); - log.log(FINE, () -> "Finished deconstructing of " + component); + log.log(FINE, () -> "Finished deconstructing of component " + component); } catch (Exception | NoClassDefFoundError e) { // May get class not found due to it being already unloaded log.log(WARNING, "Exception thrown when deconstructing component " + component, e); } catch (Error e) { 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 9ae05f5c193..efdc8f44c17 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 @@ -9,9 +9,6 @@ import com.yahoo.jdisc.SharedResource; import org.junit.Before; import org.junit.Test; -import java.util.List; -import java.util.concurrent.TimeUnit; - import static java.util.Collections.emptyList; import static java.util.Collections.singleton; import static org.junit.Assert.assertTrue; @@ -31,25 +28,25 @@ 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(List.of(abstractComponent), emptyList()); - deconstructor.executor.shutdown(); - deconstructor.executor.awaitTermination(1, TimeUnit.MINUTES); + deconstructor.deconstruct(singleton(abstractComponent), emptyList()); + int cnt = 0; + while (! abstractComponent.destructed && (cnt++ < 12000)) { + Thread.sleep(10); + } assertTrue(abstractComponent.destructed); } @Test - public void require_provider_destructed() throws InterruptedException { + public void require_provider_destructed() { TestProvider provider = new TestProvider(); - deconstructor.deconstruct(List.of(provider), emptyList()); - deconstructor.executor.shutdown(); - deconstructor.executor.awaitTermination(1, TimeUnit.MINUTES); + deconstructor.deconstruct(singleton(provider), emptyList()); assertTrue(provider.destructed); } @Test public void require_shared_resource_released() { TestSharedResource sharedResource = new TestSharedResource(); - deconstructor.deconstruct(List.of(sharedResource), emptyList()); + deconstructor.deconstruct(singleton(sharedResource), emptyList()); assertTrue(sharedResource.released); } @@ -58,8 +55,10 @@ public class DeconstructorTest { var bundle = new UninstallableMockBundle(); // Done by executor, so it takes some time even with a 0 delay. deconstructor.deconstruct(emptyList(), singleton(bundle)); - deconstructor.executor.shutdown(); - deconstructor.executor.awaitTermination(1, TimeUnit.MINUTES); + int cnt = 0; + while (! bundle.uninstalled && (cnt++ < 12000)) { + Thread.sleep(10); + } assertTrue(bundle.uninstalled); } |