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 java.io.IOException;
022import org.apache.hadoop.hbase.NamespaceDescriptor;
023import org.apache.hadoop.hbase.TableName;
024import org.apache.hadoop.hbase.master.MasterFileSystem;
025import org.apache.hadoop.hbase.master.TableNamespaceManager;
026import org.apache.yetus.audience.InterfaceAudience;
027
028import org.apache.hbase.thirdparty.com.google.common.annotations.VisibleForTesting;
029
030import org.apache.hadoop.hbase.procedure2.StateMachineProcedure;
031import org.apache.hadoop.hbase.util.FSUtils;
032
033/**
034 * Base class for all the Namespace procedures that want to use a StateMachineProcedure. It provide
035 * some basic helpers like basic locking and basic toStringClassDetails().
036 */
037@InterfaceAudience.Private
038public abstract class AbstractStateMachineNamespaceProcedure<TState>
039    extends StateMachineProcedure<MasterProcedureEnv, TState> implements TableProcedureInterface {
040
041  private final ProcedurePrepareLatch syncLatch;
042
043  protected AbstractStateMachineNamespaceProcedure() {
044    // Required by the Procedure framework to create the procedure on replay
045    syncLatch = null;
046  }
047
048  protected AbstractStateMachineNamespaceProcedure(final MasterProcedureEnv env) {
049    this(env, null);
050  }
051
052  protected AbstractStateMachineNamespaceProcedure(final MasterProcedureEnv env,
053      final ProcedurePrepareLatch latch) {
054    this.setOwner(env.getRequestUser());
055    this.syncLatch = latch;
056  }
057
058  protected abstract String getNamespaceName();
059
060  @Override
061  public TableName getTableName() {
062    return DUMMY_NAMESPACE_TABLE_NAME;
063  }
064
065  @Override
066  public abstract TableOperationType getTableOperationType();
067
068  @Override
069  public void toStringClassDetails(final StringBuilder sb) {
070    sb.append(getClass().getSimpleName());
071    sb.append(", namespace=");
072    sb.append(getNamespaceName());
073  }
074
075  @Override
076  protected boolean waitInitialized(MasterProcedureEnv env) {
077    return env.waitInitialized(this);
078  }
079
080  @Override
081  protected LockState acquireLock(final MasterProcedureEnv env) {
082    if (env.getProcedureScheduler().waitNamespaceExclusiveLock(this, getNamespaceName())) {
083      return LockState.LOCK_EVENT_WAIT;
084    }
085    return LockState.LOCK_ACQUIRED;
086  }
087
088  @Override
089  protected void releaseLock(final MasterProcedureEnv env) {
090    env.getProcedureScheduler().wakeNamespaceExclusiveLock(this, getNamespaceName());
091  }
092
093  /**
094   * Insert/update the row into the ns family of meta table.
095   * @param env MasterProcedureEnv
096   */
097  protected static void addOrUpdateNamespace(MasterProcedureEnv env, NamespaceDescriptor ns)
098      throws IOException {
099    getTableNamespaceManager(env).addOrUpdateNamespace(ns);
100  }
101
102  protected static TableNamespaceManager getTableNamespaceManager(MasterProcedureEnv env) {
103    return env.getMasterServices().getClusterSchema().getTableNamespaceManager();
104  }
105
106  /**
107   * Create the namespace directory
108   * @param env MasterProcedureEnv
109   * @param nsDescriptor NamespaceDescriptor
110   */
111  protected static void createDirectory(MasterProcedureEnv env, NamespaceDescriptor nsDescriptor)
112      throws IOException {
113    createDirectory(env.getMasterServices().getMasterFileSystem(), nsDescriptor);
114  }
115
116  @VisibleForTesting
117  public static void createDirectory(MasterFileSystem mfs, NamespaceDescriptor nsDescriptor)
118      throws IOException {
119    mfs.getFileSystem().mkdirs(FSUtils.getNamespaceDir(mfs.getRootDir(), nsDescriptor.getName()));
120  }
121
122  protected void releaseSyncLatch() {
123    ProcedurePrepareLatch.releaseLatch(syncLatch, this);
124  }
125}