Fix syncservice loop
This commit is contained in:
parent
2ba8108292
commit
82eb76c1ea
@ -97,17 +97,24 @@ func (s *syncService) HandleMessage(ctx context.Context, senderId string, messag
|
|||||||
func (s *syncService) responsibleStreamCheckLoop(ctx context.Context) {
|
func (s *syncService) responsibleStreamCheckLoop(ctx context.Context) {
|
||||||
defer close(s.streamLoopDone)
|
defer close(s.streamLoopDone)
|
||||||
checkResponsiblePeers := func() {
|
checkResponsiblePeers := func() {
|
||||||
s.log.Debug("dialing responsible peers")
|
var (
|
||||||
respPeers, err := s.connector.DialResponsiblePeers(ctx, s.spaceId)
|
activeNodeIds []string
|
||||||
|
configuration = s.connector.Configuration()
|
||||||
|
)
|
||||||
|
for _, nodeId := range configuration.NodeIds(s.spaceId) {
|
||||||
|
if s.streamPool.HasActiveStream(nodeId) {
|
||||||
|
s.log.Debug("has active stream for", zap.String("id", nodeId))
|
||||||
|
activeNodeIds = append(activeNodeIds, nodeId)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
newPeers, err := s.connector.DialResponsiblePeers(ctx, s.spaceId, activeNodeIds)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.log.Error("failed to dial peers", zap.Error(err))
|
s.log.Error("failed to dial peers", zap.Error(err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
for _, p := range respPeers {
|
|
||||||
if s.streamPool.HasActiveStream(p.Id()) {
|
for _, p := range newPeers {
|
||||||
s.log.Debug("has active stream for", zap.String("id", p.Id()))
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
stream, err := s.clientFactory.Client(p).Stream(ctx)
|
stream, err := s.clientFactory.Client(p).Stream(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
err = rpcerr.Unwrap(err)
|
err = rpcerr.Unwrap(err)
|
||||||
@ -123,7 +130,7 @@ func (s *syncService) responsibleStreamCheckLoop(ctx context.Context) {
|
|||||||
s.log.Errorf("failed to send first message to stream: %v", err)
|
s.log.Errorf("failed to send first message to stream: %v", err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
s.log.Debug("continue reading stream for", zap.String("id", p.Id()))
|
s.log.Debug("reading stream for", zap.String("id", p.Id()))
|
||||||
s.streamPool.AddAndReadStreamAsync(stream)
|
s.streamPool.AddAndReadStreamAsync(stream)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,11 +4,13 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/net/peer"
|
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/net/peer"
|
||||||
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/net/pool"
|
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/net/pool"
|
||||||
|
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/slice"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ConfConnector interface {
|
type ConfConnector interface {
|
||||||
|
Configuration() Configuration
|
||||||
GetResponsiblePeers(ctx context.Context, spaceId string) ([]peer.Peer, error)
|
GetResponsiblePeers(ctx context.Context, spaceId string) ([]peer.Peer, error)
|
||||||
DialResponsiblePeers(ctx context.Context, spaceId string) ([]peer.Peer, error)
|
DialResponsiblePeers(ctx context.Context, spaceId string, activeNodeIds []string) ([]peer.Peer, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type confConnector struct {
|
type confConnector struct {
|
||||||
@ -20,21 +22,36 @@ func NewConfConnector(conf Configuration, pool pool.Pool) ConfConnector {
|
|||||||
return &confConnector{conf: conf, pool: pool}
|
return &confConnector{conf: conf, pool: pool}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *confConnector) GetResponsiblePeers(ctx context.Context, spaceId string) ([]peer.Peer, error) {
|
func (s *confConnector) Configuration() Configuration {
|
||||||
return s.connectOneOrMany(ctx, spaceId, s.pool.Get, s.pool.GetOneOf)
|
return s.conf
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *confConnector) DialResponsiblePeers(ctx context.Context, spaceId string) ([]peer.Peer, error) {
|
func (s *confConnector) GetResponsiblePeers(ctx context.Context, spaceId string) ([]peer.Peer, error) {
|
||||||
return s.connectOneOrMany(ctx, spaceId, s.pool.Dial, s.pool.DialOneOf)
|
return s.connectOneOrMany(ctx, spaceId, nil, s.pool.Get, s.pool.GetOneOf)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *confConnector) DialResponsiblePeers(ctx context.Context, spaceId string, activeNodeIds []string) ([]peer.Peer, error) {
|
||||||
|
return s.connectOneOrMany(ctx, spaceId, activeNodeIds, s.pool.Dial, s.pool.DialOneOf)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *confConnector) connectOneOrMany(
|
func (s *confConnector) connectOneOrMany(
|
||||||
ctx context.Context, spaceId string,
|
ctx context.Context,
|
||||||
|
spaceId string,
|
||||||
|
activeNodeIds []string,
|
||||||
connectOne func(context.Context, string) (peer.Peer, error),
|
connectOne func(context.Context, string) (peer.Peer, error),
|
||||||
connectOneOf func(context.Context, []string) (peer.Peer, error)) (peers []peer.Peer, err error) {
|
connectOneOf func(context.Context, []string) (peer.Peer, error)) (peers []peer.Peer, err error) {
|
||||||
allNodes := s.conf.NodeIds(spaceId)
|
var (
|
||||||
|
inactiveNodeIds []string
|
||||||
|
allNodes = s.conf.NodeIds(spaceId)
|
||||||
|
)
|
||||||
|
for _, id := range allNodes {
|
||||||
|
if slice.FindPos(activeNodeIds, id) == -1 {
|
||||||
|
inactiveNodeIds = append(inactiveNodeIds, id)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if s.conf.IsResponsible(spaceId) {
|
if s.conf.IsResponsible(spaceId) {
|
||||||
for _, id := range allNodes {
|
for _, id := range inactiveNodeIds {
|
||||||
var p peer.Peer
|
var p peer.Peer
|
||||||
p, err = connectOne(ctx, id)
|
p, err = connectOne(ctx, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -42,7 +59,8 @@ func (s *confConnector) connectOneOrMany(
|
|||||||
}
|
}
|
||||||
peers = append(peers, p)
|
peers = append(peers, p)
|
||||||
}
|
}
|
||||||
} else {
|
} else if len(activeNodeIds) == 0 {
|
||||||
|
// that means that all connected ids
|
||||||
var p peer.Peer
|
var p peer.Peer
|
||||||
p, err = connectOneOf(ctx, allNodes)
|
p, err = connectOneOf(ctx, allNodes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user