From 788919bdaa768244bdcbcf104b1f35f065ea2a59 Mon Sep 17 00:00:00 2001 From: porosnicuadrian Date: Tue, 25 May 2021 16:59:15 +0300 Subject: [PATCH] Added context for routes methods --- apis/filter_indexes.go | 4 +- cmd/cgr-engine/cgr-engine.go | 2 +- data/tariffplans/tutroutes/Filters.csv | 5 ++ data/tariffplans/tutroutes/Routes.csv | 32 +++++++----- data/tariffplans/tutroutes/Stats.csv | 8 +-- engine/datadbmock.go | 6 +-- engine/datamanager.go | 38 +++++++------- engine/filters.go | 8 +-- engine/filters_test.go | 6 +-- engine/libindex.go | 2 +- engine/libroutes.go | 8 +-- engine/route_highestcost.go | 6 ++- engine/route_leastcost.go | 6 ++- engine/route_load_distribution.go | 6 ++- engine/route_qos.go | 5 +- engine/route_reas.go | 6 ++- engine/route_reds.go | 5 +- engine/route_weight.go | 5 +- engine/routes.go | 58 +++++++++++----------- engine/routes_test.go | 68 +++++++++++++------------- engine/storage_interface.go | 6 +-- engine/storage_internal_datadb.go | 6 +-- engine/storage_mongo_datadb.go | 12 ++--- engine/storage_redis.go | 6 +-- engine/tpreader.go | 2 +- loaders/loader.go | 2 +- loaders/loader_test.go | 4 +- migrator/routes.go | 10 ++-- services/routes.go | 21 ++++---- 29 files changed, 193 insertions(+), 160 deletions(-) diff --git a/apis/filter_indexes.go b/apis/filter_indexes.go index a9792f9cf..c1c162ff2 100644 --- a/apis/filter_indexes.go +++ b/apis/filter_indexes.go @@ -278,7 +278,7 @@ func (adms *AdminSv1) ComputeFilterIndexes(cntxt *context.Context, args *utils.A cacheIDs[utils.RouteFilterIndexIDs] = []string{utils.MetaAny} if indexes, err = engine.ComputeIndexes(cntxt, adms.dm, tnt, args.Subsystem, utils.CacheRouteFilterIndexes, nil, transactionID, func(tnt, id, ctx string) (*[]string, error) { - rp, e := adms.dm.GetRouteProfile(tnt, id, true, false, utils.NonTransactional) + rp, e := adms.dm.GetRouteProfile(cntxt, tnt, id, true, false, utils.NonTransactional) if e != nil { return nil, e } @@ -487,7 +487,7 @@ func (adms *AdminSv1) ComputeFilterIndexIDs(cntxt *context.Context, args *utils. //RouteProfile Indexes if indexes, err = engine.ComputeIndexes(cntxt, adms.dm, tnt, args.Subsystem, utils.CacheRouteFilterIndexes, &args.RouteIDs, transactionID, func(tnt, id, ctx string) (*[]string, error) { - rp, e := adms.dm.GetRouteProfile(tnt, id, true, false, utils.NonTransactional) + rp, e := adms.dm.GetRouteProfile(cntxt, tnt, id, true, false, utils.NonTransactional) if e != nil { return nil, e } diff --git a/cmd/cgr-engine/cgr-engine.go b/cmd/cgr-engine/cgr-engine.go index acd4954ff..ee9e912ca 100644 --- a/cmd/cgr-engine/cgr-engine.go +++ b/cmd/cgr-engine/cgr-engine.go @@ -143,7 +143,7 @@ func initConfigSv1(internalConfigChan chan birpc.ClientConnector, func startRPC(server *cores.Server, internalAdminSChan, internalCdrSChan, internalRsChan, internalStatSChan, - internalAttrSChan, internalChargerSChan, internalThdSChan, internalSuplSChan, + internalAttrSChan, internalChargerSChan, internalThdSChan, internalRouteSChan, internalSMGChan, internalAnalyzerSChan, internalDispatcherSChan, internalLoaderSChan, internalCacheSChan, internalEEsChan, internalRateSChan, internalActionSChan, diff --git a/data/tariffplans/tutroutes/Filters.csv b/data/tariffplans/tutroutes/Filters.csv index e69de29bb..a93e32411 100644 --- a/data/tariffplans/tutroutes/Filters.csv +++ b/data/tariffplans/tutroutes/Filters.csv @@ -0,0 +1,5 @@ +#Tenant[0],ID[1],Type[2],Path[3],Values[4],ActivationInterval[5] +cgrates.org,Fltr_tcc,*gt,~*req.Usage,3m, +cgrates.org,Fltr_tcc,*string,~*req.Destination,1007, +cgrates.org,FLTR_DEST_1003,*string,~*req.Destination,1003, + diff --git a/data/tariffplans/tutroutes/Routes.csv b/data/tariffplans/tutroutes/Routes.csv index a4b9056f5..270530850 100644 --- a/data/tariffplans/tutroutes/Routes.csv +++ b/data/tariffplans/tutroutes/Routes.csv @@ -1,17 +1,27 @@ -#Tenant,ID,FilterIDs,Weight,Sorting,SortingParameters,RouteID,RouteFilterIDs,RouteAccountIDs,RouteRatingPlanIDs,RouteResourceIDs,RouteStatIDs,RouteWeight,RouteBlocker,RouteParameters +#Tenant,ID,FilterIDs,ActivationInterval,Sorting,SortingParameters,RouteID,RouteFilterIDs,RouteAccountIDs,RouteRatingPlanIDs,RouteResourceIDs,RouteStatIDs,RouteWeight,RouteBlocker,RouteParameters,Weight -cgrates.org,ROUTE_ACNT_1001,*string:~*req.Account:1001,,*weight,,vendor1,,,,,,10,, -cgrates.org,ROUTE_ACNT_1001,,,,,vendor2,*gte:~*accounts.1001.BalanceMap.*monetary[0].Value:10,,,,,20,, +cgrates.org,ROUTE_ACNT_1001,*string:~*req.Account:1001,,*weight,,vendor1,FLTR_DEST_1003,,,,,10,,, +cgrates.org,ROUTE_ACNT_1001,,,,,vendor2,*gte:~*accounts.1001.BalanceMap.*monetary[0].Value:10,,,,,20,,, +cgrates.org,ROUTE_ACNT_1001,,,,,vendor3,FLTR_DEST_1003;*prefix:~*req.Account:10,,,,,40,,, +cgrates.org,ROUTE_ACNT_1001,,,,,vendor4,,,,,,35,,, -cgrates.org,ROUTE_ACNT_1002,*string:~*req.Account:1002,,*lc,,vendor1,*lte:~*resources.RES_GRP1.TotalUsage:5,,RP_VENDOR1,,,0,, -cgrates.org,ROUTE_ACNT_1002,,,,,vendor2,*gte:~*stats.STATS_VENDOR_2.*acd:1m,,RP_VENDOR2,,,0,, +cgrates.org,ROUTE_ACNT_1002,*string:~*req.Account:1002,,*lc,,vendor1,*lte:~*resources.RES_GRP1.TotalUsage:5,,RP_VENDOR1,,,0,,, +cgrates.org,ROUTE_ACNT_1002,,,,,vendor2,*gte:~*stats.STATS_VENDOR_2.*acd:1m,,RP_VENDOR2,,,0,,, +cgrates.org,ROUTE_ACNT_1002,,,,,vendor3,,,RP_VENDOR2,,,10,,, +cgrates.org,ROUTE_ACNT_1002,,,,,vendor4,*ai:~*req.AnswerTime:2013-06-01T00:00:00Z|2013-06-01T10:00:00Z,,RP_STANDARD,,,30,,, -cgrates.org,ROUTE_ACNT_1003,*string:~*req.Account:1003,,*qos,*acd,vendor1,,,,,STATS_VENDOR_1,0,, -cgrates.org,ROUTE_ACNT_1003,,,,,vendor2,,,,,STATS_VENDOR_2,0,, +cgrates.org,ROUTE_ACNT_1003,*string:~*req.Account:1003,,*qos,*acd;*tcc,vendor1,,,,,STATS_VENDOR_1,0,,, +cgrates.org,ROUTE_ACNT_1003,,,,,vendor2,*prefix:~*req.Destination:10,,,,STATS_VENDOR_2,0,,, +cgrates.org,ROUTE_ACNT_1003,,,,,vendor3,*gte:~*stats.STATS_VENDOR_1.*tcc:6,,,,STATS_VENDOR_1,20,,, -cgrates.org,ROUTE_ACNT_1004,*string:~*req.Account:1004,,*reas,,vendor1,,,,RES_GRP1,,0,, -cgrates.org,ROUTE_ACNT_1004,,,,,vendor2,,,,RES_GRP2,,0,, +cgrates.org,ROUTE_ACNT_1004,*string:~*req.Account:1004,,*reas,,vendor1,,,,RES_GRP1,,0,,, +cgrates.org,ROUTE_ACNT_1004,,,,,vendor2,,,,RES_GRP2,,0,,, +cgrates.org,ROUTE_ACNT_1004,,,,,vendor3,*gte:~*resources.RES_GRP1.TotalUsage:9,,,RES_GRP2,,10,,, +cgrates.org,ROUTE_ACNT_1005,*string:~*req.Account:1005,,*load,vendor1:3;*default:2,vendor1,,,,,STATS_VENDOR_1:*sum#1,,,, +cgrates.org,ROUTE_ACNT_1005,,,,,vendor2,,,,,STATS_VENDOR_2:*sum#1,10,,, +cgrates.org,ROUTE_ACNT_1005,,,,,vendor3,,,,,STATS_VENDOR_2:*distinct#~*req.Usage,,,, -cgrates.org,ROUTE_ACNT_1005,*string:~*req.Account:1005,,*load,vendor1:3;vendor2:1;*default:2,vendor1,,,,,STATS_VENDOR_1:*sum#1,,, -cgrates.org,ROUTE_ACNT_1005,,,,,vendor2,,,,,STATS_VENDOR_2:*sum#1,,, +cgrates.org,ROUTE_HC1,Fltr_tcc,,*hc,,route1,*gte:~*resources.RES_GRP2.Available:6,,RP_VENDOR2,RES_GRP2,,20,,, +cgrates.org,ROUTE_HC1,,,,,route2,*gte:~*resources.RES_GRP1.TotalUsage:9,,RP_VENDOR1,RES_GRP1,,20,,, +cgrates.org,ROUTE_HC1,,,,,route3,,,RP_VENDOR1,RES_GRP2,,10,,, diff --git a/data/tariffplans/tutroutes/Stats.csv b/data/tariffplans/tutroutes/Stats.csv index 49649740f..3fe5de26e 100644 --- a/data/tariffplans/tutroutes/Stats.csv +++ b/data/tariffplans/tutroutes/Stats.csv @@ -1,3 +1,5 @@ -#Tenant[0],Id[1],FilterIDs[2],Weight[3],QueueLength[4],TTL[5],MinItems[6],Metrics[7],MetricFilterIDs[8],Stored[9],Blocker[10],ThresholdIDs[11] -cgrates.org,STATS_VENDOR_1,*string:~*req.Category:vendor1,,100,-1,,*acd;*tcd;*acc;*tcc;*sum#1,,,,*none -cgrates.org,STATS_VENDOR_2,*string:~*req.Category:vendor2,,100,-1,,*acd;*tcd;*acc;*tcc;*sum#1,,,,*none +#Tenant[0],Id[1],FilterIDs[2],ActivationInterval[3],QueueLength[4],TTL[5],MinItems[6],Metrics[7],MetricFilterIDs[8],Stored[9],Blocker[10],Weight[11],ThresholdIDs[12] +cgrates.org,STATS_VENDOR_1,*string:~*req.Category:vendor1,,100,-1,,*acd;*tcd;*acc;*tcc;*sum#1,,,,,*none +cgrates.org,STATS_VENDOR_2,*string:~*req.Category:vendor2,,100,-1,,*acd;*tcd;*acc;*tcc;*sum#1;*distinct#~*req.Usage,,,,,*none +cgrates.org,STATS_TCC1,,,100,-1,,*tcc,,,,,*none +cgrates.org,STATS_TCC2,Fltr_tcc,,100,-1,,*tcc,,,,,*none diff --git a/engine/datadbmock.go b/engine/datadbmock.go index 5a3b53a8f..993c9dcae 100644 --- a/engine/datadbmock.go +++ b/engine/datadbmock.go @@ -235,15 +235,15 @@ func (dbM *DataDBMock) RemoveFilterDrv(string, string) error { return utils.ErrNotImplemented } -func (dbM *DataDBMock) GetRouteProfileDrv(string, string) (*RouteProfile, error) { +func (dbM *DataDBMock) GetRouteProfileDrv(*context.Context, string, string) (*RouteProfile, error) { return nil, utils.ErrNotImplemented } -func (dbM *DataDBMock) SetRouteProfileDrv(*RouteProfile) error { +func (dbM *DataDBMock) SetRouteProfileDrv(*context.Context, *RouteProfile) error { return utils.ErrNotImplemented } -func (dbM *DataDBMock) RemoveRouteProfileDrv(string, string) error { +func (dbM *DataDBMock) RemoveRouteProfileDrv(*context.Context, string, string) error { return utils.ErrNotImplemented } diff --git a/engine/datamanager.go b/engine/datamanager.go index 483c3d812..b356509da 100644 --- a/engine/datamanager.go +++ b/engine/datamanager.go @@ -167,7 +167,7 @@ func (dm *DataManager) CacheDataFromDB(ctx *context.Context, prfx string, ids [] _, err = dm.GetFilter(ctx, tntID.Tenant, tntID.ID, false, true, utils.NonTransactional) case utils.RouteProfilePrefix: tntID := utils.NewTenantID(dataID) - _, err = dm.GetRouteProfile(tntID.Tenant, tntID.ID, false, true, utils.NonTransactional) + _, err = dm.GetRouteProfile(ctx, tntID.Tenant, tntID.ID, false, true, utils.NonTransactional) case utils.AttributeProfilePrefix: tntID := utils.NewTenantID(dataID) _, err = dm.GetAttributeProfile(ctx, tntID.Tenant, tntID.ID, false, true, utils.NonTransactional) @@ -1174,7 +1174,7 @@ func (dm *DataManager) HasData(category, subject, tenant string) (has bool, err return dm.DataDB().HasDataDrv(context.TODO(), category, subject, tenant) } -func (dm *DataManager) GetRouteProfile(tenant, id string, cacheRead, cacheWrite bool, +func (dm *DataManager) GetRouteProfile(ctx *context.Context, tenant, id string, cacheRead, cacheWrite bool, transactionID string) (rpp *RouteProfile, err error) { tntID := utils.ConcatenatedKey(tenant, id) if cacheRead { @@ -1189,23 +1189,23 @@ func (dm *DataManager) GetRouteProfile(tenant, id string, cacheRead, cacheWrite err = utils.ErrNoDatabaseConn return } - rpp, err = dm.dataDB.GetRouteProfileDrv(tenant, id) + rpp, err = dm.dataDB.GetRouteProfileDrv(ctx, tenant, id) if err != nil { if itm := config.CgrConfig().DataDbCfg().Items[utils.MetaRouteProfiles]; err == utils.ErrNotFound && itm.Remote { - if err = dm.connMgr.Call(context.TODO(), config.CgrConfig().DataDbCfg().RmtConns, utils.ReplicatorSv1GetRouteProfile, + if err = dm.connMgr.Call(ctx, config.CgrConfig().DataDbCfg().RmtConns, utils.ReplicatorSv1GetRouteProfile, &utils.TenantIDWithAPIOpts{ TenantID: &utils.TenantID{Tenant: tenant, ID: id}, APIOpts: utils.GenerateDBItemOpts(itm.APIKey, itm.RouteID, utils.EmptyString, utils.FirstNonEmpty(config.CgrConfig().DataDbCfg().RmtConnID, config.CgrConfig().GeneralCfg().NodeID)), }, &rpp); err == nil { - err = dm.dataDB.SetRouteProfileDrv(rpp) + err = dm.dataDB.SetRouteProfileDrv(ctx, rpp) } } if err != nil { err = utils.CastRPCErr(err) if err == utils.ErrNotFound && cacheWrite && dm.dataDB.GetStorageType() != utils.Internal { - if errCh := Cache.Set(context.TODO(), utils.CacheRouteProfiles, tntID, nil, nil, + if errCh := Cache.Set(ctx, utils.CacheRouteProfiles, tntID, nil, nil, cacheCommit(transactionID), transactionID); errCh != nil { return nil, errCh } @@ -1219,7 +1219,7 @@ func (dm *DataManager) GetRouteProfile(tenant, id string, cacheRead, cacheWrite return nil, err } if cacheWrite { - if errCh := Cache.Set(context.TODO(), utils.CacheRouteProfiles, tntID, rpp, nil, + if errCh := Cache.Set(ctx, utils.CacheRouteProfiles, tntID, rpp, nil, cacheCommit(transactionID), transactionID); errCh != nil { return nil, errCh } @@ -1227,22 +1227,22 @@ func (dm *DataManager) GetRouteProfile(tenant, id string, cacheRead, cacheWrite return } -func (dm *DataManager) SetRouteProfile(rpp *RouteProfile, withIndex bool) (err error) { +func (dm *DataManager) SetRouteProfile(ctx *context.Context, rpp *RouteProfile, withIndex bool) (err error) { if dm == nil { return utils.ErrNoDatabaseConn } if withIndex { - if brokenReference := dm.checkFilters(context.TODO(), rpp.Tenant, rpp.FilterIDs); len(brokenReference) != 0 { + if brokenReference := dm.checkFilters(ctx, rpp.Tenant, rpp.FilterIDs); len(brokenReference) != 0 { // if we get a broken filter do not set the profile return fmt.Errorf("broken reference to filter: %+v for item with ID: %+v", brokenReference, rpp.TenantID()) } } - oldRpp, err := dm.GetRouteProfile(rpp.Tenant, rpp.ID, true, false, utils.NonTransactional) + oldRpp, err := dm.GetRouteProfile(ctx, rpp.Tenant, rpp.ID, true, false, utils.NonTransactional) if err != nil && err != utils.ErrNotFound { return err } - if err = dm.DataDB().SetRouteProfileDrv(rpp); err != nil { + if err = dm.DataDB().SetRouteProfileDrv(ctx, rpp); err != nil { return err } if withIndex { @@ -1250,13 +1250,13 @@ func (dm *DataManager) SetRouteProfile(rpp *RouteProfile, withIndex bool) (err e if oldRpp != nil { oldFiltersIDs = &oldRpp.FilterIDs } - if err := updatedIndexes(context.TODO(), dm, utils.CacheRouteFilterIndexes, rpp.Tenant, + if err := updatedIndexes(ctx, dm, utils.CacheRouteFilterIndexes, rpp.Tenant, utils.EmptyString, rpp.ID, oldFiltersIDs, rpp.FilterIDs, false); err != nil { return err } } if itm := config.CgrConfig().DataDbCfg().Items[utils.MetaRouteProfiles]; itm.Replicate { - err = replicate(context.TODO(), dm.connMgr, config.CgrConfig().DataDbCfg().RplConns, + err = replicate(ctx, dm.connMgr, config.CgrConfig().DataDbCfg().RplConns, config.CgrConfig().DataDbCfg().RplFiltered, utils.RouteProfilePrefix, rpp.TenantID(), // this are used to get the host IDs from cache utils.ReplicatorSv1SetRouteProfile, @@ -1272,27 +1272,27 @@ func (dm *DataManager) RemoveRouteProfile(tenant, id string, withIndex bool) (er if dm == nil { return utils.ErrNoDatabaseConn } - oldRpp, err := dm.GetRouteProfile(tenant, id, true, false, utils.NonTransactional) + oldRpp, err := dm.GetRouteProfile(ctx, tenant, id, true, false, utils.NonTransactional) if err != nil && err != utils.ErrNotFound { return err } - if err = dm.DataDB().RemoveRouteProfileDrv(tenant, id); err != nil { + if err = dm.DataDB().RemoveRouteProfileDrv(ctx, tenant, id); err != nil { return } if oldRpp == nil { return utils.ErrNotFound } if withIndex { - if err = removeIndexFiltersItem(context.TODO(), dm, utils.CacheRouteFilterIndexes, tenant, id, oldRpp.FilterIDs); err != nil { + if err = removeIndexFiltersItem(ctx, dm, utils.CacheRouteFilterIndexes, tenant, id, oldRpp.FilterIDs); err != nil { return } - if err = removeItemFromFilterIndex(context.TODO(), dm, utils.CacheRouteFilterIndexes, + if err = removeItemFromFilterIndex(ctx, dm, utils.CacheRouteFilterIndexes, tenant, utils.EmptyString, id, oldRpp.FilterIDs); err != nil { return } } if itm := config.CgrConfig().DataDbCfg().Items[utils.MetaRouteProfiles]; itm.Replicate { - replicate(context.TODO(), dm.connMgr, config.CgrConfig().DataDbCfg().RplConns, + replicate(ctx, dm.connMgr, config.CgrConfig().DataDbCfg().RplConns, config.CgrConfig().DataDbCfg().RplFiltered, utils.RouteProfilePrefix, utils.ConcatenatedKey(tenant, id), // this are used to get the host IDs from cache utils.ReplicatorSv1RemoveRouteProfile, @@ -1384,7 +1384,7 @@ func (dm *DataManager) SetAttributeProfile(ctx *context.Context, ap *AttributePr if oldAP != nil { oldFiltersIDs = &oldAP.FilterIDs } - if err := updatedIndexes(context.TODO(), dm, utils.CacheAttributeFilterIndexes, ap.Tenant, + if err := updatedIndexes(ctx, dm, utils.CacheAttributeFilterIndexes, ap.Tenant, utils.EmptyString, ap.ID, oldFiltersIDs, ap.FilterIDs, false); err != nil { return err } diff --git a/engine/filters.go b/engine/filters.go index 8ae23eefe..fbe985b3a 100644 --- a/engine/filters.go +++ b/engine/filters.go @@ -110,17 +110,17 @@ func verifyPrefixes(rule *FilterRule, prefixes []string) (hasPrefix bool) { //LazyPass is almost the same as Pass except that it verify if the //Element of the Values from FilterRules has as prefix one of the pathPrfxs -func (fS *FilterS) LazyPass(tenant string, filterIDs []string, +func (fS *FilterS) LazyPass(ctx *context.Context, tenant string, filterIDs []string, ev utils.DataProvider, pathPrfxs []string) (pass bool, lazyCheckRules []*FilterRule, err error) { if len(filterIDs) == 0 { return true, nil, nil } pass = true - dDP := newDynamicDP(context.TODO(), fS.cfg.FilterSCfg().ResourceSConns, fS.cfg.FilterSCfg().StatSConns, + dDP := newDynamicDP(ctx, fS.cfg.FilterSCfg().ResourceSConns, fS.cfg.FilterSCfg().StatSConns, fS.cfg.FilterSCfg().AdminSConns, tenant, ev) for _, fltrID := range filterIDs { var f *Filter - f, err = fS.dm.GetFilter(context.TODO(), tenant, fltrID, + f, err = fS.dm.GetFilter(ctx, tenant, fltrID, true, true, utils.NonTransactional) if err != nil { if err == utils.ErrNotFound { @@ -133,7 +133,7 @@ func (fS *FilterS) LazyPass(tenant string, filterIDs []string, lazyCheckRules = append(lazyCheckRules, rule) continue } - if pass, err = rule.Pass(context.TODO(), dDP); err != nil || !pass { + if pass, err = rule.Pass(ctx, dDP); err != nil || !pass { return } } diff --git a/engine/filters_test.go b/engine/filters_test.go index a2914d139..eabd5c679 100644 --- a/engine/filters_test.go +++ b/engine/filters_test.go @@ -799,7 +799,7 @@ func TestPassPartial(t *testing.T) { fEv := utils.MapStorage{} fEv.Set([]string{utils.MetaReq}, passEvent) prefixes := []string{utils.DynamicDataPrefix + utils.MetaReq} - if pass, ruleList, err := filterS.LazyPass("cgrates.org", + if pass, ruleList, err := filterS.LazyPass(context.Background(), "cgrates.org", []string{"*string:~*req.Account:1007"}, fEv, prefixes); err != nil { t.Errorf(err.Error()) } else if !pass { @@ -808,7 +808,7 @@ func TestPassPartial(t *testing.T) { t.Errorf("Expecting: %+v, received: %+v", 0, len(ruleList)) } // in PartialPass we verify the filters matching the prefixes - if pass, ruleList, err := filterS.LazyPass("cgrates.org", + if pass, ruleList, err := filterS.LazyPass(context.Background(), "cgrates.org", []string{"*string:~*req.Account:1007", "*string:~*vars.Field1:Val1"}, fEv, prefixes); err != nil { t.Errorf(err.Error()) } else if !pass { @@ -816,7 +816,7 @@ func TestPassPartial(t *testing.T) { } else if len(ruleList) != 1 { t.Errorf("Expecting: %+v, received: %+v", 1, len(ruleList)) } - if pass, ruleList, err := filterS.LazyPass("cgrates.org", + if pass, ruleList, err := filterS.LazyPass(context.Background(), "cgrates.org", []string{"*string:~*req.Account:1010", "*string:~*vars.Field1:Val1"}, fEv, prefixes); err != nil { t.Errorf(err.Error()) } else if pass { diff --git a/engine/libindex.go b/engine/libindex.go index cc7009ee1..4ca3d5f64 100644 --- a/engine/libindex.go +++ b/engine/libindex.go @@ -665,7 +665,7 @@ func UpdateFilterIndex(apiCtx *context.Context, dm *DataManager, oldFlt, newFlt idxSlice := indx.AsSlice() if _, err = ComputeIndexes(apiCtx, dm, newFlt.Tenant, utils.EmptyString, idxItmType, // compute all the indexes for afected items &idxSlice, utils.NonTransactional, func(tnt, id, ctx string) (*[]string, error) { - rt, e := dm.GetRouteProfile(tnt, id, true, false, utils.NonTransactional) + rt, e := dm.GetRouteProfile(apiCtx, tnt, id, true, false, utils.NonTransactional) if e != nil { return nil, e } diff --git a/engine/libroutes.go b/engine/libroutes.go index fe74c3922..0da12f2e2 100644 --- a/engine/libroutes.go +++ b/engine/libroutes.go @@ -23,6 +23,8 @@ import ( "sort" "strings" + "github.com/cgrates/birpc/context" + "github.com/cgrates/cgrates/utils" ) @@ -216,7 +218,7 @@ func (sRoutes *SortedRoutes) AsNavigableMap() (nm *utils.DataNode) { // RoutesSorter is the interface which needs to be implemented by routes sorters type RoutesSorter interface { - SortRoutes(string, map[string]*Route, *utils.CGREvent, *optsGetRoutes) (*SortedRoutes, error) + SortRoutes(*context.Context, string, map[string]*Route, *utils.CGREvent, *optsGetRoutes) (*SortedRoutes, error) } // NewRouteSortDispatcher constructs RouteSortDispatcher @@ -236,13 +238,13 @@ func NewRouteSortDispatcher(lcrS *RouteService) (rsd RouteSortDispatcher) { // and dispatch requests to them type RouteSortDispatcher map[string]RoutesSorter -func (ssd RouteSortDispatcher) SortRoutes(prflID, strategy string, +func (ssd RouteSortDispatcher) SortRoutes(ctx *context.Context, prflID, strategy string, suppls map[string]*Route, suplEv *utils.CGREvent, extraOpts *optsGetRoutes) (sortedRoutes *SortedRoutes, err error) { sd, has := ssd[strategy] if !has { return nil, fmt.Errorf("unsupported sorting strategy: %s", strategy) } - if sortedRoutes, err = sd.SortRoutes(prflID, suppls, suplEv, extraOpts); err != nil { + if sortedRoutes, err = sd.SortRoutes(ctx, prflID, suppls, suplEv, extraOpts); err != nil { return } if len(sortedRoutes.Routes) == 0 { diff --git a/engine/route_highestcost.go b/engine/route_highestcost.go index 9346dea3c..02cb20dfd 100644 --- a/engine/route_highestcost.go +++ b/engine/route_highestcost.go @@ -21,6 +21,8 @@ package engine import ( "fmt" + "github.com/cgrates/birpc/context" + "github.com/cgrates/cgrates/utils" ) @@ -35,7 +37,7 @@ type HightCostSorter struct { rS *RouteService } -func (hcs *HightCostSorter) SortRoutes(prflID string, routes map[string]*Route, +func (hcs *HightCostSorter) SortRoutes(ctx *context.Context, prflID string, routes map[string]*Route, ev *utils.CGREvent, extraOpts *optsGetRoutes) (sortedRoutes *SortedRoutes, err error) { sortedRoutes = &SortedRoutes{ProfileID: prflID, Sorting: hcs.sorting, @@ -47,7 +49,7 @@ func (hcs *HightCostSorter) SortRoutes(prflID string, routes map[string]*Route, utils.RouteS, route.ID)) return nil, utils.NewErrMandatoryIeMissing("RatingPlanIDs or AccountIDs") } - if srtSpl, pass, err := hcs.rS.populateSortingData(ev, route, extraOpts); err != nil { + if srtSpl, pass, err := hcs.rS.populateSortingData(ctx, ev, route, extraOpts); err != nil { return nil, err } else if pass && srtSpl != nil { sortedRoutes.Routes = append(sortedRoutes.Routes, srtSpl) diff --git a/engine/route_leastcost.go b/engine/route_leastcost.go index 23a6e48e4..7c322c540 100644 --- a/engine/route_leastcost.go +++ b/engine/route_leastcost.go @@ -21,6 +21,8 @@ package engine import ( "fmt" + "github.com/cgrates/birpc/context" + "github.com/cgrates/cgrates/utils" ) @@ -35,7 +37,7 @@ type LeastCostSorter struct { rS *RouteService } -func (lcs *LeastCostSorter) SortRoutes(prflID string, routes map[string]*Route, +func (lcs *LeastCostSorter) SortRoutes(ctx *context.Context, prflID string, routes map[string]*Route, ev *utils.CGREvent, extraOpts *optsGetRoutes) (sortedRoutes *SortedRoutes, err error) { sortedRoutes = &SortedRoutes{ProfileID: prflID, Sorting: lcs.sorting, @@ -47,7 +49,7 @@ func (lcs *LeastCostSorter) SortRoutes(prflID string, routes map[string]*Route, utils.RouteS, s.ID)) return nil, utils.NewErrMandatoryIeMissing("RatingPlanIDs or AccountIDs") } - if srtSpl, pass, err := lcs.rS.populateSortingData(ev, s, extraOpts); err != nil { + if srtSpl, pass, err := lcs.rS.populateSortingData(ctx, ev, s, extraOpts); err != nil { return nil, err } else if pass && srtSpl != nil { sortedRoutes.Routes = append(sortedRoutes.Routes, srtSpl) diff --git a/engine/route_load_distribution.go b/engine/route_load_distribution.go index a7d71a3be..25fff57ec 100644 --- a/engine/route_load_distribution.go +++ b/engine/route_load_distribution.go @@ -21,6 +21,8 @@ package engine import ( "fmt" + "github.com/cgrates/birpc/context" + "github.com/cgrates/cgrates/utils" ) @@ -37,7 +39,7 @@ type LoadDistributionSorter struct { } // SortRoutes . -func (ws *LoadDistributionSorter) SortRoutes(prflID string, +func (ws *LoadDistributionSorter) SortRoutes(ctx *context.Context, prflID string, routes map[string]*Route, suplEv *utils.CGREvent, extraOpts *optsGetRoutes) (sortedRoutes *SortedRoutes, err error) { sortedRoutes = &SortedRoutes{ ProfileID: prflID, @@ -52,7 +54,7 @@ func (ws *LoadDistributionSorter) SortRoutes(prflID string, utils.RouteS, route.ID)) return nil, utils.NewErrMandatoryIeMissing("StatIDs") } - if srtSpl, pass, err := ws.rS.populateSortingData(suplEv, route, extraOpts); err != nil { + if srtSpl, pass, err := ws.rS.populateSortingData(ctx, suplEv, route, extraOpts); err != nil { return nil, err } else if pass && srtSpl != nil { // Add the ratio in SortingData so we can used it later in SortLoadDistribution diff --git a/engine/route_qos.go b/engine/route_qos.go index a16abb477..a5ee768bd 100644 --- a/engine/route_qos.go +++ b/engine/route_qos.go @@ -19,6 +19,7 @@ along with this program. If not, see package engine import ( + "github.com/cgrates/birpc/context" "github.com/cgrates/cgrates/utils" ) @@ -33,13 +34,13 @@ type QOSRouteSorter struct { rS *RouteService } -func (qos *QOSRouteSorter) SortRoutes(prflID string, routes map[string]*Route, +func (qos *QOSRouteSorter) SortRoutes(ctx *context.Context, prflID string, routes map[string]*Route, ev *utils.CGREvent, extraOpts *optsGetRoutes) (sortedRoutes *SortedRoutes, err error) { sortedRoutes = &SortedRoutes{ProfileID: prflID, Sorting: qos.sorting, Routes: make([]*SortedRoute, 0)} for _, route := range routes { - if srtSpl, pass, err := qos.rS.populateSortingData(ev, route, extraOpts); err != nil { + if srtSpl, pass, err := qos.rS.populateSortingData(ctx, ev, route, extraOpts); err != nil { return nil, err } else if pass && srtSpl != nil { sortedRoutes.Routes = append(sortedRoutes.Routes, srtSpl) diff --git a/engine/route_reas.go b/engine/route_reas.go index 16f57dbf6..553f96e52 100644 --- a/engine/route_reas.go +++ b/engine/route_reas.go @@ -21,6 +21,8 @@ package engine import ( "fmt" + "github.com/cgrates/birpc/context" + "github.com/cgrates/cgrates/utils" ) @@ -35,7 +37,7 @@ type ResourceAscendentSorter struct { rS *RouteService } -func (ws *ResourceAscendentSorter) SortRoutes(prflID string, +func (ws *ResourceAscendentSorter) SortRoutes(ctx *context.Context, prflID string, routes map[string]*Route, suplEv *utils.CGREvent, extraOpts *optsGetRoutes) (sortedRoutes *SortedRoutes, err error) { sortedRoutes = &SortedRoutes{ProfileID: prflID, Sorting: ws.sorting, @@ -47,7 +49,7 @@ func (ws *ResourceAscendentSorter) SortRoutes(prflID string, utils.RouteS, route.ID)) return nil, utils.NewErrMandatoryIeMissing("ResourceIDs") } - if srtSpl, pass, err := ws.rS.populateSortingData(suplEv, route, extraOpts); err != nil { + if srtSpl, pass, err := ws.rS.populateSortingData(ctx, suplEv, route, extraOpts); err != nil { return nil, err } else if pass && srtSpl != nil { sortedRoutes.Routes = append(sortedRoutes.Routes, srtSpl) diff --git a/engine/route_reds.go b/engine/route_reds.go index 23b84c439..d4cf3293a 100644 --- a/engine/route_reds.go +++ b/engine/route_reds.go @@ -21,6 +21,7 @@ package engine import ( "fmt" + "github.com/cgrates/birpc/context" "github.com/cgrates/cgrates/utils" ) @@ -35,7 +36,7 @@ type ResourceDescendentSorter struct { rS *RouteService } -func (ws *ResourceDescendentSorter) SortRoutes(prflID string, +func (ws *ResourceDescendentSorter) SortRoutes(ctx *context.Context, prflID string, routes map[string]*Route, suplEv *utils.CGREvent, extraOpts *optsGetRoutes) (sortedRoutes *SortedRoutes, err error) { sortedRoutes = &SortedRoutes{ProfileID: prflID, Sorting: ws.sorting, @@ -47,7 +48,7 @@ func (ws *ResourceDescendentSorter) SortRoutes(prflID string, utils.RouteS, route.ID)) return nil, utils.NewErrMandatoryIeMissing("ResourceIDs") } - if srtSpl, pass, err := ws.rS.populateSortingData(suplEv, route, extraOpts); err != nil { + if srtSpl, pass, err := ws.rS.populateSortingData(ctx, suplEv, route, extraOpts); err != nil { return nil, err } else if pass && srtSpl != nil { sortedRoutes.Routes = append(sortedRoutes.Routes, srtSpl) diff --git a/engine/route_weight.go b/engine/route_weight.go index b914e58fe..4a3c0d2e6 100644 --- a/engine/route_weight.go +++ b/engine/route_weight.go @@ -19,6 +19,7 @@ along with this program. If not, see package engine import ( + "github.com/cgrates/birpc/context" "github.com/cgrates/cgrates/utils" ) @@ -33,13 +34,13 @@ type WeightSorter struct { rS *RouteService } -func (ws *WeightSorter) SortRoutes(prflID string, +func (ws *WeightSorter) SortRoutes(ctx *context.Context, prflID string, routes map[string]*Route, suplEv *utils.CGREvent, extraOpts *optsGetRoutes) (sortedRoutes *SortedRoutes, err error) { sortedRoutes = &SortedRoutes{ProfileID: prflID, Sorting: ws.sorting, Routes: make([]*SortedRoute, 0)} for _, route := range routes { - if srtRoute, pass, err := ws.rS.populateSortingData(suplEv, route, extraOpts); err != nil { + if srtRoute, pass, err := ws.rS.populateSortingData(ctx, suplEv, route, extraOpts); err != nil { return nil, err } else if pass && srtRoute != nil { sortedRoutes.Routes = append(sortedRoutes.Routes, srtRoute) diff --git a/engine/routes.go b/engine/routes.go index 082cfb4f0..c47284ec3 100644 --- a/engine/routes.go +++ b/engine/routes.go @@ -140,12 +140,12 @@ func (rpS *RouteService) Shutdown() { } // matchingRouteProfilesForEvent returns ordered list of matching resources which are active by the time of the call -func (rpS *RouteService) matchingRouteProfilesForEvent(tnt string, ev *utils.CGREvent) (matchingRPrf []*RouteProfile, err error) { +func (rpS *RouteService) matchingRouteProfilesForEvent(ctx *context.Context, tnt string, ev *utils.CGREvent) (matchingRPrf []*RouteProfile, err error) { evNm := utils.MapStorage{ utils.MetaReq: ev.Event, utils.MetaOpts: ev.APIOpts, } - rPrfIDs, err := MatchingItemIDsForEvent(context.TODO(), evNm, + rPrfIDs, err := MatchingItemIDsForEvent(ctx, evNm, rpS.cgrcfg.RouteSCfg().StringIndexedFields, rpS.cgrcfg.RouteSCfg().PrefixIndexedFields, rpS.cgrcfg.RouteSCfg().SuffixIndexedFields, @@ -158,14 +158,14 @@ func (rpS *RouteService) matchingRouteProfilesForEvent(tnt string, ev *utils.CGR } matchingRPrf = make([]*RouteProfile, 0, len(rPrfIDs)) for lpID := range rPrfIDs { - rPrf, err := rpS.dm.GetRouteProfile(tnt, lpID, true, true, utils.NonTransactional) + rPrf, err := rpS.dm.GetRouteProfile(ctx, tnt, lpID, true, true, utils.NonTransactional) if err != nil { if err == utils.ErrNotFound { continue } return nil, err } - if pass, err := rpS.filterS.Pass(context.TODO(), tnt, rPrf.FilterIDs, + if pass, err := rpS.filterS.Pass(ctx, tnt, rPrf.FilterIDs, evNm); err != nil { return nil, err } else if !pass { @@ -272,13 +272,13 @@ func (rpS *RouteService) costForEvent(ev *utils.CGREvent, // statMetrics will query a list of statIDs and return composed metric values // first metric found is always returned -func (rpS *RouteService) statMetrics(statIDs []string, tenant string) (stsMetric map[string]float64, err error) { +func (rpS *RouteService) statMetrics(ctx *context.Context, statIDs []string, tenant string) (stsMetric map[string]float64, err error) { stsMetric = make(map[string]float64) provStsMetrics := make(map[string][]float64) if len(rpS.cgrcfg.RouteSCfg().StatSConns) != 0 { for _, statID := range statIDs { var metrics map[string]float64 - if err = rpS.connMgr.Call(context.TODO(), rpS.cgrcfg.RouteSCfg().StatSConns, utils.StatSv1GetQueueFloatMetrics, + if err = rpS.connMgr.Call(ctx, rpS.cgrcfg.RouteSCfg().StatSConns, utils.StatSv1GetQueueFloatMetrics, &utils.TenantIDWithAPIOpts{TenantID: &utils.TenantID{Tenant: tenant, ID: statID}}, &metrics); err != nil && err.Error() != utils.ErrNotFound.Error() { utils.Logger.Warning( @@ -302,14 +302,14 @@ func (rpS *RouteService) statMetrics(statIDs []string, tenant string) (stsMetric // statMetricsForLoadDistribution will query a list of statIDs and return the sum of metrics // first metric found is always returned -func (rpS *RouteService) statMetricsForLoadDistribution(statIDs []string, tenant string) (result float64, err error) { +func (rpS *RouteService) statMetricsForLoadDistribution(ctx *context.Context, statIDs []string, tenant string) (result float64, err error) { provStsMetrics := make(map[string][]float64) if len(rpS.cgrcfg.RouteSCfg().StatSConns) != 0 { for _, statID := range statIDs { // check if we get an ID in the following form (StatID:MetricID) statWithMetric := strings.Split(statID, utils.InInFieldSep) var metrics map[string]float64 - if err = rpS.connMgr.Call(context.TODO(), + if err = rpS.connMgr.Call(ctx, rpS.cgrcfg.RouteSCfg().StatSConns, utils.StatSv1GetQueueFloatMetrics, &utils.TenantIDWithAPIOpts{ @@ -348,11 +348,11 @@ func (rpS *RouteService) statMetricsForLoadDistribution(statIDs []string, tenant } // resourceUsage returns sum of all resource usages out of list -func (rpS *RouteService) resourceUsage(resIDs []string, tenant string) (tUsage float64, err error) { +func (rpS *RouteService) resourceUsage(ctx *context.Context, resIDs []string, tenant string) (tUsage float64, err error) { if len(rpS.cgrcfg.RouteSCfg().ResourceSConns) != 0 { for _, resID := range resIDs { var res Resource - if err = rpS.connMgr.Call(context.TODO(), rpS.cgrcfg.RouteSCfg().ResourceSConns, utils.ResourceSv1GetResource, + if err = rpS.connMgr.Call(ctx, rpS.cgrcfg.RouteSCfg().ResourceSConns, utils.ResourceSv1GetResource, &utils.TenantIDWithAPIOpts{TenantID: &utils.TenantID{Tenant: tenant, ID: resID}}, &res); err != nil && err.Error() != utils.ErrNotFound.Error() { utils.Logger.Warning( fmt.Sprintf("<%s> error: %s getting resource for ID : %s", utils.RouteS, err.Error(), resID)) @@ -364,7 +364,7 @@ func (rpS *RouteService) resourceUsage(resIDs []string, tenant string) (tUsage f return } -func (rpS *RouteService) populateSortingData(ev *utils.CGREvent, route *Route, +func (rpS *RouteService) populateSortingData(ctx *context.Context, ev *utils.CGREvent, route *Route, extraOpts *optsGetRoutes) (srtRoute *SortedRoute, pass bool, err error) { sortedSpl := &SortedRoute{ RouteID: route.ID, @@ -410,7 +410,7 @@ func (rpS *RouteService) populateSortingData(ev *utils.CGREvent, route *Route, //in case we have *load strategy we use statMetricsForLoadDistribution function to calculate the result if len(route.StatIDs) != 0 { if extraOpts.sortingStragety == utils.MetaLoad { - metricSum, err := rpS.statMetricsForLoadDistribution(route.StatIDs, ev.Tenant) //create metric map for route + metricSum, err := rpS.statMetricsForLoadDistribution(ctx, route.StatIDs, ev.Tenant) //create metric map for route if err != nil { if extraOpts.ignoreErrors { utils.Logger.Warning( @@ -423,7 +423,7 @@ func (rpS *RouteService) populateSortingData(ev *utils.CGREvent, route *Route, sortedSpl.SortingData[utils.Load] = metricSum sortedSpl.sortingDataF64[utils.Load] = metricSum } else { - metricSupp, err := rpS.statMetrics(route.StatIDs, ev.Tenant) //create metric map for route + metricSupp, err := rpS.statMetrics(ctx, route.StatIDs, ev.Tenant) //create metric map for route if err != nil { if extraOpts.ignoreErrors { utils.Logger.Warning( @@ -457,7 +457,7 @@ func (rpS *RouteService) populateSortingData(ev *utils.CGREvent, route *Route, } //calculate resourceUsage if len(route.ResourceIDs) != 0 { - resTotalUsage, err := rpS.resourceUsage(route.ResourceIDs, ev.Tenant) + resTotalUsage, err := rpS.resourceUsage(ctx, route.ResourceIDs, ev.Tenant) if err != nil { if extraOpts.ignoreErrors { utils.Logger.Warning( @@ -473,7 +473,7 @@ func (rpS *RouteService) populateSortingData(ev *utils.CGREvent, route *Route, //filter the route if len(route.lazyCheckRules) != 0 { //construct the DP and pass it to filterS - dynDP := newDynamicDP(context.TODO(), rpS.cgrcfg.FilterSCfg().ResourceSConns, rpS.cgrcfg.FilterSCfg().StatSConns, + dynDP := newDynamicDP(ctx, rpS.cgrcfg.FilterSCfg().ResourceSConns, rpS.cgrcfg.FilterSCfg().StatSConns, rpS.cgrcfg.FilterSCfg().AdminSConns, ev.Tenant, utils.MapStorage{ utils.MetaReq: ev.Event, @@ -481,7 +481,7 @@ func (rpS *RouteService) populateSortingData(ev *utils.CGREvent, route *Route, }) for _, rule := range route.lazyCheckRules { // verify the rules remaining from PartialPass - if pass, err = rule.Pass(context.TODO(), dynDP); err != nil { + if pass, err = rule.Pass(ctx, dynDP); err != nil { return nil, false, err } else if !pass { return nil, false, nil @@ -558,7 +558,7 @@ type optsGetRoutes struct { } // V1GetRoutes returns the list of valid routes -func (rpS *RouteService) V1GetRoutes(args *ArgsGetRoutes, reply *SortedRoutesList) (err error) { +func (rpS *RouteService) V1GetRoutes(ctx *context.Context, args *ArgsGetRoutes, reply *SortedRoutesList) (err error) { if args.CGREvent == nil { return utils.NewErrMandatoryIeMissing(utils.CGREventString) } @@ -590,7 +590,7 @@ func (rpS *RouteService) V1GetRoutes(args *ArgsGetRoutes, reply *SortedRoutesLis ProcessRuns: processRuns, } var rplyEv AttrSProcessEventReply - if err := rpS.connMgr.Call(context.TODO(), rpS.cgrcfg.RouteSCfg().AttributeSConns, + if err := rpS.connMgr.Call(ctx, rpS.cgrcfg.RouteSCfg().AttributeSConns, utils.AttributeSv1ProcessEvent, attrArgs, &rplyEv); err == nil && len(rplyEv.AlteredFields) != 0 { args.CGREvent = rplyEv.CGREvent args.APIOpts = rplyEv.APIOpts @@ -599,7 +599,7 @@ func (rpS *RouteService) V1GetRoutes(args *ArgsGetRoutes, reply *SortedRoutesLis } } var sSps SortedRoutesList - if sSps, err = rpS.sortedRoutesForEvent(tnt, args); err != nil { + if sSps, err = rpS.sortedRoutesForEvent(ctx, tnt, args); err != nil { if err != utils.ErrNotFound { err = utils.NewErrServerError(err) } @@ -610,7 +610,7 @@ func (rpS *RouteService) V1GetRoutes(args *ArgsGetRoutes, reply *SortedRoutesLis } // V1GetRouteProfilesForEvent returns the list of valid route profiles -func (rpS *RouteService) V1GetRouteProfilesForEvent(args *utils.CGREvent, reply *[]*RouteProfile) (err error) { +func (rpS *RouteService) V1GetRouteProfilesForEvent(ctx *context.Context, args *utils.CGREvent, reply *[]*RouteProfile) (err error) { if missing := utils.MissingStructFields(args, []string{utils.ID}); len(missing) != 0 { return utils.NewErrMandatoryIeMissing(missing...) } else if args.Event == nil { @@ -620,7 +620,7 @@ func (rpS *RouteService) V1GetRouteProfilesForEvent(args *utils.CGREvent, reply if tnt == utils.EmptyString { tnt = rpS.cgrcfg.GeneralCfg().DefaultTenant } - sPs, err := rpS.matchingRouteProfilesForEvent(tnt, args) + sPs, err := rpS.matchingRouteProfilesForEvent(ctx, tnt, args) if err != nil { if err != utils.ErrNotFound { err = utils.NewErrServerError(err) @@ -633,7 +633,7 @@ func (rpS *RouteService) V1GetRouteProfilesForEvent(args *utils.CGREvent, reply // sortedRoutesForEvent will return the list of valid route IDs // for event based on filters and sorting algorithms -func (rpS *RouteService) sortedRoutesForProfile(tnt string, rPrfl *RouteProfile, ev *utils.CGREvent, +func (rpS *RouteService) sortedRoutesForProfile(ctx *context.Context, tnt string, rPrfl *RouteProfile, ev *utils.CGREvent, pag utils.Paginator, extraOpts *optsGetRoutes) (sortedRoutes *SortedRoutes, err error) { extraOpts.sortingParameters = rPrfl.SortingParameters // populate sortingParameters in extraOpts extraOpts.sortingStragety = rPrfl.Sorting // populate sortingStrategy in extraOpts @@ -645,7 +645,7 @@ func (rpS *RouteService) sortedRoutesForProfile(tnt string, rPrfl *RouteProfile, passedRoutes := make(map[string]*Route) // apply filters for event for _, route := range rPrfl.Routes { - pass, lazyCheckRules, err := rpS.filterS.LazyPass(tnt, + pass, lazyCheckRules, err := rpS.filterS.LazyPass(ctx, tnt, route.FilterIDs, nM, []string{utils.DynamicDataPrefix + utils.MetaReq, utils.DynamicDataPrefix + utils.MetaAccounts, @@ -663,7 +663,7 @@ func (rpS *RouteService) sortedRoutesForProfile(tnt string, rPrfl *RouteProfile, passedRoutes[route.ID] = route } - if sortedRoutes, err = rpS.sorter.SortRoutes(rPrfl.ID, rPrfl.Sorting, + if sortedRoutes, err = rpS.sorter.SortRoutes(ctx, rPrfl.ID, rPrfl.Sorting, passedRoutes, ev, extraOpts); err != nil { return nil, err } @@ -682,12 +682,12 @@ func (rpS *RouteService) sortedRoutesForProfile(tnt string, rPrfl *RouteProfile, // sortedRoutesForEvent will return the list of valid route IDs // for event based on filters and sorting algorithms -func (rpS *RouteService) sortedRoutesForEvent(tnt string, args *ArgsGetRoutes) (sortedRoutes SortedRoutesList, err error) { +func (rpS *RouteService) sortedRoutesForEvent(ctx *context.Context, tnt string, args *ArgsGetRoutes) (sortedRoutes SortedRoutesList, err error) { if _, has := args.CGREvent.Event[utils.Usage]; !has { args.CGREvent.Event[utils.Usage] = time.Minute // make sure we have default set for Usage } var rPrfs []*RouteProfile - if rPrfs, err = rpS.matchingRouteProfilesForEvent(tnt, args.CGREvent); err != nil { + if rPrfs, err = rpS.matchingRouteProfilesForEvent(ctx, tnt, args.CGREvent); err != nil { return } prfCount := len(rPrfs) // if the option is not present return for all profiles @@ -730,7 +730,7 @@ func (rpS *RouteService) sortedRoutesForEvent(tnt string, args *ArgsGetRoutes) ( prfPag.Offset = &offset } var sr *SortedRoutes - if sr, err = rpS.sortedRoutesForProfile(tnt, rPrfl, args.CGREvent, prfPag, extraOpts); err != nil { + if sr, err = rpS.sortedRoutesForProfile(ctx, tnt, rPrfl, args.CGREvent, prfPag, extraOpts); err != nil { return } noSrtRoutes += len(sr.Routes) @@ -743,9 +743,9 @@ func (rpS *RouteService) sortedRoutesForEvent(tnt string, args *ArgsGetRoutes) ( } // V1GetRoutesList returns the list of valid routes -func (rpS *RouteService) V1GetRoutesList(args *ArgsGetRoutes, reply *[]string) (err error) { +func (rpS *RouteService) V1GetRoutesList(ctx *context.Context, args *ArgsGetRoutes, reply *[]string) (err error) { sR := new(SortedRoutesList) - if err = rpS.V1GetRoutes(args, sR); err != nil { + if err = rpS.V1GetRoutes(ctx, args, sR); err != nil { return } *reply = sR.RoutesWithParams() diff --git a/engine/routes_test.go b/engine/routes_test.go index 6f66d3806..1a7d53e28 100644 --- a/engine/routes_test.go +++ b/engine/routes_test.go @@ -276,13 +276,13 @@ func TestRoutesCache(t *testing.T) { dmSPP.SetFilter(context.Background(), fltrSupp3, true) for _, spp := range sppTest { - if err = dmSPP.SetRouteProfile(spp, true); err != nil { + if err = dmSPP.SetRouteProfile(context.Background(), spp, true); err != nil { t.Errorf("Error: %+v", err) } } //Test each route profile from cache for _, spp := range sppTest { - if tempSpp, err := dmSPP.GetRouteProfile(spp.Tenant, + if tempSpp, err := dmSPP.GetRouteProfile(context.Background(), spp.Tenant, spp.ID, true, true, utils.NonTransactional); err != nil { t.Errorf("Error: %+v", err) } else if !reflect.DeepEqual(spp, tempSpp) { @@ -494,20 +494,20 @@ func TestRoutesmatchingRouteProfilesForEvent(t *testing.T) { } dmSPP.SetFilter(context.Background(), fltrSupp3, true) for _, spp := range sppTest { - if err = dmSPP.SetRouteProfile(spp, true); err != nil { + if err = dmSPP.SetRouteProfile(context.Background(), spp, true); err != nil { t.Errorf("Error: %+v", err) } } //Test each route profile from cache for _, spp := range sppTest { - if tempSpp, err := dmSPP.GetRouteProfile(spp.Tenant, + if tempSpp, err := dmSPP.GetRouteProfile(context.Background(), spp.Tenant, spp.ID, true, true, utils.NonTransactional); err != nil { t.Errorf("Error: %+v", err) } else if !reflect.DeepEqual(spp, tempSpp) { t.Errorf("Expecting: %+v, received: %+v", spp, tempSpp) } } - sprf, err := routeService.matchingRouteProfilesForEvent(argsGetRoutes[0].Tenant, argsGetRoutes[0].CGREvent) + sprf, err := routeService.matchingRouteProfilesForEvent(context.Background(), argsGetRoutes[0].Tenant, argsGetRoutes[0].CGREvent) if err != nil { t.Errorf("Error: %+v", err) } @@ -515,7 +515,7 @@ func TestRoutesmatchingRouteProfilesForEvent(t *testing.T) { t.Errorf("Expecting: %+v, received: %+v", sppTest[0], sprf[0]) } - sprf, err = routeService.matchingRouteProfilesForEvent(argsGetRoutes[1].Tenant, argsGetRoutes[1].CGREvent) + sprf, err = routeService.matchingRouteProfilesForEvent(context.Background(), argsGetRoutes[1].Tenant, argsGetRoutes[1].CGREvent) if err != nil { t.Errorf("Error: %+v", err) } @@ -523,7 +523,7 @@ func TestRoutesmatchingRouteProfilesForEvent(t *testing.T) { t.Errorf("Expecting: %+v, received: %+v", sppTest[1], sprf[0]) } - sprf, err = routeService.matchingRouteProfilesForEvent(argsGetRoutes[2].Tenant, argsGetRoutes[2].CGREvent) + sprf, err = routeService.matchingRouteProfilesForEvent(context.Background(), argsGetRoutes[2].Tenant, argsGetRoutes[2].CGREvent) if err != nil { t.Errorf("Error: %+v", err) } @@ -735,13 +735,13 @@ func TestRoutesSortedForEvent(t *testing.T) { } dmSPP.SetFilter(context.Background(), fltrSupp3, true) for _, spp := range sppTest { - if err = dmSPP.SetRouteProfile(spp, true); err != nil { + if err = dmSPP.SetRouteProfile(context.Background(), spp, true); err != nil { t.Errorf("Error: %+v", err) } } //Test each route profile from cache for _, spp := range sppTest { - if tempSpp, err := dmSPP.GetRouteProfile(spp.Tenant, + if tempSpp, err := dmSPP.GetRouteProfile(context.Background(), spp.Tenant, spp.ID, true, true, utils.NonTransactional); err != nil { t.Errorf("Error: %+v", err) } else if !reflect.DeepEqual(spp, tempSpp) { @@ -764,7 +764,7 @@ func TestRoutesSortedForEvent(t *testing.T) { }, }, }} - sprf, err := routeService.sortedRoutesForEvent(argsGetRoutes[0].Tenant, argsGetRoutes[0]) + sprf, err := routeService.sortedRoutesForEvent(context.Background(), argsGetRoutes[0].Tenant, argsGetRoutes[0]) if err != nil { t.Errorf("Error: %+v", err) } @@ -809,7 +809,7 @@ func TestRoutesSortedForEvent(t *testing.T) { }, }} - sprf, err = routeService.sortedRoutesForEvent(argsGetRoutes[1].Tenant, argsGetRoutes[1]) + sprf, err = routeService.sortedRoutesForEvent(context.Background(), argsGetRoutes[1].Tenant, argsGetRoutes[1]) if err != nil { t.Errorf("Error: %+v", err) } @@ -834,7 +834,7 @@ func TestRoutesSortedForEvent(t *testing.T) { }, }} - sprf, err = routeService.sortedRoutesForEvent(argsGetRoutes[2].Tenant, argsGetRoutes[2]) + sprf, err = routeService.sortedRoutesForEvent(context.Background(), argsGetRoutes[2].Tenant, argsGetRoutes[2]) if err != nil { t.Errorf("Error: %+v", err) } @@ -1046,13 +1046,13 @@ func TestRoutesSortedForEventWithLimit(t *testing.T) { dmSPP.SetFilter(context.Background(), fltrSupp3, true) for _, spp := range sppTest { - if err = dmSPP.SetRouteProfile(spp, true); err != nil { + if err = dmSPP.SetRouteProfile(context.Background(), spp, true); err != nil { t.Errorf("Error: %+v", err) } } //Test each route profile from cache for _, spp := range sppTest { - if tempSpp, err := dmSPP.GetRouteProfile(spp.Tenant, + if tempSpp, err := dmSPP.GetRouteProfile(context.Background(), spp.Tenant, spp.ID, true, true, utils.NonTransactional); err != nil { t.Errorf("Error: %+v", err) } else if !reflect.DeepEqual(spp, tempSpp) { @@ -1089,7 +1089,7 @@ func TestRoutesSortedForEventWithLimit(t *testing.T) { argsGetRoutes[1].Paginator = utils.Paginator{ Limit: utils.IntPointer(2), } - sprf, err := routeService.sortedRoutesForEvent(argsGetRoutes[1].Tenant, argsGetRoutes[1]) + sprf, err := routeService.sortedRoutesForEvent(context.Background(), argsGetRoutes[1].Tenant, argsGetRoutes[1]) if err != nil { t.Errorf("Error: %+v", err) } @@ -1301,13 +1301,13 @@ func TestRoutesSortedForEventWithOffset(t *testing.T) { dmSPP.SetFilter(context.Background(), fltrSupp3, true) for _, spp := range sppTest { - if err = dmSPP.SetRouteProfile(spp, true); err != nil { + if err = dmSPP.SetRouteProfile(context.Background(), spp, true); err != nil { t.Errorf("Error: %+v", err) } } //Test each route profile from cache for _, spp := range sppTest { - if tempSpp, err := dmSPP.GetRouteProfile(spp.Tenant, + if tempSpp, err := dmSPP.GetRouteProfile(context.Background(), spp.Tenant, spp.ID, true, true, utils.NonTransactional); err != nil { t.Errorf("Error: %+v", err) } else if !reflect.DeepEqual(spp, tempSpp) { @@ -1334,7 +1334,7 @@ func TestRoutesSortedForEventWithOffset(t *testing.T) { argsGetRoutes[1].Paginator = utils.Paginator{ Offset: utils.IntPointer(2), } - sprf, err := routeService.sortedRoutesForEvent(argsGetRoutes[1].Tenant, argsGetRoutes[1]) + sprf, err := routeService.sortedRoutesForEvent(context.Background(), argsGetRoutes[1].Tenant, argsGetRoutes[1]) if err != nil { t.Errorf("Error: %+v", err) } @@ -1546,13 +1546,13 @@ func TestRoutesSortedForEventWithLimitAndOffset(t *testing.T) { dmSPP.SetFilter(context.Background(), fltrSupp3, true) for _, spp := range sppTest { - if err = dmSPP.SetRouteProfile(spp, true); err != nil { + if err = dmSPP.SetRouteProfile(context.Background(), spp, true); err != nil { t.Errorf("Error: %+v", err) } } //Test each route profile from cache for _, spp := range sppTest { - if tempSpp, err := dmSPP.GetRouteProfile(spp.Tenant, + if tempSpp, err := dmSPP.GetRouteProfile(context.Background(), spp.Tenant, spp.ID, true, true, utils.NonTransactional); err != nil { t.Errorf("Error: %+v", err) } else if !reflect.DeepEqual(spp, tempSpp) { @@ -1580,7 +1580,7 @@ func TestRoutesSortedForEventWithLimitAndOffset(t *testing.T) { Limit: utils.IntPointer(1), Offset: utils.IntPointer(1), } - sprf, err := routeService.sortedRoutesForEvent(argsGetRoutes[1].Tenant, argsGetRoutes[1]) + sprf, err := routeService.sortedRoutesForEvent(context.Background(), argsGetRoutes[1].Tenant, argsGetRoutes[1]) if err != nil { t.Errorf("Error: %+v", err) } @@ -1826,13 +1826,13 @@ func TestRoutesAsOptsGetRoutesMaxCost(t *testing.T) { dmSPP.SetFilter(context.Background(), fltrSupp3, true) for _, spp := range sppTest { - if err = dmSPP.SetRouteProfile(spp, true); err != nil { + if err = dmSPP.SetRouteProfile(context.Background(), spp, true); err != nil { t.Errorf("Error: %+v", err) } } //Test each route profile from cache for _, spp := range sppTest { - if tempSpp, err := dmSPP.GetRouteProfile(spp.Tenant, + if tempSpp, err := dmSPP.GetRouteProfile(context.Background(), spp.Tenant, spp.ID, true, true, utils.NonTransactional); err != nil { t.Errorf("Error: %+v", err) } else if !reflect.DeepEqual(spp, tempSpp) { @@ -1841,7 +1841,7 @@ func TestRoutesAsOptsGetRoutesMaxCost(t *testing.T) { } routeService.cgrcfg.RouteSCfg().IndexedSelects = false - sprf, err := routeService.matchingRouteProfilesForEvent(argsGetRoutes[0].Tenant, argsGetRoutes[0].CGREvent) + sprf, err := routeService.matchingRouteProfilesForEvent(context.Background(), argsGetRoutes[0].Tenant, argsGetRoutes[0].CGREvent) if err != nil { t.Errorf("Error: %+v", err) } @@ -1849,7 +1849,7 @@ func TestRoutesAsOptsGetRoutesMaxCost(t *testing.T) { t.Errorf("Expecting: %+v, received: %+v", sppTest[0], sprf[0]) } - sprf, err = routeService.matchingRouteProfilesForEvent(argsGetRoutes[1].Tenant, argsGetRoutes[1].CGREvent) + sprf, err = routeService.matchingRouteProfilesForEvent(context.Background(), argsGetRoutes[1].Tenant, argsGetRoutes[1].CGREvent) if err != nil { t.Errorf("Error: %+v", err) } @@ -1857,7 +1857,7 @@ func TestRoutesAsOptsGetRoutesMaxCost(t *testing.T) { t.Errorf("Expecting: %+v, received: %+v", sppTest[1], sprf[0]) } - sprf, err = routeService.matchingRouteProfilesForEvent(argsGetRoutes[2].Tenant, argsGetRoutes[2].CGREvent) + sprf, err = routeService.matchingRouteProfilesForEvent(context.Background(), argsGetRoutes[2].Tenant, argsGetRoutes[2].CGREvent) if err != nil { t.Errorf("Error: %+v", err) } @@ -2069,13 +2069,13 @@ func TestRoutesMatchWithIndexFalse(t *testing.T) { dmSPP.SetFilter(context.Background(), fltrSupp3, true) for _, spp := range sppTest { - if err = dmSPP.SetRouteProfile(spp, true); err != nil { + if err = dmSPP.SetRouteProfile(context.Background(), spp, true); err != nil { t.Errorf("Error: %+v", err) } } //Test each route profile from cache for _, spp := range sppTest { - if tempSpp, err := dmSPP.GetRouteProfile(spp.Tenant, + if tempSpp, err := dmSPP.GetRouteProfile(context.Background(), spp.Tenant, spp.ID, true, true, utils.NonTransactional); err != nil { t.Errorf("Error: %+v", err) } else if !reflect.DeepEqual(spp, tempSpp) { @@ -2084,7 +2084,7 @@ func TestRoutesMatchWithIndexFalse(t *testing.T) { } routeService.cgrcfg.RouteSCfg().IndexedSelects = false - sprf, err := routeService.matchingRouteProfilesForEvent(argsGetRoutes[0].Tenant, argsGetRoutes[0].CGREvent) + sprf, err := routeService.matchingRouteProfilesForEvent(context.Background(), argsGetRoutes[0].Tenant, argsGetRoutes[0].CGREvent) if err != nil { t.Errorf("Error: %+v", err) } @@ -2092,7 +2092,7 @@ func TestRoutesMatchWithIndexFalse(t *testing.T) { t.Errorf("Expecting: %+v, received: %+v", sppTest[0], sprf[0]) } - sprf, err = routeService.matchingRouteProfilesForEvent(argsGetRoutes[1].Tenant, argsGetRoutes[1].CGREvent) + sprf, err = routeService.matchingRouteProfilesForEvent(context.Background(), argsGetRoutes[1].Tenant, argsGetRoutes[1].CGREvent) if err != nil { t.Errorf("Error: %+v", err) } @@ -2100,7 +2100,7 @@ func TestRoutesMatchWithIndexFalse(t *testing.T) { t.Errorf("Expecting: %+v, received: %+v", sppTest[1], sprf[0]) } - sprf, err = routeService.matchingRouteProfilesForEvent(argsGetRoutes[2].Tenant, argsGetRoutes[2].CGREvent) + sprf, err = routeService.matchingRouteProfilesForEvent(context.Background(), argsGetRoutes[2].Tenant, argsGetRoutes[2].CGREvent) if err != nil { t.Errorf("Error: %+v", err) } @@ -2193,10 +2193,10 @@ func TestRoutesSortedForEventWithLimitAndOffset2(t *testing.T) { dm: dmSPP, cfg: defaultCfg}, defaultCfg, nil) for _, spp := range sppTest { - if err = dmSPP.SetRouteProfile(spp, true); err != nil { + if err = dmSPP.SetRouteProfile(context.Background(), spp, true); err != nil { t.Errorf("Error: %+v", err) } - if tempSpp, err := dmSPP.GetRouteProfile(spp.Tenant, + if tempSpp, err := dmSPP.GetRouteProfile(context.Background(), spp.Tenant, spp.ID, true, true, utils.NonTransactional); err != nil { t.Errorf("Error: %+v", err) } else if !reflect.DeepEqual(spp, tempSpp) { @@ -2242,7 +2242,7 @@ func TestRoutesSortedForEventWithLimitAndOffset2(t *testing.T) { Limit: utils.IntPointer(2), Offset: utils.IntPointer(1), } - sprf, err := routeService.sortedRoutesForEvent(argsGetRoutes.Tenant, argsGetRoutes) + sprf, err := routeService.sortedRoutesForEvent(context.Background(), argsGetRoutes.Tenant, argsGetRoutes) if err != nil { t.Errorf("Error: %+v", err) } diff --git a/engine/storage_interface.go b/engine/storage_interface.go index 4f067c3d5..39a821a0e 100644 --- a/engine/storage_interface.go +++ b/engine/storage_interface.go @@ -75,9 +75,9 @@ type DataDB interface { GetFilterDrv(ctx *context.Context, tnt string, id string) (*Filter, error) SetFilterDrv(ctx *context.Context, f *Filter) error RemoveFilterDrv(string, string) error - GetRouteProfileDrv(string, string) (*RouteProfile, error) - SetRouteProfileDrv(*RouteProfile) error - RemoveRouteProfileDrv(string, string) error + GetRouteProfileDrv(*context.Context, string, string) (*RouteProfile, error) + SetRouteProfileDrv(*context.Context, *RouteProfile) error + RemoveRouteProfileDrv(*context.Context, string, string) error GetAttributeProfileDrv(ctx *context.Context, tnt string, id string) (*AttributeProfile, error) SetAttributeProfileDrv(ctx *context.Context, attr *AttributeProfile) error RemoveAttributeProfileDrv(*context.Context, string, string) error diff --git a/engine/storage_internal_datadb.go b/engine/storage_internal_datadb.go index 236a41dd3..1ff756f24 100644 --- a/engine/storage_internal_datadb.go +++ b/engine/storage_internal_datadb.go @@ -333,7 +333,7 @@ func (iDB *InternalDB) RemoveFilterDrv(tenant, id string) (err error) { return } -func (iDB *InternalDB) GetRouteProfileDrv(tenant, id string) (spp *RouteProfile, err error) { +func (iDB *InternalDB) GetRouteProfileDrv(ctx *context.Context, tenant, id string) (spp *RouteProfile, err error) { x, ok := Cache.Get(utils.CacheRouteProfiles, utils.ConcatenatedKey(tenant, id)) if !ok || x == nil { return nil, utils.ErrNotFound @@ -341,7 +341,7 @@ func (iDB *InternalDB) GetRouteProfileDrv(tenant, id string) (spp *RouteProfile, return x.(*RouteProfile), nil } -func (iDB *InternalDB) SetRouteProfileDrv(spp *RouteProfile) (err error) { +func (iDB *InternalDB) SetRouteProfileDrv(ctx *context.Context, spp *RouteProfile) (err error) { if err = spp.Compile(); err != nil { return } @@ -350,7 +350,7 @@ func (iDB *InternalDB) SetRouteProfileDrv(spp *RouteProfile) (err error) { return } -func (iDB *InternalDB) RemoveRouteProfileDrv(tenant, id string) (err error) { +func (iDB *InternalDB) RemoveRouteProfileDrv(ctx *context.Context, tenant, id string) (err error) { Cache.RemoveWithoutReplicate(utils.CacheRouteProfiles, utils.ConcatenatedKey(tenant, id), cacheCommit(utils.NonTransactional), utils.NonTransactional) return diff --git a/engine/storage_mongo_datadb.go b/engine/storage_mongo_datadb.go index 29a9b64d4..06cfc0e23 100644 --- a/engine/storage_mongo_datadb.go +++ b/engine/storage_mongo_datadb.go @@ -1012,9 +1012,9 @@ func (ms *MongoStorage) RemoveFilterDrv(tenant, id string) (err error) { }) } -func (ms *MongoStorage) GetRouteProfileDrv(tenant, id string) (r *RouteProfile, err error) { +func (ms *MongoStorage) GetRouteProfileDrv(ctx *context.Context, tenant, id string) (r *RouteProfile, err error) { r = new(RouteProfile) - err = ms.query(context.TODO(), func(sctx mongo.SessionContext) (err error) { + err = ms.query(ctx, func(sctx mongo.SessionContext) (err error) { cur := ms.getCol(ColRts).FindOne(sctx, bson.M{"tenant": tenant, "id": id}) if err := cur.Decode(r); err != nil { r = nil @@ -1028,8 +1028,8 @@ func (ms *MongoStorage) GetRouteProfileDrv(tenant, id string) (r *RouteProfile, return } -func (ms *MongoStorage) SetRouteProfileDrv(r *RouteProfile) (err error) { - return ms.query(context.TODO(), func(sctx mongo.SessionContext) (err error) { +func (ms *MongoStorage) SetRouteProfileDrv(ctx *context.Context, r *RouteProfile) (err error) { + return ms.query(ctx, func(sctx mongo.SessionContext) (err error) { _, err = ms.getCol(ColRts).UpdateOne(sctx, bson.M{"tenant": r.Tenant, "id": r.ID}, bson.M{"$set": r}, options.Update().SetUpsert(true), @@ -1038,8 +1038,8 @@ func (ms *MongoStorage) SetRouteProfileDrv(r *RouteProfile) (err error) { }) } -func (ms *MongoStorage) RemoveRouteProfileDrv(tenant, id string) (err error) { - return ms.query(context.TODO(), func(sctx mongo.SessionContext) (err error) { +func (ms *MongoStorage) RemoveRouteProfileDrv(ctx *context.Context, tenant, id string) (err error) { + return ms.query(ctx, func(sctx mongo.SessionContext) (err error) { dr, err := ms.getCol(ColRts).DeleteOne(sctx, bson.M{"tenant": tenant, "id": id}) if dr.DeletedCount == 0 { return utils.ErrNotFound diff --git a/engine/storage_redis.go b/engine/storage_redis.go index b3067d0ec..d278023b4 100644 --- a/engine/storage_redis.go +++ b/engine/storage_redis.go @@ -565,7 +565,7 @@ func (rs *RedisStorage) RemoveFilterDrv(tenant, id string) (err error) { return rs.Cmd(nil, redisDEL, utils.FilterPrefix+utils.ConcatenatedKey(tenant, id)) } -func (rs *RedisStorage) GetRouteProfileDrv(tenant, id string) (r *RouteProfile, err error) { +func (rs *RedisStorage) GetRouteProfileDrv(ctx *context.Context, tenant, id string) (r *RouteProfile, err error) { var values []byte if err = rs.Cmd(&values, redisGET, utils.RouteProfilePrefix+utils.ConcatenatedKey(tenant, id)); err != nil { return @@ -577,7 +577,7 @@ func (rs *RedisStorage) GetRouteProfileDrv(tenant, id string) (r *RouteProfile, return } -func (rs *RedisStorage) SetRouteProfileDrv(r *RouteProfile) (err error) { +func (rs *RedisStorage) SetRouteProfileDrv(ctx *context.Context, r *RouteProfile) (err error) { var result []byte if result, err = rs.ms.Marshal(r); err != nil { return @@ -585,7 +585,7 @@ func (rs *RedisStorage) SetRouteProfileDrv(r *RouteProfile) (err error) { return rs.Cmd(nil, redisSET, utils.RouteProfilePrefix+utils.ConcatenatedKey(r.Tenant, r.ID), string(result)) } -func (rs *RedisStorage) RemoveRouteProfileDrv(tenant, id string) (err error) { +func (rs *RedisStorage) RemoveRouteProfileDrv(ctx *context.Context, tenant, id string) (err error) { return rs.Cmd(nil, redisDEL, utils.RouteProfilePrefix+utils.ConcatenatedKey(tenant, id)) } diff --git a/engine/tpreader.go b/engine/tpreader.go index 2b3aae924..523a546c1 100644 --- a/engine/tpreader.go +++ b/engine/tpreader.go @@ -454,7 +454,7 @@ func (tpr *TpReader) WriteToDatabase(verbose, disableReverse bool) (err error) { if th, err = APItoRouteProfile(tpTH, tpr.timezone); err != nil { return } - if err = tpr.dm.SetRouteProfile(th, true); err != nil { + if err = tpr.dm.SetRouteProfile(context.TODO(), th, true); err != nil { return } if verbose { diff --git a/loaders/loader.go b/loaders/loader.go index b330f56b8..424c7866a 100644 --- a/loaders/loader.go +++ b/loaders/loader.go @@ -450,7 +450,7 @@ func (ldr *Loader) storeLoadedData(ctx *context.Context, loaderType string, } // get IDs so we can reload in cache ids = append(ids, spPrf.TenantID()) - if err := ldr.dm.SetRouteProfile(spPrf, true); err != nil { + if err := ldr.dm.SetRouteProfile(ctx, spPrf, true); err != nil { return err } cacheArgs[utils.RouteProfileIDs] = ids diff --git a/loaders/loader_test.go b/loaders/loader_test.go index 57dc09269..52a4cd7cd 100644 --- a/loaders/loader_test.go +++ b/loaders/loader_test.go @@ -904,7 +904,7 @@ func TestLoaderProcessRoutes(t *testing.T) { eSp.Routes[j].ID+strings.Join(eSp.Routes[j].FilterIDs, utils.ConcatenatedKeySep)) < 0 }) - aps, err := ldr.dm.GetRouteProfile("cgrates.org", "RoutePrf1", + aps, err := ldr.dm.GetRouteProfile(context.Background(), "cgrates.org", "RoutePrf1", true, false, utils.NonTransactional) if err != nil { t.Fatal(err) @@ -3655,7 +3655,7 @@ cgrates.org,ROUTES_REM_1 Tenant: "cgrates.org", ID: "ROUTES_REM_1", } - if err := ldr.dm.SetRouteProfile(expRoutes, true); err != nil { + if err := ldr.dm.SetRouteProfile(context.Background(), expRoutes, true); err != nil { t.Error(err) } if err := ldr.removeContent(context.Background(), utils.MetaRoutes, utils.EmptyString); err != nil { diff --git a/migrator/routes.go b/migrator/routes.go index 18c0ec688..ca93c5ac5 100644 --- a/migrator/routes.go +++ b/migrator/routes.go @@ -87,7 +87,7 @@ func (m *Migrator) migrateFromSupplierToRoute() (err error) { if spp == nil || m.dryRun { continue } - if err := m.dmOut.DataManager().SetRouteProfile(convertSupplierToRoute(spp), true); err != nil { + if err := m.dmOut.DataManager().SetRouteProfile(context.TODO(), convertSupplierToRoute(spp), true); err != nil { return err } m.stats[utils.Routes]++ @@ -120,14 +120,14 @@ func (m *Migrator) migrateCurrentRouteProfile() (err error) { if len(tntID) < 2 { return fmt.Errorf("invalid key <%s> when migrating route profiles", id) } - rPrf, err := m.dmIN.DataManager().GetRouteProfile(tntID[0], tntID[1], false, false, utils.NonTransactional) + rPrf, err := m.dmIN.DataManager().GetRouteProfile(context.TODO(), tntID[0], tntID[1], false, false, utils.NonTransactional) if err != nil { return err } if rPrf == nil || m.dryRun { continue } - if err := m.dmOut.DataManager().SetRouteProfile(rPrf, true); err != nil { + if err := m.dmOut.DataManager().SetRouteProfile(context.TODO(), rPrf, true); err != nil { return err } if err := m.dmIN.DataManager().RemoveRouteProfile(tntID[0], tntID[1], true); err != nil { @@ -185,13 +185,13 @@ func (m *Migrator) migrateRouteProfiles() (err error) { break } if !m.dryRun { - if err = m.dmIN.DataManager().SetRouteProfile(v2, true); err != nil { + if err = m.dmIN.DataManager().SetRouteProfile(context.TODO(), v2, true); err != nil { return } } m.stats[utils.Routes]++ } - // All done, update version wtih current one + // All done, update version with current one if err = m.setVersions(utils.Routes); err != nil { return } diff --git a/services/routes.go b/services/routes.go index 23b9a367b..dc3546f55 100644 --- a/services/routes.go +++ b/services/routes.go @@ -22,6 +22,8 @@ import ( "fmt" "sync" + "github.com/cgrates/cgrates/apis" + "github.com/cgrates/birpc" "github.com/cgrates/cgrates/config" "github.com/cgrates/cgrates/cores" @@ -59,8 +61,8 @@ type RouteService struct { server *cores.Server connMgr *engine.ConnManager - routeS *engine.RouteService - // rpc *v1.RouteSv1 + routeS *engine.RouteService + rpc *apis.RouteSv1 connChan chan birpc.ClientConnector anz *AnalyzerService srvDep map[string]*sync.WaitGroup @@ -86,11 +88,12 @@ func (routeS *RouteService) Start() (err error) { routeS.routeS = engine.NewRouteService(datadb, filterS, routeS.cfg, routeS.connMgr) utils.Logger.Info(fmt.Sprintf("<%s> starting <%s> subsystem", utils.CoreS, utils.RouteS)) - // routeS.rpc = v1.NewRouteSv1(routeS.routeS) - // if !routeS.cfg.DispatcherSCfg().Enabled { - // routeS.server.RpcRegister(routeS.rpc) - // } - // routeS.connChan <- routeS.anz.GetInternalCodec(routeS.rpc, utils.RouteS) + routeS.rpc = apis.NewRouteSv1(routeS.routeS) + srv, _ := birpc.NewService(routeS.rpc, "", false) + if !routeS.cfg.DispatcherSCfg().Enabled { + routeS.server.RpcRegister(srv) + } + routeS.connChan <- routeS.anz.GetInternalCodec(srv, utils.RouteS) return } @@ -105,8 +108,8 @@ func (routeS *RouteService) Shutdown() (err error) { defer routeS.Unlock() routeS.routeS.Shutdown() //we don't verify the error because shutdown never returns an error routeS.routeS = nil - // routeS.rpc = nil - //<-routeS.connChan + routeS.rpc = nil + <-routeS.connChan return }