All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.hadoop.yarn.ipc.RPCUtil Maven / Gradle / Ivy

There is a newer version: 3.4.1
Show newest version
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in org.apache.hadoop.shaded.com.liance
* with the License.  You may obtain a copy of the License at
*
*     http://www.apache.org.apache.hadoop.shaded.org.licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hadoop.shaded.org.apache.hadoop.yarn.ipc;

import java.org.apache.hadoop.shaded.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import org.apache.hadoop.shaded.org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.shaded.org.apache.hadoop.ipc.RemoteException;
import org.apache.hadoop.shaded.org.apache.hadoop.yarn.exceptions.YarnException;

import org.apache.hadoop.shaded.org.apache.hadoop.thirdparty.protobuf.ServiceException;

@InterfaceAudience.LimitedPrivate({ "MapReduce", "YARN" })
public class RPCUtil {

  /**
   * Returns an instance of {@link YarnException}
   */
  public static YarnException getRemoteException(Throwable t) {
    return new YarnException(t);
  }

  /**
   * Returns an instance of {@link YarnException}
   */
  public static YarnException getRemoteException(String message) {
    return new YarnException(message);
  }

  private static  T instantiateException(
      Class cls, RemoteException re) throws RemoteException {
    try {
      Constructor cn = cls.getConstructor(String.class);
      cn.setAccessible(true);
      T ex = cn.newInstance(re.getMessage());
      ex.initCause(re);
      return ex;
      // RemoteException contains useful information as against the
      // java.lang.reflect exceptions.
    } catch (NoSuchMethodException e) {
      throw re;
    } catch (IllegalArgumentException e) {
      throw re;
    } catch (SecurityException e) {
      throw re;
    } catch (InstantiationException e) {
      throw re;
    } catch (IllegalAccessException e) {
      throw re;
    } catch (InvocationTargetException e) {
      throw re;
    }
  }

  private static  T instantiateYarnException(
      Class cls, RemoteException re) throws RemoteException {
    return instantiateException(cls, re);
  }

  private static  T instantiateIOException(
      Class cls, RemoteException re) throws RemoteException {
    return instantiateException(cls, re);
  }

  private static  T instantiateRuntimeException(
      Class cls, RemoteException re) throws RemoteException {
    return instantiateException(cls, re);
  }

  /**
   * Utility method that unwraps and returns appropriate exceptions.
   * 
   * @param se
   *          ServiceException
   * @return An instance of the actual exception, which will be a subclass of
   *         {@link YarnException} or {@link IOException}
   */
  public static Void unwrapAndThrowException(ServiceException se)
      throws IOException, YarnException {
    Throwable cause = se.getCause();
    if (cause == null) {
      // SE generated by the RPC layer itself.
      throw new IOException(se);
    } else {
      if (cause instanceof RemoteException) {
        RemoteException re = (RemoteException) cause;
        Class realClass = null;
        try {
          realClass = Class.forName(re.getClassName());
        } catch (ClassNotFoundException cnf) {
          // Assume this to be a new exception type added to YARN. This isn't
          // absolutely correct since the RPC layer could add an exception as
          // well.
          throw instantiateYarnException(YarnException.class, re);
        }

        if (YarnException.class.isAssignableFrom(realClass)) {
          throw instantiateYarnException(
              realClass.asSubclass(YarnException.class), re);
        } else if (IOException.class.isAssignableFrom(realClass)) {
          throw instantiateIOException(realClass.asSubclass(IOException.class),
              re);
        } else if (RuntimeException.class.isAssignableFrom(realClass)) {
          throw instantiateRuntimeException(
              realClass.asSubclass(RuntimeException.class), re);
        } else {
          throw re;
        }
        // RemoteException contains useful information as against the
        // java.lang.reflect exceptions.

      } else if (cause instanceof IOException) {
        // RPC Client exception.
        throw (IOException) cause;
      } else if (cause instanceof RuntimeException) {
        // RPC RuntimeException
        throw (RuntimeException) cause;
      } else {
        // Should not be generated.
        throw new IOException(se);
      }
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy