This project has retired. For details please refer to its Attic page.
Source code
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.javabridge;
020
021import org.apache.reef.annotations.audience.Interop;
022import org.apache.reef.annotations.audience.Private;
023import org.apache.reef.driver.evaluator.EvaluatorRequest;
024import org.apache.reef.driver.evaluator.EvaluatorRequestor;
025import org.apache.reef.util.logging.LoggingScope;
026import org.apache.reef.util.logging.LoggingScopeFactory;
027
028import java.util.logging.Level;
029import java.util.logging.Logger;
030
031/**
032 * The Java-CLR bridge object for {@link org.apache.reef.driver.evaluator.EvaluatorRequestor}.
033 */
034@Private
035@Interop(
036    CppFiles = { "Clr2JavaImpl.h", "EvaluatorRequestorClr2Java.cpp" },
037    CsFiles = { "IEvaluatorRequestorClr2Java.cs", "EvaluatorRequestor.cs" })
038public final class EvaluatorRequestorBridge extends NativeBridge {
039  private static final Logger LOG = Logger.getLogger(EvaluatorRequestorBridge.class.getName());
040  private final boolean isBlocked;
041  private final EvaluatorRequestor jevaluatorRequestor;
042  private final LoggingScopeFactory loggingScopeFactory;
043
044  // accumulate how many evaluators have been submitted through this instance
045  // of EvaluatorRequestorBridge
046  private int clrEvaluatorsNumber;
047
048  public EvaluatorRequestorBridge(final EvaluatorRequestor evaluatorRequestor,
049                                  final boolean isBlocked,
050                                  final LoggingScopeFactory loggingScopeFactory) {
051    this.jevaluatorRequestor = evaluatorRequestor;
052    this.clrEvaluatorsNumber = 0;
053    this.isBlocked = isBlocked;
054    this.loggingScopeFactory = loggingScopeFactory;
055  }
056
057  public void submit(final int evaluatorsNumber, final int memory, final int virtualCore, final String rack) {
058    if (this.isBlocked) {
059      throw new RuntimeException("Cannot request additional Evaluator, this is probably because " +
060          "the Driver has crashed and restarted, and cannot ask for new container due to YARN-2433.");
061    }
062
063    if (rack != null && !rack.isEmpty()) {
064      LOG.log(Level.WARNING, "Ignoring rack preference.");
065    }
066
067    try (final LoggingScope ls = loggingScopeFactory.evaluatorRequestSubmitToJavaDriver(evaluatorsNumber)) {
068      clrEvaluatorsNumber += evaluatorsNumber;
069
070      final EvaluatorRequest request = EvaluatorRequest.newBuilder()
071          .setNumber(evaluatorsNumber)
072          .setMemory(memory)
073          .setNumberOfCores(virtualCore)
074          .build();
075
076      LOG.log(Level.FINE, "submitting evaluator request {0}", request);
077      jevaluatorRequestor.submit(request);
078    }
079  }
080
081  public int getEvaluatorNumber() {
082    return clrEvaluatorsNumber;
083  }
084
085  @Override
086  public void close() {
087  }
088}