protected static Set adjacentHosts()

in server/src/main/java/org/apache/cassandra/sidecar/coordination/InnerDcTokenAdjacentPeerProvider.java [212:268]


    protected static Set<Host> adjacentHosts(DriverUtils driverUtils,
                                             Predicate<Host> isLocal,
                                             BigInteger localMinToken,
                                             List<Pair<Host, BigInteger>> sortedLocalDcHosts,
                                             int quorum)
    {
        Set<Host> adjacentHosts = new HashSet<>(quorum);

        // all hosts in token order
        int idx = Collections.binarySearch(sortedLocalDcHosts, null, (o1, o2) -> {
            BigInteger token1 = (o1 == null) ? localMinToken : o1.getValue();
            BigInteger token2 = (o2 == null) ? localMinToken : o2.getValue();
            return token1.compareTo(token2);
        });

        if (idx < 0)
        {
            throw new IllegalStateException("Could not find local instance");
        }

        for (int i = 1; i <= quorum; i++)
        {
            // if max RF is greater than the number of other available hosts then it will wrap around
            int nextIdx = (idx + i) % (sortedLocalDcHosts.size());
            Host nextHost = sortedLocalDcHosts.get(nextIdx).getKey();
            if (isLocal.test(nextHost))
            {
                LOGGER.warn("Insufficient other hosts to satisfy quorum quorum={} numHosts={}", quorum, i);
                quorum = i - 1;
                break;
            }
        }

        for (int i = 1; i <= quorum; i++)
        {
            int nextIdx = (idx + i) % (sortedLocalDcHosts.size());
            adjacentHosts.add(sortedLocalDcHosts.get(nextIdx).getKey());
        }

        Preconditions.checkArgument(adjacentHosts.size() == quorum, String.format("Failed to find %d adjacent node(s) in the ring", quorum));
        for (Host host : adjacentHosts)
        {
            if (isLocal.test(host))
            {
                InetAddress address = driverUtils.getSocketAddress(host).getAddress();
                LOGGER.warn("Local instance selected as adjacent host localMinToken={} hostId={} address={} hostname={} canonicalHostname={}",
                            localMinToken,
                            host.getHostId(),
                            address.getHostAddress(),
                            address.getHostName(),
                            address.getCanonicalHostName());
                throw new IllegalArgumentException(String.format("Local instance selected as adjacent host: %s", host.getHostId()));
            }
        }

        return adjacentHosts;
    }