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