-
Notifications
You must be signed in to change notification settings - Fork 111
/
connections.go
271 lines (231 loc) · 7.52 KB
/
connections.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
package runtime
import (
"context"
"fmt"
"strings"
runtimev1 "github.com/rilldata/rill/proto/gen/rill/runtime/v1"
"github.com/rilldata/rill/runtime/drivers"
"github.com/rilldata/rill/runtime/services/catalog"
)
func (r *Runtime) AcquireSystemHandle(ctx context.Context, connector string) (drivers.Handle, func(), error) {
for _, c := range r.opts.SystemConnectors {
if c.Name == connector {
cfg := make(map[string]any, len(c.Config)+1)
for k, v := range c.Config {
cfg[strings.ToLower(k)] = v
}
cfg["allow_host_access"] = r.opts.AllowHostAccess
return r.connCache.get(ctx, "", c.Type, cfg, true)
}
}
return nil, nil, fmt.Errorf("connector %s doesn't exist", connector)
}
// AcquireHandle returns instance specific handle
func (r *Runtime) AcquireHandle(ctx context.Context, instanceID, connector string) (drivers.Handle, func(), error) {
driver, cfg, err := r.connectorConfig(ctx, instanceID, connector)
if err != nil {
return nil, nil, err
}
return r.connCache.get(ctx, instanceID, driver, cfg, false)
}
// EvictHandle flushes the db handle for the specific connector from the cache
func (r *Runtime) EvictHandle(ctx context.Context, instanceID, connector string, drop bool) error {
driver, cfg, err := r.connectorConfig(ctx, instanceID, connector)
if err != nil {
return err
}
r.connCache.evict(ctx, instanceID, driver, cfg)
if drop {
return drivers.Drop(driver, cfg, r.logger)
}
return nil
}
func (r *Runtime) Registry() drivers.RegistryStore {
registry, ok := r.metastore.AsRegistry()
if !ok {
// Verified as registry in New, so this should never happen
panic("metastore is not a registry")
}
return registry
}
func (r *Runtime) Repo(ctx context.Context, instanceID string) (drivers.RepoStore, func(), error) {
inst, err := r.FindInstance(ctx, instanceID)
if err != nil {
return nil, nil, err
}
conn, release, err := r.AcquireHandle(ctx, instanceID, inst.RepoConnector)
if err != nil {
return nil, nil, err
}
repo, ok := conn.AsRepoStore(instanceID)
if !ok {
release()
// Verified as repo when instance is created, so this should never happen
return nil, release, fmt.Errorf("connection for instance '%s' is not a repo", instanceID)
}
return repo, release, nil
}
func (r *Runtime) OLAP(ctx context.Context, instanceID string) (drivers.OLAPStore, func(), error) {
inst, err := r.FindInstance(ctx, instanceID)
if err != nil {
return nil, nil, err
}
conn, release, err := r.AcquireHandle(ctx, instanceID, inst.OLAPConnector)
if err != nil {
return nil, nil, err
}
olap, ok := conn.AsOLAP(instanceID)
if !ok {
release()
// Verified as OLAP when instance is created, so this should never happen
return nil, nil, fmt.Errorf("connection for instance '%s' is not an olap", instanceID)
}
return olap, release, nil
}
func (r *Runtime) Catalog(ctx context.Context, instanceID string) (drivers.CatalogStore, func(), error) {
inst, err := r.FindInstance(ctx, instanceID)
if err != nil {
return nil, nil, err
}
if inst.EmbedCatalog {
conn, release, err := r.AcquireHandle(ctx, instanceID, inst.OLAPConnector)
if err != nil {
return nil, nil, err
}
store, ok := conn.AsCatalogStore(instanceID)
if !ok {
release()
// Verified as CatalogStore when instance is created, so this should never happen
return nil, nil, fmt.Errorf("instance cannot embed catalog")
}
return store, release, nil
}
store, ok := r.metastore.AsCatalogStore(instanceID)
if !ok {
return nil, nil, fmt.Errorf("metastore cannot serve as catalog")
}
return store, func() {}, nil
}
func (r *Runtime) NewCatalogService(ctx context.Context, instanceID string) (*catalog.Service, error) {
// get all stores
olapStore, releaseOLAP, err := r.OLAP(ctx, instanceID)
if err != nil {
return nil, err
}
catalogStore, releaseCatalog, err := r.Catalog(ctx, instanceID)
if err != nil {
releaseOLAP()
return nil, err
}
repoStore, releaseRepo, err := r.Repo(ctx, instanceID)
if err != nil {
releaseOLAP()
releaseCatalog()
return nil, err
}
registry := r.Registry()
migrationMetadata := r.migrationMetaCache.get(instanceID)
releaseFunc := func() {
releaseOLAP()
releaseCatalog()
releaseRepo()
}
inst, err := r.FindInstance(ctx, instanceID)
if err != nil {
return nil, err
}
activityDims := instanceAnnotationsToAttribs(inst)
ac := r.activity.With(activityDims...)
return catalog.NewService(catalogStore, repoStore, olapStore, registry, instanceID, r.logger, migrationMetadata, releaseFunc, ac), nil
}
func (r *Runtime) connectorConfig(ctx context.Context, instanceID, name string) (string, map[string]any, error) {
inst, err := r.FindInstance(ctx, instanceID)
if err != nil {
return "", nil, err
}
// Search for connector definition in instance
var connector *runtimev1.Connector
for _, c := range inst.Connectors {
if c.Name == name {
connector = c
break
}
}
// Search for connector definition in rill.yaml
if connector == nil {
for _, c := range inst.ProjectConnectors {
if c.Name == name {
connector = c
break
}
}
}
// Search for implicit connectors (where the name matches a driver name)
if connector == nil {
_, ok := drivers.Drivers[name]
if ok {
connector = &runtimev1.Connector{
Type: name,
Name: name,
}
}
}
// Return if search for connector was unsuccessful
if connector == nil {
return "", nil, fmt.Errorf("unknown connector %q", name)
}
// Build connector config
cfg := make(map[string]any)
// Apply config from definition
for key, value := range connector.Config {
cfg[strings.ToLower(key)] = value
}
// Instance variables matching the format "connector.name.var" are applied to the connector config
vars := inst.ResolveVariables()
prefix := fmt.Sprintf("connector.%s.", name)
for k, v := range vars {
if after, found := strings.CutPrefix(k, prefix); found {
cfg[strings.ToLower(after)] = v
}
}
// For backwards compatibility, certain root-level variables apply to certain implicit connectors.
// NOTE: This switches on connector.Name, not connector.Type, because this only applies to implicit connectors.
switch connector.Name {
case "s3", "athena":
setIfNil(cfg, "aws_access_key_id", vars["aws_access_key_id"])
setIfNil(cfg, "aws_secret_access_key", vars["aws_secret_access_key"])
setIfNil(cfg, "aws_session_token", vars["aws_session_token"])
case "azure":
setIfNil(cfg, "azure_storage_account", vars["azure_storage_account"])
setIfNil(cfg, "azure_storage_key", vars["azure_storage_key"])
setIfNil(cfg, "azure_storage_sas_token", vars["azure_storage_sas_token"])
setIfNil(cfg, "azure_storage_connection_string", vars["azure_storage_connection_string"])
case "gcs":
setIfNil(cfg, "google_application_credentials", vars["google_application_credentials"])
case "bigquery":
setIfNil(cfg, "google_application_credentials", vars["google_application_credentials"])
case "motherduck":
setIfNil(cfg, "token", vars["token"])
setIfNil(cfg, "dsn", "")
}
// Apply built-in connector config
cfg["allow_host_access"] = r.opts.AllowHostAccess
// The "local_file" connector needs to know the repo root.
// TODO: This is an ugly hack. But how can we get rid of it?
if connector.Name == "local_file" {
if inst.RepoConnector != "local_file" { // The RepoConnector shouldn't be named "local_file", but let's still try to avoid infinite recursion
repo, release, err := r.Repo(ctx, instanceID)
if err != nil {
return "", nil, err
}
cfg["dsn"] = repo.Root()
release()
}
}
return connector.Type, cfg, nil
}
func setIfNil(m map[string]any, key string, value any) {
if _, ok := m[key]; !ok {
m[key] = value
}
}