summaryrefslogtreecommitdiffstats
path: root/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/maintenance/Maintainer.java
blob: 49ede9962eb929aa1964c73fb425581bc7e0a906 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.provision.maintenance;

import com.yahoo.component.AbstractComponent;
import com.yahoo.config.provision.HostName;
import com.yahoo.vespa.hosted.provision.NodeRepository;

import java.time.Duration;
import java.time.Instant;
import java.util.List;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * A maintainer is some job which runs at a fixed rate to perform some maintenance task on the node repo.
 *
 * @author bratseth
 */
public abstract class Maintainer extends AbstractComponent implements Runnable {

    protected final Logger log = Logger.getLogger(this.getClass().getName());

    private final NodeRepository nodeRepository;
    private final Duration interval;
    private final JobControl jobControl;

    private final ScheduledExecutorService service;

    public Maintainer(NodeRepository nodeRepository, Duration interval, JobControl jobControl) {
        this.nodeRepository = nodeRepository;
        this.interval = interval;
        this.jobControl = jobControl;

        HostName hostname = HostName.from(com.yahoo.net.HostName.getLocalhost());
        long delay = staggeredDelay(nodeRepository.database().cluster(), hostname, nodeRepository.clock().instant(), interval);
        service = new ScheduledThreadPoolExecutor(1);
        service.scheduleAtFixedRate(this, delay, interval.toMillis(), TimeUnit.MILLISECONDS);
        jobControl.started(name());
    }

    /** Returns the node repository */
    protected NodeRepository nodeRepository() { return nodeRepository; }

    protected static Duration min(Duration a, Duration b) {
        return a.toMillis() < b.toMillis() ? a : b;
    }

    /** Returns the interval at which this job is set to run */
    protected Duration interval() { return interval; }

    @Override
    public void run() {
        try {
            if (jobControl.isActive(name()))
                maintain();
        } catch (Throwable e) {
            log.log(Level.WARNING, this + " failed. Will retry in " + interval.toMinutes() + " minutes", e);
        }
    }

    @Override
    public void deconstruct() {
        this.service.shutdown();
    }

    /** Returns the simple name of this job */
    @Override
    public final String toString() { return name(); }

    /** Called once each time this maintenance job should run */
    protected abstract void maintain();
    
    private String name() { return this.getClass().getSimpleName(); }

    static long staggeredDelay(List<HostName> cluster, HostName host, Instant now, Duration interval) {
        if ( ! cluster.contains(host))
            return interval.toMillis();

        long offset = cluster.indexOf(host) * interval.toMillis() / cluster.size();
        long timeUntilNextRun = Math.floorMod(offset - now.toEpochMilli(), interval.toMillis());
        return timeUntilNextRun + interval.toMillis() / cluster.size();
    }

}