aboutsummaryrefslogtreecommitdiffstats
path: root/container-disc/src/main/java/com/yahoo/container/jdisc/ShutdownDeadline.java
blob: 5e03d60207139c162c4b2f1be63ceb94af713840 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.jdisc;

import com.yahoo.concurrent.DaemonThreadFactory;
import com.yahoo.vespa.defaults.Defaults;

import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import static com.yahoo.protect.Process.dumpHeap;
import static com.yahoo.protect.Process.logAndDie;

/**
 * Kills the JVM if the application is unable to shutdown before deadline.
 *
 * @author bjorncs
 */
class ShutdownDeadline implements AutoCloseable {

    private final String configId;
    private final ScheduledThreadPoolExecutor executor;

    ShutdownDeadline(String configId) {
        this.configId = configId;
        executor = new ScheduledThreadPoolExecutor(1, new DaemonThreadFactory("Shutdown deadline timer"));
        executor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
    }

    ShutdownDeadline schedule(long millis, boolean heapdumpOnShutdown) {
        executor.schedule(() -> onDeadline(heapdumpOnShutdown), millis, TimeUnit.MILLISECONDS);
        return this;
    }

    void cancel() { executor.shutdownNow(); }
    @Override public void close() { cancel();  }

    private void onDeadline(boolean heapdumpOnShutdown) {
        if (heapdumpOnShutdown) dumpHeap(heapdumpFilename(), true);
        logAndDie("Timed out waiting for application shutdown. Please check that all your request handlers " +
                "drain their request content channels.", true);
    }

    private String heapdumpFilename() {
        return Defaults.getDefaults().underVespaHome("var/crash/java_pid.") + sanitizeFileName(configId) + "."
                + ProcessHandle.current().pid() + ".hprof";
    }

    static String sanitizeFileName(String s) {
        return s.trim().replace('\\', '.').replaceAll("[/,;]", ".");
    }

}