aboutsummaryrefslogtreecommitdiffstats
path: root/node-repository/src/main/java/com/yahoo/vespa/hosted/provision/provisioning/FirmwareChecks.java
blob: ff375f2abe114790d8cac3b06c95a71b5dfa80d9 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.provision.provisioning;

import com.yahoo.lang.CachedSupplier;
import com.yahoo.vespa.hosted.provision.persistence.CuratorDb;

import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.util.Optional;

/**
 * Keeps cached data about when to do a firmware check on the hosts managed by a node repository.
 *
 * The data kept here is managed through POST and DELETE to <code>/nodes/v2/upgrade/firmware</code>
 * <p>
 * Reads and writes are not locked, as writes do not depend on prior state.
 * <p>
 * Local cache expires periodically, and on writes from this host, for testing.
 *
 * @author jonmv
 */
public class FirmwareChecks {

    private static final Duration cacheExpiry = Duration.ofMinutes(1);

    private final CuratorDb database;
    private final Clock clock;
    private final CachedSupplier<Optional<Instant>> checkAfter;

    public FirmwareChecks(CuratorDb database, Clock clock) {
        this.database = database;
        this.clock = clock;
        this.checkAfter = new CachedSupplier<>(database::readFirmwareCheck, cacheExpiry);
    }

    /** Returns the instant after which a firmware check is required, or empty if none currently are. */
    public Optional<Instant> requiredAfter() {
        return checkAfter.get();
    }

    /** Requests a firmware check for all hosts managed by this node repository. */
    public void request() {
        database.writeFirmwareCheck(Optional.of(clock.instant()));
        checkAfter.invalidate();
    }

    /** Clears any outstanding firmware checks for this node repository. */
    public void cancel() {
        database.writeFirmwareCheck(Optional.empty());
        checkAfter.invalidate();
    }

}