2020-08-25 13:37:10 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-08-25 14:44:41 +00:00
|
|
|
"context"
|
2020-09-23 13:28:42 +00:00
|
|
|
"errors"
|
2020-09-23 13:33:54 +00:00
|
|
|
"sync"
|
2020-08-25 14:44:41 +00:00
|
|
|
|
2020-09-23 13:33:54 +00:00
|
|
|
"github.com/mr-tron/base58"
|
2020-09-23 13:55:46 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
|
2020-08-25 14:44:41 +00:00
|
|
|
"github.com/nspcc-dev/neofs-api-go/v2/object"
|
2020-08-25 13:37:10 +00:00
|
|
|
objectGRPC "github.com/nspcc-dev/neofs-api-go/v2/object/grpc"
|
2020-10-03 10:14:09 +00:00
|
|
|
objectCore "github.com/nspcc-dev/neofs-node/pkg/core/object"
|
2020-09-23 13:33:54 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/bucket"
|
2020-09-23 13:55:46 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/localstore"
|
2020-10-21 09:28:42 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/morph/event"
|
2020-08-25 13:37:10 +00:00
|
|
|
objectTransportGRPC "github.com/nspcc-dev/neofs-node/pkg/network/transport/object/grpc"
|
|
|
|
objectService "github.com/nspcc-dev/neofs-node/pkg/services/object"
|
2020-10-02 11:41:29 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/object/acl"
|
2020-10-03 07:46:57 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/object/acl/eacl"
|
2020-09-23 13:55:46 +00:00
|
|
|
deletesvc "github.com/nspcc-dev/neofs-node/pkg/services/object/delete"
|
|
|
|
deletesvcV2 "github.com/nspcc-dev/neofs-node/pkg/services/object/delete/v2"
|
|
|
|
getsvc "github.com/nspcc-dev/neofs-node/pkg/services/object/get"
|
|
|
|
getsvcV2 "github.com/nspcc-dev/neofs-node/pkg/services/object/get/v2"
|
|
|
|
headsvc "github.com/nspcc-dev/neofs-node/pkg/services/object/head"
|
|
|
|
headsvcV2 "github.com/nspcc-dev/neofs-node/pkg/services/object/head/v2"
|
|
|
|
putsvc "github.com/nspcc-dev/neofs-node/pkg/services/object/put"
|
|
|
|
putsvcV2 "github.com/nspcc-dev/neofs-node/pkg/services/object/put/v2"
|
|
|
|
rangesvc "github.com/nspcc-dev/neofs-node/pkg/services/object/range"
|
|
|
|
rangesvcV2 "github.com/nspcc-dev/neofs-node/pkg/services/object/range/v2"
|
|
|
|
rangehashsvc "github.com/nspcc-dev/neofs-node/pkg/services/object/rangehash"
|
|
|
|
rangehashsvcV2 "github.com/nspcc-dev/neofs-node/pkg/services/object/rangehash/v2"
|
|
|
|
searchsvc "github.com/nspcc-dev/neofs-node/pkg/services/object/search"
|
|
|
|
searchsvcV2 "github.com/nspcc-dev/neofs-node/pkg/services/object/search/v2"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/object/util"
|
2020-10-03 10:26:42 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/object_manager/gc"
|
2020-10-21 09:28:42 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/object_manager/placement"
|
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/services/policer"
|
2020-09-23 13:55:46 +00:00
|
|
|
"github.com/panjf2000/ants/v2"
|
2020-10-22 14:21:28 +00:00
|
|
|
"go.uber.org/zap"
|
2020-08-25 13:37:10 +00:00
|
|
|
)
|
|
|
|
|
2020-09-23 13:42:50 +00:00
|
|
|
type objectSvc struct {
|
2020-09-23 13:55:46 +00:00
|
|
|
put *putsvcV2.Service
|
2020-09-23 13:42:50 +00:00
|
|
|
|
2020-09-23 13:55:46 +00:00
|
|
|
search *searchsvcV2.Service
|
2020-09-23 13:42:50 +00:00
|
|
|
|
2020-09-23 13:55:46 +00:00
|
|
|
head *headsvcV2.Service
|
|
|
|
|
|
|
|
get *getsvcV2.Service
|
|
|
|
|
|
|
|
rng *rangesvcV2.Service
|
|
|
|
|
|
|
|
rngHash *rangehashsvcV2.Service
|
|
|
|
|
|
|
|
delete *deletesvcV2.Service
|
2020-09-23 13:42:50 +00:00
|
|
|
}
|
2020-08-25 14:44:41 +00:00
|
|
|
|
2020-09-23 13:33:54 +00:00
|
|
|
type inMemBucket struct {
|
|
|
|
bucket.Bucket
|
|
|
|
*sync.RWMutex
|
|
|
|
items map[string][]byte
|
|
|
|
}
|
|
|
|
|
2020-10-22 14:21:28 +00:00
|
|
|
func (c *cfg) MaxObjectSize() uint64 {
|
|
|
|
sz, err := c.cfgNetmap.wrapper.MaxObjectSize()
|
|
|
|
if err != nil {
|
|
|
|
c.log.Error("could not get max object size value",
|
|
|
|
zap.String("error", err.Error()),
|
|
|
|
)
|
|
|
|
}
|
2020-09-23 13:35:40 +00:00
|
|
|
|
2020-10-22 14:21:28 +00:00
|
|
|
return sz
|
2020-09-23 13:35:40 +00:00
|
|
|
}
|
|
|
|
|
2020-09-23 13:33:54 +00:00
|
|
|
func newBucket() bucket.Bucket {
|
|
|
|
return &inMemBucket{
|
|
|
|
RWMutex: new(sync.RWMutex),
|
|
|
|
items: map[string][]byte{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-03 09:58:18 +00:00
|
|
|
func (b *inMemBucket) Del(key []byte) error {
|
|
|
|
b.Lock()
|
|
|
|
delete(b.items, base58.Encode(key))
|
|
|
|
b.Unlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-09-23 13:33:54 +00:00
|
|
|
func (b *inMemBucket) Get(key []byte) ([]byte, error) {
|
|
|
|
b.RLock()
|
|
|
|
v, ok := b.items[base58.Encode(key)]
|
|
|
|
b.RUnlock()
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("not found")
|
|
|
|
}
|
|
|
|
|
|
|
|
return v, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *inMemBucket) Set(key, value []byte) error {
|
|
|
|
k := base58.Encode(key)
|
|
|
|
|
|
|
|
b.Lock()
|
|
|
|
b.items[k] = makeCopy(value)
|
|
|
|
b.Unlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *inMemBucket) Iterate(handler bucket.FilterHandler) error {
|
|
|
|
if handler == nil {
|
|
|
|
return bucket.ErrNilFilterHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
b.RLock()
|
|
|
|
for key, val := range b.items {
|
|
|
|
k, err := base58.Decode(key)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
v := makeCopy(val)
|
|
|
|
|
|
|
|
if !handler(k, v) {
|
|
|
|
return bucket.ErrIteratingAborted
|
|
|
|
}
|
|
|
|
}
|
|
|
|
b.RUnlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func makeCopy(val []byte) []byte {
|
|
|
|
tmp := make([]byte, len(val))
|
|
|
|
copy(tmp, val)
|
|
|
|
|
|
|
|
return tmp
|
|
|
|
}
|
|
|
|
|
2020-09-23 13:42:50 +00:00
|
|
|
func (s *objectSvc) Put(ctx context.Context) (object.PutObjectStreamer, error) {
|
|
|
|
return s.put.Put(ctx)
|
2020-08-25 15:45:36 +00:00
|
|
|
}
|
|
|
|
|
2020-09-23 13:42:50 +00:00
|
|
|
func (s *objectSvc) Head(ctx context.Context, req *object.HeadRequest) (*object.HeadResponse, error) {
|
|
|
|
return s.head.Head(ctx, req)
|
2020-08-25 14:44:41 +00:00
|
|
|
}
|
|
|
|
|
2020-09-23 13:42:50 +00:00
|
|
|
func (s *objectSvc) Search(ctx context.Context, req *object.SearchRequest) (object.SearchObjectStreamer, error) {
|
|
|
|
return s.search.Search(ctx, req)
|
2020-08-25 14:44:41 +00:00
|
|
|
}
|
|
|
|
|
2020-09-23 13:55:46 +00:00
|
|
|
func (s *objectSvc) Get(ctx context.Context, req *object.GetRequest) (object.GetObjectStreamer, error) {
|
|
|
|
return s.get.Get(ctx, req)
|
2020-08-25 14:44:41 +00:00
|
|
|
}
|
|
|
|
|
2020-09-23 13:55:46 +00:00
|
|
|
func (s *objectSvc) Delete(ctx context.Context, req *object.DeleteRequest) (*object.DeleteResponse, error) {
|
|
|
|
return s.delete.Delete(ctx, req)
|
2020-08-25 15:58:31 +00:00
|
|
|
}
|
|
|
|
|
2020-09-23 13:55:46 +00:00
|
|
|
func (s *objectSvc) GetRange(ctx context.Context, req *object.GetRangeRequest) (object.GetRangeObjectStreamer, error) {
|
|
|
|
return s.rng.GetRange(ctx, req)
|
2020-08-25 14:44:41 +00:00
|
|
|
}
|
|
|
|
|
2020-09-23 13:55:46 +00:00
|
|
|
func (s *objectSvc) GetRangeHash(ctx context.Context, req *object.GetRangeHashRequest) (*object.GetRangeHashResponse, error) {
|
|
|
|
return s.rngHash.GetRangeHash(ctx, req)
|
2020-08-25 14:44:41 +00:00
|
|
|
}
|
|
|
|
|
2020-08-25 13:37:10 +00:00
|
|
|
func initObjectService(c *cfg) {
|
2020-09-24 12:32:30 +00:00
|
|
|
ls := localstore.New(
|
|
|
|
c.cfgObject.blobstorage,
|
|
|
|
c.cfgObject.metastorage,
|
|
|
|
)
|
2020-09-23 13:55:46 +00:00
|
|
|
keyStorage := util.NewKeyStorage(c.key, c.privateTokenStore)
|
|
|
|
nodeOwner := owner.NewID()
|
|
|
|
|
|
|
|
neo3Wallet, err := owner.NEO3WalletFromPublicKey(&c.key.PublicKey)
|
|
|
|
fatalOnErr(err)
|
|
|
|
|
|
|
|
nodeOwner.SetNeo3Wallet(neo3Wallet)
|
|
|
|
|
2020-10-03 10:26:42 +00:00
|
|
|
objGC := gc.New(
|
|
|
|
gc.WithLogger(c.log),
|
|
|
|
gc.WithRemover(ls),
|
2020-10-05 10:44:28 +00:00
|
|
|
gc.WithQueueCapacity(c.viper.GetUint32(cfgGCQueueSize)),
|
|
|
|
gc.WithSleepInterval(c.viper.GetDuration(cfgGCQueueTick)),
|
|
|
|
gc.WithWorkingInterval(c.viper.GetDuration(cfgGCTimeout)),
|
2020-10-03 10:26:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
c.workers = append(c.workers, objGC)
|
|
|
|
|
2020-10-21 09:28:42 +00:00
|
|
|
ch := make(chan *policer.Task, 1)
|
|
|
|
|
|
|
|
pol := policer.New(
|
|
|
|
policer.WithLogger(c.log),
|
|
|
|
policer.WithLocalStorage(ls),
|
|
|
|
policer.WithContainerSource(c.cfgObject.cnrStorage),
|
|
|
|
policer.WithPlacementBuilder(
|
|
|
|
placement.NewNetworkMapSourceBuilder(c.cfgObject.netMapStorage),
|
|
|
|
),
|
|
|
|
policer.WithWorkScope(
|
|
|
|
c.viper.GetInt(cfgPolicerWorkScope),
|
|
|
|
),
|
|
|
|
policer.WithExpansionRate(
|
|
|
|
c.viper.GetInt(cfgPolicerExpRate),
|
|
|
|
),
|
|
|
|
policer.WithTrigger(ch),
|
|
|
|
policer.WithRemoteHeader(
|
|
|
|
headsvc.NewRemoteHeader(keyStorage),
|
|
|
|
),
|
|
|
|
policer.WithLocalAddressSource(c),
|
|
|
|
policer.WithHeadTimeout(
|
|
|
|
c.viper.GetDuration(cfgPolicerHeadTimeout),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
addNewEpochNotificationHandler(c, func(ev event.Event) {
|
|
|
|
select {
|
|
|
|
case ch <- new(policer.Task):
|
|
|
|
case <-c.ctx.Done():
|
|
|
|
close(ch)
|
|
|
|
default:
|
|
|
|
c.log.Info("policer is busy")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
c.workers = append(c.workers, pol)
|
|
|
|
|
2020-09-23 13:55:46 +00:00
|
|
|
sPut := putsvc.NewService(
|
|
|
|
putsvc.WithKeyStorage(keyStorage),
|
2020-10-22 14:21:28 +00:00
|
|
|
putsvc.WithMaxSizeSource(c),
|
2020-09-23 13:55:46 +00:00
|
|
|
putsvc.WithLocalStorage(ls),
|
|
|
|
putsvc.WithContainerSource(c.cfgObject.cnrStorage),
|
|
|
|
putsvc.WithNetworkMapSource(c.cfgObject.netMapStorage),
|
|
|
|
putsvc.WithLocalAddressSource(c),
|
2020-10-03 10:14:09 +00:00
|
|
|
putsvc.WithFormatValidatorOpts(
|
2020-10-03 10:26:42 +00:00
|
|
|
objectCore.WithDeleteHandler(objGC),
|
2020-10-03 10:14:09 +00:00
|
|
|
),
|
2020-10-21 15:12:31 +00:00
|
|
|
putsvc.WithNetworkState(c.cfgNetmap.state),
|
2020-09-23 13:55:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
sPutV2 := putsvcV2.NewService(
|
|
|
|
putsvcV2.WithInternalService(sPut),
|
|
|
|
)
|
|
|
|
|
|
|
|
sSearch := searchsvc.NewService(
|
|
|
|
searchsvc.WithKeyStorage(keyStorage),
|
|
|
|
searchsvc.WithLocalStorage(ls),
|
|
|
|
searchsvc.WithContainerSource(c.cfgObject.cnrStorage),
|
|
|
|
searchsvc.WithNetworkMapSource(c.cfgObject.netMapStorage),
|
|
|
|
searchsvc.WithLocalAddressSource(c),
|
|
|
|
)
|
|
|
|
|
|
|
|
sSearchV2 := searchsvcV2.NewService(
|
|
|
|
searchsvcV2.WithInternalService(sSearch),
|
|
|
|
)
|
|
|
|
|
|
|
|
sHead := headsvc.NewService(
|
|
|
|
headsvc.WithKeyStorage(keyStorage),
|
|
|
|
headsvc.WithLocalStorage(ls),
|
|
|
|
headsvc.WithContainerSource(c.cfgObject.cnrStorage),
|
|
|
|
headsvc.WithNetworkMapSource(c.cfgObject.netMapStorage),
|
|
|
|
headsvc.WithLocalAddressSource(c),
|
|
|
|
headsvc.WithRightChildSearcher(searchsvc.NewRightChildSearcher(sSearch)),
|
|
|
|
)
|
|
|
|
|
|
|
|
sHeadV2 := headsvcV2.NewService(
|
|
|
|
headsvcV2.WithInternalService(sHead),
|
|
|
|
)
|
|
|
|
|
|
|
|
pool, err := ants.NewPool(10)
|
|
|
|
fatalOnErr(err)
|
|
|
|
|
|
|
|
sRange := rangesvc.NewService(
|
|
|
|
rangesvc.WithKeyStorage(keyStorage),
|
|
|
|
rangesvc.WithLocalStorage(ls),
|
|
|
|
rangesvc.WithContainerSource(c.cfgObject.cnrStorage),
|
|
|
|
rangesvc.WithNetworkMapSource(c.cfgObject.netMapStorage),
|
|
|
|
rangesvc.WithLocalAddressSource(c),
|
|
|
|
rangesvc.WithWorkerPool(pool),
|
|
|
|
rangesvc.WithHeadService(sHead),
|
|
|
|
)
|
|
|
|
|
|
|
|
sRangeV2 := rangesvcV2.NewService(
|
|
|
|
rangesvcV2.WithInternalService(sRange),
|
|
|
|
)
|
|
|
|
|
|
|
|
sGet := getsvc.NewService(
|
|
|
|
getsvc.WithRangeService(sRange),
|
|
|
|
)
|
|
|
|
|
|
|
|
sGetV2 := getsvcV2.NewService(
|
|
|
|
getsvcV2.WithInternalService(sGet),
|
|
|
|
)
|
|
|
|
|
|
|
|
sRangeHash := rangehashsvc.NewService(
|
|
|
|
rangehashsvc.WithKeyStorage(keyStorage),
|
|
|
|
rangehashsvc.WithLocalStorage(ls),
|
|
|
|
rangehashsvc.WithContainerSource(c.cfgObject.cnrStorage),
|
|
|
|
rangehashsvc.WithNetworkMapSource(c.cfgObject.netMapStorage),
|
|
|
|
rangehashsvc.WithLocalAddressSource(c),
|
|
|
|
rangehashsvc.WithHeadService(sHead),
|
|
|
|
rangehashsvc.WithRangeService(sRange),
|
|
|
|
)
|
|
|
|
|
|
|
|
sRangeHashV2 := rangehashsvcV2.NewService(
|
|
|
|
rangehashsvcV2.WithInternalService(sRangeHash),
|
|
|
|
)
|
|
|
|
|
|
|
|
sDelete := deletesvc.NewService(
|
|
|
|
deletesvc.WithKeyStorage(keyStorage),
|
|
|
|
deletesvc.WitHeadService(sHead),
|
|
|
|
deletesvc.WithPutService(sPut),
|
|
|
|
deletesvc.WithOwnerID(nodeOwner),
|
|
|
|
deletesvc.WithLinkingHeader(
|
|
|
|
headsvc.NewRelationHeader(searchsvc.NewLinkingSearcher(sSearch), sHead),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
sDeleteV2 := deletesvcV2.NewService(
|
|
|
|
deletesvcV2.WithInternalService(sDelete),
|
|
|
|
)
|
2020-08-25 14:44:41 +00:00
|
|
|
|
2020-08-25 13:37:10 +00:00
|
|
|
objectGRPC.RegisterObjectServiceServer(c.cfgGRPC.server,
|
|
|
|
objectTransportGRPC.New(
|
2020-10-03 05:09:58 +00:00
|
|
|
acl.New(
|
2020-10-03 05:19:22 +00:00
|
|
|
acl.WithSenderClassifier(
|
|
|
|
acl.NewSenderClassifier(
|
|
|
|
c.cfgNetmap.wrapper,
|
|
|
|
c.cfgNetmap.wrapper,
|
|
|
|
),
|
|
|
|
),
|
|
|
|
acl.WithContainerSource(
|
|
|
|
c.cfgObject.cnrStorage,
|
2020-10-02 11:41:29 +00:00
|
|
|
),
|
2020-10-03 05:19:22 +00:00
|
|
|
acl.WithNextService(
|
|
|
|
objectService.NewSignService(
|
|
|
|
c.key,
|
2020-10-22 11:07:06 +00:00
|
|
|
objectService.NewResponseService(
|
|
|
|
objectService.NewTransportSplitter(
|
|
|
|
c.cfgGRPC.maxChunkSize,
|
|
|
|
c.cfgGRPC.maxAddrAmount,
|
|
|
|
&objectSvc{
|
|
|
|
put: sPutV2,
|
|
|
|
search: sSearchV2,
|
|
|
|
head: sHeadV2,
|
|
|
|
rng: sRangeV2,
|
|
|
|
get: sGetV2,
|
|
|
|
rngHash: sRangeHashV2,
|
|
|
|
delete: sDeleteV2,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
c.respSvc,
|
2020-10-03 05:19:22 +00:00
|
|
|
),
|
2020-10-02 11:41:29 +00:00
|
|
|
),
|
2020-09-30 08:39:45 +00:00
|
|
|
),
|
2020-10-03 07:46:57 +00:00
|
|
|
acl.WithLocalStorage(ls),
|
|
|
|
acl.WithEACLValidatorOptions(
|
|
|
|
eacl.WithMorphClient(c.cfgObject.cnrClient),
|
|
|
|
eacl.WithLogger(c.log),
|
|
|
|
),
|
2020-10-22 12:37:59 +00:00
|
|
|
acl.WithNetmapState(c.cfgNetmap.state),
|
2020-08-25 13:37:10 +00:00
|
|
|
),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
}
|