All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.yahoo.vespa.config.server.maintenance.ConfigServerMaintainer Maven / Gradle / Ivy

There is a newer version: 8.441.21
Show newest version
// Copyright Vespa.ai. 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.ListFlag;
import com.yahoo.vespa.flags.PermanentFlags;
import java.time.Clock;
import java.time.Duration;
import java.util.Arrays;
import java.util.List;
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;

    /** Creates a maintainer where maintainers on different nodes in this cluster run with even delay. */
    ConfigServerMaintainer(ApplicationRepository applicationRepository, Curator curator, FlagSource flagSource,
                           Clock clock, Duration interval, boolean acquireLock) {
       this(applicationRepository, curator, flagSource, clock, interval, acquireLock, false);
    }

    /** Creates a maintainer where maintainers on different nodes in this cluster run with even delay. */
    ConfigServerMaintainer(ApplicationRepository applicationRepository, Curator curator, FlagSource flagSource,
                           Clock clock, Duration interval, boolean acquireLock, boolean ignoreCollision) {
        super(null, interval, clock, new JobControl(new JobControlFlags(curator, flagSource)),
              new ConfigServerJobMetrics(applicationRepository.metric()), cluster(curator), ignoreCollision, 1.0, acquireLock);
        this.applicationRepository = applicationRepository;
    }


    private static class ConfigServerJobMetrics extends JobMetrics {

        private final Metric metric;

        public ConfigServerJobMetrics(Metric metric) {
            this.metric = metric;
        }

        @Override
        public void completed(String job, double successFactorDeviation, long durationMs) {
            var context = metric.createContext(Map.of("maintainer", job));
            metric.set("maintenance.successFactorDeviation", successFactorDeviation, context);
            metric.set("maintenance.duration", durationMs, context);
        }

    }

    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 inactiveJobsFlag;

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

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

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

    }

    /** Returns all hosts configured to be part of this ZooKeeper cluster */
    public static List cluster(Curator curator) {
        return Arrays.stream(curator.zooKeeperEnsembleConnectionSpec().split(","))
                     .filter(hostAndPort -> !hostAndPort.isEmpty())
                     .map(hostAndPort -> hostAndPort.split(":")[0])
                     .toList();
    }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy