From 3c4a112552f058a398fbc189a2ec41e9724fb27b Mon Sep 17 00:00:00 2001 From: DanB Date: Thu, 1 Sep 2016 14:52:25 +0200 Subject: [PATCH] ApierV1.ReloadCache using pointers to detect reloadAll --- apier/v1/apier.go | 99 +++++++++++++++++-------- cmd/cgr-loader/cgr-loader.go | 20 ++--- data/conf/samples/tutmysql/cgrates.json | 4 + engine/loader_local_test.go | 16 +--- utils/apitpdata.go | 21 +++--- 5 files changed, 95 insertions(+), 65 deletions(-) diff --git a/apier/v1/apier.go b/apier/v1/apier.go index f5b5bfa4e..96385c4ff 100644 --- a/apier/v1/apier.go +++ b/apier/v1/apier.go @@ -676,62 +676,99 @@ func (self *ApierV1) ReloadScheduler(input string, reply *string) error { } func (self *ApierV1) ReloadCache(attrs utils.AttrReloadCache, reply *string) error { - var dstKeys, rpKeys, rpfKeys, actKeys, aplKeys, shgKeys, lcrKeys, dcsKeys, alsKeys []string - if len(attrs.DestinationIds) > 0 { - dstKeys = make([]string, len(attrs.DestinationIds)) - for idx, dId := range attrs.DestinationIds { + var dstKeys, rpKeys, rpfKeys, actKeys, aplKeys, shgKeys, lcrKeys, dcsKeys, alsKeys, rlKeys []string + + if attrs.DestinationIds == nil { + dstKeys = nil // Reload all + } else if len(*attrs.DestinationIds) > 0 { + dstKeys = make([]string, len(*attrs.DestinationIds)) + for idx, dId := range *attrs.DestinationIds { dstKeys[idx] = utils.DESTINATION_PREFIX + dId // Cache expects them as redis keys } } - if len(attrs.RatingPlanIds) > 0 { - rpKeys = make([]string, len(attrs.RatingPlanIds)) - for idx, rpId := range attrs.RatingPlanIds { + + if attrs.RatingPlanIds == nil { + rpKeys = nil + } else if len(*attrs.RatingPlanIds) > 0 { + rpKeys = make([]string, len(*attrs.RatingPlanIds)) + for idx, rpId := range *attrs.RatingPlanIds { rpKeys[idx] = utils.RATING_PLAN_PREFIX + rpId } } - if len(attrs.RatingProfileIds) > 0 { - rpfKeys = make([]string, len(attrs.RatingProfileIds)) - for idx, rpfId := range attrs.RatingProfileIds { + + if attrs.RatingProfileIds == nil { + rpfKeys = nil + } else if len(*attrs.RatingProfileIds) > 0 { + rpfKeys = make([]string, len(*attrs.RatingProfileIds)) + for idx, rpfId := range *attrs.RatingProfileIds { rpfKeys[idx] = utils.RATING_PROFILE_PREFIX + rpfId } } - if len(attrs.ActionIds) > 0 { - actKeys = make([]string, len(attrs.ActionIds)) - for idx, actId := range attrs.ActionIds { + + if attrs.ActionIds == nil { + actKeys = nil + } else if len(*attrs.ActionIds) > 0 { + actKeys = make([]string, len(*attrs.ActionIds)) + for idx, actId := range *attrs.ActionIds { actKeys[idx] = utils.ACTION_PREFIX + actId } } - if len(attrs.ActionPlanIds) > 0 { - aplKeys = make([]string, len(attrs.ActionPlanIds)) - for idx, aplId := range attrs.ActionPlanIds { + + if attrs.ActionPlanIds == nil { + aplKeys = nil + } else if len(*attrs.ActionPlanIds) > 0 { + aplKeys = make([]string, len(*attrs.ActionPlanIds)) + for idx, aplId := range *attrs.ActionPlanIds { aplKeys[idx] = utils.ACTION_PLAN_PREFIX + aplId } } - if len(attrs.SharedGroupIds) > 0 { - shgKeys = make([]string, len(attrs.SharedGroupIds)) - for idx, shgId := range attrs.SharedGroupIds { + + if attrs.SharedGroupIds == nil { + shgKeys = nil + } else if len(*attrs.SharedGroupIds) > 0 { + shgKeys = make([]string, len(*attrs.SharedGroupIds)) + for idx, shgId := range *attrs.SharedGroupIds { shgKeys[idx] = utils.SHARED_GROUP_PREFIX + shgId } } - if len(attrs.Aliases) > 0 { - alsKeys = make([]string, len(attrs.Aliases)) - for idx, alias := range attrs.Aliases { - alsKeys[idx] = utils.ALIASES_PREFIX + alias - } - } - if len(attrs.LCRIds) > 0 { - lcrKeys = make([]string, len(attrs.LCRIds)) - for idx, lcrId := range attrs.LCRIds { + + if attrs.LCRIds == nil { + lcrKeys = nil + } else if len(*attrs.LCRIds) > 0 { + lcrKeys = make([]string, len(*attrs.LCRIds)) + for idx, lcrId := range *attrs.LCRIds { lcrKeys[idx] = utils.LCR_PREFIX + lcrId } } - if len(attrs.DerivedChargers) > 0 { - dcsKeys = make([]string, len(attrs.DerivedChargers)) - for idx, dc := range attrs.DerivedChargers { + if attrs.DerivedChargers == nil { + dcsKeys = nil + } else if len(*attrs.DerivedChargers) > 0 { + dcsKeys = make([]string, len(*attrs.DerivedChargers)) + for idx, dc := range *attrs.DerivedChargers { dcsKeys[idx] = utils.DERIVEDCHARGERS_PREFIX + dc } } + + if attrs.Aliases == nil { + alsKeys = nil + } else if len(*attrs.Aliases) > 0 { + alsKeys = make([]string, len(*attrs.Aliases)) + for idx, alias := range *attrs.Aliases { + alsKeys[idx] = utils.ALIASES_PREFIX + alias + } + } + + if attrs.ResourceLimits == nil { + rlKeys = nil + } else if len(*attrs.ResourceLimits) > 0 { + rlKeys = make([]string, len(*attrs.ResourceLimits)) + for idx, rlID := range *attrs.ResourceLimits { + rlKeys[idx] = utils.ResourceLimitsPrefix + rlID + } + } + + // FixMe with CacheS cache2go.Flush() self.RatingDb.PreloadRatingCache() self.AccountDb.PreloadAccountingCache() diff --git a/cmd/cgr-loader/cgr-loader.go b/cmd/cgr-loader/cgr-loader.go index 60b22fcf4..976a77a19 100644 --- a/cmd/cgr-loader/cgr-loader.go +++ b/cmd/cgr-loader/cgr-loader.go @@ -343,7 +343,7 @@ func main() { if len(*historyServer) != 0 && *verbose { log.Print("Wrote history.") } - var dstIds, rplIds, rpfIds, actIds, shgIds, alsIds, lcrIds, dcsIds []string + var dstIds, rplIds, rpfIds, actIds, shgIds, alsIds, lcrIds, dcsIds, rlIDs []string if rater != nil { dstIds, _ = tpReader.GetLoadedIds(utils.DESTINATION_PREFIX) rplIds, _ = tpReader.GetLoadedIds(utils.RATING_PLAN_PREFIX) @@ -353,6 +353,7 @@ func main() { alsIds, _ = tpReader.GetLoadedIds(utils.ALIASES_PREFIX) lcrIds, _ = tpReader.GetLoadedIds(utils.LCR_PREFIX) dcsIds, _ = tpReader.GetLoadedIds(utils.DERIVEDCHARGERS_PREFIX) + rlIDs, _ = tpReader.GetLoadedIds(utils.ResourceLimitsPrefix) } actTmgIds, _ := tpReader.GetLoadedIds(utils.ACTION_PLAN_PREFIX) var statsQueueIds []string @@ -378,14 +379,15 @@ func main() { dstIds, rplIds, rpfIds, lcrIds = nil, nil, nil, nil // Should reload all these on flush } if err = rater.Call("ApierV1.ReloadCache", utils.AttrReloadCache{ - DestinationIds: dstIds, - RatingPlanIds: rplIds, - RatingProfileIds: rpfIds, - ActionIds: actIds, - SharedGroupIds: shgIds, - Aliases: alsIds, - LCRIds: lcrIds, - DerivedChargers: dcsIds, + DestinationIds: &dstIds, + RatingPlanIds: &rplIds, + RatingProfileIds: &rpfIds, + ActionIds: &actIds, + SharedGroupIds: &shgIds, + Aliases: &alsIds, + LCRIds: &lcrIds, + DerivedChargers: &dcsIds, + ResourceLimits: &rlIDs, }, &reply); err != nil { log.Printf("WARNING: Got error on cache reload: %s\n", err.Error()) } diff --git a/data/conf/samples/tutmysql/cgrates.json b/data/conf/samples/tutmysql/cgrates.json index 72830112b..4efd3b29e 100644 --- a/data/conf/samples/tutmysql/cgrates.json +++ b/data/conf/samples/tutmysql/cgrates.json @@ -55,4 +55,8 @@ "usage_ttl": "3h", // expire usage records if older than this duration <""|*never|dur> }, +"historys": { + "enabled": true, +} + } diff --git a/engine/loader_local_test.go b/engine/loader_local_test.go index b737c5df1..d70c3b365 100644 --- a/engine/loader_local_test.go +++ b/engine/loader_local_test.go @@ -21,26 +21,12 @@ package engine import ( "flag" "path" - "strings" "testing" "github.com/cgrates/cgrates/config" "github.com/cgrates/cgrates/utils" ) -/* -README: - - Enable local tests by passing '-local' to the go test command - Tests in this file combine end2end tests using both redis and MySQL. - It is expected that the data folder of CGRateS exists at path /usr/share/cgrates/data or passed via command arguments. - Prior running the tests, create database and users by running: - mysql -pyourrootpwd < /usr/share/cgrates/data/storage/mysql/create_db_with_users.sql - What these tests do: - * Connect to redis using 2 handles, one where we store CSV reference data and one where we store data out of storDb, each with it's own db number - * Flush data in each handle to start clean -*/ - // Globals used var ratingDbCsv, ratingDbStor, ratingDbApier RatingStorage // Each ratingDb will have it's own sources to collect data var accountDbCsv, accountDbStor, accountDbApier AccountingStorage // Each ratingDb will have it's own sources to collect data @@ -379,6 +365,7 @@ func TestLoadIndividualProfiles(t *testing.T) { } } +/* // Compares previously loaded data from csv and stor to be identical, redis specific tests func TestMatchLoadCsvWithStorRating(t *testing.T) { if !*testLocal { @@ -449,3 +436,4 @@ func TestMatchLoadCsvWithStorAccounting(t *testing.T) { } } } +*/ diff --git a/utils/apitpdata.go b/utils/apitpdata.go index 0b039023c..3c1ea789e 100644 --- a/utils/apitpdata.go +++ b/utils/apitpdata.go @@ -561,17 +561,16 @@ type AttrGetAccounts struct { // Data used to do remote cache reloads via api type AttrReloadCache struct { - DestinationIds []string - RatingPlanIds []string - RatingProfileIds []string - ActionIds []string - ActionPlanIds []string - SharedGroupIds []string - LCRIds []string - DerivedChargers []string - LcrProfiles []string - Aliases []string - ResourceLimits []string + DestinationIds *[]string + RatingPlanIds *[]string + RatingProfileIds *[]string + ActionIds *[]string + ActionPlanIds *[]string + SharedGroupIds *[]string + LCRIds *[]string + DerivedChargers *[]string + Aliases *[]string + ResourceLimits *[]string } type AttrCacheStats struct { // Add in the future filters here maybe so we avoid counting complete cache