mlc

package module
v0.0.0-...-d69fcf6 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2022 License: MPL-2.0 Imports: 19 Imported by: 0

README

MLC - Memberlist Cluster

Simple Clustering for your Application

Current Features:
  • Provides simple Leader / Follower functionality
  • Leader Elections
  • Automated Tasks based on Execution stages

mlc is currently under development. Thus, ongoing optimization and re-factoring may occur.

Documentation

Index

Constants

View Source
const (
	ProtocolVersion = 4
)

Variables

View Source
var MetaMaxSize = memberlist.MetaMaxSize

Functions

This section is empty.

Types

type AppMeta

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

func CreateAppMeta

func CreateAppMeta(nodeName string) *AppMeta

func (*AppMeta) Get

func (M *AppMeta) Get() []byte

Get returns the LocalNode AppData.

func (*AppMeta) GetAll

func (M *AppMeta) GetAll() map[string][]byte

GetAll return a mapping copy of all AppData by node name, for each node in the cluster.

func (*AppMeta) Set

func (M *AppMeta) Set(appdata []byte)

Set replaces the LocalNode AppData.

type Cluster

type Cluster struct {
	*memberlist.Memberlist

	Tasks []Task
	// contains filtered or unexported fields
}

func NewCluster

func NewCluster(N *Node, appData []byte) *Cluster

func (*Cluster) AmLeader

func (C *Cluster) AmLeader() bool

func (*Cluster) ClusterData

func (C *Cluster) ClusterData() map[string][]byte

ClusterData return a mapping snapshot of all AppData, for each node in the cluster. The AppData returned is only a Copy.

func (*Cluster) ClusterStatusHandler

func (C *Cluster) ClusterStatusHandler(w http.ResponseWriter, r *http.Request)

func (*Cluster) Connect

func (C *Cluster) Connect(peers ...string) error

func (*Cluster) Disconnect

func (C *Cluster) Disconnect() error

func (*Cluster) GetData

func (C *Cluster) GetData() []byte

Get returns a copy of the LocalNode AppData.

func (*Cluster) Leader

func (C *Cluster) Leader() (name, address string)

func (*Cluster) Notifications

func (C *Cluster) Notifications() <-chan []byte

func (*Cluster) Notify

func (C *Cluster) Notify(member *memberlist.Node, msg []byte) error

func (*Cluster) NotifyAll

func (C *Cluster) NotifyAll(msg []byte) error

func (*Cluster) OtherMembers

func (C *Cluster) OtherMembers() map[*memberlist.Node][]byte

OtherMembers returns a mapping of all other known live nodes along with that node's AppData.

func (*Cluster) RegisterTasks

func (C *Cluster) RegisterTasks(tasks ...Task)

RegisterTasks registers the given Tasks to the Cluster.

func (*Cluster) RunOnDemand

func (C *Cluster) RunOnDemand()

RunOnDemand runs all registered Tasks with the OnDemand ExecutionTask parameter.

func (*Cluster) RunTask

func (C *Cluster) RunTask(onDemand Task) error

RunTask runs the given Task with with the OnDemand ExecutionTask parameter.

func (*Cluster) SetData

func (C *Cluster) SetData(appdata []byte)

SetData replaces the LocalNode AppData.

func (*Cluster) WithDirLock

func (C *Cluster) WithDirLock(dirPath string)

func (*Cluster) WithNotificationChannel

func (C *Cluster) WithNotificationChannel(notificationChannel chan []byte)

type ClusterFunc

type ClusterFunc interface {
	Leader() (name, address string)
	AmLeader() bool
	OtherMembers() map[*memberlist.Node][]byte
	Notifications() <-chan []byte
	Notify(*memberlist.Node, []byte) error
	NotifyAll([]byte) error
	SetData([]byte)
	GetData() []byte
	ClusterData() map[string][]byte
}

ClusterFuncs are functions available for Tasks.

type DefaultEncoderDecoder

type DefaultEncoderDecoder struct{}

func (*DefaultEncoderDecoder) Decode

func (d *DefaultEncoderDecoder) Decode(data []byte) NodeMeta

func (*DefaultEncoderDecoder) Encode

func (d *DefaultEncoderDecoder) Encode(m *NodeMeta) []byte

type Delegate

type Delegate interface {
	memberlist.Delegate
	EncoderDecoder
	WithNotificationChannel(chan []byte)
}

type EncoderDecoder

type EncoderDecoder interface {
	Encode(*NodeMeta) []byte
	Decode([]byte) NodeMeta
}

type ExecutionStage

type ExecutionStage int
const (
	Bootstrap             ExecutionStage = iota // On initial 1 node cluster start
	OnDemand                                    // OnDemand Tasks, envoked via RunOnDemand or RunTask
	LeaderOnJoin                                // Newly elected Leader Tasks during an election
	LeaderOnLeave                               // Current Leader Tasks when leaving the cluster
	FollowerOnPromotion                         // During an election, Follower Tasks when promoted to Leader before running LeaderOnJoin Tasks
	FollowerOnLeaderLeave                       // During an election, Follower Tasks when not promoted to Leader befere running FollowerOnNewLeader Tasks
	FollowerOnNewLeader                         // During an election, Follower Tasks when a new Leader has been elected
	FollowerOnJoin                              // Follower Tasks when joining the cluster
	FollowerOnLeave                             // Follower Tasks when leaving the cluster
)

type Node

type Node struct {
	Config      *memberlist.Config
	DirLockPath string
	L           *log.Logger
}

func CreateNode

func CreateNode(config *NodeConfig) *Node

type NodeConfig

type NodeConfig struct {
	Name          string
	BindAddr      string
	AdvertiseAddr string
	DirLockPath   string
	BindPort      int
	AdvertisePort int
	Peers         []string
	Logger        *log.Logger
}

func (*NodeConfig) CreateNode

func (C *NodeConfig) CreateNode() *Node

func (*NodeConfig) SetDefaults

func (C *NodeConfig) SetDefaults()

type NodeMeta

type NodeMeta struct {
	Name       string
	Address    string
	Leader     string
	LeaderAddr string
	Data       []byte
}

type StatusResponse

type StatusResponse struct {
	Name          string               `json:"name"`
	Address       string               `json:"address"`
	Leader        string               `json:"leader"`
	LeaderAddress string               `json:"leaderAddress"`
	Members       []memberlist.Address `json:"members"`
}

type Task

type Task func(ExecutionStage, ClusterFunc) error

Jump to

Keyboard shortcuts

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