2019-08-28 09:50:55 +00:00
|
|
|
// Package single implements a ClusterDAGService that chunks and adds content
|
|
|
|
// to cluster without sharding, before pinning it.
|
|
|
|
package single
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
adder "github.com/ipfs/ipfs-cluster/adder"
|
|
|
|
"github.com/ipfs/ipfs-cluster/api"
|
|
|
|
|
|
|
|
cid "github.com/ipfs/go-cid"
|
|
|
|
ipld "github.com/ipfs/go-ipld-format"
|
2020-03-13 20:40:02 +00:00
|
|
|
logging "github.com/ipfs/go-log/v2"
|
2019-08-28 09:50:55 +00:00
|
|
|
peer "github.com/libp2p/go-libp2p-core/peer"
|
|
|
|
rpc "github.com/libp2p/go-libp2p-gorpc"
|
|
|
|
)
|
|
|
|
|
|
|
|
var logger = logging.Logger("singledags")
|
2020-04-14 17:58:00 +00:00
|
|
|
var _ = logger // otherwise unused
|
2019-08-28 09:50:55 +00:00
|
|
|
|
|
|
|
// DAGService is an implementation of an adder.ClusterDAGService which
|
|
|
|
// puts the added blocks directly in the peers allocated to them (without
|
|
|
|
// sharding).
|
|
|
|
type DAGService struct {
|
|
|
|
adder.BaseDAGService
|
|
|
|
|
|
|
|
rpcClient *rpc.Client
|
|
|
|
|
2022-02-28 18:44:04 +00:00
|
|
|
dests []peer.ID
|
|
|
|
addParams api.AddParams
|
|
|
|
local bool
|
2019-08-28 09:50:55 +00:00
|
|
|
|
|
|
|
ba *adder.BlockAdder
|
|
|
|
}
|
|
|
|
|
|
|
|
// New returns a new Adder with the given rpc Client. The client is used
|
|
|
|
// to perform calls to IPFS.BlockPut and Pin content on Cluster.
|
2022-02-28 18:44:04 +00:00
|
|
|
func New(rpc *rpc.Client, opts api.AddParams, local bool) *DAGService {
|
2020-04-20 13:15:52 +00:00
|
|
|
// ensure don't Add something and pin it in direct mode.
|
|
|
|
opts.Mode = api.PinModeRecursive
|
2019-08-28 09:50:55 +00:00
|
|
|
return &DAGService{
|
|
|
|
rpcClient: rpc,
|
|
|
|
dests: nil,
|
2022-02-28 18:44:04 +00:00
|
|
|
addParams: opts,
|
2019-08-31 14:10:56 +00:00
|
|
|
local: local,
|
2019-08-28 09:50:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add puts the given node in the destination peers.
|
|
|
|
func (dgs *DAGService) Add(ctx context.Context, node ipld.Node) error {
|
|
|
|
if dgs.dests == nil {
|
2022-02-28 18:44:04 +00:00
|
|
|
dests, err := adder.BlockAllocate(ctx, dgs.rpcClient, dgs.addParams.PinOptions)
|
2019-08-28 09:50:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-02-01 19:38:41 +00:00
|
|
|
|
2022-03-15 10:32:12 +00:00
|
|
|
hasLocal := false
|
|
|
|
localPid := dgs.rpcClient.ID()
|
|
|
|
for i, d := range dests {
|
|
|
|
if d == localPid || d == "" {
|
|
|
|
hasLocal = true
|
|
|
|
// ensure our allocs do not carry an empty peer
|
|
|
|
// mostly an issue with testing mocks
|
|
|
|
dests[i] = localPid
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-28 09:50:55 +00:00
|
|
|
dgs.dests = dests
|
2019-09-18 07:51:04 +00:00
|
|
|
|
|
|
|
if dgs.local {
|
2022-02-28 18:44:04 +00:00
|
|
|
// If this is a local pin, make sure that the local
|
|
|
|
// peer is among the allocations..
|
|
|
|
// UNLESS user-allocations are defined!
|
2022-03-15 10:32:12 +00:00
|
|
|
if !hasLocal && localPid != "" && len(dgs.addParams.UserAllocations) == 0 {
|
2022-02-01 19:38:41 +00:00
|
|
|
// replace last allocation with local peer
|
|
|
|
dgs.dests[len(dgs.dests)-1] = localPid
|
|
|
|
}
|
|
|
|
|
2022-03-15 10:32:12 +00:00
|
|
|
dgs.ba = adder.NewBlockAdder(dgs.rpcClient, []peer.ID{localPid})
|
2019-09-18 07:51:04 +00:00
|
|
|
} else {
|
2022-02-01 19:38:41 +00:00
|
|
|
dgs.ba = adder.NewBlockAdder(dgs.rpcClient, dgs.dests)
|
2019-09-18 07:51:04 +00:00
|
|
|
}
|
2019-08-28 09:50:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return dgs.ba.Add(ctx, node)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Finalize pins the last Cid added to this DAGService.
|
|
|
|
func (dgs *DAGService) Finalize(ctx context.Context, root cid.Cid) (cid.Cid, error) {
|
2022-02-28 18:44:04 +00:00
|
|
|
// Do not pin, just block put.
|
|
|
|
// Why? Because some people are uploading CAR files with partial DAGs
|
|
|
|
// and ideally they should be pinning only when the last partial CAR
|
|
|
|
// is uploaded. This gives them that option.
|
|
|
|
if dgs.addParams.NoPin {
|
|
|
|
return root, nil
|
|
|
|
}
|
|
|
|
|
2019-08-28 09:50:55 +00:00
|
|
|
// Cluster pin the result
|
2022-02-28 18:44:04 +00:00
|
|
|
rootPin := api.PinWithOpts(root, dgs.addParams.PinOptions)
|
2019-08-31 14:10:56 +00:00
|
|
|
rootPin.Allocations = dgs.dests
|
2019-08-28 09:50:55 +00:00
|
|
|
|
|
|
|
return root, adder.Pin(ctx, dgs.rpcClient, rootPin)
|
|
|
|
}
|
|
|
|
|
2022-03-14 14:45:51 +00:00
|
|
|
// Allocations returns the add destinations decided by the DAGService.
|
|
|
|
func (dgs *DAGService) Allocations() []peer.ID {
|
2022-03-15 10:32:12 +00:00
|
|
|
// using rpc clients without a host results in an empty peer
|
|
|
|
// which cannot be parsed to peer.ID on deserialization.
|
|
|
|
if len(dgs.dests) == 1 && dgs.dests[0] == "" {
|
|
|
|
return nil
|
|
|
|
}
|
2022-03-14 14:45:51 +00:00
|
|
|
return dgs.dests
|
|
|
|
}
|
|
|
|
|
2019-08-28 09:50:55 +00:00
|
|
|
// AddMany calls Add for every given node.
|
|
|
|
func (dgs *DAGService) AddMany(ctx context.Context, nodes []ipld.Node) error {
|
|
|
|
for _, node := range nodes {
|
|
|
|
err := dgs.Add(ctx, node)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|