1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.regionserver;
20
21 import static org.apache.hadoop.hbase.util.Bytes.getBytes;
22
23 import java.io.IOException;
24 import java.nio.ByteBuffer;
25 import java.util.List;
26 import java.util.Map;
27
28 import org.apache.commons.logging.Log;
29 import org.apache.commons.logging.LogFactory;
30 import org.apache.hadoop.classification.InterfaceAudience;
31 import org.apache.hadoop.conf.Configuration;
32 import org.apache.hadoop.hbase.HRegionLocation;
33 import org.apache.hadoop.hbase.KeyValue;
34 import org.apache.hadoop.hbase.exceptions.NotServingRegionException;
35 import org.apache.hadoop.hbase.client.Get;
36 import org.apache.hadoop.hbase.client.HTable;
37 import org.apache.hadoop.hbase.client.Result;
38 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
39 import org.apache.hadoop.hbase.thrift.ThriftServerRunner;
40 import org.apache.hadoop.hbase.thrift.ThriftUtilities;
41 import org.apache.hadoop.hbase.thrift.generated.IOError;
42 import org.apache.hadoop.hbase.thrift.generated.TRowResult;
43
44
45
46
47
48
49
50
51
52
53
54 @InterfaceAudience.Private
55 public class HRegionThriftServer extends Thread {
56
57 public static final Log LOG = LogFactory.getLog(HRegionThriftServer.class);
58
59 private final HRegionServer rs;
60 private final ThriftServerRunner serverRunner;
61
62
63
64
65
66 HRegionThriftServer(HRegionServer regionServer, Configuration conf)
67 throws IOException {
68 super("Region Thrift Server");
69 this.rs = regionServer;
70 this.serverRunner =
71 new ThriftServerRunner(conf, new HBaseHandlerRegion(conf));
72 }
73
74
75
76
77 void shutdown() {
78 serverRunner.shutdown();
79 }
80
81 @Override
82 public void run() {
83 serverRunner.run();
84 }
85
86
87
88
89
90
91 private class HBaseHandlerRegion extends ThriftServerRunner.HBaseHandler {
92
93
94
95
96
97 private boolean redirect;
98
99 HBaseHandlerRegion(final Configuration conf) throws IOException {
100 super(conf);
101 initialize(conf);
102 }
103
104
105
106
107 private void initialize(Configuration conf) {
108 this.redirect = conf.getBoolean("hbase.regionserver.thrift.redirect",
109 false);
110 }
111
112
113
114
115
116
117 @Override
118 public List<TRowResult> getRowWithColumnsTs(ByteBuffer tableName,
119 ByteBuffer rowb,
120 List<ByteBuffer> columns,
121 long timestamp,
122 Map<ByteBuffer, ByteBuffer> attributes) throws IOError {
123 try {
124 byte[] row = getBytes(rowb);
125
126 HTable table = getTable(getBytes(tableName));
127 HRegionLocation location = table.getRegionLocation(row, false);
128 byte[] regionName = location.getRegionInfo().getRegionName();
129
130 if (columns == null) {
131 Get get = new Get(row);
132 get.setTimeRange(Long.MIN_VALUE, timestamp);
133 Result result = ProtobufUtil.get(rs, regionName, get);
134 return ThriftUtilities.rowResultFromHBase(result);
135 }
136 Get get = new Get(row);
137 for(ByteBuffer column : columns) {
138 byte [][] famAndQf = KeyValue.parseColumn(getBytes(column));
139 if (famAndQf.length == 1) {
140 get.addFamily(famAndQf[0]);
141 } else {
142 get.addColumn(famAndQf[0], famAndQf[1]);
143 }
144 }
145 get.setTimeRange(Long.MIN_VALUE, timestamp);
146 Result result = ProtobufUtil.get(rs, regionName, get);
147 return ThriftUtilities.rowResultFromHBase(result);
148 } catch (NotServingRegionException e) {
149 if (!redirect) {
150 LOG.warn(e.getMessage(), e);
151 throw new IOError(e.getMessage());
152 }
153 LOG.debug("ThriftServer redirecting getRowWithColumnsTs");
154 return super.getRowWithColumnsTs(tableName, rowb, columns, timestamp,
155 attributes);
156 } catch (IOException e) {
157 LOG.warn(e.getMessage(), e);
158 throw new IOError(e.getMessage());
159 }
160 }
161 }
162 }