Преглед изворни кода

Refactor snapshot holder and merge methods.

Jing Yang пре 4 година
родитељ
комит
7b7857fd9c
2 измењених фајлова са 12 додато и 25 уклоњено
  1. 0 1
      kvraft/src/server.rs
  2. 12 24
      kvraft/src/snapshot_holder.rs

+ 0 - 1
kvraft/src/server.rs

@@ -217,7 +217,6 @@ impl KVServer {
                                 .take_snapshot(&this.state.lock(), index)
                                 .take_snapshot(&this.state.lock(), index)
                             {
                             {
                                 this.rf.lock().save_snapshot(snapshot);
                                 this.rf.lock().save_snapshot(snapshot);
-                                snapshot_holder.unblock_response(index);
                             }
                             }
                         }
                         }
                     }
                     }

+ 12 - 24
kvraft/src/snapshot_holder.rs

@@ -24,34 +24,22 @@ impl<T> SnapshotHolder<T> {
 
 
 impl<T: Serialize> SnapshotHolder<T> {
 impl<T: Serialize> SnapshotHolder<T> {
     pub fn take_snapshot(&self, state: &T, curr: usize) -> Option<Snapshot> {
     pub fn take_snapshot(&self, state: &T, curr: usize) -> Option<Snapshot> {
-        let requested = self
-            .snapshot_requests
-            .lock()
-            .first()
-            .map_or(false, |&min_index| min_index <= curr);
+        let mut requests = self.snapshot_requests.lock();
 
 
-        if requested {
-            let data = bincode::serialize(state)
-                .expect("Serialization should never fail.");
-            return Some(Snapshot {
-                data,
-                last_included_index: curr,
-            });
+        let processed = requests.partition_point(|index| *index <= curr);
+        if processed == 0 {
+            return None;
         }
         }
-        None
-    }
 
 
-    pub fn unblock_response(&self, curr: usize) {
-        let mut requests = self.snapshot_requests.lock();
-        let mut processed = 0;
-        for &index in requests.iter() {
-            if index <= curr {
-                processed += 1;
-            } else {
-                break;
-            }
-        }
         requests.drain(0..processed);
         requests.drain(0..processed);
+        drop(requests);
+
+        let data = bincode::serialize(state)
+            .expect("Serialization should never fail.");
+        return Some(Snapshot {
+            data,
+            last_included_index: curr,
+        });
     }
     }
 }
 }