globalstate

package
v0.0.0-...-0810b70 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 15, 2017 License: GPL-3.0 Imports: 14 Imported by: 0

Documentation

Overview

Package globalstate is wrapper package for Hashicorps' implementation of the Raft consensus protocol. See https://github.com/hashicorp/raft.

For a description of how the Raft algorithm works see:

TL;DR:

Raft provide an algorithm for ensuring consensus in the cluster, which we in
this project use for keeping track of:
* Last registered floor for all lifts
* Whether an lift is at a standstill or moving somewhere
* What buttons are pressed in each floor.

Index

Constants

View Source
const (
	// BtnStateUnassigned is an button that have been pressed but no further action taken.
	BtnStateUnassigned = "unassigned"
	// BtnStateAssigned is a pressed button that the leader have dispatched an lifts to.
	BtnStateAssigned = "assigned"
	// BtnStateDone is a button that is ready to be pressed (ie. no LED lit)
	BtnStateDone = "done"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type ButtonStatusUpdate

type ButtonStatusUpdate struct {
	Floor      uint
	Dir        string
	Status     string
	AssignedTo string
}

ButtonStatusUpdate defines a message with which you intend to update the global store with.

type Config

type Config struct {
	// Port for raft RCP communication. The port above will also be binded, and is used for user-initiated communication over HTTP.
	RaftPort int

	// If supplied the raft will attempt to connect to any cluster the supplied peer is connected to.
	// If left blank the FSM instantiate a brand new raft and elect itself leader.
	InitalPeer string

	// OwnIP may be manually be set. If not supplied it will be inferred by the package if needed.
	OwnIP string

	// Number of floors on the lifts in the cluster. Only used for calculating timeouts.
	Floors int

	// Called once whenever the node win or loose the raft-leadership.
	OnPromotion func()
	OnDemotion  func()

	// Called once whenever the two leader elections in a row fail to elect a leader.
	OnAquiredConsensus func()

	// Called once whenever the consensus is regained and a leader is elected.
	OnLostConsensus func()

	// Called once whenever the leader have assigned an order to the node.
	OnIncomingCommand func(floor int, dir string)

	// Used by the leader to assign orders.
	CostFunction func(s State, floor int, dir string) string

	Logger *log.Logger

	// Raft may produce a considerable amount of logging, especially whenever a node
	// fail to respond. Logging from the globalstate package itself is still active.
	DisableRaftLogging bool
}

Config contain all configuration details and callbacks for the FSM.

type FSM

type FSM struct {
	// contains filtered or unexported fields
}

FSM hold all logic and essentially IS the globalstate handle. You may instantiate several FSM as long as they have different ports.

func (*FSM) GetState

func (f *FSM) GetState() (State, error)

GetState returns a copy of the current cluster state.

func (*FSM) Init

func (f *FSM) Init(config Config) error

Init sets up and start the FSM.

func (*FSM) Shutdown

func (f *FSM) Shutdown()

Shutdown shuts down the FSM in a safe manner.

func (*FSM) UpdateButtonStatus

func (f *FSM) UpdateButtonStatus(bs ButtonStatusUpdate) error

UpdateButtonStatus update the global store with the supplied button update. If unable to reach the raft-leader it will return an error.

func (*FSM) UpdateLiftStatus

func (f *FSM) UpdateLiftStatus(ls LiftStatusUpdate) error

UpdateLiftStatus updates the globalstate with the provided liftStatus.

type LiftStatus

type LiftStatus struct {
	ID                         string
	LastFloor                  uint
	Direction                  string
	DestinationFloor           uint
	DestinationButtonDirection string
	LastUpdate                 time.Time
}

LiftStatus defines the publicly available information about the elevators in the cluster.

func (*LiftStatus) DeepCopy

func (e *LiftStatus) DeepCopy() LiftStatus

DeepCopy safely return a copy of the lift.

type LiftStatusUpdate

type LiftStatusUpdate struct {
	CurrentFloor uint
	CurrentDir   string
	DstFloor     uint
	DstBtnDir    string
}

LiftStatusUpdate defines an message with which you intend to update the global store with.

type State

type State struct {
	// Number of floors for all lifts.
	Floors uint
	// Nodes is the IP:port of all nodes in the system
	Nodes map[string]LiftStatus
	// HallUpButtons, true of they are lit. Equivalent with an order there
	HallUpButtons   map[string]Status
	HallDownButtons map[string]Status
}

State defines the centralized state managed by the raft-cluster

func NewState

func NewState(floors uint) *State

NewState returns a new state

func (*State) DeepCopy

func (s *State) DeepCopy() State

DeepCopy safely return a copy of the state

type Status

type Status struct {
	AssignedTo string    // on the form "ip:port"
	LastStatus string    // "UNASSIGNED", "ASSIGNED", "DONE"
	LastChange time.Time // Automatically set by the sender when publishing a statusupdate.
}

Status defines the status of a button. All buttons of the same type on the same floor are considered equal, and as long as the lift is online will behave the exact same way. ie. will pressing the up-button at floor 3 on one lift yield the same result as pressing the same button on another lift.

func (*Status) DeepCopy

func (s *Status) DeepCopy() Status

DeepCopy safely return a copy of the Status

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL