changeset 29:2eac51eadf92

Implement IsMaster()
author Lewin Bormann <lbo@spheniscida.de>
date Fri, 14 Oct 2016 20:19:35 +0200
parents 78f114bfcc9e
children 218d4830661a
files consensus.go example_http/example.go
diffstat 2 files changed, 12 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/consensus.go	Sun Oct 09 12:04:34 2016 +0200
+++ b/consensus.go	Fri Oct 14 20:19:35 2016 +0200
@@ -38,10 +38,20 @@
 // This means that the following (exported) functions are supposed to be called from the application that
 // uses the clusterconsensus package.
 
+// Return the current state. Note: This is only the strongly consistent current state if IsMaster() is true.
 func (p *Participant) GetState() State {
 	return p.state
 }
 
+// Returns whether this participant is the current master.
+func (p *Participant) IsMaster() bool {
+	if master, ok := p.master[p.instance]; ok {
+		return p.self == master
+	} else {
+		return false
+	}
+}
+
 // Submit one change to the state machine
 func (p *Participant) SubmitOne(c Change) error {
 	return p.Submit([]Change{c})
@@ -219,6 +229,7 @@
 	}
 
 	p.instance++
+	p.master[p.instance] = p.self
 	p.sequence = 0
 	p.participantState = state_MASTER
 	return nil
--- a/example_http/example.go	Sun Oct 09 12:04:34 2016 +0200
+++ b/example_http/example.go	Fri Oct 14 20:19:35 2016 +0200
@@ -115,7 +115,7 @@
 		}
 
 		if i%5 == 0 {
-			log.Println(len(participant.GetState().(State).inner), participant.GetState().(State))
+			log.Println("master:", participant.IsMaster(), len(participant.GetState().(State).inner), participant.GetState().(State))
 		}
 
 		i++