我创建了一个示例拓扑,以测试设置最大喷口支出属性。这是一个带有1个喷嘴和一个螺栓的简单拓扑。睡觉一秒钟后,喷口发出10万个元组和螺栓钉。我已将“最大喷口支出”属性设置为10。我认为这意味着,如果该喷口的未确认消息计数为10,则喷口将不会发出任何元组。但是当我运行拓扑时,我可以看到喷口发出2160条消息,然后等待。我的理解正确吗,还是我错过了一些东西。我正在使用风暴0.9.5。
下面是代码
public static void main(String[] args) {
TopologyBuilder builder = new TopologyBuilder();
builder.setSpout("spout", new TestSpout(), 1);
builder.setBolt("bolt", new TestBolt(),1).shuffleGrouping("spout");
Config conf = new Config();
conf.setNumWorkers(1);
conf.setMaxSpoutPending(10);
try {
StormSubmitter.submitTopology("test", conf, builder.createTopology());
} catch (AlreadyAliveException e) {
e.printStackTrace();
} catch (InvalidTopologyException e) {
e.printStackTrace();
}
}
public class TestSpout extends BaseRichSpout {
private SpoutOutputCollector collector;
private int count = 1;
@Override
public void declareOutputFields(OutputFieldsDeclarer declarer) {
declarer.declare(new Fields("spoutData"));
}
@Override
public void open(Map conf, TopologyContext context, SpoutOutputCollector collector) {
this.collector = collector;
System.out.println(context.maxTopologyMessageTimeout());
}
@Override
public void nextTuple() {
if(count <= 100000) {
System.out.println("Emitting : " + count);
collector.emit(new Values(count++ + ""));
}
}
}
public class TestBolt extends BaseRichBolt {
private OutputCollector collector;
@Override
public void prepare(Map stormConf, TopologyContext context, OutputCollector collector) {
this.collector = collector;
}
@Override
public void execute(Tuple input) {
try {
System.out.println(input.getString(0));
Thread.sleep(1000);
collector.ack(input);
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("Exception");
}
}
@Override
public void declareOutputFields(OutputFieldsDeclarer declarer) {
}
}
最佳答案
您需要为Spout.nextTuple()
方法中发出的元组分配消息ID。否则,将忽略参数max.spout.pending
。例如,您可以将count
变量用作ID(基本上,任何东西都可以用作ID。它只能是唯一的。)
@Override
public void nextTuple() {
if(count <= 100000) {
System.out.println("Emitting : " + count);
collector.emit(new Values(count++ + ""), count);
}
}
否则,Storm无法将输出元组链接到螺栓中确认的元组,即,Storm无法计算待处理的元组数。 Storm只能跟踪具有ID的元组。