这几个kill、rebanlance、activate、deactivate方法放在一起分析,是因为他们都依赖于nimbus定义的通用的 状态转移方法:
transitionName
********************************************这里只具体分析kill方法*********************************
一:初始化LocalCluster同拓扑提交一样,详见大神http://www.maoxiangyi.cn/index.php/archives/734;
二:LocalCluster完成构造之后,即可使用killTopology/ killTopologyWithOpts杀死拓扑。
方法的实现都会调用nimbus的对应方法。追踪nimbus的实现类,就能看到killTopology/ killTopologyWithOpts这两个方法,真正的实现类就是killTopologyWithOpts
@Override
public void killTopology(String name) throws NotAliveException, TException {
killTopologyWithOpts(name, new KillOptions());
}
跟踪killTopologyWithOpts方法,方法定义如下:
public void killTopologyWithOpts(String topologyName, KillOptions options)
可以看到需要两个参数,分别是拓扑的名称和kill的操作。
@Override
public void killTopologyWithOpts(String topologyName, KillOptions options)
throws NotAliveException, TException {
try {
checkTopologyActive(data, topologyName, true);
Integer wait_amt = null;
//查看是否设置了杀死拓扑的等待时间,如果设置了就等待
if (options.is_set_wait_secs()) {
wait_amt = options.get_wait_secs();
}
//
NimbusUtils.transitionName(data, topologyName, true,
StatusType.kill, wait_amt);
} catch (NotAliveException e) {
String errMsg = "KillTopology Error, no this topology "
+ topologyName;
LOG.error(errMsg, e);
throw new NotAliveException(errMsg);
} catch (Exception e) {
String errMsg = "Failed to kill topology " + topologyName;
LOG.error(errMsg, e);
throw new TException(errMsg);
}
}
流程:1,先查看拓扑是否Active,
public void checkTopologyActive(NimbusData nimbus, String topologyName,
boolean bActive) throws Exception {
if (isTopologyActive(nimbus.getStormClusterState(), topologyName) != bActive) {
if (bActive) {
throw new NotAliveException(topologyName + " is not alive");
} else {
throw new AlreadyAliveException(topologyName
+ " is already active");
}
}
}
public boolean isTopologyActive(StormClusterState stormClusterState,
String topologyName) throws Exception {
boolean rtn = false;
if (Cluster.get_topology_id(stormClusterState, topologyName) != null) {
rtn = true;
}
return rtn;
}
3:transitionName:这是在nimbus定义的通用的 状态转移方法,,他们会根据传入的转移事件做相应的处理。
public static void transitionName(NimbusData data, String topologyName,
boolean errorOnNoTransition, StatusType transition_status,
T... args) throws Exception {
//获得nimbus的信息,通过getStormClusterState查找对应的storm-id(实际上就是将storm-name 转换为storm-id的状态),如果找到了,就调用transition方法。
StormClusterState stormClusterState = data.getStormClusterState();
String topologyId = Cluster.get_topology_id(stormClusterState,
topologyName);
if (topologyId == null) {
throw new NotAliveException(topologyName);
}
//开始执行kill操作,转变拓扑状态。
transition(data, topologyId, errorOnNoTransition, transition_status,
args);
}
public staticvoid transitionName(NimbusData data, String topologyName, boolean errorOnNoTransition, StatusType transition_status, T... args) throws Exception { //通过nimbus的信息,获得storm客户端的状态。 StormClusterState stormClusterState = data.getStormClusterState(); //通过cluster的状态和拓扑的名称获得拓扑的ID。 String topologyId = Cluster.get_topology_id(stormClusterState, topologyName); if (topologyId == null) { throw new NotAliveException(topologyName); } //开始执行kill操作,转变拓扑状态 transition(data, topologyId, errorOnNoTransition, transition_status, args); }
4:transition:这是在nimbus定义的通用的 状态转移方法
transition方法会根据传入的转移事件,获取与当前Topology状态对应的状态转移函数,并且执行该函数取得转换后的新状态。
public static void transition(NimbusData data, String topologyid,
boolean errorOnNoTransition, StatusType transition_status,
T... args) {
try {
//转变拓扑的状态
data.getStatusTransition().transition(topologyid,
errorOnNoTransition, transition_status, args);
} catch (Exception e) {
// TODO Auto-generated catch block
LOG.error("Failed to do status transition,", e);
}
}
*********
最后一步追踪trantion有一步topologyLocks还没看太明白%>_<%
后续更新