mongo

package
v1.13.0 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2022 License: Apache-2.0 Imports: 13 Imported by: 1

Documentation

Index

Constants

View Source
const (
	MinVotingMembers    = 1
	MaxVotingMembers    = 7
	MaxMembers          = 50
	DefaultPriority     = 2
	DefaultVotes        = 1
	DefaultReadConcern  = "majority"
	DefaultWriteConcern = "majority"
)
View Source
const (
	MemberHealthDown MemberHealth = iota
	MemberHealthUp
	MemberStateStartup    MemberState = 0
	MemberStatePrimary    MemberState = 1
	MemberStateSecondary  MemberState = 2
	MemberStateRecovering MemberState = 3
	MemberStateStartup2   MemberState = 5
	MemberStateUnknown    MemberState = 6
	MemberStateArbiter    MemberState = 7
	MemberStateDown       MemberState = 8
	MemberStateRollback   MemberState = 9
	MemberStateRemoved    MemberState = 10
)
View Source
const ShardRemoveCompleted string = "completed"

Variables

View Source
var MemberStateStrings = map[MemberState]string{
	MemberStateStartup:    "STARTUP",
	MemberStatePrimary:    "PRIMARY",
	MemberStateSecondary:  "SECONDARY",
	MemberStateRecovering: "RECOVERING",
	MemberStateStartup2:   "STARTUP2",
	MemberStateUnknown:    "UNKNOWN",
	MemberStateArbiter:    "ARBITER",
	MemberStateDown:       "DOWN",
	MemberStateRollback:   "ROLLBACK",
	MemberStateRemoved:    "REMOVED",
}

Functions

func AddShard added in v1.8.0

func AddShard(ctx context.Context, client *mongo.Client, rsName, host string) error

func CreateRole added in v1.8.0

func CreateRole(ctx context.Context, client *mongo.Client, role string, privileges []RolePrivilege, roles []interface{}) error

func CreateUser added in v1.8.0

func CreateUser(ctx context.Context, client *mongo.Client, user, pwd string, roles ...map[string]interface{}) error

func Dial

func Dial(conf *Config) (*mongo.Client, error)

func GetFCV added in v1.8.0

func GetFCV(ctx context.Context, client *mongo.Client) (string, error)

func IsBalancerRunning added in v1.6.0

func IsBalancerRunning(ctx context.Context, client *mongo.Client) (bool, error)

func SetDefaultRWConcern added in v1.13.0

func SetDefaultRWConcern(ctx context.Context, client *mongo.Client, readConcern, writeConcern string) error

func SetFCV added in v1.8.0

func SetFCV(ctx context.Context, client *mongo.Client, version string) error

func StartBalancer added in v1.6.0

func StartBalancer(ctx context.Context, client *mongo.Client) error

func StepDown added in v1.5.0

func StepDown(ctx context.Context, client *mongo.Client, force bool) error

func StopBalancer added in v1.6.0

func StopBalancer(ctx context.Context, client *mongo.Client) error

func UpdateRole added in v1.12.0

func UpdateRole(ctx context.Context, client *mongo.Client, role string, privileges []RolePrivilege, roles []interface{}) error

func UpdateUser added in v1.5.0

func UpdateUser(ctx context.Context, client *mongo.Client, currName, newName, pass string) error

UpdateUser recreates user with new name and password should be used only when username was changed

func UpdateUserPass added in v1.5.0

func UpdateUserPass(ctx context.Context, client *mongo.Client, name, pass string) error

UpdateUserPass updates user's password

func UpdateUserRoles added in v1.12.0

func UpdateUserRoles(ctx context.Context, client *mongo.Client, username string, roles []map[string]interface{}) error

func WriteConfig

func WriteConfig(ctx context.Context, client *mongo.Client, cfg RSConfig) error

Types

type BalancerStatus added in v1.6.0

type BalancerStatus struct {
	Mode       string `json:"mode"`
	OKResponse `bson:",inline"`
}

type BuildInfo added in v1.5.0

type BuildInfo struct {
	Version    string `json:"version" bson:"version"`
	OKResponse `bson:",inline"`
}

BuildInfo contains information about mongod build params

func RSBuildInfo added in v1.5.0

func RSBuildInfo(ctx context.Context, client *mongo.Client) (BuildInfo, error)

type Config added in v1.6.0

type Config struct {
	Hosts       []string
	ReplSetName string
	Username    string
	Password    string
	TLSConf     *tls.Config
	Direct      bool
}

type ConfigMember added in v1.5.0

type ConfigMember struct {
	ID                 int         `bson:"_id" json:"_id"`
	Host               string      `bson:"host" json:"host"`
	ArbiterOnly        bool        `bson:"arbiterOnly" json:"arbiterOnly"`
	BuildIndexes       bool        `bson:"buildIndexes" json:"buildIndexes"`
	Hidden             bool        `bson:"hidden" json:"hidden"`
	Priority           int         `bson:"priority" json:"priority"`
	Tags               ReplsetTags `bson:"tags,omitempty" json:"tags,omitempty"`
	SlaveDelay         *int64      `bson:"slaveDelay,omitempty" json:"slaveDelay,omitempty"`
	SecondaryDelaySecs *int64      `bson:"secondaryDelaySecs,omitempty" json:"secondaryDelaySecs,omitempty"`
	Votes              int         `bson:"votes" json:"votes"`
}

RSMember document from 'replSetGetConfig': https://docs.mongodb.com/manual/reference/command/replSetGetConfig/#dbcmd.replSetGetConfig

func (ConfigMember) String added in v1.5.0

func (m ConfigMember) String() string

type ConfigMembers added in v1.5.0

type ConfigMembers []ConfigMember

func (*ConfigMembers) AddNew added in v1.5.0

func (m *ConfigMembers) AddNew(from ConfigMembers) bool

AddNew adds a new member from given list to the config. It adds only one at a time. Returns true if it adds any member.

func (*ConfigMembers) ExternalNodesChanged added in v1.10.0

func (m *ConfigMembers) ExternalNodesChanged(compareWith ConfigMembers) bool

ExternalNodesChanged checks if votes or priority fields changed for external nodes

func (*ConfigMembers) FixHosts added in v1.12.0

func (m *ConfigMembers) FixHosts(compareWith ConfigMembers) (changes bool)

func (*ConfigMembers) FixTags added in v1.10.0

func (m *ConfigMembers) FixTags(compareWith ConfigMembers) (changes bool)

FixTags corrects the tags of any member if they changed. Especially the "external" tag can change if cluster is switched from unmanaged to managed.

func (*ConfigMembers) RemoveOld added in v1.5.0

func (m *ConfigMembers) RemoveOld(compareWith ConfigMembers) bool

RemoveOld removes from the list those members which are not present in the given list. It always should leave at least one element. The config won't be valid for mongo otherwise. Better, if the last element has the smallest ID in order not to produce defragmentation when the next element will be added (ID = maxID + 1). Mongo replica set member ID must be between 0 and 255, so it matters.

func (*ConfigMembers) SetVotes added in v1.5.0

func (m *ConfigMembers) SetVotes(unsafePSA bool)

SetVotes sets voting parameters for members list

type DBList added in v1.7.0

type DBList struct {
	DBs []struct {
		Name string `bson:"name" json:"name"`
	} `bson:"databases" json:"databases"`
	OKResponse `bson:",inline"`
}

func ListDBs added in v1.7.0

func ListDBs(ctx context.Context, client *mongo.Client) (DBList, error)

type FCV added in v1.8.0

type FCV struct {
	FCV struct {
		Version string `json:"version" bson:"version"`
	} `json:"featureCompatibilityVersion" bson:"featureCompatibilityVersion"`
	OKResponse `bson:",inline"`
}

type IsMasterResp added in v1.10.0

type IsMasterResp struct {
	IsMaster   bool   `bson:"ismaster" json:"ismaster"`
	IsArbiter  bool   `bson:"arbiterOnly" json:"arbiterOnly"`
	Msg        string `bson:"msg" json:"msg"`
	OKResponse `bson:",inline"`
}

func IsMaster added in v1.10.0

func IsMaster(ctx context.Context, client *mongo.Client) (*IsMasterResp, error)

type Member

type Member struct {
	Id                int                 `bson:"_id" json:"_id"`
	Name              string              `bson:"name" json:"name"`
	Health            MemberHealth        `bson:"health" json:"health"`
	State             MemberState         `bson:"state" json:"state"`
	StateStr          string              `bson:"stateStr" json:"stateStr"`
	Uptime            int64               `bson:"uptime" json:"uptime"`
	Optime            *Optime             `bson:"optime" json:"optime"`
	OptimeDate        time.Time           `bson:"optimeDate" json:"optimeDate"`
	ConfigVersion     int                 `bson:"configVersion" json:"configVersion"`
	ElectionTime      primitive.Timestamp `bson:"electionTime,omitempty" json:"electionTime,omitempty"`
	ElectionDate      time.Time           `bson:"electionDate,omitempty" json:"electionDate,omitempty"`
	InfoMessage       string              `bson:"infoMessage,omitempty" json:"infoMessage,omitempty"`
	OptimeDurable     *Optime             `bson:"optimeDurable,omitempty" json:"optimeDurable,omitempty"`
	OptimeDurableDate time.Time           `bson:"optimeDurableDate,omitempty" json:"optimeDurableDate,omitempty"`
	LastHeartbeat     time.Time           `bson:"lastHeartbeat,omitempty" json:"lastHeartbeat,omitempty"`
	LastHeartbeatRecv time.Time           `bson:"lastHeartbeatRecv,omitempty" json:"lastHeartbeatRecv,omitempty"`
	PingMs            int64               `bson:"pingMs,omitempty" json:"pingMs,omitempty"`
	Self              bool                `bson:"self,omitempty" json:"self,omitempty"`
	SyncingTo         string              `bson:"syncingTo,omitempty" json:"syncingTo,omitempty"`
}

type MemberHealth added in v1.5.0

type MemberHealth int

type MemberState added in v1.5.0

type MemberState int

type OKResponse

type OKResponse struct {
	Errmsg string `bson:"errmsg,omitempty" json:"errmsg,omitempty"`
	OK     int    `bson:"ok" json:"ok"`
}

OKResponse is a standard MongoDB response

type Optime added in v1.5.0

type Optime struct {
	Timestamp primitive.Timestamp `bson:"ts" json:"ts"`
	Term      int64               `bson:"t" json:"t"`
}

type RSConfig

type RSConfig struct {
	ID                                 string        `bson:"_id" json:"_id"`
	Version                            int           `bson:"version" json:"version"`
	Members                            ConfigMembers `bson:"members" json:"members"`
	Configsvr                          bool          `bson:"configsvr,omitempty" json:"configsvr,omitempty"`
	ProtocolVersion                    int           `bson:"protocolVersion,omitempty" json:"protocolVersion,omitempty"`
	Settings                           Settings      `bson:"settings,omitempty" json:"settings,omitempty"`
	WriteConcernMajorityJournalDefault bool          `bson:"writeConcernMajorityJournalDefault,omitempty" json:"writeConcernMajorityJournalDefault,omitempty"`
}

func ReadConfig

func ReadConfig(ctx context.Context, client *mongo.Client) (RSConfig, error)

type ReplSetGetConfig

type ReplSetGetConfig struct {
	Config     *RSConfig `bson:"config" json:"config"`
	OKResponse `bson:",inline"`
}

Response document from 'replSetGetConfig': https://docs.mongodb.com/manual/reference/command/replSetGetConfig/#dbcmd.replSetGetConfig

type Role added in v1.12.0

type Role struct {
	Role       string                   `bson:"role" json:"role"`
	Roles      []map[string]interface{} `bson:"roles" json:"roles"`
	Privileges []RolePrivilege          `bson:"privileges" json:"privileges"`
}

func GetRole added in v1.12.0

func GetRole(ctx context.Context, client *mongo.Client, role string) (*Role, error)

type RoleInfo added in v1.12.0

type RoleInfo struct {
	Roles      []Role `bson:"roles" json:"roles"`
	OKResponse `bson:",inline"`
}

type RolePrivilege added in v1.12.0

type RolePrivilege struct {
	Resource map[string]interface{} `bson:"resource" json:"resource"`
	Actions  []string               `bson:"actions" json:"actions"`
}

type Settings

type Settings struct {
	ChainingAllowed         bool                   `bson:"chainingAllowed,omitempty" json:"chainingAllowed,omitempty"`
	HeartbeatIntervalMillis int64                  `bson:"heartbeatIntervalMillis,omitempty" json:"heartbeatIntervalMillis,omitempty"`
	HeartbeatTimeoutSecs    int                    `bson:"heartbeatTimeoutSecs,omitempty" json:"heartbeatTimeoutSecs,omitempty"`
	ElectionTimeoutMillis   int64                  `bson:"electionTimeoutMillis,omitempty" json:"electionTimeoutMillis,omitempty"`
	CatchUpTimeoutMillis    int64                  `bson:"catchUpTimeoutMillis,omitempty" json:"catchUpTimeoutMillis,omitempty"`
	GetLastErrorModes       map[string]ReplsetTags `bson:"getLastErrorModes,omitempty" json:"getLastErrorModes,omitempty"`
	GetLastErrorDefaults    WriteConcern           `bson:"getLastErrorDefaults,omitempty" json:"getLastErrorDefaults,omitempty"`
	ReplicaSetID            primitive.ObjectID     `bson:"replicaSetId,omitempty" json:"replicaSetId,omitempty"`
}

Settings document from 'replSetGetConfig': https://docs.mongodb.com/manual/reference/command/replSetGetConfig/#dbcmd.replSetGetConfig

type ShardList added in v1.6.0

type ShardList struct {
	Shards []struct {
		ID    string `json:"_id" bson:"_id"`
		Host  string `json:"host" bson:"host"`
		State int    `json:"state" bson:"state"`
	} `json:"shards" bson:"shards"`
	OKResponse `bson:",inline"`
}

func ListShard added in v1.6.0

func ListShard(ctx context.Context, client *mongo.Client) (ShardList, error)

type ShardRemoveResp added in v1.7.0

type ShardRemoveResp struct {
	Msg       string `json:"msg" bson:"msg"`
	State     string `json:"state" bson:"state"`
	Remaining struct {
		Chunks      int `json:"chunks" bson:"chunks"`
		JumboChunks int `json:"jumboChunks" bson:"jumboChunks"`
	} `json:"remaining" bson:"remaining"`
	OKResponse `bson:",inline"`
}

func RemoveShard added in v1.7.0

func RemoveShard(ctx context.Context, client *mongo.Client, shard string) (ShardRemoveResp, error)

type Status added in v1.5.0

type Status struct {
	Set                     string         `bson:"set" json:"set"`
	Date                    time.Time      `bson:"date" json:"date"`
	MyState                 MemberState    `bson:"myState" json:"myState"`
	Members                 []*Member      `bson:"members" json:"members"`
	Term                    int64          `bson:"term,omitempty" json:"term,omitempty"`
	HeartbeatIntervalMillis int64          `bson:"heartbeatIntervalMillis,omitempty" json:"heartbeatIntervalMillis,omitempty"`
	Optimes                 *StatusOptimes `bson:"optimes,omitempty" json:"optimes,omitempty"`
	OKResponse              `bson:",inline"`
}

func RSStatus added in v1.5.0

func RSStatus(ctx context.Context, client *mongo.Client) (Status, error)

func (*Status) GetMembersByState added in v1.5.0

func (s *Status) GetMembersByState(state MemberState, limit int) []*Member

func (*Status) GetSelf added in v1.10.0

func (s *Status) GetSelf() *Member

func (*Status) Primary added in v1.5.0

func (s *Status) Primary() *Member

type StatusOptimes added in v1.5.0

type StatusOptimes struct {
	LastCommittedOpTime *Optime `bson:"lastCommittedOpTime" json:"lastCommittedOpTime"`
	AppliedOpTime       *Optime `bson:"appliedOpTime" json:"appliedOpTime"`
	DurableOptime       *Optime `bson:"durableOpTime" json:"durableOpTime"`
}

type User added in v1.12.0

type User struct {
	Roles []map[string]interface{} `bson:"roles" json:"roles"`
}

func GetUserInfo added in v1.12.0

func GetUserInfo(ctx context.Context, client *mongo.Client, username string) (*User, error)

type UsersInfo added in v1.12.0

type UsersInfo struct {
	Users      []User `bson:"users" json:"users"`
	OKResponse `bson:",inline"`
}

type WriteConcern

type WriteConcern struct {
	WriteConcern interface{} `bson:"w" json:"w"`
	WriteTimeout int         `bson:"wtimeout" json:"wtimeout"`
	Journal      bool        `bson:"j,omitempty" json:"j,omitempty"`
}

WriteConcern document: https://docs.mongodb.com/manual/reference/write-concern/

Jump to

Keyboard shortcuts

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