summaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/maintenance/RoutingPolicyMaintainer.java
blob: 0ddc24147ee20199d1ad02adba5ff6ec31b92a75 (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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Copyright 2019 Oath Inc. 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.ApplicationId;
import com.yahoo.config.provision.RotationName;
import com.yahoo.config.provision.zone.ZoneId;
import com.yahoo.log.LogLevel;
import com.yahoo.vespa.curator.Lock;
import com.yahoo.vespa.hosted.controller.Controller;
import com.yahoo.vespa.hosted.controller.api.identifiers.DeploymentId;
import com.yahoo.vespa.hosted.controller.api.integration.configserver.LoadBalancer;
import com.yahoo.vespa.hosted.controller.api.integration.dns.AliasTarget;
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.Endpoint;
import com.yahoo.vespa.hosted.controller.application.RoutingId;
import com.yahoo.vespa.hosted.controller.application.RoutingPolicy;
import com.yahoo.vespa.hosted.controller.dns.NameServiceForwarder;
import com.yahoo.vespa.hosted.controller.dns.NameServiceQueue.Priority;
import com.yahoo.vespa.hosted.controller.persistence.CuratorDb;

import java.time.Duration;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;
import java.util.stream.Collectors;

/**
 * Maintains routing policies and their DNS records for all exclusive load balancers in this system.
 *
 * @author mortent
 * @author mpolden
 */
public class RoutingPolicyMaintainer extends Maintainer {

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

    private final NameServiceForwarder nameServiceForwarder;
    private final CuratorDb db;

    public RoutingPolicyMaintainer(Controller controller,
                                   Duration interval,
                                   JobControl jobControl,
                                   CuratorDb db) {
        super(controller, interval, jobControl);
        this.nameServiceForwarder = controller.nameServiceForwarder();
        this.db = db;
        // Update serialized format
        try (Lock lock = db.lockRoutingPolicies()) {
            for (var policy : db.readRoutingPolicies().entrySet()) {
                db.writeRoutingPolicies(policy.getKey(), policy.getValue());
            }
        }
    }

    @Override
    protected void maintain() {
        Map<DeploymentId, List<LoadBalancer>> loadBalancers = findLoadBalancers();
        removeObsoleteEndpointsFromDns(loadBalancers);
        storePolicies(loadBalancers);
        removeObsoletePolicies(loadBalancers);
        registerEndpointsInDns();
    }

    /** Find all exclusive load balancers in this system, grouped by deployment */
    private Map<DeploymentId, List<LoadBalancer>> findLoadBalancers() {
        Map<DeploymentId, List<LoadBalancer>> result = new LinkedHashMap<>();
        for (ZoneId zone : controller().zoneRegistry().zones().controllerUpgraded().ids()) {
            List<LoadBalancer> loadBalancers = controller().applications().configServer().getLoadBalancers(zone);
            for (LoadBalancer loadBalancer : loadBalancers) {
                DeploymentId deployment = new DeploymentId(loadBalancer.application(), zone);
                result.compute(deployment, (k, existing) -> {
                    if (existing == null) {
                        existing = new ArrayList<>();
                    }
                    existing.add(loadBalancer);
                    return existing;
                });
            }
        }
        return Collections.unmodifiableMap(result);
    }

    /** Create global endpoints for all current routing policies */
    private void registerEndpointsInDns() {
        try (Lock lock = db.lockRoutingPolicies()) {
            Map<RoutingId, List<RoutingPolicy>> routingTable = routingTableFrom(db.readRoutingPolicies());

            // Create DNS record for each routing ID
            for (Map.Entry<RoutingId, List<RoutingPolicy>> route : routingTable.entrySet()) {
                Endpoint endpoint = RoutingPolicy.endpointOf(route.getKey().application(), route.getKey().rotation(),
                                                             controller().system());
                Set<AliasTarget> targets = route.getValue()
                                                .stream()
                                                .filter(policy -> policy.dnsZone().isPresent())
                                                .map(policy -> new AliasTarget(policy.canonicalName(),
                                                                               policy.dnsZone().get(),
                                                                               policy.zone()))
                                                .collect(Collectors.toSet());
                try {
                    nameServiceForwarder.createAlias(RecordName.from(endpoint.dnsName()), targets, Priority.normal);
                } catch (Exception e) {
                    log.log(LogLevel.WARNING, "Failed to create or update DNS record for global rotation " +
                                              endpoint.dnsName() + ". Retrying in " + maintenanceInterval(), e);
                }
            }
        }
    }

    /** Store routing policies for all load balancers */
    private void storePolicies(Map<DeploymentId, List<LoadBalancer>> loadBalancers) {
        for (Map.Entry<DeploymentId, List<LoadBalancer>> entry : loadBalancers.entrySet()) {
            ApplicationId application = entry.getKey().applicationId();
            ZoneId zone = entry.getKey().zoneId();
            try (Lock lock = db.lockRoutingPolicies()) {
                Set<RoutingPolicy> policies = new LinkedHashSet<>(db.readRoutingPolicies(application));
                for (LoadBalancer loadBalancer : entry.getValue()) {
                    try {
                        RoutingPolicy policy = storePolicy(application, zone, loadBalancer);
                        if (!policies.add(policy)) {
                            policies.remove(policy);
                            policies.add(policy);
                        }
                    } catch (Exception e) {
                        log.log(LogLevel.WARNING, "Failed to create or update DNS record for load balancer " +
                                                  loadBalancer.hostname() + ". Retrying in " + maintenanceInterval(),
                                e);
                    }
                }
                db.writeRoutingPolicies(application, policies);
            }
        }
    }

    /** Store policy for given load balancer and request a CNAME for it */
    private RoutingPolicy storePolicy(ApplicationId application, ZoneId zone, LoadBalancer loadBalancer) {
        RoutingPolicy routingPolicy = new RoutingPolicy(application, loadBalancer.cluster(), zone,
                                                        loadBalancer.hostname(), loadBalancer.dnsZone(),
                                                        loadBalancer.rotations());
        RecordName name = RecordName.from(routingPolicy.endpointIn(controller().system()).dnsName());
        RecordData data = RecordData.fqdn(loadBalancer.hostname().value());
        nameServiceForwarder.createCname(name, data, Priority.normal);
        return routingPolicy;
    }

    /** Remove obsolete policies and their CNAME records */
    private void removeObsoletePolicies(Map<DeploymentId, List<LoadBalancer>> loadBalancers) {
        try (Lock lock = db.lockRoutingPolicies()) {
            var allPolicies = new HashMap<>(db.readRoutingPolicies());
            var removalCandidates = allPolicies.values().stream()
                                               .flatMap(Collection::stream)
                                               .collect(Collectors.toSet());
            var activeLoadBalancers = loadBalancers.values().stream()
                                                   .flatMap(Collection::stream)
                                                   .map(LoadBalancer::hostname)
                                                   .collect(Collectors.toSet());
            // Keep active load balancers by removing them from candidates
            removalCandidates.removeIf(policy -> activeLoadBalancers.contains(policy.canonicalName()));
            for (var policy : removalCandidates) {
                var dnsName = policy.endpointIn(controller().system()).dnsName();
                nameServiceForwarder.removeRecords(Record.Type.CNAME, RecordName.from(dnsName), Priority.normal);
                // Remove stale policy from curator
                var updatedPolicies = new LinkedHashSet<>(allPolicies.getOrDefault(policy.owner(), Set.of()));
                updatedPolicies.remove(policy);
                allPolicies.put(policy.owner(), updatedPolicies);
                db.writeRoutingPolicies(policy.owner(), updatedPolicies);
            }
        }
    }

    /** Remove DNS for global endpoints not referenced by given load balancers */
    private void removeObsoleteEndpointsFromDns(Map<DeploymentId, List<LoadBalancer>> loadBalancers) {
        try (Lock lock = db.lockRoutingPolicies()) {
            Set<RoutingId> removalCandidates = routingTableFrom(db.readRoutingPolicies()).keySet();
            Set<RoutingId> activeRoutingIds = routingIdsFrom(loadBalancers);
            removalCandidates.removeAll(activeRoutingIds);
            for (RoutingId id : removalCandidates) {
                Endpoint endpoint = RoutingPolicy.endpointOf(id.application(), id.rotation(), controller().system());
                nameServiceForwarder.removeRecords(Record.Type.ALIAS, RecordName.from(endpoint.dnsName()), Priority.normal);
            }
        }
    }

    /** Compute routing IDs from given load balancers */
    private static Set<RoutingId> routingIdsFrom(Map<DeploymentId, List<LoadBalancer>> loadBalancers) {
        Set<RoutingId> routingIds = new LinkedHashSet<>();
        for (List<LoadBalancer> values : loadBalancers.values()) {
            for (LoadBalancer loadBalancer : values) {
                for (RotationName rotation : loadBalancer.rotations()) {
                    routingIds.add(new RoutingId(loadBalancer.application(), rotation));
                }
            }
        }
        return Collections.unmodifiableSet(routingIds);
    }

    /** Compute a routing table from given policies */
    private static Map<RoutingId, List<RoutingPolicy>> routingTableFrom(Map<ApplicationId, Set<RoutingPolicy>> routingPolicies) {
        var flattenedPolicies = routingPolicies.values().stream().flatMap(Collection::stream).collect(Collectors.toSet());
        var routingTable = new LinkedHashMap<RoutingId, List<RoutingPolicy>>();
        for (var policy : flattenedPolicies) {
            for (var rotation : policy.rotations()) {
                var id = new RoutingId(policy.owner(), rotation);
                routingTable.compute(id, (k, policies) -> {
                    if (policies == null) {
                        policies = new ArrayList<>();
                    }
                    policies.add(policy);
                    return policies;
                });
            }
        }
        return routingTable;
    }

}