Documentation
¶
Index ¶
- Constants
- Variables
- func IsNoSkylinksNeedPinning(err error) bool
- func SkylinkFromString(s string) (skymodules.Skylink, error)
- type DB
- func (db *DB) AddServerForSkylinks(ctx context.Context, skylinks []string, server string, markPinned bool) error
- func (db *DB) ConfigValue(ctx context.Context, key string) (string, error)
- func (db *DB) CreateSkylink(ctx context.Context, skylink skymodules.Skylink, server string) (Skylink, error)
- func (db *DB) DeleteServerLoad(ctx context.Context, server string) error
- func (db *DB) DeleteSkylink(ctx context.Context, skylink skymodules.Skylink) error
- func (db *DB) Disconnect(ctx context.Context) error
- func (db *DB) FindAndLockUnderpinned(ctx context.Context, server string, skipSkylinks []string, minPinners int) (skymodules.Skylink, error)
- func (db *DB) FindSkylink(ctx context.Context, skylink skymodules.Skylink) (Skylink, error)
- func (db *DB) Hello(ctx context.Context) (*Hello, error)
- func (db *DB) MarkFailedAttempt(ctx context.Context, skylink skymodules.Skylink) error
- func (db *DB) MarkPinned(ctx context.Context, skylink skymodules.Skylink) error
- func (db *DB) MarkUnpinned(ctx context.Context, skylink skymodules.Skylink) error
- func (db *DB) NumberSessionsInProgress() int
- func (db *DB) Ping(ctx context.Context) error
- func (db *DB) RemoveServer(ctx context.Context, server string) (int64, error)
- func (db *DB) RemoveServerFromSkylinks(ctx context.Context, skylinks []string, server string) error
- func (db *DB) ResetFailedAttempts(ctx context.Context, skylink skymodules.Skylink) error
- func (db *DB) ServerLoad(ctx context.Context, server string) (int64, error)
- func (db *DB) ServerLoadPosition(ctx context.Context, server string) (int, int, error)
- func (db *DB) ServersForSkylink(ctx context.Context, skylink skymodules.Skylink) ([]string, error)
- func (db *DB) SetConfigValue(ctx context.Context, key, value string) error
- func (db *DB) SetServerLoad(ctx context.Context, server string, load int64) error
- func (db *DB) SkylinksForServer(ctx context.Context, server string) ([]string, error)
- func (db *DB) UnlockSkylink(ctx context.Context, skylink skymodules.Skylink, server string) error
- type DBCredentials
- type Hello
- type Skylink
- type SkylinkOnly
Constants ¶
const ( // KiB kilobyte KiB = 1024 // MiB megabyte MiB = 1024 * KiB // GiB gigabyte GiB = 1024 * MiB // TiB terabyte TiB = 1024 * GiB )
const ( // MaxNumFailedAttempts defines the maximum number of failed attempts at // which we would still attempt to pin a skylink. MaxNumFailedAttempts = 5 )
const ( // MongoDefaultTimeout is our default timeout for database operations. MongoDefaultTimeout = 30 * time.Second )
Variables ¶
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 )
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" )
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 ¶
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 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) AddServerForSkylinks ¶ added in v0.5.0
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
ConfigValue returns a cluster-wide configuration value, stored in the database.
func (*DB) CreateSkylink ¶
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
DeleteServerLoad removes the load info for this server. We should use this when we mark a server as dead.
func (*DB) DeleteSkylink ¶ added in v0.6.1
DeleteSkylink removes the skylink from the database.
func (*DB) Disconnect ¶
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) FindSkylink ¶
FindSkylink fetches a skylink from the DB.
func (*DB) MarkFailedAttempt ¶ added in v0.7.1
MarkFailedAttempt notes that we failed to pin this skylink.
func (*DB) MarkPinned ¶
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 ¶
MarkUnpinned marks a skylink as unpinned, meaning that all servers should stop pinning it.
func (*DB) NumberSessionsInProgress ¶ added in v0.6.1
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 ¶
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
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) RemoveServerFromSkylinks ¶ added in v0.5.0
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
ResetFailedAttempts notes that we failed to pin this skylink.
func (*DB) ServerLoad ¶ added in v0.6.0
ServerLoad returns the server load stored in the database.
func (*DB) ServerLoadPosition ¶ added in v0.6.0
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) ServersForSkylink ¶ added in v0.6.1
ServersForSkylink returns a list of servers pinning the given skylink according to the database.
func (*DB) SetConfigValue ¶ added in v0.1.0
SetConfigValue updates a cluster-wide configuration value, stored in the database.
func (*DB) SetServerLoad ¶ added in v0.6.0
SetServerLoad stores the load of this server in the database.
func (*DB) SkylinksForServer ¶ added in v0.1.0
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) UnlockSkylink ¶
UnlockSkylink removes the lock on the skylink put while we're trying to pin it to a new server.
type DBCredentials ¶
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 ¶
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