go-ethereum/swarm/storage/chunker.go

587 lines
16 KiB
Go
Raw Normal View History

// Copyright 2016 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package storage
import (
"context"
"encoding/binary"
"errors"
"fmt"
"io"
"sync"
"time"
"github.com/ethereum/go-ethereum/metrics"
2018-06-20 15:06:27 +03:00
"github.com/ethereum/go-ethereum/swarm/log"
)
/*
The distributed storage implemented in this package requires fix sized chunks of content.
Chunker is the interface to a component that is responsible for disassembling and assembling larger data.
TreeChunker implements a Chunker based on a tree structure defined as follows:
1 each node in the tree including the root and other branching nodes are stored as a chunk.
2 branching nodes encode data contents that includes the size of the dataslice covered by its entire subtree under the node as well as the hash keys of all its children :
data_{i} := size(subtree_{i}) || key_{j} || key_{j+1} .... || key_{j+n-1}
3 Leaf nodes encode an actual subslice of the input data.
4 if data size is not more than maximum chunksize, the data is stored in a single chunk
key = hash(int64(size) + data)
5 if data size is more than chunksize*branches^l, but no more than chunksize*
branches^(l+1), the data vector is split into slices of chunksize*
branches^l length (except the last one).
key = hash(int64(size) + key(slice0) + key(slice1) + ...)
The underlying hash function is configurable
*/
/*
Tree chunker is a concrete implementation of data chunking.
This chunker works in a simple way, it builds a tree out of the document so that each node either represents a chunk of real data or a chunk of data representing an branching non-leaf node of the tree. In particular each such non-leaf chunk will represent is a concatenation of the hash of its respective children. This scheme simultaneously guarantees data integrity as well as self addressing. Abstract nodes are transparent since their represented size component is strictly greater than their maximum data size, since they encode a subtree.
If all is well it is possible to implement this by simply composing readers so that no extra allocation or buffering is necessary for the data splitting and joining. This means that in principle there can be direct IO between : memory, file system, network socket (bzz peers storage request is read from the socket). In practice there may be need for several stages of internal buffering.
The hashing itself does use extra copies and allocation though, since it does need it.
*/
var (
errAppendOppNotSuported = errors.New("Append operation not supported")
2017-11-08 13:45:52 +03:00
errOperationTimedOut = errors.New("operation timed out")
)
2018-06-20 15:06:27 +03:00
const (
DefaultChunkSize int64 = 4096
)
2018-06-20 15:06:27 +03:00
type ChunkerParams struct {
chunkSize int64
hashSize int64
}
type SplitterParams struct {
ChunkerParams
reader io.Reader
putter Putter
addr Address
}
type TreeSplitterParams struct {
SplitterParams
size int64
}
type JoinerParams struct {
ChunkerParams
addr Address
getter Getter
// TODO: there is a bug, so depth can only be 0 today, see: https://github.com/ethersphere/go-ethereum/issues/344
depth int
}
type TreeChunker struct {
branches int64
hashFunc SwarmHasher
2018-06-20 15:06:27 +03:00
dataSize int64
data io.Reader
// calculated
2018-06-20 15:06:27 +03:00
addr Address
depth int
2017-11-08 13:45:52 +03:00
hashSize int64 // self.hashFunc.New().Size()
chunkSize int64 // hashSize* branches
workerCount int64 // the number of worker routines used
workerLock sync.RWMutex // lock for the worker count
2018-06-20 15:06:27 +03:00
jobC chan *hashJob
wg *sync.WaitGroup
putter Putter
getter Getter
errC chan error
quitC chan bool
}
2018-06-20 15:06:27 +03:00
/*
Join reconstructs original content based on a root key.
When joining, the caller gets returned a Lazy SectionReader, which is
seekable and implements on-demand fetching of chunks as and where it is read.
New chunks to retrieve are coming from the getter, which the caller provides.
If an error is encountered during joining, it appears as a reader error.
The SectionReader.
As a result, partial reads from a document are possible even if other parts
are corrupt or lost.
The chunks are not meant to be validated by the chunker when joining. This
is because it is left to the DPA to decide which sources are trusted.
*/
func TreeJoin(ctx context.Context, addr Address, getter Getter, depth int) *LazyChunkReader {
2018-06-20 15:06:27 +03:00
jp := &JoinerParams{
ChunkerParams: ChunkerParams{
chunkSize: DefaultChunkSize,
hashSize: int64(len(addr)),
},
addr: addr,
getter: getter,
depth: depth,
}
return NewTreeJoiner(jp).Join(ctx)
2018-06-20 15:06:27 +03:00
}
/*
When splitting, data is given as a SectionReader, and the key is a hashSize long byte slice (Key), the root hash of the entire content will fill this once processing finishes.
New chunks to store are store using the putter which the caller provides.
*/
func TreeSplit(ctx context.Context, data io.Reader, size int64, putter Putter) (k Address, wait func(context.Context) error, err error) {
2018-06-20 15:06:27 +03:00
tsp := &TreeSplitterParams{
SplitterParams: SplitterParams{
ChunkerParams: ChunkerParams{
chunkSize: DefaultChunkSize,
hashSize: putter.RefSize(),
},
reader: data,
putter: putter,
},
size: size,
}
return NewTreeSplitter(tsp).Split(ctx)
}
2018-06-20 15:06:27 +03:00
func NewTreeJoiner(params *JoinerParams) *TreeChunker {
tc := &TreeChunker{}
tc.hashSize = params.hashSize
tc.branches = params.chunkSize / params.hashSize
tc.addr = params.addr
tc.getter = params.getter
tc.depth = params.depth
tc.chunkSize = params.chunkSize
tc.workerCount = 0
tc.jobC = make(chan *hashJob, 2*ChunkProcessors)
tc.wg = &sync.WaitGroup{}
tc.errC = make(chan error)
tc.quitC = make(chan bool)
return tc
}
func NewTreeSplitter(params *TreeSplitterParams) *TreeChunker {
tc := &TreeChunker{}
tc.data = params.reader
tc.dataSize = params.size
tc.hashSize = params.hashSize
tc.branches = params.chunkSize / params.hashSize
tc.addr = params.addr
tc.chunkSize = params.chunkSize
tc.putter = params.putter
tc.workerCount = 0
tc.jobC = make(chan *hashJob, 2*ChunkProcessors)
tc.wg = &sync.WaitGroup{}
tc.errC = make(chan error)
tc.quitC = make(chan bool)
return tc
}
// String() for pretty printing
2018-06-20 15:06:27 +03:00
func (c *Chunk) String() string {
return fmt.Sprintf("Key: %v TreeSize: %v Chunksize: %v", c.Addr.Log(), c.Size, len(c.SData))
}
type hashJob struct {
2018-06-20 15:06:27 +03:00
key Address
chunk []byte
size int64
parentWg *sync.WaitGroup
}
2018-06-20 15:06:27 +03:00
func (tc *TreeChunker) incrementWorkerCount() {
tc.workerLock.Lock()
defer tc.workerLock.Unlock()
tc.workerCount += 1
}
2018-06-20 15:06:27 +03:00
func (tc *TreeChunker) getWorkerCount() int64 {
tc.workerLock.RLock()
defer tc.workerLock.RUnlock()
return tc.workerCount
}
2018-06-20 15:06:27 +03:00
func (tc *TreeChunker) decrementWorkerCount() {
tc.workerLock.Lock()
defer tc.workerLock.Unlock()
tc.workerCount -= 1
}
func (tc *TreeChunker) Split(ctx context.Context) (k Address, wait func(context.Context) error, err error) {
2018-06-20 15:06:27 +03:00
if tc.chunkSize <= 0 {
panic("chunker must be initialised")
}
2018-06-20 15:06:27 +03:00
tc.runWorker()
depth := 0
2018-06-20 15:06:27 +03:00
treeSize := tc.chunkSize
// takes lowest depth such that chunksize*HashCount^(depth+1) > size
// power series, will find the order of magnitude of the data size in base hashCount or numbers of levels of branching in the resulting tree.
2018-06-20 15:06:27 +03:00
for ; treeSize < tc.dataSize; treeSize *= tc.branches {
depth++
}
2018-06-20 15:06:27 +03:00
key := make([]byte, tc.hashSize)
// this waitgroup member is released after the root hash is calculated
2018-06-20 15:06:27 +03:00
tc.wg.Add(1)
//launch actual recursive function passing the waitgroups
2018-06-20 15:06:27 +03:00
go tc.split(depth, treeSize/tc.branches, key, tc.dataSize, tc.wg)
// closes internal error channel if all subprocesses in the workgroup finished
go func() {
// waiting for all threads to finish
2018-06-20 15:06:27 +03:00
tc.wg.Wait()
close(tc.errC)
}()
2018-06-20 15:06:27 +03:00
defer close(tc.quitC)
defer tc.putter.Close()
select {
2018-06-20 15:06:27 +03:00
case err := <-tc.errC:
if err != nil {
2018-06-20 15:06:27 +03:00
return nil, nil, err
}
case <-time.NewTimer(splitTimeout).C:
2018-06-20 15:06:27 +03:00
return nil, nil, errOperationTimedOut
}
2017-08-08 20:34:35 +03:00
2018-06-20 15:06:27 +03:00
return key, tc.putter.Wait, nil
}
2018-06-20 15:06:27 +03:00
func (tc *TreeChunker) split(depth int, treeSize int64, addr Address, size int64, parentWg *sync.WaitGroup) {
//
for depth > 0 && size < treeSize {
2018-06-20 15:06:27 +03:00
treeSize /= tc.branches
depth--
}
if depth == 0 {
// leaf nodes -> content chunks
chunkData := make([]byte, size+8)
binary.LittleEndian.PutUint64(chunkData[0:8], uint64(size))
var readBytes int64
for readBytes < size {
2018-06-20 15:06:27 +03:00
n, err := tc.data.Read(chunkData[8+readBytes:])
readBytes += int64(n)
if err != nil && !(err == io.EOF && readBytes == size) {
2018-06-20 15:06:27 +03:00
tc.errC <- err
return
}
}
select {
2018-06-20 15:06:27 +03:00
case tc.jobC <- &hashJob{addr, chunkData, size, parentWg}:
case <-tc.quitC:
}
return
}
// dept > 0
// intermediate chunk containing child nodes hashes
branchCnt := (size + treeSize - 1) / treeSize
2018-06-20 15:06:27 +03:00
var chunk = make([]byte, branchCnt*tc.hashSize+8)
var pos, i int64
binary.LittleEndian.PutUint64(chunk[0:8], uint64(size))
childrenWg := &sync.WaitGroup{}
var secSize int64
for i < branchCnt {
// the last item can have shorter data
if size-pos < treeSize {
secSize = size - pos
} else {
secSize = treeSize
}
// the hash of that data
2018-06-20 15:06:27 +03:00
subTreeKey := chunk[8+i*tc.hashSize : 8+(i+1)*tc.hashSize]
childrenWg.Add(1)
2018-06-20 15:06:27 +03:00
tc.split(depth-1, treeSize/tc.branches, subTreeKey, secSize, childrenWg)
i++
pos += treeSize
}
// wait for all the children to complete calculating their hashes and copying them onto sections of the chunk
// parentWg.Add(1)
// go func() {
childrenWg.Wait()
2018-06-20 15:06:27 +03:00
worker := tc.getWorkerCount()
if int64(len(tc.jobC)) > worker && worker < ChunkProcessors {
tc.runWorker()
}
select {
2018-06-20 15:06:27 +03:00
case tc.jobC <- &hashJob{addr, chunk, size, parentWg}:
case <-tc.quitC:
}
}
2018-06-20 15:06:27 +03:00
func (tc *TreeChunker) runWorker() {
tc.incrementWorkerCount()
go func() {
defer tc.decrementWorkerCount()
for {
select {
2018-06-20 15:06:27 +03:00
case job, ok := <-tc.jobC:
if !ok {
return
}
2018-06-20 15:06:27 +03:00
h, err := tc.putter.Put(job.chunk)
if err != nil {
tc.errC <- err
return
}
copy(job.key, h)
job.parentWg.Done()
case <-tc.quitC:
return
}
}
2018-06-20 15:06:27 +03:00
}()
}
2018-06-20 15:06:27 +03:00
func (tc *TreeChunker) Append() (Address, func(), error) {
return nil, nil, errAppendOppNotSuported
}
// LazyChunkReader implements LazySectionReader
type LazyChunkReader struct {
2018-06-20 15:06:27 +03:00
key Address // root key
chunkData ChunkData
off int64 // offset
chunkSize int64 // inherit from chunker
branches int64 // inherit from chunker
hashSize int64 // inherit from chunker
depth int
getter Getter
}
func (tc *TreeChunker) Join(ctx context.Context) *LazyChunkReader {
return &LazyChunkReader{
2018-06-20 15:06:27 +03:00
key: tc.addr,
chunkSize: tc.chunkSize,
branches: tc.branches,
hashSize: tc.hashSize,
depth: tc.depth,
getter: tc.getter,
}
}
// Size is meant to be called on the LazySectionReader
2018-06-20 15:06:27 +03:00
func (r *LazyChunkReader) Size(quitC chan bool) (n int64, err error) {
metrics.GetOrRegisterCounter("lazychunkreader.size", nil).Inc(1)
log.Debug("lazychunkreader.size", "key", r.key)
if r.chunkData == nil {
chunkData, err := r.getter.Get(Reference(r.key))
if err != nil {
return 0, err
}
2018-06-20 15:06:27 +03:00
if chunkData == nil {
select {
case <-quitC:
return 0, errors.New("aborted")
default:
return 0, fmt.Errorf("root chunk not found for %v", r.key.Hex())
}
}
r.chunkData = chunkData
}
2018-06-20 15:06:27 +03:00
return r.chunkData.Size(), nil
}
// read at can be called numerous times
// concurrent reads are allowed
// Size() needs to be called synchronously on the LazyChunkReader first
2018-06-20 15:06:27 +03:00
func (r *LazyChunkReader) ReadAt(b []byte, off int64) (read int, err error) {
metrics.GetOrRegisterCounter("lazychunkreader.readat", nil).Inc(1)
// this is correct, a swarm doc cannot be zero length, so no EOF is expected
if len(b) == 0 {
return 0, nil
}
quitC := make(chan bool)
2018-06-20 15:06:27 +03:00
size, err := r.Size(quitC)
if err != nil {
2018-06-20 15:06:27 +03:00
log.Error("lazychunkreader.readat.size", "size", size, "err", err)
return 0, err
}
errC := make(chan error)
// }
var treeSize int64
var depth int
// calculate depth and max treeSize
2018-06-20 15:06:27 +03:00
treeSize = r.chunkSize
for ; treeSize < size; treeSize *= r.branches {
depth++
}
wg := sync.WaitGroup{}
2018-06-20 15:06:27 +03:00
length := int64(len(b))
for d := 0; d < r.depth; d++ {
off *= r.chunkSize
length *= r.chunkSize
}
wg.Add(1)
2018-06-20 15:06:27 +03:00
go r.join(b, off, off+length, depth, treeSize/r.branches, r.chunkData, &wg, errC, quitC)
go func() {
wg.Wait()
close(errC)
}()
err = <-errC
if err != nil {
2018-06-20 15:06:27 +03:00
log.Error("lazychunkreader.readat.errc", "err", err)
close(quitC)
return 0, err
}
if off+int64(len(b)) >= size {
2018-06-20 15:06:27 +03:00
return int(size - off), io.EOF
}
return len(b), nil
}
2018-06-20 15:06:27 +03:00
func (r *LazyChunkReader) join(b []byte, off int64, eoff int64, depth int, treeSize int64, chunkData ChunkData, parentWg *sync.WaitGroup, errC chan error, quitC chan bool) {
defer parentWg.Done()
// find appropriate block level
2018-06-20 15:06:27 +03:00
for chunkData.Size() < treeSize && depth > r.depth {
treeSize /= r.branches
depth--
}
// leaf chunk found
2018-06-20 15:06:27 +03:00
if depth == r.depth {
extra := 8 + eoff - int64(len(chunkData))
if extra > 0 {
eoff -= extra
}
2018-06-20 15:06:27 +03:00
copy(b, chunkData[8+off:8+eoff])
return // simply give back the chunks reader for content chunks
}
// subtree
start := off / treeSize
end := (eoff + treeSize - 1) / treeSize
2018-06-20 15:06:27 +03:00
// last non-leaf chunk can be shorter than default chunk size, let's not read it further then its end
currentBranches := int64(len(chunkData)-8) / r.hashSize
if end > currentBranches {
end = currentBranches
}
wg := &sync.WaitGroup{}
defer wg.Wait()
for i := start; i < end; i++ {
soff := i * treeSize
roff := soff
seoff := soff + treeSize
if soff < off {
soff = off
}
if seoff > eoff {
seoff = eoff
}
if depth > 1 {
wg.Wait()
}
wg.Add(1)
go func(j int64) {
2018-06-20 15:06:27 +03:00
childKey := chunkData[8+j*r.hashSize : 8+(j+1)*r.hashSize]
chunkData, err := r.getter.Get(Reference(childKey))
if err != nil {
log.Error("lazychunkreader.join", "key", fmt.Sprintf("%x", childKey), "err", err)
select {
case errC <- fmt.Errorf("chunk %v-%v not found; key: %s", off, off+treeSize, fmt.Sprintf("%x", childKey)):
case <-quitC:
}
return
}
if l := len(chunkData); l < 9 {
select {
2018-06-20 15:06:27 +03:00
case errC <- fmt.Errorf("chunk %v-%v incomplete; key: %s, data length %v", off, off+treeSize, fmt.Sprintf("%x", childKey), l):
case <-quitC:
}
return
}
if soff < off {
soff = off
}
2018-06-20 15:06:27 +03:00
r.join(b[soff-off:seoff-off], soff-roff, seoff-roff, depth-1, treeSize/r.branches, chunkData, wg, errC, quitC)
}(i)
} //for
}
2018-06-20 15:06:27 +03:00
// Read keeps a cursor so cannot be called simulateously, see ReadAt
func (r *LazyChunkReader) Read(b []byte) (read int, err error) {
log.Debug("lazychunkreader.read", "key", r.key)
metrics.GetOrRegisterCounter("lazychunkreader.read", nil).Inc(1)
read, err = r.ReadAt(b, r.off)
if err != nil && err != io.EOF {
log.Error("lazychunkreader.readat", "read", read, "err", err)
metrics.GetOrRegisterCounter("lazychunkreader.read.err", nil).Inc(1)
}
2018-06-20 15:06:27 +03:00
metrics.GetOrRegisterCounter("lazychunkreader.read.bytes", nil).Inc(int64(read))
2018-06-20 15:06:27 +03:00
r.off += int64(read)
return
}
// completely analogous to standard SectionReader implementation
var errWhence = errors.New("Seek: invalid whence")
var errOffset = errors.New("Seek: invalid offset")
2018-06-20 15:06:27 +03:00
func (r *LazyChunkReader) Seek(offset int64, whence int) (int64, error) {
log.Debug("lazychunkreader.seek", "key", r.key, "offset", offset)
switch whence {
default:
return 0, errWhence
case 0:
offset += 0
case 1:
2018-06-20 15:06:27 +03:00
offset += r.off
case 2:
2018-06-20 15:06:27 +03:00
if r.chunkData == nil { //seek from the end requires rootchunk for size. call Size first
_, err := r.Size(nil)
if err != nil {
return 0, fmt.Errorf("can't get size: %v", err)
}
}
2018-06-20 15:06:27 +03:00
offset += r.chunkData.Size()
}
if offset < 0 {
return 0, errOffset
}
2018-06-20 15:06:27 +03:00
r.off = offset
return offset, nil
}