changeset 1:50b3f6da682f

Expand types by a bit
author Lewin Bormann <lbo@spheniscida.de>
date Fri, 07 Oct 2016 20:44:27 +0200
parents 073442e4dfe6
children 5915ba02104e
files types.go
diffstat 1 files changed, 56 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/types.go	Fri Oct 07 20:06:32 2016 +0200
+++ b/types.go	Fri Oct 07 20:44:27 2016 +0200
@@ -15,40 +15,79 @@
 	Install([]byte)
 }
 
-// A channel that is connected to a peer and can roundtrip messages.
-type Transport interface {
-	Send([]byte) error
-	Recv() ([]byte, error)
-}
-
 // A remote member of the consensus
 type Member struct {
 	addr string
 }
 
+type InstanceNumber uint64
+type SequenceNumber uint64
+
+const (
+	// Normal operation
+	STATE_MASTER              int = iota
+	STATE_PARTICIPANT_CLEAN       // from STATE_PARTICIPANT_PENDING; waiting for master requests
+	STATE_PARTICIPANT_PENDING     // from STATE_PARTICIPANT_CLEAN; pending changes
+	// During election
+	STATE_CANDIDATE      // from STATE_PARTICIPANT_* or STATE_MASTER
+	STATE_PENDING_MASTER // from STATE_PARTICIPANT_*; we have a staged master
+)
+
 // One participant of the consensus
 // implements participating; implementation-provided
 type Participant struct {
 	members []Member
-	master  Member
+	master  map[InstanceNumber]Member // If a past Instance is attempted to be Prepare()d, then we can answer with the master of that Instance
 	self    Member
 
-	contacts map[Member]Transport
+	participants map[Member]ConsensusClient
+
+	instance InstanceNumber // nth round
+	serial   SequenceNumber // nth submission in this round
 
-	instance uint64 // nth round
-	serial   uint64 // nth submission in this round
+	state            State
+	participantState int // See STATE_... constants
 
-	state   State
-	staging map[uint64]Change // staging area for changes
+	stagedChanges map[SequenceNumber]Change // staging area for changes (i.e. temporary log)
+	stagedMembers map[SequenceNumber]Change
 
 	// Used to deserialize changes
 	protoTypeDeserialize Change
 }
 
 // Implemented by Participant
-// Used by Server for asynchronous, external callbacks
-type Participating interface {
-	Prepare(instance uint64, m Member) bool
-	Accept(instance, serial uint64, changes []Change)
-	StartParticipation(instance, serial uint64, master Member, members []Member, snapshot []byte)
+// Used by Server for external requests calling into the participant, as well
+// as making requests to remote participants.
+type ParticipantStub interface {
+	// Master -> participants
+	Prepare(InstanceNumber, Member) (bool, error)
+
+	// Master -> participants
+	Accept(InstanceNumber, SequenceNumber, []Change) error
+	// Master -> participants
+	// Accept() with a different argument
+	AddMember(InstanceNumber, SequenceNumber, Member) error
+	// Master -> participants
+	// Accept() with a different argument
+	RemoveMember(InstanceNumber, SequenceNumber, Member) error
+
+	// Master -> new participant
+	StartParticipation(i InstanceNumber, s SequenceNumber, master Member, members []Member, snapshot []byte)
+
+	// Participant -> master (so that non-masters can submit changes)
+	Submit([]Change) error
 }
+
+type ConsensusClient interface {
+	ParticipantStub
+}
+
+type ConsensusServer interface {
+	ParticipantStub
+}
+
+// A component that receives external requests and calls a Participating component. Typically
+// some RPC or HTTP server component that dispatches incoming requests to a participant.
+type Server interface {
+	Register(string, ConsensusServer) error
+}