Search in sources :

Example 1 with ClientRequestExecutor

use of voldemort.store.socket.clientrequest.ClientRequestExecutor in project voldemort by voldemort.

the class SocketStore method request.

/**
 * This method handles submitting and then waiting for the request from the
 * server. It uses the ClientRequest API to actually write the request and
 * then read back the response. This implementation will block for a
 * response from the server.
 *
 * @param <T> Return type
 *
 * @param clientRequest ClientRequest implementation used to write the
 *        request and read the response
 * @param operationName Simple string representing the type of request
 *
 * @return Data returned by the individual requests
 */
private <T> T request(ClientRequest<T> delegate, String operationName) {
    long startTimeMs = -1;
    long startTimeNs = -1;
    if (logger.isDebugEnabled()) {
        startTimeMs = System.currentTimeMillis();
    }
    ClientRequestExecutor clientRequestExecutor = pool.checkout(destination);
    String debugMsgStr = "";
    startTimeNs = System.nanoTime();
    BlockingClientRequest<T> blockingClientRequest = null;
    try {
        blockingClientRequest = new BlockingClientRequest<T>(delegate, timeoutMs);
        clientRequestExecutor.addClientRequest(blockingClientRequest, timeoutMs, System.nanoTime() - startTimeNs);
        boolean awaitResult = blockingClientRequest.await();
        if (awaitResult == false) {
            blockingClientRequest.timeOut();
        }
        if (logger.isDebugEnabled())
            debugMsgStr += "success";
        return blockingClientRequest.getResult();
    } catch (InterruptedException e) {
        if (logger.isDebugEnabled())
            debugMsgStr += "unreachable: " + e.getMessage();
        throw new UnreachableStoreException("Failure in " + operationName + " on " + destination + ": " + e.getMessage(), e);
    } catch (UnreachableStoreException e) {
        clientRequestExecutor.close();
        if (logger.isDebugEnabled())
            debugMsgStr += "failure: " + e.getMessage();
        throw new UnreachableStoreException("Failure in " + operationName + " on " + destination + ": " + e.getMessage(), e.getCause());
    } finally {
        if (blockingClientRequest != null && !blockingClientRequest.isComplete()) {
            // close the executor if we timed out
            clientRequestExecutor.close();
        }
        // Record operation time
        long opTimeNs = Utils.elapsedTimeNs(startTimeNs, System.nanoTime());
        if (stats != null) {
            stats.recordSyncOpTimeNs(destination, opTimeNs);
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Sync request end, type: " + operationName + " requestRef: " + System.identityHashCode(delegate) + " totalTimeNs: " + opTimeNs + " start time: " + startTimeMs + " end time: " + System.currentTimeMillis() + " client:" + clientRequestExecutor.getSocketChannel().socket().getLocalAddress() + ":" + clientRequestExecutor.getSocketChannel().socket().getLocalPort() + " server: " + clientRequestExecutor.getSocketChannel().socket().getRemoteSocketAddress() + " outcome: " + debugMsgStr);
        }
        pool.checkin(destination, clientRequestExecutor);
    }
}
Also used : ClientRequestExecutor(voldemort.store.socket.clientrequest.ClientRequestExecutor) UnreachableStoreException(voldemort.store.UnreachableStoreException)

Example 2 with ClientRequestExecutor

use of voldemort.store.socket.clientrequest.ClientRequestExecutor in project voldemort by voldemort.

the class SimpleSocketPoolTest method testClientRequestExecutorLimitSomeTimeout.

@Test
public void testClientRequestExecutorLimitSomeTimeout() throws Exception {
    // start a dummy server
    AbstractSocketService server = ServerTestUtils.getSocketService(useNio, new ClientRequestHandlerFactory(null), 7666, 50, 50, 1000);
    server.start();
    final ResourcePoolConfig config = new ResourcePoolConfig().setTimeout(50, TimeUnit.MILLISECONDS).setMaxPoolSize(20);
    ClientRequestExecutorPool clientRequestExecutorPool = new ClientRequestExecutorPool(config.getMaxPoolSize(), (int) config.getTimeout(TimeUnit.MILLISECONDS), 100, 1000);
    try {
        ResourceFactory<SocketDestination, ClientRequestExecutor> factory = clientRequestExecutorPool.getFactory();
        final AbstractSocketPoolTest<SocketDestination, ClientRequestExecutor> test = new AbstractSocketPoolTest<SocketDestination, ClientRequestExecutor>() {

            @Override
            protected void doSomethingWithResource(SocketDestination key, ClientRequestExecutor resource) throws Exception {
                Thread.sleep(100);
                int random = (int) (Math.random() * 10);
                if (random >= 5)
                    resource.close();
            }

            @Override
            protected SocketDestination getRequestKey() throws Exception {
                return new SocketDestination("localhost", 7666, RequestFormatType.VOLDEMORT_V1);
            }
        };
        // borrow timeout >> doSomething() no timeout expected
        TestStats testStats = test.startTest(factory, config, 50, 200);
        assertEquals("We should see some timeoutRequests", true, testStats.timeoutRequests > 0);
        server.stop();
    } finally {
        clientRequestExecutorPool.close();
    }
}
Also used : SocketDestination(voldemort.store.socket.SocketDestination) ClientRequestExecutorPool(voldemort.store.socket.clientrequest.ClientRequestExecutorPool) ClientRequestHandlerFactory(voldemort.server.protocol.ClientRequestHandlerFactory) ClientRequestExecutor(voldemort.store.socket.clientrequest.ClientRequestExecutor) AbstractSocketService(voldemort.server.AbstractSocketService) TestStats(voldemort.socketpool.AbstractSocketPoolTest.TestStats) ResourcePoolConfig(voldemort.utils.pool.ResourcePoolConfig) Test(org.junit.Test)

Example 3 with ClientRequestExecutor

use of voldemort.store.socket.clientrequest.ClientRequestExecutor in project voldemort by voldemort.

the class ClientRequestExecutorPoolTest method testCloseWithInFlightSockets.

@Test
public void testCloseWithInFlightSockets() throws Exception {
    List<ClientRequestExecutor> list = new ArrayList<ClientRequestExecutor>();
    for (int i = 0; i < maxConnectionsPerNode; i++) list.add(pool.checkout(dest1));
    assertEquals(list.size(), pool.getStats().getCount(ClientSocketStats.Tracked.CONNECTION_CREATED_EVENT));
    assertEquals(list.size(), pool.getStats().getConnectionsActive(null));
    pool.close(dest1);
    assertEquals(list.size(), pool.getStats().getConnectionsActive(null));
    assertEquals(0, pool.getStats().getCount(ClientSocketStats.Tracked.CONNECTION_DESTROYED_EVENT));
    for (ClientRequestExecutor sas : list) pool.checkin(dest1, sas);
    assertEquals(0, pool.getStats().getConnectionsActive(null));
    assertEquals(list.size(), pool.getStats().getCount(ClientSocketStats.Tracked.CONNECTION_CREATED_EVENT));
}
Also used : ArrayList(java.util.ArrayList) ClientRequestExecutor(voldemort.store.socket.clientrequest.ClientRequestExecutor) Test(org.junit.Test)

Example 4 with ClientRequestExecutor

use of voldemort.store.socket.clientrequest.ClientRequestExecutor in project voldemort by voldemort.

the class ClientRequestExecutorPoolTest method testSocketClosedWhenCheckedInAfterPoolKeyClosed.

@Test
public void testSocketClosedWhenCheckedInAfterPoolKeyClosed() throws Exception {
    ClientRequestExecutor sas1 = pool.checkout(dest1);
    ClientRequestExecutor sas2 = pool.checkout(dest1);
    assertTrue(sas1 != sas2);
    pool.checkin(dest1, sas1);
    pool.close(dest1);
    pool.checkin(dest1, sas2);
    pool.close(dest1);
}
Also used : ClientRequestExecutor(voldemort.store.socket.clientrequest.ClientRequestExecutor) Test(org.junit.Test)

Example 5 with ClientRequestExecutor

use of voldemort.store.socket.clientrequest.ClientRequestExecutor in project voldemort by voldemort.

the class ClientRequestExecutorPoolTest method testAtRestServerBounce.

@Test
public void testAtRestServerBounce() throws Exception {
    // Create 2 resources
    ClientRequestExecutor sas1 = pool.checkout(dest1);
    ClientRequestExecutor sas2 = pool.checkout(dest1);
    pool.checkin(dest1, sas1);
    pool.checkin(dest1, sas2);
    validateResourceCount(pool, "two connections are created ", 2);
    stopServer();
    validateResourceCount(pool, "cache should have 2 dead connections ", 2);
    // It takes few milliseconds for the selector to wake-up and clear
    // connections
    Thread.sleep(5);
    testConnectionFailure(pool, dest1, ConnectException.class);
    validateResourceCount(pool, "next checkout should have cleared all dead connections", 0);
    startServer();
    sas1 = pool.checkout(dest1);
    sas2 = pool.checkout(dest1);
    pool.checkin(dest1, sas1);
    pool.checkin(dest1, sas2);
    validateResourceCount(pool, "Back to normal 2 connections expected ", 2);
}
Also used : ClientRequestExecutor(voldemort.store.socket.clientrequest.ClientRequestExecutor) Test(org.junit.Test)

Aggregations

ClientRequestExecutor (voldemort.store.socket.clientrequest.ClientRequestExecutor)12 Test (org.junit.Test)10 UnreachableStoreException (voldemort.store.UnreachableStoreException)3 ClientRequestExecutorPool (voldemort.store.socket.clientrequest.ClientRequestExecutorPool)3 ArrayList (java.util.ArrayList)2 IOException (java.io.IOException)1 AbstractSocketService (voldemort.server.AbstractSocketService)1 ClientRequestHandlerFactory (voldemort.server.protocol.ClientRequestHandlerFactory)1 TestStats (voldemort.socketpool.AbstractSocketPoolTest.TestStats)1 SocketDestination (voldemort.store.socket.SocketDestination)1 ResourcePoolConfig (voldemort.utils.pool.ResourcePoolConfig)1