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.master; 019 020import java.io.Serializable; 021import java.util.Comparator; 022 023import org.apache.hadoop.hbase.ServerName; 024import org.apache.hadoop.hbase.client.RegionInfo; 025import org.apache.yetus.audience.InterfaceAudience; 026import org.apache.yetus.audience.InterfaceStability; 027 028/** 029 * Stores the plan for the move of an individual region. 030 * 031 * Contains info for the region being moved, info for the server the region 032 * should be moved from, and info for the server the region should be moved 033 * to. 034 * 035 * The comparable implementation of this class compares only the region 036 * information and not the source/dest server info. 037 */ 038@InterfaceAudience.LimitedPrivate("Coprocessors") 039@InterfaceStability.Evolving 040public class RegionPlan implements Comparable<RegionPlan> { 041 private final RegionInfo hri; 042 private final ServerName source; 043 private ServerName dest; 044 045 public static class RegionPlanComparator implements Comparator<RegionPlan>, Serializable { 046 private static final long serialVersionUID = 4213207330485734853L; 047 048 @Override 049 public int compare(RegionPlan l, RegionPlan r) { 050 return RegionPlan.compareTo(l, r); 051 } 052 } 053 054 /** 055 * Instantiate a plan for a region move, moving the specified region from 056 * the specified source server to the specified destination server. 057 * 058 * Destination server can be instantiated as null and later set 059 * with {@link #setDestination(ServerName)}. 060 * 061 * @param hri region to be moved 062 * @param source regionserver region should be moved from 063 * @param dest regionserver region should be moved to 064 */ 065 public RegionPlan(final RegionInfo hri, ServerName source, ServerName dest) { 066 this.hri = hri; 067 this.source = source; 068 this.dest = dest; 069 } 070 071 /** 072 * Set the destination server for the plan for this region. 073 */ 074 public void setDestination(ServerName dest) { 075 this.dest = dest; 076 } 077 078 /** 079 * Get the source server for the plan for this region. 080 * @return server info for source 081 */ 082 public ServerName getSource() { 083 return source; 084 } 085 086 /** 087 * Get the destination server for the plan for this region. 088 * @return server info for destination 089 */ 090 public ServerName getDestination() { 091 return dest; 092 } 093 094 /** 095 * Get the encoded region name for the region this plan is for. 096 * @return Encoded region name 097 */ 098 public String getRegionName() { 099 return this.hri.getEncodedName(); 100 } 101 102 public RegionInfo getRegionInfo() { 103 return this.hri; 104 } 105 106 /** 107 * Compare the region info. 108 * @param other region plan you are comparing against 109 */ 110 @Override 111 public int compareTo(RegionPlan other) { 112 return compareTo(this, other); 113 } 114 115 private static int compareTo(RegionPlan left, RegionPlan right) { 116 int result = compareServerName(left.source, right.source); 117 if (result != 0) { 118 return result; 119 } 120 if (left.hri == null) { 121 if (right.hri != null) { 122 return -1; 123 } 124 } else if (right.hri == null) { 125 return +1; 126 } else { 127 result = RegionInfo.COMPARATOR.compare(left.hri, right.hri); 128 } 129 if (result != 0) { 130 return result; 131 } 132 return compareServerName(left.dest, right.dest); 133 } 134 135 private static int compareServerName(ServerName left, ServerName right) { 136 if (left == null) { 137 return right == null? 0: -1; 138 } else if (right == null) { 139 return +1; 140 } 141 return left.compareTo(right); 142 } 143 144 @Override 145 public int hashCode() { 146 final int prime = 31; 147 int result = 1; 148 result = prime * result + ((dest == null) ? 0 : dest.hashCode()); 149 result = prime * result + ((hri == null) ? 0 : hri.hashCode()); 150 result = prime * result + ((source == null) ? 0 : source.hashCode()); 151 return result; 152 } 153 154 @Override 155 public boolean equals(Object obj) { 156 if (this == obj) { 157 return true; 158 } 159 if (obj == null) { 160 return false; 161 } 162 if (getClass() != obj.getClass()) { 163 return false; 164 } 165 RegionPlan other = (RegionPlan) obj; 166 if (dest == null) { 167 if (other.dest != null) { 168 return false; 169 } 170 } else if (!dest.equals(other.dest)) { 171 return false; 172 } 173 if (hri == null) { 174 if (other.hri != null) { 175 return false; 176 } 177 } else if (!hri.equals(other.hri)) { 178 return false; 179 } 180 if (source == null) { 181 if (other.source != null) { 182 return false; 183 } 184 } else if (!source.equals(other.source)) { 185 return false; 186 } 187 return true; 188 } 189 190 @Override 191 public String toString() { 192 return "hri=" + this.hri.getEncodedName() + ", source=" + 193 (this.source == null? "": this.source.toString()) + 194 ", destination=" + (this.dest == null? "": this.dest.toString()); 195 } 196}