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  private static boolean requireTableExclusiveLock(TableProcedureInterface proc) {
051    switch (proc.getTableOperationType()) {
052      case CREATE:
053      case DELETE:
054      case DISABLE:
055      case ENABLE:
056        return true;
057      case EDIT:
058        // we allow concurrent edit on the ns family in meta table
059        return !proc.getTableName().equals(TableProcedureInterface.DUMMY_NAMESPACE_TABLE_NAME);
060      case READ:
061      case FLUSH:
062      case SNAPSHOT:
063        return false;
064      // region operations are using the shared-lock on the table
065      // and then they will grab an xlock on the region.
066      case REGION_SPLIT:
067      case REGION_MERGE:
068      case REGION_ASSIGN:
069      case REGION_UNASSIGN:
070      case REGION_EDIT:
071      case REGION_GC:
072      case MERGED_REGIONS_GC:
073      case REGION_SNAPSHOT:
074      case REGION_TRUNCATE:
075        return false;
076      default:
077        break;
078    }
079    throw new UnsupportedOperationException("unexpected type " + proc.getTableOperationType());
080  }
081
082  @Override
083  public String toString() {
084    return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).appendSuper(super.toString())
085      .append("namespaceLockStatus", namespaceLockStatus.describeLockStatus()).build();
086  }
087}