|
| 1 | +namespace Pulsar.Client.Api |
| 2 | + |
| 3 | +open System |
| 4 | +open System.Net.Sockets |
| 5 | +open System.Threading |
| 6 | +open System.Threading.Tasks |
| 7 | +open Microsoft.Extensions.Logging |
| 8 | +open Pulsar.Client.Common |
| 9 | +open Pulsar.Client.Internal |
| 10 | + |
| 11 | +type private AutoServiceInfo = { |
| 12 | + ServiceInfo: ServiceInfo |
| 13 | + EndPointResolver: EndPointResolver |
| 14 | +} |
| 15 | + |
| 16 | +type AutoClusterFailover |
| 17 | + ( |
| 18 | + primary: ServiceInfo, |
| 19 | + secondary: ServiceInfo array, |
| 20 | + failoverDelay: TimeSpan, |
| 21 | + switchBackDelay: TimeSpan, |
| 22 | + checkInterval: TimeSpan |
| 23 | + ) = |
| 24 | + |
| 25 | + let getAutoServiceInfo (serviceInfo: ServiceInfo) = |
| 26 | + { ServiceInfo = serviceInfo; EndPointResolver = EndPointResolver(serviceInfo.ServiceUrl.Addresses) } |
| 27 | + |
| 28 | + let primaryServiceInfo = getAutoServiceInfo primary |
| 29 | + let secondaryServiceInfos = secondary |> Array.map getAutoServiceInfo |
| 30 | + let mutable currentServiceInfo = primaryServiceInfo |
| 31 | + let cts = new CancellationTokenSource() |
| 32 | + |
| 33 | + let mutable recoveredTimestamp: DateTime option = None |
| 34 | + let mutable failedTimestamp: DateTime option = None |
| 35 | + |
| 36 | + let probeAvailable (resolve: EndPointResolver) = |
| 37 | + backgroundTask { |
| 38 | + let endpoint = resolve.Resolve() |
| 39 | + try |
| 40 | + use client = new TcpClient() |
| 41 | + let connectTask = client.ConnectAsync(endpoint.Host, endpoint.Port) |
| 42 | + let! completedTask = Task.WhenAny(connectTask, Task.Delay(30_000)) |
| 43 | + if completedTask = (connectTask :> Task) then |
| 44 | + do! connectTask |
| 45 | + return true |
| 46 | + else |
| 47 | + return false |
| 48 | + with ex -> |
| 49 | + Log.Logger.LogWarning(ex, "Failed to probe available, url: {0}", endpoint) |
| 50 | + return false |
| 51 | + } |
| 52 | + |
| 53 | + let run (ctx: IServiceInfoProviderContext) = |
| 54 | + Log.Logger.LogInformation("Initializing AutoClusterFailover") |
| 55 | + backgroundTask { |
| 56 | + while not cts.IsCancellationRequested do |
| 57 | + try |
| 58 | + do! Task.Delay(checkInterval, cts.Token) |
| 59 | + if currentServiceInfo = primaryServiceInfo then |
| 60 | + let! available = probeAvailable primaryServiceInfo.EndPointResolver |
| 61 | + if not available then |
| 62 | + match failedTimestamp with |
| 63 | + | None -> |
| 64 | + failedTimestamp <- Some DateTime.UtcNow |
| 65 | + | Some ts when DateTime.UtcNow - ts >= failoverDelay -> |
| 66 | + let! targetSecondary = |
| 67 | + task { |
| 68 | + let mutable found = None |
| 69 | + for sec in secondaryServiceInfos do |
| 70 | + if found.IsNone then |
| 71 | + let! avail = probeAvailable sec.EndPointResolver |
| 72 | + if avail then found <- Some sec |
| 73 | + return found |
| 74 | + } |
| 75 | + match targetSecondary with |
| 76 | + | Some sec -> |
| 77 | + Log.Logger.LogInformation("Switching to secondary cluster {0}", sec.ServiceInfo.ServiceUrl) |
| 78 | + currentServiceInfo <- sec |
| 79 | + do! ctx.UpdateServiceInfo(sec.ServiceInfo) |
| 80 | + failedTimestamp <- None |
| 81 | + | None -> |
| 82 | + Log.Logger.LogWarning("Could not find any available secondary cluster") |
| 83 | + | _ -> () |
| 84 | + else |
| 85 | + failedTimestamp <- None |
| 86 | + else |
| 87 | + let! available = probeAvailable primaryServiceInfo.EndPointResolver |
| 88 | + if available then |
| 89 | + match recoveredTimestamp with |
| 90 | + | None -> |
| 91 | + recoveredTimestamp <- Some DateTime.UtcNow |
| 92 | + | Some ts when DateTime.UtcNow - ts >= switchBackDelay -> |
| 93 | + Log.Logger.LogInformation("Switching back to primary cluster {0}", primary) |
| 94 | + currentServiceInfo <- primaryServiceInfo |
| 95 | + do! ctx.UpdateServiceInfo(primary) |
| 96 | + recoveredTimestamp <- None |
| 97 | + | _ -> () |
| 98 | + else |
| 99 | + recoveredTimestamp <- None |
| 100 | + with |
| 101 | + | :? OperationCanceledException when cts.IsCancellationRequested -> () |
| 102 | + | :? TaskCanceledException when cts.IsCancellationRequested -> () |
| 103 | + | Flatten ex -> |
| 104 | + Log.Logger.LogError(ex, "Error checking cluster") |
| 105 | + } |
| 106 | + |> ignore |
| 107 | + |
| 108 | + |
| 109 | + interface IServiceInfoProvider with |
| 110 | + member this.Initialize(context: IServiceInfoProviderContext) = |
| 111 | + run context |
| 112 | + member this.GetServiceInfo() = currentServiceInfo.ServiceInfo |
| 113 | + |
| 114 | + member this.Dispose() = |
| 115 | + cts.Cancel() |
| 116 | + cts.Dispose() |
| 117 | + |
| 118 | + |
| 119 | +type AutoClusterFailoverBuilder() = |
| 120 | + let mutable primary = None |
| 121 | + let mutable secondary = [||] |
| 122 | + let mutable failoverDelay = TimeSpan.FromSeconds(30.0) |
| 123 | + let mutable switchBackDelay = TimeSpan.FromSeconds(60.0) |
| 124 | + let mutable checkInterval = TimeSpan.FromSeconds(30.0) |
| 125 | + |
| 126 | + member this.Primary(serviceInfo: ServiceInfo) = |
| 127 | + primary <- serviceInfo |> invalidArgIfDefault "ServiceInfo can't be null" |> Some |
| 128 | + this |
| 129 | + |
| 130 | + member this.Secondary(serviceInfos: ServiceInfo seq) = |
| 131 | + secondary <- serviceInfos |> Seq.toArray |
| 132 | + this |
| 133 | + |
| 134 | + member this.FailoverDelay(delay: TimeSpan) = |
| 135 | + failoverDelay <- delay |
| 136 | + this |
| 137 | + |
| 138 | + member this.SwitchBackDelay(delay: TimeSpan) = |
| 139 | + switchBackDelay <- delay |
| 140 | + this |
| 141 | + |
| 142 | + member this.CheckInterval(interval: TimeSpan) = |
| 143 | + checkInterval <- interval |
| 144 | + this |
| 145 | + |
| 146 | + member this.Build() : IServiceInfoProvider = |
| 147 | + if primary.IsNone then |
| 148 | + invalidArg "primary" "Primary serviceInfo must be set" |
| 149 | + if Array.isEmpty secondary then |
| 150 | + invalidArg "secondary" "Secondary serviceInfo list should have at least one item" |
| 151 | + |
| 152 | + new AutoClusterFailover( |
| 153 | + primary.Value, |
| 154 | + secondary, |
| 155 | + failoverDelay, |
| 156 | + switchBackDelay, |
| 157 | + checkInterval |
| 158 | + ) :> IServiceInfoProvider |
0 commit comments