database

package
v0.7.8 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2022 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// KiB kilobyte
	KiB = 1024
	// MiB megabyte
	MiB = 1024 * KiB
	// GiB gigabyte
	GiB = 1024 * MiB
	// TiB terabyte
	TiB = 1024 * GiB
)
View Source
const (
	// MaxNumFailedAttempts defines the maximum number of failed attempts at
	// which we would still attempt to pin a skylink.
	MaxNumFailedAttempts = 5
)
View Source
const (
	// MongoDefaultTimeout is our default timeout for database operations.
	MongoDefaultTimeout = 30 * time.Second
)

Variables

View Source
var (
	// ErrInvalidSkylink is returned when a client call supplies an invalid
	// skylink hash.
	ErrInvalidSkylink = errors.New("invalid skylink")
	// ErrSkylinkExists is returned when we try to create a skylink that already
	// exists.
	ErrSkylinkExists = errors.New("skylink already exists")
	// ErrSkylinkNotExist is returned when we try to get a skylink that doesn't
	// exist.
	ErrSkylinkNotExist = errors.New("skylink does not exist")
	// ErrNoSkylinksLocked is returned when we try to lock underpinned skylinks
	// for pinning but we fail to do so.
	ErrNoSkylinksLocked = errors.New("no skylinks locked")
	// ErrNoUnderpinnedSkylinks is returned when all skylinks in the database
	// are either sufficiently pinned or pinned by the local server.
	ErrNoUnderpinnedSkylinks = errors.New("no underpinned skylinks found")
	// LockDuration defines the duration of a database lock. We lock skylinks
	// while we are trying to pin them to a new server. The goal is to only
	// allow a single server to pin a given skylink at a time.
	LockDuration = 7 * time.Hour
)
View Source
var (
	// ErrCtxFailedToConnect is the context we add to an error when we fail to
	// connect to the db.
	ErrCtxFailedToConnect = "failed to connect to the db"
)
View Source
var (
	// ErrServerLoadNotFound is returned when we don't have a record for the
	// server's load.
	ErrServerLoadNotFound = errors.New("server load not found")
)

Functions

func IsNoSkylinksNeedPinning

func IsNoSkylinksNeedPinning(err error) bool

IsNoSkylinksNeedPinning returns true when the given error indicates that there are no more skylinks that need to be pinned by the current server.

func SkylinkFromString

func SkylinkFromString(s string) (skymodules.Skylink, error)

SkylinkFromString converts a string to a Skylink.

Types

type DB

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

DB holds a connection to the database, as well as helpful shortcuts to collections and utilities.

func New

func New(ctx context.Context, creds DBCredentials, logger logger.Logger) (*DB, error)

New creates a new database connection.

func NewCustomDB

func NewCustomDB(ctx context.Context, dbName string, creds DBCredentials, logger logger.Logger) (*DB, error)

NewCustomDB creates a new database connection to a database with a custom name.

func (db *DB) AddServerForSkylinks(ctx context.Context, skylinks []string, server string, markPinned bool) error

AddServerForSkylinks adds a new server to the list of servers known to be pinning these skylinks. If a skylink does not already exist in the database it will be inserted. This operation is idempotent.

The `markPinned` flag sets the `unpin` field of a skylink to false when raised but it doesn't set it to false when not raised. The reason for that is that it accommodates a specific use case - adding a server to the list of pinners of a given skylink will set the pinned field to true is we are doing that because we know that a user is pinning it but not so if we are running a server sweep and documenting which skylinks are pinned by this server.

func (*DB) ConfigValue added in v0.1.0

func (db *DB) ConfigValue(ctx context.Context, key string) (string, error)

ConfigValue returns a cluster-wide configuration value, stored in the database.

func (db *DB) CreateSkylink(ctx context.Context, skylink skymodules.Skylink, server string) (Skylink, error)

CreateSkylink inserts a new skylink into the DB. Returns an error if it already exists.

func (*DB) DeleteServerLoad added in v0.6.0

func (db *DB) DeleteServerLoad(ctx context.Context, server string) error

DeleteServerLoad removes the load info for this server. We should use this when we mark a server as dead.

func (db *DB) DeleteSkylink(ctx context.Context, skylink skymodules.Skylink) error

DeleteSkylink removes the skylink from the database.

func (*DB) Disconnect

func (db *DB) Disconnect(ctx context.Context) error

Disconnect closes the connection to the database in an orderly fashion.

func (*DB) FindAndLockUnderpinned

func (db *DB) FindAndLockUnderpinned(ctx context.Context, server string, skipSkylinks []string, minPinners int) (skymodules.Skylink, error)

FindAndLockUnderpinned fetches and locks a single underpinned skylink from the database. The method selects only skylinks which are not pinned by the given server.

The MongoDB query is this:

db.getCollection('skylinks').find({
    "pinned": { "$ne": false },
    "$expr": { "$lt": [{ "$size": "$servers" }, 2 ]},
    "servers": { "$nin": [ "ro-tex.siasky.ivo.NOPE" ]},
	"skylink": {"$nin": [ skipSkylinks ]},
    "$or": [
        { "lock_expires" : { "$exists": false }},
        { "lock_expires" : { "$lt": new Date() }}
    ],
	"failed_attempts": {"$not": {"$gt": MaxNumFailedAttempts}},
})
func (db *DB) FindSkylink(ctx context.Context, skylink skymodules.Skylink) (Skylink, error)

FindSkylink fetches a skylink from the DB.

func (*DB) Hello added in v0.6.1

func (db *DB) Hello(ctx context.Context) (*Hello, error)

Hello returns some status information about the local DB node.

func (*DB) MarkFailedAttempt added in v0.7.1

func (db *DB) MarkFailedAttempt(ctx context.Context, skylink skymodules.Skylink) error

MarkFailedAttempt notes that we failed to pin this skylink.

func (*DB) MarkPinned

func (db *DB) MarkPinned(ctx context.Context, skylink skymodules.Skylink) error

MarkPinned marks a skylink as pinned (or no longer unpinned), meaning that Pinner should make sure it's pinned by the minimum number of servers.

func (*DB) MarkUnpinned

func (db *DB) MarkUnpinned(ctx context.Context, skylink skymodules.Skylink) error

MarkUnpinned marks a skylink as unpinned, meaning that all servers should stop pinning it.

func (*DB) NumberSessionsInProgress added in v0.6.1

func (db *DB) NumberSessionsInProgress() int

NumberSessionsInProgress returns the number of sessions that have been started for this client but have not been closed (i.e. EndSession has not been called).

func (*DB) Ping

func (db *DB) Ping(ctx context.Context) error

Ping sends a ping command to verify that the client can connect to the DB and specifically to the primary.

func (*DB) RemoveServer added in v0.6.0

func (db *DB) RemoveServer(ctx context.Context, server string) (int64, error)

RemoveServer removes the server as pinner from all skylinks in the database. Returns the number of skylinks from which the server was removed as pinner.

func (db *DB) RemoveServerFromSkylinks(ctx context.Context, skylinks []string, server string) error

RemoveServerFromSkylinks removes a server from the list of servers known to be pinning these skylinks. If a skylink does not exist in the database it will not be inserted.

func (*DB) ResetFailedAttempts added in v0.7.1

func (db *DB) ResetFailedAttempts(ctx context.Context, skylink skymodules.Skylink) error

ResetFailedAttempts notes that we failed to pin this skylink.

func (*DB) ServerLoad added in v0.6.0

func (db *DB) ServerLoad(ctx context.Context, server string) (int64, error)

ServerLoad returns the server load stored in the database.

func (*DB) ServerLoadPosition added in v0.6.0

func (db *DB) ServerLoadPosition(ctx context.Context, server string) (int, int, error)

ServerLoadPosition returns the position of the server in the list of servers based on its load. It also returns the total number of servers.

func (db *DB) ServersForSkylink(ctx context.Context, skylink skymodules.Skylink) ([]string, error)

ServersForSkylink returns a list of servers pinning the given skylink according to the database.

func (*DB) SetConfigValue added in v0.1.0

func (db *DB) SetConfigValue(ctx context.Context, key, value string) error

SetConfigValue updates a cluster-wide configuration value, stored in the database.

func (*DB) SetServerLoad added in v0.6.0

func (db *DB) SetServerLoad(ctx context.Context, server string, load int64) error

SetServerLoad stores the load of this server in the database.

func (*DB) SkylinksForServer added in v0.1.0

func (db *DB) SkylinksForServer(ctx context.Context, server string) ([]string, error)

SkylinksForServer returns a list of skylinks pinned by the given server according to the database. Note that this list doesn't necessarily match the list of skylink the server is actually pinning, it's the list the database knows of.

func (db *DB) UnlockSkylink(ctx context.Context, skylink skymodules.Skylink, server string) error

UnlockSkylink removes the lock on the skylink put while we're trying to pin it to a new server.

type DBCredentials

type DBCredentials struct {
	User     string
	Password string
	Host     string
	Port     string
}

DBCredentials is a helper struct that binds together all values needed for establishing a DB connection.

type Hello added in v0.6.1

type Hello struct {
	Hosts              []string  `bson:"hosts" json:"hosts"`
	SetName            string    `bson:"setName" json:"setName"`
	IsWriteablePrimary bool      `bson:"isWriteablePrimary" json:"isWriteablePrimary"`
	Secondary          bool      `bson:"secondary" json:"secondary"`
	Primary            string    `bson:"primary" json:"primary"`
	Me                 string    `bson:"me" json:"me"`
	LocalTime          time.Time `bson:"localTime" json:"localTime"`
	OK                 float32   `bson:"ok" json:"OK"`
}

Hello is a selection of the information returned by MongoDB as response to db.hello(), i.e. this is some basic information about the DB node.

type Skylink struct {
	ID      primitive.ObjectID `bson:"_id,omitempty"`
	Skylink string             `bson:"skylink"`
	Servers []string           `bson:"servers"`
	// Pinned tells us that at least one user is actively pinning this
	// skylink and we want to keep it alive. If Pinned is false then all
	// servers should actively unpin the skylink and stop paying for it.
	// This is not yet implemented.
	Pinned         bool      `bson:"pinned"`
	LockedBy       string    `bson:"locked_by"`
	LockExpires    time.Time `bson:"lock_expires"`
	FailedAttempts uint      `bson:"failed_attempts,omitempty"`
}

Skylink represents a skylink object in the DB.

type SkylinkOnly added in v0.6.1

type SkylinkOnly struct {
	Skylink string `bson:"skylink"`
}

SkylinkOnly is a helper type which we can use when we want to grab a skylink from the DB.s

Jump to

Keyboard shortcuts

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