diff --git a/config/settings/container/production.py b/config/settings/container/production.py index 7c70baa6d21c1468e185dcb5f785062b7ea5f2bc..ddd14cac0bffcfd6bd4e9197cdf3fb9f895c1e08 100644 --- a/config/settings/container/production.py +++ b/config/settings/container/production.py @@ -16,16 +16,23 @@ if (isinstance(is_priority_server, str) and is_priority_server.lower() in ['true', 't']): PRIORITY_SERVER = True -## If priority server, do some things -#if PRIORITY_SERVER: -# # Add custom permissions for API -# default_perms = list(REST_FRAMEWORK['DEFAULT_PERMISSION_CLASSES']) -# default_perms = ['api.permissions.IsPriorityUser'] + default_perms -# REST_FRAMEWORK['DEFAULT_PERMISSION_CLASSES'] = tuple(default_perms) -# -# # Don't do anything to databases. Priority servers use the master -# # for both read and write operations -#else: +# If priority server, only allow priority users +if PRIORITY_SERVER: + # Add custom permissions for API + default_perms = list(REST_FRAMEWORK['DEFAULT_PERMISSION_CLASSES']) + default_perms = ['api.permissions.IsPriorityUser'] + default_perms + REST_FRAMEWORK['DEFAULT_PERMISSION_CLASSES'] = tuple(default_perms) + + +# TP, March 2019: for now, it looks infeasible to use multiple databases +# since there are many operations which normal LVC users can do that +# do a write and then a read very soon after. And we can't rely on +# the read replica being updated quickly enough for that to work. +# So there are several workflows that need to be redone in order for +# this to be possible, but it's not obvious that they even can be +# reworked properly. I.e. this is a much bigger project than expected +# so we're going to have to revisit it at some point. We'll leave the +# config here for now. # # If not a priority server, we use the read-only replica database # # for reads and master for writes. # # The username, password, and database name are all replicated diff --git a/gracedb/core/db/routers.py b/gracedb/core/db/routers.py index ef951525500809d2300b4466c0bd23b4cb11f8c8..a9f8dc79ac9616100a6103e5004e0ca7029e9e4b 100644 --- a/gracedb/core/db/routers.py +++ b/gracedb/core/db/routers.py @@ -1,10 +1,29 @@ +import logging + +from django.conf import settings + + +# Set up logger +logger = logging.getLogger(__name__) class NonPriorityRouter(object): """For non-priority production workers in a swarm container deployment""" def db_for_read(self, model, **hints): + # For API throttle caches and sessions, we want to always read out + # of the master (or use a separate database) + if (model._meta.db_table == settings.CACHES['throttles']['LOCATION']): + return 'default' + elif (model._meta.app_label == 'user_sessions' and + model._meta.model_name == 'session'): + return 'default' + + # Otherwise, use the read replica return 'read_replica' def db_for_write(self, model, **hints): return 'default' + + def allow_relation(self, obj1, obj2, **hints): + return True