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.client;
20  
21  import java.io.IOException;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.hadoop.hbase.HRegionInfo;
26  import org.apache.hadoop.hbase.TableName;
27  import org.apache.hadoop.hbase.classification.InterfaceAudience;
28  import org.apache.hadoop.hbase.ipc.PayloadCarryingRpcController;
29  import org.apache.hadoop.hbase.ipc.RpcControllerFactory;
30  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
31  import org.apache.hadoop.hbase.protobuf.RequestConverter;
32  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.FlushRegionRequest;
33  import org.apache.hadoop.hbase.protobuf.generated.AdminProtos.FlushRegionResponse;
34  import org.apache.hadoop.hbase.util.Bytes;
35  import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
36  
37  import com.google.protobuf.ServiceException;
38  
39  /**
40   * A Callable for flushRegion() RPC.
41   */
42  @InterfaceAudience.Private
43  public class FlushRegionCallable extends RegionAdminServiceCallable<FlushRegionResponse> {
44  
45    private static final Log LOG = LogFactory.getLog(FlushRegionCallable.class);
46  
47    private final byte[] regionName;
48    private final boolean writeFlushWalMarker;
49    private boolean reload;
50  
51    public FlushRegionCallable(ClusterConnection connection,
52        RpcControllerFactory rpcControllerFactory, TableName tableName, byte[] regionName,
53        byte[] regionStartKey, boolean writeFlushWalMarker) {
54      super(connection, rpcControllerFactory, tableName, regionStartKey);
55      this.regionName = regionName;
56      this.writeFlushWalMarker = writeFlushWalMarker;
57    }
58  
59    public FlushRegionCallable(ClusterConnection connection,
60        RpcControllerFactory rpcControllerFactory, HRegionInfo regionInfo,
61        boolean writeFlushWalMarker) {
62      this(connection, rpcControllerFactory, regionInfo.getTable(), regionInfo.getRegionName(),
63        regionInfo.getStartKey(), writeFlushWalMarker);
64    }
65  
66    @Override
67    public FlushRegionResponse call(int callTimeout) throws Exception {
68      return flushRegion();
69    }
70  
71    @Override
72    public void prepare(boolean reload) throws IOException {
73      super.prepare(reload);
74      this.reload = reload;
75    }
76  
77    private FlushRegionResponse flushRegion() throws IOException {
78      // check whether we should still do the flush to this region. If the regions are changed due
79      // to splits or merges, etc return success
80      if (!Bytes.equals(location.getRegionInfo().getRegionName(), regionName)) {
81        if (!reload) {
82          throw new IOException("Cached location seems to be different than requested region.");
83        }
84        LOG.info("Skipping flush region, because the located region "
85            + Bytes.toStringBinary(location.getRegionInfo().getRegionName()) + " is different than "
86            + " requested region " + Bytes.toStringBinary(regionName));
87        return FlushRegionResponse.newBuilder()
88            .setLastFlushTime(EnvironmentEdgeManager.currentTime())
89            .setFlushed(false)
90            .setWroteFlushWalMarker(false)
91            .build();
92      }
93  
94      FlushRegionRequest request =
95          RequestConverter.buildFlushRegionRequest(regionName, writeFlushWalMarker);
96  
97      try {
98        PayloadCarryingRpcController controller = rpcControllerFactory.newController();
99        controller.setPriority(tableName);
100       return stub.flushRegion(controller, request);
101     } catch (ServiceException se) {
102       throw ProtobufUtil.getRemoteException(se);
103     }
104   }
105 }