-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from SkynetLabs/ivo/limit_repin
Limit repinning underpinned skylinks to underutilized servers
- Loading branch information
Showing
18 changed files
with
650 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package database | ||
|
||
import ( | ||
"context" | ||
"gitlab.com/NebulousLabs/errors" | ||
"go.mongodb.org/mongo-driver/bson" | ||
"go.mongodb.org/mongo-driver/mongo" | ||
"go.mongodb.org/mongo-driver/mongo/options" | ||
) | ||
|
||
var ( | ||
// ErrServerLoadNotFound is returned when we don't have a record for the | ||
// server's load. | ||
ErrServerLoadNotFound = errors.New("server load not found") | ||
) | ||
|
||
// DeleteServerLoad removes the load info for this server. We should use this | ||
// when we mark a server as dead. | ||
func (db *DB) DeleteServerLoad(ctx context.Context, server string) error { | ||
filter := bson.M{"server_name": server} | ||
dr, err := db.staticDB.Collection(collServerLoad).DeleteOne(ctx, filter) | ||
if err == mongo.ErrNoDocuments || dr.DeletedCount == 0 { | ||
return ErrServerLoadNotFound | ||
} | ||
return nil | ||
} | ||
|
||
// ServerLoad returns the server load stored in the database. | ||
func (db *DB) ServerLoad(ctx context.Context, server string) (int64, error) { | ||
filter := bson.M{"server_name": server} | ||
sr := db.staticDB.Collection(collServerLoad).FindOne(ctx, filter) | ||
if sr.Err() == mongo.ErrNoDocuments { | ||
return 0, ErrServerLoadNotFound | ||
} | ||
if sr.Err() != nil { | ||
return 0, sr.Err() | ||
} | ||
res := struct { | ||
Load int64 | ||
}{} | ||
err := sr.Decode(&res) | ||
if err != nil { | ||
return 0, err | ||
} | ||
return res.Load, nil | ||
} | ||
|
||
// 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) ServerLoadPosition(ctx context.Context, server string) (int, int, error) { | ||
// Fetch all server loads, order by load in descending order. | ||
opts := &options.FindOptions{ | ||
Sort: bson.M{"load": -1}, | ||
} | ||
c, err := db.staticDB.Collection(collServerLoad).Find(ctx, bson.M{}, opts) | ||
if err != nil { | ||
return 0, 0, err | ||
} | ||
found := false | ||
position := 0 | ||
cur := struct { | ||
ServerName string `bson:"server_name"` | ||
Load int64 `bson:"load"` | ||
}{} | ||
// Loop over all server loads until we find the one we need. | ||
for c.Next(ctx) { | ||
err = c.Decode(&cur) | ||
if err != nil { | ||
return 0, 0, err | ||
} | ||
if cur.ServerName == server { | ||
found = true | ||
break | ||
} | ||
position++ | ||
} | ||
if found { | ||
return position, position + 1 + c.RemainingBatchLength(), nil | ||
} | ||
return 0, 0, ErrServerLoadNotFound | ||
} | ||
|
||
// SetServerLoad stores the load of this server in the database. | ||
func (db *DB) SetServerLoad(ctx context.Context, server string, load int64) error { | ||
if server == "" { | ||
return errors.New("invalid server name") | ||
} | ||
filter := bson.M{"server_name": server} | ||
update := bson.M{"$set": bson.M{ | ||
"server_name": server, | ||
"load": load, | ||
}} | ||
opts := options.Update().SetUpsert(true) | ||
_, err := db.staticDB.Collection(collServerLoad).UpdateOne(ctx, filter, update, opts) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
package database | ||
|
||
const ( | ||
// KiB kilobyte | ||
KiB = 1024 | ||
// MiB megabyte | ||
MiB = 1024 * KiB | ||
// GiB gigabyte | ||
GiB = 1024 * MiB | ||
// TiB terabyte | ||
TiB = 1024 * GiB | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package lib | ||
|
||
import "time" | ||
|
||
// Now returns the current time in UTC, truncated to milliseconds. | ||
func Now() time.Time { | ||
return time.Now().UTC().Truncate(time.Millisecond) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.