Search in sources :

Example 1 with ExceptionSafeRunnable

use of org.bboxdb.commons.concurrent.ExceptionSafeRunnable in project bboxdb by jnidzwetzki.

the class ClientConnectionHandler method sendNextResultsForQuery.

/**
 * Send next results for the given query
 * @param packageSequence
 * @param
 * @throws PackageEncodeException
 * @throws IOException
 */
public void sendNextResultsForQuery(final short packageSequence, final short querySequence) throws IOException, PackageEncodeException {
    if (!getActiveQueries().containsKey(querySequence)) {
        logger.error("Unable to resume query {} - package {} - not found", querySequence, packageSequence);
        writeResultPackage(new ErrorResponse(packageSequence, ErrorMessages.ERROR_QUERY_NOT_FOUND));
        return;
    }
    final Runnable queryRunable = new ExceptionSafeRunnable() {

        @Override
        protected void runThread() throws IOException, PackageEncodeException {
            final ClientQuery clientQuery = getActiveQueries().get(querySequence);
            clientQuery.fetchAndSendNextTuples(packageSequence);
            if (clientQuery.isQueryDone()) {
                logger.info("Query {} is done with {} tuples, removing iterator ", querySequence, clientQuery.getTotalSendTuples());
                clientQuery.close();
                getActiveQueries().remove(querySequence);
            }
        }

        @Override
        protected void afterExceptionHook() {
            try {
                writeResultPackage(new ErrorResponse(packageSequence, ErrorMessages.ERROR_EXCEPTION));
            } catch (IOException | PackageEncodeException e) {
                logger.error("Unable to send result package", e);
            }
        }
    };
    // Submit the runnable to our pool
    if (threadPool.isShutdown()) {
        logger.warn("Thread pool is shutting down, don't execute query: {}", querySequence);
        writeResultPackage(new ErrorResponse(packageSequence, ErrorMessages.ERROR_EXCEPTION));
    } else {
        getThreadPool().submit(queryRunable);
    }
}
Also used : ExceptionSafeRunnable(org.bboxdb.commons.concurrent.ExceptionSafeRunnable) PackageEncodeException(org.bboxdb.network.packages.PackageEncodeException) IOException(java.io.IOException) ExceptionSafeRunnable(org.bboxdb.commons.concurrent.ExceptionSafeRunnable) ErrorResponse(org.bboxdb.network.packages.response.ErrorResponse)

Example 2 with ExceptionSafeRunnable

use of org.bboxdb.commons.concurrent.ExceptionSafeRunnable in project bboxdb by jnidzwetzki.

the class PackageRouter method performInsertPackageRoutingAsync.

/**
 * Perform the routing task async
 * @param packageSequence
 * @param insertTupleRequest
 * @param boundingBox
 */
public void performInsertPackageRoutingAsync(final short packageSequence, final InsertTupleRequest insertTupleRequest) {
    final Runnable routeRunable = new ExceptionSafeRunnable() {

        @Override
        protected void runThread() {
            boolean operationSuccess = true;
            try {
                final RoutingHeader routingHeader = insertTupleRequest.getRoutingHeader();
                assert (routingHeader.isRoutedPackage()) : "Tuple is not a routed package";
                if (!routingHeader.reachedFinalInstance()) {
                    routingHeader.dispatchToNextHop();
                    operationSuccess = sendInsertPackage(insertTupleRequest);
                }
            } catch (InterruptedException e) {
                logger.error("Exception while routing package", e);
                Thread.currentThread().interrupt();
                operationSuccess = false;
            } catch (PackageEncodeException e) {
                logger.error("Exception while routing package", e);
                operationSuccess = false;
            }
            if (operationSuccess) {
                final SuccessResponse responsePackage = new SuccessResponse(packageSequence);
                clientConnectionHandler.writeResultPackageNE(responsePackage);
            } else {
                final ErrorResponse responsePackage = new ErrorResponse(packageSequence, ErrorMessages.ERROR_ROUTING_FAILED);
                clientConnectionHandler.writeResultPackageNE(responsePackage);
            }
        }
    };
    // Submit the runnable to our pool
    if (threadPool.isShutdown()) {
        logger.warn("Thread pool is shutting down, don't route package: {}", packageSequence);
        final ErrorResponse responsePackage = new ErrorResponse(packageSequence, ErrorMessages.ERROR_QUERY_SHUTDOWN);
        clientConnectionHandler.writeResultPackageNE(responsePackage);
    } else {
        threadPool.submit(routeRunable);
    }
}
Also used : SuccessResponse(org.bboxdb.network.packages.response.SuccessResponse) ExceptionSafeRunnable(org.bboxdb.commons.concurrent.ExceptionSafeRunnable) PackageEncodeException(org.bboxdb.network.packages.PackageEncodeException) ExceptionSafeRunnable(org.bboxdb.commons.concurrent.ExceptionSafeRunnable) ErrorResponse(org.bboxdb.network.packages.response.ErrorResponse)

Example 3 with ExceptionSafeRunnable

use of org.bboxdb.commons.concurrent.ExceptionSafeRunnable in project bboxdb by jnidzwetzki.

the class TestExceptionSafeRunnable method testWithException.

/**
 * Test the runnable with an exception
 * @throws InterruptedException
 */
@Test(timeout = 10000)
public void testWithException() throws InterruptedException {
    final AtomicInteger runTheadCalls = new AtomicInteger(0);
    final AtomicInteger beginHookCalls = new AtomicInteger(0);
    final AtomicInteger endHookCalls = new AtomicInteger(0);
    final AtomicInteger afterExceptionCalls = new AtomicInteger(0);
    /**
     * Because the methods are protected, we need a real mock here
     * Mockito can't verify protected method calls
     */
    final ExceptionSafeRunnable runnable = new ExceptionSafeRunnable() {

        @Override
        protected void runThread() throws Exception {
            runTheadCalls.incrementAndGet();
            throw new Exception("Exception");
        }

        @Override
        protected void beginHook() {
            beginHookCalls.incrementAndGet();
        }

        @Override
        protected void endHook() {
            endHookCalls.incrementAndGet();
        }

        @Override
        protected void afterExceptionHook() {
            afterExceptionCalls.incrementAndGet();
        }
    };
    final Thread thread = new Thread(runnable);
    thread.start();
    thread.join();
    Assert.assertEquals(1, runTheadCalls.get());
    Assert.assertEquals(1, beginHookCalls.get());
    Assert.assertEquals(0, endHookCalls.get());
    Assert.assertEquals(1, afterExceptionCalls.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExceptionSafeRunnable(org.bboxdb.commons.concurrent.ExceptionSafeRunnable) RejectedException(org.bboxdb.commons.RejectedException) Test(org.junit.Test)

Example 4 with ExceptionSafeRunnable

use of org.bboxdb.commons.concurrent.ExceptionSafeRunnable in project bboxdb by jnidzwetzki.

the class TestExceptionSafeRunnable method testDefaultImplementation1.

@Test(timeout = 60000)
public void testDefaultImplementation1() throws InterruptedException {
    final ExceptionSafeRunnable runnable = new ExceptionSafeRunnable() {

        @Override
        protected void runThread() throws Exception {
        }
    };
    final Thread thread = new Thread(runnable);
    thread.start();
    thread.join();
}
Also used : ExceptionSafeRunnable(org.bboxdb.commons.concurrent.ExceptionSafeRunnable) Test(org.junit.Test)

Example 5 with ExceptionSafeRunnable

use of org.bboxdb.commons.concurrent.ExceptionSafeRunnable in project bboxdb by jnidzwetzki.

the class TestExceptionSafeRunnable method testWithoutException.

/**
 * Test the runnable without an exception
 * @throws InterruptedException
 */
@Test(timeout = 10000)
public void testWithoutException() throws InterruptedException {
    final AtomicInteger runTheadCalls = new AtomicInteger(0);
    final AtomicInteger beginHookCalls = new AtomicInteger(0);
    final AtomicInteger endHookCalls = new AtomicInteger(0);
    final AtomicInteger afterExceptionCalls = new AtomicInteger(0);
    /**
     * Because the methods are protected, we need a real mock here
     * Mockito can't verify protected method calls
     */
    final ExceptionSafeRunnable runnable = new ExceptionSafeRunnable() {

        @Override
        protected void runThread() throws Exception {
            runTheadCalls.incrementAndGet();
        }

        @Override
        protected void beginHook() {
            beginHookCalls.incrementAndGet();
        }

        @Override
        protected void endHook() {
            endHookCalls.incrementAndGet();
        }

        @Override
        protected void afterExceptionHook() {
            afterExceptionCalls.incrementAndGet();
        }
    };
    final Thread thread = new Thread(runnable);
    thread.start();
    thread.join();
    Assert.assertEquals(1, runTheadCalls.get());
    Assert.assertEquals(1, beginHookCalls.get());
    Assert.assertEquals(1, endHookCalls.get());
    Assert.assertEquals(0, afterExceptionCalls.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExceptionSafeRunnable(org.bboxdb.commons.concurrent.ExceptionSafeRunnable) Test(org.junit.Test)

Aggregations

ExceptionSafeRunnable (org.bboxdb.commons.concurrent.ExceptionSafeRunnable)8 Test (org.junit.Test)4 PackageEncodeException (org.bboxdb.network.packages.PackageEncodeException)3 ErrorResponse (org.bboxdb.network.packages.response.ErrorResponse)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 RejectedException (org.bboxdb.commons.RejectedException)2 IOException (java.io.IOException)1 QueryKeyRequest (org.bboxdb.network.packages.request.QueryKeyRequest)1 SuccessResponse (org.bboxdb.network.packages.response.SuccessResponse)1 ClientQuery (org.bboxdb.network.server.ClientQuery)1 KeyClientQuery (org.bboxdb.network.server.KeyClientQuery)1 TupleStoreName (org.bboxdb.storage.entity.TupleStoreName)1