aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/dns/NameServiceForwarder.java
blob: 40d2667b9ae0219d4536609faf386ceffb4d7f24 (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// 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.dns;

import com.yahoo.transaction.Mutex;
import com.yahoo.vespa.hosted.controller.api.integration.dns.AliasTarget;
import com.yahoo.vespa.hosted.controller.api.integration.dns.DirectTarget;
import com.yahoo.vespa.hosted.controller.api.integration.dns.NameService;
import com.yahoo.vespa.hosted.controller.api.integration.dns.Record;
import com.yahoo.vespa.hosted.controller.api.integration.dns.RecordData;
import com.yahoo.vespa.hosted.controller.api.integration.dns.RecordName;
import com.yahoo.vespa.hosted.controller.application.TenantAndApplicationId;
import com.yahoo.vespa.hosted.controller.maintenance.NameServiceDispatcher;
import com.yahoo.vespa.hosted.controller.persistence.CuratorDb;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * This adds name service requests to the {@link NameServiceQueue}.
 *
 * Name service requests passed to this are not immediately sent to a name service, but are instead persisted
 * in a curator-backed queue. Enqueued requests are later dispatched to a {@link NameService} by
 * {@link NameServiceDispatcher}.
 *
 * @author mpolden
 */
public class NameServiceForwarder {

    private static final Logger log = Logger.getLogger(NameServiceForwarder.class.getName());

    private final CuratorDb db;

    public NameServiceForwarder(CuratorDb db) {
        this.db = Objects.requireNonNull(db, "db must be non-null");
    }

    /** Create or update a given record */
    public void createRecord(Record record, NameServiceQueue.Priority priority, Optional<TenantAndApplicationId> owner) {
        forward(new CreateRecord(owner, record), priority);
    }

    /** Create or update an ALIAS record with given name and targets */
    public void createAlias(RecordName name, Set<AliasTarget> targets, NameServiceQueue.Priority priority, Optional<TenantAndApplicationId> owner) {
        var records = targets.stream()
                             .map(target -> new Record(Record.Type.ALIAS, name, target.pack()))
                             .toList();
        forward(new CreateRecords(owner, records), priority);
    }

    /** Create or update a DIRECT record with given name and targets */
    public void createDirect(RecordName name, Set<DirectTarget> targets, NameServiceQueue.Priority priority, Optional<TenantAndApplicationId> owner) {
        var records = targets.stream()
                             .map(target -> new Record(Record.Type.DIRECT, name, target.pack()))
                             .toList();
        forward(new CreateRecords(owner, records), priority);
    }

    /** Create or update a TXT record with given name and data */
    public void createTxt(RecordName name, List<RecordData> txtData, NameServiceQueue.Priority priority, Optional<TenantAndApplicationId> owner) {
        var records = txtData.stream()
                             .map(data -> new Record(Record.Type.TXT, name, data))
                             .toList();
        forward(new CreateRecords(owner, records), priority);
    }

    /** Remove all records of given type and name */
    public void removeRecords(Record.Type type, RecordName name, NameServiceQueue.Priority priority, Optional<TenantAndApplicationId> owner) {
        forward(new RemoveRecords(owner, type, name), priority);
    }

    /** Remove all records of given type, name and data */
    public void removeRecords(Record.Type type, RecordName name, RecordData data, NameServiceQueue.Priority priority, Optional<TenantAndApplicationId> owner) {
        forward(new RemoveRecords(owner, type, name, data), priority);
    }

    protected void forward(NameServiceRequest request, NameServiceQueue.Priority priority) {
        try (Mutex lock = db.lockNameServiceQueue()) {
            NameServiceQueue queue = db.readNameServiceQueue();
            var queued = queue.requests().size();
            if (queued > NameServiceQueue.QUEUE_CAPACITY) {
                log.log(Level.WARNING, "Queue is above capacity (size: " + queued + "), failing requests will be dropped. " +
                                       "This likely means that the name service is not successfully executing requests");
            }
            log.log(Level.FINE, () -> "Queueing name service request: " + request);
            db.writeNameServiceQueue(queue.with(request, priority));
        }
    }

}