mongoelector

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 9, 2024 License: MIT Imports: 18 Imported by: 0

Documentation

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: bson.D{
				{Key: "ttlExpire", Value: mongostore.ASC},
			},
		},
	},
}
View Source
var ZeroCandidateId = CandidateId(primitive.NilObjectID)

Functions

func CandidateIdCompare

func CandidateIdCompare(a CandidateId, b CandidateId) int

CandidateIdCompare returns an integer comparing two CandidateIds. The result will be 0 if a == b, -1 if a < b, and +1 if a > b.

Types

type CandidateId

type CandidateId primitive.ObjectID

func CandidateIdPointerSliceToSlice

func CandidateIdPointerSliceToSlice(inputSlice []*CandidateId) (outputSlice []CandidateId)

func MakeCandidateId

func MakeCandidateId() CandidateId

func OrganizationidSliceToPointerSlice

func OrganizationidSliceToPointerSlice(inputSlice []CandidateId) (outputSlice []*CandidateId)

func (CandidateId) Clone

func (i CandidateId) Clone() (clone CandidateId)

func (CandidateId) Compare

func (i CandidateId) Compare(x CandidateId) int

Compare returns an integer comparing two CandidateIds. The result will be 0 if this == x, -1 if this < x, and +1 if this > x.

func (CandidateId) Equal

func (i CandidateId) Equal(x CandidateId) bool

func (CandidateId) MarshalBSONValue

func (i CandidateId) MarshalBSONValue() (bsontype.Type, []byte, error)

func (CandidateId) MarshalJSON

func (i CandidateId) MarshalJSON() ([]byte, error)

func (*CandidateId) MarshalText

func (i *CandidateId) MarshalText() ([]byte, error)

func (CandidateId) String

func (i CandidateId) String() string

func (*CandidateId) UnmarshalBSONValue

func (i *CandidateId) UnmarshalBSONValue(t bsontype.Type, raw []byte) error

func (*CandidateId) UnmarshalJSON

func (i *CandidateId) UnmarshalJSON(data []byte) (err error)

func (*CandidateId) UnmarshalText

func (i *CandidateId) UnmarshalText(text []byte) error

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