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