diff --git a/engine/datamanager.go b/engine/datamanager.go index 3b7dfff69..b58a0af88 100644 --- a/engine/datamanager.go +++ b/engine/datamanager.go @@ -232,7 +232,7 @@ func (dm *DataManager) CacheDataFromDB(prfx string, ids []string, mustBeCached b _, err = dm.GetThresholdProfile(tntID.Tenant, tntID.ID, false, true, utils.NonTransactional) case utils.ThresholdPrefix: tntID := utils.NewTenantID(dataID) - _, err = dm.GetThreshold(tntID.Tenant, tntID.ID, true, utils.NonTransactional) + _, err = dm.GetThreshold(tntID.Tenant, tntID.ID, false, true, utils.NonTransactional) case utils.FilterPrefix: tntID := utils.NewTenantID(dataID) _, err = dm.GetFilter(tntID.Tenant, tntID.ID, true, utils.NonTransactional) @@ -353,9 +353,9 @@ func (dm *DataManager) RemoveFilter(tenant, id, transactionID string) (err error } func (dm *DataManager) GetThreshold(tenant, id string, - skipCache bool, transactionID string) (th *Threshold, err error) { + cacheRead, cacheWrite bool, transactionID string) (th *Threshold, err error) { tntID := utils.ConcatenatedKey(tenant, id) - if !skipCache { + if cacheRead { if x, ok := Cache.Get(utils.CacheThresholds, tntID); ok { if x == nil { return nil, utils.ErrNotFound @@ -365,14 +365,16 @@ func (dm *DataManager) GetThreshold(tenant, id string, } th, err = dm.dataDB.GetThresholdDrv(tenant, id) if err != nil { - if err == utils.ErrNotFound { + if err == utils.ErrNotFound && cacheWrite { Cache.Set(utils.CacheThresholds, tntID, nil, nil, cacheCommit(transactionID), transactionID) } return nil, err } - Cache.Set(utils.CacheThresholds, tntID, th, nil, - cacheCommit(transactionID), transactionID) + if cacheWrite { + Cache.Set(utils.CacheThresholds, tntID, th, nil, + cacheCommit(transactionID), transactionID) + } return } diff --git a/engine/onstor_it_test.go b/engine/onstor_it_test.go index f9b26f932..83fe67230 100644 --- a/engine/onstor_it_test.go +++ b/engine/onstor_it_test.go @@ -2202,7 +2202,7 @@ func testOnStorITThreshold(t *testing.T) { Hits: 10, } if _, rcvErr := onStor.GetThreshold("cgrates.org", "TH1", - false, utils.NonTransactional); rcvErr != nil && rcvErr != utils.ErrNotFound { + true, false, utils.NonTransactional); rcvErr != nil && rcvErr != utils.ErrNotFound { t.Error(rcvErr) } if err := onStor.SetThreshold(th); err != nil { @@ -2210,14 +2210,14 @@ func testOnStorITThreshold(t *testing.T) { } //get from cache if rcv, err := onStor.GetThreshold("cgrates.org", "TH1", - false, utils.NonTransactional); err != nil { + true, false, utils.NonTransactional); err != nil { t.Error(err) } else if !(reflect.DeepEqual(th, rcv)) { t.Errorf("Expecting: %v, received: %v", th, rcv) } //get from database if rcv, err := onStor.GetThreshold("cgrates.org", "TH1", - true, utils.NonTransactional); err != nil { + false, false, utils.NonTransactional); err != nil { t.Error(err) } else if !(reflect.DeepEqual(th, rcv)) { t.Errorf("Expecting: %v, received: %v", th, rcv) @@ -2236,14 +2236,14 @@ func testOnStorITThreshold(t *testing.T) { time.Sleep(sleepDelay) //get from cache if rcv, err := onStor.GetThreshold("cgrates.org", "TH1", - false, utils.NonTransactional); err != nil { + true, false, utils.NonTransactional); err != nil { t.Error(err) } else if !(reflect.DeepEqual(th, rcv)) { t.Errorf("Expecting: %v, received: %v", th, rcv) } //get from database if rcv, err := onStor.GetThreshold("cgrates.org", "TH1", - true, utils.NonTransactional); err != nil { + false, false, utils.NonTransactional); err != nil { t.Error(err) } else if !(reflect.DeepEqual(th, rcv)) { t.Errorf("Expecting: %v, received: %v", th, rcv) @@ -2253,12 +2253,12 @@ func testOnStorITThreshold(t *testing.T) { } //check cache if removed if _, rcvErr := onStor.GetThreshold(th.Tenant, th.ID, - false, utils.NonTransactional); rcvErr != utils.ErrNotFound { + true, false, utils.NonTransactional); rcvErr != utils.ErrNotFound { t.Error(rcvErr) } //check database if removed if _, rcvErr := onStor.GetThreshold(th.Tenant, th.ID, - true, utils.NonTransactional); rcvErr != utils.ErrNotFound { + false, false, utils.NonTransactional); rcvErr != utils.ErrNotFound { t.Error(rcvErr) } } diff --git a/engine/thresholds.go b/engine/thresholds.go index ee703e514..959d8449e 100644 --- a/engine/thresholds.go +++ b/engine/thresholds.go @@ -248,7 +248,7 @@ func (tS *ThresholdService) matchingThresholdsForEvent(args *ArgsProcessEvent) ( } else if !pass { continue } - t, err := tS.dm.GetThreshold(tPrfl.Tenant, tPrfl.ID, false, "") + t, err := tS.dm.GetThreshold(tPrfl.Tenant, tPrfl.ID, true, true, "") if err != nil { return nil, err } @@ -375,7 +375,7 @@ func (tS *ThresholdService) V1GetThresholdIDs(tenant string, tIDs *[]string) (er // V1GetThreshold retrieves a Threshold func (tS *ThresholdService) V1GetThreshold(tntID *utils.TenantID, t *Threshold) (err error) { - if thd, err := tS.dm.GetThreshold(tntID.Tenant, tntID.ID, false, ""); err != nil { + if thd, err := tS.dm.GetThreshold(tntID.Tenant, tntID.ID, true, true, ""); err != nil { return err } else { *t = *thd diff --git a/engine/thresholds_test.go b/engine/thresholds_test.go index fe1f65a65..c469eada9 100644 --- a/engine/thresholds_test.go +++ b/engine/thresholds_test.go @@ -228,7 +228,7 @@ func TestThresholdsCache(t *testing.T) { //Test each threshold profile from cache for _, th := range ths { if temptTh, err := dmTH.GetThreshold(th.Tenant, - th.ID, false, utils.NonTransactional); err != nil { + th.ID, true, false, utils.NonTransactional); err != nil { t.Errorf("Error: %+v", err) } else if !reflect.DeepEqual(th, temptTh) { t.Errorf("Expecting: %+v, received: %+v", th, temptTh) diff --git a/migrator/thresholds.go b/migrator/thresholds.go index a5cb817c1..9ca0a8a61 100644 --- a/migrator/thresholds.go +++ b/migrator/thresholds.go @@ -57,7 +57,7 @@ func (m *Migrator) migrateCurrentThresholds() (err error) { } for _, id := range ids { idg := strings.TrimPrefix(id, utils.ThresholdPrefix+tenant+":") - ths, err := m.dmIN.DataManager().GetThreshold(tenant, idg, true, utils.NonTransactional) + ths, err := m.dmIN.DataManager().GetThreshold(tenant, idg, false, false, utils.NonTransactional) if err != nil { return err }