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.util.Date;
021import org.apache.hadoop.hbase.ServerName;
022import org.apache.hadoop.hbase.client.RegionInfo;
023import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
024import org.apache.yetus.audience.InterfaceAudience;
025import org.apache.yetus.audience.InterfaceStability;
026
027import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
028import org.apache.hadoop.hbase.shaded.protobuf.generated.ClusterStatusProtos;
029
030/**
031 * State of a Region while undergoing transitions. This class is immutable.
032 */
033@InterfaceAudience.Private
034public class RegionState {
035
036  @InterfaceAudience.Private
037  @InterfaceStability.Evolving
038  public enum State {
039    OFFLINE, // region is in an offline state
040    OPENING, // server has begun to open but not yet done
041    OPEN, // server opened region and updated meta
042    CLOSING, // server has begun to close but not yet done
043    CLOSED, // server closed region and updated meta
044    SPLITTING, // server started split of a region
045    SPLIT, // server completed split of a region
046    FAILED_OPEN, // failed to open, and won't retry any more
047    FAILED_CLOSE, // failed to close, and won't retry any more
048    MERGING, // server started merge a region
049    MERGED, // server completed merge a region
050    SPLITTING_NEW, // new region to be created when RS splits a parent
051                   // region but hasn't be created yet, or master doesn't
052                   // know it's already created
053    MERGING_NEW, // new region to be created when RS merges two
054                 // daughter regions but hasn't be created yet, or
055                 // master doesn't know it's already created
056    ABNORMALLY_CLOSED; // the region is CLOSED because of a RS crashes. Usually it is the same
057                       // with CLOSED, but for some operations such as merge/split, we can not
058                       // apply it to a region in this state, as it may lead to data loss as we
059                       // may have some data in recovered edits.
060
061    public boolean matches(State... expected) {
062      for (State state : expected) {
063        if (this == state) {
064          return true;
065        }
066      }
067      return false;
068    }
069
070    /**
071     * Convert to protobuf ClusterStatusProtos.RegionState.State
072     */
073    public ClusterStatusProtos.RegionState.State convert() {
074      ClusterStatusProtos.RegionState.State rs;
075      switch (this) {
076        case OFFLINE:
077          rs = ClusterStatusProtos.RegionState.State.OFFLINE;
078          break;
079        case OPENING:
080          rs = ClusterStatusProtos.RegionState.State.OPENING;
081          break;
082        case OPEN:
083          rs = ClusterStatusProtos.RegionState.State.OPEN;
084          break;
085        case CLOSING:
086          rs = ClusterStatusProtos.RegionState.State.CLOSING;
087          break;
088        case CLOSED:
089          rs = ClusterStatusProtos.RegionState.State.CLOSED;
090          break;
091        case SPLITTING:
092          rs = ClusterStatusProtos.RegionState.State.SPLITTING;
093          break;
094        case SPLIT:
095          rs = ClusterStatusProtos.RegionState.State.SPLIT;
096          break;
097        case FAILED_OPEN:
098          rs = ClusterStatusProtos.RegionState.State.FAILED_OPEN;
099          break;
100        case FAILED_CLOSE:
101          rs = ClusterStatusProtos.RegionState.State.FAILED_CLOSE;
102          break;
103        case MERGING:
104          rs = ClusterStatusProtos.RegionState.State.MERGING;
105          break;
106        case MERGED:
107          rs = ClusterStatusProtos.RegionState.State.MERGED;
108          break;
109        case SPLITTING_NEW:
110          rs = ClusterStatusProtos.RegionState.State.SPLITTING_NEW;
111          break;
112        case MERGING_NEW:
113          rs = ClusterStatusProtos.RegionState.State.MERGING_NEW;
114          break;
115        case ABNORMALLY_CLOSED:
116          rs = ClusterStatusProtos.RegionState.State.ABNORMALLY_CLOSED;
117          break;
118        default:
119          throw new IllegalStateException("");
120      }
121      return rs;
122    }
123
124    /**
125     * Convert a protobuf HBaseProtos.RegionState.State to a RegionState.State
126     * @return the RegionState.State
127     */
128    public static State convert(ClusterStatusProtos.RegionState.State protoState) {
129      State state;
130      switch (protoState) {
131        case OFFLINE:
132          state = OFFLINE;
133          break;
134        case PENDING_OPEN:
135        case OPENING:
136          state = OPENING;
137          break;
138        case OPEN:
139          state = OPEN;
140          break;
141        case PENDING_CLOSE:
142        case CLOSING:
143          state = CLOSING;
144          break;
145        case CLOSED:
146          state = CLOSED;
147          break;
148        case SPLITTING:
149          state = SPLITTING;
150          break;
151        case SPLIT:
152          state = SPLIT;
153          break;
154        case FAILED_OPEN:
155          state = FAILED_OPEN;
156          break;
157        case FAILED_CLOSE:
158          state = FAILED_CLOSE;
159          break;
160        case MERGING:
161          state = MERGING;
162          break;
163        case MERGED:
164          state = MERGED;
165          break;
166        case SPLITTING_NEW:
167          state = SPLITTING_NEW;
168          break;
169        case MERGING_NEW:
170          state = MERGING_NEW;
171          break;
172        case ABNORMALLY_CLOSED:
173          state = ABNORMALLY_CLOSED;
174          break;
175        default:
176          throw new IllegalStateException("Unhandled state " + protoState);
177      }
178      return state;
179    }
180  }
181
182  private final long stamp;
183  private final RegionInfo hri;
184  private final ServerName serverName;
185  private final State state;
186
187  public static RegionState createForTesting(RegionInfo region, State state) {
188    return new RegionState(region, state, EnvironmentEdgeManager.currentTime(), null);
189  }
190
191  public RegionState(RegionInfo region, State state, ServerName serverName) {
192    this(region, state, EnvironmentEdgeManager.currentTime(), serverName);
193  }
194
195  public RegionState(RegionInfo region, State state, long stamp, ServerName serverName) {
196    this.hri = region;
197    this.state = state;
198    this.stamp = stamp;
199    this.serverName = serverName;
200  }
201
202  public State getState() {
203    return state;
204  }
205
206  public long getStamp() {
207    return stamp;
208  }
209
210  public RegionInfo getRegion() {
211    return hri;
212  }
213
214  public ServerName getServerName() {
215    return serverName;
216  }
217
218  public boolean isClosing() {
219    return state == State.CLOSING;
220  }
221
222  public boolean isClosed() {
223    return state == State.CLOSED;
224  }
225
226  public boolean isClosedOrAbnormallyClosed() {
227    return isClosed() || this.state == State.ABNORMALLY_CLOSED;
228  }
229
230  public boolean isOpening() {
231    return state == State.OPENING;
232  }
233
234  public boolean isOpened() {
235    return state == State.OPEN;
236  }
237
238  public boolean isOffline() {
239    return state == State.OFFLINE;
240  }
241
242  public boolean isSplitting() {
243    return state == State.SPLITTING;
244  }
245
246  public boolean isSplit() {
247    return state == State.SPLIT;
248  }
249
250  public boolean isSplittingNew() {
251    return state == State.SPLITTING_NEW;
252  }
253
254  public boolean isFailedOpen() {
255    return state == State.FAILED_OPEN;
256  }
257
258  public boolean isFailedClose() {
259    return state == State.FAILED_CLOSE;
260  }
261
262  public boolean isMerging() {
263    return state == State.MERGING;
264  }
265
266  public boolean isMerged() {
267    return state == State.MERGED;
268  }
269
270  public boolean isMergingNew() {
271    return state == State.MERGING_NEW;
272  }
273
274  public boolean isOnServer(final ServerName sn) {
275    return serverName != null && serverName.equals(sn);
276  }
277
278  public boolean isMergingOnServer(final ServerName sn) {
279    return isOnServer(sn) && isMerging();
280  }
281
282  public boolean isMergingNewOnServer(final ServerName sn) {
283    return isOnServer(sn) && isMergingNew();
284  }
285
286  public boolean isMergingNewOrOpenedOnServer(final ServerName sn) {
287    return isOnServer(sn) && (isMergingNew() || isOpened());
288  }
289
290  public boolean isMergingNewOrOfflineOnServer(final ServerName sn) {
291    return isOnServer(sn) && (isMergingNew() || isOffline());
292  }
293
294  public boolean isSplittingOnServer(final ServerName sn) {
295    return isOnServer(sn) && isSplitting();
296  }
297
298  public boolean isSplittingNewOnServer(final ServerName sn) {
299    return isOnServer(sn) && isSplittingNew();
300  }
301
302  public boolean isSplittingOrOpenedOnServer(final ServerName sn) {
303    return isOnServer(sn) && (isSplitting() || isOpened());
304  }
305
306  public boolean isSplittingOrSplitOnServer(final ServerName sn) {
307    return isOnServer(sn) && (isSplitting() || isSplit());
308  }
309
310  public boolean isClosingOrClosedOnServer(final ServerName sn) {
311    return isOnServer(sn) && (isClosing() || isClosed());
312  }
313
314  public boolean isOpeningOrFailedOpenOnServer(final ServerName sn) {
315    return isOnServer(sn) && (isOpening() || isFailedOpen());
316  }
317
318  public boolean isOpeningOrOpenedOnServer(final ServerName sn) {
319    return isOnServer(sn) && (isOpening() || isOpened());
320  }
321
322  public boolean isOpenedOnServer(final ServerName sn) {
323    return isOnServer(sn) && isOpened();
324  }
325
326  /**
327   * Check if a region state can transition to offline
328   */
329  public boolean isReadyToOffline() {
330    return isMerged() || isSplit() || isOffline() || isSplittingNew() || isMergingNew();
331  }
332
333  /**
334   * Check if a region state can transition to online
335   */
336  public boolean isReadyToOnline() {
337    return isOpened() || isSplittingNew() || isMergingNew();
338  }
339
340  /**
341   * Check if a region state is one of offline states that can't transition to pending_close/closing
342   * (unassign/offline)
343   */
344  public boolean isUnassignable() {
345    return isUnassignable(state);
346  }
347
348  /**
349   * Check if a region state is one of offline states that can't transition to pending_close/closing
350   * (unassign/offline)
351   */
352  public static boolean isUnassignable(State state) {
353    return state == State.MERGED || state == State.SPLIT || state == State.OFFLINE
354      || state == State.SPLITTING_NEW || state == State.MERGING_NEW;
355  }
356
357  @Override
358  public String toString() {
359    return "{" + hri.getShortNameToLog() + " state=" + state + ", ts=" + stamp + ", server="
360      + serverName + "}";
361  }
362
363  /**
364   * A slower (but more easy-to-read) stringification
365   */
366  public String toDescriptiveString() {
367    long relTime = EnvironmentEdgeManager.currentTime() - stamp;
368    return hri.getRegionNameAsString() + " state=" + state + ", ts=" + new Date(stamp) + " ("
369      + (relTime / 1000) + "s ago)" + ", server=" + serverName;
370  }
371
372  /**
373   * Convert a RegionState to an HBaseProtos.RegionState
374   * @return the converted HBaseProtos.RegionState
375   */
376  public ClusterStatusProtos.RegionState convert() {
377    ClusterStatusProtos.RegionState.Builder regionState =
378      ClusterStatusProtos.RegionState.newBuilder();
379    regionState.setRegionInfo(ProtobufUtil.toRegionInfo(hri));
380    regionState.setState(state.convert());
381    regionState.setStamp(getStamp());
382    return regionState.build();
383  }
384
385  /**
386   * Convert a protobuf HBaseProtos.RegionState to a RegionState
387   * @return the RegionState
388   */
389  public static RegionState convert(ClusterStatusProtos.RegionState proto) {
390    return new RegionState(ProtobufUtil.toRegionInfo(proto.getRegionInfo()),
391      State.convert(proto.getState()), proto.getStamp(), null);
392  }
393
394  /**
395   * Check if two states are the same, except timestamp
396   */
397  @Override
398  public boolean equals(Object obj) {
399    if (this == obj) {
400      return true;
401    }
402    if (!(obj instanceof RegionState)) {
403      return false;
404    }
405    RegionState tmp = (RegionState) obj;
406    return RegionInfo.COMPARATOR.compare(tmp.hri, hri) == 0 && tmp.state == state
407      && ((serverName != null && serverName.equals(tmp.serverName))
408        || (tmp.serverName == null && serverName == null));
409  }
410
411  /**
412   * Don't count timestamp in hash code calculation
413   */
414  @Override
415  public int hashCode() {
416    return (serverName != null ? serverName.hashCode() * 11 : 0) + hri.hashCode()
417      + 5 * state.ordinal();
418  }
419}