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
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_EXECUTORFields inherited from class org.apache.storm.task.GeneralTopologyContext
doSanityCheck, topoConf -
Constructor Summary
ConstructorsConstructorDescriptionTopologyContext(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
Modifier and TypeMethodDescriptionvoidaddTaskHook(ITaskHook hook) getExecutorData(String name) Fetches the executor-level data for the given name.getHooks()Deprecated.getTaskData(String name) Fetches the task-level data for the given name.Get component id.Gets the declared input fields for this component.getThisOutputFields(String streamId) Gets the declared output fields for the specified stream id for the component this task is a part of.Gets the declared output fields for all streams for the component this task is a part of.Gets the declared inputs to this component.Gets the set of streams declared for the component of this task.Gets information about who is consuming the outputs of this component, and how.intGets the task id of this task.intGets 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) registerMetric(String name, ICombiner combiner, int timeBucketSizeInSecs) Deprecated.registerMetric(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.Methods inherited from class org.apache.storm.task.WorkerTopologyContext
getAssignmentId, getCodeDir, getNodeToHost, getPIDDir, getResource, getSharedExecutor, getTaskToNodePort, getThisWorkerPort, getThisWorkerTasksMethods inherited from class org.apache.storm.task.GeneralTopologyContext
doSanityCheck, getComponentCommon, getComponentId, getComponentIds, getComponentOutputFields, getComponentOutputFields, getComponentStreams, getComponentTasks, getConf, getRawTopology, getSources, getStormId, getTargets, getTaskToComponent, maxTopologyMessageTimeout
-
Constructor Details
-
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 Details
-
setAllSubscribedState
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
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
-
getBlobToLastKnownVersion
-
getThisTaskId
public int getThisTaskId()Gets the task id of this task.- Returns:
- the task id
-
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
Gets the declared output fields for the specified stream id for the component this task is a part of. -
getThisOutputFieldsForStreams
Gets the declared output fields for all streams for the component this task is a part of. -
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
Gets the declared input fields for this component.- Returns:
- A map from sources to streams to fields.
-
getThisSources
Gets the declared inputs to this component.- Returns:
- A map from subscribed component/stream to the grouping subscribed with.
-
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
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
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
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
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
-
getHooks
-
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.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
- Specified by:
registerTimerin interfaceIMetricsContext
-
registerHistogram
- Specified by:
registerHistogramin interfaceIMetricsContext
-
registerMeter
- Specified by:
registerMeterin interfaceIMetricsContext
-
registerCounter
- 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
- Specified by:
registerMetricSetin interfaceIMetricsContext
-