-
Notifications
You must be signed in to change notification settings - Fork 117
/
caches.go
176 lines (144 loc) · 3.77 KB
/
caches.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
package runtime
import (
"context"
"errors"
"fmt"
"sync"
lru "github.com/hashicorp/golang-lru"
"github.com/hashicorp/golang-lru/simplelru"
"github.com/rilldata/rill/runtime/drivers"
"github.com/rilldata/rill/runtime/services/catalog"
"go.uber.org/zap"
)
var errConnectionCacheClosed = errors.New("connectionCache: closed")
type connectionCache struct {
cache *simplelru.LRU
lock sync.Mutex
closed bool
logger *zap.Logger
}
func newConnectionCache(size int, logger *zap.Logger) *connectionCache {
cache, err := simplelru.NewLRU(size, nil)
if err != nil {
panic(err)
}
return &connectionCache{cache: cache, logger: logger}
}
func (c *connectionCache) Close() error {
c.lock.Lock()
if c.closed {
c.lock.Unlock()
return errConnectionCacheClosed
}
c.closed = true
c.lock.Unlock()
var firstErr error
for _, key := range c.cache.Keys() {
val, _ := c.cache.Get(key)
err := val.(drivers.Connection).Close()
if err != nil {
c.logger.Error("failed closing cached connection", zap.Error(err))
if firstErr == nil {
firstErr = err
}
}
}
return firstErr
}
func (c *connectionCache) get(ctx context.Context, instanceID, driver, dsn string) (drivers.Connection, error) {
// TODO: This locks for all instances for the duration of Open and Migrate.
// Adapt to lock only on the lookup, and then on the individual instance's Open and Migrate.
c.lock.Lock()
defer c.lock.Unlock()
if c.closed {
return nil, errConnectionCacheClosed
}
key := instanceID + driver + dsn
val, ok := c.cache.Get(key)
if !ok {
conn, err := drivers.Open(driver, dsn, c.logger)
if err != nil {
return nil, err
}
err = conn.Migrate(ctx)
if err != nil {
return nil, err
}
c.cache.Add(key, conn)
return conn, nil
}
return val.(drivers.Connection), nil
}
type catalogCache struct {
cache map[string]*catalog.Service
lock sync.Mutex
}
func newCatalogCache() *catalogCache {
return &catalogCache{
cache: make(map[string]*catalog.Service),
}
}
func (c *catalogCache) get(ctx context.Context, rt *Runtime, instID string) (*catalog.Service, error) {
// TODO 1: opening a driver shouldn't take too long but we should still have an instance specific lock
// TODO 2: This is a cache on a cache, which may lead to undefined behavior
// TODO 3: Use LRU and not a map
c.lock.Lock()
defer c.lock.Unlock()
key := instID
service, ok := c.cache[key]
if ok {
return service, nil
}
registry, _ := rt.metastore.RegistryStore()
inst, err := registry.FindInstance(ctx, instID)
if err != nil {
return nil, err
}
olapConn, err := rt.connCache.get(ctx, instID, inst.OLAPDriver, inst.OLAPDSN)
if err != nil {
return nil, err
}
olap, _ := olapConn.OLAPStore()
var catalogStore drivers.CatalogStore
if inst.EmbedCatalog {
conn, err := rt.connCache.get(ctx, inst.ID, inst.OLAPDriver, inst.OLAPDSN)
if err != nil {
return nil, err
}
store, ok := conn.CatalogStore()
if !ok {
return nil, fmt.Errorf("instance cannot embed catalog")
}
catalogStore = store
} else {
store, ok := rt.metastore.CatalogStore()
if !ok {
return nil, fmt.Errorf("metastore cannot serve as catalog")
}
catalogStore = store
}
repoConn, err := rt.connCache.get(ctx, instID, inst.RepoDriver, inst.RepoDSN)
if err != nil {
return nil, err
}
repoStore, _ := repoConn.RepoStore()
service = catalog.NewService(catalogStore, repoStore, olap, instID, rt.logger)
c.cache[key] = service
return service, nil
}
type queryCache struct {
cache *lru.Cache
}
func newQueryCache(size int) *queryCache {
cache, err := lru.New(size)
if err != nil {
panic(err)
}
return &queryCache{cache: cache}
}
func (c *queryCache) get(key queryCacheKey) (any, bool) {
return c.cache.Get(key)
}
func (c *queryCache) add(key queryCacheKey, value any) bool {
return c.cache.Add(key, value)
}