Skip to content
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

Propagate delete events in shared streams #1673

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions kube-runtime/src/reflector/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ where
pub(crate) fn subscribe(&self, reader: Store<K>) -> ReflectHandle<K> {
ReflectHandle::new(reader, self.dispatch_tx.new_receiver())
}

// Return a number of active subscribers to this shared sender.
pub(crate) fn subscribers(&self) -> usize {
self.dispatch_tx.receiver_count()
}
}

/// A handle to a shared stream reader
Expand Down Expand Up @@ -132,10 +137,12 @@ where
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let mut this = self.project();
match ready!(this.rx.as_mut().poll_next(cx)) {
Some(obj_ref) => this
.reader
.get(&obj_ref)
.map_or(Poll::Pending, |obj| Poll::Ready(Some(obj))),
Some(obj_ref) => if obj_ref.extra.remaining_lookups.is_some() {
this.reader.remove(&obj_ref)
} else {
this.reader.get(&obj_ref)
}
.map_or(Poll::Pending, |obj| Poll::Ready(Some(obj))),
None => Poll::Ready(None),
}
}
Expand All @@ -145,8 +152,7 @@ where
#[cfg(test)]
pub(crate) mod test {
use crate::{
watcher::{Error, Event},
WatchStreamExt,
reflector::ObjectRef, watcher::{Error, Event}, WatchStreamExt
};
use std::{pin::pin, sync::Arc, task::Poll};

Expand Down Expand Up @@ -232,16 +238,21 @@ pub(crate) mod test {
let foo = Arc::new(foo);
let _bar = Arc::new(bar);

let (_, writer) = reflector::store_shared(10);
let (reader, writer) = reflector::store_shared(10);
let mut subscriber = pin!(writer.subscribe().unwrap());
let mut other_subscriber = pin!(writer.subscribe().unwrap());
let mut reflect = pin!(st.reflect_shared(writer));

// Deleted events should be skipped by subscriber.
assert!(matches!(
poll!(reflect.next()),
Poll::Ready(Some(Ok(Event::Delete(_))))
));
assert_eq!(poll!(subscriber.next()), Poll::Pending);
assert_eq!(reader.get(&ObjectRef::from_obj(&foo)), Some(foo.clone()));
assert_eq!(poll!(subscriber.next()), Poll::Ready(Some(foo.clone())));
assert_eq!(reader.get(&ObjectRef::from_obj(&foo)), Some(foo.clone()));
assert_eq!(poll!(other_subscriber.next()), Poll::Ready(Some(foo.clone())));
assert_eq!(reader.get(&ObjectRef::from_obj(&foo)), None);

assert!(matches!(
poll!(reflect.next()),
Expand Down
15 changes: 11 additions & 4 deletions kube-runtime/src/reflector/object_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
extra: Extra {
resource_version: self.resource_version().map(Cow::into_owned),
uid: self.uid().map(Cow::into_owned),
remaining_lookups: None,
},
}
}
Expand Down Expand Up @@ -156,6 +157,8 @@
pub resource_version: Option<String>,
/// The uid of the object
pub uid: Option<String>,
/// Number of remaining cache lookups on this reference
pub remaining_lookups: Option<usize>,
}

impl<K: Lookup> ObjectRef<K>
Expand Down Expand Up @@ -225,6 +228,7 @@
extra: Extra {
resource_version: None,
uid: Some(owner.uid.clone()),
remaining_lookups: None,

Check warning on line 231 in kube-runtime/src/reflector/object_ref.rs

View check run for this annotation

Codecov / codecov/patch

kube-runtime/src/reflector/object_ref.rs#L231

Added line #L231 was not covered by tests
},
})
} else {
Expand Down Expand Up @@ -268,10 +272,12 @@
dyntype: dt,
name,
namespace,
extra: Extra {
resource_version,
uid,
},
extra:
Extra {
resource_version,
uid,
..

Check warning on line 279 in kube-runtime/src/reflector/object_ref.rs

View check run for this annotation

Codecov / codecov/patch

kube-runtime/src/reflector/object_ref.rs#L275-L279

Added lines #L275 - L279 were not covered by tests
},
} = val;
ObjectReference {
api_version: Some(K::api_version(&dt).into_owned()),
Expand Down Expand Up @@ -351,6 +357,7 @@
extra: Extra {
resource_version: Some("123".to_string()),
uid: Some("638ffacd-f666-4402-ba10-7848c66ef576".to_string()),
remaining_lookups: None,
},
..minimal.clone()
};
Expand Down
32 changes: 30 additions & 2 deletions kube-runtime/src/reflector/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,14 @@ where
self.store.write().insert(key, obj);
}
watcher::Event::Delete(obj) => {
let key = obj.to_object_ref(self.dyntype.clone());
self.store.write().remove(&key);
let mut key = obj.to_object_ref(self.dyntype.clone());
let mut store = self.store.write();
store.remove(&key);
if self.dispatcher.is_some() {
// Re-insert the entry with updated key, as insert on its own doesnt modify the key
key.extra.remaining_lookups = self.dispatcher.as_ref().map(Dispatcher::subscribers);
store.insert(key, Arc::new(obj.clone()));
}
}
watcher::Event::Init => {
self.buffer = AHashMap::new();
Expand Down Expand Up @@ -159,6 +165,12 @@ where
}
}

watcher::Event::Delete(obj) => {
let mut obj_ref = obj.to_object_ref(self.dyntype.clone());
obj_ref.extra.remaining_lookups = Some(dispatcher.subscribers());
dispatcher.broadcast(obj_ref).await;
}

_ => {}
}
}
Expand Down Expand Up @@ -236,6 +248,22 @@ where
.cloned()
}

#[must_use]
pub fn remove(&self, key: &ObjectRef<K>) -> Option<Arc<K>> {
let mut store = self.store.write();
store.remove_entry(key).map(|(mut key, obj)| {
match key.extra.remaining_lookups {
Some(..=1) | None => (),
Some(lookups) => {
key.extra.remaining_lookups = Some(lookups - 1);
store.insert(key, obj.clone());
}
};

obj
})
}

/// Return a full snapshot of the current values
#[must_use]
pub fn state(&self) -> Vec<Arc<K>> {
Expand Down
Loading