Adding ReverseDestinations and RatingPlan to CacheDataFromDB, remove automatic caching for SetRatingPlan

This commit is contained in:
DanB
2016-11-29 18:33:21 +01:00
parent 9072d1c679
commit c1e4d46c9c
3 changed files with 116 additions and 20 deletions

View File

@@ -24,6 +24,7 @@ import (
"path"
"reflect"
"testing"
"time"
"github.com/cgrates/cgrates/cache"
"github.com/cgrates/cgrates/config"
@@ -43,7 +44,9 @@ var sTestsOnStorIT = []func(t *testing.T){
testOnStorITSetReqFilterIndexes,
testOnStorITGetReqFilterIndexes,
testOnStorITMatchReqFilterIndex,
testOnStorITCacheDataFromDB,
testOnStorITCacheDestinations,
testOnStorITCacheReverseDestinations,
testOnStorITCacheRatingPlan,
}
func TestOnStorITRedisConnect(t *testing.T) {
@@ -82,8 +85,9 @@ func TestOnStorITMongo(t *testing.T) {
func testOnStorITFlush(t *testing.T) {
if err := onStor.Flush(""); err != nil {
t.Error("Failed to Flush redis database", err.Error())
t.Error(err)
}
cache.Flush()
}
func testOnStorITSetGetDerivedCharges(t *testing.T) {
@@ -204,9 +208,12 @@ func testOnStorITMatchReqFilterIndex(t *testing.T) {
}
}
func testOnStorITCacheDataFromDB(t *testing.T) {
func testOnStorITCacheDestinations(t *testing.T) {
if err := onStor.CacheDataFromDB("INVALID", nil, false); err == nil || err.Error() != utils.UnsupportedCachePrefix {
t.Error(err)
}
dst := &Destination{Id: "TEST_CACHE", Prefixes: []string{"+491", "+492", "+493"}}
if err := onStor.SetDestination(dst, ""); err != nil {
if err := onStor.SetDestination(dst, utils.NonTransactional); err != nil {
t.Error(err)
}
if _, hasIt := cache.Get(utils.DESTINATION_PREFIX + dst.Id); hasIt {
@@ -227,3 +234,78 @@ func testOnStorITCacheDataFromDB(t *testing.T) {
t.Error("Wrong item in the cache")
}
}
func testOnStorITCacheReverseDestinations(t *testing.T) {
dst := &Destination{Id: "TEST_CACHE", Prefixes: []string{"+491", "+492", "+493"}}
if err := onStor.SetReverseDestination(dst, utils.NonTransactional); err != nil {
t.Error(err)
}
for _, prfx := range dst.Prefixes {
if _, hasIt := cache.Get(utils.REVERSE_DESTINATION_PREFIX + dst.Id); hasIt {
t.Errorf("Prefix: %s already in cache", prfx)
}
}
if err := onStor.CacheDataFromDB(utils.REVERSE_DESTINATION_PREFIX, dst.Prefixes, false); err != nil {
t.Error(err)
}
for _, prfx := range dst.Prefixes {
if itm, hasIt := cache.Get(utils.REVERSE_DESTINATION_PREFIX + prfx); !hasIt {
t.Error("Did not cache")
} else if !reflect.DeepEqual([]string{dst.Id}, itm.([]string)) {
t.Error("Wrong item in the cache")
}
}
}
func testOnStorITCacheRatingPlan(t *testing.T) {
rp := &RatingPlan{
Id: "TEST_RP_CACHE",
Timings: map[string]*RITiming{
"59a981b9": &RITiming{
Years: utils.Years{},
Months: utils.Months{},
MonthDays: utils.MonthDays{},
WeekDays: utils.WeekDays{1, 2, 3, 4, 5},
StartTime: "00:00:00",
},
},
Ratings: map[string]*RIRate{
"ebefae11": &RIRate{
ConnectFee: 0,
Rates: []*Rate{
&Rate{
GroupIntervalStart: 0,
Value: 0.2,
RateIncrement: time.Second,
RateUnit: time.Minute,
},
},
RoundingMethod: utils.ROUNDING_MIDDLE,
RoundingDecimals: 4,
},
},
DestinationRates: map[string]RPRateList{
"GERMANY": []*RPRate{
&RPRate{
Timing: "59a981b9",
Rating: "ebefae11",
Weight: 10,
},
},
},
}
if err := onStor.SetRatingPlan(rp, utils.NonTransactional); err != nil {
t.Error(err)
}
if _, hasIt := cache.Get(utils.RATING_PLAN_PREFIX + rp.Id); hasIt {
t.Error("Already in cache")
}
if err := onStor.CacheDataFromDB(utils.RATING_PLAN_PREFIX, []string{rp.Id}, false); err != nil {
t.Error(err)
}
if itm, hasIt := cache.Get(utils.RATING_PLAN_PREFIX + rp.Id); !hasIt {
t.Error("Did not cache")
} else if rcvRp := itm.(*RatingPlan); !reflect.DeepEqual(rp, rcvRp) {
t.Error("Wrong item in the cache")
}
}

View File

@@ -473,7 +473,9 @@ func (ms *MongoStorage) PreloadCacheForPrefix(prefix string) error {
// prfx represents the cache prefix, ids should be nil if all available data should be loaded
// mustBeCached specifies that data needs to be cached in order to be retrieved from db
func (ms *MongoStorage) CacheDataFromDB(prfx string, ids []string, mustBeCached bool) (err error) {
if !utils.IsSliceMember([]string{utils.DESTINATION_PREFIX}, prfx) {
if !utils.IsSliceMember([]string{utils.DESTINATION_PREFIX,
utils.REVERSE_DESTINATION_PREFIX,
utils.RATING_PLAN_PREFIX}, prfx) {
return utils.NewCGRError(utils.REDIS,
utils.MandatoryIEMissingCaps,
utils.UnsupportedCachePrefix,
@@ -495,12 +497,17 @@ func (ms *MongoStorage) CacheDataFromDB(prfx string, ids []string, mustBeCached
}
switch prfx {
case utils.DESTINATION_PREFIX:
if _, err = ms.GetDestination(dataID, false, utils.NonTransactional); err != nil {
return utils.NewCGRError(utils.REDIS,
utils.ServerErrorCaps,
err.Error(),
fmt.Sprintf("redis error <%s> querying GetDestination for prefix: <%s>, dataID: <%s>", prfx, dataID))
}
_, err = ms.GetDestination(dataID, false, utils.NonTransactional)
case utils.REVERSE_DESTINATION_PREFIX:
_, err = ms.GetReverseDestination(dataID, false, utils.NonTransactional)
case utils.RATING_PLAN_PREFIX:
_, err = ms.GetRatingPlan(dataID, false, utils.NonTransactional)
}
if err != nil {
return utils.NewCGRError(utils.REDIS,
utils.ServerErrorCaps,
err.Error(),
fmt.Sprintf("error <%s> querying mongo for category: <%s>, dataID: <%s>", prfx, dataID))
}
}
return
@@ -657,7 +664,7 @@ func (ms *MongoStorage) SetRatingPlan(rp *RatingPlan, transactionID string) erro
var response int
historyScribe.Call("HistoryV1.Record", rp.GetHistoryRecord(), &response)
}
cache.Set(utils.RATING_PLAN_PREFIX+rp.Id, rp, cacheCommit(transactionID), transactionID)
//cache.Set(utils.RATING_PLAN_PREFIX+rp.Id, rp, cacheCommit(transactionID), transactionID)
return err
}

View File

@@ -273,7 +273,9 @@ func (rs *RedisStorage) RebuildReverseForPrefix(prefix string) error {
// prfx represents the cache prefix, ids should be nil if all available data should be loaded
// mustBeCached specifies that data needs to be cached in order to be retrieved from db
func (rs *RedisStorage) CacheDataFromDB(prfx string, ids []string, mustBeCached bool) (err error) {
if !utils.IsSliceMember([]string{utils.DESTINATION_PREFIX}, prfx) {
if !utils.IsSliceMember([]string{utils.DESTINATION_PREFIX,
utils.REVERSE_DESTINATION_PREFIX,
utils.RATING_PLAN_PREFIX}, prfx) {
return utils.NewCGRError(utils.REDIS,
utils.MandatoryIEMissingCaps,
utils.UnsupportedCachePrefix,
@@ -295,12 +297,17 @@ func (rs *RedisStorage) CacheDataFromDB(prfx string, ids []string, mustBeCached
}
switch prfx {
case utils.DESTINATION_PREFIX:
if _, err = rs.GetDestination(dataID, false, utils.NonTransactional); err != nil {
return utils.NewCGRError(utils.REDIS,
utils.ServerErrorCaps,
err.Error(),
fmt.Sprintf("redis error <%s> querying GetDestination for prefix: <%s>, dataID: <%s>", prfx, dataID))
}
_, err = rs.GetDestination(dataID, false, utils.NonTransactional)
case utils.REVERSE_DESTINATION_PREFIX:
_, err = rs.GetReverseDestination(dataID, false, utils.NonTransactional)
case utils.RATING_PLAN_PREFIX:
_, err = rs.GetRatingPlan(dataID, false, utils.NonTransactional)
}
if err != nil {
return utils.NewCGRError(utils.REDIS,
utils.ServerErrorCaps,
err.Error(),
fmt.Sprintf("error <%s> querying redis for category: <%s>, dataID: <%s>", prfx, dataID))
}
}
return
@@ -364,7 +371,7 @@ func (rs *RedisStorage) SetRatingPlan(rp *RatingPlan, transactionID string) (err
response := 0
go historyScribe.Call("HistoryV1.Record", rp.GetHistoryRecord(), &response)
}
cache.Set(utils.RATING_PLAN_PREFIX+rp.Id, rp, cacheCommit(transactionID), transactionID)
//cache.Set(utils.RATING_PLAN_PREFIX+rp.Id, rp, cacheCommit(transactionID), transactionID)
return
}