From fe2c8bdadeca5ddcb7fc599ca841e20be81def09 Mon Sep 17 00:00:00 2001 From: Sergey Cherepanov Date: Wed, 24 Aug 2022 00:40:14 +0300 Subject: [PATCH] configuration from config --- cmd/node/node.go | 3 +- service/configuration/configuration.go | 26 ++++++++++++ service/configuration/service.go | 56 ++++++++++++++++---------- service/node/service.go | 8 ++++ 4 files changed, 70 insertions(+), 23 deletions(-) diff --git a/cmd/node/node.go b/cmd/node/node.go index 269e4890..d9433a35 100644 --- a/cmd/node/node.go +++ b/cmd/node/node.go @@ -9,6 +9,7 @@ import ( "github.com/anytypeio/go-anytype-infrastructure-experiments/config" "github.com/anytypeio/go-anytype-infrastructure-experiments/service/account" "github.com/anytypeio/go-anytype-infrastructure-experiments/service/api" + "github.com/anytypeio/go-anytype-infrastructure-experiments/service/configuration" "github.com/anytypeio/go-anytype-infrastructure-experiments/service/net/dialer" "github.com/anytypeio/go-anytype-infrastructure-experiments/service/net/pool" "github.com/anytypeio/go-anytype-infrastructure-experiments/service/net/rpc/server" @@ -97,7 +98,7 @@ func Bootstrap(a *app.App) { Register(server.New()). Register(dialer.New()). Register(pool.NewPool()). - //Register(&example.Example{}) + Register(configuration.New()). Register(document.New()). Register(message.New()). Register(requesthandler.New()). diff --git a/service/configuration/configuration.go b/service/configuration/configuration.go index 0371d958..1efa82b2 100644 --- a/service/configuration/configuration.go +++ b/service/configuration/configuration.go @@ -3,10 +3,36 @@ package configuration import ( "context" "github.com/anytypeio/go-anytype-infrastructure-experiments/service/net/peer" + "github.com/anytypeio/go-anytype-infrastructure-experiments/service/net/pool" + "github.com/anytypeio/go-chash" ) +func New() Service { + return new(service) +} + type Configuration interface { Id() string AllPeers(ctx context.Context, spaceId string) (peers []peer.Peer, err error) OnePeer(ctx context.Context, spaceId string) (p peer.Peer, err error) } + +type configuration struct { + id string + pool pool.Pool + chash chash.CHash +} + +func (c *configuration) Id() string { + return c.id +} + +func (c *configuration) AllPeers(ctx context.Context, spaceId string) (peers []peer.Peer, err error) { + //TODO implement me + panic("implement me") +} + +func (c *configuration) OnePeer(ctx context.Context, spaceId string) (p peer.Peer, err error) { + //TODO implement me + panic("implement me") +} diff --git a/service/configuration/service.go b/service/configuration/service.go index e4993f5a..a09d8db1 100644 --- a/service/configuration/service.go +++ b/service/configuration/service.go @@ -5,7 +5,9 @@ import ( "github.com/anytypeio/go-anytype-infrastructure-experiments/app" "github.com/anytypeio/go-anytype-infrastructure-experiments/app/logger" "github.com/anytypeio/go-anytype-infrastructure-experiments/config" - "github.com/anytypeio/go-anytype-infrastructure-experiments/service/net/peer" + "github.com/anytypeio/go-anytype-infrastructure-experiments/service/net/pool" + "github.com/anytypeio/go-anytype-infrastructure-experiments/service/node" + "github.com/anytypeio/go-chash" ) const CName = "configuration" @@ -20,41 +22,51 @@ var log = logger.NewNamed(CName) type Service interface { GetLast() Configuration GetById(id string) Configuration - app.ComponentRunnable + app.Component } type service struct { - accountId string - bootstrapConfig []config.Node + accountId string + pool pool.Pool + + last Configuration } func (s *service) Init(ctx context.Context, a *app.App) (err error) { conf := a.MustComponent(config.CName).(*config.Config) - s.bootstrapConfig = conf.Nodes s.accountId = conf.Account.PeerId - - return nil + s.pool = a.MustComponent(pool.CName).(pool.Pool) + configNodes := a.MustComponent(node.CName).(node.Service).Nodes() + config := &configuration{ + id: "config", + pool: s.pool, + } + if config.chash, err = chash.New(chash.Config{ + PartitionCount: partitionCount, + ReplicationFactor: replicationFactor, + }); err != nil { + return + } + members := make([]chash.Member, 0, len(configNodes)) + for _, n := range configNodes { + members = append(members, n) + } + if err = config.chash.AddMembers(members...); err != nil { + return + } + s.last = config + return } func (s *service) Name() (name string) { return CName } -func (s *service) AllPeers(ctx context.Context, spaceId string) (peers []peer.Peer, err error) { +func (s *service) GetLast() Configuration { + return s.last +} + +func (s *service) GetById(id string) Configuration { //TODO implement me panic("implement me") } - -func (s *service) OnePeer(ctx context.Context, spaceId string) (p peer.Peer, err error) { - //TODO implement me - panic("implement me") -} - -func (s *service) Run(ctx context.Context) (err error) { - //TODO implement me - panic("implement me") -} - -func (s *service) Close(ctx context.Context) (err error) { - return nil -} diff --git a/service/node/service.go b/service/node/service.go index f3b82ba0..877c8860 100644 --- a/service/node/service.go +++ b/service/node/service.go @@ -24,6 +24,14 @@ type Node struct { EncryptionKeyString string } +func (n *Node) Id() string { + return n.PeerId +} + +func (n *Node) Capacity() float64 { + return 1 +} + func New() app.Component { return &service{} }