Error while benchmarking Django 1.8 with multiple databases

I am moving a Django 1.8 project from one database to a read / write setup. I ran into the problem described in Django bug 23718 but the described work didn't help.

Does anyone face similar issues? The relevant code segments are below:

Router:

class DatabaseRouter(object):

    """Router to handle separation of reads and writes."""

    def db_for_read(self, model, **hints):
        """Reads go to a read replica."""
        return 'read'

    def db_for_write(self, model, **hints):
        """Writes always go to default."""
        return 'default'

    def allow_relation(self, obj1, obj2, **hints):
        """Allow relations bet/n objects in the same DB cluster."""
        db_list = ('default', 'read')
        if obj1._state.db in db_list and obj2._state.db in db_list:
            return True
        return None

    def allow_migrate(self, db, app_label, model_name=None, **hints):
        """All models end up in this pool."""
        return True

      

corresponding database settings

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': DB_NAME,
        'USER': DB_USER,
        'PASSWORD': DB_PASS,
        'HOST': DB_WRITE
    },
    'read': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': DB_NAME,
        'USER': DB_USER,
        'PASSWORD': DB_PASS,
        'HOST': DB_READ,
        'TEST': {
            'MIRROR': 'default',
        },
    }
}

DATABASE_ROUTERS = ['my_project.routers.DatabaseRouter']

      

work on test replication check:

class ReplicationTestCase(TestCase):

    @classmethod
    def setUpClass(cls):
        super(ReplicationTestCase, cls).setUpClass()
        connections['read']._orig_cursor = connections['read'].cursor
        connections['read'].cursor = connections['default'].cursor

    @classmethod
    def tearDownClass(cls):
        connections['read'].cursor = connections['read']._orig_cursor
        super(ReplicationTestCase, cls).tearDownClass()

      

Anything pop out? I am happy to provide stacktraces from our test environment if helpful. Thank you!

+3


source to share





All Articles