Search in sources :

Example 11 with OutputCollector

use of org.apache.storm.task.OutputCollector in project storm by apache.

the class RollingCountBoltTest method shouldEmitNothingIfNoObjectHasBeenCountedYetAndTickTupleIsReceived.

@SuppressWarnings("rawtypes")
@Test
public void shouldEmitNothingIfNoObjectHasBeenCountedYetAndTickTupleIsReceived() {
    // given
    Tuple tickTuple = MockTupleHelpers.mockTickTuple();
    RollingCountBolt bolt = new RollingCountBolt();
    Map<String, Object> conf = mock(Map.class);
    TopologyContext context = mock(TopologyContext.class);
    OutputCollector collector = mock(OutputCollector.class);
    bolt.prepare(conf, context, collector);
    // when
    bolt.execute(tickTuple);
    // then
    verifyZeroInteractions(collector);
}
Also used : OutputCollector(org.apache.storm.task.OutputCollector) TopologyContext(org.apache.storm.task.TopologyContext) Tuple(org.apache.storm.tuple.Tuple) Test(org.testng.annotations.Test)

Example 12 with OutputCollector

use of org.apache.storm.task.OutputCollector in project storm by apache.

the class CoordinatedBolt method prepare.

@Override
public void prepare(Map<String, Object> config, TopologyContext context, OutputCollector collector) {
    TimeCacheMap.ExpiredCallback<Object, TrackingInfo> callback = null;
    if (delegate instanceof TimeoutCallback) {
        callback = new TimeoutItems();
    }
    tracked = new TimeCacheMap<>(context.maxTopologyMessageTimeout(), callback);
    this.collector = collector;
    delegate.prepare(config, context, new OutputCollector(new CoordinatedOutputCollector(collector)));
    for (String component : Utils.get(context.getThisTargets(), Constants.COORDINATED_STREAM_ID, new HashMap<String, Grouping>()).keySet()) {
        for (Integer task : context.getComponentTasks(component)) {
            countOutTasks.add(task);
        }
    }
    if (!sourceArgs.isEmpty()) {
        numSourceReports = 0;
        for (Entry<String, SourceArgs> entry : sourceArgs.entrySet()) {
            if (entry.getValue().singleCount) {
                numSourceReports += 1;
            } else {
                numSourceReports += context.getComponentTasks(entry.getKey()).size();
            }
        }
    }
}
Also used : OutputCollector(org.apache.storm.task.OutputCollector) IOutputCollector(org.apache.storm.task.IOutputCollector) HashMap(java.util.HashMap) TimeCacheMap(org.apache.storm.utils.TimeCacheMap)

Example 13 with OutputCollector

use of org.apache.storm.task.OutputCollector in project storm by apache.

the class KafkaBoltTest method testCustomCallbackIsWrappedByDefaultCallbackBehavior.

@Test
public void testCustomCallbackIsWrappedByDefaultCallbackBehavior() {
    MockProducer<String, String> producer = new MockProducer<>(Cluster.empty(), false, null, null, null);
    KafkaBolt<String, String> bolt = makeBolt(producer);
    PreparableCallback customCallback = mock(PreparableCallback.class);
    bolt.withProducerCallback(customCallback);
    OutputCollector collector = mock(OutputCollector.class);
    TopologyContext context = mock(TopologyContext.class);
    Map<String, Object> topoConfig = new HashMap<>();
    bolt.prepare(topoConfig, context, collector);
    verify(customCallback).prepare(topoConfig, context);
    String key = "KEY";
    String value = "VALUE";
    Tuple testTuple = createTestTuple(key, value);
    bolt.execute(testTuple);
    assertThat(producer.history().size(), is(1));
    ProducerRecord<String, String> arg = producer.history().get(0);
    LOG.info("GOT {} ->", arg);
    LOG.info("{}, {}, {}", arg.topic(), arg.key(), arg.value());
    assertThat(arg.topic(), is("MY_TOPIC"));
    assertThat(arg.key(), is(key));
    assertThat(arg.value(), is(value));
    // Force a send error
    KafkaException ex = new KafkaException();
    producer.errorNext(ex);
    verify(customCallback).onCompletion(any(), eq(ex));
    verify(collector).reportError(ex);
    verify(collector).fail(testTuple);
}
Also used : OutputCollector(org.apache.storm.task.OutputCollector) MockProducer(org.apache.kafka.clients.producer.MockProducer) HashMap(java.util.HashMap) KafkaException(org.apache.kafka.common.KafkaException) TopologyContext(org.apache.storm.task.TopologyContext) Tuple(org.apache.storm.tuple.Tuple) Test(org.junit.Test)

Example 14 with OutputCollector

use of org.apache.storm.task.OutputCollector in project storm by apache.

the class KafkaBoltTest method testSimpleWithError.

@Test
public void testSimpleWithError() {
    MockProducer<String, String> producer = new MockProducer<>(Cluster.empty(), false, null, null, null);
    KafkaBolt<String, String> bolt = makeBolt(producer);
    OutputCollector collector = mock(OutputCollector.class);
    TopologyContext context = mock(TopologyContext.class);
    Map<String, Object> conf = new HashMap<>();
    bolt.prepare(conf, context, collector);
    String key = "KEY";
    String value = "VALUE";
    Tuple testTuple = createTestTuple(key, value);
    bolt.execute(testTuple);
    assertThat(producer.history().size(), is(1));
    ProducerRecord<String, String> arg = producer.history().get(0);
    LOG.info("GOT {} ->", arg);
    LOG.info("{}, {}, {}", arg.topic(), arg.key(), arg.value());
    assertThat(arg.topic(), is("MY_TOPIC"));
    assertThat(arg.key(), is(key));
    assertThat(arg.value(), is(value));
    // Force a send error
    KafkaException ex = new KafkaException();
    producer.errorNext(ex);
    verify(collector).reportError(ex);
    verify(collector).fail(testTuple);
}
Also used : OutputCollector(org.apache.storm.task.OutputCollector) MockProducer(org.apache.kafka.clients.producer.MockProducer) HashMap(java.util.HashMap) KafkaException(org.apache.kafka.common.KafkaException) TopologyContext(org.apache.storm.task.TopologyContext) Tuple(org.apache.storm.tuple.Tuple) Test(org.junit.Test)

Example 15 with OutputCollector

use of org.apache.storm.task.OutputCollector in project storm by apache.

the class BoltExecutor method init.

public void init(ArrayList<Task> idToTask, int idToTaskBase) throws InterruptedException {
    executorTransfer.initLocalRecvQueues();
    workerReady.await();
    while (!stormActive.get()) {
        // Topology may be deployed in deactivated mode, wait for activation
        Utils.sleepNoSimulation(100);
    }
    LOG.info("Preparing bolt {}:{}", componentId, getTaskIds());
    for (Task taskData : idToTask) {
        if (taskData == null) {
            // This happens if the min id is too small
            continue;
        }
        IBolt boltObject = (IBolt) taskData.getTaskObject();
        TopologyContext userContext = taskData.getUserContext();
        if (boltObject instanceof ICredentialsListener) {
            ((ICredentialsListener) boltObject).setCredentials(credentials);
        }
        if (Constants.SYSTEM_COMPONENT_ID.equals(componentId)) {
            BuiltinMetricsUtil.registerIconnectionServerMetric(workerData.getReceiver(), topoConf, userContext);
            // add any autocredential expiry metrics from the worker
            if (workerData.getAutoCredentials() != null) {
                for (IAutoCredentials autoCredential : workerData.getAutoCredentials()) {
                    if (autoCredential instanceof IMetricsRegistrant) {
                        IMetricsRegistrant registrant = (IMetricsRegistrant) autoCredential;
                        registrant.registerMetrics(userContext, topoConf);
                    }
                }
            }
        }
        this.outputCollector = new BoltOutputCollectorImpl(this, taskData, rand, hasEventLoggers, ackingEnabled, isDebug);
        boltObject.prepare(topoConf, userContext, new OutputCollector(outputCollector));
    }
    openOrPrepareWasCalled.set(true);
    LOG.info("Prepared bolt {}:{}", componentId, taskIds);
    setupTicks(false);
    setupMetrics();
}
Also used : OutputCollector(org.apache.storm.task.OutputCollector) Task(org.apache.storm.daemon.Task) ICredentialsListener(org.apache.storm.ICredentialsListener) IAutoCredentials(org.apache.storm.security.auth.IAutoCredentials) IMetricsRegistrant(org.apache.storm.metric.api.IMetricsRegistrant) IBolt(org.apache.storm.task.IBolt) TopologyContext(org.apache.storm.task.TopologyContext)

Aggregations

OutputCollector (org.apache.storm.task.OutputCollector)38 Tuple (org.apache.storm.tuple.Tuple)21 TopologyContext (org.apache.storm.task.TopologyContext)20 Test (org.junit.Test)19 HashMap (java.util.HashMap)16 Fields (org.apache.storm.tuple.Fields)11 Map (java.util.Map)9 IOutputCollector (org.apache.storm.task.IOutputCollector)8 HiveOptions (org.apache.storm.hive.common.HiveOptions)6 GlobalStreamId (org.apache.storm.generated.GlobalStreamId)5 Grouping (org.apache.storm.generated.Grouping)5 Before (org.junit.Before)5 Collections (java.util.Collections)4 Bolt (org.apache.storm.generated.Bolt)4 NullStruct (org.apache.storm.generated.NullStruct)4 SpoutSpec (org.apache.storm.generated.SpoutSpec)4 StormTopology (org.apache.storm.generated.StormTopology)4 JsonRecordHiveMapper (org.apache.storm.hive.bolt.mapper.JsonRecordHiveMapper)4 SpoutOutputCollector (org.apache.storm.spout.SpoutOutputCollector)4 Count (org.apache.storm.streams.operations.aggregators.Count)4