View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   * http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  package org.apache.hadoop.hbase.coprocessor.example;
20  
21  import com.google.protobuf.RpcCallback;
22  import com.google.protobuf.RpcController;
23  import com.google.protobuf.Service;
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.hadoop.hbase.Coprocessor;
27  import org.apache.hadoop.hbase.CoprocessorEnvironment;
28  import org.apache.hadoop.hbase.coprocessor.CoprocessorException;
29  import org.apache.hadoop.hbase.coprocessor.CoprocessorService;
30  import org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment;
31  import org.apache.hadoop.hbase.protobuf.ResponseConverter;
32  import org.apache.hadoop.hbase.protobuf.generated.RefreshHFilesProtos;
33  import org.apache.hadoop.hbase.regionserver.Store;
34  
35  import java.io.IOException;
36  
37  /**
38   * Coprocessor endpoint to refresh HFiles on replica.
39   * <p>
40   * <p>
41   * For the protocol buffer definition of the RefreshHFilesService, see the source file located under
42   * hbase-protocol/src/main/protobuf/RefreshHFiles.proto.
43   * </p>
44   */
45  public class RefreshHFilesEndpoint extends RefreshHFilesProtos.RefreshHFilesService
46    implements Coprocessor, CoprocessorService {
47    protected static final Log LOG = LogFactory.getLog(RefreshHFilesEndpoint.class);
48    private RegionCoprocessorEnvironment env;
49  
50    public RefreshHFilesEndpoint() {
51    }
52  
53    @Override
54    public Service getService() {
55      return this;
56    }
57  
58    @Override
59    public void refreshHFiles(RpcController controller, RefreshHFilesProtos.RefreshHFilesRequest request,
60                              RpcCallback<RefreshHFilesProtos.RefreshHFilesResponse> done) {
61      try {
62        for (Store store : env.getRegion().getStores()) {
63          LOG.debug("Refreshing HFiles for region: " + store.getRegionInfo().getRegionNameAsString() +
64                      " and store: " + store.getColumnFamilyName() + "class:" + store.getClass());
65          store.refreshStoreFiles();
66        }
67      } catch (IOException ioe) {
68        LOG.error("Exception while trying to refresh store files: ", ioe);
69        ResponseConverter.setControllerException(controller, ioe);
70      }
71      done.run(RefreshHFilesProtos.RefreshHFilesResponse.getDefaultInstance());
72    }
73  
74    @Override
75    public void start(CoprocessorEnvironment env) throws IOException {
76      if (env instanceof RegionCoprocessorEnvironment) {
77        this.env = (RegionCoprocessorEnvironment) env;
78      } else {
79        throw new CoprocessorException("Must be loaded on a table region!");
80      }
81    }
82  
83    @Override
84    public void stop(CoprocessorEnvironment env) throws IOException {
85    }
86  }