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.vortex.driver; 020 021import org.apache.reef.annotations.Unstable; 022import org.apache.reef.annotations.audience.Private; 023import org.apache.reef.client.DriverConfiguration; 024import org.apache.reef.tang.Configuration; 025import org.apache.reef.tang.Configurations; 026import org.apache.reef.util.BuilderUtils; 027import org.apache.reef.util.EnvironmentUtils; 028import org.apache.reef.util.Optional; 029 030/** 031 * Helper class for building a configuration for Vortex. 032 */ 033@Unstable 034public final class VortexJobConf { 035 private final Configuration conf; 036 037 private VortexJobConf(final Configuration conf) { 038 this.conf = conf; 039 } 040 041 /** 042 * Create a Builder object for Vortex job configuration. 043 */ 044 public static Builder newBuilder() { 045 return new VortexJobConf.Builder(); 046 } 047 048 /** 049 * Convert to the Tang Configuration. 050 */ 051 @Private 052 public Configuration getConfiguration() { 053 return conf; 054 } 055 056 /** 057 * Builder object to create a {@link VortexJobConf}. 058 */ 059 public static final class Builder implements org.apache.reef.util.Builder<VortexJobConf> { 060 private String jobName; 061 private Configuration vortexMasterConf; 062 private Optional<Configuration> userConf = Optional.empty(); 063 064 private Builder() { 065 } 066 067 /** 068 * @param vortexMasterConf Configuration for the Vortex Master, which can be built via {@link VortexMasterConf}. 069 */ 070 public Builder setVortexMasterConf(final Configuration vortexMasterConf) { 071 this.vortexMasterConf = vortexMasterConf; 072 return this; 073 } 074 075 /** 076 * @param userConf Configuration set by user (e.g., Parameters in {@link org.apache.reef.vortex.api.VortexStart} 077 */ 078 public Builder setUserConf(final Configuration userConf) { 079 this.userConf = Optional.of(userConf); 080 return this; 081 } 082 083 /** 084 * @param jobName Name of the job which is assigned to the Driver. 085 */ 086 public Builder setJobName(final String jobName) { 087 this.jobName = jobName; 088 return this; 089 } 090 091 /** 092 * Instantiate a {@link VortexJobConf} object, where a Configuration is built by Tang internally. 093 * 094 * {@link IllegalArgumentException} will be thrown if required parameters are not set 095 * (See {@link #setJobName(String)} and {@link #setVortexMasterConf(Configuration)}). 096 * 097 * Also, {@link org.apache.reef.tang.exceptions.BindException} can be thrown while merging the configurations. 098 * 099 * @return An instance of VortexJobConf object. 100 */ 101 @Override 102 public VortexJobConf build() { 103 BuilderUtils.notNull(jobName); 104 BuilderUtils.notNull(vortexMasterConf); 105 106 final Configuration vortexDriverConf = DriverConfiguration.CONF 107 .set(DriverConfiguration.GLOBAL_LIBRARIES, EnvironmentUtils.getClassLocation(VortexDriver.class)) 108 .set(DriverConfiguration.ON_DRIVER_STARTED, VortexDriver.StartHandler.class) 109 .set(DriverConfiguration.ON_EVALUATOR_ALLOCATED, VortexDriver.AllocatedEvaluatorHandler.class) 110 .set(DriverConfiguration.ON_TASK_RUNNING, VortexDriver.RunningTaskHandler.class) 111 .set(DriverConfiguration.ON_TASK_MESSAGE, VortexDriver.TaskMessageHandler.class) 112 .set(DriverConfiguration.ON_EVALUATOR_FAILED, VortexDriver.FailedEvaluatorHandler.class) 113 .set(DriverConfiguration.DRIVER_IDENTIFIER, jobName) 114 .build(); 115 116 final Configuration jobConf; 117 if (userConf.isPresent()) { 118 jobConf = Configurations.merge(vortexDriverConf, vortexMasterConf, userConf.get()); 119 } else { 120 jobConf = Configurations.merge(vortexDriverConf, vortexMasterConf); 121 } 122 return new VortexJobConf(jobConf); 123 } 124 } 125}