mongoelector

package
v0.0.0-...-137c36e Latest Latest
Warning

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

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

Documentation

Overview

Package mongoelector Leader election using mongodb.

Index

Constants

This section is empty.

Variables

View Source
var ManagedIndexes = []mongostore.Index{
	{
		CollectionName: collectionName,
		Id:             "ttlExpire_ttl",
		Version:        0,
		Model: mongo.IndexModel{
			Options: options.Index().SetExpireAfterSeconds(0),
			Keys: bsonx.Doc{
				{Key: "ttlExpire", Value: bsonx.Int32(mongostore.ASC)},
			},
		},
	},
}

Functions

This section is empty.

Types

type CandidateId

type CandidateId = mongouuid.UUID

type ElectedLeader

type ElectedLeader struct {
	Boundary       string      `bson:"_id" json:"boundary"`                  // boundary
	TTLExpire      time.Time   `bson:"ttlExpire" json:"ttlExpire"`           // the leader must reset this value before it expires and another node wins the election
	LeaderUUID     CandidateId `bson:"leaderUUID" json:"leaderUUID"`         // unique id generated by each Elector node at startup
	LeaderHostname string      `bson:"leaderHostname" json:"leaderHostname"` // hostname of the leader used by followers to connect to the leader for whatever service it provides
	LeaderPort     uint64      `bson:"leaderPort" json:"leaderPort"`         // port of the leader used by followers to connect to the leader for whatever service it provides
}

func (ElectedLeader) ConnectionString

func (el ElectedLeader) ConnectionString() string

type Elector

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

func NewElector

func NewElector(
	ctx context.Context,
	database *mongostore.DataStore,
	boundary string,
	leaderWorker LeaderWorker,
	followerWorker FollowerWorker,
	thisInstanceLeaderHostname string,
	thisInstanceLeaderPort uint64,
	options ElectorOptions,
) (e *Elector, err error)

NewElector Creates a new instance of Elector for the given boundary. boundary - a unique case-sensitive string (conventionally a path). Only one election can take place in a boundary at a time. database - a mongodb database. Must be shared by all Electors in a boundary. Should be shared by all Electors across all boundaries. leaderWorker - leaderWorker.Start() is called when this instance wins an election. leaderWorker.Close() is called when this instance loses an election followerWorker - followerWorker.Start() is called when this instance loses an election. followerWorker.Close() is called when this instance wins an election thisInstanceLeaderHostname - a hostname that will be passed to followers they can use to connect to a service on the leader, can be empty thisInstanceLeaderPort - a port that will be passed to followers they can use to connect to a service on the leader, can be empty

func (*Elector) Close

func (e *Elector) Close()

func (*Elector) GetElectedLeader

func (e *Elector) GetElectedLeader() *ElectedLeader

func (*Elector) Status

func (e *Elector) Status() (status *ElectorStatus)

type ElectorOptions

type ElectorOptions struct {
	LeaderTTLSeconds       uint64
	LeaderHeartbeatSeconds uint64
}

func NewElectorOptions

func NewElectorOptions() ElectorOptions

type ElectorStatus

type ElectorStatus struct {
	Id            CandidateId    `json:"id"`
	Boundary      string         `json:"boundary"`
	Hostname      string         `json:"hostname"`
	Port          uint64         `json:"port"`
	IsLeader      bool           `json:"isLeader"`
	ElectedLeader *ElectedLeader `json:"electedLeader"`
	Running       bool           `json:"running"`
}

type FollowerWorker

type FollowerWorker interface {
	// Start the worker. May be called multiple times in a row.
	Start(ctx context.Context, thisLeaderUUID CandidateId)

	// Stop the worker. May be called multiple times in a row.
	Stop()
}

type LeaderWorker

type LeaderWorker interface {
	// Start the worker and return. You are responsible for managing your own go routine. May be called multiple times in a row.
	Start(ctx context.Context)

	// Stop the worker and return. You are responsible for managing your own go routine. May be called multiple times in a row.
	Stop()
}

Jump to

Keyboard shortcuts

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