use of com.twitter.finagle.NoBrokersAvailableException in project distributedlog by twitter.
the class LocalRoutingService method getHost.
@Override
public synchronized SocketAddress getHost(String key, RoutingContext rContext) throws NoBrokersAvailableException {
LinkedHashSet<SocketAddress> addresses = localAddresses.get(key);
SocketAddress candidate = null;
if (null != addresses) {
for (SocketAddress host : addresses) {
if (rContext.isTriedHost(host) && !allowRetrySameHost) {
continue;
} else {
candidate = host;
break;
}
}
}
if (null != candidate) {
return candidate;
}
throw new NoBrokersAvailableException("No host available");
}
use of com.twitter.finagle.NoBrokersAvailableException in project distributedlog by twitter.
the class DistributedLogClientImpl method doSend.
/**
* Send the stream operation by routing service, excluding previous address if it is not null.
*
* @param op
* stream operation.
* @param previousAddr
* previous tried address.
*/
private void doSend(final StreamOp op, final SocketAddress previousAddr) {
if (null != previousAddr) {
op.routingContext.addTriedHost(previousAddr, StatusCode.WRITE_EXCEPTION);
}
// Get host first
SocketAddress address = ownershipCache.getOwner(op.stream);
if (null == address || op.routingContext.isTriedHost(address)) {
// pickup host by hashing
try {
address = routingService.getHost(op.stream, op.routingContext);
} catch (NoBrokersAvailableException nbae) {
op.fail(null, nbae);
return;
}
}
op.send(address);
}
use of com.twitter.finagle.NoBrokersAvailableException in project distributedlog by twitter.
the class TestConsistentHashRoutingService method testBlackoutHost.
@Test(timeout = 60000)
public void testBlackoutHost() throws Exception {
TestName name = new TestName();
RoutingService routingService = ConsistentHashRoutingService.newBuilder().serverSet(new NameServerSet(name)).resolveFromName(true).numReplicas(997).blackoutSeconds(2).build();
InetSocketAddress inetAddress = new InetSocketAddress("127.0.0.1", 3181);
Address address = Addresses.newInetAddress(inetAddress);
List<Address> addresses = new ArrayList<Address>(1);
addresses.add(address);
name.changeAddrs(addresses);
routingService.startService();
RoutingService.RoutingContext routingContext = RoutingService.RoutingContext.of(new DefaultRegionResolver());
String streamName = "test-blackout-host";
assertEquals(inetAddress, routingService.getHost(streamName, routingContext));
routingService.removeHost(inetAddress, new ChannelWriteException(new IOException("test exception")));
try {
routingService.getHost(streamName, routingContext);
fail("Should fail to get host since no brokers are available");
} catch (NoBrokersAvailableException nbae) {
// expected
}
TimeUnit.SECONDS.sleep(3);
assertEquals(inetAddress, routingService.getHost(streamName, routingContext));
routingService.stopService();
}
use of com.twitter.finagle.NoBrokersAvailableException in project distributedlog by twitter.
the class TestRegionsRoutingService method testGetHost.
@Test(timeout = 60000)
public void testGetHost() throws Exception {
int numRoutingServices = 3;
RoutingService.Builder[] routingServiceBuilders = new RoutingService.Builder[numRoutingServices];
Map<SocketAddress, String> regionMap = new HashMap<SocketAddress, String>();
for (int i = 0; i < numRoutingServices; i++) {
String finagleNameStr = "inet!127.0.0.1:" + (3181 + i);
routingServiceBuilders[i] = RoutingUtils.buildRoutingService(finagleNameStr);
SocketAddress address = new InetSocketAddress("127.0.0.1", 3181 + i);
regionMap.put(address, "region-" + i);
}
RegionsRoutingService regionsRoutingService = RegionsRoutingService.newBuilder().resolver(new DefaultRegionResolver(regionMap)).routingServiceBuilders(routingServiceBuilders).build();
regionsRoutingService.startService();
RoutingService.RoutingContext routingContext = RoutingService.RoutingContext.of(new DefaultRegionResolver()).addTriedHost(new InetSocketAddress("127.0.0.1", 3183), StatusCode.WRITE_EXCEPTION);
assertEquals(new InetSocketAddress("127.0.0.1", 3181), regionsRoutingService.getHost("any", routingContext));
routingContext = RoutingService.RoutingContext.of(new DefaultRegionResolver()).addTriedHost(new InetSocketAddress("127.0.0.1", 3181), StatusCode.WRITE_EXCEPTION);
assertEquals(new InetSocketAddress("127.0.0.1", 3182), regionsRoutingService.getHost("any", routingContext));
// add 3182 to routing context as tried host
routingContext.addTriedHost(new InetSocketAddress("127.0.0.1", 3182), StatusCode.WRITE_EXCEPTION);
assertEquals(new InetSocketAddress("127.0.0.1", 3183), regionsRoutingService.getHost("any", routingContext));
// add 3183 to routing context as tried host
routingContext.addTriedHost(new InetSocketAddress("127.0.0.1", 3183), StatusCode.WRITE_EXCEPTION);
try {
regionsRoutingService.getHost("any", routingContext);
fail("Should fail to get host since all regions are tried.");
} catch (NoBrokersAvailableException nbae) {
// expected
}
}
use of com.twitter.finagle.NoBrokersAvailableException in project distributedlog by twitter.
the class ServerSetRoutingService method getHost.
@Override
public SocketAddress getHost(String key, RoutingContext rContext) throws NoBrokersAvailableException {
SocketAddress address = null;
synchronized (hostSet) {
if (0 != hostList.size()) {
int hashCode = hasher.hashUnencodedChars(key).asInt();
int hostId = signSafeMod(hashCode, hostList.size());
address = hostList.get(hostId);
if (rContext.isTriedHost(address)) {
ArrayList<SocketAddress> newList = new ArrayList<SocketAddress>(hostList);
newList.remove(hostId);
// pickup a new host by rehashing it.
hostId = signSafeMod(hashCode, newList.size());
address = newList.get(hostId);
int i = hostId;
while (rContext.isTriedHost(address)) {
i = (i + 1) % newList.size();
if (i == hostId) {
address = null;
break;
}
address = newList.get(i);
}
}
}
}
if (null == address) {
throw new NoBrokersAvailableException("No host is available.");
}
return address;
}
Aggregations