aboutsummaryrefslogtreecommitdiffstats
path: root/routing-generator/src/main/java/com/yahoo/vespa/hosted/routing/status/ServerGroup.java
blob: dc5d46d57cbccf2d98009ab630c66cbd205ef619 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.routing.status;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * A group of servers behind a router/reverse proxy.
 *
 * @author mpolden
 */
public class ServerGroup {

    private static final double requiredUpFraction = 0.25D;

    private final Map<String, List<Server>> servers;

    public ServerGroup(List<Server> servers) {
        this.servers = servers.stream().collect(Collectors.collectingAndThen(Collectors.groupingBy(Server::upstreamName),
                                                                             Collections::unmodifiableMap));
    }

    public Map<String, List<Server>> asMap() {
        return servers;
    }

    /** Returns whether given upstream is healthy */
    public boolean isHealthy(String upstreamName) {
        List<Server> upstreamServers = servers.getOrDefault(upstreamName, List.of());
        long upCount = upstreamServers.stream()
                                      .filter(Server::up)
                                      .count();
        return upCount > upstreamServers.size() * requiredUpFraction;
    }

    public static class Server {

        private final String upstreamName;
        private final String hostport;
        private final boolean up;

        public Server(String upstreamName, String hostport, boolean up) {
            this.upstreamName = upstreamName;
            this.hostport = hostport;
            this.up = up;
        }

        public String upstreamName() {
            return upstreamName;
        }

        public String hostport() {
            return hostport;
        }

        public boolean up() {
            return up;
        }

    }

}