diff --git a/client/storage/storageservice.go b/client/storage/storageservice.go index b7f8e851..4ff0b014 100644 --- a/client/storage/storageservice.go +++ b/client/storage/storageservice.go @@ -38,6 +38,16 @@ func (s *storageService) SpaceStorage(id string) (spacestorage.SpaceStorage, err return newSpaceStorage(s.db, id) } +func (s *storageService) SpaceExists(id string) bool { + return s.db.View(func(txn *badger.Txn) error { + _, err := getTxn(txn, newSpaceKeys(id).HeaderKey()) + if err != nil { + return err + } + return nil + }) == nil +} + func (s *storageService) CreateSpaceStorage(payload spacestorage.SpaceStorageCreatePayload) (spacestorage.SpaceStorage, error) { return createSpaceStorage(s.db, payload) } diff --git a/common/commonspace/confconnector/confconnector.go b/common/commonspace/confconnector/confconnector.go index a9ef63eb..5e15d5e9 100644 --- a/common/commonspace/confconnector/confconnector.go +++ b/common/commonspace/confconnector/confconnector.go @@ -5,7 +5,7 @@ import ( "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/nodeconf" - "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/slice" + "golang.org/x/exp/slices" ) type ConfConnector interface { @@ -50,7 +50,7 @@ func (s *confConnector) connectOneOrMany( allNodes = s.conf.NodeIds(spaceId) ) for _, id := range allNodes { - if slice.FindPos(activeNodeIds, id) == -1 { + if !slices.Contains(activeNodeIds, id) { inactiveNodeIds = append(inactiveNodeIds, id) } } diff --git a/common/commonspace/objectsync/objectsync.go b/common/commonspace/objectsync/objectsync.go index b3c482a6..964c8d66 100644 --- a/common/commonspace/objectsync/objectsync.go +++ b/common/commonspace/objectsync/objectsync.go @@ -9,7 +9,6 @@ import ( "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/object/syncobjectgetter" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/objectsync/synchandler" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/spacesyncproto" - "github.com/anytypeio/go-anytype-infrastructure-experiments/common/util/periodicsync" "go.uber.org/zap" "time" ) @@ -32,7 +31,6 @@ type objectSync struct { streamPool StreamPool checker StreamChecker - periodicSync periodicsync.PeriodicSync objectGetter syncobjectgetter.SyncObjectGetter actionQueue ActionQueue @@ -42,8 +40,7 @@ type objectSync struct { func NewObjectSync( spaceId string, - confConnector confconnector.ConfConnector, - periodicSeconds int) (objectSync ObjectSync) { + confConnector confconnector.ConfConnector) (objectSync ObjectSync) { streamPool := newStreamPool(func(ctx context.Context, senderId string, message *spacesyncproto.ObjectSyncMessage) (err error) { return objectSync.HandleMessage(ctx, senderId, message) }) @@ -57,14 +54,9 @@ func NewObjectSync( clientFactory, syncCtx, syncLog) - periodicSync := periodicsync.NewPeriodicSync(periodicSeconds, 0, func(ctx context.Context) error { - checker.CheckResponsiblePeers() - return nil - }, syncLog) objectSync = newObjectSync( spaceId, streamPool, - periodicSync, checker, syncCtx, cancel) @@ -74,31 +66,28 @@ func NewObjectSync( func newObjectSync( spaceId string, streamPool StreamPool, - periodicSync periodicsync.PeriodicSync, checker StreamChecker, syncCtx context.Context, cancel context.CancelFunc, ) *objectSync { return &objectSync{ - periodicSync: periodicSync, - streamPool: streamPool, - spaceId: spaceId, - checker: checker, - syncCtx: syncCtx, - cancelSync: cancel, - actionQueue: NewDefaultActionQueue(), + streamPool: streamPool, + spaceId: spaceId, + checker: checker, + syncCtx: syncCtx, + cancelSync: cancel, + actionQueue: NewDefaultActionQueue(), } } func (s *objectSync) Init(objectGetter syncobjectgetter.SyncObjectGetter) { s.objectGetter = objectGetter s.actionQueue.Run() - s.periodicSync.Run() + go s.checker.CheckResponsiblePeers() } func (s *objectSync) Close() (err error) { s.actionQueue.Close() - s.periodicSync.Close() s.cancelSync() return s.streamPool.Close() } diff --git a/common/commonspace/spaceservice.go b/common/commonspace/spaceservice.go index 8eff65f7..c2fc660e 100644 --- a/common/commonspace/spaceservice.go +++ b/common/commonspace/spaceservice.go @@ -118,7 +118,7 @@ func (s *spaceService) NewSpace(ctx context.Context, id string) (Space, error) { } headSync := headsync.NewHeadSync(id, s.config.SyncPeriod, st, confConnector, s.treeGetter, syncStatus, log) - objectSync := objectsync.NewObjectSync(id, confConnector, s.config.SyncPeriod) + objectSync := objectsync.NewObjectSync(id, confConnector) sp := &space{ id: id, objectSync: objectSync, diff --git a/common/commonspace/spacestorage/storage.go b/common/commonspace/spacestorage/storage.go index c0207935..41d3df47 100644 --- a/common/commonspace/spacestorage/storage.go +++ b/common/commonspace/spacestorage/storage.go @@ -47,6 +47,7 @@ type SpaceStorageCreatePayload struct { type SpaceStorageProvider interface { app.Component SpaceStorage(id string) (SpaceStorage, error) + SpaceExists(id string) bool CreateSpaceStorage(payload SpaceStorageCreatePayload) (SpaceStorage, error) } diff --git a/common/commonspace/spacesyncproto/errors.go b/common/commonspace/spacesyncproto/errors.go index 44034ccb..ba9fe05c 100644 --- a/common/commonspace/spacesyncproto/errors.go +++ b/common/commonspace/spacesyncproto/errors.go @@ -8,7 +8,8 @@ import ( var ( errGroup = rpcerr.ErrGroup(ErrCodes_ErrorOffset) - ErrUnexpected = errGroup.Register(errors.New("unexpected error"), uint64(ErrCodes_Unexpected)) - ErrSpaceMissing = errGroup.Register(errors.New("space is missing"), uint64(ErrCodes_SpaceMissing)) - ErrSpaceExists = errGroup.Register(errors.New("space exists"), uint64(ErrCodes_SpaceExists)) + ErrUnexpected = errGroup.Register(errors.New("unexpected error"), uint64(ErrCodes_Unexpected)) + ErrSpaceMissing = errGroup.Register(errors.New("space is missing"), uint64(ErrCodes_SpaceMissing)) + ErrSpaceExists = errGroup.Register(errors.New("space exists"), uint64(ErrCodes_SpaceExists)) + ErrSpaceNotInCache = errGroup.Register(errors.New("space not in cache"), uint64(ErrCodes_SpaceNotInCache)) ) diff --git a/common/commonspace/spacesyncproto/protos/spacesync.proto b/common/commonspace/spacesyncproto/protos/spacesync.proto index 6e9bd3eb..b11b182b 100644 --- a/common/commonspace/spacesyncproto/protos/spacesync.proto +++ b/common/commonspace/spacesyncproto/protos/spacesync.proto @@ -7,6 +7,7 @@ enum ErrCodes { Unexpected = 0; SpaceMissing = 1; SpaceExists = 2; + SpaceNotInCache = 3; ErrorOffset = 100; } diff --git a/common/commonspace/spacesyncproto/spacesync.pb.go b/common/commonspace/spacesyncproto/spacesync.pb.go index 4491e775..a8ffc521 100644 --- a/common/commonspace/spacesyncproto/spacesync.pb.go +++ b/common/commonspace/spacesyncproto/spacesync.pb.go @@ -25,24 +25,27 @@ const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package type ErrCodes int32 const ( - ErrCodes_Unexpected ErrCodes = 0 - ErrCodes_SpaceMissing ErrCodes = 1 - ErrCodes_SpaceExists ErrCodes = 2 - ErrCodes_ErrorOffset ErrCodes = 100 + ErrCodes_Unexpected ErrCodes = 0 + ErrCodes_SpaceMissing ErrCodes = 1 + ErrCodes_SpaceExists ErrCodes = 2 + ErrCodes_SpaceNotInCache ErrCodes = 3 + ErrCodes_ErrorOffset ErrCodes = 100 ) var ErrCodes_name = map[int32]string{ 0: "Unexpected", 1: "SpaceMissing", 2: "SpaceExists", + 3: "SpaceNotInCache", 100: "ErrorOffset", } var ErrCodes_value = map[string]int32{ - "Unexpected": 0, - "SpaceMissing": 1, - "SpaceExists": 2, - "ErrorOffset": 100, + "Unexpected": 0, + "SpaceMissing": 1, + "SpaceExists": 2, + "SpaceNotInCache": 3, + "ErrorOffset": 100, } func (x ErrCodes) String() string { @@ -1071,63 +1074,64 @@ func init() { } var fileDescriptor_80e49f1f4ac27799 = []byte{ - // 887 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x9c, 0x56, 0x4d, 0x6f, 0x1b, 0x45, - 0x18, 0xf6, 0x6e, 0x9c, 0x26, 0x7e, 0xb3, 0x75, 0xdd, 0x21, 0x85, 0xc5, 0x8d, 0x5c, 0x6b, 0x0e, - 0x28, 0xe2, 0xd0, 0x8f,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0x17, 0x7a, 0x59, 0xce, 0xb5, 0x08, - 0xc8, 0x31, 0xec, 0x16, 0x14, 0x4a, 0x86, 0xad, 0xbc, 0x6a, 0x09, 0x66, 0x78, 0xb7, 0x9d, 0x73, - 0x33, 0x62, 0x79, 0x9a, 0x23, 0x1a, 0x9a, 0x22, 0x77, 0x37, 0x48, 0xa5, 0xe2, 0xc0, 0xe1, 0x41, - 0xbb, 0x71, 0x03, 0x27, 0x8e, 0xdb, 0x70, 0x4a, 0xbe, 0x6b, 0xc3, 0xa9, 0x11, 0x1d, 0x83, 0x41, - 0x45, 0xfe, 0x33, 0x9d, 0x22, 0x5f, 0x92, 0x83, 0x8d, 0xdd, 0xaa, 0xfd, 0x67, 0x18, 0xbe, 0xd1, - 0x7a, 0xe8, 0x3c, 0x74, 0x9e, 0x7c, 0xf4, 0xcf, 0xc5, 0xc8, 0x79, 0x7d, 0x31, 0x72, 0xfe, 0xbb, - 0x18, 0x39, 0x7f, 0x5e, 0x8e, 0x3a, 0xaf, 0x2f, 0x47, 0x9d, 0x7f, 0x2f, 0x47, 0x9d, 0x1f, 0x86, - 0xd7, 0xff, 0xa4, 0x78, 0x79, 0xc3, 0xfe, 0x79, 0xfc, 0x7f, 0x00, 0x00, 0x00, 0xff, 0xff, 0x8b, - 0xf0, 0x9f, 0x14, 0x77, 0x08, 0x00, 0x00, + // 903 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x9c, 0x56, 0x4f, 0x6f, 0x1b, 0x45, + 0x14, 0xf7, 0x6e, 0x9c, 0x26, 0x7e, 0xd9, 0x3a, 0xdb, 0x69, 0x0a, 0x8b, 0x1b, 0xb9, 0xd6, 0x1e, + 0x50, 0xc4, 0xa1, 0x7f, 0x52, 0x04, 0x42, 0xc0, 0x81, 0x26, 0x2e, 0x5d, 0xa1, 0x92, 0x6a, 0x0c, + 0x42, 0x42, 0x02, 0x69, 0xba, 0xfb, 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x59, 0xf2, + 0x90, 0x9c, 0xc0, 0x6e, 0xa9, 0xab, 0x64, 0xd0, 0x2a, 0xb6, 0x46, 0x75, 0x06, 0xf7, 0xdb, 0x85, + 0x38, 0x57, 0x9b, 0xe7, 0x05, 0xa2, 0xd6, 0x2e, 0x72, 0x7f, 0x43, 0x69, 0x6a, 0x61, 0x1c, 0x1c, + 0xb6, 0x3b, 0x37, 0x70, 0x92, 0xa4, 0x0d, 0xa7, 0x12, 0xc1, 0x36, 0x9c, 0x86, 0xfa, 0x51, 0x70, + 0xeb, 0x2f, 0xc2, 0x44, 0x65, 0xc8, 0xe6, 0xe4, 0x70, 0xe3, 0xc2, 0x35, 0x3e, 0x17, 0x83, 0x1b, + 0xbd, 0x47, 0xd6, 0x63, 0xeb, 0xd9, 0x87, 0x7f, 0x5f, 0x0e, 0xad, 0xb7, 0x97, 0x43, 0xeb, 0xdf, + 0xcb, 0xa1, 0xf5, 0xc7, 0xd5, 0xb0, 0xf3, 0xf6, 0x6a, 0xd8, 0xf9, 0xe7, 0x6a, 0xd8, 0xf9, 0x7e, + 0x70, 0xfd, 0xff, 0x19, 0xaf, 0x6f, 0x99, 0x3f, 0x4f, 0xff, 0x0b, 0x00, 0x00, 0xff, 0xff, 0x24, + 0x0c, 0x9c, 0xee, 0x8c, 0x08, 0x00, 0x00, } func (m *HeadSyncRange) Marshal() (dAtA []byte, err error) { diff --git a/etc/configs/node1.yml b/etc/configs/node1.yml index de26ad63..3fc0de03 100755 --- a/etc/configs/node1.yml +++ b/etc/configs/node1.yml @@ -61,7 +61,7 @@ nodes: - file space: gcTTL: 60 - syncPeriod: 600 + syncPeriod: 20 storage: path: db/node/1/data metric: diff --git a/etc/configs/node2.yml b/etc/configs/node2.yml index 6fdc51b3..6934923a 100755 --- a/etc/configs/node2.yml +++ b/etc/configs/node2.yml @@ -61,7 +61,7 @@ nodes: - file space: gcTTL: 60 - syncPeriod: 600 + syncPeriod: 20 storage: path: db/node/2/data metric: diff --git a/etc/configs/node3.yml b/etc/configs/node3.yml index 3de95882..029192f1 100755 --- a/etc/configs/node3.yml +++ b/etc/configs/node3.yml @@ -61,7 +61,7 @@ nodes: - file space: gcTTL: 60 - syncPeriod: 600 + syncPeriod: 20 storage: path: db/node/3/data metric: diff --git a/node/nodespace/rpchandler.go b/node/nodespace/rpchandler.go index f82f6d79..87dc5eb4 100644 --- a/node/nodespace/rpchandler.go +++ b/node/nodespace/rpchandler.go @@ -2,6 +2,7 @@ package nodespace import ( "context" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app/ocache" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/spacesyncproto" ) @@ -54,10 +55,17 @@ func (r *rpcHandler) SpacePush(ctx context.Context, req *spacesyncproto.SpacePus return } -func (r *rpcHandler) HeadSync(ctx context.Context, req *spacesyncproto.HeadSyncRequest) (*spacesyncproto.HeadSyncResponse, error) { - sp, err := r.s.GetSpace(ctx, req.SpaceId) +func (r *rpcHandler) HeadSync(ctx context.Context, req *spacesyncproto.HeadSyncRequest) (resp *spacesyncproto.HeadSyncResponse, err error) { + sp, err := r.s.GetOrPickSpace(ctx, req.SpaceId) if err != nil { - return nil, spacesyncproto.ErrSpaceMissing + switch err { + case ocache.ErrNotExists: + err = spacesyncproto.ErrSpaceNotInCache + case spacesyncproto.ErrSpaceMissing: + default: + err = spacesyncproto.ErrUnexpected + } + return } return sp.SpaceSyncRpc().HeadSync(ctx, req) } diff --git a/node/nodespace/service.go b/node/nodespace/service.go index e3dfacd2..0c9110ab 100644 --- a/node/nodespace/service.go +++ b/node/nodespace/service.go @@ -8,7 +8,10 @@ import ( "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/spacestorage" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/spacesyncproto" + peer "github.com/anytypeio/go-anytype-infrastructure-experiments/common/net/peer" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/net/rpc/server" + "github.com/anytypeio/go-anytype-infrastructure-experiments/common/nodeconf" + "golang.org/x/exp/slices" "time" ) @@ -22,6 +25,7 @@ func New() Service { type Service interface { GetSpace(ctx context.Context, id string) (commonspace.Space, error) + GetOrPickSpace(ctx context.Context, id string) (commonspace.Space, error) app.ComponentRunnable } @@ -29,12 +33,14 @@ type service struct { conf commonspace.Config spaceCache ocache.OCache commonSpace commonspace.SpaceService + confService nodeconf.Service spaceStorageProvider spacestorage.SpaceStorageProvider } func (s *service) Init(a *app.App) (err error) { s.conf = a.MustComponent("config").(commonspace.ConfigGetter).GetSpace() s.commonSpace = a.MustComponent(commonspace.CName).(commonspace.SpaceService) + s.confService = a.MustComponent(nodeconf.CName).(nodeconf.Service) s.spaceStorageProvider = a.MustComponent(spacestorage.CName).(spacestorage.SpaceStorageProvider) s.spaceCache = ocache.New( s.loadSpace, @@ -61,6 +67,37 @@ func (s *service) GetSpace(ctx context.Context, id string) (commonspace.Space, e return v.(commonspace.Space), nil } +func (s *service) GetOrPickSpace(ctx context.Context, id string) (sp commonspace.Space, err error) { + var v ocache.Object + peerId, err := peer.CtxPeerId(ctx) + if err != nil { + return + } + // if we are getting request from our fellow node + // don't try to wake the space, otherwise it can be possible + // that node would be waking up infinitely, depending on + // OCache ttl and HeadSync period + if slices.Contains(s.confService.GetLast().NodeIds(id), peerId) { + v, err = s.spaceCache.Pick(ctx, id) + if err != nil { + // safely checking that we don't have the space storage + // this should not open the database in case of node + if !s.spaceStorageProvider.SpaceExists(id) { + err = spacesyncproto.ErrSpaceMissing + } + return + } + } else { + // if the request is from the client it is safe to wake up the node + v, err = s.spaceCache.Get(ctx, id) + if err != nil { + return + } + } + sp = v.(commonspace.Space) + return +} + func (s *service) loadSpace(ctx context.Context, id string) (value ocache.Object, err error) { cc, err := s.commonSpace.NewSpace(ctx, id) if err != nil { diff --git a/node/storage/storageservice.go b/node/storage/storageservice.go index 129c7f05..6e8ac73a 100644 --- a/node/storage/storageservice.go +++ b/node/storage/storageservice.go @@ -4,6 +4,7 @@ import ( "github.com/anytypeio/go-anytype-infrastructure-experiments/common/app" "github.com/anytypeio/go-anytype-infrastructure-experiments/common/commonspace/spacestorage" "os" + "path" ) type storageService struct { @@ -33,6 +34,14 @@ func (s *storageService) SpaceStorage(id string) (spacestorage.SpaceStorage, err return newSpaceStorage(s.rootPath, id) } +func (s *storageService) SpaceExists(id string) bool { + dbPath := path.Join(s.rootPath, id) + if _, err := os.Stat(dbPath); err != nil { + return false + } + return true +} + func (s *storageService) CreateSpaceStorage(payload spacestorage.SpaceStorageCreatePayload) (spacestorage.SpaceStorage, error) { return createSpaceStorage(s.rootPath, payload) }