various cache fixes

This commit is contained in:
Radu Ioan Fericean
2015-11-09 20:34:46 +02:00
parent 419f387b0a
commit 6931fefbcd
6 changed files with 32 additions and 14 deletions

View File

@@ -64,7 +64,7 @@ func (self *ApierV1) GetAccountActionPlan(attrs AttrAcntAction, reply *[]*Accoun
type AttrRemActionTiming struct {
ActionPlanId string // Id identifying the ActionTimings profile
ActionTimingId string // Internal CGR id identifying particular ActionTiming, *all for all user related ActionTimings to be canceled
Tenant string // Tenant he account belongs to
Tenant string // Tenant the account belongs to
Account string // Account name
ReloadScheduler bool // If set it will reload the scheduler after adding
}
@@ -90,6 +90,9 @@ func (self *ApierV1) RemActionTiming(attrs AttrRemActionTiming, reply *string) e
if err := self.RatingDb.SetActionPlans(attrs.ActionPlanId, ats); err != nil {
return 0, err
}
if len(ats) > 0 { // update cache
self.RatingDb.CacheRatingPrefixValues(map[string][]string{utils.ACTION_PLAN_PREFIX: []string{attrs.ActionPlanId}})
}
return 0, nil
}, 0, utils.ACTION_PLAN_PREFIX)
if err != nil {

View File

@@ -1155,32 +1155,32 @@ func TestApierGetAccount(t *testing.T) {
}
var reply *engine.Account
attrs := &utils.AttrGetAccount{Tenant: "cgrates.org", Account: "1001"}
if err := rater.Call("ApierV1.GetAccount", attrs, &reply); err != nil {
if err := rater.Call("ApierV2.GetAccount", attrs, &reply); err != nil {
t.Error("Got error on ApierV1.GetAccount: ", err.Error())
} else if reply.BalanceMap[utils.MONETARY].GetTotalValue() != 11.5 { // We expect 11.5 since we have added in the previous test 1.5
t.Errorf("Calling ApierV1.GetBalance expected: 11.5, received: %f", reply.BalanceMap[utils.MONETARY].GetTotalValue())
}
attrs = &utils.AttrGetAccount{Tenant: "cgrates.org", Account: "dan"}
if err := rater.Call("ApierV1.GetAccount", attrs, &reply); err != nil {
if err := rater.Call("ApierV2.GetAccount", attrs, &reply); err != nil {
t.Error("Got error on ApierV1.GetAccount: ", err.Error())
} else if reply.BalanceMap[utils.MONETARY].GetTotalValue() != 1.5 {
t.Errorf("Calling ApierV1.GetAccount expected: 1.5, received: %f", reply.BalanceMap[utils.MONETARY].GetTotalValue())
}
// The one we have topped up though executeAction
attrs = &utils.AttrGetAccount{Tenant: "cgrates.org", Account: "dan2"}
if err := rater.Call("ApierV1.GetAccount", attrs, &reply); err != nil {
if err := rater.Call("ApierV2.GetAccount", attrs, &reply); err != nil {
t.Error("Got error on ApierV1.GetAccount: ", err.Error())
} else if reply.BalanceMap[utils.MONETARY].GetTotalValue() != 11.5 {
t.Errorf("Calling ApierV1.GetAccount expected: 10, received: %f", reply.BalanceMap[utils.MONETARY].GetTotalValue())
}
attrs = &utils.AttrGetAccount{Tenant: "cgrates.org", Account: "dan3"}
if err := rater.Call("ApierV1.GetAccount", attrs, &reply); err != nil {
if err := rater.Call("ApierV2.GetAccount", attrs, &reply); err != nil {
t.Error("Got error on ApierV1.GetAccount: ", err.Error())
} else if reply.BalanceMap[utils.MONETARY].GetTotalValue() != 3.6 {
t.Errorf("Calling ApierV1.GetAccount expected: 3.6, received: %f", reply.BalanceMap[utils.MONETARY].GetTotalValue())
}
attrs = &utils.AttrGetAccount{Tenant: "cgrates.org", Account: "dan6"}
if err := rater.Call("ApierV1.GetAccount", attrs, &reply); err != nil {
if err := rater.Call("ApierV2.GetAccount", attrs, &reply); err != nil {
t.Error("Got error on ApierV1.GetAccount: ", err.Error())
} else if reply.BalanceMap[utils.MONETARY].GetTotalValue() != 1 {
t.Errorf("Calling ApierV1.GetAccount expected: 1, received: %f", reply.BalanceMap[utils.MONETARY].GetTotalValue())
@@ -1290,7 +1290,7 @@ func TestApierGetAccountAfterLoad(t *testing.T) {
}
var reply *engine.Account
attrs := &utils.AttrGetAccount{Tenant: "cgrates.org", Account: "1001"}
if err := rater.Call("ApierV1.GetAccount", attrs, &reply); err != nil {
if err := rater.Call("ApierV2.GetAccount", attrs, &reply); err != nil {
t.Error("Got error on ApierV1.GetAccount: ", err.Error())
} else if reply.BalanceMap[utils.MONETARY].GetTotalValue() != 11 {
t.Errorf("Calling ApierV1.GetBalance expected: 11, received: %f", reply.BalanceMap[utils.MONETARY].GetTotalValue())

View File

@@ -330,27 +330,27 @@ func (at *ActionPlan) String_DISABLED() string {
}
// Helper to remove ActionPlan members based on specific filters, empty data means no always match
func RemActionPlan(ats ActionPlans, actionTimingId, balanceId string) ActionPlans {
func RemActionPlan(ats ActionPlans, actionTimingId, accountId string) ActionPlans {
for idx, at := range ats {
if len(actionTimingId) != 0 && at.Uuid != actionTimingId { // No Match for ActionPlanId, no need to move further
continue
}
if len(balanceId) == 0 { // No account defined, considered match for complete removal
if len(accountId) == 0 { // No account defined, considered match for complete removal
if len(ats) == 1 { // Removing last item, by init empty
return make([]*ActionPlan, 0)
}
ats[idx], ats = ats[len(ats)-1], ats[:len(ats)-1]
continue
}
for iBlnc, blncId := range at.AccountIds {
if blncId == balanceId {
for iAcc, accId := range at.AccountIds {
if accId == accountId {
if len(at.AccountIds) == 1 { // Only one balance, remove complete at
if len(ats) == 1 { // Removing last item, by init empty
return make([]*ActionPlan, 0)
}
ats[idx], ats = ats[len(ats)-1], ats[:len(ats)-1]
} else {
at.AccountIds[iBlnc], at.AccountIds = at.AccountIds[len(at.AccountIds)-1], at.AccountIds[:len(at.AccountIds)-1]
at.AccountIds[iAcc], at.AccountIds = at.AccountIds[len(at.AccountIds)-1], at.AccountIds[:len(at.AccountIds)-1]
}
// only remove the first one matching
break

View File

@@ -699,6 +699,7 @@ func (ms *MapStorage) SetActionPlans(key string, ats ActionPlans) (err error) {
if len(ats) == 0 {
// delete the key
delete(ms.dict, utils.ACTION_PLAN_PREFIX+key)
cache2go.RemKey(utils.ACTION_PLAN_PREFIX + key)
return
}
result, err := ms.ms.Marshal(&ats)
@@ -740,6 +741,11 @@ func (ms *MapStorage) GetDerivedChargers(key string, skipCache bool) (dcs utils.
}
func (ms *MapStorage) SetDerivedChargers(key string, dcs utils.DerivedChargers) error {
if len(dcs) == 0 {
delete(ms.dict, utils.DERIVEDCHARGERS_PREFIX+key)
cache2go.RemKey(utils.DERIVEDCHARGERS_PREFIX + key)
return nil
}
result, err := ms.ms.Marshal(dcs)
ms.dict[utils.DERIVEDCHARGERS_PREFIX+key] = result
return err

View File

@@ -1077,6 +1077,14 @@ func (ms *MongoStorage) GetActionPlans(key string, skipCache bool) (ats ActionPl
}
func (ms *MongoStorage) SetActionPlans(key string, ats ActionPlans) error {
if len(ats) == 0 {
cache2go.RemKey(utils.ACTION_PLAN_PREFIX + key)
err := ms.db.C(colApl).Remove(bson.M{"key": key})
if err != mgo.ErrNotFound {
return err
}
return nil
}
_, err := ms.db.C(colApl).Upsert(bson.M{"key": key}, &struct {
Key string
Value ActionPlans
@@ -1121,8 +1129,8 @@ func (ms *MongoStorage) GetDerivedChargers(key string, skipCache bool) (dcs util
func (ms *MongoStorage) SetDerivedChargers(key string, dcs utils.DerivedChargers) (err error) {
if len(dcs) == 0 {
err = ms.db.C(colDcs).Remove(bson.M{"key": key})
cache2go.RemKey(utils.DERIVEDCHARGERS_PREFIX + key)
err = ms.db.C(colDcs).Remove(bson.M{"key": key})
if err != mgo.ErrNotFound {
return err
}

View File

@@ -873,6 +873,7 @@ func (rs *RedisStorage) SetActionPlans(key string, ats ActionPlans) (err error)
if len(ats) == 0 {
// delete the key
_, err = rs.db.Del(utils.ACTION_PLAN_PREFIX + key)
cache2go.RemKey(utils.ACTION_PLAN_PREFIX + key)
return err
}
result, err := rs.ms.Marshal(&ats)
@@ -918,7 +919,7 @@ func (rs *RedisStorage) GetDerivedChargers(key string, skipCache bool) (dcs util
func (rs *RedisStorage) SetDerivedChargers(key string, dcs utils.DerivedChargers) (err error) {
if len(dcs) == 0 {
_, err = rs.db.Del(utils.DERIVEDCHARGERS_PREFIX + key)
// FIXME: Does cache need cleanup too?
cache2go.RemKey(utils.DERIVEDCHARGERS_PREFIX + key)
return err
}
marshaled, err := rs.ms.Marshal(dcs)