public Future<BoxedUnit> close() { return client.close(); } }
@SuppressWarnings("unchecked") private ClientBuilder enableThriftMux(ClientBuilder builder, ClientId clientId) { return builder.stack(ThriftMux.client().withClientId(clientId)); }
public Future<DLSN> write(VALUE data) { return service.apply(data); }
public RRMultiWriter(String[] streams, DistributedLogClient client) { this.streams = streams; this.client = client; scala.collection.immutable.Set<ServiceFactory<VALUE, DLSN>> scalaSet = JavaConversions.asScalaSet(initializeServices(streams, client)).toSet(); this.service = Balancers.heap(new scala.util.Random(System.currentTimeMillis())) .newBalancer( Activity.value(scalaSet), NullStatsReceiver.get(), new NoBrokersAvailableException("No partitions available") ).toService(); }
@Override @SuppressWarnings("unchecked") public ProxyClient build(SocketAddress address) { Service<ThriftClientRequest, byte[]> client = ClientBuilder.safeBuildFactory( clientBuilder .hosts((InetSocketAddress) address) .reportTo(clientStats.getFinagleStatsReceiver(address)) ).toService(); DistributedLogService.ServiceIface service = new DistributedLogService.ServiceToClient(client, new TBinaryProtocol.Factory()); return new ProxyClient(address, client, service); }
@Override public SocketAddress getHost(String key, RoutingContext rContext) throws NoBrokersAvailableException { if (rContext.isTriedHost(address)) { throw new NoBrokersAvailableException("No hosts is available : routing context = " + rContext); } return address; }
Throwable showRootCause(Optional<StreamOp> op, Throwable cause) { if (cause instanceof Failure) { Failure failure = (Failure) cause; if (failure.isFlagged(Failure.Wrapped())) { try { // if it is a wrapped failure, unwrap it first cause = failure.show(); } catch (IllegalArgumentException iae) { if (op.isPresent()) { logger.warn("Failed to unwrap finagle failure of stream {} : ", op.get().stream, iae); } else { logger.warn("Failed to unwrap finagle failure : ", iae); } } } } return cause; }
void handleFinagleFailure(Failure failure, Optional<StreamOp> op, SocketAddress addr) { if (failure.isFlagged(Failure.Restartable())) { if (op.isPresent()) { // redirect the request to other host doSend(op.get(), addr); } } else { // fail the request if it is other types of failures handleException(failure, op, addr); } }
public NameServerSet(String nameStr) { Name name; try { name = Resolver$.MODULE$.eval(nameStr); } catch (Exception exc) { logger.error("Exception in Resolver.eval for name {}", nameStr, exc); // Since this is called from various places that dont handle specific exceptions, // we have no option than to throw a runtime exception to halt the control flow // This should only happen in case of incorrect configuration. Having a log message // would help identify the problem during tests throw new RuntimeException(exc); } initialize(name); }
@Override void fail(SocketAddress address, Throwable t) { // StreamOp.fail is called to fail the overall request. In case of BulkWriteOp we take the request level // exception to apply to the first write. In fact for request level exceptions no request has ever been // attempted, but logically we associate the error with the first write. super.fail(address, t); Iterator<Promise<DLSN>> resultIterator = results.iterator(); // Fail the first write with the batch level failure. if (resultIterator.hasNext()) { Promise<DLSN> result = resultIterator.next(); result.setException(t); } // Fail the remaining writes as cancelled requests. while (resultIterator.hasNext()) { Promise<DLSN> result = resultIterator.next(); result.setException(new CancelledRequestException()); } }
public void changeAddrs(List<Address> addresses) { if (null != callback) { LOG.info("Sending a callback {}", addresses); callback.apply(Addrs.newBoundAddr(addresses)); } } }
synchronized String sendNextWrite() { long elapsedMs = stopwatch.elapsed(TimeUnit.MILLISECONDS); if (elapsedMs > requestTimeoutMs || numTriedStreams >= numStreams) { fail(new IndividualRequestTimeoutException(Duration.fromMilliseconds(elapsedMs))); return null; } try { return sendWriteToStream(nextStream); } finally { nextStream = (nextStream + 1) % numStreams; ++numTriedStreams; } }
private List<Address> getAddresses(boolean weightedAddresses) { ArrayList<Address> addresses = new ArrayList<Address>(); addresses.add(Addresses.newInetAddress(new InetSocketAddress("127.0.0.1", 3181))); addresses.add(Addresses.newInetAddress(new InetSocketAddress("127.0.0.2", 3181))); addresses.add(Addresses.newInetAddress(new InetSocketAddress("127.0.0.3", 3181))); addresses.add(Addresses.newInetAddress(new InetSocketAddress("127.0.0.4", 3181))); addresses.add(Addresses.newInetAddress(new InetSocketAddress("127.0.0.5", 3181))); addresses.add(Addresses.newInetAddress(new InetSocketAddress("127.0.0.6", 3181))); addresses.add(Addresses.newInetAddress(new InetSocketAddress("127.0.0.7", 3181))); if (weightedAddresses) { ArrayList<Address> wAddresses = new ArrayList<Address>(); for (Address address: addresses) { wAddresses.add(WeightedAddress.apply(address, 1.0)); } return wAddresses; } else { return addresses; } }
private ServiceInstance endpointAddressToServiceInstance(Address endpointAddress) { if (endpointAddress instanceof Address.Inet) { InetSocketAddress inetSocketAddress = ((Address.Inet) endpointAddress).addr(); Endpoint endpoint = new Endpoint(inetSocketAddress.getHostString(), inetSocketAddress.getPort()); HashMap<String, Endpoint> map = new HashMap<String, Endpoint>(); map.put("thrift", endpoint); return new ServiceInstance( endpoint, map, Status.ALIVE); } else { logger.error("We expect InetSocketAddress while the resolved address {} was {}", endpointAddress, endpointAddress.getClass()); throw new UnsupportedOperationException("invalid endpoint address: " + endpointAddress); } }
private void initialize(Name name) { if (name instanceof TestName) { ((TestName)name).changes(new AbstractFunction1<Addr, BoxedUnit>() { @Override public BoxedUnit apply(Addr varAddr) { return NameServerSet.this.respondToChanges(varAddr); } }); } else if (name instanceof Name.Bound) { ((Name.Bound)name).addr().changes().respond(new AbstractFunction1<Addr, BoxedUnit>() { @Override public BoxedUnit apply(Addr varAddr) { return NameServerSet.this.respondToChanges(varAddr); } }); } else { logger.error("NameServerSet only supports Name.Bound. While the resolved name {} was {}", name, name.getClass()); throw new UnsupportedOperationException("NameServerSet only supports Name.Bound"); } }
@Override public Future<Rep> apply(Req req, Service<Req, Rep> service) { Future<Rep> result = null; outstandingAsync.inc(); final Stopwatch stopwatch = Stopwatch.createStarted(); try { result = service.apply(req); serviceExec.registerSuccessfulEvent(stopwatch.stop().elapsed(TimeUnit.MICROSECONDS)); } finally { outstandingAsync.dec(); if (null == result) { serviceExec.registerFailedEvent(stopwatch.stop().elapsed(TimeUnit.MICROSECONDS)); } } return result; }
@Override public SocketAddress getHost(String key, RoutingContext rContext) throws NoBrokersAvailableException { SocketAddress host = circle.get(key, rContext); if (null != host) { return host; } throw new NoBrokersAvailableException("No host found for " + key + ", routing context : " + rContext); }
@Override synchronized public void run(Timeout timeout) throws Exception { if (!timeout.isCancelled() && null != nextAddressToSend) { doSend(nextAddressToSend); } else { fail(null, new CancelledRequestException()); } } }
throw new NoBrokersAvailableException("No host is available.");
@Override public SocketAddress getHost(String key, RoutingContext routingContext) throws NoBrokersAvailableException { for (RoutingService service : routingServices) { try { SocketAddress addr = service.getHost(key, routingContext); if (routingContext.hasUnavailableRegions()) { // current region is unavailable String region = regionResolver.resolveRegion(addr); if (routingContext.isUnavailableRegion(region)) { continue; } } if (!routingContext.isTriedHost(addr)) { return addr; } } catch (NoBrokersAvailableException nbae) { // if there isn't broker available in current service, try next service. logger.debug("No brokers available in region {} : ", service, nbae); } } throw new NoBrokersAvailableException("No host found for " + key + ", routing context : " + routingContext); }