- 
                Notifications
    You must be signed in to change notification settings 
- Fork 3.4k
HBASE-27905 Directly schedule procedures that do not need to acquire … #5267
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Open
      
      
            frostruan
  wants to merge
  3
  commits into
  apache:master
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
frostruan:HBASE-27905
  
      
      
   
  
    
  
  
  
 
  
      
    base: master
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
      
        
          +123
        
        
          −20
        
        
          
        
      
    
  
  
     Open
                    Changes from all commits
      Commits
    
    
  File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -149,19 +149,22 @@ private <T extends Comparable<T>> void doAdd(FairQueue<T> fairq, Queue<T> queue, | |
| Procedure<?> proc, boolean addFront) { | ||
| queue.add(proc, addFront); | ||
| // For the following conditions, we will put the queue back into execution | ||
| // 1. The procedure has already held the lock, or the lock has been restored when restarting, | ||
| // 1. The procedure does not need any lock at all. | ||
| // 2. The procedure has already held the lock, or the lock has been restored when restarting, | ||
| // which means it can be executed immediately. | ||
| // 2. The exclusive lock for this queue has not been held. | ||
| // 3. The given procedure has the exclusive lock permission for this queue. | ||
| // 3. The exclusive lock for this queue has not been held. | ||
| // 4. The given procedure has the exclusive lock permission for this queue. | ||
| Supplier<String> reason = null; | ||
| if (proc.hasLock()) { | ||
| if (!proc.needLock()) { | ||
| reason = () -> proc + " does not need any lock"; | ||
| } else if (proc.needLock() && proc.hasLock()) { | ||
| reason = () -> proc + " has lock"; | ||
| } else if (proc.isLockedWhenLoading()) { | ||
| reason = () -> proc + " restores lock when restarting"; | ||
| } else if (!queue.getLockStatus().hasExclusiveLock()) { | ||
| reason = () -> "the exclusive lock is not held by anyone when adding " + proc; | ||
| } else if (queue.getLockStatus().hasLockAccess(proc)) { | ||
| reason = () -> proc + " has the excusive lock access"; | ||
| reason = () -> proc + " has the exclusive lock access"; | ||
| } | ||
| if (reason != null) { | ||
| addToRunQueue(fairq, queue, reason); | ||
|  | @@ -219,6 +222,9 @@ private <T extends Comparable<T>> Procedure<?> doPoll(final FairQueue<T> fairq) | |
| // procedures, then we give up and remove the queue from run queue. | ||
| for (int i = 0, n = rq.size(); i < n; i++) { | ||
| Procedure<?> proc = rq.poll(); | ||
| if (!proc.needLock()) { | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not add this logic in the below isLockReady method? | ||
| return proc; | ||
| } | ||
| if (isLockReady(proc, rq)) { | ||
| // the queue is empty, remove from run queue | ||
| if (rq.isEmpty()) { | ||
|  | @@ -229,8 +235,15 @@ private <T extends Comparable<T>> Procedure<?> doPoll(final FairQueue<T> fairq) | |
| // we are not ready to run, add back and try the next procedure | ||
| rq.add(proc, false); | ||
| } | ||
| // no procedure is ready for execution, remove from run queue | ||
| removeFromRunQueue(fairq, rq, () -> "no procedure can be executed"); | ||
| if (hasNoLockNeededProcedure(rq)) { | ||
| if (LOG.isTraceEnabled()) { | ||
| LOG.trace("DO NOT remove {} from run queue because there are still procedures that do " | ||
| + "not need to acquire locks in the queue", rq); | ||
| } | ||
| } else { | ||
| // no procedure is ready for execution, remove from run queue | ||
| removeFromRunQueue(fairq, rq, () -> "no procedure can be executed"); | ||
| } | ||
| return null; | ||
| } | ||
|  | ||
|  | @@ -376,6 +389,19 @@ private static <T extends Comparable<T>> void removeFromRunQueue(FairQueue<T> fa | |
| } | ||
| } | ||
|  | ||
| private static <T extends Comparable<T>> boolean hasNoLockNeededProcedure(Queue<T> q) { | ||
| boolean ret = false; | ||
| // TODO: Iterate Queue in a more efficient way ? | ||
| for (int i = 0, n = q.size(); i < n; i++) { | ||
| Procedure<?> proc = q.poll(); | ||
| if (!proc.needLock()) { | ||
| ret = true; | ||
| } | ||
| q.add(proc, false); | ||
| } | ||
| return ret; | ||
| } | ||
|  | ||
| // ============================================================================ | ||
| // Table Queue Lookup Helpers | ||
| // ============================================================================ | ||
|  | @@ -616,8 +642,15 @@ public boolean waitTableExclusiveLock(final Procedure<?> procedure, final TableN | |
| logLockedResource(LockedResourceType.TABLE, table.getNameAsString()); | ||
| return true; | ||
| } | ||
| removeFromRunQueue(tableRunQueue, getTableQueue(table), | ||
| () -> procedure + " held the exclusive lock"); | ||
| TableQueue queue = getTableQueue(table); | ||
| if (hasNoLockNeededProcedure(queue)) { | ||
| if (LOG.isTraceEnabled()) { | ||
| LOG.trace("DO NOT remove {} from run queue because there are still procedures that do " | ||
| + "not need to acquire locks in the queue", queue); | ||
| } | ||
| } else { | ||
| removeFromRunQueue(tableRunQueue, queue, () -> procedure + " held the exclusive lock"); | ||
| } | ||
| return false; | ||
| } finally { | ||
| schedUnlock(); | ||
|  | @@ -932,12 +965,18 @@ public boolean waitServerExclusiveLock(final Procedure<?> procedure, | |
| if (lock.tryExclusiveLock(procedure)) { | ||
| // In tests we may pass procedures other than ServerProcedureInterface, just pass null if | ||
| // so. | ||
| removeFromRunQueue(serverRunQueue, | ||
| getServerQueue(serverName, | ||
| procedure instanceof ServerProcedureInterface | ||
| ? (ServerProcedureInterface) procedure | ||
| : null), | ||
| () -> procedure + " held exclusive lock"); | ||
| ServerQueue queue = getServerQueue(serverName, | ||
| procedure instanceof ServerProcedureInterface | ||
| ? (ServerProcedureInterface) procedure | ||
| : null); | ||
| if (hasNoLockNeededProcedure(queue)) { | ||
| if (LOG.isTraceEnabled()) { | ||
| LOG.trace("DO NOT remove {} from run queue because there are still procedures that do " | ||
| + "not need to acquire locks in the queue", queue); | ||
| } | ||
| } else { | ||
| removeFromRunQueue(serverRunQueue, queue, () -> procedure + " held exclusive lock"); | ||
| } | ||
| return false; | ||
| } | ||
| waitProcedure(lock, procedure); | ||
|  | @@ -990,8 +1029,15 @@ public boolean waitPeerExclusiveLock(Procedure<?> procedure, String peerId) { | |
| try { | ||
| final LockAndQueue lock = locking.getPeerLock(peerId); | ||
| if (lock.tryExclusiveLock(procedure)) { | ||
| removeFromRunQueue(peerRunQueue, getPeerQueue(peerId), | ||
| () -> procedure + " held exclusive lock"); | ||
| PeerQueue queue = getPeerQueue(peerId); | ||
| if (hasNoLockNeededProcedure(queue)) { | ||
| if (LOG.isTraceEnabled()) { | ||
| LOG.trace("DO NOT remove {} from run queue because there are still procedures that do " | ||
| + "not need to acquire locks in the queue", queue); | ||
| } | ||
| } else { | ||
| removeFromRunQueue(peerRunQueue, queue, () -> procedure + " held exclusive lock"); | ||
| } | ||
| return false; | ||
| } | ||
| waitProcedure(lock, procedure); | ||
|  | @@ -1040,7 +1086,15 @@ public boolean waitMetaExclusiveLock(Procedure<?> procedure) { | |
| try { | ||
| final LockAndQueue lock = locking.getMetaLock(); | ||
| if (lock.tryExclusiveLock(procedure)) { | ||
| removeFromRunQueue(metaRunQueue, getMetaQueue(), () -> procedure + " held exclusive lock"); | ||
| MetaQueue queue = getMetaQueue(); | ||
| if (hasNoLockNeededProcedure(queue)) { | ||
| if (LOG.isTraceEnabled()) { | ||
| LOG.trace("DO NOT remove {} from run queue because there are still procedures that do " | ||
| + "not need to acquire locks in the queue", queue); | ||
| } | ||
| } else { | ||
| removeFromRunQueue(metaRunQueue, queue, () -> procedure + " held exclusive lock"); | ||
| } | ||
| return false; | ||
| } | ||
| waitProcedure(lock, procedure); | ||
|  | @@ -1086,8 +1140,15 @@ public boolean waitGlobalExclusiveLock(Procedure<?> procedure, String globalId) | |
| try { | ||
| final LockAndQueue lock = locking.getGlobalLock(globalId); | ||
| if (lock.tryExclusiveLock(procedure)) { | ||
| removeFromRunQueue(globalRunQueue, getGlobalQueue(globalId), | ||
| () -> procedure + " held shared lock"); | ||
| GlobalQueue queue = getGlobalQueue(globalId); | ||
| if (hasNoLockNeededProcedure(queue)) { | ||
| if (LOG.isTraceEnabled()) { | ||
| LOG.trace("DO NOT remove {} from run queue because there are still procedures that do " | ||
| + "not need to acquire locks in the queue", queue); | ||
| } | ||
| } else { | ||
| removeFromRunQueue(globalRunQueue, queue, () -> procedure + " held shared lock"); | ||
| } | ||
| return false; | ||
| } | ||
| waitProcedure(lock, procedure); | ||
|  | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: needsLock