Search in sources :

Example 56 with SamzaException

use of org.apache.samza.SamzaException 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 57 with SamzaException

use of org.apache.samza.SamzaException in project samza by apache.

the class TaskFactoryUtil method fromTaskClassConfig.

/**
   * Create {@link StreamTaskFactory} or {@link AsyncStreamTaskFactory} based on the configured task.class.
   * @param config the {@link Config}
   * @return task factory instance
   */
private static Object fromTaskClassConfig(Config config) {
    // if there is configuration to set the job w/ a specific type of task, instantiate the corresponding task factory
    String taskClassName = new TaskConfig(config).getTaskClass().getOrElse(new AbstractFunction0<String>() {

        @Override
        public String apply() {
            throw new ConfigException("There is no task class defined in the configuration. Failed to create a valid TaskFactory");
        }
    });
    log.info("Got task class name: {}", taskClassName);
    boolean isAsyncTaskClass;
    try {
        isAsyncTaskClass = AsyncStreamTask.class.isAssignableFrom(Class.forName(taskClassName));
    } catch (Throwable t) {
        throw new ConfigException(String.format("Invalid configuration for AsyncStreamTask class: %s", taskClassName), t);
    }
    if (isAsyncTaskClass) {
        return new AsyncStreamTaskFactory() {

            @Override
            public AsyncStreamTask createInstance() {
                try {
                    return (AsyncStreamTask) Class.forName(taskClassName).newInstance();
                } catch (Throwable t) {
                    log.error("Error loading AsyncStreamTask class: {}. error: {}", taskClassName, t);
                    throw new SamzaException(String.format("Error loading AsyncStreamTask class: %s", taskClassName), t);
                }
            }
        };
    }
    return new StreamTaskFactory() {

        @Override
        public StreamTask createInstance() {
            try {
                return (StreamTask) Class.forName(taskClassName).newInstance();
            } catch (Throwable t) {
                log.error("Error loading StreamTask class: {}. error: {}", taskClassName, t);
                throw new SamzaException(String.format("Error loading StreamTask class: %s", taskClassName), t);
            }
        }
    };
}
Also used : TaskConfig(org.apache.samza.config.TaskConfig) ConfigException(org.apache.samza.config.ConfigException) SamzaException(org.apache.samza.SamzaException)

Example 58 with SamzaException

use of org.apache.samza.SamzaException in project samza by apache.

the class LocalApplicationRunner method run.

@Override
public void run(StreamApplication app) {
    try {
        // 1. initialize and plan
        ExecutionPlan plan = getExecutionPlan(app);
        writePlanJsonFile(plan.getPlanAsJson());
        // 2. create the necessary streams
        createStreams(plan.getIntermediateStreams());
        // 3. create the StreamProcessors
        if (plan.getJobConfigs().isEmpty()) {
            throw new SamzaException("No jobs to run.");
        }
        plan.getJobConfigs().forEach(jobConfig -> {
            log.debug("Starting job {} StreamProcessor with config {}", jobConfig.getName(), jobConfig);
            LocalStreamProcessorLifeCycleListener listener = new LocalStreamProcessorLifeCycleListener();
            StreamProcessor processor = createStreamProcessor(jobConfig, app, listener);
            listener.setProcessor(processor);
            processors.add(processor);
        });
        numProcessorsToStart.set(processors.size());
        // 4. start the StreamProcessors
        processors.forEach(StreamProcessor::start);
    } catch (Exception e) {
        throw new SamzaException("Failed to start application", e);
    }
}
Also used : StreamProcessor(org.apache.samza.processor.StreamProcessor) ExecutionPlan(org.apache.samza.execution.ExecutionPlan) SamzaException(org.apache.samza.SamzaException) SamzaException(org.apache.samza.SamzaException)

Aggregations

SamzaException (org.apache.samza.SamzaException)58 IOException (java.io.IOException)15 HashMap (java.util.HashMap)9 Config (org.apache.samza.config.Config)6 Test (org.junit.Test)6 Partition (org.apache.samza.Partition)5 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)5 HashSet (java.util.HashSet)4 JobConfig (org.apache.samza.config.JobConfig)4 MapConfig (org.apache.samza.config.MapConfig)4 Map (java.util.Map)3 ExecutionPlan (org.apache.samza.execution.ExecutionPlan)3 IncomingMessageEnvelope (org.apache.samza.system.IncomingMessageEnvelope)3 SystemFactory (org.apache.samza.system.SystemFactory)3 URI (java.net.URI)2 ArrayList (java.util.ArrayList)2 LinkedHashSet (java.util.LinkedHashSet)2 List (java.util.List)2 ZkInterruptedException (org.I0Itec.zkclient.exception.ZkInterruptedException)2 Path (org.apache.hadoop.fs.Path)2