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.hellohttp;
020
021import org.apache.reef.client.DriverConfiguration;
022import org.apache.reef.client.DriverLauncher;
023import org.apache.reef.client.DriverServiceConfiguration;
024import org.apache.reef.client.LauncherStatus;
025import org.apache.reef.runtime.local.client.LocalRuntimeConfiguration;
026import org.apache.reef.tang.Configuration;
027import org.apache.reef.tang.Configurations;
028import org.apache.reef.tang.exceptions.BindException;
029import org.apache.reef.tang.exceptions.InjectionException;
030import org.apache.reef.util.EnvironmentUtils;
031import org.apache.reef.webserver.HttpHandlerConfiguration;
032import org.apache.reef.webserver.HttpServerReefEventHandler;
033import org.apache.reef.webserver.ReefEventStateManager;
034
035/**
036 * Distributed shell example based on REEF HTTP Server component.
037 */
038public final class HelloREEFHttp {
039  /**
040   * The upper limit on the number of Evaluators that the local resourcemanager will hand out concurrently.
041   */
042  private static final int MAX_NUMBER_OF_EVALUATORS = 3;
043
044  /**
045   * Number of milliseconds to wait for the job to complete.
046   */
047  public static final int JOB_TIMEOUT = 60 * 1000; // 60 sec.
048
049  /**
050   * @return the driver-side configuration to be merged into the DriverConfiguration to enable the HTTP server.
051   */
052  public static Configuration getHTTPConfiguration() {
053    final Configuration httpHandlerConfiguration = HttpHandlerConfiguration.CONF
054        .set(HttpHandlerConfiguration.HTTP_HANDLERS, HttpServerReefEventHandler.class)
055        .set(HttpHandlerConfiguration.HTTP_HANDLERS, HttpServerShellCmdHandler.class)
056        .build();
057    final Configuration driverConfigurationForHttpServer = DriverServiceConfiguration.CONF
058        .set(DriverServiceConfiguration.ON_EVALUATOR_ALLOCATED,
059            ReefEventStateManager.AllocatedEvaluatorStateHandler.class)
060        .set(DriverServiceConfiguration.ON_CONTEXT_ACTIVE, ReefEventStateManager.ActiveContextStateHandler.class)
061        .set(DriverServiceConfiguration.ON_TASK_RUNNING, ReefEventStateManager.TaskRunningStateHandler.class)
062        .set(DriverServiceConfiguration.ON_DRIVER_STARTED, ReefEventStateManager.StartStateHandler.class)
063        .set(DriverServiceConfiguration.ON_DRIVER_STOP, ReefEventStateManager.StopStateHandler.class)
064        .build();
065    return Configurations.merge(httpHandlerConfiguration, driverConfigurationForHttpServer);
066  }
067
068  /**
069   * @return the configuration of the HelloREEF driver.
070   */
071  public static Configuration getDriverConfiguration() {
072    return DriverConfiguration.CONF
073        .set(DriverConfiguration.GLOBAL_LIBRARIES, EnvironmentUtils.getClassLocation(HttpShellJobDriver.class))
074        .set(DriverConfiguration.DRIVER_IDENTIFIER, "HelloREEF")
075        .set(DriverConfiguration.ON_DRIVER_STARTED, HttpShellJobDriver.StartHandler.class)
076        .set(DriverConfiguration.ON_EVALUATOR_ALLOCATED, HttpShellJobDriver.AllocatedEvaluatorHandler.class)
077        .set(DriverConfiguration.ON_EVALUATOR_FAILED, HttpShellJobDriver.FailedEvaluatorHandler.class)
078        .set(DriverConfiguration.ON_CONTEXT_ACTIVE, HttpShellJobDriver.ActiveContextHandler.class)
079        .set(DriverConfiguration.ON_CONTEXT_CLOSED, HttpShellJobDriver.ClosedContextHandler.class)
080        .set(DriverConfiguration.ON_CONTEXT_FAILED, HttpShellJobDriver.FailedContextHandler.class)
081        .set(DriverConfiguration.ON_TASK_COMPLETED, HttpShellJobDriver.CompletedTaskHandler.class)
082        .set(DriverConfiguration.ON_CLIENT_MESSAGE, HttpShellJobDriver.ClientMessageHandler.class)
083        .set(DriverConfiguration.ON_CLIENT_CLOSED, HttpShellJobDriver.HttpClientCloseHandler.class)
084        .set(DriverConfiguration.ON_DRIVER_STOP, HttpShellJobDriver.StopHandler.class)
085        .build();
086  }
087
088  /**
089   * Run Hello Reef with merged configuration.
090   *
091   * @param runtimeConf
092   * @param timeOut
093   * @return
094   * @throws BindException
095   * @throws InjectionException
096   */
097  public static LauncherStatus runHelloReef(final Configuration runtimeConf, final int timeOut)
098      throws BindException, InjectionException {
099    final Configuration driverConf =
100        Configurations.merge(HelloREEFHttp.getDriverConfiguration(), getHTTPConfiguration());
101    return DriverLauncher.getLauncher(runtimeConf).run(driverConf, timeOut);
102  }
103
104  /**
105   * Main program.
106   *
107   * @param args
108   * @throws InjectionException
109   */
110  public static void main(final String[] args) throws InjectionException {
111    final Configuration runtimeConfiguration = LocalRuntimeConfiguration.CONF
112        .set(LocalRuntimeConfiguration.MAX_NUMBER_OF_EVALUATORS, MAX_NUMBER_OF_EVALUATORS)
113        .build();
114    runHelloReef(runtimeConfiguration, HelloREEFHttp.JOB_TIMEOUT);
115  }
116
117  /**
118   * Empty private constructor to prohibit instantiation of utility class.
119   */
120  private HelloREEFHttp() {
121  }
122}