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.hellomultiruntime; 020 021import org.apache.reef.driver.evaluator.AllocatedEvaluator; 022import org.apache.reef.driver.evaluator.EvaluatorRequestor; 023import org.apache.reef.driver.task.TaskConfiguration; 024import org.apache.reef.examples.hello.HelloTask; 025import org.apache.reef.runtime.local.driver.RuntimeIdentifier; 026import org.apache.reef.tang.Configuration; 027import org.apache.reef.tang.annotations.Unit; 028import org.apache.reef.wake.EventHandler; 029import org.apache.reef.wake.time.event.StartTime; 030 031import javax.inject.Inject; 032import java.util.logging.Level; 033import java.util.logging.Logger; 034 035/** 036 * The Driver code for the Hello REEF Application with Multi Runtime. 037 */ 038@Unit 039public final class HelloMultiRuntimeDriver { 040 041 private static final Logger LOG = Logger.getLogger(HelloMultiRuntimeDriver.class.getName()); 042 043 private final EvaluatorRequestor requestor; 044 045 /** 046 * Job driver constructor - instantiated via TANG. 047 * 048 * @param requestor evaluator requestor object used to create new evaluator containers. 049 */ 050 @Inject 051 private HelloMultiRuntimeDriver(final EvaluatorRequestor requestor) { 052 this.requestor = requestor; 053 LOG.log(Level.FINE, "Instantiated 'HelloDriver'"); 054 } 055 056 /** 057 * Handles the StartTime event: Request as single Evaluator. 058 */ 059 public final class StartHandler implements EventHandler<StartTime> { 060 @Override 061 public void onNext(final StartTime startTime) { 062 HelloMultiRuntimeDriver.this.requestor.newRequest() 063 .setNumber(1) 064 .setMemory(64) 065 .setNumberOfCores(1) 066 .setRuntimeName(RuntimeIdentifier.RUNTIME_NAME) 067 .submit(); 068 069 LOG.log(Level.INFO, "Requested Local Evaluator ."); 070 071 HelloMultiRuntimeDriver.this.requestor.newRequest() 072 .setNumber(1) 073 .setMemory(64) 074 .setNumberOfCores(1) 075 .setRuntimeName(RuntimeIdentifier.RUNTIME_NAME) 076 .submit(); 077 078 LOG.log(Level.INFO, "Requested Yarn Evaluator."); 079 } 080 } 081 082 /** 083 * Handles AllocatedEvaluator: Submit the HelloTask. 084 */ 085 public final class EvaluatorAllocatedHandler implements EventHandler<AllocatedEvaluator> { 086 @Override 087 public void onNext(final AllocatedEvaluator allocatedEvaluator) { 088 LOG.log(Level.INFO, "Submitting HelloREEF task to AllocatedEvaluator: {0}", allocatedEvaluator); 089 final Configuration taskConfiguration = TaskConfiguration.CONF 090 .set(TaskConfiguration.IDENTIFIER, "HelloREEFTask") 091 .set(TaskConfiguration.TASK, HelloTask.class) 092 .build(); 093 allocatedEvaluator.submitTask(taskConfiguration); 094 } 095 } 096}