Class TopologyContext

All Implemented Interfaces:
org.apache.storm.shade.net.minidev.json.JSONAware, IMetricsContext

public class TopologyContext extends WorkerTopologyContext implements IMetricsContext
A `TopologyContext` is given to bolts and spouts in their `prepare()` and `open()` methods, respectively. This object provides information about the component's place within the topology, such as task ids, inputs and outputs, etc.

The `TopologyContext` is also used to declare `ISubscribedState` objects to synchronize state with StateSpouts this object is subscribed to.

  • Constructor Details

  • Method Details

    • setAllSubscribedState

      public <T extends ISubscribedState> T setAllSubscribedState(T obj)
      All state from all subscribed state spouts streams will be synced with the provided object.

      It is recommended that your ISubscribedState object is kept as an instance variable of this object. The recommended usage of this method is as follows:

      ```java _myState = context.setAllSubscribedState(new MyState()); ```

      Parameters:
      obj - Provided ISubscribedState implementation
      Returns:
      Returns the ISubscribedState object provided
    • setSubscribedState

      public <T extends ISubscribedState> T setSubscribedState(String componentId, T obj)
      Synchronizes the default stream from the specified state spout component id with the provided ISubscribedState object.

      The recommended usage of this method is as follows:

      ```java _myState = context.setSubscribedState(componentId, new MyState()); ```

      Parameters:
      componentId - the id of the StateSpout component to subscribe to
      obj - Provided ISubscribedState implementation
      Returns:
      Returns the ISubscribedState object provided
    • setSubscribedState

      public <T extends ISubscribedState> T setSubscribedState(String componentId, String streamId, T obj)
      Synchronizes the specified stream from the specified state spout component id with the provided ISubscribedState object.

      The recommended usage of this method is as follows:

      ```java _myState = context.setSubscribedState(componentId, streamId, new MyState()); ```

      Parameters:
      componentId - the id of the StateSpout component to subscribe to
      streamId - the stream to subscribe to
      obj - Provided ISubscribedState implementation
      Returns:
      Returns the ISubscribedState object provided
    • getBlobToLastKnownVersion

      public Map<String,Long> getBlobToLastKnownVersion()
    • getThisTaskId

      public int getThisTaskId()
      Gets the task id of this task.
      Returns:
      the task id
    • getThisComponentId

      public String getThisComponentId()
      Get component id.
      Returns:
      the component id for this task. The component id maps to a component id specified for a Spout or Bolt in the topology definition.
    • getThisOutputFields

      public Fields getThisOutputFields(String streamId)
      Gets the declared output fields for the specified stream id for the component this task is a part of.
    • getThisOutputFieldsForStreams

      public Map<String,List<String>> getThisOutputFieldsForStreams()
      Gets the declared output fields for all streams for the component this task is a part of.
    • getThisStreams

      public Set<String> getThisStreams()
      Gets the set of streams declared for the component of this task.
    • getThisTaskIndex

      public int getThisTaskIndex()
      Gets the index of this task id in getComponentTasks(getThisComponentId()). An example use case for this method is determining which task accesses which resource in a distributed resource to ensure an even distribution.
    • getThisInputFields

      public Map<String,Map<String,List<String>>> getThisInputFields()
      Gets the declared input fields for this component.
      Returns:
      A map from sources to streams to fields.
    • getThisSources

      public Map<GlobalStreamId,Grouping> getThisSources()
      Gets the declared inputs to this component.
      Returns:
      A map from subscribed component/stream to the grouping subscribed with.
    • getThisTargets

      public Map<String,Map<String,Grouping>> getThisTargets()
      Gets information about who is consuming the outputs of this component, and how.
      Returns:
      Map from stream id to component id to the Grouping used.
    • setTaskData

      public void setTaskData(String name, Object data)
      Sets the task-level data for the given name. This data is shared amongst the task and its corresponding task hooks.
      Parameters:
      name - name of the task-level data to be set
      data - task-level data
    • getTaskData

      public Object getTaskData(String name)
      Fetches the task-level data for the given name. This data is shared amongst the task and its corresponding task hooks.
      Parameters:
      name - name of the task-level data to be fetched
      Returns:
      Associated task-level data
    • setExecutorData

      public void setExecutorData(String name, Object data)
      Sets the executor-level data for the given name. This data is shared amongst tasks and corresponding task hooks managed by the given executor.
      Parameters:
      name - name of the executor-level data to be set
      data - executor-level data
    • getExecutorData

      public Object getExecutorData(String name)
      Fetches the executor-level data for the given name. This data is shared across tasks and corresponding task hook managed by the given executor.
      Parameters:
      name - name of the executor-level data to be fetched
      Returns:
      Associated executor-level data
    • addTaskHook

      public void addTaskHook(ITaskHook hook)
    • getHooks

      public List<ITaskHook> getHooks()
    • toJSONString

      public String toJSONString()
      Specified by:
      toJSONString in interface org.apache.storm.shade.net.minidev.json.JSONAware
      Overrides:
      toJSONString in class GeneralTopologyContext
    • registerMetric

      @Deprecated public <T extends IMetric> T registerMetric(String name, T metric, int timeBucketSizeInSecs)
      Deprecated.
      Description copied from interface: IMetricsContext
      Register metric.
      Specified by:
      registerMetric in interface IMetricsContext
    • registerMetric

      @Deprecated public ReducedMetric registerMetric(String name, IReducer reducer, int timeBucketSizeInSecs)
      Deprecated.
      Description copied from interface: IMetricsContext
      Register metric.
      Specified by:
      registerMetric in interface IMetricsContext
    • registerMetric

      @Deprecated public CombinedMetric registerMetric(String name, ICombiner combiner, int timeBucketSizeInSecs)
      Deprecated.
      Description copied from interface: IMetricsContext
      Register metric.
      Specified by:
      registerMetric in interface IMetricsContext
    • getRegisteredMetricByName

      @Deprecated public IMetric getRegisteredMetricByName(String name)
      Deprecated.
      Get component's metric from registered metrics by name. Notice: Normally, one component can only register one metric name once. But now registerMetric has a bug(https://issues.apache.org/jira/browse/STORM-254) cause the same metric name can register twice. So we just return the first metric we meet.
    • registerTimer

      public com.codahale.metrics.Timer registerTimer(String name)
      Specified by:
      registerTimer in interface IMetricsContext
    • registerHistogram

      public com.codahale.metrics.Histogram registerHistogram(String name)
      Specified by:
      registerHistogram in interface IMetricsContext
    • registerMeter

      public com.codahale.metrics.Meter registerMeter(String name)
      Specified by:
      registerMeter in interface IMetricsContext
    • registerCounter

      public com.codahale.metrics.Counter registerCounter(String name)
      Specified by:
      registerCounter in interface IMetricsContext
    • registerGauge

      public <T> com.codahale.metrics.Gauge<T> registerGauge(String name, com.codahale.metrics.Gauge<T> gauge)
      Specified by:
      registerGauge in interface IMetricsContext
    • registerMetricSet

      public void registerMetricSet(String prefix, com.codahale.metrics.MetricSet set)
      Specified by:
      registerMetricSet in interface IMetricsContext