|
| 1 | +namespace StackExchange.Redis; |
| 2 | + |
| 3 | +public sealed partial class HotKeysResult |
| 4 | +{ |
| 5 | + internal static readonly ResultProcessor<HotKeysResult?> Processor = new HotKeysResultProcessor(); |
| 6 | + |
| 7 | + private sealed class HotKeysResultProcessor : ResultProcessor<HotKeysResult?> |
| 8 | + { |
| 9 | + protected override bool SetResultCore(PhysicalConnection connection, Message message, in RawResult result) |
| 10 | + { |
| 11 | + if (result.IsNull) |
| 12 | + { |
| 13 | + SetResult(message, null); |
| 14 | + return true; |
| 15 | + } |
| 16 | + |
| 17 | + // an array with a single element that *is* an array/map that is the results |
| 18 | + if (result is { Resp2TypeArray: ResultType.Array, ItemsCount: 1 }) |
| 19 | + { |
| 20 | + ref readonly RawResult inner = ref result[0]; |
| 21 | + if (inner is { Resp2TypeArray: ResultType.Array, IsNull: false }) |
| 22 | + { |
| 23 | + var hotKeys = new HotKeysResult(in inner); |
| 24 | + SetResult(message, hotKeys); |
| 25 | + return true; |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + return false; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + private HotKeysResult(in RawResult result) |
| 34 | + { |
| 35 | + var metrics = HotKeysMetrics.None; // we infer this from the keys present |
| 36 | + var iter = result.GetItems().GetEnumerator(); |
| 37 | + while (iter.MoveNext()) |
| 38 | + { |
| 39 | + ref readonly RawResult key = ref iter.Current; |
| 40 | + if (!iter.MoveNext()) break; // lies about the length! |
| 41 | + ref readonly RawResult value = ref iter.Current; |
| 42 | + var hash = key.Payload.Hash64(); |
| 43 | + long i64; |
| 44 | + switch (hash) |
| 45 | + { |
| 46 | + case tracking_active.Hash when tracking_active.Is(hash, key): |
| 47 | + TrackingActive = value.GetBoolean(); |
| 48 | + break; |
| 49 | + case sample_ratio.Hash when sample_ratio.Is(hash, key) && value.TryGetInt64(out i64): |
| 50 | + SampleRatio = i64; |
| 51 | + break; |
| 52 | + case selected_slots.Hash when selected_slots.Is(hash, key) & value.Resp2TypeArray is ResultType.Array: |
| 53 | + var len = value.ItemsCount; |
| 54 | + if (len == 0) |
| 55 | + { |
| 56 | + _selectedSlots = []; |
| 57 | + continue; |
| 58 | + } |
| 59 | + |
| 60 | + var items = value.GetItems().GetEnumerator(); |
| 61 | + var slots = len == 1 ? null : new SlotRange[len]; |
| 62 | + for (int i = 0; i < len && items.MoveNext(); i++) |
| 63 | + { |
| 64 | + ref readonly RawResult pair = ref items.Current; |
| 65 | + if (pair.Resp2TypeArray is ResultType.Array) |
| 66 | + { |
| 67 | + long from = -1, to = -1; |
| 68 | + switch (pair.ItemsCount) |
| 69 | + { |
| 70 | + case 1 when pair[0].TryGetInt64(out from): |
| 71 | + to = from; // single slot |
| 72 | + break; |
| 73 | + case 2 when pair[0].TryGetInt64(out from) && pair[1].TryGetInt64(out to): |
| 74 | + break; |
| 75 | + } |
| 76 | + |
| 77 | + if (from < SlotRange.MinSlot) |
| 78 | + { |
| 79 | + // skip invalid ranges |
| 80 | + } |
| 81 | + else if (len == 1 & from == SlotRange.MinSlot & to == SlotRange.MaxSlot) |
| 82 | + { |
| 83 | + // this is the "normal" case when no slot filter was applied |
| 84 | + slots = SlotRange.SharedAllSlots; // avoid the alloc |
| 85 | + } |
| 86 | + else |
| 87 | + { |
| 88 | + slots ??= new SlotRange[len]; |
| 89 | + slots[i] = new((int)from, (int)to); |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + _selectedSlots = slots; |
| 94 | + break; |
| 95 | + case all_commands_all_slots_us.Hash when all_commands_all_slots_us.Is(hash, key) && value.TryGetInt64(out i64): |
| 96 | + AllCommandsAllSlotsMicroseconds = i64; |
| 97 | + break; |
| 98 | + case all_commands_selected_slots_us.Hash when all_commands_selected_slots_us.Is(hash, key) && value.TryGetInt64(out i64): |
| 99 | + AllCommandSelectedSlotsMicroseconds = i64; |
| 100 | + break; |
| 101 | + case sampled_command_selected_slots_us.Hash when sampled_command_selected_slots_us.Is(hash, key) && value.TryGetInt64(out i64): |
| 102 | + case sampled_commands_selected_slots_us.Hash when sampled_commands_selected_slots_us.Is(hash, key) && value.TryGetInt64(out i64): |
| 103 | + SampledCommandsSelectedSlotsMicroseconds = i64; |
| 104 | + break; |
| 105 | + case net_bytes_all_commands_all_slots.Hash when net_bytes_all_commands_all_slots.Is(hash, key) && value.TryGetInt64(out i64): |
| 106 | + AllCommandsAllSlotsNetworkBytes = i64; |
| 107 | + break; |
| 108 | + case net_bytes_all_commands_selected_slots.Hash when net_bytes_all_commands_selected_slots.Is(hash, key) && value.TryGetInt64(out i64): |
| 109 | + NetworkBytesAllCommandsSelectedSlotsRaw = i64; |
| 110 | + break; |
| 111 | + case net_bytes_sampled_commands_selected_slots.Hash when net_bytes_sampled_commands_selected_slots.Is(hash, key) && value.TryGetInt64(out i64): |
| 112 | + NetworkBytesSampledCommandsSelectedSlotsRaw = i64; |
| 113 | + break; |
| 114 | + case collection_start_time_unix_ms.Hash when collection_start_time_unix_ms.Is(hash, key) && value.TryGetInt64(out i64): |
| 115 | + CollectionStartTimeUnixMilliseconds = i64; |
| 116 | + break; |
| 117 | + case collection_duration_ms.Hash when collection_duration_ms.Is(hash, key) && value.TryGetInt64(out i64): |
| 118 | + CollectionDurationMicroseconds = i64 * 1000; // ms vs us is in question: support both, and abstract it from the caller |
| 119 | + break; |
| 120 | + case collection_duration_us.Hash when collection_duration_us.Is(hash, key) && value.TryGetInt64(out i64): |
| 121 | + CollectionDurationMicroseconds = i64; |
| 122 | + break; |
| 123 | + case total_cpu_time_sys_ms.Hash when total_cpu_time_sys_ms.Is(hash, key) && value.TryGetInt64(out i64): |
| 124 | + metrics |= HotKeysMetrics.Cpu; |
| 125 | + TotalCpuTimeSystemMicroseconds = i64 * 1000; // ms vs us is in question: support both, and abstract it from the caller |
| 126 | + break; |
| 127 | + case total_cpu_time_sys_us.Hash when total_cpu_time_sys_us.Is(hash, key) && value.TryGetInt64(out i64): |
| 128 | + metrics |= HotKeysMetrics.Cpu; |
| 129 | + TotalCpuTimeSystemMicroseconds = i64; |
| 130 | + break; |
| 131 | + case total_cpu_time_user_ms.Hash when total_cpu_time_user_ms.Is(hash, key) && value.TryGetInt64(out i64): |
| 132 | + metrics |= HotKeysMetrics.Cpu; |
| 133 | + TotalCpuTimeUserMicroseconds = i64 * 1000; // ms vs us is in question: support both, and abstract it from the caller |
| 134 | + break; |
| 135 | + case total_cpu_time_user_us.Hash when total_cpu_time_user_us.Is(hash, key) && value.TryGetInt64(out i64): |
| 136 | + metrics |= HotKeysMetrics.Cpu; |
| 137 | + TotalCpuTimeUserMicroseconds = i64; |
| 138 | + break; |
| 139 | + case total_net_bytes.Hash when total_net_bytes.Is(hash, key) && value.TryGetInt64(out i64): |
| 140 | + metrics |= HotKeysMetrics.Network; |
| 141 | + TotalNetworkBytesRaw = i64; |
| 142 | + break; |
| 143 | + case by_cpu_time_us.Hash when by_cpu_time_us.Is(hash, key) & value.Resp2TypeArray is ResultType.Array: |
| 144 | + metrics |= HotKeysMetrics.Cpu; |
| 145 | + len = value.ItemsCount / 2; |
| 146 | + if (len == 0) |
| 147 | + { |
| 148 | + _cpuByKey = []; |
| 149 | + continue; |
| 150 | + } |
| 151 | + |
| 152 | + var cpuTime = new MetricKeyCpu[len]; |
| 153 | + items = value.GetItems().GetEnumerator(); |
| 154 | + for (int i = 0; i < len && items.MoveNext(); i++) |
| 155 | + { |
| 156 | + var metricKey = items.Current.AsRedisKey(); |
| 157 | + if (items.MoveNext() && items.Current.TryGetInt64(out var metricValue)) |
| 158 | + { |
| 159 | + cpuTime[i] = new(metricKey, metricValue); |
| 160 | + } |
| 161 | + } |
| 162 | + |
| 163 | + _cpuByKey = cpuTime; |
| 164 | + break; |
| 165 | + case by_net_bytes.Hash when by_net_bytes.Is(hash, key) & value.Resp2TypeArray is ResultType.Array: |
| 166 | + metrics |= HotKeysMetrics.Network; |
| 167 | + len = value.ItemsCount / 2; |
| 168 | + if (len == 0) |
| 169 | + { |
| 170 | + _networkBytesByKey = []; |
| 171 | + continue; |
| 172 | + } |
| 173 | + |
| 174 | + var netBytes = new MetricKeyBytes[len]; |
| 175 | + items = value.GetItems().GetEnumerator(); |
| 176 | + for (int i = 0; i < len && items.MoveNext(); i++) |
| 177 | + { |
| 178 | + var metricKey = items.Current.AsRedisKey(); |
| 179 | + if (items.MoveNext() && items.Current.TryGetInt64(out var metricValue)) |
| 180 | + { |
| 181 | + netBytes[i] = new(metricKey, metricValue); |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + _networkBytesByKey = netBytes; |
| 186 | + break; |
| 187 | + } // switch |
| 188 | + } // while |
| 189 | + Metrics = metrics; |
| 190 | + } |
| 191 | + |
| 192 | +#pragma warning disable SA1134, SA1300 |
| 193 | + // ReSharper disable InconsistentNaming |
| 194 | + [FastHash] internal static partial class tracking_active { } |
| 195 | + [FastHash] internal static partial class sample_ratio { } |
| 196 | + [FastHash] internal static partial class selected_slots { } |
| 197 | + [FastHash] internal static partial class all_commands_all_slots_us { } |
| 198 | + [FastHash] internal static partial class all_commands_selected_slots_us { } |
| 199 | + [FastHash] internal static partial class sampled_command_selected_slots_us { } |
| 200 | + [FastHash] internal static partial class sampled_commands_selected_slots_us { } |
| 201 | + [FastHash] internal static partial class net_bytes_all_commands_all_slots { } |
| 202 | + [FastHash] internal static partial class net_bytes_all_commands_selected_slots { } |
| 203 | + [FastHash] internal static partial class net_bytes_sampled_commands_selected_slots { } |
| 204 | + [FastHash] internal static partial class collection_start_time_unix_ms { } |
| 205 | + [FastHash] internal static partial class collection_duration_ms { } |
| 206 | + [FastHash] internal static partial class collection_duration_us { } |
| 207 | + [FastHash] internal static partial class total_cpu_time_user_ms { } |
| 208 | + [FastHash] internal static partial class total_cpu_time_user_us { } |
| 209 | + [FastHash] internal static partial class total_cpu_time_sys_ms { } |
| 210 | + [FastHash] internal static partial class total_cpu_time_sys_us { } |
| 211 | + [FastHash] internal static partial class total_net_bytes { } |
| 212 | + [FastHash] internal static partial class by_cpu_time_us { } |
| 213 | + [FastHash] internal static partial class by_net_bytes { } |
| 214 | + |
| 215 | + // ReSharper restore InconsistentNaming |
| 216 | +#pragma warning restore SA1134, SA1300 |
| 217 | +} |
0 commit comments