Skip to content

Commit

Permalink
Refine naming of core slots (#231)
Browse files Browse the repository at this point in the history
  • Loading branch information
louyuting authored Sep 8, 2020
1 parent 8a2f5c4 commit bd07b4b
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 164 deletions.
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

0 comments on commit bd07b4b

Please sign in to comment.