diff --git a/apier/v1/aliases.go b/apier/v1/aliases.go index 962949105..7825531a0 100644 --- a/apier/v1/aliases.go +++ b/apier/v1/aliases.go @@ -45,7 +45,7 @@ func (self *ApierV1) AddRatingSubjectAliases(attrs AttrAddRatingSubjectAliases, } aliasesChanged = append(aliasesChanged, utils.RP_ALIAS_PREFIX+utils.RatingSubjectAliasKey(attrs.Tenant, alias)) } - if err := self.RatingDb.CachePrefixes(utils.RP_ALIAS_PREFIX); err != nil { + if err := self.RatingDb.CachePrefixValues(map[string][]string{utils.RP_ALIAS_PREFIX: aliasesChanged}); err != nil { return utils.NewErrServerError(err) } *reply = utils.OK @@ -73,12 +73,16 @@ func (self *ApierV1) RemRatingSubjectAliases(tenantRatingSubject engine.TenantRa return utils.NewErrMandatoryIeMissing(missing...) } if err := self.RatingDb.RemoveRpAliases([]*engine.TenantRatingSubject{&tenantRatingSubject}); err != nil { + if err == utils.ErrNotFound { + return err + } return utils.NewErrServerError(err) } - if err := self.RatingDb.CachePrefixes(utils.RP_ALIAS_PREFIX); err != nil { + // cache refresh not needed, synched in RemoveRpAliases + /*if err := self.RatingDb.CachePrefixes(utils.RP_ALIAS_PREFIX); err != nil { return utils.NewErrServerError(err) - } + }*/ *reply = utils.OK return nil } @@ -94,7 +98,7 @@ func (self *ApierV1) AddAccountAliases(attrs AttrAddAccountAliases, reply *strin } aliasesChanged = append(aliasesChanged, utils.ACC_ALIAS_PREFIX+utils.AccountAliasKey(attrs.Tenant, alias)) } - if err := self.RatingDb.CachePrefixes(utils.ACC_ALIAS_PREFIX); err != nil { + if err := self.RatingDb.CachePrefixValues(map[string][]string{utils.ACC_ALIAS_PREFIX: aliasesChanged}); err != nil { return utils.NewErrServerError(err) } *reply = utils.OK @@ -122,11 +126,15 @@ func (self *ApierV1) RemAccountAliases(tenantAccount engine.TenantAccount, reply return utils.NewErrMandatoryIeMissing(missing...) } if err := self.RatingDb.RemoveAccAliases([]*engine.TenantAccount{&tenantAccount}); err != nil { + if err == utils.ErrNotFound { + return err + } return utils.NewErrServerError(err) } - if err := self.RatingDb.CachePrefixes(utils.ACC_ALIAS_PREFIX); err != nil { + // cache refresh not needed, synched in RemoveRpAliases + /*if err := self.RatingDb.CachePrefixes(utils.ACC_ALIAS_PREFIX); err != nil { return utils.NewErrServerError(err) - } + }*/ *reply = utils.OK return nil } diff --git a/engine/calldesc.go b/engine/calldesc.go index beb3fdaa7..8024184b2 100644 --- a/engine/calldesc.go +++ b/engine/calldesc.go @@ -531,6 +531,7 @@ func (origCD *CallDescriptor) getMaxSessionDuration(origAcc *Account) (time.Dura // cc, err := cd.debit(account, true, false) //Logger.Debug("CC: " + utils.ToJSON(cc)) + //log.Print("CC: ", utils.ToIJSON(cc)) //Logger.Debug(fmt.Sprintf("ERR: %v", err)) if err != nil { return 0, err diff --git a/engine/calldesc_test.go b/engine/calldesc_test.go index 23a73a8cf..b17046165 100644 --- a/engine/calldesc_test.go +++ b/engine/calldesc_test.go @@ -860,8 +860,8 @@ func TestMaxSesionTimeEmptyBalanceAndNoCost(t *testing.T) { func TestMaxSesionTimeLong(t *testing.T) { cd := &CallDescriptor{ - TimeStart: time.Date(2015, 07, 26, 13, 37, 0, 0, time.UTC), - TimeEnd: time.Date(2015, 07, 26, 16, 37, 0, 0, time.UTC), + TimeStart: time.Date(2015, 07, 24, 13, 37, 0, 0, time.UTC), + TimeEnd: time.Date(2015, 07, 24, 15, 37, 0, 0, time.UTC), Direction: "*out", Category: "call", Tenant: "cgrates.org", @@ -877,8 +877,8 @@ func TestMaxSesionTimeLong(t *testing.T) { func TestMaxSesionTimeLongerThanMoney(t *testing.T) { cd := &CallDescriptor{ - TimeStart: time.Date(2015, 07, 26, 13, 37, 0, 0, time.UTC), - TimeEnd: time.Date(2015, 07, 26, 19, 37, 0, 0, time.UTC), + TimeStart: time.Date(2015, 07, 24, 13, 37, 0, 0, time.UTC), + TimeEnd: time.Date(2015, 07, 24, 16, 37, 0, 0, time.UTC), Direction: "*out", Category: "call", Tenant: "cgrates.org", @@ -887,7 +887,7 @@ func TestMaxSesionTimeLongerThanMoney(t *testing.T) { } acc, _ := accountingStorage.GetAccount("*out:cgrates.org:money") allowedTime, err := cd.getMaxSessionDuration(acc) - expected, err := time.ParseDuration("5h33m20s") + expected, err := time.ParseDuration("2h46m40s") if err != nil || allowedTime != expected { t.Errorf("Expected: %v got %v", expected, allowedTime) } diff --git a/engine/storage_redis.go b/engine/storage_redis.go index 658b31d1e..5f5648bda 100644 --- a/engine/storage_redis.go +++ b/engine/storage_redis.go @@ -23,6 +23,7 @@ import ( "compress/zlib" "errors" "fmt" + "strings" "github.com/cgrates/cgrates/cache2go" "github.com/cgrates/cgrates/utils" @@ -411,27 +412,25 @@ func (rs *RedisStorage) SetRpAlias(key, alias string) (err error) { // Removes the aliases of a specific account, on a tenant func (rs *RedisStorage) RemoveRpAliases(tenantRtSubjects []*TenantRatingSubject) (err error) { - alsKeys, err := rs.db.Keys(utils.RP_ALIAS_PREFIX + "*") + alsMap, err := cache2go.GetAllEntries(utils.RP_ALIAS_PREFIX) if err != nil { return err } - for _, key := range alsKeys { - alias, err := rs.GetRpAlias(key[len(utils.RP_ALIAS_PREFIX):], true) - if err != nil { - return err - } + + for key, aliasInterface := range alsMap { + alias := aliasInterface.Value().(string) for _, tntRSubj := range tenantRtSubjects { - tenantPrfx := utils.RP_ALIAS_PREFIX + tntRSubj.Tenant + utils.CONCATENATED_KEY_SEP - if len(key) < len(tenantPrfx) || tenantPrfx != key[:len(tenantPrfx)] { // filter out the tenant for accounts + tenantPrfx := tntRSubj.Tenant + utils.CONCATENATED_KEY_SEP + if len(key) < len(tenantPrfx) || !strings.HasPrefix(key, tenantPrfx) { // filter out the tenant for accounts continue } if tntRSubj.Subject != alias { continue } - cache2go.RemKey(key) - if _, err = rs.db.Del(key); err != nil { + if _, err = rs.db.Del(utils.RP_ALIAS_PREFIX + key); err != nil { return err } + cache2go.RemKey(utils.RP_ALIAS_PREFIX + key) break } } @@ -508,27 +507,25 @@ func (rs *RedisStorage) SetAccAlias(key, alias string) (err error) { } func (rs *RedisStorage) RemoveAccAliases(tenantAccounts []*TenantAccount) (err error) { - alsKeys, err := rs.db.Keys(utils.ACC_ALIAS_PREFIX + "*") + alsMap, err := cache2go.GetAllEntries(utils.ACC_ALIAS_PREFIX) if err != nil { return err } - for _, key := range alsKeys { - alias, err := rs.GetAccAlias(key[len(utils.ACC_ALIAS_PREFIX):], true) - if err != nil { - return err - } + + for key, aliasInterface := range alsMap { + alias := aliasInterface.Value().(string) for _, tntAcnt := range tenantAccounts { - tenantPrfx := utils.ACC_ALIAS_PREFIX + tntAcnt.Tenant + utils.CONCATENATED_KEY_SEP - if len(key) < len(tenantPrfx) || tenantPrfx != key[:len(tenantPrfx)] { // filter out the tenant for accounts + tenantPrfx := tntAcnt.Tenant + utils.CONCATENATED_KEY_SEP + if len(key) < len(tenantPrfx) || !strings.HasPrefix(key, tenantPrfx) { // filter out the tenant for accounts continue } if tntAcnt.Account != alias { continue } - cache2go.RemKey(key) - if _, err = rs.db.Del(key); err != nil { + if _, err = rs.db.Del(utils.ACC_ALIAS_PREFIX + key); err != nil { return err } + cache2go.RemKey(utils.ACC_ALIAS_PREFIX + key) } } return diff --git a/engine/storage_test.go b/engine/storage_test.go index 4537406a9..21cf88006 100644 --- a/engine/storage_test.go +++ b/engine/storage_test.go @@ -153,9 +153,6 @@ func TestGetRPAliases(t *testing.T) { } func TestRemRSubjAliases(t *testing.T) { - if err := ratingStorage.SetRpAlias(utils.RatingSubjectAliasKey("cgrates.org", "2001"), "1001"); err != nil { - t.Error(err) - } if err := ratingStorage.SetRpAlias(utils.RatingSubjectAliasKey("cgrates.org", "2002"), "1001"); err != nil { t.Error(err) } @@ -177,6 +174,24 @@ func TestRemRSubjAliases(t *testing.T) { } } +func TestStorageRpAliases(t *testing.T) { + if _, err := ratingStorage.GetRpAlias("cgrates.org:1991", true); err == nil { + t.Error("Found alias before setting") + } + if err := ratingStorage.SetRpAlias(utils.RatingSubjectAliasKey("cgrates.org", "1991"), "1991"); err != nil { + t.Error(err) + } + if _, err := ratingStorage.GetRpAlias("cgrates.org:1991", true); err != nil { + t.Error("Alias not found after setting") + } + if err := ratingStorage.RemoveRpAliases([]*TenantRatingSubject{&TenantRatingSubject{Tenant: "cgrates.org", Subject: "1991"}}); err != nil { + t.Error(err) + } + if _, err := ratingStorage.GetRpAlias("cgrates.org:1991", true); err == nil { + t.Error("Found alias after removing") + } +} + func TestGetAccountAliases(t *testing.T) { if err := ratingStorage.SetAccAlias(utils.AccountAliasKey("cgrates.org", "2001"), "1001"); err != nil { t.Error(err) @@ -200,6 +215,24 @@ func TestGetAccountAliases(t *testing.T) { } } +func TestStorageAccAliases(t *testing.T) { + if _, err := ratingStorage.GetAccAlias("cgrates.org:1991", true); err == nil { + t.Error("Found alias before setting") + } + if err := ratingStorage.SetAccAlias(utils.AccountAliasKey("cgrates.org", "1991"), "1991"); err != nil { + t.Error(err) + } + if _, err := ratingStorage.GetAccAlias("cgrates.org:1991", true); err != nil { + t.Error("Alias not found after setting") + } + if err := ratingStorage.RemoveAccAliases([]*TenantAccount{&TenantAccount{Tenant: "cgrates.org", Account: "1991"}}); err != nil { + t.Error(err) + } + if _, err := ratingStorage.GetAccAlias("cgrates.org:1991", true); err == nil { + t.Error("Found alias after removing") + } +} + /************************** Benchmarks *****************************/ func GetUB() *Account {