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.assignment;
019
020import java.io.IOException;
021import java.util.Optional;
022import java.util.concurrent.atomic.AtomicBoolean;
023import org.apache.hadoop.hbase.ServerName;
024import org.apache.hadoop.hbase.TableName;
025import org.apache.hadoop.hbase.client.RegionInfo;
026import org.apache.hadoop.hbase.exceptions.UnexpectedStateException;
027import org.apache.hadoop.hbase.master.procedure.MasterProcedureEnv;
028import org.apache.hadoop.hbase.master.procedure.TableProcedureInterface;
029import org.apache.hadoop.hbase.procedure2.Procedure;
030import org.apache.hadoop.hbase.procedure2.ProcedureSuspendedException;
031import org.apache.hadoop.hbase.procedure2.RemoteProcedureDispatcher.RemoteOperation;
032import org.apache.hadoop.hbase.procedure2.RemoteProcedureDispatcher.RemoteProcedure;
033import org.apache.hadoop.hbase.procedure2.RemoteProcedureException;
034import org.apache.yetus.audience.InterfaceAudience;
035
036import org.apache.hbase.thirdparty.com.google.common.annotations.VisibleForTesting;
037
038import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProcedureProtos.RegionTransitionState;
039import org.apache.hadoop.hbase.shaded.protobuf.generated.RegionServerStatusProtos.RegionStateTransition.TransitionCode;
040
041/**
042 * Leave here only for checking if we can successfully start the master.
043 * @deprecated Do not use any more.
044 * @see TransitRegionStateProcedure
045 */
046@Deprecated
047@InterfaceAudience.Private
048public abstract class RegionTransitionProcedure extends Procedure<MasterProcedureEnv>
049    implements TableProcedureInterface, RemoteProcedure<MasterProcedureEnv, ServerName> {
050
051  protected final AtomicBoolean aborted = new AtomicBoolean(false);
052
053  private RegionTransitionState transitionState = RegionTransitionState.REGION_TRANSITION_QUEUE;
054
055  private RegionInfo regionInfo;
056
057  private int attempt;
058
059  // Required by the Procedure framework to create the procedure on replay
060  public RegionTransitionProcedure() {
061  }
062
063  public RegionTransitionProcedure(final RegionInfo regionInfo) {
064    this.regionInfo = regionInfo;
065  }
066
067  @VisibleForTesting
068  public RegionInfo getRegionInfo() {
069    return regionInfo;
070  }
071
072  protected void setRegionInfo(final RegionInfo regionInfo) {
073    this.regionInfo = regionInfo;
074  }
075
076  protected void setAttempt(int attempt) {
077    this.attempt = attempt;
078  }
079
080  protected int getAttempt() {
081    return this.attempt;
082  }
083
084  @Override
085  public TableName getTableName() {
086    RegionInfo hri = getRegionInfo();
087    return hri != null ? hri.getTable() : null;
088  }
089
090  public boolean isMeta() {
091    return TableName.isMetaTableName(getTableName());
092  }
093
094  @Override
095  public void toStringClassDetails(final StringBuilder sb) {
096    sb.append(getClass().getSimpleName());
097    sb.append(" table=");
098    sb.append(getTableName());
099    sb.append(", region=");
100    sb.append(getRegionInfo() == null ? null : getRegionInfo().getEncodedName());
101  }
102
103  public RegionStateNode getRegionState(final MasterProcedureEnv env) {
104    return env.getAssignmentManager().getRegionStates().getOrCreateRegionStateNode(getRegionInfo());
105  }
106
107  void setTransitionState(final RegionTransitionState state) {
108    this.transitionState = state;
109  }
110
111  RegionTransitionState getTransitionState() {
112    return transitionState;
113  }
114
115  protected abstract boolean startTransition(MasterProcedureEnv env, RegionStateNode regionNode)
116      throws IOException, ProcedureSuspendedException;
117
118  protected abstract boolean updateTransition(MasterProcedureEnv env, RegionStateNode regionNode)
119      throws IOException, ProcedureSuspendedException;
120
121  protected abstract void finishTransition(MasterProcedureEnv env, RegionStateNode regionNode)
122      throws IOException, ProcedureSuspendedException;
123
124  protected abstract void reportTransition(MasterProcedureEnv env, RegionStateNode regionNode,
125      TransitionCode code, long seqId) throws UnexpectedStateException;
126
127  @Override
128  public abstract Optional<RemoteOperation> remoteCallBuild(MasterProcedureEnv env,
129      ServerName serverName);
130
131  protected abstract boolean remoteCallFailed(MasterProcedureEnv env, RegionStateNode regionNode,
132      IOException exception);
133
134  @Override
135  public synchronized void remoteCallFailed(final MasterProcedureEnv env,
136      final ServerName serverName, final IOException exception) {
137  }
138
139  @Override
140  protected void toStringState(StringBuilder builder) {
141    super.toStringState(builder);
142    RegionTransitionState ts = this.transitionState;
143    if (!isFinished() && ts != null) {
144      builder.append(":").append(ts);
145    }
146  }
147
148  @Override
149  protected Procedure[] execute(final MasterProcedureEnv env) {
150    return null;
151  }
152
153  @Override
154  protected void rollback(MasterProcedureEnv env) {
155  }
156
157  protected abstract boolean isRollbackSupported(final RegionTransitionState state);
158
159  @Override
160  protected boolean abort(final MasterProcedureEnv env) {
161    if (isRollbackSupported(transitionState)) {
162      aborted.set(true);
163      return true;
164    }
165    return false;
166  }
167
168  @Override
169  public void remoteOperationCompleted(MasterProcedureEnv env) {
170    // should not be called for region operation until we modified the open/close region procedure
171    throw new UnsupportedOperationException();
172  }
173
174  @Override
175  public void remoteOperationFailed(MasterProcedureEnv env, RemoteProcedureException error) {
176    // should not be called for region operation until we modified the open/close region procedure
177    throw new UnsupportedOperationException();
178  }
179
180}