package com.yahoo.vespa.hosted.provision.provisioning;

import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.yahoo.vespa.hosted.provision.persistence.CuratorDatabaseClient;

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

/**
 * 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 CuratorDatabaseClient database;
    private final Clock clock;

    private Supplier<Optional<Instant>> checkAfter;

    public FirmwareChecks(CuratorDatabaseClient database, Clock clock) {
        this.database = database;
        this.clock = clock;
        createCache();
    }

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

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

    private void createCache() {
        checkAfter = Suppliers.memoizeWithExpiration(database::readFirmwareCheck, cacheExpiry.toMillis(), TimeUnit.MILLISECONDS);
    }

}

@@ -945,16 +945,25 @@
 private 
+volatile 
 Supplier

Make volatile