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 */
018
019package org.apache.hadoop.hbase.procedure2;
020
021import java.io.IOException;
022
023import org.apache.yetus.audience.InterfaceAudience;
024import org.apache.yetus.audience.InterfaceStability;
025import org.apache.hadoop.hbase.shaded.protobuf.generated.ProcedureProtos.SequentialProcedureData;
026
027/**
028 * A SequentialProcedure describes one step in a procedure chain:
029 * <pre>
030 *   -&gt; Step 1 -&gt; Step 2 -&gt; Step 3
031 * </pre>
032 * The main difference from a base Procedure is that the execute() of a
033 * SequentialProcedure will be called only once; there will be no second
034 * execute() call once the children are finished. which means once the child
035 * of a SequentialProcedure are completed the SequentialProcedure is completed too.
036 */
037@InterfaceAudience.Private
038@InterfaceStability.Evolving
039public abstract class SequentialProcedure<TEnvironment> extends Procedure<TEnvironment> {
040  private boolean executed = false;
041
042  @Override
043  protected Procedure[] doExecute(final TEnvironment env)
044      throws ProcedureYieldException, ProcedureSuspendedException, InterruptedException {
045    updateTimestamp();
046    try {
047      Procedure[] children = !executed ? execute(env) : null;
048      executed = !executed;
049      return children;
050    } finally {
051      updateTimestamp();
052    }
053  }
054
055  @Override
056  protected void doRollback(final TEnvironment env)
057      throws IOException, InterruptedException {
058    updateTimestamp();
059    if (executed) {
060      try {
061        rollback(env);
062        executed = !executed;
063      } finally {
064        updateTimestamp();
065      }
066    }
067  }
068
069  @Override
070  protected void serializeStateData(ProcedureStateSerializer serializer)
071      throws IOException {
072    SequentialProcedureData.Builder data = SequentialProcedureData.newBuilder();
073    data.setExecuted(executed);
074    serializer.serialize(data.build());
075  }
076
077  @Override
078  protected void deserializeStateData(ProcedureStateSerializer serializer)
079      throws IOException {
080    SequentialProcedureData data = serializer.deserialize(SequentialProcedureData.class);
081    executed = data.getExecuted();
082  }
083}