TTL for the LoggingDB

+ Added and tested the TTL for the logging database
This commit is contained in:
Thorsten Sommer 2014-11-08 13:02:19 +01:00
parent d95a8da77e
commit dfd35a6164
2 changed files with 34 additions and 7 deletions

View File

@ -6,6 +6,7 @@ import (
"github.com/SommerEngineering/Ocean/Log" "github.com/SommerEngineering/Ocean/Log"
LM "github.com/SommerEngineering/Ocean/Log/Meta" LM "github.com/SommerEngineering/Ocean/Log/Meta"
"gopkg.in/mgo.v2" "gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"strconv" "strconv"
"strings" "strings"
"time" "time"
@ -21,7 +22,8 @@ func initDatabase() {
databaseUsername := ConfigurationDB.Read(`LogDBUsername`) databaseUsername := ConfigurationDB.Read(`LogDBUsername`)
databasePassword := ConfigurationDB.Read(`LogDBPassword`) databasePassword := ConfigurationDB.Read(`LogDBPassword`)
expire := strings.ToLower(ConfigurationDB.Read(`LogDBEventsExpire`)) == `true` expire := strings.ToLower(ConfigurationDB.Read(`LogDBEventsExpire`)) == `true`
expireAfterDays := 36500 expireAfterDays := 21900 // 60 years ~ maximum of MongoDB
expireValue4DisabledFunction := 21900 // 60 years ~ maximum of MongoDB
if value, errValue := strconv.Atoi(ConfigurationDB.Read(`LogDBEventsExpireAfterDays`)); errValue != nil { if value, errValue := strconv.Atoi(ConfigurationDB.Read(`LogDBEventsExpireAfterDays`)); errValue != nil {
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityMiddle, LM.ImpactUnknown, LM.MessageNameCONFIGURATION, `It was not possible to read the configuration for the expire time of logging events. Log events will not expire any more.`, errValue.Error()) Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityMiddle, LM.ImpactUnknown, LM.MessageNameCONFIGURATION, `It was not possible to read the configuration for the expire time of logging events. Log events will not expire any more.`, errValue.Error())
@ -29,7 +31,11 @@ func initDatabase() {
} else { } else {
if expire { if expire {
Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameCONFIGURATION, fmt.Sprintf("All logging events are expire after %d days.", value)) Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameCONFIGURATION, fmt.Sprintf("All logging events are expire after %d days.", value))
expireAfterDays = value if value > expireValue4DisabledFunction {
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelWARN, LM.SeverityLow, LM.ImpactLow, LM.MessageNameDATABASE, fmt.Sprintf("Cannot set the logging database's TTL to %d, because MongoDB does not allow more than %d (63 years). Use now the maximum instead.", value, expireValue4DisabledFunction))
} else {
expireAfterDays = value
}
} }
} }
@ -56,15 +62,30 @@ func initDatabase() {
// //
// Take care about all the indexes: // Take care about all the indexes:
// //
expireAfterSeconds := expireAfterDays * 24 * 60 * 60
indexTimeUTC := mgo.Index{} indexTimeUTC := mgo.Index{}
indexTimeUTC.Key = []string{`TimeUTC`} indexTimeUTC.Key = []string{`TimeUTC`}
if expire { indexTimeUTC.ExpireAfter = time.Duration(expireValue4DisabledFunction * 24 * 60 * 60)
indexTimeUTC.ExpireAfter = time.Duration(expireAfterDays*24) * time.Hour
} else {
indexTimeUTC.ExpireAfter = time.Duration(0)
}
logDBCollection.EnsureIndex(indexTimeUTC) logDBCollection.EnsureIndex(indexTimeUTC)
// Update the expire policy:
updateResult := TTLUpdateResult{}
updateCommand := bson.D{
{`collMod`, `Logbook`},
{`index`,
bson.D{
{`keyPattern`, bson.D{{`TimeUTC`, 1}}},
{`expireAfterSeconds`, expireAfterSeconds},
},
},
}
if errUpdate := logDB.Run(updateCommand, &updateResult); errUpdate != nil {
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelWARN, LM.SeverityUnknown, LM.ImpactUnknown, LM.MessageNameDATABASE, `Was not able to update the expire policy for the logging database.`, errUpdate.Error())
} else {
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelWARN, LM.SeverityUnknown, LM.ImpactUnknown, LM.MessageNameDATABASE, fmt.Sprintf(`Update the expire policy for the logging database done.`))
}
indexProject := mgo.Index{} indexProject := mgo.Index{}
indexProject.Key = []string{`Project`} indexProject.Key = []string{`Project`}
logDBCollection.EnsureIndex(indexProject) logDBCollection.EnsureIndex(indexProject)

View File

@ -16,3 +16,9 @@ type LogDBEntry struct {
MessageDescription string `bson:"MessageDescription"` MessageDescription string `bson:"MessageDescription"`
Parameters []string `bson:"Parameters"` Parameters []string `bson:"Parameters"`
} }
type TTLUpdateResult struct {
ExpireAfterSeconds_old int32 `bson:"expireAfterSeconds_old"`
ExpireAfterSeconds_new int32 `bson:"expireAfterSeconds_new"`
Ok int32 `bson:"ok"`
}