// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.maintenance;

import com.yahoo.concurrent.maintenance.JobControl;
import com.yahoo.concurrent.maintenance.JobControlState;
import com.yahoo.concurrent.maintenance.JobMetrics;
import com.yahoo.concurrent.maintenance.Maintainer;
import com.yahoo.jdisc.Metric;
import com.yahoo.path.Path;
import com.yahoo.transaction.Mutex;
import com.yahoo.vespa.config.server.ApplicationRepository;
import com.yahoo.vespa.curator.Curator;
import com.yahoo.vespa.flags.FlagSource;
import com.yahoo.vespa.flags.Flags;
import com.yahoo.vespa.flags.ListFlag;

import java.time.Duration;
import java.util.Map;
import java.util.Set;

/**
 * A maintainer is some job which runs at a fixed interval to perform some maintenance task in the config server.
 *
 * @author hmusum
 */
public abstract class ConfigServerMaintainer extends Maintainer {

    protected final ApplicationRepository applicationRepository;

    ConfigServerMaintainer(ApplicationRepository applicationRepository, Curator curator, FlagSource flagSource,
                           Duration initialDelay, Duration interval) {
        super(null, interval, initialDelay, new JobControl(new JobControlFlags(curator, flagSource)),
              jobMetrics(applicationRepository.metric()));
        this.applicationRepository = applicationRepository;
    }

    private static JobMetrics jobMetrics(Metric metric) {
        return new JobMetrics((job, consecutiveFailures) -> {
            metric.set("maintenance.consecutiveFailures", consecutiveFailures, metric.createContext(Map.of("job", job)));
        });
    }

    private static class JobControlFlags implements JobControlState {

        private static final Path root = Path.fromString("/configserver/v1/");

        private static final Path lockRoot = root.append("locks");
        private final Curator curator;

        private final ListFlag<String> inactiveJobsFlag;

        public JobControlFlags(Curator curator, FlagSource flagSource) {
            this.curator = curator;
            this.inactiveJobsFlag = Flags.INACTIVE_MAINTENANCE_JOBS.bindTo(flagSource);
        }

        @Override
        public Set<String> readInactiveJobs() {
            return Set.copyOf(inactiveJobsFlag.value());
        }

        @Override
        public Mutex lockMaintenanceJob(String job) {
            return curator.lock(lockRoot.append(job), Duration.ofSeconds(1));
        }

    }

}

@@ -377,16 +377,60 @@
 tainer;%0A
+import com.yahoo.config.provision.HostName;%0A
 import c
@@ -452,16 +452,16 @@
 Metric;%0A
-
 import c
@@ -760,16 +760,90 @@
 ration;%0A
+import java.time.Instant;%0Aimport java.util.Arrays;%0Aimport java.util.List;%0A
 import j
@@ -877,16 +877,52 @@
 til.Set;
+%0Aimport java.util.stream.Collectors;
 %0A%0A/**%0A *
@@ -1191,16 +1191,532 @@
 itory;%0A%0A
+    /** Creates a maintainer where maintainers on different nodes in this cluster run with even delay. */%0A    ConfigServerMaintainer(ApplicationRepository applicationRepository, Curator curator, FlagSource flagSource,%0A                           Instant now, Duration interval) %7B%0A        super(null, interval, now, new JobControl(new JobControlFlags(curator, flagSource)),%0A              jobMetrics(applicationRepository.metric()), cluster(curator));%0A        this.applicationRepository = applicationRepository;%0A    %7D%0A%0A
     Conf
@@ -3198,17 +3198,431 @@
      %7D%0A%0A
-
     %7D%0A%0A
+    /** Returns all hosts configured to be part of this ZooKeeper cluster */%0A    public static List%3CString%3E cluster(Curator curator) %7B%0A        return Arrays.stream(curator.zooKeeperEnsembleConnectionSpec().split(%22,%22))%0A                     .filter(hostAndPort -%3E !hostAndPort.isEmpty())%0A                     .map(hostAndPort -%3E hostAndPort.split(%22:%22)%5B0%5D)%0A                     .collect(Collectors.toList());%0A    %7D%0A%0A%0A
 %7D%0A

Support config server maintainers with evenly spaced maintenance runs