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