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.hadoop.hbase.shaded.protobuf.generated.MasterProcedureProtos.RegionTransitionState;
037import org.apache.hadoop.hbase.shaded.protobuf.generated.RegionServerStatusProtos.RegionStateTransition.TransitionCode;
038
039/**
040 * Leave here only for checking if we can successfully start the master.
041 * @deprecated Do not use any more.
042 * @see TransitRegionStateProcedure
043 */
044@Deprecated
045@InterfaceAudience.Private
046public abstract class RegionTransitionProcedure extends Procedure<MasterProcedureEnv>
047  implements TableProcedureInterface, RemoteProcedure<MasterProcedureEnv, ServerName> {
048
049  protected final AtomicBoolean aborted = new AtomicBoolean(false);
050
051  private RegionTransitionState transitionState = RegionTransitionState.REGION_TRANSITION_QUEUE;
052
053  private RegionInfo regionInfo;
054
055  private int attempt;
056
057  // Required by the Procedure framework to create the procedure on replay
058  public RegionTransitionProcedure() {
059  }
060
061  public RegionTransitionProcedure(final RegionInfo regionInfo) {
062    this.regionInfo = regionInfo;
063  }
064
065  public RegionInfo getRegionInfo() {
066    return regionInfo;
067  }
068
069  public void setRegionInfo(final RegionInfo regionInfo) {
070    this.regionInfo = regionInfo;
071  }
072
073  protected void setAttempt(int attempt) {
074    this.attempt = attempt;
075  }
076
077  protected int getAttempt() {
078    return this.attempt;
079  }
080
081  @Override
082  public TableName getTableName() {
083    RegionInfo hri = getRegionInfo();
084    return hri != null ? hri.getTable() : null;
085  }
086
087  public boolean isMeta() {
088    return TableName.isMetaTableName(getTableName());
089  }
090
091  @Override
092  public void toStringClassDetails(final StringBuilder sb) {
093    sb.append(getProcName());
094  }
095
096  @Override
097  public String getProcName() {
098    RegionInfo r = getRegionInfo();
099    return getClass().getSimpleName() + " " + getTableName()
100      + (r != null ? r.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}