| | 1 | | // Copyright (c) Microsoft. All rights reserved. |
| | 2 | | // Licensed under the MIT license. See LICENSE file in the project root for full license information. |
| | 3 | |
|
| | 4 | | namespace Microsoft.Azure.EventHubs.Processor |
| | 5 | | { |
| | 6 | | using System; |
| | 7 | | using System.Collections.Generic; |
| | 8 | | using System.Linq; |
| | 9 | | using System.Threading; |
| | 10 | | using System.Threading.Tasks; |
| | 11 | |
|
| | 12 | | abstract class PartitionPump |
| | 13 | | { |
| | 14 | | CancellationTokenSource cancellationTokenSource; |
| | 15 | |
|
| 0 | 16 | | protected PartitionPump(EventProcessorHost host, Lease lease) |
| | 17 | | { |
| 0 | 18 | | this.Host = host; |
| 0 | 19 | | this.Lease = lease; |
| 0 | 20 | | this.ProcessingAsyncLock = new AsyncLock(); |
| 0 | 21 | | this.PumpStatus = PartitionPumpStatus.Uninitialized; |
| 0 | 22 | | } |
| | 23 | |
|
| 0 | 24 | | protected EventProcessorHost Host { get; } |
| | 25 | |
|
| 0 | 26 | | protected internal Lease Lease { get; } |
| | 27 | |
|
| 0 | 28 | | protected IEventProcessor Processor { get; private set; } |
| | 29 | |
|
| 0 | 30 | | protected PartitionContext PartitionContext { get; private set; } |
| | 31 | |
|
| 0 | 32 | | protected AsyncLock ProcessingAsyncLock { get; } |
| | 33 | |
|
| | 34 | | internal void SetLeaseToken(string newToken) |
| | 35 | | { |
| 0 | 36 | | this.PartitionContext.Lease.Token = newToken; |
| 0 | 37 | | } |
| | 38 | |
|
| | 39 | | public async Task OpenAsync() |
| | 40 | | { |
| 0 | 41 | | this.PumpStatus = PartitionPumpStatus.Opening; |
| | 42 | |
|
| 0 | 43 | | this.cancellationTokenSource = new CancellationTokenSource(); |
| | 44 | |
|
| 0 | 45 | | this.PartitionContext = new PartitionContext( |
| 0 | 46 | | this.Host, |
| 0 | 47 | | this.Lease.PartitionId, |
| 0 | 48 | | this.Host.EventHubPath, |
| 0 | 49 | | this.Host.ConsumerGroupName, |
| 0 | 50 | | this.cancellationTokenSource.Token); |
| 0 | 51 | | this.PartitionContext.Lease = this.Lease; |
| | 52 | |
|
| 0 | 53 | | if (this.PumpStatus == PartitionPumpStatus.Opening) |
| | 54 | | { |
| 0 | 55 | | string action = EventProcessorHostActionStrings.CreatingEventProcessor; |
| | 56 | | try |
| | 57 | | { |
| 0 | 58 | | this.Processor = this.Host.ProcessorFactory.CreateEventProcessor(this.PartitionContext); |
| 0 | 59 | | action = EventProcessorHostActionStrings.OpeningEventProcessor; |
| 0 | 60 | | ProcessorEventSource.Log.PartitionPumpInvokeProcessorOpenStart(this.Host.HostName, this.PartitionCon |
| 0 | 61 | | await this.Processor.OpenAsync(this.PartitionContext).ConfigureAwait(false); |
| 0 | 62 | | ProcessorEventSource.Log.PartitionPumpInvokeProcessorOpenStop(this.Host.HostName, this.PartitionCont |
| 0 | 63 | | } |
| 0 | 64 | | catch (Exception e) |
| | 65 | | { |
| | 66 | | // If the processor won't create or open, only thing we can do here is pass the buck. |
| | 67 | | // Null it out so we don't try to operate on it further. |
| 0 | 68 | | ProcessorEventSource.Log.PartitionPumpError(this.Host.HostName, this.PartitionContext.PartitionId, " |
| 0 | 69 | | this.Processor = null; |
| 0 | 70 | | this.Host.EventProcessorOptions.NotifyOfException(this.Host.HostName, this.PartitionContext.Partitio |
| 0 | 71 | | this.PumpStatus = PartitionPumpStatus.OpenFailed; |
| 0 | 72 | | } |
| 0 | 73 | | } |
| | 74 | |
|
| 0 | 75 | | if (this.PumpStatus == PartitionPumpStatus.Opening) |
| | 76 | | { |
| 0 | 77 | | await this.OnOpenAsync().ConfigureAwait(false); |
| | 78 | | } |
| 0 | 79 | | } |
| | 80 | |
|
| | 81 | | protected abstract Task OnOpenAsync(); |
| | 82 | |
|
| 0 | 83 | | protected internal PartitionPumpStatus PumpStatus { get; protected set; } |
| | 84 | |
|
| | 85 | | internal bool IsClosing |
| | 86 | | { |
| | 87 | | get |
| | 88 | | { |
| 0 | 89 | | return (this.PumpStatus == PartitionPumpStatus.Closing || this.PumpStatus == PartitionPumpStatus.Closed) |
| | 90 | | } |
| | 91 | | } |
| | 92 | |
|
| | 93 | | public async Task CloseAsync(CloseReason reason) |
| | 94 | | { |
| 0 | 95 | | ProcessorEventSource.Log.PartitionPumpCloseStart(this.Host.HostName, this.PartitionContext.PartitionId, reas |
| 0 | 96 | | this.PumpStatus = PartitionPumpStatus.Closing; |
| | 97 | |
|
| | 98 | | // Release lease as the first thing since closing receiver can take up to operation timeout. |
| | 99 | | // This helps other available hosts discover lease available sooner. |
| 0 | 100 | | if (reason != CloseReason.LeaseLost) |
| | 101 | | { |
| | 102 | | // Since this pump is dead, release the lease. |
| | 103 | | try |
| | 104 | | { |
| 0 | 105 | | await this.Host.LeaseManager.ReleaseLeaseAsync(this.PartitionContext.Lease).ConfigureAwait(false); |
| 0 | 106 | | } |
| 0 | 107 | | catch (Exception e) |
| | 108 | | { |
| | 109 | | // Log and ignore any failure since expired lease will be picked by another host. |
| 0 | 110 | | this.Host.EventProcessorOptions.NotifyOfException(this.Host.HostName, this.PartitionContext.Partitio |
| 0 | 111 | | } |
| | 112 | | } |
| | 113 | |
|
| | 114 | | try |
| | 115 | | { |
| 0 | 116 | | this.cancellationTokenSource.Cancel(); |
| | 117 | |
|
| 0 | 118 | | await this.OnClosingAsync(reason).ConfigureAwait(false); |
| | 119 | |
|
| 0 | 120 | | if (this.Processor != null) |
| | 121 | | { |
| 0 | 122 | | using (await this.ProcessingAsyncLock.LockAsync().ConfigureAwait(false)) |
| | 123 | | { |
| | 124 | | // When we take the lock, any existing ProcessEventsAsync call has finished. |
| | 125 | | // Because the client has been closed, there will not be any more |
| | 126 | | // calls to onEvents in the future. Therefore we can safely call CloseAsync. |
| 0 | 127 | | ProcessorEventSource.Log.PartitionPumpInvokeProcessorCloseStart(this.Host.HostName, this.Partiti |
| 0 | 128 | | await this.Processor.CloseAsync(this.PartitionContext, reason).ConfigureAwait(false); |
| 0 | 129 | | ProcessorEventSource.Log.PartitionPumpInvokeProcessorCloseStop(this.Host.HostName, this.Partitio |
| 0 | 130 | | } |
| | 131 | | } |
| 0 | 132 | | } |
| 0 | 133 | | catch (Exception e) |
| | 134 | | { |
| 0 | 135 | | ProcessorEventSource.Log.PartitionPumpCloseError(this.Host.HostName, this.PartitionContext.PartitionId, |
| | 136 | | // If closing the processor has failed, the state of the processor is suspect. |
| | 137 | | // Report the failure to the general error handler instead. |
| 0 | 138 | | this.Host.EventProcessorOptions.NotifyOfException(this.Host.HostName, this.PartitionContext.PartitionId, |
| 0 | 139 | | } |
| | 140 | |
|
| 0 | 141 | | this.PumpStatus = PartitionPumpStatus.Closed; |
| 0 | 142 | | ProcessorEventSource.Log.PartitionPumpCloseStop(this.Host.HostName, this.PartitionContext.PartitionId); |
| 0 | 143 | | } |
| | 144 | |
|
| | 145 | | protected abstract Task OnClosingAsync(CloseReason reason); |
| | 146 | |
|
| | 147 | | protected async Task ProcessEventsAsync(IEnumerable<EventData> events) |
| | 148 | | { |
| 0 | 149 | | if (events == null) |
| | 150 | | { |
| 0 | 151 | | events = Enumerable.Empty<EventData>(); |
| | 152 | | } |
| | 153 | |
|
| | 154 | | // Synchronize to serialize calls to the processor. |
| | 155 | | // The handler is not installed until after OpenAsync returns, so ProcessEventsAsync cannot conflict with Op |
| | 156 | | // There could be a conflict between ProcessEventsAsync and CloseAsync, however. All calls to CloseAsync are |
| | 157 | | // protected by synchronizing too. |
| 0 | 158 | | using (await this.ProcessingAsyncLock.LockAsync().ConfigureAwait(false)) |
| | 159 | | { |
| 0 | 160 | | ProcessorEventSource.Log.PartitionPumpInvokeProcessorEventsStart(this.Host.HostName, |
| 0 | 161 | | this.PartitionContext.PartitionId, events?.Count() ?? 0); |
| | 162 | | try |
| | 163 | | { |
| 0 | 164 | | EventData last = events?.LastOrDefault(); |
| 0 | 165 | | if (last != null) |
| | 166 | | { |
| 0 | 167 | | ProcessorEventSource.Log.PartitionPumpInfo( |
| 0 | 168 | | this.Host.HostName, |
| 0 | 169 | | this.PartitionContext.PartitionId, |
| 0 | 170 | | "Updating offset in partition context with end of batch " + last.SystemProperties.Offset + " |
| 0 | 171 | | this.PartitionContext.SetOffsetAndSequenceNumber(last); |
| 0 | 172 | | if (this.Host.EventProcessorOptions.EnableReceiverRuntimeMetric) |
| | 173 | | { |
| 0 | 174 | | this.PartitionContext.RuntimeInformation.Update(last); |
| | 175 | | } |
| | 176 | | } |
| | 177 | |
|
| 0 | 178 | | await this.Processor.ProcessEventsAsync(this.PartitionContext, events).ConfigureAwait(false); |
| 0 | 179 | | } |
| 0 | 180 | | catch (Exception e) |
| | 181 | | { |
| 0 | 182 | | ProcessorEventSource.Log.PartitionPumpInvokeProcessorEventsError(this.Host.HostName, this.PartitionC |
| 0 | 183 | | await this.ProcessErrorAsync(e).ConfigureAwait(false); |
| | 184 | | } |
| | 185 | | finally |
| | 186 | | { |
| 0 | 187 | | ProcessorEventSource.Log.PartitionPumpInvokeProcessorEventsStop(this.Host.HostName, this.PartitionCo |
| | 188 | | } |
| 0 | 189 | | } |
| 0 | 190 | | } |
| | 191 | |
|
| | 192 | | protected Task ProcessErrorAsync(Exception error) |
| | 193 | | { |
| 0 | 194 | | return this.Processor.ProcessErrorAsync(this.PartitionContext, error); |
| | 195 | | } |
| | 196 | | } |
| | 197 | | } |