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.replication;
019
020import java.io.IOException;
021import java.util.function.LongConsumer;
022import org.apache.hadoop.conf.Configuration;
023import org.apache.hadoop.hbase.master.procedure.MasterProcedureEnv;
024import org.apache.hadoop.hbase.master.procedure.PeerProcedureInterface;
025import org.apache.hadoop.hbase.procedure2.ProcedureStateSerializer;
026import org.apache.hadoop.hbase.procedure2.ProcedureSuspendedException;
027import org.apache.hadoop.hbase.procedure2.ProcedureUtil;
028import org.apache.hadoop.hbase.procedure2.StateMachineProcedure;
029import org.apache.hadoop.hbase.util.RetryCounter;
030import org.apache.yetus.audience.InterfaceAudience;
031
032import org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProcedureProtos.PeerProcedureStateData;
033import org.apache.hadoop.hbase.shaded.protobuf.generated.ProcedureProtos;
034
035/**
036 * Base class for replication peer related procedures which do not need to hold locks(for most of
037 * the sub procedures).
038 */
039@InterfaceAudience.Private
040public abstract class AbstractPeerNoLockProcedure<TState>
041  extends StateMachineProcedure<MasterProcedureEnv, TState> implements PeerProcedureInterface {
042
043  protected String peerId;
044
045  private RetryCounter retryCounter;
046
047  protected AbstractPeerNoLockProcedure() {
048  }
049
050  protected AbstractPeerNoLockProcedure(String peerId) {
051    this.peerId = peerId;
052  }
053
054  @Override
055  public String getPeerId() {
056    return peerId;
057  }
058
059  @Override
060  protected boolean waitInitialized(MasterProcedureEnv env) {
061    return env.waitInitialized(this);
062  }
063
064  @Override
065  protected void rollbackState(MasterProcedureEnv env, TState state)
066    throws IOException, InterruptedException {
067    if (state == getInitialState()) {
068      // actually the peer related operations has no rollback, but if we haven't done any
069      // modifications on the peer storage yet, we can just return.
070      return;
071    }
072    throw new UnsupportedOperationException();
073  }
074
075  @Override
076  protected void serializeStateData(ProcedureStateSerializer serializer) throws IOException {
077    super.serializeStateData(serializer);
078    serializer.serialize(PeerProcedureStateData.newBuilder().setPeerId(peerId).build());
079  }
080
081  @Override
082  protected void deserializeStateData(ProcedureStateSerializer serializer) throws IOException {
083    super.deserializeStateData(serializer);
084    peerId = serializer.deserialize(PeerProcedureStateData.class).getPeerId();
085  }
086
087  @Override
088  protected synchronized boolean setTimeoutFailure(MasterProcedureEnv env) {
089    setState(ProcedureProtos.ProcedureState.RUNNABLE);
090    env.getProcedureScheduler().addFront(this);
091    return false;
092  }
093
094  protected final ProcedureSuspendedException suspend(Configuration conf,
095    LongConsumer backoffConsumer) throws ProcedureSuspendedException {
096    if (retryCounter == null) {
097      retryCounter = ProcedureUtil.createRetryCounter(conf);
098    }
099    long backoff = retryCounter.getBackoffTimeAndIncrementAttempts();
100    backoffConsumer.accept(backoff);
101    setTimeout(Math.toIntExact(backoff));
102    setState(ProcedureProtos.ProcedureState.WAITING_TIMEOUT);
103    skipPersistence();
104    throw new ProcedureSuspendedException();
105  }
106
107  protected final void resetRetry() {
108    retryCounter = null;
109  }
110}