From 443c594edca6f533d8e87a970c8955449173582e Mon Sep 17 00:00:00 2001 From: DanB Date: Thu, 17 Nov 2016 13:18:43 +0100 Subject: [PATCH] StorageMongo creating indexes based on storDBType --- agents/dmtagent.go | 3 +- engine/storage_mongo_datadb.go | 230 +++++++++++++++++---------------- sessionmanager/smgeneric.go | 3 +- 3 files changed, 126 insertions(+), 110 deletions(-) diff --git a/agents/dmtagent.go b/agents/dmtagent.go index 21bf137b0..3b928faf4 100644 --- a/agents/dmtagent.go +++ b/agents/dmtagent.go @@ -93,12 +93,13 @@ func (self DiameterAgent) processCCR(ccr *CCR, reqProcessor *config.DARequestPro } smgEv, err := ccr.AsSMGenericEvent(reqProcessor.CCRFields) if err != nil { + utils.Logger.Err(fmt.Sprintf(" Processing message: %+v AsSMGenericEvent, error: %s", ccr.diamMessage, err)) *cca = *NewBareCCAFromCCR(ccr, self.cgrCfg.DiameterAgentCfg().OriginHost, self.cgrCfg.DiameterAgentCfg().OriginRealm) if err := messageSetAVPsWithPath(cca.diamMessage, []interface{}{"Result-Code"}, strconv.Itoa(DiameterRatingFailed), false, self.cgrCfg.DiameterAgentCfg().Timezone); err != nil { + utils.Logger.Err(fmt.Sprintf(" Processing message: %+v messageSetAVPsWithPath, error: %s", cca.diamMessage, err.Error())) return false, err } - utils.Logger.Err(fmt.Sprintf(" Processing message: %+v AsSMGenericEvent, error: %s", ccr.diamMessage, err)) return false, ErrDiameterRatingFailed } if len(reqProcessor.Flags) != 0 { diff --git a/engine/storage_mongo_datadb.go b/engine/storage_mongo_datadb.go index ac0c573bc..d56eb75be 100644 --- a/engine/storage_mongo_datadb.go +++ b/engine/storage_mongo_datadb.go @@ -124,7 +124,13 @@ func (ms *MongoStorage) EnsureIndexes() (err error) { Background: false, // Build index in background and return immediately Sparse: false, // Only index documents containing the Key fields } - for _, col := range []string{colAct, colApl, colAtr, colDcs, colAls, colRls, colUsr, colLcr, colLht, colRpl, colDst, colRds} { + var colectNames []string // collection names containing this index + if ms.storageType == utils.TariffPlanDB { + colectNames = []string{colAct, colApl, colAtr, colDcs, colRls, colRpl, colLcr, colDst, colRds} + } else if ms.storageType == utils.DataDB { + colectNames = []string{colAls, colUsr, colLht} + } + for _, col := range colectNames { if err = db.C(col).EnsureIndex(idx); err != nil { return } @@ -136,98 +142,94 @@ func (ms *MongoStorage) EnsureIndexes() (err error) { Background: false, Sparse: false, } - for _, col := range []string{colRpf, colShg, colAcc, colCrs} { + if ms.storageType == utils.TariffPlanDB { + colectNames = []string{colRpf, colShg, colCrs} + } else if ms.storageType == utils.DataDB { + colectNames = []string{colAcc} + } + for _, col := range colectNames { if err = db.C(col).EnsureIndex(idx); err != nil { return } } - idx = mgo.Index{ - Key: []string{"tpid", "tag"}, - Unique: true, - DropDups: false, - Background: false, - Sparse: false, - } - for _, col := range []string{utils.TBL_TP_TIMINGS, utils.TBL_TP_DESTINATIONS, utils.TBL_TP_DESTINATION_RATES, utils.TBL_TP_RATING_PLANS, - utils.TBL_TP_SHARED_GROUPS, utils.TBL_TP_CDR_STATS, utils.TBL_TP_ACTIONS, utils.TBL_TP_ACTION_PLANS, utils.TBL_TP_ACTION_TRIGGERS} { - if err = db.C(col).EnsureIndex(idx); err != nil { - return - } - } - idx = mgo.Index{ - Key: []string{"tpid", "direction", "tenant", "category", "subject", "loadid"}, - Unique: true, - DropDups: false, - Background: false, - Sparse: false, - } - if err = db.C(utils.TBL_TP_RATE_PROFILES).EnsureIndex(idx); err != nil { - return - } - idx = mgo.Index{ - Key: []string{"tpid", "direction", "tenant", "category", "account", "subject"}, - Unique: true, - DropDups: false, - Background: false, - Sparse: false, - } - if err = db.C(utils.TBL_TP_LCRS).EnsureIndex(idx); err != nil { - return - } - idx = mgo.Index{ - Key: []string{"tpid", "tenant", "username"}, - Unique: true, - DropDups: false, - Background: false, - Sparse: false, - } - if err = db.C(utils.TBL_TP_USERS).EnsureIndex(idx); err != nil { - return - } - idx = mgo.Index{ - Key: []string{"tpid", "direction", "tenant", "category", "account", "subject", "context"}, - Unique: true, - DropDups: false, - Background: false, - Sparse: false, - } - if err = db.C(utils.TBL_TP_LCRS).EnsureIndex(idx); err != nil { - return - } - idx = mgo.Index{ - Key: []string{"tpid", "direction", "tenant", "category", "subject", "account", "loadid"}, - Unique: true, - DropDups: false, - Background: false, - Sparse: false, - } - if err = db.C(utils.TBL_TP_DERIVED_CHARGERS).EnsureIndex(idx); err != nil { - return - } - idx = mgo.Index{ - Key: []string{"tpid", "direction", "tenant", "account", "loadid"}, - Unique: true, - DropDups: false, - Background: false, - Sparse: false, - } - if err = db.C(utils.TBL_TP_DERIVED_CHARGERS).EnsureIndex(idx); err != nil { - return - } - idx = mgo.Index{ - Key: []string{CGRIDLow, RunIDLow, OriginIDLow}, - Unique: true, - DropDups: false, - Background: false, - Sparse: false, - } - if err = db.C(utils.TBL_CDRS).EnsureIndex(idx); err != nil { - return - } - for _, idxKey := range ms.cdrsIndexes { + if ms.storageType == utils.StorDB { idx = mgo.Index{ - Key: []string{idxKey}, - Unique: false, + Key: []string{"tpid", "tag"}, + Unique: true, + DropDups: false, + Background: false, + Sparse: false, + } + for _, col := range []string{utils.TBL_TP_TIMINGS, utils.TBL_TP_DESTINATIONS, utils.TBL_TP_DESTINATION_RATES, utils.TBL_TP_RATING_PLANS, + utils.TBL_TP_SHARED_GROUPS, utils.TBL_TP_CDR_STATS, utils.TBL_TP_ACTIONS, utils.TBL_TP_ACTION_PLANS, utils.TBL_TP_ACTION_TRIGGERS} { + if err = db.C(col).EnsureIndex(idx); err != nil { + return + } + } + + idx = mgo.Index{ + Key: []string{"tpid", "direction", "tenant", "category", "subject", "loadid"}, + Unique: true, + DropDups: false, + Background: false, + Sparse: false, + } + if err = db.C(utils.TBL_TP_RATE_PROFILES).EnsureIndex(idx); err != nil { + return + } + idx = mgo.Index{ + Key: []string{"tpid", "direction", "tenant", "category", "account", "subject"}, + Unique: true, + DropDups: false, + Background: false, + Sparse: false, + } + if err = db.C(utils.TBL_TP_LCRS).EnsureIndex(idx); err != nil { + return + } + idx = mgo.Index{ + Key: []string{"tpid", "tenant", "username"}, + Unique: true, + DropDups: false, + Background: false, + Sparse: false, + } + if err = db.C(utils.TBL_TP_USERS).EnsureIndex(idx); err != nil { + return + } + idx = mgo.Index{ + Key: []string{"tpid", "direction", "tenant", "category", "account", "subject", "context"}, + Unique: true, + DropDups: false, + Background: false, + Sparse: false, + } + if err = db.C(utils.TBL_TP_LCRS).EnsureIndex(idx); err != nil { + return + } + idx = mgo.Index{ + Key: []string{"tpid", "direction", "tenant", "category", "subject", "account", "loadid"}, + Unique: true, + DropDups: false, + Background: false, + Sparse: false, + } + if err = db.C(utils.TBL_TP_DERIVED_CHARGERS).EnsureIndex(idx); err != nil { + return + } + idx = mgo.Index{ + Key: []string{"tpid", "direction", "tenant", "account", "loadid"}, + Unique: true, + DropDups: false, + Background: false, + Sparse: false, + } + if err = db.C(utils.TBL_TP_DERIVED_CHARGERS).EnsureIndex(idx); err != nil { + return + } + idx = mgo.Index{ + Key: []string{CGRIDLow, RunIDLow, OriginIDLow}, + Unique: true, DropDups: false, Background: false, Sparse: false, @@ -235,26 +237,38 @@ func (ms *MongoStorage) EnsureIndexes() (err error) { if err = db.C(utils.TBL_CDRS).EnsureIndex(idx); err != nil { return } - } - idx = mgo.Index{ - Key: []string{CGRIDLow, RunIDLow}, - Unique: true, - DropDups: false, - Background: false, - Sparse: false, - } - if err = db.C(utils.TBLSMCosts).EnsureIndex(idx); err != nil { - return - } - idx = mgo.Index{ - Key: []string{OriginHostLow, OriginIDLow}, - Unique: false, - DropDups: false, - Background: false, - Sparse: false, - } - if err = db.C(utils.TBLSMCosts).EnsureIndex(idx); err != nil { - return + for _, idxKey := range ms.cdrsIndexes { + idx = mgo.Index{ + Key: []string{idxKey}, + Unique: false, + DropDups: false, + Background: false, + Sparse: false, + } + if err = db.C(utils.TBL_CDRS).EnsureIndex(idx); err != nil { + return + } + } + idx = mgo.Index{ + Key: []string{CGRIDLow, RunIDLow}, + Unique: true, + DropDups: false, + Background: false, + Sparse: false, + } + if err = db.C(utils.TBLSMCosts).EnsureIndex(idx); err != nil { + return + } + idx = mgo.Index{ + Key: []string{OriginHostLow, OriginIDLow}, + Unique: false, + DropDups: false, + Background: false, + Sparse: false, + } + if err = db.C(utils.TBLSMCosts).EnsureIndex(idx); err != nil { + return + } } return } diff --git a/sessionmanager/smgeneric.go b/sessionmanager/smgeneric.go index b912a7ec5..0c0eb07bb 100644 --- a/sessionmanager/smgeneric.go +++ b/sessionmanager/smgeneric.go @@ -242,7 +242,8 @@ func (smg *SMGeneric) indexSession(s *SMGSession, passiveSessions bool) { if _, hasIt := ssRIdx[s.CGRID]; !hasIt { ssRIdx[s.CGRID] = make([]*riFieldNameVal, 0) } - ssRIdx[s.CGRID] = append(ssRIdx[s.CGRID], &riFieldNameVal{runID: s.RunID, fieldName: fieldName, fieldValue: fieldVal}) + riFlds := ssRIdx[s.CGRID] // attempt to avoid map concurrency write panic + ssRIdx[s.CGRID] = append(riFlds, &riFieldNameVal{runID: s.RunID, fieldName: fieldName, fieldValue: fieldVal}) } return }