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