dqlite

package module
v1.11.9 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2023 License: Apache-2.0 Imports: 8 Imported by: 35

README

go-dqlite CI tests Coverage Status Go Report Card GoDoc

This repository provides the go-dqlite Go package, containing bindings for the dqlite C library and a pure-Go client for the dqlite wire protocol.

Usage

The best way to understand how to use the go-dqlite package is probably by looking at the source code of the demo program and use it as example.

In general your application will use code such as:

dir := "/path/to/data/directory"
address := "1.2.3.4:666" // Unique node address
cluster := []string{...} // Optional list of existing nodes, when starting a new node
app, err := app.New(dir, app.WithAddress(address), app.WithCluster(cluster))
if err != nil {
        // ...
}

db, err := app.Open(context.Background(), "my-database")
if err != nil {
        // ...
}

// db is a *sql.DB object
if _, err := db.Exec("CREATE TABLE my_table (n INT)"); err != nil
        // ...
}

Build

In order to use the go-dqlite package in your application, you'll need to have the dqlite C library installed on your system, along with its dependencies. You'll also need to put CGO_LDFLAGS_ALLOW="-Wl,-z,now" in the environment of any Go build commands (see here for the explanation).

By default, go-dqlite's client module supports storing a cache of the cluster's state in a SQLite database, locally on each cluster member. (This is not to be confused with any SQLite databases that are managed by dqlite.) In order to do this, it imports https://github.com/mattn/go-sqlite3, and so you can use the libsqlite3 build tag to control whether go-sqlite3 links to a system libsqlite3 or builds its own. You can also disable support for SQLite node stores entirely with the nosqlite3 build tag (unique to go-dqlite). If you pass this tag, your application will not link directly to libsqlite3 (but it will still link it indirectly via libdqlite, unless you've dropped the sqlite3.c amalgamation into the dqlite build).

Documentation

The documentation for this package can be found on pkg.go.dev.

Demo

To see dqlite in action, either install the Debian package from the PPA:

sudo add-apt-repository -y ppa:dqlite/dev
sudo apt install dqlite-tools libdqlite-dev

or build the dqlite C library and its dependencies from source, as described here, and then run:

go install -tags libsqlite3 ./cmd/dqlite-demo

from the top-level directory of this repository.

This builds a demo dqlite application, which exposes a simple key/value store over an HTTP API.

Once the dqlite-demo binary is installed (normally under ~/go/bin or /usr/bin/), start three nodes of the demo application:

dqlite-demo --api 127.0.0.1:8001 --db 127.0.0.1:9001 &
dqlite-demo --api 127.0.0.1:8002 --db 127.0.0.1:9002 --join 127.0.0.1:9001 &
dqlite-demo --api 127.0.0.1:8003 --db 127.0.0.1:9003 --join 127.0.0.1:9001 &

The --api flag tells the demo program where to expose its HTTP API.

The --db flag tells the demo program to use the given address for internal database replication.

The --join flag is optional and should be used only for additional nodes after the first one. It informs them about the existing cluster, so they can automatically join it.

Now we can start using the cluster. Let's insert a key pair:

curl -X PUT -d my-value http://127.0.0.1:8001/my-key

and then retrieve it from the database:

curl http://127.0.0.1:8001/my-key

Currently the first node is the leader. If we stop it and then try to query the key again curl will fail, but we can simply change the endpoint to another node and things will work since an automatic failover has taken place:

kill -TERM %1; curl http://127.0.0.1:8002/my-key

Shell

A basic SQLite-like dqlite shell is available in the dqlite-tools package or can be built with:

go install -tags libsqlite3 ./cmd/dqlite
Usage:
  dqlite -s <servers> <database> [command] [flags]

Example usage in the case of the dqlite-demo example listed above:

dqlite -s 127.0.0.1:9001 demo

dqlite> SELECT * FROM model;
my-key|my-value

The shell supports normal SQL queries plus the special .cluster and .leader commands to inspect the cluster members and the current leader.

Documentation

Index

Constants

View Source
const BootstrapID = 0x2dc171858c3155be

BootstrapID is a magic ID that should be used for the fist node in a cluster. Alternatively ID 1 can be used as well.

Variables

This section is empty.

Functions

func ConfigMultiThread added in v1.2.0

func ConfigMultiThread() error

ConfigMultiThread sets the threading mode of SQLite to Multi-thread.

By default go-dqlite configures SQLite to Single-thread mode, because the dqlite engine itself is single-threaded, and enabling Multi-thread or Serialized modes would incur in a performance penality.

If your Go process also uses SQLite directly (e.g. using the github.com/mattn/go-sqlite3 bindings) you might need to switch to Multi-thread mode in order to be thread-safe.

IMPORTANT: It's possible to successfully change SQLite's threading mode only if no SQLite APIs have been invoked yet (e.g. no database has been opened yet). Therefore you'll typically want to call ConfigMultiThread() very early in your process setup. Alternatively you can set the GO_DQLITE_MULTITHREAD environment variable to 1 at process startup, in order to prevent go-dqlite from setting Single-thread mode at all.

func GenerateID added in v1.4.0

func GenerateID(address string) uint64

GenerateID generates a unique ID for a new node, based on a hash of its address and the current time.

func ReconfigureMembership added in v1.6.0

func ReconfigureMembership(dir string, cluster []NodeInfo) error

ReconfigureMembership can be used to recover a cluster whose majority of nodes have died, and therefore has become unavailable.

It forces appending a new configuration to the raft log stored in the given directory, effectively replacing the current configuration.

func ReconfigureMembershipExt added in v1.9.0

func ReconfigureMembershipExt(dir string, cluster []NodeInfo) error

ReconfigureMembershipExt can be used to recover a cluster whose majority of nodes have died, and therefore has become unavailable.

It forces appending a new configuration to the raft log stored in the given directory, effectively replacing the current configuration. In comparision with ReconfigureMembership, this function takes the node role into account and makes use of a dqlite API that supports extending the NodeInfo struct.

Types

type Node added in v1.0.0

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

Node runs a dqlite node.

func New added in v1.0.0

func New(id uint64, address string, dir string, options ...Option) (*Node, error)

New creates a new Node instance.

func (*Node) BindAddress added in v1.0.0

func (s *Node) BindAddress() string

BindAddress returns the network address the node is listening to.

func (*Node) Close added in v1.0.0

func (s *Node) Close() error

Close the server, releasing all resources it created.

func (*Node) Recover added in v1.1.0

func (s *Node) Recover(cluster []NodeInfo) error

Recover a node by forcing a new cluster configuration.

DEPRECATED: Use ReconfigureMembership instead, which does not require instantiating a new Node object.

func (*Node) Start added in v1.0.0

func (s *Node) Start() error

Start serving requests.

type NodeInfo added in v1.0.0

type NodeInfo = client.NodeInfo

NodeInfo is a convenience alias for client.NodeInfo.

type Option added in v1.0.0

type Option func(*options)

Option can be used to tweak node parameters.

func WithBindAddress added in v1.0.0

func WithBindAddress(address string) Option

WithBindAddress sets a custom bind address for the server.

func WithDialFunc

func WithDialFunc(dial client.DialFunc) Option

WithDialFunc sets a custom dial function for the server.

func WithDiskMode added in v1.11.6

func WithDiskMode(disk bool) Option

WithDiskMode enables dqlite disk-mode on the node. WARNING: This is experimental API, use with caution and prepare for data loss. UNSTABLE: Behavior can change in future. NOT RECOMMENDED for production use-cases, use at own risk.

func WithFailureDomain added in v1.7.0

func WithFailureDomain(code uint64) Option

WithFailureDomain sets the code of the failure domain the node belongs to.

func WithNetworkLatency added in v1.0.0

func WithNetworkLatency(latency time.Duration) Option

WithNetworkLatency sets the average one-way network latency.

func WithSnapshotParams added in v1.9.0

func WithSnapshotParams(params SnapshotParams) Option

WithSnapshotParams sets the snapshot parameters of the node.

type SnapshotParams added in v1.11.0

type SnapshotParams = bindings.SnapshotParams

SnapshotParams exposes bindings.SnapshotParams. Used for setting dqlite's snapshot parameters. SnapshotParams.Threshold controls after how many raft log entries a snapshot is taken. The higher this number, the lower the frequency of the snapshots. SnapshotParams.Trailing controls how many raft log entries are retained after taking a snapshot.

Directories

Path Synopsis
cmd
internal

Jump to

Keyboard shortcuts

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