All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.flink.streaming.runtime.tasks.StreamIterationTail Maven / Gradle / Ivy

There is a newer version: 1.14.6
Show newest version
/*
 * 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.runtime.tasks;

import org.apache.flink.annotation.Internal;
import org.apache.flink.runtime.execution.Environment;
import org.apache.flink.streaming.api.operators.AbstractStreamOperator;
import org.apache.flink.streaming.api.operators.OneInputStreamOperator;
import org.apache.flink.streaming.api.operators.Output;
import org.apache.flink.streaming.api.watermark.Watermark;
import org.apache.flink.streaming.runtime.io.BlockingQueueBroker;
import org.apache.flink.streaming.runtime.streamrecord.LatencyMarker;
import org.apache.flink.streaming.runtime.streamrecord.StreamRecord;
import org.apache.flink.streaming.runtime.watermarkstatus.WatermarkStatus;
import org.apache.flink.util.OutputTag;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * A special {@link StreamTask} that is used for executing feedback edges. This is used in
 * combination with {@link StreamIterationHead}.
 */
@Internal
public class StreamIterationTail extends OneInputStreamTask {

    private static final Logger LOG = LoggerFactory.getLogger(StreamIterationTail.class);

    public StreamIterationTail(Environment environment) throws Exception {
        super(environment);
    }

    @Override
    public void init() throws Exception {

        final String iterationId = getConfiguration().getIterationId();
        if (iterationId == null || iterationId.length() == 0) {
            throw new Exception("Missing iteration ID in the task configuration");
        }

        final String brokerID =
                StreamIterationHead.createBrokerIdString(
                        getEnvironment().getJobID(),
                        iterationId,
                        getEnvironment().getTaskInfo().getIndexOfThisSubtask());

        final long iterationWaitTime = getConfiguration().getIterationWaitTime();

        LOG.info(
                "Iteration tail {} trying to acquire feedback queue under {}", getName(), brokerID);

        @SuppressWarnings("unchecked")
        BlockingQueue> dataChannel =
                (BlockingQueue>) BlockingQueueBroker.INSTANCE.get(brokerID);

        LOG.info("Iteration tail {} acquired feedback queue {}", getName(), brokerID);

        RecordPusher headOperator = new RecordPusher<>();
        headOperator.setup(
                this,
                getConfiguration(),
                new IterationTailOutput<>(dataChannel, iterationWaitTime));
        this.mainOperator = headOperator;

        // call super.init() last because that needs this.headOperator to be set up
        super.init();
    }

    private static class RecordPusher extends AbstractStreamOperator
            implements OneInputStreamOperator {

        private static final long serialVersionUID = 1L;

        @Override
        public void processElement(StreamRecord record) throws Exception {
            output.collect(record);
        }

        @Override
        public void processWatermark(Watermark mark) {
            // ignore
        }

        @Override
        public void processLatencyMarker(LatencyMarker latencyMarker) throws Exception {
            // ignore
        }
    }

    private static class IterationTailOutput implements Output> {

        @SuppressWarnings("NonSerializableFieldInSerializableClass")
        private final BlockingQueue> dataChannel;

        private final long iterationWaitTime;

        private final boolean shouldWait;

        IterationTailOutput(BlockingQueue> dataChannel, long iterationWaitTime) {
            this.dataChannel = dataChannel;
            this.iterationWaitTime = iterationWaitTime;
            this.shouldWait = iterationWaitTime > 0;
        }

        @Override
        public void emitWatermark(Watermark mark) {}

        @Override
        public void emitWatermarkStatus(WatermarkStatus watermarkStatus) {}

        @Override
        public void emitLatencyMarker(LatencyMarker latencyMarker) {}

        @Override
        public void collect(StreamRecord record) {
            try {
                if (shouldWait) {
                    dataChannel.offer(record, iterationWaitTime, TimeUnit.MILLISECONDS);
                } else {
                    dataChannel.put(record);
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

        @Override
        public  void collect(OutputTag outputTag, StreamRecord record) {
            throw new UnsupportedOperationException("Side outputs not used in iteration tail");
        }

        @Override
        public void close() {}
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy