flyte 2.0.0b32__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of flyte might be problematic. Click here for more details.

Files changed (204) hide show
  1. flyte/__init__.py +108 -0
  2. flyte/_bin/__init__.py +0 -0
  3. flyte/_bin/debug.py +38 -0
  4. flyte/_bin/runtime.py +195 -0
  5. flyte/_bin/serve.py +178 -0
  6. flyte/_build.py +26 -0
  7. flyte/_cache/__init__.py +12 -0
  8. flyte/_cache/cache.py +147 -0
  9. flyte/_cache/defaults.py +9 -0
  10. flyte/_cache/local_cache.py +216 -0
  11. flyte/_cache/policy_function_body.py +42 -0
  12. flyte/_code_bundle/__init__.py +8 -0
  13. flyte/_code_bundle/_ignore.py +121 -0
  14. flyte/_code_bundle/_packaging.py +218 -0
  15. flyte/_code_bundle/_utils.py +347 -0
  16. flyte/_code_bundle/bundle.py +266 -0
  17. flyte/_constants.py +1 -0
  18. flyte/_context.py +155 -0
  19. flyte/_custom_context.py +73 -0
  20. flyte/_debug/__init__.py +0 -0
  21. flyte/_debug/constants.py +38 -0
  22. flyte/_debug/utils.py +17 -0
  23. flyte/_debug/vscode.py +307 -0
  24. flyte/_deploy.py +408 -0
  25. flyte/_deployer.py +109 -0
  26. flyte/_doc.py +29 -0
  27. flyte/_docstring.py +32 -0
  28. flyte/_environment.py +122 -0
  29. flyte/_excepthook.py +37 -0
  30. flyte/_group.py +32 -0
  31. flyte/_hash.py +8 -0
  32. flyte/_image.py +1055 -0
  33. flyte/_initialize.py +628 -0
  34. flyte/_interface.py +119 -0
  35. flyte/_internal/__init__.py +3 -0
  36. flyte/_internal/controllers/__init__.py +129 -0
  37. flyte/_internal/controllers/_local_controller.py +239 -0
  38. flyte/_internal/controllers/_trace.py +48 -0
  39. flyte/_internal/controllers/remote/__init__.py +58 -0
  40. flyte/_internal/controllers/remote/_action.py +211 -0
  41. flyte/_internal/controllers/remote/_client.py +47 -0
  42. flyte/_internal/controllers/remote/_controller.py +583 -0
  43. flyte/_internal/controllers/remote/_core.py +465 -0
  44. flyte/_internal/controllers/remote/_informer.py +381 -0
  45. flyte/_internal/controllers/remote/_service_protocol.py +50 -0
  46. flyte/_internal/imagebuild/__init__.py +3 -0
  47. flyte/_internal/imagebuild/docker_builder.py +706 -0
  48. flyte/_internal/imagebuild/image_builder.py +277 -0
  49. flyte/_internal/imagebuild/remote_builder.py +386 -0
  50. flyte/_internal/imagebuild/utils.py +78 -0
  51. flyte/_internal/resolvers/__init__.py +0 -0
  52. flyte/_internal/resolvers/_task_module.py +21 -0
  53. flyte/_internal/resolvers/common.py +31 -0
  54. flyte/_internal/resolvers/default.py +28 -0
  55. flyte/_internal/runtime/__init__.py +0 -0
  56. flyte/_internal/runtime/convert.py +486 -0
  57. flyte/_internal/runtime/entrypoints.py +204 -0
  58. flyte/_internal/runtime/io.py +188 -0
  59. flyte/_internal/runtime/resources_serde.py +152 -0
  60. flyte/_internal/runtime/reuse.py +125 -0
  61. flyte/_internal/runtime/rusty.py +193 -0
  62. flyte/_internal/runtime/task_serde.py +362 -0
  63. flyte/_internal/runtime/taskrunner.py +209 -0
  64. flyte/_internal/runtime/trigger_serde.py +160 -0
  65. flyte/_internal/runtime/types_serde.py +54 -0
  66. flyte/_keyring/__init__.py +0 -0
  67. flyte/_keyring/file.py +115 -0
  68. flyte/_logging.py +300 -0
  69. flyte/_map.py +312 -0
  70. flyte/_module.py +72 -0
  71. flyte/_pod.py +30 -0
  72. flyte/_resources.py +473 -0
  73. flyte/_retry.py +32 -0
  74. flyte/_reusable_environment.py +102 -0
  75. flyte/_run.py +724 -0
  76. flyte/_secret.py +96 -0
  77. flyte/_task.py +550 -0
  78. flyte/_task_environment.py +316 -0
  79. flyte/_task_plugins.py +47 -0
  80. flyte/_timeout.py +47 -0
  81. flyte/_tools.py +27 -0
  82. flyte/_trace.py +119 -0
  83. flyte/_trigger.py +1000 -0
  84. flyte/_utils/__init__.py +30 -0
  85. flyte/_utils/asyn.py +121 -0
  86. flyte/_utils/async_cache.py +139 -0
  87. flyte/_utils/coro_management.py +27 -0
  88. flyte/_utils/docker_credentials.py +173 -0
  89. flyte/_utils/file_handling.py +72 -0
  90. flyte/_utils/helpers.py +134 -0
  91. flyte/_utils/lazy_module.py +54 -0
  92. flyte/_utils/module_loader.py +104 -0
  93. flyte/_utils/org_discovery.py +57 -0
  94. flyte/_utils/uv_script_parser.py +49 -0
  95. flyte/_version.py +34 -0
  96. flyte/app/__init__.py +22 -0
  97. flyte/app/_app_environment.py +157 -0
  98. flyte/app/_deploy.py +125 -0
  99. flyte/app/_input.py +160 -0
  100. flyte/app/_runtime/__init__.py +3 -0
  101. flyte/app/_runtime/app_serde.py +347 -0
  102. flyte/app/_types.py +101 -0
  103. flyte/app/extras/__init__.py +3 -0
  104. flyte/app/extras/_fastapi.py +151 -0
  105. flyte/cli/__init__.py +12 -0
  106. flyte/cli/_abort.py +28 -0
  107. flyte/cli/_build.py +114 -0
  108. flyte/cli/_common.py +468 -0
  109. flyte/cli/_create.py +371 -0
  110. flyte/cli/_delete.py +45 -0
  111. flyte/cli/_deploy.py +293 -0
  112. flyte/cli/_gen.py +176 -0
  113. flyte/cli/_get.py +370 -0
  114. flyte/cli/_option.py +33 -0
  115. flyte/cli/_params.py +554 -0
  116. flyte/cli/_plugins.py +209 -0
  117. flyte/cli/_run.py +597 -0
  118. flyte/cli/_serve.py +64 -0
  119. flyte/cli/_update.py +37 -0
  120. flyte/cli/_user.py +17 -0
  121. flyte/cli/main.py +221 -0
  122. flyte/config/__init__.py +3 -0
  123. flyte/config/_config.py +248 -0
  124. flyte/config/_internal.py +73 -0
  125. flyte/config/_reader.py +225 -0
  126. flyte/connectors/__init__.py +11 -0
  127. flyte/connectors/_connector.py +270 -0
  128. flyte/connectors/_server.py +197 -0
  129. flyte/connectors/utils.py +135 -0
  130. flyte/errors.py +243 -0
  131. flyte/extend.py +19 -0
  132. flyte/extras/__init__.py +5 -0
  133. flyte/extras/_container.py +286 -0
  134. flyte/git/__init__.py +3 -0
  135. flyte/git/_config.py +21 -0
  136. flyte/io/__init__.py +29 -0
  137. flyte/io/_dataframe/__init__.py +131 -0
  138. flyte/io/_dataframe/basic_dfs.py +223 -0
  139. flyte/io/_dataframe/dataframe.py +1026 -0
  140. flyte/io/_dir.py +910 -0
  141. flyte/io/_file.py +914 -0
  142. flyte/io/_hashing_io.py +342 -0
  143. flyte/models.py +479 -0
  144. flyte/py.typed +0 -0
  145. flyte/remote/__init__.py +35 -0
  146. flyte/remote/_action.py +738 -0
  147. flyte/remote/_app.py +57 -0
  148. flyte/remote/_client/__init__.py +0 -0
  149. flyte/remote/_client/_protocols.py +189 -0
  150. flyte/remote/_client/auth/__init__.py +12 -0
  151. flyte/remote/_client/auth/_auth_utils.py +14 -0
  152. flyte/remote/_client/auth/_authenticators/__init__.py +0 -0
  153. flyte/remote/_client/auth/_authenticators/base.py +403 -0
  154. flyte/remote/_client/auth/_authenticators/client_credentials.py +73 -0
  155. flyte/remote/_client/auth/_authenticators/device_code.py +117 -0
  156. flyte/remote/_client/auth/_authenticators/external_command.py +79 -0
  157. flyte/remote/_client/auth/_authenticators/factory.py +200 -0
  158. flyte/remote/_client/auth/_authenticators/pkce.py +516 -0
  159. flyte/remote/_client/auth/_channel.py +213 -0
  160. flyte/remote/_client/auth/_client_config.py +85 -0
  161. flyte/remote/_client/auth/_default_html.py +32 -0
  162. flyte/remote/_client/auth/_grpc_utils/__init__.py +0 -0
  163. flyte/remote/_client/auth/_grpc_utils/auth_interceptor.py +288 -0
  164. flyte/remote/_client/auth/_grpc_utils/default_metadata_interceptor.py +151 -0
  165. flyte/remote/_client/auth/_keyring.py +152 -0
  166. flyte/remote/_client/auth/_token_client.py +260 -0
  167. flyte/remote/_client/auth/errors.py +16 -0
  168. flyte/remote/_client/controlplane.py +128 -0
  169. flyte/remote/_common.py +30 -0
  170. flyte/remote/_console.py +19 -0
  171. flyte/remote/_data.py +161 -0
  172. flyte/remote/_logs.py +185 -0
  173. flyte/remote/_project.py +88 -0
  174. flyte/remote/_run.py +386 -0
  175. flyte/remote/_secret.py +142 -0
  176. flyte/remote/_task.py +527 -0
  177. flyte/remote/_trigger.py +306 -0
  178. flyte/remote/_user.py +33 -0
  179. flyte/report/__init__.py +3 -0
  180. flyte/report/_report.py +182 -0
  181. flyte/report/_template.html +124 -0
  182. flyte/storage/__init__.py +36 -0
  183. flyte/storage/_config.py +237 -0
  184. flyte/storage/_parallel_reader.py +274 -0
  185. flyte/storage/_remote_fs.py +34 -0
  186. flyte/storage/_storage.py +456 -0
  187. flyte/storage/_utils.py +5 -0
  188. flyte/syncify/__init__.py +56 -0
  189. flyte/syncify/_api.py +375 -0
  190. flyte/types/__init__.py +52 -0
  191. flyte/types/_interface.py +40 -0
  192. flyte/types/_pickle.py +145 -0
  193. flyte/types/_renderer.py +162 -0
  194. flyte/types/_string_literals.py +119 -0
  195. flyte/types/_type_engine.py +2254 -0
  196. flyte/types/_utils.py +80 -0
  197. flyte-2.0.0b32.data/scripts/debug.py +38 -0
  198. flyte-2.0.0b32.data/scripts/runtime.py +195 -0
  199. flyte-2.0.0b32.dist-info/METADATA +351 -0
  200. flyte-2.0.0b32.dist-info/RECORD +204 -0
  201. flyte-2.0.0b32.dist-info/WHEEL +5 -0
  202. flyte-2.0.0b32.dist-info/entry_points.txt +7 -0
  203. flyte-2.0.0b32.dist-info/licenses/LICENSE +201 -0
  204. flyte-2.0.0b32.dist-info/top_level.txt +1 -0
@@ -0,0 +1,225 @@
1
+ import os
2
+ import pathlib
3
+ import typing
4
+ from dataclasses import dataclass
5
+ from functools import lru_cache
6
+ from os import getenv
7
+ from pathlib import Path
8
+
9
+ import yaml
10
+
11
+ from flyte._logging import logger
12
+
13
+ # This is the default config file name for flyte
14
+ FLYTECTL_CONFIG_ENV_VAR = "FLYTECTL_CONFIG"
15
+ UCTL_CONFIG_ENV_VAR = "UCTL_CONFIG"
16
+
17
+
18
+ @dataclass
19
+ class YamlConfigEntry(object):
20
+ """
21
+ Creates a record for the config entry.
22
+ Args:
23
+ switch: dot-delimited string that should match flytectl args. Leaving it as dot-delimited instead of a list
24
+ of strings because it's easier to maintain alignment with flytectl.
25
+ config_value_type: Expected type of the value
26
+ """
27
+
28
+ switch: str
29
+ config_value_type: typing.Type = str
30
+
31
+ def get_env_name(self) -> str:
32
+ var_name = self.switch.upper().replace(".", "_")
33
+ return f"FLYTE_{var_name}"
34
+
35
+ def read_from_env(self, transform: typing.Optional[typing.Callable] = None) -> typing.Optional[typing.Any]:
36
+ """
37
+ Reads the config entry from environment variable, the structure of the env var is current
38
+ ``FLYTE_{SECTION}_{OPTION}`` all upper cased. We will change this in the future.
39
+ :return:
40
+ """
41
+ env = self.get_env_name()
42
+ v = os.environ.get(env, None)
43
+ if v is None:
44
+ return None
45
+ return transform(v) if transform else v
46
+
47
+ def read_from_file(
48
+ self, cfg: "ConfigFile", transform: typing.Optional[typing.Callable] = None
49
+ ) -> typing.Optional[typing.Any]:
50
+ if not cfg:
51
+ return None
52
+ try:
53
+ v = cfg.get(self)
54
+ if isinstance(v, bool) or bool(v is not None and v):
55
+ return transform(v) if transform else v
56
+ except Exception:
57
+ ...
58
+
59
+ return None
60
+
61
+
62
+ @dataclass
63
+ class ConfigEntry(object):
64
+ """
65
+ A top level Config entry holder, that holds multiple different representations of the config.
66
+ Legacy means the INI style config files. YAML support is for the flytectl config file, which is there by default
67
+ when flytectl starts a sandbox
68
+ """
69
+
70
+ yaml_entry: YamlConfigEntry
71
+ transform: typing.Optional[typing.Callable[[str], typing.Any]] = None
72
+
73
+ def read(self, cfg: typing.Optional["ConfigFile"] = None) -> typing.Optional[typing.Any]:
74
+ """
75
+ Reads the config Entry from the various sources in the following order,
76
+ #. First try to read from the relevant environment variable,
77
+ #. If missing, then try to read from the legacy config file, if one was parsed.
78
+ #. If missing, then try to read from the yaml file.
79
+
80
+ The constructor for ConfigFile currently does not allow specification of both the ini and yaml style formats.
81
+
82
+ :param cfg:
83
+ :return:
84
+ """
85
+ from_env = self.yaml_entry.read_from_env(self.transform)
86
+ if from_env is not None:
87
+ return from_env
88
+ if cfg and cfg.yaml_config and self.yaml_entry:
89
+ return self.yaml_entry.read_from_file(cfg, self.transform)
90
+
91
+ return None
92
+
93
+
94
+ class ConfigFile(object):
95
+ def __init__(self, location: str):
96
+ """
97
+ Load the config from this location
98
+ """
99
+ self._location = location
100
+ self._yaml_config = self._read_yaml_config(location)
101
+
102
+ @property
103
+ def path(self) -> pathlib.Path:
104
+ """
105
+ Returns the path to the config file.
106
+ :return: Path to the config file
107
+ """
108
+ return pathlib.Path(self._location)
109
+
110
+ @staticmethod
111
+ def _read_yaml_config(location: str | pathlib.Path) -> typing.Optional[typing.Dict[str, typing.Any]]:
112
+ with open(location, "r") as fh:
113
+ try:
114
+ yaml_contents = yaml.safe_load(fh)
115
+ return yaml_contents
116
+ except yaml.YAMLError as exc:
117
+ logger.warning(f"Error {exc} reading yaml config file at {location}, ignoring...")
118
+ return None
119
+
120
+ def _get_from_yaml(self, c: YamlConfigEntry) -> typing.Any:
121
+ keys = c.switch.split(".") # flytectl switches are dot delimited
122
+ d = typing.cast(typing.Dict[str, typing.Any], self.yaml_config)
123
+ try:
124
+ for k in keys:
125
+ d = d[k]
126
+ return d
127
+ except KeyError:
128
+ return None
129
+
130
+ def get(self, c: YamlConfigEntry) -> typing.Any:
131
+ return self._get_from_yaml(c)
132
+
133
+ @property
134
+ def yaml_config(self) -> typing.Dict[str, typing.Any] | None:
135
+ return self._yaml_config
136
+
137
+
138
+ def _config_path_from_git_root() -> pathlib.Path | None:
139
+ from flyte.git import config_from_root
140
+
141
+ config = config_from_root()
142
+ if config is None:
143
+ return None
144
+ return config.source
145
+
146
+
147
+ def resolve_config_path() -> pathlib.Path | None:
148
+ """
149
+ Config is read from the following locations in order of precedence:
150
+ 1. ./config.yaml if it exists
151
+ 2. ./.flyte/config.yaml if it exists
152
+ 3. <git_root>/.flyte/config.yaml if it exists
153
+ 4. `UCTL_CONFIG` environment variable
154
+ 5. `FLYTECTL_CONFIG` environment variable
155
+ 6. ~/.union/config.yaml if it exists
156
+ 7. ~/.flyte/config.yaml if it exists
157
+ """
158
+ current_location_config = Path("config.yaml")
159
+ if current_location_config.exists():
160
+ return current_location_config
161
+ logger.debug("No ./config.yaml found")
162
+
163
+ dot_flyte_config = Path(".flyte", "config.yaml")
164
+ if dot_flyte_config.exists():
165
+ return dot_flyte_config
166
+ logger.debug("No ./.flyte/config.yaml found")
167
+
168
+ git_root_config = _config_path_from_git_root()
169
+ if git_root_config:
170
+ return git_root_config
171
+ logger.debug("No .flyte/config.yaml found in git repo root")
172
+
173
+ uctl_path_from_env = getenv(UCTL_CONFIG_ENV_VAR, None)
174
+ if uctl_path_from_env:
175
+ return pathlib.Path(uctl_path_from_env)
176
+ logger.debug("No UCTL_CONFIG environment variable found, checking FLYTECTL_CONFIG")
177
+
178
+ flytectl_path_from_env = getenv(FLYTECTL_CONFIG_ENV_VAR, None)
179
+ if flytectl_path_from_env:
180
+ return pathlib.Path(flytectl_path_from_env)
181
+ logger.debug("No FLYTECTL_CONFIG environment variable found, checking default locations")
182
+
183
+ home_dir_union_config = Path(Path.home(), ".union", "config.yaml")
184
+ if home_dir_union_config.exists():
185
+ return home_dir_union_config
186
+ logger.debug("No ~/.union/config.yaml found, checking current directory")
187
+
188
+ home_dir_flytectl_config = Path(Path.home(), ".flyte", "config.yaml")
189
+ if home_dir_flytectl_config.exists():
190
+ return home_dir_flytectl_config
191
+ logger.debug("No ~/.flyte/config.yaml found, checking current directory")
192
+
193
+ return None
194
+
195
+
196
+ @lru_cache
197
+ def get_config_file(c: typing.Union[str, pathlib.Path, ConfigFile, None]) -> ConfigFile | None:
198
+ """
199
+ Checks if the given argument is a file or a configFile and returns a loaded configFile else returns None
200
+ """
201
+ if isinstance(c, (str, pathlib.Path)):
202
+ logger.debug(f"Using specified config file at {c}")
203
+ return ConfigFile(str(c))
204
+ elif isinstance(c, ConfigFile):
205
+ return c
206
+ config_path = resolve_config_path()
207
+ if config_path:
208
+ return ConfigFile(str(config_path))
209
+ return None
210
+
211
+
212
+ def read_file_if_exists(filename: typing.Optional[str], encoding=None) -> typing.Optional[str]:
213
+ """
214
+ Reads the contents of the file if passed a path. Otherwise, returns None.
215
+
216
+ :param filename: The file path to load
217
+ :param encoding: The encoding to use when reading the file.
218
+ :return: The contents of the file as a string or None.
219
+ """
220
+ if not filename:
221
+ return None
222
+
223
+ file = pathlib.Path(filename)
224
+ logger.debug(f"Reading file contents from [{file}] with current directory [{os.getcwd()}].")
225
+ return file.read_text(encoding=encoding)
@@ -0,0 +1,11 @@
1
+ from ._connector import AsyncConnector, AsyncConnectorExecutorMixin, ConnectorRegistry, Resource, ResourceMeta
2
+ from ._server import ConnectorService
3
+
4
+ __all__ = [
5
+ "AsyncConnector",
6
+ "AsyncConnectorExecutorMixin",
7
+ "ConnectorRegistry",
8
+ "ConnectorService",
9
+ "Resource",
10
+ "ResourceMeta",
11
+ ]
@@ -0,0 +1,270 @@
1
+ import asyncio
2
+ import json
3
+ import typing
4
+ from abc import ABC, abstractmethod
5
+ from dataclasses import asdict, dataclass
6
+ from typing import Any, Dict, List, Optional
7
+
8
+ from flyteidl2.core import tasks_pb2
9
+ from flyteidl2.core.execution_pb2 import TaskExecution, TaskLog
10
+ from flyteidl2.plugins import connector_pb2
11
+ from flyteidl2.plugins.connector_pb2 import Connector as ConnectorProto
12
+ from flyteidl2.plugins.connector_pb2 import (
13
+ GetTaskLogsResponse,
14
+ GetTaskMetricsResponse,
15
+ TaskCategory,
16
+ TaskExecutionMetadata,
17
+ )
18
+ from google.protobuf import json_format
19
+ from google.protobuf.struct_pb2 import Struct
20
+
21
+ from flyte import Secret
22
+ from flyte._context import internal_ctx
23
+ from flyte._initialize import get_init_config
24
+ from flyte._internal.runtime.convert import convert_from_native_to_outputs
25
+ from flyte._internal.runtime.task_serde import get_proto_task
26
+ from flyte._logging import logger
27
+ from flyte._task import TaskTemplate
28
+ from flyte.connectors.utils import is_terminal_phase
29
+ from flyte.models import NativeInterface, SerializationContext
30
+ from flyte.types._type_engine import dataclass_from_dict
31
+
32
+
33
+ @dataclass(frozen=True)
34
+ class ConnectorRegistryKey:
35
+ task_type_name: str
36
+ task_type_version: int
37
+
38
+
39
+ @dataclass
40
+ class ResourceMeta:
41
+ """
42
+ This is the metadata for the job. For example, the id of the job.
43
+ """
44
+
45
+ def encode(self) -> bytes:
46
+ """
47
+ Encode the resource meta to bytes.
48
+ """
49
+ return json.dumps(asdict(self)).encode("utf-8")
50
+
51
+ @classmethod
52
+ def decode(cls, data: bytes) -> "ResourceMeta":
53
+ """
54
+ Decode the resource meta from bytes.
55
+ """
56
+ return dataclass_from_dict(cls, json.loads(data.decode("utf-8")))
57
+
58
+
59
+ @dataclass
60
+ class Resource:
61
+ """
62
+ This is the output resource of the job.
63
+
64
+ Attributes
65
+ ----------
66
+ phase : TaskExecution.Phase
67
+ The phase of the job.
68
+ message : Optional[str]
69
+ The return message from the job.
70
+ log_links : Optional[List[TaskLog]]
71
+ The log links of the job. For example, the link to the BigQuery Console.
72
+ outputs : Optional[Union[LiteralMap, typing.Dict[str, Any]]]
73
+ The outputs of the job. If return python native types, the agent will convert them to flyte literals.
74
+ custom_info : Optional[typing.Dict[str, Any]]
75
+ The custom info of the job. For example, the job config.
76
+ """
77
+
78
+ phase: TaskExecution.Phase
79
+ message: Optional[str] = None
80
+ log_links: Optional[List[TaskLog]] = None
81
+ outputs: Optional[Dict[str, Any]] = None
82
+ custom_info: Optional[typing.Dict[str, Any]] = None
83
+
84
+
85
+ class AsyncConnector(ABC):
86
+ """
87
+ This is the base class for all async connectors, and it defines the interface that all connectors must implement.
88
+ The connector service is responsible for invoking connectors.
89
+ The executor will communicate with the connector service to create tasks, get the status of tasks, and delete tasks.
90
+
91
+ All the connectors should be registered in the ConnectorRegistry.
92
+ Connector Service will look up the connector based on the task type and version.
93
+ """
94
+
95
+ name = "Async Connector"
96
+ task_type_name: str
97
+ task_type_version: int = 0
98
+ metadata_type: ResourceMeta
99
+
100
+ @abstractmethod
101
+ async def create(
102
+ self,
103
+ task_template: tasks_pb2.TaskTemplate,
104
+ output_prefix: str,
105
+ inputs: Optional[Dict[str, typing.Any]] = None,
106
+ task_execution_metadata: Optional[TaskExecutionMetadata] = None,
107
+ **kwargs,
108
+ ) -> ResourceMeta:
109
+ """
110
+ Return a resource meta that can be used to get the status of the task.
111
+ """
112
+ raise NotImplementedError
113
+
114
+ @abstractmethod
115
+ async def get(self, resource_meta: ResourceMeta, **kwargs) -> Resource:
116
+ """
117
+ Return the status of the task, and return the outputs in some cases. For example, bigquery job
118
+ can't write the structured dataset to the output location, so it returns the output literals to the propeller,
119
+ and the propeller will write the structured dataset to the blob store.
120
+ """
121
+ raise NotImplementedError
122
+
123
+ @abstractmethod
124
+ async def delete(self, resource_meta: ResourceMeta, **kwargs):
125
+ """
126
+ Delete the task. This call should be idempotent. It should raise an error if fails to delete the task.
127
+ """
128
+ raise NotImplementedError
129
+
130
+ async def get_metrics(self, resource_meta: ResourceMeta, **kwargs) -> GetTaskMetricsResponse:
131
+ """
132
+ Return the metrics for the task.
133
+ """
134
+ raise NotImplementedError
135
+
136
+ async def get_logs(self, resource_meta: ResourceMeta, **kwargs) -> GetTaskLogsResponse:
137
+ """
138
+ Return the metrics for the task.
139
+ """
140
+ raise NotImplementedError
141
+
142
+
143
+ class ConnectorRegistry(object):
144
+ """
145
+ This is the registry for all connectors.
146
+ The connector service will look up the connector registry based on the task type and version.
147
+ """
148
+
149
+ _REGISTRY: typing.ClassVar[Dict[ConnectorRegistryKey, AsyncConnector]] = {}
150
+ _METADATA: typing.ClassVar[Dict[str, ConnectorProto]] = {}
151
+
152
+ @staticmethod
153
+ def register(connector: AsyncConnector, override: bool = False):
154
+ key = ConnectorRegistryKey(
155
+ task_type_name=connector.task_type_name, task_type_version=connector.task_type_version
156
+ )
157
+ if key in ConnectorRegistry._REGISTRY and override is False:
158
+ raise ValueError(
159
+ f"Duplicate connector for task type: {connector.task_type_name}"
160
+ f" and version: {connector.task_type_version}"
161
+ )
162
+ ConnectorRegistry._REGISTRY[key] = connector
163
+
164
+ task_category = TaskCategory(name=connector.task_type_name, version=connector.task_type_version)
165
+
166
+ if connector.name in ConnectorRegistry._METADATA:
167
+ connector_metadata = ConnectorRegistry._get_connector_metadata(connector.name)
168
+ connector_metadata.supported_task_categories.append(task_category)
169
+ else:
170
+ connector_metadata = ConnectorProto(
171
+ name=connector.name,
172
+ supported_task_categories=[task_category],
173
+ )
174
+ ConnectorRegistry._METADATA[connector.name] = connector_metadata
175
+
176
+ @staticmethod
177
+ def get_connector(task_type_name: str, task_type_version: int = 0) -> AsyncConnector:
178
+ key = ConnectorRegistryKey(task_type_name=task_type_name, task_type_version=task_type_version)
179
+ if key not in ConnectorRegistry._REGISTRY:
180
+ raise FlyteConnectorNotFound(
181
+ f"Cannot find connector for task type: {task_type_name} and version: {task_type_version}"
182
+ )
183
+ return ConnectorRegistry._REGISTRY[key]
184
+
185
+ @staticmethod
186
+ def _list_connectors() -> List[ConnectorProto]:
187
+ return list(ConnectorRegistry._METADATA.values())
188
+
189
+ @staticmethod
190
+ def _get_connector_metadata(name: str) -> ConnectorProto:
191
+ if name not in ConnectorRegistry._METADATA:
192
+ raise FlyteConnectorNotFound(f"Cannot find connector for name: {name}.")
193
+ return ConnectorRegistry._METADATA[name]
194
+
195
+
196
+ class ConnectorSecretsMixin:
197
+ def __init__(self, secrets: Dict[str, str]):
198
+ # Key is the id of the secret, value is the secret name.
199
+ self._secrets = secrets
200
+
201
+ @property
202
+ def secrets(self) -> List[Secret]:
203
+ return [Secret(key=k, as_env_var=v) for k, v in self._secrets.items()]
204
+
205
+
206
+ class AsyncConnectorExecutorMixin:
207
+ """
208
+ This mixin class is used to run the connector task locally, and it's only used for local execution.
209
+ Task should inherit from this class if the task can be run in the connector.
210
+ """
211
+
212
+ async def execute(self, **kwargs) -> Any:
213
+ task = typing.cast(TaskTemplate, self)
214
+ connector = ConnectorRegistry.get_connector(task.task_type, task.task_type_version)
215
+
216
+ ctx = internal_ctx()
217
+ tctx = internal_ctx().data.task_context
218
+ cfg = get_init_config()
219
+
220
+ if tctx is None:
221
+ raise RuntimeError("Task context is not set.")
222
+
223
+ sc = SerializationContext(
224
+ project=tctx.action.project,
225
+ domain=tctx.action.domain,
226
+ org=tctx.action.org,
227
+ code_bundle=tctx.code_bundle,
228
+ version=tctx.version,
229
+ image_cache=tctx.compiled_image_cache,
230
+ root_dir=cfg.root_dir,
231
+ )
232
+ tt = get_proto_task(task, sc)
233
+ resource_meta = await connector.create(task_template=tt, output_prefix=ctx.raw_data.path, inputs=kwargs)
234
+ resource = Resource(phase=TaskExecution.RUNNING)
235
+
236
+ while not is_terminal_phase(resource.phase):
237
+ resource = await connector.get(resource_meta=resource_meta)
238
+
239
+ if resource.log_links:
240
+ for link in resource.log_links:
241
+ logger.info(f"{link.name}: {link.uri}")
242
+ await asyncio.sleep(1)
243
+
244
+ if resource.phase != TaskExecution.SUCCEEDED:
245
+ raise RuntimeError(f"Failed to run the task {task.name} with error: {resource.message}")
246
+
247
+ # TODO: Support abort
248
+
249
+ if resource.outputs is None:
250
+ return None
251
+ return tuple(resource.outputs.values())
252
+
253
+
254
+ async def get_resource_proto(resource: Resource) -> connector_pb2.Resource:
255
+ if resource.outputs:
256
+ interface = NativeInterface.from_types(inputs={}, outputs={k: type(v) for k, v in resource.outputs.items()})
257
+ outputs = await convert_from_native_to_outputs(tuple(resource.outputs.values()), interface)
258
+ else:
259
+ outputs = None
260
+
261
+ return connector_pb2.Resource(
262
+ phase=resource.phase,
263
+ message=resource.message,
264
+ log_links=resource.log_links,
265
+ outputs=outputs,
266
+ custom_info=(json_format.Parse(json.dumps(resource.custom_info), Struct()) if resource.custom_info else None),
267
+ )
268
+
269
+
270
+ class FlyteConnectorNotFound(ValueError): ...