[openstack-dev] [oslo.db]A proposal for DB read/write separation

Mike Bayer mbayer at redhat.com
Sun Aug 10 15:57:47 UTC 2014


On Aug 10, 2014, at 11:17 AM, Li Ma <skywalker.nick at gmail.com> wrote:

> 
> How about Galera multi-master cluster? As Mike Bayer said, it is virtually synchronous by default. It is still possible that outdated rows are queried that make results not stable.

not sure if I said that :).  I know extremely little about galera.


> 
> 
> Let's move forward to synchronous replication, like Galera with causal-reads on. The dominant advantage is that it has consistent relational dataset support. The disadvantage are that it uses optimistic locking and its performance sucks (also said by Mike Bayer :-). For optimistic locking problem, I think it can be dealt with by retry-on-deadlock. It's not the topic here.

I *really* don’t think I said that, because I like optimistic locking, and I’ve never used Galera ;).

Where I am ignorant here is of what exactly occurs if you write some rows within a transaction with Galera, then do some reads in that same transaction.   I’d totally guess that Galera would need to first have SELECTs come from a slave node, then the moment it sees any kind of DML / writing, it transparently switches the rest of the transaction over to a writer node.   No idea, but it has to be something like that?   


> 
> 
> So, the transparent read/write separation is dependent on such an environment. SQLalchemy tutorial provides code sample for it [1]. Besides, Mike Bayer also provides a blog post for it [2].

So this thing with the “django-style routers”, the way that example is, it actually would work poorly with a Session that is not in “autocommit” mode, assuming you’re working with regular old databases that are doing some simple behind-the-scenes replication.   Because again, if you do a flush, those rows go to the master, if the transaction is still open, then reading from the slaves you won’t see the rows you just inserted.    So in reality, that example is kind of crappy, if you’re in a transaction (which we are) you’d really need to be doing session.using_bind(“master”) all over the place, and that is already way too verbose and hardcoded.   I’m wondering why I didn’t make a huge note of that in the post.  The point of that article was more to show that hey, you *can* control it at this level if you want to but you need to know what you’re doing.

Just to put it out there, this is what I think good high/level master/slave separation in the app level (reiterating: *if we want it in the app level at all*) should approximately look like:

@transaction.writer
def read_and_write_something(arg1, arg2, …):
    # …

@transaction.reader
def only_read_something(arg1, arg2, …):
    # …

that way there is no awareness of master/slave anything, the underlying system can decide what “reader” and “writer” means.   Do in-app switching between two databases, send out some magic signals to some commercial clustering service, have the “readers” work in “autocommit” mode, or do nothing, whatever.  The code doesn’t decide this imperatively.    But it isn’t 100% “transparent”, this small amount of declaration per-method is needed.


> 
> What I did is to re-implement it in OpenStack DB API modules in my development environment, using Galera cluster(causal-reads on). It has been running perfectly for more than a week. The routing session manager works well while maintaining data consistency.

OK so Galera would perhaps have some way to make this happen, and that’s great.    My understanding is that people are running Openstack already with Galera, that’s why we’re hitting issues with some of those SELECT..FOR UPDATEs that are being replaced with optimistic approaches as you mention.     But beyond that this isn’t any kind of “change” to oslo.db or anything else.   Run Openstack with whatever database backend you want, ideally (that is my primary agenda, sorry MySQL vendors!).


> Finally, I think if we can integrate it into oslo.db, it is a perfect plus for those who would like to deploy Galera (or other similar technology) as DB backend.

this (the word “integrate”, and what does that mean) is really the only thing making me nervous.  If the integration here is the django blog post I have, it’s not going to work with transactions.   Either the system is magical enough that a single transaction can read/write from both sources midway and there is no “integration” needed, or the transaction has to be declared up front as reader or writer.  Or you don’t use transactions except for writers, which is essentially the same as “declaration up front”.

> 
> [1] http://docs.sqlalchemy.org/en/rel_0_9/orm/session.html#custom-vertical-partitioning
> [2] http://techspot.zzzeek.org/2012/01/11/django-style-database-routers-in-sqlalchemy/
> [3] Galera replication method: http://galeracluster.com/products/technology/
> 
> 
> _______________________________________________
> OpenStack-dev mailing list
> OpenStack-dev at lists.openstack.org
> http://lists.openstack.org/cgi-bin/mailman/listinfo/openstack-dev




More information about the OpenStack-dev mailing list