Skip to content

Commit

Permalink
/go/libraries/doltcore/sql/dsess: parallelize sql.NewDatabase work
Browse files Browse the repository at this point in the history
  • Loading branch information
coffeegoddd committed Jan 13, 2025
1 parent 581c2f9 commit 3a13213
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 49 deletions.
67 changes: 43 additions & 24 deletions go/libraries/doltcore/sqle/dsess/autoincrement_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/dolthub/go-mysql-server/sql"
gmstypes "github.com/dolthub/go-mysql-server/sql/types"
"golang.org/x/sync/errgroup"

"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
"github.com/dolthub/dolt/go/libraries/doltcore/doltdb/durable"
Expand Down Expand Up @@ -398,34 +399,52 @@ func (a *AutoIncrementTracker) AcquireTableLock(ctx *sql.Context, tableName stri
}

func (a *AutoIncrementTracker) InitWithRoots(ctx context.Context, roots ...doltdb.Rootish) error {
for _, root := range roots {
r, err := root.ResolveRootValue(ctx)
if err != nil {
return err
}
rootsChan := make(chan doltdb.Rootish, len(roots))
eg, egCtx := errgroup.WithContext(ctx)
eg.Go(func() error {
for {
select {
case <-egCtx.Done():
return egCtx.Err()
case root, ok := <-rootsChan:
if !ok {
return nil
}
r, rerr := root.ResolveRootValue(egCtx)
if rerr != nil {
return rerr
}

err = r.IterTables(ctx, func(tableName doltdb.TableName, table *doltdb.Table, sch schema.Schema) (bool, error) {
if !schema.HasAutoIncrement(sch) {
return false, nil
}
rerr = r.IterTables(egCtx, func(tableName doltdb.TableName, table *doltdb.Table, sch schema.Schema) (bool, error) {
if !schema.HasAutoIncrement(sch) {
return false, nil
}

seq, err := table.GetAutoIncrementValue(ctx)
if err != nil {
return true, err
}
seq, rerr := table.GetAutoIncrementValue(egCtx)
if rerr != nil {
return true, rerr
}

tableNameStr := tableName.ToLower().Name
if oldValue, loaded := a.sequences.LoadOrStore(tableNameStr, seq); loaded && seq > oldValue.(uint64) {
a.sequences.Store(tableNameStr, seq)
}
tableNameStr := tableName.ToLower().Name
if oldValue, loaded := a.sequences.LoadOrStore(tableNameStr, seq); loaded && seq > oldValue.(uint64) {
a.sequences.Store(tableNameStr, seq)
}

return false, nil
})
return false, nil
})

if err != nil {
return err
if rerr != nil {
return rerr
}
}
}
}

return nil
})
eg.Go(func() error {
defer close(rootsChan)
for _, root := range roots {
rootsChan <- root
}
return nil
})
return eg.Wait()
}
77 changes: 52 additions & 25 deletions go/libraries/doltcore/sqle/dsess/globalstate.go
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"sync"

"github.com/dolthub/go-mysql-server/sql"
"golang.org/x/sync/errgroup"

"github.com/dolthub/dolt/go/libraries/doltcore/doltdb"
"github.com/dolthub/dolt/go/libraries/doltcore/ref"
Expand All @@ -36,39 +37,65 @@ func NewGlobalStateStoreForDb(ctx context.Context, dbName string, db *doltdb.Dol
return GlobalStateImpl{}, err
}

rootRefsChan := make(chan ref.DoltRef, len(branches)+len(remotes))
rootRefs := make([]ref.DoltRef, 0, len(branches)+len(remotes))
rootRefs = append(rootRefs, branches...)
rootRefs = append(rootRefs, remotes...)

var roots []doltdb.Rootish
for _, b := range rootRefs {
switch b.GetType() {
case ref.BranchRefType:
wsRef, err := ref.WorkingSetRefForHead(b)
if err != nil {
return GlobalStateImpl{}, err
}

ws, err := db.ResolveWorkingSet(ctx, wsRef)
if err == doltdb.ErrWorkingSetNotFound {
// use the branch head if there isn't a working set for it
cm, err := db.ResolveCommitRef(ctx, b)
if err != nil {
return GlobalStateImpl{}, err
eg, egCtx := errgroup.WithContext(ctx)
eg.Go(func() error {
for {
select {
case <-egCtx.Done():
return egCtx.Err()
case r, ok := <-rootRefsChan:
if !ok {
return nil
}
roots = append(roots, cm)
} else if err != nil {
return GlobalStateImpl{}, err
} else {
roots = append(roots, ws)
}
case ref.RemoteRefType:
cm, err := db.ResolveCommitRef(ctx, b)
if err != nil {
return GlobalStateImpl{}, err
switch r.GetType() {
case ref.BranchRefType:
wsRef, rerr := ref.WorkingSetRefForHead(r)
if rerr != nil {
return rerr
}

ws, rerr := db.ResolveWorkingSet(egCtx, wsRef)
if rerr == doltdb.ErrWorkingSetNotFound {
// use the branch head if there isn't a working set for it
cm, rerr := db.ResolveCommitRef(egCtx, r)
if rerr != nil {
return rerr
}
roots = append(roots, cm)
} else if rerr != nil {
return rerr
} else {
roots = append(roots, ws)
}
case ref.RemoteRefType:
cm, rerr := db.ResolveCommitRef(egCtx, r)
if rerr != nil {
return rerr
}
roots = append(roots, cm)
}

}
roots = append(roots, cm)
}
})

eg.Go(func() error {
defer close(rootRefsChan)
for _, r := range rootRefs {
rootRefsChan <- r
}
return nil
})

err = eg.Wait()
if err != nil {
return GlobalStateImpl{}, err
}

tracker, err := NewAutoIncrementTracker(ctx, dbName, roots...)
Expand Down

0 comments on commit 3a13213

Please sign in to comment.