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

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Logger;

import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import com.yahoo.log.LogLevel;

/**
 * A host registry that create mappings between some type T and a list of hosts, represented as
 * strings.
 * TODO: Maybe we should have a Host type, but using String for now.
 * TODO: Is there a generalized version of this pattern? Need some sort mix of Bimap and Multimap
 *
 * @author lulf
 * @since 5.3
 */
public class HostRegistry<T> implements HostValidator<T> {

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

    private final Map<T, Collection<String>> key2HostsMap = new ConcurrentHashMap<>();
    private final Map<String, T> host2KeyMap = new ConcurrentHashMap<>();

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

    public void update(T key, Collection<String> newHosts) {
        verifyHosts(key, newHosts);
        log.log(LogLevel.DEBUG, "Setting hosts for key(" + key + "), newHosts(" + newHosts + "), " +
                                "currentHosts(" + getCurrentHosts(key) + ")");
        Collection<String> removedHosts = getRemovedHosts(newHosts, getCurrentHosts(key));
        removeHosts(removedHosts);
        addHosts(key, newHosts);
    }

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

    public void removeHostsForKey(T key) {
        for (Iterator<Map.Entry<T, Collection<String>>> it = key2HostsMap.entrySet().iterator(); it.hasNext(); ) {
            Map.Entry<T, Collection<String>> entry = it.next();
            if (entry.getKey().equals(key)) {
                Collection<String> hosts = entry.getValue();
                it.remove();
                removeHosts(hosts);
            }
        }
    }

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

    Collection<String> getCurrentHosts(T key) {
        return key2HostsMap.containsKey(key) ? new ArrayList<>(key2HostsMap.get(key)) : new ArrayList<String>();
    }

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

    private static Collection<String> getRemovedHosts(final Collection<String> newHosts, Collection<String> previousHosts) {
        return Collections2.filter(previousHosts, new Predicate<String>() {
            @Override
            public boolean apply(String host) {
                return !newHosts.contains(host);
            }
        });
    }

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

    private void addHosts(T key, Collection<String> newHosts) {
        for (String host : newHosts) {
            log.log(LogLevel.DEBUG, "Adding " + host);
            host2KeyMap.put(host, key);
        }
        key2HostsMap.put(key, new ArrayList<>(newHosts));
    }

}