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; 024import org.apache.hadoop.conf.Configuration; 025import org.apache.hadoop.hbase.DoNotRetryIOException; 026import org.apache.hadoop.hbase.HBaseConfiguration; 027import org.apache.hadoop.hbase.HBaseServerException; 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 was a 034 * {@link org.apache.hadoop.hbase.DoNotRetryIOException}, {@link #isDoNotRetry()} will return true. 035 * <p> 036 * 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 private final boolean serverOverloaded; 045 046 /** 047 * Dynamic class loader to load filter/comparators 048 */ 049 private final static class ClassLoaderHolder { 050 private final static ClassLoader CLASS_LOADER; 051 052 static { 053 ClassLoader parent = RemoteWithExtrasException.class.getClassLoader(); 054 Configuration conf = HBaseConfiguration.create(); 055 CLASS_LOADER = AccessController 056 .doPrivileged((PrivilegedAction<ClassLoader>) () -> new DynamicClassLoader(conf, parent)); 057 } 058 } 059 060 public RemoteWithExtrasException(String className, String msg, final boolean doNotRetry) { 061 this(className, msg, doNotRetry, false); 062 } 063 064 public RemoteWithExtrasException(String className, String msg, final boolean doNotRetry, 065 final boolean serverOverloaded) { 066 this(className, msg, null, -1, doNotRetry, serverOverloaded); 067 } 068 069 public RemoteWithExtrasException(String className, String msg, final String hostname, 070 final int port, final boolean doNotRetry) { 071 this(className, msg, hostname, port, doNotRetry, false); 072 } 073 074 public RemoteWithExtrasException(String className, String msg, final String hostname, 075 final int port, final boolean doNotRetry, final boolean serverOverloaded) { 076 super(className, msg); 077 this.hostname = hostname; 078 this.port = port; 079 this.doNotRetry = doNotRetry; 080 this.serverOverloaded = serverOverloaded; 081 } 082 083 @Override 084 public IOException unwrapRemoteException() { 085 Class<?> realClass; 086 try { 087 // try to load a exception class from where the HBase classes are loaded or from Dynamic 088 // classloader. 089 realClass = Class.forName(getClassName(), false, ClassLoaderHolder.CLASS_LOADER); 090 } catch (ClassNotFoundException cnfe) { 091 try { 092 // cause could be a hadoop exception, try to load from hadoop classpath 093 realClass = Class.forName(getClassName(), false, super.getClass().getClassLoader()); 094 } catch (ClassNotFoundException e) { 095 return new DoNotRetryIOException( 096 "Unable to load exception received from server:" + e.getMessage(), this); 097 } 098 } 099 try { 100 return instantiateException(realClass.asSubclass(IOException.class)); 101 } catch (Exception e) { 102 return new DoNotRetryIOException( 103 "Unable to instantiate exception received from server:" + e.getMessage(), this); 104 } 105 } 106 107 private IOException instantiateException(Class<? extends IOException> cls) throws Exception { 108 Constructor<? extends IOException> cn = cls.getConstructor(String.class); 109 cn.setAccessible(true); 110 IOException ex = cn.newInstance(this.getMessage()); 111 ex.initCause(this); 112 113 if (ex instanceof HBaseServerException) { 114 // this is a newly constructed exception. 115 // if an exception defaults to meaning isServerOverloaded, we use that. 116 // otherwise, see if the remote exception value should mean setting to true. 117 HBaseServerException serverException = (HBaseServerException) ex; 118 if (serverOverloaded && !serverException.isServerOverloaded()) { 119 serverException.setServerOverloaded(true); 120 } 121 } 122 123 return ex; 124 } 125 126 /** Returns null if not set */ 127 public String getHostname() { 128 return this.hostname; 129 } 130 131 /** Returns -1 if not set */ 132 public int getPort() { 133 return this.port; 134 } 135 136 /** Returns True if origin exception was a do not retry type. */ 137 public boolean isDoNotRetry() { 138 return this.doNotRetry; 139 } 140 141 /** Returns True if the server was considered overloaded when the exception was thrown. */ 142 public boolean isServerOverloaded() { 143 return serverOverloaded; 144 } 145}