//��ȡ�ܵ�slotsʹ����
int totalSlotsToUse = Math.min(workers,
availableSlots.size() + aliveAssigned.size());
IAssignment customAssignment=CustomAssignment.getAssignmentInstance(topology_conf);
Map<NodePort, List<Integer>> keepAssigned =null;
if (!isScratch && allTaskIds!= null) {
//��ȡ���־����nodeport��task�ķ�����Ϣ
if (customAssignment != null) {
keepAssigned=customAssignment.keeperSlots(aliveAssigned, allTaskIds.size(),totalSlotsToUse);
}else{
keepAssigned = keeperSlots(aliveAssigned, allTaskIds.size(),totalSlotsToUse);// <NodePort, List>
}
}
//���·�����
int reassign_num=totalSlotsToUse;
if(keepAssigned!=null){
for (Entry<NodePort, List<Integer>> entry:keepAssigned.entrySet()) {
aliveAssigned.remove(entry.getKey());
}
reassign_num=totalSlotsToUse-keepAssigned.size();
}
//��ȡ���п��е�slot
Set<NodePort> freedSlots = new HashSet<NodePort>();
Set<NodePort> freedSlotstmp = aliveAssigned.keySet();
if (freedSlotstmp.size() == 0){
freedSlotstmp = new HashSet<NodePort>();
}
freedSlots.addAll(freedSlotstmp);
freedSlots.addAll(availableSlots);
if (customAssignment != null) {
customAssignment.setup(topology_conf, topologyid, stormClusterState, keepAssigned,supInfos);
}
Collection<List<Integer>> keepAssignedTaskSet = null;
if (keepAssigned != null){
keepAssignedTaskSet=keepAssigned.values();
}
Set<Integer> reassignIds = null;
if (allTaskIds != null){
reassignIds = new HashSet<Integer>(allTaskIds);
}
if (keepAssignedTaskSet != null && reassignIds != null){
//FIXME ����ֱ�Ӷ�keepAssignedTaskSet����removeAll,��Ч�� yannian add
for(List<Integer> rm:keepAssignedTaskSet)
{
reassignIds.removeAll(rm);
}
}
//����δkeep��task����Ӧ��slot���档
Map<Integer, NodePort> reassignment = new HashMap<Integer, NodePort>();
List<NodePort> reassignSlots=null;
List<NodePort> sortedFreeSlots = sortSlots(freedSlots);
//��ȡ�������Ҫ�������slots�ڵ�
if (customAssignment != null) {
reassignSlots = customAssignment.slotsAssignment(sortedFreeSlots, reassign_num,reassignIds);
} else {
if (sortedFreeSlots != null) {
reassignSlots = sortedFreeSlots.subList(0, reassign_num);
}
}
//����δkeep��task����Ӧ��slot���档
if(customAssignment!=null)
{
reassignment=customAssignment.tasksAssignment(reassignSlots, reassignIds);
}else{
int index = 0;
if (reassignIds != null && reassignSlots != null && reassignSlots.size() != 0)
{
for (Iterator<Integer> it = reassignIds.iterator(); it.hasNext();) {
Integer entry = it.next();
if (index >= reassignSlots.size()) {
index = 0;
}
reassignment.put(entry, reassignSlots.get(index));
index++;
}
}
}
//ת����ȡkeepAssigned���õ�keyΪtaskid��valueΪNodePort��map
Map<Integer, NodePort> stayAssignment = new HashMap<Integer, NodePort>();
if(keepAssigned!=null){
for (Entry<NodePort, List<Integer>> entry:keepAssigned.entrySet()) {
NodePort np = entry.getKey();
List<Integer> tasks = entry.getValue();
for (Integer taskid : tasks) {
stayAssignment.put(taskid, np);
}
}
}
if (reassignment.size() > 0) {
LOG.info("Reassigning " + topologyid + " to " + totalSlotsToUse + " slots isScratch="+isScratch+",totalSlotsToUse="+totalSlotsToUse);
LOG.info("Reassign ids: " + reassignIds+",keepAssigned="+keepAssigned+",existingAssigned="+existingAssigned);
LOG.info("Available slots: " + availableSlots);
}
//�����������·�������
reassignment.putAll(stayAssignment);
if (customAssignment != null) {
customAssignment.cleanup();
}
return reassignment;
}