-
Notifications
You must be signed in to change notification settings - Fork 12
/
service.go
165 lines (146 loc) · 5.07 KB
/
service.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//go:generate mockgen -destination mock_nodespace/mock_nodespace.go github.com/anyproto/any-sync-node/nodespace Service,NodeSpace
package nodespace
import (
"context"
"time"
"github.com/anyproto/any-sync/app"
"github.com/anyproto/any-sync/app/logger"
"github.com/anyproto/any-sync/app/ocache"
"github.com/anyproto/any-sync/commonspace"
"github.com/anyproto/any-sync/commonspace/config"
"github.com/anyproto/any-sync/commonspace/spacestorage"
"github.com/anyproto/any-sync/commonspace/spacesyncproto"
"github.com/anyproto/any-sync/consensus/consensusclient"
"github.com/anyproto/any-sync/coordinator/coordinatorclient"
"github.com/anyproto/any-sync/metric"
"github.com/anyproto/any-sync/net/rpc/server"
"github.com/anyproto/any-sync/net/streampool"
"github.com/anyproto/any-sync/nodeconf"
"go.uber.org/zap"
"github.com/anyproto/any-sync-node/nodehead"
"github.com/anyproto/any-sync-node/nodespace/treesyncer"
"github.com/anyproto/any-sync-node/nodestorage"
)
const CName = "node.nodespace"
var log = logger.NewNamed(CName)
func New() Service {
return &service{}
}
type Service interface {
GetSpace(ctx context.Context, id string) (NodeSpace, error)
PickSpace(ctx context.Context, id string) (NodeSpace, error)
Cache() ocache.OCache
StreamPool() streampool.StreamPool
app.ComponentRunnable
}
type service struct {
conf config.Config
spaceCache ocache.OCache
commonSpace commonspace.SpaceService
confService nodeconf.Service
consClient consensusclient.Service
spaceStorageProvider nodestorage.NodeStorage
streamPool streampool.StreamPool
nodeHead nodehead.NodeHead
metric metric.Metric
coordClient coordinatorclient.CoordinatorClient
}
func (s *service) Init(a *app.App) (err error) {
s.conf = a.MustComponent("config").(config.ConfigGetter).GetSpace()
s.commonSpace = a.MustComponent(commonspace.CName).(commonspace.SpaceService)
s.confService = a.MustComponent(nodeconf.CName).(nodeconf.Service)
s.spaceStorageProvider = a.MustComponent(spacestorage.CName).(nodestorage.NodeStorage)
s.nodeHead = a.MustComponent(nodehead.CName).(nodehead.NodeHead)
s.streamPool = a.MustComponent(streampool.CName).(streampool.Service).NewStreamPool(&streamHandler{s: s}, streampool.StreamConfig{
SendQueueSize: 100,
DialQueueWorkers: 4,
DialQueueSize: 1000,
})
s.consClient = a.MustComponent(consensusclient.CName).(consensusclient.Service)
s.spaceCache = ocache.New(
s.loadSpace,
ocache.WithLogger(log.Sugar()),
ocache.WithGCPeriod(time.Minute),
ocache.WithTTL(time.Duration(s.conf.GCTTL)*time.Second),
ocache.WithPrometheus(a.MustComponent(metric.CName).(metric.Metric).Registry(), "space", "cache"),
)
s.metric = a.MustComponent(metric.CName).(metric.Metric)
s.coordClient = app.MustComponent[coordinatorclient.CoordinatorClient](a)
return spacesyncproto.DRPCRegisterSpaceSync(a.MustComponent(server.CName).(server.DRPCServer), &rpcHandler{s})
}
func (s *service) Name() (name string) {
return CName
}
func (s *service) Run(ctx context.Context) (err error) {
return
}
func (s *service) StreamPool() streampool.StreamPool {
return s.streamPool
}
func (s *service) PickSpace(ctx context.Context, id string) (NodeSpace, error) {
v, err := s.spaceCache.Pick(ctx, id)
if err != nil {
return nil, err
}
return v.(NodeSpace), nil
}
func (s *service) GetSpace(ctx context.Context, id string) (NodeSpace, error) {
v, err := s.spaceCache.Get(ctx, id)
if err != nil {
return nil, err
}
space := v.(NodeSpace)
return space, nil
}
func (s *service) HandleMessage(ctx context.Context, senderId string, req *spacesyncproto.ObjectSyncMessage) (err error) {
var msg = &spacesyncproto.SpaceSubscription{}
if err = msg.Unmarshal(req.Payload); err != nil {
return
}
log.InfoCtx(ctx, "got subscription message", zap.Strings("spaceIds", msg.SpaceIds))
if msg.Action == spacesyncproto.SpaceSubscriptionAction_Subscribe {
return s.streamPool.AddTagsCtx(ctx, msg.SpaceIds...)
} else {
return s.streamPool.RemoveTagsCtx(ctx, msg.SpaceIds...)
}
}
func (s *service) loadSpace(ctx context.Context, id string) (value ocache.Object, err error) {
defer func() {
log.InfoCtx(ctx, "space loaded", zap.String("id", id), zap.Error(err))
}()
if err = s.checkDeletionStatus(id); err != nil {
return nil, err
}
cc, err := s.commonSpace.NewSpace(ctx, id, commonspace.Deps{TreeSyncer: treesyncer.New(id)})
if err != nil {
return
}
ns, err := newNodeSpace(cc, s.consClient, s.spaceStorageProvider)
if err != nil {
return
}
if err = ns.Init(ctx); err != nil {
return
}
return ns, nil
}
func (s *service) checkDeletionStatus(spaceId string) (err error) {
delStorage := s.spaceStorageProvider.DeletionStorage()
status, err := delStorage.SpaceStatus(spaceId)
if err != nil {
if err == nodestorage.ErrUnknownSpaceId {
return nil
}
return err
}
if status == nodestorage.SpaceStatusRemove {
return spacesyncproto.ErrSpaceIsDeleted
}
return nil
}
func (s *service) Close(ctx context.Context) (err error) {
return s.spaceCache.Close()
}
func (s *service) Cache() ocache.OCache {
return s.spaceCache
}