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;
}
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);
}
}
};
}
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);
}
}
Aggregations