From e6a41f4d1453f750a4721cb57cdf6a28369ac4ef Mon Sep 17 00:00:00 2001
From: Tanner Prestegard <tanner.prestegard@ligo.org>
Date: Tue, 12 Mar 2019 11:35:11 -0500
Subject: [PATCH] Remove multi-DB config for production

Not feasible to use multiple DBs for read or write operations at
present. We're going to have to revisit this at some point. We leave
most of the code in place but commented out.
---
 config/settings/container/production.py | 27 ++++++++++++++++---------
 gracedb/core/db/routers.py              | 19 +++++++++++++++++
 2 files changed, 36 insertions(+), 10 deletions(-)

diff --git a/config/settings/container/production.py b/config/settings/container/production.py
index 7c70baa6d..ddd14cac0 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 ef9515255..a9f8dc79a 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
-- 
GitLab