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

feat: return entry when request is blocked. #494

Open
wants to merge 15 commits into
base: master
Choose a base branch
from

Conversation

sysulq
Copy link

@sysulq sysulq commented Nov 18, 2022

Describe what this PR does / why we need it

Does this pull request fix one issue?

Fixed #493

Describe how you did it

Describe how to verify it

Special notes for reviews

@CLAassistant
Copy link

CLAassistant commented Nov 18, 2022

CLA assistant check
All committers have signed the CLA.

Copy link
Collaborator

@binbin0325 binbin0325 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please fix ci

@sysulq
Copy link
Author

sysulq commented Nov 25, 2022

@binbin0325 Seems like this golanglint-ci should be upgraded, and there are many lint errors should be resolved.

And this PR should have no affects about the failed CI.

$ golangci-lint --version
golangci-lint has version v1.50.1 built from (unknown, mod sum: "h1:C829clMcZXEORakZlwpk7M4iDw2XiwxxKaG504SZ9zY=") on (unknown)

golangci-lint run
core/hotspot/slot_test.go:48:2: S1023: redundant `return` statement (gosimple)
        return
        ^
core/hotspot/traffic_shaping_test.go:35:2: S1023: redundant `return` statement (gosimple)
        return
        ^
core/hotspot/traffic_shaping_test.go:78:2: S1023: redundant `return` statement (gosimple)
        return
        ^
core/hotspot/rule.go:104:25: SA1026: trying to marshal unsupported type map[interface{}]int64, via x.SpecificItems (staticcheck)
        b, err := json.Marshal(r)
                               ^
core/base/result_test.go:49:19: Error return value is not checked (errcheck)
        RegistryBlockType(BlockTypeNew1, "new1")
                         ^
core/base/result_test.go:87:2: var `blockTypeErr` is unused (unused)
        blockTypeErr = fmt.Errorf("block type err")
        ^
core/stat/base/leap_array.go:46:23: func `(*BucketWrap).isTimeInBucket` is unused (unused)
func (ww *BucketWrap) isTimeInBucket(now uint64, bucketLengthInMs uint32) bool {
                      ^
core/stat/base/leap_array.go:42:23: func `(*BucketWrap).resetTo` is unused (unused)
func (ww *BucketWrap) resetTo(startTime uint64) {
                      ^
logging/logging.go:217:11: S1034: assigning the result of this type assertion to a variable (switch v := v.(type)) could eliminate type assertions in switch cases (gosimple)
                        switch v.(type) {
                               ^
logging/logging.go:219:30: S1034(related information): could eliminate this type assertion (gosimple)
                                data := toSafeJSONString(v.(string))
                                                         ^
logging/logging.go:222:30: S1034(related information): could eliminate this type assertion (gosimple)
                                data := toSafeJSONString(v.(error).Error())
                                                         ^
util/atomic.go:47:2: S1008: should use 'return atomic.LoadInt32(&(b.flag)) != 0' instead of 'if atomic.LoadInt32(&(b.flag)) != 0 { return true }; return false' (gosimple)
        if atomic.LoadInt32(&(b.flag)) != 0 {
        ^
core/isolation/rule_manager.go:192:2: S1011: should replace loop with `ret = append(ret, resRules...)` (gosimple)
        for _, r := range resRules {
        ^
core/system_metric/sys_metric_stat.go:77:26: Error return value of `metric_exporter.Register` is not checked (errcheck)
        metric_exporter.Register(cpuRatioGauge)
                                ^
core/system_metric/sys_metric_stat.go:78:26: Error return value of `metric_exporter.Register` is not checked (errcheck)
        metric_exporter.Register(processMemoryGauge)
                                ^
core/stat/stat_slot.go:39:26: Error return value of `metric_exporter.Register` is not checked (errcheck)
        metric_exporter.Register(handledCounter)
                                ^
ext/datasource/helper_test.go:108:18: Error return value of `flow.ClearRules` is not checked (errcheck)
                flow.ClearRules()
                               ^
ext/datasource/helper_test.go:109:17: Error return value of `flow.LoadRules` is not checked (errcheck)
                flow.LoadRules([]*flow.Rule{
                              ^
ext/datasource/helper_test.go:126:18: Error return value of `flow.ClearRules` is not checked (errcheck)
                flow.ClearRules()
                               ^
ext/datasource/helper_test.go:132:18: Error return value of `flow.ClearRules` is not checked (errcheck)
                flow.ClearRules()
                               ^
ext/datasource/helper_test.go:216:20: Error return value of `system.ClearRules` is not checked (errcheck)
                system.ClearRules()
                                 ^
ext/datasource/helper_test.go:217:19: Error return value of `system.LoadRules` is not checked (errcheck)
                system.LoadRules([]*system.Rule{
                                ^
ext/datasource/helper_test.go:230:20: Error return value of `system.ClearRules` is not checked (errcheck)
                system.ClearRules()
                                 ^
ext/datasource/helper_test.go:236:20: Error return value of `system.ClearRules` is not checked (errcheck)
                system.ClearRules()
                                 ^
ext/datasource/file/refreshable_file_test.go:250:16: Error return value of `f.WriteString` is not checked (errcheck)
                f.WriteString("\n" + TestSystemRules)
                             ^
ext/datasource/file/refreshable_file_test.go:251:9: Error return value of `f.Sync` is not checked (errcheck)
                f.Sync()
                      ^
tests/benchmark/hotspot/hotspot_benchmark_test.go:40:2: var `hotspotParamValue` is unused (unused)
        hotspotParamValue = "hotspotParamValue"
        ^
core/log/metric/aggregator.go:78:2: S1000: should use for range instead of for { select {} } (gosimple)
        for {
        ^
core/circuitbreaker/circuit_breaker.go:63:2: S1021: should merge variable declaration with assignment on next line (gosimple)
        var state State
        ^
core/log/metric/reader.go:193:5: S1009: should omit nil check; len() for []*github.com/alibaba/sentinel-golang/core/base.MetricItem is defined as zero (gosimple)
        if items == nil || len(items) == 0 {
           ^

@sysulq
Copy link
Author

sysulq commented Nov 25, 2022

Run the command in the go.yml

$ golangci-lint run --timeout=10m -v --disable-all --enable=govet --enable=staticcheck --enable=ineffassign --enable=misspell

core/hotspot/rule.go:104:25: SA1026: trying to marshal unsupported type map[interface{}]int64, via x.SpecificItems (staticcheck)
        b, err := json.Marshal(r)

@sysulq
Copy link
Author

sysulq commented Nov 25, 2022

golang/go#55078 (comment)

Refer this, the failed CI is caused by x/sys, which use unsafe.Slice new from go1.17.x

We should consider to upgrade go mod to 1.18 or above, and it's done in this PR.

@sysulq sysulq requested a review from binbin0325 November 25, 2022 09:00
@codecov-commenter
Copy link

Codecov Report

Base: 53.17% // Head: 53.16% // Decreases project coverage by -0.00% ⚠️

Coverage data is based on head (8a7a883) compared to base (0d804bb).
Patch coverage: 100.00% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #494      +/-   ##
==========================================
- Coverage   53.17%   53.16%   -0.01%     
==========================================
  Files          91       91              
  Lines        5896     5889       -7     
==========================================
- Hits         3135     3131       -4     
+ Misses       2414     2412       -2     
+ Partials      347      346       -1     
Impacted Files Coverage Δ
api/api.go 17.85% <100.00%> (ø)
core/config/config.go 18.25% <100.00%> (ø)
core/hotspot/rule.go 69.69% <100.00%> (+0.25%) ⬆️
core/log/metric/common.go 86.00% <100.00%> (ø)
ext/datasource/file/refreshable_file.go 53.33% <100.00%> (+0.16%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

Comment on lines -55 to -56
diff -u <(echo -n) <(gofmt -d -s .)
diff -u <(echo -n) <(goimports -d .)
Copy link
Contributor

@jnan806 jnan806 Dec 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this two line will help to locate code which needs to be re-format

Comment on lines -44 to -45
- name: Install goimports
run: go get golang.org/x/tools/cmd/goimports
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

goimports check is necessary to format code

Copy link
Author

@sysulq sysulq Jan 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's recommend to use linters in golangci-lint.

https://golangci-lint.run/usage/linters/#goimports

Copy link
Contributor

@jnan806 jnan806 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And maybe need to re-format codes with goimports

Comment on lines +103 to +104
return fmt.Sprintf("{Id:%s, Resource:%s, MetricType:%+v, ControlBehavior:%+v, ParamIndex:%d, ParamKey:%s, Threshold:%d, MaxQueueingTimeMs:%d, BurstCount:%d, DurationInSec:%d, ParamsMaxCapacity:%d, SpecificItems:%+v}",
r.ID, r.Resource, r.MetricType, r.ControlBehavior, r.ParamIndex, r.ParamKey, r.Threshold, r.MaxQueueingTimeMs, r.BurstCount, r.DurationInSec, r.ParamsMaxCapacity, r.SpecificItems)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe it is more appropriate to keep the original

Copy link
Author

@sysulq sysulq Jan 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

json marshal do not support map[interface{}]int64 like SpecificItems

Copy link
Collaborator

@binbin0325 binbin0325 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This pr does several things. Can you split multiple pr?

@@ -176,7 +176,7 @@ func entry(resource string, options *EntryOptions) (*base.SentinelEntry, *base.B
// must finish the lifecycle of r.
blockErr := base.NewBlockErrorFromDeepCopy(r.BlockError())
e.Exit()
return nil, blockErr
return e, blockErr
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why return entry when blocked, and what do you need to do with it

Copy link
Author

@sysulq sysulq Jan 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leave the control to caller, and they can do any thing like log/metrics/fallback which needs entry info.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand. Do you want to get an EntryContext through entry? or you can give an example, how do you do things like log/metric/fallback

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the use case. Right now, there is no way to fetch entry info like ResourceType/TrafficType... when error happens.

https://github.com/douyu/jupiter/blob/493bb847aeb6fd28addbd6c7d621c34f0f2a2819/pkg/core/sentinel/config.go#LL103

@sysulq
Copy link
Author

sysulq commented Jan 13, 2023

This pr does several things. Can you split multiple pr?

This pr aims to return entry object when error returned, and other changes are caused by the origin master code which can not pass the ci process :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

feat: returns entry info even when request is blocked.
5 participants