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 org.apache.reef.driver.catalog.NodeDescriptor; 025import org.apache.reef.driver.catalog.ResourceCatalog; 026 027/** 028 * A request for one ore more Evaluators. 029 */ 030@Public 031@DriverSide 032@Provided 033public final class EvaluatorRequest { 034 035 private final int megaBytes; 036 private final int number; 037 private final int cores; 038 private final ResourceCatalog.Descriptor descriptor; 039 040 EvaluatorRequest(final int number, 041 final int megaBytes, 042 final int cores, 043 final ResourceCatalog.Descriptor descriptor) { 044 this.number = number; 045 this.megaBytes = megaBytes; 046 this.cores = cores; 047 this.descriptor = descriptor; 048 } 049 050 /** 051 * @return a new EvaluatorRequest Builder. 052 */ 053 public static Builder newBuilder() { 054 return new Builder(); 055 } 056 057 /** 058 * @return a new EvaluatorRequest Builder with settings initialized 059 * from an existing request. 060 */ 061 public static Builder newBuilder(final EvaluatorRequest request) { 062 return new Builder(request); 063 } 064 065 /** 066 * Access the number of Evaluators requested. 067 * 068 * @return the number of Evaluators requested. 069 */ 070 public int getNumber() { 071 return this.number; 072 } 073 074 /** 075 * Access the number of core of Evaluators requested. 076 * 077 * @return the number of cores requested. 078 */ 079 public int getNumberOfCores() { 080 return this.cores; 081 } 082 083 /** 084 * Access the {@link NodeDescriptor} used as the template for this 085 * {@link EvaluatorRequest}. 086 * 087 * @return the {@link NodeDescriptor} used as the template for this 088 * {@link EvaluatorRequest}. 089 */ 090 public final ResourceCatalog.Descriptor getDescriptor() { 091 return this.descriptor; 092 } 093 094 /** 095 * @return the minimum size of Evaluator requested. 096 */ 097 public int getMegaBytes() { 098 return megaBytes; 099 } 100 101 /** 102 * {@link EvaluatorRequest}s are build using this Builder. 103 */ 104 public static class Builder implements org.apache.reef.util.Builder<EvaluatorRequest> { 105 106 private int n = 1; 107 private ResourceCatalog.Descriptor descriptor = null; 108 private int megaBytes = -1; 109 private int cores = 1; //if not set, default to 1 110 111 private Builder() { 112 } 113 114 private Builder(final EvaluatorRequest request) { 115 setNumber(request.getNumber()); 116 fromDescriptor(request.getDescriptor()); 117 } 118 119 /** 120 * @param megaBytes the amount of megabytes to request for the Evaluator. 121 * @return this builder 122 */ 123 public Builder setMemory(final int megaBytes) { 124 this.megaBytes = megaBytes; 125 return this; 126 } 127 128 /** 129 * set number of cores 130 * 131 * @param cores the number of cores 132 * @return 133 */ 134 public Builder setNumberOfCores(final int cores) { 135 this.cores = cores; 136 return this; 137 } 138 139 /** 140 * Set the number of Evaluators requested. 141 * 142 * @param n 143 * @return this Builder. 144 */ 145 public Builder setNumber(final int n) { 146 this.n = n; 147 return this; 148 } 149 150 /** 151 * Builds the {@link EvaluatorRequest}. 152 */ 153 @Override 154 public EvaluatorRequest build() { 155 return new EvaluatorRequest(this.n, this.megaBytes, this.cores, this.descriptor); 156 } 157 158 /** 159 * Pre-fill this {@link EvaluatorRequest} from the given 160 * {@link NodeDescriptor}. Any value not changed in subsequent calls to 161 * this Builder will be taken from the given descriptor. 162 * 163 * @param rd the descriptor used to pre-fill this request. 164 * @return this 165 */ 166 public Builder fromDescriptor(final ResourceCatalog.Descriptor rd) { 167 this.descriptor = rd; 168 return this; 169 } 170 } 171}