diff --git a/engine/action_timing.go b/engine/action_timing.go index 9262bd71d..6cb5b94ee 100644 --- a/engine/action_timing.go +++ b/engine/action_timing.go @@ -240,7 +240,7 @@ func (at *ActionTiming) Execute() (err error) { }) } } - go storageLogger.LogActionTiming(SCHED_SOURCE, at, aac) + storageLogger.LogActionTiming(SCHED_SOURCE, at, aac) return } diff --git a/engine/action_trigger.go b/engine/action_trigger.go index e395bb6c7..d34a52191 100644 --- a/engine/action_trigger.go +++ b/engine/action_trigger.go @@ -59,7 +59,7 @@ func (at *ActionTrigger) Execute(ub *UserBalance) (err error) { go Logger.Info(fmt.Sprintf("Executing %v: %v", ub.Id, a)) err = actionFunction(ub, a) } - go storageLogger.LogActionTrigger(ub.Id, RATER_SOURCE, at, aac) + storageLogger.LogActionTrigger(ub.Id, RATER_SOURCE, at, aac) at.Executed = true storageGetter.SetUserBalance(ub) return diff --git a/engine/destinations_test.go b/engine/destinations_test.go index 70f5cc2ef..4602a6585 100644 --- a/engine/destinations_test.go +++ b/engine/destinations_test.go @@ -20,10 +20,8 @@ package engine import ( "encoding/json" - "fmt" "github.com/cgrates/cgrates/cache2go" "github.com/cgrates/cgrates/utils" - "reflect" "testing" ) diff --git a/engine/storage_radix.go b/engine/storage_radix.go index d126e6e26..fb8ccd08f 100644 --- a/engine/storage_radix.go +++ b/engine/storage_radix.go @@ -33,7 +33,7 @@ type RadixStorage struct { } func NewRadixStorage(address string, db int, pass string) (DataStorage, error) { - ndb, err := redis.Dial("tcp", address) + ndb, err := redis.DialTimeout("tcp", address, 5*time.Second) if err != nil { return nil, err } @@ -55,10 +55,8 @@ func (rs *RadixStorage) Close() { } func (rs *RadixStorage) Flush() (err error) { - if r := rs.db.Cmd("flushdb"); r.Err != nil { - return r.Err - } - return + r := rs.db.Cmd("flushdb") + return r.Err } func (rs *RadixStorage) GetRatingProfile(key string) (rp *RatingProfile, err error) { @@ -85,45 +83,41 @@ func (rs *RadixStorage) SetRatingProfile(rp *RatingProfile) (err error) { func (rs *RadixStorage) GetDestination(key string) (dest *Destination, err error) { var values []string - if values, err = rs.db.Cmd("hkeys", DESTINATION_PREFIX+key).List(); len(values) > 0 && err == nil { + if values, err = rs.db.Cmd("smembers", DESTINATION_PREFIX+key).List(); len(values) > 0 && err == nil { dest = &Destination{Id: key, Prefixes: values} } return } func (rs *RadixStorage) DestinationContainsPrefix(key string, prefix string) (precision int, err error) { + if r := rs.db.Cmd("sadd", TEMP_DESTINATION_PREFIX+prefix, utils.SplitPrefixInterface(prefix)); r.Err != nil { + return 0, r.Err + } var values []string - var pfs []interface{} - pfs = append(pfs, DESTINATION_PREFIX+key) - pfs = append(pfs, utils.SplitPrefixInterface(prefix)...) - if values, err = rs.db.Cmd("hmget", pfs...).List(); err == nil { - for i, p := range values { - if p != "" { - return len(prefix) - i, nil + if values, err = rs.db.Cmd("sinter", DESTINATION_PREFIX+key, TEMP_DESTINATION_PREFIX+prefix).List(); err == nil { + for _, p := range values { + if len(p) > precision { + precision = len(p) } } } + if r := rs.db.Cmd("del", TEMP_DESTINATION_PREFIX+prefix); r.Err != nil { + Logger.Err(fmt.Sprintf("Error removing temp: %v", r.Err)) + } return } func (rs *RadixStorage) SetDestination(dest *Destination) (err error) { - newPrefixes := make(map[string]string, len(dest.Prefixes)) - for _, p := range dest.Prefixes { - newPrefixes[p] = "*" - } - if r := rs.db.Cmd("hmset", DESTINATION_PREFIX+dest.Id, newPrefixes); r.Err != nil { - return r.Err - } - if err == nil && historyScribe != nil { + r := rs.db.Cmd("sadd", DESTINATION_PREFIX+dest.Id, dest.Prefixes) + if r.Err == nil && historyScribe != nil { response := 0 historyScribe.Record(&history.Record{DESTINATION_PREFIX + dest.Id, dest}, &response) } - return + return r.Err } func (rs *RadixStorage) GetActions(key string) (as Actions, err error) { - var values []byte - if values, err = rs.db.Cmd("get", ACTION_PREFIX+key).Bytes(); err == nil { + if values, err := rs.db.Cmd("get", ACTION_PREFIX+key).Bytes(); err == nil { err = rs.ms.Unmarshal(values, &as) } return @@ -225,12 +219,12 @@ func (rs *RadixStorage) LogActionTrigger(ubId, source string, at *ActionTrigger, if err != nil { return } - mas, err := rs.ms.Marshal(&as) + mas, err := rs.ms.Marshal(as) if err != nil { return } - rs.db.Cmd("set", LOG_ACTION_TRIGGER_PREFIX+source+"_"+time.Now().Format(time.RFC3339Nano), fmt.Sprintf("%s*%s*%s", ubId, string(mat), string(mas))) - return + r := rs.db.Cmd("set", LOG_ACTION_TRIGGER_PREFIX+source+"_"+time.Now().Format(time.RFC3339Nano), fmt.Sprintf("%v*%v*%v", ubId, string(mat), string(mas))) + return r.Err } func (rs *RadixStorage) LogActionTiming(source string, at *ActionTiming, as Actions) (err error) { @@ -238,12 +232,12 @@ func (rs *RadixStorage) LogActionTiming(source string, at *ActionTiming, as Acti if err != nil { return } - mas, err := rs.ms.Marshal(&as) + mas, err := rs.ms.Marshal(as) if err != nil { return } - rs.db.Cmd("set", LOG_ACTION_TIMMING_PREFIX+source+"_"+time.Now().Format(time.RFC3339Nano), fmt.Sprintf("%s*%s", string(mat), string(mas))) - return + r := rs.db.Cmd("set", LOG_ACTION_TIMMING_PREFIX+source+"_"+time.Now().Format(time.RFC3339Nano), fmt.Sprintf("%v*%v", string(mat), string(mas))) + return r.Err } func (rs *RadixStorage) LogError(uuid, source, errstr string) (err error) { diff --git a/engine/storage_redigo.go b/engine/storage_redigo.go index 9ddc7c132..82e0b9d0d 100644 --- a/engine/storage_redigo.go +++ b/engine/storage_redigo.go @@ -20,6 +20,8 @@ package engine import ( "fmt" + "github.com/cgrates/cgrates/history" + "github.com/cgrates/cgrates/utils" "github.com/garyburd/redigo/redis" "time" ) @@ -31,7 +33,7 @@ type RedigoStorage struct { } func NewRedigoStorage(address string, db int, pass string) (DataStorage, error) { - ndb, err := redis.Dial("tcp", address) + ndb, err := redis.DialTimeout("tcp", address, 5*time.Second, time.Second, time.Second) if err != nil { return nil, err } @@ -69,28 +71,45 @@ func (rs *RedigoStorage) GetRatingProfile(key string) (rp *RatingProfile, err er func (rs *RedigoStorage) SetRatingProfile(rp *RatingProfile) (err error) { result, err := rs.ms.Marshal(rp) _, err = rs.db.Do("set", RATING_PROFILE_PREFIX+rp.Id, result) + if err == nil && historyScribe != nil { + response := 0 + historyScribe.Record(&history.Record{RATING_PROFILE_PREFIX + rp.Id, rp}, &response) + } return } func (rs *RedigoStorage) GetDestination(key string) (dest *Destination, err error) { - var values []byte - if values, err = redis.Bytes(rs.db.Do("get", DESTINATION_PREFIX+key)); err == nil { - dest = &Destination{Id: key} - err = rs.ms.Unmarshal(values, dest) + var values []string + if values, err = redis.Strings(rs.db.Do("smembers", DESTINATION_PREFIX+key)); len(values) > 0 && err == nil { + dest = &Destination{Id: key, Prefixes: values} } return } func (rs *RedigoStorage) DestinationContainsPrefix(key string, prefix string) (precision int, err error) { + if _, err := rs.db.Do("sadd", redis.Args{}.Add(TEMP_DESTINATION_PREFIX+prefix).AddFlat(utils.SplitPrefixInterface(prefix))...); err != nil { + return 0, err + } + var values []string + if values, err = redis.Strings(rs.db.Do("sinter", DESTINATION_PREFIX+key, TEMP_DESTINATION_PREFIX+prefix)); err == nil { + for _, p := range values { + if len(p) > precision { + precision = len(p) + } + } + } + if _, err := rs.db.Do("del", TEMP_DESTINATION_PREFIX+prefix); err != nil { + Logger.Err("Error removing temp ") + } return } func (rs *RedigoStorage) SetDestination(dest *Destination) (err error) { - var result []byte - if result, err = rs.ms.Marshal(dest); err != nil { - return + _, err = rs.db.Do("sadd", redis.Args{}.Add(DESTINATION_PREFIX+dest.Id).AddFlat(dest.Prefixes)...) + if err == nil && historyScribe != nil { + response := 0 + historyScribe.Record(&history.Record{DESTINATION_PREFIX + dest.Id, dest}, &response) } - _, err = rs.db.Do("set", DESTINATION_PREFIX+dest.Id, result) return } @@ -191,11 +210,11 @@ func (rs *RedigoStorage) LogActionTrigger(ubId, source string, at *ActionTrigger if err != nil { return } - mas, err := rs.ms.Marshal(&as) + mas, err := rs.ms.Marshal(as) if err != nil { return } - rs.db.Do("set", LOG_ACTION_TRIGGER_PREFIX+source+"_"+time.Now().Format(time.RFC3339Nano), []byte(fmt.Sprintf("%s*%s*%s", ubId, string(mat), string(mas)))) + _, err = rs.db.Do("set", LOG_ACTION_TRIGGER_PREFIX+source+"_"+time.Now().Format(time.RFC3339Nano), []byte(fmt.Sprintf("%s*%s*%s", ubId, string(mat), string(mas)))) return } @@ -204,7 +223,7 @@ func (rs *RedigoStorage) LogActionTiming(source string, at *ActionTiming, as Act if err != nil { return } - mas, err := rs.ms.Marshal(&as) + mas, err := rs.ms.Marshal(as) if err != nil { return } diff --git a/engine/storage_redis.go b/engine/storage_redis.go index baaa20349..ae4447777 100644 --- a/engine/storage_redis.go +++ b/engine/storage_redis.go @@ -225,11 +225,11 @@ func (rs *RedisStorage) LogActionTrigger(ubId, source string, at *ActionTrigger, if err != nil { return } - mas, err := rs.ms.Marshal(&as) + mas, err := rs.ms.Marshal(as) if err != nil { return } - rs.db.Set(LOG_ACTION_TRIGGER_PREFIX+source+"_"+time.Now().Format(time.RFC3339Nano), []byte(fmt.Sprintf("%s*%s*%s", ubId, string(mat), string(mas)))) + rs.db.Set(LOG_ACTION_TRIGGER_PREFIX+source+"_"+time.Now().Format(time.RFC3339Nano), []byte(fmt.Sprintf("%v*%v*%v", ubId, string(mat), string(mas)))) return } @@ -238,11 +238,11 @@ func (rs *RedisStorage) LogActionTiming(source string, at *ActionTiming, as Acti if err != nil { return } - mas, err := rs.ms.Marshal(&as) + mas, err := rs.ms.Marshal(as) if err != nil { return } - _, err = rs.db.Set(LOG_ACTION_TIMMING_PREFIX+source+"_"+time.Now().Format(time.RFC3339Nano), []byte(fmt.Sprintf("%s*%s", string(mat), string(mas)))) + _, err = rs.db.Set(LOG_ACTION_TIMMING_PREFIX+source+"_"+time.Now().Format(time.RFC3339Nano), []byte(fmt.Sprintf("%v*%v", string(mat), string(mas)))) return }