Coverage Report

Created: 2026-03-02 14:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/build/source/nativelink-util/src/platform_properties.rs
Line
Count
Source
1
// Copyright 2024 The NativeLink Authors. All rights reserved.
2
//
3
// Licensed under the Functional Source License, Version 1.1, Apache 2.0 Future License (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
//    See LICENSE file for details
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::borrow::Cow;
16
use std::collections::HashMap;
17
18
use nativelink_metric::{
19
    MetricFieldData, MetricKind, MetricPublishKnownKindData, MetricsComponent, publish,
20
};
21
use nativelink_proto::build::bazel::remote::execution::v2::Platform as ProtoPlatform;
22
use nativelink_proto::build::bazel::remote::execution::v2::platform::Property as ProtoProperty;
23
use serde::{Deserialize, Serialize};
24
use tracing::info;
25
26
/// `PlatformProperties` helps manage the configuration of platform properties to
27
/// keys and types. The scheduler uses these properties to decide what jobs
28
/// can be assigned to different workers. For example, if a job states it needs
29
/// a specific key, it will never be run on a worker that does not have at least
30
/// all the platform property keys configured on the worker.
31
///
32
/// Additional rules may be applied based on `PlatformPropertyValue`.
33
#[derive(Eq, PartialEq, Clone, Debug, Default, Serialize, Deserialize, MetricsComponent)]
34
pub struct PlatformProperties {
35
    #[metric]
36
    pub properties: HashMap<String, PlatformPropertyValue>,
37
}
38
39
impl PlatformProperties {
40
    #[must_use]
41
92
    pub const fn new(map: HashMap<String, PlatformPropertyValue>) -> Self {
42
92
        Self { properties: map }
43
92
    }
44
45
    /// Determines if the worker's `PlatformProperties` is satisfied by this struct.
46
    #[must_use]
47
44
    pub fn is_satisfied_by(&self, worker_properties: &Self, full_worker_logging: bool) -> bool {
48
49
        for (
property11
,
check_value11
) in &self.properties {
49
11
            if let PlatformPropertyValue::Ignore(_) = check_value {
  Branch (49:20): [True: 1, False: 10]
  Branch (49:20): [Folded - Ignored]
50
1
                continue; // always matches
51
10
            }
52
10
            if let Some(worker_value) = worker_properties.properties.get(property) {
  Branch (52:20): [True: 10, False: 0]
  Branch (52:20): [Folded - Ignored]
53
10
                if !check_value.is_satisfied_by(worker_value) {
  Branch (53:20): [True: 6, False: 4]
  Branch (53:20): [Folded - Ignored]
54
6
                    if full_worker_logging {
  Branch (54:24): [True: 2, False: 4]
  Branch (54:24): [Folded - Ignored]
55
2
                        match check_value {
56
                            PlatformPropertyValue::Minimum(_) => {
57
2
                                info!(
58
2
                                    "Property mismatch on worker property {property}. {worker_value:?} < {check_value:?}"
59
                                );
60
                            }
61
                            _ => {
62
0
                                info!(
63
0
                                    "Property mismatch on worker property {property}. {worker_value:?} != {check_value:?}"
64
                                );
65
                            }
66
                        }
67
4
                    }
68
6
                    return false;
69
4
                }
70
            } else {
71
0
                if full_worker_logging {
  Branch (71:20): [True: 0, False: 0]
  Branch (71:20): [Folded - Ignored]
72
0
                    info!("Property missing on worker property {property}");
73
0
                }
74
0
                return false;
75
            }
76
        }
77
38
        true
78
44
    }
79
}
80
81
impl From<ProtoPlatform> for PlatformProperties {
82
0
    fn from(platform: ProtoPlatform) -> Self {
83
0
        let mut properties = HashMap::with_capacity(platform.properties.len());
84
0
        for property in platform.properties {
85
0
            properties.insert(
86
0
                property.name,
87
0
                PlatformPropertyValue::Unknown(property.value),
88
0
            );
89
0
        }
90
0
        Self { properties }
91
0
    }
92
}
93
94
impl From<&PlatformProperties> for ProtoPlatform {
95
34
    fn from(val: &PlatformProperties) -> Self {
96
        Self {
97
34
            properties: val
98
34
                .properties
99
34
                .iter()
100
34
                .map(|(name, value)| ProtoProperty {
101
5
                    name: name.clone(),
102
5
                    value: value.as_str().to_string(),
103
5
                })
104
34
                .collect(),
105
        }
106
34
    }
107
}
108
109
/// Holds the associated value of the key and type.
110
///
111
/// Exact    - Means the worker must have this exact value.
112
/// Minimum  - Means that workers must have at least this number available. When
113
///            a worker executes a task that has this value, the worker will have
114
///            this value subtracted from the available resources of the worker.
115
/// Priority - Means the worker is given this information, but does not restrict
116
///            what workers can take this value. However, the worker must have the
117
///            associated key present to be matched.
118
///            TODO(palfrey) In the future this will be used by the scheduler and
119
///            worker to cause the scheduler to prefer certain workers over others,
120
///            but not restrict them based on these values.
121
/// Ignore   - Jobs can request this key, but workers do not have to have it. This allows
122
///            for example the `InputRootAbsolutePath` case for chromium builds, where we can safely
123
///            ignore it without having to change the worker configs.
124
#[derive(Eq, PartialEq, Hash, Clone, Ord, PartialOrd, Debug, Serialize, Deserialize)]
125
pub enum PlatformPropertyValue {
126
    Exact(String),
127
    Minimum(u64),
128
    Priority(String),
129
    Ignore(String),
130
    Unknown(String),
131
}
132
133
impl PlatformPropertyValue {
134
    /// Same as `PlatformProperties::is_satisfied_by`, but on an individual value.
135
    #[must_use]
136
12
    pub fn is_satisfied_by(&self, worker_value: &Self) -> bool {
137
12
        if self == worker_value {
  Branch (137:12): [True: 5, False: 7]
  Branch (137:12): [Folded - Ignored]
138
5
            return true;
139
7
        }
140
7
        match self {
141
6
            Self::Minimum(v) => {
142
6
                if let Self::Minimum(worker_v) = worker_value {
  Branch (142:24): [True: 6, False: 0]
  Branch (142:24): [Folded - Ignored]
143
6
                    return worker_v >= v;
144
0
                }
145
0
                false
146
            }
147
            // Priority is used to pass info to the worker and not restrict which
148
            // workers can be selected, but might be used to prefer certain workers
149
            // over others.
150
1
            Self::Priority(_) | Self::Ignore(_) => true,
151
            // Success exact case is handled above.
152
0
            Self::Exact(_) | Self::Unknown(_) => false,
153
        }
154
12
    }
155
156
7
    pub fn as_str(&self) -> Cow<'_, str> {
157
7
        match self {
158
2
            Self::Exact(value)
159
0
            | Self::Priority(value)
160
0
            | Self::Unknown(value)
161
2
            | Self::Ignore(
value0
) => Cow::Borrowed(value),
162
5
            Self::Minimum(value) => Cow::Owned(value.to_string()),
163
        }
164
7
    }
165
}
166
167
impl MetricsComponent for PlatformPropertyValue {
168
0
    fn publish(
169
0
        &self,
170
0
        kind: MetricKind,
171
0
        field_metadata: MetricFieldData,
172
0
    ) -> Result<MetricPublishKnownKindData, nativelink_metric::Error> {
173
0
        let name = field_metadata.name.into_owned();
174
0
        let help = field_metadata.help.as_ref();
175
0
        match self {
176
0
            Self::Exact(v) => publish!(name, v, kind, help, "exact"),
177
0
            Self::Minimum(v) => publish!(name, v, kind, help, "minimum"),
178
0
            Self::Priority(v) => publish!(name, v, kind, help, "priority"),
179
0
            Self::Ignore(v) => publish!(name, v, kind, help, "ignore"),
180
0
            Self::Unknown(v) => publish!(name, v, kind, help, "unknown"),
181
        }
182
183
0
        Ok(MetricPublishKnownKindData::Component)
184
0
    }
185
}