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;
022
023import org.apache.hadoop.hbase.ServerName;
024import org.apache.hadoop.hbase.exceptions.UnexpectedStateException;
025import org.apache.hadoop.hbase.master.procedure.MasterProcedureEnv;
026import org.apache.hadoop.hbase.procedure2.ProcedureMetrics;
027import org.apache.hadoop.hbase.procedure2.ProcedureStateSerializer;
028import org.apache.hadoop.hbase.procedure2.ProcedureSuspendedException;
029import org.apache.hadoop.hbase.procedure2.RemoteProcedureDispatcher.RemoteOperation;
030import org.apache.yetus.audience.InterfaceAudience;
031
032import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil;
033import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProcedureProtos.AssignRegionStateData;
034import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProcedureProtos.RegionTransitionState;
035import org.apache.hadoop.hbase.shaded.protobuf.generated.RegionServerStatusProtos.RegionStateTransition.TransitionCode;
036
037/**
038 * Leave here only for checking if we can successfully start the master.
039 * @deprecated Do not use any more.
040 * @see TransitRegionStateProcedure
041 */
042@Deprecated
043@InterfaceAudience.Private
044public class AssignProcedure extends RegionTransitionProcedure {
045
046  private boolean forceNewPlan = false;
047
048  protected volatile ServerName targetServer;
049
050  public AssignProcedure() {
051  }
052
053  @Override
054  public TableOperationType getTableOperationType() {
055    return TableOperationType.REGION_ASSIGN;
056  }
057
058  @Override
059  protected boolean isRollbackSupported(final RegionTransitionState state) {
060    switch (state) {
061      case REGION_TRANSITION_QUEUE:
062        return true;
063      default:
064        return false;
065    }
066  }
067
068  @Override
069  protected void serializeStateData(ProcedureStateSerializer serializer) throws IOException {
070    final AssignRegionStateData.Builder state =
071      AssignRegionStateData.newBuilder().setTransitionState(getTransitionState())
072        .setRegionInfo(ProtobufUtil.toRegionInfo(getRegionInfo()));
073    if (forceNewPlan) {
074      state.setForceNewPlan(true);
075    }
076    if (this.targetServer != null) {
077      state.setTargetServer(ProtobufUtil.toServerName(this.targetServer));
078    }
079    if (getAttempt() > 0) {
080      state.setAttempt(getAttempt());
081    }
082    serializer.serialize(state.build());
083  }
084
085  @Override
086  protected void deserializeStateData(ProcedureStateSerializer serializer) throws IOException {
087    final AssignRegionStateData state = serializer.deserialize(AssignRegionStateData.class);
088    setTransitionState(state.getTransitionState());
089    setRegionInfo(ProtobufUtil.toRegionInfo(state.getRegionInfo()));
090    forceNewPlan = state.getForceNewPlan();
091    if (state.hasTargetServer()) {
092      this.targetServer = ProtobufUtil.toServerName(state.getTargetServer());
093    }
094    if (state.hasAttempt()) {
095      setAttempt(state.getAttempt());
096    }
097  }
098
099  @Override
100  protected boolean startTransition(final MasterProcedureEnv env, final RegionStateNode regionNode)
101      throws IOException {
102    return true;
103  }
104
105  @Override
106  protected boolean updateTransition(final MasterProcedureEnv env, final RegionStateNode regionNode)
107      throws IOException, ProcedureSuspendedException {
108    return true;
109  }
110
111  @Override
112  protected void finishTransition(final MasterProcedureEnv env, final RegionStateNode regionNode)
113      throws IOException {
114  }
115
116  @Override
117  protected void reportTransition(final MasterProcedureEnv env, final RegionStateNode regionNode,
118      final TransitionCode code, final long openSeqNum) throws UnexpectedStateException {
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 boolean remoteCallFailed(final MasterProcedureEnv env, final RegionStateNode regionNode,
129      final IOException exception) {
130    return true;
131  }
132
133  @Override
134  public void toStringClassDetails(StringBuilder sb) {
135    super.toStringClassDetails(sb);
136    if (this.targetServer != null) sb.append(", target=").append(this.targetServer);
137  }
138
139  @Override
140  protected ProcedureMetrics getProcedureMetrics(MasterProcedureEnv env) {
141    return env.getAssignmentManager().getAssignmentManagerMetrics().getAssignProcMetrics();
142  }
143}