blob: 284e3a69b610230afe2aefbe4874327b1217686f (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.jdisc.component;
import com.yahoo.component.AbstractComponent;
import com.yahoo.concurrent.ThreadFactoryFactory;
import com.yahoo.container.di.ComponentDeconstructor;
import com.yahoo.container.di.componentgraph.Provider;
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;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.logging.Logger;
import static java.util.logging.Level.WARNING;
/**
* @author Tony Vaagenes
* @author gv
*/
public class Deconstructor implements ComponentDeconstructor {
private static final Logger log = Logger.getLogger(Deconstructor.class.getName());
private final ScheduledExecutorService executor =
Executors.newScheduledThreadPool(1, ThreadFactoryFactory.getThreadFactory("component-deconstructor"));
private final Duration delay;
public Deconstructor(boolean delayDeconstruction) {
this.delay = delayDeconstruction ? Duration.ofSeconds(60) : Duration.ZERO;
}
@Override
public void deconstruct(Collection<Object> components) {
Collection<AbstractComponent> destructibleComponents = new ArrayList<>();
for (var component : components) {
if (component instanceof AbstractComponent) {
AbstractComponent abstractComponent = (AbstractComponent) component;
if (abstractComponent.isDeconstructable()) {
destructibleComponents.add(abstractComponent);
}
} else if (component instanceof Provider) {
// 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);
} 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();
}
}
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 Collection<AbstractComponent> components;
DestructComponentTask(Collection<AbstractComponent> components) {
this.components = components;
}
/**
* Returns a random delay betweeen 0 and 10 minutes which will be different across identical containers invoking this at the same time.
* Used to randomize restart to avoid simultaneous cluster restarts.
*/
private Duration getRandomizedShutdownDelay() {
long seconds = (long) random.nextDouble() * 60 * 10;
return Duration.ofSeconds(seconds);
}
@Override
public void run() {
log.info("Starting deconstruction of " + components.size() + " components");
for (var component : components) {
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) {
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);
}
}
log.info("Finished deconstructing " + components.size() + " components");
}
}
}
|