From 72c3d1ac29d9c50fd615872d83ce723213b0eb26 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Wed, 26 Nov 2014 16:39:33 +0200 Subject: [PATCH 1/9] another optimization for stale dest ids --- apier/v1/apier.go | 3 --- engine/destinations.go | 3 +-- utils/slice.go | 18 +++++++++++++----- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/apier/v1/apier.go b/apier/v1/apier.go index fe83aafb1..0a04b5f08 100644 --- a/apier/v1/apier.go +++ b/apier/v1/apier.go @@ -22,7 +22,6 @@ import ( "encoding/json" "errors" "fmt" - "log" "path" "github.com/cgrates/cgrates/cache2go" @@ -793,11 +792,9 @@ func (self *ApierV1) ReloadCache(attrs utils.ApiReloadCache, reply *string) erro dcsKeys[idx] = engine.DERIVEDCHARGERS_PREFIX + dc } } - log.Print("Cache Rating") if err := self.RatingDb.CacheRating(dstKeys, rpKeys, rpfKeys, rpAlsKeys, lcrKeys); err != nil { return err } - log.Print("Cache Accounting") if err := self.AccountDb.CacheAccounting(actKeys, shgKeys, accAlsKeys, dcsKeys); err != nil { return err } diff --git a/engine/destinations.go b/engine/destinations.go index 993ff301f..770f7354b 100644 --- a/engine/destinations.go +++ b/engine/destinations.go @@ -91,10 +91,9 @@ func CleanStalePrefixes(destIds []string) { } for prefix, idIDs := range prefixMap { dIDs := idIDs.Value().([]interface{}) - strdIDs := utils.ConvertInterfaceSliceToStringMap(dIDs) changed := false for _, searchedDID := range destIds { - if i, found := strdIDs[searchedDID]; found { + if i, found := utils.GetSliceInterfaceIndex(dIDs, searchedDID); found { if len(dIDs) == 1 { // remove de prefix from cache cache2go.RemKey(DESTINATION_PREFIX + prefix) diff --git a/utils/slice.go b/utils/slice.go index bec15c525..1e1bb7879 100644 --- a/utils/slice.go +++ b/utils/slice.go @@ -52,10 +52,18 @@ func SliceMemberHasPrefix(ss []string, prfx string) bool { return false } -func ConvertInterfaceSliceToStringMap(is []interface{}) (result map[string]int) { - result = make(map[string]int) - for index, i := range is { - result[i.(string)] = index +type InterfaceStrings []interface{} + +func (a InterfaceStrings) Len() int { return len(a) } +func (a InterfaceStrings) Swap(i, j int) { a[i], a[j] = a[j], a[i] } +func (a InterfaceStrings) Less(i, j int) bool { return a[i].(string) < a[j].(string) } + +// Binary string search in slice +// returns true if found and the index +func GetSliceInterfaceIndex(ss []interface{}, s interface{}) (int, bool) { + sort.Sort(InterfaceStrings(ss)) + if i := sort.Search(len(ss), func(i int) bool { return ss[i].(string) >= s.(string) }); i < len(ss) && ss[i] == s { + return i, true } - return result + return len(ss), false } From 728354331b0b17651f2818bf1a460e899c445fea Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Wed, 26 Nov 2014 17:44:32 +0200 Subject: [PATCH 2/9] sort only once for slice --- engine/destinations.go | 2 ++ utils/slice.go | 1 - 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/engine/destinations.go b/engine/destinations.go index 770f7354b..b43e0d3a1 100644 --- a/engine/destinations.go +++ b/engine/destinations.go @@ -20,6 +20,7 @@ package engine import ( "encoding/json" + "sort" "strings" "github.com/cgrates/cgrates/cache2go" @@ -92,6 +93,7 @@ func CleanStalePrefixes(destIds []string) { for prefix, idIDs := range prefixMap { dIDs := idIDs.Value().([]interface{}) changed := false + sort.Sort(utils.InterfaceStrings(dIDs)) for _, searchedDID := range destIds { if i, found := utils.GetSliceInterfaceIndex(dIDs, searchedDID); found { if len(dIDs) == 1 { diff --git a/utils/slice.go b/utils/slice.go index 1e1bb7879..ecd80157a 100644 --- a/utils/slice.go +++ b/utils/slice.go @@ -61,7 +61,6 @@ func (a InterfaceStrings) Less(i, j int) bool { return a[i].(string) < a[j].(str // Binary string search in slice // returns true if found and the index func GetSliceInterfaceIndex(ss []interface{}, s interface{}) (int, bool) { - sort.Sort(InterfaceStrings(ss)) if i := sort.Search(len(ss), func(i int) bool { return ss[i].(string) >= s.(string) }); i < len(ss) && ss[i] == s { return i, true } From 680985200fdd22202d9fd9fac17705716298c56c Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Wed, 26 Nov 2014 18:28:07 +0200 Subject: [PATCH 3/9] Using map[interface{}]struct{} instead of []interface{} in cache --- cache2go/cache_test.go | 2 +- cache2go/store.go | 20 +++++++------------- engine/account.go | 4 ++-- engine/destinations.go | 16 +++++----------- engine/destinations_test.go | 11 ++++++----- engine/ratingprofile.go | 4 ++-- engine/units_counter.go | 12 +++++------- utils/slice.go | 25 ------------------------- 8 files changed, 28 insertions(+), 66 deletions(-) diff --git a/cache2go/cache_test.go b/cache2go/cache_test.go index 7b915ae5a..1bae5050b 100644 --- a/cache2go/cache_test.go +++ b/cache2go/cache_test.go @@ -89,7 +89,7 @@ func TestCachePush(t *testing.T) { CachePush("ccc_t1", "1") CachePush("ccc_t1", "2") v, err := GetCached("ccc_t1") - if err != nil || len(v.([]interface{})) != 2 { + if err != nil || len(v.(map[interface{}]struct{})) != 2 { t.Error("Error in cache push: ", v) } } diff --git a/cache2go/store.go b/cache2go/store.go index a2148ad33..1f63f7ee4 100644 --- a/cache2go/store.go +++ b/cache2go/store.go @@ -37,21 +37,15 @@ func (cs cacheDoubleStore) Put(key string, value interface{}) { } func (cs cacheDoubleStore) Append(key string, value interface{}) { - var elements []interface{} - v, err := cs.Get(key) - if err == nil { - elements = v.([]interface{}) + var elements map[interface{}]struct{} + if v, err := cs.Get(key); err == nil { + elements = v.(map[interface{}]struct{}) + } else { + elements = make(map[interface{}]struct{}) } // check if the val is already present - found := false - for _, v := range elements { - if value == v { - found = true - break - } - } - if !found { - elements = append(elements, value) + if _, found := elements[value]; !found { + elements[value] = struct{}{} } cache.Put(key, elements) } diff --git a/engine/account.go b/engine/account.go index a089c59ac..1f6451b5d 100644 --- a/engine/account.go +++ b/engine/account.go @@ -160,8 +160,8 @@ func (ub *Account) getBalancesForPrefix(prefix, category string, balances Balanc if b.DestinationId != "" && b.DestinationId != utils.ANY { for _, p := range utils.SplitPrefix(prefix, MIN_PREFIX_MATCH) { if x, err := cache2go.GetCached(DESTINATION_PREFIX + p); err == nil { - destIds := x.([]interface{}) - for _, dId := range destIds { + destIds := x.(map[interface{}]struct{}) + for dId, _ := range destIds { if dId == b.DestinationId { b.precision = len(p) usefulBalances = append(usefulBalances, b) diff --git a/engine/destinations.go b/engine/destinations.go index b43e0d3a1..7af1d826e 100644 --- a/engine/destinations.go +++ b/engine/destinations.go @@ -20,11 +20,9 @@ package engine import ( "encoding/json" - "sort" "strings" "github.com/cgrates/cgrates/cache2go" - "github.com/cgrates/cgrates/utils" "github.com/cgrates/cgrates/history" ) @@ -76,11 +74,8 @@ func (d *Destination) GetHistoryRecord() history.Record { // Reverse search in cache to see if prefix belongs to destination id func CachedDestHasPrefix(destId, prefix string) bool { if cached, err := cache2go.GetCached(DESTINATION_PREFIX + prefix); err == nil { - for _, cachedDstId := range cached.([]interface{}) { - if destId == cachedDstId { - return true - } - } + _, found := cached.(map[interface{}]struct{})[destId] + return found } return false } @@ -91,17 +86,16 @@ func CleanStalePrefixes(destIds []string) { return } for prefix, idIDs := range prefixMap { - dIDs := idIDs.Value().([]interface{}) + dIDs := idIDs.Value().(map[interface{}]struct{}) changed := false - sort.Sort(utils.InterfaceStrings(dIDs)) for _, searchedDID := range destIds { - if i, found := utils.GetSliceInterfaceIndex(dIDs, searchedDID); found { + if _, found := dIDs[searchedDID]; found { if len(dIDs) == 1 { // remove de prefix from cache cache2go.RemKey(DESTINATION_PREFIX + prefix) } else { // delete the destination from list and put the new list in chache - dIDs[i], dIDs = dIDs[len(dIDs)-1], dIDs[:len(dIDs)-1] + delete(dIDs, searchedDID) changed = true } } diff --git a/engine/destinations_test.go b/engine/destinations_test.go index 9eed163b4..31b9515e3 100644 --- a/engine/destinations_test.go +++ b/engine/destinations_test.go @@ -126,17 +126,18 @@ func TestNonCachedDestWrongPrefix(t *testing.T) { } func TestCleanStalePrefixes(t *testing.T) { - cache2go.Cache(DESTINATION_PREFIX+"1", []interface{}{"D1", "D2"}) - cache2go.Cache(DESTINATION_PREFIX+"2", []interface{}{"D1"}) - cache2go.Cache(DESTINATION_PREFIX+"3", []interface{}{"D2"}) + x := struct{}{} + cache2go.Cache(DESTINATION_PREFIX+"1", map[interface{}]struct{}{"D1": x, "D2": x}) + cache2go.Cache(DESTINATION_PREFIX+"2", map[interface{}]struct{}{"D1": x}) + cache2go.Cache(DESTINATION_PREFIX+"3", map[interface{}]struct{}{"D2": x}) CleanStalePrefixes([]string{"D1"}) - if r, err := cache2go.GetCached(DESTINATION_PREFIX + "1"); err != nil || len(r.([]interface{})) != 1 { + if r, err := cache2go.GetCached(DESTINATION_PREFIX + "1"); err != nil || len(r.(map[interface{}]struct{})) != 1 { t.Error("Error cleaning stale destination ids", r) } if r, err := cache2go.GetCached(DESTINATION_PREFIX + "2"); err == nil { t.Error("Error removing stale prefix: ", r) } - if r, err := cache2go.GetCached(DESTINATION_PREFIX + "3"); err != nil || len(r.([]interface{})) != 1 { + if r, err := cache2go.GetCached(DESTINATION_PREFIX + "3"); err != nil || len(r.(map[interface{}]struct{})) != 1 { t.Error("Error performing stale cleaning: ", r) } } diff --git a/engine/ratingprofile.go b/engine/ratingprofile.go index 85a877823..c667ab266 100644 --- a/engine/ratingprofile.go +++ b/engine/ratingprofile.go @@ -127,8 +127,8 @@ func (rp *RatingProfile) GetRatingPlansForPrefix(cd *CallDescriptor) (err error) } else { for _, p := range utils.SplitPrefix(cd.Destination, MIN_PREFIX_MATCH) { if x, err := cache2go.GetCached(DESTINATION_PREFIX + p); err == nil { - destIds := x.([]interface{}) - for _, idId := range destIds { + destIds := x.(map[interface{}]struct{}) + for idId, _ := range destIds { dId := idId.(string) if _, ok := rpl.DestinationRates[dId]; ok { rps = rpl.RateIntervalList(dId) diff --git a/engine/units_counter.go b/engine/units_counter.go index ab975a28f..8c192c27c 100644 --- a/engine/units_counter.go +++ b/engine/units_counter.go @@ -78,13 +78,11 @@ func (uc *UnitsCounter) addUnits(amount float64, prefix string) { } for _, p := range utils.SplitPrefix(prefix, MIN_PREFIX_MATCH) { if x, err := cache2go.GetCached(DESTINATION_PREFIX + p); err == nil { - destIds := x.([]interface{}) - for _, dId := range destIds { - if dId == mb.DestinationId { - mb.Value += amount - counted = true - break - } + destIds := x.(map[interface{}]struct{}) + if _, found := destIds[mb.DestinationId]; found { + mb.Value += amount + counted = true + break } } if counted { diff --git a/utils/slice.go b/utils/slice.go index ecd80157a..964e95d75 100644 --- a/utils/slice.go +++ b/utils/slice.go @@ -32,16 +32,6 @@ func IsSliceMember(ss []string, s string) bool { return false } -// Binary string search in slice -// returns true if found and the index -func GetSliceMemberIndex(ss []string, s string) (int, bool) { - sort.Strings(ss) - if i := sort.SearchStrings(ss, s); i < len(ss) && ss[i] == s { - return i, true - } - return len(ss), false -} - //Iterates over slice members and returns true if one starts with prefix func SliceMemberHasPrefix(ss []string, prfx string) bool { for _, mbr := range ss { @@ -51,18 +41,3 @@ func SliceMemberHasPrefix(ss []string, prfx string) bool { } return false } - -type InterfaceStrings []interface{} - -func (a InterfaceStrings) Len() int { return len(a) } -func (a InterfaceStrings) Swap(i, j int) { a[i], a[j] = a[j], a[i] } -func (a InterfaceStrings) Less(i, j int) bool { return a[i].(string) < a[j].(string) } - -// Binary string search in slice -// returns true if found and the index -func GetSliceInterfaceIndex(ss []interface{}, s interface{}) (int, bool) { - if i := sort.Search(len(ss), func(i int) bool { return ss[i].(string) >= s.(string) }); i < len(ss) && ss[i] == s { - return i, true - } - return len(ss), false -} From 4735c7c0a42330303a3ef66ffe900fcc946a69fa Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Wed, 26 Nov 2014 18:30:40 +0200 Subject: [PATCH 4/9] updates simple cache store too --- cache2go/store.go | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/cache2go/store.go b/cache2go/store.go index 1f63f7ee4..9676005b6 100644 --- a/cache2go/store.go +++ b/cache2go/store.go @@ -129,22 +129,17 @@ func (cs cacheSimpleStore) Put(key string, value interface{}) { } func (cs cacheSimpleStore) Append(key string, value interface{}) { - var elements []interface{} - if ti, exists := cs.cache[key]; exists { - elements = ti.value.([]interface{}) + var elements map[interface{}]struct{} + if v, err := cs.Get(key); err == nil { + elements = v.(map[interface{}]struct{}) + } else { + elements = make(map[interface{}]struct{}) } // check if the val is already present - found := false - for _, v := range elements { - if value == v { - found = true - break - } + if _, found := elements[value]; !found { + elements[value] = struct{}{} } - if !found { - elements = append(elements, value) - } - cs.Put(key, elements) + cache.Put(key, elements) } func (cs cacheSimpleStore) Get(key string) (interface{}, error) { From dac0248c150e760a28abb2d86deb9bdb1d164145 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Thu, 27 Nov 2014 12:06:47 +0200 Subject: [PATCH 5/9] load all destination in cache if more than 0.5 --- cache2go/cache.go | 2 +- cache2go/store.go | 14 +++++++------- engine/storage_map.go | 2 +- engine/storage_redis.go | 3 ++- 4 files changed, 11 insertions(+), 10 deletions(-) diff --git a/cache2go/cache.go b/cache2go/cache.go index 711b02125..43b0067bf 100644 --- a/cache2go/cache.go +++ b/cache2go/cache.go @@ -158,7 +158,7 @@ func Flush() { } } -func CountEntries(prefix string) (result int64) { +func CountEntries(prefix string) (result int) { mux.RLock() defer mux.RUnlock() return cache.CountEntriesForPrefix(prefix) diff --git a/cache2go/store.go b/cache2go/store.go index 9676005b6..0c6fd9880 100644 --- a/cache2go/store.go +++ b/cache2go/store.go @@ -16,7 +16,7 @@ type cacheStore interface { GetAge(string) (time.Duration, error) Delete(string) DeletePrefix(string) - CountEntriesForPrefix(string) int64 + CountEntriesForPrefix(string) int GetAllForPrefix(string) (map[string]timestampedValue, error) GetKeysForPrefix(string) []string } @@ -37,7 +37,7 @@ func (cs cacheDoubleStore) Put(key string, value interface{}) { } func (cs cacheDoubleStore) Append(key string, value interface{}) { - var elements map[interface{}]struct{} + var elements map[interface{}]struct{} // using map for faster check if element is present if v, err := cs.Get(key); err == nil { elements = v.(map[interface{}]struct{}) } else { @@ -81,9 +81,9 @@ func (cs cacheDoubleStore) DeletePrefix(prefix string) { delete(cs, prefix) } -func (cs cacheDoubleStore) CountEntriesForPrefix(prefix string) int64 { +func (cs cacheDoubleStore) CountEntriesForPrefix(prefix string) int { if _, ok := cs[prefix]; ok { - return int64(len(cs[prefix])) + return len(cs[prefix]) } return 0 } @@ -110,13 +110,13 @@ func (cs cacheDoubleStore) GetKeysForPrefix(prefix string) (keys []string) { // faster to access type cacheSimpleStore struct { cache map[string]timestampedValue - counters map[string]int64 + counters map[string]int } func newSimpleStore() cacheSimpleStore { return cacheSimpleStore{ cache: make(map[string]timestampedValue), - counters: make(map[string]int64), + counters: make(map[string]int), } } @@ -198,7 +198,7 @@ func (cs cacheSimpleStore) descount(key string) { } } -func (cs cacheSimpleStore) CountEntriesForPrefix(prefix string) int64 { +func (cs cacheSimpleStore) CountEntriesForPrefix(prefix string) int { if _, ok := cs.counters[prefix]; ok { return cs.counters[prefix] } diff --git a/engine/storage_map.go b/engine/storage_map.go index 55db2969f..575d5b1e2 100644 --- a/engine/storage_map.go +++ b/engine/storage_map.go @@ -58,7 +58,7 @@ func (ms *MapStorage) GetKeysForPrefix(string) ([]string, error) { func (ms *MapStorage) CacheRating(dKeys, rpKeys, rpfKeys, alsKeys, lcrKeys []string) error { cache2go.BeginTransaction() - if dKeys == nil { + if dKeys == nil || (float64(cache2go.CountEntries(DESTINATION_PREFIX))*0.5 < float64(len(dKeys))) { cache2go.RemPrefixKey(DESTINATION_PREFIX) } else { CleanStalePrefixes(dKeys) diff --git a/engine/storage_redis.go b/engine/storage_redis.go index ec6d14b67..207a17854 100644 --- a/engine/storage_redis.go +++ b/engine/storage_redis.go @@ -73,7 +73,8 @@ func (rs *RedisStorage) GetKeysForPrefix(prefix string) ([]string, error) { func (rs *RedisStorage) CacheRating(dKeys, rpKeys, rpfKeys, alsKeys, lcrKeys []string) (err error) { cache2go.BeginTransaction() - if dKeys == nil { + if dKeys == nil || (float64(cache2go.CountEntries(DESTINATION_PREFIX))*0.5 < float64(len(dKeys))) { + // if need to load more than a half of exiting keys load them all Logger.Info("Caching all destinations") if dKeys, err = rs.db.Keys(DESTINATION_PREFIX + "*"); err != nil { cache2go.RollbackTransaction() From b7891b87bac1189ed501b47d7f04c7f90e6d5cbb Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Thu, 27 Nov 2014 12:10:33 +0200 Subject: [PATCH 6/9] type fixes --- utils/apitpdata.go | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/utils/apitpdata.go b/utils/apitpdata.go index 0b33d38ab..f8ea55f85 100644 --- a/utils/apitpdata.go +++ b/utils/apitpdata.go @@ -624,14 +624,14 @@ type AttrCacheStats struct { // Add in the future filters here maybe so we avoid } type CacheStats struct { - Destinations int64 - RatingPlans int64 - RatingProfiles int64 - Actions int64 - SharedGroups int64 - RatingAliases int64 - AccountAliases int64 - DerivedChargers int64 + Destinations int + RatingPlans int + RatingProfiles int + Actions int + SharedGroups int + RatingAliases int + AccountAliases int + DerivedChargers int } type AttrCachedItemAge struct { From 3e03b328b028d0fbb5df24e73c9e810ad0693411 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Thu, 27 Nov 2014 14:44:20 +0200 Subject: [PATCH 7/9] added destinations cache threshold --- engine/storage_interface.go | 2 ++ engine/storage_map.go | 2 +- engine/storage_redis.go | 2 +- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/engine/storage_interface.go b/engine/storage_interface.go index 0430dcd23..a001f388b 100644 --- a/engine/storage_interface.go +++ b/engine/storage_interface.go @@ -59,6 +59,8 @@ const ( CREATE_CDRS_TABLES_SQL = "create_cdrs_tables.sql" CREATE_TARIFFPLAN_TABLES_SQL = "create_tariffplan_tables.sql" TEST_SQL = "TEST_SQL" + + DESTINATIONS_LOAD_THRESHOLD = 0.1 ) type Storage interface { diff --git a/engine/storage_map.go b/engine/storage_map.go index 575d5b1e2..813d52215 100644 --- a/engine/storage_map.go +++ b/engine/storage_map.go @@ -58,7 +58,7 @@ func (ms *MapStorage) GetKeysForPrefix(string) ([]string, error) { func (ms *MapStorage) CacheRating(dKeys, rpKeys, rpfKeys, alsKeys, lcrKeys []string) error { cache2go.BeginTransaction() - if dKeys == nil || (float64(cache2go.CountEntries(DESTINATION_PREFIX))*0.5 < float64(len(dKeys))) { + if dKeys == nil || (float64(cache2go.CountEntries(DESTINATION_PREFIX))*DESTINATIONS_LOAD_THRESHOLD < float64(len(dKeys))) { cache2go.RemPrefixKey(DESTINATION_PREFIX) } else { CleanStalePrefixes(dKeys) diff --git a/engine/storage_redis.go b/engine/storage_redis.go index 207a17854..418d6a7ba 100644 --- a/engine/storage_redis.go +++ b/engine/storage_redis.go @@ -73,7 +73,7 @@ func (rs *RedisStorage) GetKeysForPrefix(prefix string) ([]string, error) { func (rs *RedisStorage) CacheRating(dKeys, rpKeys, rpfKeys, alsKeys, lcrKeys []string) (err error) { cache2go.BeginTransaction() - if dKeys == nil || (float64(cache2go.CountEntries(DESTINATION_PREFIX))*0.5 < float64(len(dKeys))) { + if dKeys == nil || (float64(cache2go.CountEntries(DESTINATION_PREFIX))*DESTINATIONS_LOAD_THRESHOLD < float64(len(dKeys))) { // if need to load more than a half of exiting keys load them all Logger.Info("Caching all destinations") if dKeys, err = rs.db.Keys(DESTINATION_PREFIX + "*"); err != nil { From 98d3bd6cde5353dd7246420f074854502e314c79 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Thu, 27 Nov 2014 14:52:06 +0200 Subject: [PATCH 8/9] fixed status command in console --- console/status.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/console/status.go b/console/status.go index e58151f5d..f316a4765 100644 --- a/console/status.go +++ b/console/status.go @@ -50,7 +50,7 @@ func (self *CmdStatus) RpcParams() interface{} { } func (self *CmdStatus) RpcResult() interface{} { - var s string + var s map[string]interface{} return &s } From d0fb4aab90c5e490e1145f65bba3232dfb1fef09 Mon Sep 17 00:00:00 2001 From: Radu Ioan Fericean Date: Thu, 27 Nov 2014 15:37:31 +0200 Subject: [PATCH 9/9] added coredump file to debian init command --- cmd/cgr-engine/cgr-engine.go | 28 +--------------------------- data/scripts/pkg/debian/cgrates.init | 4 +++- 2 files changed, 4 insertions(+), 28 deletions(-) diff --git a/cmd/cgr-engine/cgr-engine.go b/cmd/cgr-engine/cgr-engine.go index 4e5cafceb..da112849c 100644 --- a/cmd/cgr-engine/cgr-engine.go +++ b/cmd/cgr-engine/cgr-engine.go @@ -22,10 +22,8 @@ import ( "errors" "flag" "fmt" - "io/ioutil" "log" "os" - "runtime" //"runtime" "strconv" "time" @@ -304,22 +302,7 @@ func writePid() { } } -func start() { - defer func() { - if r := recover(); r != nil { - engine.Logger.Crit(fmt.Sprintf("CRITICAL ERROR: %v", r)) - var stack [8192]byte - runtime.Stack(stack[:], false) - if tmpFile, err := ioutil.TempFile(os.TempDir(), "cgr_coredump"); err != nil { - engine.Logger.Crit(fmt.Sprintf("Cannot create coredump file: %v", err)) - engine.Logger.Crit(string(stack[:])) - } else { - tmpFile.Write(stack[:]) - tmpFile.Close() - engine.Logger.Crit(fmt.Sprintf("Core dumped: %s", tmpFile.Name())) - } - } - }() +func main() { flag.Parse() if *version { fmt.Println("CGRateS " + utils.VERSION) @@ -529,12 +512,3 @@ func start() { } engine.Logger.Info("Stopped all components. CGRateS shutdown!") } - -func main() { - defer func() { - if e := recover(); e != nil { - fmt.Fprintf(os.Stderr, "PANIC: %v\n", e) - } - }() - start() -} diff --git a/data/scripts/pkg/debian/cgrates.init b/data/scripts/pkg/debian/cgrates.init index 488c8b94f..e2417f293 100755 --- a/data/scripts/pkg/debian/cgrates.init +++ b/data/scripts/pkg/debian/cgrates.init @@ -23,6 +23,7 @@ GROUP=cgrates ENGINE_OPTS="" RUNDIR=/var/run/$NAME PIDFILE=$RUNDIR/cgr-engine.pid +COREDUMP=$RUNDIR/cgr-coredump.log SCRIPTNAME=/etc/init.d/$NAME DEFAULTS=/etc/default/$NAME ENABLE=false @@ -62,7 +63,8 @@ do_start() # 0 if daemon has been started # 1 if daemon was already running # 2 if daemon could not be started - start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \ + echo "Started at:" `date`>>$COREDUMP + start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null 2>>$COREDUMP \ || return 1 start-stop-daemon --start --quiet --pidfile $PIDFILE --make-pidfile --exec $DAEMON --chuid $USER:$GROUP --background -- \ $ENGINE_OPTS \