Search in sources :

Example 1 with ProcessingException

use of com.homeaway.datapullclient.exception.ProcessingException in project datapull by homeaway.

the class DataPullRequestProcessor method runDataPull.

private void runDataPull(String json, boolean isStart, boolean validateJson) throws ProcessingException {
    String originalInputJson = json;
    json = extractUserJsonFromS3IfProvided(json, isStart);
    final EMRProperties emrProperties = this.config.getEmrProperties();
    if (log.isDebugEnabled())
        log.debug("runDataPull -> json = " + json + " isStart = " + isStart);
    try {
        if (validateJson) {
            json = validateAndEnrich(json);
        }
        log.info("Running datapull for json : " + json + " cron expression = " + isStart + "env =" + env);
        final ObjectNode node = new ObjectMapper().readValue(json, ObjectNode.class);
        List<Map.Entry<String, JsonNode>> result = new LinkedList<Map.Entry<String, JsonNode>>();
        Iterator<Map.Entry<String, JsonNode>> nodes = node.fields();
        while (nodes.hasNext()) {
            result.add(nodes.next());
        }
        JsonNode clusterNode = result.stream().filter(y -> y.getKey().equalsIgnoreCase("cluster")).map(x -> x.getValue()).findAny().get();
        JsonNode migrationsNode = result.stream().filter(y -> y.getKey().equalsIgnoreCase("migrations")).map(x -> x.getValue()).findAny().get();
        if (clusterNode == null)
            throw new ProcessingException("Invalid Json!!! Cluster properties cannot be null");
        String creator = node.has(CREATOR) ? node.findValue(CREATOR).asText() : "";
        ObjectMapper mapper = new ObjectMapper();
        ClusterProperties reader = mapper.treeToValue(clusterNode, ClusterProperties.class);
        Migration[] myObjects = mapper.treeToValue(migrationsNode, Migration[].class);
        String cronExp = Objects.toString(reader.getCronExpression(), "");
        if (!cronExp.isEmpty())
            cronExp = validateAndProcessCronExpression(cronExp);
        String pipeline = Objects.toString(reader.getPipelineName(), UUID.randomUUID().toString());
        String pipelineEnv = Objects.toString(reader.getAwsEnv(), env);
        DataPullProperties dataPullProperties = config.getDataPullProperties();
        String applicationHistoryFolder = dataPullProperties.getApplicationHistoryFolder();
        String s3RepositoryBucketName = dataPullProperties.getS3BucketName();
        String jobName = pipelineEnv + PIPELINE_NAME_DELIMITER + EMR + PIPELINE_NAME_DELIMITER + pipeline + PIPELINE_NAME_DELIMITER + PIPELINE_NAME_SUFFIX;
        String applicationHistoryFolderPath = applicationHistoryFolder == null || applicationHistoryFolder.isEmpty() ? s3RepositoryBucketName + "/" + DATAPULL_HISTORY_FOLDER : applicationHistoryFolder;
        String bootstrapFilePath = s3RepositoryBucketName + "/" + BOOTSTRAP_FOLDER;
        String filePath = applicationHistoryFolderPath + "/" + jobName;
        String bootstrapFile = jobName + ".sh";
        String jksFilePath = bootstrapFilePath + "/" + bootstrapFile;
        String bootstrapActionStringFromUser = Objects.toString(reader.getBootstrapactionstring(), "");
        String defaultBootstrapString = emrProperties.getDefaultBootstrapString();
        Boolean haveBootstrapAction = createBootstrapScript(myObjects, bootstrapFile, bootstrapFilePath, bootstrapActionStringFromUser, defaultBootstrapString);
        DataPullTask task = createDataPullTask(filePath, jksFilePath, reader, jobName, creator, node.path("sparkjarfile").asText(), haveBootstrapAction);
        if (!isStart) {
            json = originalInputJson.equals(json) ? json : originalInputJson;
            saveConfig(applicationHistoryFolderPath, jobName + ".json", json);
        }
        if (!isStart && tasksMap.containsKey(jobName))
            cancelExistingTask(jobName);
        if (!(isStart && cronExp.isEmpty())) {
            Future<?> future = !cronExp.isEmpty() ? scheduler.schedule(task, new CronTrigger(cronExp)) : scheduler.schedule(task, new Date(System.currentTimeMillis() + 1 * 1000));
            tasksMap.put(jobName, future);
        }
    } catch (IOException e) {
        throw new ProcessingException("exception while starting datapull " + e.getLocalizedMessage());
    }
    if (log.isDebugEnabled())
        log.debug("runDataPull <- return");
}
Also used : java.util(java.util) DataPullClientService(com.homeaway.datapullclient.service.DataPullClientService) DataPullContextHolder(com.homeaway.datapullclient.config.DataPullContextHolder) Autowired(org.springframework.beans.factory.annotation.Autowired) DataPullProperties(com.homeaway.datapullclient.config.DataPullProperties) ProcessingException(com.homeaway.datapullclient.exception.ProcessingException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) EMRProperties(com.homeaway.datapullclient.config.EMRProperties) Value(org.springframework.beans.factory.annotation.Value) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) Future(java.util.concurrent.Future) JSONObject(org.json.JSONObject) ByteArrayInputStream(java.io.ByteArrayInputStream) JsonInputFile(com.homeaway.datapullclient.input.JsonInputFile) Service(org.springframework.stereotype.Service) AmazonS3(com.amazonaws.services.s3.AmazonS3) JsonNode(com.fasterxml.jackson.databind.JsonNode) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) SchemaLoader(org.everit.json.schema.loader.SchemaLoader) Resource(org.springframework.core.io.Resource) Migration(com.homeaway.datapullclient.input.Migration) ValidationException(org.everit.json.schema.ValidationException) Source(com.homeaway.datapullclient.input.Source) DataPullContext(com.homeaway.datapullclient.config.DataPullContext) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JSONTokener(org.json.JSONTokener) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) IOException(java.io.IOException) com.amazonaws.services.s3.model(com.amazonaws.services.s3.model) ClusterProperties(com.homeaway.datapullclient.input.ClusterProperties) InputStreamReader(java.io.InputStreamReader) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) CronTrigger(org.springframework.scheduling.support.CronTrigger) InvalidPointedJsonException(com.homeaway.datapullclient.exception.InvalidPointedJsonException) Slf4j(lombok.extern.slf4j.Slf4j) PostConstruct(javax.annotation.PostConstruct) DataPullClientConfig(com.homeaway.datapullclient.config.DataPullClientConfig) Schema(org.everit.json.schema.Schema) ResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver) BufferedReader(java.io.BufferedReader) CronTrigger(org.springframework.scheduling.support.CronTrigger) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Migration(com.homeaway.datapullclient.input.Migration) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) DataPullProperties(com.homeaway.datapullclient.config.DataPullProperties) ClusterProperties(com.homeaway.datapullclient.input.ClusterProperties) EMRProperties(com.homeaway.datapullclient.config.EMRProperties) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ProcessingException(com.homeaway.datapullclient.exception.ProcessingException)

Example 2 with ProcessingException

use of com.homeaway.datapullclient.exception.ProcessingException in project datapull by homeaway.

the class DataPullRequestProcessor method readFileFromS3.

public String readFileFromS3(AmazonS3 s3Client, String bucketName, String path) throws ProcessingException {
    S3Object object = s3Client.getObject(new GetObjectRequest(bucketName, path));
    StringBuilder out = new StringBuilder();
    try (BufferedReader reader = new BufferedReader(new InputStreamReader(object.getObjectContent()))) {
        String line;
        while ((line = reader.readLine()) != null) {
            out.append(line);
        }
    } catch (IOException exception) {
        throw new ProcessingException("Input json file invalid");
    }
    return out.toString();
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) ProcessingException(com.homeaway.datapullclient.exception.ProcessingException)

Example 3 with ProcessingException

use of com.homeaway.datapullclient.exception.ProcessingException in project datapull by homeaway.

the class DataPullRequestProcessor method runHistoricalTasksAndReadSchemaJson.

@PostConstruct
public void runHistoricalTasksAndReadSchemaJson() throws ProcessingException {
    readExistingDataPullInputs();
    try {
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        Resource[] resources = resolver.getResources("classpath*:/input_json_schema.json");
        BufferedReader reader = new BufferedReader(new InputStreamReader(resources[0].getInputStream()));
        JSONObject jsonSchema = new JSONObject(new JSONTokener(resources[0].getInputStream()));
        inputJsonSchema = SchemaLoader.load(jsonSchema);
    } catch (IOException e) {
        throw new ProcessingException("Unable to create JSON validator", e);
    }
}
Also used : JSONTokener(org.json.JSONTokener) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) ResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver) InputStreamReader(java.io.InputStreamReader) JSONObject(org.json.JSONObject) Resource(org.springframework.core.io.Resource) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) ProcessingException(com.homeaway.datapullclient.exception.ProcessingException) PostConstruct(javax.annotation.PostConstruct)

Example 4 with ProcessingException

use of com.homeaway.datapullclient.exception.ProcessingException in project datapull by homeaway.

the class DataPullRequestHandler method startDataPull.

@Override
public ResponseEntity startDataPull(HttpEntity<String> inputJson) {
    if (log.isDebugEnabled())
        log.debug("startDataPull -> inputJson=" + inputJson);
    ResponseEntity entity = null;
    try {
        service.runDataPull(inputJson.getBody());
        entity = new ResponseEntity(HttpStatus.ACCEPTED.value(), "Request Succesfully registered : " + inputJson);
    } catch (ProcessingException e) {
        throw new InputException("DataPull application failed for inputJson : " + inputJson + " \n " + e.getMessage());
    }
    if (log.isDebugEnabled())
        log.debug("startDataPull <- return");
    return entity;
}
Also used : SimpleResponseEntity(com.homeaway.datapullclient.data.SimpleResponseEntity) ResponseEntity(com.homeaway.datapullclient.data.ResponseEntity) InputException(com.homeaway.datapullclient.exception.InputException) ProcessingException(com.homeaway.datapullclient.exception.ProcessingException)

Example 5 with ProcessingException

use of com.homeaway.datapullclient.exception.ProcessingException in project datapull by homeaway.

the class DataPullRequestProcessor method extractUserJsonFromS3IfProvided.

public String extractUserJsonFromS3IfProvided(String json, boolean isStart) throws ProcessingException {
    List<String> jsonS3PathList = new ArrayList<>();
    try {
        ObjectMapper mapper = new ObjectMapper();
        JsonNode jsonNode = mapper.readTree(json);
        while (jsonNode.has("jsoninputfile")) {
            JsonNode jsonInputFileNode = jsonNode.path("jsoninputfile");
            JsonInputFile jsonInputFile = mapper.treeToValue(jsonInputFileNode, JsonInputFile.class);
            String s3path = jsonInputFile.getS3Path();
            if (jsonS3PathList.contains(s3path)) {
                throw new ProcessingException("JSON is pointing to same JSON.");
            }
            jsonS3PathList.add(s3path);
            AmazonS3 s3Client = config.getS3Client();
            String bucketName = s3path.substring(0, s3path.indexOf("/"));
            String path = s3path.substring(s3path.indexOf("/") + 1);
            json = readFileFromS3(s3Client, bucketName, path);
            jsonNode = mapper.readTree(json);
        }
    } catch (IOException e) {
        if (isStart) {
            if (jsonS3PathList.size() != 0) {
                throw new InvalidPointedJsonException("Invalid input json at path - " + jsonS3PathList.get(jsonS3PathList.size() - 1));
            } else {
                throw new InvalidPointedJsonException("Invalid input json - " + json);
            }
        } else {
            throw new ProcessingException(e.getMessage());
        }
    }
    return json;
}
Also used : JsonInputFile(com.homeaway.datapullclient.input.JsonInputFile) AmazonS3(com.amazonaws.services.s3.AmazonS3) InvalidPointedJsonException(com.homeaway.datapullclient.exception.InvalidPointedJsonException) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ProcessingException(com.homeaway.datapullclient.exception.ProcessingException)

Aggregations

ProcessingException (com.homeaway.datapullclient.exception.ProcessingException)7 IOException (java.io.IOException)4 BufferedReader (java.io.BufferedReader)3 InputStreamReader (java.io.InputStreamReader)3 JSONObject (org.json.JSONObject)3 JSONTokener (org.json.JSONTokener)3 AmazonS3 (com.amazonaws.services.s3.AmazonS3)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 DataPullContext (com.homeaway.datapullclient.config.DataPullContext)2 SimpleResponseEntity (com.homeaway.datapullclient.data.SimpleResponseEntity)2 InputException (com.homeaway.datapullclient.exception.InputException)2 InvalidPointedJsonException (com.homeaway.datapullclient.exception.InvalidPointedJsonException)2 JsonInputFile (com.homeaway.datapullclient.input.JsonInputFile)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 PostConstruct (javax.annotation.PostConstruct)2 ValidationException (org.everit.json.schema.ValidationException)2 Resource (org.springframework.core.io.Resource)2 PathMatchingResourcePatternResolver (org.springframework.core.io.support.PathMatchingResourcePatternResolver)2 ResourcePatternResolver (org.springframework.core.io.support.ResourcePatternResolver)2