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.client;
020
021import org.apache.reef.annotations.Provided;
022import org.apache.reef.annotations.audience.ClientSide;
023import org.apache.reef.annotations.audience.Private;
024import org.apache.reef.client.REEF;
025import org.apache.reef.client.parameters.DriverConfigurationProviders;
026import org.apache.reef.runtime.common.client.api.JobSubmissionEvent;
027import org.apache.reef.runtime.common.client.api.JobSubmissionHandler;
028import org.apache.reef.runtime.common.launch.parameters.ErrorHandlerRID;
029import org.apache.reef.tang.Configuration;
030import org.apache.reef.tang.ConfigurationBuilder;
031import org.apache.reef.tang.ConfigurationProvider;
032import org.apache.reef.tang.Tang;
033import org.apache.reef.tang.annotations.Name;
034import org.apache.reef.tang.annotations.NamedParameter;
035import org.apache.reef.tang.annotations.Parameter;
036import org.apache.reef.util.REEFVersion;
037import org.apache.reef.util.logging.LoggingScope;
038import org.apache.reef.util.logging.LoggingScopeFactory;
039
040import javax.inject.Inject;
041import java.util.Set;
042import java.util.logging.Logger;
043
044@ClientSide
045@Provided
046@Private
047public final class REEFImplementation implements REEF {
048
049  private final static Logger LOG = Logger.getLogger(REEFImplementation.class.getName());
050
051  private final JobSubmissionHandler jobSubmissionHandler;
052  private final RunningJobs runningJobs;
053  private final JobSubmissionHelper jobSubmissionHelper;
054  private final ClientWireUp clientWireUp;
055  private final LoggingScopeFactory loggingScopeFactory;
056  private final Set<ConfigurationProvider> configurationProviders;
057
058  /**
059   * @param jobSubmissionHandler
060   * @param runningJobs
061   * @param jobSubmissionHelper
062   * @param jobStatusMessageHandler is passed only to make sure it is instantiated
063   * @param clientWireUp
064   * @param reefVersion             provides the current version of REEF.
065   * @param configurationProviders
066   */
067  @Inject
068  REEFImplementation(final JobSubmissionHandler jobSubmissionHandler,
069                     final RunningJobs runningJobs,
070                     final JobSubmissionHelper jobSubmissionHelper,
071                     final JobStatusMessageHandler jobStatusMessageHandler,
072                     final ClientWireUp clientWireUp,
073                     final LoggingScopeFactory loggingScopeFactory,
074                     final REEFVersion reefVersion,
075                     final @Parameter(DriverConfigurationProviders.class) Set<ConfigurationProvider> configurationProviders) {
076    this.jobSubmissionHandler = jobSubmissionHandler;
077    this.runningJobs = runningJobs;
078    this.jobSubmissionHelper = jobSubmissionHelper;
079    this.clientWireUp = clientWireUp;
080    this.configurationProviders = configurationProviders;
081    clientWireUp.performWireUp();
082    this.loggingScopeFactory = loggingScopeFactory;
083    reefVersion.logVersion();
084  }
085
086  @Override
087  public final void close() {
088    this.runningJobs.closeAllJobs();
089    this.clientWireUp.close();
090  }
091
092  @Override
093  public void submit(final Configuration driverConf) {
094    try (LoggingScope ls = this.loggingScopeFactory.reefSubmit()) {
095      final Configuration driverConfiguration = createDriverConfiguration(driverConf);
096      final JobSubmissionEvent submissionMessage;
097      try {
098        if (this.clientWireUp.isClientPresent()) {
099          submissionMessage = this.jobSubmissionHelper.getJobSubmissionBuilder(driverConf)
100              .setRemoteId(this.clientWireUp.getRemoteManagerIdentifier())
101              .build();
102        } else {
103          submissionMessage = this.jobSubmissionHelper.getJobSubmissionBuilder(driverConf)
104              .setRemoteId(ErrorHandlerRID.NONE)
105              .build();
106        }
107      } catch (final Exception e) {
108        throw new RuntimeException("Exception while processing driver configuration.", e);
109      }
110
111      this.jobSubmissionHandler.onNext(submissionMessage);
112    }
113  }
114
115  /**
116   * Assembles the final Driver Configuration by merging in all the Configurations provided by ConfigurationProviders.
117   *
118   * @param driverConfiguration
119   * @return
120   */
121  private Configuration createDriverConfiguration(final Configuration driverConfiguration) {
122    final ConfigurationBuilder configurationBuilder = Tang.Factory.getTang()
123        .newConfigurationBuilder(driverConfiguration);
124    for (final ConfigurationProvider configurationProvider : this.configurationProviders) {
125      configurationBuilder.addConfiguration(configurationProvider.getConfiguration());
126    }
127    return configurationBuilder.build();
128  }
129
130  @NamedParameter(doc = "The driver remote identifier.")
131  public final static class DriverRemoteIdentifier implements Name<String> {
132  }
133
134
135}