org.apache.flink.streaming.api.functions.async.ResultFuture Java Examples

The following examples show how to use org.apache.flink.streaming.api.functions.async.ResultFuture. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AsyncWaitOperatorTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void asyncInvoke(final Integer input, final ResultFuture<Integer> resultFuture) throws Exception {
	this.executorService.submit(new Runnable() {
		@Override
		public void run() {
			try {
				latch.await();
			}
			catch (InterruptedException e) {
				// do nothing
			}

			resultFuture.complete(Collections.singletonList(input));
		}
	});
}
 
Example #2
Source File: AsyncIOExample.java    From flink-learning with Apache License 2.0 6 votes vote down vote up
@Override
public void asyncInvoke(final Integer input, final ResultFuture<String> resultFuture) {
    executorService.submit(() -> {
        // wait for while to simulate async operation here
        long sleep = (long) (ThreadLocalRandom.current().nextFloat() * sleepFactor);
        try {
            Thread.sleep(sleep);

            if (ThreadLocalRandom.current().nextFloat() < failRatio) {
                resultFuture.completeExceptionally(new Exception("wahahahaha..."));
            } else {
                resultFuture.complete(
                        Collections.singletonList("key-" + (input % 10)));
            }
        } catch (InterruptedException e) {
            resultFuture.complete(new ArrayList<>(0));
        }
    });
}
 
Example #3
Source File: AsyncIOExample.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Override
public void asyncInvoke(final Integer input, final ResultFuture<String> resultFuture) {
	executorService.submit(() -> {
		// wait for while to simulate async operation here
		long sleep = (long) (ThreadLocalRandom.current().nextFloat() * sleepFactor);
		try {
			Thread.sleep(sleep);

			if (ThreadLocalRandom.current().nextFloat() < failRatio) {
				resultFuture.completeExceptionally(new Exception("wahahahaha..."));
			} else {
				resultFuture.complete(
					Collections.singletonList("key-" + (input % 10)));
			}
		} catch (InterruptedException e) {
			resultFuture.complete(new ArrayList<>(0));
		}
	});
}
 
Example #4
Source File: AsyncWaitOperatorTest.java    From Flink-CEPplus with Apache License 2.0 6 votes vote down vote up
@Override
public void asyncInvoke(final Integer input, final ResultFuture<Integer> resultFuture) throws Exception {
	this.executorService.submit(new Runnable() {
		@Override
		public void run() {
			try {
				latch.await();
			}
			catch (InterruptedException e) {
				// do nothing
			}

			resultFuture.complete(Collections.singletonList(input));
		}
	});
}
 
Example #5
Source File: ItemInfoEnrichment.java    From flink-tutorials with Apache License 2.0 6 votes vote down vote up
@Override
public void asyncInvoke(QueryResult queryResult, ResultFuture<QueryResult> resultFuture) throws Exception {
	executor.submit(() -> {
		try {
			itemQuery.setString(1, queryResult.itemInfo.itemId);
			ResultSet rs = itemQuery.executeQuery();
			if (rs.next()) {
				queryResult.itemInfo.setItemName(rs.getString("name"));
			}

			resultFuture.complete(Collections.singletonList(queryResult));
		} catch (SQLException t) {
			resultFuture.completeExceptionally(t);
		}
	});
}
 
Example #6
Source File: AsyncIOExample.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void asyncInvoke(final Integer input, final ResultFuture<String> resultFuture) {
	executorService.submit(() -> {
		// wait for while to simulate async operation here
		long sleep = (long) (ThreadLocalRandom.current().nextFloat() * sleepFactor);
		try {
			Thread.sleep(sleep);

			if (ThreadLocalRandom.current().nextFloat() < failRatio) {
				resultFuture.completeExceptionally(new Exception("wahahahaha..."));
			} else {
				resultFuture.complete(
					Collections.singletonList("key-" + (input % 10)));
			}
		} catch (InterruptedException e) {
			resultFuture.complete(new ArrayList<>(0));
		}
	});
}
 
Example #7
Source File: AsyncIOExample.java    From flink-learning with Apache License 2.0 6 votes vote down vote up
@Override
public void asyncInvoke(final Integer input, final ResultFuture<String> resultFuture) {
    executorService.submit(() -> {
        // wait for while to simulate async operation here
        long sleep = (long) (ThreadLocalRandom.current().nextFloat() * sleepFactor);
        try {
            Thread.sleep(sleep);

            if (ThreadLocalRandom.current().nextFloat() < failRatio) {
                resultFuture.completeExceptionally(new Exception("wahahahaha..."));
            } else {
                resultFuture.complete(
                        Collections.singletonList("key-" + (input % 10)));
            }
        } catch (InterruptedException e) {
            resultFuture.complete(new ArrayList<>(0));
        }
    });
}
 
Example #8
Source File: AsyncFunctionHelper.java    From sylph with Apache License 2.0 6 votes vote down vote up
@Override
public void asyncInvoke(Row input, ResultFuture<Row> asyncCollector)
        throws Exception
{
    CompletableFuture<Collection<Row>> resultFuture = CompletableFuture.supplyAsync(() -> {
        List<Row> rows = new ArrayList<>();
        transForm.process(new FlinkRecord(input, streamRowType), record -> rows.add(FlinkRecord.parserRow(record)));
        return rows;
    });

    // 设置请求完成时的回调: 将结果传递给 collector
    resultFuture.whenComplete((result, error) -> {
        if (error != null) {
            asyncCollector.completeExceptionally(error);
        }
        else {
            asyncCollector.complete(result);
        }
    });
}
 
Example #9
Source File: AsyncWaitOperator.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void processElement(StreamRecord<IN> element) throws Exception {
	// add element first to the queue
	final ResultFuture<OUT> entry = addToWorkQueue(element);

	final ResultHandler resultHandler = new ResultHandler(element, entry);

	// register a timeout for the entry if timeout is configured
	if (timeout > 0L) {
		final long timeoutTimestamp = timeout + getProcessingTimeService().getCurrentProcessingTime();

		final ScheduledFuture<?> timeoutTimer = getProcessingTimeService().registerTimer(
			timeoutTimestamp,
			timestamp -> userFunction.timeout(element.getValue(), resultHandler));

		resultHandler.setTimeoutTimer(timeoutTimer);
	}

	userFunction.asyncInvoke(element.getValue(), resultHandler);
}
 
Example #10
Source File: OrderedStreamElementQueue.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public Optional<ResultFuture<OUT>> tryPut(StreamElement streamElement) {
	if (queue.size() < capacity) {
		StreamElementQueueEntry<OUT> queueEntry = createEntry(streamElement);

		queue.add(queueEntry);

		LOG.debug("Put element into ordered stream element queue. New filling degree " +
			"({}/{}).", queue.size(), capacity);

		return Optional.of(queueEntry);
	} else {
		LOG.debug("Failed to put element into ordered stream element queue because it " +
			"was full ({}/{}).", queue.size(), capacity);

		return Optional.empty();
	}
}
 
Example #11
Source File: UnorderedStreamElementQueue.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public Optional<ResultFuture<OUT>> tryPut(StreamElement streamElement) {
	if (size() < capacity) {
		StreamElementQueueEntry<OUT> queueEntry;
		if (streamElement.isRecord()) {
			queueEntry = addRecord((StreamRecord<?>) streamElement);
		} else if (streamElement.isWatermark()) {
			queueEntry = addWatermark((Watermark) streamElement);
		} else {
			throw new UnsupportedOperationException("Cannot enqueue " + streamElement);
		}

		numberOfEntries++;

		LOG.debug("Put element into unordered stream element queue. New filling degree " +
				"({}/{}).", size(), capacity);

		return Optional.of(queueEntry);
	} else {
		LOG.debug("Failed to put element into unordered stream element queue because it " +
				"was full ({}/{}).", size(), capacity);

		return Optional.empty();
	}
}
 
Example #12
Source File: StreamElementQueueTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Test
public void testPop() {
	StreamElementQueue<Integer> queue = createStreamElementQueue(2);

	// add two elements to reach capacity
	putSuccessfully(queue, new Watermark(0L));
	ResultFuture<Integer> recordResult = putSuccessfully(queue, new StreamRecord<>(42, 1L));

	assertEquals(2, queue.size());

	// remove completed elements (watermarks are always completed)
	assertEquals(Arrays.asList(new Watermark(0L)), popCompleted(queue));
	assertEquals(1, queue.size());

	// now complete the stream record
	recordResult.complete(Collections.singleton(43));

	assertEquals(Arrays.asList(new StreamRecord<>(43, 1L)), popCompleted(queue));
	assertEquals(0, queue.size());
	assertTrue(queue.isEmpty());
}
 
Example #13
Source File: StreamElementQueueTest.java    From flink with Apache License 2.0 6 votes vote down vote up
/**
 * Tests that a put operation fails if the queue is full.
 */
@Test
public void testPutOnFull() throws Exception {
	final StreamElementQueue<Integer> queue = createStreamElementQueue(1);

	// fill up queue
	ResultFuture<Integer> resultFuture = putSuccessfully(queue, new StreamRecord<>(42, 0L));
	assertEquals(1, queue.size());

	// cannot add more
	putUnsuccessfully(queue, new StreamRecord<>(43, 1L));

	// popping the completed element frees the queue again
	resultFuture.complete(Collections.singleton(42 * 42));
	assertEquals(Arrays.asList(new StreamRecord<Integer>(42 * 42, 0L)), popCompleted(queue));

	// now the put operation should complete
	putSuccessfully(queue, new StreamRecord<>(43, 1L));
}
 
Example #14
Source File: AsyncWaitOperatorTest.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void asyncInvoke(final Integer input, final ResultFuture<Integer> resultFuture) throws Exception {
	this.executorService.submit(new Runnable() {
		@Override
		public void run() {
			try {
				latch.await();
			}
			catch (InterruptedException e) {
				// do nothing
			}

			resultFuture.complete(Collections.singletonList(input));
		}
	});
}
 
Example #15
Source File: AsyncIOExample.java    From flink with Apache License 2.0 6 votes vote down vote up
@Override
public void asyncInvoke(final Integer input, final ResultFuture<String> resultFuture) {
	executorService.submit(() -> {
		// wait for while to simulate async operation here
		long sleep = (long) (ThreadLocalRandom.current().nextFloat() * sleepFactor);
		try {
			Thread.sleep(sleep);

			if (ThreadLocalRandom.current().nextFloat() < failRatio) {
				resultFuture.completeExceptionally(new Exception("wahahahaha..."));
			} else {
				resultFuture.complete(
					Collections.singletonList("key-" + (input % 10)));
			}
		} catch (InterruptedException e) {
			resultFuture.complete(new ArrayList<>(0));
		}
	});
}
 
Example #16
Source File: AsyncWaitOperatorTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void asyncInvoke(final Integer input, final ResultFuture<Integer> resultFuture) throws Exception {
	executorService.submit(new Runnable() {
		@Override
		public void run() {
			resultFuture.complete(Collections.singletonList(input * 2));
		}
	});
}
 
Example #17
Source File: AsyncWaitOperatorTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void asyncInvoke(final Integer input, final ResultFuture<Integer> resultFuture) throws Exception {
	executorService.submit(() -> {
		try {
			Thread.sleep(delayed);
		} catch (InterruptedException e) {
			resultFuture.completeExceptionally(e);
		}
		resultFuture.complete(Collections.singletonList(input * 2));
	});
}
 
Example #18
Source File: AsyncLookupJoinHarnessTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void asyncInvoke(BaseRow input, ResultFuture<BaseRow> resultFuture) throws Exception {
	int id = input.getInt(0);
	CompletableFuture
		.supplyAsync((Supplier<Collection<BaseRow>>) () -> data.get(id), executor)
		.thenAcceptAsync(resultFuture::complete, executor);
}
 
Example #19
Source File: MysqlAsyncSideFunction.java    From alchemy with Apache License 2.0 5 votes vote down vote up
@Override
public void timeout(Row input, ResultFuture<Row> resultFuture) throws Exception {
    if (this.sideTable.getSide().isLogTimeoutOnly()) {
        LOG.error("async request timeout from mysql");
    } else {
        super.timeout(input, resultFuture);
    }
}
 
Example #20
Source File: Future.java    From alchemy with Apache License 2.0 5 votes vote down vote up
public boolean completeExceptionally(Throwable error){
    if (future instanceof ResultFuture){
        ResultFuture resultFuture = (ResultFuture) future;
        resultFuture.completeExceptionally(error);
        return true;
    }else{
        return ((CompletableFuture)future).completeExceptionally(error);
    }
}
 
Example #21
Source File: Future.java    From alchemy with Apache License 2.0 5 votes vote down vote up
public <I> void  complete(Collection<I> result){
    if (future instanceof ResultFuture){
        ResultFuture<I> resultFuture = (ResultFuture) future;
        resultFuture.complete(result);
    }else{
        ((CompletableFuture<Collection<I>>)future).complete(result);
    }
}
 
Example #22
Source File: AsyncWaitOperatorTest.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public void asyncInvoke(final Integer input, final ResultFuture<Integer> resultFuture) throws Exception {
	executorService.submit(new Runnable() {
		@Override
		public void run() {
			resultFuture.complete(Collections.singletonList(input * 2));
		}
	});
}
 
Example #23
Source File: AlertRuleAsyncIOFunction.java    From flink-learning with Apache License 2.0 5 votes vote down vote up
@Override
public void asyncInvoke(MetricEvent metricEvent, ResultFuture<MetricEvent> resultFuture) throws Exception {
    ps.setString(1, metricEvent.getName());
    ResultSet resultSet = ps.executeQuery();
    Map<String, Object> fields = metricEvent.getFields();
    if (resultSet.next()) {
        String thresholds = resultSet.getString("thresholds");
        String measurement = resultSet.getString("measurement");
        if (fields.get(measurement) != null && (double) fields.get(measurement) > Double.valueOf(thresholds)) {
            List<MetricEvent> list = new ArrayList<>();
            list.add(metricEvent);
            resultFuture.complete(Collections.singletonList(metricEvent));
        }
    }
}
 
Example #24
Source File: LengthenUrlsFunction.java    From flink-crawler with Apache License 2.0 5 votes vote down vote up
@Override
public void asyncInvoke(final RawUrl url, ResultFuture<RawUrl> future) throws Exception {
    record(this.getClass(), url);

    _executor.execute(new Runnable() {

        @Override
        public void run() {
            RawUrl lengthenedUrl = _lengthener.lengthen(url);
            future.complete(Collections.singleton(lengthenedUrl));
        }
    });
}
 
Example #25
Source File: AsyncLookupJoinRunner.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void asyncInvoke(RowData input, ResultFuture<RowData> resultFuture) throws Exception {
	JoinedRowResultFuture outResultFuture = resultFutureBuffer.take();
	// the input row is copied when object reuse in AsyncWaitOperator
	outResultFuture.reset(input, resultFuture);

	// fetcher has copied the input field when object reuse is enabled
	fetcher.asyncInvoke(input, outResultFuture);
}
 
Example #26
Source File: AsyncLookupJoinRunner.java    From flink with Apache License 2.0 5 votes vote down vote up
public void reset(RowData row, ResultFuture<RowData> realOutput) {
	this.realOutput = realOutput;
	this.leftRow = row;
	joinConditionResultFuture.setInput(row);
	joinConditionResultFuture.setResultFuture(delegate);
	delegate.reset();
}
 
Example #27
Source File: AsyncLookupJoinHarnessTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void asyncInvoke(RowData input, ResultFuture<RowData> resultFuture) throws Exception {
	int id = input.getInt(0);
	CompletableFuture
		.supplyAsync((Supplier<Collection<RowData>>) () -> data.get(id), executor)
		.thenAcceptAsync(resultFuture::complete, executor);
}
 
Example #28
Source File: OrderedStreamElementQueueTest.java    From flink with Apache License 2.0 5 votes vote down vote up
/**
 * Tests that only the head element is pulled from the ordered queue if it has been
 * completed.
 */
@Test
public void testCompletionOrder() {
	final OrderedStreamElementQueue<Integer> queue = new OrderedStreamElementQueue<>(4);

	ResultFuture<Integer> entry1 = putSuccessfully(queue, new StreamRecord<>(1, 0L));
	ResultFuture<Integer> entry2 = putSuccessfully(queue, new StreamRecord<>(2, 1L));
	putSuccessfully(queue, new Watermark(2L));
	ResultFuture<Integer> entry4 = putSuccessfully(queue, new StreamRecord<>(3, 3L));

	Assert.assertEquals(Collections.emptyList(), popCompleted(queue));
	Assert.assertEquals(4, queue.size());
	Assert.assertFalse(queue.isEmpty());

	entry2.complete(Collections.singleton(11));
	entry4.complete(Collections.singleton(13));

	Assert.assertEquals(Collections.emptyList(), popCompleted(queue));
	Assert.assertEquals(4, queue.size());
	Assert.assertFalse(queue.isEmpty());

	entry1.complete(Collections.singleton(10));

	List<StreamElement> expected = Arrays.asList(
		new StreamRecord<>(10, 0L),
		new StreamRecord<>(11, 1L),
		new Watermark(2L),
		new StreamRecord<>(13, 3L));
	Assert.assertEquals(expected, popCompleted(queue));
	Assert.assertEquals(0, queue.size());
	Assert.assertTrue(queue.isEmpty());
}
 
Example #29
Source File: AsyncWaitOperatorTest.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void asyncInvoke(final Integer input, final ResultFuture<Integer> resultFuture) throws Exception {
	executorService.submit(new Runnable() {
		@Override
		public void run() {
			resultFuture.complete(Collections.singletonList(input * 2));
		}
	});
}
 
Example #30
Source File: AsyncLookupJoinRunner.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public void asyncInvoke(BaseRow input, ResultFuture<BaseRow> resultFuture) throws Exception {
	JoinedRowResultFuture outResultFuture = resultFutureBuffer.take();
	// the input row is copied when object reuse in AsyncWaitOperator
	outResultFuture.reset(input, resultFuture);

	// fetcher has copied the input field when object reuse is enabled
	fetcher.asyncInvoke(input, outResultFuture);
}