Package org.apache.storm.task
Class TopologyContext
- java.lang.Object
-
- org.apache.storm.task.GeneralTopologyContext
-
- org.apache.storm.task.WorkerTopologyContext
-
- org.apache.storm.task.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.
-
-
Field Summary
-
Fields inherited from class org.apache.storm.task.WorkerTopologyContext
SHARED_EXECUTOR
-
Fields inherited from class org.apache.storm.task.GeneralTopologyContext
doSanityCheck, topoConf
-
-
Constructor Summary
Constructors Constructor Description TopologyContext(StormTopology topology, Map<String,Object> topoConf, Map<Integer,String> taskToComponent, Map<String,List<Integer>> componentToSortedTasks, Map<String,Map<String,Fields>> componentToStreamToFields, Map<String,Long> blobToLastKnownVersionShared, String stormId, String codeDir, String pidDir, Integer taskId, Integer workerPort, List<Integer> workerTasks, Map<String,Object> defaultResources, Map<String,Object> userResources, Map<String,Object> executorData, Map<Integer,Map<Integer,Map<String,IMetric>>> registeredMetrics, AtomicBoolean openOrPrepareWasCalled, StormMetricRegistry metricRegistry)
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description voidaddTaskHook(ITaskHook hook)Map<String,Long>getBlobToLastKnownVersion()ObjectgetExecutorData(String name)Fetches the executor-level data for the given name.List<ITaskHook>getHooks()IMetricgetRegisteredMetricByName(String name)Deprecated.ObjectgetTaskData(String name)Fetches the task-level data for the given name.StringgetThisComponentId()Get component id.Map<String,Map<String,List<String>>>getThisInputFields()Gets the declared input fields for this component.FieldsgetThisOutputFields(String streamId)Gets the declared output fields for the specified stream id for the component this task is a part of.Map<String,List<String>>getThisOutputFieldsForStreams()Gets the declared output fields for all streams for the component this task is a part of.Map<GlobalStreamId,Grouping>getThisSources()Gets the declared inputs to this component.Set<String>getThisStreams()Gets the set of streams declared for the component of this task.Map<String,Map<String,Grouping>>getThisTargets()Gets information about who is consuming the outputs of this component, and how.intgetThisTaskId()Gets the task id of this task.intgetThisTaskIndex()Gets the index of this task id in getComponentTasks(getThisComponentId()).com.codahale.metrics.CounterregisterCounter(String name)<T> com.codahale.metrics.Gauge<T>registerGauge(String name, com.codahale.metrics.Gauge<T> gauge)com.codahale.metrics.HistogramregisterHistogram(String name)com.codahale.metrics.MeterregisterMeter(String name)CombinedMetricregisterMetric(String name, ICombiner combiner, int timeBucketSizeInSecs)Deprecated.ReducedMetricregisterMetric(String name, IReducer reducer, int timeBucketSizeInSecs)Deprecated.<T extends IMetric>
TregisterMetric(String name, T metric, int timeBucketSizeInSecs)Deprecated.voidregisterMetricSet(String prefix, com.codahale.metrics.MetricSet set)com.codahale.metrics.TimerregisterTimer(String name)<T extends ISubscribedState>
TsetAllSubscribedState(T obj)All state from all subscribed state spouts streams will be synced with the provided object.voidsetExecutorData(String name, Object data)Sets the executor-level data for the given name.<T extends ISubscribedState>
TsetSubscribedState(String componentId, String streamId, T obj)Synchronizes the specified stream from the specified state spout component id with the provided ISubscribedState object.<T extends ISubscribedState>
TsetSubscribedState(String componentId, T obj)Synchronizes the default stream from the specified state spout component id with the provided ISubscribedState object.voidsetTaskData(String name, Object data)Sets the task-level data for the given name.StringtoJSONString()-
Methods inherited from class org.apache.storm.task.WorkerTopologyContext
getAssignmentId, getCodeDir, getNodeToHost, getPIDDir, getResource, getSharedExecutor, getTaskToNodePort, getThisWorkerPort, getThisWorkerTasks
-
Methods inherited from class org.apache.storm.task.GeneralTopologyContext
doSanityCheck, getComponentCommon, getComponentId, getComponentIds, getComponentOutputFields, getComponentOutputFields, getComponentStreams, getComponentTasks, getConf, getRawTopology, getSources, getStormId, getTargets, getTaskToComponent, maxTopologyMessageTimeout
-
-
-
-
Constructor Detail
-
TopologyContext
public TopologyContext(StormTopology topology, Map<String,Object> topoConf, Map<Integer,String> taskToComponent, Map<String,List<Integer>> componentToSortedTasks, Map<String,Map<String,Fields>> componentToStreamToFields, Map<String,Long> blobToLastKnownVersionShared, String stormId, String codeDir, String pidDir, Integer taskId, Integer workerPort, List<Integer> workerTasks, Map<String,Object> defaultResources, Map<String,Object> userResources, Map<String,Object> executorData, Map<Integer,Map<Integer,Map<String,IMetric>>> registeredMetrics, AtomicBoolean openOrPrepareWasCalled, StormMetricRegistry metricRegistry)
-
-
Method Detail
-
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 toobj- 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 tostreamId- the stream to subscribe toobj- Provided ISubscribedState implementation- Returns:
- Returns the ISubscribedState object provided
-
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 setdata- 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 setdata- 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)
-
toJSONString
public String toJSONString()
- Specified by:
toJSONStringin interfaceorg.apache.storm.shade.net.minidev.json.JSONAware- Overrides:
toJSONStringin classGeneralTopologyContext
-
registerMetric
@Deprecated public <T extends IMetric> T registerMetric(String name, T metric, int timeBucketSizeInSecs)
Deprecated.Description copied from interface:IMetricsContextRegister metric.- Specified by:
registerMetricin interfaceIMetricsContext
-
registerMetric
@Deprecated public ReducedMetric registerMetric(String name, IReducer reducer, int timeBucketSizeInSecs)
Deprecated.Description copied from interface:IMetricsContextRegister metric.- Specified by:
registerMetricin interfaceIMetricsContext
-
registerMetric
@Deprecated public CombinedMetric registerMetric(String name, ICombiner combiner, int timeBucketSizeInSecs)
Deprecated.Description copied from interface:IMetricsContextRegister metric.- Specified by:
registerMetricin interfaceIMetricsContext
-
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:
registerTimerin interfaceIMetricsContext
-
registerHistogram
public com.codahale.metrics.Histogram registerHistogram(String name)
- Specified by:
registerHistogramin interfaceIMetricsContext
-
registerMeter
public com.codahale.metrics.Meter registerMeter(String name)
- Specified by:
registerMeterin interfaceIMetricsContext
-
registerCounter
public com.codahale.metrics.Counter registerCounter(String name)
- Specified by:
registerCounterin interfaceIMetricsContext
-
registerGauge
public <T> com.codahale.metrics.Gauge<T> registerGauge(String name, com.codahale.metrics.Gauge<T> gauge)
- Specified by:
registerGaugein interfaceIMetricsContext
-
registerMetricSet
public void registerMetricSet(String prefix, com.codahale.metrics.MetricSet set)
- Specified by:
registerMetricSetin interfaceIMetricsContext
-
-