Search in sources :

Example 91 with TimeoutException

use of java.util.concurrent.TimeoutException in project flink by apache.

the class AsyncWaitOperator method processElement.

@Override
public void processElement(StreamRecord<IN> element) throws Exception {
    final StreamRecordQueueEntry<OUT> streamRecordBufferEntry = new StreamRecordQueueEntry<>(element);
    if (timeout > 0L) {
        // register a timeout for this AsyncStreamRecordBufferEntry
        long timeoutTimestamp = timeout + getProcessingTimeService().getCurrentProcessingTime();
        final ScheduledFuture<?> timerFuture = getProcessingTimeService().registerTimer(timeoutTimestamp, new ProcessingTimeCallback() {

            @Override
            public void onProcessingTime(long timestamp) throws Exception {
                streamRecordBufferEntry.collect(new TimeoutException("Async function call has timed out."));
            }
        });
        // Cancel the timer once we've completed the stream record buffer entry. This will remove
        // the register trigger task
        streamRecordBufferEntry.onComplete(new AcceptFunction<StreamElementQueueEntry<Collection<OUT>>>() {

            @Override
            public void accept(StreamElementQueueEntry<Collection<OUT>> value) {
                timerFuture.cancel(true);
            }
        }, executor);
    }
    addAsyncBufferEntry(streamRecordBufferEntry);
    userFunction.asyncInvoke(element.getValue(), streamRecordBufferEntry);
}
Also used : StreamElementQueueEntry(org.apache.flink.streaming.api.operators.async.queue.StreamElementQueueEntry) TimeoutException(java.util.concurrent.TimeoutException) ProcessingTimeCallback(org.apache.flink.streaming.runtime.tasks.ProcessingTimeCallback) Collection(java.util.Collection) StreamRecordQueueEntry(org.apache.flink.streaming.api.operators.async.queue.StreamRecordQueueEntry) TimeoutException(java.util.concurrent.TimeoutException)

Example 92 with TimeoutException

use of java.util.concurrent.TimeoutException in project flink by apache.

the class AsyncWaitOperatorTest method testAsyncTimeout.

@Test
public void testAsyncTimeout() throws Exception {
    final long timeout = 10L;
    final AsyncWaitOperator<Integer, Integer> operator = new AsyncWaitOperator<>(new LazyAsyncFunction(), timeout, 2, AsyncDataStream.OutputMode.ORDERED);
    final Environment mockEnvironment = mock(Environment.class);
    final Configuration taskConfiguration = new Configuration();
    final ExecutionConfig executionConfig = new ExecutionConfig();
    final TaskMetricGroup metricGroup = new UnregisteredTaskMetricsGroup();
    final TaskManagerRuntimeInfo taskManagerRuntimeInfo = new TestingTaskManagerRuntimeInfo();
    final TaskInfo taskInfo = new TaskInfo("foobarTask", 1, 0, 1, 1);
    when(mockEnvironment.getTaskConfiguration()).thenReturn(taskConfiguration);
    when(mockEnvironment.getExecutionConfig()).thenReturn(executionConfig);
    when(mockEnvironment.getMetricGroup()).thenReturn(metricGroup);
    when(mockEnvironment.getTaskManagerInfo()).thenReturn(taskManagerRuntimeInfo);
    when(mockEnvironment.getTaskInfo()).thenReturn(taskInfo);
    when(mockEnvironment.getUserClassLoader()).thenReturn(AsyncWaitOperatorTest.class.getClassLoader());
    final OneInputStreamOperatorTestHarness<Integer, Integer> testHarness = new OneInputStreamOperatorTestHarness<>(operator, IntSerializer.INSTANCE, mockEnvironment);
    final long initialTime = 0L;
    final ConcurrentLinkedQueue<Object> expectedOutput = new ConcurrentLinkedQueue<>();
    testHarness.open();
    testHarness.setProcessingTime(initialTime);
    synchronized (testHarness.getCheckpointLock()) {
        testHarness.processElement(new StreamRecord<>(1, initialTime));
        testHarness.setProcessingTime(initialTime + 5L);
        testHarness.processElement(new StreamRecord<>(2, initialTime + 5L));
    }
    // trigger the timeout of the first stream record
    testHarness.setProcessingTime(initialTime + timeout + 1L);
    // allow the second async stream record to be processed
    LazyAsyncFunction.countDown();
    // wait until all async collectors in the buffer have been emitted out.
    synchronized (testHarness.getCheckpointLock()) {
        testHarness.close();
    }
    expectedOutput.add(new StreamRecord<>(2, initialTime + 5L));
    TestHarnessUtil.assertOutputEquals("Output with watermark was not correct.", expectedOutput, testHarness.getOutput());
    ArgumentCaptor<Throwable> argumentCaptor = ArgumentCaptor.forClass(Throwable.class);
    verify(mockEnvironment).failExternally(argumentCaptor.capture());
    Throwable failureCause = argumentCaptor.getValue();
    Assert.assertNotNull(failureCause.getCause());
    Assert.assertTrue(failureCause.getCause() instanceof ExecutionException);
    Assert.assertNotNull(failureCause.getCause().getCause());
    Assert.assertTrue(failureCause.getCause().getCause() instanceof TimeoutException);
}
Also used : UnregisteredTaskMetricsGroup(org.apache.flink.runtime.operators.testutils.UnregisteredTaskMetricsGroup) Configuration(org.apache.flink.configuration.Configuration) TestingTaskManagerRuntimeInfo(org.apache.flink.runtime.util.TestingTaskManagerRuntimeInfo) TaskManagerRuntimeInfo(org.apache.flink.runtime.taskmanager.TaskManagerRuntimeInfo) TaskMetricGroup(org.apache.flink.runtime.metrics.groups.TaskMetricGroup) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) OneInputStreamOperatorTestHarness(org.apache.flink.streaming.util.OneInputStreamOperatorTestHarness) TaskInfo(org.apache.flink.api.common.TaskInfo) TestingTaskManagerRuntimeInfo(org.apache.flink.runtime.util.TestingTaskManagerRuntimeInfo) Environment(org.apache.flink.runtime.execution.Environment) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) StreamMockEnvironment(org.apache.flink.streaming.runtime.tasks.StreamMockEnvironment) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) ExecutionException(java.util.concurrent.ExecutionException) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test)

Example 93 with TimeoutException

use of java.util.concurrent.TimeoutException in project camel by apache.

the class CamelContinuationServlet method doService.

@Override
protected void doService(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
    log.trace("Service: {}", request);
    // is there a consumer registered for the request.
    HttpConsumer consumer = getServletResolveConsumerStrategy().resolve(request, getConsumers());
    if (consumer == null) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }
    // figure out if continuation is enabled and what timeout to use
    boolean useContinuation = false;
    Long continuationTimeout = null;
    HttpCommonEndpoint endpoint = consumer.getEndpoint();
    if (endpoint instanceof JettyHttpEndpoint) {
        JettyHttpEndpoint jettyEndpoint = (JettyHttpEndpoint) endpoint;
        Boolean epUseContinuation = jettyEndpoint.getUseContinuation();
        Long epContinuationTimeout = jettyEndpoint.getContinuationTimeout();
        if (epUseContinuation != null) {
            useContinuation = epUseContinuation;
        } else {
            useContinuation = jettyEndpoint.getComponent().isUseContinuation();
        }
        if (epContinuationTimeout != null) {
            continuationTimeout = epContinuationTimeout;
        } else {
            continuationTimeout = jettyEndpoint.getComponent().getContinuationTimeout();
        }
    }
    if (useContinuation) {
        log.trace("Start request with continuation timeout of {}", continuationTimeout != null ? continuationTimeout : "jetty default");
    } else {
        log.trace("Usage of continuation is disabled, either by component or endpoint configuration, fallback to normal servlet processing instead");
        super.doService(request, response);
        return;
    }
    if (consumer.getEndpoint().getHttpMethodRestrict() != null) {
        Iterator<?> it = ObjectHelper.createIterable(consumer.getEndpoint().getHttpMethodRestrict()).iterator();
        boolean match = false;
        while (it.hasNext()) {
            String method = it.next().toString();
            if (method.equalsIgnoreCase(request.getMethod())) {
                match = true;
                break;
            }
        }
        if (!match) {
            response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
            return;
        }
    }
    if ("TRACE".equals(request.getMethod()) && !consumer.isTraceEnabled()) {
        response.sendError(HttpServletResponse.SC_METHOD_NOT_ALLOWED);
        return;
    }
    // we do not support java serialized objects unless explicit enabled
    String contentType = request.getContentType();
    if (HttpConstants.CONTENT_TYPE_JAVA_SERIALIZED_OBJECT.equals(contentType) && !consumer.getEndpoint().getComponent().isAllowJavaSerializedObject()) {
        response.sendError(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);
        return;
    }
    final Exchange result = (Exchange) request.getAttribute(EXCHANGE_ATTRIBUTE_NAME);
    if (result == null) {
        // no asynchronous result so leverage continuation
        final Continuation continuation = ContinuationSupport.getContinuation(request);
        if (continuation.isInitial() && continuationTimeout != null) {
            // set timeout on initial
            continuation.setTimeout(continuationTimeout);
        }
        // are we suspended and a request is dispatched initially?
        if (consumer.isSuspended() && continuation.isInitial()) {
            response.sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
            return;
        }
        if (continuation.isExpired()) {
            String id = (String) continuation.getAttribute(EXCHANGE_ATTRIBUTE_ID);
            // remember this id as expired
            expiredExchanges.put(id, id);
            log.warn("Continuation expired of exchangeId: {}", id);
            consumer.getBinding().doWriteExceptionResponse(new TimeoutException(), response);
            return;
        }
        // a new request so create an exchange
        final Exchange exchange = new DefaultExchange(consumer.getEndpoint(), ExchangePattern.InOut);
        if (consumer.getEndpoint().isBridgeEndpoint()) {
            exchange.setProperty(Exchange.SKIP_GZIP_ENCODING, Boolean.TRUE);
            exchange.setProperty(Exchange.SKIP_WWW_FORM_URLENCODED, Boolean.TRUE);
        }
        if (consumer.getEndpoint().isDisableStreamCache()) {
            exchange.setProperty(Exchange.DISABLE_HTTP_STREAM_CACHE, Boolean.TRUE);
        }
        HttpHelper.setCharsetFromContentType(request.getContentType(), exchange);
        exchange.setIn(new HttpMessage(exchange, request, response));
        // set context path as header
        String contextPath = consumer.getEndpoint().getPath();
        exchange.getIn().setHeader("CamelServletContextPath", contextPath);
        updateHttpPath(exchange, contextPath);
        if (log.isTraceEnabled()) {
            log.trace("Suspending continuation of exchangeId: {}", exchange.getExchangeId());
        }
        continuation.setAttribute(EXCHANGE_ATTRIBUTE_ID, exchange.getExchangeId());
        // we want to handle the UoW
        try {
            consumer.createUoW(exchange);
        } catch (Exception e) {
            log.error("Error processing request", e);
            throw new ServletException(e);
        }
        // must suspend before we process the exchange
        continuation.suspend();
        ClassLoader oldTccl = overrideTccl(exchange);
        if (log.isTraceEnabled()) {
            log.trace("Processing request for exchangeId: {}", exchange.getExchangeId());
        }
        // use the asynchronous API to process the exchange
        consumer.getAsyncProcessor().process(exchange, new AsyncCallback() {

            public void done(boolean doneSync) {
                // check if the exchange id is already expired
                boolean expired = expiredExchanges.remove(exchange.getExchangeId()) != null;
                if (!expired) {
                    if (log.isTraceEnabled()) {
                        log.trace("Resuming continuation of exchangeId: {}", exchange.getExchangeId());
                    }
                    // resume processing after both, sync and async callbacks
                    continuation.setAttribute(EXCHANGE_ATTRIBUTE_NAME, exchange);
                    continuation.resume();
                } else {
                    log.warn("Cannot resume expired continuation of exchangeId: {}", exchange.getExchangeId());
                }
            }
        });
        if (oldTccl != null) {
            restoreTccl(exchange, oldTccl);
        }
        // method again when its resumed
        return;
    }
    try {
        // now lets output to the response
        if (log.isTraceEnabled()) {
            log.trace("Resumed continuation and writing response for exchangeId: {}", result.getExchangeId());
        }
        Integer bs = consumer.getEndpoint().getResponseBufferSize();
        if (bs != null) {
            log.trace("Using response buffer size: {}", bs);
            response.setBufferSize(bs);
        }
        consumer.getBinding().writeResponse(result, response);
    } catch (IOException e) {
        log.error("Error processing request", e);
        throw e;
    } catch (Exception e) {
        log.error("Error processing request", e);
        throw new ServletException(e);
    } finally {
        consumer.doneUoW(result);
    }
}
Also used : DefaultExchange(org.apache.camel.impl.DefaultExchange) Continuation(org.eclipse.jetty.continuation.Continuation) AsyncCallback(org.apache.camel.AsyncCallback) IOException(java.io.IOException) HttpConsumer(org.apache.camel.http.common.HttpConsumer) ServletException(javax.servlet.ServletException) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) DefaultExchange(org.apache.camel.impl.DefaultExchange) Exchange(org.apache.camel.Exchange) ServletException(javax.servlet.ServletException) HttpMessage(org.apache.camel.http.common.HttpMessage) HttpCommonEndpoint(org.apache.camel.http.common.HttpCommonEndpoint) TimeoutException(java.util.concurrent.TimeoutException)

Example 94 with TimeoutException

use of java.util.concurrent.TimeoutException in project camel by apache.

the class EtcdKeysProducer method processGet.

private void processGet(EtcdClient client, String path, Exchange exchange) throws Exception {
    EtcdKeyGetRequest request = client.get(path);
    setRequestTimeout(request, exchange);
    setRequestRecursive(request, exchange);
    try {
        exchange.getIn().setHeader(EtcdConstants.ETCD_NAMESPACE, getNamespace());
        exchange.getIn().setBody(request.send().get());
    } catch (TimeoutException e) {
        throw new ExchangeTimedOutException(exchange, configuration.getTimeout());
    }
}
Also used : EtcdKeyGetRequest(mousio.etcd4j.requests.EtcdKeyGetRequest) ExchangeTimedOutException(org.apache.camel.ExchangeTimedOutException) TimeoutException(java.util.concurrent.TimeoutException)

Example 95 with TimeoutException

use of java.util.concurrent.TimeoutException in project camel by apache.

the class EtcdKeysProducer method processDel.

private void processDel(EtcdClient client, String path, boolean dir, Exchange exchange) throws Exception {
    EtcdKeyDeleteRequest request = client.delete(path);
    setRequestTimeout(request, exchange);
    setRequestRecursive(request, exchange);
    if (dir) {
        request.dir();
    }
    try {
        exchange.getIn().setHeader(EtcdConstants.ETCD_NAMESPACE, getNamespace());
        exchange.getIn().setBody(request.send().get());
    } catch (TimeoutException e) {
        throw new ExchangeTimedOutException(exchange, configuration.getTimeout());
    }
}
Also used : EtcdKeyDeleteRequest(mousio.etcd4j.requests.EtcdKeyDeleteRequest) ExchangeTimedOutException(org.apache.camel.ExchangeTimedOutException) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

TimeoutException (java.util.concurrent.TimeoutException)788 ExecutionException (java.util.concurrent.ExecutionException)249 IOException (java.io.IOException)184 Test (org.junit.Test)149 ArrayList (java.util.ArrayList)75 CountDownLatch (java.util.concurrent.CountDownLatch)73 ExecutorService (java.util.concurrent.ExecutorService)71 Future (java.util.concurrent.Future)54 CancellationException (java.util.concurrent.CancellationException)44 Test (org.testng.annotations.Test)44 List (java.util.List)39 HashMap (java.util.HashMap)38 Map (java.util.Map)38 File (java.io.File)36 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)36 TimeUnit (java.util.concurrent.TimeUnit)34 AtomicReference (java.util.concurrent.atomic.AtomicReference)26 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)22 URI (java.net.URI)21 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)21