Repository navigation
Expand file tree
/
Copy pathjob.py
More file actions
164 lines (135 loc) · 5.33 KB
/
Copy pathjob.py
File metadata and controls
164 lines (135 loc) · 5.33 KB
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
import logging
import time
from typing import Optional
from kubernetes import client
from kubernetes.client.exceptions import ApiException
from feast.infra.common.materialization_job import (
MaterializationJob,
MaterializationJobStatus,
)
logger = logging.getLogger(__name__)
_MAX_RETRIES = 3
_RETRY_BACKOFF_BASE = 2
_RETRYABLE_STATUS_CODES = {429, 500, 502, 503, 504}
def _is_retryable(exc: ApiException) -> bool:
return exc.status in _RETRYABLE_STATUS_CODES
def _rbac_hint(status: int) -> str:
if status in (401, 403):
return (
" Check that the Feast server ServiceAccount has the required"
" Role/RoleBinding for sparkapplications and configmaps in this namespace."
)
return ""
_STATE_MAP = {
"": MaterializationJobStatus.WAITING,
"SUBMITTED": MaterializationJobStatus.WAITING,
"RUNNING": MaterializationJobStatus.RUNNING,
"COMPLETED": MaterializationJobStatus.SUCCEEDED,
"FAILED": MaterializationJobStatus.ERROR,
"SUBMISSION_FAILED": MaterializationJobStatus.ERROR,
"PENDING_RERUN": MaterializationJobStatus.WAITING,
"INVALIDATING": MaterializationJobStatus.WAITING,
"SUCCEEDING": MaterializationJobStatus.RUNNING,
"FAILING": MaterializationJobStatus.RUNNING,
"SUSPENDING": MaterializationJobStatus.WAITING,
"SUSPENDED": MaterializationJobStatus.WAITING,
"RESUMING": MaterializationJobStatus.WAITING,
"UNKNOWN": MaterializationJobStatus.WAITING,
}
assert len(_STATE_MAP) == 14
class CompletedMaterializationJob(MaterializationJob):
"""Lightweight stub for a FV whose materialization already succeeded.
Used by ``_build_per_fv_jobs`` so that each FV gets an independent job
object. Unlike ``SparkApplicationMaterializationJob``, this never polls
the K8s API — the outcome is already known from the registry state.
"""
def __init__(self, job_id: str):
super().__init__()
self._job_id = job_id
def status(self) -> MaterializationJobStatus:
return MaterializationJobStatus.SUCCEEDED
def error(self) -> Optional[BaseException]:
return None
def should_be_retried(self) -> bool:
return False
def job_id(self) -> str:
return f"feast-sa-{self._job_id}"
def url(self) -> Optional[str]:
return None
class SparkApplicationMaterializationJob(MaterializationJob):
def __init__(
self,
job_id: str,
namespace: str,
custom_api: client.CustomObjectsApi,
error: Optional[BaseException] = None,
):
super().__init__()
self._job_id = job_id
self.namespace = namespace
self.custom_api = custom_api
self._error: Optional[BaseException] = error
def status(self) -> MaterializationJobStatus:
if self._error is not None:
return MaterializationJobStatus.ERROR
obj = self._get_cr_with_retry()
if obj is None:
return (
MaterializationJobStatus.ERROR
if self._error
else MaterializationJobStatus.RUNNING
)
state = obj.get("status", {}).get("applicationState", {}).get("state", "")
result = _STATE_MAP.get(state, MaterializationJobStatus.WAITING)
if result == MaterializationJobStatus.ERROR:
msg = (
obj.get("status", {})
.get("applicationState", {})
.get("errorMessage", f"SparkApplication failed: {state}")
)
self._error = Exception(msg)
return result
def _get_cr_with_retry(self) -> Optional[dict]:
"""Fetch SparkApplication CR with exponential backoff on transient errors."""
last_exc = None
for attempt in range(_MAX_RETRIES):
try:
return self.custom_api.get_namespaced_custom_object(
group="sparkoperator.k8s.io",
version="v1beta2",
namespace=self.namespace,
plural="sparkapplications",
name=f"feast-sa-{self._job_id}",
)
except ApiException as e:
if e.status == 404:
self._error = Exception(
f"SparkApplication feast-sa-{self._job_id} not found"
)
return None
if not _is_retryable(e):
self._error = Exception(
f"Kubernetes API error polling feast-sa-{self._job_id}: "
f"HTTP {e.status} {e.reason}.{_rbac_hint(e.status)}"
)
return None
last_exc = e
wait = _RETRY_BACKOFF_BASE**attempt
logger.warning(
f"API poll attempt {attempt + 1}/{_MAX_RETRIES} failed "
f"(HTTP {e.status}), retrying in {wait}s"
)
time.sleep(wait)
self._error = Exception(
f"Failed to poll SparkApplication after {_MAX_RETRIES} attempts: "
f"{last_exc.reason if last_exc else 'unknown'}"
)
return None
def error(self) -> Optional[BaseException]:
return self._error
def should_be_retried(self) -> bool:
return False
def job_id(self) -> str:
return f"feast-sa-{self._job_id}"
def url(self) -> Optional[str]:
return None