mongoutil

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2025 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	MongoPrimaryRole   = "PRIMARY"
	MongoSecondaryRole = "SECONDARY"
	MongoArbiterRole   = "ARBITER"
	MongoUnknownRole   = "UNKNOWN"
)

MongoDB node role types

View Source
const (
	MongoPrimaryState   = "PRIMARY"
	MongoSecondaryState = "SECONDARY"
	MongoStartupState   = "STARTUP"
	MongoStartup2State  = "STARTUP2"

	MongoRecoveringState = "RECOVERING"
	MongoArbiterState    = "ARBITER"
	MongoDownState       = "DOWN"
	MongoUnknownState    = "UNKNOWN"
	MongoRemovedState    = "REMOVED"
)

MongoDB node state types

Variables

This section is empty.

Functions

This section is empty.

Types

type AdminConnections

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

AdminConnections manages multiple MongoDB connections

func (*AdminConnections) Add

func (c *AdminConnections) Add(addr string) error

Add adds a new client for the given address

func (*AdminConnections) Get

func (c *AdminConnections) Get(addr string) (IMongoClient, error)

Get returns a client for the given address

func (*AdminConnections) GetAll

func (c *AdminConnections) GetAll() map[string]IMongoClient

GetAll returns all clients

func (*AdminConnections) Remove

func (c *AdminConnections) Remove(addr string) error

Remove removes the client for the given address

func (*AdminConnections) Reset

func (c *AdminConnections) Reset()

Reset closes and removes all connections

type AdminOptions

type AdminOptions struct {
	ConnectionTimeout time.Duration
	Username          string
	Password          string
	AuthDatabase      string
}

AdminOptions optional options for mongodb admin

type IAdminConnections

type IAdminConnections interface {
	// Get returns a client for the given address
	Get(addr string) (IMongoClient, error)

	// GetAll returns all clients
	GetAll() map[string]IMongoClient

	// Add adds a new client for the given address
	Add(addr string) error

	// Remove removes the client for the given address
	Remove(addr string) error

	// Reset closes and removes all connections
	Reset()
}

IAdminConnections interface for managing multiple MongoDB connections

func NewAdminConnections

func NewAdminConnections(addrs []string, options *AdminOptions, log logr.Logger) IAdminConnections

NewAdminConnections creates a new AdminConnections instance

type IMongoClient

type IMongoClient interface {

	// Disconnect closes the connection to MongoDB
	Disconnect(ctx context.Context) error

	// GetReplicaSetStatus returns the replica set status
	GetReplicaSetStatus(ctx context.Context) (*ReplicaSetStatus, error)

	// GetReplicaSetConfig returns the replica set configuration
	GetReplicaSetConfig(ctx context.Context) (*ReplicaSetConfig, error)

	// InitiateReplicaSet initializes a new replica set
	InitiateReplicaSet(ctx context.Context, config *ReplicaSetConfig) error

	// AddMemberToReplicaSet adds a new member to the replica set
	AddMemberToReplicaSet(ctx context.Context, host string, port int) error

	// RemoveMemberFromReplicaSet removes a member from the replica set
	RemoveMemberFromReplicaSet(ctx context.Context, host string, port int) error

	// StepDown forces the primary to step down
	StepDown(ctx context.Context, stepDownSecs int) error

	// IsMaster checks if the current node is the primary
	IsMaster(ctx context.Context) (*IsMasterResult, error)
}

IMongoClient interface for MongoDB client operations

func NewMongoClient

func NewMongoClient(address string, opts *AdminOptions) (IMongoClient, error)

NewMongoClient creates a new MongoDB client

type IReplicaSetAdmin

type IReplicaSetAdmin interface {
	// Connections returns the connection map of all clients
	Connections() IAdminConnections

	// Close the admin connections
	Close()

	// GetReplicaSetInfos return the ReplicaSetInfos for all nodes
	GetReplicaSetInfos(ctx context.Context, memberCount int) *ReplicaSetInfos

	// InitiateReplicaSet initializes a new replica set
	InitiateReplicaSet(ctx context.Context, addr, replicaSetName string, members []ReplicaSetConfigMember) error

	// AddMember adds a new member to the replica set
	AddMember(ctx context.Context, primaryAddr, memberHost string, memberPort int) error

	// RemoveMember removes a member from the replica set
	RemoveMember(ctx context.Context, primaryAddr, memberHost string, memberPort int) error

	// StepDown forces the primary to step down
	StepDown(ctx context.Context, primaryAddr string, stepDownSecs int) error
}

IReplicaSetAdmin MongoDB replica set admin interface

func NewReplicaSetAdmin

func NewReplicaSetAdmin(addrs []string, options *AdminOptions, log logr.Logger) IReplicaSetAdmin

NewReplicaSetAdmin returns new IReplicaSetAdmin instance

type IsMasterResult

type IsMasterResult struct {
	IsMaster                     bool      `bson:"ismaster"`
	IsSecondary                  bool      `bson:"secondary"`
	MaxBsonObjectSize            int       `bson:"maxBsonObjectSize"`
	MaxMessageSizeBytes          int       `bson:"maxMessageSizeBytes"`
	MaxWriteBatchSize            int       `bson:"maxWriteBatchSize"`
	LocalTime                    time.Time `bson:"localTime"`
	LogicalSessionTimeoutMinutes int       `bson:"logicalSessionTimeoutMinutes"`
	ConnectionId                 int       `bson:"connectionId"`
	MinWireVersion               int       `bson:"minWireVersion"`
	MaxWireVersion               int       `bson:"maxWireVersion"`
	ReadOnly                     bool      `bson:"readOnly"`
	OK                           float64   `bson:"ok"`
	SetName                      string    `bson:"setName,omitempty"`
	SetVersion                   int       `bson:"setVersion,omitempty"`
	Hosts                        []string  `bson:"hosts,omitempty"`
	Primary                      string    `bson:"primary,omitempty"`
	Me                           string    `bson:"me,omitempty"`
}

IsMasterResult represents the result of isMaster command

type MongoClient

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

MongoClient wraps the MongoDB client

func (*MongoClient) AddMemberToReplicaSet

func (c *MongoClient) AddMemberToReplicaSet(ctx context.Context, host string, port int) error

AddMemberToReplicaSet adds a new member to the replica set

func (*MongoClient) Disconnect

func (c *MongoClient) Disconnect(ctx context.Context) error

Disconnect closes the connection to MongoDB

func (*MongoClient) GetReplicaSetConfig

func (c *MongoClient) GetReplicaSetConfig(ctx context.Context) (*ReplicaSetConfig, error)

GetReplicaSetConfig returns the replica set configuration

func (*MongoClient) GetReplicaSetStatus

func (c *MongoClient) GetReplicaSetStatus(ctx context.Context) (*ReplicaSetStatus, error)

GetReplicaSetStatus returns the replica set status

func (*MongoClient) InitiateReplicaSet

func (c *MongoClient) InitiateReplicaSet(ctx context.Context, config *ReplicaSetConfig) error

InitiateReplicaSet initializes a new replica set

func (*MongoClient) IsMaster

func (c *MongoClient) IsMaster(ctx context.Context) (*IsMasterResult, error)

IsMaster checks if the current node is the primary

func (*MongoClient) RemoveMemberFromReplicaSet

func (c *MongoClient) RemoveMemberFromReplicaSet(ctx context.Context, host string, port int) error

RemoveMemberFromReplicaSet removes a member from the replica set

func (*MongoClient) StepDown

func (c *MongoClient) StepDown(ctx context.Context, stepDownSecs int) error

StepDown forces the primary to step down

type ReplicaSetAdmin

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

ReplicaSetAdmin wraps MongoDB replica set admin logic

func (*ReplicaSetAdmin) AddMember

func (a *ReplicaSetAdmin) AddMember(ctx context.Context, primaryAddr, memberHost string, memberPort int) error

AddMember adds a new member to the replica set

func (*ReplicaSetAdmin) Close

func (a *ReplicaSetAdmin) Close()

Close used to close all possible resources instance by the ReplicaSetAdmin

func (*ReplicaSetAdmin) Connections

func (a *ReplicaSetAdmin) Connections() IAdminConnections

Connections returns the connection map of all clients

func (*ReplicaSetAdmin) GetReplicaSetInfos

func (a *ReplicaSetAdmin) GetReplicaSetInfos(ctx context.Context, memberCount int) *ReplicaSetInfos

GetReplicaSetInfos return the ReplicaSetInfos for all nodes

func (*ReplicaSetAdmin) InitiateReplicaSet

func (a *ReplicaSetAdmin) InitiateReplicaSet(ctx context.Context, addr, replicaSetName string, members []ReplicaSetConfigMember) error

InitiateReplicaSet initializes a new replica set

func (*ReplicaSetAdmin) RemoveMember

func (a *ReplicaSetAdmin) RemoveMember(ctx context.Context, primaryAddr, memberHost string, memberPort int) error

RemoveMember removes a member from the replica set

func (*ReplicaSetAdmin) StepDown

func (a *ReplicaSetAdmin) StepDown(ctx context.Context, primaryAddr string, stepDownSecs int) error

StepDown forces the primary to step down

type ReplicaSetConfig

type ReplicaSetConfig struct {
	Id       string                    `bson:"_id"`
	Version  int                       `bson:"version"`
	Members  []ReplicaSetConfigMember  `bson:"members"`
	Settings *ReplicaSetConfigSettings `bson:"settings,omitempty"`
}

ReplicaSetConfig represents the MongoDB replica set configuration

type ReplicaSetConfigMember

type ReplicaSetConfigMember struct {
	ID                 int     `bson:"_id"`
	Host               string  `bson:"host"`
	Priority           float64 `bson:"priority"`
	Hidden             bool    `bson:"hidden,omitempty"`
	ArbiterOnly        bool    `bson:"arbiterOnly,omitempty"`
	Votes              int     `bson:"votes,omitempty"`
	SecondaryDelaySecs int     `bson:"secondaryDelaySecs,omitempty"`
	BuildIndexes       bool    `bson:"buildIndexes,omitempty"`
}

ReplicaSetConfigMember represents a member in the replica set configuration

type ReplicaSetConfigSettings

type ReplicaSetConfigSettings struct {
	ChainingAllowed         bool                      `bson:"chainingAllowed,omitempty"`
	HeartbeatIntervalMillis int                       `bson:"heartbeatIntervalMillis,omitempty"`
	HeartbeatTimeoutSecs    int                       `bson:"heartbeatTimeoutSecs,omitempty"`
	ElectionTimeoutMillis   int                       `bson:"electionTimeoutMillis,omitempty"`
	CatchUpTimeoutMillis    int                       `bson:"catchUpTimeoutMillis,omitempty"`
	GetLastErrorModes       map[string]map[string]int `bson:"getLastErrorModes,omitempty"`
	GetLastErrorDefaults    map[string]interface{}    `bson:"getLastErrorDefaults,omitempty"`
	ReplicaSetId            primitive.ObjectID        `bson:"replicaSetId,omitempty"`
}

ReplicaSetConfigSettings represents the settings section of replica set configuration

type ReplicaSetInfoStatus

type ReplicaSetInfoStatus string

ReplicaSetInfoStatus describes the various status of replica set

const (
	// ReplicaSetInfoConsistent all the nodes have the expected role and configuration
	ReplicaSetInfoConsistent ReplicaSetInfoStatus = "Consistent"
	// ReplicaSetInfoInconsistent some nodes have unexpected configuration
	ReplicaSetInfoInconsistent ReplicaSetInfoStatus = "Inconsistent"
	// ReplicaSetInfoUnset the replica set is not configured
	ReplicaSetInfoUnset ReplicaSetInfoStatus = "Unset"
	// ReplicaSetInfoUnavailable no nodes are reachable
	ReplicaSetInfoUnavailable ReplicaSetInfoStatus = "Unavailable"
	// ReplicaSetInfoPartial status of the replicaset info: data is not complete (some nodes didn't respond) but group is avaiable
	ReplicaSetInfoPartial = "Partial"
)

type ReplicaSetInfos

type ReplicaSetInfos struct {
	Infos  map[string]*ReplicaSetNode
	Status ReplicaSetInfoStatus
}

ReplicaSetInfos contains information about all nodes in the replica set

func NewReplicaSetInfos

func NewReplicaSetInfos() *ReplicaSetInfos

NewReplicaSetInfos creates a new ReplicaSetInfos instance

func (*ReplicaSetInfos) ElectPrimary

func (i *ReplicaSetInfos) ElectPrimary() string

ElectPrimary selects a suitable node to become the primary In Kubernetes, we rely on automatic mechanisms rather than manual priority configuration

type ReplicaSetNode

type ReplicaSetNode struct {
	Host        string
	Port        int
	ID          int
	State       string
	Role        string
	Health      int
	Priority    float64
	Hidden      bool
	ArbiterOnly bool
	Votes       int
}

ReplicaSetNode represents a MongoDB replica set member

func NewDefaultReplicaSetNode

func NewDefaultReplicaSetNode() *ReplicaSetNode

NewDefaultReplicaSetNode creates a new ReplicaSetNode with default values

func (*ReplicaSetNode) GetRole

GetRole returns the MongoDBReplicasetRole based on the node's state

type ReplicaSetStatus

type ReplicaSetStatus struct {
	Set     string                   `bson:"set"`
	Date    interface{}              `bson:"date"`
	MyState int                      `bson:"myState"`
	Term    int                      `bson:"term,omitempty"`
	Members []ReplicaSetStatusMember `bson:"members"`
}

ReplicaSetStatus represents the result of rs.status() command

type ReplicaSetStatusMember

type ReplicaSetStatusMember struct {
	ID                int         `bson:"_id"`
	Name              string      `bson:"name"`
	Health            int         `bson:"health"`
	State             int         `bson:"state"`
	StateStr          string      `bson:"stateStr"`
	Uptime            int         `bson:"uptime,omitempty"`
	OptimeDate        interface{} `bson:"optimeDate,omitempty"`
	ConfigVersion     int         `bson:"configVersion,omitempty"`
	LastHeartbeat     interface{} `bson:"lastHeartbeat,omitempty"`
	LastHeartbeatRecv interface{} `bson:"lastHeartbeatRecv,omitempty"`
	PingMs            int         `bson:"pingMs,omitempty"`
}

ReplicaSetStatusMember represents a member in the replica set status

Jump to

Keyboard shortcuts

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