-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathconnector.go
More file actions
384 lines (333 loc) · 10.1 KB
/
connector.go
File metadata and controls
384 lines (333 loc) · 10.1 KB
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// Copyright 2026 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package embedded
import (
"context"
"database/sql/driver"
"errors"
"fmt"
"os"
"path/filepath"
"sync"
"sync/atomic"
"time"
"github.com/cenkalti/backoff/v4"
"github.com/dolthub/dolt/go/cmd/dolt/commands"
"github.com/dolthub/dolt/go/cmd/dolt/commands/engine"
"github.com/dolthub/dolt/go/cmd/dolt/doltversion"
"github.com/dolthub/dolt/go/libraries/doltcore/dbfactory"
"github.com/dolthub/dolt/go/libraries/doltcore/env"
"github.com/dolthub/dolt/go/libraries/events"
"github.com/dolthub/dolt/go/libraries/utils/config"
"github.com/dolthub/dolt/go/libraries/utils/filesys"
eventsapi "github.com/dolthub/eventsapi_schema/dolt/services/eventsapi/v1alpha1"
gmssql "github.com/dolthub/go-mysql-server/sql"
"github.com/dolthub/vitess/go/mysql"
)
var _ driver.Connector = (*Connector)(nil)
const defaultDoltVersion = "0.40.17"
// newLocalContextForConnector exists to make Connector.Connect testable without needing
// to construct a fully initialized Dolt engine / session. Production code should leave this nil.
var newLocalContextForConnector func(se *engine.SqlEngine, ctx context.Context) (*gmssql.Context, error)
func newLocalContext(se *engine.SqlEngine, ctx context.Context) (*gmssql.Context, error) {
if newLocalContextForConnector != nil {
return newLocalContextForConnector(se, ctx)
}
baseSession := gmssql.NewBaseSession()
doltSession, err := se.NewDoltSession(ctx, baseSession)
if err != nil {
return nil, err
}
pl := se.GetUnderlyingEngine().ProcessList
gmsCtx := se.ContextFactory(ctx,
gmssql.WithSession(doltSession),
gmssql.WithProcessList(pl),
)
gmsCtx.Session.SetClient(gmssql.Client{User: "root", Address: "%", Capabilities: 0})
pl.AddConnection(gmsCtx.Session.ID(), "%")
pl.ConnectionReady(gmsCtx.Session)
return gmsCtx, nil
}
// Connector is a database/sql driver connector for embedded Dolt.
//
// Callers should construct it with NewConnector and then pass it to sql.OpenDB.
// The connector shares a single underlying embedded engine across connections,
// and creates a per-connection session context on each Connect.
type Connector struct {
cfg Config
driver *doltDriver
mu sync.Mutex
se *engine.SqlEngine
openCh chan struct{}
closed bool
}
// NewConnector constructs a new connector for embedded Dolt. The embedded engine
// is opened lazily on first Connect (and shared thereafter).
//
// If cfg.BackOff is non-nil, opening the engine will be retried for retryable
// open errors (e.g. lock contention) using that backoff, bounded by the Connect
// context.
func NewConnector(cfg Config) (*Connector, error) {
if cfg.Directory == "" {
return nil, errors.New("config.Directory is required")
}
if cfg.CommitName == "" {
return nil, errors.New("config.CommitName is required")
}
if cfg.CommitEmail == "" {
return nil, errors.New("config.CommitEmail is required")
}
if cfg.Version == "" {
cfg.Version = defaultDoltVersion
}
// Validate directory exists (ParseDSN does this, but callers can build Config directly).
var fs filesys.Filesys = filesys.LocalFS
exists, isDir := fs.Exists(cfg.Directory)
if !exists {
return nil, fmt.Errorf("'%s' does not exist", cfg.Directory)
} else if !isDir {
return nil, fmt.Errorf("%s: is a file. need to specify a directory", cfg.Directory)
}
return &Connector{
cfg: cfg,
driver: &doltDriver{},
}, nil
}
// Driver implements driver.Connector.
func (c *Connector) Driver() driver.Driver {
return c.driver
}
// Connect implements driver.Connector.
func (c *Connector) Connect(ctx context.Context) (driver.Conn, error) {
se, err := c.getOrOpenEngine(ctx)
if err != nil {
return nil, err
}
gmsCtx, err := newLocalContext(se, ctx)
if err != nil {
return nil, err
}
if c.cfg.Database != "" {
gmsCtx.SetCurrentDatabase(c.cfg.Database)
}
if c.cfg.ClientFoundRows {
client := gmsCtx.Client()
gmsCtx.SetClient(gmssql.Client{
User: client.User,
Address: client.Address,
Capabilities: client.Capabilities | mysql.CapabilityClientFoundRows,
})
}
return &DoltConn{
se: se,
gmsCtx: gmsCtx,
cfg: &c.cfg,
}, nil
}
// Close closes the shared embedded engine, if it has been opened.
// It is safe to call multiple times.
func (c *Connector) Close() error {
c.mu.Lock()
c.closed = true
se := c.se
c.se = nil
ch := c.openCh
c.openCh = nil
c.mu.Unlock()
// If an open is in progress, let it finish; Close doesn't block on it.
// (Connect calls will see closed=true and fail.)
if ch != nil {
select {
case <-ch:
default:
}
}
if se != nil {
return se.Close()
}
return nil
}
func (c *Connector) getOrOpenEngine(ctx context.Context) (*engine.SqlEngine, error) {
for {
c.mu.Lock()
if c.closed {
c.mu.Unlock()
return nil, errors.New("connector is closed")
}
if c.se != nil {
se := c.se
c.mu.Unlock()
return se, nil
}
if c.openCh != nil {
ch := c.openCh
c.mu.Unlock()
select {
case <-ch:
// Loop and re-check se/closed.
continue
case <-ctx.Done():
return nil, ctx.Err()
}
}
// Become the opener.
ch := make(chan struct{})
c.openCh = ch
c.mu.Unlock()
se, err := c.openEngineWithRetry(ctx)
c.mu.Lock()
// If we got a successful engine and connector isn't closed, store it.
if err == nil && !c.closed {
c.se = se
} else if se != nil {
// If open succeeded but connector is closed, immediately close it.
_ = se.Close()
}
c.openCh = nil
close(ch)
c.mu.Unlock()
if err != nil {
return nil, err
}
// Loop to return the stored engine (or error if closed).
}
}
func (c *Connector) openEngineWithRetry(ctx context.Context) (*engine.SqlEngine, error) {
// Dolt user config (commit metadata).
doltCfg := config.NewMapConfig(map[string]string{
config.UserNameKey: c.cfg.CommitName,
config.UserEmailKey: c.cfg.CommitEmail,
})
seCfg := &engine.SqlEngineConfig{
IsReadOnly: false,
ServerUser: "root",
Autocommit: true,
}
if seCfg.DBLoadParams == nil {
seCfg.DBLoadParams = make(map[string]interface{})
}
// The SqlEngine always owns the DoltDB instances it is
// operating against. We don't want two different sql.DB
// instances opened in the same process against the same
// directory sharing their DoltDB instances, and
// correspondingly the NomsBlockStore, journal, files they
// open.
seCfg.DBLoadParams[dbfactory.DisableSingletonCacheParam] = struct{}{}
// For deterministic retries on lock contention.
if c.cfg.BackOff != nil {
seCfg.DBLoadParams[dbfactory.FailOnJournalLockTimeoutParam] = struct{}{}
}
var fs filesys.Filesys = filesys.LocalFS
wd, err := fs.WithWorkingDir(c.cfg.Directory)
if err != nil {
return nil, err
}
open := func(openCtx context.Context) (*engine.SqlEngine, error) {
return openSqlEngine(openCtx, doltCfg, wd, c.cfg.Directory, c.cfg.Version, seCfg)
}
if c.cfg.BackOff == nil {
return open(ctx)
}
// BackOff is stateful; reset before use.
c.cfg.BackOff.Reset()
bo := backoff.WithContext(c.cfg.BackOff, ctx)
var lastErr error
var se *engine.SqlEngine
op := func() error {
s, err := open(ctx)
if err == nil {
se = s
return nil
}
lastErr = err
if isRetryableOpenErr(err) {
return err
}
return backoff.Permanent(err)
}
if err := backoff.Retry(op, bo); err != nil {
if lastErr != nil {
return nil, lastErr
}
return nil, err
}
return se, nil
}
// Two tracking vars to ensure we only emit metrics once per process, and that we don't emit if the env var is set.
// These are atomic bools to avoid races in test, or when there are multiple connectors in the same process.
var metricsDisabled = &atomic.Bool{}
var metricsSent = &atomic.Bool{}
const metricsDisabledEnvKey = "DOLT_METRICS_DISABLED"
func init() {
if _, disabled := os.LookupEnv(metricsDisabledEnvKey); disabled {
metricsDisabled.Store(true)
}
}
// emitUsageEvent emits a usage event to the event server at most once every 24 hours.
func emitUsageEvent(ctx context.Context, mrEnv *env.MultiRepoEnv) {
defer func() {
recover()
}()
if metricsDisabled.Load() || !metricsSent.CompareAndSwap(false, true) {
return
}
var dEnv *env.DoltEnv
mrEnv.Iter(func(name string, d *env.DoltEnv) (stop bool, err error) {
dEnv = d
return true, nil
})
// no dolt db created yet, which means we can't create a GRPC dialer
if dEnv == nil {
return
}
dir, err := dEnv.TempTableFilesDir()
if err != nil {
return
}
mtimeFile, tooSoon := tooSoonToEmitMetrics(dir)
if tooSoon {
return
}
emitter, closeFunc, err := commands.GRPCEmitterForConfig(dEnv, events.WithApplication(eventsapi.AppID_APP_DOLT_EMBEDDED))
if err != nil {
return
}
defer closeFunc()
evt := events.NewEvent(eventsapi.ClientEventType_SQL_SERVER)
evtCollector := events.NewCollector(doltversion.Version, emitter)
evtCollector.CloseEventAndAdd(evt)
clientEvents := evtCollector.Close()
_ = emitter.LogEvents(ctx, doltversion.Version, clientEvents)
// update the last modified time
_ = os.Chtimes(mtimeFile, time.Now(), time.Now())
}
const metricsInterval = time.Hour * 24
// tooSoonToEmitMetrics checks if it's been less than 24 hours since the last metrics event was emitted, by checking
// the mod time of a file in the given directory.
// Returns the path to the file used for tracking mod time, and whether it's too soon to emit metrics.
func tooSoonToEmitMetrics(dir string) (string, bool) {
mtimeFile := filepath.Join(dir, "dolt_embedded_metrics")
f, err := os.OpenFile(mtimeFile, os.O_CREATE, 0644)
if err != nil {
return "", true
}
info, err := f.Stat()
if err != nil {
return "", true
}
if time.Now().Sub(info.ModTime()) < metricsInterval {
return "", true
}
return mtimeFile, false
}