2022-04-22 13:30:20 +00:00
|
|
|
package tree
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/sha256"
|
2022-05-27 11:16:12 +00:00
|
|
|
"encoding/hex"
|
2022-09-07 07:25:03 +00:00
|
|
|
"errors"
|
2022-04-22 13:30:20 +00:00
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/pilorama"
|
2023-09-27 08:02:06 +00:00
|
|
|
tracingPkg "git.frostfs.info/TrueCloudLab/frostfs-node/pkg/tracing"
|
2023-05-31 09:24:04 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-observability/tracing"
|
2023-03-07 13:38:26 +00:00
|
|
|
cidSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
netmapSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
|
2023-04-13 12:36:20 +00:00
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
2022-04-22 13:30:20 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type movePair struct {
|
|
|
|
cid cidSDK.ID
|
|
|
|
treeID string
|
2023-01-25 11:12:02 +00:00
|
|
|
op *pilorama.Move
|
2022-04-22 13:30:20 +00:00
|
|
|
}
|
|
|
|
|
2022-05-28 13:48:25 +00:00
|
|
|
type replicationTask struct {
|
|
|
|
n netmapSDK.NodeInfo
|
|
|
|
req *ApplyRequest
|
|
|
|
}
|
|
|
|
|
2022-07-26 10:37:12 +00:00
|
|
|
type applyOp struct {
|
|
|
|
treeID string
|
2023-03-21 12:43:12 +00:00
|
|
|
cid cidSDK.ID
|
2022-07-26 10:37:12 +00:00
|
|
|
pilorama.Move
|
|
|
|
}
|
|
|
|
|
2022-04-22 13:30:20 +00:00
|
|
|
const (
|
2022-05-28 13:48:25 +00:00
|
|
|
defaultReplicatorCapacity = 64
|
|
|
|
defaultReplicatorWorkerCount = 64
|
|
|
|
defaultReplicatorSendTimeout = time.Second * 5
|
2024-10-30 08:02:52 +00:00
|
|
|
defaultSyncBatchSize = 1000
|
2022-04-22 13:30:20 +00:00
|
|
|
)
|
|
|
|
|
2023-04-13 12:36:20 +00:00
|
|
|
func (s *Service) localReplicationWorker(ctx context.Context) {
|
2022-07-26 10:37:12 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-s.closeCh:
|
|
|
|
return
|
|
|
|
case op := <-s.replicateLocalCh:
|
2023-04-13 12:36:20 +00:00
|
|
|
ctx, span := tracing.StartSpanFromContext(ctx, "TreeService.HandleReplicationOperation",
|
|
|
|
trace.WithAttributes(
|
|
|
|
attribute.String("tree_id", op.treeID),
|
|
|
|
attribute.String("container_id", op.cid.EncodeToString()),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
err := s.forest.TreeApply(ctx, op.cid, op.treeID, &op.Move, false)
|
2022-07-26 10:37:12 +00:00
|
|
|
if err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
s.log.Error(logs.TreeFailedToApplyReplicatedOperation,
|
2022-07-26 10:37:12 +00:00
|
|
|
zap.String("err", err.Error()))
|
|
|
|
}
|
2023-04-13 12:36:20 +00:00
|
|
|
span.End()
|
2022-07-26 10:37:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 13:58:49 +00:00
|
|
|
func (s *Service) replicationWorker(ctx context.Context) {
|
2022-05-28 13:48:25 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-s.closeCh:
|
|
|
|
return
|
|
|
|
case task := <-s.replicationTasks:
|
2024-02-06 14:34:32 +00:00
|
|
|
_ = s.ReplicateTreeOp(ctx, task.n, task.req)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-13 12:36:20 +00:00
|
|
|
|
2024-02-06 14:34:32 +00:00
|
|
|
func (s *Service) ReplicateTreeOp(ctx context.Context, n netmapSDK.NodeInfo, req *ApplyRequest) error {
|
|
|
|
ctx, span := tracing.StartSpanFromContext(ctx, "TreeService.HandleReplicationTask",
|
|
|
|
trace.WithAttributes(
|
|
|
|
attribute.String("public_key", hex.EncodeToString(n.PublicKey())),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
var lastErr error
|
|
|
|
var lastAddr string
|
|
|
|
|
|
|
|
n.IterateNetworkEndpoints(func(addr string) bool {
|
|
|
|
ctx, span := tracing.StartSpanFromContext(ctx, "TreeService.HandleReplicationTaskOnEndpoint",
|
|
|
|
trace.WithAttributes(
|
|
|
|
attribute.String("public_key", hex.EncodeToString(n.PublicKey())),
|
|
|
|
attribute.String("address", addr),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
lastAddr = addr
|
|
|
|
|
|
|
|
c, err := s.cache.get(ctx, addr)
|
|
|
|
if err != nil {
|
|
|
|
lastErr = fmt.Errorf("can't create client: %w", err)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, s.replicatorTimeout)
|
|
|
|
_, lastErr = c.Apply(ctx, req)
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
return lastErr == nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if lastErr != nil {
|
|
|
|
if errors.Is(lastErr, errRecentlyFailed) {
|
|
|
|
s.log.Debug(logs.TreeDoNotSendUpdateToTheNode,
|
|
|
|
zap.String("last_error", lastErr.Error()),
|
|
|
|
zap.String("trace_id", tracingPkg.GetTraceID(ctx)))
|
|
|
|
} else {
|
|
|
|
s.log.Warn(logs.TreeFailedToSentUpdateToTheNode,
|
|
|
|
zap.String("last_error", lastErr.Error()),
|
|
|
|
zap.String("address", lastAddr),
|
|
|
|
zap.String("key", hex.EncodeToString(n.PublicKey())),
|
|
|
|
zap.String("trace_id", tracingPkg.GetTraceID(ctx)))
|
2022-05-28 13:48:25 +00:00
|
|
|
}
|
2024-02-06 14:34:32 +00:00
|
|
|
s.metrics.AddReplicateTaskDuration(time.Since(start), false)
|
|
|
|
return lastErr
|
2022-05-28 13:48:25 +00:00
|
|
|
}
|
2024-02-06 14:34:32 +00:00
|
|
|
s.metrics.AddReplicateTaskDuration(time.Since(start), true)
|
|
|
|
return nil
|
2022-05-28 13:48:25 +00:00
|
|
|
}
|
|
|
|
|
2022-04-22 13:30:20 +00:00
|
|
|
func (s *Service) replicateLoop(ctx context.Context) {
|
2024-08-30 16:20:55 +00:00
|
|
|
for range s.replicatorWorkerCount {
|
2023-03-21 13:58:49 +00:00
|
|
|
go s.replicationWorker(ctx)
|
2023-04-13 12:36:20 +00:00
|
|
|
go s.localReplicationWorker(ctx)
|
2022-05-28 13:48:25 +00:00
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
for len(s.replicationTasks) != 0 {
|
|
|
|
<-s.replicationTasks
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2022-04-22 13:30:20 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-s.closeCh:
|
2022-05-28 13:48:25 +00:00
|
|
|
return
|
2022-04-22 13:30:20 +00:00
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case op := <-s.replicateCh:
|
2023-05-24 07:01:50 +00:00
|
|
|
start := time.Now()
|
2022-05-28 13:48:25 +00:00
|
|
|
err := s.replicate(op)
|
2022-04-22 13:30:20 +00:00
|
|
|
if err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
s.log.Error(logs.TreeErrorDuringReplication,
|
2022-04-22 13:30:20 +00:00
|
|
|
zap.String("err", err.Error()),
|
|
|
|
zap.Stringer("cid", op.cid),
|
|
|
|
zap.String("treeID", op.treeID))
|
|
|
|
}
|
2023-05-24 07:01:50 +00:00
|
|
|
s.metrics.AddReplicateWaitDuration(time.Since(start), err == nil)
|
2022-04-22 13:30:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-28 13:48:25 +00:00
|
|
|
func (s *Service) replicate(op movePair) error {
|
2022-04-22 13:30:20 +00:00
|
|
|
req := newApplyRequest(&op)
|
2022-10-10 15:32:04 +00:00
|
|
|
err := SignMessage(req, s.key)
|
2022-05-05 11:00:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't sign data: %w", err)
|
|
|
|
}
|
2022-04-22 13:30:20 +00:00
|
|
|
|
2022-05-30 17:46:29 +00:00
|
|
|
nodes, localIndex, err := s.getContainerNodes(op.cid)
|
2022-04-22 13:30:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't get container nodes: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-05-30 17:46:29 +00:00
|
|
|
for i := range nodes {
|
|
|
|
if i != localIndex {
|
|
|
|
s.replicationTasks <- replicationTask{nodes[i], req}
|
2022-05-24 11:27:09 +00:00
|
|
|
}
|
2022-04-22 13:30:20 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-25 11:12:02 +00:00
|
|
|
func (s *Service) pushToQueue(cid cidSDK.ID, treeID string, op *pilorama.Move) {
|
2022-05-28 13:48:25 +00:00
|
|
|
select {
|
|
|
|
case s.replicateCh <- movePair{
|
2022-04-22 13:30:20 +00:00
|
|
|
cid: cid,
|
|
|
|
treeID: treeID,
|
|
|
|
op: op,
|
2022-05-28 13:48:25 +00:00
|
|
|
}:
|
2023-01-20 15:04:20 +00:00
|
|
|
default:
|
2022-04-22 13:30:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func newApplyRequest(op *movePair) *ApplyRequest {
|
|
|
|
rawCID := make([]byte, sha256.Size)
|
|
|
|
op.cid.Encode(rawCID)
|
|
|
|
|
|
|
|
return &ApplyRequest{
|
|
|
|
Body: &ApplyRequest_Body{
|
|
|
|
ContainerId: rawCID,
|
|
|
|
TreeId: op.treeID,
|
|
|
|
Operation: &LogMove{
|
|
|
|
ParentId: op.op.Parent,
|
|
|
|
Meta: op.op.Meta.Bytes(),
|
|
|
|
ChildId: op.op.Child,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|