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.ipc;
19  
20  import java.util.List;
21  
22  import org.apache.hadoop.hbase.classification.InterfaceAudience;
23  import org.apache.hadoop.hbase.CellScannable;
24  import org.apache.hadoop.hbase.CellScanner;
25  import org.apache.hadoop.hbase.CellUtil;
26  import org.apache.hadoop.hbase.HConstants;
27  import org.apache.hadoop.hbase.TableName;
28  
29  import com.google.protobuf.RpcCallback;
30  import com.google.protobuf.RpcController;
31  
32  /**
33   * Optionally carries Cells across the proxy/service interface down into ipc. On its
34   * way out it optionally carries a set of result Cell data.  We stick the Cells here when we want
35   * to avoid having to protobuf them.  This class is used ferrying data across the proxy/protobuf
36   * service chasm.  Used by client and server ipc'ing.
37   */
38  @InterfaceAudience.Private
39  public class PayloadCarryingRpcController implements RpcController, CellScannable {
40    public static final int PRIORITY_UNSET = -1;
41  
42    /**
43     * Priority to set on this request.  Set it here in controller so available composing the
44     * request.  This is the ordained way of setting priorities going forward.  We will be
45     * undoing the old annotation-based mechanism.
46     */
47    // Currently only multi call makes use of this.  Eventually this should be only way to set
48    // priority.
49    private int priority = PRIORITY_UNSET;
50  
51    // TODO: Fill out the rest of this class methods rather than return UnsupportedOperationException
52  
53    /**
54     * They are optionally set on construction, cleared after we make the call, and then optionally
55     * set on response with the result. We use this lowest common denominator access to Cells because
56     * sometimes the scanner is backed by a List of Cells and other times, it is backed by an
57     * encoded block that implements CellScanner.
58     */
59    private CellScanner cellScanner;
60  
61    public PayloadCarryingRpcController() {
62      this((CellScanner)null);
63    }
64  
65    public PayloadCarryingRpcController(final CellScanner cellScanner) {
66      this.cellScanner = cellScanner;
67    }
68  
69    public PayloadCarryingRpcController(final List<CellScannable> cellIterables) {
70      this.cellScanner = cellIterables == null? null: CellUtil.createCellScanner(cellIterables);
71    }
72  
73    /**
74     * @return One-shot cell scanner (you cannot back it up and restart)
75     */
76    @Override
77    public CellScanner cellScanner() {
78      return cellScanner;
79    }
80  
81    public void setCellScanner(final CellScanner cellScanner) {
82      this.cellScanner = cellScanner;
83    }
84  
85    @Override
86    public String errorText() {
87      throw new UnsupportedOperationException();
88    }
89  
90    @Override
91    public boolean failed() {
92      throw new UnsupportedOperationException();
93    }
94  
95    @Override
96    public boolean isCanceled() {
97      throw new UnsupportedOperationException();
98    }
99  
100   @Override
101   public void notifyOnCancel(RpcCallback<Object> arg0) {
102     throw new UnsupportedOperationException();
103   }
104 
105   @Override
106   public void reset() {
107     throw new UnsupportedOperationException();
108   }
109 
110   @Override
111   public void setFailed(String arg0) {
112     throw new UnsupportedOperationException();
113   }
114 
115   @Override
116   public void startCancel() {
117     throw new UnsupportedOperationException();
118   }
119 
120   /**
121    * @param priority Priority for this request; should fall roughly in the range
122    * {@link HConstants#NORMAL_QOS} to {@link HConstants#HIGH_QOS}
123    */
124   public void setPriority(int priority) {
125     this.priority = priority;
126   }
127 
128   /**
129    * @param tn Set priority based off the table we are going against.
130    */
131   public void setPriority(final TableName tn) {
132     this.priority = tn != null && tn.isSystemTable()? HConstants.HIGH_QOS: HConstants.NORMAL_QOS;
133   }
134 
135   /**
136    * @return The priority of this request
137    */
138   public int getPriority() {
139     return priority;
140   }
141 }