Search in sources :

Example 1 with Migration

use of com.homeaway.datapullclient.input.Migration 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 Migration

use of com.homeaway.datapullclient.input.Migration in project datapull by homeaway.

the class DataPullRequestProcessor method createBootstrapScript.

private Boolean createBootstrapScript(Migration[] myObjects, String bootstrapFile, String bootstrapFilePath, String bootstrapActionStringFromUser, String defaultbootstrapString) throws ProcessingException {
    StringBuilder stringBuilder = new StringBuilder();
    List<String> list = new ArrayList<>();
    Boolean haveBootstrapAction = false;
    for (Migration mig : myObjects) {
        if (mig.getSource() != null) {
            if (mig.getSource().getJksfiles() != null) {
                list.addAll(Arrays.asList(mig.getSource().getJksfiles()));
            }
        } else {
            Source[] sources = mig.getSources();
            if (sources != null && sources.length > 0) {
                for (Source source : sources) {
                    if (source.getJksfiles() != null) {
                        list.addAll(Arrays.asList(source.getJksfiles()));
                    }
                }
            }
        }
        if (mig.getDestination() != null) {
            if (mig.getDestination().getJksfiles() != null) {
                list.addAll(Arrays.asList(mig.getDestination().getJksfiles()));
            }
        }
    }
    if (!list.isEmpty() || !bootstrapActionStringFromUser.isEmpty()) {
        stringBuilder = createBootstrapString(list.toArray(), bootstrapActionStringFromUser);
    }
    if (!defaultbootstrapString.isEmpty()) {
        stringBuilder.append(defaultbootstrapString);
    }
    if (stringBuilder.length() > 0) {
        saveConfig(bootstrapFilePath, bootstrapFile, stringBuilder.toString());
        haveBootstrapAction = true;
    }
    return haveBootstrapAction;
}
Also used : Migration(com.homeaway.datapullclient.input.Migration) Source(com.homeaway.datapullclient.input.Source)

Aggregations

Migration (com.homeaway.datapullclient.input.Migration)2 Source (com.homeaway.datapullclient.input.Source)2 AmazonS3 (com.amazonaws.services.s3.AmazonS3)1 com.amazonaws.services.s3.model (com.amazonaws.services.s3.model)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 DataPullClientConfig (com.homeaway.datapullclient.config.DataPullClientConfig)1 DataPullContext (com.homeaway.datapullclient.config.DataPullContext)1 DataPullContextHolder (com.homeaway.datapullclient.config.DataPullContextHolder)1 DataPullProperties (com.homeaway.datapullclient.config.DataPullProperties)1 EMRProperties (com.homeaway.datapullclient.config.EMRProperties)1 InvalidPointedJsonException (com.homeaway.datapullclient.exception.InvalidPointedJsonException)1 ProcessingException (com.homeaway.datapullclient.exception.ProcessingException)1 ClusterProperties (com.homeaway.datapullclient.input.ClusterProperties)1 JsonInputFile (com.homeaway.datapullclient.input.JsonInputFile)1 DataPullClientService (com.homeaway.datapullclient.service.DataPullClientService)1 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1