Search in sources :

Example 1 with SetTaskContainerMapping

use of org.apache.samza.coordinator.stream.messages.SetTaskContainerMapping in project samza by apache.

the class TaskAssignmentManager method writeTaskContainerMapping.

/**
   * Method to write task container info to coordinator stream.
   *
   * @param taskName    the task name
   * @param containerId the SamzaContainer ID or {@code null} to delete the mapping
   */
public void writeTaskContainerMapping(String taskName, String containerId) {
    String existingContainerId = taskNameToContainerId.get(taskName);
    if (existingContainerId != null && !existingContainerId.equals(containerId)) {
        log.info("Task \"{}\" moved from container {} to container {}", new Object[] { taskName, existingContainerId, containerId });
    } else {
        log.debug("Task \"{}\" assigned to container {}", taskName, containerId);
    }
    if (containerId == null) {
        send(new Delete(getSource(), taskName, SetTaskContainerMapping.TYPE));
        taskNameToContainerId.remove(taskName);
    } else {
        send(new SetTaskContainerMapping(getSource(), taskName, String.valueOf(containerId)));
        taskNameToContainerId.put(taskName, containerId);
    }
}
Also used : Delete(org.apache.samza.coordinator.stream.messages.Delete) SetTaskContainerMapping(org.apache.samza.coordinator.stream.messages.SetTaskContainerMapping)

Example 2 with SetTaskContainerMapping

use of org.apache.samza.coordinator.stream.messages.SetTaskContainerMapping in project samza by apache.

the class TaskAssignmentManager method readTaskAssignment.

/**
   * Method to allow read container task information from coordinator stream. This method is used
   * in {@link org.apache.samza.coordinator.JobModelManager}.
   *
   * @return the map of taskName: containerId
   */
public Map<String, String> readTaskAssignment() {
    taskNameToContainerId.clear();
    for (CoordinatorStreamMessage message : getBootstrappedStream(SetTaskContainerMapping.TYPE)) {
        if (message.isDelete()) {
            taskNameToContainerId.remove(message.getKey());
            log.debug("Got TaskContainerMapping delete message: {}", message);
        } else {
            SetTaskContainerMapping mapping = new SetTaskContainerMapping(message);
            taskNameToContainerId.put(mapping.getKey(), mapping.getTaskAssignment());
            log.debug("Got TaskContainerMapping message: {}", mapping);
        }
    }
    for (Map.Entry<String, String> entry : taskNameToContainerId.entrySet()) {
        log.debug("Assignment for task \"{}\": {}", entry.getKey(), entry.getValue());
    }
    return Collections.unmodifiableMap(new HashMap<>(taskNameToContainerId));
}
Also used : CoordinatorStreamMessage(org.apache.samza.coordinator.stream.messages.CoordinatorStreamMessage) Map(java.util.Map) HashMap(java.util.HashMap) SetTaskContainerMapping(org.apache.samza.coordinator.stream.messages.SetTaskContainerMapping)

Aggregations

SetTaskContainerMapping (org.apache.samza.coordinator.stream.messages.SetTaskContainerMapping)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CoordinatorStreamMessage (org.apache.samza.coordinator.stream.messages.CoordinatorStreamMessage)1 Delete (org.apache.samza.coordinator.stream.messages.Delete)1