2020-12-23 16:53:11 +00:00
|
|
|
package auditor
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2023-04-06 08:36:25 +00:00
|
|
|
"context"
|
2020-12-23 16:53:11 +00:00
|
|
|
"sync"
|
|
|
|
"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/util/rand"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
|
|
"git.frostfs.info/TrueCloudLab/tzhash/tz"
|
2020-12-23 16:53:11 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2023-04-06 08:36:25 +00:00
|
|
|
func (c *Context) executePDP(ctx context.Context) {
|
|
|
|
c.processPairs(ctx)
|
2020-12-23 16:53:11 +00:00
|
|
|
c.writePairsResult()
|
|
|
|
}
|
|
|
|
|
2023-04-06 08:36:25 +00:00
|
|
|
func (c *Context) processPairs(ctx context.Context) {
|
2020-12-23 16:53:11 +00:00
|
|
|
wg := new(sync.WaitGroup)
|
|
|
|
|
|
|
|
for i := range c.pairs {
|
|
|
|
p := &c.pairs[i]
|
|
|
|
wg.Add(1)
|
|
|
|
|
|
|
|
if err := c.pdpWorkerPool.Submit(func() {
|
2023-04-06 08:36:25 +00:00
|
|
|
c.processPair(ctx, p)
|
2020-12-23 16:53:11 +00:00
|
|
|
wg.Done()
|
|
|
|
}); err != nil {
|
|
|
|
wg.Done()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
2021-10-19 13:34:34 +00:00
|
|
|
c.pdpWorkerPool.Release()
|
2020-12-23 16:53:11 +00:00
|
|
|
}
|
|
|
|
|
2023-04-06 08:36:25 +00:00
|
|
|
func (c *Context) processPair(ctx context.Context, p *gamePair) {
|
2020-12-23 16:53:11 +00:00
|
|
|
c.distributeRanges(p)
|
2023-04-06 08:36:25 +00:00
|
|
|
c.collectHashes(ctx, p)
|
2020-12-23 16:53:11 +00:00
|
|
|
c.analyzeHashes(p)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) distributeRanges(p *gamePair) {
|
|
|
|
p.rn1 = make([]*object.Range, hashRangeNumber-1)
|
|
|
|
p.rn2 = make([]*object.Range, hashRangeNumber-1)
|
|
|
|
|
|
|
|
for i := 0; i < hashRangeNumber-1; i++ {
|
|
|
|
p.rn1[i] = object.NewRange()
|
|
|
|
p.rn2[i] = object.NewRange()
|
|
|
|
}
|
|
|
|
|
|
|
|
notches := c.splitPayload(p.id)
|
|
|
|
|
|
|
|
{ // node 1
|
|
|
|
// [0:n2]
|
|
|
|
p.rn1[0].SetLength(notches[1])
|
|
|
|
|
|
|
|
// [n2:n3]
|
|
|
|
p.rn1[1].SetOffset(notches[1])
|
|
|
|
p.rn1[1].SetLength(notches[2] - notches[1])
|
|
|
|
|
|
|
|
// [n3:full]
|
|
|
|
p.rn1[2].SetOffset(notches[2])
|
|
|
|
p.rn1[2].SetLength(notches[3] - notches[2])
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // node 2
|
|
|
|
// [0:n1]
|
|
|
|
p.rn2[0].SetLength(notches[0])
|
|
|
|
|
|
|
|
// [n1:n2]
|
|
|
|
p.rn2[1].SetOffset(notches[0])
|
|
|
|
p.rn2[1].SetLength(notches[1] - notches[0])
|
|
|
|
|
|
|
|
// [n2:full]
|
|
|
|
p.rn2[2].SetOffset(notches[1])
|
|
|
|
p.rn2[2].SetLength(notches[3] - notches[1])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
func (c *Context) splitPayload(id oid.ID) []uint64 {
|
2020-12-23 16:53:11 +00:00
|
|
|
var (
|
|
|
|
prev uint64
|
|
|
|
size = c.objectSize(id)
|
|
|
|
notches = make([]uint64, 0, hashRangeNumber)
|
|
|
|
)
|
|
|
|
|
|
|
|
for i := uint64(0); i < hashRangeNumber; i++ {
|
|
|
|
if i < hashRangeNumber-1 {
|
2022-01-11 14:07:59 +00:00
|
|
|
max := size - prev - (hashRangeNumber - i)
|
|
|
|
if max == 0 {
|
|
|
|
prev++
|
|
|
|
} else {
|
|
|
|
prev += rand.Uint64()%max + 1
|
|
|
|
}
|
2020-12-23 16:53:11 +00:00
|
|
|
} else {
|
2022-01-11 14:07:59 +00:00
|
|
|
prev = size
|
2020-12-23 16:53:11 +00:00
|
|
|
}
|
|
|
|
|
2022-01-11 14:07:59 +00:00
|
|
|
notches = append(notches, prev)
|
2020-12-23 16:53:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return notches
|
|
|
|
}
|
|
|
|
|
2023-04-06 08:36:25 +00:00
|
|
|
func (c *Context) collectHashes(ctx context.Context, p *gamePair) {
|
2022-06-08 23:18:26 +00:00
|
|
|
fn := func(n netmap.NodeInfo, rngs []*object.Range) [][]byte {
|
2022-03-29 12:48:28 +00:00
|
|
|
// Here we randomize the order a bit: the hypothesis is that this
|
|
|
|
// makes it harder for an unscrupulous node to come up with a
|
|
|
|
// reliable cheating strategy.
|
|
|
|
order := make([]int, len(rngs))
|
|
|
|
for i := range order {
|
|
|
|
order[i] = i
|
|
|
|
}
|
|
|
|
rand.Shuffle(len(order), func(i, j int) { order[i], order[j] = order[j], order[i] })
|
|
|
|
|
2022-05-19 18:26:27 +00:00
|
|
|
var getRangeHashPrm GetRangeHashPrm
|
|
|
|
getRangeHashPrm.CID = c.task.ContainerID()
|
|
|
|
getRangeHashPrm.OID = p.id
|
|
|
|
getRangeHashPrm.Node = n
|
|
|
|
|
2022-03-29 12:48:28 +00:00
|
|
|
res := make([][]byte, len(rngs))
|
|
|
|
for _, i := range order {
|
2022-01-11 14:07:59 +00:00
|
|
|
var sleepDur time.Duration
|
|
|
|
if c.maxPDPSleep > 0 {
|
|
|
|
sleepDur = time.Duration(rand.Uint64() % c.maxPDPSleep)
|
|
|
|
}
|
2020-12-23 16:53:11 +00:00
|
|
|
|
2023-04-12 14:35:10 +00:00
|
|
|
c.log.Debug(logs.AuditorSleepBeforeGetRangeHash,
|
2020-12-23 16:53:11 +00:00
|
|
|
zap.Stringer("interval", sleepDur),
|
|
|
|
)
|
|
|
|
|
2022-01-11 14:07:59 +00:00
|
|
|
time.Sleep(sleepDur)
|
2020-12-23 16:53:11 +00:00
|
|
|
|
2022-05-19 18:26:27 +00:00
|
|
|
getRangeHashPrm.Range = rngs[i]
|
|
|
|
|
2023-04-06 08:36:25 +00:00
|
|
|
h, err := c.cnrCom.GetRangeHash(ctx, getRangeHashPrm)
|
2020-12-23 16:53:11 +00:00
|
|
|
if err != nil {
|
2023-04-12 14:35:10 +00:00
|
|
|
c.log.Debug(logs.AuditorCouldNotGetPayloadRangeHash,
|
2020-12-23 16:53:11 +00:00
|
|
|
zap.Stringer("id", p.id),
|
2022-10-11 12:49:34 +00:00
|
|
|
zap.String("node", netmap.StringifyPublicKey(n)),
|
2020-12-23 16:53:11 +00:00
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
2022-03-29 12:48:28 +00:00
|
|
|
return res
|
2020-12-23 16:53:11 +00:00
|
|
|
}
|
2022-03-29 12:48:28 +00:00
|
|
|
res[i] = h
|
2020-12-23 16:53:11 +00:00
|
|
|
}
|
2022-03-29 12:48:28 +00:00
|
|
|
return res
|
2020-12-23 16:53:11 +00:00
|
|
|
}
|
|
|
|
|
2022-03-29 12:48:28 +00:00
|
|
|
p.hh1 = fn(p.n1, p.rn1)
|
|
|
|
p.hh2 = fn(p.n2, p.rn2)
|
2020-12-23 16:53:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) analyzeHashes(p *gamePair) {
|
|
|
|
if len(p.hh1) != hashRangeNumber-1 || len(p.hh2) != hashRangeNumber-1 {
|
|
|
|
c.failNodesPDP(p.n1, p.n2)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
h1, err := tz.Concat([][]byte{p.hh2[0], p.hh2[1]})
|
|
|
|
if err != nil || !bytes.Equal(p.hh1[0], h1) {
|
|
|
|
c.failNodesPDP(p.n1, p.n2)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
h2, err := tz.Concat([][]byte{p.hh1[1], p.hh1[2]})
|
|
|
|
if err != nil || !bytes.Equal(p.hh2[2], h2) {
|
|
|
|
c.failNodesPDP(p.n1, p.n2)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
fh, err := tz.Concat([][]byte{h1, h2})
|
|
|
|
if err != nil || !bytes.Equal(fh, c.objectHomoHash(p.id)) {
|
|
|
|
c.failNodesPDP(p.n1, p.n2)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.passNodesPDP(p.n1, p.n2)
|
|
|
|
}
|
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
func (c *Context) failNodesPDP(ns ...netmap.NodeInfo) {
|
2020-12-23 16:53:11 +00:00
|
|
|
c.pairedMtx.Lock()
|
|
|
|
|
|
|
|
for i := range ns {
|
|
|
|
c.pairedNodes[ns[i].Hash()].failedPDP = true
|
|
|
|
}
|
|
|
|
|
|
|
|
c.pairedMtx.Unlock()
|
|
|
|
}
|
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
func (c *Context) passNodesPDP(ns ...netmap.NodeInfo) {
|
2020-12-23 16:53:11 +00:00
|
|
|
c.pairedMtx.Lock()
|
|
|
|
|
|
|
|
for i := range ns {
|
|
|
|
c.pairedNodes[ns[i].Hash()].passedPDP = true
|
|
|
|
}
|
|
|
|
|
|
|
|
c.pairedMtx.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Context) writePairsResult() {
|
|
|
|
var failCount, okCount int
|
|
|
|
|
|
|
|
c.iteratePairedNodes(
|
2022-06-08 23:18:26 +00:00
|
|
|
func(netmap.NodeInfo) { failCount++ },
|
|
|
|
func(netmap.NodeInfo) { okCount++ },
|
2020-12-23 16:53:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
failedNodes := make([][]byte, 0, failCount)
|
|
|
|
passedNodes := make([][]byte, 0, okCount)
|
|
|
|
|
|
|
|
c.iteratePairedNodes(
|
2022-06-08 23:18:26 +00:00
|
|
|
func(n netmap.NodeInfo) {
|
2020-12-23 16:53:11 +00:00
|
|
|
failedNodes = append(failedNodes, n.PublicKey())
|
|
|
|
},
|
2022-06-08 23:18:26 +00:00
|
|
|
func(n netmap.NodeInfo) {
|
2020-12-23 16:53:11 +00:00
|
|
|
passedNodes = append(passedNodes, n.PublicKey())
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
|
|
|
c.report.SetPDPResults(passedNodes, failedNodes)
|
|
|
|
}
|
|
|
|
|
2022-06-08 23:18:26 +00:00
|
|
|
func (c *Context) iteratePairedNodes(onFail, onPass func(netmap.NodeInfo)) {
|
2020-12-23 16:53:11 +00:00
|
|
|
for _, pairedNode := range c.pairedNodes {
|
|
|
|
if pairedNode.failedPDP {
|
|
|
|
onFail(pairedNode.node)
|
|
|
|
}
|
|
|
|
|
|
|
|
if pairedNode.passedPDP {
|
|
|
|
onPass(pairedNode.node)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|