From 40b166893af7e9c5a99c4aa7a4ced2ceb0201f79 Mon Sep 17 00:00:00 2001 From: porosnicuadrian Date: Fri, 16 Jul 2021 17:14:52 +0300 Subject: [PATCH] Fixed code for health index functions + tests --- apier/v1/filter_indexes_health_it_test.go | 210 +++++++++++++++++- .../dispatchers/DispatcherHosts.csv | 2 +- engine/datamanager.go | 3 +- engine/libindex_health.go | 2 +- utils/consts.go | 2 + 5 files changed, 213 insertions(+), 6 deletions(-) diff --git a/apier/v1/filter_indexes_health_it_test.go b/apier/v1/filter_indexes_health_it_test.go index b75295515..7a2a88e9b 100644 --- a/apier/v1/filter_indexes_health_it_test.go +++ b/apier/v1/filter_indexes_health_it_test.go @@ -49,10 +49,19 @@ var ( testV1FIdxHResetStorDb, testV1FIdxHLoadFromFolderTutorial, testV1FIdxGetThresholdsIndexesHealth, + /* testV1FIdxGetResourcesIndexesHealth, testV1FIdxGetStatsIndexesHealth, testV1FIdxGetRoutesIndexesHealth, testV1FIdxGetChargersIndexesHealth, + testV1FIdxGetAttributesIndexesHealth, + + testV1FIdxHdxInitDataDb, + testV1FIdxHResetStorDb, + testV1FIdxHLoadFromFolderDispatchers, + testV1FIdxHGetDispatchersIndexesHealth, + + */ testV1FIdxHStopEngine, } @@ -238,6 +247,24 @@ func testV1FIdxGetThresholdsIndexesHealth(t *testing.T) { t.Errorf("UNexpected reply returned") } + // check all the indexes for thresholds + expiIdx = []string{ + "*string:*req.Account:1001:THD_ACNT_1001", + "*string:*req.Account:1004:TEST_PROFILE1", + "*prefix:*opts.Destination:+442:TEST_PROFILE1", + "*prefix:*opts.Destination:+554:TEST_PROFILE1", + } + if err := tFIdxHRpc.Call(utils.APIerSv1GetFilterIndexes, &AttrGetFilterIndexes{ + ItemType: utils.MetaThresholds, + }, &result); err != nil { + t.Error(err) + } else { + sort.Strings(result) + sort.Strings(expiIdx) + if !reflect.DeepEqual(expiIdx, result) { + t.Errorf("Expecting: %+v, received: %+v", expiIdx, result) + } + } //as we removed the object, the index specified is removed too, so the health of the indexes is fine expRPly = &engine.FilterIHReply{ MissingIndexes: map[string][]string{}, @@ -555,7 +582,6 @@ func testV1FIdxGetChargersIndexesHealth(t *testing.T) { } } - /* // all indexes are set and points to their objects correctly expRPly := &engine.FilterIHReply{ MissingIndexes: map[string][]string{}, @@ -584,9 +610,189 @@ func testV1FIdxGetChargersIndexesHealth(t *testing.T) { t.Errorf("Unexpected reply returned") } - */ + //as we removed the object, the index specified is removed too, so the health of the indexes is fine + if err := tFIdxHRpc.Call(utils.APIerSv1GetRoutesIndexesHealth, + args, &rplyFl); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(rplyFl, expRPly) { + t.Errorf("Expected %+v, received %+v", utils.ToJSON(expRPly), utils.ToJSON(rplyFl)) + } } +func testV1FIdxGetAttributesIndexesHealth(t *testing.T) { + // Attributes.csv from tutorial tariffplan got lots of profiles, so we will not set another attribute for this test + // check all the indexes for attributes + // simpleauth context + expIdx := []string{ + "*string:*req.Account:1001:ATTR_1001_SIMPLEAUTH", + "*string:*req.Account:1002:ATTR_1002_SIMPLEAUTH", + "*string:*req.Account:1003:ATTR_1003_SIMPLEAUTH", + } + var result []string + if err := tFIdxHRpc.Call(utils.APIerSv1GetFilterIndexes, &AttrGetFilterIndexes{ + ItemType: utils.MetaAttributes, + Context: "simpleauth", + }, &result); err != nil { + t.Error(err) + } else { + sort.Strings(result) + sort.Strings(expIdx) + if !reflect.DeepEqual(expIdx, result) { + t.Errorf("Expecting: %+v, received: %+v", expIdx, result) + } + } + + //*sessions context + expIdx = []string{ + "*string:*req.Account:1001:ATTR_1001_SESSIONAUTH", + "*string:*req.Account:1002:ATTR_1002_SESSIONAUTH", + "*string:*req.Account:1003:ATTR_1003_SESSIONAUTH", + } + if err := tFIdxHRpc.Call(utils.APIerSv1GetFilterIndexes, &AttrGetFilterIndexes{ + ItemType: utils.MetaAttributes, + Context: utils.MetaSessionS, + }, &result); err != nil { + t.Error(err) + } else { + sort.Strings(result) + sort.Strings(expIdx) + if !reflect.DeepEqual(expIdx, result) { + t.Errorf("Expecting: %+v, received: %+v", expIdx, result) + } + } + + // *any context tenant: cgrates.org + expIdx = []string{ + "*string:*req.SubscriberId:1006:ATTR_ACC_ALIAS", + } + if err := tFIdxHRpc.Call(utils.APIerSv1GetFilterIndexes, &AttrGetFilterIndexes{ + ItemType: utils.MetaAttributes, + Context: utils.MetaAny, + }, &result); err != nil { + t.Error(err) + } else { + sort.Strings(result) + sort.Strings(expIdx) + if !reflect.DeepEqual(expIdx, result) { + t.Errorf("Expecting: %+v, received: %+v", expIdx, result) + } + } + + // *any context tenant: cgrates.com + expIdx = []string{ + "*string:*req.SubscriberId:1006:ATTR_TNT_ALIAS", + "*string:*req.Account:1001:ATTR_TNT_1001", + "*string:*req.Account:testDiamInitWithSessionDisconnect:ATTR_TNT_DISC", + "*string:*req.SubscriberId:testDiamItEmulateTerminate:ATTR_ACC_EMULATE_TERMINATE", + } + if err := tFIdxHRpc.Call(utils.APIerSv1GetFilterIndexes, &AttrGetFilterIndexes{ + Tenant: "cgrates.com", + ItemType: utils.MetaAttributes, + Context: utils.MetaAny, + }, &result); err != nil { + t.Error(err) + } else { + sort.Strings(result) + sort.Strings(expIdx) + if !reflect.DeepEqual(expIdx, result) { + t.Errorf("Expecting: %+v, received: %+v", expIdx, result) + } + } + + //as we removed the object, the index specified is removed too, so the health of the indexes is fine + expRPly := &engine.FilterIHReply{ + MissingIndexes: map[string][]string{}, + BrokenIndexes: map[string][]string{}, + MissingFilters: map[string][]string{}, + } + args := &engine.IndexHealthArgsWith3Ch{} + var rplyFl *engine.FilterIHReply + if err := tFIdxHRpc.Call(utils.APIerSv1GetAttributesIndexesHealth, + args, &rplyFl); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(rplyFl, expRPly) { + t.Errorf("Expected %+v, received %+v", utils.ToJSON(expRPly), utils.ToJSON(rplyFl)) + } +} + +func testV1FIdxHLoadFromFolderDispatchers(t *testing.T) { + var reply string + attrs := &utils.AttrLoadTpFromFolder{FolderPath: path.Join(*dataDir, "tariffplans", "dispatchers")} + if err := tFIdxHRpc.Call(utils.APIerSv1LoadTariffPlanFromFolder, attrs, &reply); err != nil { + t.Error(err) + } + time.Sleep(100 * time.Millisecond) +} + +func testV1FIdxHGetDispatchersIndexesHealth(t *testing.T) { + // *any context + expIdx := []string{ + "*none:*any:*any:PING1", + "*string:*req.EventName:NonexistingHost:PING2", + "*string:*req.EventName:Event1:EVENT1", + "*string:*req.EventName:RoundRobin:EVENT2", + "*string:*req.EventName:Random:EVENT3", + "*string:*req.EventName:Broadcast:EVENT4", + "*string:*req.EventName:Internal:EVENT5", + "*string:*opts.*method:DispatcherSv1.GetProfilesForEvent:EVENT6", + "*string:*opts.EventType:LoadDispatcher:EVENT7", + } + var result []string + if err := tFIdxHRpc.Call(utils.APIerSv1GetFilterIndexes, &AttrGetFilterIndexes{ + ItemType: utils.MetaDispatchers, + Context: utils.MetaAny, + }, &result); err != nil { + t.Error(err) + } else { + sort.Strings(result) + sort.Strings(expIdx) + if !reflect.DeepEqual(expIdx, result) { + t.Errorf("Expecting: %+v, received: %+v", expIdx, result) + } + } + + // all indexes are set and points to their objects correctly + expRPly := &engine.FilterIHReply{ + MissingIndexes: map[string][]string{}, + BrokenIndexes: map[string][]string{}, + MissingFilters: map[string][]string{}, + } + args := &engine.IndexHealthArgsWith3Ch{} + var rplyFl *engine.FilterIHReply + if err := tFIdxHRpc.Call(utils.APIerSv1GetDispatchersIndexesHealth, + args, &rplyFl); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(rplyFl, expRPly) { + t.Errorf("Expected %+v, received %+v", utils.ToJSON(expRPly), utils.ToJSON(rplyFl)) + } + + + var reply string + // removing a profile + their indexes + if err := tFIdxHRpc.Call(utils.APIerSv1RemoveDispatcherProfile, + &utils.TenantIDWithAPIOpts{ + TenantID: &utils.TenantID{ + Tenant: "cgrates.org", + ID: "PING2", + }, + }, &reply); err != nil { + t.Error(err) + } else if reply != utils. OK { + t.Errorf("Unexpected reply returned") + } + + //as we removed the object, the index specified is removed too, so the health of the indexes is fine + args = &engine.IndexHealthArgsWith3Ch{} + if err := tFIdxHRpc.Call(utils.APIerSv1GetDispatchersIndexesHealth, + args, &rplyFl); err != nil { + t.Error(err) + } else if !reflect.DeepEqual(rplyFl, expRPly) { + t.Errorf("Expected %+v, received %+v", utils.ToJSON(expRPly), utils.ToJSON(rplyFl)) + } +} + + + func testV1FIdxHStopEngine(t *testing.T) { if err := engine.KillEngine(100); err != nil { t.Error(err) diff --git a/data/tariffplans/dispatchers/DispatcherHosts.csv b/data/tariffplans/dispatchers/DispatcherHosts.csv index 66a1f1e68..e46289ffc 100644 --- a/data/tariffplans/dispatchers/DispatcherHosts.csv +++ b/data/tariffplans/dispatchers/DispatcherHosts.csv @@ -1,5 +1,5 @@ #Tenant[0],ID[1],Address[2],Transport[3],Synchronous[4],ConnectAttempts[5],Reconnects[6],ConnectTimeout[7],ReplyTimeout[8],Tls[9],ClientKey[10],ClientCertificate[11],CaCertificate[12] cgrates.org,SELF,*internal,,false,1,3,"1m","2m",true,"key1","cert1","ca_cert1" cgrates.org,ALL,127.0.0.1:6012,*json,false,1,3,"1m","2m",true,"key2","cert2","ca_cert2" -cgrates.org,ALL2,127.0.0.1:7012,*json,false,1,3,"1m","2m",true,"key3","cert3","ca_cert3", +cgrates.org,ALL2,127.0.0.1:7012,*json,false,1,3,"1m","2m",true,"key3","cert3","ca_cert3" cgrates.org,NonexistingHost,127.0.0.1:10012,*json,false,1,3,"1m","2m",true,"key4","cert4","ca_cert4" \ No newline at end of file diff --git a/engine/datamanager.go b/engine/datamanager.go index 1c03f21af..2a6eaf4df 100644 --- a/engine/datamanager.go +++ b/engine/datamanager.go @@ -863,12 +863,11 @@ func (dm *DataManager) GetThresholdProfile(tenant, id string, cacheRead, cacheWr } if err != nil { err = utils.CastRPCErr(err) - if err == utils.ErrNotFound && cacheWrite { + if err == utils.ErrNotFound && cacheWrite && dm.dataDB.GetStorageType() != utils.INTERNAL { if errCh := Cache.Set(utils.CacheThresholdProfiles, tntID, nil, nil, cacheCommit(transactionID), transactionID); errCh != nil { return nil, errCh } - } return nil, err } diff --git a/engine/libindex_health.go b/engine/libindex_health.go index 7cc9def50..b885b5527 100644 --- a/engine/libindex_health.go +++ b/engine/libindex_health.go @@ -473,7 +473,7 @@ func updateFilterIHMisingIndx(dm *DataManager, fltrCache, fltrIdxCache *ltcache. if len(filterIDs) == 0 { // no filter so check the *none:*any:*any index idxKey := utils.ConcatenatedKey(utils.MetaNone, utils.MetaAny, utils.MetaAny) var rcvIndx utils.StringSet - if rcvIndx, err = getIHFltrIdxFromCache(dm, nil, indxType, tntCtx, idxKey); err != nil { + if rcvIndx, err = getIHFltrIdxFromCache(dm, fltrCache, indxType, tntCtx, idxKey); err != nil { if err != utils.ErrNotFound { return } diff --git a/utils/consts.go b/utils/consts.go index 8a6d74637..b24184b8d 100644 --- a/utils/consts.go +++ b/utils/consts.go @@ -1285,6 +1285,8 @@ const ( APIerSv1GetStatsIndexesHealth = "APIerSv1.GetStatsIndexesHealth" APIerSv1GetRoutesIndexesHealth = "APIerSv1.GetRoutesIndexesHealth" APIerSv1GetChargersIndexesHealth = "APIerSv1.GetChargersIndexesHealth" + APIerSv1GetAttributesIndexesHealth = "APIerSv1.GetAttributesIndexesHealth" + APIerSv1GetDispatchersIndexesHealth = "APIerSv1.GetDispatchersIndexesHealth" APIerSv1Ping = "APIerSv1.Ping" APIerSv1SetDispatcherProfile = "APIerSv1.SetDispatcherProfile" APIerSv1GetDispatcherProfile = "APIerSv1.GetDispatcherProfile"