From e7224a4a39d9c0224e4748d4c0ccf259a0e24973 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Fri, 12 Jun 2015 12:38:23 +0300 Subject: [PATCH] started derived chargers move, tests failing --- engine/calldesc.go | 22 +++++++------- engine/calldesc_test.go | 2 +- engine/destinations_test.go | 16 +++++------ engine/handler_derivedcharging.go | 4 +-- engine/libtest.go | 4 +-- engine/loader_csv_test.go | 6 ++-- engine/ratingplan_test.go | 4 +-- engine/ratingprofile.go | 2 +- engine/responder.go | 4 +-- engine/responder_test.go | 31 ++++++++++---------- engine/stats_test.go | 16 +++++------ engine/storage_interface.go | 8 +++--- engine/storage_map.go | 24 ++++++++-------- engine/storage_redis.go | 46 +++++++++++++++--------------- engine/storage_redis_local_test.go | 2 +- engine/storage_test.go | 36 +++++++++++------------ engine/tp_reader.go | 4 +-- 17 files changed, 116 insertions(+), 115 deletions(-) diff --git a/engine/calldesc.go b/engine/calldesc.go index 25119bed8..d93681b9f 100644 --- a/engine/calldesc.go +++ b/engine/calldesc.go @@ -49,19 +49,19 @@ func init() { Logger.Err(fmt.Sprintf("Could not connect to syslog: %v", err)) } if DEBUG { - dataStorage, _ = NewMapStorage() + ratingStorage, _ = NewMapStorage() accountingStorage, _ = NewMapStorage() } else { - //dataStorage, _ = NewMongoStorage(db_server, "27017", "cgrates_test", "", "") - dataStorage, _ = NewRedisStorage("127.0.0.1:6379", 12, "", utils.MSGPACK) + //ratingStorage, _ = NewMongoStorage(db_server, "27017", "cgrates_test", "", "") + ratingStorage, _ = NewRedisStorage("127.0.0.1:6379", 12, "", utils.MSGPACK) accountingStorage, _ = NewRedisStorage("127.0.0.1:6379", 13, "", utils.MSGPACK) } - storageLogger = dataStorage.(LogStorage) + storageLogger = ratingStorage.(LogStorage) } var ( Logger utils.LoggerInterface - dataStorage RatingStorage + ratingStorage RatingStorage accountingStorage AccountingStorage storageLogger LogStorage cdrStorage CdrStorage @@ -73,7 +73,7 @@ var ( // Exported method to set the storage getter. func SetRatingStorage(sg RatingStorage) { - dataStorage = sg + ratingStorage = sg } func SetAccountingStorage(ag AccountingStorage) { @@ -176,7 +176,7 @@ func (cd *CallDescriptor) getRatingPlansForPrefix(key string, recursionDepth int err = errors.New("Max fallback recursion depth reached!" + key) return } - rpf, err := dataStorage.GetRatingProfile(key, false) + rpf, err := ratingStorage.GetRatingProfile(key, false) if err != nil || rpf == nil { return err } @@ -667,8 +667,8 @@ func (cd *CallDescriptor) RefundIncrements() (left float64, err error) { func (cd *CallDescriptor) FlushCache() (err error) { cache2go.Flush() - dataStorage.CacheRating(nil, nil, nil, nil, nil) - accountingStorage.CacheAccounting(nil, nil, nil, nil) + ratingStorage.CacheRating(nil, nil, nil, nil, nil, nil) + accountingStorage.CacheAccounting(nil, nil, nil) return nil } @@ -719,7 +719,7 @@ func (cd *CallDescriptor) GetLCRFromStorage() (*LCR, error) { utils.LCRKey(utils.ANY, utils.ANY, utils.ANY, utils.ANY, utils.ANY), } for _, key := range keyVariants { - if lcr, err := dataStorage.GetLCR(key, false); err != nil && err.Error() != utils.ERR_NOT_FOUND { + if lcr, err := ratingStorage.GetLCR(key, false); err != nil && err.Error() != utils.ERR_NOT_FOUND { return nil, err } else if err == nil { return lcr, nil @@ -832,7 +832,7 @@ func (cd *CallDescriptor) GetLCR(stats StatsInterface) (*LCRCost, error) { continue } rpfKey := utils.ConcatenatedKey(ratingProfileSearchKey, supplier) - if rpf, err := dataStorage.GetRatingProfile(rpfKey, false); err != nil { + if rpf, err := ratingStorage.GetRatingProfile(rpfKey, false); err != nil { lcrCost.SupplierCosts = append(lcrCost.SupplierCosts, &LCRSupplierCost{ Supplier: supplier, Error: fmt.Sprintf("Rating plan error: %s", err.Error()), diff --git a/engine/calldesc_test.go b/engine/calldesc_test.go index ea26f2a70..362692c87 100644 --- a/engine/calldesc_test.go +++ b/engine/calldesc_test.go @@ -1044,7 +1044,7 @@ func BenchmarkStorageGetting(b *testing.B) { cd := &CallDescriptor{Direction: "*out", Category: "0", Tenant: "vdf", Subject: "rif", Destination: "0256", TimeStart: t1, TimeEnd: t2} b.StartTimer() for i := 0; i < b.N; i++ { - dataStorage.GetRatingProfile(cd.GetKey(cd.Subject), false) + ratingStorage.GetRatingProfile(cd.GetKey(cd.Subject), false) } } diff --git a/engine/destinations_test.go b/engine/destinations_test.go index ca7a43d97..efa6ba208 100644 --- a/engine/destinations_test.go +++ b/engine/destinations_test.go @@ -39,11 +39,11 @@ func TestDestinationStoreRestore(t *testing.T) { func TestDestinationStorageStore(t *testing.T) { nationale := &Destination{Id: "nat", Prefixes: []string{"0257", "0256", "0723"}} - err := dataStorage.SetDestination(nationale) + err := ratingStorage.SetDestination(nationale) if err != nil { t.Error("Error storing destination: ", err) } - result, err := dataStorage.GetDestination(nationale.Id) + result, err := ratingStorage.GetDestination(nationale.Id) if nationale.containsPrefix("0257") == 0 || nationale.containsPrefix("0256") == 0 || nationale.containsPrefix("0723") == 0 { t.Errorf("Expected %q was %q", nationale, result) } @@ -74,28 +74,28 @@ func TestDestinationContainsPrefixWrong(t *testing.T) { } func TestDestinationGetExists(t *testing.T) { - d, err := dataStorage.GetDestination("NAT") + d, err := ratingStorage.GetDestination("NAT") if err != nil || d == nil { t.Error("Could not get destination: ", d) } } func TestDestinationGetExistsCache(t *testing.T) { - dataStorage.GetDestination("NAT") + ratingStorage.GetDestination("NAT") if _, err := cache2go.GetCached(DESTINATION_PREFIX + "0256"); err != nil { t.Error("Destination not cached:", err) } } func TestDestinationGetNotExists(t *testing.T) { - d, err := dataStorage.GetDestination("not existing") + d, err := ratingStorage.GetDestination("not existing") if d != nil { t.Error("Got false destination: ", d, err) } } func TestDestinationGetNotExistsCache(t *testing.T) { - dataStorage.GetDestination("not existing") + ratingStorage.GetDestination("not existing") if d, err := cache2go.GetCached("not existing"); err == nil { t.Error("Bad destination cached: ", d) } @@ -147,7 +147,7 @@ func TestCleanStalePrefixes(t *testing.T) { func BenchmarkDestinationStorageStoreRestore(b *testing.B) { nationale := &Destination{Id: "nat", Prefixes: []string{"0257", "0256", "0723"}} for i := 0; i < b.N; i++ { - dataStorage.SetDestination(nationale) - dataStorage.GetDestination(nationale.Id) + ratingStorage.SetDestination(nationale) + ratingStorage.GetDestination(nationale.Id) } } diff --git a/engine/handler_derivedcharging.go b/engine/handler_derivedcharging.go index a0fc1bcaa..5f0c2b125 100644 --- a/engine/handler_derivedcharging.go +++ b/engine/handler_derivedcharging.go @@ -23,7 +23,7 @@ import ( ) // Handles retrieving of DerivedChargers profile based on longest match from AccountingDb -func HandleGetDerivedChargers(acntStorage AccountingStorage, attrs *utils.AttrDerivedChargers) (utils.DerivedChargers, error) { +func HandleGetDerivedChargers(ratingStorage RatingStorage, attrs *utils.AttrDerivedChargers) (utils.DerivedChargers, error) { var dcs utils.DerivedChargers strictKey := utils.DerivedChargersKey(attrs.Direction, attrs.Tenant, attrs.Category, attrs.Account, attrs.Subject) anySubjKey := utils.DerivedChargersKey(attrs.Direction, attrs.Tenant, attrs.Category, attrs.Account, utils.ANY) @@ -31,7 +31,7 @@ func HandleGetDerivedChargers(acntStorage AccountingStorage, attrs *utils.AttrDe anyCategKey := utils.DerivedChargersKey(attrs.Direction, attrs.Tenant, utils.ANY, utils.ANY, utils.ANY) anyTenantKey := utils.DerivedChargersKey(attrs.Direction, utils.ANY, utils.ANY, utils.ANY, utils.ANY) for _, dcKey := range []string{strictKey, anySubjKey, anyAcntKey, anyCategKey, anyTenantKey} { - if dcsDb, err := acntStorage.GetDerivedChargers(dcKey, false); err != nil && err.Error() != utils.ERR_NOT_FOUND { + if dcsDb, err := ratingStorage.GetDerivedChargers(dcKey, false); err != nil && err.Error() != utils.ERR_NOT_FOUND { return nil, err } else if dcsDb != nil { dcs = dcsDb diff --git a/engine/libtest.go b/engine/libtest.go index 325c82953..029a72aba 100644 --- a/engine/libtest.go +++ b/engine/libtest.go @@ -47,8 +47,8 @@ func InitDataDb(cfg *config.CGRConfig) error { return err } } - ratingDb.CacheRating(nil, nil, nil, nil, nil) - accountDb.CacheAccounting(nil, nil, nil, nil) + ratingDb.CacheRating(nil, nil, nil, nil, nil, nil) + accountDb.CacheAccounting(nil, nil, nil) return nil } diff --git a/engine/loader_csv_test.go b/engine/loader_csv_test.go index 6ee54c262..0d7004168 100644 --- a/engine/loader_csv_test.go +++ b/engine/loader_csv_test.go @@ -211,7 +211,7 @@ CDRST2,,,ACD,,,,,,,,,,,,,,,,,,,, var csvr *TpReader func init() { - csvr = NewTpReader(dataStorage, accountingStorage, NewStringCSVStorage(',', destinations, timings, rates, destinationRates, ratingPlans, ratingProfiles, + csvr = NewTpReader(ratingStorage, accountingStorage, NewStringCSVStorage(',', destinations, timings, rates, destinationRates, ratingPlans, ratingProfiles, sharedGroups, lcrs, actions, actionTimings, actionTriggers, accountActions, derivedCharges, cdrStats), "") if err := csvr.LoadDestinations(); err != nil { log.Print("error in LoadDestinations:", err) @@ -256,8 +256,8 @@ func init() { log.Print("error in LoadCdrStats:", err) } csvr.WriteToDatabase(false, false) - dataStorage.CacheRating(nil, nil, nil, nil, nil) - accountingStorage.CacheAccounting(nil, nil, nil, nil) + ratingStorage.CacheRating(nil, nil, nil, nil, nil, nil) + accountingStorage.CacheAccounting(nil, nil, nil) } func TestLoadDestinations(t *testing.T) { diff --git a/engine/ratingplan_test.go b/engine/ratingplan_test.go index 736c40d0f..93d8500f8 100644 --- a/engine/ratingplan_test.go +++ b/engine/ratingplan_test.go @@ -449,9 +449,9 @@ func BenchmarkRatingPlanRestore(b *testing.B) { EndTime: "15:00:00"}} rp := &RatingPlan{Id: "test"} rp.AddRateInterval("NAT", i) - dataStorage.SetRatingPlan(rp) + ratingStorage.SetRatingPlan(rp) b.ResetTimer() for i := 0; i < b.N; i++ { - dataStorage.GetRatingPlan(rp.Id, true) + ratingStorage.GetRatingPlan(rp.Id, true) } } diff --git a/engine/ratingprofile.go b/engine/ratingprofile.go index 0aad3d0f2..3e00bf61e 100644 --- a/engine/ratingprofile.go +++ b/engine/ratingprofile.go @@ -116,7 +116,7 @@ func (ris RatingInfos) String() string { func (rp *RatingProfile) GetRatingPlansForPrefix(cd *CallDescriptor) (err error) { var ris RatingInfos for index, rpa := range rp.RatingPlanActivations.GetActiveForCall(cd) { - rpl, err := dataStorage.GetRatingPlan(rpa.RatingPlanId, false) + rpl, err := ratingStorage.GetRatingPlan(rpa.RatingPlanId, false) if err != nil || rpl == nil { Logger.Err(fmt.Sprintf("Error checking destination: %v", err)) continue diff --git a/engine/responder.go b/engine/responder.go index ef3cace25..4aadfaec8 100644 --- a/engine/responder.go +++ b/engine/responder.go @@ -217,7 +217,7 @@ func (rs *Responder) GetDerivedChargers(attrs *utils.AttrDerivedChargers, dcs *u if rs.Bal != nil { return errors.New("BALANCER_UNSUPPORTED_METHOD") } - if dcsH, err := HandleGetDerivedChargers(accountingStorage, attrs); err != nil { + if dcsH, err := HandleGetDerivedChargers(ratingStorage, attrs); err != nil { return err } else if dcsH != nil { *dcs = dcsH @@ -285,7 +285,7 @@ func (rs *Responder) Shutdown(arg string, reply *string) (err error) { if rs.Bal != nil { rs.Bal.Shutdown("Responder.Shutdown") } - dataStorage.(Storage).Close() + ratingStorage.(Storage).Close() defer func() { rs.ExitChan <- true }() *reply = "Done!" return diff --git a/engine/responder_test.go b/engine/responder_test.go index eed6f8111..d0919ac7e 100644 --- a/engine/responder_test.go +++ b/engine/responder_test.go @@ -41,10 +41,10 @@ func TestResponderGetDerivedChargers(t *testing.T) { CategoryField: "test", AccountField: "test", SubjectField: "test", DestinationField: "test", SetupTimeField: "test", AnswerTimeField: "test", UsageField: "test"}} rsponder = &Responder{} attrs := &utils.AttrDerivedChargers{Tenant: "cgrates.org", Category: "call", Direction: "*out", Account: "responder_test", Subject: "responder_test"} - if err := accountingStorage.SetDerivedChargers(utils.DerivedChargersKey(utils.OUT, utils.ANY, utils.ANY, utils.ANY, utils.ANY), cfgedDC); err != nil { + if err := ratingStorage.SetDerivedChargers(utils.DerivedChargersKey(utils.OUT, utils.ANY, utils.ANY, utils.ANY, utils.ANY), cfgedDC); err != nil { t.Error(err) } - if err := accountingStorage.CacheAccounting(nil, []string{}, []string{}, []string{}); err != nil { + if err := accountingStorage.CacheAccounting(nil, []string{}, []string{}); err != nil { t.Error(err) } var dcs utils.DerivedChargers @@ -69,7 +69,7 @@ func TestGetDerivedMaxSessionTime(t *testing.T) { t.Error("Unexpected maxSessionTime received: ", maxSessionTime) } deTMobile := &Destination{Id: "DE_TMOBILE", Prefixes: []string{"+49151", "+49160", "+49170", "+49171", "+49175"}} - if err := dataStorage.SetDestination(deTMobile); err != nil { + if err := ratingStorage.SetDestination(deTMobile); err != nil { t.Error(err) } b10 := &Balance{Value: 10, Weight: 10, DestinationIds: "DE_TMOBILE"} @@ -91,11 +91,11 @@ func TestGetDerivedMaxSessionTime(t *testing.T) { &utils.DerivedCharger{RunId: "extra3", ReqTypeField: "^" + utils.META_PSEUDOPREPAID, DirectionField: "*default", TenantField: "*default", CategoryField: "*default", AccountField: "^rif", SubjectField: "^rif", DestinationField: "^+49151708707", SetupTimeField: "*default", AnswerTimeField: "*default", UsageField: "*default"}, } - if err := accountingStorage.SetDerivedChargers(keyCharger1, charger1); err != nil { + if err := ratingStorage.SetDerivedChargers(keyCharger1, charger1); err != nil { t.Error("Error on setting DerivedChargers", err.Error()) } - dataStorage.CacheRating(nil, nil, nil, nil, nil) - accountingStorage.CacheAccounting(nil, nil, nil, nil) + ratingStorage.CacheRating(nil, nil, nil, nil, nil, nil) + accountingStorage.CacheAccounting(nil, nil, nil) if rifStoredAcnt, err := accountingStorage.GetAccount(utils.ConcatenatedKey(utils.OUT, testTenant, "rif")); err != nil { t.Error(err) //} else if rifStoredAcnt.BalanceMap[utils.VOICE+OUTBOUND].Equal(rifsAccount.BalanceMap[utils.VOICE+OUTBOUND]) { @@ -146,10 +146,10 @@ func TestGetSessionRuns(t *testing.T) { SetupTimeField: utils.META_DEFAULT, PddField: utils.META_DEFAULT, AnswerTimeField: utils.META_DEFAULT, UsageField: utils.META_DEFAULT, SupplierField: utils.META_DEFAULT, DisconnectCauseField: utils.META_DEFAULT} charger1 := utils.DerivedChargers{extra1DC, extra2DC, extra3DC} - if err := accountingStorage.SetDerivedChargers(keyCharger1, charger1); err != nil { + if err := ratingStorage.SetDerivedChargers(keyCharger1, charger1); err != nil { t.Error("Error on setting DerivedChargers", err.Error()) } - accountingStorage.CacheAccounting(nil, nil, nil, nil) + accountingStorage.CacheAccounting(nil, nil, nil) sesRuns := make([]*SessionRun, 0) eSRuns := []*SessionRun{ &SessionRun{DerivedCharger: extra1DC, @@ -166,9 +166,9 @@ func TestGetSessionRuns(t *testing.T) { } func TestGetLCR(t *testing.T) { - rsponder.Stats = NewStats(dataStorage) // Load stats instance + rsponder.Stats = NewStats(ratingStorage) // Load stats instance dstDe := &Destination{Id: "GERMANY", Prefixes: []string{"+49"}} - if err := dataStorage.SetDestination(dstDe); err != nil { + if err := ratingStorage.SetDestination(dstDe); err != nil { t.Error(err) } rp1 := &RatingPlan{ @@ -280,7 +280,7 @@ func TestGetLCR(t *testing.T) { }, } for _, rpf := range []*RatingPlan{rp1, rp2, rp3} { - if err := dataStorage.SetRatingPlan(rpf); err != nil { + if err := ratingStorage.SetRatingPlan(rpf); err != nil { t.Error(err) } } @@ -315,7 +315,7 @@ func TestGetLCR(t *testing.T) { }}, } for _, rpfl := range []*RatingProfile{danRpfl, rifRpfl, ivoRpfl} { - if err := dataStorage.SetRatingProfile(rpfl); err != nil { + if err := ratingStorage.SetRatingProfile(rpfl); err != nil { t.Error(err) } } @@ -356,15 +356,16 @@ func TestGetLCR(t *testing.T) { }, } for _, lcr := range []*LCR{lcrStatic, lcrLowestCost, lcrQosThreshold, lcrQos} { - if err := dataStorage.SetLCR(lcr); err != nil { + if err := ratingStorage.SetLCR(lcr); err != nil { t.Error(err) } } - if err := dataStorage.CacheRating([]string{DESTINATION_PREFIX + dstDe.Id}, + if err := ratingStorage.CacheRating([]string{DESTINATION_PREFIX + dstDe.Id}, []string{RATING_PLAN_PREFIX + rp1.Id, RATING_PLAN_PREFIX + rp2.Id}, []string{RATING_PROFILE_PREFIX + danRpfl.Id, RATING_PROFILE_PREFIX + rifRpfl.Id}, []string{}, - []string{LCR_PREFIX + lcrStatic.GetId(), LCR_PREFIX + lcrLowestCost.GetId()}); err != nil { + []string{LCR_PREFIX + lcrStatic.GetId(), LCR_PREFIX + lcrLowestCost.GetId()}, + []string{}); err != nil { t.Error(err) } cdStatic := &CallDescriptor{ diff --git a/engine/stats_test.go b/engine/stats_test.go index 6b07dd1be..90937ad7f 100644 --- a/engine/stats_test.go +++ b/engine/stats_test.go @@ -203,7 +203,7 @@ func TestAcceptCdr(t *testing.T) { } func TestStatsQueueIds(t *testing.T) { - cdrStats := NewStats(dataStorage) + cdrStats := NewStats(ratingStorage) ids := []string{} if err := cdrStats.GetQueueIds(0, &ids); err != nil { t.Error("Errorf getting queue ids: ", err) @@ -216,7 +216,7 @@ func TestStatsQueueIds(t *testing.T) { } func TestStatsAppendCdr(t *testing.T) { - cdrStats := NewStats(dataStorage) + cdrStats := NewStats(ratingStorage) cdr := &StoredCdr{ Tenant: "cgrates.org", Category: "call", @@ -238,7 +238,7 @@ func TestStatsAppendCdr(t *testing.T) { } func TestStatsGetValues(t *testing.T) { - cdrStats := NewStats(dataStorage) + cdrStats := NewStats(ratingStorage) cdr := &StoredCdr{ Tenant: "cgrates.org", Category: "call", @@ -267,7 +267,7 @@ func TestStatsGetValues(t *testing.T) { } func TestStatsReloadQueues(t *testing.T) { - cdrStats := NewStats(dataStorage) + cdrStats := NewStats(ratingStorage) cdr := &StoredCdr{ Tenant: "cgrates.org", Category: "call", @@ -299,7 +299,7 @@ func TestStatsReloadQueues(t *testing.T) { } func TestStatsReloadQueuesWithDefault(t *testing.T) { - cdrStats := NewStats(dataStorage) + cdrStats := NewStats(ratingStorage) cdrStats.AddQueue(&CdrStats{ Id: utils.META_DEFAULT, }, nil) @@ -335,7 +335,7 @@ func TestStatsReloadQueuesWithDefault(t *testing.T) { } func TestStatsReloadQueuesWithIds(t *testing.T) { - cdrStats := NewStats(dataStorage) + cdrStats := NewStats(ratingStorage) cdr := &StoredCdr{ Tenant: "cgrates.org", Category: "call", @@ -367,7 +367,7 @@ func TestStatsReloadQueuesWithIds(t *testing.T) { } func TestStatsResetQueues(t *testing.T) { - cdrStats := NewStats(dataStorage) + cdrStats := NewStats(ratingStorage) cdr := &StoredCdr{ Tenant: "cgrates.org", Category: "call", @@ -399,7 +399,7 @@ func TestStatsResetQueues(t *testing.T) { } func TestStatsResetQueuesWithIds(t *testing.T) { - cdrStats := NewStats(dataStorage) + cdrStats := NewStats(ratingStorage) cdr := &StoredCdr{ Tenant: "cgrates.org", Category: "call", diff --git a/engine/storage_interface.go b/engine/storage_interface.go index d1d6994e7..2df0e233e 100644 --- a/engine/storage_interface.go +++ b/engine/storage_interface.go @@ -70,7 +70,7 @@ type Storage interface { // Interface for storage providers. type RatingStorage interface { Storage - CacheRating([]string, []string, []string, []string, []string) error + CacheRating([]string, []string, []string, []string, []string, []string) error HasData(string, string) (bool, error) GetRatingPlan(string, bool) (*RatingPlan, error) SetRatingPlan(*RatingPlan) error @@ -87,12 +87,14 @@ type RatingStorage interface { SetCdrStats(*CdrStats) error GetCdrStats(string) (*CdrStats, error) GetAllCdrStats() ([]*CdrStats, error) + GetDerivedChargers(string, bool) (utils.DerivedChargers, error) + SetDerivedChargers(string, utils.DerivedChargers) error } type AccountingStorage interface { Storage HasData(string, string) (bool, error) - CacheAccounting([]string, []string, []string, []string) error + CacheAccounting([]string, []string, []string) error GetActions(string, bool) (Actions, error) SetActions(string, Actions) error GetSharedGroup(string, bool) (*SharedGroup, error) @@ -106,8 +108,6 @@ type AccountingStorage interface { GetActionPlans(string) (ActionPlans, error) SetActionPlans(string, ActionPlans) error GetAllActionPlans() (map[string]ActionPlans, error) - GetDerivedChargers(string, bool) (utils.DerivedChargers, error) - SetDerivedChargers(string, utils.DerivedChargers) error } type CdrStorage interface { diff --git a/engine/storage_map.go b/engine/storage_map.go index f31357eeb..27598638a 100644 --- a/engine/storage_map.go +++ b/engine/storage_map.go @@ -62,7 +62,7 @@ func (ms *MapStorage) GetKeysForPrefix(prefix string) ([]string, error) { return keysForPrefix, nil } -func (ms *MapStorage) CacheRating(dKeys, rpKeys, rpfKeys, alsKeys, lcrKeys []string) error { +func (ms *MapStorage) CacheRating(dKeys, rpKeys, rpfKeys, alsKeys, lcrKeys, dcsKeys []string) error { cache2go.BeginTransaction() if dKeys == nil || (float64(cache2go.CountEntries(DESTINATION_PREFIX))*DESTINATIONS_LOAD_THRESHOLD < float64(len(dKeys))) { cache2go.RemPrefixKey(DESTINATION_PREFIX) @@ -81,6 +81,9 @@ func (ms *MapStorage) CacheRating(dKeys, rpKeys, rpfKeys, alsKeys, lcrKeys []str if lcrKeys == nil { cache2go.RemPrefixKey(LCR_PREFIX) } + if dcsKeys == nil { + cache2go.RemPrefixKey(DERIVEDCHARGERS_PREFIX) + } for k, _ := range ms.dict { if strings.HasPrefix(k, DESTINATION_PREFIX) { if _, err := ms.GetDestination(k[len(DESTINATION_PREFIX):]); err != nil { @@ -116,12 +119,19 @@ func (ms *MapStorage) CacheRating(dKeys, rpKeys, rpfKeys, alsKeys, lcrKeys []str return err } } + if strings.HasPrefix(k, DERIVEDCHARGERS_PREFIX) { + cache2go.RemKey(k) + if _, err := ms.GetDerivedChargers(k[len(DERIVEDCHARGERS_PREFIX):], true); err != nil { + cache2go.RollbackTransaction() + return err + } + } } cache2go.CommitTransaction() return nil } -func (ms *MapStorage) CacheAccounting(actKeys, shgKeys, alsKeys, dcsKeys []string) error { +func (ms *MapStorage) CacheAccounting(actKeys, shgKeys, alsKeys []string) error { cache2go.BeginTransaction() if actKeys == nil { cache2go.RemPrefixKey(ACTION_PREFIX) // Forced until we can fine tune it @@ -132,9 +142,6 @@ func (ms *MapStorage) CacheAccounting(actKeys, shgKeys, alsKeys, dcsKeys []strin if alsKeys == nil { cache2go.RemPrefixKey(ACC_ALIAS_PREFIX) } - if dcsKeys == nil { - cache2go.RemPrefixKey(DERIVEDCHARGERS_PREFIX) - } for k, _ := range ms.dict { if strings.HasPrefix(k, ACTION_PREFIX) { cache2go.RemKey(k) @@ -157,13 +164,6 @@ func (ms *MapStorage) CacheAccounting(actKeys, shgKeys, alsKeys, dcsKeys []strin return err } } - if strings.HasPrefix(k, DERIVEDCHARGERS_PREFIX) { - cache2go.RemKey(k) - if _, err := ms.GetDerivedChargers(k[len(DERIVEDCHARGERS_PREFIX):], true); err != nil { - cache2go.RollbackTransaction() - return err - } - } } cache2go.CommitTransaction() return nil diff --git a/engine/storage_redis.go b/engine/storage_redis.go index 76cc03986..96dadb77c 100644 --- a/engine/storage_redis.go +++ b/engine/storage_redis.go @@ -71,7 +71,7 @@ func (rs *RedisStorage) GetKeysForPrefix(prefix string) ([]string, error) { return rs.db.Keys(prefix + "*") } -func (rs *RedisStorage) CacheRating(dKeys, rpKeys, rpfKeys, alsKeys, lcrKeys []string) (err error) { +func (rs *RedisStorage) CacheRating(dKeys, rpKeys, rpfKeys, alsKeys, lcrKeys, dcsKeys []string) (err error) { cache2go.BeginTransaction() if dKeys == nil || (float64(cache2go.CountEntries(DESTINATION_PREFIX))*DESTINATIONS_LOAD_THRESHOLD < float64(len(dKeys))) { // if need to load more than a half of exiting keys load them all @@ -178,11 +178,32 @@ func (rs *RedisStorage) CacheRating(dKeys, rpKeys, rpfKeys, alsKeys, lcrKeys []s if len(alsKeys) != 0 { Logger.Info("Finished rating profile aliases caching.") } + // DerivedChargers caching + if dcsKeys == nil { + Logger.Info("Caching all derived chargers") + if dcsKeys, err = rs.db.Keys(DERIVEDCHARGERS_PREFIX + "*"); err != nil { + cache2go.RollbackTransaction() + return err + } + cache2go.RemPrefixKey(DERIVEDCHARGERS_PREFIX) + } else if len(dcsKeys) != 0 { + Logger.Info(fmt.Sprintf("Caching derived chargers: %v", dcsKeys)) + } + for _, key := range dcsKeys { + cache2go.RemKey(key) + if _, err = rs.GetDerivedChargers(key[len(DERIVEDCHARGERS_PREFIX):], true); err != nil { + cache2go.RollbackTransaction() + return err + } + } + if len(dcsKeys) != 0 { + Logger.Info("Finished derived chargers caching.") + } cache2go.CommitTransaction() return nil } -func (rs *RedisStorage) CacheAccounting(actKeys, shgKeys, alsKeys, dcsKeys []string) (err error) { +func (rs *RedisStorage) CacheAccounting(actKeys, shgKeys, alsKeys []string) (err error) { cache2go.BeginTransaction() if actKeys == nil { cache2go.RemPrefixKey(ACTION_PREFIX) @@ -248,27 +269,6 @@ func (rs *RedisStorage) CacheAccounting(actKeys, shgKeys, alsKeys, dcsKeys []str if len(alsKeys) != 0 { Logger.Info("Finished account aliases caching.") } - // DerivedChargers caching - if dcsKeys == nil { - Logger.Info("Caching all derived chargers") - if dcsKeys, err = rs.db.Keys(DERIVEDCHARGERS_PREFIX + "*"); err != nil { - cache2go.RollbackTransaction() - return err - } - cache2go.RemPrefixKey(DERIVEDCHARGERS_PREFIX) - } else if len(dcsKeys) != 0 { - Logger.Info(fmt.Sprintf("Caching derived chargers: %v", dcsKeys)) - } - for _, key := range dcsKeys { - cache2go.RemKey(key) - if _, err = rs.GetDerivedChargers(key[len(DERIVEDCHARGERS_PREFIX):], true); err != nil { - cache2go.RollbackTransaction() - return err - } - } - if len(dcsKeys) != 0 { - Logger.Info("Finished derived chargers caching.") - } cache2go.CommitTransaction() return nil } diff --git a/engine/storage_redis_local_test.go b/engine/storage_redis_local_test.go index 0b562d6a9..9c93b7841 100644 --- a/engine/storage_redis_local_test.go +++ b/engine/storage_redis_local_test.go @@ -48,7 +48,7 @@ func TestFlush(t *testing.T) { if err := rds.Flush(""); err != nil { t.Error("Failed to Flush redis database", err.Error()) } - rds.CacheAccounting(nil, nil, nil, nil) + rds.CacheAccounting(nil, nil, nil) } func TestSetGetDerivedCharges(t *testing.T) { diff --git a/engine/storage_test.go b/engine/storage_test.go index 977452773..068925d58 100644 --- a/engine/storage_test.go +++ b/engine/storage_test.go @@ -77,7 +77,7 @@ func TestMsgpackTime(t *testing.T) { } func TestStorageDestinationContainsPrefixShort(t *testing.T) { - dest, err := dataStorage.GetDestination("NAT") + dest, err := ratingStorage.GetDestination("NAT") precision := dest.containsPrefix("0723") if err != nil || precision != 4 { t.Error("Error finding prefix: ", err, precision) @@ -85,7 +85,7 @@ func TestStorageDestinationContainsPrefixShort(t *testing.T) { } func TestStorageDestinationContainsPrefixLong(t *testing.T) { - dest, err := dataStorage.GetDestination("NAT") + dest, err := ratingStorage.GetDestination("NAT") precision := dest.containsPrefix("0723045326") if err != nil || precision != 4 { t.Error("Error finding prefix: ", err, precision) @@ -93,7 +93,7 @@ func TestStorageDestinationContainsPrefixLong(t *testing.T) { } func TestStorageDestinationContainsPrefixNotExisting(t *testing.T) { - dest, err := dataStorage.GetDestination("NAT") + dest, err := ratingStorage.GetDestination("NAT") precision := dest.containsPrefix("072") if err != nil || precision != 0 { t.Error("Error finding prefix: ", err, precision) @@ -101,12 +101,12 @@ func TestStorageDestinationContainsPrefixNotExisting(t *testing.T) { } func TestCacheRefresh(t *testing.T) { - dataStorage.SetDestination(&Destination{"T11", []string{"0"}}) - dataStorage.GetDestination("T11") - dataStorage.SetDestination(&Destination{"T11", []string{"1"}}) + ratingStorage.SetDestination(&Destination{"T11", []string{"0"}}) + ratingStorage.GetDestination("T11") + ratingStorage.SetDestination(&Destination{"T11", []string{"1"}}) t.Log("Test cache refresh") - dataStorage.CacheRating(nil, nil, nil, nil, nil) - d, err := dataStorage.GetDestination("T11") + ratingStorage.CacheRating(nil, nil, nil, nil, nil, nil) + d, err := ratingStorage.GetDestination("T11") p := d.containsPrefix("1") if err != nil || p == 0 { t.Error("Error refreshing cache:", d) @@ -130,18 +130,18 @@ func TestStoreInterfaces(t *testing.T) { } func TestGetRPAliases(t *testing.T) { - if err := dataStorage.SetRpAlias(utils.RatingSubjectAliasKey("cgrates.org", "2001"), "1001"); err != nil { + if err := ratingStorage.SetRpAlias(utils.RatingSubjectAliasKey("cgrates.org", "2001"), "1001"); err != nil { t.Error(err) } - if err := dataStorage.SetRpAlias(utils.RatingSubjectAliasKey("cgrates.org", "2002"), "1001"); err != nil { + if err := ratingStorage.SetRpAlias(utils.RatingSubjectAliasKey("cgrates.org", "2002"), "1001"); err != nil { t.Error(err) } - if err := dataStorage.SetRpAlias(utils.RatingSubjectAliasKey("itsyscom.com", "2003"), "1001"); err != nil { + if err := ratingStorage.SetRpAlias(utils.RatingSubjectAliasKey("itsyscom.com", "2003"), "1001"); err != nil { t.Error(err) } expectAliases := sort.StringSlice([]string{"2001", "2002"}) expectAliases.Sort() - if aliases, err := dataStorage.GetRPAliases("cgrates.org", "1001", true); err != nil { + if aliases, err := ratingStorage.GetRPAliases("cgrates.org", "1001", true); err != nil { t.Error(err) } else { aliases := sort.StringSlice(aliases) @@ -153,24 +153,24 @@ func TestGetRPAliases(t *testing.T) { } func TestRemRSubjAliases(t *testing.T) { - if err := dataStorage.SetRpAlias(utils.RatingSubjectAliasKey("cgrates.org", "2001"), "1001"); err != nil { + if err := ratingStorage.SetRpAlias(utils.RatingSubjectAliasKey("cgrates.org", "2001"), "1001"); err != nil { t.Error(err) } - if err := dataStorage.SetRpAlias(utils.RatingSubjectAliasKey("cgrates.org", "2002"), "1001"); err != nil { + if err := ratingStorage.SetRpAlias(utils.RatingSubjectAliasKey("cgrates.org", "2002"), "1001"); err != nil { t.Error(err) } - if err := dataStorage.SetRpAlias(utils.RatingSubjectAliasKey("itsyscom.com", "2003"), "1001"); err != nil { + if err := ratingStorage.SetRpAlias(utils.RatingSubjectAliasKey("itsyscom.com", "2003"), "1001"); err != nil { t.Error(err) } - if err := dataStorage.RemoveRpAliases([]*TenantRatingSubject{&TenantRatingSubject{Tenant: "cgrates.org", Subject: "1001"}}); err != nil { + if err := ratingStorage.RemoveRpAliases([]*TenantRatingSubject{&TenantRatingSubject{Tenant: "cgrates.org", Subject: "1001"}}); err != nil { t.Error(err) } - if cgrAliases, err := dataStorage.GetRPAliases("cgrates.org", "1001", true); err != nil { + if cgrAliases, err := ratingStorage.GetRPAliases("cgrates.org", "1001", true); err != nil { t.Error(err) } else if len(cgrAliases) != 0 { t.Error("Subject aliases not removed: ", cgrAliases) } - if iscAliases, err := dataStorage.GetRPAliases("itsyscom.com", "1001", true); err != nil { // Make sure the aliases were removed at tenant level + if iscAliases, err := ratingStorage.GetRPAliases("itsyscom.com", "1001", true); err != nil { // Make sure the aliases were removed at tenant level t.Error(err) } else if !reflect.DeepEqual(iscAliases, []string{"2003"}) { t.Errorf("Unexpected aliases: %v", iscAliases) diff --git a/engine/tp_reader.go b/engine/tp_reader.go index 3c5d5f06c..5186de80d 100644 --- a/engine/tp_reader.go +++ b/engine/tp_reader.go @@ -890,7 +890,7 @@ func (tpr *TpReader) LoadDerivedChargersFiltered(filter *TpDerivedCharger, save } if save { for dcsKey, dcs := range tpr.derivedChargers { - if err := tpr.accountingStorage.SetDerivedChargers(dcsKey, dcs); err != nil { + if err := tpr.ratingStorage.SetDerivedChargers(dcsKey, dcs); err != nil { return err } } @@ -1145,7 +1145,7 @@ func (tpr *TpReader) WriteToDatabase(flush, verbose bool) (err error) { log.Print("Derived Chargers:") } for key, dcs := range tpr.derivedChargers { - err = tpr.accountingStorage.SetDerivedChargers(key, dcs) + err = tpr.ratingStorage.SetDerivedChargers(key, dcs) if err != nil { return err }