- 
                Notifications
    You must be signed in to change notification settings 
- Fork 491
keep knowledge of ongoing merges across merge pipelines #5633
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
      
      
            trinity-1686a
  wants to merge
  11
  commits into
  main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
trinity/recover-forgotten-merges
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
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.
          
          
  
     Open
                    Changes from 4 commits
      Commits
    
    
            Show all changes
          
          
            11 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      9e1ab35
              
                track merges accross pipelines
              
              
                trinity-1686a 1299985
              
                test no double merge after pipeline restart
              
              
                trinity-1686a 100d400
              
                improve test
              
              
                trinity-1686a f63f24d
              
                add test to tracker
              
              
                trinity-1686a 1972bb3
              
                Merge branch main into trinity/recover-forgotten-merges
              
              
                trinity-1686a 074fbc6
              
                increase sleep time, fixing a flaky test
              
              
                trinity-1686a 1826849
              
                add sleep time, fixing another flaky test
              
              
                trinity-1686a ae99e04
              
                typo
              
              
                trinity-1686a 0200e63
              
                make RecordUnacknowledgedDrop::acknoledge() consume values
              
              
                trinity-1686a c60607d
              
                fix some flaky tests
              
              
                trinity-1686a 2a6f742
              
                add license header
              
              
                trinity-1686a 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
      
      Oops, something went wrong.
      
    
  
  
    
      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
    
  
  
    
              
  
    
      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 | 
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| use std::mem::MaybeUninit; | ||
| use std::ops::Deref; | ||
| use std::sync::atomic::{AtomicBool, Ordering}; | ||
| use std::sync::mpsc::{channel, Receiver, Sender}; | ||
| use std::sync::{Arc, Mutex}; | ||
|  | ||
| use census::{Inventory, TrackedObject as InventoredObject}; | ||
|  | ||
| pub type TrackedObject<T> = InventoredObject<RecordUnacknoledgedDrop<T>>; | ||
|  | ||
| #[derive(Clone)] | ||
| pub struct Tracker<T> { | ||
|         
                  trinity-1686a marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved         
                  trinity-1686a marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| inner_inventory: Inventory<RecordUnacknoledgedDrop<T>>, | ||
| unacknoledged_drop_receiver: Arc<Mutex<Receiver<T>>>, | ||
| return_channel: Sender<T>, | ||
| } | ||
|  | ||
| #[derive(Debug)] | ||
| pub struct RecordUnacknoledgedDrop<T> { | ||
| // safety: this is always kept initialized except after Self::drop, where we move that | ||
| // that value away to either send it through the return channel, or drop it manually | ||
| inner: MaybeUninit<T>, | ||
| acknoledged: AtomicBool, | ||
| return_channel: Sender<T>, | ||
| } | ||
|  | ||
| impl<T> RecordUnacknoledgedDrop<T> { | ||
| pub fn acknoledge(&self) { | ||
|         
                  trinity-1686a marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved         
                  trinity-1686a marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| self.acknoledged.store(true, Ordering::Relaxed); | ||
| } | ||
|  | ||
| pub fn untracked(value: T) -> Self { | ||
| let (sender, _receiver) = channel(); | ||
| RecordUnacknoledgedDrop { | ||
| inner: MaybeUninit::new(value), | ||
| acknoledged: true.into(), | ||
|         
                  trinity-1686a marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| return_channel: sender, | ||
| } | ||
| } | ||
| } | ||
|  | ||
| impl<T> Deref for RecordUnacknoledgedDrop<T> { | ||
| type Target = T; | ||
| fn deref(&self) -> &T { | ||
| unsafe { | ||
| // safety: see struct definition, this operation is valid except after drop. | ||
| self.inner.assume_init_ref() | ||
| } | ||
| } | ||
| } | ||
|  | ||
| impl<T> Drop for RecordUnacknoledgedDrop<T> { | ||
| fn drop(&mut self) { | ||
| let item = unsafe { | ||
| // safety: see struct definition. Additionally, we don't touch to self.inner | ||
| // after this point so there is no risk of making a 2nd copy and cause a | ||
| // double-free | ||
| self.inner.assume_init_read() | ||
| }; | ||
| if !*self.acknoledged.get_mut() { | ||
| // if send fails, no one cared about getting that notification, it's fine to | ||
| // drop item | ||
| let _ = self.return_channel.send(item); | ||
| } | ||
| } | ||
| } | ||
|  | ||
| impl<T> Default for Tracker<T> { | ||
| fn default() -> Self { | ||
| Self::new() | ||
| } | ||
| } | ||
|  | ||
| impl<T> Tracker<T> { | ||
| pub fn new() -> Self { | ||
| let (sender, receiver) = channel(); | ||
| Tracker { | ||
| inner_inventory: Inventory::new(), | ||
| unacknoledged_drop_receiver: Arc::new(Mutex::new(receiver)), | ||
| return_channel: sender, | ||
| } | ||
| } | ||
|  | ||
| /// Return whether it is safe to recreate this tracker. | ||
| /// | ||
| /// A tracker is considered safe to recreate if this is the only instance left, | ||
| /// and it conaints no alive object (it may contain dead objects though). | ||
|         
                  trinity-1686a marked this conversation as resolved.
              Outdated
          
            Show resolved
            Hide resolved | ||
| /// | ||
| /// Once this return true, it will stay that way until [Tracker::track] or [Tracker::clone] are | ||
| /// called. | ||
| pub fn safe_to_recreate(&self) -> bool { | ||
| Arc::strong_count(&self.unacknoledged_drop_receiver) == 1 && self.inner_inventory.len() == 0 | ||
| } | ||
|  | ||
| pub fn list_ongoing(&self) -> Vec<TrackedObject<T>> { | ||
| self.inner_inventory.list() | ||
| } | ||
|  | ||
| pub fn take_dead(&self) -> Vec<T> { | ||
| let mut res = Vec::new(); | ||
| let receiver = self.unacknoledged_drop_receiver.lock().unwrap(); | ||
| while let Ok(dead_entry) = receiver.try_recv() { | ||
| res.push(dead_entry); | ||
| } | ||
| res | ||
| } | ||
|  | ||
| pub fn track(&self, value: T) -> TrackedObject<T> { | ||
| self.inner_inventory.track(RecordUnacknoledgedDrop { | ||
| inner: MaybeUninit::new(value), | ||
| acknoledged: false.into(), | ||
| return_channel: self.return_channel.clone(), | ||
| }) | ||
| } | ||
| } | ||
|  | ||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::{TrackedObject, Tracker}; | ||
|  | ||
| #[track_caller] | ||
| fn assert_tracked_eq<T: PartialEq + std::fmt::Debug>( | ||
| got: Vec<TrackedObject<T>>, | ||
| expected: Vec<T>, | ||
| ) { | ||
| assert_eq!( | ||
| got.len(), | ||
| expected.len(), | ||
| "expected vec of same lenght, {} != {}", | ||
| got.len(), | ||
| expected.len() | ||
| ); | ||
| for (got_item, expected_item) in got.into_iter().zip(expected) { | ||
| assert_eq!(**got_item, expected_item); | ||
| } | ||
| } | ||
|  | ||
| #[test] | ||
| fn test_single_tracker() { | ||
| let tracker = Tracker::<u32>::new(); | ||
|  | ||
| assert!(tracker.list_ongoing().is_empty()); | ||
| assert!(tracker.take_dead().is_empty()); | ||
| assert!(tracker.safe_to_recreate()); | ||
|  | ||
| { | ||
| let tracked_1 = tracker.track(1); | ||
| assert_tracked_eq(tracker.list_ongoing(), vec![1]); | ||
| assert!(tracker.take_dead().is_empty()); | ||
| assert!(!tracker.safe_to_recreate()); | ||
| std::mem::drop(tracked_1); // done for clarity and silence unused var warn | ||
| } | ||
|  | ||
| assert!(tracker.list_ongoing().is_empty()); | ||
| assert!(tracker.safe_to_recreate()); | ||
| assert_eq!(tracker.take_dead(), vec![1]); | ||
| assert!(tracker.safe_to_recreate()); | ||
| } | ||
|  | ||
| #[test] | ||
| fn test_two_tracker() { | ||
| let tracker = Tracker::<u32>::new(); | ||
| let tracker2 = tracker.clone(); | ||
|  | ||
| assert!(tracker.list_ongoing().is_empty()); | ||
| assert!(tracker.take_dead().is_empty()); | ||
| assert!(!tracker.safe_to_recreate()); | ||
|  | ||
| { | ||
| let tracked_1 = tracker.track(1); | ||
| assert_tracked_eq(tracker.list_ongoing(), vec![1]); | ||
| assert_tracked_eq(tracker2.list_ongoing(), vec![1]); | ||
| assert!(tracker.take_dead().is_empty()); | ||
| assert!(tracker2.take_dead().is_empty()); | ||
| assert!(!tracker.safe_to_recreate()); | ||
| std::mem::drop(tracked_1); // done for clarity and silence unused var warn | ||
| } | ||
|  | ||
| assert!(tracker.list_ongoing().is_empty()); | ||
| assert!(tracker2.list_ongoing().is_empty()); | ||
| assert_eq!(tracker2.take_dead(), vec![1]); | ||
| // we took awai the dead from tracker2, so they don't show up in tracker | ||
| assert!(tracker.take_dead().is_empty()); | ||
| } | ||
| } | ||
  
    
      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
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
      
      Oops, something went wrong.
        
    
  
  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.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.