Skip to content

Commit 4c7850c

Browse files
committed
Convert job ID to uuid
In some CI runs it was observed that unexpected results were being returned for middleware jobs. This commit converts our job ids from being monotonically incrementing integer to proper uuid so that the job id that client is trying to track is guaranteed to uniquely identify it regardless of which HA node is being connected to.
1 parent 4f404a5 commit 4c7850c

File tree

2 files changed

+7
-12
lines changed

2 files changed

+7
-12
lines changed

src/middlewared/middlewared/job.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import asyncio
22
import contextlib
3-
from collections import OrderedDict
43
import copy
54
import enum
65
import errno
@@ -17,6 +16,7 @@
1716
from middlewared.pipe import Pipes
1817
from middlewared.utils.privilege import credential_is_limited_to_own_jobs, credential_has_full_admin
1918
from middlewared.utils.time_utils import utc_now
19+
from uuid import uuid4
2020

2121

2222
logger = logging.getLogger(__name__)
@@ -225,8 +225,7 @@ class JobsDeque:
225225

226226
def __init__(self, maxlen=1000):
227227
self.maxlen = maxlen
228-
self.count = 0
229-
self.__dict = OrderedDict()
228+
self.__dict = {}
230229
with contextlib.suppress(FileNotFoundError):
231230
shutil.rmtree(LOGS_DIR)
232231

@@ -244,7 +243,6 @@ def _get_next_id(self):
244243
return self.count
245244

246245
def add(self, job):
247-
job.set_id(self._get_next_id())
248246
if len(self.__dict) > self.maxlen:
249247
for old_job_id, old_job in self.__dict.items():
250248
if old_job.state in (State.SUCCESS, State.FAILED, State.ABORTED):
@@ -291,7 +289,7 @@ def __init__(self, middleware, method_name, serviceobj, method, args, options, p
291289
self.app = app
292290
self.audit_callback = audit_callback
293291

294-
self.id = None
292+
self.id = str(uuid4())
295293
self.lock = None
296294
self.result = None
297295
self.error = None
@@ -377,9 +375,6 @@ def get_lock_name(self):
377375
errno.EINVAL)
378376
return lock_name
379377

380-
def set_id(self, id_):
381-
self.id = id_
382-
383378
def set_result(self, result):
384379
self.result = result
385380

src/middlewared/middlewared/service/core_service.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ def __job_by_credential_and_id(self, credential, job_id, access):
109109
@filterable
110110
@filterable_returns(Dict(
111111
'job',
112-
Int('id'),
112+
Str('id'),
113113
Str('method'),
114114
List('arguments'),
115115
Bool('transient'),
@@ -178,7 +178,7 @@ def get_jobs(self, app, filters, options):
178178
return jobs
179179

180180
@no_authz_required
181-
@accepts(Int('id'), Str('filename'), Bool('buffered', default=False))
181+
@accepts(Str('id'), Str('filename'), Bool('buffered', default=False))
182182
@pass_app(rest=True)
183183
async def job_download_logs(self, app, id_, filename, buffered):
184184
"""
@@ -195,15 +195,15 @@ async def job_download_logs(self, app, id_, filename, buffered):
195195
return (await self._download(app, 'filesystem.get', [job.logs_path], filename, buffered))[1]
196196

197197
@no_authz_required
198-
@accepts(Int('id'))
198+
@accepts(Str('id'))
199199
@job()
200200
async def job_wait(self, job, id_):
201201
target_job = self.__job_by_credential_and_id(job.credentials, id_, JobAccess.READ)
202202

203203
return await job.wrap(target_job)
204204

205205
@private
206-
@accepts(Int('id'), Dict(
206+
@accepts(Str('id'), Dict(
207207
'job-update',
208208
Dict('progress', additional_attrs=True),
209209
))

0 commit comments

Comments
 (0)