| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Diagnostics.Tracing; |
| | 6 | | using Azure.Core.Diagnostics; |
| | 7 | |
|
| | 8 | | namespace Azure.Messaging.EventHubs.Diagnostics |
| | 9 | | { |
| | 10 | | /// <summary> |
| | 11 | | /// Serves as an ETW event source for logging of information about Partition Load Balancer. |
| | 12 | | /// </summary> |
| | 13 | | /// |
| | 14 | | /// <remarks> |
| | 15 | | /// When defining Start/Stop tasks, it is highly recommended that the |
| | 16 | | /// the StopEvent.Id must be exactly StartEvent.Id + 1. |
| | 17 | | /// </remarks> |
| | 18 | | /// |
| | 19 | | [EventSource(Name = EventSourceName)] |
| | 20 | | internal class PartitionLoadBalancerEventSource : EventSource |
| | 21 | | { |
| | 22 | | /// <summary>The name to use for the event source.</summary> |
| | 23 | | private const string EventSourceName = "Azure-Messaging-EventHubs-Processor-PartitionLoadBalancer"; |
| | 24 | |
|
| | 25 | | /// <summary> |
| | 26 | | /// Provides a singleton instance of the event source for callers to |
| | 27 | | /// use for logging. |
| | 28 | | /// </summary> |
| | 29 | | /// |
| 214 | 30 | | public static PartitionLoadBalancerEventSource Log { get; } = new PartitionLoadBalancerEventSource(); |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// Prevents an instance of the <see cref="PartitionLoadBalancerEventSource"/> class from being created |
| | 34 | | /// outside the scope of this library. |
| | 35 | | /// </summary> |
| | 36 | | /// |
| 2 | 37 | | internal PartitionLoadBalancerEventSource() : base(EventSourceName, EventSourceSettings.Default, AzureEventSourc |
| | 38 | | { |
| 2 | 39 | | } |
| | 40 | |
|
| | 41 | | /// <summary> |
| | 42 | | /// Indicates the minimum amount of partitions every event processor needs to own when the distribution is bal |
| | 43 | | /// </summary> |
| | 44 | | /// |
| | 45 | | /// <param name="count"> Minimum partitions per event processor.</param> |
| | 46 | | /// |
| | 47 | | [Event(1, Level = EventLevel.Verbose, Message = "Expected minimum partitions per event processor '{0}'.")] |
| | 48 | | public virtual void MinimumPartitionsPerEventProcessor(int count) |
| | 49 | | { |
| 0 | 50 | | if (IsEnabled()) |
| | 51 | | { |
| 0 | 52 | | WriteEvent(1, count); |
| | 53 | | } |
| 0 | 54 | | } |
| | 55 | |
|
| | 56 | | /// <summary> |
| | 57 | | /// Indicates the current ownership count. |
| | 58 | | /// </summary> |
| | 59 | | /// |
| | 60 | | /// <param name="identifier">A unique name used to identify the associated event processor.</param> |
| | 61 | | /// <param name="count"> Current ownership count.</param> |
| | 62 | | /// |
| | 63 | | [Event(2, Level = EventLevel.Informational, Message = "Current ownership count is {0}. (Identifier: '{1}')")] |
| | 64 | | public virtual void CurrentOwnershipCount(int count, |
| | 65 | | string identifier) |
| | 66 | | { |
| 0 | 67 | | if (IsEnabled()) |
| | 68 | | { |
| 0 | 69 | | WriteEvent(2, count, identifier ?? string.Empty); |
| | 70 | | } |
| 0 | 71 | | } |
| | 72 | |
|
| | 73 | | /// <summary> |
| | 74 | | /// Indicates the list of unclaimed partitions. |
| | 75 | | /// </summary> |
| | 76 | | /// |
| | 77 | | /// <param name="unclaimedPartitions">List of unclaimed partitions.</param> |
| | 78 | | /// |
| | 79 | | [Event(3, Level = EventLevel.Informational, Message = "Unclaimed partitions: '{0}'.")] |
| | 80 | | public virtual void UnclaimedPartitions(HashSet<string> unclaimedPartitions) |
| | 81 | | { |
| 0 | 82 | | if (IsEnabled()) |
| | 83 | | { |
| 0 | 84 | | WriteEvent(3, string.Join(", ", unclaimedPartitions)); |
| | 85 | | } |
| 0 | 86 | | } |
| | 87 | |
|
| | 88 | | /// <summary> |
| | 89 | | /// Indicates that an attempt to claim ownership for a specific partition has started. |
| | 90 | | /// </summary> |
| | 91 | | /// |
| | 92 | | /// <param name="partitionId">The identifier of the Event Hub partition whose ownership claim attempt is startin |
| | 93 | | /// |
| | 94 | | [Event(4, Level = EventLevel.Informational, Message = "Attempting to claim ownership of partition '{0}'.")] |
| | 95 | | public virtual void ClaimOwnershipStart(string partitionId) |
| | 96 | | { |
| 0 | 97 | | if (IsEnabled()) |
| | 98 | | { |
| 0 | 99 | | WriteEvent(4, partitionId ?? string.Empty); |
| | 100 | | } |
| 0 | 101 | | } |
| | 102 | |
|
| | 103 | | /// <summary> |
| | 104 | | /// Indicates that an exception was encountered while claiming ownership for a specific partition. |
| | 105 | | /// </summary> |
| | 106 | | /// |
| | 107 | | /// <param name="partitionId">The identifier of the Event Hub partition.</param> |
| | 108 | | /// <param name="errorMessage">The message for the exception that occurred.</param> |
| | 109 | | /// |
| | 110 | | [Event(5, Level = EventLevel.Error, Message = "Failed to claim ownership of partition '{0}'. (ErrorMessage: '{1} |
| | 111 | | public virtual void ClaimOwnershipError(string partitionId, |
| | 112 | | string errorMessage) |
| | 113 | | { |
| 0 | 114 | | if (IsEnabled()) |
| | 115 | | { |
| 0 | 116 | | WriteEvent(5, partitionId ?? string.Empty, errorMessage ?? string.Empty); |
| | 117 | | } |
| 0 | 118 | | } |
| | 119 | |
|
| | 120 | | /// <summary> |
| | 121 | | /// Indicates that the load is unbalanced and the associated event processor should own more partitions. |
| | 122 | | /// </summary> |
| | 123 | | /// |
| | 124 | | /// <param name="identifier">A unique name used to identify the associated event processor.</param> |
| | 125 | | /// |
| | 126 | | [Event(6, Level = EventLevel.Informational, Message = "Load is unbalanced and this load balancer should steal a |
| | 127 | | public virtual void ShouldStealPartition(string identifier) |
| | 128 | | { |
| 0 | 129 | | if (IsEnabled()) |
| | 130 | | { |
| 0 | 131 | | WriteEvent(6, identifier ?? string.Empty); |
| | 132 | | } |
| 0 | 133 | | } |
| | 134 | |
|
| | 135 | | /// <summary> |
| | 136 | | /// Indicates that stealable partitions were found, so randomly picking one of them to claim. |
| | 137 | | /// </summary> |
| | 138 | | /// |
| | 139 | | /// <param name="identifier">A unique name used to identify the associated event processor.</param> |
| | 140 | | /// |
| | 141 | | [Event(7, Level = EventLevel.Informational, Message = "No unclaimed partitions, stealing from another event proc |
| | 142 | | public virtual void StealPartition(string identifier) |
| | 143 | | { |
| 0 | 144 | | if (IsEnabled()) |
| | 145 | | { |
| 0 | 146 | | WriteEvent(7, identifier ?? string.Empty); |
| | 147 | | } |
| 0 | 148 | | } |
| | 149 | |
|
| | 150 | | /// <summary> |
| | 151 | | /// Indicates that an attempt to renew ownership has started, so they don't expire. |
| | 152 | | /// </summary> |
| | 153 | | /// |
| | 154 | | /// <param name="identifier">A unique name used to identify the associated event processor.</param> |
| | 155 | | /// |
| | 156 | | [Event(8, Level = EventLevel.Verbose, Message = "Attempting to renew ownership. (Identifier: '{0}')")] |
| | 157 | | public virtual void RenewOwnershipStart(string identifier) |
| | 158 | | { |
| 0 | 159 | | if (IsEnabled()) |
| | 160 | | { |
| 0 | 161 | | WriteEvent(8, identifier ?? string.Empty); |
| | 162 | | } |
| 0 | 163 | | } |
| | 164 | |
|
| | 165 | | /// <summary> |
| | 166 | | /// Indicates that an exception was encountered while renewing ownership. |
| | 167 | | /// </summary> |
| | 168 | | /// |
| | 169 | | /// <param name="identifier">A unique name used to identify the associated event processor.</param> |
| | 170 | | /// <param name="errorMessage">The message for the exception that occurred.</param> |
| | 171 | | /// |
| | 172 | | [Event(9, Level = EventLevel.Error, Message = "Failed to renew ownership. (Identifier: '{0}'; ErrorMessage: '{0} |
| | 173 | | public virtual void RenewOwnershipError(string identifier, |
| | 174 | | string errorMessage) |
| | 175 | | { |
| 0 | 176 | | if (IsEnabled()) |
| | 177 | | { |
| 0 | 178 | | WriteEvent(9, identifier ?? string.Empty, errorMessage ?? string.Empty); |
| | 179 | | } |
| 0 | 180 | | } |
| | 181 | |
|
| | 182 | | /// <summary> |
| | 183 | | /// Indicates that an attempt to renew ownership has completed, so they don't expire. |
| | 184 | | /// </summary> |
| | 185 | | /// |
| | 186 | | /// <param name="identifier">A unique name used to identify the associated event processor.</param> |
| | 187 | | /// |
| | 188 | | [Event(10, Level = EventLevel.Verbose, Message = "Attempt to renew ownership has completed. (Identifier: '{0}')" |
| | 189 | | public virtual void RenewOwnershipComplete(string identifier) |
| | 190 | | { |
| 0 | 191 | | if (IsEnabled()) |
| | 192 | | { |
| 0 | 193 | | WriteEvent(10, identifier ?? string.Empty); |
| | 194 | | } |
| 0 | 195 | | } |
| | 196 | | } |
| | 197 | | } |