vaijosh commented on code in PR #5462:
URL: https://github.com/apache/hbase/pull/5462#discussion_r1367662015


##########
hbase-server/src/main/java/org/apache/hadoop/hbase/master/procedure/TruncateRegionProcedure.java:
##########
@@ -0,0 +1,212 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hbase.master.procedure;
+
+import java.io.IOException;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.hbase.HBaseIOException;
+import org.apache.hadoop.hbase.TableName;
+import org.apache.hadoop.hbase.client.RegionInfo;
+import org.apache.hadoop.hbase.master.MasterCoprocessorHost;
+import org.apache.hadoop.hbase.master.MasterFileSystem;
+import org.apache.hadoop.hbase.master.assignment.RegionStateNode;
+import org.apache.hadoop.hbase.master.assignment.TransitRegionStateProcedure;
+import org.apache.hadoop.hbase.regionserver.HRegionFileSystem;
+import org.apache.hadoop.hbase.util.CommonFSUtils;
+import org.apache.yetus.audience.InterfaceAudience;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import 
org.apache.hadoop.hbase.shaded.protobuf.generated.MasterProcedureProtos.TruncateRegionState;
+
[email protected]
+public class TruncateRegionProcedure
+  extends AbstractStateMachineRegionProcedure<TruncateRegionState> {
+  private static final Logger LOG = 
LoggerFactory.getLogger(TruncateRegionProcedure.class);
+
+  @SuppressWarnings("unused")
+  public TruncateRegionProcedure() {
+    // Required by the Procedure framework to create the procedure on replay
+    super();
+  }
+
+  public TruncateRegionProcedure(final MasterProcedureEnv env, final 
RegionInfo hri)
+    throws HBaseIOException {
+    super(env, hri);
+    checkOnline(env, getRegion());
+  }
+
+  public TruncateRegionProcedure(final MasterProcedureEnv env, final 
RegionInfo region,
+    ProcedurePrepareLatch latch) throws HBaseIOException {
+    super(env, region, latch);
+    preflightChecks(env, true);
+  }
+
+  @Override
+  protected Flow executeFromState(final MasterProcedureEnv env, 
TruncateRegionState state)
+    throws InterruptedException {
+    if (LOG.isTraceEnabled()) {
+      LOG.trace(this + " execute state=" + state);
+    }
+    try {
+      switch (state) {
+        case TRUNCATE_REGION_PRE_OPERATION:
+          if (!prepareTruncate()) {
+            assert isFailed() : "the truncate should have an exception here";
+            return Flow.NO_MORE_STATE;
+          }
+          checkOnline(env, getRegion());
+          assert getRegion().getReplicaId() == RegionInfo.DEFAULT_REPLICA_ID 
|| isFailed()
+            : "Can't truncate replicas directly. "
+              + "Replicas are auto-truncated when their primary is truncated.";
+          preTruncate(env);
+          setNextState(TruncateRegionState.TRUNCATE_REGION_MAKE_OFFLINE);
+          break;
+        case TRUNCATE_REGION_MAKE_OFFLINE:
+          addChildProcedure(createUnAssignProcedures(env));
+          setNextState(TruncateRegionState.TRUNCATE_REGION_REMOVE);
+          break;
+        case TRUNCATE_REGION_REMOVE:
+          deleteRegionFromFileSystem(env);
+          setNextState(TruncateRegionState.TRUNCATE_REGION_MAKE_ONLINE);
+          break;
+        case TRUNCATE_REGION_MAKE_ONLINE:
+          addChildProcedure(createAssignProcedures(env));
+          setNextState(TruncateRegionState.TRUNCATE_REGION_POST_OPERATION);
+          break;
+        case TRUNCATE_REGION_POST_OPERATION:
+          postTruncate(env);
+          LOG.debug("truncate '" + getTableName() + "' completed");
+          return Flow.NO_MORE_STATE;
+        default:
+          throw new UnsupportedOperationException("unhandled state=" + state);
+      }
+    } catch (IOException e) {
+      if (isRollbackSupported(state)) {
+        setFailure("master-truncate-region", e);
+      } else {
+        LOG.warn("Retriable error trying to truncate region=" + 
getRegion().getRegionNameAsString()
+          + " state=" + state, e);
+      }
+    }
+    return Flow.HAS_MORE_STATE;
+  }
+
+  private void deleteRegionFromFileSystem(final MasterProcedureEnv env) throws 
IOException {
+    RegionStateNode regionNode =
+      
env.getAssignmentManager().getRegionStates().getRegionStateNode(getRegion());
+    try {
+      regionNode.lock();
+      final MasterFileSystem mfs = 
env.getMasterServices().getMasterFileSystem();
+      final Path tableDir = CommonFSUtils.getTableDir(mfs.getRootDir(), 
getTableName());
+      
HRegionFileSystem.deleteRegionFromFileSystem(env.getMasterConfiguration(),
+        mfs.getFileSystem(), tableDir, getRegion());
+    } finally {
+      regionNode.unlock();
+    }
+  }
+
+  @Override
+  protected void rollbackState(final MasterProcedureEnv env, final 
TruncateRegionState state)
+    throws IOException {
+    if (state == TruncateRegionState.TRUNCATE_REGION_PRE_OPERATION) {
+      // Nothing to rollback, pre-truncate is just table-state checks.
+      return;
+    }
+    if (state == TruncateRegionState.TRUNCATE_REGION_MAKE_OFFLINE) {
+      RegionStateNode regionNode =
+        
env.getAssignmentManager().getRegionStates().getRegionStateNode(getRegion());
+      if (regionNode == null) {
+        // Region was unassigned by state TRUNCATE_REGION_MAKE_OFFLINE.
+        // So Assign it back
+        addChildProcedure(createAssignProcedures(env));
+      }
+      return;
+    }
+    // The truncate doesn't have a rollback. The execution will succeed, at 
some point.
+    throw new UnsupportedOperationException("unhandled state=" + state);
+  }
+
+  @Override
+  protected void completionCleanup(final MasterProcedureEnv env) {
+    releaseSyncLatch();
+  }
+
+  @Override
+  protected boolean isRollbackSupported(final TruncateRegionState state) {
+    return state == TruncateRegionState.TRUNCATE_REGION_PRE_OPERATION;
+  }
+
+  @Override
+  protected TruncateRegionState getState(final int stateId) {
+    return TruncateRegionState.forNumber(stateId);
+  }
+
+  @Override
+  protected int getStateId(final TruncateRegionState state) {
+    return state.getNumber();
+  }
+
+  @Override
+  protected TruncateRegionState getInitialState() {
+    return TruncateRegionState.TRUNCATE_REGION_PRE_OPERATION;
+  }
+
+  @Override

Review Comment:
   > Why was serializeStateData and deserializeStateData dropped? We need that 
right, at least super call? Also, Is user being serialized/deserialized 
somewhere? Also I prefer having the state declaration in proto for 
extensibility in future which was also dropped.
   Hi @NihalJain ,
   I checked this aspect and have following observations.
   - AbstractStateMachineRegionProcedure currently has 8 inheritors, out of 
which only 5 classes overrides serialise and deserialise because they have 
extra fields. So looks like general pattern is to override it only when its 
necessary. 
   - Super class methods automatically gets and serialise/deserialise "region", 
even if we don't override in TruncateRegion. I verified it using debugger.
   - User is not serialised, instead its populated in 
AbstractStateMachineTableProcedure constructor from the MasterProcedureEnv.
   - At moment we don't foresee extra parameters for truncate_region. So 
whenever we get that requirements we can always add state proto and override 
the serialise/deserialise methods.
   
   So based on these findings, I feel its okay to rely on superclass 
implementation.
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to