From cfa9a4fc4d3a210780b687040c34731e2596002d Mon Sep 17 00:00:00 2001 From: gjoranv Date: Sat, 26 Oct 2019 23:25:21 +0200 Subject: Deconstruct all components in one task. The executor has only one thread, so this should not make a significant difference. --- .../container/jdisc/component/Deconstructor.java | 55 ++++++++++++---------- 1 file changed, 29 insertions(+), 26 deletions(-) (limited to 'container-disc') 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 4920051cbee..284e3a69b61 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.ArrayList; import java.util.Collection; import java.util.Random; import java.util.concurrent.Executors; @@ -35,17 +36,17 @@ public class Deconstructor implements ComponentDeconstructor { this.delay = delayDeconstruction ? Duration.ofSeconds(60) : Duration.ZERO; } - @Override public void deconstruct(Collection components) { + Collection destructibleComponents = new ArrayList<>(); for (var component : components) { if (component instanceof AbstractComponent) { AbstractComponent abstractComponent = (AbstractComponent) component; if (abstractComponent.isDeconstructable()) { - executor.schedule(new DestructComponentTask(abstractComponent), delay.getSeconds(), TimeUnit.SECONDS); + destructibleComponents.add(abstractComponent); } } else if (component instanceof Provider) { - // TODO Providers should most likely be deconstructed similarily to AbstractComponent + // TODO Providers should most likely be deconstructed similarly to AbstractComponent log.info("Starting deconstruction of provider " + component); ((Provider) component).deconstruct(); log.info("Finished deconstruction of provider " + component); @@ -55,15 +56,17 @@ public class Deconstructor implements ComponentDeconstructor { ((SharedResource) component).release(); } } + if (! destructibleComponents.isEmpty()) + executor.schedule(new DestructComponentTask(destructibleComponents), delay.getSeconds(), TimeUnit.SECONDS); } private static class DestructComponentTask implements Runnable { private final Random random = new Random(System.nanoTime()); - private final AbstractComponent component; + private final Collection components; - DestructComponentTask(AbstractComponent component) { - this.component = component; + DestructComponentTask(Collection components) { + this.components = components; } /** @@ -77,29 +80,29 @@ public class Deconstructor implements ComponentDeconstructor { @Override public void run() { - log.info("Starting deconstruction of component " + component); - try { - component.deconstruct(); - log.info("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) { + log.info("Starting deconstruction of " + components.size() + " components"); + for (var component : components) { + log.info("Starting deconstruction of component " + component); try { - Duration shutdownDelay = getRandomizedShutdownDelay(); - log.log(LogLevel.FATAL, "Error when deconstructing component " + component + ". Will sleep for " + - shutdownDelay.getSeconds() + " seconds then restart", e); - Thread.sleep(shutdownDelay.toMillis()); + component.deconstruct(); + log.info("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) { + try { + Duration shutdownDelay = getRandomizedShutdownDelay(); + log.log(LogLevel.FATAL, "Error when deconstructing component " + component + ". Will sleep for " + + shutdownDelay.getSeconds() + " seconds then restart", e); + Thread.sleep(shutdownDelay.toMillis()); + } catch (InterruptedException exception) { + log.log(WARNING, "Randomized wait before dying disrupted. Dying now."); + } + com.yahoo.protect.Process.logAndDie("Shutting down due to error when deconstructing component " + component); + } catch (Throwable e) { + log.log(WARNING, "Non-error not exception throwable thrown when deconstructing component " + component, e); } - catch (InterruptedException exception) { - log.log(WARNING, "Randomized wait before dying disrupted. Dying now."); - } - com.yahoo.protect.Process.logAndDie("Shutting down due to error when deconstructing component " + component); - } - catch (Throwable e) { - log.log(WARNING, "Non-error not exception throwable thrown when deconstructing component " + component, e); } + log.info("Finished deconstructing " + components.size() + " components"); } } -- cgit v1.2.3