Bug fixing

+ DB access is now right and uses copied sessions
+ DB session is now specifying the safe state and the mode
+ Fixed the issue with too early ICCC messages regarding to late cache
+ Add the MIME type for Dart
+ Fixed the issuse with wrong order of shutdown handlers
- TODO: Testing of these changes
This commit is contained in:
Thorsten Sommer 2014-06-04 21:41:18 +02:00
parent 86451938ec
commit 7120a729bd
19 changed files with 85 additions and 48 deletions

View File

@ -5,15 +5,22 @@ import "labix.org/v2/mgo"
/* /*
Get the database instance of the MGo Mongo driver. Get the database instance of the MGo Mongo driver.
*/ */
func DB() (result *mgo.Database) { func DB() (session *mgo.Session, database *mgo.Database) {
result = db session = mainSession.Copy()
database = session.DB(databaseDB)
database.Login(databaseUsername, databasePassword)
return return
} }
/* /*
Get directly the GridFS instance of the Mgo Mongo driver. Get directly the GridFS instance of the Mgo Mongo driver.
*/ */
func GridFS() (result *mgo.GridFS) { func GridFS() (session *mgo.Session, filesystem *mgo.GridFS) {
result = gridFS session = mainSession.Copy()
database := session.DB(databaseDB)
database.Login(databaseUsername, databasePassword)
filesystem = database.GridFS(`fs`)
return return
} }

View File

@ -10,20 +10,20 @@ func init() {
Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameDATABASE, `Init the customer database.`) Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameDATABASE, `Init the customer database.`)
databaseHost := ConfigurationDB.Read(`CustomerDBHost`) databaseHost := ConfigurationDB.Read(`CustomerDBHost`)
databaseDB := ConfigurationDB.Read(`CustomerDBDatabase`) databaseDB = ConfigurationDB.Read(`CustomerDBDatabase`)
databaseUsername := ConfigurationDB.Read(`CustomerDBUsername`) databaseUsername = ConfigurationDB.Read(`CustomerDBUsername`)
databasePassword := ConfigurationDB.Read(`CustomerDBPassword`) databasePassword = ConfigurationDB.Read(`CustomerDBPassword`)
// Connect to MongoDB: // Connect to MongoDB:
if newSession, errDial := mgo.Dial(databaseHost); errDial != nil { if newSession, errDial := mgo.Dial(databaseHost); errDial != nil {
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityUnknown, LM.ImpactUnknown, LM.MessageNameDATABASE, `It was not possible to connect to the MongoDB host `+databaseHost, errDial.Error()) Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityUnknown, LM.ImpactUnknown, LM.MessageNameDATABASE, `It was not possible to connect to the MongoDB host `+databaseHost, errDial.Error())
return return
} else { } else {
session = newSession mainSession = newSession
} }
// Use the correct database: // Use the correct database:
db = session.DB(databaseDB) db := mainSession.DB(databaseDB)
if db == nil { if db == nil {
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityCritical, LM.ImpactCritical, LM.MessageNameDATABASE, `Was not able to get the customer database.`) Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityCritical, LM.ImpactCritical, LM.MessageNameDATABASE, `Was not able to get the customer database.`)
return return
@ -35,8 +35,15 @@ func init() {
return return
} }
// In case of write operations, wait for the majority of servers to be done:
mainSession.SetSafe(&mgo.Safe{WMode: "majority"})
// Set the consistency mode to read from any secondary server and write to the primary.
// Copied sessions can overwrite this setting of necessary.
mainSession.SetMode(mgo.Eventual, true)
// Get the GridFS: // Get the GridFS:
gridFS = db.GridFS(`fs`) gridFS := db.GridFS(`fs`)
if gridFS == nil { if gridFS == nil {
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityCritical, LM.ImpactCritical, LM.MessageNameDATABASE, `Was not able to get the GridFS from the database.`) Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityCritical, LM.ImpactCritical, LM.MessageNameDATABASE, `Was not able to get the GridFS from the database.`)
return return

View File

@ -14,6 +14,4 @@ This function is called if the Ocean server is shutting down.
*/ */
func (a ShutdownFunction) Shutdown() { func (a ShutdownFunction) Shutdown() {
Log.LogShort(senderName, LM.CategoryAPP, LM.LevelWARN, LM.MessageNameSHUTDOWN, `Close now the customer database connection.`) Log.LogShort(senderName, LM.CategoryAPP, LM.LevelWARN, LM.MessageNameSHUTDOWN, `Close now the customer database connection.`)
db.Logout()
session.Close()
} }

View File

@ -4,8 +4,9 @@ import "labix.org/v2/mgo"
import LM "github.com/SommerEngineering/Ocean/Log/Meta" import LM "github.com/SommerEngineering/Ocean/Log/Meta"
var ( var (
session *mgo.Session = nil mainSession *mgo.Session = nil
db *mgo.Database = nil
gridFS *mgo.GridFS = nil
senderName LM.Sender = `System::CustomerDB` senderName LM.Sender = `System::CustomerDB`
databaseUsername string = ``
databasePassword string = ``
databaseDB string = ``
) )

View File

@ -1,4 +1,4 @@
/* /*
This is the "[I]nter Data [C]enter Se[c]ure [C]ommunication" :) This is the "[I]nter Data [C]enter and Appli[c]ation [C]ommunication"
*/ */
package ICCC package ICCC

View File

@ -21,5 +21,6 @@ func init() {
initDB() initDB()
registerHost2Database() registerHost2Database()
cacheTimerLogic(false)
initCacheTimer() initCacheTimer()
} }

View File

@ -10,7 +10,7 @@ func initDB() {
defer Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameINIT, `Done init the ICCC collection.`) defer Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameINIT, `Done init the ICCC collection.`)
// Get the database: // Get the database:
db = CustomerDB.DB() dbSession, db = CustomerDB.DB()
if db == nil { if db == nil {
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityCritical, LM.ImpactCritical, LM.MessageNameDATABASE, `Was not able to get the customer database.`) Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityCritical, LM.ImpactCritical, LM.MessageNameDATABASE, `Was not able to get the customer database.`)

View File

@ -9,10 +9,14 @@ import "github.com/SommerEngineering/Ocean/Log"
import LM "github.com/SommerEngineering/Ocean/Log/Meta" import LM "github.com/SommerEngineering/Ocean/Log/Meta"
func initCacheTimer() { func initCacheTimer() {
go func() { go func() {
for { for {
cacheTimerLogic(true)
}
}()
}
func cacheTimerLogic(waiting bool) {
if Shutdown.IsDown() { if Shutdown.IsDown() {
return return
} }
@ -29,13 +33,14 @@ func initCacheTimer() {
} }
cacheListenerDatabaseLock.Unlock() cacheListenerDatabaseLock.Unlock()
Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameEXECUTE, `The listener cache was refreshed with the values from the database.`, fmt.Sprintf(`last count=%d`, lastCount), fmt.Sprintf(`new count=%d`, cacheListenerDatabase.Len()))
if waiting {
nextDuration := time.Duration(5) * time.Minute nextDuration := time.Duration(5) * time.Minute
if cacheListenerDatabase.Len() == 0 { if cacheListenerDatabase.Len() == 0 {
nextDuration = time.Duration(10) * time.Second nextDuration = time.Duration(10) * time.Second
} }
Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameEXECUTE, `The listener cache was refreshed with the values from the database.`, fmt.Sprintf(`last count=%d`, lastCount), fmt.Sprintf(`new count=%d`, cacheListenerDatabase.Len()))
time.Sleep(nextDuration) time.Sleep(nextDuration)
} }
}()
} }

View File

@ -26,5 +26,7 @@ func (a ShutdownFunction) Shutdown() {
collectionListener.Update(selectionUpdate, entry) collectionListener.Update(selectionUpdate, entry)
} }
db.Logout()
dbSession.Close()
Log.LogShort(senderName, LM.CategoryAPP, LM.LevelWARN, LM.MessageNameSHUTDOWN, `Done shutting down now all ICCC listener for this host.`) Log.LogShort(senderName, LM.CategoryAPP, LM.LevelWARN, LM.MessageNameSHUTDOWN, `Done shutting down now all ICCC listener for this host.`)
} }

View File

@ -16,6 +16,7 @@ const (
var ( var (
senderName LM.Sender = `ICCC` senderName LM.Sender = `ICCC`
db *mgo.Database = nil db *mgo.Database = nil
dbSession *mgo.Session = nil
collectionListener *mgo.Collection = nil collectionListener *mgo.Collection = nil
collectionHosts *mgo.Collection = nil collectionHosts *mgo.Collection = nil
reservedSystemChannels []string = []string{ChannelSYSTEM, ChannelNUMGEN, ChannelSHUTDOWN, ChannelSTARTUP, ChannelICCC} reservedSystemChannels []string = []string{ChannelSYSTEM, ChannelNUMGEN, ChannelSHUTDOWN, ChannelSTARTUP, ChannelICCC}

View File

@ -3,6 +3,7 @@ package MimeTypes
var TypeWebHTML = MimeType{MimeType: "text/html", FileExtension: []string{".html", ".htm"}} var TypeWebHTML = MimeType{MimeType: "text/html", FileExtension: []string{".html", ".htm"}}
var TypeWebCSS = MimeType{MimeType: "text/css", FileExtension: []string{".css"}} var TypeWebCSS = MimeType{MimeType: "text/css", FileExtension: []string{".css"}}
var TypeWebJavaScript = MimeType{MimeType: "text/javascript", FileExtension: []string{".js"}} var TypeWebJavaScript = MimeType{MimeType: "text/javascript", FileExtension: []string{".js"}}
var TypeWebDart = MimeType{MimeType: "application/dart", FileExtension: []string{".dart"}}
var TypeXML = MimeType{MimeType: "text/xml", FileExtension: []string{".xml"}} var TypeXML = MimeType{MimeType: "text/xml", FileExtension: []string{".xml"}}
var TypeArchiveZIP = MimeType{MimeType: "application/zip", FileExtension: []string{".zip"}} var TypeArchiveZIP = MimeType{MimeType: "application/zip", FileExtension: []string{".zip"}}
var TypeArchiveGZ = MimeType{MimeType: "application/gzip", FileExtension: []string{".gz"}} var TypeArchiveGZ = MimeType{MimeType: "application/gzip", FileExtension: []string{".gz"}}

View File

@ -10,7 +10,7 @@ func initDB() {
defer Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameINIT, `Done init of number generator collection.`) defer Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameINIT, `Done init of number generator collection.`)
// Get the database: // Get the database:
db = CustomerDB.DB() dbSession, db = CustomerDB.DB()
if db == nil { if db == nil {
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityCritical, LM.ImpactCritical, LM.MessageNameDATABASE, `Was not able to get the customer database.`) Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityCritical, LM.ImpactCritical, LM.MessageNameDATABASE, `Was not able to get the customer database.`)

View File

@ -8,4 +8,6 @@ type ShutdownFunction struct {
func (a ShutdownFunction) Shutdown() { func (a ShutdownFunction) Shutdown() {
Log.LogShort(senderName, LM.CategoryAPP, LM.LevelWARN, LM.MessageNameSHUTDOWN, `Shutting down the number generator.`) Log.LogShort(senderName, LM.CategoryAPP, LM.LevelWARN, LM.MessageNameSHUTDOWN, `Shutting down the number generator.`)
db.Logout()
dbSession.Close()
} }

View File

@ -10,6 +10,7 @@ var (
isActive bool = false isActive bool = false
getHandler string = `` getHandler string = ``
db *mgo.Database = nil db *mgo.Database = nil
dbSession *mgo.Session = nil
collectionNumGen *mgo.Collection = nil collectionNumGen *mgo.Collection = nil
channelBufferSize int = 10 channelBufferSize int = 10
channelList map[string]chan int64 = nil channelList map[string]chan int64 = nil

View File

@ -9,7 +9,7 @@ type ShutdownHandler interface {
} }
func AddShutdownHandler(handler ShutdownHandler) { func AddShutdownHandler(handler ShutdownHandler) {
shutdownHandlers.PushBack(handler) shutdownHandlers.PushFront(handler)
} }
func executeShutdown() { func executeShutdown() {

View File

@ -26,7 +26,9 @@ func init() {
logStaticFileRequests = ConfigurationDB.Read(`LogStaticFileRequests`) == `true` logStaticFileRequests = ConfigurationDB.Read(`LogStaticFileRequests`) == `true`
// Read the static files' data from GridFS: // Read the static files' data from GridFS:
gridFS := CustomerDB.GridFS() dbSession, gridFS := CustomerDB.GridFS()
defer dbSession.Close()
if gridFile, errGridFile := gridFS.Open(`staticFiles.zip`); errGridFile != nil { if gridFile, errGridFile := gridFS.Open(`staticFiles.zip`); errGridFile != nil {
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityCritical, LM.ImpactCritical, LM.MessageNameDATABASE, `Was not able to open the static files out of the GridFS!`, errGridFile.Error()) Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityCritical, LM.ImpactCritical, LM.MessageNameDATABASE, `Was not able to open the static files out of the GridFS!`, errGridFile.Error())
return return

View File

@ -78,11 +78,17 @@ func initSystem() {
Log.LoggingIsReady() Log.LoggingIsReady()
// Register all system shutdown handlers: // Register all system shutdown handlers:
//
// Please notice: If the shutdown event occurs ...
// * all application handlers are called (order: last comed, first served)
// * then, these system handlers are called (order: last comed, first served)
// * and finally, the logging device / system gets closed
Shutdown.InitShutdown() Shutdown.InitShutdown()
Shutdown.AddShutdownHandler(ICCC.ShutdownFunction{}) Shutdown.AddShutdownHandler(ICCC.ShutdownFunction{})
Shutdown.AddShutdownHandler(NumGen.ShutdownFunction{}) Shutdown.AddShutdownHandler(NumGen.ShutdownFunction{})
Shutdown.AddShutdownHandler(ConfigurationDB.ShutdownFunction{}) Shutdown.AddShutdownHandler(ConfigurationDB.ShutdownFunction{})
Shutdown.AddShutdownHandler(CustomerDB.ShutdownFunction{}) Shutdown.AddShutdownHandler(CustomerDB.ShutdownFunction{})
// The logging subsystem is not registered here, because it will be automated called at the end // The logging subsystem is not registered here, because it will be automated called at the end
// Register all system ICCC commands: // Register all system ICCC commands:

View File

@ -13,7 +13,9 @@ func init() {
Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameSTARTUP, `Starting the template engine.`) Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameSTARTUP, `Starting the template engine.`)
defer Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameSTARTUP, `Starting the template engine done.`) defer Log.LogShort(senderName, LM.CategorySYSTEM, LM.LevelINFO, LM.MessageNameSTARTUP, `Starting the template engine done.`)
gridFS := CustomerDB.GridFS() dbSession, gridFS := CustomerDB.GridFS()
defer dbSession.Close()
if gridFile, errGridFile := gridFS.Open(`templates.zip`); errGridFile != nil { if gridFile, errGridFile := gridFS.Open(`templates.zip`); errGridFile != nil {
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityCritical, LM.ImpactCritical, LM.MessageNameDATABASE, `Was not able to open the templates out of the GridFS!`, errGridFile.Error()) Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityCritical, LM.ImpactCritical, LM.MessageNameDATABASE, `Was not able to open the templates out of the GridFS!`, errGridFile.Error())
return return

View File

@ -17,7 +17,8 @@ func init() {
} }
filename = ConfigurationDB.Read(`FilenameWebResources`) filename = ConfigurationDB.Read(`FilenameWebResources`)
gridFS := CustomerDB.GridFS() dbSession, gridFS := CustomerDB.GridFS()
defer dbSession.Close()
if gridFile, errGridFile := gridFS.Open(filename); errGridFile != nil { if gridFile, errGridFile := gridFS.Open(filename); errGridFile != nil {
Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityCritical, LM.ImpactCritical, LM.MessageNameDATABASE, `Was not able to open the web content out of the GridFS!`, filename, errGridFile.Error()) Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityCritical, LM.ImpactCritical, LM.MessageNameDATABASE, `Was not able to open the web content out of the GridFS!`, filename, errGridFile.Error())