Files
graph/x/histogram.go
zeekay f5aaeb712e Hanzo Graph — the engine, on zapdb, ZAP-native
Derived from Dgraph (github.com/dgraph-io/dgraph) at commit 64804bd2, Apache-2.0.
See NOTICE.

WHAT THIS IS. The graph engine as a Go library: posting lists, the DQL parser,
the type and tokenizer system, the index and schema layers, and the wire types.
39,170 lines of the upstream's 166,337.

THE STORE is github.com/luxfi/zapdb, a Badger fork that kept the package name,
the subpackages (options, pb, y) and the API — Open, OpenManaged, DB, Options —
so the 74 import sites are aliased rather than rewritten.

THE CODEC is ZAP. zapdb serializes with ZAP, not protobuf: its pb package builds
the ZAP types by default and the protobuf ones only under `-tags grpc`, a path
that does not compile (table/builder.go calls pb.Marshal, which that tag
removes). One call site in posting needed converting; every other type here is
Hanzo's own protobuf and is untouched.

NO TRANSPORT. An embedded engine dials nothing, so nothing here imports
google.golang.org/grpc. Removed: 20 transport functions in x, the generated
service stubs for Raft, Zero and Worker (1,657 lines), and the JWT helper —
identity belongs to IAM. The core called none of them.

NOT KEPT: worker, conn, raftwal, the cluster commands, the GraphQL layer and the
test harnesses. `query`, the DQL execution engine, went with them because it
depends on worker; parsing DQL is available here, executing it is not.

The history of the upstream project lives at github.com/dgraph-io/dgraph. This
repository begins at the derivation because three quarters of that history
describes code this fork does not carry.
2026-08-20 21:26:27 -07:00

116 lines
3.4 KiB
Go

// Copyright 2015 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package x
import (
"errors"
"sync"
"time"
"github.com/HdrHistogram/hdrhistogram-go"
)
const (
// The number of histograms to keep in rolling window.
histWrapNum = 2
)
// A slidingHistogram is a wrapper around an
// hdrhistogram.WindowedHistogram. The caller must enforce proper
// synchronization.
type slidingHistogram struct {
windowed *hdrhistogram.WindowedHistogram
nextT time.Time
duration time.Duration
}
// newSlidingHistogram creates a new windowed HDRHistogram with the given
// parameters. Data is kept in the active window for approximately the given
// duration. See the documentation for hdrhistogram.WindowedHistogram for
// details.
func newSlidingHistogram(duration time.Duration, maxVal int64, sigFigs int) *slidingHistogram {
if duration <= 0 {
Panic(errors.New(
"cannot create a sliding histogram with nonpositive duration"))
}
return &slidingHistogram{
nextT: time.Now(),
duration: duration,
windowed: hdrhistogram.NewWindowed(histWrapNum, 0, maxVal, sigFigs),
}
}
func (h *slidingHistogram) tick() {
h.nextT = h.nextT.Add(h.duration / histWrapNum)
h.windowed.Rotate()
}
func (h *slidingHistogram) nextTick() time.Time {
return h.nextT
}
func (h *slidingHistogram) Current() *hdrhistogram.Histogram {
for h.nextTick().Before(time.Now()) {
h.tick()
}
return h.windowed.Merge()
}
func (h *slidingHistogram) RecordValue(v int64) error {
return h.windowed.Current.RecordValue(v)
}
// A Histogram collects observed values by keeping bucketed counts. For
// convenience, pb.y two sets of buckets are kept: A cumulative set (i.e.
// data is never evicted) and a windowed set (which keeps only recently
// collected samples).
//
// Top-level methods generally apply to the cumulative buckets; the windowed
// variant is exposed through the Windowed method.
type Histogram struct {
sync.Mutex
cumulative *hdrhistogram.Histogram
sliding *slidingHistogram
maxVal int64
}
// NewHistogram initializes a given Histogram. The contained windowed histogram
// rotates every 'duration'; both the windowed and the cumulative histogram
// track nonnegative values up to 'maxVal' with 'sigFigs' decimal points of
// precision.
func NewHistogram(duration time.Duration, maxVal int64, sigFigs int) *Histogram {
dHist := newSlidingHistogram(duration, maxVal, sigFigs)
h := &Histogram{}
h.cumulative = hdrhistogram.New(0, maxVal, sigFigs)
h.sliding = dHist
h.maxVal = maxVal
return h
}
// RecordValue adds the given value to the histogram. Recording a value in
// excess of the configured maximum value for that histogram results in
// recording the maximum value instead.
func (h *Histogram) RecordValue(v int64) {
h.Lock()
defer h.Unlock()
if h.sliding.RecordValue(v) != nil {
_ = h.sliding.RecordValue(h.maxVal)
}
if h.cumulative.RecordValue(v) != nil {
_ = h.cumulative.RecordValue(h.maxVal)
}
}