| | 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 | |
|
| | 8 | | namespace Azure.Messaging.EventHubs.Consumer |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// The set of options that can be specified to configure behavior when reading events from an |
| | 12 | | /// <see cref="EventHubConsumerClient" />. |
| | 13 | | /// </summary> |
| | 14 | | /// |
| | 15 | | public class ReadEventOptions |
| | 16 | | { |
| | 17 | | /// <summary>The maximum amount of time to wait to for an event to be available before emitting an empty item; i |
| | 18 | | private TimeSpan? _maximumWaitTime = null; |
| | 19 | |
|
| | 20 | | /// <summary>The event catch count to use when reading events.</summary> |
| 130 | 21 | | private int _cacheEventCount = 100; |
| | 22 | |
|
| | 23 | | /// <summary>The prefetch count to use when reading events.</summary> |
| 130 | 24 | | private int _prefetchCount = 300; |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// When populated, the owner level indicates that a reading is intended to be performed exclusively for event |
| | 28 | | /// requested partition and for the associated consumer group. To do so, reading will attempt to assert owner |
| | 29 | | /// over the partition; in the case where more than one exclusive reader attempts to assert ownership for the |
| | 30 | | /// partition/consumer group pair, the one having a larger <see cref="OwnerLevel"/> value will "win." |
| | 31 | | /// |
| | 32 | | /// When an exclusive reader is used, other readers which are non-exclusive or which have a lower owner level |
| | 33 | | /// not be allowed to be created, if they already exist, will encounter an exception during the next attempted |
| | 34 | | /// </summary> |
| | 35 | | /// |
| | 36 | | /// <value>The relative priority to associate with an exclusive reader; for a non-exclusive reader, this value s |
| | 37 | | /// |
| | 38 | | /// <remarks> |
| | 39 | | /// An <see cref="EventHubsException"/> will occur if an <see cref="EventHubConsumerClient"/> is unable to rea |
| | 40 | | /// requested Event Hub partition for the given consumer group. In this case, the <see cref="EventHubsExcepti |
| | 41 | | /// will be set to <see cref="EventHubsException.FailureReason.ConsumerDisconnected"/>. |
| | 42 | | /// </remarks> |
| | 43 | | /// |
| | 44 | | /// <seealso cref="EventHubsException"/> |
| | 45 | | /// <seealso cref="EventHubsException.FailureReason.ConsumerDisconnected"/> |
| | 46 | | /// |
| 196 | 47 | | public long? OwnerLevel { get; set; } |
| | 48 | |
|
| | 49 | | /// <summary> |
| | 50 | | /// Indicates whether or not the reader should request information on the last enqueued event on the partition |
| | 51 | | /// associated with a given event, and track that information as events are read. |
| | 52 | | /// </summary> |
| | 53 | | /// |
| | 54 | | /// <value><c>true</c> if information about a partition's last event should be requested and tracked; otherwise, |
| | 55 | | /// |
| | 56 | | /// <remarks> |
| | 57 | | /// When information about a partition's last enqueued event is being tracked, each event received from the Ev |
| | 58 | | /// service will carry metadata about the partition that it otherwise would not. This results in a small amoun |
| | 59 | | /// additional network bandwidth consumption that is generally a favorable trade-off when considered |
| | 60 | | /// against periodically making requests for partition properties using one of the Event Hub clients. |
| | 61 | | /// </remarks> |
| | 62 | | /// |
| 326 | 63 | | public bool TrackLastEnqueuedEventProperties { get; set; } = true; |
| | 64 | |
|
| | 65 | | /// <summary> |
| | 66 | | /// The maximum amount of time to wait to for an event to be available when reading before reading an empty |
| | 67 | | /// event. |
| | 68 | | /// </summary> |
| | 69 | | /// |
| | 70 | | /// <value> |
| | 71 | | /// If specified, should there be no events available before this waiting period expires, an empty event will |
| | 72 | | /// allowing control to return to the reader that was waiting. |
| | 73 | | /// |
| | 74 | | /// If <c>null</c>, the reader will wait forever for items to be available unless reading is canceled. Empty i |
| | 75 | | /// not be returned. |
| | 76 | | /// </value> |
| | 77 | | /// |
| | 78 | | public TimeSpan? MaximumWaitTime |
| | 79 | | { |
| 128 | 80 | | get => _maximumWaitTime; |
| | 81 | |
|
| | 82 | | set |
| | 83 | | { |
| 30 | 84 | | if (value.HasValue) |
| | 85 | | { |
| 28 | 86 | | Argument.AssertNotNegative(value.Value, nameof(MaximumWaitTime)); |
| | 87 | | } |
| | 88 | |
|
| 28 | 89 | | _maximumWaitTime = value; |
| 28 | 90 | | } |
| | 91 | | } |
| | 92 | |
|
| | 93 | | /// <summary> |
| | 94 | | /// The maximum number of events that will be read from the Event Hubs service and held in a local memory |
| | 95 | | /// cache when reading is active and events are being emitted to an enumerator for processing. |
| | 96 | | /// </summary> |
| | 97 | | /// |
| | 98 | | /// <value> |
| | 99 | | /// The <see cref="CacheEventCount" /> is a control that developers can use to help tune performance for the s |
| | 100 | | /// needs of an application, given its expected size of events, throughput needs, and expected scenarios for u |
| | 101 | | /// Event Hubs. |
| | 102 | | /// </value> |
| | 103 | | /// |
| | 104 | | /// <remarks> |
| | 105 | | /// The size of this cache has an influence on the efficiency of reading events from the Event Hubs service. |
| | 106 | | /// larger the size of the cache, the more efficiently service operations can be buffered in the background to |
| | 107 | | /// improve throughput. This comes at the cost of additional memory use and potentially increases network I/O |
| | 108 | | /// |
| | 109 | | /// For scenarios where the size of events is small and many events are flowing through the system, using a la |
| | 110 | | /// <see cref="CacheEventCount"/> and <see cref="PrefetchCount" /> may help improve throughput. For scenarios |
| | 111 | | /// the size of events is larger or when processing of events is expected to be a heavier and slower operation |
| | 112 | | /// a smaller size <see cref="CacheEventCount"/> and <see cref="PrefetchCount"/> may help manage resource use |
| | 113 | | /// incurring a non-trivial cost to throughput. |
| | 114 | | /// |
| | 115 | | /// Regardless of the values, it is generally recommended that the <see cref="PrefetchCount" /> be at least 2- |
| | 116 | | /// times as large as the <see cref="CacheEventCount" /> to allow for efficient buffering of service operation |
| | 117 | | /// </remarks> |
| | 118 | | /// |
| | 119 | | public int CacheEventCount |
| | 120 | | { |
| 228 | 121 | | get => _cacheEventCount; |
| | 122 | |
|
| | 123 | | set |
| | 124 | | { |
| 12 | 125 | | Argument.AssertAtLeast(value, 1, nameof(CacheEventCount)); |
| 10 | 126 | | _cacheEventCount = value; |
| 10 | 127 | | } |
| | 128 | | } |
| | 129 | |
|
| | 130 | | /// <summary> |
| | 131 | | /// The number of events that will be eagerly requested from the Event Hubs service and staged locally without |
| | 132 | | /// whether a reader is currently active, intended to help maximize throughput by buffering service operations |
| | 133 | | /// readers needing to wait for service operations to complete. |
| | 134 | | /// </summary> |
| | 135 | | /// |
| | 136 | | /// <value> |
| | 137 | | /// The <see cref="PrefetchCount" /> is a control that developers can use to help tune performance for the spe |
| | 138 | | /// needs of an application, given its expected size of events, throughput needs, and expected scenarios for u |
| | 139 | | /// Event Hubs. |
| | 140 | | /// </value> |
| | 141 | | /// |
| | 142 | | /// <remarks> |
| | 143 | | /// The size of the prefetch count has an influence on the efficiency of reading events from the Event Hubs se |
| | 144 | | /// larger the size of the cache, the more efficiently service operations can be buffered in the background to |
| | 145 | | /// improve throughput. This comes at the cost of additional memory use and potentially increases network I/O |
| | 146 | | /// |
| | 147 | | /// For scenarios where the size of events is small and many events are flowing through the system, using a la |
| | 148 | | /// <see cref="CacheEventCount"/> and <see cref="PrefetchCount" /> may help improve throughput. For scenarios |
| | 149 | | /// the size of events is larger or when processing of events is expected to be a heavier and slower operation |
| | 150 | | /// a smaller size <see cref="CacheEventCount"/> and <see cref="PrefetchCount"/> may help manage resource use |
| | 151 | | /// incurring a non-trivial cost to throughput. |
| | 152 | | /// |
| | 153 | | /// Regardless of the values, it is generally recommended that the <see cref="PrefetchCount" /> be at least 2- |
| | 154 | | /// times as large as the <see cref="CacheEventCount" /> to allow for efficient buffering of service operation |
| | 155 | | /// </remarks> |
| | 156 | | /// |
| | 157 | | public int PrefetchCount |
| | 158 | | { |
| 150 | 159 | | get => _prefetchCount; |
| | 160 | |
|
| | 161 | | set |
| | 162 | | { |
| 10 | 163 | | Argument.AssertAtLeast(value, 0, nameof(PrefetchCount)); |
| 8 | 164 | | _prefetchCount = value; |
| 8 | 165 | | } |
| | 166 | | } |
| | 167 | |
|
| | 168 | | /// <summary> |
| | 169 | | /// Determines whether the specified <see cref="System.Object" /> is equal to this instance. |
| | 170 | | /// </summary> |
| | 171 | | /// |
| | 172 | | /// <param name="obj">The <see cref="System.Object" /> to compare with this instance.</param> |
| | 173 | | /// |
| | 174 | | /// <returns><c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c> |
| | 175 | | /// |
| | 176 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| 0 | 177 | | public override bool Equals(object obj) => base.Equals(obj); |
| | 178 | |
|
| | 179 | | /// <summary> |
| | 180 | | /// Returns a hash code for this instance. |
| | 181 | | /// </summary> |
| | 182 | | /// |
| | 183 | | /// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a ha |
| | 184 | | /// |
| | 185 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| 0 | 186 | | public override int GetHashCode() => base.GetHashCode(); |
| | 187 | |
|
| | 188 | | /// <summary> |
| | 189 | | /// Converts the instance to string representation. |
| | 190 | | /// </summary> |
| | 191 | | /// |
| | 192 | | /// <returns>A <see cref="System.String" /> that represents this instance.</returns> |
| | 193 | | /// |
| | 194 | | [EditorBrowsable(EditorBrowsableState.Never)] |
| 0 | 195 | | public override string ToString() => base.ToString(); |
| | 196 | |
|
| | 197 | | /// <summary> |
| | 198 | | /// Creates a new copy of the current <see cref="ReadEventOptions" />, cloning its attributes into a new insta |
| | 199 | | /// </summary> |
| | 200 | | /// |
| | 201 | | /// <returns>A new copy of <see cref="ReadEventOptions" />.</returns> |
| | 202 | | /// |
| | 203 | | internal ReadEventOptions Clone() => |
| 24 | 204 | | new ReadEventOptions |
| 24 | 205 | | { |
| 24 | 206 | | OwnerLevel = OwnerLevel, |
| 24 | 207 | | TrackLastEnqueuedEventProperties = TrackLastEnqueuedEventProperties, |
| 24 | 208 | | _maximumWaitTime = _maximumWaitTime, |
| 24 | 209 | | _cacheEventCount = _cacheEventCount, |
| 24 | 210 | | _prefetchCount = _prefetchCount, |
| 24 | 211 | | }; |
| | 212 | | } |
| | 213 | | } |