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  
19  package org.apache.hadoop.hbase.ipc;
20  
21  import java.io.IOException;
22  import java.util.concurrent.atomic.AtomicReference;
23  
24  import org.apache.hadoop.hbase.classification.InterfaceAudience;
25  
26  import com.google.protobuf.RpcCallback;
27  import com.google.protobuf.RpcController;
28  
29  @InterfaceAudience.Private
30  public class TimeLimitedRpcController implements RpcController {
31  
32    /**
33     * The time, in ms before the call should expire.
34     */
35    protected volatile Integer callTimeout;
36    protected volatile boolean cancelled = false;
37    protected final AtomicReference<RpcCallback<Object>> cancellationCb =
38        new AtomicReference<RpcCallback<Object>>(null);
39  
40    protected final AtomicReference<RpcCallback<IOException>> failureCb =
41        new AtomicReference<RpcCallback<IOException>>(null);
42  
43    private IOException exception;
44  
45    public Integer getCallTimeout() {
46      return callTimeout;
47    }
48  
49    public void setCallTimeout(int callTimeout) {
50      this.callTimeout = callTimeout;
51    }
52  
53    public boolean hasCallTimeout(){
54      return callTimeout != null;
55    }
56  
57    @Override
58    public String errorText() {
59      if (exception != null) {
60        return exception.getMessage();
61      } else {
62        return null;
63      }
64    }
65  
66    /**
67     * For use in async rpc clients
68     * @return true if failed
69     */
70    @Override
71    public boolean failed() {
72      return this.exception != null;
73    }
74  
75    @Override
76    public boolean isCanceled() {
77      return cancelled;
78    }
79  
80    @Override
81    public void notifyOnCancel(RpcCallback<Object> cancellationCb) {
82      this.cancellationCb.set(cancellationCb);
83      if (this.cancelled) {
84        cancellationCb.run(null);
85      }
86    }
87  
88    /**
89     * Notify a callback on error.
90     * For use in async rpc clients
91     *
92     * @param failureCb the callback to call on error
93     */
94    public void notifyOnFail(RpcCallback<IOException> failureCb) {
95      this.failureCb.set(failureCb);
96      if (this.exception != null) {
97        failureCb.run(this.exception);
98      }
99    }
100 
101   @Override
102   public void reset() {
103     exception = null;
104     cancelled = false;
105     failureCb.set(null);
106     cancellationCb.set(null);
107     callTimeout = null;
108   }
109 
110   @Override
111   public void setFailed(String reason) {
112     this.exception = new IOException(reason);
113     if (this.failureCb.get() != null) {
114       this.failureCb.get().run(this.exception);
115     }
116   }
117 
118   /**
119    * Set failed with an exception to pass on.
120    * For use in async rpc clients
121    *
122    * @param e exception to set with
123    */
124   public void setFailed(IOException e) {
125     this.exception = e;
126     if (this.failureCb.get() != null) {
127       this.failureCb.get().run(this.exception);
128     }
129   }
130 
131   @Override
132   public void startCancel() {
133     cancelled = true;
134     if (cancellationCb.get() != null) {
135       cancellationCb.get().run(null);
136     }
137   }
138 }