Skip to content

Commit cdbbbdd

Browse files
feat: alternate backing stores for parquet cache
Experimental safe and unsafe lru impls, that can replace current `cache` which is backed by dashmap.
1 parent 23b7794 commit cdbbbdd

File tree

7 files changed

+421
-1
lines changed

7 files changed

+421
-1
lines changed

Cargo.lock

Lines changed: 12 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,15 @@ flate2 = "1.0.27"
7575
futures = "0.3.31"
7676
futures-util = "0.3.31"
7777
hashbrown = { version = "0.15.1", features = ["serde"] }
78+
hashlink = "0.10.0"
7879
hex = "0.4.3"
7980
http = "0.2.9"
8081
humantime = "2.1.0"
8182
hyper = "0.14"
8283
insta = { version = "1.39", features = ["json", "redactions", "yaml"] }
8384
indexmap = { version = "2.2.6" }
8485
itertools = "0.13.0"
86+
jemalloc_pprof = "0.6.0"
8587
libc = { version = "0.2" }
8688
mime = "0.3.17"
8789
mockito = { version = "1.4.0", default-features = false }

influxdb3_cache/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ chrono.workspace = true
2727
dashmap.workspace = true
2828
datafusion.workspace = true
2929
futures.workspace = true
30+
hashbrown.workspace = true
31+
hashlink.workspace = true
3032
indexmap.workspace = true
3133
parking_lot.workspace = true
3234
object_store.workspace = true
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub(crate) mod safe_lru;
2+
pub(crate) mod unsafe_lru_parquet_cache;
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
use std::sync::Arc;
2+
3+
use bytes::Bytes;
4+
use hashlink::LinkedHashMap;
5+
use observability_deps::tracing::debug;
6+
7+
#[allow(dead_code)]
8+
struct LruCache {
9+
map: LinkedHashMap<Arc<str>, Bytes>,
10+
max_capacity_bytes: u64,
11+
curr_capacity_bytes: u64,
12+
}
13+
14+
#[allow(dead_code)]
15+
impl LruCache {
16+
pub(crate) fn new(capacity_bytes: u64) -> Self {
17+
LruCache {
18+
map: LinkedHashMap::new(),
19+
max_capacity_bytes: capacity_bytes,
20+
curr_capacity_bytes: 0,
21+
}
22+
}
23+
24+
pub(crate) fn put(&mut self, key: Arc<str>, val: Bytes) {
25+
let new_val_size = val.len() as u64;
26+
if self.curr_capacity_bytes + new_val_size > self.max_capacity_bytes {
27+
let mut to_deduct = self.curr_capacity_bytes + new_val_size - self.max_capacity_bytes;
28+
while to_deduct > 0 && !self.map.is_empty() {
29+
// need to drop elements
30+
if let Some((popped_key, popped_val)) = self.map.pop_front() {
31+
debug!(?popped_key, ">>> removed key from parquet cache to reclaim space");
32+
to_deduct -= popped_val.len() as u64;
33+
}
34+
}
35+
}
36+
// at this point there should be enough space to add new val
37+
self.curr_capacity_bytes += val.len() as u64;
38+
self.map.insert(key, val);
39+
}
40+
41+
pub(crate) fn get(&mut self, key: Arc<str>) -> Option<Bytes> {
42+
if let Some(val) = self.map.get(&key).cloned() {
43+
self.map.to_back(&key);
44+
return Some(val);
45+
}
46+
None
47+
}
48+
}
49+
50+
#[cfg(test)]
51+
mod tests {
52+
use std::sync::Arc;
53+
54+
use bytes::Bytes;
55+
use observability_deps::tracing::debug;
56+
57+
use crate::parquet_cache::experimental::safe_lru::LruCache;
58+
59+
60+
#[test_log::test(test)]
61+
fn test_safe_lru() {
62+
let mut cache = LruCache::new(100);
63+
let key_1 = Arc::from("/some/path_1");
64+
cache.put(Arc::clone(&key_1), Bytes::from_static(b"hello"));
65+
debug!("Running test");
66+
let key_2 = Arc::from("/some/path_2");
67+
let text_2 = Bytes::from_static(
68+
r#"
69+
Lorem Ipsum
70+
"Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit..."
71+
"There is no one who loves pain itself, who seeks after it and wants to have it, simply because it is pain..."
72+
What is Lorem Ipsum?
73+
74+
Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
75+
Why do we use it?
76+
77+
It is a long established fact that a reader will be distracted by the readable content of a page when looking at its layout. The point of using Lorem Ipsum is that it has a more-or-less normal distribution of letters, as opposed to using 'Content here, content here', making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still in their infancy. Various versions have evolved over the years, sometimes by accident, sometimes on purpose (injected humour and the like).
78+
79+
Where does it come from?
80+
81+
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.
82+
83+
The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.
84+
Where can I get some?
85+
86+
There are many variations of passages of Lorem Ipsum available, but the majority have suffered alteration in some form, by injected humour, or randomised words which don't look even slightly believable. If you are going to use a passage of Lorem Ipsum, you need to be sure there isn't anything embarrassing hidden in the middle of text. All the Lorem Ipsum generators on the Internet tend to repeat predefined chunks as necessary, making this the first true generator on the Internet. It uses a dictionary of over 200 Latin words, combined with a handful of model sentence structures, to generate Lorem Ipsum which looks reasonable. The generated Lorem Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.
87+
88+
paragraphs
89+
words
90+
bytes
91+
lists
92+
Start with 'Lorem
93+
ipsum dolor sit amet...'
94+
95+
96+
Donate: If you use this site regularly and would like to help keep the site on the Internet, please consider donating a small sum to help pay for the hosting and bandwidth bill. There is no minimum donation, any sum is appreciated - click here to donate using PayPal. Thank you for your support. Donate bitcoin: 16UQLq1HZ3CNwhvgrarV6pMoA2CDjb4tyF
97+
Translations: Can you help translate this site into a foreign language ? Please email us with details if you can help.
98+
There is a set of mock banners available here in three colours and in a range of standard banner sizes:
99+
BannersBannersBanners
100+
NodeJS Python Interface GTK Lipsum Rails .NET
101+
The standard Lorem Ipsum passage, used since the 1500s
102+
103+
"Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
104+
Section 1.10.32 of "de Finibus Bonorum et Malorum", written by Cicero in 45 BC
105+
106+
"Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?"
107+
1914 translation by H. Rackham
108+
109+
"But I must explain to you how all this mistaken idea of denouncing pleasure and praising pain was born and I will give you a complete account of the system, and expound the actual teachings of the great explorer of the truth, the master-builder of human happiness. No one rejects, dislikes, or avoids pleasure itself, because it is pleasure, but because those who do not know how to pursue pleasure rationally encounter consequences that are extremely painful. Nor again is there anyone who loves or pursues or desires to obtain pain of itself, because it is pain, but because occasionally circumstances occur in which toil and pain can procure him some great pleasure. To take a trivial example, which of us ever undertakes laborious physical exercise, except to obtain some advantage from it? But who has any right to find fault with a man who chooses to enjoy a pleasure that has no annoying consequences, or one who avoids a pain that produces no resultant pleasure?"
110+
Section 1.10.33 of "de Finibus Bonorum et Malorum", written by Cicero in 45 BC
111+
112+
"At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat."
113+
1914 translation by H. Rackham
114+
115+
"On the other hand, we denounce with righteous indignation and dislike men who are so beguiled and demoralized by the charms of pleasure of the moment, so blinded by desire, that they cannot foresee the pain and trouble that are bound to ensue; and equal blame belongs to those who fail in their duty through weakness of will, which is the same as saying through shrinking from toil and pain. These cases are perfectly simple and easy to distinguish. In a free hour, when our power of choice is untrammelled and when nothing prevents our being able to do what we like best, every pleasure is to be welcomed and every pain avoided. But in certain circumstances and owing to the claims of duty or the obligations of business it will frequently occur that pleasures have to be repudiated and annoyances accepted. The wise man therefore always holds in these matters to this principle of selection: he rejects pleasures to secure other greater pleasures, or else he endures pains to avoid worse pains."
116+
117+
Privacy Policy ·"#.as_bytes()
118+
);
119+
debug!("Running test 2");
120+
cache.put(Arc::clone(&key_2), text_2);
121+
let val = cache.get(Arc::clone(&key_1));
122+
123+
debug!("Running test 3");
124+
debug!(?val, ">>> from get");
125+
let val = cache.get(Arc::clone(&key_2));
126+
debug!(?val, ">>> from get");
127+
128+
let val = cache.get(Arc::clone(&key_1));
129+
// this should be none
130+
debug!(?val, ">>> from get");
131+
}
132+
133+
}

0 commit comments

Comments
 (0)