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  
47      private static final long serialVersionUID = 4213207330485734853L;
48  
49      @Override
50      public int compare(RegionPlan l, RegionPlan r) {
51        long diff = r.getRegionInfo().getRegionId() - l.getRegionInfo().getRegionId();
52        if (diff < 0) return -1;
53        if (diff > 0) return 1;
54        return 0;
55      }
56    }
57  
58    /**
59     * Instantiate a plan for a region move, moving the specified region from
60     * the specified source server to the specified destination server.
61     *
62     * Destination server can be instantiated as null and later set
63     * with {@link #setDestination(ServerName)}.
64     *
65     * @param hri region to be moved
66     * @param source regionserver region should be moved from
67     * @param dest regionserver region should be moved to
68     */
69    public RegionPlan(final HRegionInfo hri, ServerName source, ServerName dest) {
70      this.hri = hri;
71      this.source = source;
72      this.dest = dest;
73    }
74  
75    /**
76     * Set the destination server for the plan for this region.
77     */
78    public void setDestination(ServerName dest) {
79      this.dest = dest;
80    }
81  
82    /**
83     * Get the source server for the plan for this region.
84     * @return server info for source
85     */
86    public ServerName getSource() {
87      return source;
88    }
89  
90    /**
91     * Get the destination server for the plan for this region.
92     * @return server info for destination
93     */
94    public ServerName getDestination() {
95      return dest;
96    }
97  
98    /**
99     * Get the encoded region name for the region this plan is for.
100    * @return Encoded region name
101    */
102   public String getRegionName() {
103     return this.hri.getEncodedName();
104   }
105 
106   public HRegionInfo getRegionInfo() {
107     return this.hri;
108   }
109 
110   /**
111    * Compare the region info.
112    * @param o region plan you are comparing against
113    */
114   @Override
115   public int compareTo(RegionPlan o) {
116     return getRegionName().compareTo(o.getRegionName());
117   }
118 
119   @Override
120   public int hashCode() {
121     return getRegionName().hashCode();
122   }
123 
124   @Override
125   public boolean equals(Object obj) {
126     if (this == obj) {
127       return true;
128     }
129     if (obj == null || getClass() != obj.getClass()) {
130       return false;
131     }
132     RegionPlan other = (RegionPlan) obj;
133     return compareTo(other) == 0;
134   }
135 
136   @Override
137   public String toString() {
138     return "hri=" + this.hri.getRegionNameAsString() + ", src=" +
139       (this.source == null? "": this.source.toString()) +
140       ", dest=" + (this.dest == null? "": this.dest.toString());
141   }
142 }