Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added function for histograms merge #73

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 27 additions & 5 deletions histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ var bucketMultiplier = math.Pow(10, 1.0/bucketsPerDecimal)
// Zero histogram is usable.
type Histogram struct {
// Mu gurantees synchronous update for all the counters and sum.
mu sync.Mutex
mu sync.RWMutex

decimalBuckets [decimalBucketsCount]*[bucketsPerDecimal]uint64

Expand Down Expand Up @@ -109,14 +109,36 @@ func (h *Histogram) Update(v float64) {
h.mu.Unlock()
}

// Merge merges histograms
func (h *Histogram) Merge(b *Histogram) {
h.mu.Lock()
defer h.mu.Unlock()

b.mu.RLock()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does Lock cover RLock as well?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://pkg.go.dev/sync#RWMutex.Lock

Lock locks rw for writing. If the lock is already locked for reading or writing, Lock blocks until the lock is available.

so just Lock is enough

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there're locks on different objects - write lock on a target histogram and read lock on src histogram

Copy link
Contributor

@tenmozes tenmozes May 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh sorry, makes sense ;)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

potentially, it may cause deadlock.

If goroutine 1 calls merge at histogram A for histogram B and goroutine 2 calls merge at histogram B for histogram A.

defer b.mu.RUnlock()

h.lower += b.lower
h.upper += b.upper
h.sum += b.sum

for i, db := range b.decimalBuckets {
if db == nil {
continue
}
for j := range db {
h.decimalBuckets[i][j] = db[j]
}
}
}

// VisitNonZeroBuckets calls f for all buckets with non-zero counters.
//
// vmrange contains "<start>...<end>" string with bucket bounds. The lower bound
// isn't included in the bucket, while the upper bound is included.
// This is required to be compatible with Prometheus-style histogram buckets
// with `le` (less or equal) labels.
func (h *Histogram) VisitNonZeroBuckets(f func(vmrange string, count uint64)) {
h.mu.Lock()
h.mu.RLock()
if h.lower > 0 {
f(lowerBucketRange, h.lower)
}
Expand All @@ -135,7 +157,7 @@ func (h *Histogram) VisitNonZeroBuckets(f func(vmrange string, count uint64)) {
if h.upper > 0 {
f(upperBucketRange, h.upper)
}
h.mu.Unlock()
h.mu.RUnlock()
}

// NewHistogram creates and returns new histogram with the given name.
Expand Down Expand Up @@ -223,9 +245,9 @@ func (h *Histogram) marshalTo(prefix string, w io.Writer) {
}

func (h *Histogram) getSum() float64 {
h.mu.Lock()
h.mu.RLock()
sum := h.sum
h.mu.Unlock()
h.mu.RUnlock()
return sum
}

Expand Down
29 changes: 29 additions & 0 deletions histogram_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,35 @@ import (
"time"
)

func TestHistogramMerge(t *testing.T) {
name := `TestHistogramMerge`
h := NewHistogram(name)
// Write data to histogram
for i := 98; i < 218; i++ {
h.Update(float64(i))
}

b := NewHistogram("test")
for i := 98; i < 218; i++ {
h.Update(float64(i))
}

h.Merge(b)

// Make sure the histogram prints <prefix>_bucket on marshalTo call
testMarshalTo(t, h, "prefix", `prefix_bucket{vmrange="8.799e+01...1.000e+02"} 6
prefix_bucket{vmrange="1.000e+02...1.136e+02"} 26
prefix_bucket{vmrange="1.136e+02...1.292e+02"} 32
prefix_bucket{vmrange="1.292e+02...1.468e+02"} 34
prefix_bucket{vmrange="1.468e+02...1.668e+02"} 40
prefix_bucket{vmrange="1.668e+02...1.896e+02"} 46
prefix_bucket{vmrange="1.896e+02...2.154e+02"} 52
prefix_bucket{vmrange="2.154e+02...2.448e+02"} 4
prefix_sum 37800
prefix_count 240
`)
}

func TestGetVMRange(t *testing.T) {
f := func(bucketIdx int, vmrangeExpected string) {
t.Helper()
Expand Down
Loading