Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var TableNameElection = "election"
TableNameElection is the table name of election.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct { // ID is id of the current member. // It is used to distinguish different members. // It must be unique among all members. ID string // Name is the human-readable name of the current member. // It is used for logging and diagnostics. Name string // Address is the address of the current member. Address string // Storage is the storage to store the election record. Storage Storage // LeaderCallback is the callback function when the current member becomes leader. // If current member loses the leadership, the ctx will be canceled. Callback should // return as soon as possible when the ctx is canceled. LeaderCallback func(ctx context.Context) error // LeaseDuration is the duration that a client will wait before // it can remove this member when the client hasn't observed any // renewals from the member. // // A client will wait a full LeaseDuration without observing any // renewals from the member before it can remove the member. // When all clients are shutdown and a new set of clients are started // with different id against the same storage, they must wait a full // LeaseDuration before they can acquire a leadership. So the LeaseDuration // should be set as short as possible to avoid the long waits in this case. // // It defaults to 10 seconds if not set. LeaseDuration time.Duration // RenewInterval is the interval that the current member tries to // refresh the election record and renew the lease. // // It defaults to 2 seconds if not set. RenewInterval time.Duration // RenewDeadline is the duration that the current member waits before // it gives up on the leadership when it can't renew the lease. To avoid // two members from both acting as leader at the same time, RenewDeadline // should be set shorter than LeaseDuration - RenewInterval. // // It defaults to 8 seconds if not set. RenewDeadline time.Duration // ExitOnRenewFail indicates whether the current member should exit when // failing to renew the lease. ExitOnRenewFail bool }
Config is the configuration for the leader election.
func (*Config) AdjustAndValidate ¶
AdjustAndValidate adjusts the config and validates it.
type DO ¶
type DO struct { ID uint32 `gorm:"column:id;type:int(10) unsigned;primaryKey" json:"id"` LeaderID string `gorm:"column:leader_id;type:text;not null" json:"leader_id"` Record *Record `gorm:"column:record;type:text" json:"record"` Version uint64 `gorm:"column:version;type:bigint(20) unsigned;not null" json:"version"` }
DO mapped from table <election>
type Elector ¶
type Elector interface { // RunElection runs the elector to continuously campaign for leadership // until the context is canceled. RunElection(ctx context.Context) error // IsLeader returns true if the current member is the leader // and its lease is still valid. IsLeader() bool // GetLeader returns the last observed leader whose lease is still valid. GetLeader() (*Member, bool) // GetMembers returns all members. GetMembers() []*Member // ResignLeader resigns the leadership and let the elector // not to try to campaign for leadership during the duration. ResignLeader(ctx context.Context, duration time.Duration) error }
Elector is a leader election client.
type Member ¶
type Member struct { // ID is the ID of the member. ID string `json:"id"` // Name is the human-readable name of the member. // It is used for logging and diagnostics. Name string `json:"name"` // Address is the address of the member. Address string `json:"address"` // LeaseDuration is the duration that a client will wait before // it can remove this member when the client hasn't observed // any renewals from the member. LeaseDuration time.Duration `json:"lease_duration"` // RenewTime is the last time when the member renewed its lease. RenewTime time.Time `json:"renew_time"` }
Member is a member in a leader election.
type ORMStorage ¶
type ORMStorage struct {
// contains filtered or unexported fields
}
ORMStorage is a storage implementation based on SQL database.
func NewORMStorage ¶
func NewORMStorage(db *gorm.DB, tableName string) (*ORMStorage, error)
NewORMStorage creates a new ORMStorage.
func NewORMStorageFromSQLDB ¶
func NewORMStorageFromSQLDB(backendDB *sql.DB, tableName string) (*ORMStorage, error)
NewORMStorageFromSQLDB creates a new ORMStorage from sql.DB.
func (*ORMStorage) Get ¶
func (s *ORMStorage) Get(ctx context.Context) (*Record, error)
Get implements Storage.Get.
func (*ORMStorage) TxnWithLeaderLock ¶
func (s *ORMStorage) TxnWithLeaderLock(ctx context.Context, leaderID string, fc func(tx *gorm.DB) error) error
TxnWithLeaderLock execute a transaction with leader row locked.
type Record ¶
type Record struct { // LeaderID is the ID of the leader. If it is empty, it means // there is no leader and all members may try to become leader. LeaderID string `json:"leader_id"` // Members is the members that are eligible to become leader, // which includes the leader itself. Members []*Member `json:"members"` // Version is the internal version of the record. It can be used by // specific storage implementation to determine whether the record has // been changed by another writer. The caller of the storage interface // should treat this value as opaque. // // Skip serializing this field, because it is not part of the record content. Version int64 `json:"-"` }
Record holds the information of a leader election.
func (*Record) FindMember ¶
FindMember finds the member with the given ID in the record.
type SQLStorage ¶
type SQLStorage struct {
// contains filtered or unexported fields
}
SQLStorage is a storage implementation based on SQL database.
func NewInMemorySQLStorage ¶
func NewInMemorySQLStorage(dbName string, tableName string) (*SQLStorage, error)
NewInMemorySQLStorage creates a new SQLStorage in memory based on SQLite.
func NewSQLStorage ¶
func NewSQLStorage(db *sql.DB, tableName string) (*SQLStorage, error)
NewSQLStorage creates a new SQLStorage.
type Storage ¶
type Storage interface { // Get gets the current record from the storage. It returns the current record stored // in the storage and any error encountered. Note that if the record has not created yet, // it should return an empty record and a nil error. Get(ctx context.Context) (*Record, error) // Update updates the record in the storage if the stored record version matches the given // record version. It returns ErrRecordConflict if the stored record version does not match // or any other error encountered. Update(ctx context.Context, record *Record, isLeaderChanged bool) error }
Storage describes the requirements of a storage that can be used for leader election.