AMContainerAllocatedTransition(基于hadoop 2.7.6)
前述RmAppAttempt的状态转换:
AttemptStartedTransition:NEW–>SUBMITTED
ScheduleTransition:SUBMITTED–>SCHEDULED
主要逻辑步骤:
1.调用scheduler.allocate方法,触发RMContainerEventType.ACQUIRED事件(该事件的处理器AcquiredTransition会将Container加入到containerAllocationExpirer)
2.触发RMStateStoreEventType.STORE_APP_ATTEMPT事件
AMContainerAllocatedTransition转换器处理后,RmAppAttempt的状态由SCHEDULED转换为ALLOCATED_SAVING.
/**
* 主要逻辑:
* 1.调用scheduler.allocate方法
* 2.触发RMStateStoreEventType.STORE_APP_ATTEMPT事件
*/
private static final class AMContainerAllocatedTransition
implements
MultipleArcTransition<RMAppAttemptImpl, RMAppAttemptEvent, RMAppAttemptState> {
@Override
public RMAppAttemptState transition(RMAppAttemptImpl appAttempt,
RMAppAttemptEvent event) {
// Acquire the AM container from the scheduler.
/**
*
* 1.释放已完成的Container列表
* 2.将新需求更新到对应的数据结构中(appSchedulingInfo.requests:Map<Priority, Map<String, ResourceRequest>>)
* 3.返回已经为该应用分配的Containers
*
*/
Allocation amContainerAllocation =
appAttempt.scheduler.allocate(appAttempt.applicationAttemptId,
EMPTY_CONTAINER_REQUEST_LIST, EMPTY_CONTAINER_RELEASE_LIST, null,
null);
/**
* 存在创建NMToken失败的情况,则重新触发RMAppAttemptEventType.CONTAINER_ALLOCATED用于再次分配资源
*/
if (amContainerAllocation.getContainers().size() == 0) {
/**
* 重新触发事件:RMAppAttemptEventType.CONTAINER_ALLOCATED
*/
appAttempt.retryFetchingAMContainer(appAttempt);
return RMAppAttemptState.SCHEDULED;
}
/**
* AM只需要一个Container
*/
appAttempt.setMasterContainer(amContainerAllocation.getContainers()
.get(0));
RMContainerImpl rmMasterContainer = (RMContainerImpl)appAttempt.scheduler
.getRMContainer(appAttempt.getMasterContainer().getId());
/**
* 设置为AmContainer
*/
rmMasterContainer.setAMContainer(true);
appAttempt.rmContext.getNMTokenSecretManager()
.clearNodeSetForAttempt(appAttempt.applicationAttemptId);
appAttempt.getSubmissionContext().setResource(
appAttempt.getMasterContainer().getResource());
/**
* 触发事件:RMStateStoreAppAttemptEvent(RMStateStoreEventType.STORE_APP_ATTEMPT)
*/
appAttempt.storeAttempt();
return RMAppAttemptState.ALLOCATED_SAVING;
}
}