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.client;
19  
20  import java.io.IOException;
21  import java.security.PrivilegedExceptionAction;
22  import java.util.ArrayList;
23  import java.util.List;
24  import java.util.concurrent.ExecutorService;
25  
26  import org.apache.hadoop.classification.InterfaceAudience;
27  import org.apache.hadoop.conf.Configuration;
28  import org.apache.hadoop.hbase.HRegionLocation;
29  import org.apache.hadoop.hbase.HTableDescriptor;
30  import org.apache.hadoop.hbase.MasterNotRunningException;
31  import org.apache.hadoop.hbase.ServerName;
32  import org.apache.hadoop.hbase.TableName;
33  import org.apache.hadoop.hbase.ZooKeeperConnectionException;
34  import org.apache.hadoop.hbase.client.coprocessor.Batch;
35  import org.apache.hadoop.hbase.client.coprocessor.Batch.Callback;
36  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.AdminService;
37  import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.ClientService;
38  import org.apache.hadoop.hbase.protobuf.generated.MasterAdminProtos.MasterAdminService;
39  import org.apache.hadoop.hbase.protobuf.generated.MasterMonitorProtos.MasterMonitorService;
40  import org.apache.hadoop.security.UserGroupInformation;
41  
42  /**
43   * This is a HConnection wrapper. Whenever a RPC connection is created,
44   * this class makes sure the specific user is used as the ticket. We assume
45   * just these methods will create any RPC connection based on the default
46   * HConnection implementation: getClient, getAdmin, getKeepAliveMasterMonitorService,
47   * and getKeepAliveMasterAdminService.
48   *
49   * This class is put here only because the HConnection interface exposes
50   * packaged private class MasterMonitorKeepAliveConnection and
51   * MasterAddKeepAliveConnection.
52   */
53  @InterfaceAudience.Private
54  @SuppressWarnings("deprecation")
55  public class HConnectionWrapper implements HConnection {
56    private final UserGroupInformation ugi;
57    private final HConnection hconnection;
58  
59    public HConnectionWrapper(final UserGroupInformation ugi,
60        final HConnection hconnection) {
61      this.hconnection = hconnection;
62      this.ugi = ugi;
63    }
64  
65    @Override
66    public HTableInterface getTable(String tableName) throws IOException {
67      return hconnection.getTable(tableName);
68    }
69  
70    @Override
71    public HTableInterface getTable(byte[] tableName) throws IOException {
72      return hconnection.getTable(tableName);
73    }
74  
75    @Override
76    public HTableInterface getTable(TableName tableName) throws IOException {
77      return hconnection.getTable(tableName);
78    }
79  
80    @Override
81    public HTableInterface getTable(String tableName, ExecutorService pool)  throws IOException {
82      return hconnection.getTable(tableName, pool);
83    }
84  
85    @Override
86    public HTableInterface getTable(byte[] tableName, ExecutorService pool)  throws IOException {
87      return hconnection.getTable(tableName, pool);
88    }
89  
90    @Override
91    public HTableInterface getTable(TableName tableName, ExecutorService pool)  throws IOException {
92      return hconnection.getTable(tableName, pool);
93    }
94  
95    @Override
96    public void abort(String why, Throwable e) {
97      hconnection.abort(why, e);
98    }
99  
100   @Override
101   public boolean isAborted() {
102     return hconnection.isAborted();
103   }
104 
105   @Override
106   public void close() throws IOException {
107     hconnection.close();
108   }
109 
110   @Override
111   public Configuration getConfiguration() {
112     return hconnection.getConfiguration();
113   }
114 
115   @Override
116   public boolean isMasterRunning() throws MasterNotRunningException,
117       ZooKeeperConnectionException {
118     return hconnection.isMasterRunning();
119   }
120 
121   @Override
122   public boolean isTableEnabled(TableName tableName) throws IOException {
123     return hconnection.isTableEnabled(tableName);
124   }
125 
126   @Override
127   public boolean isTableEnabled(byte[] tableName) throws IOException {
128     return isTableEnabled(TableName.valueOf(tableName));
129   }
130 
131   @Override
132   public boolean isTableDisabled(TableName tableName) throws IOException {
133     return hconnection.isTableDisabled(tableName);
134   }
135 
136   @Override
137   public boolean isTableDisabled(byte[] tableName) throws IOException {
138     return isTableDisabled(TableName.valueOf(tableName));
139   }
140 
141   @Override
142   public boolean isTableAvailable(TableName tableName) throws IOException {
143     return hconnection.isTableAvailable(tableName);
144   }
145 
146   @Override
147   public boolean isTableAvailable(byte[] tableName) throws IOException {
148     return isTableAvailable(TableName.valueOf(tableName));
149   }
150 
151   @Override
152   public boolean isTableAvailable(TableName tableName, byte[][] splitKeys) throws IOException {
153     return hconnection.isTableAvailable(tableName, splitKeys);
154   }
155 
156   @Override
157   public boolean isTableAvailable(byte[] tableName, byte[][] splitKeys) throws IOException {
158     return isTableAvailable(TableName.valueOf(tableName), splitKeys);
159   }
160 
161   @Override
162   public HTableDescriptor[] listTables() throws IOException {
163     return hconnection.listTables();
164   }
165 
166   @Override
167   public String[] getTableNames() throws IOException {
168     return hconnection.getTableNames();
169   }
170 
171   @Override
172   public TableName[] listTableNames() throws IOException {
173     return hconnection.listTableNames();
174   }
175 
176   @Override
177   public HTableDescriptor getHTableDescriptor(TableName tableName) throws IOException {
178     return hconnection.getHTableDescriptor(tableName);
179   }
180 
181   @Override
182   public HTableDescriptor getHTableDescriptor(byte[] tableName) throws IOException {
183     return getHTableDescriptor(TableName.valueOf(tableName));
184   }
185 
186   @Override
187   public HRegionLocation locateRegion(TableName tableName, byte[] row) throws IOException {
188     return hconnection.locateRegion(tableName, row);
189   }
190 
191   @Override
192   public HRegionLocation locateRegion(byte[] tableName, byte[] row) throws IOException {
193     return locateRegion(TableName.valueOf(tableName), row);
194   }
195 
196   @Override
197   public void clearRegionCache() {
198     hconnection.clearRegionCache();
199   }
200 
201   @Override
202   public void clearRegionCache(TableName tableName) {
203     hconnection.clearRegionCache(tableName);
204   }
205 
206   @Override
207   public void clearRegionCache(byte[] tableName) {
208     clearRegionCache(TableName.valueOf(tableName));
209   }
210 
211   @Override
212   public void deleteCachedRegionLocation(HRegionLocation location) {
213     hconnection.deleteCachedRegionLocation(location);
214   }
215 
216   @Override
217   public HRegionLocation relocateRegion(TableName tableName, byte[] row) throws IOException {
218     return hconnection.relocateRegion(tableName, row);
219   }
220 
221   @Override
222   public HRegionLocation relocateRegion(byte[] tableName, byte[] row) throws IOException {
223     return relocateRegion(TableName.valueOf(tableName), row);
224   }
225 
226   @Override
227   public void updateCachedLocations(TableName tableName,
228                                     byte[] rowkey,
229                                     Object exception,
230                                     HRegionLocation source) {
231     hconnection.updateCachedLocations(tableName, rowkey, exception, source);
232   }
233 
234   @Override
235   public void updateCachedLocations(byte[] tableName,
236                                     byte[] rowkey,
237                                     Object exception,
238                                     HRegionLocation source) {
239     updateCachedLocations(TableName.valueOf(tableName), rowkey, exception, source);
240   }
241 
242   @Override
243   public HRegionLocation locateRegion(byte[] regionName) throws IOException {
244     return hconnection.locateRegion(regionName);
245   }
246 
247   @Override
248   public List<HRegionLocation> locateRegions(TableName tableName) throws IOException {
249     return hconnection.locateRegions(tableName);
250   }
251 
252   @Override
253   public List<HRegionLocation> locateRegions(byte[] tableName) throws IOException {
254     return locateRegions(TableName.valueOf(tableName));
255   }
256 
257   @Override
258   public List<HRegionLocation> locateRegions(TableName tableName,
259                                              boolean useCache,
260                                              boolean offlined) throws IOException {
261     return hconnection.locateRegions(tableName, useCache, offlined);
262   }
263 
264   @Override
265   public List<HRegionLocation> locateRegions(byte[] tableName,
266                                              boolean useCache,
267                                              boolean offlined) throws IOException {
268     return locateRegions(TableName.valueOf(tableName));
269   }
270 
271   @Override
272   public MasterAdminService.BlockingInterface getMasterAdmin() throws IOException {
273     return hconnection.getMasterAdmin();
274   }
275 
276   @Override
277   public MasterMonitorService.BlockingInterface getMasterMonitor()
278       throws IOException {
279     return hconnection.getMasterMonitor();
280   }
281 
282   @Override
283   public AdminService.BlockingInterface getAdmin(
284       ServerName serverName) throws IOException {
285     return hconnection.getAdmin(serverName);
286   }
287 
288   @Override
289   public ClientService.BlockingInterface getClient(
290       final ServerName serverName) throws IOException {
291     try {
292       return ugi.doAs(new PrivilegedExceptionAction<ClientService.BlockingInterface>() {
293         @Override
294         public ClientService.BlockingInterface run() throws IOException {
295           return hconnection.getClient(serverName);
296          }
297        });
298      } catch (InterruptedException e) {
299        Thread.currentThread().interrupt();
300        throw new IOException(e);
301      }
302   }
303 
304   @Override
305   public AdminService.BlockingInterface getAdmin(
306       final ServerName serverName, final boolean getMaster) throws IOException {
307     try {
308      return ugi.doAs(new PrivilegedExceptionAction<AdminService.BlockingInterface>() {
309        @Override
310        public AdminService.BlockingInterface run() throws IOException {
311          return hconnection.getAdmin(serverName, getMaster);
312         }
313       });
314     } catch (InterruptedException e) {
315       Thread.currentThread().interrupt();
316       throw new IOException(e);
317     }
318   }
319 
320   @Override
321   public HRegionLocation getRegionLocation(TableName tableName,
322                                            byte[] row, boolean reload) throws IOException {
323     return hconnection.getRegionLocation(tableName, row, reload);
324   }
325 
326   @Override
327   public HRegionLocation getRegionLocation(byte[] tableName,
328                                            byte[] row, boolean reload) throws IOException {
329     return getRegionLocation(TableName.valueOf(tableName), row, reload);
330   }
331 
332   @Override
333   public void processBatch(List<? extends Row> actions, TableName tableName, ExecutorService pool,
334                            Object[] results) throws IOException, InterruptedException {
335     hconnection.processBatch(actions, tableName, pool, results);
336   }
337 
338   @Override
339   public void processBatch(List<? extends Row> actions, byte[] tableName, ExecutorService pool,
340                            Object[] results) throws IOException, InterruptedException {
341     processBatch(actions, TableName.valueOf(tableName), pool, results);
342   }
343 
344   @Override
345   public <R> void processBatchCallback(List<? extends Row> list, TableName tableName,
346                                        ExecutorService pool,
347                                        Object[] results,
348                                        Callback<R> callback)
349       throws IOException, InterruptedException {
350     hconnection.processBatchCallback(list, tableName, pool, results, callback);
351   }
352 
353   @Override
354   public <R> void processBatchCallback(List<? extends Row> list, byte[] tableName,
355                                        ExecutorService pool,
356                                        Object[] results,
357                                        Callback<R> callback)
358       throws IOException, InterruptedException {
359     processBatchCallback(list, TableName.valueOf(tableName), pool, results, callback);
360   }
361 
362   @Override
363   public void setRegionCachePrefetch(TableName tableName, boolean enable) {
364     hconnection.setRegionCachePrefetch(tableName, enable);
365   }
366 
367   @Override
368   public void setRegionCachePrefetch(byte[] tableName, boolean enable) {
369     setRegionCachePrefetch(TableName.valueOf(tableName), enable);
370   }
371 
372   @Override
373   public boolean getRegionCachePrefetch(TableName tableName) {
374     return hconnection.getRegionCachePrefetch(tableName);
375   }
376 
377   @Override
378   public boolean getRegionCachePrefetch(byte[] tableName) {
379     return getRegionCachePrefetch(TableName.valueOf(tableName));
380   }
381 
382   @Override
383   public int getCurrentNrHRS() throws IOException {
384     return hconnection.getCurrentNrHRS();
385   }
386 
387   @Override
388   public HTableDescriptor[] getHTableDescriptorsByTableName(
389       List<TableName> tableNames) throws IOException {
390     return hconnection.getHTableDescriptorsByTableName(tableNames);
391   }
392 
393   @Override
394   public HTableDescriptor[] getHTableDescriptors(
395       List<String> names) throws IOException {
396     List<TableName> tableNames = new ArrayList<TableName>(names.size());
397     for(String name : names) {
398       tableNames.add(TableName.valueOf(name));
399     }
400     return getHTableDescriptorsByTableName(tableNames);
401   }
402 
403   @Override
404   public boolean isClosed() {
405     return hconnection.isClosed();
406   }
407 
408   @Override
409   public void clearCaches(ServerName sn) {
410     hconnection.clearCaches(sn);
411   }
412 
413   @Override
414   public MasterMonitorKeepAliveConnection getKeepAliveMasterMonitorService()
415       throws MasterNotRunningException {
416     try {
417       return ugi.doAs(new PrivilegedExceptionAction<MasterMonitorKeepAliveConnection>() {
418         @Override
419         public MasterMonitorKeepAliveConnection run() throws MasterNotRunningException {
420           return hconnection.getKeepAliveMasterMonitorService();
421          }
422        });
423      } catch (IOException ie) {
424        throw new MasterNotRunningException(ie);
425      } catch (InterruptedException e) {
426        Thread.currentThread().interrupt();
427        throw new MasterNotRunningException(e);
428      }
429   }
430 
431   @Override
432   public MasterAdminKeepAliveConnection getKeepAliveMasterAdminService()
433       throws MasterNotRunningException {
434     try {
435       return ugi.doAs(new PrivilegedExceptionAction<MasterAdminKeepAliveConnection>() {
436         @Override
437         public MasterAdminKeepAliveConnection run() throws MasterNotRunningException {
438           return hconnection.getKeepAliveMasterAdminService();
439          }
440        });
441     } catch (IOException ie) {
442       throw new MasterNotRunningException(ie);
443     } catch (InterruptedException e) {
444       Thread.currentThread().interrupt();
445       throw new MasterNotRunningException(e);
446     }
447   }
448 
449   @Override
450   public boolean isDeadServer(ServerName serverName) {
451     return hconnection.isDeadServer(serverName);
452   }
453 }