statedb

package
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2026 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (

	// TxStatusTableName is the name of the system table holding per-transaction status.
	TxStatusTableName = "tx_status"
)

Variables

This section is empty.

Functions

func ConfigureConnReadDeadline

func ConfigureConnReadDeadline(poolConfig *pgxpool.Config)

ConfigureConnReadDeadline configures pool connections with a per-read deadline so that reads on dead connections fail instead of hanging indefinitely.

We use a custom DialFunc that wraps every net.Conn with deadlineConn, which calls SetReadDeadline before each Read. Pool callbacks like AfterConnect or BeforeAcquire only fire once, so they can only set a single absolute deadline that expires even if the connection is healthy. Wrapping net.Conn lets us reset the deadline on every Read call.

The read timeout is set to 60 seconds, which is more than enough for read-set validation and commit operations. This can be made configurable if needed.

On macOS, Docker Desktop runs a userspace TCP proxy inside a Linux VM. When a container dies the proxy does not immediately close forwarded connections, so reads hang indefinitely. The deadlineConn wrapper ensures stuck reads fail within the timeout, allowing the pool to replace them. On Linux, reads already fail immediately via TCP RST when a node dies, so the wrapper is harmless.

func DataSourceName

func DataSourceName(d DataSourceNameParams) (string, error)

DataSourceName returns the data source name of the database.

func FmtNsID

func FmtNsID(sqlTemplate, namespaceID string) string

FmtNsID replaces the namespace placeholder with the namespace ID in an SQL template string.

func GetTablePreSplitTablets

func GetTablePreSplitTablets(ctx context.Context, pg *pgxpool.Pool, config *Config) (int, error)

GetTablePreSplitTablets determines the number of tablets to use for table pre-splitting. Returns 0 for PostgreSQL or when TablePreSplitTablets is not configured. Returns the configured value for YugabyteDB.

func IsYugabyteDB

func IsYugabyteDB(ctx context.Context, pool *pgxpool.Pool) (bool, error)

IsYugabyteDB queries the database version string to determine whether the backend is YugabyteDB. YugabyteDB's version() output contains "-YB-" (e.g., "PostgreSQL 11.2-YB-2.20.1.0 ..."), which distinguishes it from standard PostgreSQL.

func MakeNsTablesQuery

func MakeNsTablesQuery(nsID string, tablets int) string

MakeNsTablesQuery returns the query to create the table and functions for a namespace.

func NewPool

func NewPool(ctx context.Context, config *Config) (*pgxpool.Pool, error)

NewPool creates a new pool from a database config.

func SetupSystemTablesAndNamespaces

func SetupSystemTablesAndNamespaces(
	ctx context.Context, config *Config,
) error

SetupSystemTablesAndNamespaces creates the required system tables and namespaces in the database. This function should be called before operating against the database. It is safe to run multiple times.

func TableName

func TableName(nsID string) string

TableName returns the table name for the given namespace.

Types

type Config

type Config struct {
	Endpoints            []*connection.Endpoint `mapstructure:"endpoints" default:"localhost:5433"`
	Username             string                 `mapstructure:"username"`
	Password             string                 `mapstructure:"password"`
	Database             string                 `mapstructure:"database" default:"yugabyte"`
	MaxConnections       int32                  `mapstructure:"max-connections" default:"20" validate:"gt=0"`
	MinConnections       int32                  `mapstructure:"min-connections" default:"1" validate:"gte=0"`
	LoadBalance          bool                   `mapstructure:"load-balance"`
	Retry                *retry.Profile         `mapstructure:"retry" default:"max-elapsed-time=10m"`
	TLS                  TLSConfig              `mapstructure:"tls"`
	TablePreSplitTablets int                    `mapstructure:"table-pre-split-tablets"`
}

Config is the configuration for database initialization and connection. This configuration is shared across multiple services (VC, Query) and the init-db CLI command.

func (*Config) DataSourceName

func (d *Config) DataSourceName() (string, error)

DataSourceName returns the data source name of the database.

func (*Config) EndpointsString

func (d *Config) EndpointsString() string

EndpointsString returns the address:port as a string with comma as a separator between endpoints.

type DataSourceNameParams

type DataSourceNameParams struct {
	Username        string
	Password        string
	Database        string
	EndpointsString string
	LoadBalance     bool
	TLS             TLSConfig
}

DataSourceNameParams defines the parameters required to construct a database connection string.

type TLSConfig

type TLSConfig struct {
	Mode       string `mapstructure:"mode"`
	CACertPath string `mapstructure:"ca-cert-path"`
}

TLSConfig holds the database TLS mode and its necessary credentials.

Jump to

Keyboard shortcuts

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