Example usage for org.apache.hadoop.yarn.api.records ApplicationId toString

List of usage examples for org.apache.hadoop.yarn.api.records ApplicationId toString

Introduction

In this page you can find the example usage for org.apache.hadoop.yarn.api.records ApplicationId toString.

Prototype

@Override
    public String toString() 

Source Link

Usage

From source file:com.twitter.pig.backend.hadoop.executionengine.tez.TezJobControlCompiler.java

License:Apache License

/**
 * The method that creates the Job corresponding to a MapReduceOper.
 * The assumption is that//w  ww .j a  va2 s .  c  o m
 * every MapReduceOper will have a load and a store. The JobConf removes
 * the load operator and serializes the input filespec so that PigInputFormat can
 * take over the creation of splits. It also removes the store operator
 * and serializes the output filespec so that PigOutputFormat can take over
 * record writing. The remaining portion of the map plan and reduce plans are
 * serialized and stored for the PigMapReduce or PigMapOnly objects to take over
 * the actual running of the plans.
 * The Mapper & Reducer classes and the required key value formats are set.
 * Checks if this is a map only job and uses PigMapOnly class as the mapper
 * and uses PigMapReduce otherwise.
 * If it is a Map Reduce job, it is bound to have a package operator. Remove it from
 * the reduce plan and serializes it so that the PigMapReduce class can use it to package
 * the indexed tuples received by the reducer.
 * @param mro - The MapReduceOper for which the JobConf is required
 * @param config - the Configuration object from which JobConf is built
 * @param pigContext - The PigContext passed on from execution engine
 * @param mrrChain 
 * @return Job corresponding to mro
 * @throws JobCreationException
 */
@SuppressWarnings({ "unchecked", "deprecation" })
private TezJob getJob(TezOperPlan dag, TezConfiguration conf, PigContext pigContext)
        throws JobCreationException {

    org.apache.hadoop.mapreduce.Job nwJob = null;

    try {
        nwJob = new org.apache.hadoop.mapreduce.Job(conf);
    } catch (Exception e) {
        throw new JobCreationException(e);
    }

    //Configuration conf = nwJob.getConfiguration();

    try {
        String buffPercent = conf.get("mapred.job.reduce.markreset.buffer.percent");
        if (buffPercent == null || Double.parseDouble(buffPercent) <= 0) {
            log.info("mapred.job.reduce.markreset.buffer.percent is not set, set to default 0.3");
            conf.set("mapred.job.reduce.markreset.buffer.percent", "0.3");
        } else {
            log.info("mapred.job.reduce.markreset.buffer.percent is set to "
                    + conf.get("mapred.job.reduce.markreset.buffer.percent"));
        }

        // Convert mapred.output.* to output.compression.*, See PIG-1791
        if ("true".equals(conf.get("mapred.output.compress"))) {
            conf.set("output.compression.enabled", "true");
            String codec = conf.get("mapred.output.compression.codec");
            if (codec == null) {
                throw new JobCreationException(
                        "'mapred.output.compress' is set but no value is specified for 'mapred.output.compression.codec'.");
            } else {
                conf.set("output.compression.codec", codec);
            }
        }

        // if user specified the job name using -D switch, Pig won't reset the name then.
        if (System.getProperty("mapred.job.name") == null
                && pigContext.getProperties().getProperty(PigContext.JOB_NAME) != null) {
            conf.set("mapreduce.job.name", pigContext.getProperties().getProperty(PigContext.JOB_NAME));
            //nwJob.setJobName(pigContext.getProperties().getProperty(PigContext.JOB_NAME));
        }

        if (pigContext.getProperties().getProperty(PigContext.JOB_PRIORITY) != null) {
            // If the job priority was set, attempt to get the corresponding enum value
            // and set the hadoop job priority.
            String jobPriority = pigContext.getProperties().getProperty(PigContext.JOB_PRIORITY).toUpperCase();
            try {
                // Allow arbitrary case; the Hadoop job priorities are all upper case.
                conf.set("mapred.job.priority", JobPriority.valueOf(jobPriority).toString());

            } catch (IllegalArgumentException e) {
                StringBuffer sb = new StringBuffer("The job priority must be one of [");
                JobPriority[] priorities = JobPriority.values();
                for (int i = 0; i < priorities.length; ++i) {
                    if (i > 0)
                        sb.append(", ");
                    sb.append(priorities[i]);
                }
                sb.append("].  You specified [" + jobPriority + "]");
                throw new JobCreationException(sb.toString());
            }
        }

        nwJob.setInputFormatClass(PigInputFormat.class);
        nwJob.setOutputFormatClass(PigOutputFormat.class);

        conf.setClass("mapreduce.job.inputformat.class", PigInputFormat.class, InputFormat.class);
        conf.setClass("mapreduce.job.outputformat.class", PigOutputFormat.class, OutputFormat.class);

        //nwJob.setInputFormatClass(PigInputFormat.class);
        //nwJob.setOutputFormatClass(PigOutputFormat.class);

        // tmp file compression setups
        if (Utils.tmpFileCompression(pigContext)) {
            conf.setBoolean("pig.tmpfilecompression", true);
            conf.set("pig.tmpfilecompression.codec", Utils.tmpFileCompressionCodec(pigContext));
        }

        // It's a hack to set distributed cache file for hadoop 23. Once MiniMRCluster do not require local
        // jar on fixed location, this can be removed
        if (pigContext.getExecType() == ExecType.MAPREDUCE) {
            String newfiles = conf.get("alternative.mapreduce.job.cache.files");
            if (newfiles != null) {
                String files = conf.get("mapreduce.job.cache.files");
                conf.set("mapreduce.job.cache.files",
                        files == null ? newfiles.toString() : files + "," + newfiles);
            }
        }

        // Serialize the UDF specific context info.
        UDFContext.getUDFContext().serialize(conf);

        FileSystem remoteFs = FileSystem.get(conf);

        ApplicationId appId = tezClient.createApplication();

        Path remoteStagingDir = remoteFs.makeQualified(new Path(
                conf.get(TezConfiguration.TEZ_AM_STAGING_DIR, TezConfiguration.TEZ_AM_STAGING_DIR_DEFAULT),
                appId.toString()));
        tezClient.ensureExists(remoteStagingDir);

        DAG tezDag = createDAG(plan, remoteFs, conf, appId, remoteStagingDir);

        Map<String, LocalResource> amLocalResources = new HashMap<String, LocalResource>();

        amLocalResources.put("pig-tez.jar",
                tezDag.getVertices().get(0).getTaskLocalResources().get("pig-tez.jar"));
        amLocalResources.put("dag_job.jar",
                tezDag.getVertices().get(0).getTaskLocalResources().get("dag_job.jar"));

        return new TezJob(conf, appId, tezDag, remoteStagingDir, null, null, null, null, amLocalResources);

    } catch (Exception e) {
        int errCode = 2017;
        String msg = "Internal error creating job configuration.";
        throw new JobCreationException(msg, errCode, PigException.BUG, e);
    }

    //jobStoreMap.put(cjob,new Pair<List<POStore>, Path>(storeLocations, tmpLocation));

    /*
    } catch (JobCreationException jce) {
    throw jce;
    } catch(Exception e) {
    int errCode = 2017;
    String msg = "Internal error creating job configuration.";
    throw new JobCreationException(msg, errCode, PigException.BUG, e);
    }   */

}

From source file:de.huberlin.wbi.hiway.common.Client.java

License:Apache License

/**
 * Main run function for the client.//from ww  w .ja  va 2s  . c  o  m
 * 
 * @return true if application completed successfully.
 */
private boolean run() throws IOException, YarnException {

    /* log */ System.out.println("Running Client");

    yarnClient.start();
    YarnClusterMetrics clusterMetrics = yarnClient.getYarnClusterMetrics();

    /* log */ System.out.println(
            "Got Cluster metric info from ASM" + ", numNodeManagers=" + clusterMetrics.getNumNodeManagers());

    List<NodeReport> clusterNodeReports = yarnClient.getNodeReports(NodeState.RUNNING);
    /* log */ System.out.println("Got Cluster node info from ASM");
    /* log */ for (NodeReport node : clusterNodeReports)
        System.out.println("Got node report from ASM for" + ", nodeId=" + node.getNodeId() + ", nodeAddress"
                + node.getHttpAddress() + ", nodeRackName" + node.getRackName() + ", nodeNumContainers"
                + node.getNumContainers());

    QueueInfo queueInfo = yarnClient.getQueueInfo(this.amQueue);
    /* log */ System.out.println("Queue info" + ", queueName=" + queueInfo.getQueueName()
            + ", queueCurrentCapacity=" + queueInfo.getCurrentCapacity() + ", queueMaxCapacity="
            + queueInfo.getMaximumCapacity() + ", queueApplicationCount=" + queueInfo.getApplications().size()
            + ", queueChildQueueCount=" + queueInfo.getChildQueues().size());

    List<QueueUserACLInfo> listAclInfo = yarnClient.getQueueAclsInfo();
    /* log */ for (QueueUserACLInfo aclInfo : listAclInfo)
        for (QueueACL userAcl : aclInfo.getUserAcls())
            System.out.println("User ACL Info for Queue" + ", queueName=" + aclInfo.getQueueName()
                    + ", userAcl=" + userAcl.name());

    // Get a new application id
    YarnClientApplication app = yarnClient.createApplication();
    GetNewApplicationResponse appResponse = app.getNewApplicationResponse();

    // Get min/max resource capabilities from RM and change memory ask if needed
    int maxVC = appResponse.getMaximumResourceCapability().getVirtualCores();
    /* log */ System.out.println("Max vCores capabililty of resources in this cluster " + maxVC);
    int maxMem = appResponse.getMaximumResourceCapability().getMemory();
    /* log */ System.out.println("Max mem capabililty of resources in this cluster " + maxMem);
    // A resource ask cannot exceed the max.
    if (amVCores > maxVC) {
        /* log */ System.out.println("AM vCores specified above max threshold of cluster. Using max value."
                + ", specified=" + amVCores + ", max=" + maxVC);
        amVCores = maxVC;
    }
    if (amMemory > maxMem) {
        /* log */ System.out.println("AM memory specified above max threshold of cluster. Using max value."
                + ", specified=" + amMemory + ", max=" + maxMem);
        amMemory = maxMem;
    }

    // set the application name
    ApplicationSubmissionContext appContext = app.getApplicationSubmissionContext();
    appContext.setApplicationType(conf.get(HiWayConfiguration.HIWAY_AM_APPLICATION_TYPE,
            HiWayConfiguration.HIWAY_AM_APPLICATION_TYPE_DEFAULT));
    appContext.setApplicationName("run " + workflowParam + " (type: " + workflowType.toString() + ")");
    ApplicationId appId = appContext.getApplicationId();
    String hdfsBaseDirectoryName = conf.get(HiWayConfiguration.HIWAY_AM_DIRECTORY_BASE,
            HiWayConfiguration.HIWAY_AM_DIRECTORY_BASE_DEFAULT);
    String hdfsSandboxDirectoryName = conf.get(HiWayConfiguration.HIWAY_AM_DIRECTORY_CACHE,
            HiWayConfiguration.HIWAY_AM_DIRECTORY_CACHE_DEFAULT);
    Path hdfsBaseDirectory = new Path(new Path(hdfs.getUri()), hdfsBaseDirectoryName);
    Data.setHdfsBaseDirectory(hdfsBaseDirectory);
    Path hdfsSandboxDirectory = new Path(hdfsBaseDirectory, hdfsSandboxDirectoryName);
    Path hdfsApplicationDirectory = new Path(hdfsSandboxDirectory, appId.toString());
    Data.setHdfsApplicationDirectory(hdfsApplicationDirectory);
    Data.setHdfs(hdfs);

    Path wfSource, wfDest, wfTemp = null;
    try {
        wfSource = new Path(new URI(workflowParam).getPath());
    } catch (URISyntaxException e) {
        wfSource = new Path(workflowParam);
    }
    wfDest = new Path(hdfsApplicationDirectory + "/" + wfSource.getName());

    // (1) if workflow file in hdfs, then transfer to temp file in local fs
    if (hdfs.exists(wfSource)) {
        wfTemp = new Path("./." + wfSource.getName());
        System.out.println("Workflow found in HDFS at location " + wfSource);
        hdfs.copyToLocalFile(false, wfSource, wfTemp);
    }

    // (2) if galaxy workflow, then copy and replace input ports
    if (workflowType.equals(HiWayConfiguration.HIWAY_WORKFLOW_LANGUAGE_OPTS.galaxy)) {
        wfTemp = preProcessGalaxyWorkflow(wfSource, wfTemp);
    }

    if (wfTemp != null) {
        hdfs.copyFromLocalFile(wfTemp, wfDest);
        new File(wfTemp.toString()).delete();
    } else {
        hdfs.copyFromLocalFile(wfSource, wfDest);
    }

    if (summaryPath != null)
        summary = new Data(summaryPath);
    if (customMemPath != null)
        (new Data(customMemPath)).stageOut();

    // Set up the container launch context for the application master
    ContainerLaunchContext amContainer = Records.newRecord(ContainerLaunchContext.class);

    /* set the env variables to be setup in the env where the application master will be run */
    System.out.println("Set the environment for the application master");
    Map<String, String> env = new HashMap<>();

    StringBuilder classPathEnv = new StringBuilder(Environment.CLASSPATH.$()).append(File.pathSeparatorChar)
            .append("./*");
    for (String c : conf.getStrings(YarnConfiguration.YARN_APPLICATION_CLASSPATH,
            YarnConfiguration.DEFAULT_YARN_APPLICATION_CLASSPATH)) {
        classPathEnv.append(':');
        classPathEnv.append(File.pathSeparatorChar);
        classPathEnv.append(c.trim());
    }

    if (conf.getBoolean(YarnConfiguration.IS_MINI_YARN_CLUSTER, false)) {
        classPathEnv.append(':');
        classPathEnv.append(System.getProperty("java.class.path"));
    }

    env.put("CLASSPATH", classPathEnv.toString());

    amContainer.setEnvironment(env);

    // Set the necessary command to execute the application master
    Vector<CharSequence> vargs = new Vector<>(30);

    // Set java executable command
    System.out.println("Setting up app master command");
    vargs.add(Environment.JAVA_HOME.$() + "/bin/java");
    if (HiWayConfiguration.debug)
        vargs.add(
                "-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9010 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false");
    // Set Xmx based on am memory size
    vargs.add("-Xmx" + amMemory + "m");
    vargs.add("-Xss" + "16m");
    // Set class name

    switch (workflowType) {
    case dax:
        vargs.add(HiWayConfiguration.HIWAY_WORKFLOW_LANGUAGE_DAX_AM_CLASS);
        break;
    case log:
        vargs.add(HiWayConfiguration.HIWAY_WORKFLOW_LANGUAGE_LOG_AM_CLASS);
        break;
    case galaxy:
        vargs.add(HiWayConfiguration.HIWAY_WORKFLOW_LANGUAGE_GALAXY_AM_CLASS);
        break;
    case cuneiformE:
        vargs.add(HiWayConfiguration.HIWAY_WORKFLOW_LANGUAGE_CUNEIFORME_AM_CLASS);
        break;
    default:
        vargs.add(HiWayConfiguration.HIWAY_WORKFLOW_LANGUAGE_CUNEIFORMJ_AM_CLASS);
    }

    vargs.add("--scheduler " + schedulerName.toString());
    if (memory != null)
        vargs.add("--memory " + memory);
    if (summary != null)
        vargs.add("--summary " + summary.getName());
    if (customMemPath != null)
        vargs.add("--custom " + customMemPath);
    vargs.add("--appid " + appId.toString());
    if (HiWayConfiguration.debug)
        vargs.add("--debug");
    if (HiWayConfiguration.verbose)
        vargs.add("--verbose");
    vargs.add(workflowParam);
    vargs.add("> >(tee AppMaster.stdout " + ApplicationConstants.LOG_DIR_EXPANSION_VAR + "/AppMaster.stdout)");
    vargs.add("2> >(tee AppMaster.stderr " + ApplicationConstants.LOG_DIR_EXPANSION_VAR
            + "/AppMaster.stderr >&2)");

    // Get final command
    StringBuilder command = new StringBuilder();
    for (CharSequence str : vargs) {
        command.append(str).append(" ");
    }

    System.out.println("Completed setting up app master command " + command.toString());
    List<String> commands = new ArrayList<>();
    commands.add(command.toString());
    amContainer.setCommands(commands);

    // Set up resource type requirements
    Resource capability = Records.newRecord(Resource.class);
    capability.setVirtualCores(amVCores);
    capability.setMemory(amMemory);
    appContext.setResource(capability);

    // Setup security tokens
    if (UserGroupInformation.isSecurityEnabled()) {
        Credentials credentials = new Credentials();
        String tokenRenewer = conf.get(YarnConfiguration.RM_PRINCIPAL);
        if (tokenRenewer == null || tokenRenewer.length() == 0) {
            throw new IOException("Can't get Master Kerberos principal for the RM to use as renewer");
        }

        // For now, only getting tokens for the default file-system.
        final Token<?> tokens[] = hdfs.addDelegationTokens(tokenRenewer, credentials);
        if (tokens != null) {
            for (Token<?> token : tokens) {
                System.out.println("Got dt for " + hdfs.getUri() + "; " + token);
            }
        }
        try (DataOutputBuffer dob = new DataOutputBuffer()) {
            credentials.writeTokenStorageToStream(dob);
            ByteBuffer fsTokens = ByteBuffer.wrap(dob.getData(), 0, dob.getLength());
            amContainer.setTokens(fsTokens);
        }
    }

    appContext.setAMContainerSpec(amContainer);

    // Set the priority for the application master
    Priority pri = Records.newRecord(Priority.class);
    pri.setPriority(amPriority);
    appContext.setPriority(pri);

    // Set the queue to which this application is to be submitted in the RM
    appContext.setQueue(amQueue);

    // Submit the application to the applications manager
    /* log */ System.out.println("Submitting application to ASM");
    yarnClient.submitApplication(appContext);

    // Monitor the application
    boolean success = monitorApplication(appId);

    if (success && summary != null) {
        summary.stageIn();
    }

    return success;

}

From source file:edu.uci.ics.asterix.aoya.AsterixYARNClient.java

License:Apache License

private static void startAction(AsterixYARNClient client) throws YarnException {
    YarnClientApplication app;/*from  w  w w.  j  a v a  2  s  . c  om*/
    List<DFSResourceCoordinate> res;
    ApplicationId appId;
    try {
        app = client.makeApplicationContext();
        res = client.deployConfig();
        res.addAll(client.distributeBinaries());
        appId = client.deployAM(app, res, client.mode);
        LOG.info("Asterix started up with Application ID: " + appId.toString());
        if (Utils.waitForLiveness(appId, "Waiting for AsterixDB instance to resume ", client.yarnClient,
                client.instanceName, client.conf, client.ccRestPort)) {
            System.out.println("Asterix successfully deployed and is now running.");
        } else {
            LOG.fatal("AsterixDB appears to have failed to install and start");
            throw new YarnException("AsterixDB appears to have failed to install and start");
        }
    } catch (IOException e) {
        throw new YarnException(e);
    }
}

From source file:edu.uci.ics.asterix.aoya.AsterixYARNClient.java

License:Apache License

private static void installAction(AsterixYARNClient client) throws YarnException {
    YarnClientApplication app;//from  ww  w . ja v  a  2  s.  c om
    List<DFSResourceCoordinate> res;
    ApplicationId appId;
    try {
        app = client.makeApplicationContext();
        client.installConfig();
        client.writeAsterixConfig(Utils.parseYarnClusterConfig(client.asterixConf));
        client.installAsterixConfig(false);
        res = client.deployConfig();
        res.addAll(client.distributeBinaries());

        appId = client.deployAM(app, res, client.mode);
        LOG.info("Asterix started up with Application ID: " + appId.toString());
        if (Utils.waitForLiveness(appId, "Waiting for new AsterixDB Instance to start ", client.yarnClient,
                client.instanceName, client.conf, client.ccRestPort)) {
            System.out.println("Asterix successfully deployed and is now running.");
        } else {
            LOG.fatal("AsterixDB appears to have failed to install and start");
            throw new YarnException("AsterixDB appears to have failed to install and start");
        }
    } catch (IOException e) {
        LOG.fatal("Asterix failed to deploy on to cluster");
        throw new YarnException(e);
    }
}

From source file:edu.uci.ics.asterix.aoya.AsterixYARNClient.java

License:Apache License

/**
 * Start a YARN job to copy all data-containing resources of an AsterixDB instance to HDFS
 * @param app//from   www  . jav a2s .  co m
 * @param resources
 * @throws IOException
 * @throws YarnException
 */

private void backupInstance(YarnClientApplication app, List<DFSResourceCoordinate> resources)
        throws IOException, YarnException {
    stopInstanceIfRunning();
    ApplicationId backerUpper = deployAM(app, resources, Mode.BACKUP);
    boolean backupStart;
    backupStart = Utils.waitForApplication(backerUpper, yarnClient,
            "Waiting for backup " + backerUpper.toString() + "to start", ccRestPort);
    if (!backupStart) {
        LOG.fatal("Backup failed to start");
        throw new YarnException();
    }
    boolean complete;
    complete = waitForCompletion(backerUpper, "Backup in progress");
    if (!complete) {
        LOG.fatal("Backup failed- timeout waiting for completion");
        return;
    }
    System.out.println("Backup of instance succeeded.");
}

From source file:edu.uci.ics.asterix.aoya.Utils.java

License:Apache License

/**
 * Lists the deployed instances of AsterixDB on a YARN cluster
 * //from  w w w .j a  v a 2s . c o  m
 * @param conf
 *            Hadoop configuration object
 * @param confDirRel
 *            Relative AsterixDB configuration path for DFS
 * @throws IOException
 */

public static void listInstances(Configuration conf, String confDirRel) throws IOException {
    FileSystem fs = FileSystem.get(conf);
    Path instanceFolder = new Path(fs.getHomeDirectory(), confDirRel);
    if (!fs.exists(instanceFolder)) {
        System.out.println("No running or stopped AsterixDB instances exist in this cluster.");
        return;
    }
    FileStatus[] instances = fs.listStatus(instanceFolder);
    if (instances.length != 0) {
        System.out.println("Existing AsterixDB instances: ");
        for (int i = 0; i < instances.length; i++) {
            FileStatus st = instances[i];
            String name = st.getPath().getName();
            ApplicationId lockFile = AsterixYARNClient.getLockFile(name, conf);
            if (lockFile != null) {
                System.out.println(
                        "Instance " + name + " is running with Application ID: " + lockFile.toString());
            } else {
                System.out.println("Instance " + name + " is stopped");
            }
        }
    } else {
        System.out.println("No running or stopped AsterixDB instances exist in this cluster");
    }
}

From source file:eu.stratosphere.yarn.Client.java

License:Apache License

public void run(String[] args) throws Exception {

    if (UserGroupInformation.isSecurityEnabled()) {
        throw new RuntimeException("Stratosphere YARN client does not have security support right now."
                + "File a bug, we will fix it asap");
    }/*from  ww w . j  a  va  2  s.  c o m*/
    //Utils.logFilesInCurrentDirectory(LOG);
    //
    //   Command Line Options
    //
    Options options = new Options();
    options.addOption(VERBOSE);
    options.addOption(STRATOSPHERE_CONF_DIR);
    options.addOption(STRATOSPHERE_JAR);
    options.addOption(JM_MEMORY);
    options.addOption(TM_MEMORY);
    options.addOption(TM_CORES);
    options.addOption(CONTAINER);
    options.addOption(GEN_CONF);
    options.addOption(QUEUE);
    options.addOption(QUERY);
    options.addOption(SHIP_PATH);

    CommandLineParser parser = new PosixParser();
    CommandLine cmd = null;
    try {
        cmd = parser.parse(options, args);
    } catch (MissingOptionException moe) {
        System.out.println(moe.getMessage());
        printUsage();
        System.exit(1);
    }

    if (System.getProperty("log4j.configuration") == null) {
        Logger root = Logger.getRootLogger();
        root.removeAllAppenders();
        PatternLayout layout = new PatternLayout("%d{HH:mm:ss,SSS} %-5p %-60c %x - %m%n");
        ConsoleAppender appender = new ConsoleAppender(layout, "System.err");
        root.addAppender(appender);
        if (cmd.hasOption(VERBOSE.getOpt())) {
            root.setLevel(Level.DEBUG);
            LOG.debug("CLASSPATH: " + System.getProperty("java.class.path"));
        } else {
            root.setLevel(Level.INFO);
        }
    }

    // Jar Path
    Path localJarPath;
    if (cmd.hasOption(STRATOSPHERE_JAR.getOpt())) {
        String userPath = cmd.getOptionValue(STRATOSPHERE_JAR.getOpt());
        if (!userPath.startsWith("file://")) {
            userPath = "file://" + userPath;
        }
        localJarPath = new Path(userPath);
    } else {
        localJarPath = new Path(
                "file://" + Client.class.getProtectionDomain().getCodeSource().getLocation().getPath());
    }

    if (cmd.hasOption(GEN_CONF.getOpt())) {
        LOG.info("Placing default configuration in current directory");
        File outFile = generateDefaultConf(localJarPath);
        LOG.info("File written to " + outFile.getAbsolutePath());
        System.exit(0);
    }

    // Conf Path 
    Path confPath = null;
    String confDirPath = "";
    if (cmd.hasOption(STRATOSPHERE_CONF_DIR.getOpt())) {
        confDirPath = cmd.getOptionValue(STRATOSPHERE_CONF_DIR.getOpt()) + "/";
        File confFile = new File(confDirPath + CONFIG_FILE_NAME);
        if (!confFile.exists()) {
            LOG.fatal("Unable to locate configuration file in " + confFile);
            System.exit(1);
        }
        confPath = new Path(confFile.getAbsolutePath());
    } else {
        System.out.println("No configuration file has been specified");

        // no configuration path given.
        // -> see if there is one in the current directory
        File currDir = new File(".");
        File[] candidates = currDir.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(final File dir, final String name) {
                return name != null && name.endsWith(".yaml");
            }
        });
        if (candidates == null || candidates.length == 0) {
            System.out.println(
                    "No configuration file has been found in current directory.\n" + "Copying default.");
            File outFile = generateDefaultConf(localJarPath);
            confPath = new Path(outFile.toURI());
        } else {
            if (candidates.length > 1) {
                System.out.println("Multiple .yaml configuration files were found in the current directory\n"
                        + "Please specify one explicitly");
                System.exit(1);
            } else if (candidates.length == 1) {
                confPath = new Path(candidates[0].toURI());
            }
        }
    }
    List<File> shipFiles = new ArrayList<File>();
    // path to directory to ship
    if (cmd.hasOption(SHIP_PATH.getOpt())) {
        String shipPath = cmd.getOptionValue(SHIP_PATH.getOpt());
        File shipDir = new File(shipPath);
        if (shipDir.isDirectory()) {
            shipFiles = new ArrayList<File>(Arrays.asList(shipDir.listFiles(new FilenameFilter() {
                @Override
                public boolean accept(File dir, String name) {
                    return !(name.equals(".") || name.equals(".."));
                }
            })));
        } else {
            LOG.warn("Ship directory is not a directory!");
        }
    }
    boolean hasLog4j = false;
    //check if there is a log4j file
    if (confDirPath.length() > 0) {
        File l4j = new File(confDirPath + "/log4j.properties");
        if (l4j.exists()) {
            shipFiles.add(l4j);
            hasLog4j = true;
        }
    }

    // queue
    String queue = "default";
    if (cmd.hasOption(QUEUE.getOpt())) {
        queue = cmd.getOptionValue(QUEUE.getOpt());
    }

    // JobManager Memory
    int jmMemory = 512;
    if (cmd.hasOption(JM_MEMORY.getOpt())) {
        jmMemory = Integer.valueOf(cmd.getOptionValue(JM_MEMORY.getOpt()));
    }

    // Task Managers memory
    int tmMemory = 1024;
    if (cmd.hasOption(TM_MEMORY.getOpt())) {
        tmMemory = Integer.valueOf(cmd.getOptionValue(TM_MEMORY.getOpt()));
    }

    // Task Managers vcores
    int tmCores = 1;
    if (cmd.hasOption(TM_CORES.getOpt())) {
        tmCores = Integer.valueOf(cmd.getOptionValue(TM_CORES.getOpt()));
    }
    Utils.getStratosphereConfiguration(confPath.toUri().getPath());
    int jmPort = GlobalConfiguration.getInteger(ConfigConstants.JOB_MANAGER_IPC_PORT_KEY, 0);
    if (jmPort == 0) {
        LOG.warn("Unable to find job manager port in configuration!");
        jmPort = ConfigConstants.DEFAULT_JOB_MANAGER_IPC_PORT;
    }
    conf = Utils.initializeYarnConfiguration();

    // intialize HDFS
    LOG.info("Copy App Master jar from local filesystem and add to local environment");
    // Copy the application master jar to the filesystem 
    // Create a local resource to point to the destination jar path 
    final FileSystem fs = FileSystem.get(conf);

    if (fs.getScheme().startsWith("file")) {
        LOG.warn("The file system scheme is '" + fs.getScheme() + "'. This indicates that the "
                + "specified Hadoop configuration path is wrong and the sytem is using the default Hadoop configuration values."
                + "The Stratosphere YARN client needs to store its files in a distributed file system");
    }

    // Create yarnClient
    final YarnClient yarnClient = YarnClient.createYarnClient();
    yarnClient.init(conf);
    yarnClient.start();

    // Query cluster for metrics
    if (cmd.hasOption(QUERY.getOpt())) {
        showClusterMetrics(yarnClient);
    }
    if (!cmd.hasOption(CONTAINER.getOpt())) {
        LOG.fatal("Missing required argument " + CONTAINER.getOpt());
        printUsage();
        yarnClient.stop();
        System.exit(1);
    }

    // TM Count
    final int taskManagerCount = Integer.valueOf(cmd.getOptionValue(CONTAINER.getOpt()));

    System.out.println("Using values:");
    System.out.println("\tContainer Count = " + taskManagerCount);
    System.out.println("\tJar Path = " + localJarPath.toUri().getPath());
    System.out.println("\tConfiguration file = " + confPath.toUri().getPath());
    System.out.println("\tJobManager memory = " + jmMemory);
    System.out.println("\tTaskManager memory = " + tmMemory);
    System.out.println("\tTaskManager cores = " + tmCores);

    // Create application via yarnClient
    YarnClientApplication app = yarnClient.createApplication();
    GetNewApplicationResponse appResponse = app.getNewApplicationResponse();
    Resource maxRes = appResponse.getMaximumResourceCapability();
    if (tmMemory > maxRes.getMemory() || tmCores > maxRes.getVirtualCores()) {
        LOG.fatal("The cluster does not have the requested resources for the TaskManagers available!\n"
                + "Maximum Memory: " + maxRes.getMemory() + ", Maximum Cores: " + tmCores);
        yarnClient.stop();
        System.exit(1);
    }
    if (jmMemory > maxRes.getMemory()) {
        LOG.fatal("The cluster does not have the requested resources for the JobManager available!\n"
                + "Maximum Memory: " + maxRes.getMemory());
        yarnClient.stop();
        System.exit(1);
    }
    int totalMemoryRequired = jmMemory + tmMemory * taskManagerCount;
    ClusterResourceDescription freeClusterMem = getCurrentFreeClusterResources(yarnClient);
    if (freeClusterMem.totalFreeMemory < totalMemoryRequired) {
        LOG.fatal("This YARN session requires " + totalMemoryRequired + "MB of memory in the cluster. "
                + "There are currently only " + freeClusterMem.totalFreeMemory + "MB available.");
        yarnClient.stop();
        System.exit(1);
    }
    if (tmMemory > freeClusterMem.containerLimit) {
        LOG.fatal("The requested amount of memory for the TaskManagers (" + tmMemory + "MB) is more than "
                + "the largest possible YARN container: " + freeClusterMem.containerLimit);
        yarnClient.stop();
        System.exit(1);
    }
    if (jmMemory > freeClusterMem.containerLimit) {
        LOG.fatal("The requested amount of memory for the JobManager (" + jmMemory + "MB) is more than "
                + "the largest possible YARN container: " + freeClusterMem.containerLimit);
        yarnClient.stop();
        System.exit(1);
    }

    // respect custom JVM options in the YAML file
    final String javaOpts = GlobalConfiguration.getString(ConfigConstants.STRATOSPHERE_JVM_OPTIONS, "");

    // Set up the container launch context for the application master
    ContainerLaunchContext amContainer = Records.newRecord(ContainerLaunchContext.class);

    String amCommand = "$JAVA_HOME/bin/java" + " -Xmx" + Utils.calculateHeapSize(jmMemory) + "M " + javaOpts;
    if (hasLog4j) {
        amCommand += " -Dlog.file=\"" + ApplicationConstants.LOG_DIR_EXPANSION_VAR
                + "/jobmanager-log4j.log\" -Dlog4j.configuration=file:log4j.properties";
    }
    amCommand += " eu.stratosphere.yarn.ApplicationMaster" + " " + " 1>"
            + ApplicationConstants.LOG_DIR_EXPANSION_VAR + "/jobmanager-stdout.log" + " 2>"
            + ApplicationConstants.LOG_DIR_EXPANSION_VAR + "/jobmanager-stderr.log";
    amContainer.setCommands(Collections.singletonList(amCommand));

    System.err.println("amCommand=" + amCommand);

    // Set-up ApplicationSubmissionContext for the application
    ApplicationSubmissionContext appContext = app.getApplicationSubmissionContext();
    final ApplicationId appId = appContext.getApplicationId();

    // Setup jar for ApplicationMaster
    LocalResource appMasterJar = Records.newRecord(LocalResource.class);
    LocalResource stratosphereConf = Records.newRecord(LocalResource.class);
    Path remotePathJar = Utils.setupLocalResource(conf, fs, appId.toString(), localJarPath, appMasterJar,
            fs.getHomeDirectory());
    Path remotePathConf = Utils.setupLocalResource(conf, fs, appId.toString(), confPath, stratosphereConf,
            fs.getHomeDirectory());
    Map<String, LocalResource> localResources = new HashMap<String, LocalResource>(2);
    localResources.put("stratosphere.jar", appMasterJar);
    localResources.put("stratosphere-conf.yaml", stratosphereConf);

    // setup security tokens (code from apache storm)
    final Path[] paths = new Path[3 + shipFiles.size()];
    StringBuffer envShipFileList = new StringBuffer();
    // upload ship files
    for (int i = 0; i < shipFiles.size(); i++) {
        File shipFile = shipFiles.get(i);
        LocalResource shipResources = Records.newRecord(LocalResource.class);
        Path shipLocalPath = new Path("file://" + shipFile.getAbsolutePath());
        paths[3 + i] = Utils.setupLocalResource(conf, fs, appId.toString(), shipLocalPath, shipResources,
                fs.getHomeDirectory());
        localResources.put(shipFile.getName(), shipResources);

        envShipFileList.append(paths[3 + i]);
        if (i + 1 < shipFiles.size()) {
            envShipFileList.append(',');
        }
    }

    paths[0] = remotePathJar;
    paths[1] = remotePathConf;
    paths[2] = new Path(fs.getHomeDirectory(), ".stratosphere/" + appId.toString() + "/");
    FsPermission permission = new FsPermission(FsAction.ALL, FsAction.ALL, FsAction.ALL);
    fs.setPermission(paths[2], permission); // set permission for path.
    Utils.setTokensFor(amContainer, paths, this.conf);

    amContainer.setLocalResources(localResources);
    fs.close();

    // Setup CLASSPATH for ApplicationMaster
    Map<String, String> appMasterEnv = new HashMap<String, String>();
    Utils.setupEnv(conf, appMasterEnv);
    // set configuration values
    appMasterEnv.put(Client.ENV_TM_COUNT, String.valueOf(taskManagerCount));
    appMasterEnv.put(Client.ENV_TM_CORES, String.valueOf(tmCores));
    appMasterEnv.put(Client.ENV_TM_MEMORY, String.valueOf(tmMemory));
    appMasterEnv.put(Client.STRATOSPHERE_JAR_PATH, remotePathJar.toString());
    appMasterEnv.put(Client.ENV_APP_ID, appId.toString());
    appMasterEnv.put(Client.ENV_CLIENT_HOME_DIR, fs.getHomeDirectory().toString());
    appMasterEnv.put(Client.ENV_CLIENT_SHIP_FILES, envShipFileList.toString());
    appMasterEnv.put(Client.ENV_CLIENT_USERNAME, UserGroupInformation.getCurrentUser().getShortUserName());

    amContainer.setEnvironment(appMasterEnv);

    // Set up resource type requirements for ApplicationMaster
    Resource capability = Records.newRecord(Resource.class);
    capability.setMemory(jmMemory);
    capability.setVirtualCores(1);

    appContext.setApplicationName("Stratosphere"); // application name
    appContext.setAMContainerSpec(amContainer);
    appContext.setResource(capability);
    appContext.setQueue(queue);

    // file that we write into the conf/ dir containing the jobManager address.
    final File addrFile = new File(confDirPath + CliFrontend.JOBMANAGER_ADDRESS_FILE);

    Runtime.getRuntime().addShutdownHook(new Thread() {
        @Override
        public void run() {
            try {
                LOG.info("Killing the Stratosphere-YARN application.");
                yarnClient.killApplication(appId);
                LOG.info("Deleting files in " + paths[2]);
                FileSystem shutFS = FileSystem.get(conf);
                shutFS.delete(paths[2], true); // delete conf and jar file.
                shutFS.close();
            } catch (Exception e) {
                LOG.warn("Exception while killing the YARN application", e);
            }
            try {
                addrFile.delete();
            } catch (Exception e) {
                LOG.warn("Exception while deleting the jobmanager address file", e);
            }
            LOG.info("YARN Client is shutting down");
            yarnClient.stop();
        }
    });

    LOG.info("Submitting application master " + appId);
    yarnClient.submitApplication(appContext);
    ApplicationReport appReport = yarnClient.getApplicationReport(appId);
    YarnApplicationState appState = appReport.getYarnApplicationState();
    boolean told = false;
    char[] el = { '/', '|', '\\', '-' };
    int i = 0;
    while (appState != YarnApplicationState.FINISHED && appState != YarnApplicationState.KILLED
            && appState != YarnApplicationState.FAILED) {
        if (!told && appState == YarnApplicationState.RUNNING) {
            System.err
                    .println("Stratosphere JobManager is now running on " + appReport.getHost() + ":" + jmPort);
            System.err.println("JobManager Web Interface: " + appReport.getTrackingUrl());
            // write jobmanager connect information

            PrintWriter out = new PrintWriter(addrFile);
            out.println(appReport.getHost() + ":" + jmPort);
            out.close();
            addrFile.setReadable(true, false); // readable for all.
            told = true;
        }
        if (!told) {
            System.err.print(el[i++] + "\r");
            if (i == el.length) {
                i = 0;
            }
            Thread.sleep(500); // wait for the application to switch to RUNNING
        } else {
            Thread.sleep(5000);
        }

        appReport = yarnClient.getApplicationReport(appId);
        appState = appReport.getYarnApplicationState();
    }

    LOG.info("Application " + appId + " finished with" + " state " + appState + " at "
            + appReport.getFinishTime());
    if (appState == YarnApplicationState.FAILED || appState == YarnApplicationState.KILLED) {
        LOG.warn("Application failed. Diagnostics " + appReport.getDiagnostics());
    }

}

From source file:gobblin.yarn.GobblinYarnAppLauncher.java

License:Apache License

private Map<String, LocalResource> addAppMasterLocalResources(ApplicationId applicationId) throws IOException {
    Path appWorkDir = GobblinClusterUtils.getAppWorkDirPath(this.fs, this.applicationName,
            applicationId.toString());
    Path appMasterWorkDir = new Path(appWorkDir, GobblinYarnConfigurationKeys.APP_MASTER_WORK_DIR_NAME);

    Map<String, LocalResource> appMasterResources = Maps.newHashMap();

    if (this.config.hasPath(GobblinYarnConfigurationKeys.LIB_JARS_DIR_KEY)) {
        Path libJarsDestDir = new Path(appWorkDir, GobblinYarnConfigurationKeys.LIB_JARS_DIR_NAME);
        addLibJars(new Path(this.config.getString(GobblinYarnConfigurationKeys.LIB_JARS_DIR_KEY)),
                Optional.of(appMasterResources), libJarsDestDir);
    }//from w ww.ja  v  a 2  s.  c om
    if (this.config.hasPath(GobblinYarnConfigurationKeys.APP_MASTER_JARS_KEY)) {
        Path appJarsDestDir = new Path(appMasterWorkDir, GobblinYarnConfigurationKeys.APP_JARS_DIR_NAME);
        addAppJars(this.config.getString(GobblinYarnConfigurationKeys.APP_MASTER_JARS_KEY),
                Optional.of(appMasterResources), appJarsDestDir);
    }
    if (this.config.hasPath(GobblinYarnConfigurationKeys.APP_MASTER_FILES_LOCAL_KEY)) {
        Path appFilesDestDir = new Path(appMasterWorkDir, GobblinYarnConfigurationKeys.APP_FILES_DIR_NAME);
        addAppLocalFiles(this.config.getString(GobblinYarnConfigurationKeys.APP_MASTER_FILES_LOCAL_KEY),
                Optional.of(appMasterResources), appFilesDestDir);
    }
    if (this.config.hasPath(GobblinYarnConfigurationKeys.APP_MASTER_FILES_REMOTE_KEY)) {
        addAppRemoteFiles(this.config.getString(GobblinYarnConfigurationKeys.APP_MASTER_FILES_REMOTE_KEY),
                appMasterResources);
    }
    if (this.config.hasPath(GobblinClusterConfigurationKeys.JOB_CONF_PATH_KEY)) {
        Path appFilesDestDir = new Path(appMasterWorkDir, GobblinYarnConfigurationKeys.APP_FILES_DIR_NAME);
        addJobConfPackage(this.config.getString(GobblinClusterConfigurationKeys.JOB_CONF_PATH_KEY),
                appFilesDestDir, appMasterResources);
    }

    return appMasterResources;
}

From source file:gobblin.yarn.GobblinYarnAppLauncher.java

License:Apache License

private void addContainerLocalResources(ApplicationId applicationId) throws IOException {
    Path appWorkDir = GobblinClusterUtils.getAppWorkDirPath(this.fs, this.applicationName,
            applicationId.toString());
    Path containerWorkDir = new Path(appWorkDir, GobblinYarnConfigurationKeys.CONTAINER_WORK_DIR_NAME);

    if (this.config.hasPath(GobblinYarnConfigurationKeys.CONTAINER_JARS_KEY)) {
        Path appJarsDestDir = new Path(containerWorkDir, GobblinYarnConfigurationKeys.APP_JARS_DIR_NAME);
        addAppJars(this.config.getString(GobblinYarnConfigurationKeys.CONTAINER_JARS_KEY),
                Optional.<Map<String, LocalResource>>absent(), appJarsDestDir);
    }//from  w  w  w.j  a v  a 2s .c  o m
    if (this.config.hasPath(GobblinYarnConfigurationKeys.CONTAINER_FILES_LOCAL_KEY)) {
        Path appFilesDestDir = new Path(containerWorkDir, GobblinYarnConfigurationKeys.APP_FILES_DIR_NAME);
        addAppLocalFiles(this.config.getString(GobblinYarnConfigurationKeys.CONTAINER_FILES_LOCAL_KEY),
                Optional.<Map<String, LocalResource>>absent(), appFilesDestDir);
    }
}

From source file:gobblin.yarn.GobblinYarnAppLauncher.java

License:Apache License

@VisibleForTesting
void cleanUpAppWorkDirectory(ApplicationId applicationId) throws IOException {
    Path appWorkDir = GobblinClusterUtils.getAppWorkDirPath(this.fs, this.applicationName,
            applicationId.toString());
    if (this.fs.exists(appWorkDir)) {
        LOGGER.info("Deleting application working directory " + appWorkDir);
        this.fs.delete(appWorkDir, true);
    }/*www  .  j a v  a2s.c o  m*/
}