public void assignToLeafTasks(List<TaskRequestEvent> taskRequests) {
Collections.shuffle(taskRequests);
Iterator<TaskRequestEvent> it = taskRequests.iterator();
TaskRequestEvent taskRequest;
while (it.hasNext() && leafTasks.size() > 0) {
taskRequest = it.next();
LOG.debug("assignToLeafTasks: " + taskRequest.getExecutionBlockId() + "," +
"containerId=" + taskRequest.getContainerId());
ContainerProxy container = context.getResourceAllocator().getContainer(taskRequest.getContainerId());
if(container == null) {
continue;
}
String host = container.getTaskHostName();
QueryUnitAttemptId attemptId = null;
LinkedList<QueryUnitAttemptId> list = null;
// local disk allocation
if(!leafTaskHostMapping.containsKey(host)){
host = NetUtils.normalizeHost(host);
}
TaskBlockLocation taskBlockLocation = leafTaskHostMapping.get(host);
if (taskBlockLocation != null) {
list = taskBlockLocation.getQueryUnitAttemptIdList(taskRequest.getContainerId());
}
while (list != null && list.size() > 0) {
QueryUnitAttemptId tId = list.removeFirst();
if (leafTasks.contains(tId)) {
leafTasks.remove(tId);
attemptId = tId;
//LOG.info(attemptId + " Assigned based on host match " + hostName);
hostLocalAssigned++;
break;
}
}
// rack allocation
if (attemptId == null) {
String rack = RackResolver.resolve(host).getNetworkLocation();
list = leafTasksRackMapping.get(rack);
while(list != null && list.size() > 0) {
QueryUnitAttemptId tId = list.removeFirst();
if (leafTasks.contains(tId)) {
leafTasks.remove(tId);
attemptId = tId;
//LOG.info(attemptId + "Assigned based on rack match " + rack);
rackLocalAssigned++;
break;
}
}
// random allocation
if (attemptId == null && leafTaskNum() > 0) {
attemptId = leafTasks.iterator().next();
leafTasks.remove(attemptId);
//LOG.info(attemptId + " Assigned based on * match");
}
}
SubQuery subQuery = context.getQuery().getSubQuery(attemptId.getQueryUnitId().getExecutionBlockId());
if (attemptId != null) {
QueryUnit task = subQuery.getQueryUnit(attemptId.getQueryUnitId());
QueryUnitRequest taskAssign = new QueryUnitRequestImpl(
attemptId,
new ArrayList<Fragment>(task.getAllFragments()),
"",
false,
task.getLogicalPlan().toJson(),
context.getQueryContext(),
subQuery.getDataChannel(), subQuery.getBlock().getEnforcer());
if (!subQuery.getBlock().isRoot()) {
taskAssign.setInterQuery();
}
context.getEventHandler().handle(new TaskAttemptAssignedEvent(attemptId,
taskRequest.getContainerId(),
host, container.getTaskPort()));
assignedRequest.add(attemptId);
totalAssigned++;
taskRequest.getCallback().run(taskAssign.getProto());
} else {
throw new RuntimeException("Illegal State!!!!!!!!!!!!!!!!!!!!!");
}
}