database

package
v0.0.0-...-4f3f676 Latest Latest
Warning

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

Go to latest
Published: Dec 20, 2024 License: AGPL-3.0 Imports: 33 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BootstrapDqlite

func BootstrapDqlite(
	ctx context.Context,
	mgr BootstrapNodeManager,
	uuid model.UUID,
	logger logger.Logger,
	opts ...BootstrapOpt,
) error

BootstrapDqlite opens a new database for the controller, and runs the DDL to create its schema.

It accepts an optional list of functions to perform operations on the controller database.

func InsertControllerNodeID

func InsertControllerNodeID(ctx context.Context, runner coredatabase.TxnRunner, nodeID uint64) error

InsertControllerNodeID inserts the node ID of the controller node into the controller_node table.

func IsErrConstraintCheck

func IsErrConstraintCheck(err error) bool

IsErrConstraintCheck returns true if the input error was returned by SQLite due to violation of a constraint check.

func IsErrConstraintForeignKey

func IsErrConstraintForeignKey(err error) bool

IsErrConstraintForeignKey returns true if the input error was returned by SQLite due to violation of a constraint foreign key.

func IsErrConstraintNotNull

func IsErrConstraintNotNull(err error) bool

IsErrConstraintNotNull returns true if the input error was returned by SQLite due to violation of a constraint not null.

func IsErrConstraintPrimaryKey

func IsErrConstraintPrimaryKey(err error) bool

IsErrConstraintPrimaryKey returns true if the input error was returned by SQLite due to violation of a constraint primary key.

func IsErrConstraintRowID

func IsErrConstraintRowID(err error) bool

IsErrConstraintRowID returns true if the input error was returned by SQLite due to violation of a unique constraint.

func IsErrConstraintTrigger

func IsErrConstraintTrigger(err error) bool

IsErrConstraintTrigger returns true if the input error was returned by SQLite due to violation of a constraint trigger.

func IsErrConstraintUnique

func IsErrConstraintUnique(err error) bool

IsErrConstraintUnique returns true if the input error was returned by SQLite due to violation of a unique constraint.

func IsErrError

func IsErrError(err error) bool

IsErrError returns true if the input error was returned by SQLite due to a generic error. This is normally used when we can't determine the specific error type. For example: - no such trigger - no such table This is useful when trying to determine if the error is sqlite specific or internal to Juju.

func IsErrNotFound

func IsErrNotFound(err error) bool

IsErrNotFound returns true if the input error was returned by SQLite due to a missing record.

func IsError

func IsError(err error) bool

IsError reports if the any type passed to it is a database driver error in Juju. The purpose of this function is so that our domain error masking can assert if a specific error needs to be hidden from layers above that of the domain/state.

func IsErrorTarget

func IsErrorTarget(target any) bool

IsErrorTarget reports if the any type passed to it is a database driver error.

func Retry

func Retry(ctx context.Context, fn func() error) error

Retry defines a generic retry function for applying transactions on a given database. It expects that no individual transaction function should take longer than the default timeout.

This should not be used directly, instead the TxnRunner should be used to handle transactions.

func StdTxn

func StdTxn(ctx context.Context, db *sql.DB, fn func(context.Context, *sql.Tx) error) error

StdTxn defines a generic txn function for applying transactions on a given database. It expects that no individual transaction function should take longer than the default timeout. There are no retry semantics for running the function.

This should not be used directly, instead the TxnRunner should be used to handle transactions.

func Txn

func Txn(ctx context.Context, db *sqlair.DB, fn func(context.Context, *sqlair.TX) error) error

Txn executes the input function against the tracked database, using the sqlair package. The sqlair package provides a mapping library for SQL queries and statements. Retry semantics are applied automatically based on transient failures. This is the function that almost all downstream database consumers should use.

This should not be used directly, instead the TxnRunner should be used to handle transactions.

Types

type BootstrapNodeManager

type BootstrapNodeManager interface {
	// EnsureDataDir ensures that a directory for Dqlite data exists at
	// a path determined by the agent config, then returns that path.
	EnsureDataDir() (string, error)

	// IsLoopbackPreferred returns true if the Dqlite application should
	// be bound to the loopback address.
	IsLoopbackPreferred() bool

	// WithLoopbackAddressOption returns a Dqlite application
	// Option that will bind Dqlite to the loopback IP.
	WithLoopbackAddressOption() app.Option

	// WithPreferredCloudLocalAddressOption uses the input network config
	// source to return a local-cloud address to which to bind Dqlite,
	// provided that a unique one can be determined.
	WithPreferredCloudLocalAddressOption(network.ConfigSource) (app.Option, error)

	// WithTLSOption returns a Dqlite application Option for TLS encryption
	// of traffic between clients and clustered application nodes.
	WithTLSOption() (app.Option, error)

	// WithLogFuncOption returns a Dqlite application Option
	// that will proxy Dqlite log output via this factory's
	// logger where the level is recognised.
	WithLogFuncOption() app.Option

	// WithTracingOption returns a Dqlite application Option
	// that will enable tracing of Dqlite operations.
	WithTracingOption() app.Option
}

BootstrapNodeManager is an interface for managing the bootstrap of a Dqlite node.

type BootstrapOpt

type BootstrapOpt func(
	ctx context.Context,
	controller, model coredatabase.TxnRunner,
) error

BootstrapOpt is a function run when bootstrapping a database, used to insert initial data into the model.

type DBMigration

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

DBMigration is used to apply a series of deltas to a database.

func NewDBMigration

func NewDBMigration(db database.TxnRunner, logger logger.Logger, schema Schema) *DBMigration

NewDBMigration returns a reference to a new migration that is used to apply the input deltas to the input database. The deltas are applied in the order supplied.

func (*DBMigration) Apply

func (m *DBMigration) Apply(ctx context.Context) error

Apply executes all deltas against the database inside a transaction.

type NodeManager

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

NodeManager is responsible for interrogating a single Dqlite node, and emitting configuration for starting its Dqlite `App` based on operational requirements and controller agent config.

func NewNodeManager

func NewNodeManager(cfg agent.Config, isLoopbackPreferred bool, logger logger.Logger, slowQueryLogger coredatabase.SlowQueryLogger) *NodeManager

NewNodeManager returns a new NodeManager reference based on the input agent configuration.

If isLoopbackPreferred is true, we bind Dqlite to 127.0.0.1 and eschew TLS termination. This is useful primarily in unit testing and a temporary workaround for CAAS, which does not yet support enable-ha.

If it is false, we attempt to identify a unique local-cloud address. If we find one, we use it as the bind address. Otherwise, we fall back to the loopback binding.

func (*NodeManager) ClusterServers

func (m *NodeManager) ClusterServers(ctx context.Context) ([]dqlite.NodeInfo, error)

ClusterServers returns the node information for Dqlite nodes configured to be in the cluster.

func (*NodeManager) EnsureDataDir

func (m *NodeManager) EnsureDataDir() (string, error)

EnsureDataDir ensures that a directory for Dqlite data exists at a path determined by the agent config, then returns that path.

func (*NodeManager) IsExistingNode

func (m *NodeManager) IsExistingNode() (bool, error)

IsExistingNode returns true if this machine or container has ever started a Dqlite `App` before. Specifically, this is whether the Dqlite data directory is empty.

func (*NodeManager) IsLoopbackBound

func (m *NodeManager) IsLoopbackBound(ctx context.Context) (bool, error)

IsLoopbackBound returns true if we are a cluster of one, and bound to the loopback IP address.

func (*NodeManager) IsLoopbackPreferred

func (m *NodeManager) IsLoopbackPreferred() bool

IsLoopbackPreferred returns true if we should prefer to bind Dqlite to the loopback IP address. This is currently true for CAAS and unit testing. Once CAAS supports enable-ha we'll have to revisit this.

func (*NodeManager) NodeInfo

func (m *NodeManager) NodeInfo() (dqlite.NodeInfo, error)

NodeInfo reads the local node information file in the Dqlite directory and returns the dqlite.NodeInfo represented by its contents.

func (*NodeManager) SetClusterServers

func (m *NodeManager) SetClusterServers(ctx context.Context, servers []dqlite.NodeInfo) error

SetClusterServers reconfigures the Dqlite cluster by writing the input servers to Dqlite's Raft log and the local node YAML store. This should only be called on a stopped Dqlite node.

func (*NodeManager) SetClusterToLocalNode

func (m *NodeManager) SetClusterToLocalNode(ctx context.Context) error

SetClusterToLocalNode reconfigures the Dqlite cluster so that it has the local node as its only member. This is intended as a disaster recovery utility, and should only be called: 1. At great need. 2. With steadfast guarantees of data integrity.

func (*NodeManager) SetNodeInfo

func (m *NodeManager) SetNodeInfo(server dqlite.NodeInfo) error

SetNodeInfo rewrites the local node information file in the Dqlite data directory, so that it matches the input NodeInfo. This should only be called on a stopped Dqlite node.

func (*NodeManager) WithAddressOption

func (m *NodeManager) WithAddressOption(ip string) app.Option

WithAddressOption returns a Dqlite application Option for specifying the local address:port to use.

func (*NodeManager) WithClusterOption

func (m *NodeManager) WithClusterOption(addrs []string) app.Option

WithClusterOption returns a Dqlite application Option for initialising Dqlite as the member of a cluster with peers representing other controllers.

func (*NodeManager) WithLogFuncOption

func (m *NodeManager) WithLogFuncOption() app.Option

WithLogFuncOption returns a Dqlite application Option that will proxy Dqlite log output via this factory's logger where the level is recognised.

func (*NodeManager) WithLoopbackAddressOption

func (m *NodeManager) WithLoopbackAddressOption() app.Option

WithLoopbackAddressOption returns a Dqlite application Option that will bind Dqlite to the loopback IP.

func (*NodeManager) WithPreferredCloudLocalAddressOption

func (m *NodeManager) WithPreferredCloudLocalAddressOption(source corenetwork.ConfigSource) (app.Option, error)

WithPreferredCloudLocalAddressOption uses the input network config source to return a local-cloud address to which to bind Dqlite, provided that a unique one can be determined. If there are zero or multiple local-cloud addresses detected on the host, we fall back to binding to the loopback address. This method is only relevant to bootstrap. At all other times (such as when joining a cluster) the bind address is determined externally and passed as the argument to WithAddressOption.

func (*NodeManager) WithTLSOption

func (m *NodeManager) WithTLSOption() (app.Option, error)

WithTLSOption returns a Dqlite application Option for TLS encryption of traffic between clients and clustered application nodes.

func (*NodeManager) WithTracingOption

func (m *NodeManager) WithTracingOption() app.Option

WithTracingOption returns a Dqlite application Option that will enable tracing of Dqlite queries.

type NullDuration

type NullDuration struct {
	Duration time.Duration
	Valid    bool
}

NullDuration represents a nullable time.Duration.

func NewNullDuration

func NewNullDuration(d time.Duration) NullDuration

NewNullDuration returns a new NullDuration with the given duration.

func (*NullDuration) Scan

func (nd *NullDuration) Scan(value interface{}) error

Scan implements the sql.Scanner interface.

func (NullDuration) Value

func (nd NullDuration) Value() (driver.Value, error)

Value implements the driver.Valuer interface.

type Schema

type Schema interface {
	// Ensure applies the schema to the database, if it has not already been
	// applied. It returns the list of changes that were applied.
	Ensure(context.Context, database.TxnRunner) (schema.ChangeSet, error)
}

Schema is used to apply a schema to a database.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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