forked from TraceMachina/nativelink
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.rs
More file actions
472 lines (424 loc) · 14.8 KB
/
common.rs
File metadata and controls
472 lines (424 loc) · 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
// Copyright 2024 The NativeLink Authors. All rights reserved.
//
// Licensed under the Functional Source License, Version 1.1, Apache 2.0 Future License (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// See LICENSE file for details
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use core::cmp::{Eq, Ordering};
use core::hash::{BuildHasher, Hash};
use core::ops::{Deref, DerefMut};
use std::collections::HashMap;
use std::fmt;
use std::io::{Cursor, Write};
use bytes::{Buf, BufMut, Bytes, BytesMut};
use nativelink_error::{Error, ResultExt, make_input_err};
use nativelink_metric::{
MetricFieldData, MetricKind, MetricPublishKnownKindData, MetricsComponent,
};
use nativelink_proto::build::bazel::remote::execution::v2::Digest;
use prost::Message;
use serde::de::Visitor;
use serde::ser::Error as _;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use tracing::error;
pub use crate::fs;
#[derive(Default, Clone, Copy, Eq, PartialEq, Hash)]
#[repr(C)]
pub struct DigestInfo {
/// Raw hash in packed form.
packed_hash: PackedHash,
/// Possibly the size of the digest in bytes.
size_bytes: u64,
}
impl MetricsComponent for DigestInfo {
fn publish(
&self,
_kind: MetricKind,
field_metadata: MetricFieldData,
) -> Result<MetricPublishKnownKindData, nativelink_metric::Error> {
format!("{self}").publish(MetricKind::String, field_metadata)
}
}
impl DigestInfo {
pub const fn new(packed_hash: [u8; 32], size_bytes: u64) -> Self {
Self {
size_bytes,
packed_hash: PackedHash(packed_hash),
}
}
pub fn try_new<T>(hash: &str, size_bytes: T) -> Result<Self, Error>
where
T: TryInto<u64> + fmt::Display + Copy,
{
let packed_hash =
PackedHash::from_hex(hash).err_tip(|| format!("Invalid sha256 hash: {hash}"))?;
let size_bytes = size_bytes
.try_into()
.map_err(|_| make_input_err!("Could not convert {} into u64", size_bytes))?;
// The proto `Digest` takes an i64, so to keep compatibility
// we only allow sizes that can fit into an i64.
if size_bytes > i64::MAX as u64 {
return Err(make_input_err!(
"Size bytes is too large: {} - max: {}",
size_bytes,
i64::MAX
));
}
Ok(Self {
packed_hash,
size_bytes,
})
}
pub const fn zero_digest() -> Self {
Self {
size_bytes: 0,
packed_hash: PackedHash::new(),
}
}
pub const fn packed_hash(&self) -> &PackedHash {
&self.packed_hash
}
pub const fn set_packed_hash(&mut self, packed_hash: [u8; 32]) {
self.packed_hash = PackedHash(packed_hash);
}
pub const fn size_bytes(&self) -> u64 {
self.size_bytes
}
/// Returns a struct that can turn the `DigestInfo` into a string.
const fn stringifier(&self) -> DigestStackStringifier<'_> {
DigestStackStringifier::new(self)
}
}
/// Counts the number of digits a number needs if it were to be
/// converted to a string.
const fn count_digits(mut num: u64) -> usize {
let mut count = 0;
while num != 0 {
count += 1;
num /= 10;
}
count
}
/// An optimized version of a function that can convert a `DigestInfo`
/// into a str on the stack.
struct DigestStackStringifier<'a> {
digest: &'a DigestInfo,
/// Buffer that can hold the string representation of the `DigestInfo`.
/// - Hex is '2 * sizeof(PackedHash)'.
/// - Digits can be at most `count_digits(u64::MAX)`.
/// - We also have a hyphen separator.
buf: [u8; size_of::<PackedHash>() * 2 + count_digits(u64::MAX) + 1],
}
impl<'a> DigestStackStringifier<'a> {
const fn new(digest: &'a DigestInfo) -> Self {
DigestStackStringifier {
digest,
buf: [b'-'; size_of::<PackedHash>() * 2 + count_digits(u64::MAX) + 1],
}
}
fn as_str(&mut self) -> Result<&str, Error> {
// Populate the buffer and return the amount of bytes written
// to the buffer.
let len = {
let mut cursor = Cursor::new(&mut self.buf[..]);
let hex = self.digest.packed_hash.to_hex().map_err(|e| {
make_input_err!(
"Could not convert PackedHash to hex - {e:?} - {:?}",
self.digest
)
})?;
cursor
.write_all(&hex)
.err_tip(|| format!("Could not write hex to buffer - {hex:?} - {hex:?}",))?;
// Note: We already have a hyphen at this point because we
// initialized the buffer with hyphens.
cursor.advance(1);
cursor
.write_fmt(format_args!("{}", self.digest.size_bytes()))
.err_tip(|| format!("Could not write size_bytes to buffer - {hex:?}",))?;
cursor.position() as usize
};
// Convert the buffer into utf8 string.
core::str::from_utf8(&self.buf[..len]).map_err(|e| {
make_input_err!(
"Could not convert [u8] to string - {} - {:?} - {:?}",
self.digest,
self.buf,
e,
)
})
}
}
/// Custom serializer for `DigestInfo` because the default Serializer
/// would try to encode the data as a byte array, but we use {hex}-{size}.
impl Serialize for DigestInfo {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut stringifier = self.stringifier();
serializer.serialize_str(
stringifier
.as_str()
.err_tip(|| "During serialization of DigestInfo")
.map_err(S::Error::custom)?,
)
}
}
/// Custom deserializer for `DigestInfo` because the default Deserializer
/// would try to decode the data as a byte array, but we use {hex}-{size}.
impl<'de> Deserialize<'de> for DigestInfo {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct DigestInfoVisitor;
impl Visitor<'_> for DigestInfoVisitor {
type Value = DigestInfo;
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("a string representing a DigestInfo")
}
fn visit_str<E>(self, s: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
let Some((hash, size)) = s.split_once('-') else {
return Err(E::custom(
"Invalid DigestInfo format, expected '-' separator",
));
};
let size_bytes = size
.parse::<u64>()
.map_err(|e| E::custom(format!("Could not parse size_bytes: {e:?}")))?;
DigestInfo::try_new(hash, size_bytes)
.map_err(|e| E::custom(format!("Could not create DigestInfo: {e:?}")))
}
}
deserializer.deserialize_str(DigestInfoVisitor)
}
}
impl fmt::Display for DigestInfo {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut stringifier = self.stringifier();
f.write_str(
stringifier
.as_str()
.err_tip(|| "During serialization of DigestInfo")
.map_err(|e| {
error!("Could not convert DigestInfo to string - {e:?}");
fmt::Error
})?,
)
}
}
impl fmt::Debug for DigestInfo {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut stringifier = self.stringifier();
match stringifier.as_str() {
Ok(s) => f.debug_tuple("DigestInfo").field(&s).finish(),
Err(e) => {
error!("Could not convert DigestInfo to string - {e:?}");
Err(fmt::Error)
}
}
}
}
impl Ord for DigestInfo {
fn cmp(&self, other: &Self) -> Ordering {
self.packed_hash
.cmp(&other.packed_hash)
.then_with(|| self.size_bytes.cmp(&other.size_bytes))
}
}
impl PartialOrd for DigestInfo {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl TryFrom<Digest> for DigestInfo {
type Error = Error;
fn try_from(digest: Digest) -> Result<Self, Self::Error> {
let packed_hash = PackedHash::from_hex(&digest.hash)
.err_tip(|| format!("Invalid sha256 hash: {}", digest.hash))?;
let size_bytes = digest
.size_bytes
.try_into()
.map_err(|_| make_input_err!("Could not convert {} into u64", digest.size_bytes))?;
Ok(Self {
packed_hash,
size_bytes,
})
}
}
impl TryFrom<&Digest> for DigestInfo {
type Error = Error;
fn try_from(digest: &Digest) -> Result<Self, Self::Error> {
let packed_hash = PackedHash::from_hex(&digest.hash)
.err_tip(|| format!("Invalid sha256 hash: {}", digest.hash))?;
let size_bytes = digest
.size_bytes
.try_into()
.map_err(|_| make_input_err!("Could not convert {} into u64", digest.size_bytes))?;
Ok(Self {
packed_hash,
size_bytes,
})
}
}
impl From<DigestInfo> for Digest {
fn from(val: DigestInfo) -> Self {
Self {
hash: val.packed_hash.to_string(),
size_bytes: val.size_bytes.try_into().unwrap_or_else(|e| {
error!("Could not convert {} into u64 - {e:?}", val.size_bytes);
// This is a placeholder value that can help a user identify
// that the conversion failed.
-255
}),
}
}
}
impl From<&DigestInfo> for Digest {
fn from(val: &DigestInfo) -> Self {
Self {
hash: val.packed_hash.to_string(),
size_bytes: val.size_bytes.try_into().unwrap_or_else(|e| {
error!("Could not convert {} into u64 - {e:?}", val.size_bytes);
// This is a placeholder value that can help a user identify
// that the conversion failed.
-255
}),
}
}
}
#[derive(
Debug, Serialize, Deserialize, Default, Clone, Copy, Eq, PartialEq, Hash, PartialOrd, Ord,
)]
pub struct PackedHash([u8; 32]);
const SIZE_OF_PACKED_HASH: usize = 32;
impl PackedHash {
const fn new() -> Self {
Self([0; SIZE_OF_PACKED_HASH])
}
fn from_hex(hash: &str) -> Result<Self, Error> {
let mut packed_hash = [0u8; 32];
hex::decode_to_slice(hash, &mut packed_hash)
.map_err(|e| make_input_err!("Invalid sha256 hash: {hash} - {e:?}"))?;
Ok(Self(packed_hash))
}
/// Converts the packed hash into a hex string.
#[inline]
fn to_hex(self) -> Result<[u8; SIZE_OF_PACKED_HASH * 2], fmt::Error> {
let mut hash = [0u8; SIZE_OF_PACKED_HASH * 2];
hex::encode_to_slice(self.0, &mut hash).map_err(|e| {
error!("Could not convert PackedHash to hex - {e:?} - {:?}", self.0);
fmt::Error
})?;
Ok(hash)
}
}
impl fmt::Display for PackedHash {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let hash = self.to_hex()?;
match core::str::from_utf8(&hash) {
Ok(hash) => f.write_str(hash)?,
Err(_) => f.write_str(&format!("Could not convert hash to utf8 {:?}", self.0))?,
}
Ok(())
}
}
impl Deref for PackedHash {
type Target = [u8; 32];
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for PackedHash {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
// Simple utility trait that makes it easier to apply `.try_map` to Vec.
// This will convert one vector into another vector with a different type.
pub trait VecExt<T> {
fn try_map<F, U>(self, f: F) -> Result<Vec<U>, Error>
where
Self: Sized,
F: (Fn(T) -> Result<U, Error>) + Sized;
}
impl<T> VecExt<T> for Vec<T> {
fn try_map<F, U>(self, f: F) -> Result<Vec<U>, Error>
where
Self: Sized,
F: (Fn(T) -> Result<U, Error>) + Sized,
{
let mut output = Vec::with_capacity(self.len());
for item in self {
output.push((f)(item)?);
}
Ok(output)
}
}
// Simple utility trait that makes it easier to apply `.try_map` to HashMap.
// This will convert one HashMap into another keeping the key the same, but
// different value type.
pub trait HashMapExt<K: Eq + Hash, T, S: BuildHasher> {
fn try_map<F, U>(self, f: F) -> Result<HashMap<K, U, S>, Error>
where
Self: Sized,
F: (Fn(T) -> Result<U, Error>) + Sized;
}
impl<K: Eq + Hash, T, S: BuildHasher + Clone> HashMapExt<K, T, S> for HashMap<K, T, S> {
fn try_map<F, U>(self, f: F) -> Result<HashMap<K, U, S>, Error>
where
Self: Sized,
F: (Fn(T) -> Result<U, Error>) + Sized,
{
let mut output = HashMap::with_capacity_and_hasher(self.len(), (*self.hasher()).clone());
for (k, v) in self {
output.insert(k, (f)(v)?);
}
Ok(output)
}
}
// Utility to encode our proto into GRPC stream format.
pub fn encode_stream_proto<T: Message>(proto: &T) -> Result<Bytes, Box<dyn core::error::Error>> {
// See below comment on spec.
use core::mem::size_of;
const PREFIX_BYTES: usize = size_of::<u8>() + size_of::<u32>();
let mut buf = BytesMut::new();
for _ in 0..PREFIX_BYTES {
// Advance our buffer first.
// We will backfill it once we know the size of the message.
buf.put_u8(0);
}
proto.encode(&mut buf)?;
let len = buf.len() - PREFIX_BYTES;
{
let mut buf = &mut buf[0..PREFIX_BYTES];
// See: https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md#:~:text=Compressed-Flag
// for more details on spec.
// Compressed-Flag -> 0 / 1 # encoded as 1 byte unsigned integer.
buf.put_u8(0);
// Message-Length -> {length of Message} # encoded as 4 byte unsigned integer (big endian).
buf.put_u32(len as u32);
// Message -> *{binary octet}.
}
Ok(buf.freeze())
}
/// Small utility to reseed the global RNG.
/// Done this way because we use it in a macro
/// and macro's can't load external crates.
#[inline]
pub fn reseed_rng_for_test() -> Result<(), Error> {
rand::rng()
.reseed()
.map_err(|e| make_input_err!("Could not reseed RNG - {e:?}"))
}