-
Notifications
You must be signed in to change notification settings - Fork 4
/
servicer.py
304 lines (276 loc) · 12.8 KB
/
servicer.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
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# Copyright 2022 TIER IV, INC. All rights reserved.
#
# 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.
"""OTA Service API v2 implementation."""
from __future__ import annotations
import asyncio
import logging
import time
from concurrent.futures import ThreadPoolExecutor
from functools import partial
from typing import Dict
from otaclient.configs import ECUContact
from otaclient.configs.cfg import cfg, ecu_info, proxy_info
from otaclient.grpc._otaproxy_ctx import OTAProxyLauncher
from otaclient.grpc.api_v2.ecu_status import ECUStatusStorage
from otaclient.grpc.api_v2.types import convert_from_apiv2_update_request
from otaclient.ota_core import OTAClient, OTAClientControlFlags
from otaclient_api.v2 import types as api_types
from otaclient_api.v2.api_caller import ECUNoResponse, OTAClientCall
logger = logging.getLogger(__name__)
class OTAClientAPIServicer:
"""Handlers for otaclient service API.
This class also handles otaproxy lifecyle and dependence managing.
"""
OTAPROXY_SHUTDOWN_DELAY = cfg.OTAPROXY_MINIMUM_SHUTDOWN_INTERVAL
def __init__(
self,
otaclient_inst: OTAClient,
ecu_status_storage: ECUStatusStorage,
*,
control_flag: OTAClientControlFlags,
executor: ThreadPoolExecutor,
):
self._executor = executor
self._run_in_executor = partial(
asyncio.get_running_loop().run_in_executor, executor
)
self.sub_ecus = ecu_info.secondaries
self.listen_addr = ecu_info.ip_addr
self.listen_port = cfg.OTA_API_SERVER_PORT
self.my_ecu_id = ecu_info.ecu_id
self._otaclient_control_flags = control_flag
self._otaclient_inst = otaclient_inst
self._ecu_status_storage = ecu_status_storage
self._polling_waiter = self._ecu_status_storage.get_polling_waiter()
# otaproxy lifecycle and dependency managing
# NOTE: _debug_status_checking_shutdown_event is for test only,
# allow us to stop background task without changing codes.
# In normal running this event will never be set.
self._debug_status_checking_shutdown_event = asyncio.Event()
if proxy_info.enable_local_ota_proxy:
self._otaproxy_launcher = OTAProxyLauncher(executor=executor)
asyncio.create_task(self._otaproxy_lifecycle_managing())
asyncio.create_task(self._otaclient_control_flags_managing())
else:
# if otaproxy is not enabled, no dependency relationship will be formed,
# always allow local otaclient to reboot
self._otaclient_control_flags.set_can_reboot_flag()
# internal
async def _otaproxy_lifecycle_managing(self):
"""Task entry for managing otaproxy's launching/shutdown.
NOTE: cache_dir cleanup is handled here, when all ECUs are in SUCCESS ota_status,
cache_dir will be removed.
"""
otaproxy_last_launched_timestamp = 0
while not self._debug_status_checking_shutdown_event.is_set():
cur_timestamp = int(time.time())
any_requires_network = self._ecu_status_storage.any_requires_network
if self._otaproxy_launcher.is_running:
# NOTE: do not shutdown otaproxy too quick after it just starts!
# If otaproxy just starts less than <OTAPROXY_SHUTDOWN_DELAY> seconds,
# skip the shutdown this time.
if (
not any_requires_network
and cur_timestamp
> otaproxy_last_launched_timestamp + self.OTAPROXY_SHUTDOWN_DELAY
):
await self._otaproxy_launcher.stop()
otaproxy_last_launched_timestamp = 0
else: # otaproxy is not running
if any_requires_network:
await self._otaproxy_launcher.start(init_cache=False)
otaproxy_last_launched_timestamp = cur_timestamp
# when otaproxy is not running and any_requires_network is False,
# cleanup the cache dir when all ECUs are in SUCCESS ota_status
elif self._ecu_status_storage.all_success:
self._otaproxy_launcher.cleanup_cache_dir()
await self._polling_waiter()
async def _otaclient_control_flags_managing(self):
"""Task entry for set/clear otaclient control flags.
Prevent self ECU from rebooting when their is at least one ECU
under UPDATING ota_status.
"""
while not self._debug_status_checking_shutdown_event.is_set():
_can_reboot = self._otaclient_control_flags.is_can_reboot_flag_set()
if not self._ecu_status_storage.in_update_child_ecus_id:
if not _can_reboot:
logger.info(
"local otaclient can reboot as no child ECU is in UPDATING ota_status"
)
self._otaclient_control_flags.set_can_reboot_flag()
else:
if _can_reboot:
logger.info(
f"local otaclient cannot reboot as child ECUs {self._ecu_status_storage.in_update_child_ecus_id}"
" are in UPDATING ota_status"
)
self._otaclient_control_flags.clear_can_reboot_flag()
await self._polling_waiter()
# API stub
async def update(
self, request: api_types.UpdateRequest
) -> api_types.UpdateResponse:
logger.info(f"receive update request: {request}")
update_acked_ecus = set()
response = api_types.UpdateResponse()
# first: dispatch update request to all directly connected subECUs
tasks: Dict[asyncio.Task, ECUContact] = {}
for ecu_contact in self.sub_ecus:
if not request.if_contains_ecu(ecu_contact.ecu_id):
continue
_task = asyncio.create_task(
OTAClientCall.update_call(
ecu_contact.ecu_id,
str(ecu_contact.ip_addr),
ecu_contact.port,
request=request,
timeout=cfg.WAITING_SUBECU_ACK_REQ_TIMEOUT,
)
)
tasks[_task] = ecu_contact
if tasks: # NOTE: input for asyncio.wait must not be empty!
done, _ = await asyncio.wait(tasks)
for _task in done:
try:
_ecu_resp: api_types.UpdateResponse = _task.result()
update_acked_ecus.update(_ecu_resp.ecus_acked_update)
response.merge_from(_ecu_resp)
except ECUNoResponse as e:
_ecu_contact = tasks[_task]
logger.warning(
f"{_ecu_contact} doesn't respond to update request on-time"
f"(within {cfg.WAITING_SUBECU_ACK_REQ_TIMEOUT}s): {e!r}"
)
# NOTE(20230517): aligns with the previous behavior that create
# response with RECOVERABLE OTA error for unresponsive
# ECU.
response.add_ecu(
api_types.UpdateResponseEcu(
ecu_id=_ecu_contact.ecu_id,
result=api_types.FailureType.RECOVERABLE,
)
)
tasks.clear()
# second: dispatch update request to local if required by incoming request
if update_req_ecu := request.find_ecu(self.my_ecu_id):
if not self._otaclient_inst.started:
logger.error("otaclient is not running, abort")
response.add_ecu(
api_types.UpdateResponseEcu(
ecu_id=self.my_ecu_id,
result=api_types.FailureType.UNRECOVERABLE,
)
)
elif self._otaclient_inst.is_busy:
response.add_ecu(
api_types.UpdateResponseEcu(
ecu_id=self.my_ecu_id,
result=api_types.FailureType.RECOVERABLE,
)
)
else:
self._run_in_executor(
self._otaclient_inst.update,
convert_from_apiv2_update_request(update_req_ecu),
).add_done_callback(
lambda _: logger.info("update execution thread finished")
)
update_acked_ecus.add(self.my_ecu_id)
response.add_ecu(
api_types.UpdateResponseEcu(
ecu_id=self.my_ecu_id,
result=api_types.FailureType.NO_FAILURE,
)
)
# finally, trigger ecu_status_storage entering active mode if needed
if update_acked_ecus:
logger.info(f"ECUs accept OTA request: {update_acked_ecus}")
asyncio.create_task(
self._ecu_status_storage.on_ecus_accept_update_request(
update_acked_ecus
)
)
return response
async def rollback(
self, request: api_types.RollbackRequest
) -> api_types.RollbackResponse:
logger.info(f"receive rollback request: {request}")
response = api_types.RollbackResponse()
# first: dispatch rollback request to all directly connected subECUs
tasks: Dict[asyncio.Task, ECUContact] = {}
for ecu_contact in self.sub_ecus:
if not request.if_contains_ecu(ecu_contact.ecu_id):
continue
_task = asyncio.create_task(
OTAClientCall.rollback_call(
ecu_contact.ecu_id,
str(ecu_contact.ip_addr),
ecu_contact.port,
request=request,
timeout=cfg.WAITING_SUBECU_ACK_REQ_TIMEOUT,
)
)
tasks[_task] = ecu_contact
if tasks: # NOTE: input for asyncio.wait must not be empty!
done, _ = await asyncio.wait(tasks)
for _task in done:
try:
_ecu_resp: api_types.RollbackResponse = _task.result()
response.merge_from(_ecu_resp)
except ECUNoResponse as e:
_ecu_contact = tasks[_task]
logger.warning(
f"{_ecu_contact} doesn't respond to rollback request on-time"
f"(within {cfg.WAITING_SUBECU_ACK_REQ_TIMEOUT}s): {e!r}"
)
# NOTE(20230517): aligns with the previous behavior that create
# response with RECOVERABLE OTA error for unresponsive
# ECU.
response.add_ecu(
api_types.RollbackResponseEcu(
ecu_id=_ecu_contact.ecu_id,
result=api_types.FailureType.RECOVERABLE,
)
)
tasks.clear()
# second: dispatch rollback request to local if required
if request.find_ecu(self.my_ecu_id):
if not self._otaclient_inst.started:
logger.error("otaclient is not running, abort")
response.add_ecu(
api_types.RollbackResponseEcu(
ecu_id=self.my_ecu_id,
result=api_types.FailureType.UNRECOVERABLE,
)
)
elif self._otaclient_inst.is_busy:
response.add_ecu(
api_types.RollbackResponseEcu(
ecu_id=self.my_ecu_id,
result=api_types.FailureType.RECOVERABLE,
)
)
else:
self._run_in_executor(self._otaclient_inst.rollback).add_done_callback(
lambda _: logger.info("rollback execution thread finished")
)
response.add_ecu(
api_types.RollbackResponseEcu(
ecu_id=self.my_ecu_id,
result=api_types.FailureType.NO_FAILURE,
)
)
return response
async def status(self, _=None) -> api_types.StatusResponse:
return await self._ecu_status_storage.export()