-
Notifications
You must be signed in to change notification settings - Fork 16.4k
[AIP-44] Add internal API definition. #27892
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
11be405
cc901d1
0c6843e
2d2bf1e
64a9da7
c9eb436
97a5986
15ee13e
4c55f94
528ebe9
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you 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. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you 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. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you 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. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import json | ||
| import logging | ||
|
|
||
| from flask import Response | ||
|
|
||
| from airflow.api_connexion.types import APIResponse | ||
| from airflow.dag_processing.processor import DagFileProcessor | ||
| from airflow.serialization.serialized_objects import BaseSerialization | ||
|
|
||
| log = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| def _build_methods_map(list) -> dict: | ||
| return {f"{func.__module__}.{func.__name__}": func for func in list} | ||
|
|
||
|
|
||
| METHODS_MAP = _build_methods_map( | ||
| [ | ||
| DagFileProcessor.update_import_errors, | ||
| ] | ||
| ) | ||
|
|
||
|
|
||
| def internal_airflow_api( | ||
| body: dict, | ||
| ) -> APIResponse: | ||
| """Handler for Internal API /internal_api/v1/rpcapi endpoint.""" | ||
| log.debug("Got request") | ||
| json_rpc = body.get("jsonrpc") | ||
| if json_rpc != "2.0": | ||
| log.error("Not jsonrpc-2.0 request.") | ||
| return Response(response="Expected jsonrpc 2.0 request.", status=400) | ||
|
|
||
| method_name = str(body.get("method")) | ||
| if method_name not in METHODS_MAP: | ||
| log.error("Unrecognized method: %s.", method_name) | ||
| return Response(response=f"Unrecognized method: {method_name}.", status=400) | ||
|
|
||
| handler = METHODS_MAP[method_name] | ||
| params = {} | ||
| try: | ||
| if body.get("params"): | ||
| params_json = json.loads(str(body.get("params"))) | ||
| params = BaseSerialization.deserialize(params_json) | ||
| except Exception as err: | ||
| log.error("Error deserializing parameters.") | ||
| log.error(err) | ||
| return Response(response="Error deserializing parameters.", status=400) | ||
|
|
||
| log.debug("Calling method %.", {method_name}) | ||
| try: | ||
| output = handler(**params) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just realized something here. Dont we have an issue here? All functions listed in
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right. I have an issue for that #28267 |
||
| output_json = BaseSerialization.serialize(output) | ||
| log.debug("Returning response") | ||
| return Response( | ||
| response=json.dumps(output_json or "{}"), headers={"Content-Type": "application/json"} | ||
| ) | ||
| except Exception as e: | ||
| log.error("Error when calling method %s.", method_name) | ||
| log.error(e) | ||
| return Response(response=f"Error executing method: {method_name}.", status=500) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you 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. | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import inspect | ||
| import json | ||
| from functools import wraps | ||
| from typing import Callable, TypeVar | ||
|
|
||
| import requests | ||
|
|
||
| from airflow.configuration import conf | ||
| from airflow.exceptions import AirflowConfigException, AirflowException | ||
| from airflow.serialization.serialized_objects import BaseSerialization | ||
| from airflow.typing_compat import ParamSpec | ||
|
|
||
| PS = ParamSpec("PS") | ||
| RT = TypeVar("RT") | ||
|
|
||
|
|
||
| class InternalApiConfig: | ||
| """Stores and caches configuration for Internal API.""" | ||
|
|
||
| _initialized = False | ||
| _use_internal_api = False | ||
| _internal_api_endpoint = "" | ||
|
|
||
| @staticmethod | ||
| def get_use_internal_api(): | ||
| if not InternalApiConfig._initialized: | ||
| InternalApiConfig._init_values() | ||
| return InternalApiConfig._use_internal_api | ||
|
|
||
| @staticmethod | ||
| def get_internal_api_endpoint(): | ||
| if not InternalApiConfig._initialized: | ||
| InternalApiConfig._init_values() | ||
| return InternalApiConfig._internal_api_endpoint | ||
|
|
||
| @staticmethod | ||
| def _init_values(): | ||
| use_internal_api = conf.getboolean("core", "database_access_isolation") | ||
| internal_api_endpoint = "" | ||
| if use_internal_api: | ||
| internal_api_url = conf.get("core", "internal_api_url") | ||
| internal_api_endpoint = internal_api_url + "/internal_api/v1/rpcapi" | ||
| if not internal_api_endpoint.startswith("http://"): | ||
| raise AirflowConfigException("[core]internal_api_url must start with http://") | ||
|
|
||
| InternalApiConfig._initialized = True | ||
| InternalApiConfig._use_internal_api = use_internal_api | ||
| InternalApiConfig._internal_api_endpoint = internal_api_endpoint | ||
|
|
||
|
|
||
| def internal_api_call(func: Callable[PS, RT | None]) -> Callable[PS, RT | None]: | ||
| """Decorator for methods which may be executed in database isolation mode. | ||
|
|
||
| If [core]database_access_isolation is true then such method are not executed locally, | ||
| but instead RPC call is made to Database API (aka Internal API). This makes some components | ||
| decouple from direct Airflow database access. | ||
| Each decorated method must be present in METHODS list in airflow.api_internal.endpoints.rpc_api_endpoint. | ||
potiuk marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Only static methods can be decorated. This decorator must be before "provide_session". | ||
|
|
||
| See [AIP-44](https://cwiki.apache.org/confluence/display/AIRFLOW/AIP-44+Airflow+Internal+API) | ||
| for more information . | ||
| """ | ||
| headers = { | ||
| "Content-Type": "application/json", | ||
| } | ||
|
|
||
| def make_jsonrpc_request(method_name: str, params_json: str) -> bytes: | ||
| data = {"jsonrpc": "2.0", "method": method_name, "params": params_json} | ||
| internal_api_endpoint = InternalApiConfig.get_internal_api_endpoint() | ||
| response = requests.post(url=internal_api_endpoint, data=json.dumps(data), headers=headers) | ||
| if response.status_code != 200: | ||
| raise AirflowException( | ||
| f"Got {response.status_code}:{response.reason} when sending the internal api request." | ||
| ) | ||
| return response.content | ||
|
|
||
| @wraps(func) | ||
| def wrapper(*args, **kwargs) -> RT | None: | ||
| use_internal_api = InternalApiConfig.get_use_internal_api() | ||
| if not use_internal_api: | ||
| return func(*args, **kwargs) | ||
|
|
||
| bound = inspect.signature(func).bind(*args, **kwargs) | ||
| arguments_dict = dict(bound.arguments) | ||
| if "session" in arguments_dict: | ||
| del arguments_dict["session"] | ||
| args_json = json.dumps(BaseSerialization.serialize(arguments_dict)) | ||
| method_name = f"{func.__module__}.{func.__name__}" | ||
| result = make_jsonrpc_request(method_name, args_json) | ||
| if result: | ||
| return BaseSerialization.deserialize(json.loads(result)) | ||
| else: | ||
| return None | ||
|
|
||
| return wrapper | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| # Licensed to the Apache Software Foundation (ASF) under one | ||
| # or more contributor license agreements. See the NOTICE file | ||
| # distributed with this work for additional information | ||
| # regarding copyright ownership. The ASF licenses this file | ||
| # to you 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. | ||
|
|
||
| --- | ||
| openapi: 3.0.2 | ||
| info: | ||
| title: Airflow Internal API | ||
| version: 1.0.0 | ||
| description: | | ||
| This is Airflow Internal API - which is a proxy for components running | ||
| customer code for connecting to Airflow Database. | ||
|
|
||
| It is not intended to be used by any external code. | ||
|
|
||
| You can find more information in AIP-44 | ||
| https://cwiki.apache.org/confluence/display/AIRFLOW/AIP-44+Airflow+Internal+API | ||
|
|
||
|
|
||
| servers: | ||
| - url: /internal_api/v1 | ||
| description: Airflow Internal API | ||
| paths: | ||
| "/rpcapi": | ||
| post: | ||
| operationId: rpcapi | ||
| deprecated: false | ||
| x-openapi-router-controller: airflow.api_internal.endpoints.rpc_api_endpoint | ||
| operationId: internal_airflow_api | ||
| tags: | ||
| - JSONRPC | ||
| parameters: [] | ||
| responses: | ||
| '200': | ||
| description: Successful response | ||
| requestBody: | ||
| x-body-name: body | ||
| required: true | ||
| content: | ||
| application/json: | ||
| schema: | ||
| type: object | ||
| required: | ||
| - method | ||
| - jsonrpc | ||
| - params | ||
| properties: | ||
| jsonrpc: | ||
| type: string | ||
| default: '2.0' | ||
| description: JSON-RPC Version (2.0) | ||
| method: | ||
| type: string | ||
| description: Method name | ||
| params: | ||
| title: Parameters | ||
| type: string | ||
| x-headers: [] | ||
| x-explorer-enabled: true | ||
| x-proxy-enabled: true | ||
| components: | ||
| schemas: | ||
| JsonRpcRequired: | ||
|
||
| type: object | ||
| required: | ||
| - method | ||
| - jsonrpc | ||
| properties: | ||
| method: | ||
| type: string | ||
| description: Method name | ||
| jsonrpc: | ||
| type: string | ||
| default: '2.0' | ||
| description: JSON-RPC Version (2.0) | ||
| discriminator: | ||
| propertyName: method_name | ||
| tags: [] | ||
Uh oh!
There was an error while loading. Please reload this page.