Skip to content

Commit 076d1ff

Browse files
committed
Merge branch 'tickets/DM-51696'
2 parents b37ce6c + 8667067 commit 076d1ff

File tree

3 files changed

+32
-18
lines changed

3 files changed

+32
-18
lines changed

python/activator/activator.py

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -170,16 +170,21 @@ def _get_storage_client():
170170

171171

172172
@functools.cache
173-
def _get_central_butler(uri):
173+
def _get_write_butler():
174174
"""Lazy initialization of central Butler.
175+
"""
176+
return get_central_butler(write_repo, instrument_name, writeable=True)
175177

176-
Parameters
177-
----------
178-
uri : `str`
179-
The URI of the repository to load. Only one Butler object is
180-
constructed for any repository.
178+
179+
@functools.cache
180+
def _get_read_butler():
181+
"""Lazy initialization of central Butler.
181182
"""
182-
return get_central_butler(uri, instrument_name)
183+
if read_repo != write_repo:
184+
return get_central_butler(read_repo, instrument_name, writeable=False)
185+
else:
186+
# Don't initialize an extra Butler from scratch
187+
return _get_write_butler()
183188

184189

185190
@functools.cache
@@ -192,7 +197,7 @@ def _get_local_repo():
192197
The directory containing the repo, to be removed when the
193198
process exits.
194199
"""
195-
repo = make_local_repo(local_repos, _get_central_butler(read_repo), instrument_name)
200+
repo = make_local_repo(local_repos, _get_read_butler(), instrument_name)
196201
tracker = LocalRepoTracker.get()
197202
tracker.register(os.getpid(), repo.name)
198203
return repo
@@ -452,8 +457,8 @@ def create_app():
452457
# Check initialization and abort early
453458
_get_consumer()
454459
_get_storage_client()
455-
_get_central_butler(read_repo)
456-
_get_central_butler(write_repo)
460+
_get_read_butler()
461+
_get_write_butler()
457462
_get_local_repo()
458463

459464
app = flask.Flask(__name__)
@@ -500,8 +505,8 @@ def keda_start():
500505
# Check initialization and abort early
501506
_get_consumer()
502507
_get_storage_client()
503-
_get_central_butler(read_repo)
504-
_get_central_butler(write_repo)
508+
_get_read_butler()
509+
_get_write_butler()
505510
_get_local_repo()
506511

507512
redis_session = RedisStreamSession(
@@ -923,8 +928,8 @@ def process_visit(expected_visit: FannedOutVisit):
923928

924929
# Create a fresh MiddlewareInterface object to avoid accidental
925930
# "cross-talk" between different visits.
926-
mwi = MiddlewareInterface(_get_central_butler(read_repo),
927-
_get_central_butler(write_repo),
931+
mwi = MiddlewareInterface(_get_read_butler(),
932+
_get_write_butler(),
928933
image_bucket,
929934
expected_visit,
930935
pre_pipelines,

python/activator/middleware_interface.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@
9191

9292

9393
@connect.retry(2, SQL_EXCEPTIONS, wait=repo_retry)
94-
def get_central_butler(central_repo: str, instrument_class: str):
94+
def get_central_butler(central_repo: str, instrument_class: str, writeable: bool):
9595
"""Provide a Butler that can access the given repository and read and write
9696
data for the given instrument.
9797
@@ -102,6 +102,8 @@ def get_central_butler(central_repo: str, instrument_class: str):
102102
instrument_class : `str`
103103
The name of the instrument whose data will be retrieved or written. May
104104
be either the fully qualified class name or the short name.
105+
writeable : `bool`
106+
Whether or not it's safe to attempt writes to this Butler.
105107
106108
Returns
107109
-------
@@ -110,7 +112,7 @@ def get_central_butler(central_repo: str, instrument_class: str):
110112
``instrument_name`` data.
111113
"""
112114
return Butler(central_repo,
113-
writeable=True,
115+
writeable=writeable,
114116
inferDefaults=False,
115117
)
116118

tests/test_middleware_interface.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,14 +291,21 @@ def setUp(self):
291291
prefix="file://")
292292

293293
def test_get_butler(self):
294-
for butler in [get_central_butler(self.central_repo, "lsst.obs.lsst.LsstComCamSim"),
295-
get_central_butler(self.central_repo, instname),
294+
for butler in [get_central_butler(self.central_repo, "lsst.obs.lsst.LsstComCamSim", writeable=True),
295+
get_central_butler(self.central_repo, instname, writeable=True),
296296
]:
297297
# TODO: better way to test repo location?
298298
self.assertTrue(
299299
butler.getURI("skyMap", skymap=skymap_name, collections=f"{instname}/defaults").ospath
300300
.startswith(self.central_repo))
301301
self.assertTrue(butler.isWriteable())
302+
for butler in [get_central_butler(self.central_repo, "lsst.obs.lsst.LsstComCamSim", writeable=False),
303+
get_central_butler(self.central_repo, instname, writeable=False),
304+
]:
305+
self.assertTrue(
306+
butler.getURI("skyMap", skymap=skymap_name, collections=f"{instname}/defaults").ospath
307+
.startswith(self.central_repo))
308+
self.assertFalse(butler.isWriteable())
302309

303310
def test_make_local_repo(self):
304311
for inst in [instname, "lsst.obs.lsst.LsstComCamSim"]:

0 commit comments

Comments
 (0)