/
pipeline_utils.py
182 lines (154 loc) · 6.11 KB
/
pipeline_utils.py
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
# -*- coding: utf-8 -*-
# Copyright 2021 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# 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.
#
import copy
import json
from typing import Any, Dict, Mapping, Optional, Union
class PipelineRuntimeConfigBuilder(object):
"""Pipeline RuntimeConfig builder.
Constructs a RuntimeConfig spec with pipeline_root and parameter overrides.
"""
def __init__(
self,
pipeline_root: str,
parameter_types: Mapping[str, str],
parameter_values: Optional[Dict[str, Any]] = None,
):
"""Creates a PipelineRuntimeConfigBuilder object.
Args:
pipeline_root (str):
Required. The root of the pipeline outputs.
parameter_types (Mapping[str, str]):
Required. The mapping from pipeline parameter name to its type.
parameter_values (Dict[str, Any]):
Optional. The mapping from runtime parameter name to its value.
"""
self._pipeline_root = pipeline_root
self._parameter_types = parameter_types
self._parameter_values = copy.deepcopy(parameter_values or {})
@classmethod
def from_job_spec_json(
cls, job_spec: Mapping[str, Any],
) -> "PipelineRuntimeConfigBuilder":
"""Creates a PipelineRuntimeConfigBuilder object from PipelineJob json spec.
Args:
job_spec (Mapping[str, Any]):
Required. The PipelineJob spec.
Returns:
A PipelineRuntimeConfigBuilder object.
"""
runtime_config_spec = job_spec["runtimeConfig"]
parameter_input_definitions = (
job_spec["pipelineSpec"]["root"]
.get("inputDefinitions", {})
.get("parameters", {})
)
parameter_types = {k: v["type"] for k, v in parameter_input_definitions.items()}
pipeline_root = runtime_config_spec.get("gcs_output_directory")
parameter_values = _parse_runtime_parameters(runtime_config_spec)
return cls(pipeline_root, parameter_types, parameter_values)
def update_pipeline_root(self, pipeline_root: Optional[str]) -> None:
"""Updates pipeline_root value.
Args:
pipeline_root (str):
Optional. The root of the pipeline outputs.
"""
if pipeline_root:
self._pipeline_root = pipeline_root
def update_runtime_parameters(
self, parameter_values: Optional[Mapping[str, Any]] = None
) -> None:
"""Merges runtime parameter values.
Args:
parameter_values (Mapping[str, Any]):
Optional. The mapping from runtime parameter names to its values.
"""
if parameter_values:
parameters = dict(parameter_values)
for k, v in parameter_values.items():
if isinstance(v, (dict, list, bool)):
parameters[k] = json.dumps(v)
self._parameter_values.update(parameters)
def build(self) -> Dict[str, Any]:
"""Build a RuntimeConfig proto.
Raises:
ValueError: if the pipeline root is not specified.
"""
if not self._pipeline_root:
raise ValueError(
"Pipeline root must be specified, either during "
"compile time, or when calling the service."
)
return {
"gcs_output_directory": self._pipeline_root,
"parameters": {
k: self._get_vertex_value(k, v)
for k, v in self._parameter_values.items()
if v is not None
},
}
def _get_vertex_value(
self, name: str, value: Union[int, float, str]
) -> Dict[str, Any]:
"""Converts primitive values into Vertex pipeline Value proto message.
Args:
name (str):
Required. The name of the pipeline parameter.
value (Union[int, float, str]):
Required. The value of the pipeline parameter.
Returns:
A dictionary represents the Vertex pipeline Value proto message.
Raises:
ValueError: if the parameter name is not found in pipeline root
inputs, or value is none.
"""
if value is None:
raise ValueError("None values should be filterd out.")
if name not in self._parameter_types:
raise ValueError(
"The pipeline parameter {} is not found in the "
"pipeline job input definitions.".format(name)
)
result = {}
if self._parameter_types[name] == "INT":
result["intValue"] = value
elif self._parameter_types[name] == "DOUBLE":
result["doubleValue"] = value
elif self._parameter_types[name] == "STRING":
result["stringValue"] = value
else:
raise TypeError("Got unknown type of value: {}".format(value))
return result
def _parse_runtime_parameters(
runtime_config_spec: Mapping[str, Any]
) -> Optional[Dict[str, Any]]:
"""Extracts runtime parameters from runtime config json spec.
Raises:
TypeError: if the parameter type is not one of 'INT', 'DOUBLE', 'STRING'.
"""
runtime_parameters = runtime_config_spec.get("parameters")
if not runtime_parameters:
return None
result = {}
for name, value in runtime_parameters.items():
if "intValue" in value:
result[name] = int(value["intValue"])
elif "doubleValue" in value:
result[name] = float(value["doubleValue"])
elif "stringValue" in value:
result[name] = value["stringValue"]
else:
raise TypeError("Got unknown type of value: {}".format(value))
return result