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 com.google.protobuf.RpcCallback;
021import com.google.protobuf.RpcController;
022import com.google.protobuf.Service;
023import java.io.IOException;
024import java.util.Collections;
025import org.apache.hadoop.hbase.CoprocessorEnvironment;
026import org.apache.hadoop.hbase.coprocessor.CoprocessorException;
027import org.apache.hadoop.hbase.coprocessor.RegionCoprocessor;
028import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
029import org.apache.hadoop.hbase.ipc.CoprocessorRpcUtils;
030import org.apache.hadoop.hbase.protobuf.generated.RefreshHFilesProtos;
031import org.apache.hadoop.hbase.regionserver.Store;
032import org.apache.yetus.audience.InterfaceAudience;
033import org.slf4j.Logger;
034import org.slf4j.LoggerFactory;
035
036/**
037 * Coprocessor endpoint to refresh HFiles on replica.
038 * <p>
039 * <p>
040 * For the protocol buffer definition of the RefreshHFilesService, see the source file located under
041 * hbase-protocol/src/main/protobuf/RefreshHFiles.proto.
042 * </p>
043 */
044@InterfaceAudience.Private
045public class RefreshHFilesEndpoint extends RefreshHFilesProtos.RefreshHFilesService
046  implements RegionCoprocessor {
047  protected static final Logger LOG = LoggerFactory.getLogger(RefreshHFilesEndpoint.class);
048  private RegionCoprocessorEnvironment env;
049
050  public RefreshHFilesEndpoint() {
051  }
052
053  @Override
054  public Iterable<Service> getServices() {
055    return Collections.singleton(this);
056  }
057
058  @Override
059  public void refreshHFiles(RpcController controller,
060    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}