@@ -30,8 +30,7 @@ import (
3030 "github.com/signal18/replication-manager/utils/s18log"
3131 "github.com/signal18/replication-manager/utils/state"
3232 log "github.com/sirupsen/logrus"
33- logsqlerr "github.com/sirupsen/logrus"
34- logsqlgen "github.com/sirupsen/logrus"
33+ logsql "github.com/sirupsen/logrus"
3534 "golang.org/x/crypto/ssh"
3635)
3736
@@ -171,6 +170,11 @@ type Cluster struct {
171170 WaitingFailover int `json:"waitingFailover"`
172171 Configurator configurator.Configurator `json:"configurator"`
173172 DiffVariables []VariableDiff `json:"diffVariables"`
173+ inInitNodes bool `json:"-"`
174+ CanInitNodes bool `json:"canInitNodes"`
175+ errorInitNodes error `json:"-"`
176+ SqlErrorLog * logsql.Logger `json:"-"`
177+ SqlGeneralLog * logsql.Logger `json:"-"`
174178 sync.Mutex
175179 crcTable * crc64.Table
176180}
@@ -251,7 +255,8 @@ const (
251255
252256// Init initial cluster definition
253257func (cluster * Cluster ) Init (conf config.Config , cfgGroup string , tlog * s18log.TermLog , log * s18log.HttpLog , termlength int , runUUID string , repmgrVersion string , repmgrHostname string , key []byte ) error {
254-
258+ cluster .SqlErrorLog = logsql .New ()
259+ cluster .SqlGeneralLog = logsql .New ()
255260 cluster .crcTable = crc64 .MakeTable (crc64 .ECMA ) // http://golang.org/pkg/hash/crc64/#pkg-constants
256261 cluster .switchoverChan = make (chan bool )
257262 // should use buffered channels or it will block
@@ -263,6 +268,7 @@ func (cluster *Cluster) Init(conf config.Config, cfgGroup string, tlog *s18log.T
263268 cluster .addtableCond = nbc .New ()
264269 cluster .altertableCond = nbc .New ()
265270 cluster .canFlashBack = true
271+ cluster .CanInitNodes = true
266272 cluster .runOnceAfterTopology = true
267273 cluster .testStopCluster = true
268274 cluster .testStartCluster = true
@@ -309,34 +315,34 @@ func (cluster *Cluster) Init(conf config.Config, cfgGroup string, tlog *s18log.T
309315 MaxSize : cluster .Conf .LogRotateMaxSize ,
310316 MaxBackups : cluster .Conf .LogRotateMaxBackup ,
311317 MaxAge : cluster .Conf .LogRotateMaxAge ,
312- Level : logsqlerr .DebugLevel ,
313- Formatter : & logsqlerr .TextFormatter {
318+ Level : logsql .DebugLevel ,
319+ Formatter : & logsql .TextFormatter {
314320 DisableColors : true ,
315321 TimestampFormat : "2006-01-02 15:04:05" ,
316322 FullTimestamp : true ,
317323 },
318324 })
319325 if err != nil {
320- logsqlerr .WithError (err ).Error ("Can't init error sql log file" )
326+ cluster . SqlErrorLog .WithError (err ).Error ("Can't init error sql log file" )
321327 }
322- logsqlerr .AddHook (hookerr )
328+ cluster . SqlErrorLog .AddHook (hookerr )
323329
324330 hookgen , err := s18log .NewRotateFileHook (s18log.RotateFileConfig {
325331 Filename : cluster .WorkingDir + "/sql_general.log" ,
326332 MaxSize : cluster .Conf .LogRotateMaxSize ,
327333 MaxBackups : cluster .Conf .LogRotateMaxBackup ,
328334 MaxAge : cluster .Conf .LogRotateMaxAge ,
329- Level : logsqlerr .DebugLevel ,
330- Formatter : & logsqlgen .TextFormatter {
335+ Level : logsql .DebugLevel ,
336+ Formatter : & logsql .TextFormatter {
331337 DisableColors : true ,
332338 TimestampFormat : "2006-01-02 15:04:05" ,
333339 FullTimestamp : true ,
334340 },
335341 })
336342 if err != nil {
337- logsqlgen .WithError (err ).Error ("Can't init general sql log file" )
343+ cluster . SqlGeneralLog .WithError (err ).Error ("Can't init general sql log file" )
338344 }
339- logsqlgen .AddHook (hookgen )
345+ cluster . SqlGeneralLog .AddHook (hookgen )
340346 cluster .LoadAPIUsers ()
341347 // createKeys do nothing yet
342348 cluster .createKeys ()
@@ -359,18 +365,25 @@ func (cluster *Cluster) Init(conf config.Config, cfgGroup string, tlog *s18log.T
359365
360366 return nil
361367}
368+
362369func (cluster * Cluster ) initOrchetratorNodes () {
370+ if cluster .inInitNodes {
371+ return
372+ }
373+ cluster .inInitNodes = true
374+ defer func () { cluster .inInitNodes = false }()
363375
376+ //defer cluster.insideInitNodes = false
364377 //cluster.LogPrintf(LvlInfo, "Loading nodes from orchestrator %s", cluster.Conf.ProvOrchestrator)
365378 switch cluster .GetOrchestrator () {
366379 case config .ConstOrchestratorOpenSVC :
367- cluster .Agents , _ = cluster .OpenSVCGetNodes ()
380+ cluster .Agents , cluster . errorInitNodes = cluster .OpenSVCGetNodes ()
368381 case config .ConstOrchestratorKubernetes :
369- cluster .Agents , _ = cluster .K8SGetNodes ()
382+ cluster .Agents , cluster . errorInitNodes = cluster .K8SGetNodes ()
370383 case config .ConstOrchestratorSlapOS :
371- cluster .Agents , _ = cluster .SlapOSGetNodes ()
384+ cluster .Agents , cluster . errorInitNodes = cluster .SlapOSGetNodes ()
372385 case config .ConstOrchestratorLocalhost :
373- cluster .Agents , _ = cluster .LocalhostGetNodes ()
386+ cluster .Agents , cluster . errorInitNodes = cluster .LocalhostGetNodes ()
374387 case config .ConstOrchestratorOnPremise :
375388 default :
376389 log .Fatalln ("prov-orchestrator not supported" , cluster .Conf .ProvOrchestrator )
@@ -424,8 +437,8 @@ func (cluster *Cluster) Run() {
424437 for k , v := range cluster .Servers {
425438 cluster .LogPrintf (LvlDbg , "Server [%d]: URL: %-15s State: %6s PrevState: %6s" , k , v .URL , v .State , v .PrevState )
426439 }
427- if cluster .master != nil {
428- cluster .LogPrintf (LvlDbg , "Master [ ]: URL: %-15s State: %6s PrevState: %6s" , cluster .master .URL , cluster .master .State , cluster .master .PrevState )
440+ if cluster .GetMaster () != nil {
441+ cluster .LogPrintf (LvlDbg , "Master [ ]: URL: %-15s State: %6s PrevState: %6s" , cluster .master .URL , cluster .GetMaster () .State , cluster .GetMaster () .PrevState )
429442 for k , v := range cluster .slaves {
430443 cluster .LogPrintf (LvlDbg , "Slave [%d]: URL: %-15s State: %6s PrevState: %6s" , k , v .URL , v .State , v .PrevState )
431444 }
@@ -444,7 +457,7 @@ func (cluster *Cluster) Run() {
444457 if ! cluster .IsInFailover () {
445458 cluster .initProxies ()
446459 }
447- cluster .initOrchetratorNodes ()
460+ go cluster .initOrchetratorNodes ()
448461 cluster .ResticFetchRepo ()
449462 cluster .runOnceAfterTopology = false
450463 } else {
@@ -461,7 +474,7 @@ func (cluster *Cluster) Run() {
461474 cluster .InjectProxiesTraffic ()
462475 }
463476 if cluster .sme .GetHeartbeats ()% 30 == 0 {
464- cluster .initOrchetratorNodes ()
477+ go cluster .initOrchetratorNodes ()
465478 cluster .MonitorQueryRules ()
466479 cluster .MonitorVariablesDiff ()
467480 cluster .ResticFetchRepo ()
@@ -471,9 +484,12 @@ func (cluster *Cluster) Run() {
471484 cluster .sme .PreserveState ("WARN0093" )
472485 cluster .sme .PreserveState ("WARN0084" )
473486 cluster .sme .PreserveState ("WARN0095" )
474- cluster .sme .PreserveState ("ERR00082" )
475487 cluster .sme .PreserveState ("WARN0101" )
476488 }
489+ if ! cluster .CanInitNodes {
490+ cluster .SetState ("ERR00082" , state.State {ErrType : "WARNING" , ErrDesc : fmt .Sprintf (clusterError ["ERR00082" ], cluster .errorInitNodes ), ErrFrom : "OPENSVC" })
491+ }
492+
477493 if cluster .sme .GetHeartbeats ()% 36000 == 0 {
478494 cluster .ResticPurgeRepo ()
479495 } else {
@@ -561,8 +577,13 @@ func (cluster *Cluster) StateProcessing() {
561577 }
562578 // cluster.statecloseChan <- s
563579 }
580+ var states []string
581+ if cluster .runOnceAfterTopology {
582+ states = cluster .sme .GetFirstStates ()
564583
565- states := cluster .sme .GetStates ()
584+ } else {
585+ states = cluster .sme .GetStates ()
586+ }
566587 for i := range states {
567588 cluster .LogPrintf ("STATE" , states [i ])
568589 }
@@ -573,7 +594,9 @@ func (cluster *Cluster) StateProcessing() {
573594 }
574595
575596 for _ , s := range cluster .sme .GetLastOpenedStates () {
597+
576598 cluster .CheckAlert (s )
599+
577600 }
578601
579602 cluster .sme .ClearState ()
@@ -715,7 +738,7 @@ func (cluster *Cluster) FailoverForce() error {
715738
716739 }
717740 }
718- if cluster .master == nil {
741+ if cluster .GetMaster () == nil {
719742 cluster .LogPrintf (LvlErr , "Could not find a failed server in the hosts list" )
720743 return errors .New ("ERROR: Could not find a failed server in the hosts list" )
721744 }
@@ -861,21 +884,21 @@ func (cluster *Cluster) MonitorSchema() {
861884 if ! cluster .Conf .MonitorSchemaChange {
862885 return
863886 }
864- if cluster .master == nil {
887+ if cluster .GetMaster () == nil {
865888 return
866889 }
867- if cluster .master .State == stateFailed || cluster .master .State == stateMaintenance || cluster .master .State == stateUnconn {
890+ if cluster .GetMaster () .State == stateFailed || cluster .GetMaster () .State == stateMaintenance || cluster .GetMaster () .State == stateUnconn {
868891 return
869892 }
870- if cluster .master .Conn == nil {
893+ if cluster .GetMaster () .Conn == nil {
871894 return
872895 }
873896 cluster .sme .SetMonitorSchemaState ()
874- cluster .master .Conn .SetConnMaxLifetime (3595 * time .Second )
897+ cluster .GetMaster () .Conn .SetConnMaxLifetime (3595 * time .Second )
875898
876- tables , tablelist , logs , err := dbhelper .GetTables (cluster .master .Conn , cluster .master .DBVersion )
877- cluster .LogSQL (logs , err , cluster .master .URL , "Monitor" , LvlErr , "Could not fetch master tables %s" , err )
878- cluster .master .Tables = tablelist
899+ tables , tablelist , logs , err := dbhelper .GetTables (cluster .GetMaster () .Conn , cluster .GetMaster () .DBVersion )
900+ cluster .LogSQL (logs , err , cluster .GetMaster () .URL , "Monitor" , LvlErr , "Could not fetch master tables %s" , err )
901+ cluster .GetMaster () .Tables = tablelist
879902
880903 var tableCluster []string
881904 var duplicates []* ServerMonitor
@@ -889,7 +912,7 @@ func (cluster *Cluster) MonitorSchema() {
889912
890913 duplicates = append (duplicates , cluster .GetMaster ())
891914 tableCluster = append (tableCluster , cluster .GetName ())
892- oldtable , err := cluster .master .GetTableFromDict (t .TableSchema + "." + t .TableName )
915+ oldtable , err := cluster .GetMaster () .GetTableFromDict (t .TableSchema + "." + t .TableName )
893916 haschanged := false
894917 if err != nil {
895918 if err .Error () == "Empty" {
@@ -936,7 +959,7 @@ func (cluster *Cluster) MonitorSchema() {
936959 }
937960 cluster .DBIndexSize = totindexsize
938961 cluster .DBTableSize = tottablesize
939- cluster .master .DictTables = tables
962+ cluster .GetMaster () .DictTables = tables
940963 cluster .sme .RemoveMonitorSchemaState ()
941964}
942965
@@ -993,7 +1016,7 @@ func (cluster *Cluster) LostArbitration(realmasterurl string) {
9931016 }
9941017 if cluster .Conf .ArbitrationFailedMasterScript != "" {
9951018 cluster .LogPrintf (LvlInfo , "Calling abitration failed for master script" )
996- out , err := exec .Command (cluster .Conf .ArbitrationFailedMasterScript , cluster .master .Host , cluster .master .Port ).CombinedOutput ()
1019+ out , err := exec .Command (cluster .Conf .ArbitrationFailedMasterScript , cluster .GetMaster () .Host , cluster .GetMaster () .Port ).CombinedOutput ()
9971020 if err != nil {
9981021 cluster .LogPrintf (LvlErr , "%s" , err )
9991022 }
0 commit comments