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.runtime.common.driver; 020 021import org.apache.reef.driver.catalog.ResourceCatalog; 022import org.apache.reef.driver.evaluator.EvaluatorRequest; 023import org.apache.reef.driver.evaluator.EvaluatorRequestor; 024import org.apache.reef.runtime.common.driver.api.ResourceRequestEvent; 025import org.apache.reef.runtime.common.driver.api.ResourceRequestEventImpl; 026import org.apache.reef.runtime.common.driver.api.ResourceRequestHandler; 027import org.apache.reef.runtime.common.utils.Constants; 028import org.apache.reef.util.logging.LoggingScope; 029import org.apache.reef.util.logging.LoggingScopeFactory; 030 031import javax.inject.Inject; 032import java.util.logging.Level; 033import java.util.logging.Logger; 034 035 036/** 037 * Implementation of the EvaluatorRequestor that translates the request and hands it down to the underlying RM. 038 */ 039public final class EvaluatorRequestorImpl implements EvaluatorRequestor { 040 041 private static final Logger LOG = Logger.getLogger(EvaluatorRequestorImpl.class.getName()); 042 043 private final ResourceCatalog resourceCatalog; 044 private final ResourceRequestHandler resourceRequestHandler; 045 private final LoggingScopeFactory loggingScopeFactory; 046 047 /** 048 * @param resourceCatalog 049 * @param resourceRequestHandler 050 */ 051 @Inject 052 public EvaluatorRequestorImpl(final ResourceCatalog resourceCatalog, 053 final ResourceRequestHandler resourceRequestHandler, 054 final LoggingScopeFactory loggingScopeFactory) { 055 this.resourceCatalog = resourceCatalog; 056 this.resourceRequestHandler = resourceRequestHandler; 057 this.loggingScopeFactory = loggingScopeFactory; 058 } 059 060 @Override 061 public synchronized void submit(final EvaluatorRequest req) { 062 LOG.log(Level.FINEST, "Got an EvaluatorRequest: number: {0}, memory = {1}, cores = {2}.", 063 new Object[] {req.getNumber(), req.getMegaBytes(), req.getNumberOfCores()}); 064 065 if (req.getMegaBytes() <= 0) { 066 throw new IllegalArgumentException("Given an unsupported memory size: " + req.getMegaBytes()); 067 } 068 if (req.getNumberOfCores() <= 0) { 069 throw new IllegalArgumentException("Given an unsupported core number: " + req.getNumberOfCores()); 070 } 071 if (req.getNumber() <= 0) { 072 throw new IllegalArgumentException("Given an unsupported number of evaluators: " + req.getNumber()); 073 } 074 if (req.getNodeNames() == null) { 075 throw new IllegalArgumentException("Node names cannot be null"); 076 } 077 if (req.getRackNames() == null) { 078 throw new IllegalArgumentException("Rack names cannot be null"); 079 } 080 if(req.getRuntimeName() == null) { 081 throw new IllegalArgumentException("Runtime name cannot be null"); 082 } 083 // for backwards compatibility, we will always set the relax locality flag 084 // to true unless the user configured racks, in which case we will check for 085 // the ANY modifier (*), if not there, then we won't relax the locality 086 boolean relaxLocality = true; 087 if (!req.getRackNames().isEmpty()) { 088 for (final String rackName : req.getRackNames()) { 089 if (Constants.ANY_RACK.equals(rackName)) { 090 relaxLocality = true; 091 break; 092 } 093 relaxLocality = false; 094 } 095 } 096 // if the user specified any node, then we assume they do not want to relax locality 097 if (!req.getNodeNames().isEmpty()) { 098 relaxLocality = false; 099 } 100 101 try (LoggingScope ls = loggingScopeFactory.evaluatorSubmit(req.getNumber())) { 102 final ResourceRequestEvent request = ResourceRequestEventImpl 103 .newBuilder() 104 .setResourceCount(req.getNumber()) 105 .setVirtualCores(req.getNumberOfCores()) 106 .setMemorySize(req.getMegaBytes()) 107 .addNodeNames(req.getNodeNames()) 108 .addRackNames(req.getRackNames()) 109 .setRelaxLocality(relaxLocality) 110 .setRuntimeName(req.getRuntimeName()) 111 .build(); 112 this.resourceRequestHandler.onNext(request); 113 } 114 } 115}