2022-05-24 11:23:27 +00:00
|
|
|
package tree
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/internal/logs"
|
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
|
|
|
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-05-24 11:23:27 +00:00
|
|
|
"go.uber.org/zap"
|
2024-10-24 06:45:57 +00:00
|
|
|
"google.golang.org/grpc"
|
2022-05-24 11:23:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var errNoSuitableNode = errors.New("no node was found to execute the request")
|
|
|
|
|
2024-10-24 06:45:57 +00:00
|
|
|
func relayUnary[Req any, Resp any](ctx context.Context, s *Service, ns []netmapSDK.NodeInfo, req *Req, callback func(TreeServiceClient, context.Context, *Req, ...grpc.CallOption) (*Resp, error)) (*Resp, error) {
|
|
|
|
var resp *Resp
|
|
|
|
var outErr error
|
|
|
|
err := s.forEachNode(ctx, ns, func(c TreeServiceClient) bool {
|
|
|
|
resp, outErr = callback(c, ctx, req)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return resp, outErr
|
|
|
|
}
|
|
|
|
|
2022-05-27 12:55:02 +00:00
|
|
|
// forEachNode executes callback for each node in the container until true is returned.
|
|
|
|
// Returns errNoSuitableNode if there was no successful attempt to dial any node.
|
|
|
|
func (s *Service) forEachNode(ctx context.Context, cntNodes []netmapSDK.NodeInfo, f func(c TreeServiceClient) bool) error {
|
2022-05-24 11:23:27 +00:00
|
|
|
for _, n := range cntNodes {
|
2022-05-26 12:40:44 +00:00
|
|
|
if bytes.Equal(n.PublicKey(), s.rawPub) {
|
2022-05-24 11:23:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var called bool
|
|
|
|
for _, n := range cntNodes {
|
|
|
|
var stop bool
|
|
|
|
n.IterateNetworkEndpoints(func(endpoint string) bool {
|
2023-04-13 12:36:20 +00:00
|
|
|
ctx, span := tracing.StartSpanFromContext(ctx, "TreeService.IterateNetworkEndpoints",
|
|
|
|
trace.WithAttributes(
|
|
|
|
attribute.String("endpoint", endpoint),
|
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
|
2022-05-28 09:27:55 +00:00
|
|
|
c, err := s.cache.get(ctx, endpoint)
|
2022-05-24 11:23:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-10-21 07:22:54 +00:00
|
|
|
s.log.Debug(ctx, logs.TreeRedirectingTreeServiceQuery, zap.String("endpoint", endpoint),
|
2023-09-27 08:02:06 +00:00
|
|
|
zap.String("trace_id", tracingPkg.GetTraceID(ctx)))
|
|
|
|
|
2022-05-24 11:23:27 +00:00
|
|
|
called = true
|
|
|
|
stop = f(c)
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
if stop {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !called {
|
|
|
|
return errNoSuitableNode
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|