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.driver.evaluator; 020 021import org.apache.reef.annotations.Provided; 022import org.apache.reef.annotations.audience.DriverSide; 023import org.apache.reef.annotations.audience.Public; 024import java.util.ArrayList; 025import java.util.Collections; 026import java.util.List; 027 028/** 029 * A request for one ore more Evaluators. 030 */ 031@Public 032@DriverSide 033@Provided 034public final class EvaluatorRequest { 035 036 private final int megaBytes; 037 private final int number; 038 private final int cores; 039 private final List<String> nodeNames; 040 private final List<String> rackNames; 041 private final String runtimeName; 042 043 EvaluatorRequest(final int number, 044 final int megaBytes, 045 final int cores, 046 final List<String> nodeNames, 047 final List<String> rackNames) { 048 this(number, megaBytes, cores, nodeNames, rackNames, ""); 049 } 050 051 EvaluatorRequest(final int number, 052 final int megaBytes, 053 final int cores, 054 final List<String> nodeNames, 055 final List<String> rackNames, 056 final String runtimeName) { 057 this.number = number; 058 this.megaBytes = megaBytes; 059 this.cores = cores; 060 this.nodeNames = nodeNames; 061 this.rackNames = rackNames; 062 this.runtimeName = runtimeName; 063 } 064 065 /** 066 * Get a new builder. 067 * 068 * @return a new EvaluatorRequest Builder. 069 */ 070 public static Builder newBuilder() { 071 return new Builder(); 072 } 073 074 /** 075 * Get a new builder from the existing request. 076 * 077 * @return a new EvaluatorRequest Builder with settings initialized 078 * from an existing request. 079 */ 080 public static Builder newBuilder(final EvaluatorRequest request) { 081 return new Builder(request); 082 } 083 084 /** 085 * Access the number of Evaluators requested. 086 * 087 * @return the number of Evaluators requested. 088 */ 089 public int getNumber() { 090 return this.number; 091 } 092 093 /** 094 * Access the number of core of Evaluators requested. 095 * 096 * @return the number of cores requested. 097 */ 098 public int getNumberOfCores() { 099 return this.cores; 100 } 101 102 /** 103 * Access the number of memory requested. 104 * 105 * @return the minimum size of Evaluator requested. 106 */ 107 public int getMegaBytes() { 108 return megaBytes; 109 } 110 111 /** 112 * Access the preferred node. 113 * 114 * @return the node names that we prefer the Evaluator to run on 115 */ 116 public List<String> getNodeNames() { 117 return Collections.unmodifiableList(nodeNames); 118 } 119 120 /** 121 * Access the preferred rack name. 122 * 123 * @return the rack names that we prefer the Evaluator to run on 124 */ 125 public List<String> getRackNames() { 126 return Collections.unmodifiableList(rackNames); 127 } 128 129 /** 130 * Access the required runtime name. 131 * 132 * @return the runtime name that we need the Evaluator to run on 133 */ 134 public String getRuntimeName() { 135 return runtimeName; 136 } 137 138 /** 139 * {@link EvaluatorRequest}s are build using this Builder. 140 */ 141 public static final class Builder implements org.apache.reef.util.Builder<EvaluatorRequest> { 142 143 private int n = 1; 144 private int megaBytes = -1; 145 private int cores = 1; //if not set, default to 1 146 private final List<String> nodeNames = new ArrayList<>(); 147 private final List<String> rackNames = new ArrayList<>(); 148 private String runtimeName = ""; 149 150 private Builder() { 151 } 152 153 /** 154 * Pre-populates the builder with the values extracted from the request. 155 * 156 * @param request the request 157 * @return this Builder 158 */ 159 private Builder(final EvaluatorRequest request) { 160 setNumber(request.getNumber()); 161 setMemory(request.getMegaBytes()); 162 setNumberOfCores(request.getNumberOfCores()); 163 setRuntimeName(request.getRuntimeName()); 164 for (final String nodeName : request.getNodeNames()) { 165 addNodeName(nodeName); 166 } 167 for (final String rackName : request.getRackNames()) { 168 addRackName(rackName); 169 } 170 } 171 172 /** 173 * Set the amount of memory. 174 * 175 * @param megaBytes the amount of megabytes to request for the Evaluator. 176 * @return this builder 177 */ 178 @SuppressWarnings("checkstyle:hiddenfield") 179 public Builder setMemory(final int megaBytes) { 180 this.megaBytes = megaBytes; 181 return this; 182 } 183 184 /** 185 * Set the name of the desired runtime. 186 * 187 * @param runtimeName to request for the Evaluator. 188 * @return this builder 189 */ 190 @SuppressWarnings("checkstyle:hiddenfield") 191 public Builder setRuntimeName(final String runtimeName) { 192 this.runtimeName = runtimeName; 193 return this; 194 } 195 196 /** 197 * Set number of cores. 198 * 199 * @param cores the number of cores 200 * @return this Builder. 201 */ 202 @SuppressWarnings("checkstyle:hiddenfield") 203 public Builder setNumberOfCores(final int cores) { 204 this.cores = cores; 205 return this; 206 } 207 208 /** 209 * Set the number of Evaluators requested. 210 * 211 * @param n the number of evaluators 212 * @return this Builder. 213 */ 214 @SuppressWarnings("checkstyle:hiddenfield") 215 public Builder setNumber(final int n) { 216 this.n = n; 217 return this; 218 } 219 220 /** 221 * Adds a node name.It is the preferred location where the evaluator should 222 * run on. If the node is available, the RM will try to allocate the 223 * evaluator there 224 * 225 * @param nodeName a preferred node name 226 * @return this Builder. 227 */ 228 public Builder addNodeName(final String nodeName) { 229 this.nodeNames.add(nodeName); 230 return this; 231 } 232 233 /** 234 * Adds a rack name. It is the preferred location where the evaluator should 235 * run on. If the rack is available, the RM will try to allocate the 236 * evaluator in one of its nodes. The RM will try to match node names first, 237 * and then fallback to rack names 238 * 239 * @param rackName a preferred rack name 240 * @return this Builder. 241 */ 242 public Builder addRackName(final String rackName) { 243 this.rackNames.add(rackName); 244 return this; 245 } 246 247 /** 248 * Builds the {@link EvaluatorRequest}. 249 */ 250 @Override 251 public EvaluatorRequest build() { 252 return new EvaluatorRequest(this.n, this.megaBytes, this.cores, this.nodeNames, this.rackNames, this.runtimeName); 253 } 254 } 255}