Search in sources :

Example 56 with FutureCallback

use of com.linkedin.common.callback.FutureCallback in project rest.li by linkedin.

the class ConfigWriter method writeConfig.

public void writeConfig() throws ExecutionException, TimeoutException, InterruptedException {
    long startTime = System.currentTimeMillis();
    FutureCallback<None> callback = new FutureCallback<None>();
    _store.start(callback);
    callback.get(_timeout, _timeoutUnit);
    final Semaphore outstandingPutSemaphore = new Semaphore(_maxOutstandingWrites);
    for (final String key : _source.keySet()) {
        Map<String, Object> map = merge(_source.get(key), _defaultMap);
        T properties = _builder.fromMap(map);
        Callback<None> putCallback = new Callback<None>() {

            @Override
            public void onSuccess(None none) {
                outstandingPutSemaphore.release();
            }

            @Override
            public void onError(Throwable e) {
                _log.error("Put failed for {}", key, e);
                outstandingPutSemaphore.release();
            }
        };
        if (!outstandingPutSemaphore.tryAcquire(_timeout, _timeoutUnit)) {
            _log.error("Put timed out for {}", key);
            throw new TimeoutException();
        }
        _store.put(key, properties, putCallback);
    }
    // Wait until all puts are finished.
    if (!outstandingPutSemaphore.tryAcquire(_maxOutstandingWrites, _timeout, _timeoutUnit)) {
        _log.error("Put timed out with {} outstanding writes", _maxOutstandingWrites - outstandingPutSemaphore.availablePermits());
        throw new TimeoutException();
    }
    FutureCallback<None> shutdownCallback = new FutureCallback<None>();
    _store.shutdown(shutdownCallback);
    shutdownCallback.get(_timeout, _timeoutUnit);
    long elapsedTime = System.currentTimeMillis() - startTime;
    _log.info("A total of {}.{}s elapsed to write configs to store.", elapsedTime / 1000, elapsedTime % 1000);
}
Also used : Semaphore(java.util.concurrent.Semaphore) FutureCallback(com.linkedin.common.callback.FutureCallback) Callback(com.linkedin.common.callback.Callback) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) TimeoutException(java.util.concurrent.TimeoutException)

Example 57 with FutureCallback

use of com.linkedin.common.callback.FutureCallback in project rest.li by linkedin.

the class TestRouteLookupClient method testRouteLookupClientCallback.

@Test
public void testRouteLookupClientCallback() throws InterruptedException, ExecutionException, TimeoutException {
    RouteLookup routeLookup = new SimpleTestRouteLookup();
    final D2Client d2Client = new D2ClientBuilder().setZkHosts("localhost:2121").build();
    d2Client.start(new FutureCallback<None>());
    RouteLookupClient routeLookupClient = new RouteLookupClient(d2Client, routeLookup, "WestCoast");
    RestRequest dummyRestRequest = new RestRequestBuilder(URI.create("d2://simple_uri")).build();
    FutureCallback<RestResponse> futureCallback = new FutureCallback<RestResponse>();
    routeLookupClient.restRequest(dummyRestRequest, futureCallback, "5555");
    try {
        RestResponse response = futureCallback.get(10, TimeUnit.SECONDS);
        Assert.fail("Unexpected success, request should have thrown a ServiceUnavailableException");
    } catch (Exception e) {
        String message = e.getMessage();
        if (!message.contains("_serviceName=simple_uriWestCoast5555Foo")) {
            Assert.fail("request was not rewritten to point at the d2 service simple_uriWestCoast5555Foo");
        }
    }
}
Also used : D2Client(com.linkedin.d2.balancer.D2Client) RestResponse(com.linkedin.r2.message.rest.RestResponse) D2ClientBuilder(com.linkedin.d2.balancer.D2ClientBuilder) TimeoutException(java.util.concurrent.TimeoutException) ExecutionException(java.util.concurrent.ExecutionException) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 58 with FutureCallback

use of com.linkedin.common.callback.FutureCallback in project rest.li by linkedin.

the class RouteLookupClient method restRequest.

@Override
public Future<RestResponse> restRequest(final RestRequest request, final RequestContext requestContext, String routekey) {
    final FutureCallback<String> futureCallback = new FutureCallback<String>();
    String originalServiceName = LoadBalancerUtil.getServiceNameFromUri(request.getURI());
    String resultServiceName;
    _routeLookup.run(originalServiceName, _routingGroup, routekey, futureCallback);
    try {
        resultServiceName = futureCallback.get();
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    } catch (ExecutionException e) {
        throw new RuntimeException(e);
    }
    RestRequest resultRequest = createNewRequestWithNewServiceName(request, resultServiceName);
    Future<RestResponse> resultFuture = _client.restRequest(resultRequest, requestContext);
    return resultFuture;
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponse(com.linkedin.r2.message.rest.RestResponse) ExecutionException(java.util.concurrent.ExecutionException) FutureCallback(com.linkedin.common.callback.FutureCallback)

Example 59 with FutureCallback

use of com.linkedin.common.callback.FutureCallback in project rest.li by linkedin.

the class ZooKeeperAnnouncerJmx method markDown.

@Override
public void markDown() throws PropertyStoreException {
    FutureCallback<None> callback = new FutureCallback<None>();
    _announcer.markDown(callback);
    try {
        callback.get(10, TimeUnit.SECONDS);
    } catch (Exception e) {
        throw new PropertyStoreException(e);
    }
}
Also used : PropertyStoreException(com.linkedin.d2.discovery.stores.PropertyStoreException) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) IOException(java.io.IOException) PropertyStoreException(com.linkedin.d2.discovery.stores.PropertyStoreException)

Example 60 with FutureCallback

use of com.linkedin.common.callback.FutureCallback in project rest.li by linkedin.

the class ZooKeeperAnnouncerJmx method reset.

@Override
public void reset() throws PropertyStoreException {
    FutureCallback<None> callback = new FutureCallback<None>();
    _announcer.reset(callback);
    try {
        callback.get(10, TimeUnit.SECONDS);
    } catch (Exception e) {
        throw new PropertyStoreException(e);
    }
}
Also used : PropertyStoreException(com.linkedin.d2.discovery.stores.PropertyStoreException) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) IOException(java.io.IOException) PropertyStoreException(com.linkedin.d2.discovery.stores.PropertyStoreException)

Aggregations

FutureCallback (com.linkedin.common.callback.FutureCallback)152 Test (org.testng.annotations.Test)116 None (com.linkedin.common.util.None)77 RestRequest (com.linkedin.r2.message.rest.RestRequest)50 ExecutionException (java.util.concurrent.ExecutionException)50 RequestContext (com.linkedin.r2.message.RequestContext)47 RestResponse (com.linkedin.r2.message.rest.RestResponse)43 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)36 HashMap (java.util.HashMap)26 CountDownLatch (java.util.concurrent.CountDownLatch)25 TransportCallbackAdapter (com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter)22 ByteString (com.linkedin.data.ByteString)21 RemoteInvocationException (com.linkedin.r2.RemoteInvocationException)21 URI (java.net.URI)21 TimeoutException (java.util.concurrent.TimeoutException)21 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)20 UriProperties (com.linkedin.d2.balancer.properties.UriProperties)16 ArrayList (java.util.ArrayList)16 RestException (com.linkedin.r2.message.rest.RestException)15 AsyncSharedPoolImpl (com.linkedin.r2.transport.http.client.AsyncSharedPoolImpl)14