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 */
018
019package org.apache.hadoop.hbase.coprocessor.example;
020
021import com.google.protobuf.RpcCallback;
022import com.google.protobuf.RpcController;
023import com.google.protobuf.Service;
024import java.io.IOException;
025import java.util.Collections;
026import org.apache.hadoop.hbase.CoprocessorEnvironment;
027import org.apache.hadoop.hbase.coprocessor.CoprocessorException;
028import org.apache.hadoop.hbase.coprocessor.RegionCoprocessor;
029import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
030import org.apache.hadoop.hbase.ipc.CoprocessorRpcUtils;
031import org.apache.hadoop.hbase.protobuf.generated.RefreshHFilesProtos;
032import org.apache.hadoop.hbase.regionserver.Store;
033import org.apache.yetus.audience.InterfaceAudience;
034import org.slf4j.Logger;
035import org.slf4j.LoggerFactory;
036
037/**
038 * Coprocessor endpoint to refresh HFiles on replica.
039 * <p>
040 * <p>
041 * For the protocol buffer definition of the RefreshHFilesService, see the source file located under
042 * hbase-protocol/src/main/protobuf/RefreshHFiles.proto.
043 * </p>
044 */
045@InterfaceAudience.Private
046public class RefreshHFilesEndpoint extends RefreshHFilesProtos.RefreshHFilesService
047  implements RegionCoprocessor {
048  protected static final Logger LOG = LoggerFactory.getLogger(RefreshHFilesEndpoint.class);
049  private RegionCoprocessorEnvironment env;
050
051  public RefreshHFilesEndpoint() {
052  }
053
054  @Override
055  public Iterable<Service> getServices() {
056    return Collections.singleton(this);
057  }
058
059  @Override
060  public void refreshHFiles(RpcController controller, RefreshHFilesProtos.RefreshHFilesRequest request,
061                            RpcCallback<RefreshHFilesProtos.RefreshHFilesResponse> done) {
062    try {
063      for (Store store : env.getRegion().getStores()) {
064        LOG.debug("Refreshing HFiles for region: " + store.getRegionInfo().getRegionNameAsString() +
065                    " and store: " + store.getColumnFamilyName() + "class:" + store.getClass());
066        store.refreshStoreFiles();
067      }
068    } catch (IOException ioe) {
069      LOG.error("Exception while trying to refresh store files: ", ioe);
070      CoprocessorRpcUtils.setControllerException(controller, ioe);
071    }
072    done.run(RefreshHFilesProtos.RefreshHFilesResponse.getDefaultInstance());
073  }
074
075  @Override
076  public void start(CoprocessorEnvironment env) throws IOException {
077    if (env instanceof RegionCoprocessorEnvironment) {
078      this.env = (RegionCoprocessorEnvironment) env;
079    } else {
080      throw new CoprocessorException("Must be loaded on a table region!");
081    }
082  }
083
084  @Override
085  public void stop(CoprocessorEnvironment env) throws IOException {
086  }
087}