backtype.storm.task
Class TopologyContext

java.lang.Object
  extended by backtype.storm.task.TopologyContext

public class TopologyContext
extends java.lang.Object

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 Summary
TopologyContext(StormTopology topology, java.util.Map<java.lang.Integer,java.lang.String> taskToComponent, java.lang.String stormId, java.lang.String codeDir, java.lang.String pidDir, java.lang.Integer taskId)
           
 
Method Summary
 java.lang.String getCodeDir()
          Gets the location of the external resources for this worker on the local filesystem.
 ComponentCommon getComponentCommon(java.lang.String componentId)
           
 java.lang.String getComponentId(int taskId)
          Gets the component id for the specified task id.
 java.util.Set<java.lang.String> getComponentIds()
          Gets a list of all component ids in this topology
 Fields getComponentOutputFields(GlobalStreamId id)
          Gets the declared output fields for the specified global stream id.
 Fields getComponentOutputFields(java.lang.String componentId, java.lang.String streamId)
          Gets the declared output fields for the specified component/stream.
 java.util.Set<java.lang.String> getComponentStreams(java.lang.String componentId)
          Gets the set of streams declared for the specified component.
 java.util.List<java.lang.Integer> getComponentTasks(java.lang.String componentId)
          Gets the task ids allocated for the given component id.
 java.lang.String getPIDDir()
          If this task spawns any subprocesses, those subprocesses must immediately write their PID to this directory on the local filesystem to ensure that Storm properly destroys that process when the worker is shutdown.
 StormTopology getRawTopology()
          Gets the Thrift object representing the topology.
 java.util.Map<GlobalStreamId,Grouping> getSources(java.lang.String componentId)
          Gets the declared inputs to the specified component.
 java.lang.String getStormId()
          Gets the unique id assigned to this topology.
 java.util.Map<java.lang.String,java.util.Map<java.lang.String,Grouping>> getTargets(java.lang.String componentId)
          Gets information about who is consuming the outputs of the specified component, and how.
 java.lang.Object getTaskData()
           
 java.util.Map<java.lang.Integer,java.lang.String> getTaskToComponent()
          Gets a map from task id to component id.
 java.lang.String getThisComponentId()
          Gets the component id for this task.
 Fields getThisOutputFields(java.lang.String streamId)
          Gets the declared output fields for the specified stream id for the component this task is a part of.
 java.util.Map<GlobalStreamId,Grouping> getThisSources()
          Gets the declared inputs to this component.
 java.util.Set<java.lang.String> getThisStreams()
          Gets the set of streams declared for the component of this task.
 java.util.Map<java.lang.String,java.util.Map<java.lang.String,Grouping>> getThisTargets()
          Gets information about who is consuming the outputs of this component, and how.
 int getThisTaskId()
          Gets the task id of this task.
 int getThisTaskIndex()
          Gets the index of this task id in getComponentTasks(getThisComponentId()).
 int maxTopologyMessageTimeout(java.util.Map<java.lang.String,java.lang.Object> topologyConfig)
           
<T extends ISubscribedState>
T
setAllSubscribedState(T obj)
          All state from all subscribed state spouts streams will be synced with the provided object.
<T extends ISubscribedState>
T
setSubscribedState(java.lang.String componentId, java.lang.String streamId, T obj)
          Synchronizes the specified stream from the specified state spout component id with the provided ISubscribedState object.
<T extends ISubscribedState>
T
setSubscribedState(java.lang.String componentId, T obj)
          Synchronizes the default stream from the specified state spout component id with the provided ISubscribedState object.
 void setTaskData(java.lang.Object data)
           
 java.lang.String toJSONString()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TopologyContext

public TopologyContext(StormTopology topology,
                       java.util.Map<java.lang.Integer,java.lang.String> taskToComponent,
                       java.lang.String stormId,
                       java.lang.String codeDir,
                       java.lang.String pidDir,
                       java.lang.Integer taskId)
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:

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

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

setSubscribedState

public <T extends ISubscribedState> T setSubscribedState(java.lang.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:

_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(java.lang.String componentId,
                                                         java.lang.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:

_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

getStormId

public java.lang.String getStormId()
Gets the unique id assigned to this topology. The id is the storm name with a unique nonce appended to it.

Returns:
the storm id

getThisTaskId

public int getThisTaskId()
Gets the task id of this task.

Returns:
the task id

getRawTopology

public StormTopology getRawTopology()
Gets the Thrift object representing the topology.

Returns:
the Thrift definition representing the topology

getComponentId

public java.lang.String getComponentId(int taskId)
Gets the component id for the specified task id. The component id maps to a component id specified for a Spout or Bolt in the topology definition.

Parameters:
taskId - the task id
Returns:
the component id for the input task id

getThisComponentId

public java.lang.String getThisComponentId()
Gets the component id for this task. The component id maps to a component id specified for a Spout or Bolt in the topology definition.

Returns:

getThisOutputFields

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


getThisStreams

public java.util.Set<java.lang.String> getThisStreams()
Gets the set of streams declared for the component of this task.


getComponentStreams

public java.util.Set<java.lang.String> getComponentStreams(java.lang.String componentId)
Gets the set of streams declared for the specified component.


getComponentTasks

public java.util.List<java.lang.Integer> getComponentTasks(java.lang.String componentId)
Gets the task ids allocated for the given component id. The task ids are always returned in ascending order.


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.


getComponentOutputFields

public Fields getComponentOutputFields(java.lang.String componentId,
                                       java.lang.String streamId)
Gets the declared output fields for the specified component/stream.


getComponentOutputFields

public Fields getComponentOutputFields(GlobalStreamId id)
Gets the declared output fields for the specified global stream id.


getThisSources

public java.util.Map<GlobalStreamId,Grouping> getThisSources()
Gets the declared inputs to this component.

Returns:
A map from subscribed component/stream to the grouping subscribed with.

getSources

public java.util.Map<GlobalStreamId,Grouping> getSources(java.lang.String componentId)
Gets the declared inputs to the specified component.

Returns:
A map from subscribed component/stream to the grouping subscribed with.

getThisTargets

public java.util.Map<java.lang.String,java.util.Map<java.lang.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.

getTargets

public java.util.Map<java.lang.String,java.util.Map<java.lang.String,Grouping>> getTargets(java.lang.String componentId)
Gets information about who is consuming the outputs of the specified component, and how.

Returns:
Map from stream id to component id to the Grouping used.

toJSONString

public java.lang.String toJSONString()

getCodeDir

public java.lang.String getCodeDir()
Gets the location of the external resources for this worker on the local filesystem. These external resources typically include bolts implemented in other languages, such as Ruby or Python.


getPIDDir

public java.lang.String getPIDDir()
If this task spawns any subprocesses, those subprocesses must immediately write their PID to this directory on the local filesystem to ensure that Storm properly destroys that process when the worker is shutdown.


getTaskToComponent

public java.util.Map<java.lang.Integer,java.lang.String> getTaskToComponent()
Gets a map from task id to component id.


getComponentIds

public java.util.Set<java.lang.String> getComponentIds()
Gets a list of all component ids in this topology


getComponentCommon

public ComponentCommon getComponentCommon(java.lang.String componentId)

setTaskData

public void setTaskData(java.lang.Object data)

getTaskData

public java.lang.Object getTaskData()

maxTopologyMessageTimeout

public int maxTopologyMessageTimeout(java.util.Map<java.lang.String,java.lang.Object> topologyConfig)