Merge pull request #694 from Edwardro22/master

modified ResLimiter
This commit is contained in:
Dan Christian Bogos
2017-08-02 08:45:32 +02:00
committed by GitHub
13 changed files with 50 additions and 103 deletions

View File

@@ -410,7 +410,7 @@ CREATE TABLE tp_resource_limits (
`blocker` BOOLEAN NOT NULL,
`stored` BOOLEAN NOT NULL,
`weight` decimal(8,2) NOT NULL,
`action_trigger_ids` varchar(64) NOT NULL,
`thresholds` varchar(64) NOT NULL,
`created_at` TIMESTAMP,
PRIMARY KEY (`id`),
KEY `tpid` (`tpid`),
@@ -433,11 +433,10 @@ CREATE TABLE tp_stats (
`queue_length` int(11) NOT NULL,
`ttl` varchar(32) NOT NULL,
`metrics` varchar(64) NOT NULL,
`store` BOOLEAN NOT NULL,
`thresholds` varchar(64) NOT NULL,
`blocker` BOOLEAN NOT NULL,
`stored` BOOLEAN NOT NULL,
`weight` decimal(8,2) NOT NULL,
`thresholds` varchar(64) NOT NULL,
`created_at` TIMESTAMP,
PRIMARY KEY (`id`),
KEY `tpid` (`tpid`),

View File

@@ -406,7 +406,7 @@ CREATE TABLE tp_resource_limits (
"blocker" BOOLEAN NOT NULL,
"stored" BOOLEAN NOT NULL,
"weight" NUMERIC(8,2) NOT NULL,
"action_trigger_ids" varchar(64) NOT NULL,
"thresholds" varchar(64) NOT NULL,
"created_at" TIMESTAMP WITH TIME ZONE
);
CREATE INDEX tp_resource_limits_idx ON tp_resource_limits (tpid);
@@ -429,11 +429,10 @@ CREATE TABLE tp_stats (
"queue_length" INTEGER NOT NULL,
"ttl" varchar(32) NOT NULL,
"metrics" varchar(64) NOT NULL,
"store" BOOLEAN NOT NULL,
"thresholds" varchar(64) NOT NULL,
"blocker" BOOLEAN NOT NULL,
"stored" BOOLEAN NOT NULL,
"weight" decimal(8,2) NOT NULL,
"thresholds" varchar(64) NOT NULL,
"created_at" TIMESTAMP WITH TIME ZONE
);
CREATE INDEX tp_stats_idx ON tp_stats (tpid);

View File

@@ -1,2 +1,2 @@
#Id,FilterType,FilterFieldName,FilterFieldValues,ActivationInterval,QueueLength,TTL,Metrics,Store,Thresholds,Blocker,Stored,Weight
Stats1,*string,Account,1001;1002,2014-07-29T15:00:00Z,100,1s,*asr;*acd;*acc,true,THRESH1;THRESH2,true,true,20
#Id,FilterType,FilterFieldName,FilterFieldValues,ActivationInterval,QueueLength,TTL,Metrics,Blocker,Stored,Weight,Thresholds
Stats1,*string,Account,1001;1002,2014-07-29T15:00:00Z,100,1s,*asr;*acd;*acc,true,true,20,THRESH1;THRESH2
1 #Id FilterType FilterFieldName FilterFieldValues ActivationInterval QueueLength TTL Metrics Store Blocker Stored Weight Thresholds
2 Stats1 *string Account 1001;1002 2014-07-29T15:00:00Z 100 1s *asr;*acd;*acc true true true 20 THRESH1;THRESH2

View File

@@ -1,2 +1,2 @@
#Id,FilterType,FilterFieldName,FilterFieldValues,ActivationInterval,QueueLength,TTL,Metrics,Store,Thresholds,Blocker,Stored,Weight
Stats1,*string,Account,1001;1002,2014-07-29T15:00:00Z,100,1s,*asr;*acd;*acc,true,THRESH1;THRESH2,true,true,20
#Id,FilterType,FilterFieldName,FilterFieldValues,ActivationInterval,QueueLength,TTL,Metrics,Blocker,Stored,Weight,Thresholds
Stats1,*string,Account,1001;1002,2014-07-29T15:00:00Z,100,1s,*asr;*acd;*acc,true,true,20,THRESH1;THRESH2
1 #Id FilterType FilterFieldName FilterFieldValues ActivationInterval QueueLength TTL Metrics Store Blocker Stored Weight Thresholds
2 Stats1 *string Account 1001;1002 2014-07-29T15:00:00Z 100 1s *asr;*acd;*acc true true true 20 THRESH1;THRESH2

View File

@@ -267,15 +267,15 @@ cgrates.org,mas,true,another,value,10
`
resLimits = `
#Id,FilterType,FilterFieldName,FilterFieldValues,ActivationInterval,TTL,Limit,AllocationMessage,Weight,ActionTriggers
#Id,FilterType,FilterFieldName,FilterFieldValues,ActivationInterval,TTL,Limit,AllocationMessage,Weight,Thresholds
ResGroup21,*string,HdrAccount,1001;1002,2014-07-29T15:00:00Z,1s,2,call,true,true,10,
ResGroup21,*string_prefix,HdrDestination,10;20,,,,,,,,
ResGroup21,*rsr_fields,,HdrSubject(~^1.*1$);HdrDestination(1002),,,,,,,,
ResGroup22,*destinations,HdrDestination,DST_FS,2014-07-29T15:00:00Z,3600s,2,premium_call,true,true,10,
`
stats = `
#Id,FilterType,FilterFieldName,FilterFieldValues,ActivationInterval,QueueLength,TTL,Metrics,Store,Thresholds,Weight
Stats1,*string,Account,1001;1002,2014-07-29T15:00:00Z,100,1s,*asr;*acd;*acc,true,THRESH1;THRESH2,true,true,20
#Id,FilterType,FilterFieldName,FilterFieldValues,ActivationInterval,QueueLength,TTL,Metrics,Blocker,Stored,Weight,Thresholds
Stats1,*string,Account,1001;1002,2014-07-29T15:00:00Z,100,1s,*asr;*acd;*acc,true,true,20,THRESH1;THRESH2
`
)
@@ -1436,7 +1436,6 @@ func TestLoadStats(t *testing.T) {
QueueLength: 100,
TTL: "1s",
Metrics: []string{"*asr", "*acd", "*acc"},
Store: true,
Thresholds: []string{"THRESH1", "THRESH2"},
Blocker: true,
Stored: true,

View File

@@ -1847,8 +1847,11 @@ func (tps TpResourceLimits) AsTPResourceLimits() (result []*utils.TPResourceLimi
rl.ActivationInterval.ActivationTime = aiSplt[0]
}
}
if tp.ActionTriggerIds != "" {
rl.ActionTriggerIDs = append(rl.ActionTriggerIDs, strings.Split(tp.ActionTriggerIds, utils.INFIELD_SEP)...)
if tp.Thresholds != "" {
trshSplt := strings.Split(tp.Thresholds, utils.INFIELD_SEP)
for _, trsh := range trshSplt {
rl.Thresholds = append(rl.Thresholds, trsh)
}
}
if tp.FilterType != "" {
rl.Filters = append(rl.Filters, &utils.TPRequestFilter{
@@ -1889,11 +1892,11 @@ func APItoModelResourceLimit(rl *utils.TPResourceLimit) (mdls TpResourceLimits)
mdl.ActivationInterval += utils.INFIELD_SEP + rl.ActivationInterval.ExpiryTime
}
}
for i, atid := range rl.ActionTriggerIDs {
for i, val := range rl.Thresholds {
if i != 0 {
mdl.ActionTriggerIds = mdl.ActionTriggerIds + utils.INFIELD_SEP + atid
mdl.Thresholds = mdl.Thresholds + utils.INFIELD_SEP + val
} else {
mdl.ActionTriggerIds = atid
mdl.Thresholds = val
}
}
}
@@ -1971,9 +1974,6 @@ func (tps TpStatsS) AsTPStats() (result []*utils.TPStats) {
st.Metrics = append(st.Metrics, metr)
}
}
if tp.Store != false {
st.Store = tp.Store
}
if tp.Thresholds != "" {
trshSplt := strings.Split(tp.Thresholds, utils.INFIELD_SEP)
for _, trsh := range trshSplt {
@@ -2025,7 +2025,6 @@ func APItoModelStats(st *utils.TPStats) (mdls TpStatsS) {
mdl.Stored = st.Stored
mdl.Weight = st.Weight
mdl.QueueLength = st.QueueLength
mdl.Store = st.Store
for _, val := range st.Metrics {
mdl.Metrics = mdl.Metrics + utils.INFIELD_SEP + val
}
@@ -2059,7 +2058,6 @@ func APItoTPStats(tpST *utils.TPStats, timezone string) (st *StatsQueue, err err
st = &StatsQueue{
ID: tpST.ID,
QueueLength: tpST.QueueLength,
Store: tpST.Store,
Weight: tpST.Weight,
Blocker: tpST.Blocker,
Stored: tpST.Stored,

View File

@@ -680,14 +680,14 @@ func TestTpResourceLimitsAsTPResourceLimits(t *testing.T) {
Blocker: false,
Weight: 10.0,
Limit: "45",
ActionTriggerIds: "WARN_RES1;WARN_RES2"},
Thresholds: "WARN_RES1;WARN_RES2"},
&TpResourceLimit{
Tpid: "TEST_TPID",
Tag: "ResGroup1",
FilterType: MetaStringPrefix,
FilterFieldName: "Category",
FilterFieldValues: "call;inbound_call",
ActionTriggerIds: "WARN3"},
Thresholds: "WARN3"},
&TpResourceLimit{
Tpid: "TEST_TPID",
Tag: "ResGroup2",
@@ -719,11 +719,11 @@ func TestTpResourceLimitsAsTPResourceLimits(t *testing.T) {
ActivationInterval: &utils.TPActivationInterval{
ActivationTime: tps[0].ActivationInterval,
},
Stored: tps[0].Stored,
Blocker: tps[0].Blocker,
Weight: tps[0].Weight,
Limit: tps[0].Limit,
ActionTriggerIDs: []string{"WARN_RES1", "WARN_RES2", "WARN3"},
Stored: tps[0].Stored,
Blocker: tps[0].Blocker,
Weight: tps[0].Weight,
Limit: tps[0].Limit,
Thresholds: []string{"WARN_RES1", "WARN_RES2", "WARN3"},
},
&utils.TPResourceLimit{
TPid: tps[2].Tpid,
@@ -808,7 +808,6 @@ func TestTPStatsAsTPStats(t *testing.T) {
QueueLength: 100,
TTL: "1s",
Metrics: "*asr;*acd;*acc",
Store: true,
Thresholds: "THRESH1;THRESH2",
Stored: false,
Blocker: false,
@@ -832,7 +831,6 @@ func TestTPStatsAsTPStats(t *testing.T) {
QueueLength: tps[0].QueueLength,
TTL: tps[0].TTL,
Metrics: []string{"*asr", "*acd", "*acc"},
Store: tps[0].Store,
Thresholds: []string{"THRESH1", "THRESH2"},
Stored: tps[0].Stored,
Blocker: tps[0].Blocker,
@@ -856,7 +854,6 @@ func TestAPItoTPStats(t *testing.T) {
QueueLength: 100,
TTL: "1s",
Metrics: []string{"*asr", "*acd", "*acc"},
Store: true,
Thresholds: []string{"THRESH1", "THRESH2"},
Stored: false,
Blocker: false,
@@ -866,7 +863,6 @@ func TestAPItoTPStats(t *testing.T) {
eTPs := &StatsQueue{ID: tps.ID,
QueueLength: tps.QueueLength,
Metrics: []string{"*asr", "*acd", "*acc"},
Store: tps.Store,
Thresholds: []string{"THRESH1", "THRESH2"},
Filters: make([]*RequestFilter, len(tps.Filters)),
Stored: tps.Stored,

View File

@@ -464,7 +464,7 @@ type TpResourceLimit struct {
Blocker bool `index:"8" re:""`
Stored bool `index:"9" re:""`
Weight float64 `index:"10" re:"\d+\.?\d*"`
ActionTriggerIds string `index:"11" re:""`
Thresholds string `index:"11" re:""`
CreatedAt time.Time
}
@@ -489,10 +489,9 @@ type TpStats struct {
QueueLength int `index:"5" re:""`
TTL string `index:"6" re:""`
Metrics string `index:"7" re:""`
Store bool `index:"8" re:""`
Thresholds string `index:"9" re:""`
Blocker bool `index:"10" re:""`
Stored bool `index:"11" re:""`
Weight float64 `index:"12" re:"\d+\.?\d*"`
Blocker bool `index:"8" re:""`
Stored bool `index:"9" re:""`
Weight float64 `index:"10" re:"\d+\.?\d*"`
Thresholds string `index:"11" re:""`
CreatedAt time.Time
}

View File

@@ -741,7 +741,7 @@ func testOnStorITCacheResourceLimit(t *testing.T) {
ActivationInterval: &utils.ActivationInterval{ActivationTime: time.Date(2014, 7, 3, 13, 43, 0, 0, time.UTC).Local()},
ExpiryTime: time.Date(2015, 7, 3, 13, 43, 0, 0, time.UTC).Local(),
Limit: 1,
ActionTriggers: make(ActionTriggers, 0),
Thresholds: []string{"TEST_ACTIONS"},
UsageTTL: time.Duration(1 * time.Millisecond),
Usage: make(map[string]*ResourceUsage),
}
@@ -1681,7 +1681,7 @@ func testOnStorITCRUDResourceLimit(t *testing.T) {
ActivationInterval: &utils.ActivationInterval{ActivationTime: time.Date(2014, 7, 3, 13, 43, 0, 0, time.UTC).Local()},
ExpiryTime: time.Date(2015, 7, 3, 13, 43, 0, 0, time.UTC).Local(),
Limit: 1,
ActionTriggers: make(ActionTriggers, 0),
Thresholds: []string{"TEST_ACTIONS"},
UsageTTL: time.Duration(1 * time.Millisecond),
Usage: make(map[string]*ResourceUsage),
}

View File

@@ -46,7 +46,7 @@ type ResourceLimit struct {
Stored bool
Weight float64 // Weight to sort the ResourceLimits
Limit float64 // Limit value
ActionTriggers ActionTriggers // Thresholds to check after changing Limit
Thresholds []string // Thresholds to check after changing Limit
UsageTTL time.Duration // Expire usage after this duration
AllocationMessage string // message returned by the winning resourceLimit on allocation
Usage map[string]*ResourceUsage // Keep a record of usage, bounded with timestamps so we can expire too long records

View File

@@ -64,28 +64,8 @@ func TestResourceLimitRecordUsage(t *testing.T) {
ExpiryTime: time.Date(2014, 7, 3, 13, 43, 0, 1, time.UTC),
Weight: 100,
Limit: 2,
ActionTriggers: ActionTriggers{
&ActionTrigger{
// ID string // original csv tag
// UniqueID string // individual id
ThresholdType: utils.TRIGGER_MAX_EVENT_COUNTER,
ThresholdValue: 2,
// Recurrent bool // reset excuted flag each run
// MinSleep time.Duration // Minimum duration between two executions in case of recurrent triggers
// ExpirationDate time.Time
// ActivationDate time.Time
Balance: &BalanceFilter{
Type: utils.StringPointer(utils.MONETARY),
Directions: utils.StringMapPointer(utils.NewStringMap(utils.OUT)),
},
ActionsID: "TEST_ACTIONS",
// Weight float64
// ActionsID string
// MinQueuedItems int // Trigger actions only if this number is hit (stats only)
// Executed bool
// LastExecutionTime time.Time
},
},
Thresholds: []string{"TEST_ACTIONS"},
UsageTTL: time.Duration(1 * time.Millisecond),
AllocationMessage: "ALLOC",
Usage: map[string]*ResourceUsage{
@@ -179,29 +159,8 @@ func TestRLSort(t *testing.T) {
ExpiryTime: time.Date(2014, 7, 3, 13, 43, 0, 1, time.UTC),
Weight: 50,
Limit: 2,
ActionTriggers: ActionTriggers{
&ActionTrigger{
// ID string // original csv tag
// UniqueID string // individual id
ThresholdType: utils.TRIGGER_MAX_EVENT_COUNTER,
ThresholdValue: 2,
// Recurrent bool // reset excuted flag each run
// MinSleep time.Duration // Minimum duration between two executions in case of recurrent triggers
// ExpirationDate time.Time
// ActivationDate time.Time
Balance: &BalanceFilter{
Type: utils.StringPointer(utils.MONETARY),
Directions: utils.StringMapPointer(utils.NewStringMap(utils.OUT)),
},
ActionsID: "TEST_ACTIONS",
// Weight float64
// ActionsID string
// MinQueuedItems int // Trigger actions only if this number is hit (stats only)
// Executed bool
// LastExecutionTime time.Time
},
},
UsageTTL: time.Duration(1 * time.Millisecond),
Thresholds: []string{"TEST_ACTIONS"},
UsageTTL: time.Duration(1 * time.Millisecond),
// AllocationMessage: "ALLOC2",
Usage: map[string]*ResourceUsage{
ru2.ID: ru2,

View File

@@ -1428,11 +1428,11 @@ func testStorDBitCRUDTpResourceLimits(t *testing.T) {
//WRITE
var snd = []*utils.TPResourceLimit{
&utils.TPResourceLimit{
TPid: "testTPid",
ID: "testTag1",
Weight: 0.0,
Limit: "test",
ActionTriggerIDs: []string{"1x", "2x"},
TPid: "testTPid",
ID: "testTag1",
Weight: 0.0,
Limit: "test",
Thresholds: []string{"1x", "2x"},
Filters: []*utils.TPRequestFilter{
&utils.TPRequestFilter{
Type: "filtertype",
@@ -1447,7 +1447,7 @@ func testStorDBitCRUDTpResourceLimits(t *testing.T) {
ActivationInterval: &utils.TPActivationInterval{ActivationTime: "test"},
Weight: 0.0,
Limit: "test",
ActionTriggerIDs: []string{"1x", "2x"},
Thresholds: []string{"1x", "2x"},
Filters: []*utils.TPRequestFilter{
&utils.TPRequestFilter{
Type: "filtertype",
@@ -1479,8 +1479,8 @@ func testStorDBitCRUDTpResourceLimits(t *testing.T) {
if !(reflect.DeepEqual(snd[0].Limit, rcv[0].Limit) || reflect.DeepEqual(snd[0].Limit, rcv[1].Limit)) {
t.Errorf("Expecting: %+v, received: %+v || %+v", snd[0].Limit, rcv[0].Limit, rcv[1].Limit)
}
if !(reflect.DeepEqual(snd[0].ActionTriggerIDs, rcv[0].ActionTriggerIDs) || reflect.DeepEqual(snd[0].ActionTriggerIDs, rcv[1].ActionTriggerIDs)) {
t.Errorf("Expecting: %+v, received: %+v || %+v", snd[0].ActionTriggerIDs, rcv[0].ActionTriggerIDs, rcv[1].ActionTriggerIDs)
if !(reflect.DeepEqual(snd[0].Thresholds, rcv[0].Thresholds) || reflect.DeepEqual(snd[0].Thresholds, rcv[1].Thresholds)) {
t.Errorf("Expecting: %+v, received: %+v || %+v", snd[0].Thresholds, rcv[0].Thresholds, rcv[1].Thresholds)
}
for i, _ := range snd[0].Filters {
if !(reflect.DeepEqual(snd[0].Filters[i], rcv[0].Filters[i]) || reflect.DeepEqual(snd[0].Filters[i], rcv[1].Filters[i])) {
@@ -1514,8 +1514,8 @@ func testStorDBitCRUDTpResourceLimits(t *testing.T) {
if !(reflect.DeepEqual(snd[0].Limit, rcv[0].Limit) || reflect.DeepEqual(snd[0].Limit, rcv[1].Limit)) {
t.Errorf("Expecting: %+v, received: %+v || %+v", snd[0].Limit, rcv[0].Limit, rcv[1].Limit)
}
if !(reflect.DeepEqual(snd[0].ActionTriggerIDs, rcv[0].ActionTriggerIDs) || reflect.DeepEqual(snd[0].ActionTriggerIDs, rcv[1].ActionTriggerIDs)) {
t.Errorf("Expecting: %+v, received: %+v || %+v", snd[0].ActionTriggerIDs, rcv[0].ActionTriggerIDs, rcv[1].ActionTriggerIDs)
if !(reflect.DeepEqual(snd[0].Thresholds, rcv[0].Thresholds) || reflect.DeepEqual(snd[0].Thresholds, rcv[1].Thresholds)) {
t.Errorf("Expecting: %+v, received: %+v || %+v", snd[0].Thresholds, rcv[0].Thresholds, rcv[1].Thresholds)
}
}
// REMOVE
@@ -1551,7 +1551,6 @@ func testStorDBitCRUDTpStats(t *testing.T) {
QueueLength: 100,
TTL: "1s",
Metrics: []string{"*asr", "*acd", "*acc"},
Store: true,
Thresholds: []string{"THRESH1", "THRESH2"},
Weight: 20.0,
},

View File

@@ -1274,7 +1274,7 @@ type TPResourceLimit struct {
Blocker bool // blocker flag to stop processing on filters matched
Stored bool
Weight float64 // Weight to sort the ResourceLimits
ActionTriggerIDs []string // Thresholds to check after changing Limit
Thresholds []string // Thresholds to check after changing Limit
}
type TPRequestFilter struct {
@@ -1336,9 +1336,8 @@ type TPStats struct {
QueueLength int
TTL string
Metrics []string
Store bool
Thresholds []string
Blocker bool // blocker flag to stop processing on filters matched
Stored bool
Weight float64
Thresholds []string
}