2019-01-08 03:05:11 +00:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package psserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
|
|
|
|
"storj.io/storj/pkg/kademlia"
|
|
|
|
"storj.io/storj/pkg/pb"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// Error is a standard error class for this package.
|
|
|
|
Error = errs.Class("kademlia bucket refresher error")
|
|
|
|
)
|
|
|
|
|
2019-01-23 10:39:03 +00:00
|
|
|
// Monitor contains the information needed to run the bucket refresher service
|
|
|
|
type Monitor struct { // TODO: rename to something clearer
|
2019-01-08 03:05:11 +00:00
|
|
|
log *zap.Logger
|
|
|
|
ticker *time.Ticker
|
|
|
|
rt *kademlia.RoutingTable
|
|
|
|
server *Server
|
|
|
|
}
|
|
|
|
|
2019-01-23 10:39:03 +00:00
|
|
|
// NewMonitor creates a disk monitor
|
|
|
|
func NewMonitor(log *zap.Logger, interval time.Duration, rt *kademlia.RoutingTable, server *Server) *Monitor {
|
|
|
|
return &Monitor{
|
2019-01-08 03:05:11 +00:00
|
|
|
log: log,
|
|
|
|
ticker: time.NewTicker(interval),
|
|
|
|
rt: rt,
|
|
|
|
server: server,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Run runs the bucket refresher service
|
2019-01-23 10:39:03 +00:00
|
|
|
func (service *Monitor) Run(ctx context.Context) error {
|
2019-01-08 03:05:11 +00:00
|
|
|
for {
|
|
|
|
err := service.process(ctx)
|
|
|
|
if err != nil {
|
|
|
|
service.log.Error("process", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-service.ticker.C: // wait for the next interval to happen
|
|
|
|
case <-ctx.Done(): // or the bucket refresher service is canceled via context
|
2019-01-23 10:39:03 +00:00
|
|
|
return ctx.Err()
|
2019-01-08 03:05:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// process will attempt to update the kademlia bucket with the latest information about the storage node
|
2019-01-23 10:39:03 +00:00
|
|
|
func (service *Monitor) process(ctx context.Context) error {
|
2019-01-08 03:05:11 +00:00
|
|
|
stats, err := service.server.Stats(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
self := service.rt.Local()
|
|
|
|
|
|
|
|
self.Restrictions = &pb.NodeRestrictions{
|
|
|
|
FreeBandwidth: stats.AvailableBandwidth,
|
|
|
|
FreeDisk: stats.AvailableSpace,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the routing table with latest restrictions
|
|
|
|
if err := service.rt.UpdateSelf(&self); err != nil {
|
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|