2016-08-29 14:18:00 -05:00
|
|
|
// 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 (
|
2018-07-13 10:40:28 -05:00
|
|
|
"context"
|
2016-08-29 14:18:00 -05:00
|
|
|
"time"
|
|
|
|
|
2018-06-20 07:06:27 -05:00
|
|
|
"github.com/ethereum/go-ethereum/swarm/log"
|
2018-07-13 10:40:28 -05:00
|
|
|
"github.com/ethereum/go-ethereum/swarm/spancontext"
|
|
|
|
opentracing "github.com/opentracing/opentracing-go"
|
2016-08-29 14:18:00 -05:00
|
|
|
)
|
|
|
|
|
2018-06-20 07:06:27 -05:00
|
|
|
var (
|
|
|
|
// NetStore.Get timeout for get and get retries
|
|
|
|
// This is the maximum period that the Get will block.
|
|
|
|
// If it is reached, Get will return ErrChunkNotFound.
|
|
|
|
netStoreRetryTimeout = 30 * time.Second
|
|
|
|
// Minimal period between calling get method on NetStore
|
|
|
|
// on retry. It protects calling get very frequently if
|
|
|
|
// it returns ErrChunkNotFound very fast.
|
|
|
|
netStoreMinRetryDelay = 3 * time.Second
|
|
|
|
// Timeout interval before retrieval is timed out.
|
|
|
|
// It is used in NetStore.get on waiting for ReqC to be
|
|
|
|
// closed on a single retrieve request.
|
|
|
|
searchTimeout = 10 * time.Second
|
|
|
|
)
|
|
|
|
|
|
|
|
// NetStore implements the ChunkStore interface,
|
|
|
|
// this chunk access layer assumed 2 chunk stores
|
|
|
|
// local storage eg. LocalStore and network storage eg., NetStore
|
|
|
|
// access by calling network is blocking with a timeout
|
2016-08-29 14:18:00 -05:00
|
|
|
type NetStore struct {
|
|
|
|
localStore *LocalStore
|
2018-07-13 10:40:28 -05:00
|
|
|
retrieve func(ctx context.Context, chunk *Chunk) error
|
2016-08-29 14:18:00 -05:00
|
|
|
}
|
|
|
|
|
2018-07-13 10:40:28 -05:00
|
|
|
func NewNetStore(localStore *LocalStore, retrieve func(ctx context.Context, chunk *Chunk) error) *NetStore {
|
2018-06-20 07:06:27 -05:00
|
|
|
return &NetStore{localStore, retrieve}
|
2016-08-29 14:18:00 -05:00
|
|
|
}
|
|
|
|
|
2018-06-20 07:06:27 -05:00
|
|
|
// Get is the entrypoint for local retrieve requests
|
|
|
|
// waits for response or times out
|
|
|
|
//
|
|
|
|
// Get uses get method to retrieve request, but retries if the
|
|
|
|
// ErrChunkNotFound is returned by get, until the netStoreRetryTimeout
|
|
|
|
// is reached.
|
2018-07-13 10:40:28 -05:00
|
|
|
func (ns *NetStore) Get(ctx context.Context, addr Address) (chunk *Chunk, err error) {
|
|
|
|
|
|
|
|
var sp opentracing.Span
|
|
|
|
ctx, sp = spancontext.StartSpan(
|
|
|
|
ctx,
|
|
|
|
"netstore.get.global")
|
|
|
|
defer sp.Finish()
|
|
|
|
|
2018-06-20 07:06:27 -05:00
|
|
|
timer := time.NewTimer(netStoreRetryTimeout)
|
|
|
|
defer timer.Stop()
|
2016-08-29 14:18:00 -05:00
|
|
|
|
2018-06-20 07:06:27 -05:00
|
|
|
// result and resultC provide results from the goroutine
|
|
|
|
// where NetStore.get is called.
|
|
|
|
type result struct {
|
|
|
|
chunk *Chunk
|
|
|
|
err error
|
2016-08-29 14:18:00 -05:00
|
|
|
}
|
2018-06-20 07:06:27 -05:00
|
|
|
resultC := make(chan result)
|
2016-08-29 14:18:00 -05:00
|
|
|
|
2018-06-20 07:06:27 -05:00
|
|
|
// quitC ensures that retring goroutine is terminated
|
|
|
|
// when this function returns.
|
|
|
|
quitC := make(chan struct{})
|
|
|
|
defer close(quitC)
|
2017-12-11 15:56:06 -06:00
|
|
|
|
2018-06-20 07:06:27 -05:00
|
|
|
// do retries in a goroutine so that the timer can
|
|
|
|
// force this method to return after the netStoreRetryTimeout.
|
|
|
|
go func() {
|
|
|
|
// limiter ensures that NetStore.get is not called more frequently
|
|
|
|
// then netStoreMinRetryDelay. If NetStore.get takes longer
|
|
|
|
// then netStoreMinRetryDelay, the next retry call will be
|
|
|
|
// without a delay.
|
|
|
|
limiter := time.NewTimer(netStoreMinRetryDelay)
|
|
|
|
defer limiter.Stop()
|
|
|
|
|
|
|
|
for {
|
2018-07-13 10:40:28 -05:00
|
|
|
chunk, err := ns.get(ctx, addr, 0)
|
2018-06-20 07:06:27 -05:00
|
|
|
if err != ErrChunkNotFound {
|
|
|
|
// break retry only if the error is nil
|
|
|
|
// or other error then ErrChunkNotFound
|
|
|
|
select {
|
|
|
|
case <-quitC:
|
|
|
|
// Maybe NetStore.Get function has returned
|
|
|
|
// by the timer.C while we were waiting for the
|
|
|
|
// results. Terminate this goroutine.
|
|
|
|
case resultC <- result{chunk: chunk, err: err}:
|
|
|
|
// Send the result to the parrent goroutine.
|
|
|
|
}
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-quitC:
|
|
|
|
// NetStore.Get function has returned, possibly
|
|
|
|
// by the timer.C, which makes this goroutine
|
|
|
|
// not needed.
|
|
|
|
return
|
|
|
|
case <-limiter.C:
|
|
|
|
}
|
|
|
|
// Reset the limiter for the next iteration.
|
|
|
|
limiter.Reset(netStoreMinRetryDelay)
|
|
|
|
log.Debug("NetStore.Get retry chunk", "key", addr)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case r := <-resultC:
|
|
|
|
return r.chunk, r.err
|
|
|
|
case <-timer.C:
|
|
|
|
return nil, ErrChunkNotFound
|
2016-08-29 14:18:00 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-20 07:06:27 -05:00
|
|
|
// GetWithTimeout makes a single retrieval attempt for a chunk with a explicit timeout parameter
|
2018-07-13 10:40:28 -05:00
|
|
|
func (ns *NetStore) GetWithTimeout(ctx context.Context, addr Address, timeout time.Duration) (chunk *Chunk, err error) {
|
|
|
|
return ns.get(ctx, addr, timeout)
|
2018-06-20 07:06:27 -05:00
|
|
|
}
|
2016-08-29 14:18:00 -05:00
|
|
|
|
2018-07-13 10:40:28 -05:00
|
|
|
func (ns *NetStore) get(ctx context.Context, addr Address, timeout time.Duration) (chunk *Chunk, err error) {
|
2018-06-20 07:06:27 -05:00
|
|
|
if timeout == 0 {
|
|
|
|
timeout = searchTimeout
|
2016-08-29 14:18:00 -05:00
|
|
|
}
|
2018-07-13 10:40:28 -05:00
|
|
|
|
|
|
|
var sp opentracing.Span
|
|
|
|
ctx, sp = spancontext.StartSpan(
|
|
|
|
ctx,
|
|
|
|
"netstore.get")
|
|
|
|
defer sp.Finish()
|
|
|
|
|
2018-06-20 07:06:27 -05:00
|
|
|
if ns.retrieve == nil {
|
2018-07-13 10:40:28 -05:00
|
|
|
chunk, err = ns.localStore.Get(ctx, addr)
|
2018-06-20 07:06:27 -05:00
|
|
|
if err == nil {
|
|
|
|
return chunk, nil
|
|
|
|
}
|
|
|
|
if err != ErrFetching {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var created bool
|
2018-07-13 10:40:28 -05:00
|
|
|
chunk, created = ns.localStore.GetOrCreateRequest(ctx, addr)
|
2018-06-20 07:06:27 -05:00
|
|
|
|
|
|
|
if chunk.ReqC == nil {
|
|
|
|
return chunk, nil
|
|
|
|
}
|
2016-08-29 14:18:00 -05:00
|
|
|
|
2018-06-20 07:06:27 -05:00
|
|
|
if created {
|
2018-07-13 10:40:28 -05:00
|
|
|
err := ns.retrieve(ctx, chunk)
|
2018-06-20 07:06:27 -05:00
|
|
|
if err != nil {
|
|
|
|
// mark chunk request as failed so that we can retry it later
|
|
|
|
chunk.SetErrored(ErrChunkUnavailable)
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-08-29 14:18:00 -05:00
|
|
|
}
|
|
|
|
}
|
2018-06-20 07:06:27 -05:00
|
|
|
|
|
|
|
t := time.NewTicker(timeout)
|
|
|
|
defer t.Stop()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-t.C:
|
|
|
|
// mark chunk request as failed so that we can retry
|
|
|
|
chunk.SetErrored(ErrChunkNotFound)
|
|
|
|
return nil, ErrChunkNotFound
|
|
|
|
case <-chunk.ReqC:
|
|
|
|
}
|
|
|
|
chunk.SetErrored(nil)
|
2016-08-29 14:18:00 -05:00
|
|
|
return chunk, nil
|
|
|
|
}
|
2017-02-08 11:01:12 -06:00
|
|
|
|
2018-06-20 07:06:27 -05:00
|
|
|
// Put is the entrypoint for local store requests coming from storeLoop
|
2018-07-13 10:40:28 -05:00
|
|
|
func (ns *NetStore) Put(ctx context.Context, chunk *Chunk) {
|
|
|
|
ns.localStore.Put(ctx, chunk)
|
2018-06-20 07:06:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close chunk store
|
|
|
|
func (ns *NetStore) Close() {
|
|
|
|
ns.localStore.Close()
|
|
|
|
}
|