2022-05-12 11:19:44 +00:00
|
|
|
package tree
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/sha256"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
2022-12-19 15:53:03 +00:00
|
|
|
"math"
|
2022-12-10 10:50:08 +00:00
|
|
|
"math/rand"
|
2022-12-12 11:49:40 +00:00
|
|
|
"sync"
|
2022-05-12 11:19:44 +00:00
|
|
|
|
2022-12-23 17:35:35 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/local_object_storage/pilorama"
|
2022-12-10 10:50:08 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/morph/client/netmap"
|
2022-12-23 17:35:35 +00:00
|
|
|
"github.com/TrueCloudLab/frostfs-node/pkg/network"
|
|
|
|
cid "github.com/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
netmapSDK "github.com/TrueCloudLab/frostfs-sdk-go/netmap"
|
2022-12-12 11:49:40 +00:00
|
|
|
"github.com/panjf2000/ants/v2"
|
2022-10-18 14:15:24 +00:00
|
|
|
"go.uber.org/zap"
|
2022-05-12 11:19:44 +00:00
|
|
|
"google.golang.org/grpc"
|
2022-07-30 07:24:16 +00:00
|
|
|
"google.golang.org/grpc/credentials/insecure"
|
2022-05-12 11:19:44 +00:00
|
|
|
)
|
|
|
|
|
2022-10-06 20:18:26 +00:00
|
|
|
// ErrNotInContainer is returned when operation could not be performed
|
|
|
|
// because the node is not included in the container.
|
|
|
|
var ErrNotInContainer = errors.New("node is not in container")
|
|
|
|
|
2022-12-12 11:49:40 +00:00
|
|
|
const defaultSyncWorkerCount = 20
|
|
|
|
|
2022-12-19 15:18:41 +00:00
|
|
|
// synchronizeAllTrees synchronizes all the trees of the container. It fetches
|
2022-10-18 14:15:24 +00:00
|
|
|
// tree IDs from the other container nodes. Returns ErrNotInContainer if the node
|
|
|
|
// is not included in the container.
|
2022-12-19 15:18:41 +00:00
|
|
|
func (s *Service) synchronizeAllTrees(ctx context.Context, cid cid.ID) error {
|
2022-10-06 20:18:26 +00:00
|
|
|
nodes, pos, err := s.getContainerNodes(cid)
|
2022-05-12 11:19:44 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't get container nodes: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-10-06 20:18:26 +00:00
|
|
|
if pos < 0 {
|
|
|
|
return ErrNotInContainer
|
|
|
|
}
|
|
|
|
|
2022-10-10 19:14:05 +00:00
|
|
|
var d pilorama.CIDDescriptor
|
|
|
|
d.CID = cid
|
|
|
|
d.Position = pos
|
|
|
|
d.Size = len(nodes)
|
|
|
|
|
2022-12-10 10:50:08 +00:00
|
|
|
nodes = randomizeNodeOrder(nodes, pos)
|
2022-10-18 14:15:24 +00:00
|
|
|
if len(nodes) == 0 {
|
2022-12-15 12:15:43 +00:00
|
|
|
return nil
|
2022-10-18 14:15:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rawCID := make([]byte, sha256.Size)
|
|
|
|
cid.Encode(rawCID)
|
|
|
|
|
|
|
|
req := &TreeListRequest{
|
|
|
|
Body: &TreeListRequest_Body{
|
|
|
|
ContainerId: rawCID,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = SignMessage(req, s.key)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not sign request: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var resp *TreeListResponse
|
|
|
|
var treesToSync []string
|
|
|
|
var outErr error
|
|
|
|
|
|
|
|
err = s.forEachNode(ctx, nodes, func(c TreeServiceClient) bool {
|
|
|
|
resp, outErr = c.TreeList(ctx, req)
|
|
|
|
if outErr != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
treesToSync = resp.GetBody().GetIds()
|
|
|
|
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
outErr = err
|
|
|
|
}
|
|
|
|
|
|
|
|
if outErr != nil {
|
|
|
|
return fmt.Errorf("could not fetch tree ID list: %w", outErr)
|
|
|
|
}
|
|
|
|
|
2022-12-19 15:53:03 +00:00
|
|
|
s.cnrMapMtx.Lock()
|
|
|
|
oldStatus := s.cnrMap[cid]
|
|
|
|
s.cnrMapMtx.Unlock()
|
|
|
|
|
|
|
|
syncStatus := map[string]uint64{}
|
|
|
|
for i := range treesToSync {
|
|
|
|
syncStatus[treesToSync[i]] = 0
|
|
|
|
}
|
|
|
|
for tid := range oldStatus {
|
|
|
|
if _, ok := syncStatus[tid]; ok {
|
|
|
|
syncStatus[tid] = oldStatus[tid]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-18 14:15:24 +00:00
|
|
|
for _, tid := range treesToSync {
|
2022-12-19 15:53:03 +00:00
|
|
|
h := s.synchronizeTree(ctx, d, syncStatus[tid], tid, nodes)
|
|
|
|
if syncStatus[tid] < h {
|
|
|
|
syncStatus[tid] = h
|
2022-10-18 14:15:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-19 15:53:03 +00:00
|
|
|
s.cnrMapMtx.Lock()
|
|
|
|
s.cnrMap[cid] = syncStatus
|
|
|
|
s.cnrMapMtx.Unlock()
|
|
|
|
|
2022-10-18 14:15:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SynchronizeTree tries to synchronize log starting from the last stored height.
|
|
|
|
func (s *Service) SynchronizeTree(ctx context.Context, cid cid.ID, treeID string) error {
|
|
|
|
nodes, pos, err := s.getContainerNodes(cid)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("can't get container nodes: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if pos < 0 {
|
|
|
|
return ErrNotInContainer
|
|
|
|
}
|
|
|
|
|
|
|
|
var d pilorama.CIDDescriptor
|
|
|
|
d.CID = cid
|
|
|
|
d.Position = pos
|
|
|
|
d.Size = len(nodes)
|
|
|
|
|
2022-12-10 10:50:08 +00:00
|
|
|
nodes = randomizeNodeOrder(nodes, pos)
|
2022-10-18 14:15:24 +00:00
|
|
|
if len(nodes) == 0 {
|
2022-12-15 12:15:43 +00:00
|
|
|
return nil
|
2022-10-18 14:15:24 +00:00
|
|
|
}
|
|
|
|
|
2022-12-19 15:53:03 +00:00
|
|
|
s.synchronizeTree(ctx, d, 0, treeID, nodes)
|
|
|
|
return nil
|
2022-10-18 14:15:24 +00:00
|
|
|
}
|
|
|
|
|
2022-12-19 15:53:03 +00:00
|
|
|
func (s *Service) synchronizeTree(ctx context.Context, d pilorama.CIDDescriptor, from uint64,
|
|
|
|
treeID string, nodes []netmapSDK.NodeInfo) uint64 {
|
|
|
|
s.log.Debug("synchronize tree",
|
|
|
|
zap.Stringer("cid", d.CID),
|
|
|
|
zap.String("tree", treeID),
|
|
|
|
zap.Uint64("from", from))
|
2022-05-12 11:19:44 +00:00
|
|
|
|
2022-12-19 15:53:03 +00:00
|
|
|
newHeight := uint64(math.MaxUint64)
|
2022-05-12 11:19:44 +00:00
|
|
|
for _, n := range nodes {
|
2022-12-19 15:53:03 +00:00
|
|
|
height := from
|
2022-05-12 11:19:44 +00:00
|
|
|
n.IterateNetworkEndpoints(func(addr string) bool {
|
|
|
|
var a network.Address
|
|
|
|
if err := a.FromString(addr); err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-07-30 07:24:16 +00:00
|
|
|
cc, err := grpc.DialContext(ctx, a.URIAddr(), grpc.WithTransportCredentials(insecure.NewCredentials()))
|
2022-05-12 11:19:44 +00:00
|
|
|
if err != nil {
|
|
|
|
// Failed to connect, try the next address.
|
|
|
|
return false
|
|
|
|
}
|
2022-12-16 06:54:00 +00:00
|
|
|
defer cc.Close()
|
2022-05-12 11:19:44 +00:00
|
|
|
|
|
|
|
treeClient := NewTreeServiceClient(cc)
|
|
|
|
for {
|
2022-10-10 19:14:05 +00:00
|
|
|
h, err := s.synchronizeSingle(ctx, d, treeID, height, treeClient)
|
2022-05-12 11:19:44 +00:00
|
|
|
if height < h {
|
|
|
|
height = h
|
|
|
|
}
|
|
|
|
if err != nil || h <= height {
|
|
|
|
// Error with the response, try the next node.
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2022-12-19 15:53:03 +00:00
|
|
|
if height <= from { // do not increase starting height on fail
|
|
|
|
newHeight = from
|
|
|
|
} else if height < newHeight { // take minimum across all clients
|
|
|
|
newHeight = height
|
|
|
|
}
|
2022-05-12 11:19:44 +00:00
|
|
|
}
|
2022-12-19 15:53:03 +00:00
|
|
|
if newHeight == math.MaxUint64 {
|
|
|
|
newHeight = from
|
|
|
|
}
|
|
|
|
return newHeight
|
2022-05-12 11:19:44 +00:00
|
|
|
}
|
|
|
|
|
2022-10-10 19:14:05 +00:00
|
|
|
func (s *Service) synchronizeSingle(ctx context.Context, d pilorama.CIDDescriptor, treeID string, height uint64, treeClient TreeServiceClient) (uint64, error) {
|
2022-05-12 11:19:44 +00:00
|
|
|
rawCID := make([]byte, sha256.Size)
|
2022-10-10 19:14:05 +00:00
|
|
|
d.CID.Encode(rawCID)
|
2022-05-12 11:19:44 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
newHeight := height
|
|
|
|
req := &GetOpLogRequest{
|
|
|
|
Body: &GetOpLogRequest_Body{
|
|
|
|
ContainerId: rawCID,
|
|
|
|
TreeId: treeID,
|
|
|
|
Height: newHeight,
|
|
|
|
},
|
|
|
|
}
|
2022-10-10 15:32:04 +00:00
|
|
|
if err := SignMessage(req, s.key); err != nil {
|
2022-05-12 11:19:44 +00:00
|
|
|
return newHeight, err
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := treeClient.GetOpLog(ctx, req)
|
|
|
|
if err != nil {
|
|
|
|
return newHeight, fmt.Errorf("can't initialize client: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
res, err := c.Recv()
|
|
|
|
for ; err == nil; res, err = c.Recv() {
|
|
|
|
lm := res.GetBody().GetOperation()
|
|
|
|
m := &pilorama.Move{
|
|
|
|
Parent: lm.ParentId,
|
|
|
|
Child: lm.ChildId,
|
|
|
|
}
|
|
|
|
if err := m.Meta.FromBytes(lm.Meta); err != nil {
|
|
|
|
return newHeight, err
|
|
|
|
}
|
2022-12-19 16:01:57 +00:00
|
|
|
if err := s.forest.TreeApply(d, treeID, m, true); err != nil {
|
2022-05-12 11:19:44 +00:00
|
|
|
return newHeight, err
|
|
|
|
}
|
|
|
|
if m.Time > newHeight {
|
|
|
|
newHeight = m.Time + 1
|
|
|
|
} else {
|
|
|
|
newHeight++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if height == newHeight || err != nil && !errors.Is(err, io.EOF) {
|
|
|
|
return newHeight, err
|
|
|
|
}
|
|
|
|
height = newHeight
|
|
|
|
}
|
|
|
|
}
|
2022-10-18 21:33:45 +00:00
|
|
|
|
|
|
|
// ErrAlreadySyncing is returned when a service synchronization has already
|
|
|
|
// been started.
|
|
|
|
var ErrAlreadySyncing = errors.New("service is being synchronized")
|
|
|
|
|
|
|
|
// ErrShuttingDown is returned when the service is shitting down and could not
|
|
|
|
// accept any calls.
|
|
|
|
var ErrShuttingDown = errors.New("service is shutting down")
|
|
|
|
|
|
|
|
// SynchronizeAll forces tree service to synchronize all the trees according to
|
|
|
|
// netmap information. Must not be called before Service.Start.
|
|
|
|
// Returns ErrAlreadySyncing if synchronization has been started and blocked
|
|
|
|
// by another routine.
|
|
|
|
// Note: non-blocking operation.
|
|
|
|
func (s *Service) SynchronizeAll() error {
|
|
|
|
select {
|
|
|
|
case <-s.closeCh:
|
|
|
|
return ErrShuttingDown
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case s.syncChan <- struct{}{}:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
return ErrAlreadySyncing
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) syncLoop(ctx context.Context) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-s.closeCh:
|
|
|
|
return
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case <-s.syncChan:
|
|
|
|
s.log.Debug("syncing trees...")
|
|
|
|
|
|
|
|
cnrs, err := s.cfg.cnrSource.List()
|
|
|
|
if err != nil {
|
|
|
|
s.log.Error("could not fetch containers", zap.Error(err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
newMap := make(map[cid.ID]struct{}, len(s.cnrMap))
|
|
|
|
cnrsToSync := make([]cid.ID, 0, len(cnrs))
|
|
|
|
|
2022-12-19 15:53:03 +00:00
|
|
|
var removed []cid.ID
|
2022-10-18 21:33:45 +00:00
|
|
|
for _, cnr := range cnrs {
|
|
|
|
_, pos, err := s.getContainerNodes(cnr)
|
|
|
|
if err != nil {
|
|
|
|
s.log.Error("could not calculate container nodes",
|
|
|
|
zap.Stringer("cid", cnr),
|
|
|
|
zap.Error(err))
|
2022-12-19 15:53:03 +00:00
|
|
|
removed = append(removed, cnr)
|
2022-10-18 21:33:45 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if pos < 0 {
|
|
|
|
// node is not included in the container.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-12-14 07:02:41 +00:00
|
|
|
newMap[cnr] = struct{}{}
|
|
|
|
cnrsToSync = append(cnrsToSync, cnr)
|
2022-10-18 21:33:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// sync new containers
|
2022-12-12 11:49:40 +00:00
|
|
|
var wg sync.WaitGroup
|
2022-10-18 21:33:45 +00:00
|
|
|
for _, cnr := range cnrsToSync {
|
2022-12-12 11:49:40 +00:00
|
|
|
wg.Add(1)
|
|
|
|
cnr := cnr
|
|
|
|
err := s.syncPool.Submit(func() {
|
|
|
|
defer wg.Done()
|
|
|
|
s.log.Debug("syncing container trees...", zap.Stringer("cid", cnr))
|
|
|
|
|
2022-12-19 15:18:41 +00:00
|
|
|
err := s.synchronizeAllTrees(ctx, cnr)
|
2022-12-12 11:49:40 +00:00
|
|
|
if err != nil {
|
|
|
|
s.log.Error("could not sync trees", zap.Stringer("cid", cnr), zap.Error(err))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
s.log.Debug("container trees have been synced", zap.Stringer("cid", cnr))
|
|
|
|
})
|
2022-10-18 21:33:45 +00:00
|
|
|
if err != nil {
|
2022-12-12 11:49:40 +00:00
|
|
|
wg.Done()
|
|
|
|
s.log.Error("could not query trees for synchronization",
|
|
|
|
zap.Stringer("cid", cnr),
|
|
|
|
zap.Error(err))
|
|
|
|
if errors.Is(err, ants.ErrPoolClosed) {
|
|
|
|
return
|
|
|
|
}
|
2022-10-18 21:33:45 +00:00
|
|
|
}
|
|
|
|
}
|
2022-12-12 11:49:40 +00:00
|
|
|
wg.Wait()
|
2022-10-18 21:33:45 +00:00
|
|
|
|
2022-12-19 15:53:03 +00:00
|
|
|
s.cnrMapMtx.Lock()
|
2022-10-18 21:33:45 +00:00
|
|
|
for cnr := range s.cnrMap {
|
2022-12-14 07:02:41 +00:00
|
|
|
if _, ok := newMap[cnr]; ok {
|
|
|
|
continue
|
|
|
|
}
|
2022-12-19 15:53:03 +00:00
|
|
|
removed = append(removed, cnr)
|
|
|
|
}
|
|
|
|
for i := range removed {
|
|
|
|
delete(s.cnrMap, removed[i])
|
|
|
|
}
|
|
|
|
s.cnrMapMtx.Unlock()
|
2022-12-14 07:02:41 +00:00
|
|
|
|
2022-12-19 15:53:03 +00:00
|
|
|
for _, cnr := range removed {
|
2022-10-18 21:33:45 +00:00
|
|
|
s.log.Debug("removing redundant trees...", zap.Stringer("cid", cnr))
|
|
|
|
|
2022-11-08 12:32:38 +00:00
|
|
|
err = s.DropTree(ctx, cnr, "")
|
2022-10-18 21:33:45 +00:00
|
|
|
if err != nil {
|
|
|
|
s.log.Error("could not remove redundant tree",
|
|
|
|
zap.Stringer("cid", cnr),
|
|
|
|
zap.Error(err))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s.log.Debug("trees have been synchronized")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-12-10 10:50:08 +00:00
|
|
|
|
|
|
|
// randomizeNodeOrder shuffles nodes and removes not a `pos` index.
|
|
|
|
// It is assumed that 0 <= pos < len(nodes)
|
|
|
|
func randomizeNodeOrder(cnrNodes []netmap.NodeInfo, pos int) []netmap.NodeInfo {
|
|
|
|
if len(cnrNodes) == 1 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
nodes := make([]netmap.NodeInfo, len(cnrNodes)-1)
|
|
|
|
n := copy(nodes, cnrNodes[:pos])
|
|
|
|
copy(nodes[n:], cnrNodes[pos+1:])
|
|
|
|
|
|
|
|
rand.Shuffle(len(nodes), func(i, j int) {
|
|
|
|
nodes[i], nodes[j] = nodes[j], nodes[i]
|
|
|
|
})
|
|
|
|
return nodes
|
|
|
|
}
|