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.procedure2;
020
021import org.apache.yetus.audience.InterfaceAudience;
022
023/**
024 * Interface to get status of a Lock without getting access to acquire/release lock. Currently used
025 * in MasterProcedureScheduler where we want to give Queues access to lock's status for scheduling
026 * purposes, but not the ability to acquire/release it.
027 */
028@InterfaceAudience.Private
029public interface LockStatus {
030
031  /**
032   * Return whether this lock has already been held,
033   * <p/>
034   * Notice that, holding the exclusive lock or shared lock are both considered as locked, i.e, this
035   * method usually equals to {@code hasExclusiveLock() || getSharedLockCount() > 0}.
036   */
037  default boolean isLocked() {
038    return hasExclusiveLock() || getSharedLockCount() > 0;
039  }
040
041  /**
042   * Whether the exclusive lock has been held.
043   */
044  boolean hasExclusiveLock();
045
046  /**
047   * Return true if the procedure itself holds the exclusive lock, or any ancestors of the give
048   * procedure hold the exclusive lock.
049   */
050  boolean hasLockAccess(Procedure<?> proc);
051
052  /**
053   * Get the procedure which holds the exclusive lock.
054   */
055  Procedure<?> getExclusiveLockOwnerProcedure();
056
057  /**
058   * Return the id of the procedure which holds the exclusive lock, if exists. Or a negative value
059   * which means no one holds the exclusive lock.
060   * <p/>
061   * Notice that, in HBase, we assume that the procedure id is positive, or at least non-negative.
062   */
063  default long getExclusiveLockProcIdOwner() {
064    Procedure<?> proc = getExclusiveLockOwnerProcedure();
065    return proc != null ? proc.getProcId() : -1L;
066  }
067
068  /**
069   * Get the number of procedures which hold the shared lock.
070   */
071  int getSharedLockCount();
072}