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

Refine API and exported structure naming in core pkg #231

Merged
merged 1 commit into from
Sep 8, 2020
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
10 changes: 5 additions & 5 deletions api/slot_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ func GlobalSlotChain() *base.SlotChain {

func BuildDefaultSlotChain() *base.SlotChain {
sc := base.NewSlotChain()
sc.AddStatPrepareSlotLast(&stat.StatNodePrepareSlot{})
sc.AddRuleCheckSlotLast(&system.SystemAdaptiveSlot{})
sc.AddRuleCheckSlotLast(&flow.FlowSlot{})
sc.AddStatPrepareSlotLast(&stat.ResourceNodePrepareSlot{})
sc.AddRuleCheckSlotLast(&system.AdaptiveSlot{})
sc.AddRuleCheckSlotLast(&flow.Slot{})
sc.AddRuleCheckSlotLast(&circuitbreaker.Slot{})
sc.AddRuleCheckSlotLast(&hotspot.Slot{})
sc.AddStatSlotLast(&stat.StatisticSlot{})
sc.AddStatSlotLast(&log.LogSlot{})
sc.AddStatSlotLast(&stat.Slot{})
sc.AddStatSlotLast(&log.Slot{})
sc.AddStatSlotLast(&circuitbreaker.MetricStatSlot{})
sc.AddStatSlotLast(&hotspot.ConcurrencyStatSlot{})
return sc
Expand Down
5 changes: 2 additions & 3 deletions core/flow/slot.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@ import (
"github.com/alibaba/sentinel-golang/logging"
)

// FlowSlot
type FlowSlot struct {
type Slot struct {
}

func (s *FlowSlot) Check(ctx *base.EntryContext) *base.TokenResult {
func (s *Slot) Check(ctx *base.EntryContext) *base.TokenResult {
res := ctx.Resource.Name()
tcs := getTrafficControllerListFor(res)
result := ctx.RuleCheckResult
Expand Down
8 changes: 4 additions & 4 deletions core/log/slot.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import (
"github.com/alibaba/sentinel-golang/core/base"
)

type LogSlot struct {
type Slot struct {
}

func (s *LogSlot) OnEntryPassed(_ *base.EntryContext) {
func (s *Slot) OnEntryPassed(_ *base.EntryContext) {

}

func (s *LogSlot) OnEntryBlocked(ctx *base.EntryContext, blockError *base.BlockError) {
func (s *Slot) OnEntryBlocked(ctx *base.EntryContext, blockError *base.BlockError) {
// TODO: write sentinel-block.log here
}

func (s *LogSlot) OnCompleted(_ *base.EntryContext) {
func (s *Slot) OnCompleted(_ *base.EntryContext) {

}
41 changes: 3 additions & 38 deletions core/stat/resource_node.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,23 @@
package stat

import (
"fmt"
"sync"

"github.com/alibaba/sentinel-golang/core/base"
sbase "github.com/alibaba/sentinel-golang/core/stat/base"
)

type ResourceNode struct {
BaseStatNode

resourceName string
resourceType base.ResourceType
// key is "sampleCount/intervalInMs"
readOnlyStats map[string]*sbase.SlidingWindowMetric
updateLock sync.RWMutex
}

// NewResourceNode creates a new resource node with given name and classification.
func NewResourceNode(resourceName string, resourceType base.ResourceType) *ResourceNode {
return &ResourceNode{
// TODO: make this configurable
BaseStatNode: *NewBaseStatNode(base.DefaultSampleCount, base.DefaultIntervalMs),
resourceName: resourceName,
resourceType: resourceType,
readOnlyStats: make(map[string]*sbase.SlidingWindowMetric),
BaseStatNode: *NewBaseStatNode(base.DefaultSampleCount, base.DefaultIntervalMs),
resourceName: resourceName,
resourceType: resourceType,
}
}

Expand All @@ -36,30 +28,3 @@ func (n *ResourceNode) ResourceType() base.ResourceType {
func (n *ResourceNode) ResourceName() string {
return n.resourceName
}

func (n *ResourceNode) GetSlidingWindowMetric(key string) *sbase.SlidingWindowMetric {
n.updateLock.RLock()
defer n.updateLock.RUnlock()
return n.readOnlyStats[key]
}

func (n *ResourceNode) GetOrCreateSlidingWindowMetric(sampleCount, intervalInMs uint32) *sbase.SlidingWindowMetric {
key := fmt.Sprintf("%d/%d", sampleCount, intervalInMs)
fastVal := n.GetSlidingWindowMetric(key)
if fastVal != nil {
return fastVal
}

n.updateLock.Lock()
defer n.updateLock.Unlock()

v, exist := n.readOnlyStats[key]
if exist {
return v
}

newSlidingWindow := sbase.NewSlidingWindowMetric(sampleCount, intervalInMs, n.arr)
n.readOnlyStats[key] = newSlidingWindow
// TODO clean unused entity in readOnlyStats.
return newSlidingWindow
}
90 changes: 0 additions & 90 deletions core/stat/resource_node_test.go

This file was deleted.

4 changes: 2 additions & 2 deletions core/stat/stat_prepare_slot.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import (
"github.com/alibaba/sentinel-golang/core/base"
)

type StatNodePrepareSlot struct {
type ResourceNodePrepareSlot struct {
}

func (s *StatNodePrepareSlot) Prepare(ctx *base.EntryContext) {
func (s *ResourceNodePrepareSlot) Prepare(ctx *base.EntryContext) {
node := GetOrCreateResourceNode(ctx.Resource.Name(), ctx.Resource.Classification())
// Set the resource node to the context.
ctx.StatNode = node
Expand Down
20 changes: 7 additions & 13 deletions core/stat/stat_slot.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,24 @@ import (
"github.com/alibaba/sentinel-golang/util"
)

const SlotName = "StatisticSlot"

type StatisticSlot struct {
}

func (s *StatisticSlot) String() string {
return SlotName
type Slot struct {
}

func (s *StatisticSlot) OnEntryPassed(ctx *base.EntryContext) {
func (s *Slot) OnEntryPassed(ctx *base.EntryContext) {
s.recordPassFor(ctx.StatNode, ctx.Input.AcquireCount)
if ctx.Resource.FlowType() == base.Inbound {
s.recordPassFor(InboundNode(), ctx.Input.AcquireCount)
}
}

func (s *StatisticSlot) OnEntryBlocked(ctx *base.EntryContext, blockError *base.BlockError) {
func (s *Slot) OnEntryBlocked(ctx *base.EntryContext, blockError *base.BlockError) {
s.recordBlockFor(ctx.StatNode, ctx.Input.AcquireCount)
if ctx.Resource.FlowType() == base.Inbound {
s.recordBlockFor(InboundNode(), ctx.Input.AcquireCount)
}
}

func (s *StatisticSlot) OnCompleted(ctx *base.EntryContext) {
func (s *Slot) OnCompleted(ctx *base.EntryContext) {
rt := util.CurrentTimeMillis() - ctx.StartTime()
ctx.PutRt(rt)
s.recordCompleteFor(ctx.StatNode, ctx.Input.AcquireCount, rt, ctx.Err())
Expand All @@ -37,22 +31,22 @@ func (s *StatisticSlot) OnCompleted(ctx *base.EntryContext) {
}
}

func (s *StatisticSlot) recordPassFor(sn base.StatNode, count uint32) {
func (s *Slot) recordPassFor(sn base.StatNode, count uint32) {
if sn == nil {
return
}
sn.IncreaseGoroutineNum()
sn.AddMetric(base.MetricEventPass, uint64(count))
}

func (s *StatisticSlot) recordBlockFor(sn base.StatNode, count uint32) {
func (s *Slot) recordBlockFor(sn base.StatNode, count uint32) {
if sn == nil {
return
}
sn.AddMetric(base.MetricEventBlock, uint64(count))
}

func (s *StatisticSlot) recordCompleteFor(sn base.StatNode, count uint32, rt uint64, err error) {
func (s *Slot) recordCompleteFor(sn base.StatNode, count uint32, rt uint64, err error) {
if sn == nil {
return
}
Expand Down
6 changes: 3 additions & 3 deletions core/system/slot.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import (
"github.com/alibaba/sentinel-golang/core/stat"
)

type SystemAdaptiveSlot struct {
type AdaptiveSlot struct {
}

func (s *SystemAdaptiveSlot) Check(ctx *base.EntryContext) *base.TokenResult {
func (s *AdaptiveSlot) Check(ctx *base.EntryContext) *base.TokenResult {
if ctx == nil || ctx.Resource == nil || ctx.Resource.FlowType() != base.Inbound {
return nil
}
Expand All @@ -29,7 +29,7 @@ func (s *SystemAdaptiveSlot) Check(ctx *base.EntryContext) *base.TokenResult {
return result
}

func (s *SystemAdaptiveSlot) doCheckRule(rule *Rule) (bool, float64) {
func (s *AdaptiveSlot) doCheckRule(rule *Rule) (bool, float64) {
threshold := rule.TriggerCount
switch rule.MetricType {
case InboundQPS:
Expand Down
12 changes: 6 additions & 6 deletions core/system/slot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func TestCheckNilInput(t *testing.T) {
var sas *SystemAdaptiveSlot
var sas *AdaptiveSlot

t.Run("NilInput", func(t *testing.T) {
r := sas.Check(nil)
Expand All @@ -29,7 +29,7 @@ func TestCheckNilInput(t *testing.T) {
}

func TestCheckEmptyRule(t *testing.T) {
var sas *SystemAdaptiveSlot
var sas *AdaptiveSlot
rw := base.NewResourceWrapper("test", base.ResTypeCommon, base.Inbound)
r := sas.Check(&base.EntryContext{
Resource: rw,
Expand All @@ -39,7 +39,7 @@ func TestCheckEmptyRule(t *testing.T) {
}

func TestDoCheckRuleConcurrency(t *testing.T) {
var sas *SystemAdaptiveSlot
var sas *AdaptiveSlot
rule := &Rule{MetricType: Concurrency,
TriggerCount: 0.5}

Expand All @@ -59,7 +59,7 @@ func TestDoCheckRuleConcurrency(t *testing.T) {
}

func TestDoCheckRuleLoad(t *testing.T) {
var sas *SystemAdaptiveSlot
var sas *AdaptiveSlot
rule := &Rule{MetricType: Load,
TriggerCount: 0.5}

Expand All @@ -80,7 +80,7 @@ func TestDoCheckRuleLoad(t *testing.T) {
}

func TestDoCheckRuleCpuUsage(t *testing.T) {
var sas *SystemAdaptiveSlot
var sas *AdaptiveSlot
rule := &Rule{MetricType: CpuUsage,
TriggerCount: 0.5}

Expand All @@ -101,7 +101,7 @@ func TestDoCheckRuleCpuUsage(t *testing.T) {
}

func TestDoCheckRuleDefault(t *testing.T) {
var sas *SystemAdaptiveSlot
var sas *AdaptiveSlot
rule := &Rule{MetricType: MetricTypeSize,
TriggerCount: 0.5}

Expand Down