base

package
v0.0.0-...-3c16d7b Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 30, 2023 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

Package stat/base provides fundamental data structures of statistics.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AtomicBucketWrapArray

type AtomicBucketWrapArray struct {
	// contains filtered or unexported fields
}

AtomicBucketWrapArray represents a thread-safe circular array.

The length of the array should be provided on-create and cannot be modified.

func NewAtomicBucketWrapArray

func NewAtomicBucketWrapArray(len int, bucketLengthInMs uint32, generator BucketGenerator) *AtomicBucketWrapArray

NewAtomicBucketWrapArray creates an AtomicBucketWrapArray and initializes data of each BucketWrap.

The len represents the length of the circular array. The bucketLengthInMs represents bucket length of each bucket (in milliseconds). The generator accepts a BucketGenerator to generate and refresh buckets.

func NewAtomicBucketWrapArrayWithTime

func NewAtomicBucketWrapArrayWithTime(len int, bucketLengthInMs uint32, now uint64, generator BucketGenerator) *AtomicBucketWrapArray

type BucketGenerator

type BucketGenerator interface {
	// NewEmptyBucket creates new raw data inside the bucket.
	NewEmptyBucket() interface{}

	// ResetBucketTo refreshes the BucketWrap to provided startTime and resets all data inside the given bucket.
	ResetBucketTo(bucket *BucketWrap, startTime uint64) *BucketWrap
}

BucketGenerator represents the "generic" interface for generating and refreshing buckets.

type BucketLeapArray

type BucketLeapArray struct {
	// contains filtered or unexported fields
}

BucketLeapArray is the sliding window implementation based on LeapArray (as the sliding window infrastructure) and MetricBucket (as the data type). The MetricBucket is used to record statistic metrics per minimum time unit (i.e. the bucket time span).

func NewBucketLeapArray

func NewBucketLeapArray(sampleCount uint32, intervalInMs uint32) *BucketLeapArray

NewBucketLeapArray creates a BucketLeapArray with given attributes.

The sampleCount represents the number of buckets, while intervalInMs represents the total time span of sliding window. Note that the sampleCount and intervalInMs must be positive and satisfies the condition that intervalInMs%sampleCount == 0. The validation must be done before call NewBucketLeapArray.

func (*BucketLeapArray) AddCount

func (bla *BucketLeapArray) AddCount(event base.MetricEvent, count int64)

func (*BucketLeapArray) BucketLengthInMs

func (bla *BucketLeapArray) BucketLengthInMs() uint32

func (*BucketLeapArray) Count

func (bla *BucketLeapArray) Count(event base.MetricEvent) int64

Count returns the sum count for the given MetricEvent within all valid (non-expired) buckets.

func (*BucketLeapArray) CountWithTime

func (bla *BucketLeapArray) CountWithTime(now uint64, event base.MetricEvent) int64

func (*BucketLeapArray) DataType

func (bla *BucketLeapArray) DataType() string

func (*BucketLeapArray) GetIntervalInSecond

func (bla *BucketLeapArray) GetIntervalInSecond() float64

func (*BucketLeapArray) IntervalInMs

func (bla *BucketLeapArray) IntervalInMs() uint32

func (*BucketLeapArray) MaxConcurrency

func (bla *BucketLeapArray) MaxConcurrency() int32

func (*BucketLeapArray) MinRt

func (bla *BucketLeapArray) MinRt() int64

func (*BucketLeapArray) NewEmptyBucket

func (bla *BucketLeapArray) NewEmptyBucket() interface{}

func (*BucketLeapArray) ResetBucketTo

func (bla *BucketLeapArray) ResetBucketTo(bw *BucketWrap, startTime uint64) *BucketWrap

func (*BucketLeapArray) SampleCount

func (bla *BucketLeapArray) SampleCount() uint32

func (*BucketLeapArray) UpdateConcurrency

func (bla *BucketLeapArray) UpdateConcurrency(concurrency int32)

func (*BucketLeapArray) Values

func (bla *BucketLeapArray) Values(now uint64) []*BucketWrap

Values returns all valid (non-expired) buckets.

func (*BucketLeapArray) ValuesConditional

func (bla *BucketLeapArray) ValuesConditional(now uint64, predicate base.TimePredicate) []*BucketWrap

type BucketWrap

type BucketWrap struct {
	// BucketStart represents start timestamp of this statistic bucket wrapper.
	BucketStart uint64
	// Value represents the actual data structure of the metrics (e.g. MetricBucket).
	Value atomic.Value
}

BucketWrap represents a slot to record metrics.

In order to reduce memory footprint, BucketWrap does not hold the length of the bucket. The length of BucketWrap could be seen in LeapArray. The scope of time is [startTime, startTime+bucketLength). The size of BucketWrap is 24(8+16) bytes.

type LeapArray

type LeapArray struct {
	// contains filtered or unexported fields
}

LeapArray represents the fundamental implementation of a sliding window data-structure.

Some important attributes: the sampleCount represents the number of buckets, while intervalInMs represents the total time span of the sliding window.

For example, assuming sampleCount=5, intervalInMs is 1000ms, so the bucketLength is 200ms. Let's give a diagram to illustrate. Suppose current timestamp is 1188, bucketLength is 200ms, intervalInMs is 1000ms, then time span of current bucket is [1000, 1200). The representation of the underlying structure:

 B0       B1      B2     B3      B4
 |_______|_______|_______|_______|_______|
1000    1200    400     600     800    (1000) ms
       ^
    time=1188

func NewLeapArray

func NewLeapArray(sampleCount uint32, intervalInMs uint32, generator BucketGenerator) (*LeapArray, error)

func (*LeapArray) CurrentBucket

func (la *LeapArray) CurrentBucket(bg BucketGenerator) (*BucketWrap, error)

func (*LeapArray) Values

func (la *LeapArray) Values() []*BucketWrap

Values returns all valid (non-expired) buckets between [curBucketEnd-windowInterval, curBucketEnd], where curBucketEnd=curBucketStart+bucketLength.

func (*LeapArray) ValuesConditional

func (la *LeapArray) ValuesConditional(now uint64, predicate base.TimePredicate) []*BucketWrap

ValuesConditional returns all buckets of which the startTimestamp satisfies the given timestamp condition (predicate). The function uses the parameter "now" as the target timestamp.

type MetricBucket

type MetricBucket struct {
	// contains filtered or unexported fields
}

MetricBucket represents the entity to record metrics per minimum time unit (i.e. the bucket time span). Note that all operations of the MetricBucket are required to be thread-safe.

func NewMetricBucket

func NewMetricBucket() *MetricBucket

func (*MetricBucket) Add

func (mb *MetricBucket) Add(event base.MetricEvent, count int64)

Add statistic count for the given metric event.

func (*MetricBucket) AddRt

func (mb *MetricBucket) AddRt(rt int64)

func (*MetricBucket) Get

func (mb *MetricBucket) Get(event base.MetricEvent) int64

Get current statistic count of the given metric event.

func (*MetricBucket) MaxConcurrency

func (mb *MetricBucket) MaxConcurrency() int32

func (*MetricBucket) MinRt

func (mb *MetricBucket) MinRt() int64

func (*MetricBucket) UpdateConcurrency

func (mb *MetricBucket) UpdateConcurrency(concurrency int32)

type SlidingWindowMetric

type SlidingWindowMetric struct {
	// contains filtered or unexported fields
}

SlidingWindowMetric represents the sliding window metric wrapper. It does not store any data and is the wrapper of BucketLeapArray to adapt to different internal bucket.

SlidingWindowMetric is designed as a high-level, read-only statistic structure for functionalities of Sentinel

func NewSlidingWindowMetric

func NewSlidingWindowMetric(sampleCount, intervalInMs uint32, real *BucketLeapArray) (*SlidingWindowMetric, error)

NewSlidingWindowMetric creates a SlidingWindowMetric with given attributes. The pointer to the internal statistic BucketLeapArray should be valid.

func (*SlidingWindowMetric) AvgRT

func (m *SlidingWindowMetric) AvgRT() float64

func (*SlidingWindowMetric) GetMaxOfSingleBucket

func (m *SlidingWindowMetric) GetMaxOfSingleBucket(event base.MetricEvent) int64

func (*SlidingWindowMetric) GetPreviousQPS

func (m *SlidingWindowMetric) GetPreviousQPS(event base.MetricEvent) float64

func (*SlidingWindowMetric) GetQPS

func (m *SlidingWindowMetric) GetQPS(event base.MetricEvent) float64

func (*SlidingWindowMetric) GetSum

func (m *SlidingWindowMetric) GetSum(event base.MetricEvent) int64

func (*SlidingWindowMetric) MaxConcurrency

func (m *SlidingWindowMetric) MaxConcurrency() int32

func (*SlidingWindowMetric) MinRT

func (m *SlidingWindowMetric) MinRT() float64

func (*SlidingWindowMetric) SecondMetricsOnCondition

func (m *SlidingWindowMetric) SecondMetricsOnCondition(predicate base.TimePredicate) []*base.MetricItem

SecondMetricsOnCondition aggregates metric items by second on condition that the startTime of the statistic buckets satisfies the time predicate.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL