Repository: alibaba/sentinel-golang
Pull request #358 (2020-12-20 13:08:35)

Isolate system metric statistic to independent pkg core/system_metric

### Describe what this PR does / why we need it
Isolate system metric statistic to independent pkg core/system_metric


### Does this pull request fix one issue?

<!--If that, add "Fixes #xxxx" below in the next line. For example, Fixes #15. Otherwise, add "NONE" -->

### Describe how you did it


### Describe how to verify it


### Special notes for reviews

codecov-io: # [Codecov](https://codecov.io/gh/alibaba/sentinel-golang/pull/358?src=pr&el=h1) Report
> Merging [#358](https://codecov.io/gh/alibaba/sentinel-golang/pull/358?src=pr&el=desc) (c7a2841) into [master](https://codecov.io/gh/alibaba/sentinel-golang/commit/b6f3c682bef25402b40ca81a1abd89d624bae0bf?el=desc) (b6f3c68) will **decrease** coverage by `0.02%`.
> The diff coverage is `44.44%`.

[![Impacted file tree graph](https://codecov.io/gh/alibaba/sentinel-golang/pull/358/graphs/tree.svg?width=650&height=150&src=pr&token=SrpzbhKA3E)](https://codecov.io/gh/alibaba/sentinel-golang/pull/358?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##           master     #358      +/-   ##
==========================================
- Coverage   50.89%   50.86%   -0.03%     
==========================================
  Files          69       69              
  Lines        3861     3863       +2     
==========================================
  Hits         1965     1965              
- Misses       1629     1631       +2     
  Partials      267      267              
```


| [Impacted Files](https://codecov.io/gh/alibaba/sentinel-golang/pull/358?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [api/init.go](https://codecov.io/gh/alibaba/sentinel-golang/pull/358/diff?src=pr&el=tree#diff-YXBpL2luaXQuZ28=) | `0.00% <0.00%> (ø)` | |
| [core/system\_metric/sys\_stat.go](https://codecov.io/gh/alibaba/sentinel-golang/pull/358/diff?src=pr&el=tree#diff-Y29yZS9zeXN0ZW1fbWV0cmljL3N5c19zdGF0Lmdv) | `44.44% <33.33%> (ø)` | |
| [core/system/slot.go](https://codecov.io/gh/alibaba/sentinel-golang/pull/358/diff?src=pr&el=tree#diff-Y29yZS9zeXN0ZW0vc2xvdC5nbw==) | `49.15% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/alibaba/sentinel-golang/pull/358?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/alibaba/sentinel-golang/pull/358?src=pr&el=footer). Last update [b6f3c68...c7a2841](https://codecov.io/gh/alibaba/sentinel-golang/pull/358?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/api/init.go b/api/init.go
index d72c60d..eac11b7 100644
--- a/api/init.go
+++ b/api/init.go
@@ -19,7 +19,7 @@ import (
 
 	"github.com/alibaba/sentinel-golang/core/config"
 	"github.com/alibaba/sentinel-golang/core/log/metric"
-	"github.com/alibaba/sentinel-golang/core/system"
+	"github.com/alibaba/sentinel-golang/core/system_metric"
 	"github.com/alibaba/sentinel-golang/util"
 	"github.com/pkg/errors"
 )
@@ -85,7 +85,7 @@ func initCoreComponents() error {
 	}
 
 	if config.SystemStatCollectIntervalMs() > 0 {
-		system.InitCollector(config.SystemStatCollectIntervalMs())
+		system_metric.InitCollector(config.SystemStatCollectIntervalMs())
 	}
 
 	if config.UseCacheTime() {
diff --git a/core/system/slot.go b/core/system/slot.go
index 388ccc9..e4c0888 100644
--- a/core/system/slot.go
+++ b/core/system/slot.go
@@ -17,6 +17,7 @@ package system
 import (
 	"github.com/alibaba/sentinel-golang/core/base"
 	"github.com/alibaba/sentinel-golang/core/stat"
+	"github.com/alibaba/sentinel-golang/core/system_metric"
 )
 
 const (
@@ -87,7 +88,7 @@ func (s *AdaptiveSlot) doCheckRule(rule *Rule) (bool, string, float64) {
 		}
 		return res, msg, rt
 	case Load:
-		l := CurrentLoad()
+		l := system_metric.CurrentLoad()
 		if l > threshold {
 			if rule.Strategy != BBR || !checkBbrSimple() {
 				msg = "system load check blocked"
@@ -96,7 +97,7 @@ func (s *AdaptiveSlot) doCheckRule(rule *Rule) (bool, string, float64) {
 		}
 		return true, "", l
 	case CpuUsage:
-		c := CurrentCpuUsage()
+		c := system_metric.CurrentCpuUsage()
 		if c > threshold {
 			if rule.Strategy != BBR || !checkBbrSimple() {
 				msg = "system cpu usage check blocked"
diff --git a/core/system/sys_stat.go b/core/system_metric/sys_metric_stat.go
similarity index 86%
rename from core/system/sys_stat.go
rename to core/system_metric/sys_metric_stat.go
index e54fa96..0f16427 100644
--- a/core/system/sys_stat.go
+++ b/core/system_metric/sys_metric_stat.go
@@ -12,7 +12,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package system
+package system_metric
 
 import (
 	"math"
@@ -27,7 +27,7 @@ import (
 )
 
 const (
-	notRetrievedValue float64 = -1.0
+	NotRetrievedValue float64 = -1.0
 )
 
 var (
@@ -41,8 +41,8 @@ var (
 )
 
 func init() {
-	currentLoad.Store(notRetrievedValue)
-	currentCpuUsage.Store(notRetrievedValue)
+	currentLoad.Store(NotRetrievedValue)
+	currentCpuUsage.Store(NotRetrievedValue)
 }
 
 func InitCollector(intervalMs uint32) {
@@ -124,15 +124,25 @@ func calculateKernelCpuTick(stat *cpu.TimesStat) float64 {
 func CurrentLoad() float64 {
 	r, ok := currentLoad.Load().(float64)
 	if !ok {
-		return notRetrievedValue
+		return NotRetrievedValue
 	}
 	return r
 }
 
+// Note: SetSystemLoad is used for unit test, the user shouldn't call this function.
+func SetSystemLoad(load float64) {
+	currentLoad.Store(load)
+}
+
 func CurrentCpuUsage() float64 {
 	r, ok := currentCpuUsage.Load().(float64)
 	if !ok {
-		return notRetrievedValue
+		return NotRetrievedValue
 	}
 	return r
 }
+
+// Note: SetSystemCpuUsage is used for unit test, the user shouldn't call this function.
+func SetSystemCpuUsage(cpuUsage float64) {
+	currentCpuUsage.Store(cpuUsage)
+}


Test patch:
diff --git a/core/system/slot_test.go b/core/system/slot_test.go
index f6508b1..47ed852 100644
--- a/core/system/slot_test.go
+++ b/core/system/slot_test.go
@@ -19,6 +19,7 @@ import (
 
 	"github.com/alibaba/sentinel-golang/core/base"
 	"github.com/alibaba/sentinel-golang/core/stat"
+	"github.com/alibaba/sentinel-golang/core/system_metric"
 	"github.com/alibaba/sentinel-golang/util"
 	"github.com/stretchr/testify/assert"
 )
@@ -80,38 +81,40 @@ func TestDoCheckRuleLoad(t *testing.T) {
 
 	t.Run("TrueLoad", func(t *testing.T) {
 		isOK, _, v := sas.doCheckRule(rule)
-		assert.True(t, util.Float64Equals(notRetrievedValue, v))
+		assert.True(t, util.Float64Equals(system_metric.NotRetrievedValue, v))
 		assert.Equal(t, true, isOK)
 	})
 
 	t.Run("BBRTrueLoad", func(t *testing.T) {
 		rule.Strategy = BBR
-		currentLoad.Store(float64(1.0))
+		system_metric.SetSystemLoad(1.0)
 		isOK, _, v := sas.doCheckRule(rule)
 		assert.Equal(t, true, isOK)
 		assert.True(t, util.Float64Equals(float64(1.0), v))
-		currentLoad.Store(float64(notRetrievedValue))
+		system_metric.SetSystemLoad(system_metric.NotRetrievedValue)
 	})
 }
 
 func TestDoCheckRuleCpuUsage(t *testing.T) {
 	var sas *AdaptiveSlot
-	rule := &Rule{MetricType: CpuUsage,
-		TriggerCount: 0.5}
+	rule := &Rule{
+		MetricType:   CpuUsage,
+		TriggerCount: 0.5,
+	}
 
 	t.Run("TrueCpuUsage", func(t *testing.T) {
 		isOK, _, v := sas.doCheckRule(rule)
-		assert.True(t, util.Float64Equals(notRetrievedValue, v))
+		assert.True(t, util.Float64Equals(system_metric.NotRetrievedValue, v))
 		assert.Equal(t, true, isOK)
 	})
 
 	t.Run("BBRTrueCpuUsage", func(t *testing.T) {
 		rule.Strategy = BBR
-		currentCpuUsage.Store(float64(0.8))
+		system_metric.SetSystemCpuUsage(0.8)
 		isOK, _, v := sas.doCheckRule(rule)
 		assert.Equal(t, true, isOK)
-		assert.True(t, util.Float64Equals(float64(0.8), v))
-		currentCpuUsage.Store(float64(notRetrievedValue))
+		assert.True(t, util.Float64Equals(0.8, v))
+		system_metric.SetSystemCpuUsage(system_metric.NotRetrievedValue)
 	})
 }
 
diff --git a/core/system/sys_stat_test.go b/core/system_metric/sys_metric_stat_test.go
similarity index 85%
rename from core/system/sys_stat_test.go
rename to core/system_metric/sys_metric_stat_test.go
index ead3261..ce0a82a 100644
--- a/core/system/sys_stat_test.go
+++ b/core/system_metric/sys_metric_stat_test.go
@@ -12,7 +12,7 @@
 // See the License for the specific language governing permissions and
 // limitations under the License.
 
-package system
+package system_metric
 
 import (
 	"testing"
@@ -23,7 +23,7 @@ import (
 )
 
 func Test_recordCpuUsage(t *testing.T) {
-	defer currentCpuUsage.Store(notRetrievedValue)
+	defer currentCpuUsage.Store(NotRetrievedValue)
 
 	var emptyStat *cpu.TimesStat = nil
 	// total: 2260, user+nice: 950, system+irqs=210
@@ -53,7 +53,7 @@ func Test_recordCpuUsage(t *testing.T) {
 	expected := float64(1600+430-950-210) / (4180 - 2260)
 
 	recordCpuUsage(emptyStat, cur)
-	assert.True(t, util.Float64Equals(notRetrievedValue, CurrentCpuUsage()))
+	assert.True(t, util.Float64Equals(NotRetrievedValue, CurrentCpuUsage()))
 
 	recordCpuUsage(prev, prev)
 	assert.True(t, util.Float64Equals(0.0, CurrentCpuUsage()))
@@ -63,10 +63,10 @@ func Test_recordCpuUsage(t *testing.T) {
 }
 
 func TestCurrentLoad(t *testing.T) {
-	defer currentLoad.Store(notRetrievedValue)
+	defer currentLoad.Store(NotRetrievedValue)
 
 	cLoad := CurrentLoad()
-	assert.True(t, util.Float64Equals(notRetrievedValue, cLoad))
+	assert.True(t, util.Float64Equals(NotRetrievedValue, cLoad))
 
 	v := float64(1.0)
 	currentLoad.Store(v)
@@ -75,10 +75,10 @@ func TestCurrentLoad(t *testing.T) {
 }
 
 func TestCurrentCpuUsage(t *testing.T) {
-	defer currentCpuUsage.Store(notRetrievedValue)
+	defer currentCpuUsage.Store(NotRetrievedValue)
 
 	cpuUsage := CurrentCpuUsage()
-	assert.Equal(t, notRetrievedValue, cpuUsage)
+	assert.Equal(t, NotRetrievedValue, cpuUsage)
 
 	v := float64(0.3)
 	currentCpuUsage.Store(v)