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.commons.lang3.exception.ExceptionUtils; 022import org.apache.reef.annotations.audience.Interop; 023import org.apache.reef.annotations.audience.Private; 024import org.apache.reef.driver.evaluator.EvaluatorRequestor; 025import org.apache.reef.driver.evaluator.FailedEvaluator; 026import org.apache.reef.exception.NonSerializableException; 027import org.apache.reef.io.naming.Identifiable; 028import org.apache.reef.util.logging.LoggingScopeFactory; 029 030import java.util.Set; 031import java.util.logging.Logger; 032 033/** 034 * The Java-CLR bridge object for {@link org.apache.reef.driver.evaluator.FailedEvaluator}. 035 */ 036@Private 037@Interop(CppFiles = { "FailedEvaluatorClr2Java.cpp" }, CsFiles = { "IFailedEvaluatorClr2Java", "FailedEvaluator" }) 038public final class FailedEvaluatorBridge extends NativeBridge implements Identifiable { 039 private static final Logger LOG = Logger.getLogger(FailedEvaluatorBridge.class.getName()); 040 private final FailedEvaluator jfailedEvaluator; 041 private final EvaluatorRequestorBridge evaluatorRequestorBridge; 042 private final String evaluatorId; 043 private final ActiveContextBridgeFactory activeContextBridgeFactory; 044 045 public FailedEvaluatorBridge(final FailedEvaluator failedEvaluator, 046 final EvaluatorRequestor evaluatorRequestor, 047 final boolean blockedForAdditionalEvaluator, 048 final LoggingScopeFactory loggingScopeFactory, 049 final ActiveContextBridgeFactory activeContextBridgeFactory, 050 final Set<String> definedRuntimes) { 051 this.jfailedEvaluator = failedEvaluator; 052 this.evaluatorId = failedEvaluator.getId(); 053 this.evaluatorRequestorBridge = 054 new EvaluatorRequestorBridge(evaluatorRequestor, blockedForAdditionalEvaluator, loggingScopeFactory, 055 definedRuntimes); 056 this.activeContextBridgeFactory = activeContextBridgeFactory; 057 } 058 059 /** 060 * @return the Evaluator number. 061 */ 062 public int getNewlyRequestedEvaluatorNumber() { 063 return evaluatorRequestorBridge.getEvaluatorNumber(); 064 } 065 066 /** 067 * @return the Evaluator requestor. 068 */ 069 public EvaluatorRequestorBridge getEvaluatorRequestorBridge() { 070 return evaluatorRequestorBridge; 071 } 072 073 /** 074 * @return the non-serializable error in bytes, may translate into a serialized C# Exception. 075 */ 076 public byte[] getErrorBytes() { 077 if (jfailedEvaluator.getEvaluatorException() != null && 078 jfailedEvaluator.getEvaluatorException().getCause() instanceof NonSerializableException) { 079 return ((NonSerializableException)jfailedEvaluator.getEvaluatorException().getCause()).getError(); 080 } 081 082 // If not an instance of NonSerializableException, that means that the Exception is from Java. 083 return null; 084 } 085 086 /** 087 * @return the localized message of the Evaluator Exception. 088 */ 089 public String getCause() { 090 if (jfailedEvaluator.getEvaluatorException() != null) { 091 return jfailedEvaluator.getEvaluatorException().getLocalizedMessage(); 092 } 093 094 return null; 095 } 096 097 /** 098 * @return the stack trace of the Evaluator Exception. 099 */ 100 public String getStackTrace() { 101 if (jfailedEvaluator.getEvaluatorException() != null) { 102 return ExceptionUtils.getStackTrace(jfailedEvaluator.getEvaluatorException()); 103 } 104 105 return null; 106 } 107 108 /** 109 * @return the list of failed Contexts associated with the Evaluator. 110 */ 111 public FailedContextBridge[] getFailedContexts() { 112 if (jfailedEvaluator.getFailedContextList() == null) { 113 return new FailedContextBridge[0]; 114 } 115 116 final FailedContextBridge[] failedContextBridges = 117 new FailedContextBridge[jfailedEvaluator.getFailedContextList().size()]; 118 119 for (int i = 0; i < jfailedEvaluator.getFailedContextList().size(); i++) { 120 failedContextBridges[i] = new FailedContextBridge( 121 jfailedEvaluator.getFailedContextList().get(i), activeContextBridgeFactory); 122 } 123 124 return failedContextBridges; 125 } 126 127 /** 128 * @return the failed task running on the Evaluator, if any. 129 */ 130 public FailedTaskBridge getFailedTask() { 131 if (!jfailedEvaluator.getFailedTask().isPresent()) { 132 return null; 133 } 134 135 return new FailedTaskBridge(jfailedEvaluator.getFailedTask().get(), activeContextBridgeFactory); 136 } 137 138 @Override 139 public void close() { 140 } 141 142 @Override 143 public String getId() { 144 return evaluatorId; 145 } 146} 147