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.master.procedure;
019
020import org.apache.commons.lang3.builder.ToStringBuilder;
021import org.apache.commons.lang3.builder.ToStringStyle;
022import org.apache.hadoop.hbase.TableName;
023import org.apache.hadoop.hbase.procedure2.LockStatus;
024import org.apache.hadoop.hbase.procedure2.Procedure;
025import org.apache.yetus.audience.InterfaceAudience;
026
027@InterfaceAudience.Private
028class TableQueue extends Queue<TableName> {
029  private final LockStatus namespaceLockStatus;
030
031  public TableQueue(TableName tableName, int priority, LockStatus tableLock,
032    LockStatus namespaceLockStatus) {
033    super(tableName, priority, tableLock);
034    this.namespaceLockStatus = namespaceLockStatus;
035  }
036
037  @Override
038  public boolean isAvailable() {
039    return !isEmpty() && !namespaceLockStatus.hasExclusiveLock();
040  }
041
042  @Override
043  public boolean requireExclusiveLock(Procedure<?> proc) {
044    return requireTableExclusiveLock((TableProcedureInterface) proc);
045  }
046
047  /**
048   * @param proc must not be null
049   */
050  static boolean requireTableExclusiveLock(TableProcedureInterface proc) {
051    switch (proc.getTableOperationType()) {
052      case CREATE:
053      case DELETE:
054      case DISABLE:
055      case SNAPSHOT:
056      case ENABLE:
057      case RESTORE_BACKUP_SYSTEM_TABLE:
058        return true;
059      case EDIT:
060        // we allow concurrent edit on the ns family in meta table
061        return !proc.getTableName().equals(TableProcedureInterface.DUMMY_NAMESPACE_TABLE_NAME);
062      case READ:
063      case FLUSH:
064        return false;
065      // region operations are using the shared-lock on the table
066      // and then they will grab an xlock on the region.
067      case REGION_SPLIT:
068      case REGION_MERGE:
069      case REGION_ASSIGN:
070      case REGION_UNASSIGN:
071      case REGION_EDIT:
072      case REGION_GC:
073      case MERGED_REGIONS_GC:
074      case REGION_SNAPSHOT:
075      case REGION_TRUNCATE:
076        return false;
077      default:
078        break;
079    }
080    throw new UnsupportedOperationException("unexpected type " + proc.getTableOperationType());
081  }
082
083  @Override
084  public String toString() {
085    return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).appendSuper(super.toString())
086      .append("namespaceLockStatus", namespaceLockStatus.describeLockStatus()).build();
087  }
088}