summaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/maintenance/NameServiceDispatcher.java
blob: 19616f862ab5f0543fe7b87c5e0aed1a6deac443 (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
// Copyright Yahoo. 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.vespa.hosted.controller.Controller;
import com.yahoo.vespa.hosted.controller.api.integration.dns.NameService;
import com.yahoo.vespa.hosted.controller.dns.NameServiceQueue;
import com.yahoo.vespa.hosted.controller.persistence.CuratorDb;

import java.time.Clock;
import java.time.Duration;
import java.util.logging.Level;

/**
 * This dispatches requests from {@link NameServiceQueue} to a {@link NameService}. Successfully dispatched requests are
 * removed from the queue.
 *
 * @author mpolden
 */
public class NameServiceDispatcher extends ControllerMaintainer {

    private final Clock clock;
    private final CuratorDb db;
    private final NameService nameService;

    public NameServiceDispatcher(Controller controller, Duration interval) {
        super(controller, interval);
        this.clock = controller.clock();
        this.db = controller.curator();
        this.nameService = controller.serviceRegistry().nameService();
    }

    @Override
    protected double maintain() {
        // Dispatch 1 request per second on average. Note that this is not entirely accurate because a NameService
        // implementation may need to perform multiple API-specific requests to execute a single NameServiceRequest
        int requestCount = trueIntervalInSeconds();
        try (var lock = db.lockNameServiceQueue()) {
            var queue = db.readNameServiceQueue();
            var instant = clock.instant();
            var remaining = queue.dispatchTo(nameService, requestCount);
            if (queue == remaining) return 1.0; // Queue unchanged

            var dispatched = queue.first(requestCount);
            if (!dispatched.requests().isEmpty()) {
                Level logLevel = controller().system().isCd() ? Level.INFO : Level.FINE;
                log.log(logLevel, "Dispatched name service request(s) in " +
                                  Duration.between(instant, clock.instant()) +
                                  ": " + dispatched.requests());
            }
            db.writeNameServiceQueue(remaining);
        }
        return 1.0;
    }

    /** The true interval at which this runs in this cluster */
    private int trueIntervalInSeconds() {
        return (int) interval().dividedBy(db.cluster().size()).getSeconds();
    }

}