Search in sources :

Example 6 with ClientConnectionsEntry

use of org.redisson.connection.ClientConnectionsEntry in project redisson by redisson.

the class WeightedRoundRobinBalancer method getEntry.

@Override
public ClientConnectionsEntry getEntry(List<ClientConnectionsEntry> clients) {
    Set<InetSocketAddress> addresses = getAddresses(clients);
    if (!addresses.equals(weights.keySet())) {
        Set<InetSocketAddress> newAddresses = new HashSet<InetSocketAddress>(addresses);
        newAddresses.removeAll(weights.keySet());
        for (InetSocketAddress addr : newAddresses) {
            weights.put(addr, new WeightEntry(defaultWeight));
        }
    }
    Map<InetSocketAddress, WeightEntry> weightsCopy = new HashMap<InetSocketAddress, WeightEntry>(weights);
    synchronized (this) {
        for (Iterator<WeightEntry> iterator = weightsCopy.values().iterator(); iterator.hasNext(); ) {
            WeightEntry entry = iterator.next();
            if (entry.isWeightCounterZero()) {
                iterator.remove();
            }
        }
        if (weightsCopy.isEmpty()) {
            for (WeightEntry entry : weights.values()) {
                entry.resetWeightCounter();
            }
            weightsCopy = weights;
        }
        List<ClientConnectionsEntry> clientsCopy = findClients(clients, weightsCopy);
        // case, there should always be a connection to the master.
        if (clientsCopy.isEmpty()) {
            for (WeightEntry entry : weights.values()) {
                entry.resetWeightCounter();
            }
            weightsCopy = weights;
            clientsCopy = findClients(clients, weightsCopy);
        }
        int ind = Math.abs(index.incrementAndGet() % clientsCopy.size());
        ClientConnectionsEntry entry = clientsCopy.get(ind);
        WeightEntry weightEntry = weights.get(entry.getClient().getAddr());
        weightEntry.decWeightCounter();
        return entry;
    }
}
Also used : HashMap(java.util.HashMap) InetSocketAddress(java.net.InetSocketAddress) ClientConnectionsEntry(org.redisson.connection.ClientConnectionsEntry) HashSet(java.util.HashSet)

Example 7 with ClientConnectionsEntry

use of org.redisson.connection.ClientConnectionsEntry in project redisson by redisson.

the class ConnectionPool method get.

public RFuture<T> get(RedisCommand<?> command) {
    for (int j = entries.size() - 1; j >= 0; j--) {
        final ClientConnectionsEntry entry = getEntry();
        if (!entry.isFreezed() && tryAcquireConnection(entry)) {
            return acquireConnection(command, entry);
        }
    }
    List<InetSocketAddress> failedAttempts = new LinkedList<InetSocketAddress>();
    List<InetSocketAddress> freezed = new LinkedList<InetSocketAddress>();
    for (ClientConnectionsEntry entry : entries) {
        if (entry.isFreezed()) {
            freezed.add(entry.getClient().getAddr());
        } else {
            failedAttempts.add(entry.getClient().getAddr());
        }
    }
    StringBuilder errorMsg = new StringBuilder(getClass().getSimpleName() + " no available Redis entries. ");
    if (!freezed.isEmpty()) {
        errorMsg.append(" Disconnected hosts: " + freezed);
    }
    if (!failedAttempts.isEmpty()) {
        errorMsg.append(" Hosts disconnected due to `failedAttempts` limit reached: " + failedAttempts);
    }
    RedisConnectionException exception = new RedisConnectionException(errorMsg.toString());
    return connectionManager.newFailedFuture(exception);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ClientConnectionsEntry(org.redisson.connection.ClientConnectionsEntry) RedisConnectionException(org.redisson.client.RedisConnectionException) LinkedList(java.util.LinkedList)

Aggregations

ClientConnectionsEntry (org.redisson.connection.ClientConnectionsEntry)7 InetSocketAddress (java.net.InetSocketAddress)4 RedisConnectionException (org.redisson.client.RedisConnectionException)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1