Few fixes to syncservice and streamchecker

This commit is contained in:
mcrakhman 2022-12-09 21:53:24 +01:00 committed by Mikhail Iudin
parent 30a8194638
commit ed3d7a7720
No known key found for this signature in database
GPG Key ID: FAAAA8BAABDFF1C0
7 changed files with 40 additions and 15 deletions

View File

@ -13,6 +13,7 @@ import (
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/periodicsync" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/periodicsync"
"go.uber.org/zap" "go.uber.org/zap"
"strings" "strings"
"time"
) )
type TreeHeads struct { type TreeHeads struct {
@ -54,7 +55,7 @@ func NewDiffService(
l := log.With(zap.String("spaceId", spaceId)) l := log.With(zap.String("spaceId", spaceId))
factory := spacesyncproto.ClientFactoryFunc(spacesyncproto.NewDRPCSpaceClient) factory := spacesyncproto.ClientFactoryFunc(spacesyncproto.NewDRPCSpaceClient)
syncer := newDiffSyncer(spaceId, diff, confConnector, cache, storage, factory, l) syncer := newDiffSyncer(spaceId, diff, confConnector, cache, storage, factory, l)
periodicSync := periodicsync.NewPeriodicSync(syncPeriod, syncer.Sync, l) periodicSync := periodicsync.NewPeriodicSync(syncPeriod, time.Minute, syncer.Sync, l)
return &diffService{ return &diffService{
spaceId: spaceId, spaceId: spaceId,

View File

@ -9,7 +9,7 @@ import (
) )
type StreamChecker interface { type StreamChecker interface {
CheckResponsiblePeers(ctx context.Context) error CheckResponsiblePeers()
} }
type streamChecker struct { type streamChecker struct {
@ -18,6 +18,7 @@ type streamChecker struct {
streamPool StreamPool streamPool StreamPool
clientFactory spacesyncproto.ClientFactory clientFactory spacesyncproto.ClientFactory
log *zap.Logger log *zap.Logger
syncCtx context.Context
} }
func NewStreamChecker( func NewStreamChecker(
@ -25,6 +26,7 @@ func NewStreamChecker(
connector nodeconf.ConfConnector, connector nodeconf.ConfConnector,
streamPool StreamPool, streamPool StreamPool,
clientFactory spacesyncproto.ClientFactory, clientFactory spacesyncproto.ClientFactory,
syncCtx context.Context,
log *zap.Logger) StreamChecker { log *zap.Logger) StreamChecker {
return &streamChecker{ return &streamChecker{
spaceId: spaceId, spaceId: spaceId,
@ -32,10 +34,11 @@ func NewStreamChecker(
streamPool: streamPool, streamPool: streamPool,
clientFactory: clientFactory, clientFactory: clientFactory,
log: log, log: log,
syncCtx: syncCtx,
} }
} }
func (s *streamChecker) CheckResponsiblePeers(ctx context.Context) (err error) { func (s *streamChecker) CheckResponsiblePeers() {
var ( var (
activeNodeIds []string activeNodeIds []string
configuration = s.connector.Configuration() configuration = s.connector.Configuration()
@ -47,14 +50,14 @@ func (s *streamChecker) CheckResponsiblePeers(ctx context.Context) (err error) {
continue continue
} }
} }
newPeers, err := s.connector.DialInactiveResponsiblePeers(ctx, s.spaceId, activeNodeIds) newPeers, err := s.connector.DialInactiveResponsiblePeers(s.syncCtx, 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 newPeers { for _, p := range newPeers {
stream, err := s.clientFactory.Client(p).Stream(ctx) stream, err := s.clientFactory.Client(p).Stream(s.syncCtx)
if err != nil { if err != nil {
err = rpcerr.Unwrap(err) err = rpcerr.Unwrap(err)
s.log.Error("failed to open stream", zap.Error(err)) s.log.Error("failed to open stream", zap.Error(err))

View File

@ -265,6 +265,7 @@ Loop:
select { select {
case <-limiter: case <-limiter:
case <-stream.Context().Done(): case <-stream.Context().Done():
log.Debug("stream context done")
break Loop break Loop
} }
go func() { go func() {

View File

@ -34,6 +34,9 @@ type syncService struct {
checker StreamChecker checker StreamChecker
periodicSync periodicsync.PeriodicSync periodicSync periodicsync.PeriodicSync
objectGetter objectgetter.ObjectGetter objectGetter objectgetter.ObjectGetter
syncCtx context.Context
cancelSync context.CancelFunc
} }
func NewSyncService( func NewSyncService(
@ -45,18 +48,25 @@ func NewSyncService(
}) })
clientFactory := spacesyncproto.ClientFactoryFunc(spacesyncproto.NewDRPCSpaceClient) clientFactory := spacesyncproto.ClientFactoryFunc(spacesyncproto.NewDRPCSpaceClient)
syncLog := log.Desugar().With(zap.String("id", spaceId)) syncLog := log.Desugar().With(zap.String("id", spaceId))
syncCtx, cancel := context.WithCancel(context.Background())
checker := NewStreamChecker( checker := NewStreamChecker(
spaceId, spaceId,
confConnector, confConnector,
streamPool, streamPool,
clientFactory, clientFactory,
syncCtx,
syncLog) syncLog)
periodicSync := periodicsync.NewPeriodicSync(periodicSeconds, checker.CheckResponsiblePeers, syncLog) periodicSync := periodicsync.NewPeriodicSync(periodicSeconds, 0, func(ctx context.Context) error {
checker.CheckResponsiblePeers()
return nil
}, syncLog)
syncService = newSyncService( syncService = newSyncService(
spaceId, spaceId,
streamPool, streamPool,
periodicSync, periodicSync,
checker) checker,
syncCtx,
cancel)
return return
} }
@ -65,12 +75,16 @@ func newSyncService(
streamPool StreamPool, streamPool StreamPool,
periodicSync periodicsync.PeriodicSync, periodicSync periodicsync.PeriodicSync,
checker StreamChecker, checker StreamChecker,
syncCtx context.Context,
cancel context.CancelFunc,
) *syncService { ) *syncService {
return &syncService{ return &syncService{
periodicSync: periodicSync, periodicSync: periodicSync,
streamPool: streamPool, streamPool: streamPool,
spaceId: spaceId, spaceId: spaceId,
checker: checker, checker: checker,
syncCtx: syncCtx,
cancelSync: cancel,
} }
} }
@ -81,6 +95,7 @@ func (s *syncService) Init(objectGetter objectgetter.ObjectGetter) {
func (s *syncService) Close() (err error) { func (s *syncService) Close() (err error) {
s.periodicSync.Close() s.periodicSync.Close()
s.cancelSync()
return s.streamPool.Close() return s.streamPool.Close()
} }

View File

@ -2,7 +2,6 @@
package synctree package synctree
import ( import (
"context"
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/spacesyncproto" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/spacesyncproto"
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/syncservice" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/syncservice"
"github.com/anytypeio/go-anytype-infrastructure-experiments/common/nodeconf" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/nodeconf"
@ -45,7 +44,7 @@ func (s *syncClient) BroadcastAsync(message *treechangeproto.TreeSyncMessage) (e
if err != nil { if err != nil {
return return
} }
s.checker.CheckResponsiblePeers(context.Background()) s.checker.CheckResponsiblePeers()
return s.StreamPool.BroadcastAsync(objMsg) return s.StreamPool.BroadcastAsync(objMsg)
} }
@ -63,7 +62,7 @@ func (s *syncClient) BroadcastAsyncOrSendResponsible(message *treechangeproto.Tr
return return
} }
if s.configuration.IsResponsible(s.spaceId) { if s.configuration.IsResponsible(s.spaceId) {
s.checker.CheckResponsiblePeers(context.Background()) s.checker.CheckResponsiblePeers()
return s.StreamPool.SendAsync(s.configuration.NodeIds(s.spaceId), objMsg) return s.StreamPool.SendAsync(s.configuration.NodeIds(s.spaceId), objMsg)
} }
return s.BroadcastAsync(message) return s.BroadcastAsync(message)

View File

@ -14,7 +14,7 @@ type PeriodicSync interface {
type SyncerFunc func(ctx context.Context) error type SyncerFunc func(ctx context.Context) error
func NewPeriodicSync(periodSeconds int, syncer SyncerFunc, l *zap.Logger) PeriodicSync { func NewPeriodicSync(periodSeconds int, timeout time.Duration, syncer SyncerFunc, l *zap.Logger) PeriodicSync {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
return &periodicSync{ return &periodicSync{
syncer: syncer, syncer: syncer,
@ -23,6 +23,7 @@ func NewPeriodicSync(periodSeconds int, syncer SyncerFunc, l *zap.Logger) Period
syncCancel: cancel, syncCancel: cancel,
syncLoopDone: make(chan struct{}), syncLoopDone: make(chan struct{}),
periodSeconds: periodSeconds, periodSeconds: periodSeconds,
timeout: timeout,
} }
} }
@ -33,6 +34,7 @@ type periodicSync struct {
syncCancel context.CancelFunc syncCancel context.CancelFunc
syncLoopDone chan struct{} syncLoopDone chan struct{}
periodSeconds int periodSeconds int
timeout time.Duration
} }
func (p *periodicSync) Run() { func (p *periodicSync) Run() {
@ -43,8 +45,12 @@ func (p *periodicSync) syncLoop(periodSeconds int) {
period := time.Duration(periodSeconds) * time.Second period := time.Duration(periodSeconds) * time.Second
defer close(p.syncLoopDone) defer close(p.syncLoopDone)
doSync := func() { doSync := func() {
ctx, cancel := context.WithTimeout(p.syncCtx, time.Minute) ctx := p.syncCtx
if p.timeout != 0 {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(p.syncCtx, p.timeout)
defer cancel() defer cancel()
}
if err := p.syncer(ctx); err != nil { if err := p.syncer(ctx); err != nil {
p.log.Warn("periodic sync error", zap.Error(err)) p.log.Warn("periodic sync error", zap.Error(err))
} }

View File

@ -23,7 +23,7 @@ func TestPeriodicSync_Run(t *testing.T) {
times += 1 times += 1
return nil return nil
} }
pSync := NewPeriodicSync(secs, diffSyncer, l) pSync := NewPeriodicSync(secs, 0, diffSyncer, l)
pSync.Run() pSync.Run()
pSync.Close() pSync.Close()
@ -38,7 +38,7 @@ func TestPeriodicSync_Run(t *testing.T) {
times += 1 times += 1
return nil return nil
} }
pSync := NewPeriodicSync(secs, diffSyncer, l) pSync := NewPeriodicSync(secs, 0, diffSyncer, l)
pSync.Run() pSync.Run()
time.Sleep(time.Second * time.Duration(secs)) time.Sleep(time.Second * time.Duration(secs))