| | 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.Threading; |
| | 8 | | using System.Threading.Tasks; |
| | 9 | | using Microsoft.Azure.EventHubs.Primitives; |
| | 10 | |
|
| | 11 | | /// <summary> |
| | 12 | | /// Encapsulates information related to an Event Hubs partition used by <see cref="IEventProcessor"/>. |
| | 13 | | /// </summary> |
| | 14 | | public class PartitionContext |
| | 15 | | { |
| | 16 | | readonly EventProcessorHost host; |
| | 17 | |
|
| | 18 | | /// Creates a new <see cref="PartitionContext"/> object. |
| 0 | 19 | | public PartitionContext(EventProcessorHost host, string partitionId, string eventHubPath, string consumerGroupNa |
| | 20 | | { |
| 0 | 21 | | this.host = host; |
| 0 | 22 | | this.PartitionId = partitionId; |
| 0 | 23 | | this.EventHubPath = eventHubPath; |
| 0 | 24 | | this.ConsumerGroupName = consumerGroupName; |
| 0 | 25 | | this.CancellationToken = cancellationToken; |
| 0 | 26 | | this.ThisLock = new object(); |
| 0 | 27 | | this.Offset = EventPosition.FromStart().Offset; |
| 0 | 28 | | this.SequenceNumber = 0; |
| 0 | 29 | | this.RuntimeInformation = new ReceiverRuntimeInformation(partitionId); |
| 0 | 30 | | } |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// Gets triggered when the partition gets closed. |
| | 34 | | /// </summary> |
| 0 | 35 | | public CancellationToken CancellationToken { get; } |
| | 36 | |
|
| | 37 | | /// <summary> |
| | 38 | | /// Gets the name of the consumer group. |
| | 39 | | /// </summary> |
| 0 | 40 | | public string ConsumerGroupName { get; } |
| | 41 | |
|
| | 42 | | /// <summary> |
| | 43 | | /// Gets the path of the event hub. |
| | 44 | | /// </summary> |
| 0 | 45 | | public string EventHubPath { get; } |
| | 46 | |
|
| | 47 | | /// <summary> |
| | 48 | | /// Gets the partition ID for the context. |
| | 49 | | /// </summary> |
| 0 | 50 | | public string PartitionId { get; } |
| | 51 | |
|
| | 52 | | /// <summary> |
| | 53 | | /// Gets the host owner for the partition. |
| | 54 | | /// </summary> |
| 0 | 55 | | public string Owner => this.Lease.Owner; |
| | 56 | |
|
| | 57 | | /// <summary> |
| | 58 | | /// Gets the approximate receiver runtime information for a logical partition of an Event Hub. |
| | 59 | | /// To enable the setting, refer to <see cref="EventProcessorOptions.EnableReceiverRuntimeMetric"/> |
| | 60 | | /// </summary> |
| 0 | 61 | | public ReceiverRuntimeInformation RuntimeInformation { get; } |
| | 62 | |
|
| 0 | 63 | | internal string Offset { get; set; } |
| | 64 | |
|
| 0 | 65 | | internal long SequenceNumber { get; set; } |
| | 66 | |
|
| | 67 | | /// <summary> |
| | 68 | | /// Gets the most recent checkpointed lease. |
| | 69 | | /// </summary> |
| | 70 | | // Unlike other properties which are immutable after creation, the lease is updated dynamically and needs a sett |
| 0 | 71 | | public Lease Lease { get; internal set; } |
| | 72 | |
|
| 0 | 73 | | object ThisLock { get; } |
| | 74 | |
|
| | 75 | | internal void SetOffsetAndSequenceNumber(EventData eventData) |
| | 76 | | { |
| 0 | 77 | | Guard.ArgumentNotNull(nameof(eventData), eventData); |
| | 78 | |
|
| 0 | 79 | | lock (this.ThisLock) |
| | 80 | | { |
| 0 | 81 | | this.Offset = eventData.SystemProperties.Offset; |
| 0 | 82 | | this.SequenceNumber = eventData.SystemProperties.SequenceNumber; |
| 0 | 83 | | } |
| 0 | 84 | | } |
| | 85 | |
|
| | 86 | | internal async Task<EventPosition> GetInitialOffsetAsync() // throws InterruptedException, ExecutionException |
| | 87 | | { |
| 0 | 88 | | Checkpoint startingCheckpoint = await this.host.CheckpointManager.GetCheckpointAsync(this.PartitionId).Confi |
| | 89 | | EventPosition eventPosition; |
| | 90 | |
|
| 0 | 91 | | if (startingCheckpoint == null) |
| | 92 | | { |
| | 93 | | // No checkpoint was ever stored. Use the initialOffsetProvider instead. |
| 0 | 94 | | ProcessorEventSource.Log.PartitionPumpInfo(this.host.HostName, this.PartitionId, "Calling user-provided |
| 0 | 95 | | eventPosition = this.host.EventProcessorOptions.InitialOffsetProvider(this.PartitionId); |
| 0 | 96 | | ProcessorEventSource.Log.PartitionPumpInfo( |
| 0 | 97 | | this.host.HostName, |
| 0 | 98 | | this.PartitionId, |
| 0 | 99 | | $"Initial Position Provider. Offset:{eventPosition.Offset}, SequenceNumber:{eventPosition.SequenceNu |
| | 100 | | } |
| | 101 | | else |
| | 102 | | { |
| 0 | 103 | | this.Offset = startingCheckpoint.Offset; |
| 0 | 104 | | this.SequenceNumber = startingCheckpoint.SequenceNumber; |
| 0 | 105 | | ProcessorEventSource.Log.PartitionPumpInfo( |
| 0 | 106 | | this.host.HostName, |
| 0 | 107 | | this.PartitionId, $"Retrieved starting offset/sequenceNumber: {this.Offset}/{this.SequenceNumber}"); |
| 0 | 108 | | eventPosition = EventPosition.FromOffset(this.Offset); |
| | 109 | | } |
| | 110 | |
|
| 0 | 111 | | return eventPosition; |
| 0 | 112 | | } |
| | 113 | |
|
| | 114 | | /// <summary> |
| | 115 | | /// Writes the current offset and sequenceNumber to the checkpoint store via the checkpoint manager. |
| | 116 | | /// </summary> |
| | 117 | | public Task CheckpointAsync() |
| | 118 | | { |
| | 119 | | // Capture the current offset and sequenceNumber. Synchronize to be sure we get a matched pair |
| | 120 | | // instead of catching an update halfway through. Do the capturing here because by the time the checkpoint |
| | 121 | | // task runs, the fields in this object may have changed, but we should only write to store what the user |
| | 122 | | // has directed us to write. |
| | 123 | | Checkpoint capturedCheckpoint; |
| 0 | 124 | | lock(this.ThisLock) |
| | 125 | | { |
| | 126 | | // Don't checkpoint for start of stream so that we can still respect initial offset provider. |
| 0 | 127 | | if (this.Offset == "-1") |
| | 128 | | { |
| 0 | 129 | | return Task.CompletedTask; |
| | 130 | | } |
| | 131 | |
|
| 0 | 132 | | capturedCheckpoint = new Checkpoint(this.PartitionId, this.Offset, this.SequenceNumber); |
| 0 | 133 | | } |
| | 134 | |
|
| 0 | 135 | | return this.PersistCheckpointAsync(capturedCheckpoint); |
| 0 | 136 | | } |
| | 137 | |
|
| | 138 | | /// <summary> |
| | 139 | | /// Stores the offset and sequenceNumber from the provided received EventData instance, then writes those |
| | 140 | | /// values to the checkpoint store via the checkpoint manager. |
| | 141 | | /// </summary> |
| | 142 | | /// <param name="eventData">A received EventData with valid offset and sequenceNumber</param> |
| | 143 | | /// <exception cref="ArgumentNullException">If suplied eventData is null</exception> |
| | 144 | | /// <exception cref="ArgumentOutOfRangeException">If the sequenceNumber is less than the last checkpointed value |
| | 145 | | public Task CheckpointAsync(EventData eventData) |
| | 146 | | { |
| 0 | 147 | | Guard.ArgumentNotNull(nameof(eventData), eventData); |
| | 148 | |
|
| | 149 | | // We have never seen this sequence number yet |
| 0 | 150 | | if (eventData.SystemProperties.SequenceNumber > this.SequenceNumber) |
| | 151 | | { |
| 0 | 152 | | throw new ArgumentOutOfRangeException("eventData.SystemProperties.SequenceNumber"); |
| | 153 | | } |
| | 154 | |
|
| 0 | 155 | | return this.PersistCheckpointAsync(new Checkpoint(this.PartitionId, eventData.SystemProperties.Offset, event |
| | 156 | | } |
| | 157 | |
|
| | 158 | | /// <summary> |
| | 159 | | /// Writes the current offset and sequenceNumber to the checkpoint store via the checkpoint manager. |
| | 160 | | /// </summary> |
| | 161 | | public Task CheckpointAsync(Checkpoint checkPoint) |
| | 162 | | { |
| 0 | 163 | | return this.PersistCheckpointAsync(checkPoint); |
| | 164 | | } |
| | 165 | |
|
| | 166 | | /// <summary> |
| | 167 | | /// Provides the parition context in the following format:"PartitionContext({EventHubPath}/{ConsumerGroupName}/{Part |
| | 168 | | /// </summary> |
| | 169 | | /// <returns></returns> |
| | 170 | | public override string ToString() |
| | 171 | | { |
| 0 | 172 | | return $"PartitionContext({this.EventHubPath}/{this.ConsumerGroupName}/{this.PartitionId}/{this.SequenceNumb |
| | 173 | | } |
| | 174 | |
|
| | 175 | | async Task PersistCheckpointAsync(Checkpoint checkpoint) |
| | 176 | | { |
| 0 | 177 | | ProcessorEventSource.Log.PartitionPumpCheckpointStart(this.host.HostName, checkpoint.PartitionId, checkpoint |
| | 178 | | try |
| | 179 | | { |
| 0 | 180 | | Checkpoint inStoreCheckpoint = await this.host.CheckpointManager.GetCheckpointAsync(checkpoint.Partition |
| 0 | 181 | | if (inStoreCheckpoint == null || checkpoint.SequenceNumber >= inStoreCheckpoint.SequenceNumber) |
| | 182 | | { |
| 0 | 183 | | if (inStoreCheckpoint == null) |
| | 184 | | { |
| 0 | 185 | | await this.host.CheckpointManager.CreateCheckpointIfNotExistsAsync(checkpoint.PartitionId).Confi |
| | 186 | | } |
| | 187 | |
|
| 0 | 188 | | await this.host.CheckpointManager.UpdateCheckpointAsync(this.Lease, checkpoint).ConfigureAwait(false |
| | 189 | |
|
| | 190 | | // Update internal lease if update call above is successful. |
| 0 | 191 | | this.Lease.Offset = checkpoint.Offset; |
| 0 | 192 | | this.Lease.SequenceNumber = checkpoint.SequenceNumber; |
| | 193 | | } |
| | 194 | | else |
| | 195 | | { |
| 0 | 196 | | string msg = $"Ignoring out of date checkpoint with offset {checkpoint.Offset}/sequence number {chec |
| 0 | 197 | | $" because current persisted checkpoint has higher offset {inStoreCheckpoint.Offset}/sequenc |
| 0 | 198 | | ProcessorEventSource.Log.PartitionPumpError(this.host.HostName, checkpoint.PartitionId, msg); |
| 0 | 199 | | throw new ArgumentOutOfRangeException("offset/sequenceNumber", msg); |
| | 200 | | } |
| 0 | 201 | | } |
| 0 | 202 | | catch (Exception e) |
| | 203 | | { |
| 0 | 204 | | ProcessorEventSource.Log.PartitionPumpCheckpointError(this.host.HostName, checkpoint.PartitionId, e.ToSt |
| 0 | 205 | | throw; |
| | 206 | | } |
| | 207 | | finally |
| | 208 | | { |
| 0 | 209 | | ProcessorEventSource.Log.PartitionPumpCheckpointStop(this.host.HostName, checkpoint.PartitionId); |
| | 210 | | } |
| 0 | 211 | | } |
| | 212 | | } |
| | 213 | | } |