001/*
002 *
003 * Licensed to the Apache Software Foundation (ASF) under one
004 * or more contributor license agreements.  See the NOTICE file
005 * distributed with this work for additional information
006 * regarding copyright ownership.  The ASF licenses this file
007 * to you under the Apache License, Version 2.0 (the
008 * "License"); you may not use this file except in compliance
009 * with the License.  You may obtain a copy of the License at
010 *
011 *     http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 */
019
020package org.apache.hadoop.hbase.client.example;
021
022import java.io.Closeable;
023import java.io.IOException;
024import org.apache.hadoop.conf.Configuration;
025import org.apache.hadoop.hbase.HConstants;
026import org.apache.hadoop.hbase.TableName;
027import org.apache.hadoop.hbase.client.Connection;
028import org.apache.hadoop.hbase.client.ConnectionFactory;
029import org.apache.hadoop.hbase.client.Table;
030import org.apache.hadoop.hbase.client.coprocessor.Batch;
031import org.apache.hadoop.hbase.ipc.CoprocessorRpcUtils.BlockingRpcCallback;
032import org.apache.hadoop.hbase.ipc.ServerRpcController;
033import org.apache.hadoop.hbase.protobuf.generated.RefreshHFilesProtos;
034import org.apache.yetus.audience.InterfaceAudience;
035import org.slf4j.Logger;
036import org.slf4j.LoggerFactory;
037
038/**
039 * This client class is for invoking the refresh HFile function deployed on the
040 * Region Server side via the RefreshHFilesService.
041 */
042@InterfaceAudience.Private
043public class RefreshHFilesClient implements Closeable {
044  private static final Logger LOG = LoggerFactory.getLogger(RefreshHFilesClient.class);
045  private final Connection connection;
046
047  /**
048   * Constructor with Conf object
049   *
050   * @param cfg
051   */
052  public RefreshHFilesClient(Configuration cfg) {
053    try {
054      this.connection = ConnectionFactory.createConnection(cfg);
055    } catch (IOException e) {
056      throw new RuntimeException(e);
057    }
058  }
059
060  @Override
061  public void close() throws IOException {
062    if (this.connection != null && !this.connection.isClosed()) {
063      this.connection.close();
064    }
065  }
066
067  public void refreshHFiles(final TableName tableName) throws Throwable {
068    try (Table table = connection.getTable(tableName)) {
069      refreshHFiles(table);
070    }
071  }
072
073  public void refreshHFiles(final Table table) throws Throwable {
074    final RefreshHFilesProtos.RefreshHFilesRequest request = RefreshHFilesProtos.RefreshHFilesRequest
075                                                               .getDefaultInstance();
076    table.coprocessorService(RefreshHFilesProtos.RefreshHFilesService.class, HConstants.EMPTY_START_ROW,
077                             HConstants.EMPTY_END_ROW,
078                             new Batch.Call<RefreshHFilesProtos.RefreshHFilesService,
079                                             RefreshHFilesProtos.RefreshHFilesResponse>() {
080                               @Override
081                               public RefreshHFilesProtos.RefreshHFilesResponse call(
082                                 RefreshHFilesProtos.RefreshHFilesService refreshHFilesService)
083                                 throws IOException {
084                                 ServerRpcController controller = new ServerRpcController();
085                                 BlockingRpcCallback<RefreshHFilesProtos.RefreshHFilesResponse> rpcCallback =
086                                   new BlockingRpcCallback<>();
087                                 refreshHFilesService.refreshHFiles(controller, request, rpcCallback);
088                                 if (controller.failedOnException()) {
089                                   throw controller.getFailedOn();
090                                 }
091                                 return rpcCallback.get();
092                               }
093                             });
094    LOG.debug("Done refreshing HFiles");
095  }
096}