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.evaluator;
020
021import org.apache.reef.annotations.audience.DriverSide;
022import org.apache.reef.annotations.audience.Private;
023import org.apache.reef.driver.catalog.NodeDescriptor;
024import org.apache.reef.driver.catalog.ResourceCatalog;
025import org.apache.reef.driver.evaluator.EvaluatorType;
026import org.apache.reef.runtime.common.driver.resourcemanager.NodeDescriptorHandler;
027import org.apache.reef.runtime.common.driver.resourcemanager.ResourceAllocationEvent;
028import org.apache.reef.runtime.common.driver.resourcemanager.ResourceStatusEvent;
029import org.apache.reef.tang.Injector;
030import org.apache.reef.tang.exceptions.BindException;
031import org.apache.reef.tang.exceptions.InjectionException;
032
033import javax.inject.Inject;
034import java.util.logging.Level;
035import java.util.logging.Logger;
036
037/**
038 * Helper class that creates new EvaluatorManager instances from alloations.
039 */
040@Private
041@DriverSide
042public final class EvaluatorManagerFactory {
043  private static final Logger LOG = Logger.getLogger(EvaluatorManagerFactory.class.getName());
044
045  private final Injector injector;
046  private final ResourceCatalog resourceCatalog;
047
048  @Inject
049  EvaluatorManagerFactory(final Injector injector, final ResourceCatalog resourceCatalog, final NodeDescriptorHandler nodeDescriptorHandler) {
050    this.injector = injector;
051    this.resourceCatalog = resourceCatalog;
052  }
053
054  /**
055   * Helper method to create a new EvaluatorManager instance
056   *
057   * @param id   identifier of the Evaluator
058   * @param desc NodeDescriptor on which the Evaluator executes.
059   * @return a new EvaluatorManager instance.
060   */
061  private final EvaluatorManager getNewEvaluatorManagerInstance(final String id, final EvaluatorDescriptorImpl desc) {
062    LOG.log(Level.FINEST, "Creating Evaluator Manager for Evaluator ID {0}", id);
063    final Injector child = this.injector.forkInjector();
064
065    try {
066      child.bindVolatileParameter(EvaluatorManager.EvaluatorIdentifier.class, id);
067      child.bindVolatileParameter(EvaluatorManager.EvaluatorDescriptorName.class, desc);
068    } catch (final BindException e) {
069      throw new RuntimeException("Unable to bind evaluator identifier and name.", e);
070    }
071
072    final EvaluatorManager result;
073    try {
074      result = child.getInstance(EvaluatorManager.class);
075    } catch (final InjectionException e) {
076      throw new RuntimeException("Unable to instantiate a new EvaluatorManager for Evaluator ID: " + id, e);
077    }
078    return result;
079  }
080
081  /**
082   * Instantiates a new EvaluatorManager based on a resource allocation.
083   *
084   * @param resourceAllocationEvent
085   * @return
086   */
087  public final EvaluatorManager getNewEvaluatorManager(final ResourceAllocationEvent resourceAllocationEvent) {
088    final NodeDescriptor nodeDescriptor = this.resourceCatalog.getNode(resourceAllocationEvent.getNodeId());
089
090    if (nodeDescriptor == null) {
091      throw new RuntimeException("Unknown resource: " + resourceAllocationEvent.getNodeId());
092    }
093    final EvaluatorDescriptorImpl evaluatorDescriptor = new EvaluatorDescriptorImpl(nodeDescriptor,
094        EvaluatorType.UNDECIDED, resourceAllocationEvent.getResourceMemory(), resourceAllocationEvent.getVirtualCores().get());
095
096    LOG.log(Level.FINEST, "Resource allocation: new evaluator id[{0}]", resourceAllocationEvent.getIdentifier());
097    return this.getNewEvaluatorManagerInstance(resourceAllocationEvent.getIdentifier(), evaluatorDescriptor);
098  }
099
100  public final EvaluatorManager createForEvaluatorFailedDuringDriverRestart(final ResourceStatusEvent resourceStatusEvent) {
101    if (!resourceStatusEvent.getIsFromPreviousDriver().get()) {
102      throw new RuntimeException("Invalid resourceStatusEvent, must be status for resource from previous Driver.");
103    }
104    return getNewEvaluatorManagerInstance(resourceStatusEvent.getIdentifier(), new EvaluatorDescriptorImpl(null, EvaluatorType.UNDECIDED, 128, 1));
105  }
106}