| | 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.Threading.Tasks; |
| | 9 | |
|
| | 10 | | class EventHubPartitionPump : PartitionPump |
| | 11 | | { |
| | 12 | | EventHubClient eventHubClient; |
| | 13 | | PartitionReceiver partitionReceiver; |
| | 14 | | PartitionReceiveHandler partitionReceiveHandler; |
| | 15 | |
|
| | 16 | | public EventHubPartitionPump(EventProcessorHost host, Lease lease) |
| 0 | 17 | | : base(host, lease) |
| | 18 | | { |
| 0 | 19 | | } |
| | 20 | |
|
| | 21 | | protected override async Task OnOpenAsync() |
| | 22 | | { |
| 0 | 23 | | bool openedOK = false; |
| 0 | 24 | | int retryCount = 0; |
| 0 | 25 | | Exception lastException = null; |
| | 26 | | do |
| | 27 | | { |
| | 28 | | try |
| | 29 | | { |
| 0 | 30 | | await OpenClientsAsync().ConfigureAwait(false); |
| 0 | 31 | | openedOK = true; |
| 0 | 32 | | } |
| 0 | 33 | | catch (Exception e) |
| | 34 | | { |
| 0 | 35 | | lastException = e; |
| 0 | 36 | | ProcessorEventSource.Log.PartitionPumpWarning( |
| 0 | 37 | | this.Host.HostName, this.PartitionContext.PartitionId, "Failure creating client or receiver, ret |
| | 38 | |
|
| | 39 | | // Don't retry if we already lost the lease. |
| 0 | 40 | | if (e is ReceiverDisconnectedException) |
| | 41 | | { |
| 0 | 42 | | break; |
| | 43 | | } |
| | 44 | |
|
| 0 | 45 | | retryCount++; |
| 0 | 46 | | } |
| | 47 | | } |
| 0 | 48 | | while (!openedOK && (retryCount < 5)); |
| | 49 | |
|
| 0 | 50 | | if (!openedOK) |
| | 51 | | { |
| | 52 | | // IEventProcessor.onOpen is called from the base PartitionPump and must have returned in order for exec |
| | 53 | | // so we can report this error to it instead of the general error handler. |
| 0 | 54 | | await this.Processor.ProcessErrorAsync(this.PartitionContext, lastException).ConfigureAwait(false); |
| 0 | 55 | | this.PumpStatus = PartitionPumpStatus.OpenFailed; |
| | 56 | | } |
| | 57 | |
|
| 0 | 58 | | if (this.PumpStatus == PartitionPumpStatus.Opening) |
| | 59 | | { |
| 0 | 60 | | this.partitionReceiveHandler = new PartitionReceiveHandler(this); |
| | 61 | | // IEventProcessor.OnOpen is called from the base PartitionPump and must have returned in order for exec |
| | 62 | | // meaning it is safe to set the handler and start calling IEventProcessor.OnEvents. |
| | 63 | | // Set the status to running before setting the client handler, so the IEventProcessor.OnEvents can neve |
| 0 | 64 | | this.PumpStatus = PartitionPumpStatus.Running; |
| 0 | 65 | | this.partitionReceiver.SetReceiveHandler( |
| 0 | 66 | | this.partitionReceiveHandler, |
| 0 | 67 | | this.Host.EventProcessorOptions.InvokeProcessorAfterReceiveTimeout); |
| | 68 | | } |
| | 69 | |
|
| 0 | 70 | | if (this.PumpStatus == PartitionPumpStatus.OpenFailed) |
| | 71 | | { |
| 0 | 72 | | this.PumpStatus = PartitionPumpStatus.Closing; |
| 0 | 73 | | await this.CleanUpClientsAsync().ConfigureAwait(false); |
| 0 | 74 | | this.PumpStatus = PartitionPumpStatus.Closed; |
| | 75 | | } |
| 0 | 76 | | } |
| | 77 | |
|
| | 78 | | async Task OpenClientsAsync() // throws EventHubsException, IOException, InterruptedException, ExecutionExceptio |
| | 79 | | { |
| | 80 | | // Create new clients |
| 0 | 81 | | EventPosition eventPosition = await this.PartitionContext.GetInitialOffsetAsync().ConfigureAwait(false); |
| 0 | 82 | | long epoch = this.Lease.Epoch; |
| 0 | 83 | | ProcessorEventSource.Log.PartitionPumpCreateClientsStart(this.Host.HostName, this.PartitionContext.Partition |
| 0 | 84 | | $"Offset:{eventPosition.Offset}, SequenceNumber:{eventPosition.SequenceNumber}, DateTime:{eventPosition. |
| 0 | 85 | | this.eventHubClient = this.Host.CreateEventHubClient(); |
| 0 | 86 | | this.eventHubClient.WebProxy = this.Host.EventProcessorOptions.WebProxy; |
| | 87 | |
|
| 0 | 88 | | var receiverOptions = new ReceiverOptions() |
| 0 | 89 | | { |
| 0 | 90 | | // Enable receiver metrics? |
| 0 | 91 | | EnableReceiverRuntimeMetric = this.Host.EventProcessorOptions.EnableReceiverRuntimeMetric, |
| 0 | 92 | |
|
| 0 | 93 | | // Use host name as the identifier for debugging purpose |
| 0 | 94 | | // Shorten host name if name is longer than max allowed lenght. |
| 0 | 95 | | Identifier = this.Host.HostName.Length > ClientConstants.MaxReceiverIdentifierLength ? |
| 0 | 96 | | this.Host.HostName.Substring(0, ClientConstants.MaxReceiverIdentifierLength) : this.Host.HostName |
| 0 | 97 | | }; |
| | 98 | |
|
| | 99 | | // Create new receiver and set options |
| 0 | 100 | | this.partitionReceiver = this.eventHubClient.CreateEpochReceiver( |
| 0 | 101 | | this.PartitionContext.ConsumerGroupName, |
| 0 | 102 | | this.PartitionContext.PartitionId, |
| 0 | 103 | | eventPosition, |
| 0 | 104 | | epoch, |
| 0 | 105 | | receiverOptions); |
| | 106 | |
|
| 0 | 107 | | this.partitionReceiver.PrefetchCount = this.Host.EventProcessorOptions.PrefetchCount; |
| | 108 | |
|
| 0 | 109 | | ProcessorEventSource.Log.PartitionPumpCreateClientsStop(this.Host.HostName, this.PartitionContext.PartitionI |
| 0 | 110 | | } |
| | 111 | |
|
| | 112 | | async Task CleanUpClientsAsync() // swallows all exceptions |
| | 113 | | { |
| 0 | 114 | | if (this.partitionReceiver != null) |
| | 115 | | { |
| | 116 | | // Taking the lock means that there is no ProcessEventsAsync call in progress. |
| | 117 | | Task closeTask; |
| 0 | 118 | | using (await this.ProcessingAsyncLock.LockAsync().ConfigureAwait(false)) |
| | 119 | | { |
| | 120 | | // Calling PartitionReceiver.CloseAsync will gracefully close the IPartitionReceiveHandler we have i |
| 0 | 121 | | ProcessorEventSource.Log.PartitionPumpInfo(this.Host.HostName, this.PartitionContext.PartitionId, "C |
| 0 | 122 | | closeTask = this.partitionReceiver.CloseAsync(); |
| 0 | 123 | | } |
| | 124 | |
|
| 0 | 125 | | await closeTask.ConfigureAwait(false); |
| 0 | 126 | | this.partitionReceiver = null; |
| | 127 | | } |
| | 128 | |
|
| 0 | 129 | | if (this.eventHubClient != null) |
| | 130 | | { |
| 0 | 131 | | ProcessorEventSource.Log.PartitionPumpInfo(this.Host.HostName, this.PartitionContext.PartitionId, "Closi |
| 0 | 132 | | await this.eventHubClient.CloseAsync().ConfigureAwait(false); |
| 0 | 133 | | this.eventHubClient = null; |
| | 134 | | } |
| 0 | 135 | | } |
| | 136 | |
|
| | 137 | | protected override Task OnClosingAsync(CloseReason reason) |
| | 138 | | { |
| | 139 | | // Close the EH clients. Errors are swallowed, nothing we could do about them anyway. |
| 0 | 140 | | return CleanUpClientsAsync(); |
| | 141 | | } |
| | 142 | |
|
| | 143 | | class PartitionReceiveHandler : IPartitionReceiveHandler |
| | 144 | | { |
| | 145 | | readonly EventHubPartitionPump eventHubPartitionPump; |
| | 146 | |
|
| 0 | 147 | | public PartitionReceiveHandler(EventHubPartitionPump eventHubPartitionPump) |
| | 148 | | { |
| 0 | 149 | | this.eventHubPartitionPump = eventHubPartitionPump; |
| 0 | 150 | | this.MaxBatchSize = eventHubPartitionPump.Host.EventProcessorOptions.MaxBatchSize; |
| 0 | 151 | | } |
| | 152 | |
|
| 0 | 153 | | public int MaxBatchSize { get; set; } |
| | 154 | |
|
| | 155 | | public Task ProcessEventsAsync(IEnumerable<EventData> events) |
| | 156 | | { |
| | 157 | | // This method is called on the thread that the EH client uses to run the pump. |
| | 158 | | // There is one pump per EventHubClient. Since each PartitionPump creates a new EventHubClient, |
| | 159 | | // using that thread to call OnEvents does no harm. Even if OnEvents is slow, the pump will |
| | 160 | | // get control back each time OnEvents returns, and be able to receive a new batch of messages |
| | 161 | | // with which to make the next OnEvents call. The pump gains nothing by running faster than OnEvents. |
| 0 | 162 | | return this.eventHubPartitionPump.ProcessEventsAsync(events); |
| | 163 | | } |
| | 164 | |
|
| | 165 | | public async Task ProcessErrorAsync(Exception error) |
| | 166 | | { |
| | 167 | | bool faultPump; |
| | 168 | |
|
| 0 | 169 | | if (error == null) |
| | 170 | | { |
| 0 | 171 | | error = new Exception("No error info supplied by EventHub client"); |
| | 172 | | } |
| | 173 | |
|
| 0 | 174 | | if (error is ReceiverDisconnectedException) |
| | 175 | | { |
| | 176 | | // Trace as warning since ReceiverDisconnectedException is part of lease stealing logic. |
| 0 | 177 | | ProcessorEventSource.Log.PartitionPumpWarning( |
| 0 | 178 | | this.eventHubPartitionPump.Host.HostName, this.eventHubPartitionPump.PartitionContext.PartitionI |
| 0 | 179 | | "EventHub client disconnected, probably another host took the partition", error.Message); |
| | 180 | |
|
| | 181 | | // Shutdown the message pump when receiver is disconnected. |
| 0 | 182 | | faultPump = true; |
| | 183 | | } |
| | 184 | | else |
| | 185 | | { |
| 0 | 186 | | ProcessorEventSource.Log.PartitionPumpError( |
| 0 | 187 | | this.eventHubPartitionPump.Host.HostName, this.eventHubPartitionPump.PartitionContext.PartitionI |
| | 188 | |
|
| | 189 | | // No need to fault the pump, we expect receiver to recover on its own. |
| 0 | 190 | | faultPump = false; |
| | 191 | | } |
| | 192 | |
|
| | 193 | | try |
| | 194 | | { |
| | 195 | | // We would like to deliver all errors in the pump to error handler. |
| 0 | 196 | | await this.eventHubPartitionPump.ProcessErrorAsync(error).ConfigureAwait(false); |
| 0 | 197 | | } |
| | 198 | | finally |
| | 199 | | { |
| | 200 | | // Fault pump only when needed. |
| 0 | 201 | | if (faultPump) |
| | 202 | | { |
| 0 | 203 | | this.eventHubPartitionPump.PumpStatus = PartitionPumpStatus.Errored; |
| | 204 | | } |
| | 205 | | } |
| 0 | 206 | | } |
| | 207 | | } |
| | 208 | | } |
| | 209 | | } |