Coverage Report

Created: 2026-01-30 00:00

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
81
    pub const fn new(map: HashMap<String, PlatformPropertyValue>) -> Self {
42
81
        Self { properties: map }
43
81
    }
44
45
    /// Determines if the worker's `PlatformProperties` is satisfied by this struct.
46
    #[must_use]
47
37
    pub fn is_satisfied_by(&self, worker_properties: &Self, full_worker_logging: bool) -> bool {
48
42
        for (
property8
,
check_value8
) in &self.properties {
49
8
            if let PlatformPropertyValue::Ignore(_) = check_value {
  Branch (49:20): [True: 1, False: 7]
  Branch (49:20): [Folded - Ignored]
50
1
                continue; // always matches
51
7
            }
52
7
            if let Some(worker_value) = worker_properties.properties.get(property) {
  Branch (52:20): [True: 7, False: 0]
  Branch (52:20): [Folded - Ignored]
53
7
                if !check_value.is_satisfied_by(worker_value) {
  Branch (53:20): [True: 3, False: 4]
  Branch (53:20): [Folded - Ignored]
54
3
                    if full_worker_logging {
  Branch (54:24): [True: 0, False: 3]
  Branch (54:24): [Folded - Ignored]
55
0
                        info!(
56
0
                            "Property mismatch on worker property {property}. {worker_value:?} != {check_value:?}"
57
                        );
58
3
                    }
59
3
                    return false;
60
4
                }
61
            } else {
62
0
                if full_worker_logging {
  Branch (62:20): [True: 0, False: 0]
  Branch (62:20): [Folded - Ignored]
63
0
                    info!("Property missing on worker property {property}");
64
0
                }
65
0
                return false;
66
            }
67
        }
68
34
        true
69
37
    }
70
}
71
72
impl From<ProtoPlatform> for PlatformProperties {
73
0
    fn from(platform: ProtoPlatform) -> Self {
74
0
        let mut properties = HashMap::with_capacity(platform.properties.len());
75
0
        for property in platform.properties {
76
0
            properties.insert(
77
0
                property.name,
78
0
                PlatformPropertyValue::Unknown(property.value),
79
0
            );
80
0
        }
81
0
        Self { properties }
82
0
    }
83
}
84
85
impl From<&PlatformProperties> for ProtoPlatform {
86
34
    fn from(val: &PlatformProperties) -> Self {
87
        Self {
88
34
            properties: val
89
34
                .properties
90
34
                .iter()
91
34
                .map(|(name, value)| ProtoProperty {
92
5
                    name: name.clone(),
93
5
                    value: value.as_str().to_string(),
94
5
                })
95
34
                .collect(),
96
        }
97
34
    }
98
}
99
100
/// Holds the associated value of the key and type.
101
///
102
/// Exact    - Means the worker must have this exact value.
103
/// Minimum  - Means that workers must have at least this number available. When
104
///            a worker executes a task that has this value, the worker will have
105
///            this value subtracted from the available resources of the worker.
106
/// Priority - Means the worker is given this information, but does not restrict
107
///            what workers can take this value. However, the worker must have the
108
///            associated key present to be matched.
109
///            TODO(palfrey) In the future this will be used by the scheduler and
110
///            worker to cause the scheduler to prefer certain workers over others,
111
///            but not restrict them based on these values.
112
/// Ignore   - Jobs can request this key, but workers do not have to have it. This allows
113
///            for example the `InputRootAbsolutePath` case for chromium builds, where we can safely
114
///            ignore it without having to change the worker configs.
115
#[derive(Eq, PartialEq, Hash, Clone, Ord, PartialOrd, Debug, Serialize, Deserialize)]
116
pub enum PlatformPropertyValue {
117
    Exact(String),
118
    Minimum(u64),
119
    Priority(String),
120
    Ignore(String),
121
    Unknown(String),
122
}
123
124
impl PlatformPropertyValue {
125
    /// Same as `PlatformProperties::is_satisfied_by`, but on an individual value.
126
    #[must_use]
127
9
    pub fn is_satisfied_by(&self, worker_value: &Self) -> bool {
128
9
        if self == worker_value {
  Branch (128:12): [True: 5, False: 4]
  Branch (128:12): [Folded - Ignored]
129
5
            return true;
130
4
        }
131
4
        match self {
132
3
            Self::Minimum(v) => {
133
3
                if let Self::Minimum(worker_v) = worker_value {
  Branch (133:24): [True: 3, False: 0]
  Branch (133:24): [Folded - Ignored]
134
3
                    return worker_v >= v;
135
0
                }
136
0
                false
137
            }
138
            // Priority is used to pass info to the worker and not restrict which
139
            // workers can be selected, but might be used to prefer certain workers
140
            // over others.
141
1
            Self::Priority(_) | Self::Ignore(_) => true,
142
            // Success exact case is handled above.
143
0
            Self::Exact(_) | Self::Unknown(_) => false,
144
        }
145
9
    }
146
147
7
    pub fn as_str(&self) -> Cow<'_, str> {
148
7
        match self {
149
2
            Self::Exact(value)
150
0
            | Self::Priority(value)
151
0
            | Self::Unknown(value)
152
2
            | Self::Ignore(
value0
) => Cow::Borrowed(value),
153
5
            Self::Minimum(value) => Cow::Owned(value.to_string()),
154
        }
155
7
    }
156
}
157
158
impl MetricsComponent for PlatformPropertyValue {
159
0
    fn publish(
160
0
        &self,
161
0
        kind: MetricKind,
162
0
        field_metadata: MetricFieldData,
163
0
    ) -> Result<MetricPublishKnownKindData, nativelink_metric::Error> {
164
0
        let name = field_metadata.name.into_owned();
165
0
        let help = field_metadata.help.as_ref();
166
0
        match self {
167
0
            Self::Exact(v) => publish!(name, v, kind, help, "exact"),
168
0
            Self::Minimum(v) => publish!(name, v, kind, help, "minimum"),
169
0
            Self::Priority(v) => publish!(name, v, kind, help, "priority"),
170
0
            Self::Ignore(v) => publish!(name, v, kind, help, "ignore"),
171
0
            Self::Unknown(v) => publish!(name, v, kind, help, "unknown"),
172
        }
173
174
0
        Ok(MetricPublishKnownKindData::Component)
175
0
    }
176
}