View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.procedure2;
19  
20  import java.io.IOException;
21  
22  import org.apache.hadoop.ipc.RemoteException;
23  import org.apache.hadoop.hbase.classification.InterfaceAudience;
24  import org.apache.hadoop.hbase.classification.InterfaceStability;
25  import org.apache.hadoop.hbase.protobuf.generated.ErrorHandlingProtos.ForeignExceptionMessage;
26  import org.apache.hadoop.hbase.util.ForeignExceptionUtil;
27  
28  import com.google.protobuf.InvalidProtocolBufferException;
29  
30  /**
31   * A RemoteProcedureException is an exception from another thread or process.
32   * <p>
33   * RemoteProcedureExceptions are sent to 'remote' peers to signal an abort in the face of failures.
34   * When serialized for transmission we encode using Protobufs to ensure version compatibility.
35   * <p>
36   * RemoteProcedureException exceptions contain a Throwable as its cause.
37   * This can be a "regular" exception generated locally or a ProxyThrowable that is a representation
38   * of the original exception created on original 'remote' source.  These ProxyThrowables have their
39   * their stacks traces and messages overridden to reflect the original 'remote' exception.
40   */
41  @InterfaceAudience.Private
42  @InterfaceStability.Evolving
43  @SuppressWarnings("serial")
44  public class RemoteProcedureException extends ProcedureException {
45  
46    /**
47     * Name of the throwable's source such as a host or thread name.  Must be non-null.
48     */
49    private final String source;
50  
51    /**
52     * Create a new RemoteProcedureException that can be serialized.
53     * It is assumed that this came form a local source.
54     * @param source
55     * @param cause
56     */
57    public RemoteProcedureException(String source, Throwable cause) {
58      super(cause);
59      assert source != null;
60      assert cause != null;
61      this.source = source;
62    }
63  
64    public String getSource() {
65      return source;
66    }
67  
68    public IOException unwrapRemoteException() {
69      if (getCause() instanceof RemoteException) {
70        return ((RemoteException)getCause()).unwrapRemoteException();
71      }
72      if (getCause() instanceof IOException) {
73        return (IOException)getCause();
74      }
75      return new IOException(getCause());
76    }
77  
78    @Override
79    public String toString() {
80      String className = getCause().getClass().getName();
81      return className + " via " + getSource() + ":" + getLocalizedMessage();
82    }
83  
84    /**
85     * Converts a RemoteProcedureException to an array of bytes.
86     * @param source the name of the external exception source
87     * @param t the "local" external exception (local)
88     * @return protobuf serialized version of RemoteProcedureException
89     */
90    public static byte[] serialize(String source, Throwable t) {
91      return toProto(source, t).toByteArray();
92    }
93  
94    /**
95     * Takes a series of bytes and tries to generate an RemoteProcedureException instance for it.
96     * @param bytes
97     * @return the ForeignExcpetion instance
98     * @throws InvalidProtocolBufferException if there was deserialization problem this is thrown.
99     */
100   public static RemoteProcedureException deserialize(byte[] bytes)
101       throws InvalidProtocolBufferException {
102     return fromProto(ForeignExceptionMessage.parseFrom(bytes));
103   }
104 
105   public ForeignExceptionMessage convert() {
106     return ForeignExceptionUtil.toProtoForeignException(getSource(), getCause());
107   }
108 
109   public static ForeignExceptionMessage toProto(String source, Throwable t) {
110     return ForeignExceptionUtil.toProtoForeignException(source, t);
111   }
112 
113   public static RemoteProcedureException fromProto(final ForeignExceptionMessage eem) {
114     return new RemoteProcedureException(eem.getSource(), ForeignExceptionUtil.toIOException(eem));
115   }
116 }