Search in sources :

Example 6 with StreamingRuntimeContext

use of org.apache.flink.streaming.api.operators.StreamingRuntimeContext in project flink by apache.

the class SpoutWrapperTest method testRunPrepare.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testRunPrepare() throws Exception {
    final StormConfig stormConfig = new StormConfig();
    stormConfig.put(this.r.nextInt(), this.r.nextInt());
    final Configuration flinkConfig = new Configuration();
    flinkConfig.setInteger("testKey", this.r.nextInt());
    final ExecutionConfig taskConfig = mock(ExecutionConfig.class);
    when(taskConfig.getGlobalJobParameters()).thenReturn(null).thenReturn(stormConfig).thenReturn(flinkConfig);
    final StreamingRuntimeContext taskContext = mock(StreamingRuntimeContext.class);
    when(taskContext.getExecutionConfig()).thenReturn(taskConfig);
    when(taskContext.getTaskName()).thenReturn("name");
    final IRichSpout spout = mock(IRichSpout.class);
    SpoutWrapper spoutWrapper = new SpoutWrapper(spout);
    spoutWrapper.setRuntimeContext(taskContext);
    spoutWrapper.cancel();
    // test without configuration
    spoutWrapper.run(mock(SourceContext.class));
    verify(spout).open(any(Map.class), any(TopologyContext.class), any(SpoutOutputCollector.class));
    // test with StormConfig
    spoutWrapper.run(mock(SourceContext.class));
    verify(spout).open(eq(stormConfig), any(TopologyContext.class), any(SpoutOutputCollector.class));
    // test with Configuration
    final TestDummySpout testSpout = new TestDummySpout();
    spoutWrapper = new SpoutWrapper(testSpout);
    spoutWrapper.setRuntimeContext(taskContext);
    spoutWrapper.cancel();
    spoutWrapper.run(mock(SourceContext.class));
    for (Entry<String, String> entry : flinkConfig.toMap().entrySet()) {
        Assert.assertEquals(entry.getValue(), testSpout.config.get(entry.getKey()));
    }
}
Also used : StormConfig(org.apache.flink.storm.util.StormConfig) Configuration(org.apache.flink.configuration.Configuration) StreamingRuntimeContext(org.apache.flink.streaming.api.operators.StreamingRuntimeContext) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) SourceContext(org.apache.flink.streaming.api.functions.source.SourceFunction.SourceContext) TestDummySpout(org.apache.flink.storm.util.TestDummySpout) IRichSpout(org.apache.storm.topology.IRichSpout) SpoutOutputCollector(org.apache.storm.spout.SpoutOutputCollector) TopologyContext(org.apache.storm.task.TopologyContext) Map(java.util.Map) AbstractTest(org.apache.flink.storm.util.AbstractTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 7 with StreamingRuntimeContext

use of org.apache.flink.streaming.api.operators.StreamingRuntimeContext in project flink by apache.

the class SpoutWrapperTest method testRunExecuteFinite.

@Test
public void testRunExecuteFinite() throws Exception {
    final int numberOfCalls = this.r.nextInt(50);
    final LinkedList<Tuple1<Integer>> expectedResult = new LinkedList<Tuple1<Integer>>();
    for (int i = numberOfCalls - 1; i >= 0; --i) {
        expectedResult.add(new Tuple1<Integer>(new Integer(i)));
    }
    final StreamingRuntimeContext taskContext = mock(StreamingRuntimeContext.class);
    when(taskContext.getExecutionConfig()).thenReturn(mock(ExecutionConfig.class));
    when(taskContext.getTaskName()).thenReturn("name");
    final FiniteTestSpout spout = new FiniteTestSpout(numberOfCalls);
    final SpoutWrapper<Tuple1<Integer>> spoutWrapper = new SpoutWrapper<Tuple1<Integer>>(spout, -1);
    spoutWrapper.setRuntimeContext(taskContext);
    final TestContext collector = new TestContext();
    spoutWrapper.run(collector);
    Assert.assertEquals(expectedResult, collector.result);
}
Also used : StreamingRuntimeContext(org.apache.flink.streaming.api.operators.StreamingRuntimeContext) Tuple1(org.apache.flink.api.java.tuple.Tuple1) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) FiniteTestSpout(org.apache.flink.storm.util.FiniteTestSpout) LinkedList(java.util.LinkedList) AbstractTest(org.apache.flink.storm.util.AbstractTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 8 with StreamingRuntimeContext

use of org.apache.flink.streaming.api.operators.StreamingRuntimeContext in project flink by apache.

the class SpoutWrapperTest method testCancel.

@Test
public void testCancel() throws Exception {
    final int numberOfCalls = 5 + this.r.nextInt(5);
    final StreamingRuntimeContext taskContext = mock(StreamingRuntimeContext.class);
    when(taskContext.getExecutionConfig()).thenReturn(mock(ExecutionConfig.class));
    when(taskContext.getTaskName()).thenReturn("name");
    final IRichSpout spout = new FiniteTestSpout(numberOfCalls);
    final SpoutWrapper<Tuple1<Integer>> spoutWrapper = new SpoutWrapper<Tuple1<Integer>>(spout);
    spoutWrapper.setRuntimeContext(taskContext);
    spoutWrapper.cancel();
    final TestContext collector = new TestContext();
    spoutWrapper.run(collector);
    Assert.assertEquals(new LinkedList<Tuple1<Integer>>(), collector.result);
}
Also used : IRichSpout(org.apache.storm.topology.IRichSpout) StreamingRuntimeContext(org.apache.flink.streaming.api.operators.StreamingRuntimeContext) Tuple1(org.apache.flink.api.java.tuple.Tuple1) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) FiniteTestSpout(org.apache.flink.storm.util.FiniteTestSpout) AbstractTest(org.apache.flink.storm.util.AbstractTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 9 with StreamingRuntimeContext

use of org.apache.flink.streaming.api.operators.StreamingRuntimeContext in project flink by apache.

the class SourceFunctionUtil method runSourceFunction.

public static <T extends Serializable> List<T> runSourceFunction(SourceFunction<T> sourceFunction) throws Exception {
    final List<T> outputs = new ArrayList<T>();
    if (sourceFunction instanceof RichFunction) {
        AbstractStreamOperator<?> operator = mock(AbstractStreamOperator.class);
        when(operator.getExecutionConfig()).thenReturn(new ExecutionConfig());
        RuntimeContext runtimeContext = new StreamingRuntimeContext(operator, new MockEnvironment("MockTask", 3 * 1024 * 1024, new MockInputSplitProvider(), 1024), new HashMap<String, Accumulator<?, ?>>());
        ((RichFunction) sourceFunction).setRuntimeContext(runtimeContext);
        ((RichFunction) sourceFunction).open(new Configuration());
    }
    try {
        SourceFunction.SourceContext<T> ctx = new CollectingSourceContext<T>(new Object(), outputs);
        sourceFunction.run(ctx);
    } catch (Exception e) {
        throw new RuntimeException("Cannot invoke source.", e);
    }
    return outputs;
}
Also used : Accumulator(org.apache.flink.api.common.accumulators.Accumulator) SourceFunction(org.apache.flink.streaming.api.functions.source.SourceFunction) StreamingRuntimeContext(org.apache.flink.streaming.api.operators.StreamingRuntimeContext) Configuration(org.apache.flink.configuration.Configuration) RichFunction(org.apache.flink.api.common.functions.RichFunction) ArrayList(java.util.ArrayList) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) MockEnvironment(org.apache.flink.runtime.operators.testutils.MockEnvironment) RuntimeContext(org.apache.flink.api.common.functions.RuntimeContext) StreamingRuntimeContext(org.apache.flink.streaming.api.operators.StreamingRuntimeContext) MockInputSplitProvider(org.apache.flink.runtime.operators.testutils.MockInputSplitProvider)

Example 10 with StreamingRuntimeContext

use of org.apache.flink.streaming.api.operators.StreamingRuntimeContext in project flink by apache.

the class RMQSource method open.

@Override
public void open(Configuration config) throws Exception {
    super.open(config);
    try {
        connection = setupConnection();
        channel = setupChannel(connection);
        if (channel == null) {
            throw new RuntimeException("None of RabbitMQ channels are available");
        }
        setupQueue();
        consumer = new QueueingConsumer(channel);
        RuntimeContext runtimeContext = getRuntimeContext();
        if (runtimeContext instanceof StreamingRuntimeContext && ((StreamingRuntimeContext) runtimeContext).isCheckpointingEnabled()) {
            autoAck = false;
            // enables transaction mode
            channel.txSelect();
        } else {
            autoAck = true;
        }
        LOG.debug("Starting RabbitMQ source with autoAck status: " + autoAck);
        channel.basicConsume(queueName, autoAck, consumer);
    } catch (IOException e) {
        IOUtils.closeAllQuietly(channel, connection);
        throw new RuntimeException("Cannot create RMQ connection with " + queueName + " at " + rmqConnectionConfig.getHost(), e);
    }
    this.deliveryDeserializer.open(RuntimeContextInitializationContextAdapters.deserializationAdapter(getRuntimeContext(), metricGroup -> metricGroup.addGroup("user")));
    running = true;
}
Also used : RuntimeContext(org.apache.flink.api.common.functions.RuntimeContext) IOUtils(org.apache.flink.util.IOUtils) Logger(org.slf4j.Logger) ConnectionFactory(com.rabbitmq.client.ConnectionFactory) Configuration(org.apache.flink.configuration.Configuration) LoggerFactory(org.slf4j.LoggerFactory) ExceptionUtils(org.apache.flink.util.ExceptionUtils) IOException(java.io.IOException) RuntimeContextInitializationContextAdapters(org.apache.flink.api.common.serialization.RuntimeContextInitializationContextAdapters) Connection(com.rabbitmq.client.Connection) Delivery(com.rabbitmq.client.Delivery) Preconditions(org.apache.flink.util.Preconditions) VisibleForTesting(org.apache.flink.annotation.VisibleForTesting) DeserializationSchema(org.apache.flink.api.common.serialization.DeserializationSchema) RMQConnectionConfig(org.apache.flink.streaming.connectors.rabbitmq.common.RMQConnectionConfig) List(java.util.List) MessageAcknowledgingSourceBase(org.apache.flink.streaming.api.functions.source.MessageAcknowledgingSourceBase) Envelope(com.rabbitmq.client.Envelope) ResultTypeQueryable(org.apache.flink.api.java.typeutils.ResultTypeQueryable) TypeInformation(org.apache.flink.api.common.typeinfo.TypeInformation) Channel(com.rabbitmq.client.Channel) StreamingRuntimeContext(org.apache.flink.streaming.api.operators.StreamingRuntimeContext) MultipleIdsMessageAcknowledgingSourceBase(org.apache.flink.streaming.api.functions.source.MultipleIdsMessageAcknowledgingSourceBase) AMQP(com.rabbitmq.client.AMQP) StreamingRuntimeContext(org.apache.flink.streaming.api.operators.StreamingRuntimeContext) IOException(java.io.IOException) RuntimeContext(org.apache.flink.api.common.functions.RuntimeContext) StreamingRuntimeContext(org.apache.flink.streaming.api.operators.StreamingRuntimeContext)

Aggregations

StreamingRuntimeContext (org.apache.flink.streaming.api.operators.StreamingRuntimeContext)28 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)13 Configuration (org.apache.flink.configuration.Configuration)11 Test (org.junit.Test)11 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)10 AbstractTest (org.apache.flink.storm.util.AbstractTest)9 Map (java.util.Map)5 Properties (java.util.Properties)5 RuntimeContext (org.apache.flink.api.common.functions.RuntimeContext)5 StreamConfig (org.apache.flink.streaming.api.graph.StreamConfig)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 VisibleForTesting (org.apache.flink.annotation.VisibleForTesting)4 SourceContext (org.apache.flink.streaming.api.functions.source.SourceFunction.SourceContext)4 IRichBolt (org.apache.storm.topology.IRichBolt)4 HashMap (java.util.HashMap)3 Internal (org.apache.flink.annotation.Internal)3 RuntimeContextInitializationContextAdapters (org.apache.flink.api.common.serialization.RuntimeContextInitializationContextAdapters)3 StormConfig (org.apache.flink.storm.util.StormConfig)3 IRichSpout (org.apache.storm.topology.IRichSpout)3