| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.ComponentModel; |
| | 6 | | using Azure.Core; |
| | 7 | | using Azure.Messaging.EventHubs.Consumer; |
| | 8 | | using Azure.Messaging.EventHubs.Core; |
| | 9 | | using Azure.Messaging.EventHubs.Processor; |
| | 10 | |
|
| | 11 | | namespace Azure.Messaging.EventHubs.Primitives |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// The set of options that can be specified when creating an <see cref="EventProcessor{TPartition}" /> |
| | 15 | | /// to configure its behavior. |
| | 16 | | /// </summary> |
| | 17 | | /// |
| | 18 | | public class EventProcessorOptions |
| | 19 | | { |
| | 20 | | /// <summary>The set of options to use for configuring the connection to the Event Hubs service.</summary> |
| 396 | 21 | | private EventHubConnectionOptions _connectionOptions = new EventHubConnectionOptions(); |
| | 22 | |
|
| | 23 | | /// <summary>The set of options to govern retry behavior and try timeouts.</summary> |
| 396 | 24 | | private EventHubsRetryOptions _retryOptions = new EventHubsRetryOptions(); |
| | 25 | |
|
| | 26 | | /// <summary>The maximum amount of time to wait for a batch of events to become available before emitting an emp |
| 396 | 27 | | private TimeSpan? _maximumWaitTime = TimeSpan.FromSeconds(60); |
| | 28 | |
|
| | 29 | | /// <summary>The prefetch count to use for the event processor.</summary> |
| 396 | 30 | | private int _prefetchCount = 300; |
| | 31 | |
|
| | 32 | | /// <summary>The desired amount of time to allow between load balancing verification attempts.</summary> |
| 396 | 33 | | private TimeSpan _loadBalancingUpdateInterval = TimeSpan.FromSeconds(10); |
| | 34 | |
|
| | 35 | | /// <summary>The desired amount of time to consider a partition owned by a specific event processor.</summary> |
| 396 | 36 | | private TimeSpan _partitionOwnershipExpirationInterval = TimeSpan.FromSeconds(30); |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// The options used for configuring the connection to the Event Hubs service. |
| | 40 | | /// </summary> |
| | 41 | | /// |
| | 42 | | public EventHubConnectionOptions ConnectionOptions |
| | 43 | | { |
| 130 | 44 | | get => _connectionOptions; |
| | 45 | |
|
| | 46 | | set |
| | 47 | | { |
| 8 | 48 | | Argument.AssertNotNull(value, nameof(ConnectionOptions)); |
| 6 | 49 | | _connectionOptions = value; |
| 6 | 50 | | } |
| | 51 | | } |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// The set of options to use for determining whether a failed operation should be retried and, |
| | 55 | | /// if so, the amount of time to wait between retry attempts. These options also control the |
| | 56 | | /// amount of time allowed for receiving event batches and other interactions with the Event Hubs service. |
| | 57 | | /// </summary> |
| | 58 | | /// |
| | 59 | | public EventHubsRetryOptions RetryOptions |
| | 60 | | { |
| 324 | 61 | | get => _retryOptions; |
| | 62 | |
|
| | 63 | | set |
| | 64 | | { |
| 38 | 65 | | Argument.AssertNotNull(value, nameof(RetryOptions)); |
| 36 | 66 | | _retryOptions = value; |
| 36 | 67 | | } |
| | 68 | | } |
| | 69 | |
|
| | 70 | | /// <summary> |
| | 71 | | /// The maximum amount of time to wait for an event to become available for a given partition before emitting |
| | 72 | | /// an empty batch of events. |
| | 73 | | /// </summary> |
| | 74 | | /// |
| | 75 | | /// <value> |
| | 76 | | /// If <c>null</c>, the processor will wait indefinitely for a batch of events to become available and will no |
| | 77 | | /// dispatch them to be processed while waiting; otherwise, a batch will always be emitted within this interva |
| | 78 | | /// it is empty. |
| | 79 | | /// </value> |
| | 80 | | /// |
| | 81 | | public TimeSpan? MaximumWaitTime |
| | 82 | | { |
| 6088388 | 83 | | get => _maximumWaitTime; |
| | 84 | |
|
| | 85 | | set |
| | 86 | | { |
| 10 | 87 | | if (value.HasValue) |
| | 88 | | { |
| 8 | 89 | | Argument.AssertNotNegative(value.Value, nameof(MaximumWaitTime)); |
| | 90 | | } |
| | 91 | |
|
| 4 | 92 | | _maximumWaitTime = value; |
| 4 | 93 | | } |
| | 94 | | } |
| | 95 | |
|
| | 96 | | /// <summary> |
| | 97 | | /// The number of events that will be eagerly requested from the Event Hubs service and staged locally without |
| | 98 | | /// whether the processor is currently active, intended to help maximize throughput by buffering service opera |
| | 99 | | /// readers needing to wait for service operations to complete. |
| | 100 | | /// </summary> |
| | 101 | | /// |
| | 102 | | /// <value> |
| | 103 | | /// The <see cref="PrefetchCount" /> is a control that developers can use to help tune performance for the spe |
| | 104 | | /// needs of an application, given its expected size of events, throughput needs, and expected scenarios for u |
| | 105 | | /// Event Hubs. |
| | 106 | | /// </value> |
| | 107 | | /// |
| | 108 | | /// <remarks> |
| | 109 | | /// The size of the prefetch count has an influence on the efficiency of reading events from the Event Hubs se |
| | 110 | | /// larger the size of the cache, the more efficiently service operations can be buffered in the background to |
| | 111 | | /// improve throughput. This comes at the cost of additional memory use and potentially increases network I/O |
| | 112 | | /// |
| | 113 | | /// For scenarios where the size of events is small and many events are flowing through the system, requesting |
| | 114 | | /// events in a batch and using a higher <see cref="PrefetchCount" /> may help improve throughput. For scenar |
| | 115 | | /// the size of events is larger or when processing of events is expected to be a heavier and slower operation |
| | 116 | | /// fewer events in a batch and using a smaller <see cref="PrefetchCount"/> may help manage resource use witho |
| | 117 | | /// incurring a non-trivial cost to throughput. |
| | 118 | | /// |
| | 119 | | /// Regardless of the values, it is generally recommended that the <see cref="PrefetchCount" /> be at least 2- |
| | 120 | | /// times as large as the number of events in a batch to allow for efficient buffering of service operations. |
| | 121 | | /// </remarks> |
| | 122 | | /// |
| | 123 | | public int PrefetchCount |
| | 124 | | { |
| 112 | 125 | | get => _prefetchCount; |
| | 126 | |
|
| | 127 | | set |
| | 128 | | { |
| 12 | 129 | | Argument.AssertAtLeast(value, 0, nameof(PrefetchCount)); |
| 6 | 130 | | _prefetchCount = value; |
| 6 | 131 | | } |
| | 132 | | } |
| | 133 | |
|
| | 134 | | /// <summary> |
| | 135 | | /// The desired amount of time to allow between load balancing verification attempts. |
| | 136 | | /// </summary> |
| | 137 | | /// |
| | 138 | | /// <remarks> |
| | 139 | | /// Because load balancing holds less priority than processing events, this interval |
| | 140 | | /// should be considered the minimum time that will elapse between verification attempts; operations |
| | 141 | | /// with higher priority may cause a minor delay longer than this interval for load balancing. |
| | 142 | | /// </remarks> |
| | 143 | | /// |
| | 144 | | public TimeSpan LoadBalancingUpdateInterval |
| | 145 | | { |
| 106 | 146 | | get => _loadBalancingUpdateInterval; |
| | 147 | |
|
| | 148 | | set |
| | 149 | | { |
| 48 | 150 | | Argument.AssertNotNegative(value, nameof(LoadBalancingUpdateInterval)); |
| 42 | 151 | | _loadBalancingUpdateInterval = value; |
| 42 | 152 | | } |
| | 153 | | } |
| | 154 | |
|
| | 155 | | /// <summary> |
| | 156 | | /// The desired amount of time to consider a partition owned by a specific event processor |
| | 157 | | /// instance before the ownership is considered stale and the partition eligible to be requested |
| | 158 | | /// by another event processor that wishes to assume responsibility for processing it. |
| | 159 | | /// </summary> |
| | 160 | | /// |
| | 161 | | public TimeSpan PartitionOwnershipExpirationInterval |
| | 162 | | { |
| 268 | 163 | | get => _partitionOwnershipExpirationInterval; |
| | 164 | |
|
| | 165 | | set |
| | 166 | | { |
| 8 | 167 | | Argument.AssertNotNegative(value, nameof(PartitionOwnershipExpirationInterval)); |
| 2 | 168 | | _partitionOwnershipExpirationInterval = value; |
| 2 | 169 | | } |
| | 170 | | } |
| | 171 | |
|
| | 172 | | /// <summary> |
| | 173 | | /// A unique name used to identify the event processor. If <c>null</c> or empty, a GUID will be used as the |
| | 174 | | /// identifier. |
| | 175 | | /// </summary> |
| | 176 | | /// |
| 442 | 177 | | public string Identifier { get; set; } |
| | 178 | |
|
| | 179 | | /// <summary> |
| | 180 | | /// Indicates whether or not the processor should request information on the last enqueued event on the partit |
| | 181 | | /// associated with a given event, and track that information as events are received. |
| | 182 | | /// </summary> |
| | 183 | | /// |
| | 184 | | /// <value><c>true</c> if information about a partition's last event should be requested and tracked; otherwise, |
| | 185 | | /// |
| | 186 | | /// <remarks> |
| | 187 | | /// When information about a partition's last enqueued event is being tracked, each event received from the Ev |
| | 188 | | /// service will carry metadata about the partition that it otherwise would not. This results in a small amoun |
| | 189 | | /// additional network bandwidth consumption that is generally a favorable trade-off when considered |
| | 190 | | /// against periodically making requests for partition properties using one of the Event Hub clients. |
| | 191 | | /// </remarks> |
| | 192 | | /// |
| 662 | 193 | | public bool TrackLastEnqueuedEventProperties { get; set; } = true; |
| | 194 | |
|
| | 195 | | /// <summary> |
| | 196 | | /// The position within a partition where the event processor should |
| | 197 | | /// begin reading events when no checkpoint can be found. |
| | 198 | | /// </summary> |
| | 199 | | /// |
| | 200 | | /// <remarks> |
| | 201 | | /// In the event that a custom starting point is desired for a single partition, or each partition should star |
| | 202 | | /// it is recommended that those values be returned by the <see cref="EventProcessor{TPartition}.ListCheckpoin |
| | 203 | | /// if they were previously saved checkpoints. |
| | 204 | | /// </remarks> |
| | 205 | | /// |
| | 206 | | /// <seealso cref="EventProcessor{TPartition}.ListCheckpointsAsync"/> |
| | 207 | | /// |
| 704 | 208 | | public EventPosition DefaultStartingPosition { get; set; } = EventPosition.Earliest; |
| | 209 | |
|
| | 210 | | /// <summary> |
| | 211 | | /// The strategy that an event processor will use to make decisions about |
| | 212 | | /// partition ownership when performing load balancing to share work with |
| | 213 | | /// other event processors. |
| | 214 | | /// </summary> |
| | 215 | | /// |
| | 216 | | /// <seealso cref="Processor.LoadBalancingStrategy" /> |
| | 217 | | /// |
| 322 | 218 | | public LoadBalancingStrategy LoadBalancingStrategy { get; set; } = LoadBalancingStrategy.Balanced; |
| | 219 | |
|
| | 220 | | /// <summary> |
| | 221 | | /// Determines whether the specified <see cref="System.Object" /> is equal to this instance. |
| | 222 | | /// </summary> |
| | 223 | | /// |
| | 224 | | /// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param> |
| | 225 | | /// |
| | 226 | | /// <returns><c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c> |
| | 227 | | /// |
| | 228 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| 0 | 229 | | public override bool Equals(object obj) => base.Equals(obj); |
| | 230 | |
|
| | 231 | | /// <summary> |
| | 232 | | /// Returns a hash code for this instance. |
| | 233 | | /// </summary> |
| | 234 | | /// |
| | 235 | | /// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a ha |
| | 236 | | /// |
| | 237 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| 0 | 238 | | public override int GetHashCode() => base.GetHashCode(); |
| | 239 | |
|
| | 240 | | /// <summary> |
| | 241 | | /// Converts the instance to string representation. |
| | 242 | | /// </summary> |
| | 243 | | /// |
| | 244 | | /// <returns>A <see cref="System.String" /> that represents this instance.</returns> |
| | 245 | | /// |
| | 246 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| 0 | 247 | | public override string ToString() => base.ToString(); |
| | 248 | |
|
| | 249 | | /// <summary> |
| | 250 | | /// Creates a new copy of the current <see cref="EventProcessorOptions" />, cloning its attributes into a new |
| | 251 | | /// </summary> |
| | 252 | | /// |
| | 253 | | /// <returns>A new copy of <see cref="EventProcessorOptions" />.</returns> |
| | 254 | | /// |
| | 255 | | internal EventProcessorOptions Clone() => |
| 102 | 256 | | new EventProcessorOptions |
| 102 | 257 | | { |
| 102 | 258 | | _connectionOptions = ConnectionOptions.Clone(), |
| 102 | 259 | | _retryOptions = RetryOptions.Clone(), |
| 102 | 260 | | _prefetchCount = PrefetchCount, |
| 102 | 261 | | _maximumWaitTime = MaximumWaitTime, |
| 102 | 262 | | _loadBalancingUpdateInterval = LoadBalancingUpdateInterval, |
| 102 | 263 | | _partitionOwnershipExpirationInterval = PartitionOwnershipExpirationInterval, |
| 102 | 264 | | Identifier = Identifier, |
| 102 | 265 | | TrackLastEnqueuedEventProperties = TrackLastEnqueuedEventProperties, |
| 102 | 266 | | DefaultStartingPosition = DefaultStartingPosition, |
| 102 | 267 | | LoadBalancingStrategy = LoadBalancingStrategy |
| 102 | 268 | | }; |
| | 269 | | } |
| | 270 | | } |