summaryrefslogtreecommitdiffstats
path: root/configserver/src/main/java/com/yahoo/vespa/config/server/host/HostRegistry.java
blob: b89f3bba8359c88d01f6c16f104c6598593bfa78 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.host;

import com.google.common.collect.Collections2;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.TenantName;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

/**
 * A host registry with a mapping between hosts (hostname as a String) and some type T
 * TODO: Maybe we should have a Host type, but using String for now.
 *
 * @author Ulf Lilleengen
 */
public class HostRegistry implements HostValidator {

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

    private final Map<String, ApplicationId> host2KeyMap = new ConcurrentHashMap<>();

    public ApplicationId getKeyForHost(String hostName) {
        return host2KeyMap.get(hostName);
    }

    public synchronized void update(ApplicationId key, Collection<String> newHosts) {
        verifyHosts(key, newHosts);
        Collection<String> currentHosts = getHostsForKey(key);
        log.log(Level.FINE, () -> "Setting hosts for key '" + key + "', " +
                                  "newHosts: " + newHosts + ", " +
                                  "currentHosts: " + currentHosts);
        Collection<String> removedHosts = getRemovedHosts(newHosts, currentHosts);
        removeHosts(removedHosts);
        addHosts(key, newHosts);
    }

    @Override
    public synchronized void verifyHosts(ApplicationId applicationId, Collection<String> newHosts) {
        for (String host : newHosts) {
            if (hostAlreadyTaken(host, applicationId)) {
                throw new IllegalArgumentException("'" + applicationId + "' tried to allocate host '" + host +
                                                   "', but the host is already taken by '" + host2KeyMap.get(host) + "'");
            }
        }
    }

    public synchronized void removeHostsForKey(ApplicationId key) {
        host2KeyMap.entrySet().removeIf(entry -> entry.getValue().equals(key));
    }

    public synchronized void removeHostsForKey(TenantName key) {
        host2KeyMap.entrySet().removeIf(entry -> entry.getValue().tenant().equals(key));
    }

    public synchronized Collection<String> getAllHosts() {
        return Collections.unmodifiableCollection(new ArrayList<>(host2KeyMap.keySet()));
    }

    public synchronized Collection<String> getHostsForKey(ApplicationId key) {
        return host2KeyMap.entrySet().stream()
                .filter(entry -> entry.getValue().equals(key))
                .map(Map.Entry::getKey)
                .collect(Collectors.toSet());
    }

    private boolean hostAlreadyTaken(String host, ApplicationId key) {
        return host2KeyMap.containsKey(host) && !key.equals(host2KeyMap.get(host));
    }

    private static Collection<String> getRemovedHosts(Collection<String> newHosts, Collection<String> previousHosts) {
        return Collections2.filter(previousHosts, host -> !newHosts.contains(host));
    }

    private void removeHosts(Collection<String> removedHosts) {
        for (String host : removedHosts) {
            log.log(Level.FINE, () -> "Removing " + host);
            host2KeyMap.remove(host);
        }
    }

    private void addHosts(ApplicationId key, Collection<String> newHosts) {
        for (String host : newHosts) {
            log.log(Level.FINE, () -> "Adding " + host);
            host2KeyMap.put(host, key);
        }
    }

}