From a124dbc7ea0396360485d8451db07844b495971f Mon Sep 17 00:00:00 2001 From: mcrakhman Date: Mon, 6 Mar 2023 11:08:27 +0100 Subject: [PATCH] Update diffsyncer to directly sync settings tree --- commonspace/headsync/diffsyncer.go | 4 ++++ commonspace/headsync/diffsyncer_test.go | 17 ++++++++++++++++- commonspace/objectsync/objectsync.go | 12 +++++++++--- commonspace/spaceservice.go | 2 +- 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/commonspace/headsync/diffsyncer.go b/commonspace/headsync/diffsyncer.go index 3f3f3f82..0e7d9e3f 100644 --- a/commonspace/headsync/diffsyncer.go +++ b/commonspace/headsync/diffsyncer.go @@ -121,6 +121,10 @@ func (d *diffSyncer) syncWithPeer(ctx context.Context, p peer.Peer) (err error) newIds, changedIds, removedIds, err := d.diff.Diff(ctx, rdiff) err = rpcerr.Unwrap(err) if err != nil && err != spacesyncproto.ErrSpaceMissing { + if err == spacesyncproto.ErrSpaceIsDeleted { + d.log.Debug("got space deleted while syncing") + d.syncTrees(ctx, p.Id(), []string{d.storage.SpaceSettingsId()}) + } d.syncStatus.SetNodesOnline(p.Id(), false) return fmt.Errorf("diff error: %v", err) } diff --git a/commonspace/headsync/diffsyncer_test.go b/commonspace/headsync/diffsyncer_test.go index e73eb55a..f9622ca6 100644 --- a/commonspace/headsync/diffsyncer_test.go +++ b/commonspace/headsync/diffsyncer_test.go @@ -207,7 +207,7 @@ func TestDiffSyncer_Sync(t *testing.T) { require.NoError(t, diffSyncer.Sync(ctx)) }) - t.Run("diff syncer sync other error", func(t *testing.T) { + t.Run("diff syncer sync unexpected", func(t *testing.T) { peerManagerMock.EXPECT(). GetResponsiblePeers(gomock.Any()). Return([]peer.Peer{mockPeer{}}, nil) @@ -217,4 +217,19 @@ func TestDiffSyncer_Sync(t *testing.T) { require.NoError(t, diffSyncer.Sync(ctx)) }) + + t.Run("diff syncer sync space is deleted error", func(t *testing.T) { + peerManagerMock.EXPECT(). + GetResponsiblePeers(gomock.Any()). + Return([]peer.Peer{mockPeer{}}, nil) + diffMock.EXPECT(). + Diff(gomock.Any(), gomock.Eq(NewRemoteDiff(spaceId, clientMock))). + Return(nil, nil, nil, spacesyncproto.ErrSpaceIsDeleted) + stMock.EXPECT().SpaceSettingsId().Return("settingsId") + cacheMock.EXPECT(). + GetTree(gomock.Any(), spaceId, "settingsId"). + Return(nil, nil) + + require.NoError(t, diffSyncer.Sync(ctx)) + }) } diff --git a/commonspace/objectsync/objectsync.go b/commonspace/objectsync/objectsync.go index a1f17b49..7c72e635 100644 --- a/commonspace/objectsync/objectsync.go +++ b/commonspace/objectsync/objectsync.go @@ -7,6 +7,7 @@ import ( "github.com/anytypeio/any-sync/commonspace/object/syncobjectgetter" "github.com/anytypeio/any-sync/commonspace/objectsync/synchandler" "github.com/anytypeio/any-sync/commonspace/peermanager" + "github.com/anytypeio/any-sync/commonspace/spacestorage" "github.com/anytypeio/any-sync/commonspace/spacesyncproto" "github.com/anytypeio/any-sync/nodeconf" "go.uber.org/zap" @@ -32,6 +33,7 @@ type objectSync struct { messagePool MessagePool objectGetter syncobjectgetter.SyncObjectGetter configuration nodeconf.Configuration + spaceStorage spacestorage.SpaceStorage syncCtx context.Context cancelSync context.CancelFunc @@ -43,13 +45,15 @@ func NewObjectSync( spaceIsDeleted *atomic.Bool, configuration nodeconf.Configuration, peerManager peermanager.PeerManager, - objectGetter syncobjectgetter.SyncObjectGetter) ObjectSync { + objectGetter syncobjectgetter.SyncObjectGetter, + storage spacestorage.SpaceStorage) ObjectSync { syncCtx, cancel := context.WithCancel(context.Background()) os := newObjectSync( spaceId, spaceIsDeleted, configuration, objectGetter, + storage, syncCtx, cancel) msgPool := newMessagePool(peerManager, os.handleMessage) @@ -62,11 +66,13 @@ func newObjectSync( spaceIsDeleted *atomic.Bool, configuration nodeconf.Configuration, objectGetter syncobjectgetter.SyncObjectGetter, + spaceStorage spacestorage.SpaceStorage, syncCtx context.Context, cancel context.CancelFunc, ) *objectSync { return &objectSync{ objectGetter: objectGetter, + spaceStorage: spaceStorage, spaceId: spaceId, syncCtx: syncCtx, cancelSync: cancel, @@ -95,8 +101,8 @@ func (s *objectSync) handleMessage(ctx context.Context, senderId string, msg *sp log := log.With(zap.String("objectId", msg.ObjectId), zap.String("replyId", msg.ReplyId)) if s.spaceIsDeleted.Load() { log = log.With(zap.Bool("isDeleted", true)) - // preventing sync with other clients - if !slices.Contains(s.configuration.NodeIds(s.spaceId), senderId) { + // preventing sync with other clients if they are not just syncing the settings tree + if !slices.Contains(s.configuration.NodeIds(s.spaceId), senderId) && msg.ObjectId != s.spaceStorage.SpaceSettingsId() { return spacesyncproto.ErrSpaceIsDeleted } } diff --git a/commonspace/spaceservice.go b/commonspace/spaceservice.go index d10fb35b..89e9283b 100644 --- a/commonspace/spaceservice.go +++ b/commonspace/spaceservice.go @@ -148,7 +148,7 @@ func (s *spaceService) NewSpace(ctx context.Context, id string) (Space, error) { } headSync := headsync.NewHeadSync(id, spaceIsDeleted, s.config.SyncPeriod, lastConfiguration, st, peerManager, getter, syncStatus, s.credentialProvider, log) - objectSync := objectsync.NewObjectSync(id, spaceIsDeleted, lastConfiguration, peerManager, getter) + objectSync := objectsync.NewObjectSync(id, spaceIsDeleted, lastConfiguration, peerManager, getter, st) sp := &space{ id: id, objectSync: objectSync,