        StringBuffer sb;
        sb = new StringBuffer();

package com.ms.silverking.cloud.dht.net;

import java.net.InetSocketAddress;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import com.google.common.collect.ImmutableMap;
import com.ms.silverking.collection.CollectionUtil;
import com.ms.silverking.collection.HashedSetMap;
import com.ms.silverking.log.Log;
import com.ms.silverking.net.AddrAndPort;
import com.ms.silverking.net.IPAndPort;

/**
 * Background: Under "normal" operation, each SK daemon has a fixed port bound to a single IP address. The port
 * is the same for all daemons. This implies that only a single daemon may run per server. It also implies that all
 * traffic will go over a single interface.
 *

 * IP aliasing allows us to overcome these limitations. IP aliasing does this in two ways:
 * 1) The aliasing feature can be used to allow a daemon to be identified by a "fake" IPAndPort. This IPAndPort is
 * used in the topology and almost all of the rest of the code as if it were a real IP. This IPAliasMap class is
 * used by MessageGroupBase (and a few other classes) to translate this IPAndPort into an actual IPAndPort when
 * communication needs to take place. By design, most of the code is unaware of this translation.
 * 2) The aliasing feature allows traffic to be distributed across multiple interfaces. This is done by associating
 * multiple interfaces with an actual IPAndPort. In this case, MessageGroupBase will translate the usual IPAndPort
 * into one of the many remote interfaces in a round-robin fashion.
 *

 * Terminology: for consistency in the above two cases, this class calls the IP address used to identify a daemon
 * the "daemon IP" or "daemon" for short. Note that the port is fixed to the SK instance port for this case.
 * Each of the one or more IPAndPorts associated with a daemon IP is called an "interface IP and port" or
 * "interface" for short. The "interface IP and port" are always a real IP and port. The daemon IP is "fake"
 * for case 1, but an actual IP for case 2.
 *

 * For case (1) each daemon IP acts as an alias, and has a single interface IP and port to which it is associated.
 * For case (2) each daemon IP has several interface IPAndPorts with which it is associated.
 *

 * Colloquially, for case (1) we can call the daemon IP an "alias". We don't do that in this code to avoid confusion
 * with case (2) where we could call the interface IP and ports aliases. (The daemon IP is real in this case.)
 *

 * Notes on case (1) - setting up "aliases"/fake IP addresses for each daemon IP.
 * - the topology, host groups, etc, must be composed of these daemon IPs, not interface IPs
 * - the DHT config must have an ipAliasMap field in the form ipAliasMap={daemonIp=interfaceIp:port}
 * As mentioned previously, the vast majority of the code will use this daemon IP as if it were the actual IP.
 * In addition, users will only use the daemon IP.
 *

 * All translation between daemon IP and interface IP and ports will take place in this class.
 * Clients will select a daemon IP address from the topology as their preferredServer. This is dealiased in
 * DHTSessionImpl to an interface IP and port, to which the client communicates.
 * Aliasing also allows nodes to deviate from the fixed port in DHT config; such nodes would require an alias
 * map entry which maps a fake IP to the port actually in use for a given address. The node itself must be passed
 * -port N on the command line at start up in order to run on a port different to that specified in DHT config.
 * This also allows many nodes to run on the same machine; several daemon IP entries in the maps could exist,
 * which resolve to the same host, but with unique ports. Note that each daemon IP must be unique in this case
 *

 * It is not required that all nodes have an alias identity, so long as it is consistent between the topology
 * and host group files, and such nodes can be omitted from the alias map. The alias resolution, when it
 * does not find an entry for such a node, would leave the address unchanged and clients/servers would proceed
 * to try and connect directly
 *

 * Notes on case 2:
 * When aliasing is used to support multi-homing, each daemon IP will be associated with
 * several interface and ports, and the dht config will have an entry of the
 * form: ipAliasMap={daemonIp=interfaceIp:port,...}. Note that all ports must be the same for case 2.
 * The ports may also be omitted.
 */
public class IPAliasMap {

    // one-to-many
    private final Map<IPAndPort, IPAndPort[]> daemonToInterfaces;

    // many-to-one
    private final Map<IPAndPort, IPAndPort> interfaceToDaemon;

    private final HashedSetMap<String, IPAndPort> interfaceIPToDaemons;

    // used for round-robin between aliases
    private final AtomicInteger interfaceIndex;

    // (e.g. for distributing traffic across multi-homed interfaces)
    private static final boolean logDaemonToInterfaceMapping = true;

    private static final boolean logInterfaceToDaemonMapping = true;

    public IPAliasMap(Map<IPAndPort, IPAndPort[]> daemonToInterfaces) {
        interfaceToDaemon = new ConcurrentHashMap<>();
        interfaceIPToDaemons = new HashedSetMap<>();
        if (daemonToInterfaces == null) {
            this.daemonToInterfaces = ImmutableMap.of();
            interfaceIndex = null;
        } else {
            sanityCheckMap(daemonToInterfaces);
            this.daemonToInterfaces = new ConcurrentHashMap<>();
            interfaceIndex = new AtomicInteger();
            this.daemonToInterfaces.putAll(daemonToInterfaces);
            if (logDaemonToInterfaceMapping) {
                IPAliasingUtil.logAliasMap(daemonToInterfaces);
            }
            addReverseMappings(daemonToInterfaces);
        }
    }

    public static IPAliasMap identityMap() {
        return new IPAliasMap(null);
    }

    private void sanityCheckMap(Map<IPAndPort, IPAndPort[]> daemonToInterfaces) {
        ensureDaemonPortsMatch(daemonToInterfaces);
    }

    private void ensureDaemonPortsMatch(Map<IPAndPort, IPAndPort[]> daemonToInterfaces) {
        int daemonPort;
        daemonPort = -1;
        for (IPAndPort daemonIPAndPort : daemonToInterfaces.keySet()) {
            if (daemonPort < 0) {
                daemonPort = daemonIPAndPort.getPort();
            } else {
                if (daemonIPAndPort.getPort() != daemonPort) {
                    throw new RuntimeException("IPAliasMap daemon ports must all be the same");
                }
            }
        }
    }

    public Map<IPAndPort, IPAndPort[]> getDaemonToInterfacesMap() {
        return ImmutableMap.copyOf(daemonToInterfaces);
    }

    public Map<IPAndPort, IPAndPort> getInterfaceToDaemonMap() {
        return ImmutableMap.copyOf(interfaceToDaemon);
    }

    public AddrAndPort daemonToInterface(AddrAndPort daemon) {
        AddrAndPort[] interfaces;
        interfaces = daemonToInterfaces.get(daemon);
        if (interfaces != null) {
            // Below is random. For now we implement round robin instead
            // Round robin
            return interfaces[interfaceIndex.getAndIncrement() % interfaces.length];
        } else {
            return daemon;
        }
    }

    public IPAndPort interfaceToDaemon(InetSocketAddress interfaceIPAndPort) {
        return interfaceToDaemon(new IPAndPort(interfaceIPAndPort));
    }

    public IPAndPort interfaceToDaemon(IPAndPort interfaceIPAndPort) {
        IPAndPort daemon;
        daemon = interfaceToDaemon.get(interfaceIPAndPort);
        return daemon != null ? daemon : interfaceIPAndPort;
    }

    /**
     * For all entries in the daemonToInterfaces argument, update the
     * reverse map. That is, update the interfaceToDaemon map with
     * the corresponding mappings.
     *
     * @param daemonToInterfaces the forward map to reverse
     */
    private void addReverseMappings(Map<IPAndPort, IPAndPort[]> daemonToInterfaces) {
        for (Map.Entry<IPAndPort, IPAndPort[]> entry : daemonToInterfaces.entrySet()) {
            addInterfacesToDaemon(entry.getValue(), entry.getKey());
        }
    }

    // only used at startup
    private void addInterfacesToDaemon(IPAndPort[] interfaces, IPAndPort daemon) {
        for (IPAndPort interfaceIPAndPort : interfaces) {
            addInterfaceToDaemon(interfaceIPAndPort, daemon, false);
            addIPToDaemonMapping(interfaceIPAndPort.getIPAsString(), daemon);
        }
    }

    // used at runtime to map reverse connections
    public void addInterfaceToDaemon(IPAndPort interfaceIPAndPort, IPAndPort daemonIPAndPort) {
        addInterfaceToDaemon(interfaceIPAndPort, daemonIPAndPort, true);
    }

    private void addInterfaceToDaemon(IPAndPort interfaceIPAndPort, IPAndPort daemonIPAndPort, boolean allowRemapping) {
        IPAndPort prev;
        prev = interfaceToDaemon.put(interfaceIPAndPort, daemonIPAndPort);
        if (logInterfaceToDaemonMapping && (prev == null || !prev.equals(daemonIPAndPort))) {
            Log.infof("IPAliasMap adding interfaceIPAndPort=>daemonIPAndPort: %s=>%s", interfaceIPAndPort, daemonIPAndPort);
        }
        if (!allowRemapping && prev != null && !prev.equals(daemonIPAndPort)) {
            throw new RuntimeException("Interface maps to multiple daemons: " + interfaceIPAndPort);
        }
    }

    private void addIPToDaemonMapping(String ip, IPAndPort daemonIPAndPort) {
        interfaceIPToDaemons.addValue(ip, daemonIPAndPort);
    }

    /**
     * Given an interface ip, randomly select one of the possibly many associated daemons
     *
     * @param ip
     * @return
     */
    public IPAndPort interfaceIPToRandomDaemon(String ip) {
        return interfaceIPToDaemons.getAnyValue(ip);
    }

    /**
     * If an interface ip uniquely identifies a daemon ip, return that daemon
     *
     * @param ip
     * @return the uniquely associated daemon
     */
    public IPAndPort interfaceIPToDaemon_ifUnique(String ip) {
        Set<IPAndPort> daemonIPAndPorts;
        daemonIPAndPorts = interfaceIPToDaemons.getSet(ip);
        if (daemonIPAndPorts == null || daemonIPAndPorts.size() > 1) {
            return null;
        } else {
            return daemonIPAndPorts.iterator().next();
        }
    }

    @Override
    public String toString() {

        sb.append("{");
        for (Map.Entry<IPAndPort, IPAndPort[]> entry : daemonToInterfaces.entrySet()) {
            sb.append(String.format("%s -> [", entry.getKey()));
            for (IPAndPort _interface : entry.getValue()) {
                sb.append(_interface + " ");
            }
            sb.append("]\n");
        }
        sb.append("}");
        return sb.toString() + "\n" + CollectionUtil.mapToString(interfaceToDaemon);
    }
}


sb.append("{");
        for (Map.Entry<IPAndPort, IPAndPort[]> entry : daemonToInterfaces.entrySet()) {
            sb.append(String.format("%s -> [", entry.getKey()));
            for (IPAndPort _interface : entry.getValue()) {
                sb.append(_interface + " ");
            }
            sb.append("]\n");
        }

package com.ms.silverking.cloud.dht.net;

import java.net.InetSocketAddress;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import com.google.common.collect.ImmutableMap;
import com.ms.silverking.collection.CollectionUtil;
import com.ms.silverking.collection.HashedSetMap;
import com.ms.silverking.log.Log;
import com.ms.silverking.net.AddrAndPort;
import com.ms.silverking.net.IPAndPort;

/**
 * Background: Under "normal" operation, each SK daemon has a fixed port bound to a single IP address. The port
 * is the same for all daemons. This implies that only a single daemon may run per server. It also implies that all
 * traffic will go over a single interface.
 *

 * IP aliasing allows us to overcome these limitations. IP aliasing does this in two ways:
 * 1) The aliasing feature can be used to allow a daemon to be identified by a "fake" IPAndPort. This IPAndPort is
 * used in the topology and almost all of the rest of the code as if it were a real IP. This IPAliasMap class is
 * used by MessageGroupBase (and a few other classes) to translate this IPAndPort into an actual IPAndPort when
 * communication needs to take place. By design, most of the code is unaware of this translation.
 * 2) The aliasing feature allows traffic to be distributed across multiple interfaces. This is done by associating
 * multiple interfaces with an actual IPAndPort. In this case, MessageGroupBase will translate the usual IPAndPort
 * into one of the many remote interfaces in a round-robin fashion.
 *

 * Terminology: for consistency in the above two cases, this class calls the IP address used to identify a daemon
 * the "daemon IP" or "daemon" for short. Note that the port is fixed to the SK instance port for this case.
 * Each of the one or more IPAndPorts associated with a daemon IP is called an "interface IP and port" or
 * "interface" for short. The "interface IP and port" are always a real IP and port. The daemon IP is "fake"
 * for case 1, but an actual IP for case 2.
 *

 * For case (1) each daemon IP acts as an alias, and has a single interface IP and port to which it is associated.
 * For case (2) each daemon IP has several interface IPAndPorts with which it is associated.
 *

 * Colloquially, for case (1) we can call the daemon IP an "alias". We don't do that in this code to avoid confusion
 * with case (2) where we could call the interface IP and ports aliases. (The daemon IP is real in this case.)
 *

 * Notes on case (1) - setting up "aliases"/fake IP addresses for each daemon IP.
 * - the topology, host groups, etc, must be composed of these daemon IPs, not interface IPs
 * - the DHT config must have an ipAliasMap field in the form ipAliasMap={daemonIp=interfaceIp:port}
 * As mentioned previously, the vast majority of the code will use this daemon IP as if it were the actual IP.
 * In addition, users will only use the daemon IP.
 *

 * All translation between daemon IP and interface IP and ports will take place in this class.
 * Clients will select a daemon IP address from the topology as their preferredServer. This is dealiased in
 * DHTSessionImpl to an interface IP and port, to which the client communicates.
 * Aliasing also allows nodes to deviate from the fixed port in DHT config; such nodes would require an alias
 * map entry which maps a fake IP to the port actually in use for a given address. The node itself must be passed
 * -port N on the command line at start up in order to run on a port different to that specified in DHT config.
 * This also allows many nodes to run on the same machine; several daemon IP entries in the maps could exist,
 * which resolve to the same host, but with unique ports. Note that each daemon IP must be unique in this case
 *

 * It is not required that all nodes have an alias identity, so long as it is consistent between the topology
 * and host group files, and such nodes can be omitted from the alias map. The alias resolution, when it
 * does not find an entry for such a node, would leave the address unchanged and clients/servers would proceed
 * to try and connect directly
 *

 * Notes on case 2:
 * When aliasing is used to support multi-homing, each daemon IP will be associated with
 * several interface and ports, and the dht config will have an entry of the
 * form: ipAliasMap={daemonIp=interfaceIp:port,...}. Note that all ports must be the same for case 2.
 * The ports may also be omitted.
 */
public class IPAliasMap {

    // one-to-many
    private final Map<IPAndPort, IPAndPort[]> daemonToInterfaces;

    // many-to-one
    private final Map<IPAndPort, IPAndPort> interfaceToDaemon;

    private final HashedSetMap<String, IPAndPort> interfaceIPToDaemons;

    // used for round-robin between aliases
    private final AtomicInteger interfaceIndex;

    // (e.g. for distributing traffic across multi-homed interfaces)
    private static final boolean logDaemonToInterfaceMapping = true;

    private static final boolean logInterfaceToDaemonMapping = true;

    public IPAliasMap(Map<IPAndPort, IPAndPort[]> daemonToInterfaces) {
        interfaceToDaemon = new ConcurrentHashMap<>();
        interfaceIPToDaemons = new HashedSetMap<>();
        if (daemonToInterfaces == null) {
            this.daemonToInterfaces = ImmutableMap.of();
            interfaceIndex = null;
        } else {
            sanityCheckMap(daemonToInterfaces);
            this.daemonToInterfaces = new ConcurrentHashMap<>();
            interfaceIndex = new AtomicInteger();
            this.daemonToInterfaces.putAll(daemonToInterfaces);
            if (logDaemonToInterfaceMapping) {
                IPAliasingUtil.logAliasMap(daemonToInterfaces);
            }
            addReverseMappings(daemonToInterfaces);
        }
    }

    public static IPAliasMap identityMap() {
        return new IPAliasMap(null);
    }

    private void sanityCheckMap(Map<IPAndPort, IPAndPort[]> daemonToInterfaces) {
        ensureDaemonPortsMatch(daemonToInterfaces);
    }

    private void ensureDaemonPortsMatch(Map<IPAndPort, IPAndPort[]> daemonToInterfaces) {
        int daemonPort;
        daemonPort = -1;
        for (IPAndPort daemonIPAndPort : daemonToInterfaces.keySet()) {
            if (daemonPort < 0) {
                daemonPort = daemonIPAndPort.getPort();
            } else {
                if (daemonIPAndPort.getPort() != daemonPort) {
                    throw new RuntimeException("IPAliasMap daemon ports must all be the same");
                }
            }
        }
    }

    public Map<IPAndPort, IPAndPort[]> getDaemonToInterfacesMap() {
        return ImmutableMap.copyOf(daemonToInterfaces);
    }

    public Map<IPAndPort, IPAndPort> getInterfaceToDaemonMap() {
        return ImmutableMap.copyOf(interfaceToDaemon);
    }

    public AddrAndPort daemonToInterface(AddrAndPort daemon) {
        AddrAndPort[] interfaces;
        interfaces = daemonToInterfaces.get(daemon);
        if (interfaces != null) {
            // Below is random. For now we implement round robin instead
            // Round robin
            return interfaces[interfaceIndex.getAndIncrement() % interfaces.length];
        } else {
            return daemon;
        }
    }

    public IPAndPort interfaceToDaemon(InetSocketAddress interfaceIPAndPort) {
        return interfaceToDaemon(new IPAndPort(interfaceIPAndPort));
    }

    public IPAndPort interfaceToDaemon(IPAndPort interfaceIPAndPort) {
        IPAndPort daemon;
        daemon = interfaceToDaemon.get(interfaceIPAndPort);
        return daemon != null ? daemon : interfaceIPAndPort;
    }

    /**
     * For all entries in the daemonToInterfaces argument, update the
     * reverse map. That is, update the interfaceToDaemon map with
     * the corresponding mappings.
     *
     * @param daemonToInterfaces the forward map to reverse
     */
    private void addReverseMappings(Map<IPAndPort, IPAndPort[]> daemonToInterfaces) {
        for (Map.Entry<IPAndPort, IPAndPort[]> entry : daemonToInterfaces.entrySet()) {
            addInterfacesToDaemon(entry.getValue(), entry.getKey());
        }
    }

    // only used at startup
    private void addInterfacesToDaemon(IPAndPort[] interfaces, IPAndPort daemon) {
        for (IPAndPort interfaceIPAndPort : interfaces) {
            addInterfaceToDaemon(interfaceIPAndPort, daemon, false);
            addIPToDaemonMapping(interfaceIPAndPort.getIPAsString(), daemon);
        }
    }

    // used at runtime to map reverse connections
    public void addInterfaceToDaemon(IPAndPort interfaceIPAndPort, IPAndPort daemonIPAndPort) {
        addInterfaceToDaemon(interfaceIPAndPort, daemonIPAndPort, true);
    }

    private void addInterfaceToDaemon(IPAndPort interfaceIPAndPort, IPAndPort daemonIPAndPort, boolean allowRemapping) {
        IPAndPort prev;
        prev = interfaceToDaemon.put(interfaceIPAndPort, daemonIPAndPort);
        if (logInterfaceToDaemonMapping && (prev == null || !prev.equals(daemonIPAndPort))) {
            Log.infof("IPAliasMap adding interfaceIPAndPort=>daemonIPAndPort: %s=>%s", interfaceIPAndPort, daemonIPAndPort);
        }
        if (!allowRemapping && prev != null && !prev.equals(daemonIPAndPort)) {
            throw new RuntimeException("Interface maps to multiple daemons: " + interfaceIPAndPort);
        }
    }

    private void addIPToDaemonMapping(String ip, IPAndPort daemonIPAndPort) {
        interfaceIPToDaemons.addValue(ip, daemonIPAndPort);
    }

    /**
     * Given an interface ip, randomly select one of the possibly many associated daemons
     *
     * @param ip
     * @return
     */
    public IPAndPort interfaceIPToRandomDaemon(String ip) {
        return interfaceIPToDaemons.getAnyValue(ip);
    }

    /**
     * If an interface ip uniquely identifies a daemon ip, return that daemon
     *
     * @param ip
     * @return the uniquely associated daemon
     */
    public IPAndPort interfaceIPToDaemon_ifUnique(String ip) {
        Set<IPAndPort> daemonIPAndPorts;
        daemonIPAndPorts = interfaceIPToDaemons.getSet(ip);
        if (daemonIPAndPorts == null || daemonIPAndPorts.size() > 1) {
            return null;
        } else {
            return daemonIPAndPorts.iterator().next();
        }
    }

    @Override
    public String toString() {
        StringBuffer sb;
        sb = new StringBuffer();

        sb.append("}");
        return sb.toString() + "\n" + CollectionUtil.mapToString(interfaceToDaemon);
    }
}


sb.append("}");
        return sb.toString() + "\n" + CollectionUtil.mapToString(interfaceToDaemon);
    }

package com.ms.silverking.cloud.dht.net;

import java.net.InetSocketAddress;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import com.google.common.collect.ImmutableMap;
import com.ms.silverking.collection.CollectionUtil;
import com.ms.silverking.collection.HashedSetMap;
import com.ms.silverking.log.Log;
import com.ms.silverking.net.AddrAndPort;
import com.ms.silverking.net.IPAndPort;

/**
 * Background: Under "normal" operation, each SK daemon has a fixed port bound to a single IP address. The port
 * is the same for all daemons. This implies that only a single daemon may run per server. It also implies that all
 * traffic will go over a single interface.
 *

 * IP aliasing allows us to overcome these limitations. IP aliasing does this in two ways:
 * 1) The aliasing feature can be used to allow a daemon to be identified by a "fake" IPAndPort. This IPAndPort is
 * used in the topology and almost all of the rest of the code as if it were a real IP. This IPAliasMap class is
 * used by MessageGroupBase (and a few other classes) to translate this IPAndPort into an actual IPAndPort when
 * communication needs to take place. By design, most of the code is unaware of this translation.
 * 2) The aliasing feature allows traffic to be distributed across multiple interfaces. This is done by associating
 * multiple interfaces with an actual IPAndPort. In this case, MessageGroupBase will translate the usual IPAndPort
 * into one of the many remote interfaces in a round-robin fashion.
 *

 * Terminology: for consistency in the above two cases, this class calls the IP address used to identify a daemon
 * the "daemon IP" or "daemon" for short. Note that the port is fixed to the SK instance port for this case.
 * Each of the one or more IPAndPorts associated with a daemon IP is called an "interface IP and port" or
 * "interface" for short. The "interface IP and port" are always a real IP and port. The daemon IP is "fake"
 * for case 1, but an actual IP for case 2.
 *

 * For case (1) each daemon IP acts as an alias, and has a single interface IP and port to which it is associated.
 * For case (2) each daemon IP has several interface IPAndPorts with which it is associated.
 *

 * Colloquially, for case (1) we can call the daemon IP an "alias". We don't do that in this code to avoid confusion
 * with case (2) where we could call the interface IP and ports aliases. (The daemon IP is real in this case.)
 *

 * Notes on case (1) - setting up "aliases"/fake IP addresses for each daemon IP.
 * - the topology, host groups, etc, must be composed of these daemon IPs, not interface IPs
 * - the DHT config must have an ipAliasMap field in the form ipAliasMap={daemonIp=interfaceIp:port}
 * As mentioned previously, the vast majority of the code will use this daemon IP as if it were the actual IP.
 * In addition, users will only use the daemon IP.
 *

 * All translation between daemon IP and interface IP and ports will take place in this class.
 * Clients will select a daemon IP address from the topology as their preferredServer. This is dealiased in
 * DHTSessionImpl to an interface IP and port, to which the client communicates.
 * Aliasing also allows nodes to deviate from the fixed port in DHT config; such nodes would require an alias
 * map entry which maps a fake IP to the port actually in use for a given address. The node itself must be passed
 * -port N on the command line at start up in order to run on a port different to that specified in DHT config.
 * This also allows many nodes to run on the same machine; several daemon IP entries in the maps could exist,
 * which resolve to the same host, but with unique ports. Note that each daemon IP must be unique in this case
 *

 * It is not required that all nodes have an alias identity, so long as it is consistent between the topology
 * and host group files, and such nodes can be omitted from the alias map. The alias resolution, when it
 * does not find an entry for such a node, would leave the address unchanged and clients/servers would proceed
 * to try and connect directly
 *

 * Notes on case 2:
 * When aliasing is used to support multi-homing, each daemon IP will be associated with
 * several interface and ports, and the dht config will have an entry of the
 * form: ipAliasMap={daemonIp=interfaceIp:port,...}. Note that all ports must be the same for case 2.
 * The ports may also be omitted.
 */
public class IPAliasMap {

    // one-to-many
    private final Map<IPAndPort, IPAndPort[]> daemonToInterfaces;

    // many-to-one
    private final Map<IPAndPort, IPAndPort> interfaceToDaemon;

    private final HashedSetMap<String, IPAndPort> interfaceIPToDaemons;

    // used for round-robin between aliases
    private final AtomicInteger interfaceIndex;

    // (e.g. for distributing traffic across multi-homed interfaces)
    private static final boolean logDaemonToInterfaceMapping = true;

    private static final boolean logInterfaceToDaemonMapping = true;

    public IPAliasMap(Map<IPAndPort, IPAndPort[]> daemonToInterfaces) {
        interfaceToDaemon = new ConcurrentHashMap<>();
        interfaceIPToDaemons = new HashedSetMap<>();
        if (daemonToInterfaces == null) {
            this.daemonToInterfaces = ImmutableMap.of();
            interfaceIndex = null;
        } else {
            sanityCheckMap(daemonToInterfaces);
            this.daemonToInterfaces = new ConcurrentHashMap<>();
            interfaceIndex = new AtomicInteger();
            this.daemonToInterfaces.putAll(daemonToInterfaces);
            if (logDaemonToInterfaceMapping) {
                IPAliasingUtil.logAliasMap(daemonToInterfaces);
            }
            addReverseMappings(daemonToInterfaces);
        }
    }

    public static IPAliasMap identityMap() {
        return new IPAliasMap(null);
    }

    private void sanityCheckMap(Map<IPAndPort, IPAndPort[]> daemonToInterfaces) {
        ensureDaemonPortsMatch(daemonToInterfaces);
    }

    private void ensureDaemonPortsMatch(Map<IPAndPort, IPAndPort[]> daemonToInterfaces) {
        int daemonPort;
        daemonPort = -1;
        for (IPAndPort daemonIPAndPort : daemonToInterfaces.keySet()) {
            if (daemonPort < 0) {
                daemonPort = daemonIPAndPort.getPort();
            } else {
                if (daemonIPAndPort.getPort() != daemonPort) {
                    throw new RuntimeException("IPAliasMap daemon ports must all be the same");
                }
            }
        }
    }

    public Map<IPAndPort, IPAndPort[]> getDaemonToInterfacesMap() {
        return ImmutableMap.copyOf(daemonToInterfaces);
    }

    public Map<IPAndPort, IPAndPort> getInterfaceToDaemonMap() {
        return ImmutableMap.copyOf(interfaceToDaemon);
    }

    public AddrAndPort daemonToInterface(AddrAndPort daemon) {
        AddrAndPort[] interfaces;
        interfaces = daemonToInterfaces.get(daemon);
        if (interfaces != null) {
            // Below is random. For now we implement round robin instead
            // Round robin
            return interfaces[interfaceIndex.getAndIncrement() % interfaces.length];
        } else {
            return daemon;
        }
    }

    public IPAndPort interfaceToDaemon(InetSocketAddress interfaceIPAndPort) {
        return interfaceToDaemon(new IPAndPort(interfaceIPAndPort));
    }

    public IPAndPort interfaceToDaemon(IPAndPort interfaceIPAndPort) {
        IPAndPort daemon;
        daemon = interfaceToDaemon.get(interfaceIPAndPort);
        return daemon != null ? daemon : interfaceIPAndPort;
    }

    /**
     * For all entries in the daemonToInterfaces argument, update the
     * reverse map. That is, update the interfaceToDaemon map with
     * the corresponding mappings.
     *
     * @param daemonToInterfaces the forward map to reverse
     */
    private void addReverseMappings(Map<IPAndPort, IPAndPort[]> daemonToInterfaces) {
        for (Map.Entry<IPAndPort, IPAndPort[]> entry : daemonToInterfaces.entrySet()) {
            addInterfacesToDaemon(entry.getValue(), entry.getKey());
        }
    }

    // only used at startup
    private void addInterfacesToDaemon(IPAndPort[] interfaces, IPAndPort daemon) {
        for (IPAndPort interfaceIPAndPort : interfaces) {
            addInterfaceToDaemon(interfaceIPAndPort, daemon, false);
            addIPToDaemonMapping(interfaceIPAndPort.getIPAsString(), daemon);
        }
    }

    // used at runtime to map reverse connections
    public void addInterfaceToDaemon(IPAndPort interfaceIPAndPort, IPAndPort daemonIPAndPort) {
        addInterfaceToDaemon(interfaceIPAndPort, daemonIPAndPort, true);
    }

    private void addInterfaceToDaemon(IPAndPort interfaceIPAndPort, IPAndPort daemonIPAndPort, boolean allowRemapping) {
        IPAndPort prev;
        prev = interfaceToDaemon.put(interfaceIPAndPort, daemonIPAndPort);
        if (logInterfaceToDaemonMapping && (prev == null || !prev.equals(daemonIPAndPort))) {
            Log.infof("IPAliasMap adding interfaceIPAndPort=>daemonIPAndPort: %s=>%s", interfaceIPAndPort, daemonIPAndPort);
        }
        if (!allowRemapping && prev != null && !prev.equals(daemonIPAndPort)) {
            throw new RuntimeException("Interface maps to multiple daemons: " + interfaceIPAndPort);
        }
    }

    private void addIPToDaemonMapping(String ip, IPAndPort daemonIPAndPort) {
        interfaceIPToDaemons.addValue(ip, daemonIPAndPort);
    }

    /**
     * Given an interface ip, randomly select one of the possibly many associated daemons
     *
     * @param ip
     * @return
     */
    public IPAndPort interfaceIPToRandomDaemon(String ip) {
        return interfaceIPToDaemons.getAnyValue(ip);
    }

    /**
     * If an interface ip uniquely identifies a daemon ip, return that daemon
     *
     * @param ip
     * @return the uniquely associated daemon
     */
    public IPAndPort interfaceIPToDaemon_ifUnique(String ip) {
        Set<IPAndPort> daemonIPAndPorts;
        daemonIPAndPorts = interfaceIPToDaemons.getSet(ip);
        if (daemonIPAndPorts == null || daemonIPAndPorts.size() > 1) {
            return null;
        } else {
            return daemonIPAndPorts.iterator().next();
        }
    }

    @Override
    public String toString() {
        StringBuffer sb;
        sb = new StringBuffer();
        sb.append("{");
        for (Map.Entry<IPAndPort, IPAndPort[]> entry : daemonToInterfaces.entrySet()) {
            sb.append(String.format("%s -> [", entry.getKey()));
            for (IPAndPort _interface : entry.getValue()) {
                sb.append(_interface + " ");
            }
            sb.append("]\n");
        }


package com.ms.silverking.cloud.dht.net;

import java.net.InetSocketAddress;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import com.google.common.collect.ImmutableMap;
import com.ms.silverking.collection.CollectionUtil;
import com.ms.silverking.collection.HashedSetMap;
import com.ms.silverking.log.Log;
import com.ms.silverking.net.AddrAndPort;
import com.ms.silverking.net.IPAndPort;

/**
 * Background: Under "normal" operation, each SK daemon has a fixed port bound to a single IP address. The port
 * is the same for all daemons. This implies that only a single daemon may run per server. It also implies that all
 * traffic will go over a single interface.
 *

 * IP aliasing allows us to overcome these limitations. IP aliasing does this in two ways:
 * 1) The aliasing feature can be used to allow a daemon to be identified by a "fake" IPAndPort. This IPAndPort is
 * used in the topology and almost all of the rest of the code as if it were a real IP. This IPAliasMap class is
 * used by MessageGroupBase (and a few other classes) to translate this IPAndPort into an actual IPAndPort when
 * communication needs to take place. By design, most of the code is unaware of this translation.
 * 2) The aliasing feature allows traffic to be distributed across multiple interfaces. This is done by associating
 * multiple interfaces with an actual IPAndPort. In this case, MessageGroupBase will translate the usual IPAndPort
 * into one of the many remote interfaces in a round-robin fashion.
 *

 * Terminology: for consistency in the above two cases, this class calls the IP address used to identify a daemon
 * the "daemon IP" or "daemon" for short. Note that the port is fixed to the SK instance port for this case.
 * Each of the one or more IPAndPorts associated with a daemon IP is called an "interface IP and port" or
 * "interface" for short. The "interface IP and port" are always a real IP and port. The daemon IP is "fake"
 * for case 1, but an actual IP for case 2.
 *

 * For case (1) each daemon IP acts as an alias, and has a single interface IP and port to which it is associated.
 * For case (2) each daemon IP has several interface IPAndPorts with which it is associated.
 *

 * Colloquially, for case (1) we can call the daemon IP an "alias". We don't do that in this code to avoid confusion
 * with case (2) where we could call the interface IP and ports aliases. (The daemon IP is real in this case.)
 *

 * Notes on case (1) - setting up "aliases"/fake IP addresses for each daemon IP.
 * - the topology, host groups, etc, must be composed of these daemon IPs, not interface IPs
 * - the DHT config must have an ipAliasMap field in the form ipAliasMap={daemonIp=interfaceIp:port}
 * As mentioned previously, the vast majority of the code will use this daemon IP as if it were the actual IP.
 * In addition, users will only use the daemon IP.
 *

 * All translation between daemon IP and interface IP and ports will take place in this class.
 * Clients will select a daemon IP address from the topology as their preferredServer. This is dealiased in
 * DHTSessionImpl to an interface IP and port, to which the client communicates.
 * Aliasing also allows nodes to deviate from the fixed port in DHT config; such nodes would require an alias
 * map entry which maps a fake IP to the port actually in use for a given address. The node itself must be passed
 * -port N on the command line at start up in order to run on a port different to that specified in DHT config.
 * This also allows many nodes to run on the same machine; several daemon IP entries in the maps could exist,
 * which resolve to the same host, but with unique ports. Note that each daemon IP must be unique in this case
 *

 * It is not required that all nodes have an alias identity, so long as it is consistent between the topology
 * and host group files, and such nodes can be omitted from the alias map. The alias resolution, when it
 * does not find an entry for such a node, would leave the address unchanged and clients/servers would proceed
 * to try and connect directly
 *

 * Notes on case 2:
 * When aliasing is used to support multi-homing, each daemon IP will be associated with
 * several interface and ports, and the dht config will have an entry of the
 * form: ipAliasMap={daemonIp=interfaceIp:port,...}. Note that all ports must be the same for case 2.
 * The ports may also be omitted.
 */
public class IPAliasMap {

    // one-to-many
    private final Map<IPAndPort, IPAndPort[]> daemonToInterfaces;

    // many-to-one
    private final Map<IPAndPort, IPAndPort> interfaceToDaemon;

    private final HashedSetMap<String, IPAndPort> interfaceIPToDaemons;

    // used for round-robin between aliases
    private final AtomicInteger interfaceIndex;

    // (e.g. for distributing traffic across multi-homed interfaces)
    private static final boolean logDaemonToInterfaceMapping = true;

    private static final boolean logInterfaceToDaemonMapping = true;

    public IPAliasMap(Map<IPAndPort, IPAndPort[]> daemonToInterfaces) {
        interfaceToDaemon = new ConcurrentHashMap<>();
        interfaceIPToDaemons = new HashedSetMap<>();
        if (daemonToInterfaces == null) {
            this.daemonToInterfaces = ImmutableMap.of();
            interfaceIndex = null;
        } else {
            sanityCheckMap(daemonToInterfaces);
            this.daemonToInterfaces = new ConcurrentHashMap<>();
            interfaceIndex = new AtomicInteger();
            this.daemonToInterfaces.putAll(daemonToInterfaces);
            if (logDaemonToInterfaceMapping) {
                IPAliasingUtil.logAliasMap(daemonToInterfaces);
            }
            addReverseMappings(daemonToInterfaces);
        }
    }

    public static IPAliasMap identityMap() {
        return new IPAliasMap(null);
    }

    private void sanityCheckMap(Map<IPAndPort, IPAndPort[]> daemonToInterfaces) {
        ensureD

    }

    private void ensureDaemonPortsMatch(Map<IPAndPort, IPAndPort[]> daemonToInterfaces) {
        int daemonPort;
        daemonPort = -1;
        for (IPAndPort daemonIPAndPort : daemonToInterfaces.keySet()) {
            if (daemonPort < 0) {
                daemonPort = daemonIPAndPort.getPort();
            } else {
                if (daemonIPAndPort.getPort() != daemonPort) {
                    throw new RuntimeException("IPAliasMap daemon ports must all be the same");
                }
            }
        }
    }

    public Map<IPAndPort, IPAndPort[]> getDaemonToInterfacesMap() {
        return ImmutableMap.copyOf(daemonToInterfaces);
    }

    public Map<IPAndPort, IPAndPort> getInterfaceToDaemonMap() {
        return ImmutableMap.copyOf(interfaceToDaemon);
    }

    public AddrAndPort daemonToInterface(AddrAndPort daemon) {
        AddrAndPort[] interfaces;
        interfaces = daemonToInterfaces.get(daemon);
        if (interfaces != null) {
            // Below is random. For now we implement round robin instead
            // Round robin
            return interfaces[interfaceIndex.getAndIncrement() % interfaces.length];
        } else {
            return daemon;
        }
    }

    public IPAndPort interfaceToDaemon(InetSocketAddress interfaceIPAndPort) {
        return interfaceToDaemon(new IPAndPort(interfaceIPAndPort));
    }

    public IPAndPort interfaceToDaemon(IPAndPort interfaceIPAndPort) {
        IPAndPort daemon;
        daemon = interfaceToDaemon.get(interfaceIPAndPort);
        return daemon != null ? daemon : interfaceIPAndPort;
    }

    /**
     * For all entries in the daemonToInterfaces argument, update the
     * reverse map. That is, update the interfaceToDaemon map with
     * the corresponding mappings.
     *
     * @param daemonToInterfaces the forward map to reverse
     */
    private void addReverseMappings(Map<IPAndPort, IPAndPort[]> daemonToInterfaces) {
        for (Map.Entry<IPAndPort, IPAndPort[]> entry : daemonToInterfaces.entrySet()) {
            addInterfacesToDaemon(entry.getValue(), entry.getKey());
        }
    }

    // only used at startup
    private void addInterfacesToDaemon(IPAndPort[] interfaces, IPAndPort daemon) {
        for (IPAndPort interfaceIPAndPort : interfaces) {
            addInterfaceToDaemon(interfaceIPAndPort, daemon, false);
            addIPToDaemonMapping(interfaceIPAndPort.getIPAsString(), daemon);
        }
    }

    // used at runtime to map reverse connections
    public void addInterfaceToDaemon(IPAndPort interfaceIPAndPort, IPAndPort daemonIPAndPort) {
        addInterfaceToDaemon(interfaceIPAndPort, daemonIPAndPort, true);
    }

    private void addInterfaceToDaemon(IPAndPort interfaceIPAndPort, IPAndPort daemonIPAndPort, boolean allowRemapping) {
        IPAndPort prev;
        prev = interfaceToDaemon.put(interfaceIPAndPort, daemonIPAndPort);
        if (logInterfaceToDaemonMapping && (prev == null || !prev.equals(daemonIPAndPort))) {
            Log.infof("IPAliasMap adding interfaceIPAndPort=>daemonIPAndPort: %s=>%s", interfaceIPAndPort, daemonIPAndPort);
        }
        if (!allowRemapping && prev != null && !prev.equals(daemonIPAndPort)) {
            throw new RuntimeException("Interface maps to multiple daemons: " + interfaceIPAndPort);
        }
    }

    private void addIPToDaemonMapping(String ip, IPAndPort daemonIPAndPort) {
        interfaceIPToDaemons.addValue(ip, daemonIPAndPort);
    }

    /**
     * Given an interface ip, randomly select one of the possibly many associated daemons
     *
     * @param ip
     * @return
     */
    public IPAndPort interfaceIPToRandomDaemon(String ip) {
        return interfaceIPToDaemons.getAnyValue(ip);
    }

    /**
     * If an interface ip uniquely identifies a daemon ip, return that daemon
     *
     * @param ip
     * @return the uniquely associated daemon
     */
    public IPAndPort interfaceIPToDaemon_ifUnique(String ip) {
        Set<IPAndPort> daemonIPAndPorts;
        daemonIPAndPorts = interfaceIPToDaemons.getSet(ip);
        if (daemonIPAndPorts == null || daemonIPAndPorts.size() > 1) {
            return null;
        } else {
            return daemonIPAndPorts.iterator().next();
        }
    }

    @Override
    public String toString() {
        StringBuffer sb;
        sb = new StringBuffer();
        sb.append("{");
        for (Map.Entry<IPAndPort, IPAndPort[]> entry : daemonToInterfaces.entrySet()) {
            sb.append(String.format("%s -> [", entry.getKey()));
            for (IPAndPort _interface : entry.getValue()) {
                sb.append(_interface + " ");
            }
            sb.append("]\n");
        }
        sb.append("}");
        return sb.toString() + "\n" + CollectionUtil.mapToString(interfaceToDaemon);
    }
}


public IPAndPort interfaceIPToDaemon_ifUnique(String ip) {
        Set<IPAndPort> daemonIPAndPorts;
        daemonIPAndPorts = interfaceIPToDaemons.getSet(ip);
        if (daemonIPAndPorts == null || daemonIPAndPorts.size() > 1) {
            return null;
        } else {
            return daemonIPAndPorts.iterator().next();
        }
    }

package com.ms.silverking.cloud.dht.net;

import java.net.InetSocketAddress;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import com.google.common.collect.ImmutableMap;
import com.ms.silverking.collection.CollectionUtil;
import com.ms.silverking.collection.HashedSetMap;
import com.ms.silverking.log.Log;
import com.ms.silverking.net.AddrAndPort;
import com.ms.silverking.net.IPAndPort;

/**
 * Background: Under "normal" operation, each SK daemon has a fixed port bound to a single IP address. The port
 * is the same for all daemons. This implies that only a single daemon may run per server. It also implies that all
 * traffic will go over a single interface.
 *

 * IP aliasing allows us to overcome these limitations. IP aliasing does this in two ways:
 * 1) The aliasing feature can be used to allow a daemon to be identified by a "fake" IPAndPort. This IPAndPort is
 * used in the topology and almost all of the rest of the code as if it were a real IP. This IPAliasMap class is
 * used by MessageGroupBase (and a few other classes) to translate this IPAndPort into an actual IPAndPort when
 * communication needs to take place. By design, most of the code is unaware of this translation.
 * 2) The aliasing feature allows traffic to be distributed across multiple interfaces. This is done by associating
 * multiple interfaces with an actual IPAndPort. In this case, MessageGroupBase will translate the usual IPAndPort
 * into one of the many remote interfaces in a round-robin fashion.
 *

 * Terminology: for consistency in the above two cases, this class calls the IP address used to identify a daemon
 * the "daemon IP" or "daemon" for short. Note that the port is fixed to the SK instance port for this case.
 * Each of the one or more IPAndPorts associated with a daemon IP is called an "interface IP and port" or
 * "interface" for short. The "interface IP and port" are always a real IP and port. The daemon IP is "fake"
 * for case 1, but an actual IP for case 2.
 *

 * For case (1) each daemon IP acts as an alias, and has a single interface IP and port to which it is associated.
 * For case (2) each daemon IP has several interface IPAndPorts with which it is associated.
 *

 * Colloquially, for case (1) we can call the daemon IP an "alias". We don't do that in this code to avoid confusion
 * with case (2) where we could call the interface IP and ports aliases. (The daemon IP is real in this case.)
 *

 * Notes on case (1) - setting up "aliases"/fake IP addresses for each daemon IP.
 * - the topology, host groups, etc, must be composed of these daemon IPs, not interface IPs
 * - the DHT config must have an ipAliasMap field in the form ipAliasMap={daemonIp=interfaceIp:port}
 * As mentioned previously, the vast majority of the code will use this daemon IP as if it were the actual IP.
 * In addition, users will only use the daemon IP.
 *

 * All translation between daemon IP and interface IP and ports will take place in this class.
 * Clients will select a daemon IP address from the topology as their preferredServer. This is dealiased in
 * DHTSessionImpl to an interface IP and port, to which the client communicates.
 * Aliasing also allows nodes to deviate from the fixed port in DHT config; such nodes would require an alias
 * map entry which maps a fake IP to the port actually in use for a given address. The node itself must be passed
 * -port N on the command line at start up in order to run on a port different to that specified in DHT config.
 * This also allows many nodes to run on the same machine; several daemon IP entries in the maps could exist,
 * which resolve to the same host, but with unique ports. Note that each daemon IP must be unique in this case
 *

 * It is not required that all nodes have an alias identity, so long as it is consistent between the topology
 * and host group files, and such nodes can be omitted from the alias map. The alias resolution, when it
 * does not find an entry for such a node, would leave the address unchanged and clients/servers would proceed
 * to try and connect directly
 *

 * Notes on case 2:
 * When aliasing is used to support multi-homing, each daemon IP will be associated with
 * several interface and ports, and the dht config will have an entry of the
 * form: ipAliasMap={daemonIp=interfaceIp:port,...}. Note that all ports must be the same for case 2.
 * The ports may also be omitted.
 */
public class IPAliasMap {

    // one-to-many
    private final Map<IPAndPort, IPAndPort[]> daemonToInterfaces;

    // many-to-one
    private final Map<IPAndPort, IPAndPort> interfaceToDaemon;

    private final HashedSetMap<String, IPAndPort> interfaceIPToDaemons;

    // used for round-robin between aliases
    private final AtomicInteger interfaceIndex;

    // (e.g. for distributing traffic across multi-homed interfaces)
    private static final boolean logDaemonToInterfaceMapping = true;

    private static final boolean logInterfaceToDaemonMapping = true;

    public IPAliasMap(Map<IPAndPort, IPAndPort[]> daemonToInterfaces) {
        interfaceToDaemon = new ConcurrentHashMap<>();
        interfaceIPToDaemons = new HashedSetMap<>();
        if (daemonToInterfaces == null) {
            this.daemonToInterfaces = ImmutableMap.of();
            interfaceIndex = null;
        } else {
            sanityCheckMap(daemonToInterfaces);
            this.daemonToInterfaces = new ConcurrentHashMap<>();
            interfaceIndex = new AtomicInteger();
            this.daemonToInterfaces.putAll(daemonToInterfaces);
            if (logDaemonToInterfaceMapping) {
                IPAliasingUtil.logAliasMap(daemonToInterfaces);
            }
            addReverseMappings(daemonToInterfaces);
        }
    }

    public static IPAliasMap identityMap() {
        return new IPAliasMap(null);
    }

    private void sanityCheckMap(Map<IPAndPort, IPAndPort[]> daemonToInterfaces) {
        ensureDaemonPortsMatch(daemonToInterfaces);
    }

    private void ensureDaemonPortsMatch(Map<IPAndPort, IPAndPort[]> daemonToInterfaces) {
        int daemonPort;
        daemonPort = -1;
        for (IPAndPort daemonIPAndPort : daemonToInterfaces.keySet()) {
            if (daemonPort < 0) {
                daemonPort = daemonIPAndPort.getPort();
            } else {
                if (daemonIPAndPort.getPort() != daemonPort) {
                    throw new RuntimeException("IPAliasMap daemon ports must all be the same");
                }
            }
        }
    }

    public Map<IPAndPort, IPAndPort[]> getDaemonToInterfacesMap() {
        return ImmutableMap.copyOf(daemonToInterfaces);
    }

    public Map<IPAndPort, IPAndPort> getInterfaceToDaemonMap() {
        return ImmutableMap.copyOf(interfaceToDaemon);
    }

    public AddrAndPort daemonToInterface(AddrAndPort daemon) {
        AddrAndPort[] interfaces;
        interfaces = daemonToInterfaces.get(daemon);
        if (interfaces != null) {
            // Below is random. For now we implement round robin instead
            // Round robin
            return interfaces[interfaceIndex.getAndIncrement() % interfaces.length];
        } else {
            return daemon;
        }
    }

    public IPAndPort interfaceToDaemon(InetSocketAddress interfaceIPAndPort) {
        return interfaceToDaemon(new IPAndPort(interfaceIPAndPort));
    }

    public IPAndPort interfaceToDaemon(IPAndPort interfaceIPAndPort) {
        IPAndPort daemon;
        daemon = interfaceToDaemon.get(interfaceIPAndPort);
        return daemon != null ? daemon : interfaceIPAndPort;
    }

    /**
     * For all entries in the daemonToInterfaces argument, update the
     * reverse map. That is, update the interfaceToDaemon map with
     * the corresponding mappings.
     *
     * @param daemonToInterfaces the forward map to reverse
     */
    private void addReverseMappings(Map<IPAndPort, IPAndPort[]> daemonToInterfaces) {
        for (Map.Entry<IPAndPort, IPAndPort[]> entry : daemonToInterfaces.entrySet()) {
            addInterfacesToDaemon(entry.getValue(), entry.getKey());
        }
    }

    // only used at startup
    private void addInterfacesToDaemon(IPAndPort[] interfaces, IPAndPort daemon) {
        for (IPAndPort interfaceIPAndPort : interfaces) {
            addInterfaceToDaemon(interfaceIPAndPort, daemon, false);
            addIPToDaemonMapping(interfaceIPAndPort.getIPAsString(), daemon);
        }
    }

    // used at runtime to map reverse connections
    public void addInterfaceToDaemon(IPAndPort interfaceIPAndPort, IPAndPort daemonIPAndPort) {
        addInterfaceToDaemon(interfaceIPAndPort, daemonIPAndPort, true);
    }

    private void addInterfaceToDaemon(IPAndPort interfaceIPAndPort, IPAndPort daemonIPAndPort, boolean allowRemapping) {
        IPAndPort prev;
        prev = interfaceToDaemon.put(interfaceIPAndPort, daemonIPAndPort);
        if (logInterfaceToDaemonMapping && (prev == null || !prev.equals(daemonIPAndPort))) {
            Log.infof("IPAliasMap adding interfaceIPAndPort=>daemonIPAndPort: %s=>%s", interfaceIPAndPort, daemonIPAndPort);
        }
        if (!allowRemapping && prev != null && !prev.equals(daemonIPAndPort)) {
            throw new RuntimeException("Interface maps to multiple daemons: " + interfaceIPAndPort);
        }
    }

    private void addIPToDaemonMapping(String ip, IPAndPort daemonIPAndPort) {
        interfaceIPToDaemons.addValue(ip, daemonIPAndPort);
    }

    /**
     * Given an interface ip, randomly select one of the possibly many associated daemons
     *
     * @param ip
     * @return
     */
    public IPAndPort interfaceIPToRandomDaemon(String ip) {
        return interfaceIPToDaemons.getAnyValue(ip);
    }

    /**
     * If an interface ip uniquely identifies a daemon ip, return that daemon
     *
     * @param ip
     * @return the uniquely associated daemon
     */

    @Override
    public String toString() {
        StringBuffer sb;
        sb = new StringBuffer();
        sb.append("{");
        for (Map.Entry<IPAndPort, IPAndPort[]> entry : daemonToInterfaces.entrySet()) {
            sb.append(String.format("%s -> [", entry.getKey()));
            for (IPAndPort _interface : entry.getValue()) {
                sb.append(_interface + " ");
            }
            sb.append("]\n");
        }
        sb.append("}");
        return sb.toString() + "\n" + CollectionUtil.mapToString(interfaceToDaemon);
    }
}


if (interfaces != null) {
            // Below is random. For now we implement round robin instead
            // Round robin
            return interfaces[interfaceIndex.getAndIncrement() % interfaces.length];
        } else {
            return daemon;
        }

package com.ms.silverking.cloud.dht.net;

import java.net.InetSocketAddress;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import com.google.common.collect.ImmutableMap;
import com.ms.silverking.collection.CollectionUtil;
import com.ms.silverking.collection.HashedSetMap;
import com.ms.silverking.log.Log;
import com.ms.silverking.net.AddrAndPort;
import com.ms.silverking.net.IPAndPort;

/**
 * Background: Under "normal" operation, each SK daemon has a fixed port bound to a single IP address. The port
 * is the same for all daemons. This implies that only a single daemon may run per server. It also implies that all
 * traffic will go over a single interface.
 *

 * IP aliasing allows us to overcome these limitations. IP aliasing does this in two ways:
 * 1) The aliasing feature can be used to allow a daemon to be identified by a "fake" IPAndPort. This IPAndPort is
 * used in the topology and almost all of the rest of the code as if it were a real IP. This IPAliasMap class is
 * used by MessageGroupBase (and a few other classes) to translate this IPAndPort into an actual IPAndPort when
 * communication needs to take place. By design, most of the code is unaware of this translation.
 * 2) The aliasing feature allows traffic to be distributed across multiple interfaces. This is done by associating
 * multiple interfaces with an actual IPAndPort. In this case, MessageGroupBase will translate the usual IPAndPort
 * into one of the many remote interfaces in a round-robin fashion.
 *

 * Terminology: for consistency in the above two cases, this class calls the IP address used to identify a daemon
 * the "daemon IP" or "daemon" for short. Note that the port is fixed to the SK instance port for this case.
 * Each of the one or more IPAndPorts associated with a daemon IP is called an "interface IP and port" or
 * "interface" for short. The "interface IP and port" are always a real IP and port. The daemon IP is "fake"
 * for case 1, but an actual IP for case 2.
 *

 * For case (1) each daemon IP acts as an alias, and has a single interface IP and port to which it is associated.
 * For case (2) each daemon IP has several interface IPAndPorts with which it is associated.
 *

 * Colloquially, for case (1) we can call the daemon IP an "alias". We don't do that in this code to avoid confusion
 * with case (2) where we could call the interface IP and ports aliases. (The daemon IP is real in this case.)
 *

 * Notes on case (1) - setting up "aliases"/fake IP addresses for each daemon IP.
 * - the topology, host groups, etc, must be composed of these daemon IPs, not interface IPs
 * - the DHT config must have an ipAliasMap field in the form ipAliasMap={daemonIp=interfaceIp:port}
 * As mentioned previously, the vast majority of the code will use this daemon IP as if it were the actual IP.
 * In addition, users will only use the daemon IP.
 *

 * All translation between daemon IP and interface IP and ports will take place in this class.
 * Clients will select a daemon IP address from the topology as their preferredServer. This is dealiased in
 * DHTSessionImpl to an interface IP and port, to which the client communicates.
 * Aliasing also allows nodes to deviate from the fixed port in DHT config; such nodes would require an alias
 * map entry which maps a fake IP to the port actually in use for a given address. The node itself must be passed
 * -port N on the command line at start up in order to run on a port different to that specified in DHT config.
 * This also allows many nodes to run on the same machine; several daemon IP entries in the maps could exist,
 * which resolve to the same host, but with unique ports. Note that each daemon IP must be unique in this case
 *

 * It is not required that all nodes have an alias identity, so long as it is consistent between the topology
 * and host group files, and such nodes can be omitted from the alias map. The alias resolution, when it
 * does not find an entry for such a node, would leave the address unchanged and clients/servers would proceed
 * to try and connect directly
 *

 * Notes on case 2:
 * When aliasing is used to support multi-homing, each daemon IP will be associated with
 * several interface and ports, and the dht config will have an entry of the
 * form: ipAliasMap={daemonIp=interfaceIp:port,...}. Note that all ports must be the same for case 2.
 * The ports may also be omitted.
 */
public class IPAliasMap {

    // one-to-many
    private final Map<IPAndPort, IPAndPort[]> daemonToInterfaces;

    // many-to-one
    private final Map<IPAndPort, IPAndPort> interfaceToDaemon;

    private final HashedSetMap<String, IPAndPort> interfaceIPToDaemons;

    // used for round-robin between aliases
    private final AtomicInteger interfaceIndex;

    // (e.g. for distributing traffic across multi-homed interfaces)
    private static final boolean logDaemonToInterfaceMapping = true;

    private static final boolean logInterfaceToDaemonMapping = true;

    public IPAliasMap(Map<IPAndPort, IPAndPort[]> daemonToInterfaces) {
        interfaceToDaemon = new ConcurrentHashMap<>();
        interfaceIPToDaemons = new HashedSetMap<>();
        if (daemonToInterfaces == null) {
            this.daemonToInterfaces = ImmutableMap.of();
            interfaceIndex = null;
        } else {
            sanityCheckMap(daemonToInterfaces);
            this.daemonToInterfaces = new ConcurrentHashMap<>();
            interfaceIndex = new AtomicInteger();
            this.daemonToInterfaces.putAll(daemonToInterfaces);
            if (logDaemonToInterfaceMapping) {
                IPAliasingUtil.logAliasMap(daemonToInterfaces);
            }
            addReverseMappings(daemonToInterfaces);
        }
    }

    public static IPAliasMap identityMap() {
        return new IPAliasMap(null);
    }

    private void sanityCheckMap(Map<IPAndPort, IPAndPort[]> daemonToInterfaces) {
        ensureDaemonPortsMatch(daemonToInterfaces);
    }

    private void ensureDaemonPortsMatch(Map<IPAndPort, IPAndPort[]> daemonToInterfaces) {
        int daemonPort;
        daemonPort = -1;
        for (IPAndPort daemonIPAndPort : daemonToInterfaces.keySet()) {
            if (daemonPort < 0) {
                daemonPort = daemonIPAndPort.getPort();
            } else {
                if (daemonIPAndPort.getPort() != daemonPort) {
                    throw new RuntimeException("IPAliasMap daemon ports must all be the same");
                }
            }
        }
    }

    public Map<IPAndPort, IPAndPort[]> getDaemonToInterfacesMap() {
        return ImmutableMap.copyOf(daemonToInterfaces);
    }

    public Map<IPAndPort, IPAndPort> getInterfaceToDaemonMap() {
        return ImmutableMap.copyOf(interfaceToDaemon);
    }

    public AddrAndPort daemonToInterface(AddrAndPort daemon) {
        AddrAndPort[] interfaces;
        interfaces = daemonToInterfaces.get(daemon);

    }

    public IPAndPort interfaceToDaemon(InetSocketAddress interfaceIPAndPort) {
        return interfaceToDaemon(new IPAndPort(interfaceIPAndPort));
    }

    public IPAndPort interfaceToDaemon(IPAndPort interfaceIPAndPort) {
        IPAndPort daemon;
        daemon = interfaceToDaemon.get(interfaceIPAndPort);
        return daemon != null ? daemon : interfaceIPAndPort;
    }

    /**
     * For all entries in the daemonToInterfaces argument, update the
     * reverse map. That is, update the interfaceToDaemon map with
     * the corresponding mappings.
     *
     * @param daemonToInterfaces the forward map to reverse
     */
    private void addReverseMappings(Map<IPAndPort, IPAndPort[]> daemonToInterfaces) {
        for (Map.Entry<IPAndPort, IPAndPort[]> entry : daemonToInterfaces.entrySet()) {
            addInterfacesToDaemon(entry.getValue(), entry.getKey());
        }
    }

    // only used at startup
    private void addInterfacesToDaemon(IPAndPort[] interfaces, IPAndPort daemon) {
        for (IPAndPort interfaceIPAndPort : interfaces) {
            addInterfaceToDaemon(interfaceIPAndPort, daemon, false);
            addIPToDaemonMapping(interfaceIPAndPort.getIPAsString(), daemon);
        }
    }

    // used at runtime to map reverse connections
    public void addInterfaceToDaemon(IPAndPort interfaceIPAndPort, IPAndPort daemonIPAndPort) {
        addInterfaceToDaemon(interfaceIPAndPort, daemonIPAndPort, true);
    }

    private void addInterfaceToDaemon(IPAndPort interfaceIPAndPort, IPAndPort daemonIPAndPort, boolean allowRemapping) {
        IPAndPort prev;
        prev = interfaceToDaemon.put(interfaceIPAndPort, daemonIPAndPort);
        if (logInterfaceToDaemonMapping && (prev == null || !prev.equals(daemonIPAndPort))) {
            Log.infof("IPAliasMap adding interfaceIPAndPort=>daemonIPAndPort: %s=>%s", interfaceIPAndPort, daemonIPAndPort);
        }
        if (!allowRemapping && prev != null && !prev.equals(daemonIPAndPort)) {
            throw new RuntimeException("Interface maps to multiple daemons: " + interfaceIPAndPort);
        }
    }

    private void addIPToDaemonMapping(String ip, IPAndPort daemonIPAndPort) {
        interfaceIPToDaemons.addValue(ip, daemonIPAndPort);
    }

    /**
     * Given an interface ip, randomly select one of the possibly many associated daemons
     *
     * @param ip
     * @return
     */
    public IPAndPort interfaceIPToRandomDaemon(String ip) {
        return interfaceIPToDaemons.getAnyValue(ip);
    }

    /**
     * If an interface ip uniquely identifies a daemon ip, return that daemon
     *
     * @param ip
     * @return the uniquely associated daemon
     */
    public IPAndPort interfaceIPToDaemon_ifUnique(String ip) {
        Set<IPAndPort> daemonIPAndPorts;
        daemonIPAndPorts = interfaceIPToDaemons.getSet(ip);
        if (daemonIPAndPorts == null || daemonIPAndPorts.size() > 1) {
            return null;
        } else {
            return daemonIPAndPorts.iterator().next();
        }
    }

    @Override
    public String toString() {
        StringBuffer sb;
        sb = new StringBuffer();
        sb.append("{");
        for (Map.Entry<IPAndPort, IPAndPort[]> entry : daemonToInterfaces.entrySet()) {
            sb.append(String.format("%s -> [", entry.getKey()));
            for (IPAndPort _interface : entry.getValue()) {
                sb.append(_interface + " ");
            }
            sb.append("]\n");
        }
        sb.append("}");
        return sb.toString() + "\n" + CollectionUtil.mapToString(interfaceToDaemon);
    }
}


package com.ms.silverking.cloud.dht.net;

import java.net.InetSocketAddress;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import com.google.common.collect.ImmutableMap;
import com.ms.silverking.collection.CollectionUtil;
import com.ms.silverking.collection.HashedSetMap;
import com.ms.silverking.log.Log;
import com.ms.silverking.net.AddrAndPort;
import com.ms.silverking.net.IPAndPort;

/**
 * Background: Under "normal" operation, each SK daemon has a fixed port bound to a single IP address. The port
 * is the same for all daemons. This implies that only a single daemon may run per server. It also implies that all
 * traffic will go over a single interface.
 *

 * IP aliasing allows us to overcome these limitations. IP aliasing does this in two ways:
 * 1) The aliasing feature can be used to allow a daemon to be identified by a "fake" IPAndPort. This IPAndPort is
 * used in the topology and almost all of the rest of the code as if it were a real IP. This IPAliasMap class is
 * used by MessageGroupBase (and a few other classes) to translate this IPAndPort into an actual IPAndPort when
 * communication needs to take place. By design, most of the code is unaware of this translation.
 * 2) The aliasing feature allows traffic to be distributed across multiple interfaces. This is done by associating
 * multiple interfaces with an actual IPAndPort. In this case, MessageGroupBase will translate the usual IPAndPort
 * into one of the many remote interfaces in a round-robin fashion.
 *

 * Terminology: for consistency in the above two cases, this class calls the IP address used to identify a daemon
 * the "daemon IP" or "daemon" for short. Note that the port is fixed to the SK instance port for this case.
 * Each of the one or more IPAndPorts associated with a daemon IP is called an "interface IP and port" or
 * "interface" for short. The "interface IP and port" are always a real IP and port. The daemon IP is "fake"
 * for case 1, but an actual IP for case 2.
 *

 * For case (1) each daemon IP acts as an alias, and has a single interface IP and port to which it is associated.
 * For case (2) each daemon IP has several interface IPAndPorts with which it is associated.
 *

 * Colloquially, for case (1) we can call the daemon IP an "alias". We don't do that in this code to avoid confusion
 * with case (2) where we could call the interface IP and ports aliases. (The daemon IP is real in this case.)
 *

 * Notes on case (1) - setting up "aliases"/fake IP addresses for each daemon IP.
 * - the topology, host groups, etc, must be composed of these daemon IPs, not interface IPs
 * - the DHT config must have an ipAliasMap field in the form ipAliasMap={daemonIp=interfaceIp:port}
 * As mentioned previously, the vast majority of the code will use this daemon IP as if it were the actual IP.
 * In addition, users will only use the daemon IP.
 *

 * All translation between daemon IP and interface IP and ports will take place in this class.
 * Clients will select a daemon IP address from the topology as their preferredServer. This is dealiased in
 * DHTSessionImpl to an interface IP and port, to which the client communicates.
 * Aliasing also allows nodes to deviate from the fixed port in DHT config; such nodes would require an alias
 * map entry which maps a fake IP to the port actually in use for a given address. The node itself must be passed
 * -port N on the command line at start up in order to run on a port different to that specified in DHT config.
 * This also allows many nodes to run on the same machine; several daemon IP entries in the maps could exist,
 * which resolve to the same host, but with unique ports. Note that each daemon IP must be unique in this case
 *

 * It is not required that all nodes have an alias identity, so long as it is consistent between the topology
 * and host group files, and such nodes can be omitted from the alias map. The alias resolution, when it
 * does not find an entry for such a node, would leave the address unchanged and clients/servers would proceed
 * to try and connect directly
 *

 * Notes on case 2:
 * When aliasing is used to support multi-homing, each daemon IP will be associated with
 * several interface and ports, and the dht config will have an entry of the
 * form: ipAliasMap={daemonIp=interfaceIp:port,...}. Note that all ports must be the same for case 2.
 * The ports may also be omitted.
 */
public class IPAliasMap {

    // one-to-many
    private final Map<IPAndPort, IPAndPort[]> daemonToInterfaces;

    // many-to-one
    private final Map<IPAndPort, IPAndPort> interfaceToDaemon;

    private final HashedSetMap<String, IPAndPort> interfaceIPToDaemons;

    // used for round-robin between aliases
    private final AtomicInteger interfaceIndex;

    // (e.g. for distributing traffic across multi-homed interfaces)
    private static final boolean logDaemonToInterfaceMapping = true;

    private static final boolean logInterfaceToDaemonMapping = true;

    public IPAliasMap(Map<IPAndPort, IPAndPort[]> daemonToInterfaces) {
        interfaceToDaemon = new ConcurrentHashMap<>();
        interfaceIPToDaemons = new HashedSetMap<>();
        if (daemonToInterfaces == null) {
            this.daemonToInterfaces = ImmutableMap.of();
            interfaceIndex = null;
        } else {
            sanityCheckMap(daemonToInterfaces);
            this.daemonToInterfaces = new ConcurrentHashMap<>();
            interfaceIndex = new AtomicInteger();
            this.daemonToInterfaces.putAll(daemonToInterfaces);
            if (logDaemonToInterfaceMapping) {
                IPAliasingUtil.logAliasMap(daemonToInterfaces);
            }
            addReverseMappings(daemonToInterfaces);
        }
    }

    public static IPAliasMap identityMap() {
        return new IPAliasMap(null);
    }

    private void sanityCheckMap(Map<IPAndPort, IPAndPort[]> daemonToInterfaces) {
        ensureDaemonPortsMatch(daemonToInterfaces);
    }

    private void ensureDaemonPortsMatch(Map<IPAndPort, IPAndPort[]> daemonToInterfaces) {
        int daemonPort;
        daemonPort = -1;
        for (IPAndPort daemonIPAndPort : daemonToInterfaces.keySet()) {
            if (daemonPort < 0) {
                daemonPort = daemonIPAndPort.getPort();
            } else {
                if (daemonIPAndPort.getPort() != daemonPort) {
                    throw new RuntimeException("IPAliasMap daemon ports must all be the same");
                }
            }
        }
    }

    public Map<IPAndPort, IPAndPort[]> getDaemonToInterfacesMap() {
        return ImmutableMap.copyOf(daemonToInterfaces);
    }

    public Map<IPAndPort, IPAndPort> getInterfaceToDaemonMap() {
        return ImmutableMap.copyOf(interfaceToDaemon);
    }

    public AddrAndPort daemonToInterface(AddrAndPort daemon) {
        AddrAndPort[] interfaces;
        interfaces = daemonToInterfaces.get(daemon);
        if (interfaces != null) {
            // Below is random. For now we implement round robin instead
            // Round robin
            return interfaces[interfaceIndex.getAndIncrement() % interfaces.length];
        } else {
            return daemon;
        }
    }

    public IPAndPort interfaceToDaemon(InetSocketAddress interfaceIPAndPort) {
        return interfaceToDaemon(new IPAndPort(interfaceIPAndPort));
    }

    public IPAndPort interfaceToDaemon(IPAndPort interfaceIPAndPort) {
        IPAndPort daemon;
        daemon = interfaceToDaemon.get(interfaceIPAndPort);
        return daemon != null ? daemon : interfaceIPAndPort;
    }

    /**
     * For all entries in the daemonToInterfaces argument, update the
     * reverse map. That is, update the interfaceToDaemon map with
     * the corresponding mappings.
     *
     * @param daemonToInterfaces the forward map to reverse
     */
    private void addReverseMappings(Map<IPAndPort, IPAndPort[]> daemonToInterfaces) {
        for (Map.Entry<IPAndPort, IPAndPort[]> entry : daemonToInterfaces.entrySet()) {
            addInterfacesToDaemon(entry.getValue(), entry.getKey());
        }
    }

    // only used at startup
    private void addInterfacesToDaemon(IPAndPort[] interfaces, IPAndPort daemon) {
        for (IPAndPort interfaceIPAndPort : interfaces) {
            addInterfaceToDaemon(interfaceIPAndPort, daemon, false);
            addIPToDaemonMapping(interfaceIPAndPort.getIPAsString(), daemon);
        }
    }

    // used at runtime to map reverse connections
    public void addInterfaceToDaemon(IPAndPort interfaceIPAndPort, IPAndPort daemonIPAndPort) {
        addInterfaceToDaemon(interfaceIPAndPort, daemonIPAndPort, true);
    }

    private void addInterfaceToDaemon(IPAndPort interfaceIPAndPort, IPAndPort daemonIPAndPort, boolean allowRemapping) {
        IPAndPort prev;
        prev = interfaceToDaemon.put(interfaceIPAndPort, daemonIPAndPort);
        if (logInterfaceToDaemonMapping && (prev == null || !prev.equals(daemonIPAndPort))) {
            Log.infof("IPAliasMap adding interfaceIPAndPort=>daemonIPAndPort: %s=>%s", interfaceIPAndPort, daemonIPAndPort);
        }
        if (!allowRemapping && prev != null && !prev.equals(daemonIPAndPort)) {
            throw new RuntimeException("Interface maps to multiple daemons: " + interfaceIPAndPort);
        }
    }

    private void addIPToDaemonMapping(String ip, IPAndPort daemonIPAndPort) {
        interfaceIPToDaemons.addValue(ip, daemonIPAndPort);
    }

    /**
     * Given an interface ip, randomly select one of the possibly many associated

 daemons
     *
     * @param ip
     * @return
     */
    public IPAndPort interfaceIPToRandomDaemon(String ip) {
        return interfaceIPToDaemons.getAnyValue(ip);
    }

    /**
     * If an interface ip uniquely identifies a daemon ip, return that daemon
     *
     * @param ip
     * @return the uniquely associated daemon
     */
    public IPAndPort interfaceIPToDaemon_ifUnique(String ip) {
        Set<IPAndPort> daemonIPAndPorts;
        daemonIPAndPorts = interfaceIPToDaemons.getSet(ip);
        if (daemonIPAndPorts == null || daemonIPAndPorts.size() > 1) {
            return null;
        } else {
            return daemonIPAndPorts.iterator().next();
        }
    }

    @Override
    public String toString() {
        StringBuffer sb;
        sb = new StringBuffer();
        sb.append("{");
        for (Map.Entry<IPAndPort, IPAndPort[]> entry : daemonToInterfaces.entrySet()) {
            sb.append(String.format("%s -> [", entry.getKey()));
            for (IPAndPort _interface : entry.getValue()) {
                sb.append(_interface + " ");
            }
            sb.append("]\n");
        }
        sb.append("}");
        return sb.toString() + "\n" + CollectionUtil.mapToString(interfaceToDaemon);
    }
}


private static List<NodeAllocationResult> buildNodeDecisions(NodesToAllocate nodesToAllocate, FetchResult<NodeGatewayStartedShards> fetchedShardData, Set<String> inSyncAllocationIds)

package org.elasticsearch.gateway;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.message.ParameterizedMessage;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.cluster.routing.RecoverySource;
import org.elasticsearch.cluster.routing.RoutingNode;
import org.elasticsearch.cluster.routing.RoutingNodes;
import org.elasticsearch.cluster.routing.ShardRouting;
import org.elasticsearch.cluster.routing.UnassignedInfo.AllocationStatus;
import org.elasticsearch.cluster.routing.allocation.AllocateUnassignedDecision;
import org.elasticsearch.cluster.routing.allocation.NodeAllocationResult;
import org.elasticsearch.cluster.routing.allocation.NodeAllocationResult.ShardStoreInfo;
import org.elasticsearch.cluster.routing.allocation.RoutingAllocation;
import org.elasticsearch.cluster.routing.allocation.decider.Decision;
import org.elasticsearch.cluster.routing.allocation.decider.Decision.Type;
import org.elasticsearch.env.ShardLockObtainFailedException;
import org.elasticsearch.gateway.AsyncShardFetch.FetchResult;
import org.elasticsearch.gateway.TransportNodesListGatewayStartedShards.NodeGatewayStartedShards;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * The primary shard allocator allocates unassigned primary shards to nodes that hold
 * valid copies of the unassigned primaries.  It does this by iterating over all unassigned
 * primary shards in the routing table and fetching shard metadata from each node in the cluster
 * that holds a copy of the shard.  The shard metadata from each node is compared against the
 * set of valid allocation IDs and for all valid shard copies (if any), the primary shard allocator
 * executes the allocation deciders to chose a copy to assign the primary shard to.
 *
 * Note that the PrimaryShardAllocator does *not* allocate primaries on index creation
 * (see {@link org.elasticsearch.cluster.routing.allocation.allocator.BalancedShardsAllocator}),
 * nor does it allocate primaries when a primary shard failed and there is a valid replica
 * copy that can immediately be promoted to primary, as this takes place in {@link RoutingNodes#failShard}.
 */
public abstract class PrimaryShardAllocator extends BaseGatewayShardAllocator {

    /**
     * Is the allocator responsible for allocating the given {@link ShardRouting}?
     */
    private static boolean isResponsibleFor(final ShardRouting shard) {
        return // must be primary
        shard.primary() && // must be unassigned
        shard.unassigned() && // only handle either an existing store or a snapshot recovery
        (shard.recoverySource().getType() == RecoverySource.Type.EXISTING_STORE || shard.recoverySource().getType() == RecoverySource.Type.SNAPSHOT);
    }

    @Override
    public AllocateUnassignedDecision makeAllocationDecision(final ShardRouting unassignedShard, final RoutingAllocation allocation, final Logger logger) {
        if (isResponsibleFor(unassignedShard) == false) {
            // this allocator is not responsible for allocating this shard
            return AllocateUnassignedDecision.NOT_TAKEN;
        }
        final boolean explain = allocation.debugDecision();
        final FetchResult<NodeGatewayStartedShards> shardState = fetchData(unassignedShard, allocation);
        if (shardState.hasData() == false) {
            allocation.setHasPendingAsyncFetch();
            List<NodeAllocationResult> nodeDecisions = null;
            if (explain) {
                nodeDecisions = buildDecisionsForAllNodes(unassignedShard, allocation);
            }
            return AllocateUnassignedDecision.no(AllocationStatus.FETCHING_SHARD_DATA, nodeDecisions);
        }
        // don't create a new IndexSetting object for every shard as this could cause a lot of garbage
        // on cluster restart if we allocate a boat load of shards
        final IndexMetaData indexMetaData = allocation.metaData().getIndexSafe(unassignedShard.index());
        final Set<String> inSyncAllocationIds = indexMetaData.inSyncAllocationIds(unassignedShard.id());
        final boolean snapshotRestore = unassignedShard.recoverySource().getType() == RecoverySource.Type.SNAPSHOT;
        assert inSyncAllocationIds.isEmpty() == false;
        // use in-sync allocation ids to select nodes
        final NodeShardsResult nodeShardsResult = buildNodeShardsResult(unassignedShard, snapshotRestore, allocation.getIgnoreNodes(unassignedShard.shardId()), inSyncAllocationIds, shardState, logger);
        final boolean enoughAllocationsFound = nodeShardsResult.orderedAllocationCandidates.size() > 0;
        logger.debug("[{}][{}]: found {} allocation candidates of {} based on allocation ids: [{}]", unassignedShard.index(), unassignedShard.id(), nodeShardsResult.orderedAllocationCandidates.size(), unassignedShard, inSyncAllocationIds);
        if (enoughAllocationsFound == false) {
            if (snapshotRestore) {
                // let BalancedShardsAllocator take care of allocating this shard
                logger.debug("[{}][{}]: missing local data, will restore from [{}]", unassignedShard.index(), unassignedShard.id(), unassignedShard.recoverySource());
                return AllocateUnassignedDecision.NOT_TAKEN;
            } else {
                // We have a shard that was previously allocated, but we could not find a valid shard copy to allocate the primary.
                // We could just be waiting for the node that holds the primary to start back up, in which case the allocation for
                // this shard will be picked up when the node joins and we do another allocation reroute
                logger.debug("[{}][{}]: not allocating, number_of_allocated_shards_found [{}]", unassignedShard.index(), unassignedShard.id(), nodeShardsResult.allocationsFound);
                return AllocateUnassignedDecision.no(AllocationStatus.NO_VALID_SHARD_COPY, explain ? buildNodeDecisions(null, shardState, inSyncAllocationIds) : null);
            }
        }
        NodesToAllocate nodesToAllocate = buildNodesToAllocate(allocation, nodeShardsResult.orderedAllocationCandidates, unassignedShard, false);
        DiscoveryNode node = null;
        String allocationId = null;
        boolean throttled = false;
        if (nodesToAllocate.yesNodeShards.isEmpty() == false) {
            DecidedNode decidedNode = nodesToAllocate.yesNodeShards.get(0);
            logger.debug("[{}][{}]: allocating [{}] to [{}] on primary allocation", unassignedShard.index(), unassignedShard.id(), unassignedShard, decidedNode.nodeShardState.getNode());
            node = decidedNode.nodeShardState.getNode();
            allocationId = decidedNode.nodeShardState.allocationId();
        } else if (nodesToAllocate.throttleNodeShards.isEmpty() && !nodesToAllocate.noNodeShards.isEmpty()) {
            // The deciders returned a NO decision for all nodes with shard copies, so we check if primary shard
            // can be force-allocated to one of the nodes.
            nodesToAllocate = buildNodesToAllocate(allocation, nodeShardsResult.orderedAllocationCandidates, unassignedShard, true);
            if (nodesToAllocate.yesNodeShards.isEmpty() == false) {
                final DecidedNode decidedNode = nodesToAllocate.yesNodeShards.get(0);
                final NodeGatewayStartedShards nodeShardState = decidedNode.nodeShardState;
                logger.debug("[{}][{}]: allocating [{}] to [{}] on forced primary allocation", unassignedShard.index(), unassignedShard.id(), unassignedShard, nodeShardState.getNode());
                node = nodeShardState.getNode();
                allocationId = nodeShardState.allocationId();
            } else if (nodesToAllocate.throttleNodeShards.isEmpty() == false) {
                logger.debug("[{}][{}]: throttling allocation [{}] to [{}] on forced primary allocation", unassignedShard.index(), unassignedShard.id(), unassignedShard, nodesToAllocate.throttleNodeShards);
                throttled = true;
            } else {
                logger.debug("[{}][{}]: forced primary allocation denied [{}]", unassignedShard.index(), unassignedShard.id(), unassignedShard);
            }
        } else {
            // we are throttling this, since we are allowed to allocate to this node but there are enough allocations
            // taking place on the node currently, ignore it for now
            logger.debug("[{}][{}]: throttling allocation [{}] to [{}] on primary allocation", unassignedShard.index(), unassignedShard.id(), unassignedShard, nodesToAllocate.throttleNodeShards);
            throttled = true;
        }
        List<NodeAllocationResult> nodeResults = null;
        if (explain) {
            nodeResults = buildNodeDecisions(nodesToAllocate, shardState, inSyncAllocationIds);
        }
        if (allocation.hasPendingAsyncFetch()) {
            return AllocateUnassignedDecision.no(AllocationStatus.FETCHING_SHARD_DATA, nodeResults);
        } else if (node != null) {
            return AllocateUnassignedDecision.yes(node, allocationId, nodeResults, false);
        } else if (throttled) {
            return AllocateUnassignedDecision.throttle(nodeResults);
        } else {
            return AllocateUnassignedDecision.no(AllocationStatus.DECIDERS_NO, nodeResults, true);
        }
    }

    /**
     * Builds a map of nodes to the corresponding allocation decisions for those nodes.
     */

{
        List<NodeAllocationResult> nodeResults = new ArrayList<>();
        Collection<NodeGatewayStartedShards> ineligibleShards;
        if (nodesToAllocate != null) {
            final Set<DiscoveryNode> discoNodes = new HashSet<>();
            nodeResults.addAll(Stream.of(nodesToAllocate.yesNodeShards, nodesToAllocate.throttleNodeShards, nodesToAllocate.noNodeShards).flatMap(Collection::stream).map(dnode -> {
                discoNodes.add(dnode.nodeShardState.getNode());
                return new NodeAllocationResult(dnode.nodeShardState.getNode(), shardStoreInfo(dnode.nodeShardState, inSyncAllocationIds), dnode.decision);
            }).collect(Collectors.toList()));
            ineligibleShards = fetchedShardData.getData().values().stream().filter(shardData -> discoNodes.contains(shardData.getNode()) == false).collect(Collectors.toList());
        } else {
            // there were no shard copies that were eligible for being assigned the allocation,
            // so all fetched shard data are ineligible shards
            ineligibleShards = fetchedShardData.getData().values();
        }
        nodeResults.addAll(ineligibleShards.stream().map(shardData -> new NodeAllocationResult(shardData.getNode(), shardStoreInfo(shardData, inSyncAllocationIds), null)).collect(Collectors.toList()));
        return nodeResults;
    }

    private static ShardStoreInfo shardStoreInfo(NodeGatewayStartedShards nodeShardState, Set<String> inSyncAllocationIds) {
        final Exception storeErr = nodeShardState.storeException();
        final boolean inSync = nodeShardState.allocationId() != null && inSyncAllocationIds.contains(nodeShardState.allocationId());
        return new ShardStoreInfo(nodeShardState.allocationId(), inSync, storeErr);
    }

    private static final Comparator<NodeGatewayStartedShards> NO_STORE_EXCEPTION_FIRST_COMPARATOR = Comparator.comparing((NodeGatewayStartedShards state) -> state.storeException() == null).reversed();

    private static final Comparator<NodeGatewayStartedShards> PRIMARY_FIRST_COMPARATOR = Comparator.comparing(NodeGatewayStartedShards::primary).reversed();

    /**
     * Builds a list of nodes. If matchAnyShard is set to false, only nodes that have an allocation id matching
     * inSyncAllocationIds are added to the list. Otherwise, any node that has a shard is added to the list, but
     * entries with matching allocation id are always at the front of the list.
     */
    protected static NodeShardsResult buildNodeShardsResult(ShardRouting shard, boolean matchAnyShard, Set<String> ignoreNodes, Set<String> inSyncAllocationIds, FetchResult<NodeGatewayStartedShards> shardState, Logger logger) {
        List<NodeGatewayStartedShards> nodeShardStates = new ArrayList<>();
        int numberOfAllocationsFound = 0;
        for (NodeGatewayStartedShards nodeShardState : shardState.getData().values()) {
            DiscoveryNode node = nodeShardState.getNode();
            String allocationId = nodeShardState.allocationId();
            if (ignoreNodes.contains(node.getId())) {
                continue;
            }
            if (nodeShardState.storeException() == null) {
                if (allocationId == null) {
                    logger.trace("[{}] on node [{}] has no shard state information", shard, nodeShardState.getNode());
                } else {
                    logger.trace("[{}] on node [{}] has allocation id [{}]", shard, nodeShardState.getNode(), allocationId);
                }
            } else {
                final String finalAllocationId = allocationId;
                if (nodeShardState.storeException() instanceof ShardLockObtainFailedException) {
                    logger.trace(() -> new ParameterizedMessage("[{}] on node [{}] has allocation id [{}] but the store can not be " + "opened as it's locked, treating as valid shard", shard, nodeShardState.getNode(), finalAllocationId), nodeShardState.storeException());
                } else {
                    logger.trace(() -> new ParameterizedMessage("[{}] on node [{}] has allocation id [{}] but the store can not be " + "opened, treating as no allocation id", shard, nodeShardState.getNode(), finalAllocationId), nodeShardState.storeException());
                    allocationId = null;
                }
            }
            if (allocationId != null) {
                assert nodeShardState.storeException() == null || nodeShardState.storeException() instanceof ShardLockObtainFailedException : "only allow store that can be opened or that throws a ShardLockObtainFailedException while being opened but got a " + "store throwing " + nodeShardState.storeException();
                numberOfAllocationsFound++;
                if (matchAnyShard || inSyncAllocationIds.contains(nodeShardState.allocationId())) {
                    nodeShardStates.add(nodeShardState);
                }
            }
        }
        // allocation preference
        final Comparator<NodeGatewayStartedShards> comparator;
        if (matchAnyShard) {
            // prefer shards with matching allocation ids
            Comparator<NodeGatewayStartedShards> matchingAllocationsFirst = Comparator.comparing((NodeGatewayStartedShards state) -> inSyncAllocationIds.contains(state.allocationId())).reversed();
            comparator = matchingAllocationsFirst.thenComparing(NO_STORE_EXCEPTION_FIRST_COMPARATOR).thenComparing(PRIMARY_FIRST_COMPARATOR);
        } else {
            comparator = NO_STORE_EXCEPTION_FIRST_COMPARATOR.thenComparing(PRIMARY_FIRST_COMPARATOR);
        }
        nodeShardStates.sort(comparator);
        if (logger.isTraceEnabled()) {
            logger.trace("{} candidates for allocation: {}", shard, nodeShardStates.stream().map(s -> s.getNode().getName()).collect(Collectors.joining(", ")));
        }
        return new NodeShardsResult(nodeShardStates, numberOfAllocationsFound);
    }

    /**
     * Split the list of node shard states into groups yes/no/throttle based on allocation deciders
     */
    private NodesToAllocate buildNodesToAllocate(RoutingAllocation allocation, List<NodeGatewayStartedShards> nodeShardStates, ShardRouting shardRouting, boolean forceAllocate) {
        List<DecidedNode> yesNodeShards = new ArrayList<>();
        List<DecidedNode> throttledNodeShards = new ArrayList<>();
        List<DecidedNode> noNodeShards = new ArrayList<>();
        for (NodeGatewayStartedShards nodeShardState : nodeShardStates) {
            RoutingNode node = allocation.routingNodes().node(nodeShardState.getNode().getId());
            if (node == null) {
                continue;
            }
            Decision decision = forceAllocate ? allocation.deciders().canForceAllocatePrimary(shardRouting, node, allocation) : allocation.deciders().canAllocate(shardRouting, node, allocation);
            DecidedNode decidedNode = new DecidedNode(nodeShardState, decision);
            if (decision.type() == Type.THROTTLE) {
                throttledNodeShards.add(decidedNode);
            } else if (decision.type() == Type.NO) {
                noNodeShards.add(decidedNode);
            } else {
                yesNodeShards.add(decidedNode);
            }
        }
        return new NodesToAllocate(Collections.unmodifiableList(yesNodeShards), Collections.unmodifiableList(throttledNodeShards), Collections.unmodifiableList(noNodeShards));
    }

    protected abstract FetchResult<NodeGatewayStartedShards> fetchData(ShardRouting shard, RoutingAllocation allocation);

    private static class NodeShardsResult {

        final List<NodeGatewayStartedShards> orderedAllocationCandidates;

        final int allocationsFound;

        NodeShardsResult(List<NodeGatewayStartedShards> orderedAllocationCandidates, int allocationsFound) {
            this.orderedAllocationCandidates = orderedAllocationCandidates;
            this.allocationsFound = allocationsFound;
        }
    }

    static class NodesToAllocate {

        final List<DecidedNode> yesNodeShards;

        final List<DecidedNode> throttleNodeShards;

        final List<DecidedNode> noNodeShards;

        NodesToAllocate(List<DecidedNode> yesNodeShards, List<DecidedNode> throttleNodeShards, List<DecidedNode> noNodeShards) {
            this.yesNodeShards = yesNodeShards;
            this.throttleNodeShards = throttleNodeShards;
            this.noNodeShards = noNodeShards;
        }
    }

    /**
     * This class encapsulates the shard state retrieved from a node and the decision that was made
     * by the allocator for allocating to the node that holds the shard copy.
     */
    private static class DecidedNode {

        final NodeGatewayStartedShards nodeShardState;

        final Decision decision;

        private DecidedNode(NodeGatewayStartedShards nodeShardState, Decision decision) {
            this.nodeShardState = nodeShardState;
            this.decision = decision;
        }
    }
}