node

package
v0.0.0-...-8459ba7 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2022 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var SafetyDelta = uint64(100 * time.Millisecond)

SafetyDelta is the period of time that a heart beat should be late after the remthreshold for the cleaner to remove it.

View Source
var Timeout = 5 * time.Second

The timeout to execute Raft commands

Functions

func NewInMemStore

func NewInMemStore() *inMemStore

Types

type CleanableResource

type CleanableResource interface {
	GetResources() map[string]*ServiceEntry
}

CleanableResource represents a resource (services) accessible by the cleanup service.

type Cleaner

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

func NewCleaner

func NewCleaner(period, remThreshold time.Duration, node *Node) *Cleaner

func (*Cleaner) Start

func (c *Cleaner) Start()

type Command

type Command struct {
	Type string `json:"type"`
	Key  string `json:"key"`
	// some commands don't have a value such as `DELETE` and `GET`
	Value string `json:"value,omitempty"`
}

Command is what we will use to change the state of the Replicate state machine. The type field defines how the message is going to be interpreted by the system.

type DelRequest

type DelRequest struct {
	Name     string
	Instance InstanceEntry
}

type HttpServer

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

HttpServer is the component that will interact with the outside world through a REST API to perform different operations:

  1. Perform `Join` requests from other nodes that will later join the cluster (if this is a leader)
  2. Reply to client requests to investigate the state of the key-value store.

func NewServer

func NewServer(addr string, node *Node) *HttpServer

NewServer will create a new `HttpServer` that will listen on `addr` later on after `Start` is Called.

func (*HttpServer) ServeHTTP

func (s *HttpServer) ServeHTTP(res http.ResponseWriter, req *http.Request)

ServeHTTP is an implementation of the `http.Handler` interface to process incoming client requets.

func (*HttpServer) Shutdown

func (s *HttpServer) Shutdown()

Shutdown will be used for graceful shutdown.

func (*HttpServer) Start

func (s *HttpServer) Start() error

Start will start listening for incoming client requests. The HttpServer will create and run in it's own goroutine.

type Instance

type Instance struct {
	Port   uint16 `json:"port"`
	Host   string `json:"host"`
	Uptime uint64 `json:"uptime"`
}

type InstanceEntry

type InstanceEntry struct {
	Port       uint16
	Host       string
	LastBeatMs uint64
	Created    time.Time
}

type InstanceRegistration

type InstanceRegistration struct {
	ServiceName string `json:"service"`
	Host        string `json:"host"`
	Port        uint16 `json:"port"`
}

type JoinRequest

type JoinRequest struct {
	Id   string `json:"id"`
	Addr string `json:"addr"`
}

JoinRequest is the message received by the API to handle new nodes joining the cluster.

type Node

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

func NewNode

func NewNode(id, dataDir, raftAddr string, store StorageEngine) *Node

func (*Node) AddPeer

func (n *Node) AddPeer(id, addr string) error

func (*Node) Bootstrap

func (n *Node) Bootstrap(isLeader bool) error

Bootstrap creates the underlying Raft node and configures it according to the supplied parameters / node state.

If the `isLeader` param is set, it will initiate a cluster with size `1` with the current node as the leader.

type Service

type Service struct {
	Name      string     `json:"name"`
	Instances []Instance `json:"instances"`
}

type ServiceEntry

type ServiceEntry struct {
	Name      string
	Instances []*InstanceEntry
}

type ServicesResponse

type ServicesResponse struct {
	Services []Service
}

ServicesResponse is the message returned by the leader when the `/services` endpoint is queried.

type StorageEngine

type StorageEngine interface {
	// Inherit the behaviour of a Raft state machine.
	raft.FSM

	// Inherit the CleanableReource
	CleanableResource

	// Put the key-value pair into the underlying storage, and return an error if it's not possible to
	// finish the operation.
	Put(string, string) error

	// Get the value identified by the given key.
	Get(string) (string, error)

	// Delete the value identifier by the given key from the underlying storage,
	// and return it.
	Delete(string) (string, error)

	// GetServices will return the list of known live services to the heartbeat service
	// at query time.
	GetServices() *ServicesResponse

	// RegisterInstance will update the store's service list with the new
	// instance, This is usually resulting from a new service starting somewhere,
	// and doing a heartbeat request.
	RegisterInstance(InstanceRegistration)

	// DeleteInstance will delete the corresponding entry (instance) from the replicated
	// state machine
	DeleteInstance(string, InstanceEntry)
}

A storage engine abstraction over the key-value store.

The engine can be thought of as a Finite state machine that can be fed into the Raft cluster.

Jump to

Keyboard shortcuts

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