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

Li Ma skywalker.nick at gmail.com
Mon Aug 11 04:30:51 UTC 2014


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

Hi Mike Bayer, I'm so sorry I mistake you from Mike Wilson in the last post. :-) Also, say sorry to Mike Wilson.

> 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.

You are totally right.

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

The first approach that I had in mind is the decorator-based method to separates read/write ops like what you said. To some degree, it is almost the same app-level approach to the master/slave configuration, due to transparency to developers. However, as I stated before, the current approach is merely used in OpenStack. Decorator is more friendly than use_slave_flag or something like that. If ideally transparency cannot be achieved, to say the least, decorator-based app-level switching is a great improvement, compared with the current implementation.

> OK so Galera would perhaps have some way to make this happen, and that's great.

If any Galera expert here, please correct me. At least in my experiment, transactions work in that way.

> this (the word “integrate”, and what does that mean) is really the only thing making me nervous.

Mike, just feel free. What I'd like to do is to add a django-style routing method as a plus in oslo.db, like:

[database]
# Original master/slave configuration
master_connection = 
slave_connection = 

# Only Support Synchronous Replication
enable_auto_routing = True

[db_cluster]
master_connection = 
master_connection = 
...
slave_connection = 
slave_connection = 
...

HOWEVER, I think it needs more investigation, so this is why I'd like to put it in the mailing list in the early stage to raise some discussions in depth. I'm not a Galera expert. I really appreciate any challenges here.

Thanks,
Li Ma


----- Original Message -----
From: "Mike Bayer" <mbayer at redhat.com>
To: "OpenStack Development Mailing List (not for usage questions)" <openstack-dev at lists.openstack.org>
Sent: 星期日, 2014年 8 月 10日 下午 11:57:47
Subject: Re: [openstack-dev] [oslo.db]A proposal for DB read/write separation


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


_______________________________________________
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