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

org.apache.flink.streaming.api.functions.async.RichAsyncFunction 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.api.functions.async;

import org.apache.flink.annotation.PublicEvolving;
import org.apache.flink.api.common.ExecutionConfig;
import org.apache.flink.api.common.accumulators.Accumulator;
import org.apache.flink.api.common.accumulators.DoubleCounter;
import org.apache.flink.api.common.accumulators.Histogram;
import org.apache.flink.api.common.accumulators.IntCounter;
import org.apache.flink.api.common.accumulators.LongCounter;
import org.apache.flink.api.common.aggregators.Aggregator;
import org.apache.flink.api.common.cache.DistributedCache;
import org.apache.flink.api.common.functions.AbstractRichFunction;
import org.apache.flink.api.common.functions.BroadcastVariableInitializer;
import org.apache.flink.api.common.functions.IterationRuntimeContext;
import org.apache.flink.api.common.functions.RichFunction;
import org.apache.flink.api.common.functions.RuntimeContext;
import org.apache.flink.api.common.state.AggregatingState;
import org.apache.flink.api.common.state.AggregatingStateDescriptor;
import org.apache.flink.api.common.state.FoldingState;
import org.apache.flink.api.common.state.FoldingStateDescriptor;
import org.apache.flink.api.common.state.ListState;
import org.apache.flink.api.common.state.ListStateDescriptor;
import org.apache.flink.api.common.state.MapState;
import org.apache.flink.api.common.state.MapStateDescriptor;
import org.apache.flink.api.common.state.ReducingState;
import org.apache.flink.api.common.state.ReducingStateDescriptor;
import org.apache.flink.api.common.state.ValueState;
import org.apache.flink.api.common.state.ValueStateDescriptor;
import org.apache.flink.metrics.MetricGroup;
import org.apache.flink.types.Value;
import org.apache.flink.util.Preconditions;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * Rich variant of the {@link AsyncFunction}. As a {@link RichFunction}, it gives access to the
 * {@link RuntimeContext} and provides setup and teardown methods:
 * {@link RichFunction#open(org.apache.flink.configuration.Configuration)} and
 * {@link RichFunction#close()}.
 *
 * 

State related apis in {@link RuntimeContext} are not supported yet because the key may get * changed while accessing states in the working thread. * *

{@link IterationRuntimeContext#getIterationAggregator(String)} is not supported since the * aggregator may be modified by multiple threads. * * @param The type of the input elements. * @param The type of the returned elements. */ @PublicEvolving public abstract class RichAsyncFunction extends AbstractRichFunction implements AsyncFunction { private static final long serialVersionUID = 3858030061138121840L; @Override public void setRuntimeContext(RuntimeContext runtimeContext) { Preconditions.checkNotNull(runtimeContext); if (runtimeContext instanceof IterationRuntimeContext) { super.setRuntimeContext( new RichAsyncFunctionIterationRuntimeContext( (IterationRuntimeContext) runtimeContext)); } else { super.setRuntimeContext(new RichAsyncFunctionRuntimeContext(runtimeContext)); } } @Override public abstract void asyncInvoke(IN input, ResultFuture resultFuture) throws Exception; // ----------------------------------------------------------------------------------------- // Wrapper classes // ----------------------------------------------------------------------------------------- /** * A wrapper class for async function's {@link RuntimeContext}. The async function runtime * context only supports basic operations which are thread safe. Consequently, state access, * accumulators, broadcast variables and the distributed cache are disabled. */ private static class RichAsyncFunctionRuntimeContext implements RuntimeContext { private final RuntimeContext runtimeContext; RichAsyncFunctionRuntimeContext(RuntimeContext context) { runtimeContext = Preconditions.checkNotNull(context); } @Override public String getTaskName() { return runtimeContext.getTaskName(); } @Override public MetricGroup getMetricGroup() { return runtimeContext.getMetricGroup(); } @Override public int getNumberOfParallelSubtasks() { return runtimeContext.getNumberOfParallelSubtasks(); } @Override public int getMaxNumberOfParallelSubtasks() { return runtimeContext.getMaxNumberOfParallelSubtasks(); } @Override public int getIndexOfThisSubtask() { return runtimeContext.getIndexOfThisSubtask(); } @Override public int getAttemptNumber() { return runtimeContext.getAttemptNumber(); } @Override public String getTaskNameWithSubtasks() { return runtimeContext.getTaskNameWithSubtasks(); } @Override public ExecutionConfig getExecutionConfig() { return runtimeContext.getExecutionConfig(); } @Override public ClassLoader getUserCodeClassLoader() { return runtimeContext.getUserCodeClassLoader(); } // ----------------------------------------------------------------------------------- // Unsupported operations // ----------------------------------------------------------------------------------- @Override public DistributedCache getDistributedCache() { throw new UnsupportedOperationException("Distributed cache is not supported in rich async functions."); } @Override public ValueState getState(ValueStateDescriptor stateProperties) { throw new UnsupportedOperationException("State is not supported in rich async functions."); } @Override public ListState getListState(ListStateDescriptor stateProperties) { throw new UnsupportedOperationException("State is not supported in rich async functions."); } @Override public ReducingState getReducingState(ReducingStateDescriptor stateProperties) { throw new UnsupportedOperationException("State is not supported in rich async functions."); } @Override public AggregatingState getAggregatingState(AggregatingStateDescriptor stateProperties) { throw new UnsupportedOperationException("State is not supported in rich async functions."); } @Override public FoldingState getFoldingState(FoldingStateDescriptor stateProperties) { throw new UnsupportedOperationException("State is not supported in rich async functions."); } @Override public MapState getMapState(MapStateDescriptor stateProperties) { throw new UnsupportedOperationException("State is not supported in rich async functions."); } @Override public void addAccumulator(String name, Accumulator accumulator) { throw new UnsupportedOperationException("Accumulators are not supported in rich async functions."); } @Override public Accumulator getAccumulator(String name) { throw new UnsupportedOperationException("Accumulators are not supported in rich async functions."); } @Override public Map> getAllAccumulators() { throw new UnsupportedOperationException("Accumulators are not supported in rich async functions."); } @Override public IntCounter getIntCounter(String name) { throw new UnsupportedOperationException("Int counters are not supported in rich async functions."); } @Override public LongCounter getLongCounter(String name) { throw new UnsupportedOperationException("Long counters are not supported in rich async functions."); } @Override public DoubleCounter getDoubleCounter(String name) { throw new UnsupportedOperationException("Long counters are not supported in rich async functions."); } @Override public Histogram getHistogram(String name) { throw new UnsupportedOperationException("Histograms are not supported in rich async functions."); } @Override public boolean hasBroadcastVariable(String name) { throw new UnsupportedOperationException("Broadcast variables are not supported in rich async functions."); } @Override public List getBroadcastVariable(String name) { throw new UnsupportedOperationException("Broadcast variables are not supported in rich async functions."); } @Override public C getBroadcastVariableWithInitializer(String name, BroadcastVariableInitializer initializer) { throw new UnsupportedOperationException("Broadcast variables are not supported in rich async functions."); } } private static class RichAsyncFunctionIterationRuntimeContext extends RichAsyncFunctionRuntimeContext implements IterationRuntimeContext { private final IterationRuntimeContext iterationRuntimeContext; RichAsyncFunctionIterationRuntimeContext(IterationRuntimeContext iterationRuntimeContext) { super(iterationRuntimeContext); this.iterationRuntimeContext = Preconditions.checkNotNull(iterationRuntimeContext); } @Override public int getSuperstepNumber() { return iterationRuntimeContext.getSuperstepNumber(); } // ----------------------------------------------------------------------------------- // Unsupported operations // ----------------------------------------------------------------------------------- @Override public > T getIterationAggregator(String name) { throw new UnsupportedOperationException("Iteration aggregators are not supported in rich async functions."); } @Override public T getPreviousIterationAggregate(String name) { throw new UnsupportedOperationException("Iteration aggregators are not supported in rich async functions."); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy