mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
`ClientConnector` is no longer defined within `rpcclient` in its latest
version. It has been changed to be obtained from the `cgrates/birpc`
library instead.
Replaced `net/rpc` with `cgrates/birpc` and `net/rpc/jsonrpc` with
`cgrates/birpc/jsonrpc` libraries.
The implementations of `CallBiRPC()` and `Handlers()` were removed,
along with the methods associated with them.
The `rpcclient.BIRPCConector` and the methods prefixed with `BiRPC` were
removed from the `BiRPClient` interface.
The `BiRPClient` interface was renamed to `BIRPCClient`, although not
sure if needed (seems useful just to test if the structure is correct).
`rpcclient.BiRPCConector` has been replaced with `context.ClientConnector`,
which is now passed alongside `context.Context` within the same struct
(`cgrates/birpc/context.Context`). Consequently, functions that were
previously relying on it are now receiving the context instead. The
changes were made in the following functions:
- `engine/connmanager.go` - `*ConnManager.Call`
- `engine/connmanager.go` - `*ConnManager.getConn`
- `engine/connmanager.go` - `*ConnManager.getConnWithConfig`
- `engine/libengine.go` - `NewRPCPool`
- `engine/libengine.go` - `NewRPCConnection`
- `agents/libagents.go` - `processRequest`
Compilation errors related to the `rpcclient.NewRPCClient` function were
resolved by adding the missing `context`, `max_reconnect_interval`, and
`delayFunc` parameters. Additionally, context was added to all calls made
by the client. An effort was made to avoid passing hardcoded values as
much as possible, and extra flags were added where necessary for cgr
binaries.
The `max_reconnect_interval` parameter is now passed from parent
functions, which required adjustments to the function signature.
A new context field was added to all agent objects to ensure access to
it before sending it to the `connmanager's Call`, effectively replacing
`birpcclient`. Although an alternative would have been to create the
new service and add it to the context right before passing it to the
handlers, the chosen approach is definitely more comfortable.
With the addition of a context field for the SIP servers agents, an
additional error needed to be handled, coming from the creation of the
service. Agent constructors within the services package log errors as
they occur and return. Alternate solutions considered were either
shutting down the engine instead of returning, or just logging the
occurrence as a warning, particularly when the `ctx.Client` isn't
required, especially in cases where bidirectional connections are not
needed. For the latter option, it's crucial to return the object with
the error rather than nil or to make the error nil immediately after
logging.
Context has been integrated into all internal Call implementations to
ensure the objects conform to the `birpc.ClientConnector` interface.
These implementations will be removed in the near future as all service
objects are being wrapped in a `birpc.Service` type that satisfies the
`birpc.ClientConnector` interface. Currently, they are being retained
as a reference in case of any unexpected issues that arise.
Ensured that the `birpc.Service` wrapped service objects are passed to
the internal channel getters rather than the objects themselves.
Add context.TODO() to all \*ConnManager.Call function calls. To be
replaced with the context passed to the Method, when available.
For all `*ConnManager.Call` function calls, `context.TODO()` has been
added. This will be replaced with the context passed to the method when
it becomes available.
The value returned by StringGetOpts is now passed directly to the
FirstNonEmpty function, instead of being assigned to a variable
first.
The implementation of the `*AnalyzerService.GetInternalBiRPCCodec`
function has been removed from the services package. Additionally,
the AnalyzerBiRPCConnector type definition and its associated methods
have been removed.
The codec implementation has been revised to include the following
changes:
- `rpc.ServerCodec` -> `birpc.ServerCodec`;
- `rpc2.ServerCodec` -> `birpc.BirpcCodec`;
- `rpc2.Request` -> `birpc.Request`;
- `rpc2.Response` -> `birpc.Response`;
- The constructors for json and gob birpc codecs in `cenkalti/rpc`
have been replaced with ones from the `birpc/jsonrpc` library;
- The gob codec implementation has been removed in favor of the
version already implemented in the birpc external library.
The server implementation has been updated with the following changes:
- A field that represents a simple RPC server has been added to the
Server struct;
- Both the simple and bidirectional RPC servers are now initialized
inside the Server constructor, eliminating the need for nil checks;
- Usage of `net/rpc` and `cenkalti/rpc2` has been replaced with
`cgrates/birpc`;
- Additional `(Bi)RPCUnregisterName` methods have been added;
- The implementations for (bi)json/gob servers have been somewhat
simplified.
Before deleting the Call functions and using the `birpc.NewService`
method to register the methods for all cgrates components, update the
Call functions to satisfy the `birpc.ClientConnector` interface. This
way it will be a bit safer. Had to be done for SessionS though.
The `BiRPCCall` method has been removed from coreutils.go. The
`RPCCall` and `APIerRPCCall` methods are also to be removed in the
future.
Ensured that all methods for `SessionSv1` and `SessionS` have the
correct function signature with context. The same adjustments were made
for the session dispatcher methods and for the `SessionSv1Interface`.
Also removed sessionsbirpc.go and smgbirpc.go files.
Implemented the following methods to help with the registration of
methods across all subsystems:
- `NewServiceWithName`;
- `NewDispatcherService` for all dispatcher methods;
- `NewService` for the remaining methods that are already named
correctly.
Compared to the constructor from the external library, these also make
sure that the naming of the methods is consistent with our constants.
Added context to the Call methods for the mock client connectors (used
in tests).
Removed unused rpc fields from inside the following services:
- EeS
- LoaderS
- ResourceS
- RouteS
- StatS
- ThresholdS
- SessionS
- CoreS
Updated the methods implementing the logic for API methods to align
with the latest changes, ensuring consistency and correctness. The
modifications include:
- Adjusting the function signature to the new format
(ctx, args, reply).
- Prefixing names with 'V*' to indicate that they are utilized by
or registered as APIs.
- Containing the complete logic within the methods, enabling APIs
to call them and return their reply directly.
The subsystems affected by these changes are detailed as follows:
- CoreS: Additional methods were implementing utilizing the
existing ones. Though modifying them directly was possible, certain
methods (e.g., StopCPUProfiling()) were used elsewhere and not as
RPC requests.
- CDRs: Renamed V1CountCDRs to V1GetCDRsCount.
- StatS: V1GetQueueFloatMetrics, V1GetQueueStringMetrics,
V1GetStatQueue accept different arguments compared to API functions
(opted to register StatSv1 instead).
- ResourceS: Renamed V1ResourcesForEvent to V1GetResourcesForEvent
to align with API naming.
- DispatcherS: Renamed V1GetProfilesForEvent to
DispatcherSv1GetProfilesForEvent.
- For the rest, adding context to the function signature was enough.
In the unit tests, wrapping the object within a biprc.Service is now
ensured before passing it to the internal connections map under the
corresponding key.
Some tests that are covering error cases, are also checking the other
return value besides the error. That check has been removed since it
is redundant.
Revised the RPC/BiRPC clients' constructors (for use in tests)
A different approach has been chosen for the handling of ping methods
within subsystems. Instead of defining the same structure in every file,
the ping methods were added inside the Service constructor function.
Though the existing Ping methods were left as they were, they will be
removed in the future.
An additional method has been implemented to register the Ping method
from outside of the engine package.
Implemented Sleep and CapsError methods for SessionS (before they were
exclusively for bidirectional use, I believe).
A specific issue has been fixed within the CapsError SessionSv1 API
implementation, which is designed to overwrite methods that cannot be
allocated due to the threshold limit being reached. Previously, it was
deallocating when writing the response, even when a spot hadn't been
allocated in the first place (due to the cap being hit). The reason
behind this, especially why the test was passing before, still needs
to be looked into, as the problem should have occurred from before.
Implement `*SessionSv1.RegisterInternalBiJSONConn` method in apier.
All agent methods have been registered under the SessionSv1 name. For
the correct method names, the leading "V1" prefix has been trimmed
using the `birpc.NewServiceWithMethodsRename` function.
Revise the RegisterRpcParams function to populate the parameters
while relying on the `*birpc.Service` type instead. This will
automatically also deal with the validation. At the moment,
any error encountered is logged without being returned. Might
be changed in the future.
Inside the cgrRPCAction function, `mapstructure.Decode`'s output parameter
is now guaranteed to always be a pointer.
Updated go.mod and go.sum.
Fixed some typos.
598 lines
18 KiB
Go
598 lines
18 KiB
Go
/*
|
|
Real-time Online/Offline Charging System (OCS) for Telecom & ISP environments
|
|
Copyright (C) ITsysCOM GmbH
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU General Public License as published by
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
(at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
*/
|
|
|
|
package engine
|
|
|
|
import (
|
|
"fmt"
|
|
"runtime"
|
|
"sort"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/cgrates/birpc/context"
|
|
"github.com/cgrates/cgrates/config"
|
|
"github.com/cgrates/cgrates/guardian"
|
|
"github.com/cgrates/cgrates/utils"
|
|
)
|
|
|
|
// ThresholdProfileWithAPIOpts is used in replicatorV1 for dispatcher
|
|
type ThresholdProfileWithAPIOpts struct {
|
|
*ThresholdProfile
|
|
APIOpts map[string]any
|
|
}
|
|
|
|
// ThresholdProfile the profile for threshold
|
|
type ThresholdProfile struct {
|
|
Tenant string
|
|
ID string
|
|
FilterIDs []string
|
|
ActivationInterval *utils.ActivationInterval // Time when this limit becomes active and expires
|
|
MaxHits int
|
|
MinHits int
|
|
MinSleep time.Duration
|
|
Blocker bool // blocker flag to stop processing on filters matched
|
|
Weight float64 // Weight to sort the thresholds
|
|
ActionIDs []string
|
|
Async bool
|
|
|
|
lkID string // holds the reference towards guardian lock key
|
|
}
|
|
|
|
// TenantID returns the concatenated key beteen tenant and ID
|
|
func (tp *ThresholdProfile) TenantID() string {
|
|
return utils.ConcatenatedKey(tp.Tenant, tp.ID)
|
|
}
|
|
|
|
// thresholdProfileLockKey returns the ID used to lock a ThresholdProfile with guardian
|
|
func thresholdProfileLockKey(tnt, id string) string {
|
|
return utils.ConcatenatedKey(utils.CacheThresholdProfiles, tnt, id)
|
|
}
|
|
|
|
// lock will lock the ThresholdProfile using guardian and store the lock within r.lkID
|
|
// if lkID is passed as argument, the lock is considered as executed
|
|
func (tp *ThresholdProfile) lock(lkID string) {
|
|
if lkID == utils.EmptyString {
|
|
lkID = guardian.Guardian.GuardIDs("",
|
|
config.CgrConfig().GeneralCfg().LockingTimeout,
|
|
thresholdProfileLockKey(tp.Tenant, tp.ID))
|
|
}
|
|
tp.lkID = lkID
|
|
}
|
|
|
|
// unlock will unlock the ThresholdProfile and clear rp.lkID
|
|
func (tp *ThresholdProfile) unlock() {
|
|
if tp.lkID == utils.EmptyString {
|
|
return
|
|
}
|
|
guardian.Guardian.UnguardIDs(tp.lkID)
|
|
tp.lkID = utils.EmptyString
|
|
}
|
|
|
|
// isLocked returns the locks status of this ThresholdProfile
|
|
func (tp *ThresholdProfile) isLocked() bool {
|
|
return tp.lkID != utils.EmptyString
|
|
}
|
|
|
|
// ThresholdWithAPIOpts is used in replicatorV1 for dispatcher
|
|
type ThresholdWithAPIOpts struct {
|
|
*Threshold
|
|
APIOpts map[string]any
|
|
}
|
|
|
|
// Threshold is the unit matched by filters
|
|
type Threshold struct {
|
|
Tenant string
|
|
ID string
|
|
Hits int // number of hits for this threshold
|
|
Snooze time.Time // prevent threshold to run too early
|
|
|
|
lkID string // ID of the lock used when matching the threshold
|
|
tPrfl *ThresholdProfile
|
|
dirty *bool // needs save
|
|
}
|
|
|
|
// TenantID returns the concatenated key beteen tenant and ID
|
|
func (t *Threshold) TenantID() string {
|
|
return utils.ConcatenatedKey(t.Tenant, t.ID)
|
|
}
|
|
|
|
// thresholdLockKey returns the ID used to lock a threshold with guardian
|
|
func thresholdLockKey(tnt, id string) string {
|
|
return utils.ConcatenatedKey(utils.CacheThresholds, tnt, id)
|
|
}
|
|
|
|
// lock will lock the threshold using guardian and store the lock within r.lkID
|
|
// if lkID is passed as argument, the lock is considered as executed
|
|
func (t *Threshold) lock(lkID string) {
|
|
if lkID == utils.EmptyString {
|
|
lkID = guardian.Guardian.GuardIDs("",
|
|
config.CgrConfig().GeneralCfg().LockingTimeout,
|
|
thresholdLockKey(t.Tenant, t.ID))
|
|
}
|
|
t.lkID = lkID
|
|
}
|
|
|
|
// unlock will unlock the threshold and clear r.lkID
|
|
func (t *Threshold) unlock() {
|
|
if t.lkID == utils.EmptyString {
|
|
return
|
|
}
|
|
guardian.Guardian.UnguardIDs(t.lkID)
|
|
t.lkID = utils.EmptyString
|
|
}
|
|
|
|
// isLocked returns the locks status of this threshold
|
|
func (t *Threshold) isLocked() bool {
|
|
return t.lkID != utils.EmptyString
|
|
}
|
|
|
|
// ProcessEvent processes an ThresholdEvent
|
|
// concurrentActions limits the number of simultaneous action sets executed
|
|
func (t *Threshold) ProcessEvent(args *utils.CGREvent, dm *DataManager, fltrS *FilterS) (err error) {
|
|
if t.Snooze.After(time.Now()) || // snoozed, not executing actions
|
|
t.Hits < t.tPrfl.MinHits || // number of hits was not met, will not execute actions
|
|
(t.tPrfl.MaxHits != -1 &&
|
|
t.Hits > t.tPrfl.MaxHits) {
|
|
return
|
|
}
|
|
var tntAcnt string
|
|
var acnt string
|
|
if utils.IfaceAsString(args.APIOpts[utils.MetaEventType]) == utils.AccountUpdate {
|
|
acnt, _ = args.FieldAsString(utils.ID)
|
|
} else {
|
|
acnt, _ = args.FieldAsString(utils.AccountField)
|
|
}
|
|
if _, has := args.APIOpts[utils.MetaAccount]; has {
|
|
acnt, _ = args.OptAsString(utils.MetaAccount)
|
|
}
|
|
if acnt != utils.EmptyString {
|
|
tntAcnt = utils.ConcatenatedKey(args.Tenant, acnt)
|
|
}
|
|
|
|
for _, actionSetID := range t.tPrfl.ActionIDs {
|
|
at := &ActionTiming{
|
|
Uuid: utils.GenUUID(),
|
|
ActionsID: actionSetID,
|
|
ExtraData: args,
|
|
}
|
|
if tntAcnt != utils.EmptyString {
|
|
at.accountIDs = utils.NewStringMap(tntAcnt)
|
|
}
|
|
if t.tPrfl.Async {
|
|
go func() {
|
|
if errExec := at.Execute(fltrS); errExec != nil {
|
|
utils.Logger.Warning(fmt.Sprintf("<ThresholdS> failed executing actions: %s, error: %s", actionSetID, errExec.Error()))
|
|
}
|
|
}()
|
|
} else if errExec := at.Execute(fltrS); errExec != nil {
|
|
utils.Logger.Warning(fmt.Sprintf("<ThresholdS> failed executing actions: %s, error: %s", actionSetID, errExec.Error()))
|
|
err = utils.ErrPartiallyExecuted
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
// Thresholds is a sortable slice of Threshold
|
|
type Thresholds []*Threshold
|
|
|
|
// Sort sorts based on Weight
|
|
func (ts Thresholds) Sort() {
|
|
sort.Slice(ts, func(i, j int) bool { return ts[i].tPrfl.Weight > ts[j].tPrfl.Weight })
|
|
}
|
|
|
|
// unlock will unlock thresholds part of this slice
|
|
func (ts Thresholds) unlock() {
|
|
for _, t := range ts {
|
|
t.unlock()
|
|
if t.tPrfl != nil {
|
|
t.tPrfl.unlock()
|
|
}
|
|
}
|
|
}
|
|
|
|
// NewThresholdService the constructor for ThresoldS service
|
|
func NewThresholdService(dm *DataManager, cgrcfg *config.CGRConfig, filterS *FilterS) *ThresholdService {
|
|
return &ThresholdService{
|
|
dm: dm,
|
|
cgrcfg: cgrcfg,
|
|
filterS: filterS,
|
|
stopBackup: make(chan struct{}),
|
|
loopStopped: make(chan struct{}),
|
|
storedTdIDs: make(utils.StringSet),
|
|
}
|
|
}
|
|
|
|
// ThresholdService manages Threshold execution and storing them to dataDB
|
|
type ThresholdService struct {
|
|
dm *DataManager
|
|
cgrcfg *config.CGRConfig
|
|
filterS *FilterS
|
|
stopBackup chan struct{}
|
|
loopStopped chan struct{}
|
|
storedTdIDs utils.StringSet // keep a record of stats which need saving, map[statsTenantID]bool
|
|
stMux sync.RWMutex // protects storedTdIDs
|
|
}
|
|
|
|
// Reload stops the backupLoop and restarts it
|
|
func (tS *ThresholdService) Reload() {
|
|
close(tS.stopBackup)
|
|
<-tS.loopStopped // wait until the loop is done
|
|
tS.stopBackup = make(chan struct{})
|
|
go tS.runBackup()
|
|
}
|
|
|
|
// StartLoop starts the gorutine with the backup loop
|
|
func (tS *ThresholdService) StartLoop() {
|
|
go tS.runBackup()
|
|
}
|
|
|
|
// Shutdown is called to shutdown the service
|
|
func (tS *ThresholdService) Shutdown() {
|
|
utils.Logger.Info("<ThresholdS> shutdown initialized")
|
|
close(tS.stopBackup)
|
|
tS.storeThresholds()
|
|
utils.Logger.Info("<ThresholdS> shutdown complete")
|
|
}
|
|
|
|
// backup will regularly store thresholds changed to dataDB
|
|
func (tS *ThresholdService) runBackup() {
|
|
storeInterval := tS.cgrcfg.ThresholdSCfg().StoreInterval
|
|
if storeInterval <= 0 {
|
|
tS.loopStopped <- struct{}{}
|
|
return
|
|
}
|
|
for {
|
|
tS.storeThresholds()
|
|
select {
|
|
case <-tS.stopBackup:
|
|
tS.loopStopped <- struct{}{}
|
|
return
|
|
case <-time.After(storeInterval):
|
|
}
|
|
}
|
|
}
|
|
|
|
// storeThresholds represents one task of complete backup
|
|
func (tS *ThresholdService) storeThresholds() {
|
|
var failedTdIDs []string
|
|
for { // don't stop until we store all dirty thresholds
|
|
tS.stMux.Lock()
|
|
tID := tS.storedTdIDs.GetOne()
|
|
if tID != "" {
|
|
tS.storedTdIDs.Remove(tID)
|
|
}
|
|
tS.stMux.Unlock()
|
|
if tID == "" {
|
|
break // no more keys, backup completed
|
|
}
|
|
tIf, ok := Cache.Get(utils.CacheThresholds, tID)
|
|
if !ok || tIf == nil {
|
|
utils.Logger.Warning(fmt.Sprintf("<ThresholdS> failed retrieving from cache treshold with ID: %s", tID))
|
|
continue
|
|
}
|
|
t := tIf.(*Threshold)
|
|
t.lock(utils.EmptyString)
|
|
if err := tS.StoreThreshold(t); err != nil {
|
|
failedTdIDs = append(failedTdIDs, tID) // record failure so we can schedule it for next backup
|
|
}
|
|
t.unlock()
|
|
// randomize the CPU load and give up thread control
|
|
runtime.Gosched()
|
|
}
|
|
if len(failedTdIDs) != 0 { // there were errors on save, schedule the keys for next backup
|
|
tS.stMux.Lock()
|
|
tS.storedTdIDs.AddSlice(failedTdIDs)
|
|
tS.stMux.Unlock()
|
|
}
|
|
}
|
|
|
|
// StoreThreshold stores the threshold in DB and corrects dirty flag
|
|
func (tS *ThresholdService) StoreThreshold(t *Threshold) (err error) {
|
|
if t.dirty == nil || !*t.dirty {
|
|
return
|
|
}
|
|
if err = tS.dm.SetThreshold(t); err != nil {
|
|
utils.Logger.Warning(
|
|
fmt.Sprintf("<ThresholdS> failed saving Threshold with tenant: %s and ID: %s, error: %s",
|
|
t.Tenant, t.ID, err.Error()))
|
|
return
|
|
}
|
|
//since we no longer handle cache in DataManager do here a manual caching
|
|
if tntID := t.TenantID(); Cache.HasItem(utils.CacheThresholds, tntID) { // only cache if previously there
|
|
if err = Cache.Set(utils.CacheThresholds, tntID, t, nil,
|
|
true, utils.NonTransactional); err != nil {
|
|
utils.Logger.Warning(
|
|
fmt.Sprintf("<ThresholdService> failed caching Threshold with ID: %s, error: %s",
|
|
t.TenantID(), err.Error()))
|
|
return
|
|
}
|
|
}
|
|
*t.dirty = false
|
|
return
|
|
}
|
|
|
|
// matchingThresholdsForEvent returns ordered list of matching thresholds which are active for an Event
|
|
func (tS *ThresholdService) matchingThresholdsForEvent(tnt string, args *utils.CGREvent) (ts Thresholds, err error) {
|
|
evNm := utils.MapStorage{
|
|
utils.MetaReq: args.Event,
|
|
utils.MetaOpts: args.APIOpts,
|
|
}
|
|
var thdIDs []string
|
|
if thdIDs, err = utils.GetStringSliceOpts(args, tS.cgrcfg.ThresholdSCfg().Opts.ProfileIDs,
|
|
utils.OptsThresholdsProfileIDs); err != nil {
|
|
return
|
|
}
|
|
var ignFilters bool
|
|
if ignFilters, err = utils.GetBoolOpts(args, tS.cgrcfg.ThresholdSCfg().Opts.ProfileIgnoreFilters,
|
|
utils.OptsThresholdsProfileIgnoreFilters); err != nil {
|
|
return
|
|
}
|
|
tIDs := utils.NewStringSet(thdIDs)
|
|
if len(tIDs) == 0 {
|
|
ignFilters = false
|
|
tIDs, err = MatchingItemIDsForEvent(evNm,
|
|
tS.cgrcfg.ThresholdSCfg().StringIndexedFields,
|
|
tS.cgrcfg.ThresholdSCfg().PrefixIndexedFields,
|
|
tS.cgrcfg.ThresholdSCfg().SuffixIndexedFields,
|
|
tS.dm, utils.CacheThresholdFilterIndexes, tnt,
|
|
tS.cgrcfg.ThresholdSCfg().IndexedSelects,
|
|
tS.cgrcfg.ThresholdSCfg().NestedFields,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
ts = make(Thresholds, 0, len(tIDs))
|
|
for tID := range tIDs {
|
|
lkPrflID := guardian.Guardian.GuardIDs("",
|
|
config.CgrConfig().GeneralCfg().LockingTimeout,
|
|
thresholdProfileLockKey(tnt, tID))
|
|
var tPrfl *ThresholdProfile
|
|
if tPrfl, err = tS.dm.GetThresholdProfile(tnt, tID, true, true, utils.NonTransactional); err != nil {
|
|
guardian.Guardian.UnguardIDs(lkPrflID)
|
|
if err == utils.ErrNotFound {
|
|
err = nil
|
|
continue
|
|
}
|
|
ts.unlock()
|
|
return nil, err
|
|
}
|
|
tPrfl.lock(lkPrflID)
|
|
if tPrfl.ActivationInterval != nil && args.Time != nil &&
|
|
!tPrfl.ActivationInterval.IsActiveAtTime(*args.Time) { // not active
|
|
tPrfl.unlock()
|
|
continue
|
|
}
|
|
if !ignFilters {
|
|
var pass bool
|
|
if pass, err = tS.filterS.Pass(tnt, tPrfl.FilterIDs,
|
|
evNm); err != nil {
|
|
tPrfl.unlock()
|
|
ts.unlock()
|
|
return nil, err
|
|
} else if !pass {
|
|
tPrfl.unlock()
|
|
continue
|
|
}
|
|
}
|
|
lkID := guardian.Guardian.GuardIDs(utils.EmptyString,
|
|
config.CgrConfig().GeneralCfg().LockingTimeout,
|
|
thresholdLockKey(tPrfl.Tenant, tPrfl.ID))
|
|
var t *Threshold
|
|
if t, err = tS.dm.GetThreshold(tPrfl.Tenant, tPrfl.ID, true, true, ""); err != nil {
|
|
guardian.Guardian.UnguardIDs(lkID)
|
|
tPrfl.unlock()
|
|
if err == utils.ErrNotFound { // corner case where the threshold was removed due to MaxHits
|
|
err = nil
|
|
continue
|
|
}
|
|
ts.unlock()
|
|
return nil, err
|
|
}
|
|
t.lock(lkID)
|
|
if t.dirty == nil || tPrfl.MaxHits == -1 || t.Hits < tPrfl.MaxHits {
|
|
t.dirty = utils.BoolPointer(false)
|
|
}
|
|
t.tPrfl = tPrfl
|
|
ts = append(ts, t)
|
|
}
|
|
// All good, convert from Map to Slice so we can sort
|
|
if len(ts) == 0 {
|
|
return nil, utils.ErrNotFound
|
|
}
|
|
ts.Sort()
|
|
for i, t := range ts {
|
|
if t.tPrfl.Blocker && i != len(ts)-1 { // blocker will stop processing and we are not at last index
|
|
Thresholds(ts[i+1:]).unlock()
|
|
ts = ts[:i+1]
|
|
break
|
|
}
|
|
}
|
|
return
|
|
}
|
|
|
|
// processEvent processes a new event, dispatching to matching thresholds
|
|
func (tS *ThresholdService) processEvent(tnt string, args *utils.CGREvent) (thresholdsIDs []string, err error) {
|
|
var matchTs Thresholds
|
|
if matchTs, err = tS.matchingThresholdsForEvent(tnt, args); err != nil {
|
|
return nil, err
|
|
}
|
|
var withErrors bool
|
|
thresholdsIDs = make([]string, 0, len(matchTs))
|
|
for _, t := range matchTs {
|
|
thresholdsIDs = append(thresholdsIDs, t.ID)
|
|
t.Hits++
|
|
if err = t.ProcessEvent(args, tS.dm, tS.filterS); err != nil {
|
|
utils.Logger.Warning(
|
|
fmt.Sprintf("<ThresholdService> threshold: %s, ignoring event: %s, error: %s",
|
|
t.TenantID(), utils.ConcatenatedKey(tnt, args.ID), err.Error()))
|
|
withErrors = true
|
|
continue
|
|
}
|
|
if t.dirty == nil || t.Hits == t.tPrfl.MaxHits { // one time threshold
|
|
if err = tS.dm.RemoveThreshold(t.Tenant, t.ID); err != nil {
|
|
utils.Logger.Warning(
|
|
fmt.Sprintf("<ThresholdService> failed removing from database non-recurrent threshold: %s, error: %s",
|
|
t.TenantID(), err.Error()))
|
|
withErrors = true
|
|
}
|
|
//since we don't handle in DataManager caching we do a manual remove here
|
|
if tntID := t.TenantID(); Cache.HasItem(utils.CacheThresholds, tntID) { // only cache if previously there
|
|
if err = Cache.Set(utils.CacheThresholds, tntID, nil, nil,
|
|
true, utils.NonTransactional); err != nil {
|
|
utils.Logger.Warning(
|
|
fmt.Sprintf("<ThresholdService> failed removing from cache non-recurrent threshold: %s, error: %s",
|
|
t.TenantID(), err.Error()))
|
|
withErrors = true
|
|
}
|
|
}
|
|
continue
|
|
}
|
|
t.Snooze = time.Now().Add(t.tPrfl.MinSleep)
|
|
// recurrent threshold
|
|
*t.dirty = true // mark it to be saved
|
|
if tS.cgrcfg.ThresholdSCfg().StoreInterval == -1 {
|
|
tS.StoreThreshold(t)
|
|
} else {
|
|
tS.stMux.Lock()
|
|
tS.storedTdIDs.Add(t.TenantID())
|
|
tS.stMux.Unlock()
|
|
}
|
|
}
|
|
matchTs.unlock()
|
|
if withErrors {
|
|
err = utils.ErrPartiallyExecuted
|
|
}
|
|
return
|
|
}
|
|
|
|
// V1ProcessEvent implements ThresholdService method for processing an Event
|
|
func (tS *ThresholdService) V1ProcessEvent(ctx *context.Context, args *utils.CGREvent, reply *[]string) (err error) {
|
|
if args == nil {
|
|
return utils.NewErrMandatoryIeMissing(utils.CGREventString)
|
|
}
|
|
if missing := utils.MissingStructFields(args, []string{utils.ID}); len(missing) != 0 { //Params missing
|
|
return utils.NewErrMandatoryIeMissing(missing...)
|
|
} else if args.Event == nil {
|
|
return utils.NewErrMandatoryIeMissing(utils.Event)
|
|
}
|
|
tnt := args.Tenant
|
|
if tnt == utils.EmptyString {
|
|
tnt = tS.cgrcfg.GeneralCfg().DefaultTenant
|
|
}
|
|
var ids []string
|
|
if ids, err = tS.processEvent(tnt, args); err != nil {
|
|
return
|
|
}
|
|
*reply = ids
|
|
return
|
|
}
|
|
|
|
// V1GetThresholdsForEvent queries thresholds matching an Event
|
|
func (tS *ThresholdService) V1GetThresholdsForEvent(ctx *context.Context, args *utils.CGREvent, reply *Thresholds) (err error) {
|
|
if args == nil {
|
|
return utils.NewErrMandatoryIeMissing(utils.CGREventString)
|
|
}
|
|
if missing := utils.MissingStructFields(args, []string{utils.ID}); len(missing) != 0 { //Params missing
|
|
return utils.NewErrMandatoryIeMissing(missing...)
|
|
} else if args.Event == nil {
|
|
return utils.NewErrMandatoryIeMissing(utils.Event)
|
|
}
|
|
tnt := args.Tenant
|
|
if tnt == utils.EmptyString {
|
|
tnt = tS.cgrcfg.GeneralCfg().DefaultTenant
|
|
}
|
|
var ts Thresholds
|
|
if ts, err = tS.matchingThresholdsForEvent(tnt, args); err == nil {
|
|
*reply = ts
|
|
ts.unlock()
|
|
}
|
|
return
|
|
}
|
|
|
|
// V1GetThresholdIDs returns list of thresholdIDs configured for a tenant
|
|
func (tS *ThresholdService) V1GetThresholdIDs(ctx *context.Context, tenant string, tIDs *[]string) (err error) {
|
|
if tenant == utils.EmptyString {
|
|
tenant = tS.cgrcfg.GeneralCfg().DefaultTenant
|
|
}
|
|
prfx := utils.ThresholdPrefix + tenant + utils.ConcatenatedKeySep
|
|
keys, err := tS.dm.DataDB().GetKeysForPrefix(prfx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
retIDs := make([]string, len(keys))
|
|
for i, key := range keys {
|
|
retIDs[i] = key[len(prfx):]
|
|
}
|
|
*tIDs = retIDs
|
|
return
|
|
}
|
|
|
|
// V1GetThreshold retrieves a Threshold
|
|
func (tS *ThresholdService) V1GetThreshold(ctx *context.Context, tntID *utils.TenantID, t *Threshold) (err error) {
|
|
var thd *Threshold
|
|
tnt := tntID.Tenant
|
|
if tnt == utils.EmptyString {
|
|
tnt = tS.cgrcfg.GeneralCfg().DefaultTenant
|
|
}
|
|
// make sure threshold is locked at process level
|
|
lkID := guardian.Guardian.GuardIDs(utils.EmptyString,
|
|
config.CgrConfig().GeneralCfg().LockingTimeout,
|
|
thresholdLockKey(tnt, tntID.ID))
|
|
defer guardian.Guardian.UnguardIDs(lkID)
|
|
if thd, err = tS.dm.GetThreshold(tnt, tntID.ID, true, true, ""); err != nil {
|
|
return
|
|
}
|
|
*t = *thd
|
|
return
|
|
}
|
|
|
|
// V1ResetThreshold resets the threshold hits
|
|
func (tS *ThresholdService) V1ResetThreshold(ctx *context.Context, tntID *utils.TenantID, rply *string) (err error) {
|
|
var thd *Threshold
|
|
tnt := tntID.Tenant
|
|
if tnt == utils.EmptyString {
|
|
tnt = tS.cgrcfg.GeneralCfg().DefaultTenant
|
|
}
|
|
// make sure threshold is locked at process level
|
|
lkID := guardian.Guardian.GuardIDs(utils.EmptyString,
|
|
config.CgrConfig().GeneralCfg().LockingTimeout,
|
|
thresholdLockKey(tnt, tntID.ID))
|
|
defer guardian.Guardian.UnguardIDs(lkID)
|
|
if thd, err = tS.dm.GetThreshold(tnt, tntID.ID, true, true, ""); err != nil {
|
|
return
|
|
}
|
|
if thd.Hits != 0 {
|
|
thd.Hits = 0
|
|
thd.Snooze = time.Time{}
|
|
thd.dirty = utils.BoolPointer(true) // mark it to be saved
|
|
if tS.cgrcfg.ThresholdSCfg().StoreInterval == -1 {
|
|
if err = tS.StoreThreshold(thd); err != nil {
|
|
return
|
|
}
|
|
} else {
|
|
tS.stMux.Lock()
|
|
tS.storedTdIDs.Add(thd.TenantID())
|
|
tS.stMux.Unlock()
|
|
}
|
|
}
|
|
*rply = utils.OK
|
|
return
|
|
}
|