yiisoft / yii2-redis

Yii 2 Redis extension.
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
452 stars 183 forks source link

orderBy() does not works on Redis slaves #181

Open nnrudakov opened 5 years ago

nnrudakov commented 5 years ago

Redis SORT command returns an error on slave instances and orderBy() always returns empty result.

To reproduce the problem make Redis master and slave, make and add any ActiveRecord model. Do query with orderBy() on slave instance:

$model = Model::find()->orderBy(['id' => \SORT_DESC])->one();

null will be return. Lua script that will be executed:

local allpks=redis.pcall('SORT', 'model', 'BY', 'model' .. ':a:*->' .. 'id', 'DESC')
if allpks['err'] then
    allpks=redis.pcall('SORT', 'model', 'BY', 'model' .. ':a:*->' .. 'id', 'DESC', 'ALPHA')
end
local pks={}
-- inside doesn't matter
return pks -- {} this is will be return

allpks will be contain an error:

(error) @user_script: 3: -READONLY You can't write against a read only replica.

And empty pks will be return.

Additional info

Q A
Yii vesion 2.0.16
Yii2-Redis vesion 2.0.9
PHP version 7.0+
Redis version 3.6+
Operating system Debian