Commit b5134faf authored by acud's avatar acud Committed by GitHub

10/12 part 2: remove spancontext dependency, remove mockstore, migrate sctx (#91)

* add sctx, remove globalstore, remove go-ethereum dependency, disable cgo on build
parent 9d8925d2
......@@ -10,7 +10,7 @@ endif
all: build lint vet test binary
.PHONY: binary
binary: export CGO_ENABLED=1 # set to 0 when go-ethereum/metrics dependecy is removed
binary: export CGO_ENABLED=0
binary: dist FORCE
$(GO) version
$(GO) build -trimpath -ldflags "$(LDFLAGS)" -o dist/bee ./cmd/bee
......
......@@ -6,7 +6,6 @@ require (
github.com/btcsuite/btcd v0.20.1-beta
github.com/coreos/go-semver v0.3.0
github.com/dgraph-io/badger/v2 v2.0.3
github.com/ethereum/go-ethereum v1.9.2
github.com/ethersphere/swarm v0.5.7
github.com/gogo/protobuf v1.3.1
github.com/gorilla/handlers v1.4.2
......
// Copyright 2020 The Swarm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.package storage
package sctx
import "context"
type (
HTTPRequestIDKey struct{}
requestHostKey struct{}
tagKey struct{}
)
// SetHost sets the http request host in the context
func SetHost(ctx context.Context, domain string) context.Context {
return context.WithValue(ctx, requestHostKey{}, domain)
}
// GetHost gets the request host from the context
func GetHost(ctx context.Context) string {
v, ok := ctx.Value(requestHostKey{}).(string)
if ok {
return v
}
return ""
}
// SetTag sets the tag unique identifier in the context
func SetTag(ctx context.Context, tagId uint32) context.Context {
return context.WithValue(ctx, tagKey{}, tagId)
}
// GetTag gets the tag unique identifier from the context
func GetTag(ctx context.Context) uint32 {
v, ok := ctx.Value(tagKey{}).(uint32)
if ok {
return v
}
return 0
}
......@@ -29,7 +29,6 @@ import (
"github.com/ethersphere/bee/pkg/swarm"
"github.com/ethersphere/bee/pkg/tags"
"github.com/ethersphere/swarm/shed"
"github.com/ethersphere/swarm/storage/mock"
"github.com/prometheus/client_golang/prometheus"
)
......@@ -132,12 +131,6 @@ type DB struct {
// Options struct holds optional parameters for configuring DB.
type Options struct {
// MockStore is a mock node store that is used to store
// chunk data in a central store. It can be used to reduce
// total storage space requirements in testing large number
// of swarm nodes with chunk data deduplication provided by
// the mock global store.
MockStore *mock.NodeStore
// Capacity is a limit that triggers garbage collection when
// number of items in gcIndex equals or exceeds it.
Capacity uint64
......@@ -220,54 +213,29 @@ func New(path string, baseKey []byte, o *Options, logger logging.Logger) (db *DB
if err != nil {
return nil, err
}
// Functions for retrieval data index.
var (
encodeValueFunc func(fields shed.Item) (value []byte, err error)
decodeValueFunc func(keyItem shed.Item, value []byte) (e shed.Item, err error)
)
if o.MockStore != nil {
encodeValueFunc = func(fields shed.Item) (value []byte, err error) {
b := make([]byte, 16)
binary.BigEndian.PutUint64(b[:8], fields.BinID)
binary.BigEndian.PutUint64(b[8:16], uint64(fields.StoreTimestamp))
err = o.MockStore.Put(fields.Address, fields.Data)
if err != nil {
return nil, err
}
return b, nil
}
decodeValueFunc = func(keyItem shed.Item, value []byte) (e shed.Item, err error) {
e.StoreTimestamp = int64(binary.BigEndian.Uint64(value[8:16]))
e.BinID = binary.BigEndian.Uint64(value[:8])
e.Data, err = o.MockStore.Get(keyItem.Address)
return e, err
}
} else {
encodeValueFunc = func(fields shed.Item) (value []byte, err error) {
// Index storing actual chunk address, data and bin id.
db.retrievalDataIndex, err = db.shed.NewIndex("Address->StoreTimestamp|BinID|Data", shed.IndexFuncs{
EncodeKey: func(fields shed.Item) (key []byte, err error) {
return fields.Address, nil
},
DecodeKey: func(key []byte) (e shed.Item, err error) {
e.Address = key
return e, nil
},
EncodeValue: func(fields shed.Item) (value []byte, err error) {
b := make([]byte, 16)
binary.BigEndian.PutUint64(b[:8], fields.BinID)
binary.BigEndian.PutUint64(b[8:16], uint64(fields.StoreTimestamp))
value = append(b, fields.Data...)
return value, nil
}
decodeValueFunc = func(keyItem shed.Item, value []byte) (e shed.Item, err error) {
},
DecodeValue: func(keyItem shed.Item, value []byte) (e shed.Item, err error) {
e.StoreTimestamp = int64(binary.BigEndian.Uint64(value[8:16]))
e.BinID = binary.BigEndian.Uint64(value[:8])
e.Data = value[16:]
return e, nil
}
}
// Index storing actual chunk address, data and bin id.
db.retrievalDataIndex, err = db.shed.NewIndex("Address->StoreTimestamp|BinID|Data", shed.IndexFuncs{
EncodeKey: func(fields shed.Item) (key []byte, err error) {
return fields.Address, nil
},
DecodeKey: func(key []byte) (e shed.Item, err error) {
e.Address = key
return e, nil
},
EncodeValue: encodeValueFunc,
DecodeValue: decodeValueFunc,
})
if err != nil {
return nil, err
......
......@@ -25,7 +25,7 @@ import (
"time"
"github.com/ethersphere/bee/pkg/swarm"
"github.com/ethersphere/swarm/spancontext"
"github.com/ethersphere/bee/pkg/tracing"
"github.com/opentracing/opentracing-go"
)
......@@ -69,7 +69,7 @@ type Tag struct {
}
// NewTag creates a new tag, and returns it
func NewTag(uid uint32, s string, total int64, anon bool) *Tag {
func NewTag(ctx context.Context, uid uint32, s string, total int64, anon bool, tracer *tracing.Tracer) *Tag {
t := &Tag{
Uid: uid,
Anonymous: anon,
......@@ -80,7 +80,7 @@ func NewTag(uid uint32, s string, total int64, anon bool) *Tag {
// context here is used only to store the root span `new.upload.tag` within Tag,
// we don't need any type of ctx Deadline or cancellation for this particular ctx
t.ctx, t.span = spancontext.StartSpan(context.Background(), "new.upload.tag")
t.span, _, t.ctx = tracer.StartSpanFromContext(ctx, "new.upload.tag", nil)
return t
}
......
......@@ -17,6 +17,7 @@
package tags
import (
"context"
"sync"
"testing"
"time"
......@@ -184,7 +185,7 @@ func TestTagsMultipleConcurrentIncrementsSyncMap(t *testing.T) {
// TestMarshallingWithAddr tests that marshalling and unmarshalling is done correctly when the
// tag Address (byte slice) contains some arbitrary value
func TestMarshallingWithAddr(t *testing.T) {
tg := NewTag(111, "test/tag", 10, false)
tg := NewTag(context.Background(), 111, "test/tag", 10, false, nil)
tg.Address = swarm.NewAddress([]byte{0, 1, 2, 3, 4, 5, 6})
for _, f := range allStates {
......@@ -235,7 +236,7 @@ func TestMarshallingWithAddr(t *testing.T) {
// TestMarshallingNoAddress tests that marshalling and unmarshalling is done correctly
func TestMarshallingNoAddr(t *testing.T) {
tg := NewTag(111, "test/tag", 10, false)
tg := NewTag(context.Background(), 111, "test/tag", 10, false, nil)
for _, f := range allStates {
tg.Inc(f)
}
......
......@@ -26,8 +26,8 @@ import (
"sync"
"time"
"github.com/ethersphere/bee/pkg/sctx"
"github.com/ethersphere/bee/pkg/swarm"
"github.com/ethersphere/swarm/sctx"
)
var (
......@@ -50,7 +50,7 @@ func NewTags() *Tags {
// Create creates a new tag, stores it by the name and returns it
// it returns an error if the tag with this name already exists
func (ts *Tags) Create(s string, total int64, anon bool) (*Tag, error) {
t := NewTag(TagUidFunc(), s, total, anon)
t := NewTag(context.Background(), TagUidFunc(), s, total, anon, nil)
if _, loaded := ts.tags.LoadOrStore(t.Uid, t); loaded {
return nil, errExists
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment