This project has retired. For details please refer to its Attic page.
Source code
001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *   http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied.  See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.reef.examples.distributedshell;
020
021import org.apache.reef.client.DriverConfiguration;
022import org.apache.reef.client.DriverLauncher;
023import org.apache.reef.client.LauncherStatus;
024import org.apache.reef.examples.library.Command;
025import org.apache.reef.runtime.local.client.LocalRuntimeConfiguration;
026import org.apache.reef.runtime.yarn.client.YarnClientConfiguration;
027import org.apache.reef.tang.Configuration;
028import org.apache.reef.tang.Injector;
029import org.apache.reef.tang.JavaConfigurationBuilder;
030import org.apache.reef.tang.Tang;
031import org.apache.reef.tang.exceptions.InjectionException;
032import org.apache.reef.tang.formats.CommandLine;
033import org.apache.reef.util.EnvironmentUtils;
034
035import java.io.IOException;
036import java.util.logging.Level;
037import java.util.logging.Logger;
038
039/** The Client for REEF distributed shell example. */
040public final class ShellClient {
041
042  private static final Logger LOG = Logger.getLogger(ShellClient.class.getName());
043
044  /** Number of milliseconds to wait for the job to complete. */
045  private static final int JOB_TIMEOUT = 60000; // 1 min.
046
047  private static final Tang TANG = Tang.Factory.getTang();
048
049  private static final Configuration STATIC_DRIVER_CONFIG = DriverConfiguration.CONF
050      .set(DriverConfiguration.DRIVER_IDENTIFIER, "DistributedShell")
051      .set(DriverConfiguration.GLOBAL_LIBRARIES, EnvironmentUtils.getClassLocation(ShellDriver.class))
052      .set(DriverConfiguration.ON_DRIVER_STARTED, ShellDriver.StartHandler.class)
053      .set(DriverConfiguration.ON_EVALUATOR_ALLOCATED, ShellDriver.EvaluatorAllocatedHandler.class)
054      .build();
055
056  /**
057   * Start the distributed shell job.
058   * @param args command line parameters.
059   * @throws InjectionException configuration error.
060   */
061  public static void main(final String[] args) throws InjectionException, IOException {
062
063    final JavaConfigurationBuilder driverConfigBuilder = TANG.newConfigurationBuilder(STATIC_DRIVER_CONFIG);
064
065    new CommandLine(driverConfigBuilder)
066        .registerShortNameOfClass(Command.class)
067        .registerShortNameOfClass(NumEvaluators.class)
068        .registerShortNameOfClass(RuntimeName.class)
069        .processCommandLine(args);
070
071    final Configuration driverConfig = driverConfigBuilder.build();
072
073    final Injector injector = TANG.newInjector(driverConfig);
074
075    final int numEvaluators = injector.getNamedInstance(NumEvaluators.class);
076    final String runtimeName = injector.getNamedInstance(RuntimeName.class);
077    final String command = injector.getNamedInstance(Command.class);
078
079    LOG.log(Level.INFO, "REEF distributed shell: {0} evaluators on {1} runtime :: {2}",
080        new Object[] {numEvaluators, runtimeName, command});
081
082    final Configuration runtimeConfig;
083
084    switch (runtimeName) {
085    case "local":
086      runtimeConfig = LocalRuntimeConfiguration.CONF
087          .set(LocalRuntimeConfiguration.MAX_NUMBER_OF_EVALUATORS, numEvaluators)
088          .build();
089      break;
090    case "yarn":
091      runtimeConfig = YarnClientConfiguration.CONF.build();
092      break;
093    default:
094      LOG.log(Level.SEVERE, "Unknown runtime: {0}", runtimeName);
095      throw new IllegalArgumentException("Unknown runtime: " + runtimeName);
096    }
097
098    final LauncherStatus status = DriverLauncher.getLauncher(runtimeConfig).run(driverConfig, JOB_TIMEOUT);
099
100    LOG.log(Level.INFO, "REEF job completed: {0}", status);
101  }
102
103  /** This class should never be instantiated. */
104  private ShellClient() { }
105}