summaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/maintenance/Maintainer.java
blob: 9c3c1dc1f5ede0319563cab9fa1ad2b6687728b7 (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
110
111
// 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.controller.maintenance;

import com.yahoo.component.AbstractComponent;
import com.yahoo.config.provision.HostName;
import com.yahoo.config.provision.SystemName;
import com.yahoo.vespa.curator.Lock;
import com.yahoo.vespa.hosted.controller.Controller;

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

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

    protected static final Logger log = Logger.getLogger(Maintainer.class.getName());

    private final Controller controller;
    private final Duration maintenanceInterval;
    private final JobControl jobControl;
    private final ScheduledExecutorService service;
    private final String name;

    /** The systems in which this maintainer should run */
    private final Set<SystemName> activeSystems;

    public Maintainer(Controller controller, Duration interval, JobControl jobControl) {
        this(controller, interval, jobControl, null, EnumSet.allOf(SystemName.class));
    }

    public Maintainer(Controller controller, Duration interval, JobControl jobControl, String name, Set<SystemName> activeSystems) {
        if (interval.isNegative() || interval.isZero())
            throw new IllegalArgumentException("Interval must be positive, but was " + interval);

        this.controller = controller;
        this.maintenanceInterval = interval;
        this.jobControl = jobControl;
        this.name = name;
        this.activeSystems = Set.copyOf(activeSystems);

        service = new ScheduledThreadPoolExecutor(1);
        long delay = staggeredDelay(controller.curator().cluster(), controller.hostname(), controller.clock().instant(), interval);
        service.scheduleAtFixedRate(this, delay, interval.toMillis(), TimeUnit.MILLISECONDS);
        jobControl.started(name());
    }
    
    protected Controller controller() { return controller; }
    
    @Override
    public void run() {
        try {
            if ( ! activeSystems.contains(controller.system())) {
                return;
            }
            if (jobControl.isActive(name())) {
                try (Lock lock = jobControl.curator().lockMaintenanceJob(name())) {
                    maintain();
                }
            }
        }
        catch (TimeoutException e) {
            // another controller instance is running this job at the moment; ok
        }
        catch (Throwable t) {
            log.log(Level.WARNING, this + " failed. Will retry in " + maintenanceInterval.toMinutes() + " minutes", t);
        }
    }

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

    /** Called once each time this maintenance job should run */
    protected abstract void maintain();

    public Duration maintenanceInterval() { return maintenanceInterval; }
    
    public final String name() {
        return name == null ? this.getClass().getSimpleName() : name;
    }
    
    /** Returns the name of this */
    @Override
    public final String toString() {
        return name();
    }

    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();
    }

}