Thanks a lot!
我把你推荐的文章放在这里,希望中国的朋友不费劲可以看到。DAL是一个大型数据量系统的重要的底层架构,希望对大家有帮助。
I did temporarily give up on anti-together behaviour to study AR better. And took another task: sharding support. Result is attached as ZIP (several classes extending AR). Description following.
Main thing about sharding support was to give AR ability to decide on what connection to use dynamically. Depending on some object property or any environment condition. So we can switch to db-replica or concrete DB-node (where required entity is located). Unfortunately Yii can’t read our minds so we need a tool to let it know which connection (which server) we are going to use. Following Yii AR interface look choose() method was implemented.
To end-user It looks like:
$users = User::model()->choose($shard)->findAll();
To make it work we need logic to decide which connection to use depending on concrete $shard var (what’s inside is different in different cases). This logic is situated in DbConnectionManager. Sharding support library (or patch) includes CDbConnectionManagerAbstract defining it’s interface. In most cases you just need to implement
abstract protected function _createDbConnection($key);
That’s it: this function will get $shard as $key parameter. And supposed to return CDbConnection. To make it work DbConnectionManager should be defined in config next to ‘db’:
‘dbConnectionManager’ => array(
'class' => 'application.tests.classes.ConcreteDbConnectionManager',
)
Usage of described extensions doesn’t break standard way Yii works either. We only use DbConnectionManager if choose() called. And it’s object-visible. Next model you create will start from default connection from config.
Besides the BC it makes possible creating "System tables" which is common requirement for any sharding implementation.
Everything I described is way more wider described in unit-tests attached to implementation. Tests include some test Models and CShardingTestCase is full of examples of what you get in your code to work with it.
One more thing about it: it’s impossible to make Sharding work out-of-box in most cases. And our implementation is way more complex then default one we can suppose. BUT! Nothing stops us from creating default bindings to implement most basic and completely automatic sharding in extension to our extension. And that’s my current vector.
Could you please check all this stuff and tell me what’s the best way to make this lib accessible to community and improve it further?
Thanks.