/build/source/nativelink-scheduler/src/platform_property_manager.rs
Line | Count | Source |
1 | | // Copyright 2024 The NativeLink Authors. All rights reserved. |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // http://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | use std::collections::HashMap; |
16 | | |
17 | | use nativelink_config::schedulers::PropertyType; |
18 | | use nativelink_error::{Code, Error, ResultExt, make_input_err}; |
19 | | use nativelink_metric::{ |
20 | | MetricFieldData, MetricKind, MetricPublishKnownKindData, MetricsComponent, group, |
21 | | }; |
22 | | use nativelink_util::platform_properties::{PlatformProperties, PlatformPropertyValue}; |
23 | | |
24 | | /// Helps manage known properties and conversion into `PlatformPropertyValue`. |
25 | | #[derive(Debug)] |
26 | | pub struct PlatformPropertyManager { |
27 | | known_properties: HashMap<String, PropertyType>, |
28 | | } |
29 | | |
30 | | // TODO(allada) We cannot use the `MetricsComponent` trait here because |
31 | | // the `PropertyType` lives in the `nativelink-config` crate which is not |
32 | | // a dependency of the `nativelink-metric-collector` crate. |
33 | | impl MetricsComponent for PlatformPropertyManager { |
34 | 0 | fn publish( |
35 | 0 | &self, |
36 | 0 | _kind: MetricKind, |
37 | 0 | field_metadata: MetricFieldData, |
38 | 0 | ) -> Result<MetricPublishKnownKindData, nativelink_metric::Error> { |
39 | 0 | let _enter = group!("known_properties").entered(); |
40 | 0 | for (k, v) in &self.known_properties { |
41 | 0 | group!(k).in_scope(|| { |
42 | 0 | format!("{v:?}").publish(MetricKind::String, field_metadata.clone()) |
43 | 0 | })?; |
44 | | } |
45 | 0 | Ok(MetricPublishKnownKindData::Component) |
46 | 0 | } |
47 | | } |
48 | | |
49 | | impl PlatformPropertyManager { |
50 | | #[must_use] |
51 | 28 | pub const fn new(known_properties: HashMap<String, PropertyType>) -> Self { |
52 | 28 | Self { known_properties } |
53 | 28 | } |
54 | | |
55 | | /// Returns the `known_properties` map. |
56 | | #[must_use] |
57 | 0 | pub const fn get_known_properties(&self) -> &HashMap<String, PropertyType> { |
58 | 0 | &self.known_properties |
59 | 0 | } |
60 | | |
61 | | /// Given a map of key-value pairs, returns a map of `PlatformPropertyValue` based on the |
62 | | /// configuration passed into the `PlatformPropertyManager` constructor. |
63 | 46 | pub fn make_platform_properties( |
64 | 46 | &self, |
65 | 46 | properties: HashMap<String, String>, |
66 | 46 | ) -> Result<PlatformProperties, Error> { |
67 | 46 | let mut platform_properties = HashMap::with_capacity(properties.len()); |
68 | 57 | for (key, value11 ) in properties { |
69 | 11 | let prop_value = self.make_prop_value(&key, &value)?0 ; |
70 | 11 | platform_properties.insert(key, prop_value); |
71 | | } |
72 | 46 | Ok(PlatformProperties::new(platform_properties)) |
73 | 46 | } |
74 | | |
75 | | /// Given a specific key and value, returns the translated `PlatformPropertyValue`. This will |
76 | | /// automatically convert any strings to the type-value pairs of `PlatformPropertyValue` based |
77 | | /// on the configuration passed into the `PlatformPropertyManager` constructor. |
78 | 11 | pub fn make_prop_value(&self, key: &str, value: &str) -> Result<PlatformPropertyValue, Error> { |
79 | 11 | if let Some(prop_type) = self.known_properties.get(key) { Branch (79:16): [True: 11, False: 0]
Branch (79:16): [Folded - Ignored]
|
80 | 11 | return match prop_type { |
81 | | PropertyType::Minimum => Ok(PlatformPropertyValue::Minimum( |
82 | 9 | value.parse::<u64>().err_tip_with_code(|e| { |
83 | 0 | ( |
84 | 0 | Code::InvalidArgument, |
85 | 0 | format!("Cannot convert to platform property to u64: {value} - {e}"), |
86 | 0 | ) |
87 | 0 | })?, |
88 | | )), |
89 | 2 | PropertyType::Exact => Ok(PlatformPropertyValue::Exact(value.to_string())), |
90 | 0 | PropertyType::Priority => Ok(PlatformPropertyValue::Priority(value.to_string())), |
91 | | }; |
92 | 0 | } |
93 | 0 | Err(make_input_err!("Unknown platform property '{}'", key)) |
94 | 11 | } |
95 | | } |