Skip to content

Commit 6a75a52

Browse files
committed
/go/libraries/doltcore/sqle/{dsess,globalstate,writer}: make ai tracker load async
1 parent 1681cbd commit 6a75a52

File tree

4 files changed

+74
-9
lines changed

4 files changed

+74
-9
lines changed

Diff for: go/libraries/doltcore/sqle/dsess/autoincrement_tracker.go

+64-5
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ type AutoIncrementTracker struct {
4848
sequences *sync.Map // map[string]uint64
4949
mm *mutexmap.MutexMap
5050
lockMode LockMode
51+
init sync.Once
52+
wg *sync.WaitGroup
53+
initErr error
5154
}
5255

5356
var _ globalstate.AutoIncrementTracker = &AutoIncrementTracker{}
@@ -61,8 +64,10 @@ func NewAutoIncrementTracker(ctx context.Context, dbName string, roots ...doltdb
6164
dbName: dbName,
6265
sequences: &sync.Map{},
6366
mm: mutexmap.NewMutexMap(),
67+
wg: &sync.WaitGroup{},
68+
init: sync.Once{},
6469
}
65-
ait.InitWithRoots(ctx, roots...)
70+
ait.runInitWithRootsAsync(ctx, roots...)
6671
return &ait, nil
6772
}
6873

@@ -76,13 +81,22 @@ func loadAutoIncValue(sequences *sync.Map, tableName string) uint64 {
7681
}
7782

7883
// Current returns the next value to be generated in the auto increment sequence for the table named
79-
func (a *AutoIncrementTracker) Current(tableName string) uint64 {
80-
return loadAutoIncValue(a.sequences, tableName)
84+
func (a *AutoIncrementTracker) Current(tableName string) (uint64, error) {
85+
err := a.waitForInit()
86+
if err != nil {
87+
return 0, err
88+
}
89+
return loadAutoIncValue(a.sequences, tableName), nil
8190
}
8291

8392
// Next returns the next auto increment value for the table named using the provided value from an insert (which may
8493
// be null or 0, in which case it will be generated from the sequence).
8594
func (a *AutoIncrementTracker) Next(tbl string, insertVal interface{}) (uint64, error) {
95+
err := a.waitForInit()
96+
if err != nil {
97+
return 0, err
98+
}
99+
86100
tbl = strings.ToLower(tbl)
87101

88102
given, err := CoerceAutoIncrementValue(insertVal)
@@ -113,6 +127,10 @@ func (a *AutoIncrementTracker) Next(tbl string, insertVal interface{}) (uint64,
113127
}
114128

115129
func (a *AutoIncrementTracker) CoerceAutoIncrementValue(val interface{}) (uint64, error) {
130+
err := a.waitForInit()
131+
if err != nil {
132+
return 0, err
133+
}
116134
return CoerceAutoIncrementValue(val)
117135
}
118136

@@ -140,6 +158,11 @@ func CoerceAutoIncrementValue(val interface{}) (uint64, error) {
140158
// table. Otherwise, the update is silently disregarded. So far this matches the MySQL behavior, but Dolt uses the
141159
// maximum value for this table across all branches.
142160
func (a *AutoIncrementTracker) Set(ctx *sql.Context, tableName string, table *doltdb.Table, ws ref.WorkingSetRef, newAutoIncVal uint64) (*doltdb.Table, error) {
161+
err := a.waitForInit()
162+
if err != nil {
163+
return nil, err
164+
}
165+
143166
tableName = strings.ToLower(tableName)
144167

145168
release := a.mm.Lock(tableName)
@@ -338,16 +361,27 @@ func getMaxIndexValue(ctx context.Context, indexData durable.Index) (uint64, err
338361
}
339362

340363
// AddNewTable initializes a new table with an auto increment column to the tracker, as necessary
341-
func (a *AutoIncrementTracker) AddNewTable(tableName string) {
364+
func (a *AutoIncrementTracker) AddNewTable(tableName string) error {
365+
err := a.waitForInit()
366+
if err != nil {
367+
return err
368+
}
369+
342370
tableName = strings.ToLower(tableName)
343371
// only initialize the sequence for this table if no other branch has such a table
344372
a.sequences.LoadOrStore(tableName, uint64(1))
373+
return nil
345374
}
346375

347376
// DropTable drops the table with the name given.
348377
// To establish the new auto increment value, callers must also pass all other working sets in scope that may include
349378
// a table with the same name, omitting the working set that just deleted the table named.
350379
func (a *AutoIncrementTracker) DropTable(ctx *sql.Context, tableName string, wses ...*doltdb.WorkingSet) error {
380+
err := a.waitForInit()
381+
if err != nil {
382+
return err
383+
}
384+
351385
tableName = strings.ToLower(tableName)
352386

353387
release := a.mm.Lock(tableName)
@@ -389,6 +423,11 @@ func (a *AutoIncrementTracker) DropTable(ctx *sql.Context, tableName string, wse
389423
}
390424

391425
func (a *AutoIncrementTracker) AcquireTableLock(ctx *sql.Context, tableName string) (func(), error) {
426+
err := a.waitForInit()
427+
if err != nil {
428+
return nil, err
429+
}
430+
392431
_, i, _ := sql.SystemVariables.GetGlobal("innodb_autoinc_lock_mode")
393432
lockMode := LockMode(i.(int64))
394433
if lockMode == LockMode_Interleaved {
@@ -398,7 +437,22 @@ func (a *AutoIncrementTracker) AcquireTableLock(ctx *sql.Context, tableName stri
398437
return a.mm.Lock(tableName), nil
399438
}
400439

401-
func (a *AutoIncrementTracker) InitWithRoots(ctx context.Context, roots ...doltdb.Rootish) error {
440+
func (a *AutoIncrementTracker) waitForInit() error {
441+
a.wg.Wait()
442+
return a.initErr
443+
}
444+
445+
func (a *AutoIncrementTracker) runInitWithRootsAsync(ctx context.Context, roots ...doltdb.Rootish) {
446+
a.init.Do(func() {
447+
a.wg.Add(1)
448+
go func() {
449+
defer a.wg.Done()
450+
a.initErr = a.initWithRoots(ctx, roots...)
451+
}()
452+
})
453+
}
454+
455+
func (a *AutoIncrementTracker) initWithRoots(ctx context.Context, roots ...doltdb.Rootish) error {
402456
eg, egCtx := errgroup.WithContext(ctx)
403457
eg.SetLimit(128)
404458

@@ -435,3 +489,8 @@ func (a *AutoIncrementTracker) InitWithRoots(ctx context.Context, roots ...doltd
435489

436490
return eg.Wait()
437491
}
492+
493+
func (a *AutoIncrementTracker) InitWithRoots(ctx context.Context, roots ...doltdb.Rootish) error {
494+
a.runInitWithRootsAsync(ctx, roots...)
495+
return a.waitForInit()
496+
}

Diff for: go/libraries/doltcore/sqle/globalstate/auto_increment_tracker.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ import (
2727
// interface here because implementations need to reach into session state, requiring a dependency on this package.
2828
type AutoIncrementTracker interface {
2929
// Current returns the current auto increment value for the given table.
30-
Current(tableName string) uint64
30+
Current(tableName string) (uint64, error)
3131
// Next returns the next auto increment value for the given table, and increments the current value.
3232
Next(tbl string, insertVal interface{}) (uint64, error)
3333
// AddNewTable adds a new table to the tracker, initializing the auto increment value to 1.
34-
AddNewTable(tableName string)
34+
AddNewTable(tableName string) error
3535
// DropTable removes a table from the tracker.
3636
DropTable(ctx *sql.Context, tableName string, wses ...*doltdb.WorkingSet) error
3737
// CoerceAutoIncrementValue coerces the given value to a uint64, returning an error if it can't be done.

Diff for: go/libraries/doltcore/sqle/writer/noms_write_session.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,10 @@ func (s *nomsWriteSession) flush(ctx *sql.Context) (*doltdb.WorkingSet, error) {
179179
// Update the auto increment value for the table if a tracker was provided
180180
// TODO: the table probably needs an autoincrement tracker no matter what
181181
if schema.HasAutoIncrement(ed.Schema()) {
182-
v := s.aiTracker.Current(name)
182+
v, err := s.aiTracker.Current(name)
183+
if err != nil {
184+
return err
185+
}
183186
tbl, err = tbl.SetAutoIncrementValue(ctx, v)
184187
if err != nil {
185188
return err

Diff for: go/libraries/doltcore/sqle/writer/prolly_write_session.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,10 @@ func (s *prollyWriteSession) flush(ctx *sql.Context, autoIncSet bool, manualAuto
165165
// override was specified (e.g. if the next value was set explicitly)
166166
if schema.HasAutoIncrement(wr.sch) {
167167
// TODO: need schema name for auto increment
168-
autoIncVal := s.aiTracker.Current(name.Name)
168+
autoIncVal, err := s.aiTracker.Current(name.Name)
169+
if err != nil {
170+
return err
171+
}
169172
override, hasManuallySetAi := manualAutoIncrementsSettings[name.Name]
170173
if hasManuallySetAi {
171174
autoIncVal = override

0 commit comments

Comments
 (0)