Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 140D5200BCA for ; Mon, 21 Nov 2016 12:32:06 +0100 (CET) Received: by cust-asf.ponee.io (Postfix) id 1367C160B25; Mon, 21 Nov 2016 11:32:06 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id ED51E160B1C for ; Mon, 21 Nov 2016 12:32:04 +0100 (CET) Received: (qmail 4028 invoked by uid 500); 21 Nov 2016 11:32:04 -0000 Mailing-List: contact issues-help@flink.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@flink.apache.org Delivered-To: mailing list issues@flink.apache.org Received: (qmail 3925 invoked by uid 99); 21 Nov 2016 11:32:04 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd2-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 21 Nov 2016 11:32:04 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd2-us-west.apache.org (ASF Mail Server at spamd2-us-west.apache.org) with ESMTP id B38A71A7A89 for ; Mon, 21 Nov 2016 11:32:03 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd2-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -7.019 X-Spam-Level: X-Spam-Status: No, score=-7.019 tagged_above=-999 required=6.31 tests=[KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_DNSWL_HI=-5, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, RP_MATCHES_RCVD=-2.999] autolearn=disabled Received: from mx1-lw-us.apache.org ([10.40.0.8]) by localhost (spamd2-us-west.apache.org [10.40.0.9]) (amavisd-new, port 10024) with ESMTP id rz62NwIkDcVx for ; Mon, 21 Nov 2016 11:32:01 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx1-lw-us.apache.org (ASF Mail Server at mx1-lw-us.apache.org) with SMTP id 13BE45FE34 for ; Mon, 21 Nov 2016 11:32:00 +0000 (UTC) Received: (qmail 3163 invoked by uid 99); 21 Nov 2016 11:32:00 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 21 Nov 2016 11:32:00 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 45CCEF17C2; Mon, 21 Nov 2016 11:32:00 +0000 (UTC) From: tillrohrmann To: issues@flink.incubator.apache.org Reply-To: issues@flink.incubator.apache.org References: In-Reply-To: Subject: [GitHub] flink pull request #2629: [FLINK-4391] Provide support for asynchronous oper... Content-Type: text/plain Message-Id: <20161121113200.45CCEF17C2@git1-us-west.apache.org> Date: Mon, 21 Nov 2016 11:32:00 +0000 (UTC) archived-at: Mon, 21 Nov 2016 11:32:06 -0000 Github user tillrohrmann commented on a diff in the pull request: https://github.com/apache/flink/pull/2629#discussion_r88707993 --- Diff: flink-streaming-java/src/main/java/org/apache/flink/streaming/api/operators/async/AsyncCollectorBuffer.java --- @@ -0,0 +1,453 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.streaming.api.operators.async; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.annotation.VisibleForTesting; +import org.apache.flink.streaming.api.datastream.AsyncDataStream; +import org.apache.flink.streaming.api.operators.Output; +import org.apache.flink.streaming.api.operators.TimestampedCollector; +import org.apache.flink.streaming.api.watermark.Watermark; +import org.apache.flink.streaming.runtime.streamrecord.LatencyMarker; +import org.apache.flink.streaming.runtime.streamrecord.StreamElement; +import org.apache.flink.streaming.runtime.streamrecord.StreamRecord; +import org.apache.flink.util.Preconditions; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +/** + * AsyncCollectorBuffer will hold all {@link AsyncCollector} in its internal buffer, + * and emit results from {@link AsyncCollector} to the next operators following it by + * calling {@link Output#collect(Object)} + */ +@Internal +public class AsyncCollectorBuffer { + + /** + * Max number of {@link AsyncCollector} in the buffer. + */ + private final int bufferSize; + + private final AsyncDataStream.OutputMode mode; + + private final AsyncWaitOperator operator; + + /** + * Keep all {@code AsyncCollector} and their input {@link StreamElement} + */ + private final Map, StreamElement> queue = new LinkedHashMap<>(); + /** + * For the AsyncWaitOperator chained with StreamSource, the checkpoint thread may get the + * {@link org.apache.flink.streaming.runtime.tasks.StreamTask#lock} while {@link AsyncCollectorBuffer#queue} + * is full since main thread waits on this lock. The StreamElement in + * {@link AsyncWaitOperator#processElement(StreamRecord)} should be treated as a part of all StreamElements + * in its queue. It will be kept in the operator state while snapshotting. + */ + private StreamElement extraStreamElement; + + /** + * {@link TimestampedCollector} and {@link Output} to collect results and watermarks. + */ + private final Output> output; + private final TimestampedCollector timestampedCollector; + + /** + * Checkpoint lock from {@link org.apache.flink.streaming.runtime.tasks.StreamTask#lock} + */ + private final Object lock; + + private final Emitter emitter; + private final Thread emitThread; + + private IOException error; + + public AsyncCollectorBuffer( + int bufferSize, + AsyncDataStream.OutputMode mode, + Output> output, + TimestampedCollector collector, + Object lock, + AsyncWaitOperator operator) { + Preconditions.checkArgument(bufferSize > 0, "Future buffer size should be greater than 0."); + Preconditions.checkNotNull(output, "Output should not be NULL."); + Preconditions.checkNotNull(collector, "TimestampedCollector should not be NULL."); + Preconditions.checkNotNull(lock, "Checkpoint lock should not be NULL."); + Preconditions.checkNotNull(operator, "Reference to AsyncWaitOperator should not be NULL."); + + this.bufferSize = bufferSize; + this.mode = mode; + this.output = output; + this.timestampedCollector = collector; + this.operator = operator; + this.lock = lock; + + this.emitter = new Emitter(); + this.emitThread = new Thread(emitter); + } + + /** + * Add an {@link StreamRecord} into the buffer. A new {@link AsyncCollector} will be created and returned + * corresponding to the input StreamRecord. + *

+ * If buffer is full, caller will wait until a new space is available. + * + * @param record StreamRecord + * @return An AsyncCollector + * @throws Exception InterruptedException or IOException from AsyncCollector. + */ + public AsyncCollector addStreamRecord(StreamRecord record) throws InterruptedException, IOException { + while (queue.size() >= bufferSize) { + // hold the input StreamRecord until it is placed in the buffer + extraStreamElement = record; + + lock.wait(); + } + + if (error != null) { + throw error; + } + + AsyncCollector collector = new AsyncCollector(this); + + queue.put(collector, record); + + extraStreamElement = null; + + return collector; + } + + /** + * Add a {@link Watermark} into queue. A new AsyncCollector will be created and returned. + *

+ * If queue is full, caller will wait here. + * + * @param watermark Watermark + * @return AsyncCollector + * @throws Exception InterruptedException or IOException from AsyncCollector. + */ + public AsyncCollector addWatermark(Watermark watermark) throws InterruptedException, IOException { + return processMark(watermark); + } + + /** + * Add a {@link LatencyMarker} into queue. A new AsyncCollector will be created and returned. + *

+ * If queue is full, caller will wait here. + * + * @param latencyMarker LatencyMarker + * @return AsyncCollector + * @throws Exception InterruptedException or IOException from AsyncCollector. + */ + public AsyncCollector addLatencyMarker(LatencyMarker latencyMarker) throws InterruptedException, IOException { + return processMark(latencyMarker); + } + + private AsyncCollector processMark(StreamElement mark) throws InterruptedException, IOException { + while (queue.size() >= bufferSize) { + // hold the input StreamRecord until it is placed in the buffer + extraStreamElement = mark; + + lock.wait(); + } + + if (error != null) { + throw error; + } + + AsyncCollector collector = new AsyncCollector(this, true); + + queue.put(collector, mark); + + extraStreamElement = null; + + return collector; + } + + /** + * Notify the emitter thread and main thread that an AsyncCollector has completed. + * + * @param collector Completed AsyncCollector + */ + void markCollectorCompleted(AsyncCollector collector) { + synchronized (lock) { + collector.markDone(); + + // notify main thread to keep working + lock.notifyAll(); + } + } + + /** + * Caller will wait here if buffer is not empty, meaning that not all async i/o tasks have returned yet. + * + * @throws Exception InterruptedException or IOException from AsyncCollector. + */ + void waitEmpty() throws InterruptedException, IOException { + while (queue.size() != 0) { + if (error != null) { + throw error; + } + + lock.wait(); + } + } + + public void startEmitterThread() { + this.emitThread.start(); + } + + public void stopEmitterThread() { + emitter.stop(); + + emitThread.interrupt(); + } + + /** + * Get all StreamElements in the AsyncCollector queue. + *

+ * Emitter Thread can not output records and will wait for a while due to checkpoiting procedure + * holding the checkpoint lock. + * + * @return A List containing StreamElements. + */ + public List getStreamElementsInBuffer() { + List ret = new ArrayList<>(queue.size()); + for (Map.Entry, StreamElement> entry : queue.entrySet()) { + ret.add(entry.getValue()); + } + + // add the lonely input outside of queue into return set. + if (extraStreamElement != null) { + ret.add(extraStreamElement); + } + + return ret; + } + + @VisibleForTesting + public Map, StreamElement> getQueue() { + return this.queue; + } + + /** + * A working thread to output results from {@link AsyncCollector} to the next operator. + */ + private class Emitter implements Runnable { + private volatile boolean running = true; + + private void output(AsyncCollector collector, StreamElement element) throws Exception { + List result = collector.getResult(); + + // update timestamp for output stream records based on the input stream record. + if (element == null) { + throw new Exception("No input stream element for current AsyncCollector"); + } + + if (element.isRecord()) { + if (result == null) { + throw new Exception("Result for stream record "+element+" is null"); --- End diff -- Maybe `IOException`? --- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastructure@apache.org or file a JIRA ticket with INFRA. ---