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.scheduler.client; 020 021import org.apache.commons.cli.ParseException; 022import org.apache.reef.client.DriverConfiguration; 023import org.apache.reef.client.REEF; 024import org.apache.reef.examples.scheduler.driver.SchedulerDriver; 025import org.apache.reef.examples.scheduler.driver.http.SchedulerHttpHandler; 026import org.apache.reef.runtime.local.client.LocalRuntimeConfiguration; 027import org.apache.reef.tang.Configuration; 028import org.apache.reef.tang.Configurations; 029import org.apache.reef.tang.Tang; 030import org.apache.reef.tang.annotations.Name; 031import org.apache.reef.tang.annotations.NamedParameter; 032import org.apache.reef.tang.exceptions.InjectionException; 033import org.apache.reef.tang.formats.CommandLine; 034import org.apache.reef.util.EnvironmentUtils; 035import org.apache.reef.webserver.HttpHandlerConfiguration; 036 037import java.io.IOException; 038 039/** 040 * REEF TaskScheduler. 041 */ 042public final class SchedulerREEF { 043 /** 044 * The upper limit on the number of Evaluators that the local resourcemanager will hand out concurrently. 045 */ 046 private static final int MAX_NUMBER_OF_EVALUATORS = 3; 047 048 /** 049 * Command line parameter = true to reuse evaluators,. 050 * or false to allocate/close for each iteration 051 */ 052 @NamedParameter(doc = "Whether or not to reuse evaluators", 053 short_name = "retain", default_value = "true") 054 public static final class Retain implements Name<Boolean> { 055 } 056 057 /** 058 * @return The http configuration to use reef-webserver 059 */ 060 private static Configuration getHttpConf() { 061 final Configuration httpHandlerConf = HttpHandlerConfiguration.CONF 062 .set(HttpHandlerConfiguration.HTTP_HANDLERS, SchedulerHttpHandler.class) 063 .build(); 064 return httpHandlerConf; 065 } 066 067 /** 068 * @return The Driver configuration. 069 */ 070 private static Configuration getDriverConf() { 071 final Configuration driverConf = DriverConfiguration.CONF 072 .set(DriverConfiguration.GLOBAL_LIBRARIES, EnvironmentUtils.getClassLocation(SchedulerDriver.class)) 073 .set(DriverConfiguration.DRIVER_IDENTIFIER, "TaskScheduler") 074 .set(DriverConfiguration.ON_DRIVER_STARTED, SchedulerDriver.StartHandler.class) 075 .set(DriverConfiguration.ON_EVALUATOR_ALLOCATED, SchedulerDriver.EvaluatorAllocatedHandler.class) 076 .set(DriverConfiguration.ON_CONTEXT_ACTIVE, SchedulerDriver.ActiveContextHandler.class) 077 .set(DriverConfiguration.ON_TASK_COMPLETED, SchedulerDriver.CompletedTaskHandler.class) 078 .build(); 079 080 return driverConf; 081 } 082 083 /** 084 * Run the Task scheduler. If '-retain true' option is passed via command line, 085 * the scheduler reuses evaluators to submit new Tasks. 086 * @param runtimeConf The runtime configuration (e.g. Local, YARN, etc) 087 * @param args Command line arguments. 088 * @throws InjectionException 089 * @throws java.io.IOException 090 */ 091 public static void runTaskScheduler(final Configuration runtimeConf, final String[] args) 092 throws InjectionException, IOException, ParseException { 093 final Tang tang = Tang.Factory.getTang(); 094 095 final Configuration commandLineConf = CommandLine.parseToConfiguration(args, Retain.class); 096 097 // Merge the configurations to run Driver 098 final Configuration driverConf = Configurations.merge(getDriverConf(), getHttpConf(), commandLineConf); 099 100 final REEF reef = tang.newInjector(runtimeConf).getInstance(REEF.class); 101 reef.submit(driverConf); 102 } 103 104 /** 105 * Main program. 106 * @param args 107 * @throws InjectionException 108 */ 109 public static void main(final String[] args) throws InjectionException, IOException, ParseException { 110 final Configuration runtimeConfiguration = LocalRuntimeConfiguration.CONF 111 .set(LocalRuntimeConfiguration.MAX_NUMBER_OF_EVALUATORS, MAX_NUMBER_OF_EVALUATORS) 112 .build(); 113 runTaskScheduler(runtimeConfiguration, args); 114 } 115 116 /** 117 * Empty private constructor to prohibit instantiation of utility class. 118 */ 119 private SchedulerREEF() { 120 } 121}