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  @VisibleForTesting
073  public void setRegionInfo(final RegionInfo regionInfo) {
074    this.regionInfo = regionInfo;
075  }
076
077  protected void setAttempt(int attempt) {
078    this.attempt = attempt;
079  }
080
081  protected int getAttempt() {
082    return this.attempt;
083  }
084
085  @Override
086  public TableName getTableName() {
087    RegionInfo hri = getRegionInfo();
088    return hri != null ? hri.getTable() : null;
089  }
090
091  public boolean isMeta() {
092    return TableName.isMetaTableName(getTableName());
093  }
094
095  @Override
096  public void toStringClassDetails(final StringBuilder sb) {
097    sb.append(getClass().getSimpleName());
098    sb.append(" table=");
099    sb.append(getTableName());
100    sb.append(", region=");
101    sb.append(getRegionInfo() == null ? null : getRegionInfo().getEncodedName());
102  }
103
104  public RegionStateNode getRegionState(final MasterProcedureEnv env) {
105    return env.getAssignmentManager().getRegionStates().getOrCreateRegionStateNode(getRegionInfo());
106  }
107
108  void setTransitionState(final RegionTransitionState state) {
109    this.transitionState = state;
110  }
111
112  RegionTransitionState getTransitionState() {
113    return transitionState;
114  }
115
116  protected abstract boolean startTransition(MasterProcedureEnv env, RegionStateNode regionNode)
117      throws IOException, ProcedureSuspendedException;
118
119  protected abstract boolean updateTransition(MasterProcedureEnv env, RegionStateNode regionNode)
120      throws IOException, ProcedureSuspendedException;
121
122  protected abstract void finishTransition(MasterProcedureEnv env, RegionStateNode regionNode)
123      throws IOException, ProcedureSuspendedException;
124
125  protected abstract void reportTransition(MasterProcedureEnv env, RegionStateNode regionNode,
126      TransitionCode code, long seqId) throws UnexpectedStateException;
127
128  @Override
129  public abstract Optional<RemoteOperation> remoteCallBuild(MasterProcedureEnv env,
130      ServerName serverName);
131
132  protected abstract boolean remoteCallFailed(MasterProcedureEnv env, RegionStateNode regionNode,
133      IOException exception);
134
135  @Override
136  public synchronized void remoteCallFailed(final MasterProcedureEnv env,
137      final ServerName serverName, final IOException exception) {
138  }
139
140  @Override
141  protected void toStringState(StringBuilder builder) {
142    super.toStringState(builder);
143    RegionTransitionState ts = this.transitionState;
144    if (!isFinished() && ts != null) {
145      builder.append(":").append(ts);
146    }
147  }
148
149  @Override
150  protected Procedure[] execute(final MasterProcedureEnv env) {
151    return null;
152  }
153
154  @Override
155  protected void rollback(MasterProcedureEnv env) {
156  }
157
158  protected abstract boolean isRollbackSupported(final RegionTransitionState state);
159
160  @Override
161  protected boolean abort(final MasterProcedureEnv env) {
162    if (isRollbackSupported(transitionState)) {
163      aborted.set(true);
164      return true;
165    }
166    return false;
167  }
168
169  @Override
170  public void remoteOperationCompleted(MasterProcedureEnv env) {
171    // should not be called for region operation until we modified the open/close region procedure
172    throw new UnsupportedOperationException();
173  }
174
175  @Override
176  public void remoteOperationFailed(MasterProcedureEnv env, RemoteProcedureException error) {
177    // should not be called for region operation until we modified the open/close region procedure
178    throw new UnsupportedOperationException();
179  }
180
181}