-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
1,759 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// Copyright The HTNN Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package sentinel | ||
|
||
import ( | ||
sentinelApi "github.com/alibaba/sentinel-golang/api" | ||
sentinelConf "github.com/alibaba/sentinel-golang/core/config" | ||
"github.com/alibaba/sentinel-golang/logging" | ||
|
||
"mosn.io/htnn/api/pkg/filtermanager/api" | ||
"mosn.io/htnn/api/pkg/plugins" | ||
"mosn.io/htnn/plugins/plugins/sentinel/rules" | ||
"mosn.io/htnn/types/plugins/sentinel" | ||
) | ||
|
||
func init() { | ||
plugins.RegisterPlugin(sentinel.Name, &plugin{}) | ||
} | ||
|
||
type plugin struct { | ||
sentinel.Plugin | ||
} | ||
|
||
func (p *plugin) Factory() api.FilterFactory { | ||
return factory | ||
} | ||
|
||
func (p *plugin) Config() api.PluginConfig { | ||
return &config{} | ||
} | ||
|
||
type config struct { | ||
sentinel.CustomConfig | ||
|
||
params sentinelApi.EntryOption | ||
attachments []*sentinel.Source | ||
m *res2RuleMap | ||
} | ||
|
||
type res2RuleMap struct { | ||
f map[string]*sentinel.FlowRule | ||
hs map[string]*sentinel.HotSpotRule | ||
cb map[string]*sentinel.CircuitBreakerRule | ||
} | ||
|
||
func (conf *config) Init(cb api.ConfigCallbackHandler) error { | ||
sc := sentinelConf.NewDefaultConfig() | ||
sc.Sentinel.Log.Logger = logging.NewConsoleLogger() | ||
sc.Sentinel.Log.Dir = "/tmp/sentinel" | ||
if err := sentinelApi.InitWithConfig(sc); err != nil { | ||
return err | ||
} | ||
|
||
if _, err := loadRules(conf); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func loadRules(conf *config) (bool, error) { | ||
conf.m = &res2RuleMap{ | ||
f: make(map[string]*sentinel.FlowRule), | ||
hs: make(map[string]*sentinel.HotSpotRule), | ||
cb: make(map[string]*sentinel.CircuitBreakerRule), | ||
} | ||
|
||
conf.params = sentinelApi.WithArgs() | ||
conf.attachments = make([]*sentinel.Source, 0) | ||
hs := conf.GetHotSpot() | ||
if hs != nil { | ||
args := make([]interface{}, len(hs.GetParams())) | ||
for i, p := range hs.GetParams() { | ||
args[i] = p | ||
} | ||
conf.params = sentinelApi.WithArgs(args...) | ||
conf.attachments = hs.GetAttachments() | ||
} | ||
|
||
if ok, err := rules.LoadFlowRules(conf.GetFlow(), conf.m.f); !ok || err != nil { | ||
return ok, err | ||
} | ||
|
||
if ok, err := rules.LoadHotSpotRules(hs, conf.m.hs); !ok || err != nil { | ||
return ok, err | ||
} | ||
|
||
if ok, err := rules.LoadCircuitBreakerRules(conf.GetCircuitBreaker(), conf.m.cb); !ok || err != nil { | ||
return ok, err | ||
} | ||
|
||
return true, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
// Copyright The HTNN Authors. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package sentinel | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"google.golang.org/protobuf/encoding/protojson" | ||
) | ||
|
||
func TestConfig(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
input string | ||
err string | ||
}{ | ||
{ | ||
name: "resource are required", | ||
input: `{}`, | ||
err: "invalid Config.Resource: value is required", | ||
}, | ||
{ | ||
name: "one of flow, hotSpot, circuitBreaker is required", | ||
input: `{"resource": {"from": "HEADER", "key": "test"}}`, | ||
err: "config must have at least one of 'flow', 'hotSpot', 'circuitBreaker'", | ||
}, | ||
{ | ||
name: "flow err: threshold must be greater than 0", | ||
input: `{"resource": {"from": "HEADER", "key": "test"}, "flow": {"rules": [{"resource": "flow"}]}}`, | ||
err: "'threshold' must be greater than 0", | ||
}, | ||
{ | ||
name: "flow ok: current resource", | ||
input: `{"resource": {"from": "HEADER", "key": "test"}, "flow": {"rules": [{"resource": "flow", "threshold": 10}]}}`, | ||
err: "", | ||
}, | ||
{ | ||
name: "flow ok: related resource", | ||
input: `{"resource": {"from": "HEADER", "key": "test"}, "flow": {"rules": [{"resource": "f2", "relationStrategy": "ASSOCIATED_RESOURCE", "refResource": "f1"}]}}`, | ||
err: "", | ||
}, | ||
{ | ||
name: "hot spot err: one of params, attachments is required", | ||
input: `{"resource": {"from": "HEADER", "key": "test"}, "hotSpot": {}}`, | ||
err: "'params' and 'attachments' cannot both be empty", | ||
}, | ||
{ | ||
name: "hot spot err: threshold must be greater than 0", | ||
input: `{"resource": {"from": "HEADER", "key": "test"}, "hotSpot": {"params": ["test"], "rules": [{"resource": "hs"}]}}`, | ||
err: "invalid HotSpotRule.Threshold: value must be greater than 0", | ||
}, | ||
{ | ||
name: "hot spot ok", | ||
input: `{"resource": {"from": "HEADER", "key": "test"}, "hotSpot": {"params": ["test"], "rules": [{"resource": "hs", "metricType": "QPS", "threshold": 10}]}}`, | ||
err: "", | ||
}, | ||
{ | ||
name: "circuit breaker err: threshold must be greater than 0", | ||
input: `{"resource": {"from": "HEADER", "key": "test"}, "circuitBreaker": {"rules": [{"resource": "cb"}]}}`, | ||
err: "invalid CircuitBreakerRule.Threshold: value must be greater than 0", | ||
}, | ||
{ | ||
name: "circuit breaker ok", | ||
input: `{"resource": {"from": "HEADER", "key": "test"}, "circuitBreaker": {"rules": [{"resource": "cb", "threshold": 10}]}}`, | ||
err: "", | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
conf := &config{} | ||
err := protojson.Unmarshal([]byte(tt.input), conf) | ||
if err == nil { | ||
err = conf.Validate() | ||
} | ||
if tt.err == "" { | ||
assert.Nil(t, err) | ||
err = conf.Init(nil) | ||
assert.Nil(t, err) | ||
} else { | ||
fmt.Println(err) | ||
assert.ErrorContains(t, err, tt.err) | ||
} | ||
}) | ||
} | ||
|
||
} |
Oops, something went wrong.