|
| 1 | +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +use vfs::{self, Vfs, Change}; |
| 12 | + |
| 13 | +use std::collections::HashMap; |
| 14 | +use std::mem; |
| 15 | +use std::path::{Path, PathBuf}; |
| 16 | +use std::sync::{Arc, Mutex}; |
| 17 | +use std::thread::{self, Thread}; |
| 18 | +use std::time::Duration; |
| 19 | + |
| 20 | +/// A queue for ensuring that changes happen in version order. |
| 21 | +/// |
| 22 | +/// Assumptions: |
| 23 | +/// * Each change comes on its own thread |
| 24 | +/// * Version numbers are sequential |
| 25 | +/// * Version numbers are per-file and independent |
| 26 | +/// |
| 27 | +/// If a version number is missed, then we will wait for a few seconds and then |
| 28 | +/// panic. The theory is that it is better to burn down the whole RLS than continue |
| 29 | +/// with inconsistent state. |
| 30 | +/// |
| 31 | +/// This is necessary because the RLS spawns a new thread for every message it |
| 32 | +/// is sent. It is possible that a client sends multiple changes in order, but |
| 33 | +/// basically at the same time (this is especially common when 'undo'ing). The |
| 34 | +/// threads would then race to commit the changes to the VFS. This queue serialises |
| 35 | +/// those changes. |
| 36 | +
|
| 37 | +const CHANGE_QUEUE_TIMEOUT: u64 = 5; |
| 38 | + |
| 39 | +// We need a newtype because of public in private warnings :-( |
| 40 | +pub struct ChangeQueue(ChangeQueue_); |
| 41 | + |
| 42 | +impl ChangeQueue { |
| 43 | + pub fn new(vfs: Arc<Vfs>) -> ChangeQueue { |
| 44 | + ChangeQueue(ChangeQueue_::new(VfsSink(vfs))) |
| 45 | + } |
| 46 | + |
| 47 | + pub fn on_changes(&self, file_name: &Path, version: u64, changes: &[Change]) -> Result<(), vfs::Error> { |
| 48 | + self.0.on_changes(file_name, version, changes) |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +struct ChangeQueue_<S = VfsSink> { |
| 53 | + sink: S, |
| 54 | + queues: Mutex<HashMap<PathBuf, Queue>>, |
| 55 | +} |
| 56 | + |
| 57 | +impl<S: ChangeSink> ChangeQueue_<S> { |
| 58 | + fn new(sink: S) -> ChangeQueue_<S> { |
| 59 | + ChangeQueue_ { |
| 60 | + sink, |
| 61 | + queues: Mutex::new(HashMap::new()), |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + pub fn on_changes(&self, file_name: &Path, version: u64, changes: &[Change]) -> Result<(), vfs::Error> { |
| 66 | + trace!("on_changes: {} {:?}", version, changes); |
| 67 | + |
| 68 | + // It is important to hold the lock on self.queues for the whole time |
| 69 | + // from checking the current version until we are done making the change. |
| 70 | + // However, we must drop the lock if our thread suspends so that other |
| 71 | + // threads can make the changes we're blocked waiting for. |
| 72 | + let mut queues = self.queues.lock().unwrap(); |
| 73 | + let cur_version = { |
| 74 | + let queue = queues.entry(file_name.to_owned()).or_insert(Queue::new()); |
| 75 | + queue.cur_version |
| 76 | + }; |
| 77 | + if cur_version.is_some() && Some(version) != cur_version { |
| 78 | + trace!("Blocking change {}, current: {:?}", version, cur_version); |
| 79 | + { |
| 80 | + let mut queue = queues.get_mut(file_name).unwrap(); |
| 81 | + queue.queued.insert(version, thread::current()); |
| 82 | + } |
| 83 | + mem::drop(queues); |
| 84 | + thread::park_timeout(Duration::from_secs(CHANGE_QUEUE_TIMEOUT)); |
| 85 | + |
| 86 | + // We've been woken up - either because our change is next, or the timeout expired. |
| 87 | + queues = self.queues.lock().unwrap(); |
| 88 | + } |
| 89 | + |
| 90 | + let mut queue = queues.get_mut(file_name).unwrap(); |
| 91 | + // Fail if we timed-out rather than our thread was unparked. |
| 92 | + if cur_version.is_some() && Some(version) != queue.cur_version { |
| 93 | + eprintln!("Missing change, aborting. Found {}, expected {:?}", version, queue.cur_version); |
| 94 | + S::on_error(); |
| 95 | + } |
| 96 | + |
| 97 | + queue.commit_change(version, changes, &self.sink) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +struct Queue { |
| 102 | + cur_version: Option<u64>, |
| 103 | + queued: HashMap<u64, Thread>, |
| 104 | +} |
| 105 | + |
| 106 | +impl Queue { |
| 107 | + fn new() -> Queue { |
| 108 | + Queue { |
| 109 | + cur_version: None, |
| 110 | + queued: HashMap::new(), |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + fn commit_change<S: ChangeSink>(&mut self, version: u64, changes: &[Change], sink: &S) -> Result<(), vfs::Error> { |
| 115 | + trace!("commit_change {}, current: {:?}", version, self.cur_version); |
| 116 | + |
| 117 | + let result = sink.change(changes)?; |
| 118 | + let cur_version = version + 1; |
| 119 | + self.cur_version = Some(cur_version); |
| 120 | + |
| 121 | + if let Some(t) = self.queued.remove(&cur_version) { |
| 122 | + trace!("waking up change {}", cur_version); |
| 123 | + t.unpark(); |
| 124 | + } |
| 125 | + |
| 126 | + Ok(result) |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +// A wrapper around the VFS so we can test easily. |
| 131 | +trait ChangeSink { |
| 132 | + // Make a change to the VFS (or mock the change). |
| 133 | + fn change(&self, changes: &[Change]) -> Result<(), vfs::Error>; |
| 134 | + // How to handle a sequencing error. |
| 135 | + fn on_error() -> !; |
| 136 | +} |
| 137 | + |
| 138 | +struct VfsSink(Arc<Vfs>); |
| 139 | + |
| 140 | +impl ChangeSink for VfsSink { |
| 141 | + fn change(&self, changes: &[Change]) -> Result<(), vfs::Error> { |
| 142 | + self.0.on_changes(changes) |
| 143 | + } |
| 144 | + |
| 145 | + // Burn down the whole RLS. |
| 146 | + fn on_error() -> ! { |
| 147 | + ::std::process::abort(); |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +#[cfg(test)] |
| 152 | +mod test { |
| 153 | + use super::*; |
| 154 | + |
| 155 | + use std::sync::{Mutex, Arc}; |
| 156 | + use std::path::PathBuf; |
| 157 | + |
| 158 | + struct TestSink { |
| 159 | + expected: Mutex<HashMap<PathBuf, u64>>, |
| 160 | + } |
| 161 | + |
| 162 | + impl TestSink { |
| 163 | + fn new() -> TestSink { |
| 164 | + TestSink { |
| 165 | + expected: Mutex::new(HashMap::new()), |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + impl ChangeSink for TestSink { |
| 171 | + fn change(&self, changes: &[Change]) -> Result<(), vfs::Error> { |
| 172 | + if let Change::AddFile { ref text, ref file } = changes[0] { |
| 173 | + let index: u64 = text.parse().unwrap(); |
| 174 | + let mut expected = self.expected.lock().unwrap(); |
| 175 | + let expected = expected.entry(file.to_owned()).or_insert(0); |
| 176 | + assert_eq!(*expected, index); |
| 177 | + *expected = index + 1; |
| 178 | + Ok(()) |
| 179 | + } else { |
| 180 | + panic!(); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + fn on_error() -> ! { |
| 185 | + panic!(); |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + #[test] |
| 190 | + fn test_queue_seq() { |
| 191 | + // Sanity test that checks we get the expected behaviour with no threading. |
| 192 | + |
| 193 | + let queue = ChangeQueue_::new(TestSink::new()); |
| 194 | + queue.on_changes(Path::new("foo"), 0, &[Change::AddFile { file: PathBuf::new(), text: "0".to_owned() }]).unwrap(); |
| 195 | + queue.on_changes(Path::new("foo"), 1, &[Change::AddFile { file: PathBuf::new(), text: "1".to_owned() }]).unwrap(); |
| 196 | + queue.on_changes(Path::new("foo"), 2, &[Change::AddFile { file: PathBuf::new(), text: "2".to_owned() }]).unwrap(); |
| 197 | + queue.on_changes(Path::new("foo"), 3, &[Change::AddFile { file: PathBuf::new(), text: "3".to_owned() }]).unwrap(); |
| 198 | + } |
| 199 | + |
| 200 | + #[test] |
| 201 | + fn test_queue_concurrent() { |
| 202 | + let queue = Arc::new(ChangeQueue_::new(TestSink::new())); |
| 203 | + let mut threads = vec![]; |
| 204 | + let foo = Path::new("foo"); |
| 205 | + let bar = Path::new("bar"); |
| 206 | + for i in 3..100 { |
| 207 | + let queue_ = queue.clone(); |
| 208 | + threads.push(thread::spawn(move || { |
| 209 | + queue_.on_changes(foo, i, &[Change::AddFile { file: foo.to_owned(), text: (i-3).to_string() }]).unwrap(); |
| 210 | + })); |
| 211 | + |
| 212 | + let queue_ = queue.clone(); |
| 213 | + threads.push(thread::spawn(move || { |
| 214 | + queue_.on_changes(bar, i, &[Change::AddFile { file: bar.to_owned(), text: (i-3).to_string() }]).unwrap(); |
| 215 | + })); |
| 216 | + } |
| 217 | + |
| 218 | + for h in threads { |
| 219 | + h.join().unwrap(); |
| 220 | + } |
| 221 | + } |
| 222 | + |
| 223 | + #[test] |
| 224 | + #[should_panic] |
| 225 | + fn test_queue_skip() { |
| 226 | + // Skip a change - the queue should panic rather than loop forever. |
| 227 | + let queue = Arc::new(ChangeQueue_::new(TestSink::new())); |
| 228 | + let mut threads = vec![]; |
| 229 | + for i in 0..100 { |
| 230 | + if i == 45 { |
| 231 | + continue; |
| 232 | + } |
| 233 | + let queue = queue.clone(); |
| 234 | + threads.push(thread::spawn(move || { |
| 235 | + queue.on_changes(Path::new("foo"), i, &[Change::AddFile { file: PathBuf::new(), text: i.to_string() }]).unwrap(); |
| 236 | + })); |
| 237 | + } |
| 238 | + |
| 239 | + for h in threads { |
| 240 | + h.join().unwrap(); |
| 241 | + } |
| 242 | + } |
| 243 | +} |
0 commit comments