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  package org.apache.hadoop.hbase.master;
19  
20  import java.io.Serializable;
21  import java.util.Comparator;
22  
23  import org.apache.hadoop.hbase.classification.InterfaceAudience;
24  import org.apache.hadoop.hbase.classification.InterfaceStability;
25  import org.apache.hadoop.hbase.HRegionInfo;
26  import org.apache.hadoop.hbase.ServerName;
27  
28  /**
29   * Stores the plan for the move of an individual region.
30   *
31   * Contains info for the region being moved, info for the server the region
32   * should be moved from, and info for the server the region should be moved
33   * to.
34   *
35   * The comparable implementation of this class compares only the region
36   * information and not the source/dest server info.
37   */
38  @InterfaceAudience.LimitedPrivate("Coprocessors")
39  @InterfaceStability.Evolving
40  public class RegionPlan implements Comparable<RegionPlan> {
41    private final HRegionInfo hri;
42    private final ServerName source;
43    private ServerName dest;
44  
45    public static class RegionPlanComparator implements Comparator<RegionPlan>, Serializable {
46      private static final long serialVersionUID = 4213207330485734853L;
47  
48      @Override
49      public int compare(RegionPlan l, RegionPlan r) {
50        return RegionPlan.compareTo(l, r);
51      }
52    }
53  
54    /**
55     * Instantiate a plan for a region move, moving the specified region from
56     * the specified source server to the specified destination server.
57     *
58     * Destination server can be instantiated as null and later set
59     * with {@link #setDestination(ServerName)}.
60     *
61     * @param hri region to be moved
62     * @param source regionserver region should be moved from
63     * @param dest regionserver region should be moved to
64     */
65    public RegionPlan(final HRegionInfo hri, ServerName source, ServerName dest) {
66      this.hri = hri;
67      this.source = source;
68      this.dest = dest;
69    }
70  
71    /**
72     * Set the destination server for the plan for this region.
73     */
74    public void setDestination(ServerName dest) {
75      this.dest = dest;
76    }
77  
78    /**
79     * Get the source server for the plan for this region.
80     * @return server info for source
81     */
82    public ServerName getSource() {
83      return source;
84    }
85  
86    /**
87     * Get the destination server for the plan for this region.
88     * @return server info for destination
89     */
90    public ServerName getDestination() {
91      return dest;
92    }
93  
94    /**
95     * Get the encoded region name for the region this plan is for.
96     * @return Encoded region name
97     */
98    public String getRegionName() {
99      return this.hri.getEncodedName();
100   }
101 
102   public HRegionInfo 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 = left.hri.compareTo(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.getRegionNameAsString() + ", src=" +
193       (this.source == null? "": this.source.toString()) +
194       ", dest=" + (this.dest == null? "": this.dest.toString());
195   }
196 }