2017-08-04 19:35:30 +00:00
|
|
|
// Package ascendalloc implements an ipfscluster.PinAllocator, which returns
|
|
|
|
// allocations based on sorting the metrics in ascending order. Thus, peers with
|
|
|
|
// smallest metrics are first in the list. This allocator can be used with a
|
|
|
|
// number of informers, as long as they provide a numeric metric value.
|
2017-03-27 13:07:12 +00:00
|
|
|
package ascendalloc
|
2017-02-13 15:46:53 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
|
2017-08-04 19:35:30 +00:00
|
|
|
"github.com/ipfs/ipfs-cluster/allocator/util"
|
2017-02-13 15:46:53 +00:00
|
|
|
"github.com/ipfs/ipfs-cluster/api"
|
|
|
|
|
|
|
|
rpc "github.com/hsanjuan/go-libp2p-gorpc"
|
|
|
|
cid "github.com/ipfs/go-cid"
|
|
|
|
logging "github.com/ipfs/go-log"
|
|
|
|
peer "github.com/libp2p/go-libp2p-peer"
|
|
|
|
)
|
|
|
|
|
2017-03-27 13:07:12 +00:00
|
|
|
var logger = logging.Logger("ascendalloc")
|
2017-02-13 15:46:53 +00:00
|
|
|
|
2017-08-04 19:35:30 +00:00
|
|
|
// AscendAllocator extends the SimpleAllocator
|
|
|
|
type AscendAllocator struct{}
|
2017-02-13 15:46:53 +00:00
|
|
|
|
2017-08-04 19:35:30 +00:00
|
|
|
// NewAscendAllocator returns an initialized AscendAllocator
|
|
|
|
func NewAllocator() AscendAllocator {
|
|
|
|
return AscendAllocator{}
|
2017-02-13 15:46:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetClient does nothing in this allocator
|
2017-08-04 19:35:30 +00:00
|
|
|
func (alloc AscendAllocator) SetClient(c *rpc.Client) {}
|
2017-02-13 15:46:53 +00:00
|
|
|
|
|
|
|
// Shutdown does nothing in this allocator
|
2017-08-04 19:35:30 +00:00
|
|
|
func (alloc AscendAllocator) Shutdown() error { return nil }
|
2017-02-13 15:46:53 +00:00
|
|
|
|
2017-03-27 13:07:12 +00:00
|
|
|
// Allocate returns where to allocate a pin request based on metrics which
|
|
|
|
// carry a numeric value such as "used disk". We do not pay attention to
|
2017-08-04 19:35:30 +00:00
|
|
|
// the metrics of the currently allocated peers and we just sort the
|
|
|
|
// candidates based on their metric values (smallest to largest).
|
|
|
|
func (alloc AscendAllocator) Allocate(c *cid.Cid, current, candidates map[peer.ID]api.Metric) ([]peer.ID, error) {
|
2017-02-13 15:46:53 +00:00
|
|
|
// sort our metrics
|
2017-08-04 19:35:30 +00:00
|
|
|
sortable := util.NewMetricSorter(candidates, false)
|
2017-03-27 13:07:12 +00:00
|
|
|
sort.Sort(sortable)
|
2017-08-04 19:35:30 +00:00
|
|
|
return sortable.Peers, nil
|
2017-02-13 15:46:53 +00:00
|
|
|
}
|