From 470b85a9df630de629c3cd29671d18fa0921c3d7 Mon Sep 17 00:00:00 2001 From: louyuting <1849491904@qq.com> Date: Tue, 8 Sep 2020 22:59:02 +0800 Subject: [PATCH] refine code in flow/circuitbreaker/stat/system module remove unused code --- api/slot_chain.go | 10 ++-- core/flow/slot.go | 5 +- core/log/slot.go | 8 +-- core/stat/resource_node.go | 41 ++------------- core/stat/resource_node_test.go | 90 --------------------------------- core/stat/stat_prepare_slot.go | 4 +- core/stat/stat_slot.go | 20 +++----- core/system/slot.go | 6 +-- core/system/slot_test.go | 12 ++--- 9 files changed, 32 insertions(+), 164 deletions(-) delete mode 100644 core/stat/resource_node_test.go diff --git a/api/slot_chain.go b/api/slot_chain.go index 72045be41..61746e65d 100644 --- a/api/slot_chain.go +++ b/api/slot_chain.go @@ -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 diff --git a/core/flow/slot.go b/core/flow/slot.go index 281c8c650..0e8f58ca2 100644 --- a/core/flow/slot.go +++ b/core/flow/slot.go @@ -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 diff --git a/core/log/slot.go b/core/log/slot.go index 6d27deb09..572fb581e 100644 --- a/core/log/slot.go +++ b/core/log/slot.go @@ -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) { } diff --git a/core/stat/resource_node.go b/core/stat/resource_node.go index 5cb9a5903..4ba78e506 100644 --- a/core/stat/resource_node.go +++ b/core/stat/resource_node.go @@ -1,11 +1,7 @@ package stat import ( - "fmt" - "sync" - "github.com/alibaba/sentinel-golang/core/base" - sbase "github.com/alibaba/sentinel-golang/core/stat/base" ) type ResourceNode struct { @@ -13,19 +9,15 @@ type ResourceNode struct { 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, } } @@ -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 -} diff --git a/core/stat/resource_node_test.go b/core/stat/resource_node_test.go deleted file mode 100644 index ae43840ef..000000000 --- a/core/stat/resource_node_test.go +++ /dev/null @@ -1,90 +0,0 @@ -package stat - -import ( - "fmt" - "sync" - "testing" - - "github.com/alibaba/sentinel-golang/core/base" - "github.com/stretchr/testify/assert" -) - -func TestResourceNode_GetOrCreateSlidingWindowMetric(t *testing.T) { - type args struct { - sampleCount uint32 - intervalInMs uint32 - } - tests := []struct { - name string - }{ - { - name: "TestResourceNode_GetOrCreateSlidingWindowMetric", - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - n := NewResourceNode("aa", base.ResTypeCommon) - - argsList := []args{ - { - sampleCount: 10, - intervalInMs: 10000, - }, - { - sampleCount: 5, - intervalInMs: 10000, - }, - { - sampleCount: 2, - intervalInMs: 10000, - }, - { - sampleCount: 1, - intervalInMs: 10000, - }, - { - sampleCount: 10, - intervalInMs: 5000, - }, - { - sampleCount: 2, - intervalInMs: 5000, - }, - { - sampleCount: 5, - intervalInMs: 5000, - }, - { - sampleCount: 1, - intervalInMs: 5000, - }, - { - sampleCount: 1, - intervalInMs: 2000, - }, - { - sampleCount: 2, - intervalInMs: 2000, - }, - } - - wg := &sync.WaitGroup{} - wg.Add(100) - for i := 0; i < 100; i++ { - go func(g *sync.WaitGroup) { - for _, as := range argsList { - n.GetOrCreateSlidingWindowMetric(as.sampleCount, as.intervalInMs) - } - g.Done() - }(wg) - } - wg.Wait() - - for _, as := range argsList { - key := fmt.Sprintf("%d/%d", as.sampleCount, as.intervalInMs) - assert.True(t, n.GetSlidingWindowMetric(key) != nil) - } - assert.True(t, len(n.readOnlyStats) == 10) - }) - } -} diff --git a/core/stat/stat_prepare_slot.go b/core/stat/stat_prepare_slot.go index 092e88530..67fc873f0 100644 --- a/core/stat/stat_prepare_slot.go +++ b/core/stat/stat_prepare_slot.go @@ -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 diff --git a/core/stat/stat_slot.go b/core/stat/stat_slot.go index 23d083c89..1df3e04a2 100644 --- a/core/stat/stat_slot.go +++ b/core/stat/stat_slot.go @@ -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()) @@ -37,7 +31,7 @@ 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 } @@ -45,14 +39,14 @@ func (s *StatisticSlot) recordPassFor(sn base.StatNode, count uint32) { 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 } diff --git a/core/system/slot.go b/core/system/slot.go index 785d7f09c..9c8e3e39e 100644 --- a/core/system/slot.go +++ b/core/system/slot.go @@ -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 } @@ -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: diff --git a/core/system/slot_test.go b/core/system/slot_test.go index fe21ddf35..287a72c9f 100644 --- a/core/system/slot_test.go +++ b/core/system/slot_test.go @@ -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) @@ -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, @@ -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} @@ -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} @@ -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} @@ -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}