magento2 icon indicating copy to clipboard operation
magento2 copied to clipboard

Support connection retries for Redis session and compatible with colinmollenhour/php-redis-session-abstract v1.6.0

Open TuVanDev opened this issue 9 months ago • 7 comments

This pull request resolves the compatibility issues with colinmollenhour/php-redis-session-abstract v1.6.0.

Description (*)

The newest version of Cm RedisSession, version v1.6.0 has caused compatibility issues with Magento and Adobe Commerce projects, as they introduce a new method in their Interface which the Magento core class implemented.

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Framework\Session\SaveHandler\Redis;

class Config implements \Cm\RedisSession\Handler\ConfigInterface
{
}

The changes made in this pull request include:

  • Added support retries connection for Redis sessions
  • Added unit tests

Related Pull Requests

  • N/A

Fixed Issues (if relevant)

  1. Fixes magento/magento2#38728

Manual testing scenarios (*)

  1. Install a new Magento project, or update the composer packages for the Magento project by running the command composer update
  2. Run bin/magento setup:di:compile
  • Expected result: Magento generated code and dependency injection configuration as expected.

  • Actual result: An error is encountered:

Compilation was started.
Application code generator... 3/9 [=========>------------------]  33% 3 secs 362.0 MiB
Fatal error: Class Magento\Framework\Session\SaveHandler\Redis\Config contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Cm\RedisSession\Handler\ConfigInterface::getRetries) in /var/www/html/vendor/magento/framework/Session/SaveHandler/Redis/Config.php on line 16

Questions or comments

Patches

Until the issue is resolved in the next Magento version, you can apply the two patches I have prepared:

  1. The patch for the magento/framework package: File name: magento-magento2-base_GitHub-PR-38729-compatible-with-collinmollenhour-redis-session-v1.6.0.patch
diff --git a/vendor/magento/framework/Session/SaveHandler/Redis/Config.php b/vendor/magento/framework/Session/SaveHandler/Redis/Config.php
index 70b666f8..ac6e6227 100644
--- a/vendor/magento/framework/Session/SaveHandler/Redis/Config.php
+++ b/vendor/magento/framework/Session/SaveHandler/Redis/Config.php
@@ -45,6 +45,11 @@ class Config implements \Cm\RedisSession\Handler\ConfigInterface
      */
     const PARAM_TIMEOUT                 = 'session/redis/timeout';
 
+    /**
+     * Configuration path for number of connection retries
+     */
+    const PARAM_RETRIES = 'session/redis/retries';
+
     /**
      * Configuration path for persistent identifier
      */
@@ -220,6 +225,14 @@ class Config implements \Cm\RedisSession\Handler\ConfigInterface
         return $this->deploymentConfig->get(self::PARAM_TIMEOUT);
     }
 
+    /**
+     * @inheritdoc
+     */
+    public function getRetries()
+    {
+        return $this->deploymentConfig->get(self::PARAM_RETRIES);
+    }
+
     /**
      * @inheritdoc
      */
diff --git a/vendor/magento/framework/Session/Test/Unit/SaveHandler/Redis/ConfigTest.php b/vendor/magento/framework/Session/Test/Unit/SaveHandler/Redis/ConfigTest.php
index 75944922..75f8d5ba 100644
--- a/vendor/magento/framework/Session/Test/Unit/SaveHandler/Redis/ConfigTest.php
+++ b/vendor/magento/framework/Session/Test/Unit/SaveHandler/Redis/ConfigTest.php
@@ -114,6 +114,16 @@ class ConfigTest extends TestCase
         $this->assertEquals($this->config->getTimeout(), $expected);
     }
 
+    public function testGetRetries()
+    {
+        $expected = 10;
+        $this->deploymentConfigMock->expects($this->once())
+            ->method('get')
+            ->willReturn(Config::PARAM_RETRIES)
+            ->willReturn($expected);
+        $this->assertEquals($this->config->getRetries(), $expected);
+    }
+
     public function testGetPersistentIdentifier()
     {
         $expected = 'sess01';

  1. The patch for the magento/magento2-base package: File name: magento-magento2-framework_GitHub-PR-38729-compatible-with-collinmollenhour-redis-session-v1.6.0.patch
diff --git a/vendor/magento/magento2-base/setup/src/Magento/Setup/Model/ConfigOptionsList/Session.php b/vendor/magento/magento2-base/setup/src/Magento/Setup/Model/ConfigOptionsList/Session.php
index 4a3a02b3..eeaa6174 100644
--- a/vendor/magento/magento2-base/setup/src/Magento/Setup/Model/ConfigOptionsList/Session.php
+++ b/vendor/magento/magento2-base/setup/src/Magento/Setup/Model/ConfigOptionsList/Session.php
@@ -23,6 +23,7 @@ class Session implements ConfigOptionsListInterface
     const INPUT_KEY_SESSION_REDIS_PORT = 'session-save-redis-port';
     const INPUT_KEY_SESSION_REDIS_PASSWORD = 'session-save-redis-password';
     const INPUT_KEY_SESSION_REDIS_TIMEOUT = 'session-save-redis-timeout';
+    const INPUT_KEY_SESSION_REDIS_RETRIES = 'session-save-redis-retries';
     const INPUT_KEY_SESSION_REDIS_PERSISTENT_IDENTIFIER = 'session-save-redis-persistent-id';
     const INPUT_KEY_SESSION_REDIS_DATABASE = 'session-save-redis-db';
     const INPUT_KEY_SESSION_REDIS_COMPRESSION_THRESHOLD = 'session-save-redis-compression-threshold';
@@ -47,6 +48,7 @@ class Session implements ConfigOptionsListInterface
     const CONFIG_PATH_SESSION_REDIS_PORT = 'session/redis/port';
     const CONFIG_PATH_SESSION_REDIS_PASSWORD = 'session/redis/password';
     const CONFIG_PATH_SESSION_REDIS_TIMEOUT = 'session/redis/timeout';
+    const CONFIG_PATH_SESSION_REDIS_RETRIES = 'session/redis/retries';
     const CONFIG_PATH_SESSION_REDIS_PERSISTENT_IDENTIFIER = 'session/redis/persistent_identifier';
     const CONFIG_PATH_SESSION_REDIS_DATABASE = 'session/redis/database';
     const CONFIG_PATH_SESSION_REDIS_COMPRESSION_THRESHOLD = 'session/redis/compression_threshold';
@@ -75,6 +77,7 @@ class Session implements ConfigOptionsListInterface
         self::INPUT_KEY_SESSION_REDIS_PORT => '6379',
         self::INPUT_KEY_SESSION_REDIS_PASSWORD => '',
         self::INPUT_KEY_SESSION_REDIS_TIMEOUT => '2.5',
+        self::INPUT_KEY_SESSION_REDIS_RETRIES => '0',
         self::INPUT_KEY_SESSION_REDIS_PERSISTENT_IDENTIFIER => '',
         self::INPUT_KEY_SESSION_REDIS_DATABASE => '2',
         self::INPUT_KEY_SESSION_REDIS_COMPRESSION_THRESHOLD => '2048',
@@ -117,6 +120,7 @@ class Session implements ConfigOptionsListInterface
         self::INPUT_KEY_SESSION_REDIS_PORT => self::CONFIG_PATH_SESSION_REDIS_PORT,
         self::INPUT_KEY_SESSION_REDIS_PASSWORD => self::CONFIG_PATH_SESSION_REDIS_PASSWORD,
         self::INPUT_KEY_SESSION_REDIS_TIMEOUT => self::CONFIG_PATH_SESSION_REDIS_TIMEOUT,
+        self::INPUT_KEY_SESSION_REDIS_RETRIES => self::CONFIG_PATH_SESSION_REDIS_RETRIES,
         self::INPUT_KEY_SESSION_REDIS_PERSISTENT_IDENTIFIER => self::CONFIG_PATH_SESSION_REDIS_PERSISTENT_IDENTIFIER,
         self::INPUT_KEY_SESSION_REDIS_DATABASE => self::CONFIG_PATH_SESSION_REDIS_DATABASE,
         self::INPUT_KEY_SESSION_REDIS_COMPRESSION_THRESHOLD => self::CONFIG_PATH_SESSION_REDIS_COMPRESSION_THRESHOLD,
@@ -177,6 +181,12 @@ class Session implements ConfigOptionsListInterface
                 self::CONFIG_PATH_SESSION_REDIS_TIMEOUT,
                 'Connection timeout, in seconds'
             ),
+            new TextConfigOption(
+                self::INPUT_KEY_SESSION_REDIS_RETRIES,
+                TextConfigOption::FRONTEND_WIZARD_TEXT,
+                self::CONFIG_PATH_SESSION_REDIS_RETRIES,
+                'Redis connection retries.'
+            ),
             new TextConfigOption(
                 self::INPUT_KEY_SESSION_REDIS_PERSISTENT_IDENTIFIER,
                 TextConfigOption::FRONTEND_WIZARD_TEXT,
diff --git a/vendor/magento/magento2-base/setup/src/Magento/Setup/Test/Unit/Model/ConfigOptionsList/SessionTest.php b/vendor/magento/magento2-base/setup/src/Magento/Setup/Test/Unit/Model/ConfigOptionsList/SessionTest.php
index 98ef1d60..cbd5b1de 100644
--- a/vendor/magento/magento2-base/setup/src/Magento/Setup/Test/Unit/Model/ConfigOptionsList/SessionTest.php
+++ b/vendor/magento/magento2-base/setup/src/Magento/Setup/Test/Unit/Model/ConfigOptionsList/SessionTest.php
@@ -147,6 +147,7 @@ class SessionTest extends TestCase
                     'port' => '',
                     'password' => '',
                     'timeout' => '',
+                    'retries' => '',
                     'persistent_identifier' => '',
                     'database' => '',
                     'compression_threshold' => '',
@@ -204,6 +205,7 @@ class SessionTest extends TestCase
                     'port' => '',
                     'password' => '',
                     'timeout' => '',
+                    'retries' => '',
                     'persistent_identifier' => '',
                     'database' => '',
                     'compression_threshold' => '',

To apply patches for Magento Open Source and Adobe Commerce, please refer to the following documentation:

  • Document on how to apply a composer patch for Magento open source project: https://experienceleague.adobe.com/docs/commerce-operations/upgrade-guide/patches/apply.html The configuration for applying the two patches using the cweagans/composer-patches composer patch plugin to address this issue should look like the following:
"extra": {
    "magento-force": "override",
    "patches": {
        "magento/framework": {
            "Github-PR-38729 - Compatible with colinmollenhour/php-redis-session-abstract v1.6.0 and support connection retries for Redis session": "patches/magento-magento2-framework_GitHub-PR-38729-compatible-with-collinmollenhour-redis-session-v1.6.0.patch"
        },
        "magento/magento2-base": {
            "Github-PR-38729 - Compatible with colinmollenhour/php-redis-session-abstract v1.6.0 and support connection retries for Redis session": "patches/magento-magento2-base_GitHub-PR-38729-compatible-with-collinmollenhour-redis-session-v1.6.0.patch"
        }
    }
}

Feature information

If this pull request is accepted, you will be able to configure the number of retries for the Redis connection in the app/etc/env.php file, under the session section:

'session' => [
    'save' => 'redis',
    'redis' => [
        // additional configurations
        'retries' => 10,
        // other configurations
    ]
]

Contribution checklist (*)

  • [x] Pull request has a meaningful description of its purpose
  • [x] All commits are accompanied by meaningful commit messages
  • [ ] All new or changed code is covered with unit/integration tests (if applicable)
  • [ ] README.md files for modified modules are updated and included in the pull request if any README.md predefined sections require an update
  • [ ] All automated tests passed successfully (all builds are green)

TuVanDev avatar May 16 '24 04:05 TuVanDev