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, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.ipc; 019 020import java.io.IOException; 021import java.lang.reflect.Constructor; 022import java.security.AccessController; 023import java.security.PrivilegedAction; 024 025import org.apache.hadoop.conf.Configuration; 026import org.apache.hadoop.hbase.DoNotRetryIOException; 027import org.apache.hadoop.hbase.HBaseConfiguration; 028import org.apache.hadoop.hbase.util.DynamicClassLoader; 029import org.apache.hadoop.ipc.RemoteException; 030import org.apache.yetus.audience.InterfaceAudience; 031 032/** 033 * A {@link RemoteException} with some extra information. If source exception 034 * was a {@link org.apache.hadoop.hbase.DoNotRetryIOException}, 035 * {@link #isDoNotRetry()} will return true. 036 * <p>A {@link RemoteException} hosts exceptions we got from the server. 037 */ 038@SuppressWarnings("serial") 039@InterfaceAudience.Public 040public class RemoteWithExtrasException extends RemoteException { 041 private final String hostname; 042 private final int port; 043 private final boolean doNotRetry; 044 045 /** 046 * Dynamic class loader to load filter/comparators 047 */ 048 private final static class ClassLoaderHolder { 049 private final static ClassLoader CLASS_LOADER; 050 051 static { 052 ClassLoader parent = RemoteWithExtrasException.class.getClassLoader(); 053 Configuration conf = HBaseConfiguration.create(); 054 CLASS_LOADER = AccessController.doPrivileged((PrivilegedAction<ClassLoader>) 055 () -> new DynamicClassLoader(conf, parent) 056 ); 057 } 058 } 059 060 public RemoteWithExtrasException(String className, String msg, final boolean doNotRetry) { 061 this(className, msg, null, -1, doNotRetry); 062 } 063 064 public RemoteWithExtrasException(String className, String msg, final String hostname, 065 final int port, final boolean doNotRetry) { 066 super(className, msg); 067 this.hostname = hostname; 068 this.port = port; 069 this.doNotRetry = doNotRetry; 070 } 071 072 @Override 073 public IOException unwrapRemoteException() { 074 Class<?> realClass; 075 try { 076 // try to load a exception class from where the HBase classes are loaded or from Dynamic 077 // classloader. 078 realClass = Class.forName(getClassName(), false, ClassLoaderHolder.CLASS_LOADER); 079 } catch (ClassNotFoundException cnfe) { 080 try { 081 // cause could be a hadoop exception, try to load from hadoop classpath 082 realClass = Class.forName(getClassName(), false, super.getClass().getClassLoader()); 083 } catch (ClassNotFoundException e) { 084 return new DoNotRetryIOException( 085 "Unable to load exception received from server:" + e.getMessage(), this); 086 } 087 } 088 try { 089 return instantiateException(realClass.asSubclass(IOException.class)); 090 } catch (Exception e) { 091 return new DoNotRetryIOException( 092 "Unable to instantiate exception received from server:" + e.getMessage(), this); 093 } 094 } 095 096 private IOException instantiateException(Class<? extends IOException> cls) throws Exception { 097 Constructor<? extends IOException> cn = cls.getConstructor(String.class); 098 cn.setAccessible(true); 099 IOException ex = cn.newInstance(this.getMessage()); 100 ex.initCause(this); 101 return ex; 102 } 103 104 /** 105 * @return null if not set 106 */ 107 public String getHostname() { 108 return this.hostname; 109 } 110 111 /** 112 * @return -1 if not set 113 */ 114 public int getPort() { 115 return this.port; 116 } 117 118 /** 119 * @return True if origin exception was a do not retry type. 120 */ 121 public boolean isDoNotRetry() { 122 return this.doNotRetry; 123 } 124}