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 org.apache.hadoop.hbase.ServerName;
023import org.apache.hadoop.hbase.exceptions.UnexpectedStateException;
024import org.apache.hadoop.hbase.master.procedure.MasterProcedureEnv;
025import org.apache.hadoop.hbase.procedure2.ProcedureMetrics;
026import org.apache.hadoop.hbase.procedure2.ProcedureStateSerializer;
027import org.apache.hadoop.hbase.procedure2.RemoteProcedureDispatcher.RemoteOperation;
028import org.apache.yetus.audience.InterfaceAudience;
029
030import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
031import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProcedureProtos.RegionTransitionState;
032import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProcedureProtos.UnassignRegionStateData;
033import org.apache.hadoop.hbase.shaded.protobuf.generated.RegionServerStatusProtos.RegionStateTransition.TransitionCode;
034
035/**
036 * Leave here only for checking if we can successfully start the master.
037 * @deprecated Do not use any more.
038 * @see TransitRegionStateProcedure
039 */
040@Deprecated
041@InterfaceAudience.Private
042public class UnassignProcedure extends RegionTransitionProcedure {
043
044  protected volatile ServerName hostingServer;
045
046  protected volatile ServerName destinationServer;
047
048  private boolean removeAfterUnassigning;
049
050  public UnassignProcedure() {
051  }
052
053  @Override
054  public TableOperationType getTableOperationType() {
055    return TableOperationType.REGION_UNASSIGN;
056  }
057
058  @Override
059  protected boolean isRollbackSupported(final RegionTransitionState state) {
060    switch (state) {
061      case REGION_TRANSITION_QUEUE:
062      case REGION_TRANSITION_DISPATCH:
063        return true;
064      default:
065        return false;
066    }
067  }
068
069  @Override
070  protected void serializeStateData(ProcedureStateSerializer serializer) throws IOException {
071    UnassignRegionStateData.Builder state =
072      UnassignRegionStateData.newBuilder().setTransitionState(getTransitionState())
073        .setHostingServer(ProtobufUtil.toServerName(this.hostingServer))
074        .setRegionInfo(ProtobufUtil.toRegionInfo(getRegionInfo()));
075    if (this.destinationServer != null) {
076      state.setDestinationServer(ProtobufUtil.toServerName(destinationServer));
077    }
078    if (removeAfterUnassigning) {
079      state.setRemoveAfterUnassigning(true);
080    }
081    if (getAttempt() > 0) {
082      state.setAttempt(getAttempt());
083    }
084    serializer.serialize(state.build());
085  }
086
087  @Override
088  protected void deserializeStateData(ProcedureStateSerializer serializer) throws IOException {
089    final UnassignRegionStateData state = serializer.deserialize(UnassignRegionStateData.class);
090    setTransitionState(state.getTransitionState());
091    setRegionInfo(ProtobufUtil.toRegionInfo(state.getRegionInfo()));
092    this.hostingServer = ProtobufUtil.toServerName(state.getHostingServer());
093    if (state.hasDestinationServer()) {
094      this.destinationServer = ProtobufUtil.toServerName(state.getDestinationServer());
095    }
096    removeAfterUnassigning = state.getRemoveAfterUnassigning();
097    if (state.hasAttempt()) {
098      setAttempt(state.getAttempt());
099    }
100  }
101
102  @Override
103  protected boolean startTransition(final MasterProcedureEnv env,
104    final RegionStateNode regionNode) {
105    // nothing to do here. we skip the step in the constructor
106    // by jumping to REGION_TRANSITION_DISPATCH
107    throw new UnsupportedOperationException();
108  }
109
110  @Override
111  protected boolean updateTransition(final MasterProcedureEnv env, final RegionStateNode regionNode)
112    throws IOException {
113    return true;
114  }
115
116  @Override
117  protected void finishTransition(final MasterProcedureEnv env, final RegionStateNode regionNode)
118    throws IOException {
119  }
120
121  @Override
122  public Optional<RemoteOperation> remoteCallBuild(final MasterProcedureEnv env,
123    final ServerName serverName) {
124    return Optional.empty();
125  }
126
127  @Override
128  protected void reportTransition(final MasterProcedureEnv env, final RegionStateNode regionNode,
129    final TransitionCode code, final long seqId) throws UnexpectedStateException {
130  }
131
132  /**
133   * @return If true, we will re-wake up this procedure; if false, the procedure stays suspended.
134   */
135  @Override
136  protected boolean remoteCallFailed(final MasterProcedureEnv env, final RegionStateNode regionNode,
137    final IOException exception) {
138    return true;
139  }
140
141  @Override
142  public void toStringClassDetails(StringBuilder sb) {
143    super.toStringClassDetails(sb);
144    sb.append(", server=").append(this.hostingServer);
145  }
146
147  @Override
148  protected ProcedureMetrics getProcedureMetrics(MasterProcedureEnv env) {
149    return env.getAssignmentManager().getAssignmentManagerMetrics().getUnassignProcMetrics();
150  }
151}