-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathtest_streaming_job_scheduling.py
More file actions
246 lines (180 loc) · 7.72 KB
/
test_streaming_job_scheduling.py
File metadata and controls
246 lines (180 loc) · 7.72 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import copy
import hashlib
import json
from datetime import datetime
from unittest.mock import Mock, patch
import pytest
from feast import Client as FeastClient
from feast.config import Config
from feast.data_format import AvroFormat
from feast.data_source import KafkaSource
from feast.feature_table import FeatureTable
from feast_spark.client import Client
from feast_spark.job_service import ensure_stream_ingestion_jobs
from feast_spark.pyspark.abc import SparkJobStatus, StreamIngestionJob
from feast_spark.pyspark.launcher import _feature_table_to_argument, _source_to_argument
@pytest.fixture
def feast_client():
c = FeastClient(
job_service_pause_between_jobs=0,
options={
"whitelisted_projects": "default,ride",
"lock_mgr_redis_host": "localhost",
"lock_mgr_redis_port": "0",
},
)
c.list_projects = Mock(return_value=["default", "ride", "invalid_project"])
c.list_feature_tables = Mock()
yield c
@pytest.fixture
def spark_client(feast_client):
c = Client(feast_client)
c.list_jobs = Mock()
c.start_stream_to_online_ingestion = Mock()
yield c
@pytest.fixture
def feature_table():
return FeatureTable(
name="ft",
entities=[],
features=[],
stream_source=KafkaSource(
topic="t",
bootstrap_servers="",
message_format=AvroFormat(""),
event_timestamp_column="",
),
)
class SimpleStreamingIngestionJob(StreamIngestionJob):
def __init__(
self, id: str, project: str, feature_table: FeatureTable, status: SparkJobStatus
):
self._id = id
self._feature_table = feature_table
self._project = project
self._status = status
self._hash = hash
def get_hash(self) -> str:
source = _source_to_argument(self._feature_table.stream_source, Config())
feature_table = _feature_table_to_argument(None, self._project, self._feature_table) # type: ignore
job_json = json.dumps(
{"source": source, "feature_table": feature_table}, sort_keys=True,
)
return hashlib.md5(job_json.encode()).hexdigest()
def get_feature_table(self) -> str:
return self._feature_table.name
def get_id(self) -> str:
return self._id
def get_status(self) -> SparkJobStatus:
return self._status
def cancel(self):
self._status = SparkJobStatus.COMPLETED
def get_start_time(self) -> datetime:
pass
@patch("redis.Redis")
def test_new_job_creation(mock_redis, spark_client, feature_table):
""" No job existed prior to call """
spark_client.feature_store.list_feature_tables.return_value = [feature_table]
spark_client.list_jobs.return_value = []
ensure_stream_ingestion_jobs(spark_client, all_projects=True)
assert spark_client.start_stream_to_online_ingestion.call_count == 2
def test_no_changes(spark_client, feature_table):
""" Feature Table spec is the same """
job = SimpleStreamingIngestionJob(
"", "default", feature_table, SparkJobStatus.IN_PROGRESS
)
job2 = SimpleStreamingIngestionJob(
"", "ride", feature_table, SparkJobStatus.IN_PROGRESS
)
spark_client.feature_store.list_feature_tables.return_value = [feature_table]
spark_client.list_jobs.return_value = [job, job2]
ensure_stream_ingestion_jobs(spark_client, all_projects=True)
assert job.get_status() == SparkJobStatus.IN_PROGRESS
spark_client.start_stream_to_online_ingestion.assert_not_called()
@patch("redis.Redis")
def test_update_existing_job(mock_redis, spark_client, feature_table):
""" Feature Table spec was updated """
new_ft = copy.deepcopy(feature_table)
new_ft.stream_source._kafka_options.topic = "new_t"
job = SimpleStreamingIngestionJob(
"", "default", feature_table, SparkJobStatus.IN_PROGRESS
)
spark_client.feature_store.list_feature_tables.return_value = [new_ft]
spark_client.list_jobs.return_value = [job]
ensure_stream_ingestion_jobs(spark_client, all_projects=True)
assert job.get_status() == SparkJobStatus.COMPLETED
assert spark_client.start_stream_to_online_ingestion.call_count == 2
@patch("redis.Redis")
def test_not_cancelling_starting_job(mock_redis, spark_client, feature_table):
""" Feature Table spec was updated but previous version is still starting """
new_ft = copy.deepcopy(feature_table)
new_ft.stream_source._kafka_options.topic = "new_t"
job = SimpleStreamingIngestionJob(
"", "default", feature_table, SparkJobStatus.STARTING
)
spark_client.feature_store.list_feature_tables.return_value = [new_ft]
spark_client.list_jobs.return_value = [job]
ensure_stream_ingestion_jobs(spark_client, all_projects=True)
assert job.get_status() == SparkJobStatus.STARTING
assert spark_client.start_stream_to_online_ingestion.call_count == 2
@patch("redis.Redis")
def test_not_retrying_failed_job(mock_redis, spark_client, feature_table):
""" Job has failed on previous try """
job = SimpleStreamingIngestionJob(
"", "default", feature_table, SparkJobStatus.FAILED
)
spark_client.feature_store.list_feature_tables.return_value = [feature_table]
spark_client.list_jobs.return_value = [job]
ensure_stream_ingestion_jobs(spark_client, all_projects=True)
spark_client.list_jobs.assert_called_once_with(include_terminated=True)
assert job.get_status() == SparkJobStatus.FAILED
spark_client.start_stream_to_online_ingestion.assert_called_once_with(
feature_table, [], project="ride"
)
@patch("redis.Redis")
def test_restarting_completed_job(mock_redis, spark_client, feature_table):
""" Job has succesfully finished on previous try """
job = SimpleStreamingIngestionJob(
"", "default", feature_table, SparkJobStatus.COMPLETED
)
spark_client.feature_store.list_feature_tables.return_value = [feature_table]
spark_client.list_jobs.return_value = [job]
ensure_stream_ingestion_jobs(spark_client, all_projects=True)
assert spark_client.start_stream_to_online_ingestion.call_count == 2
@patch("redis.Redis")
def test_stopping_running_job(mock_redis, spark_client, feature_table):
""" Streaming source was deleted """
new_ft = copy.deepcopy(feature_table)
new_ft.stream_source = None
job = SimpleStreamingIngestionJob(
"", "default", feature_table, SparkJobStatus.IN_PROGRESS
)
spark_client.feature_store.list_feature_tables.return_value = [new_ft]
spark_client.list_jobs.return_value = [job]
ensure_stream_ingestion_jobs(spark_client, all_projects=True)
assert job.get_status() == SparkJobStatus.COMPLETED
spark_client.start_stream_to_online_ingestion.assert_not_called()
@patch("redis.Redis")
def test_restarting_failed_jobs(mock_redis, feature_table):
""" If configured - restart failed jobs """
feast_client = FeastClient(
job_service_pause_between_jobs=0,
job_service_retry_failed_jobs=True,
options={
"whitelisted_projects": "default,ride",
"lock_mgr_redis_host": "localhost",
"lock_mgr_redis_port": "0",
},
)
feast_client.list_projects = Mock(return_value=["default"])
feast_client.list_feature_tables = Mock()
spark_client = Client(feast_client)
spark_client.list_jobs = Mock()
spark_client.start_stream_to_online_ingestion = Mock()
spark_client.feature_store.list_feature_tables.return_value = [feature_table]
spark_client.list_jobs.return_value = []
ensure_stream_ingestion_jobs(spark_client, all_projects=True)
spark_client.list_jobs.assert_called_once_with(include_terminated=False)
spark_client.start_stream_to_online_ingestion.assert_called_once_with(
feature_table, [], project="default"
)