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.master.procedure;
020
021import org.apache.hadoop.hbase.TableName;
022import org.apache.yetus.audience.InterfaceAudience;
023import org.apache.hadoop.hbase.procedure2.StateMachineProcedure;
024
025/**
026 * Base class for all the Namespace procedures that want to use a StateMachineProcedure.
027 * It provide some basic helpers like basic locking and basic toStringClassDetails().
028 */
029@InterfaceAudience.Private
030public abstract class AbstractStateMachineNamespaceProcedure<TState>
031    extends StateMachineProcedure<MasterProcedureEnv, TState>
032    implements TableProcedureInterface {
033
034  private final ProcedurePrepareLatch syncLatch;
035
036  protected AbstractStateMachineNamespaceProcedure() {
037    // Required by the Procedure framework to create the procedure on replay
038    syncLatch = null;
039  }
040
041  protected AbstractStateMachineNamespaceProcedure(final MasterProcedureEnv env) {
042    this(env, null);
043  }
044
045  protected AbstractStateMachineNamespaceProcedure(final MasterProcedureEnv env,
046      final ProcedurePrepareLatch latch) {
047    this.setOwner(env.getRequestUser());
048    this.syncLatch = latch;
049  }
050
051  protected abstract String getNamespaceName();
052
053  @Override
054  public TableName getTableName() {
055    return TableName.NAMESPACE_TABLE_NAME;
056  }
057
058  @Override
059  public abstract TableOperationType getTableOperationType();
060
061  @Override
062  public void toStringClassDetails(final StringBuilder sb) {
063    sb.append(getClass().getSimpleName());
064    sb.append(", namespace=");
065    sb.append(getNamespaceName());
066  }
067
068  @Override
069  protected boolean waitInitialized(MasterProcedureEnv env) {
070    return env.waitInitialized(this);
071  }
072
073  @Override
074  protected LockState acquireLock(final MasterProcedureEnv env) {
075    if (env.getProcedureScheduler().waitNamespaceExclusiveLock(this, getNamespaceName())) {
076      return LockState.LOCK_EVENT_WAIT;
077    }
078    return LockState.LOCK_ACQUIRED;
079  }
080
081  @Override
082  protected void releaseLock(final MasterProcedureEnv env) {
083    env.getProcedureScheduler().wakeNamespaceExclusiveLock(this, getNamespaceName());
084  }
085
086  protected void releaseSyncLatch() {
087    ProcedurePrepareLatch.releaseLatch(syncLatch, this);
088  }
089}