001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.coprocessor.example;
019
020import java.io.IOException;
021import java.util.Collections;
022import org.apache.hadoop.hbase.CoprocessorEnvironment;
023import org.apache.hadoop.hbase.coprocessor.CoprocessorException;
024import org.apache.hadoop.hbase.coprocessor.RegionCoprocessor;
025import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
026import org.apache.hadoop.hbase.ipc.CoprocessorRpcUtils;
027import org.apache.hadoop.hbase.regionserver.Store;
028import org.apache.yetus.audience.InterfaceAudience;
029import org.slf4j.Logger;
030import org.slf4j.LoggerFactory;
031
032import org.apache.hbase.thirdparty.com.google.protobuf.RpcCallback;
033import org.apache.hbase.thirdparty.com.google.protobuf.RpcController;
034import org.apache.hbase.thirdparty.com.google.protobuf.Service;
035
036import org.apache.hadoop.hbase.shaded.protobuf.generated.RefreshHFilesProtos;
037
038/**
039 * Coprocessor endpoint to refresh HFiles on replica.
040 * <p>
041 * <p>
042 * For the protocol buffer definition of the RefreshHFilesService, see the source file located under
043 * hbase-protocol/src/main/protobuf/RefreshHFiles.proto.
044 * </p>
045 */
046@InterfaceAudience.Private
047public class RefreshHFilesEndpoint extends RefreshHFilesProtos.RefreshHFilesService
048  implements RegionCoprocessor {
049  protected static final Logger LOG = LoggerFactory.getLogger(RefreshHFilesEndpoint.class);
050  private RegionCoprocessorEnvironment env;
051
052  public RefreshHFilesEndpoint() {
053  }
054
055  @Override
056  public Iterable<Service> getServices() {
057    return Collections.singleton(this);
058  }
059
060  @Override
061  public void refreshHFiles(RpcController controller,
062    RefreshHFilesProtos.RefreshHFilesRequest request,
063    RpcCallback<RefreshHFilesProtos.RefreshHFilesResponse> done) {
064    try {
065      for (Store store : env.getRegion().getStores()) {
066        LOG.debug("Refreshing HFiles for region: " + store.getRegionInfo().getRegionNameAsString()
067          + " and store: " + store.getColumnFamilyName() + "class:" + store.getClass());
068        store.refreshStoreFiles();
069      }
070    } catch (IOException ioe) {
071      LOG.error("Exception while trying to refresh store files: ", ioe);
072      CoprocessorRpcUtils.setControllerException(controller, ioe);
073    }
074    done.run(RefreshHFilesProtos.RefreshHFilesResponse.getDefaultInstance());
075  }
076
077  @Override
078  public void start(CoprocessorEnvironment env) throws IOException {
079    if (env instanceof RegionCoprocessorEnvironment) {
080      this.env = (RegionCoprocessorEnvironment) env;
081    } else {
082      throw new CoprocessorException("Must be loaded on a table region!");
083    }
084  }
085
086  @Override
087  public void stop(CoprocessorEnvironment env) throws IOException {
088  }
089}