Documentation ¶
Overview ¶
package frunk provides a distributed SQLite instance.
Distributed consensus is provided via the Raft algorithm.
Index ¶
- Constants
- Variables
- func JoinAllowed(raftDir string) (bool, error)
- func NumPeers(raftDir string) (int, error)
- type BackupFormat
- type ClusterState
- type Connection
- func (c *Connection) AbortTransaction() error
- func (c *Connection) Close() error
- func (c *Connection) Execute(ex *ExecuteRequest) (*ExecuteResponse, error)
- func (c *Connection) ExecuteOrAbort(ex *ExecuteRequest) (resp *ExecuteResponse, retErr error)
- func (c *Connection) IdleTimedOut() bool
- func (c *Connection) Query(qr *QueryRequest) (*QueryResponse, error)
- func (c *Connection) Restore(dbConn *sdb.Conn, s *Store)
- func (c *Connection) SetLastUsedNow()
- func (c *Connection) Stats() (interface{}, error)
- func (c *Connection) String() string
- func (c *Connection) TransactionActive() bool
- func (c *Connection) TxTimedOut() bool
- type ConnectionOptions
- type ConsistencyLevel
- type DBConfig
- type ExecuteRequest
- type ExecuteResponse
- type Listener
- type Peer
- type QueryRequest
- type QueryResponse
- type RaftResponse
- type SequenceChunk
- type SequenceChunkResponse
- type Server
- type Servers
- type Store
- func (s *Store) Addr() string
- func (s *Store) Apply(l *raft.Log) interface{}
- func (s *Store) Backup(leader bool, fmt BackupFormat, dst io.Writer) error
- func (s *Store) Close(wait bool) error
- func (s *Store) Connect(opt *ConnectionOptions) (*Connection, error)
- func (s *Store) Connection(id uint64) (*Connection, bool)
- func (s *Store) DeregisterObserver(o *raft.Observer)
- func (s *Store) Exec(query string) (*ExecuteResponse, error)
- func (s *Store) ExecEx(query string, atomic bool) (*ExecuteResponse, error)
- func (s *Store) ExecuteEx(ex *ExecuteRequest) (*ExecuteResponse, error)
- func (s *Store) ExecuteOrAbort(ex *ExecuteRequest) (resp *ExecuteResponse, retErr error)
- func (s *Store) Get(key []byte) ([]byte, error)
- func (s *Store) GetSequenceChunk(sequenceName string) (*SequenceChunkResponse, error)
- func (s *Store) ID() string
- func (s *Store) IsLeader() bool
- func (s *Store) Join(id, addr string, metadata map[string]string) error
- func (s *Store) LeaderAddr() string
- func (s *Store) LeaderID() (string, error)
- func (s *Store) Metadata(id, key string) string
- func (s *Store) NextSequenceValueById(sequenceName string) (uint64, error)
- func (s *Store) Nodes() ([]*Server, error)
- func (s *Store) Open(enableSingle bool, peers ...raft.Server) error
- func (s *Store) Path() string
- func (s *Store) Query(query string) (*QueryResponse, error)
- func (s *Store) QueryEx(qr *QueryRequest) (*QueryResponse, error)
- func (s *Store) RegisterObserver(o *raft.Observer)
- func (s *Store) Remove(id string) error
- func (s *Store) Restore(rc io.ReadCloser) error
- func (s *Store) SequenceIndexById(sequenceName string) (uint64, error)
- func (s *Store) Set(key, value []byte) error
- func (s *Store) SetMetadata(md map[string]string) error
- func (s *Store) Snapshot() (raft.FSMSnapshot, error)
- func (s *Store) State() ClusterState
- func (s *Store) Stats() (map[string]interface{}, error)
- func (s *Store) WaitForApplied(timeout time.Duration) error
- func (s *Store) WaitForAppliedIndex(idx uint64, timeout time.Duration) error
- func (s *Store) WaitForLeader(timeout time.Duration) (string, error)
- type StoreConfig
- type Transport
- type TxStateChange
Constants ¶
const ( SequenceRangeSize = 1000 SequencePartitions = 5 SequencePreretrieve = 50 )
Variables ¶
var ( // ErrNotLeader is returned when a node attempts to execute a leader-only // operation. ErrNotLeader = errors.New("not leader") // ErrOpenTimeout is returned when the Store does not apply its initial // logs within the specified time. ErrOpenTimeout = errors.New("timeout waiting for initial logs application") // ErrInvalidBackupFormat is returned when the requested backup format // is not valid. ErrInvalidBackupFormat = errors.New("invalid backup format") // ErrTransactionActive is returned when an operation is blocked by an // active transaction. ErrTransactionActive = errors.New("transaction in progress") // ErrDefaultConnection is returned when an attempt is made to delete the // default connection. ErrDefaultConnection = errors.New("cannot delete default connection") // ErrConnectionDoesNotExist is returned when an operation is attempted // on a non-existent connection. This can happen if the connection // was previously open but is now closed. ErrConnectionDoesNotExist = errors.New("connection does not exist") // ErrStoreInvalidState is returned when a Store is in an invalid // state for the requested operation. ErrStoreInvalidState = errors.New("store not in valid state") )
Functions ¶
func JoinAllowed ¶
JoinAllowed returns whether the config files within raftDir indicate that the node can join a cluster.
Types ¶
type BackupFormat ¶
type BackupFormat int
BackupFormat represents the backup formats supported by the Store.
const ( // BackupSQL is dump of the database in SQL text format. BackupSQL BackupFormat = iota // BackupBinary is a copy of the SQLite database file. BackupBinary )
type ClusterState ¶
type ClusterState int
ClusterState defines the possible Raft states the current node can be in
const ( Leader ClusterState = iota Follower Candidate Shutdown Unknown )
Represents the Raft cluster states
type Connection ¶
type Connection struct { ID uint64 `json:"id,omitempty"` // Connection ID, used as a handle by clients. CreatedAt time.Time `json:"created_at,omitempty"` LastUsedAt time.Time `json:"last_used_at,omitempty"` IdleTimeout time.Duration `json:"idle_timeout,omitempty"` TxTimeout time.Duration `json:"tx_timeout,omitempty"` TxStartedAt time.Time `json:"tx_started_at,omitempty"` // contains filtered or unexported fields }
Connection is a connection to the database.
func NewConnection ¶
NewConnection returns a connection to the database.
func (*Connection) AbortTransaction ¶
func (c *Connection) AbortTransaction() error
AbortTransaction aborts -- rolls back -- any active transaction. Calling code should know exactly what it is doing if it decides to call this function. It can be used to clean up any dangling state that may result from certain error scenarios.
func (*Connection) Close ¶
func (c *Connection) Close() error
Close closes the connection via consensus.
func (*Connection) Execute ¶
func (c *Connection) Execute(ex *ExecuteRequest) (*ExecuteResponse, error)
Execute executes queries that return no rows, but do modify the database.
func (*Connection) ExecuteOrAbort ¶
func (c *Connection) ExecuteOrAbort(ex *ExecuteRequest) (resp *ExecuteResponse, retErr error)
ExecuteOrAbort executes the requests, but aborts any active transaction on the underlying database in the case of any error.
func (*Connection) IdleTimedOut ¶
func (c *Connection) IdleTimedOut() bool
IdleTimedOut returns if the connection has not been active in the idle time.
func (*Connection) Query ¶
func (c *Connection) Query(qr *QueryRequest) (*QueryResponse, error)
Query executes queries that return rows, and do not modify the database.
func (*Connection) Restore ¶
func (c *Connection) Restore(dbConn *sdb.Conn, s *Store)
Restore prepares a partially ready connection.
func (*Connection) SetLastUsedNow ¶
func (c *Connection) SetLastUsedNow()
SetLastUsedNow marks the connection as being used now.
func (*Connection) Stats ¶
func (c *Connection) Stats() (interface{}, error)
Stats returns the status of the connection.
func (*Connection) String ¶
func (c *Connection) String() string
String implements the Stringer interface on the Connection.
func (*Connection) TransactionActive ¶
func (c *Connection) TransactionActive() bool
TransactionActive returns whether a transaction is active on the connection.
func (*Connection) TxTimedOut ¶
func (c *Connection) TxTimedOut() bool
TxTimedOut returns if the transaction has been open, without activity in transaction-idle time.
type ConnectionOptions ¶
ConnectionOptions controls connection behaviour.
type ConsistencyLevel ¶
type ConsistencyLevel int
ConsistencyLevel represents the available read consistency levels.
const ( None ConsistencyLevel = iota Weak Strong )
Represents the available consistency levels.
type DBConfig ¶
type DBConfig struct { DSN string // Any custom DSN Memory bool // Whether the database is in-memory only. }
DBConfig represents the configuration of the underlying SQLite database.
func NewDBConfig ¶
NewDBConfig returns a new DB config instance.
type ExecuteRequest ¶
ExecuteRequest represents a query that returns now rows, but does modify the database.
type ExecuteResponse ¶
type ExecuteResponse struct { Results []*sdb.Result Time float64 Raft RaftResponse }
ExecuteResponse encapsulates a response to an execute.
type Listener ¶
type Listener interface { net.Listener Dial(address string, timeout time.Duration) (net.Conn, error) }
Listener is the interface Raft-compatible network layers should implement.
type QueryRequest ¶
type QueryRequest struct { Queries []string Timings bool Atomic bool Lvl ConsistencyLevel }
QueryRequest represents a query that returns rows, and does not modify the database.
type QueryResponse ¶
type QueryResponse struct { Rows []*sdb.Rows Time float64 Raft *RaftResponse }
QueryResponse encapsulates a response to a query.
type RaftResponse ¶
type RaftResponse struct { Index uint64 `json:"index,omitempty"` NodeID string `json:"node_id,omitempty"` }
RaftResponse is the Raft metadata that will be included with responses, if the associated request modified the Raft log.
type SequenceChunk ¶
type SequenceChunk struct { Store // contains filtered or unexported fields }
func (*SequenceChunk) GetSequenceIndex ¶
func (sequence *SequenceChunk) GetSequenceIndex() uint64
func (*SequenceChunk) Next ¶
func (sequence *SequenceChunk) Next() (uint64, error)
type SequenceChunkResponse ¶
type Store ¶
type Store struct { SnapshotThreshold uint64 SnapshotInterval time.Duration HeartbeatTimeout time.Duration ApplyTimeout time.Duration // contains filtered or unexported fields }
Store is a SQLite database, where all changes are made via Raft consensus.
func (*Store) Backup ¶
Backup writes a snapshot of the underlying database to dst
If leader is true, this operation is performed with a read consistency level equivalent to "weak". Otherwise no guarantees are made about the read consistency level.
func (*Store) Close ¶
Close closes the store. If wait is true, waits for a graceful shutdown. Once closed, a Store may not be re-opened.
func (*Store) Connect ¶
func (s *Store) Connect(opt *ConnectionOptions) (*Connection, error)
Connect returns a new connection to the database. Changes made to the database through this connection are applied via the Raft consensus system. The Store must have been opened first. Must be called on the leader or an error will we returned.
Any connection returned by this call are READ_COMMITTED isolated from all other connections, including the connection built-in to the Store itself.
func (*Store) Connection ¶
func (s *Store) Connection(id uint64) (*Connection, bool)
Connection returns the connection for the given ID.
func (*Store) DeregisterObserver ¶
DeregisterObserver deregisters an observer of Raft events
func (*Store) Exec ¶
func (s *Store) Exec(query string) (*ExecuteResponse, error)
Exec executes a write-only query against the database.
func (*Store) ExecEx ¶
func (s *Store) ExecEx(query string, atomic bool) (*ExecuteResponse, error)
ExecEx executes a write-only query against the database. With the option of being atomic or not.
func (*Store) ExecuteEx ¶
func (s *Store) ExecuteEx(ex *ExecuteRequest) (*ExecuteResponse, error)
ExecuteEx executes queries that return no rows, but do modify the database. Changes made to the database through this call are applied via the Raft consensus system. The Store must have been opened first. Must be called on the leader or an error will we returned. The changes are made using the database connection built-in to the Store.
func (*Store) ExecuteOrAbort ¶
func (s *Store) ExecuteOrAbort(ex *ExecuteRequest) (resp *ExecuteResponse, retErr error)
ExecuteOrAbort executes the requests, but aborts any active transaction on the underlying database in the case of any error. Any changes are made using the database connection built-in to the Store.
func (*Store) GetSequenceChunk ¶
func (s *Store) GetSequenceChunk(sequenceName string) (*SequenceChunkResponse, error)
func (*Store) Join ¶
Join joins a node, identified by id and located at addr, to this store. The node must be ready to respond to Raft communications at that address.
func (*Store) LeaderAddr ¶
LeaderAddr returns the Raft address of the current leader. Returns a blank string if there is no leader.
func (*Store) LeaderID ¶
LeaderID returns the node ID of the Raft leader. Returns a blank string if there is no leader, or an error.
func (*Store) NextSequenceValueById ¶
func (*Store) Open ¶
Open opens the store. If enableSingle is set, and there are no existing peers, then this node becomes the first node, and therefore leader, of the cluster.
func (*Store) Query ¶
func (s *Store) Query(query string) (*QueryResponse, error)
Query allows read-only queries to be issued to the database.
func (*Store) QueryEx ¶
func (s *Store) QueryEx(qr *QueryRequest) (*QueryResponse, error)
QueryEx executes queries that return rows, and do not modify the database. The queries are made using the database connection built-in to the Store. Depending on the read consistency requested, it may or may not need to be called on the leader.
func (*Store) RegisterObserver ¶
RegisterObserver registers an observer of Raft events
func (*Store) Restore ¶
func (s *Store) Restore(rc io.ReadCloser) error
Restore restores the node to a previous state.
func (*Store) SequenceIndexById ¶
func (*Store) SetMetadata ¶
SetMetadata adds the metadata md to any existing metadata for this node.
func (*Store) Snapshot ¶
func (s *Store) Snapshot() (raft.FSMSnapshot, error)
Snapshot returns a snapshot of the store. The caller must ensure that no Raft transaction is taking place during this call. Hashicorp Raft guarantees that this function will not be called concurrently with Apply.
func (*Store) State ¶
func (s *Store) State() ClusterState
State returns the current node's Raft state
func (*Store) WaitForApplied ¶
WaitForApplied waits for all Raft log entries to to be applied to the underlying database.
func (*Store) WaitForAppliedIndex ¶
WaitForAppliedIndex blocks until a given log index has been applied, or the timeout expires.
type StoreConfig ¶
type StoreConfig struct { DBConf *DBConfig // The DBConfig object for this Store. Dir string // The working directory for raft. Tn Transport // The underlying Transport for raft. ID string // Node ID. Logger *log.Logger // The logger to use to log stuff. }
StoreConfig represents the configuration of the underlying Store.
type Transport ¶
type Transport struct {
// contains filtered or unexported fields
}
Transport is the network service provided to Raft, and wraps a Listener.
func NewTransport ¶
NewTransport returns an initialized Transport.
type TxStateChange ¶
type TxStateChange struct {
// contains filtered or unexported fields
}
TxStateChange is a helper that detects when the transaction state on a connection changes.
func NewTxStateChange ¶
func NewTxStateChange(c *Connection) *TxStateChange
NewTxStateChange returns an initialized TxStateChange
func (*TxStateChange) CheckAndSet ¶
func (t *TxStateChange) CheckAndSet()
CheckAndSet sets whether a transaction has begun or ended on the connection since the TxStateChange was created. Once CheckAndSet has been called, this function will panic if called a second time.