0

我创建了一个带有 Spout 的 Storm 拓扑,它发出许多用于基准测试的元组。一旦从 spout 发出所有元组或拓扑中不再有任何元组流动,我想停止/终止我的拓扑。

这是我的拓扑结构。

LocalCluster cluster = new LocalCluster();
TopologyBuilder builder = new TopologyBuilder();
Config conf = new Config();
//Disabled ACK'ing for higher throughput
conf.put(Config.TOPOLOGY_ACKER_EXECUTORS, 0); 

LoadGeneratorSource loadGenerator = new LoadGeneratorSource(runtime,numberOfTuplesToBeEmitted);
builder.setSpout("loadGenerator", loadGenerator);

//Some Bolts Here

while (loadGenerator.isRunning()){
//Active Waiting
}
//DO SOME STUFF WITH JAVA
cluster.killTopology("StormBenchmarkTopology");

问题是我在这个范围内引用的 loadGenerator 实例与在 spout 线程中运行的实例不同。因此,isRuning() 总是返回 true,即使在 spout 线程中,当没有更多的元组要发出时,它的值为 false。

这是 LoadGeneratorSource 类的一部分。


public class LoadGeneratorSource extends BaseRichSpout {

    private final int throughput;
    private boolean running;
    private final long runtime;


    public LoadGeneratorSource(long runtime,int throughput) {
        this.throughput = throughput;
        this.runtime = runtime;
    }

    @Override
    public void nextTuple() {
        ThroughputStatistics.getInstance().pause(false);

        long endTime = System.currentTimeMillis() + runtime;
        while (running) {
            long startTs = System.currentTimeMillis();

            for (int i = 0; i < throughput; i++) {
                try {
                    emitValue(readNextTuple());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            while (System.currentTimeMillis() < startTs + 1000) {
                // active waiting
            }

            if (endTime <= System.currentTimeMillis())
                setRunning(false);
        }
    }

    public boolean isRunning() {
        return running;
    }

    public void setRunning(boolean running) {
        this.running = running;
    }

    //MORE STUFF

}

一旦不再有从 spout 发出或在拓扑中流动的元组,有人能告诉我一种停止我的拓扑的方法吗?提前感谢您的帮助。

4

1 回答 1

0

这似乎是 spout 中杀死风暴拓扑的副本。请尝试那里给出的答案。

只是为了快速总结一下;您尝试执行此操作的方式不起作用,但您可以使用 spout 中的 NimbusClient 要求 Nimbus 终止您的拓扑。附带的好处是,一旦您部署到真正的集群,它也将起作用。

于 2019-03-26T21:43:21.067 回答