View Javadoc

1   /*
2    *
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   */
19  
20  package org.apache.hadoop.hbase.client.example;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  import org.apache.hadoop.conf.Configuration;
25  import org.apache.hadoop.hbase.HConstants;
26  import org.apache.hadoop.hbase.TableName;
27  import org.apache.hadoop.hbase.client.Connection;
28  import org.apache.hadoop.hbase.client.ConnectionFactory;
29  import org.apache.hadoop.hbase.client.Table;
30  import org.apache.hadoop.hbase.client.coprocessor.Batch;
31  import org.apache.hadoop.hbase.ipc.BlockingRpcCallback;
32  import org.apache.hadoop.hbase.ipc.ServerRpcController;
33  import org.apache.hadoop.hbase.protobuf.generated.RefreshHFilesProtos;
34  
35  import java.io.Closeable;
36  import java.io.IOException;
37  
38  /**
39   * This client class is for invoking the refresh HFile function deployed on the
40   * Region Server side via the RefreshHFilesService.
41   */
42  public class RefreshHFilesClient implements Closeable {
43    private static final Log LOG = LogFactory.getLog(RefreshHFilesClient.class);
44    private final Connection connection;
45  
46    /**
47     * Constructor with Conf object
48     *
49     * @param cfg
50     */
51    public RefreshHFilesClient(Configuration cfg) {
52      try {
53        this.connection = ConnectionFactory.createConnection(cfg);
54      } catch (IOException e) {
55        throw new RuntimeException(e);
56      }
57    }
58  
59    @Override
60    public void close() throws IOException {
61      if (this.connection != null && !this.connection.isClosed()) {
62        this.connection.close();
63      }
64    }
65  
66    public void refreshHFiles(final TableName tableName) throws Throwable {
67      try (Table table = connection.getTable(tableName)) {
68        refreshHFiles(table);
69      }
70    }
71  
72    public void refreshHFiles(final Table table) throws Throwable {
73      final RefreshHFilesProtos.RefreshHFilesRequest request = RefreshHFilesProtos.RefreshHFilesRequest
74                                                                 .getDefaultInstance();
75      table.coprocessorService(RefreshHFilesProtos.RefreshHFilesService.class, HConstants.EMPTY_START_ROW,
76                               HConstants.EMPTY_END_ROW,
77                               new Batch.Call<RefreshHFilesProtos.RefreshHFilesService,
78                                               RefreshHFilesProtos.RefreshHFilesResponse>() {
79                                 @Override
80                                 public RefreshHFilesProtos.RefreshHFilesResponse call(
81                                   RefreshHFilesProtos.RefreshHFilesService refreshHFilesService)
82                                   throws IOException {
83                                   ServerRpcController controller = new ServerRpcController();
84                                   BlockingRpcCallback<RefreshHFilesProtos.RefreshHFilesResponse> rpcCallback =
85                                     new BlockingRpcCallback<>();
86                                   refreshHFilesService.refreshHFiles(controller, request, rpcCallback);
87                                   if (controller.failedOnException()) {
88                                     throw controller.getFailedOn();
89                                   }
90                                   return rpcCallback.get();
91                                 }
92                               });
93      LOG.debug("Done refreshing HFiles");
94    }
95  }