Skip to content

Commit

Permalink
Fix Histogram synchronization issue (#3534)
Browse files Browse the repository at this point in the history
  • Loading branch information
utpilla authored Aug 23, 2022
1 parent ad55478 commit 968dc52
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 24 deletions.
2 changes: 0 additions & 2 deletions src/OpenTelemetry/Metrics/HistogramBuckets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ internal HistogramBuckets(double[] explicitBounds)
this.SnapshotBucketCounts = explicitBounds != null ? new long[explicitBounds.Length + 1] : new long[0];
}

internal object LockObject => this.SnapshotBucketCounts;

public Enumerator GetEnumerator() => new(this);

internal HistogramBuckets Copy()
Expand Down
70 changes: 48 additions & 22 deletions src/OpenTelemetry/Metrics/MetricPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -341,14 +341,16 @@ internal void Update(double number)
{
if (Interlocked.Exchange(ref this.histogramBuckets.IsCriticalSectionOccupied, 1) == 0)
{
// Lock acquired
unchecked
{
this.runningValue.AsLong++;
this.histogramBuckets.RunningSum += number;
this.histogramBuckets.RunningBucketCounts[i]++;
}

this.histogramBuckets.IsCriticalSectionOccupied = 0;
// Release lock
Interlocked.Exchange(ref this.histogramBuckets.IsCriticalSectionOccupied, 0);
break;
}

Expand All @@ -365,13 +367,15 @@ internal void Update(double number)
{
if (Interlocked.Exchange(ref this.histogramBuckets.IsCriticalSectionOccupied, 1) == 0)
{
// Lock acquired
unchecked
{
this.runningValue.AsLong++;
this.histogramBuckets.RunningSum += number;
}

this.histogramBuckets.IsCriticalSectionOccupied = 0;
// Release lock
Interlocked.Exchange(ref this.histogramBuckets.IsCriticalSectionOccupied, 0);
break;
}

Expand Down Expand Up @@ -497,44 +501,66 @@ internal void TakeSnapshot(bool outputDelta)

case AggregationType.Histogram:
{
lock (this.histogramBuckets.LockObject)
var sw = default(SpinWait);
while (true)
{
this.snapshotValue.AsLong = this.runningValue.AsLong;
this.histogramBuckets.SnapshotSum = this.histogramBuckets.RunningSum;
if (outputDelta)
{
this.runningValue.AsLong = 0;
this.histogramBuckets.RunningSum = 0;
}

for (int i = 0; i < this.histogramBuckets.RunningBucketCounts.Length; i++)
if (Interlocked.Exchange(ref this.histogramBuckets.IsCriticalSectionOccupied, 1) == 0)
{
this.histogramBuckets.SnapshotBucketCounts[i] = this.histogramBuckets.RunningBucketCounts[i];
// Lock acquired
this.snapshotValue.AsLong = this.runningValue.AsLong;
this.histogramBuckets.SnapshotSum = this.histogramBuckets.RunningSum;
if (outputDelta)
{
this.histogramBuckets.RunningBucketCounts[i] = 0;
this.runningValue.AsLong = 0;
this.histogramBuckets.RunningSum = 0;
}

for (int i = 0; i < this.histogramBuckets.RunningBucketCounts.Length; i++)
{
this.histogramBuckets.SnapshotBucketCounts[i] = this.histogramBuckets.RunningBucketCounts[i];
if (outputDelta)
{
this.histogramBuckets.RunningBucketCounts[i] = 0;
}
}

this.MetricPointStatus = MetricPointStatus.NoCollectPending;

// Release lock
Interlocked.Exchange(ref this.histogramBuckets.IsCriticalSectionOccupied, 0);
break;
}

this.MetricPointStatus = MetricPointStatus.NoCollectPending;
sw.SpinOnce();
}

break;
}

case AggregationType.HistogramSumCount:
{
lock (this.histogramBuckets.LockObject)
var sw = default(SpinWait);
while (true)
{
this.snapshotValue.AsLong = this.runningValue.AsLong;
this.histogramBuckets.SnapshotSum = this.histogramBuckets.RunningSum;
if (outputDelta)
if (Interlocked.Exchange(ref this.histogramBuckets.IsCriticalSectionOccupied, 1) == 0)
{
this.runningValue.AsLong = 0;
this.histogramBuckets.RunningSum = 0;
// Lock acquired
this.snapshotValue.AsLong = this.runningValue.AsLong;
this.histogramBuckets.SnapshotSum = this.histogramBuckets.RunningSum;
if (outputDelta)
{
this.runningValue.AsLong = 0;
this.histogramBuckets.RunningSum = 0;
}

this.MetricPointStatus = MetricPointStatus.NoCollectPending;

// Release lock
Interlocked.Exchange(ref this.histogramBuckets.IsCriticalSectionOccupied, 0);
break;
}

this.MetricPointStatus = MetricPointStatus.NoCollectPending;
sw.SpinOnce();
}

break;
Expand Down

0 comments on commit 968dc52

Please sign in to comment.