Skip to content

Commit 22d3ee3

Browse files
Merge pull request #187 from magento-commerce/MCLOUD-13455
[MCLOUD 13455] - Valkey Configuration Support and fixes
2 parents 7a26350 + 886ef96 commit 22d3ee3

File tree

24 files changed

+1507
-1873
lines changed

24 files changed

+1507
-1873
lines changed

src/Config/Factory/Cache.php

Lines changed: 48 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Cache
3333
public const REDIS_BACKEND_REDIS_CACHE = '\Magento\Framework\Cache\Backend\Redis';
3434

3535
public const VALKEY_BACKEND_CM_CACHE = 'Cm_Cache_Backend_Redis';
36-
public const VALKEY_BACKEND_VALKEY_CACHE = '\Magento\Framework\Cache\Backend\Valkey';
36+
public const VALKEY_BACKEND_VALKEY_CACHE = '\Magento\Framework\Cache\Backend\Redis';
3737

3838
public const VALKEY_BACKEND_REMOTE_SYNCHRONIZED_CACHE = '\Magento\Framework\Cache\Backend\RemoteSynchronizedCache';
3939
public const REDIS_BACKEND_REMOTE_SYNCHRONIZED_CACHE = '\Magento\Framework\Cache\Backend\RemoteSynchronizedCache';
@@ -76,11 +76,11 @@ class Cache
7676
private ConfigMerger $configMerger;
7777

7878
/**
79-
* @param Valkey $valkey
80-
* @param Redis $redis
79+
* @param Valkey $valkey
80+
* @param Redis $redis
8181
* @param DeployInterface $stageConfig
8282
* @param LoggerInterface $logger
83-
* @param ConfigMerger $configMerger
83+
* @param ConfigMerger $configMerger
8484
*/
8585
public function __construct(
8686
Redis $redis,
@@ -103,6 +103,7 @@ public function __construct(
103103
* configuration in relationships and if so, makes cache configuration for redis.
104104
* Returns an empty array in other case.
105105
*
106+
* @SuppressWarnings("CyclomaticComplexity")
106107
* @return array
107108
* @throws ConfigException
108109
*/
@@ -147,11 +148,10 @@ public function get(): array
147148
}
148149

149150
// Determine backend based on available configuration
150-
$backendConfig = !empty($redisConfig) ? $redisConfig : $valkeyConfig;
151-
$envCacheBackendModel = (string)$this->stageConfig->get(DeployInterface::VAR_CACHE_REDIS_BACKEND);
152-
151+
$backendConfig = !empty($redisConfig) ? $redisConfig : $valkeyConfig;
152+
$cacheBackendModel = !empty($redisConfig) ? $envCacheRadisBackendModel :$envCacheValkeyBackendModel;
153153
if ($this->isSynchronizedConfigStructure()) {
154-
$cacheCacheBackend = $this->getSynchronizedConfigStructure($envCacheBackendModel, $backendConfig);
154+
$cacheCacheBackend = $this->getSynchronizedConfigStructure($cacheBackendModel, $backendConfig);
155155
$cacheCacheBackend['backend_options']['remote_backend_options'] = array_merge(
156156
$cacheCacheBackend['backend_options']['remote_backend_options'],
157157
$this->getSlaveConnection($envCacheConfiguration, $backendConfig)
@@ -165,14 +165,14 @@ public function get(): array
165165
],
166166
];
167167
} else {
168-
$cacheCacheBackend = $this->getUnsyncedConfigStructure($envCacheBackendModel, $backendConfig);
168+
$cacheCacheBackend = $this->getUnsyncedConfigStructure($cacheBackendModel, $backendConfig);
169169
$slaveConnection = $this->getSlaveConnection($envCacheConfiguration, $backendConfig);
170170
if ($slaveConnection) {
171171
$cacheCacheBackend['frontend_options']['write_control'] = false;
172-
$cacheCacheBackend['backend_options'] = array_merge(
173-
$cacheCacheBackend['backend_options'],
174-
$slaveConnection
175-
);
172+
$cacheCacheBackend['backend_options'] = array_merge(
173+
$cacheCacheBackend['backend_options'],
174+
$slaveConnection
175+
);
176176
}
177177
$finalConfig = [
178178
'frontend' => [
@@ -197,8 +197,8 @@ public function get(): array
197197
* also if CACHE_CONFIGURATION is compatible with slave connections.
198198
* Otherwise, retrieves an empty array.
199199
*
200-
* @param array $envCacheConfiguration
201-
* @param array $backendConfig
200+
* @param array $envCacheConfiguration
201+
* @param array $backendConfig
202202
* @return array
203203
* @throws ConfigException
204204
*/
@@ -232,24 +232,25 @@ private function getSlaveConnection(array $envCacheConfiguration, array $backend
232232

233233
$this->logger->info(sprintf('Set %s slave connection', $backendType));
234234
} else {
235-
$this->logger->notice(
236-
sprintf(
237-
'The variable \'%s\' is ignored as you\'ve changed cache connection settings in \'%s\'',
238-
$useRedisSlave ? DeployInterface::VAR_REDIS_USE_SLAVE_CONNECTION : DeployInterface::VAR_VALKEY_USE_SLAVE_CONNECTION,
239-
DeployInterface::VAR_CACHE_CONFIGURATION
240-
)
241-
);
235+
$this->logger->notice(
236+
sprintf(
237+
'The variable \'%s\' is ignored as you\'ve changed cache connection settings in \'%s\'',
238+
$useRedisSlave ?
239+
DeployInterface::VAR_REDIS_USE_SLAVE_CONNECTION :
240+
DeployInterface::VAR_VALKEY_USE_SLAVE_CONNECTION,
241+
DeployInterface::VAR_CACHE_CONFIGURATION
242+
)
243+
);
242244
}
243245
}
244246

245-
246247
return $config;
247248
}
248249

249250
/**
250251
* Checks that given cache configuration is valid.
251252
*
252-
* @param array $cacheConfiguration
253+
* @param array $cacheConfiguration
253254
* @return bool
254255
*/
255256
private function isCacheConfigurationValid(array $cacheConfiguration): bool
@@ -263,10 +264,10 @@ private function isCacheConfigurationValid(array $cacheConfiguration): bool
263264
*
264265
* Returns false if server or port was changed in merged configuration otherwise false.
265266
*
266-
* @param array $envCacheConfig
267-
* @param array $backendConfig
268-
* @return bool
269-
* @throws ConfigException
267+
* @param array $envCacheConfig
268+
* @param array $backendConfig
269+
* @return bool
270+
* @throws ConfigException
270271
* @SuppressWarnings("PHPMD.CyclomaticComplexity")
271272
*/
272273
private function isConfigurationCompatibleWithSlaveConnection(
@@ -281,7 +282,8 @@ private function isConfigurationCompatibleWithSlaveConnection(
281282
?? null;
282283

283284
if (($host !== null && $host !== $backendConfig['host'])
284-
|| ($port !== null && $port !== $backendConfig['port'])) {
285+
|| ($port !== null && $port !== $backendConfig['port'])
286+
) {
285287
return false;
286288
}
287289
} else {
@@ -290,7 +292,8 @@ private function isConfigurationCompatibleWithSlaveConnection(
290292
$port = $envCacheConfig['frontend'][$type]['backend_options']['port'] ?? null;
291293

292294
if (($host !== null && $host !== $backendConfig['host'])
293-
|| ($port !== null && $port !== $backendConfig['port'])) {
295+
|| ($port !== null && $port !== $backendConfig['port'])
296+
) {
294297
return false;
295298
}
296299
}
@@ -302,15 +305,14 @@ private function isConfigurationCompatibleWithSlaveConnection(
302305
/**
303306
* Returns backend config for unsynced cache implementation.
304307
*
305-
* @param string $envCacheBackendModel
306-
* @param array $backendConfig
308+
* @param string $envCacheBackendModel
309+
* @param array $backendConfig
307310
* @return array
308311
*/
309312
private function getUnsyncedConfigStructure(string $envCacheBackendModel, array $backendConfig): array
310313
{
311-
$cacheBackendModelRedis='Cm_Cache_Backend_Redis';
312-
$config = [
313-
'backend' => $cacheBackendModelRedis,
314+
$config = [
315+
'backend' => $envCacheBackendModel,
314316
'backend_options' => [
315317
'server' => $backendConfig['host'],
316318
'port' => $backendConfig['port'],
@@ -327,20 +329,17 @@ private function getUnsyncedConfigStructure(string $envCacheBackendModel, array
327329
/**
328330
* Returns backend config for synchronized cache implementation.
329331
*
330-
* @param string $envCacheBackendModel
331-
* @param array $backendConfig
332+
* @param string $envCacheBackendModel
333+
* @param array $backendConfig
332334
* @return array
333335
*/
334336
private function getSynchronizedConfigStructure(string $envCacheBackendModel, array $backendConfig): array
335337
{
336-
$backendClass = $backendConfig['host'] === 'valkey'
337-
? '\Magento\Framework\Cache\Backend\Valkey'
338-
: '\Magento\Framework\Cache\Backend\Redis';
339338

340339
$config = [
341340
'backend' => $envCacheBackendModel,
342341
'backend_options' => [
343-
'remote_backend' => $backendClass,
342+
'remote_backend' => '\Magento\Framework\Cache\Backend\Redis',
344343
'remote_backend_options' => [
345344
'server' => $backendConfig['host'],
346345
'port' => $backendConfig['port'],
@@ -377,15 +376,14 @@ private function isSynchronizedConfigStructure(): bool
377376
$redisModel = (string)$this->stageConfig->get(DeployInterface::VAR_CACHE_REDIS_BACKEND);
378377
$valkeyModel = (string)$this->stageConfig->get(DeployInterface::VAR_CACHE_VALKEY_BACKEND);
379378
return $redisModel === self::REDIS_BACKEND_REMOTE_SYNCHRONIZED_CACHE ||
380-
$valkeyModel === self::VALKEY_BACKEND_REMOTE_SYNCHRONIZED_CACHE;
379+
$valkeyModel === self::VALKEY_BACKEND_REMOTE_SYNCHRONIZED_CACHE;
381380
}
382381

383-
/**
384-
* @return array
385-
*/
386-
public function isValkeyEnabled()
387-
{
388-
return $this->valkey->getConfiguration();
389-
}
390-
382+
/**
383+
* @return array
384+
*/
385+
public function isValkeyEnabled(): array
386+
{
387+
return $this->valkey->getConfiguration();
388+
}
391389
}

src/Config/Validator/Deploy/ServiceVersion.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99

1010
use Magento\MagentoCloud\App\Error;
1111
use Magento\MagentoCloud\App\GenericException;
12+
use Magento\MagentoCloud\Config\Validator;
13+
use Magento\MagentoCloud\Config\ValidatorInterface;
1214
use Magento\MagentoCloud\Service\Detector\DatabaseType;
1315
use Magento\MagentoCloud\Service\ServiceInterface;
1416
use Magento\MagentoCloud\Service\ServiceFactory;
1517
use Magento\MagentoCloud\Service\Validator as ServiceVersionValidator;
16-
use Magento\MagentoCloud\Config\Validator;
17-
use Magento\MagentoCloud\Config\ValidatorInterface;
1818
use Psr\Log\LoggerInterface;
1919

2020
/**
@@ -80,8 +80,8 @@ public function validate(): Validator\ResultInterface
8080
$services = [
8181
ServiceInterface::NAME_RABBITMQ,
8282
ServiceInterface::NAME_REDIS,
83-
ServiceInterface::NAME_VALKEY,
8483
ServiceInterface::NAME_REDIS_SESSION,
84+
ServiceInterface::NAME_VALKEY,
8585
ServiceInterface::NAME_VALKEY_SESSION,
8686
ServiceInterface::NAME_ELASTICSEARCH,
8787
ServiceInterface::NAME_OPENSEARCH,

src/Service/Validator.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,17 @@ class Validator
2020
* Supported version constraints of Redis services
2121
*/
2222
private const REDIS_SUPPORT_VERSIONS = [
23-
'*' => '~3.2.0 || ~4.0.0 || ~5.0.0 || ~6.0.0 || ~6.2.0 || ~7.0.0 || ~7.2.0',
23+
'<=2.4.8' => '~3.2.0 || ~4.0.0 || ~5.0.0 || ~6.0.0 || ~6.2.0 || ~7.0.0 || ~7.2.0',
2424
];
2525

2626
/**
2727
* Supported version constraints of Valkey services
2828
*/
2929
private const VALKEY_SUPPORT_VERSIONS = [
30-
'*' => ' ~7.2.0 || ~8.0.0',
30+
'2.4.5-p13 <2.4.6' => '~8.0.0 || ~8.0.1',
31+
'2.4.6-p11 <2.4.7' => '~8.0.0 || ~8.0.1',
32+
'2.4.7-p6 <2.4.8' => '~8.0.0 || ~8.0.1',
33+
'>=2.4.8' => ' ~8.0.0 || ~8.0.1',
3134
];
3235

3336
/**
@@ -219,7 +222,11 @@ private function getSupportedVersions(): array
219222
}
220223
}
221224
if (!isset($this->supportedVersionList[$serviceName])
222-
&& $serviceName !== ServiceInterface::NAME_OPENSEARCH) {
225+
&& !in_array($serviceName, [
226+
ServiceInterface::NAME_OPENSEARCH,
227+
ServiceInterface::NAME_VALKEY,
228+
ServiceInterface::NAME_VALKEY_SESSION
229+
], true)) {
223230
throw new ServiceMismatchException(sprintf(
224231
'Service "%s" does not have defined configurations for "%s" Magento version',
225232
$serviceName,

0 commit comments

Comments
 (0)