-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.go
329 lines (287 loc) · 7.7 KB
/
cache.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package nitecache
import (
"context"
"errors"
"fmt"
"github.com/MysteriousPotato/nitecache/inmem"
"github.com/MysteriousPotato/nitecache/servicepb"
"google.golang.org/grpc/credentials/insecure"
"sync"
"time"
"github.com/MysteriousPotato/nitecache/hashring"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
var (
ErrDuplicatePeer = errors.New("duplicate peer detected")
ErrTableNotFound = errors.New("table not found")
ErrMissingMembers = errors.New("peers must contain at least one member")
ErrCacheDestroyed = errors.New("can't use cache after tear down")
)
type (
CacheOpt func(c *Cache)
// Cache hold the nitecache instance. The zero value is not read for use.
//
// Refer to [NewCache] for creating an instance.
Cache struct {
ring *hashring.Ring
self Member
clients clients
clientMu *sync.Mutex
tables map[string]table
tablesMu *sync.Mutex
metrics *metrics
virtualNodes int
hashFunc hashring.HashFunc
timeout time.Duration
members []Member
grpcOpts []grpc.ServerOption
service server
transportCredentials credentials.TransportCredentials
}
)
type Member struct {
ID string
Addr string
}
type table interface {
getLocally(ctx context.Context, key string) (inmem.Item[[]byte], bool, error)
putLocally(key string, item inmem.Item[[]byte]) error
evictLocally(key string) error
evictAllLocally(keys []string)
callLocally(ctx context.Context, key, procedure string, args []byte) (inmem.Item[[]byte], error)
tearDown()
}
// NewCache Creates a new [Cache] instance
//
// This should only be called once for a same set of peers, so that connections can be reused.
//
// Create a new [Table] using [NewTable] if you need to store different values.
//
// Members must have a unique ID and Addr.
//
// Ex.:
//
// func() {
// self := nitecache.Member{ID: "1", Addr: "localhost:8000"}
// c, err := nitecache.NewCache(self, nitecache.Member{self})
// ...
// }
func NewCache(self Member, peers []Member, opts ...CacheOpt) (*Cache, error) {
c := &Cache{
self: self,
clients: clients{},
clientMu: &sync.Mutex{},
tables: make(map[string]table),
tablesMu: &sync.Mutex{},
metrics: newMetrics(),
virtualNodes: 32,
hashFunc: hashring.DefaultHashFunc,
timeout: time.Second * 3,
members: []Member{},
transportCredentials: insecure.NewCredentials(),
}
for _, opt := range opts {
opt(c)
}
var peersIncludeSelf bool
for _, peer := range peers {
if peer.ID == self.ID {
peersIncludeSelf = true
break
}
}
if !peersIncludeSelf {
peers = append(peers, self)
}
var err error
c.service, err = newService(self.Addr, c)
if err != nil {
return nil, fmt.Errorf("unable to create cache service: %w", err)
}
if err := c.SetPeers(peers); err != nil {
return nil, err
}
return c, nil
}
// VirtualNodeOpt sets the number of points/node on the hashring
// Defaults to 32
func VirtualNodeOpt(nodes int) func(c *Cache) {
return func(c *Cache) {
c.virtualNodes = nodes
}
}
// TimeoutOpt sets the timeout for grpc clients
// Defaults to 3 seconds
func TimeoutOpt(timeout time.Duration) func(c *Cache) {
return func(c *Cache) {
c.timeout = timeout
}
}
// HashFuncOpt sets the hash function used to determine hashring keys
// Defaults to FNV-1 algorithm
func HashFuncOpt(hashFunc hashring.HashFunc) func(c *Cache) {
return func(c *Cache) {
c.hashFunc = hashFunc
}
}
// GRPCTransportCredentials sets the credentials for the gRPC server
func GRPCTransportCredentials(opts credentials.TransportCredentials) func(c *Cache) {
return func(c *Cache) {
c.transportCredentials = opts
}
}
// GRPCServerOpts sets the options when creating the gRPC service.
func GRPCServerOpts(opts ...grpc.ServerOption) func(c *Cache) {
return func(c *Cache) {
c.grpcOpts = opts
}
}
// GetMetrics Returns a copy of the current cache Metrics.
// For Metrics specific to a [Table], refer to [Table.GetMetrics].
func (c *Cache) GetMetrics() (Metrics, error) {
if c.isZero() {
return Metrics{}, ErrCacheDestroyed
}
return c.metrics.getCopy(), nil
}
// SetPeers will update the cache members to the new value.
func (c *Cache) SetPeers(peers []Member) error {
if c.isZero() {
return ErrCacheDestroyed
}
if len(peers) == 0 {
return ErrMissingMembers
}
membersAddrMap := map[string]struct{}{}
membersIDMap := map[string]struct{}{}
var containsSelf bool
for _, p := range peers {
if _, ok := membersAddrMap[p.Addr]; ok {
return fmt.Errorf("%w for Address %v", ErrDuplicatePeer, p.Addr)
}
if _, ok := membersIDMap[p.ID]; ok {
return fmt.Errorf("%w for ID %v", ErrDuplicatePeer, p.ID)
}
if c.self.ID == p.ID {
containsSelf = true
}
membersAddrMap[p.Addr] = struct{}{}
membersIDMap[p.ID] = struct{}{}
}
if !containsSelf {
peers = append(peers, c.self)
}
members := make([]string, len(peers))
for i, p := range peers {
members[i] = p.ID
}
var err error
if c.ring == nil {
c.ring, err = hashring.New(hashring.Opt{
Members: members,
VirtualNodes: c.virtualNodes,
HashFunc: c.hashFunc,
})
if err != nil {
return fmt.Errorf("unable to create hashring: %w", err)
}
} else {
if err := c.ring.SetMembers(members); err != nil {
return fmt.Errorf("unable to update hashring: %w", err)
}
}
if err := c.setClients(peers); err != nil {
return err
}
return nil
}
// TearDown properly tears down all [Table] from [Cache], closes all client connections and stops the grpc server.
//
// Once called, using it or any of its table references cause [ErrCacheDestroyed] to be returned.
func (c *Cache) TearDown() error {
if c.isZero() {
return ErrCacheDestroyed
}
var errs []error
for _, client := range c.clients {
if err := client.conn.Close(); err != nil {
errs = append(errs, err)
}
}
c.service.server.GracefulStop()
for i := range c.tables {
c.tables[i].tearDown()
}
*c = Cache{}
return errors.Join(errs...)
}
// ListenAndServe starts the cache grpc server
func (c *Cache) ListenAndServe() error {
if c.isZero() {
return ErrCacheDestroyed
}
return c.service.server.Serve(c.service.listener)
}
// HealthCheckPeers checks the status of the cache's grpc clients
func (c *Cache) HealthCheckPeers(ctx context.Context) error {
if c.isZero() {
return ErrCacheDestroyed
}
var errs []error
for _, client := range c.clients {
if _, err := client.HealthCheck(ctx, &servicepb.Empty{}); err != nil {
errs = append(errs, err)
}
}
return errors.Join(errs...)
}
func (c *Cache) getTable(name string) (table, error) {
t, ok := c.tables[name]
if !ok {
return nil, ErrTableNotFound
}
return t, nil
}
func (c *Cache) getClient(p string) (*client, error) {
cl, ok := c.clients[p]
if !ok {
return nil, fmt.Errorf("unable to find peer client with ID %v", p)
}
return cl, nil
}
// Cleanup clients that are not present in peers and create new clients for new peers
func (c *Cache) setClients(peers []Member) error {
c.clientMu.Lock()
defer c.clientMu.Unlock()
peersMap := map[string]Member{}
for _, p := range peers {
peersMap[p.ID] = p
}
var errs []error
for id := range c.clients {
if _, ok := peersMap[id]; !ok {
if err := c.clients[id].conn.Close(); err != nil {
errs = append(errs, err)
}
delete(c.clients, id)
}
}
for id, p := range peersMap {
if _, ok := c.clients[id]; ok {
continue
}
client, err := newClient(p.Addr, c)
if err != nil {
return err
}
c.clients[id] = client
}
if errs != nil {
return errors.Join(errs...)
}
return nil
}
func (c *Cache) isZero() bool {
return c == nil || c.tables == nil
}