/** * Get the node entry matching a container on this node * @param container container * @return matching node instance for the role */ public NodeEntry getOrCreate(Container container) { return getOrCreate(ContainerPriority.extractRole(container)); }
/** * Map from a container to a role key by way of its priority * @param container container * @return role key */ public static int extractRole(Container container) { Priority priority = container.getPriority(); return extractRole(priority); }
/** * Look up the status entry of a container or raise an exception * * @param c container * @return the status entry * @throws RuntimeException if the role cannot be found */ public RoleStatus lookupRoleStatus(Container c) { return lookupRoleStatus(ContainerPriority.extractRole(c)); }
/** * Look up the status entry of a container or raise an exception * * @param c container * @return the status entry * @throws RuntimeException if the role cannot be found */ public RoleStatus lookupRoleStatus(Container c) { return lookupRoleStatus(ContainerPriority.extractRole(c)); }
/** * Map from a container to a role key by way of its priority * @param container container * @return role key */ public static int extractRole(Container container) { Priority priority = container.getPriority(); return extractRole(priority); }
/** * Priority record to role mapper * @param priorityRecord priority record * @return the role # */ public static int extractRole(Priority priorityRecord) { Preconditions.checkNotNull(priorityRecord); return extractRole(priorityRecord.getPriority()); }
/** * Priority record to role mapper * @param priorityRecord priority record * @return the role # */ public static int extractRole(Priority priorityRecord) { Preconditions.checkNotNull(priorityRecord); return extractRole(priorityRecord.getPriority()); }
/** * Get the node entry matching a container on this node * @param container container * @return matching node instance for the role */ public NodeEntry getOrCreate(Container container) { return getOrCreate(ContainerPriority.extractRole(container)); }
/** * Comparator: which host is more recent? * @param c1 container 1 * @param c2 container 2 * @return 1 if c2 older-than c1, 0 if equal; -1 if c1 older-than c2 */ @Override public int compare(Container c1, Container c2) { int role1 = ContainerPriority.extractRole(c1); int role2 = ContainerPriority.extractRole(c2); if (role1 < role2) return -1; if (role1 > role2) return 1; long age = getAgeOf(c1); long age2 = getAgeOf(c2); if (age > age2) { return -1; } else if (age < age2) { return 1; } // equal return 0; } }
/** * Comparator: which host is more recent? * @param c1 container 1 * @param c2 container 2 * @return 1 if c2 older-than c1, 0 if equal; -1 if c1 older-than c2 */ @Override public int compare(Container c1, Container c2) { int role1 = ContainerPriority.extractRole(c1); int role2 = ContainerPriority.extractRole(c2); if (role1 < role2) return -1; if (role1 > role2) return 1; long age = getAgeOf(c1); long age2 = getAgeOf(c2); if (age > age2) { return -1; } else if (age < age2) { return 1; } // equal return 0; } }
/** * Convert a priority record to a string, extracting role and locality * @param priorityRecord priority record. May be null * @return a string value */ public static String toString(Priority priorityRecord) { if (priorityRecord==null) { return "(null)"; } else { return String.format(Locale.ENGLISH, "role %d (locality=%b)", extractRole(priorityRecord), hasLocation(priorityRecord.getPriority())); } } }
/** * Convert a priority record to a string, extracting role and locality * @param priorityRecord priority record. May be null * @return a string value */ public static String toString(Priority priorityRecord) { if (priorityRecord==null) { return "(null)"; } else { return String.format(Locale.ENGLISH, "role %d (locality=%b)", extractRole(priorityRecord), hasLocation(priorityRecord.getPriority())); } } }
/** * Take a list of requests and split them into specific host requests and * generic assignments. This is to give requested hosts priority * in container assignments if more come back than expected * @param rh RoleHistory instance * @param inAllocated the list of allocated containers * @param outPlaceRequested initially empty list of requested locations * @param outUnplaced initially empty list of unrequested hosts */ public synchronized void partitionRequests(RoleHistory rh, List<Container> inAllocated, List<Container> outPlaceRequested, List<Container> outUnplaced) { Collections.sort(inAllocated, new newerThan(rh)); for (Container container : inAllocated) { int role = ContainerPriority.extractRole(container); String hostname = RoleHistoryUtils.hostnameOf(container); if (placedRequests.containsKey(new OutstandingRequest(role, hostname))) { outPlaceRequested.add(container); } else { outUnplaced.add(container); } } }
/** * Take a list of requests and split them into specific host requests and * generic assignments. This is to give requested hosts priority * in container assignments if more come back than expected * @param rh RoleHistory instance * @param inAllocated the list of allocated containers * @param outPlaceRequested initially empty list of requested locations * @param outUnplaced initially empty list of unrequested hosts */ public synchronized void partitionRequests(RoleHistory rh, List<Container> inAllocated, List<Container> outPlaceRequested, List<Container> outUnplaced) { Collections.sort(inAllocated, new newerThan(rh)); for (Container container : inAllocated) { int role = ContainerPriority.extractRole(container); String hostname = RoleHistoryUtils.hostnameOf(container); if (placedRequests.containsKey(new OutstandingRequest(role, hostname))) { outPlaceRequested.add(container); } else { outUnplaced.add(container); } } }
/** * If the node is marked as available; queue it for assignments. * Unsynced: requires caller to be in a sync block. * @param container completed container * @param nodeEntry node * @param available available flag * @return true if the node was queued */ private boolean maybeQueueNodeForWork(Container container, NodeEntry nodeEntry, boolean available) { if (available) { //node is free nodeEntry.setLastUsed(now()); NodeInstance ni = getOrCreateNodeInstance(container); int roleId = ContainerPriority.extractRole(container); log.debug("Node {} is now available for role id {}", ni, roleId); listRecentNodesForRoleId(roleId).addFirst(ni); } return available; }
/** * If the node is marked as available; queue it for assignments. * Unsynced: requires caller to be in a sync block. * @param container completed container * @param nodeEntry node * @param available available flag * @return true if the node was queued */ private boolean maybeQueueNodeForWork(Container container, NodeEntry nodeEntry, boolean available) { if (available) { //node is free nodeEntry.setLastUsed(now()); NodeInstance ni = getOrCreateNodeInstance(container); int roleId = ContainerPriority.extractRole(container); log.debug("Node {} is now available for role id {}", ni, roleId); listRecentNodesForRoleId(roleId).addFirst(ni); } return available; }
@Override public void rebuildContainerDetails(List<Container> liveContainers, String applicationId, Map<Integer, ProviderRole> providerRoleMap) { for (Container container : liveContainers) { // get the role name and label ProviderRole role = providerRoleMap.get(ContainerPriority .extractRole(container)); if (role != null) { String roleName = role.name; String roleGroup = role.group; String label = getContainerLabel(container, roleName, roleGroup); log.info("Rebuilding in-memory: container {} in role {} in cluster {}", container.getId(), roleName, applicationId); getComponentStatuses().put(label, new ComponentInstanceState(roleGroup, container.getId(), applicationId)); } else { log.warn("Role not found for container {} in cluster {}", container.getId(), applicationId); } } }
/** * Get the age of a node hosting container. If it is not known in the history, * return 0. * @param c container * @return age, null if there's no entry for it. */ private long getAgeOf(Container c) { long age = 0; NodeInstance node = rh.getExistingNodeInstance(c); int role = ContainerPriority.extractRole(c); if (node != null) { NodeEntry nodeEntry = node.get(role); if (nodeEntry != null) { age = nodeEntry.getLastUsed(); } } return age; }
@Override public void rebuildContainerDetails(List<Container> liveContainers, String applicationId, Map<Integer, ProviderRole> providerRoleMap) { for (Container container : liveContainers) { // get the role name and label ProviderRole role = providerRoleMap.get(ContainerPriority .extractRole(container)); if (role != null) { String roleName = role.name; String roleGroup = role.group; String label = getContainerLabel(container, roleName, roleGroup); log.info("Rebuilding in-memory: container {} in role {} in cluster {}", container.getId(), roleName, applicationId); getComponentStatuses().put(label, new ComponentInstanceState(roleGroup, container.getId(), applicationId)); } else { log.warn("Role not found for container {} in cluster {}", container.getId(), applicationId); } } }
/** * Get the age of a node hosting container. If it is not known in the history, * return 0. * @param c container * @return age, null if there's no entry for it. */ private long getAgeOf(Container c) { long age = 0; NodeInstance node = rh.getExistingNodeInstance(c); int role = ContainerPriority.extractRole(c); if (node != null) { NodeEntry nodeEntry = node.get(role); if (nodeEntry != null) { age = nodeEntry.getLastUsed(); } } return age; }