Documentation ¶
Index ¶
- Constants
- Variables
- func GetPools(ctx context.Context, db *mongo.Database) (<-chan PoolResult, error)
- func Setup(ctx context.Context, db *mongo.Database) error
- func UpdatePool(ctx context.Context, db *mongo.Database, pool Pool) error
- type Pool
- func CapacityPoolCreate(ctx context.Context, db *mongo.Database, pool Pool) (Pool, error)
- func GetExpiredPools(ctx context.Context, db *mongo.Database, ts int64) ([]Pool, error)
- func GetNextExpiredPool(ctx context.Context, db *mongo.Database, ts int64) (Pool, error)
- func GetPool(ctx context.Context, db *mongo.Database, id schema.ID) (Pool, error)
- func GetPoolsByOwner(ctx context.Context, db *mongo.Database, owner int64) ([]Pool, error)
- func NewPool(id schema.ID, ownerID int64, sponsorTID int64, nodeIDs []string) Pool
- func (p *Pool) AddCapacity(CUs float64, SUs float64, IPUs float64)
- func (p *Pool) AddWorkload(id schema.ID, CU float64, SU float64, IPv4U float64)
- func (p *Pool) AllowedInPool(nodeID string) bool
- func (p *Pool) RemoveWorkload(id schema.ID, CU float64, SU float64, IPv4U float64)
- func (p *Pool) SyncCurrentCapacity()
- type PoolResult
- type Reservation
- type ReservationData
Constants ¶
const (
// CapacityPoolCollection db collection name
CapacityPoolCollection = "capacity-pools"
)
const (
// CapacityReservationCollection db collection name
CapacityReservationCollection = "capacity-reservations"
)
Variables ¶
var ( // ErrPoolNotFound is returned when looking for a specific pool, which is not // there. ErrPoolNotFound = errors.New("the specified pool could not be found") // ErrReservationNotFound is returned when a reservation with a given ID is not there ErrReservationNotFound = errors.New("the specified reservation was not found") )
Functions ¶
Types ¶
type Pool ¶
type Pool struct { // ID is the id of the pool, which needs to be referenced in workloads // wanting to use this pool to deploy. It can also be used to increase // the pool ID schema.ID `bson:"_id" json:"pool_id"` // CUs and SUs are the `compute unit seconds` and `storage unit seconds`. // These values represent the amount left in the pool when it was last // updated, and do not represent current values (unless the pool just // got updated) Cus float64 `bson:"cus" json:"cus"` Sus float64 `bson:"sus" json:"sus"` IPv4us float64 `bson:"ipv4us" json:"ipv4us"` // node ids on which this pool is applicable, only workloads deployed // on these nodes must be deployed in the pool. NodeIDs []string `bson:"node_ids" json:"node_ids"` // unix timestamp when the counters where last synced. Syncing happens by // deducting the amount of spent CU and SU, since the last sync, from // the pool, and updating this field. LastUpdated int64 `bson:"last_updated" json:"last_updated"` // amount of active CU and SU tied to this pool. This is the amount of // CU and SU that needs to be deducted from the pool. ActiveCU float64 `bson:"active_cu" json:"active_cu"` ActiveSU float64 `bson:"active_su" json:"active_su"` ActiveIPv4U float64 `bson:"active_ipv4" json:"active_ipv4"` // timestamp when either CU or SU expires according to the current capacity // still left and the capacity being used. EmptyAt int64 `bson:"empty_at" json:"empty_at"` // CustomerTid is the threebot id of the pool owner. Only the owner can // assign workloads to the pool CustomerTid int64 `bson:"customer_tid" json:"customer_tid"` // SponsorTid is the original sponsor of the pool when created. SponsorTid int64 `bson:"sponsor_tid" json:"sponsor_tid"` // ActiveWorkloadIDs for this pool, this list contains only unique entries ActiveWorkloadIDs []schema.ID `bson:"active_workload_ids" json:"active_workload_ids"` }
Pool is an abstract representation of an amount of capacity purchased by someone. The system must update the amount of CU and SU that is in use from the pool when a workload tied to pool is created or otherwise changes state. The pool is not thread safe - it is the responsibility of the consumer of this struct to ensure only 1 access at a time is performed.
func CapacityPoolCreate ¶
CapacityPoolCreate save new capacity pool to the database
func GetExpiredPools ¶
GetExpiredPools returns a list of all expired pools
func GetNextExpiredPool ¶
GetNextExpiredPool gets the first pool to expire after the given timestamp
func GetPoolsByOwner ¶
GetPoolsByOwner gets all pools for an owner
func NewPool ¶
NewPool sets up a new pool, ready to use, with the given data.
If id is 0, it will be set on first save
func (*Pool) AddCapacity ¶
AddCapacity adds new capacity to the pool
func (*Pool) AddWorkload ¶
AddWorkload adds the used CU and SU of a deployed workload to the currently active CU and SU of the pool, and adds the id to the actively used ids.
func (*Pool) AllowedInPool ¶
AllowedInPool verifies that a nodeID is in the pool.
func (*Pool) RemoveWorkload ¶
RemoveWorkload remove the used CU and SU of a deployed workload to the currently active CU and SU of the pool, and removes the id from the actively used id's.
func (*Pool) SyncCurrentCapacity ¶
func (p *Pool) SyncCurrentCapacity()
SyncCurrentCapacity recalculate the current capacity in the pool
type PoolResult ¶ added in v0.4.8
PoolResult wrapper object that holds errors
type Reservation ¶
type Reservation struct { ID schema.ID `bson:"_id" json:"id"` JSON string `bson:"json" json:"json"` DataReservation ReservationData `bson:"data_reservation" json:"data_reservation"` CustomerTid int64 `bson:"customer_tid" json:"customer_tid"` CustomerSignature string `bson:"customer_signature" json:"customer_signature"` SponsorTid int64 `bson:"sponsor_tid" json:"sponsor_tid"` SponsorSignature string `bson:"sponsor_signature" json:"sponsor_signature"` }
Reservation is a reservation type to create a new capacity pool, or to add capacity to an existing pool. Ownership of a new pool is tied to the person who signs the request to set up the pool. Once a pool is set up, anyone can top it up with additional capacity. Even though there is no restriction of who can add capacity to the pool, only the owner can assign workloads to it.
This type is based on the `generated.Reservation` type. The main reason for this being a separate type, is that the aforementioned type is actually not flexible, and strongly tied to regular workloads. One solution would be to make a pool a type of workload, but this would be a serious hack.
Furthermore, note that some fields have been stripped. Reason is, that a capacity pool is only meant to serve as an abstract concept, internal to the explorer, and later the farmer threebot. As such, there are no dedicated signature fields. Other workload specific info is also stripped. Note that the way of signing the reservation is kept. While this method is questionable to say the least, it does mean that we will have a much easier time if we decide to merge the 2 reservation types in the future, which we should still do.
func CapacityReservationCreate ¶
func CapacityReservationCreate(ctx context.Context, db *mongo.Database, reservation Reservation) (Reservation, error)
CapacityReservationCreate saves a new capacity reservation to the database
func CapacityReservationGet ¶
func CapacityReservationGet(ctx context.Context, db *mongo.Database, id schema.ID) (Reservation, error)
CapacityReservationGet loads a capacity reservation with the given id
func (*Reservation) Verify ¶
func (pr *Reservation) Verify(pk string) error
Verify the provided signature against the reservation JSON, with the provided key. The key is the public key of the user, as a hex string
func (*Reservation) VerifyCustomerAndSponsor ¶ added in v0.5.0
func (pr *Reservation) VerifyCustomerAndSponsor(customerPk, sponsorPk string) error
VerifyCustomerAndSponsor the provided signatures of customer and sponsor against the reservation JSON, with the provided customerPk The key is the public key of the user, as a hex string sponsorPk The key is the public key of the sponsor, as a hex string
func (*Reservation) VerifySponsor ¶ added in v0.5.0
func (pr *Reservation) VerifySponsor(pk string) error
VerifySponsor the provided sponsor signature against the reservation JSON, with the provided key. The key is the public key of the sponsor, as a hex string
type ReservationData ¶
type ReservationData struct { PoolID int64 `bson:"pool_id" json:"pool_id"` CUs uint64 `bson:"cus" json:"cus"` SUs uint64 `bson:"sus" json:"sus"` IPv4Us uint64 `bson:"ipv4us" json:"ipv4us"` NodeIDs []string `bson:"node_ids" json:"node_ids"` Currencies []string `bson:"currencies" json:"currencies"` }
ReservationData is the actual data sent in a capacity pool reservation. If PoolID is a non-zero value, this reservation will add the requested capacity to the existing pool with the given ID.
Although CU and SU values for workloads can be (and likely will be) floating points, we only allow purchasing full units. Since such a unit is actually very small, this is not a problem for over purchasing, and it simplifies some stuff on our end.