|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of CodeIgniter 4 framework. |
| 7 | + * |
| 8 | + * (c) CodeIgniter Foundation <admin@codeigniter.com> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view |
| 11 | + * the LICENSE file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CodeIgniter\Cache\Handlers; |
| 15 | + |
| 16 | +use Redis; |
| 17 | +use RedisException; |
| 18 | +use RuntimeException; |
| 19 | + |
| 20 | +/** |
| 21 | + * Discovers the current Redis master from a list of Sentinel nodes. |
| 22 | + * |
| 23 | + * phpredis has no built-in Sentinel failover handling, so the Redis cache |
| 24 | + * and session handlers use this utility to resolve the master address before |
| 25 | + * connecting. It prefers the `RedisSentinel` class (phpredis >= 5.3) and |
| 26 | + * falls back to a plain `SENTINEL get-master-addr-by-name` command for older |
| 27 | + * versions. |
| 28 | + */ |
| 29 | +class RedisSentinel |
| 30 | +{ |
| 31 | + /** |
| 32 | + * Default Sentinel port. |
| 33 | + */ |
| 34 | + private const DEFAULT_SENTINEL_PORT = 26379; |
| 35 | + |
| 36 | + /** |
| 37 | + * Queries the given Sentinel nodes for the address of the named master. |
| 38 | + * |
| 39 | + * Each node is tried in order; the first one that answers wins. When no |
| 40 | + * node can return the master address a RuntimeException is thrown so the |
| 41 | + * caller can surface a clear error. |
| 42 | + * |
| 43 | + * @param list<array{host: string, port?: int}> $nodes Sentinel nodes to query. |
| 44 | + * @param string $service Sentinel master name, e.g. "mymaster". |
| 45 | + * @param float $timeout Connection timeout (seconds) per node. |
| 46 | + * |
| 47 | + * @return array{0: string, 1: int} The master host and port. |
| 48 | + * |
| 49 | + * @throws RuntimeException When no Sentinel node can discover the master. |
| 50 | + */ |
| 51 | + public static function discoverMaster(array $nodes, string $service, float $timeout = 0.0): array |
| 52 | + { |
| 53 | + if ($nodes === []) { |
| 54 | + throw new RuntimeException('No Redis Sentinel nodes configured.'); |
| 55 | + } |
| 56 | + |
| 57 | + foreach ($nodes as $node) { |
| 58 | + $host = $node['host'] ?? ''; |
| 59 | + $port = $node['port'] ?? self::DEFAULT_SENTINEL_PORT; |
| 60 | + |
| 61 | + if ($host === '') { |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + $address = self::queryNode($host, (int) $port, $service, $timeout); |
| 66 | + |
| 67 | + if ($address !== null) { |
| 68 | + return $address; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + throw new RuntimeException(sprintf('Redis Sentinel unable to discover master "%s".', $service)); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Queries a single Sentinel node for the master address. |
| 77 | + * |
| 78 | + * @return array{0: string, 1: int}|null |
| 79 | + */ |
| 80 | + private static function queryNode(string $host, int $port, string $service, float $timeout): ?array |
| 81 | + { |
| 82 | + // Prefer the dedicated RedisSentinel class (phpredis >= 5.3). |
| 83 | + if (class_exists(\RedisSentinel::class)) { |
| 84 | + try { |
| 85 | + $sentinel = new \RedisSentinel($host, $port, $timeout); |
| 86 | + $result = $sentinel->getMasterAddrByName($service); |
| 87 | + |
| 88 | + if ($result === false) { |
| 89 | + return null; |
| 90 | + } |
| 91 | + |
| 92 | + return self::normalise($result); |
| 93 | + } catch (RedisException) { |
| 94 | + // Node unreachable or command failed; try the next one. |
| 95 | + return null; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + // Fall back to the SENTINEL command on a plain Redis connection. |
| 100 | + try { |
| 101 | + $redis = new Redis(); |
| 102 | + $redis->connect($host, $port, $timeout); |
| 103 | + |
| 104 | + $result = $redis->rawcommand('SENTINEL', 'get-master-addr-by-name', $service); |
| 105 | + |
| 106 | + try { |
| 107 | + $redis->close(); |
| 108 | + } catch (RedisException) { |
| 109 | + // Connection already dead, that's fine. |
| 110 | + } |
| 111 | + |
| 112 | + if ($result === false) { |
| 113 | + return null; |
| 114 | + } |
| 115 | + |
| 116 | + return self::normalise($result); |
| 117 | + } catch (RedisException) { |
| 118 | + return null; |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Normalises the varied reply shapes into a [host, port] pair. |
| 124 | + * |
| 125 | + * phpredis returns either a flat `['host', 'port']` list (rawCommand and |
| 126 | + * most RedisSentinel builds) or an associative `[['ip' => .., 'port' => ..]]` |
| 127 | + * shape on some builds. Both are coerced to `array{0:string, 1:int}`. |
| 128 | + * |
| 129 | + * @param array<array-key, mixed> $result |
| 130 | + * |
| 131 | + * @return array{0: string, 1: int}|null |
| 132 | + */ |
| 133 | + private static function normalise(array $result): ?array |
| 134 | + { |
| 135 | + // Some RedisSentinel builds wrap the entry in an outer array. |
| 136 | + $entry = array_is_list($result) && isset($result[0]) && is_array($result[0]) |
| 137 | + ? $result[0] |
| 138 | + : $result; |
| 139 | + |
| 140 | + if (isset($entry['ip'], $entry['port'])) { |
| 141 | + return [(string) $entry['ip'], (int) $entry['port']]; |
| 142 | + } |
| 143 | + |
| 144 | + if (isset($entry[0], $entry[1]) && is_string($entry[0]) && (is_string($entry[1]) || is_int($entry[1]))) { |
| 145 | + return [(string) $entry[0], (int) $entry[1]]; |
| 146 | + } |
| 147 | + |
| 148 | + return null; |
| 149 | + } |
| 150 | +} |
0 commit comments