replicaset: Index | Files

package replicaset

import ""

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


Package Files



const (
    StartupState = iota
const (
    // MaxPeers defines the maximum number of peers that mongo supports.
    MaxPeers = 7


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.

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

func Add Uses

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 Uses

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

BuildInfo returns the mongod build info for the given session.

func Initiate Uses

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 for more details.

func IsReady Uses

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 Uses

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 Uses

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 Uses

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 Uses

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 Uses

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.

type Config Uses

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 Uses

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 Uses

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 Uses

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 for more details

func CurrentMembers Uses

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

CurrentMembers returns the current members of the replica set.

type MemberState Uses

type MemberState int

MemberState represents the state of a replica set member. See

func (MemberState) String Uses

func (state MemberState) String() string

String returns a string describing the state.

type MemberStatus Uses

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 Uses

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


func CurrentStatus Uses

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

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

Package replicaset imports 11 packages (graph) and is imported by 327 packages. Updated 2019-07-23. Refresh now. Tools for package owners.