Search in sources :

Example 1 with MultiCallback

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

the class LastSeenLoadBalancerWithFacilities method start.

// #################### lifecycle ####################
@Override
public void start(final Callback<None> callback) {
    try {
        _zkPersistentConnection.start();
    } catch (IOException e) {
        LOG.error("Error in starting connection while starting load balancer. The connection be already started. " + "The LoadBalancer will continue booting up", e);
    }
    MultiCallback multiCallback = new MultiCallback(callback, 4);
    _lsClusterStore.start(multiCallback);
    _lsServiceStore.start(multiCallback);
    _lsUrisStore.start(multiCallback);
    _loadBalancer.start(multiCallback);
}
Also used : MultiCallback(com.linkedin.common.callback.MultiCallback) IOException(java.io.IOException)

Example 2 with MultiCallback

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

the class LastSeenZKStore method shutdown.

@Override
public void shutdown(Callback<None> shutdown) {
    MultiCallback multiCallback = new MultiCallback(shutdown, 2);
    _fsStore.shutdown(multiCallback);
    _zkAwareStore.shutdown(multiCallback);
}
Also used : MultiCallback(com.linkedin.common.callback.MultiCallback)

Example 3 with MultiCallback

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

the class ZookeeperConnectionManagerTest method testMarkUpDuringSessionExpirationManyCallbacks.

@Test(invocationCount = 10, timeOut = 10000, retryAnalyzer = ThreeRetries.class)
public void testMarkUpDuringSessionExpirationManyCallbacks() throws Exception {
    ZooKeeperAnnouncer announcer = getZooKeeperAnnouncer(_cluster, _uri, WEIGHT);
    ZKPersistentConnection zkPersistentConnection = getZkPersistentConnection();
    ZooKeeperConnectionManager manager = createManager(true, zkPersistentConnection, announcer);
    // set up many concurrent callbacks
    FutureCallback<None> allMarkupsSucceed = new FutureCallback<>();
    int count = 1000;
    Callback<None> markUpAllServersCallback = new MultiCallback(allMarkupsSucceed, 2 * count);
    ExecutorService executorService = Executors.newScheduledThreadPool(100);
    for (int i = 0; i < count; i++) {
        executorService.execute(() -> {
            manager.markDownAllServers(new IgnoreCancelledCallback(markUpAllServersCallback));
            manager.markUpAllServers(new IgnoreCancelledCallback(markUpAllServersCallback));
        });
    }
    // expiring the connection
    long oldSessionId = zkPersistentConnection.getZooKeeper().getSessionId();
    ZKTestUtil.expireSession("localhost:" + PORT, zkPersistentConnection.getZooKeeper(), 10, TimeUnit.SECONDS);
    ZKTestUtil.waitForNewSessionEstablished(oldSessionId, zkPersistentConnection, 10, TimeUnit.SECONDS);
    try {
        allMarkupsSucceed.get(1, TimeUnit.MILLISECONDS);
        Assert.fail("All the callbacks were resolved before expiring the connection, which means it won't test that callbacks are invoked even after session expiration");
    } catch (Throwable e) {
    // expected
    }
    allMarkupsSucceed.get();
    // making sure that a new connection has been established. There should be no need to wait, because at least one markup should have been run on
    // the new connection, which means that by this part of code it should already have been established
    ZKTestUtil.waitForNewSessionEstablished(oldSessionId, zkPersistentConnection, 0, TimeUnit.SECONDS);
    // data validation
    dataValidation(_uri, _cluster, WEIGHT);
    shutdownManager(manager);
    executorService.shutdown();
}
Also used : ZKPersistentConnection(com.linkedin.d2.discovery.stores.zk.ZKPersistentConnection) ExecutorService(java.util.concurrent.ExecutorService) MultiCallback(com.linkedin.common.callback.MultiCallback) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 4 with MultiCallback

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

the class ZookeeperConnectionManagerTest method testMarkUpAndDownMultipleTimesFinalUp.

@Test(invocationCount = 10, timeOut = 10000, groups = { "ci-flaky" })
public void testMarkUpAndDownMultipleTimesFinalUp() throws Exception {
    ZooKeeperAnnouncer announcer = getZooKeeperAnnouncer(_cluster, _uri, WEIGHT);
    ZooKeeperConnectionManager manager = createManager(true, announcer);
    FutureCallback<None> managerStartCallback = new FutureCallback<>();
    manager.start(managerStartCallback);
    managerStartCallback.get(10, TimeUnit.SECONDS);
    // set up many concurrent callbacks
    FutureCallback<None> allMarkupsDownsSucceed = new FutureCallback<>();
    int count = 1000;
    Callback<None> markUpAllServersCallback = new MultiCallback(allMarkupsDownsSucceed, count * 2);
    ExecutorService executorService = Executors.newScheduledThreadPool(100);
    for (int i = 0; i < count; i++) {
        executorService.execute(() -> {
            manager.markDownAllServers(new IgnoreCancelledCallback(markUpAllServersCallback));
            manager.markUpAllServers(new IgnoreCancelledCallback(markUpAllServersCallback));
        });
    }
    allMarkupsDownsSucceed.get();
    // data validation
    dataValidation(_uri, _cluster, WEIGHT);
    shutdownManager(manager);
    executorService.shutdown();
}
Also used : ExecutorService(java.util.concurrent.ExecutorService) MultiCallback(com.linkedin.common.callback.MultiCallback) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 5 with MultiCallback

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

the class HttpNettyClient method shutdown.

@Override
public void shutdown(Callback<None> callback) {
    LOG.info("Shutdown requested");
    if (_state.compareAndSet(NettyClientState.RUNNING, NettyClientState.SHUTTING_DOWN)) {
        LOG.info("Shutting down");
        MultiCallback poolShutdown = new MultiCallback(new Callback<None>() {

            private void releaseCallbacks() {
                _userCallbacks.forEach(transportCallback -> transportCallback.onResponse(TransportResponseImpl.error(new TimeoutException("Operation did not complete before shutdown"))));
            }

            @Override
            public void onError(Throwable e) {
                releaseCallbacks();
                callback.onError(e);
            }

            @Override
            public void onSuccess(None result) {
                releaseCallbacks();
                callback.onSuccess(result);
            }
        }, 2);
        _channelPoolManager.shutdown(poolShutdown, () -> _state.set(NettyClientState.REQUESTS_STOPPING), () -> _state.set(NettyClientState.SHUTDOWN), _shutdownTimeout);
        _sslChannelPoolManager.shutdown(poolShutdown, () -> _state.set(NettyClientState.REQUESTS_STOPPING), () -> _state.set(NettyClientState.SHUTDOWN), _shutdownTimeout);
    } else {
        callback.onError(new IllegalStateException("Shutdown has already been requested."));
    }
    TimingKey.unregisterKey(TIMING_KEY);
}
Also used : ScheduledFuture(java.util.concurrent.ScheduledFuture) SocketAddress(java.net.SocketAddress) WireAttributeHelper(com.linkedin.r2.transport.common.WireAttributeHelper) LoggerFactory(org.slf4j.LoggerFactory) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) TimeoutException(java.util.concurrent.TimeoutException) NettyClientState(com.linkedin.r2.netty.common.NettyClientState) TransportCallback(com.linkedin.r2.transport.common.bridge.common.TransportCallback) RestResponse(com.linkedin.r2.message.rest.RestResponse) RequestTimeoutUtil(com.linkedin.r2.util.RequestTimeoutUtil) InetAddress(java.net.InetAddress) Request(com.linkedin.r2.message.Request) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) Map(java.util.Map) URI(java.net.URI) Cancellable(com.linkedin.r2.util.Cancellable) Timeout(com.linkedin.r2.util.Timeout) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) StreamingTimeout(com.linkedin.r2.netty.common.StreamingTimeout) TimingKey(com.linkedin.r2.message.timing.TimingKey) ChannelPipeline(io.netty.channel.ChannelPipeline) MultiCallback(com.linkedin.common.callback.MultiCallback) InetSocketAddress(java.net.InetSocketAddress) InvokedOnceTransportCallback(com.linkedin.r2.transport.http.client.InvokedOnceTransportCallback) HttpClientFactory(com.linkedin.r2.transport.http.client.HttpClientFactory) ChannelPoolManager(com.linkedin.r2.transport.http.client.common.ChannelPoolManager) StreamExecutionCallback(com.linkedin.r2.netty.callback.StreamExecutionCallback) R2Constants(com.linkedin.r2.filter.R2Constants) None(com.linkedin.common.util.None) TimingContextUtil(com.linkedin.r2.message.timing.TimingContextUtil) HttpProtocolVersion(com.linkedin.r2.transport.http.common.HttpProtocolVersion) Callback(com.linkedin.common.callback.Callback) TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) ArgumentUtil(com.linkedin.util.ArgumentUtil) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) AtomicReference(java.util.concurrent.atomic.AtomicReference) HttpBridge(com.linkedin.r2.transport.http.common.HttpBridge) ChannelFutureListener(io.netty.channel.ChannelFutureListener) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) NettyChannelAttributes(com.linkedin.r2.netty.common.NettyChannelAttributes) ShutdownTimeoutException(com.linkedin.r2.netty.common.ShutdownTimeoutException) ExecutorService(java.util.concurrent.ExecutorService) MessageType(com.linkedin.r2.transport.common.MessageType) RestRequest(com.linkedin.r2.message.rest.RestRequest) EventLoopGroup(io.netty.channel.EventLoopGroup) Logger(org.slf4j.Logger) Clock(com.linkedin.util.clock.Clock) SslHandshakeTimingHandler(com.linkedin.r2.netty.handler.common.SslHandshakeTimingHandler) SslSessionValidator(com.linkedin.r2.transport.http.client.common.ssl.SslSessionValidator) Messages(com.linkedin.r2.message.Messages) UnknownHostException(java.net.UnknownHostException) Channel(io.netty.channel.Channel) TimeUnit(java.util.concurrent.TimeUnit) HttpScheme(io.netty.handler.codec.http.HttpScheme) TransportResponseImpl(com.linkedin.r2.transport.common.bridge.common.TransportResponseImpl) RequestContext(com.linkedin.r2.message.RequestContext) AsyncPool(com.linkedin.r2.transport.http.client.AsyncPool) ChannelPool(io.netty.channel.pool.ChannelPool) UnknownSchemeException(com.linkedin.r2.netty.common.UnknownSchemeException) TimingImportance(com.linkedin.r2.message.timing.TimingImportance) MultiCallback(com.linkedin.common.callback.MultiCallback) None(com.linkedin.common.util.None) TimeoutException(java.util.concurrent.TimeoutException) ShutdownTimeoutException(com.linkedin.r2.netty.common.ShutdownTimeoutException)

Aggregations

MultiCallback (com.linkedin.common.callback.MultiCallback)11 None (com.linkedin.common.util.None)7 FutureCallback (com.linkedin.common.callback.FutureCallback)4 ExecutorService (java.util.concurrent.ExecutorService)4 Test (org.testng.annotations.Test)4 Callback (com.linkedin.common.callback.Callback)2 Request (com.linkedin.r2.message.Request)2 RequestContext (com.linkedin.r2.message.RequestContext)2 RestRequest (com.linkedin.r2.message.rest.RestRequest)2 RestResponse (com.linkedin.r2.message.rest.RestResponse)2 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)2 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)2 TimingContextUtil (com.linkedin.r2.message.timing.TimingContextUtil)2 TimingImportance (com.linkedin.r2.message.timing.TimingImportance)2 TimingKey (com.linkedin.r2.message.timing.TimingKey)2 NettyClientState (com.linkedin.r2.netty.common.NettyClientState)2 UnknownSchemeException (com.linkedin.r2.netty.common.UnknownSchemeException)2 MessageType (com.linkedin.r2.transport.common.MessageType)2 TransportClient (com.linkedin.r2.transport.common.bridge.client.TransportClient)2 TransportCallback (com.linkedin.r2.transport.common.bridge.common.TransportCallback)2