mongodb

package
v0.9.0-beta.2 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2024 License: AGPL-3.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PrimaryPriority   = 2
	SecondaryPriority = 1

	ServiceType = "mongodb"
)
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 CreateRole

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

func CreateUser

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

func DropUser

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

DropUser delete user

func NewCommands

func NewCommands() engines.ClusterCommands

func NewLocalUnauthClient

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

func NewManager

func NewManager(properties engines.Properties) (engines.DBManager, error)

func NewMongodbClient

func NewMongodbClient(ctx context.Context, config *Config) (*mongo.Client, error)

func NewMongosClient

func NewMongosClient(ctx context.Context, hosts []string) (*mongo.Client, error)

func NewReplSetClient

func NewReplSetClient(ctx context.Context, hosts []string) (*mongo.Client, error)

func NewStandaloneClient

func NewStandaloneClient(ctx context.Context, host string) (*mongo.Client, error)

func SetReplSetConfig

func SetReplSetConfig(ctx context.Context, rsClient *mongo.Client, cfg *RSConfig) error

func UpdateRole

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

func UpdateUserPass

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

UpdateUserPass updates user's password

func UpdateUserRoles

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

Types

type BalancerStatus

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

type BuildInfo

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

BuildInfo contains information about mongod build params

type Commands

type Commands struct {
	// contains filtered or unexported fields
}

func (Commands) ConnectCommand

func (r Commands) ConnectCommand(connectInfo *engines.AuthInfo) []string

func (Commands) ConnectExample

func (r Commands) ConnectExample(info *engines.ConnectionInfo, client string) string

func (Commands) Container

func (r Commands) Container() string

func (Commands) ExecuteCommand

func (r Commands) ExecuteCommand([]string) ([]string, []corev1.EnvVar, error)

type Config

type Config struct {
	Hosts            []string
	Username         string
	Password         string
	ReplSetName      string
	DatabaseName     string
	Params           string
	Direct           bool
	OperationTimeout time.Duration
}

func GetConfig

func GetConfig() *Config

func NewConfig

func NewConfig(properties map[string]string) (*Config, error)

func (*Config) DeepCopy

func (config *Config) DeepCopy() *Config

func (*Config) GetDBPort

func (config *Config) GetDBPort() int

type ConfigMember

type ConfigMember struct {
	ID                 int         `bson:"_id" json:"_id"`
	Host               string      `bson:"host" json:"host"`
	ArbiterOnly        *bool       `bson:"arbiterOnly,omitempty" json:"arbiterOnly,omitempty"`
	BuildIndexes       *bool       `bson:"buildIndexes,omitempty" json:"buildIndexes,omitempty"`
	Hidden             *bool       `bson:"hidden,omitempty" json:"hidden,omitempty"`
	Priority           int         `bson:"priority,omitempty" json:"priority,omitempty"`
	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,omitempty" json:"votes,omitempty"`
}

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

type ConfigMembers

type ConfigMembers []ConfigMember

type DBList

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

type FCV

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

type IsMasterResp

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

type LockResp

type LockResp struct {
	Info       string `bson:"info" json:"info"`
	LockCount  int64  `bson:"lockCount" json:"lockCount"`
	OKResponse `bson:",inline"`
}

type Manager

type Manager struct {
	engines.DBManagerBase
	Client   *mongo.Client
	Database *mongo.Database
}
var Mgr *Manager

func (*Manager) CreateRoot

func (mgr *Manager) CreateRoot(ctx context.Context) error

func (*Manager) Demote

func (mgr *Manager) Demote(context.Context) error

func (*Manager) Follow

func (mgr *Manager) Follow(ctx context.Context, cluster *dcs.Cluster) error

func (*Manager) GetHealthiestMember

func (mgr *Manager) GetHealthiestMember(cluster *dcs.Cluster, candidate string) *dcs.Member

func (*Manager) GetLeaderClient

func (mgr *Manager) GetLeaderClient(ctx context.Context, cluster *dcs.Cluster) (*mongo.Client, error)

func (*Manager) GetMemberAddrs

func (mgr *Manager) GetMemberAddrs(ctx context.Context, cluster *dcs.Cluster) []string

func (*Manager) GetMemberAddrsFromRSConfig

func (mgr *Manager) GetMemberAddrsFromRSConfig(rsConfig *RSConfig) []string

func (*Manager) GetMemberState

func (mgr *Manager) GetMemberState(ctx context.Context) (string, error)

func (*Manager) GetReplSetClient

func (mgr *Manager) GetReplSetClient(ctx context.Context, cluster *dcs.Cluster) (*mongo.Client, error)

func (*Manager) GetReplSetClientWithHosts

func (mgr *Manager) GetReplSetClientWithHosts(ctx context.Context, hosts []string) (*mongo.Client, error)

func (*Manager) GetReplSetConfig

func (mgr *Manager) GetReplSetConfig(ctx context.Context) (*RSConfig, error)

func (*Manager) GetReplSetStatus

func (mgr *Manager) GetReplSetStatus(ctx context.Context) (*ReplSetStatus, error)

func (*Manager) GetReplicaRole

func (mgr *Manager) GetReplicaRole(ctx context.Context, cluster *dcs.Cluster) (string, error)

func (*Manager) HasOtherHealthyLeader

func (mgr *Manager) HasOtherHealthyLeader(ctx context.Context, cluster *dcs.Cluster) *dcs.Member

func (*Manager) HasOtherHealthyMembers

func (mgr *Manager) HasOtherHealthyMembers(ctx context.Context, cluster *dcs.Cluster, leader string) []*dcs.Member

HasOtherHealthyMembers Are there any healthy members other than the leader?

func (*Manager) InitializeCluster

func (mgr *Manager) InitializeCluster(ctx context.Context, cluster *dcs.Cluster) error

func (*Manager) InitiateReplSet

func (mgr *Manager) InitiateReplSet(ctx context.Context, cluster *dcs.Cluster) error

InitiateReplSet is a method to create MongoDB cluster

func (*Manager) IsClusterHealthy

func (mgr *Manager) IsClusterHealthy(ctx context.Context, cluster *dcs.Cluster) bool

func (*Manager) IsClusterInitialized

func (mgr *Manager) IsClusterInitialized(ctx context.Context, cluster *dcs.Cluster) (bool, error)

IsClusterInitialized is a method to check if cluster is initialized or not

func (*Manager) IsCurrentMemberHealthy

func (mgr *Manager) IsCurrentMemberHealthy(ctx context.Context, cluster *dcs.Cluster) bool

func (*Manager) IsCurrentMemberInCluster

func (mgr *Manager) IsCurrentMemberInCluster(ctx context.Context, cluster *dcs.Cluster) bool

func (*Manager) IsDBStartupReady

func (mgr *Manager) IsDBStartupReady() bool

func (*Manager) IsLeader

func (mgr *Manager) IsLeader(ctx context.Context, cluster *dcs.Cluster) (bool, error)

func (*Manager) IsLeaderMember

func (mgr *Manager) IsLeaderMember(ctx context.Context, cluster *dcs.Cluster, dcsMember *dcs.Member) (bool, error)

func (*Manager) IsMemberHealthy

func (mgr *Manager) IsMemberHealthy(ctx context.Context, cluster *dcs.Cluster, member *dcs.Member) bool

func (*Manager) IsPromoted

func (mgr *Manager) IsPromoted(ctx context.Context) bool

func (*Manager) IsRootCreated

func (mgr *Manager) IsRootCreated(ctx context.Context) (bool, error)

func (*Manager) IsRunning

func (mgr *Manager) IsRunning() bool

func (*Manager) JoinCurrentMemberToCluster

func (mgr *Manager) JoinCurrentMemberToCluster(ctx context.Context, cluster *dcs.Cluster) error

func (*Manager) LeaveMemberFromCluster

func (mgr *Manager) LeaveMemberFromCluster(ctx context.Context, cluster *dcs.Cluster, memberName string) error

func (*Manager) Lock

func (mgr *Manager) Lock(ctx context.Context, reason string) error

func (*Manager) Promote

func (mgr *Manager) Promote(ctx context.Context, cluster *dcs.Cluster) error

func (*Manager) Recover

func (mgr *Manager) Recover(ctx context.Context, cluster *dcs.Cluster) error

func (*Manager) Unlock

func (mgr *Manager) Unlock(ctx context.Context) error

func (*Manager) UpdateCurrentMemberHost added in v0.8.2

func (mgr *Manager) UpdateCurrentMemberHost(ctx context.Context, cluster *dcs.Cluster) 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

type MemberHealth int

type MemberState

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

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,omitempty" json:"_id,omitempty"`
	Version                            int           `bson:"version,omitempty" json:"version,omitempty"`
	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:"-" json:"settings,omitempty"`
	WriteConcernMajorityJournalDefault bool          `bson:"writeConcernMajorityJournalDefault,omitempty" json:"writeConcernMajorityJournalDefault,omitempty"`
}

func GetReplSetConfig

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

type ReplSetGetConfig

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

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

type ReplSetStatus

type ReplSetStatus 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 GetReplSetStatus

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

func (*ReplSetStatus) GetMembersByState

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

func (*ReplSetStatus) GetSelf

func (s *ReplSetStatus) GetSelf() *Member

func (*ReplSetStatus) Primary

func (s *ReplSetStatus) Primary() *Member

type Role

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

func GetRole

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

type RoleInfo

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

type RolePrivilege

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

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"`
}

type ShardRemoveResp

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"`
}

type StatusOptimes

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

type User

type User struct {
	ID    string                   `bson:"_id" json:"_id"`
	User  string                   `bson:"user" json:"user"`
	DB    string                   `bson:"db" json:"db"`
	Roles []map[string]interface{} `bson:"roles" json:"roles"`
}

func GetUser

func GetUser(ctx context.Context, client *mongo.Client, userName string) (*User, error)

type UsersInfo

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