prometheus-client-mmap 0.19.1 → 0.20.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,190 @@
1
+ use magnus::exception::*;
2
+ use magnus::{Error, RString, StaticSymbol, Symbol, Value};
3
+ use std::ffi::OsString;
4
+ use std::fs::File;
5
+ use std::io::{self, Read, Seek};
6
+ use std::os::unix::ffi::OsStringExt;
7
+ use std::path::PathBuf;
8
+
9
+ use crate::err;
10
+ use crate::error::{MmapError, RubyError};
11
+ use crate::util;
12
+ use crate::Result;
13
+
14
+ /// The details of a `*.db` file.
15
+ #[derive(Debug)]
16
+ pub struct FileInfo {
17
+ pub file: File,
18
+ pub path: PathBuf,
19
+ pub len: usize,
20
+ pub multiprocess_mode: Symbol,
21
+ pub type_: StaticSymbol,
22
+ pub pid: String,
23
+ }
24
+
25
+ impl FileInfo {
26
+ /// Receive the details of a file from Ruby and store as a `FileInfo`.
27
+ pub fn open_from_params(params: &[Value; 4]) -> magnus::error::Result<Self> {
28
+ if params.len() != 4 {
29
+ return Err(err!(
30
+ arg_error(),
31
+ "wrong number of arguments {} instead of 4",
32
+ params.len()
33
+ ));
34
+ }
35
+
36
+ let filepath = RString::from_value(params[0])
37
+ .ok_or_else(|| err!(arg_error(), "can't convert filepath to String"))?;
38
+
39
+ // SAFETY: We immediately copy the string buffer from Ruby, preventing
40
+ // it from being mutated out from under us.
41
+ let path_bytes: Vec<_> = unsafe { filepath.as_slice().to_owned() };
42
+ let path = PathBuf::from(OsString::from_vec(path_bytes));
43
+
44
+ let mut file = File::open(&path).map_err(|_| {
45
+ err!(
46
+ arg_error(),
47
+ "Can't open {}, errno: {}",
48
+ path.display(),
49
+ util::errno()
50
+ )
51
+ })?;
52
+
53
+ let stat = file
54
+ .metadata()
55
+ .map_err(|_| err!(io_error(), "Can't stat file, errno: {}", util::errno()))?;
56
+
57
+ let length = util::cast_chk::<_, usize>(stat.len(), "file size")?;
58
+
59
+ let multiprocess_mode = Symbol::from_value(params[1])
60
+ .ok_or_else(|| err!(arg_error(), "expected multiprocess_mode to be a symbol"))?;
61
+
62
+ let type_ = StaticSymbol::from_value(params[2])
63
+ .ok_or_else(|| err!(arg_error(), "expected file type to be a symbol"))?;
64
+
65
+ let pid = RString::from_value(params[3])
66
+ .ok_or_else(|| err!(arg_error(), "expected pid to be a String"))?;
67
+
68
+ file.rewind()
69
+ .map_err(|_| err!(io_error(), "Can't fseek 0, errno: {}", util::errno()))?;
70
+
71
+ Ok(Self {
72
+ file,
73
+ path,
74
+ len: length,
75
+ multiprocess_mode,
76
+ type_,
77
+ pid: pid.to_string()?,
78
+ })
79
+ }
80
+
81
+ /// Read the contents of the associated file into the buffer provided by
82
+ /// the caller.
83
+ pub fn read_from_file(&mut self, buf: &mut Vec<u8>) -> Result<()> {
84
+ buf.clear();
85
+ buf.try_reserve(self.len).map_err(|_| {
86
+ MmapError::legacy(
87
+ format!("Can't malloc {}, errno: {}", self.len, util::errno()),
88
+ RubyError::Io,
89
+ )
90
+ })?;
91
+
92
+ match self.file.read_to_end(buf) {
93
+ Ok(n) if n == self.len => Ok(()),
94
+ Ok(_) => Err(MmapError::io(
95
+ "read",
96
+ &self.path,
97
+ io::Error::from(io::ErrorKind::UnexpectedEof),
98
+ )),
99
+ Err(e) => Err(MmapError::io("read", &self.path, e)),
100
+ }
101
+ }
102
+ }
103
+
104
+ #[cfg(test)]
105
+ mod test {
106
+ use magnus::{eval, RArray, StaticSymbol, Symbol};
107
+ use rand::{thread_rng, Rng};
108
+ use sha2::{Digest, Sha256};
109
+
110
+ use super::*;
111
+ use crate::testhelper::TestFile;
112
+
113
+ #[test]
114
+ fn test_open_from_params() {
115
+ let _cleanup = unsafe { magnus::embed::init() };
116
+ let ruby = magnus::Ruby::get().unwrap();
117
+ crate::init(&ruby).unwrap();
118
+
119
+ let file_data = b"foobar";
120
+ let TestFile {
121
+ file: _file,
122
+ path,
123
+ dir: _dir,
124
+ } = TestFile::new(file_data);
125
+
126
+ let pid = "worker-1_0";
127
+ let args = RArray::from_value(
128
+ eval(&format!("['{}', :max, :gauge, '{pid}']", path.display())).unwrap(),
129
+ )
130
+ .unwrap();
131
+ let arg0 = args.shift().unwrap();
132
+ let arg1 = args.shift().unwrap();
133
+ let arg2 = args.shift().unwrap();
134
+ let arg3 = args.shift().unwrap();
135
+
136
+ let out = FileInfo::open_from_params(&[arg0, arg1, arg2, arg3]);
137
+ assert!(out.is_ok());
138
+
139
+ let out = out.unwrap();
140
+
141
+ assert_eq!(out.path, path);
142
+ assert_eq!(out.len, file_data.len());
143
+ assert_eq!(out.multiprocess_mode, Symbol::new("max"));
144
+ assert_eq!(out.type_, Symbol::new("gauge"));
145
+ assert_eq!(out.pid, pid);
146
+ }
147
+
148
+ #[test]
149
+ fn test_read_from_file() {
150
+ let _cleanup = unsafe { magnus::embed::init() };
151
+ let ruby = magnus::Ruby::get().unwrap();
152
+ crate::init(&ruby).unwrap();
153
+
154
+ const BUF_LEN: usize = 1 << 20; // 1MiB
155
+
156
+ // Create a buffer with random data.
157
+ let mut buf = vec![0u8; BUF_LEN];
158
+ thread_rng().fill(buf.as_mut_slice());
159
+
160
+ let TestFile {
161
+ file,
162
+ path,
163
+ dir: _dir,
164
+ } = TestFile::new(&buf);
165
+
166
+ let mut info = FileInfo {
167
+ file,
168
+ path,
169
+ len: buf.len(),
170
+ multiprocess_mode: Symbol::new("puma"),
171
+ type_: StaticSymbol::new("max"),
172
+ pid: "worker-0_0".to_string(),
173
+ };
174
+
175
+ let mut out_buf = Vec::new();
176
+ info.read_from_file(&mut out_buf).unwrap();
177
+
178
+ assert_eq!(buf.len(), out_buf.len(), "buffer lens");
179
+
180
+ let mut in_hasher = Sha256::new();
181
+ in_hasher.update(&buf);
182
+ let in_hash = in_hasher.finalize();
183
+
184
+ let mut out_hasher = Sha256::new();
185
+ out_hasher.update(&out_buf);
186
+ let out_hash = out_hasher.finalize();
187
+
188
+ assert_eq!(in_hash, out_hash, "content hashes");
189
+ }
190
+ }
@@ -0,0 +1,79 @@
1
+ use magnus::exception::*;
2
+ use magnus::prelude::*;
3
+ use magnus::value::{Fixnum, Lazy, LazyId};
4
+ use magnus::{class, define_class, exception, function, method, Ruby};
5
+ use std::mem::size_of;
6
+
7
+ use crate::mmap::MmapedFile;
8
+
9
+ pub mod error;
10
+ pub mod file_entry;
11
+ pub mod file_info;
12
+ mod macros;
13
+ pub mod map;
14
+ pub mod mmap;
15
+ pub mod parser;
16
+ pub mod raw_entry;
17
+ pub mod util;
18
+
19
+ #[cfg(test)]
20
+ mod testhelper;
21
+
22
+ type Result<T> = std::result::Result<T, crate::error::MmapError>;
23
+
24
+ const MAP_SHARED: i64 = libc::MAP_SHARED as i64;
25
+ const HEADER_SIZE: usize = 2 * size_of::<u32>();
26
+
27
+ static SYM_GAUGE: LazyId = LazyId::new("gauge");
28
+ static SYM_MIN: LazyId = LazyId::new("min");
29
+ static SYM_MAX: LazyId = LazyId::new("max");
30
+ static SYM_LIVESUM: LazyId = LazyId::new("livesum");
31
+ static SYM_PID: LazyId = LazyId::new("pid");
32
+ static SYM_SAMPLES: LazyId = LazyId::new("samples");
33
+
34
+ static PROM_EPARSING_ERROR: Lazy<ExceptionClass> = Lazy::new(|_| {
35
+ let prom_err = define_class(
36
+ "PrometheusParsingError",
37
+ exception::runtime_error().as_r_class(),
38
+ )
39
+ .expect("failed to create class `PrometheusParsingError`");
40
+ ExceptionClass::from_value(prom_err.as_value())
41
+ .expect("failed to create exception class from `PrometheusParsingError`")
42
+ });
43
+
44
+ #[magnus::init]
45
+ fn init(ruby: &Ruby) -> magnus::error::Result<()> {
46
+ // Initialize the static symbols
47
+ LazyId::force(&SYM_GAUGE, ruby);
48
+ LazyId::force(&SYM_MIN, ruby);
49
+ LazyId::force(&SYM_MAX, ruby);
50
+ LazyId::force(&SYM_LIVESUM, ruby);
51
+ LazyId::force(&SYM_PID, ruby);
52
+ LazyId::force(&SYM_SAMPLES, ruby);
53
+
54
+ // Initialize `PrometheusParsingError` class.
55
+ Lazy::force(&PROM_EPARSING_ERROR, ruby);
56
+
57
+ let klass = define_class("FastMmapedFileRs", class::object())?;
58
+ klass.undef_default_alloc_func();
59
+
60
+ // UNWRAP: We know `MAP_SHARED` fits in a `Fixnum`.
61
+ klass.const_set("MAP_SHARED", Fixnum::from_i64(MAP_SHARED).unwrap())?;
62
+
63
+ klass.define_singleton_method("to_metrics", function!(MmapedFile::to_metrics, 1))?;
64
+
65
+ // Required for subclassing to work
66
+ klass.define_alloc_func::<MmapedFile>();
67
+ klass.define_singleton_method("new", method!(MmapedFile::new, -1))?;
68
+ klass.define_method("initialize", method!(MmapedFile::initialize, 1))?;
69
+ klass.define_method("slice", method!(MmapedFile::slice, -1))?;
70
+ klass.define_method("sync", method!(MmapedFile::sync, -1))?;
71
+ klass.define_method("munmap", method!(MmapedFile::munmap, 0))?;
72
+
73
+ klass.define_method("used", method!(MmapedFile::load_used, 0))?;
74
+ klass.define_method("used=", method!(MmapedFile::save_used, 1))?;
75
+ klass.define_method("fetch_entry", method!(MmapedFile::fetch_entry, 3))?;
76
+ klass.define_method("upsert_entry", method!(MmapedFile::upsert_entry, 3))?;
77
+
78
+ Ok(())
79
+ }
@@ -0,0 +1,14 @@
1
+ #[macro_export]
2
+ macro_rules! err {
3
+ (with_errno: $err_t:expr, $($arg:expr),*) => {
4
+ {
5
+ let err = format!($($arg),*);
6
+ let strerror = strerror(errno());
7
+ Error::new($err_t, format!("{err} ({strerror})"))
8
+ }
9
+ };
10
+
11
+ ($err_t:expr, $($arg:expr),*) => {
12
+ Error::new($err_t, format!($($arg),*))
13
+ };
14
+ }