Skip to content

Commit 9f2c887

Browse files
arhiopterecsshiftedreality
authored andcommitted
MAGECLOUD-2899: Redis Slave Nodes Have no Health Checks / Are not Stable (#435)
1 parent d107b0c commit 9f2c887

File tree

6 files changed

+158
-12
lines changed

6 files changed

+158
-12
lines changed

patches.json

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
{
22
"magento/magento2-base": {
3-
"Fix credis pipeline issue": {
4-
"2.1.4 - 2.1.10": "MAGETWO-67097__fix_credis_pipeline_bug__2.1.4.patch",
5-
"2.2.0": "MAGETWO-67097__fix_credis_pipeline_bug__2.1.4.patch"
6-
},
73
"Fix asset locker race condition when using Redis": {
84
"2.1.4 - 2.1.14": "MDVA-2470__fix_asset_locking_race_condition__2.1.4.patch",
95
"2.2.0 - 2.2.5": "MDVA-2470__fix_asset_locking_race_condition__2.2.0.patch"
@@ -164,5 +160,17 @@
164160
"Fix monolog Slack Handler bug for magento 2.1.x": {
165161
"1.16.0": "MAGECLOUD-2793__fix_monolog_slack_handler_2.1.x.patch"
166162
}
163+
},
164+
"colinmollenhour/cache-backend-redis": {
165+
"The ability to read from the master Redis instance if the slave Redis is unavailable:": {
166+
"1.10.2": "MAGECLOUD-2899__fix_redis_slave_configuration__2.1.16.patch",
167+
"1.10.4": "MAGECLOUD-2899__fix_redis_slave_configuration__2.2.3.patch",
168+
"1.10.5": "MAGECLOUD-2899__fix_redis_slave_configuration__2.3.0.patch"
169+
}
170+
},
171+
"colinmollenhour/credis": {
172+
"Fix credis pipeline issue": {
173+
"1.6": "MAGETWO-67097__fix_credis_pipeline_bug__2.1.4.patch"
174+
}
167175
}
168176
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
diff -Nuar a/vendor/colinmollenhour/cache-backend-redis/Cm/Cache/Backend/Redis.php b/vendor/colinmollenhour/cache-backend-redis/Cm/Cache/Backend/Redis.php
2+
--- a/vendor/colinmollenhour/cache-backend-redis/Cm/Cache/Backend/Redis.php
3+
+++ b/vendor/colinmollenhour/cache-backend-redis/Cm/Cache/Backend/Redis.php
4+
@@ -111,6 +111,13 @@ class Cm_Cache_Backend_Redis extends Zend_Cache_Backend implements Zend_Cache_Ba
5+
*/
6+
protected $_luaMaxCStack = 5000;
7+
8+
+ /**
9+
+ * If 'retry_reads_on_master' is truthy then reads will be retried against master when slave returns "(nil)" value
10+
+ *
11+
+ * @var boolean
12+
+ */
13+
+ protected $_retryReadsOnMaster = false;
14+
+
15+
/**
16+
* @var stdClass
17+
*/
18+
@@ -316,6 +323,10 @@ class Cm_Cache_Backend_Redis extends Zend_Cache_Backend implements Zend_Cache_Ba
19+
$this->_luaMaxCStack = (int) $options['lua_max_c_stack'];
20+
}
21+
22+
+ if (isset($options['retry_reads_on_master'])) {
23+
+ $this->_retryReadsOnMaster = (bool) $options['retry_reads_on_master'];
24+
+ }
25+
+
26+
if (isset($options['auto_expire_lifetime'])) {
27+
$this->_autoExpireLifetime = (int) $options['auto_expire_lifetime'];
28+
}
29+
@@ -371,6 +382,11 @@ class Cm_Cache_Backend_Redis extends Zend_Cache_Backend implements Zend_Cache_Ba
30+
{
31+
if ($this->_slave) {
32+
$data = $this->_slave->hGet(self::PREFIX_KEY.$id, self::FIELD_DATA);
33+
+
34+
+ // Prevent compounded effect of cache flood on asynchronously replicating master/slave setup
35+
+ if ($this->_retryReadsOnMaster && $data === false) {
36+
+ $data = $this->_redis->hGet(self::PREFIX_KEY.$id, self::FIELD_DATA);
37+
+ }
38+
} else {
39+
$data = $this->_redis->hGet(self::PREFIX_KEY.$id, self::FIELD_DATA);
40+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
diff -Nuar a/vendor/colinmollenhour/cache-backend-redis/Cm/Cache/Backend/Redis.php b/vendor/colinmollenhour/cache-backend-redis/Cm/Cache/Backend/Redis.php
2+
--- a/vendor/colinmollenhour/cache-backend-redis/Cm/Cache/Backend/Redis.php
3+
+++ b/vendor/colinmollenhour/cache-backend-redis/Cm/Cache/Backend/Redis.php
4+
@@ -111,6 +111,13 @@ class Cm_Cache_Backend_Redis extends Zend_Cache_Backend implements Zend_Cache_Ba
5+
*/
6+
protected $_luaMaxCStack = 5000;
7+
8+
+ /**
9+
+ * If 'retry_reads_on_master' is truthy then reads will be retried against master when slave returns "(nil)" value
10+
+ *
11+
+ * @var boolean
12+
+ */
13+
+ protected $_retryReadsOnMaster = false;
14+
+
15+
/**
16+
* @var stdClass
17+
*/
18+
@@ -326,6 +333,10 @@ class Cm_Cache_Backend_Redis extends Zend_Cache_Backend implements Zend_Cache_Ba
19+
$this->_luaMaxCStack = (int) $options['lua_max_c_stack'];
20+
}
21+
22+
+ if (isset($options['retry_reads_on_master'])) {
23+
+ $this->_retryReadsOnMaster = (bool) $options['retry_reads_on_master'];
24+
+ }
25+
+
26+
if (isset($options['auto_expire_lifetime'])) {
27+
$this->_autoExpireLifetime = (int) $options['auto_expire_lifetime'];
28+
}
29+
@@ -428,6 +439,11 @@ class Cm_Cache_Backend_Redis extends Zend_Cache_Backend implements Zend_Cache_Ba
30+
{
31+
if ($this->_slave) {
32+
$data = $this->_slave->hGet(self::PREFIX_KEY.$id, self::FIELD_DATA);
33+
+
34+
+ // Prevent compounded effect of cache flood on asynchronously replicating master/slave setup
35+
+ if ($this->_retryReadsOnMaster && $data === false) {
36+
+ $data = $this->_redis->hGet(self::PREFIX_KEY.$id, self::FIELD_DATA);
37+
+ }
38+
} else {
39+
$data = $this->_redis->hGet(self::PREFIX_KEY.$id, self::FIELD_DATA);
40+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
diff -Nuar a/vendor/colinmollenhour/cache-backend-redis/Cm/Cache/Backend/Redis.php b/vendor/colinmollenhour/cache-backend-redis/Cm/Cache/Backend/Redis.php
2+
--- a/vendor/colinmollenhour/cache-backend-redis/Cm/Cache/Backend/Redis.php
3+
+++ b/vendor/colinmollenhour/cache-backend-redis/Cm/Cache/Backend/Redis.php
4+
@@ -111,6 +111,13 @@ class Cm_Cache_Backend_Redis extends Zend_Cache_Backend implements Zend_Cache_Ba
5+
*/
6+
protected $_luaMaxCStack = 5000;
7+
8+
+ /**
9+
+ * If 'retry_reads_on_master' is truthy then reads will be retried against master when slave returns "(nil)" value
10+
+ *
11+
+ * @var boolean
12+
+ */
13+
+ protected $_retryReadsOnMaster = false;
14+
+
15+
/**
16+
* @var stdClass
17+
*/
18+
@@ -339,6 +346,10 @@ class Cm_Cache_Backend_Redis extends Zend_Cache_Backend implements Zend_Cache_Ba
19+
$this->_luaMaxCStack = (int) $options['lua_max_c_stack'];
20+
}
21+
22+
+ if (isset($options['retry_reads_on_master'])) {
23+
+ $this->_retryReadsOnMaster = (bool) $options['retry_reads_on_master'];
24+
+ }
25+
+
26+
if (isset($options['auto_expire_lifetime'])) {
27+
$this->_autoExpireLifetime = (int) $options['auto_expire_lifetime'];
28+
}
29+
@@ -441,6 +452,11 @@ class Cm_Cache_Backend_Redis extends Zend_Cache_Backend implements Zend_Cache_Ba
30+
{
31+
if ($this->_slave) {
32+
$data = $this->_slave->hGet(self::PREFIX_KEY.$id, self::FIELD_DATA);
33+
+
34+
+ // Prevent compounded effect of cache flood on asynchronously replicating master/slave setup
35+
+ if ($this->_retryReadsOnMaster && $data === false) {
36+
+ $data = $this->_redis->hGet(self::PREFIX_KEY.$id, self::FIELD_DATA);
37+
+ }
38+
} else {
39+
$data = $this->_redis->hGet(self::PREFIX_KEY.$id, self::FIELD_DATA);
40+
}

src/Config/Factory/Cache.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ public function get(): array
110110
if ($slaveConnectionData) {
111111
if ($this->isConfigurationCompatibleWithSlaveConnection($envCacheConfiguration, $redisConfig)) {
112112
$redisCache['backend_options']['load_from_slave'] = $slaveConnectionData;
113+
$redisCache['backend_options']['read_timeout'] = 1;
114+
$redisCache['backend_options']['retry_reads_on_master'] = 1;
115+
$redisCache['frontend_options']['write_control'] = false;
113116
$this->logger->info('Set Redis slave connection');
114117
} else {
115118
$this->logger->notice(

src/Test/Unit/Config/Factory/CacheTest.php

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -215,16 +215,31 @@ public function getFromRelationshipsDataProvider()
215215
],
216216
]
217217
];
218-
$resultMasterSlaveConnection = $resultMasterOnlyConnection;
219-
$resultMasterSlaveConnection['frontend']['default']['backend_options']['load_from_slave'] = [
220-
'server' => 'slave.host',
221-
'port' => 'slave.port',
222-
];
223-
$resultMasterSlaveConnection['frontend']['page_cache']['backend_options']['load_from_slave'] = [
224-
'server' => 'slave.host',
225-
'port' => 'slave.port',
218+
219+
$slaveConfiguration = [
220+
'backend_options' => [
221+
'load_from_slave' => [
222+
'server' => 'slave.host',
223+
'port' => 'slave.port',
224+
],
225+
'read_timeout' => 1,
226+
'retry_reads_on_master' => 1,
227+
],
228+
'frontend_options' => [
229+
'write_control' => false,
230+
]
226231
];
227232

233+
$resultMasterSlaveConnection = $resultMasterOnlyConnection;
234+
$resultMasterSlaveConnection['frontend']['default'] = array_merge_recursive(
235+
$resultMasterSlaveConnection['frontend']['default'],
236+
$slaveConfiguration
237+
);
238+
$resultMasterSlaveConnection['frontend']['page_cache'] = array_merge_recursive(
239+
$resultMasterSlaveConnection['frontend']['page_cache'],
240+
$slaveConfiguration
241+
);
242+
228243
$resultMasterSlaveConnectionWithMergedValue = $resultMasterSlaveConnection;
229244
$resultMasterSlaveConnectionWithMergedValue['frontend']['default']['backend_options']['value'] = 'key';
230245

0 commit comments

Comments
 (0)