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.util.Collection; 22 import java.util.Iterator; 23 24 import org.apache.hadoop.hbase.classification.InterfaceAudience; 25 import org.apache.hadoop.hbase.HRegionInfo; 26 27 /** 28 * Utility methods which contain the logic for regions and replicas. 29 */ 30 @InterfaceAudience.Private 31 public class RegionReplicaUtil { 32 33 /** 34 * The default replicaId for the region 35 */ 36 static final int DEFAULT_REPLICA_ID = 0; 37 38 /** 39 * Returns the HRegionInfo for the given replicaId. HRegionInfo's correspond to 40 * a range of a table, but more than one "instance" of the same range can be 41 * deployed which are differentiated by the replicaId. 42 * @param replicaId the replicaId to use 43 * @return an HRegionInfo object corresponding to the same range (table, start and 44 * end key), but for the given replicaId. 45 */ 46 public static HRegionInfo getRegionInfoForReplica(HRegionInfo regionInfo, int replicaId) { 47 if (regionInfo.getReplicaId() == replicaId) { 48 return regionInfo; 49 } 50 HRegionInfo replicaInfo = new HRegionInfo(regionInfo.getTable(), regionInfo.getStartKey(), 51 regionInfo.getEndKey(), regionInfo.isSplit(), regionInfo.getRegionId(), replicaId); 52 53 replicaInfo.setOffline(regionInfo.isOffline()); 54 return replicaInfo; 55 } 56 57 /** 58 * Returns the HRegionInfo for the default replicaId (0). HRegionInfo's correspond to 59 * a range of a table, but more than one "instance" of the same range can be 60 * deployed which are differentiated by the replicaId. 61 * @return an HRegionInfo object corresponding to the same range (table, start and 62 * end key), but for the default replicaId. 63 */ 64 public static HRegionInfo getRegionInfoForDefaultReplica(HRegionInfo regionInfo) { 65 return getRegionInfoForReplica(regionInfo, DEFAULT_REPLICA_ID); 66 } 67 68 /** @return true if this replicaId corresponds to default replica for the region */ 69 public static boolean isDefaultReplica(int replicaId) { 70 return DEFAULT_REPLICA_ID == replicaId; 71 } 72 73 /** @return true if this region is a default replica for the region */ 74 public static boolean isDefaultReplica(HRegionInfo hri) { 75 return hri.getReplicaId() == DEFAULT_REPLICA_ID; 76 } 77 78 /** 79 * Removes the non-default replicas from the passed regions collection 80 * @param regions 81 */ 82 public static void removeNonDefaultRegions(Collection<HRegionInfo> regions) { 83 Iterator<HRegionInfo> iterator = regions.iterator(); 84 while (iterator.hasNext()) { 85 HRegionInfo hri = iterator.next(); 86 if (!RegionReplicaUtil.isDefaultReplica(hri)) { 87 iterator.remove(); 88 } 89 } 90 } 91 }