Example usage for java.net SocketAddress equals

List of usage examples for java.net SocketAddress equals

Introduction

In this page you can find the example usage for java.net SocketAddress equals.

Prototype

public boolean equals(Object obj) 

Source Link

Document

Indicates whether some other object is "equal to" this one.

Usage

From source file:com.twitter.distributedlog.client.routing.ConsistentHashRoutingService.java

private void removeHostInternal(SocketAddress host, Optional<Throwable> reason) {
    synchronized (shardId2Address) {
        Integer shardId = address2ShardId.remove(host);
        if (null != shardId) {
            SocketAddress curHost = shardId2Address.get(shardId);
            if (null != curHost && curHost.equals(host)) {
                shardId2Address.remove(shardId);
            }//from w  w  w  .  j a  va 2s  .c o  m
            circle.remove(shardId, host);
            if (reason.isPresent()) {
                if (reason.get() instanceof ChannelException) {
                    logger.info(
                            "Shard {} ({}) left due to ChannelException, black it out for {} seconds (message = {})",
                            new Object[] { shardId, host, blackoutSeconds, reason.get().toString() });
                    BlackoutHost blackoutHost = new BlackoutHost(shardId, host);
                    hashedWheelTimer.newTimeout(blackoutHost, blackoutSeconds, TimeUnit.SECONDS);
                } else {
                    logger.info("Shard {} ({}) left due to exception {}",
                            new Object[] { shardId, host, reason.get().toString() });
                }
            } else {
                logger.info("Shard {} ({}) left after server set change", shardId, host);
            }
        } else if (reason.isPresent()) {
            logger.info("Node {} left due to exception {}", host, reason.get().toString());
        } else {
            logger.info("Node {} left after server set change", host);
        }
    }
}

From source file:com.twitter.distributedlog.client.routing.ConsistentHashRoutingService.java

@Override
protected synchronized void performServerSetChange(ImmutableSet<DLSocketAddress> serviceInstances) {
    Set<SocketAddress> joinedList = new HashSet<SocketAddress>();
    Set<SocketAddress> removedList = new HashSet<SocketAddress>();

    Map<Integer, SocketAddress> newMap = new HashMap<Integer, SocketAddress>();
    synchronized (shardId2Address) {
        for (DLSocketAddress serviceInstance : serviceInstances) {
            if (serviceInstance.getShard() >= 0) {
                newMap.put(serviceInstance.getShard(), serviceInstance.getSocketAddress());
            } else {
                Integer shard = address2ShardId.get(serviceInstance.getSocketAddress());
                if (null == shard) {
                    // Assign a random negative shardId
                    int shardId;
                    do {
                        shardId = Math.min(-1, (int) (Math.random() * Integer.MIN_VALUE));
                    } while (null != shardId2Address.get(shardId));
                    shard = shardId;/*  w  w w.  ja v a2  s. c o m*/
                }
                newMap.put(shard, serviceInstance.getSocketAddress());
            }
        }
    }

    Map<Integer, SocketAddress> left;
    synchronized (shardId2Address) {
        MapDifference<Integer, SocketAddress> difference = Maps.difference(shardId2Address, newMap);
        left = difference.entriesOnlyOnLeft();
        for (Integer shard : left.keySet()) {
            if (shard >= 0) {
                SocketAddress host = shardId2Address.get(shard);
                if (null != host) {
                    // we don't remove those hosts that just disappered on serverset proactively,
                    // since it might be just because serverset become flaky
                    // address2ShardId.remove(host);
                    // circle.remove(shard, host);
                    logger.info("Shard {} ({}) left temporarily.", shard, host);
                }
            } else {
                // shard id is negative - they are resolved from finagle name, which instances don't have shard id
                // in this case, if they are removed from serverset, we removed them directly
                SocketAddress host = left.get(shard);
                if (null != host) {
                    removeHostInternal(host, Optional.<Throwable>absent());
                    removedList.add(host);
                }
            }
        }
        // we need to find if any shards are replacing old shards
        for (Integer shard : newMap.keySet()) {
            SocketAddress oldHost = shardId2Address.get(shard);
            SocketAddress newHost = newMap.get(shard);
            if (!newHost.equals(oldHost)) {
                join(shard, newHost, removedList);
                joinedList.add(newHost);
            }
        }
    }

    for (SocketAddress addr : removedList) {
        for (RoutingListener listener : listeners) {
            listener.onServerLeft(addr);
        }
    }

    for (SocketAddress addr : joinedList) {
        for (RoutingListener listener : listeners) {
            listener.onServerJoin(addr);
        }
    }
}

From source file:de.javakaffee.web.msm.integration.MemcachedFailoverIntegrationTest.java

public void waitForReconnect(final MemcachedClient client, final InetSocketAddress serverAddressToCheck,
        final long timeToWait) throws InterruptedException, RuntimeException {
    final long start = System.currentTimeMillis();
    while (System.currentTimeMillis() < start + timeToWait) {
        for (final SocketAddress address : client.getAvailableServers()) {
            if (address.equals(serverAddressToCheck)) {
                return;
            }//w  w w  . j  a  va2 s .  c o m
        }
        Thread.sleep(100);
    }
    throw new RuntimeException("MemcachedClient did not reconnect after " + timeToWait + " millis.");
}

From source file:com.twitter.distributedlog.service.balancer.ClusterBalancer.java

public void balance(int rebalanceWaterMark, double rebalanceTolerancePercentage, int rebalanceConcurrency,
        Optional<String> source, Optional<RateLimiter> rebalanceRateLimiter) {
    Map<SocketAddress, Set<String>> distribution = monitor.getStreamOwnershipDistribution();
    if (distribution.size() <= 1) {
        return;/*from   w  w  w .ja  va 2 s  . co  m*/
    }
    SocketAddress sourceAddr = null;
    if (source.isPresent()) {
        sourceAddr = DLSocketAddress.parseSocketAddress(source.get());
        logger.info("Balancer source is {}", sourceAddr);
        if (!distribution.containsKey(sourceAddr)) {
            return;
        }
    }
    // Get the list of hosts ordered by number of streams in DESC order
    List<Host> hosts = new ArrayList<Host>(distribution.size());
    for (Map.Entry<SocketAddress, Set<String>> entry : distribution.entrySet()) {
        Host host = new Host(entry.getKey(), entry.getValue(), clientBuilder);
        hosts.add(host);
    }
    Collections.sort(hosts, new HostComparator());
    try {

        // find the host to move streams from.
        int hostIdxMoveFrom = -1;
        if (null != sourceAddr) {
            for (Host host : hosts) {
                ++hostIdxMoveFrom;
                if (sourceAddr.equals(host.address)) {
                    break;
                }
            }
        }

        // compute the average load.
        int totalStream = 0;
        for (Host host : hosts) {
            totalStream += host.streams.size();
        }
        double averageLoad;
        if (hostIdxMoveFrom >= 0) {
            averageLoad = ((double) totalStream / (hosts.size() - 1));
        } else {
            averageLoad = ((double) totalStream / hosts.size());
        }

        int moveFromLowWaterMark;
        int moveToHighWaterMark = Math.max(1,
                (int) (averageLoad + averageLoad * rebalanceTolerancePercentage / 100.0f));

        if (hostIdxMoveFrom >= 0) {
            moveFromLowWaterMark = Math.max(0, rebalanceWaterMark);
            moveStreams(hosts, new AtomicInteger(hostIdxMoveFrom), moveFromLowWaterMark,
                    new AtomicInteger(hosts.size() - 1), moveToHighWaterMark, rebalanceRateLimiter);
            moveRemainingStreamsFromSource(hosts.get(hostIdxMoveFrom), hosts, rebalanceRateLimiter);
        } else {
            moveFromLowWaterMark = Math.max((int) Math.ceil(averageLoad), rebalanceWaterMark);
            AtomicInteger moveFrom = new AtomicInteger(0);
            AtomicInteger moveTo = new AtomicInteger(hosts.size() - 1);
            while (moveFrom.get() < moveTo.get()) {
                moveStreams(hosts, moveFrom, moveFromLowWaterMark, moveTo, moveToHighWaterMark,
                        rebalanceRateLimiter);
                moveFrom.incrementAndGet();
            }
        }
    } finally {
        for (Host host : hosts) {
            host.close();
        }
    }
}

From source file:org.apache.distributedlog.client.routing.ConsistentHashRoutingService.java

private void removeHostInternal(SocketAddress host, Optional<Throwable> reason) {
    synchronized (shardId2Address) {
        Integer shardId = address2ShardId.remove(host);
        if (null != shardId) {
            SocketAddress curHost = shardId2Address.get(shardId);
            if (null != curHost && curHost.equals(host)) {
                shardId2Address.remove(shardId);
            }//from  w w  w. j  a  va2s . c  o m
            circle.remove(shardId, host);
            if (reason.isPresent()) {
                if (reason.get() instanceof ChannelException) {
                    logger.info(
                            "Shard {} ({}) left due to ChannelException, black it out for {} seconds"
                                    + " (message = {})",
                            new Object[] { shardId, host, blackoutSeconds, reason.get().toString() });
                    BlackoutHost blackoutHost = new BlackoutHost(shardId, host);
                    hashedWheelTimer.newTimeout(blackoutHost, blackoutSeconds, TimeUnit.SECONDS);
                } else {
                    logger.info("Shard {} ({}) left due to exception {}",
                            new Object[] { shardId, host, reason.get().toString() });
                }
            } else {
                logger.info("Shard {} ({}) left after server set change", shardId, host);
            }
        } else if (reason.isPresent()) {
            logger.info("Node {} left due to exception {}", host, reason.get().toString());
        } else {
            logger.info("Node {} left after server set change", host);
        }
    }
}

From source file:org.apache.distributedlog.client.routing.ConsistentHashRoutingService.java

@Override
protected synchronized void performServerSetChange(ImmutableSet<DLSocketAddress> serviceInstances) {
    Set<SocketAddress> joinedList = new HashSet<SocketAddress>();
    Set<SocketAddress> removedList = new HashSet<SocketAddress>();

    Map<Integer, SocketAddress> newMap = new HashMap<Integer, SocketAddress>();
    synchronized (shardId2Address) {
        for (DLSocketAddress serviceInstance : serviceInstances) {
            if (serviceInstance.getShard() >= 0) {
                newMap.put(serviceInstance.getShard(), serviceInstance.getSocketAddress());
            } else {
                Integer shard = address2ShardId.get(serviceInstance.getSocketAddress());
                if (null == shard) {
                    // Assign a random negative shardId
                    int shardId;
                    do {
                        shardId = Math.min(-1, (int) (Math.random() * Integer.MIN_VALUE));
                    } while (null != shardId2Address.get(shardId));
                    shard = shardId;//from  w  w w .  j av  a 2 s  .  co  m
                }
                newMap.put(shard, serviceInstance.getSocketAddress());
            }
        }
    }

    Map<Integer, SocketAddress> left;
    synchronized (shardId2Address) {
        MapDifference<Integer, SocketAddress> difference = Maps.difference(shardId2Address, newMap);
        left = difference.entriesOnlyOnLeft();
        for (Map.Entry<Integer, SocketAddress> shardEntry : left.entrySet()) {
            int shard = shardEntry.getKey();
            if (shard >= 0) {
                SocketAddress host = shardId2Address.get(shard);
                if (null != host) {
                    // we don't remove those hosts that just disappered on serverset proactively,
                    // since it might be just because serverset become flaky
                    // address2ShardId.remove(host);
                    // circle.remove(shard, host);
                    logger.info("Shard {} ({}) left temporarily.", shard, host);
                }
            } else {
                // shard id is negative - they are resolved from finagle name, which instances don't have shard id
                // in this case, if they are removed from serverset, we removed them directly
                SocketAddress host = shardEntry.getValue();
                if (null != host) {
                    removeHostInternal(host, Optional.<Throwable>absent());
                    removedList.add(host);
                }
            }
        }
        // we need to find if any shards are replacing old shards
        for (Map.Entry<Integer, SocketAddress> shard : newMap.entrySet()) {
            SocketAddress oldHost = shardId2Address.get(shard.getKey());
            SocketAddress newHost = shard.getValue();
            if (!newHost.equals(oldHost)) {
                join(shard.getKey(), newHost, removedList);
                joinedList.add(newHost);
            }
        }
    }

    for (SocketAddress addr : removedList) {
        for (RoutingListener listener : listeners) {
            listener.onServerLeft(addr);
        }
    }

    for (SocketAddress addr : joinedList) {
        for (RoutingListener listener : listeners) {
            listener.onServerJoin(addr);
        }
    }
}