2020-10-21 11:49:02 +00:00
|
|
|
package replicator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-06-22 11:12:57 +00:00
|
|
|
"encoding/hex"
|
2020-10-21 11:49:02 +00:00
|
|
|
|
2020-11-19 08:21:58 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/engine"
|
2020-10-21 11:49:02 +00:00
|
|
|
putsvc "github.com/nspcc-dev/neofs-node/pkg/services/object/put"
|
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2022-06-09 19:11:35 +00:00
|
|
|
// TaskResult is a replication result interface.
|
|
|
|
type TaskResult interface {
|
|
|
|
// SubmitSuccessfulReplication must save successful
|
|
|
|
// replication result. ID is a netmap identification
|
|
|
|
// of a node that accepted the replica.
|
|
|
|
SubmitSuccessfulReplication(id uint64)
|
|
|
|
}
|
|
|
|
|
2021-11-10 11:47:52 +00:00
|
|
|
// HandleTask executes replication task inside invoking goroutine.
|
2022-06-09 19:11:35 +00:00
|
|
|
// Passes all the nodes that accepted the replication to the TaskResult.
|
|
|
|
func (p *Replicator) HandleTask(ctx context.Context, task *Task, res TaskResult) {
|
2020-10-21 11:49:02 +00:00
|
|
|
defer func() {
|
2021-12-22 16:19:31 +00:00
|
|
|
p.log.Debug("finish work",
|
2020-10-21 11:49:02 +00:00
|
|
|
zap.Uint32("amount of unfinished replicas", task.quantity),
|
|
|
|
)
|
|
|
|
}()
|
|
|
|
|
2022-09-19 10:57:59 +00:00
|
|
|
if task.obj == nil {
|
|
|
|
var err error
|
|
|
|
task.obj, err = engine.Get(p.localStorage, task.addr)
|
|
|
|
if err != nil {
|
|
|
|
p.log.Error("could not get object from local storage",
|
|
|
|
zap.Stringer("object", task.addr),
|
|
|
|
zap.Error(err))
|
2020-10-21 11:49:02 +00:00
|
|
|
|
2022-09-19 10:57:59 +00:00
|
|
|
return
|
|
|
|
}
|
2020-10-21 11:49:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
prm := new(putsvc.RemotePutPrm).
|
2022-09-19 10:57:59 +00:00
|
|
|
WithObject(task.obj)
|
2020-10-21 11:49:02 +00:00
|
|
|
|
|
|
|
for i := 0; task.quantity > 0 && i < len(task.nodes); i++ {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
2021-12-22 16:19:31 +00:00
|
|
|
log := p.log.With(
|
|
|
|
zap.String("node", hex.EncodeToString(task.nodes[i].PublicKey())),
|
|
|
|
zap.Stringer("object", task.addr),
|
|
|
|
)
|
2020-10-21 11:49:02 +00:00
|
|
|
|
|
|
|
callCtx, cancel := context.WithTimeout(ctx, p.putTimeout)
|
|
|
|
|
2022-09-19 10:57:59 +00:00
|
|
|
err := p.remoteSender.PutObject(callCtx, prm.WithNodeInfo(task.nodes[i]))
|
2020-10-21 11:49:02 +00:00
|
|
|
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error("could not replicate object",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
} else {
|
2021-12-22 16:19:31 +00:00
|
|
|
log.Debug("object successfully replicated")
|
2020-10-21 11:49:02 +00:00
|
|
|
|
|
|
|
task.quantity--
|
2022-06-09 19:11:35 +00:00
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
res.SubmitSuccessfulReplication(task.nodes[i].Hash())
|
2020-10-21 11:49:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|