Search in sources :

Example 1 with SpoutSpec

use of org.apache.storm.generated.SpoutSpec in project storm by apache.

the class TopologySpoutLag method lag.

public static Map<String, Map<String, Object>> lag(StormTopology stormTopology, Map topologyConf) {
    Map<String, Map<String, Object>> result = new HashMap<>();
    Map<String, SpoutSpec> spouts = stormTopology.get_spouts();
    String className = null;
    for (Map.Entry<String, SpoutSpec> spout : spouts.entrySet()) {
        try {
            SpoutSpec spoutSpec = spout.getValue();
            ComponentObject componentObject = spoutSpec.get_spout_object();
            // FIXME: yes it's a trick so we might be better to find alternative way...
            className = getClassNameFromComponentObject(componentObject);
            logger.debug("spout classname: {}", className);
            if (className.endsWith("storm.kafka.spout.KafkaSpout")) {
                result.put(spout.getKey(), getLagResultForNewKafkaSpout(spout.getKey(), spoutSpec, topologyConf));
            } else if (className.endsWith("storm.kafka.KafkaSpout")) {
                result.put(spout.getKey(), getLagResultForOldKafkaSpout(spout.getKey(), spoutSpec, topologyConf));
            }
        } catch (Exception e) {
            logger.warn("Exception thrown while getting lag for spout id: " + spout.getKey() + " and spout class: " + className);
            logger.warn("Exception message:" + e.getMessage(), e);
        }
    }
    return result;
}
Also used : SpoutSpec(org.apache.storm.generated.SpoutSpec) HashMap(java.util.HashMap) ComponentObject(org.apache.storm.generated.ComponentObject) HashMap(java.util.HashMap) Map(java.util.Map) IOException(java.io.IOException) ParseException(org.json.simple.parser.ParseException)

Example 2 with SpoutSpec

use of org.apache.storm.generated.SpoutSpec in project storm by apache.

the class StormCommon method addAcker.

@SuppressWarnings("unchecked")
public static void addAcker(Map conf, StormTopology topology) {
    int ackerNum = Utils.getInt(conf.get(Config.TOPOLOGY_ACKER_EXECUTORS), Utils.getInt(conf.get(Config.TOPOLOGY_WORKERS)));
    Map<GlobalStreamId, Grouping> inputs = ackerInputs(topology);
    Map<String, StreamInfo> outputStreams = new HashMap<String, StreamInfo>();
    outputStreams.put(Acker.ACKER_ACK_STREAM_ID, Thrift.directOutputFields(Arrays.asList("id", "time-delta-ms")));
    outputStreams.put(Acker.ACKER_FAIL_STREAM_ID, Thrift.directOutputFields(Arrays.asList("id", "time-delta-ms")));
    outputStreams.put(Acker.ACKER_RESET_TIMEOUT_STREAM_ID, Thrift.directOutputFields(Arrays.asList("id", "time-delta-ms")));
    Map<String, Object> ackerConf = new HashMap<>();
    ackerConf.put(Config.TOPOLOGY_TASKS, ackerNum);
    ackerConf.put(Config.TOPOLOGY_TICK_TUPLE_FREQ_SECS, Utils.getInt(conf.get(Config.TOPOLOGY_MESSAGE_TIMEOUT_SECS)));
    Bolt acker = Thrift.prepareSerializedBoltDetails(inputs, makeAckerBolt(), outputStreams, ackerNum, ackerConf);
    for (Bolt bolt : topology.get_bolts().values()) {
        ComponentCommon common = bolt.get_common();
        common.put_to_streams(Acker.ACKER_ACK_STREAM_ID, Thrift.outputFields(Arrays.asList("id", "ack-val")));
        common.put_to_streams(Acker.ACKER_FAIL_STREAM_ID, Thrift.outputFields(Arrays.asList("id")));
        common.put_to_streams(Acker.ACKER_RESET_TIMEOUT_STREAM_ID, Thrift.outputFields(Arrays.asList("id")));
    }
    for (SpoutSpec spout : topology.get_spouts().values()) {
        ComponentCommon common = spout.get_common();
        Map spoutConf = componentConf(spout);
        spoutConf.put(Config.TOPOLOGY_TICK_TUPLE_FREQ_SECS, Utils.getInt(conf.get(Config.TOPOLOGY_MESSAGE_TIMEOUT_SECS)));
        common.set_json_conf(JSONValue.toJSONString(spoutConf));
        common.put_to_streams(Acker.ACKER_INIT_STREAM_ID, Thrift.outputFields(Arrays.asList("id", "init-val", "spout-task")));
        common.put_to_inputs(Utils.getGlobalStreamId(Acker.ACKER_COMPONENT_ID, Acker.ACKER_ACK_STREAM_ID), Thrift.prepareDirectGrouping());
        common.put_to_inputs(Utils.getGlobalStreamId(Acker.ACKER_COMPONENT_ID, Acker.ACKER_FAIL_STREAM_ID), Thrift.prepareDirectGrouping());
        common.put_to_inputs(Utils.getGlobalStreamId(Acker.ACKER_COMPONENT_ID, Acker.ACKER_RESET_TIMEOUT_STREAM_ID), Thrift.prepareDirectGrouping());
    }
    topology.put_to_bolts(Acker.ACKER_COMPONENT_ID, acker);
}
Also used : ComponentCommon(org.apache.storm.generated.ComponentCommon) HashMap(java.util.HashMap) Grouping(org.apache.storm.generated.Grouping) Bolt(org.apache.storm.generated.Bolt) MetricsConsumerBolt(org.apache.storm.metric.MetricsConsumerBolt) IBolt(org.apache.storm.task.IBolt) EventLoggerBolt(org.apache.storm.metric.EventLoggerBolt) SystemBolt(org.apache.storm.metric.SystemBolt) StateSpoutSpec(org.apache.storm.generated.StateSpoutSpec) SpoutSpec(org.apache.storm.generated.SpoutSpec) GlobalStreamId(org.apache.storm.generated.GlobalStreamId) StreamInfo(org.apache.storm.generated.StreamInfo) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 3 with SpoutSpec

use of org.apache.storm.generated.SpoutSpec in project atlas by apache.

the class StormAtlasHook method addSpouts.

private void addSpouts(Map<String, SpoutSpec> spouts, Map<String, Referenceable> nodeEntities) {
    for (Map.Entry<String, SpoutSpec> entry : spouts.entrySet()) {
        final String spoutName = entry.getKey();
        Referenceable spoutReferenceable = createSpoutInstance(spoutName, entry.getValue());
        nodeEntities.put(spoutName, spoutReferenceable);
    }
}
Also used : SpoutSpec(org.apache.storm.generated.SpoutSpec) Referenceable(org.apache.atlas.v1.model.instance.Referenceable) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with SpoutSpec

use of org.apache.storm.generated.SpoutSpec in project atlas by apache.

the class StormAtlasHook method addTopologyInputs.

private void addTopologyInputs(Referenceable topologyReferenceable, Map<String, SpoutSpec> spouts, Map stormConf, String topologyOwner, List<Referenceable> dependentEntities) {
    final ArrayList<Referenceable> inputDataSets = new ArrayList<>();
    for (Map.Entry<String, SpoutSpec> entry : spouts.entrySet()) {
        Serializable instance = Utils.javaDeserialize(entry.getValue().get_spout_object().get_serialized_java(), Serializable.class);
        String simpleName = instance.getClass().getSimpleName();
        final Referenceable datasetRef = createDataSet(simpleName, topologyOwner, instance, stormConf, dependentEntities);
        if (datasetRef != null) {
            inputDataSets.add(datasetRef);
        }
    }
    topologyReferenceable.set("inputs", inputDataSets);
}
Also used : Serializable(java.io.Serializable) Referenceable(org.apache.atlas.v1.model.instance.Referenceable) SpoutSpec(org.apache.storm.generated.SpoutSpec) ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with SpoutSpec

use of org.apache.storm.generated.SpoutSpec in project storm by apache.

the class Testing method completeTopology.

/**
 * Run a topology to completion capturing all of the messages that are emitted.  This only works when all of the spouts are
 * instances of {@link org.apache.storm.testing.CompletableSpout} or are overwritten by MockedSources in param
 * @param cluster the cluster to submit the topology to
 * @param topology the topology itself
 * @param param parameters to describe how to complete a topology
 * @return a map of the component to the list of tuples it emitted
 * @throws TException on any error from nimbus.
 */
public static Map<String, List<FixedTuple>> completeTopology(ILocalCluster cluster, StormTopology topology, CompleteTopologyParam param) throws TException, InterruptedException {
    Map<String, List<FixedTuple>> ret = null;
    CapturedTopology<StormTopology> capTopo = captureTopology(topology);
    topology = capTopo.topology;
    String topoName = param.getTopologyName();
    if (topoName == null) {
        topoName = "topologytest-" + Utils.uuid();
    }
    Map<String, SpoutSpec> spouts = topology.get_spouts();
    MockedSources ms = param.getMockedSources();
    if (ms != null) {
        for (Entry<String, List<FixedTuple>> mocked : ms.getData().entrySet()) {
            FixedTupleSpout newSpout = new FixedTupleSpout(mocked.getValue());
            spouts.get(mocked.getKey()).set_spout_object(Thrift.serializeComponentObject(newSpout));
        }
    }
    List<Object> spoutObjects = spouts.values().stream().map((spec) -> Thrift.deserializeComponentObject(spec.get_spout_object())).collect(Collectors.toList());
    for (Object o : spoutObjects) {
        if (!(o instanceof CompletableSpout)) {
            throw new RuntimeException("Cannot complete topology unless every spout is a CompletableSpout (or mocked to be); failed by " + o);
        }
    }
    for (Object spout : spoutObjects) {
        ((CompletableSpout) spout).startup();
    }
    cluster.submitTopology(topoName, param.getStormConf(), topology);
    if (Time.isSimulating()) {
        cluster.advanceClusterTime(11);
    }
    IStormClusterState state = cluster.getClusterState();
    String topoId = state.getTopoId(topoName).get();
    // Give the topology time to come up without using it to wait for the spouts to complete
    simulateWait(cluster);
    Integer timeoutMs = param.getTimeoutMs();
    if (timeoutMs == null) {
        timeoutMs = TEST_TIMEOUT_MS;
    }
    whileTimeout(timeoutMs, () -> !isEvery(spoutObjects, (o) -> ((CompletableSpout) o).isExhausted()), () -> {
        try {
            simulateWait(cluster);
        } catch (Exception e) {
            throw new RuntimeException();
        }
    });
    KillOptions killOpts = new KillOptions();
    killOpts.set_wait_secs(0);
    cluster.killTopologyWithOpts(topoName, killOpts);
    whileTimeout(timeoutMs, () -> state.assignmentInfo(topoId, null) != null, () -> {
        try {
            simulateWait(cluster);
        } catch (Exception e) {
            throw new RuntimeException();
        }
    });
    if (param.getCleanupState()) {
        for (Object o : spoutObjects) {
            ((CompletableSpout) o).clean();
        }
        ret = capTopo.capturer.getAndRemoveResults();
    } else {
        ret = capTopo.capturer.getAndClearResults();
    }
    return ret;
}
Also used : SimulatedTime(org.apache.storm.utils.Time.SimulatedTime) TopologyContext(org.apache.storm.task.TopologyContext) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) INimbus(org.apache.storm.scheduler.INimbus) ArrayList(java.util.ArrayList) Bolt(org.apache.storm.generated.Bolt) Tuple(org.apache.storm.tuple.Tuple) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StormTopology(org.apache.storm.generated.StormTopology) Map(java.util.Map) CompleteTopologyParam(org.apache.storm.testing.CompleteTopologyParam) MkTupleParam(org.apache.storm.testing.MkTupleParam) FixedTupleSpout(org.apache.storm.testing.FixedTupleSpout) KillOptions(org.apache.storm.generated.KillOptions) MockedSources(org.apache.storm.testing.MockedSources) TestJob(org.apache.storm.testing.TestJob) MkClusterParam(org.apache.storm.testing.MkClusterParam) Logger(org.slf4j.Logger) Predicate(java.util.function.Predicate) Collection(java.util.Collection) CompletableSpout(org.apache.storm.testing.CompletableSpout) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Grouping(org.apache.storm.generated.Grouping) StreamInfo(org.apache.storm.generated.StreamInfo) FixedTuple(org.apache.storm.testing.FixedTuple) Fields(org.apache.storm.tuple.Fields) IStormClusterState(org.apache.storm.cluster.IStormClusterState) Utils(org.apache.storm.utils.Utils) Collectors(java.util.stream.Collectors) TException(org.apache.storm.thrift.TException) GlobalStreamId(org.apache.storm.generated.GlobalStreamId) Time(org.apache.storm.utils.Time) TupleCaptureBolt(org.apache.storm.testing.TupleCaptureBolt) List(java.util.List) RegisteredGlobalState(org.apache.storm.utils.RegisteredGlobalState) ConfigUtils(org.apache.storm.utils.ConfigUtils) TupleImpl(org.apache.storm.tuple.TupleImpl) SpoutSpec(org.apache.storm.generated.SpoutSpec) Entry(java.util.Map.Entry) TrackedTopology(org.apache.storm.testing.TrackedTopology) StormTopology(org.apache.storm.generated.StormTopology) CompletableSpout(org.apache.storm.testing.CompletableSpout) TException(org.apache.storm.thrift.TException) FixedTupleSpout(org.apache.storm.testing.FixedTupleSpout) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MockedSources(org.apache.storm.testing.MockedSources) SpoutSpec(org.apache.storm.generated.SpoutSpec) ArrayList(java.util.ArrayList) List(java.util.List) IStormClusterState(org.apache.storm.cluster.IStormClusterState) KillOptions(org.apache.storm.generated.KillOptions)

Aggregations

SpoutSpec (org.apache.storm.generated.SpoutSpec)32 HashMap (java.util.HashMap)25 Bolt (org.apache.storm.generated.Bolt)24 Map (java.util.Map)20 StormTopology (org.apache.storm.generated.StormTopology)13 GlobalStreamId (org.apache.storm.generated.GlobalStreamId)8 Grouping (org.apache.storm.generated.Grouping)7 StateSpoutSpec (org.apache.storm.generated.StateSpoutSpec)7 ArrayList (java.util.ArrayList)6 ComponentCommon (org.apache.storm.generated.ComponentCommon)6 StreamInfo (org.apache.storm.generated.StreamInfo)5 List (java.util.List)4 IOException (java.io.IOException)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 NormalizedResourceRequest (org.apache.storm.scheduler.resource.normalization.NormalizedResourceRequest)3 BaseRichBolt (org.apache.storm.topology.base.BaseRichBolt)3 Fields (org.apache.storm.tuple.Fields)3 Serializable (java.io.Serializable)2 TreeMap (java.util.TreeMap)2 Collectors (java.util.stream.Collectors)2