# Storm的可靠性与ack机制

## 1 Spout的可靠性

• ISpout.java部分源码：
public interface ISpout extends Serializable {

/**
* Called when a task for this component is initialized within a worker on the cluster.
* It provides the spout with the environment in which the spout executes.
*
* This includes the:
*
* @param conf The Storm configuration for this spout. This is the configuration provided to the topology merged in with cluster configuration on this machine.
* @param collector The collector is used to emit tuples from this spout. Tuples can be emitted at any time, including the open and close methods. The collector is thread-safe and should be saved as an instance variable of this spout object.
*/
void open(Map conf, TopologyContext context, SpoutOutputCollector collector);

/**
* When this method is called, Storm is requesting that the Spout emit tuples to the
* output collector. This method should be non-blocking, so if the Spout has no tuples
* to emit, this method should return. nextTuple, ack, and fail are all called in a tight
* loop in a single thread in the spout task. When there are no tuples to emit, it is courteous
* to have nextTuple sleep for a short amount of time (like a single millisecond)
* so as not to waste too much CPU.
*/
void nextTuple();

/**
* Storm has determined that the tuple emitted by this spout with the msgId identifier
* has been fully processed. Typically, an implementation of this method will take that
* message off the queue and prevent it from being replayed.
*/
void ack(Object msgId);

/**
* The tuple emitted by this spout with the msgId identifier has failed to be
* fully processed. Typically, an implementation of this method will put that
* message back on the queue to be replayed at a later time.
*/
void fail(Object msgId);
}


## 2 ack机制详细定义

Storm会跟踪spout产生的每一个tuple，给tuple指定ID就可以告诉Storm，无论执行成功还是失败，spout都要接收tuple所传过的每一个节点上返回的通知。一个十分重要的原则是：

• 一个tuple处理成功，指的是这个tuple以及这个tuple所衍生的所有tuple都已经被每个bolt成功处理。

• tuple处理失败是指tuple或这个tuple衍生的某一个tuple处理失败。