Skip to content

Commit

Permalink
Add NodeTypeNamingNode and NodeTypePaymentProcessingNode to nodeconf
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonyAkentiev committed Nov 1, 2023
1 parent 1b018a2 commit fe8cba6
Show file tree
Hide file tree
Showing 6 changed files with 237 additions and 46 deletions.
13 changes: 11 additions & 2 deletions commonspace/spaceutils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package commonspace
import (
"context"
"fmt"
"testing"
"time"

accountService "github.com/anyproto/any-sync/accountservice"
"github.com/anyproto/any-sync/app"
"github.com/anyproto/any-sync/app/ocache"
Expand All @@ -22,8 +25,6 @@ import (
"github.com/anyproto/any-sync/testutil/accounttest"
"github.com/anyproto/go-chash"
"github.com/stretchr/testify/require"
"testing"
"time"
)

//
Expand Down Expand Up @@ -103,6 +104,14 @@ func (m *mockConf) CoordinatorPeers() []string {
return nil
}

func (m *mockConf) NamingNodePeers() []string {
return nil
}

func (m *mockConf) PaymentProcessingNodePeers() []string {
return nil
}

func (m *mockConf) PeerAddresses(peerId string) (addrs []string, ok bool) {
if peerId == m.configuration.Nodes[0].PeerId {
return m.configuration.Nodes[0].Addresses, true
Expand Down
4 changes: 3 additions & 1 deletion nodeconf/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ const (
NodeTypeConsensus NodeType = "consensus"
NodeTypeFile NodeType = "file"

NodeTypeCoordinator NodeType = "coordinator"
NodeTypeCoordinator NodeType = "coordinator"
NodeTypeNamingNode NodeType = "namingNode"
NodeTypePaymentProcessingNode NodeType = "paymentProcessingNode"
)

type Node struct {
Expand Down
28 changes: 28 additions & 0 deletions nodeconf/mock_nodeconf/mock_nodeconf.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 71 additions & 10 deletions nodeconf/nodeconf.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package nodeconf

import (
"github.com/anyproto/go-chash"
"strings"

"github.com/anyproto/go-chash"
)

type NodeConf interface {
Expand All @@ -20,6 +21,11 @@ type NodeConf interface {
ConsensusPeers() []string
// CoordinatorPeers returns list of coordinator nodes
CoordinatorPeers() []string
// Please see any-ns-node repo for details
// Usually one network has only 1 naming node, but we support array of NNs
NamingNodePeers() []string
// Please see any-pp-node repo for details
PaymentProcessingNodePeers() []string
// PeerAddresses returns peer addresses by peer id
PeerAddresses(peerId string) (addrs []string, ok bool)
// CHash returns nodes consistent table
Expand All @@ -31,15 +37,17 @@ type NodeConf interface {
}

type nodeConf struct {
id string
accountId string
filePeers []string
consensusPeers []string
coordinatorPeers []string
chash chash.CHash
allMembers []Node
c Configuration
addrs map[string][]string
id string
accountId string
filePeers []string
consensusPeers []string
coordinatorPeers []string
namingNodePeers []string
paymentProcessingNodePeers []string
chash chash.CHash
allMembers []Node
c Configuration
addrs map[string][]string
}

func (c *nodeConf) Id() string {
Expand Down Expand Up @@ -82,6 +90,14 @@ func (c *nodeConf) CoordinatorPeers() []string {
return c.coordinatorPeers
}

func (c *nodeConf) NamingNodePeers() []string {
return c.namingNodePeers
}

func (c *nodeConf) PaymentProcessingNodePeers() []string {
return c.paymentProcessingNodePeers
}

func (c *nodeConf) PeerAddresses(peerId string) (addrs []string, ok bool) {
addrs, ok = c.addrs[peerId]
if ok && len(addrs) == 0 {
Expand Down Expand Up @@ -113,3 +129,48 @@ func ReplKey(spaceId string) (replKey string) {
}
return spaceId
}

func ConfigurationToNodeConf(c Configuration) (nc *nodeConf, err error) {
nc = &nodeConf{
id: c.Id,
c: c,

// WARN: do not forget to set it later, Configuration does not feature it
//accountId: s.accountId,

addrs: map[string][]string{},
}
if nc.chash, err = chash.New(chash.Config{
PartitionCount: PartitionCount,
ReplicationFactor: ReplicationFactor,
}); err != nil {
return
}

members := make([]chash.Member, 0, len(c.Nodes))
for _, n := range c.Nodes {
if n.HasType(NodeTypeTree) {
members = append(members, n)
}
if n.HasType(NodeTypeConsensus) {
nc.consensusPeers = append(nc.consensusPeers, n.PeerId)
}
if n.HasType(NodeTypeFile) {
nc.filePeers = append(nc.filePeers, n.PeerId)
}
if n.HasType(NodeTypeCoordinator) {
nc.coordinatorPeers = append(nc.coordinatorPeers, n.PeerId)
}
if n.HasType(NodeTypeNamingNode) {
nc.namingNodePeers = append(nc.namingNodePeers, n.PeerId)
}
if n.HasType(NodeTypePaymentProcessingNode) {
nc.paymentProcessingNodePeers = append(nc.paymentProcessingNodePeers, n.PeerId)
}

nc.allMembers = append(nc.allMembers, n)
nc.addrs[n.PeerId] = n.Addresses
}
err = nc.chash.AddMembers(members...)
return
}
109 changes: 107 additions & 2 deletions nodeconf/nodeconf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ package nodeconf

import (
"fmt"
"math/rand"
"testing"

"github.com/anyproto/go-chash"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"math/rand"
"testing"
"go.uber.org/zap"

"gopkg.in/yaml.v3"
)

func TestReplKey(t *testing.T) {
Expand Down Expand Up @@ -60,6 +64,107 @@ func TestConfiguration_NodeIds(t *testing.T) {

}

func TestNodeConf_NamingNodePeers(t *testing.T) {
ch, err := chash.New(chash.Config{
PartitionCount: PartitionCount,
ReplicationFactor: ReplicationFactor,
})
require.NoError(t, err)

conf := &nodeConf{
id: "last",
accountId: "1",
chash: ch,
namingNodePeers: []string{
"naming-node-1",
"naming-node-2",
"naming-node-3",
},
}

assert.Equal(t, []string{"naming-node-1", "naming-node-2", "naming-node-3"}, conf.NamingNodePeers())
}

func TestNodeConf_PaymentProcessingNodePeers(t *testing.T) {
ch, err := chash.New(chash.Config{
PartitionCount: PartitionCount,
ReplicationFactor: ReplicationFactor,
})
require.NoError(t, err)

conf := &nodeConf{
id: "last",
accountId: "1",
chash: ch,
paymentProcessingNodePeers: []string{
"payment-processing-node-1",
"payment-processing-node-2",
"payment-processing-node-3",
},
}

assert.Equal(t, []string{"payment-processing-node-1", "payment-processing-node-2", "payment-processing-node-3"}, conf.PaymentProcessingNodePeers())
}

func TestNewNodeConfFromYaml(t *testing.T) {
yamlData := `
id: 64ba63209976be4a733bbb91
networkId: N4Gvo3v5wL31RrYgX3PrhAGMYvdWe5rAgtVB8cZySYWrkhb6
nodes:
- peerId: 12D3KooWA8EXV3KjBxEU5EnsPfneLx84vMWAtTBQBeyooN82KSuS
addresses:
- 127.0.0.1:4830
types:
- coordinator
- peerId: 12D3KooWA8EXV3KjBxEU5EnsPfneLx84vMWAtTBQBeyooN82KSuS
addresses:
- 127.0.0.1:4830
types:
- namingNode
- peerId: 12D3KooXXXEXV3KjBxEU5EnsPfneLx84vMWAtTBQBeyooN82KSuS
addresses:
- 127.0.0.1:4830
types:
- paymentProcessingNode
- peerId: 12D3KooYYYEXV3KjBxEU5EnsPfneLx84vMWAtTBQBeyooN82KSuS
addresses:
- 127.0.0.1:4830
types:
- file
- peerId: consensus1
addresses:
- 127.0.0.1:4830
types:
- consensus
- peerId: consensus2
addresses:
- 127.0.0.1:4830
types:
- consensus
creationTime: 2023-07-21T11:51:12.970882+01:00
`

var conf Configuration
err := yaml.Unmarshal([]byte(yamlData), &conf)
require.NoError(t, err)

log.Info("conf", zap.Any("conf", conf))

nodeConf, err := ConfigurationToNodeConf(conf)
require.NoError(t, err)

assert.Equal(t, "64ba63209976be4a733bbb91", nodeConf.Id())
assert.Equal(t, "N4Gvo3v5wL31RrYgX3PrhAGMYvdWe5rAgtVB8cZySYWrkhb6", nodeConf.c.NetworkId)

// should not be set by ConfigurationToNodeConf
assert.Equal(t, "", nodeConf.accountId)
assert.Equal(t, []string{"12D3KooYYYEXV3KjBxEU5EnsPfneLx84vMWAtTBQBeyooN82KSuS"}, nodeConf.FilePeers())
assert.Equal(t, []string{"consensus1", "consensus2"}, nodeConf.ConsensusPeers())
assert.Equal(t, []string{"12D3KooWA8EXV3KjBxEU5EnsPfneLx84vMWAtTBQBeyooN82KSuS"}, nodeConf.CoordinatorPeers())
assert.Equal(t, []string{"12D3KooWA8EXV3KjBxEU5EnsPfneLx84vMWAtTBQBeyooN82KSuS"}, nodeConf.NamingNodePeers())
assert.Equal(t, []string{"12D3KooXXXEXV3KjBxEU5EnsPfneLx84vMWAtTBQBeyooN82KSuS"}, nodeConf.PaymentProcessingNodePeers())
}

type testMember string

func (t testMember) Id() string {
Expand Down
Loading

0 comments on commit fe8cba6

Please sign in to comment.