replicaset

package module
v0.0.0-...-0303c85 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2021 License: LGPL-3.0 Imports: 11 Imported by: 66

README

juju/replicaset

This package provides convenience functions and structures for creating and managing MongoDB replica sets via the mgo driver.

Documentation

Overview

Package replicaset provides convenience functions and structures for creating and managing MongoDB replica sets via the mgo driver.

Index

Constants

View Source
const (
	StartupState = iota
	PrimaryState
	SecondaryState
	RecoveringState
	FatalState
	Startup2State
	UnknownState
	ArbiterState
	DownState
	RollbackState
	ShunnedState
)
View Source
const (
	// MaxPeers defines the maximum number of peers that mongo supports.
	MaxPeers = 7
)

Variables

View Source
var CurrentConfig = currentConfig

CurrentConfig returns the Config for the given session's replica set. If there is no current config, the error returned will be mgo.ErrNotFound.

View Source
var ErrMasterNotConfigured = fmt.Errorf("mongo master not configured")

Functions

func Add

func Add(session *mgo.Session, members ...Member) error

Add adds the given members to the session's replica set. Duplicates of existing replicas will be ignored.

Members will have their Ids set automatically if they are not already > 0

func BuildInfo

func BuildInfo(session *mgo.Session) (mgo.BuildInfo, error)

BuildInfo returns the mongod build info for the given session.

func Initiate

func Initiate(session *mgo.Session, address, name string, tags map[string]string) error

Initiate sets up a replica set with the given replica set name with the single given member. It need be called only once for a given mongo replica set. The tags specified will be added as tags on the member that is created in the replica set.

Note that you must set DialWithInfo and set Direct = true when dialing into a specific non-initiated mongo server.

See http://docs.mongodb.org/manual/reference/method/rs.initiate/ for more details.

func IsReady

func IsReady(session *mgo.Session) (bool, error)

IsReady checks on the status of all members in the replicaset associated with the provided session. If we can connect and the majority of members are ready then the result is true.

func MasterHostPort

func MasterHostPort(session *mgo.Session) (string, error)

MasterHostPort returns the "address:port" string for the primary mongo server in the replicaset. It returns ErrMasterNotConfigured if the replica set has not yet been initiated.

func Remove

func Remove(session *mgo.Session, addrs ...string) error

Remove removes members with the given addresses from the replica set. It is not an error to remove addresses of non-existent replica set members.

func Set

func Set(session *mgo.Session, members []Member) error

Set changes the current set of replica set members. Members will have their ids set automatically if their ids are not already > 0.

func StepDownPrimary

func StepDownPrimary(session *mgo.Session) error

StepDownPrimary asks the current mongo primary to step down. Note that triggering a step down causes all client connections to be disconnected. We explicitly treat the io.EOF we get as not being an error, but all other sessions will also be disconnected.

func WaitUntilReady

func WaitUntilReady(session *mgo.Session, timeout int) error

WaitUntilReady waits until all members of the replicaset are ready. It will retry every 10 seconds until the timeout is reached. Dropped connections will trigger a reconnect.

Types

type Config

type Config struct {
	Name            string   `bson:"_id"`
	Version         int      `bson:"version"`
	ProtocolVersion int      `bson:"protocolVersion,omitempty"`
	Members         []Member `bson:"members"`
}

Config is the document stored in mongodb that defines the servers in the replica set

type IsMasterResults

type IsMasterResults struct {
	// The following fields hold information about the specific mongodb node.
	IsMaster  bool      `bson:"ismaster"`
	Secondary bool      `bson:"secondary"`
	Arbiter   bool      `bson:"arbiterOnly"`
	Address   string    `bson:"me"`
	LocalTime time.Time `bson:"localTime"`

	// The following fields hold information about the replica set.
	ReplicaSetName string   `bson:"setName"`
	Addresses      []string `bson:"hosts"`
	Arbiters       []string `bson:"arbiters"`
	PrimaryAddress string   `bson:"primary"`
}

Config reports information about the configuration of a given mongo node

func IsMaster

func IsMaster(session *mgo.Session) (*IsMasterResults, error)

IsMaster returns information about the configuration of the node that the given session is connected to.

type Member

type Member struct {
	// Id is a unique id for a member in a set.
	Id int `bson:"_id"`

	// Address holds the network address of the member,
	// in the form hostname:port.
	Address string `bson:"host"`

	// Arbiter holds whether the member is an arbiter only.
	// This value is optional; it defaults to false.
	Arbiter *bool `bson:"arbiterOnly,omitempty"`

	// BuildIndexes determines whether the mongod builds indexes on this member.
	// This value is optional; it defaults to true.
	BuildIndexes *bool `bson:"buildIndexes,omitempty"`

	// Hidden determines whether the replica set hides this member from
	// the output of IsMaster.
	// This value is optional; it defaults to false.
	Hidden *bool `bson:"hidden,omitempty"`

	// Priority determines eligibility of a member to become primary.
	// This value is optional; it defaults to 1.
	Priority *float64 `bson:"priority,omitempty"`

	// Tags store additional information about a replica member, often used for
	// customizing read preferences and write concern.
	Tags map[string]string `bson:"tags,omitempty"`

	// SlaveDelay describes the number of seconds behind the master that this
	// replica set member should lag rounded up to the nearest second.
	// This value is optional; it defaults to 0.
	SlaveDelay *time.Duration `bson:"slaveDelay,omitempty"`

	// Votes controls the number of votes a server has in a replica set election.
	// This value is optional; it defaults to 1.
	Votes *int `bson:"votes,omitempty"`
}

Member holds configuration information for a replica set member.

See http://docs.mongodb.org/manual/reference/replica-configuration/ for more details

func CurrentMembers

func CurrentMembers(session *mgo.Session) ([]Member, error)

CurrentMembers returns the current members of the replica set.

type MemberState

type MemberState int

MemberState represents the state of a replica set member. See http://docs.mongodb.org/manual/reference/replica-states/

func (MemberState) String

func (state MemberState) String() string

String returns a string describing the state.

type MemberStatus

type MemberStatus struct {
	// Id holds the replica set id of the member that the status is describing.
	Id int `bson:"_id"`

	// Address holds address of the member that the status is describing.
	Address string `bson:"name"`

	// Self holds whether this is the status for the member that
	// the session is connected to.
	Self bool `bson:"self"`

	// ErrMsg holds the most recent error or status message received
	// from the member.
	ErrMsg string `bson:"errmsg"`

	// Healthy reports whether the member is up. It is true for the
	// member that the request was made to.
	Healthy bool `bson:"health"`

	// State describes the current state of the member.
	State MemberState `bson:"state"`

	// Uptime describes how long the member has been online.
	Uptime time.Duration `bson:"uptime"`

	// Ping describes the length of time a round-trip packet takes to travel
	// between the remote member and the local instance.  It is zero for the
	// member that the session is connected to.
	Ping time.Duration `bson:"pingMS"`
}

Status holds the status of a replica set member returned from replSetGetStatus.

type Status

type Status struct {
	Name    string         `bson:"set"`
	Members []MemberStatus `bson:"members"`
}

Status holds data about the status of members of the replica set returned from replSetGetStatus

See http://docs.mongodb.org/manual/reference/command/replSetGetStatus/#dbcmd.replSetGetStatus

func CurrentStatus

func CurrentStatus(session *mgo.Session) (*Status, error)

CurrentStatus returns the status of the replica set for the given session.

Jump to

Keyboard shortcuts

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