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.procedure2.LockStatus; 023import org.apache.hadoop.hbase.procedure2.Procedure; 024import org.apache.hadoop.hbase.procedure2.ProcedureDeque; 025import org.apache.hadoop.hbase.util.AvlUtil.AvlLinkedNode; 026import org.apache.yetus.audience.InterfaceAudience; 027 028@InterfaceAudience.Private 029abstract class Queue<TKey extends Comparable<TKey>> extends AvlLinkedNode<Queue<TKey>> { 030 031 /** 032 * @param proc must not be null 033 */ 034 abstract boolean requireExclusiveLock(Procedure<?> proc); 035 036 private final TKey key; 037 private final int priority; 038 private final ProcedureDeque runnables = new ProcedureDeque(); 039 // Reference to status of lock on entity this queue represents. 040 private final LockStatus lockStatus; 041 042 protected Queue(TKey key, LockStatus lockStatus) { 043 this(key, 1, lockStatus); 044 } 045 046 protected Queue(TKey key, int priority, LockStatus lockStatus) { 047 assert priority >= 1 : "priority must be greater than or equal to 1"; 048 this.key = key; 049 this.priority = priority; 050 this.lockStatus = lockStatus; 051 } 052 053 protected TKey getKey() { 054 return key; 055 } 056 057 public int getPriority() { 058 return priority; 059 } 060 061 protected LockStatus getLockStatus() { 062 return lockStatus; 063 } 064 065 public boolean isAvailable() { 066 return !isEmpty(); 067 } 068 069 // ====================================================================== 070 // Functions to handle procedure queue 071 // ====================================================================== 072 public void add(Procedure<?> proc, boolean addToFront) { 073 if (addToFront) { 074 runnables.addFirst(proc); 075 } else { 076 runnables.addLast(proc); 077 } 078 } 079 080 public Procedure<?> peek() { 081 return runnables.peek(); 082 } 083 084 public Procedure<?> poll() { 085 return runnables.poll(); 086 } 087 088 public boolean isEmpty() { 089 return runnables.isEmpty(); 090 } 091 092 public int size() { 093 return runnables.size(); 094 } 095 096 // ====================================================================== 097 // Generic Helpers 098 // ====================================================================== 099 public int compareKey(TKey cmpKey) { 100 return key.compareTo(cmpKey); 101 } 102 103 @Override 104 public int compareTo(Queue<TKey> other) { 105 return compareKey(other.key); 106 } 107 108 @Override 109 public String toString() { 110 return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).append("key", key) 111 .append("lockStatus", lockStatus.describeLockStatus()).append("size", size()).build(); 112 } 113}