Skip to content

Commit 9408861

Browse files
committed
WIP: add better locking to cache workers
1 parent d453c07 commit 9408861

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

django/applications/catmaid/control/node.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2158,7 +2158,7 @@ def update_grid_cell(project_id, grid_id, w_i, h_i, d_i, cell_width,
21582158
cell_height, cell_depth, params, allow_empty, lod_levels,
21592159
lod_bucket_size, lod_strategy, update_json_cache,
21602160
update_json_text_cache, update_msgpack_cache, provider=None,
2161-
cursor=None, delete_empty=True) -> bool:
2161+
cursor=None, delete_empty=True, ignore_locked=True) -> bool:
21622162
params['left'] = w_i * cell_width
21632163
params['right'] = (w_i + 1) * cell_width
21642164
params['top'] = h_i * cell_height
@@ -2172,6 +2172,35 @@ def update_grid_cell(project_id, grid_id, w_i, h_i, d_i, cell_width,
21722172
if not cursor:
21732173
cursor = connection.cursor()
21742174

2175+
# Put a lock on this grid cache_cell. If ignore_locked is true, this update
2176+
# call is canceled if no lock can be acquired right away.
2177+
try:
2178+
cursor.execute(f"""
2179+
SELECT grid_id
2180+
FROM node_grid_cache_cell
2181+
WHERE grid_id = %(grid_id)s AND x_index = %(x_index)s AND
2182+
y_index = %(y_index)s AND z_index = %(z_index)s
2183+
FOR ${'' if delete_empty and not allow_empty else 'NO KEY'} UPDATE
2184+
${'NOWAIT' if ignore_locked else ''}
2185+
LIMIT 1;
2186+
""", {
2187+
'grid_id': grid_id,
2188+
'x_index': w_i,
2189+
'y_index': h_i,
2190+
'z_index': d_i,
2191+
})
2192+
except e as Error:
2193+
logger.error(e)
2194+
# We couldn't get the lock.
2195+
if ignore_locked:
2196+
return True
2197+
pass
2198+
2199+
n_cells = cursor.fetchall()[0]
2200+
if n_cells == 0:
2201+
# If this cell is already locked,
2202+
return
2203+
21752204
result_tuple = _node_list_tuples_query(params, project_id, provider,
21762205
include_labels=True)
21772206

django/applications/catmaid/management/commands/catmaid_cache_update_worker.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ def update(self, updates, cursor):
5151
g = grid_map[update['grid_id']]
5252
w_i, h_i, d_i = update['x'], update['y'], update['z']
5353

54+
# Lock dirty cell
55+
5456
params = {
5557
'project_id': g.project_id,
5658
'limit': settings.NODE_LIST_MAXIMUM_COUNT,
@@ -68,7 +70,7 @@ def update(self, updates, cursor):
6870
params, g.allow_empty, g.n_lod_levels, g.lod_min_bucket_size,
6971
g.lod_strategy, g.has_json_data, g.has_json_text_data,
7072
g.has_msgpack_data, provider=provider, cursor=cursor,
71-
delete_empty=True)
73+
delete_empty=True, ignore_locked=True)
7274

7375
if updated:
7476
updated_cells += 1

0 commit comments

Comments
 (0)