Search in sources :

Example 36 with TaskName

use of org.apache.samza.container.TaskName in project samza by apache.

the class GroupByContainerCount method buildContainerModels.

/**
   * Translates the list of TaskGroup instances to a set of ContainerModel instances, using the
   * set of TaskModel instances.
   *
   * @param tasks             the TaskModels to assign to the ContainerModels.
   * @param containerTasks    the TaskGroups defining how the tasks should be grouped.
   * @return                  a mutable set of ContainerModels.
   */
private Set<ContainerModel> buildContainerModels(Set<TaskModel> tasks, List<TaskGroup> containerTasks) {
    // Map task names to models
    Map<String, TaskModel> taskNameToModel = new HashMap<>();
    for (TaskModel model : tasks) {
        taskNameToModel.put(model.getTaskName().getTaskName(), model);
    }
    // Build container models
    Set<ContainerModel> containerModels = new HashSet<>();
    for (TaskGroup container : containerTasks) {
        Map<TaskName, TaskModel> containerTaskModels = new HashMap<>();
        for (String taskName : container.taskNames) {
            TaskModel model = taskNameToModel.get(taskName);
            containerTaskModels.put(model.getTaskName(), model);
        }
        containerModels.add(new ContainerModel(container.containerId, Integer.valueOf(container.containerId), containerTaskModels));
    }
    return Collections.unmodifiableSet(containerModels);
}
Also used : HashMap(java.util.HashMap) TaskName(org.apache.samza.container.TaskName) TaskModel(org.apache.samza.job.model.TaskModel) HashSet(java.util.HashSet) ContainerModel(org.apache.samza.job.model.ContainerModel)

Example 37 with TaskName

use of org.apache.samza.container.TaskName in project samza by apache.

the class SamzaObjectMapper method getObjectMapper.

/**
   * @return Returns a new ObjectMapper that's been configured to (de)serialize
   *         Samza's job data model, and simple data types such as TaskName,
   *         Partition, Config, and SystemStreamPartition.
   */
public static ObjectMapper getObjectMapper() {
    ObjectMapper mapper = new ObjectMapper();
    SimpleModule module = new SimpleModule("SamzaModule", new Version(1, 0, 0, ""));
    // Setup custom serdes for simple data types.
    module.addSerializer(Partition.class, new PartitionSerializer());
    module.addSerializer(SystemStreamPartition.class, new SystemStreamPartitionSerializer());
    module.addKeySerializer(SystemStreamPartition.class, new SystemStreamPartitionKeySerializer());
    module.addSerializer(TaskName.class, new TaskNameSerializer());
    module.addDeserializer(Partition.class, new PartitionDeserializer());
    module.addDeserializer(SystemStreamPartition.class, new SystemStreamPartitionDeserializer());
    module.addKeyDeserializer(SystemStreamPartition.class, new SystemStreamPartitionKeyDeserializer());
    module.addDeserializer(Config.class, new ConfigDeserializer());
    // Setup mixins for data models.
    mapper.getSerializationConfig().addMixInAnnotations(TaskModel.class, JsonTaskModelMixIn.class);
    mapper.getDeserializationConfig().addMixInAnnotations(TaskModel.class, JsonTaskModelMixIn.class);
    mapper.getSerializationConfig().addMixInAnnotations(ContainerModel.class, JsonContainerModelMixIn.class);
    mapper.getSerializationConfig().addMixInAnnotations(JobModel.class, JsonJobModelMixIn.class);
    mapper.getDeserializationConfig().addMixInAnnotations(JobModel.class, JsonJobModelMixIn.class);
    module.addDeserializer(ContainerModel.class, new JsonDeserializer<ContainerModel>() {

        @Override
        public ContainerModel deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException {
            ObjectCodec oc = jp.getCodec();
            JsonNode node = oc.readTree(jp);
            int containerId = node.get("container-id").getIntValue();
            if (node.get("container-id") == null) {
                throw new SamzaException("JobModel did not contain a container-id. This can never happen. JobModel corrupt!");
            }
            String processorId;
            if (node.get("processor-id") == null) {
                processorId = String.valueOf(containerId);
            } else {
                processorId = node.get("processor-id").getTextValue();
            }
            Map<TaskName, TaskModel> tasksMapping = OBJECT_MAPPER.readValue(node.get("tasks"), new TypeReference<Map<TaskName, TaskModel>>() {
            });
            return new ContainerModel(processorId, containerId, tasksMapping);
        }
    });
    // Convert camel case to hyphenated field names, and register the module.
    mapper.setPropertyNamingStrategy(new CamelCaseToDashesStrategy());
    mapper.registerModule(module);
    return mapper;
}
Also used : JsonNode(org.codehaus.jackson.JsonNode) ObjectCodec(org.codehaus.jackson.ObjectCodec) SamzaException(org.apache.samza.SamzaException) ContainerModel(org.apache.samza.job.model.ContainerModel) Version(org.codehaus.jackson.Version) DeserializationContext(org.codehaus.jackson.map.DeserializationContext) TypeReference(org.codehaus.jackson.type.TypeReference) JsonProcessingException(org.codehaus.jackson.JsonProcessingException) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) JsonParser(org.codehaus.jackson.JsonParser) IOException(java.io.IOException) TaskName(org.apache.samza.container.TaskName) HashMap(java.util.HashMap) Map(java.util.Map) SimpleModule(org.codehaus.jackson.map.module.SimpleModule) TaskModel(org.apache.samza.job.model.TaskModel)

Example 38 with TaskName

use of org.apache.samza.container.TaskName in project samza by apache.

the class MockTaskProxy method getJobModel.

@Override
protected JobModel getJobModel(JobInstance jobInstance) {
    if (jobInstance.getJobId().contains("Bad") || jobInstance.getJobName().contains("Bad")) {
        throw new IllegalArgumentException("No tasks found.");
    }
    TaskModel task1Model = new TaskModel(new TaskName(TASK_1_NAME), SYSTEM_STREAM_PARTITIONS, CHANGE_LOG_PARTITION);
    TaskModel task2Model = new TaskModel(new TaskName(TASK_2_NAME), SYSTEM_STREAM_PARTITIONS, CHANGE_LOG_PARTITION);
    ContainerModel task1ContainerModel = new ContainerModel(TASK_1_CONTAINER_ID, 1, ImmutableMap.of(new TaskName(TASK_1_NAME), task1Model));
    ContainerModel task2ContainerModel = new ContainerModel(TASK_2_CONTAINER_ID, 2, ImmutableMap.of(new TaskName(TASK_2_NAME), task2Model));
    return new JobModel(new MapConfig(), ImmutableMap.of(TASK_1_CONTAINER_ID, task1ContainerModel, TASK_2_CONTAINER_ID, task2ContainerModel));
}
Also used : TaskName(org.apache.samza.container.TaskName) JobModel(org.apache.samza.job.model.JobModel) MapConfig(org.apache.samza.config.MapConfig) TaskModel(org.apache.samza.job.model.TaskModel) ContainerModel(org.apache.samza.job.model.ContainerModel)

Aggregations

TaskName (org.apache.samza.container.TaskName)38 HashMap (java.util.HashMap)30 TaskInstance (org.apache.samza.container.TaskInstance)13 SystemStreamPartition (org.apache.samza.system.SystemStreamPartition)13 CountDownLatch (java.util.concurrent.CountDownLatch)11 HashSet (java.util.HashSet)10 Test (org.junit.Test)10 ContainerModel (org.apache.samza.job.model.ContainerModel)9 TaskModel (org.apache.samza.job.model.TaskModel)8 Set (java.util.Set)7 MapConfig (org.apache.samza.config.MapConfig)7 Partition (org.apache.samza.Partition)6 Config (org.apache.samza.config.Config)6 IncomingMessageEnvelope (org.apache.samza.system.IncomingMessageEnvelope)6 JobModel (org.apache.samza.job.model.JobModel)5 Map (java.util.Map)3 JobModelManager (org.apache.samza.coordinator.JobModelManager)3 IOException (java.io.IOException)2 LocalityManager (org.apache.samza.container.LocalityManager)2 HttpServer (org.apache.samza.coordinator.server.HttpServer)2