aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/maintenance/NameServiceDispatcher.java
blob: d2c8d70844edaddb3aaf438afe35f832cb2dcff7 (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
61
62
63
64
// 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.config.provision.SystemName;
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 consumes requests from the {@link NameServiceQueue} by submitting them to a {@link NameService}. Successfully
 * consumed 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;
    private final int requestCount;

    public NameServiceDispatcher(Controller controller, Duration interval) {
        this(controller, interval, requestCount(controller.system()));
    }

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

    @Override
    protected double maintain() {
        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;
    }

    private static int requestCount(SystemName system) {
        // Dispatch more requests at a time in CD as integration tests expect reasonably quick DNS changes
        return system.isCd() ? 3 : 1;
    }

}