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