blob: 3ee9650d4ca122a526e80cebf634c9ebcae97bf0 (
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
65
66
67
68
69
70
|
// Copyright Vespa.ai. 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.time.Instant;
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;
NameServiceDispatcher(Controller controller, NameService nameService, Duration interval) {
super(controller, interval);
this.clock = controller.clock();
this.db = controller.curator();
this.nameService = nameService;
}
public NameServiceDispatcher(Controller controller, Duration interval) {
this(controller, controller.serviceRegistry().nameService(), interval);
}
@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();
final NameServiceQueue initial;
try (var lock = db.lockNameServiceQueue()) {
initial = db.readNameServiceQueue();
}
if (initial.requests().isEmpty() || requestCount == 0) return 1.0;
Instant instant = clock.instant();
NameServiceQueue remaining = initial.dispatchTo(nameService, requestCount);
NameServiceQueue dispatched = initial.without(remaining);
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);
}
try (var lock = db.lockNameServiceQueue()) {
db.writeNameServiceQueue(db.readNameServiceQueue().replace(initial, remaining));
}
return dispatched.requests().size() / (double) Math.min(requestCount, initial.requests().size());
}
/** The true interval at which this runs in this cluster */
private int trueIntervalInSeconds() {
return (int) interval().dividedBy(db.cluster().size()).getSeconds();
}
}
|