-
Notifications
You must be signed in to change notification settings - Fork 117
/
connection_cache.go
339 lines (294 loc) · 8.71 KB
/
connection_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
330
331
332
333
334
335
336
337
338
339
package runtime
import (
"context"
"errors"
"fmt"
"slices"
"strings"
"sync"
"time"
"github.com/hashicorp/golang-lru/simplelru"
"github.com/rilldata/rill/runtime/drivers"
"github.com/rilldata/rill/runtime/pkg/activity"
"github.com/rilldata/rill/runtime/pkg/observability"
"go.uber.org/zap"
"golang.org/x/exp/maps"
)
var errConnectionCacheClosed = errors.New("connectionCache: closed")
const migrateTimeout = 2 * time.Minute
// connectionCache is a thread-safe cache for open connections.
// Connections should preferably be opened only via the connection cache.
//
// TODO: It opens connections async, but it will close them sync when evicted. If a handle's close hangs, this can block the cache.
// We should move the closing to the background. However, it must then handle the case of trying to re-open a connection that's currently closing in the background.
type connectionCache struct {
size int
runtime *Runtime
logger *zap.Logger
activity activity.Client
closed bool
migrateCtx context.Context // ctx used for connection migrations
migrateCtxCancel context.CancelFunc // cancel all running migrations
lock sync.Mutex
acquired map[string]*connWithRef // items with non-zero references (in use) which should not be evicted
lru *simplelru.LRU // items with no references (opened, but not in use) ready for eviction
}
type connWithRef struct {
instanceID string
handle drivers.Handle
err error
refs int
ready chan struct{}
}
func newConnectionCache(size int, logger *zap.Logger, rt *Runtime, ac activity.Client) *connectionCache {
// LRU cache that closes evicted connections
lru, err := simplelru.NewLRU(size, func(key interface{}, value interface{}) {
// Skip if the conn has refs, since the callback also gets called when transferring to acquired cache
conn := value.(*connWithRef)
if conn.refs != 0 {
return
}
if conn.handle != nil {
if err := conn.handle.Close(); err != nil {
logger.Error("failed closing cached connection", zap.String("key", key.(string)), zap.Error(err))
}
}
})
if err != nil {
panic(err)
}
ctx, cancel := context.WithCancel(context.Background())
return &connectionCache{
size: size,
runtime: rt,
logger: logger,
activity: ac,
migrateCtx: ctx,
migrateCtxCancel: cancel,
acquired: make(map[string]*connWithRef),
lru: lru,
}
}
func (c *connectionCache) Close() error {
c.lock.Lock()
defer c.lock.Unlock()
if c.closed {
return errConnectionCacheClosed
}
c.closed = true
// Cancel currently running migrations
c.migrateCtxCancel()
var firstErr error
for _, key := range c.lru.Keys() {
val, ok := c.lru.Get(key)
if !ok {
continue
}
conn := val.(*connWithRef)
if conn.handle == nil {
continue
}
err := conn.handle.Close()
if err != nil {
c.logger.Error("failed closing cached connection", zap.Error(err))
if firstErr == nil {
firstErr = err
}
}
}
for _, value := range c.acquired {
if value.handle == nil {
continue
}
err := value.handle.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 string, config map[string]any, shared bool) (drivers.Handle, func(), error) {
var key string
if shared {
// not using instanceID to ensure all instances share the same handle
key = driver + generateKey(config)
} else {
key = instanceID + driver + generateKey(config)
}
c.lock.Lock()
if c.closed {
c.lock.Unlock()
return nil, nil, errConnectionCacheClosed
}
// Get conn from caches
conn, ok := c.acquired[key]
if ok {
conn.refs++
} else {
var val any
val, ok = c.lru.Get(key)
if ok {
// Conn was found in LRU - move to acquired cache
conn = val.(*connWithRef)
conn.refs++ // NOTE: Must increment before call to c.lru.remove to avoid closing the conn
c.lru.Remove(key)
c.acquired[key] = conn
}
}
// Cached conn not found, open a new one
if !ok {
conn = &connWithRef{
instanceID: instanceID,
refs: 1, // Since refs is assumed to already have been incremented when checking conn.ready
ready: make(chan struct{}),
}
c.acquired[key] = conn
if len(c.acquired)+c.lru.Len() > c.size {
c.logger.Warn("number of connections acquired and in LRU exceed total configured size", zap.Int("acquired", len(c.acquired)), zap.Int("lru", c.lru.Len()))
}
// Open and migrate the connection in a separate goroutine (outside lock).
// Incrementing ref and releasing the conn for this operation separately to cover the case where all waiting goroutines are cancelled before the migration completes.
conn.refs++
go func() {
handle, err := c.openAndMigrate(c.migrateCtx, instanceID, driver, shared, config)
c.lock.Lock()
conn.handle = handle
conn.err = err
c.releaseConn(key, conn)
wasClosed := c.closed
c.lock.Unlock()
close(conn.ready)
// The cache might have been closed while the connection was being opened.
// Since we acquired the lock, the close will have already been completed, so we need to close the connection here.
if wasClosed && handle != nil {
_ = handle.Close()
}
}()
}
// We can now release the lock and wait for the connection to be ready (it might already be)
c.lock.Unlock()
// Wait for connection to be ready or context to be cancelled
var err error
select {
case <-conn.ready:
case <-ctx.Done():
err = ctx.Err() // Will always be non-nil, ensuring releaseConn is called
}
// Lock again for accessing conn
c.lock.Lock()
defer c.lock.Unlock()
if err == nil {
err = conn.err
}
if err != nil {
c.releaseConn(key, conn)
return nil, nil, err
}
release := func() {
c.lock.Lock()
c.releaseConn(key, conn)
c.lock.Unlock()
}
return conn.handle, release, nil
}
func (c *connectionCache) releaseConn(key string, conn *connWithRef) {
conn.refs--
if conn.refs == 0 {
// No longer referenced. Move from acquired to LRU.
if !c.closed {
delete(c.acquired, key)
c.lru.Add(key, conn)
}
}
}
func (c *connectionCache) openAndMigrate(ctx context.Context, instanceID, driver string, shared bool, config map[string]any) (drivers.Handle, error) {
logger := c.logger
if instanceID != "default" {
logger = c.logger.With(zap.String("instance_id", instanceID), zap.String("driver", driver))
}
ctx, cancel := context.WithTimeout(ctx, migrateTimeout)
defer cancel()
activityClient := c.activity
if !shared {
inst, err := c.runtime.Instance(ctx, instanceID)
if err != nil {
return nil, err
}
activityDims := instanceAnnotationsToAttribs(inst)
if activityClient != nil {
activityClient = activityClient.With(activityDims...)
}
}
handle, err := drivers.Open(driver, config, shared, activityClient, logger)
if err == nil && ctx.Err() != nil {
err = fmt.Errorf("timed out while opening driver %q", driver)
}
if err != nil {
return nil, err
}
err = handle.Migrate(ctx)
if err != nil {
handle.Close()
if errors.Is(err, ctx.Err()) {
err = fmt.Errorf("timed out while migrating driver %q: %w", driver, err)
}
return nil, err
}
return handle, nil
}
// evictAll closes all connections for an instance.
func (c *connectionCache) evictAll(ctx context.Context, instanceID string) {
c.lock.Lock()
defer c.lock.Unlock()
if c.closed {
return
}
for key, conn := range c.acquired {
if conn.instanceID != instanceID {
continue
}
if conn.handle != nil {
err := conn.handle.Close()
if err != nil {
c.logger.Error("connection cache: failed to close cached connection", zap.Error(err), zap.String("instance", instanceID), observability.ZapCtx(ctx))
}
conn.handle = nil
conn.err = fmt.Errorf("connection evicted") // Defensive, should never be accessed
}
delete(c.acquired, key)
}
for _, key := range c.lru.Keys() {
connT, ok := c.lru.Get(key)
if !ok {
panic("connection cache: key not found in LRU")
}
conn := connT.(*connWithRef)
if conn.instanceID != instanceID {
continue
}
if conn.handle != nil {
err := conn.handle.Close()
if err != nil {
c.logger.Error("connection cache: failed to close cached connection", zap.Error(err), zap.String("instance", instanceID), observability.ZapCtx(ctx))
}
conn.handle = nil
conn.err = fmt.Errorf("connection evicted") // Defensive, should never be accessed
}
c.lru.Remove(key)
}
}
func generateKey(m map[string]any) string {
sb := strings.Builder{}
keys := maps.Keys(m)
slices.Sort(keys)
for _, key := range keys {
sb.WriteString(key)
sb.WriteString(":")
sb.WriteString(fmt.Sprint(m[key]))
sb.WriteString(" ")
}
return sb.String()
}