| | | 1 | | using Microsoft.Extensions.Logging; |
| | | 2 | | using StackExchange.Redis; |
| | | 3 | | using System.Globalization; |
| | | 4 | | using System.Net; |
| | | 5 | | |
| | | 6 | | namespace AsyncResponse.Channels.Redis; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// The slice of a <c>CLUSTER NODES</c> reply the recovery scan and the liveness probe need: which |
| | | 10 | | /// address each node is reachable at, and whether it owns slots as a primary. Parsed from the raw |
| | | 11 | | /// reply — the text format is Redis's own wire contract — rather than read through the client's |
| | | 12 | | /// <c>ClusterConfiguration</c>, which cannot be constructed outside the client and would leave |
| | | 13 | | /// this classification untestable. |
| | | 14 | | /// <para> |
| | | 15 | | /// One line per node: <c>id ip:port@cport[,hostname] flags master-id ping pong epoch link-state |
| | | 16 | | /// [slot ...]</c>. Redis 3 omits <c>@cport</c>; an IPv6 address carries colons of its own, so the |
| | | 17 | | /// port is whatever follows the LAST colon. |
| | | 18 | | /// </para> |
| | | 19 | | /// </summary> |
| | | 20 | | internal static class RedisClusterNodeTable |
| | | 21 | | { |
| | 250 | 22 | | internal readonly record struct Node(string Address, string? HostName, int Port, bool IsReplica, bool HasSlots) |
| | | 23 | | { |
| | | 24 | | /// <summary> |
| | | 25 | | /// A primary with a slot field of any kind — a migration marker counts: a primary that is |
| | | 26 | | /// importing its first slot already holds the keys moved into it so far. |
| | | 27 | | /// </summary> |
| | 24 | 28 | | public bool IsSlotOwner => !IsReplica && HasSlots; |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | private const int FirstSlotField = 8; |
| | | 32 | | |
| | | 33 | | internal static List<Node> Parse(string nodeTable) |
| | | 34 | | { |
| | 8 | 35 | | var nodes = new List<Node>(); |
| | 68 | 36 | | foreach (var line in nodeTable.Split('\n', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntrie |
| | | 37 | | { |
| | 26 | 38 | | var fields = line.Split(' ', StringSplitOptions.RemoveEmptyEntries); |
| | 26 | 39 | | if (fields.Length < 3) |
| | | 40 | | continue; |
| | | 41 | | |
| | 24 | 42 | | var address = fields[1]; |
| | 24 | 43 | | string? hostName = null; |
| | 24 | 44 | | if (address.IndexOf(',') is var comma and >= 0) |
| | | 45 | | { |
| | 2 | 46 | | hostName = address[(comma + 1)..]; |
| | 2 | 47 | | address = address[..comma]; |
| | | 48 | | } |
| | | 49 | | |
| | 24 | 50 | | if (address.IndexOf('@') is var bus and >= 0) |
| | 20 | 51 | | address = address[..bus]; |
| | | 52 | | |
| | 24 | 53 | | var hasSlots = fields.Length > FirstSlotField; |
| | 24 | 54 | | var portSeparator = address.LastIndexOf(':'); |
| | 24 | 55 | | var port = 0; |
| | 24 | 56 | | if (portSeparator <= 0 |
| | 24 | 57 | | || !int.TryParse(address.AsSpan(portSeparator + 1), NumberStyles.None, CultureInfo.InvariantCulture, out |
| | | 58 | | { |
| | | 59 | | // No usable address (the noaddr flag prints ":0@0"). A line that lists slots is |
| | | 60 | | // kept anyway, under an address no endpoint can match: dropped, a slot owner |
| | | 61 | | // nobody can reach would vanish from the table, and a coverage check over the |
| | | 62 | | // rest would pass without its shard. |
| | 2 | 63 | | if (!hasSlots) |
| | | 64 | | continue; |
| | | 65 | | |
| | 0 | 66 | | (address, portSeparator, port) = (string.Empty, 0, 0); |
| | | 67 | | } |
| | | 68 | | |
| | 22 | 69 | | var flags = fields[2].Split(','); |
| | 22 | 70 | | nodes.Add(new Node( |
| | 22 | 71 | | address[..portSeparator], |
| | 22 | 72 | | string.IsNullOrEmpty(hostName) ? null : hostName, |
| | 22 | 73 | | port, |
| | 30 | 74 | | IsReplica: Array.Exists(flags, static flag => flag is "slave" or "replica"), |
| | 22 | 75 | | HasSlots: hasSlots)); |
| | | 76 | | } |
| | | 77 | | |
| | 8 | 78 | | return nodes; |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// Reads and parses <paramref name="clusterNode"/>'s view of the node table, or <c>null</c> |
| | | 83 | | /// when it cannot be read or lists nothing — which every caller treats as "unknown", never as |
| | | 84 | | /// "no slot owners". |
| | | 85 | | /// </summary> |
| | | 86 | | internal static async Task<List<Node>?> TryReadAsync(IServer clusterNode, ILogger logger) |
| | | 87 | | { |
| | | 88 | | string? nodeTable; |
| | | 89 | | try |
| | | 90 | | { |
| | 20 | 91 | | nodeTable = await clusterNode.ClusterNodesRawAsync().ConfigureAwait(false); |
| | 18 | 92 | | } |
| | 2 | 93 | | catch (Exception ex) when (ex is RedisException or TimeoutException or InvalidOperationException) |
| | | 94 | | { |
| | 2 | 95 | | logger.LogDebug(ex, "Could not read CLUSTER NODES from {EndPoint}.", clusterNode.EndPoint); |
| | 2 | 96 | | return null; |
| | | 97 | | } |
| | | 98 | | |
| | 18 | 99 | | if (string.IsNullOrWhiteSpace(nodeTable)) |
| | 12 | 100 | | return null; |
| | | 101 | | |
| | 6 | 102 | | var nodes = Parse(nodeTable); |
| | 6 | 103 | | return nodes.Count == 0 ? null : nodes; |
| | 20 | 104 | | } |
| | | 105 | | |
| | | 106 | | /// <summary> |
| | | 107 | | /// <c>true</c> only when the table LISTS <paramref name="endPoint"/> and lists it as a replica |
| | | 108 | | /// or as a node without slots. An endpoint the table does not list (a DNS name the cluster |
| | | 109 | | /// does not announce, a stale configuration entry) is unknown, and unknown is not "owns |
| | | 110 | | /// nothing". |
| | | 111 | | /// </summary> |
| | | 112 | | internal static bool OwnsNoSlots(List<Node> nodes, EndPoint? endPoint) |
| | | 113 | | { |
| | 210 | 114 | | foreach (var node in nodes) |
| | | 115 | | { |
| | 88 | 116 | | if (IsSameNode(node, endPoint)) |
| | 18 | 117 | | return !node.IsSlotOwner; |
| | | 118 | | } |
| | | 119 | | |
| | 8 | 120 | | return false; |
| | 18 | 121 | | } |
| | | 122 | | |
| | | 123 | | /// <summary>Whether <paramref name="endPoint"/> is the address (or announced hostname) and port the table lists <pa |
| | | 124 | | internal static bool IsSameNode(Node node, EndPoint? endPoint) |
| | | 125 | | { |
| | 94 | 126 | | var (host, port) = endPoint switch |
| | 94 | 127 | | { |
| | 72 | 128 | | IPEndPoint ip => (ip.Address.ToString(), ip.Port), |
| | 12 | 129 | | DnsEndPoint dns => (dns.Host, dns.Port), |
| | 10 | 130 | | _ => (null, 0) |
| | 94 | 131 | | }; |
| | | 132 | | |
| | 94 | 133 | | return host is not null |
| | 94 | 134 | | && node.Port == port |
| | 94 | 135 | | && (SameHost(node.Address, host) || (node.HostName is { } announced && SameHost(announced, host))); |
| | | 136 | | } |
| | | 137 | | |
| | | 138 | | private static bool SameHost(string left, string right) |
| | 88 | 139 | | => IPAddress.TryParse(left, out var leftAddress) && IPAddress.TryParse(right, out var rightAddress) |
| | 88 | 140 | | ? leftAddress.Equals(rightAddress) |
| | 88 | 141 | | : string.Equals(left, right, StringComparison.OrdinalIgnoreCase); |
| | | 142 | | } |