Skip to content

Commit

Permalink
fix preexisting drop datebase issue
Browse files Browse the repository at this point in the history
  • Loading branch information
max-hoffman committed Dec 12, 2024
1 parent d550ebb commit 5ae8af3
Show file tree
Hide file tree
Showing 5 changed files with 467 additions and 456 deletions.
61 changes: 34 additions & 27 deletions go/libraries/doltcore/sqle/statsnoms/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@ func (n *NomsStatsDatabase) LoadBranchStats(ctx *sql.Context, branch string) err
return err
} else if !ok {
ctx.GetLogger().Debugf("statistics load: detected schema change incompatility, purging %s/%s", branch, n.sourceDb.Name())
//log.Printf("statistics load: detected schema change incompatility, purging %s/%s", branch, n.sourceDb.Name())
if err := n.DeleteBranchStats(ctx, branch, true); err != nil {
return err
}
Expand Down Expand Up @@ -463,37 +462,45 @@ func (n *NomsStatsDatabase) GetSchemaHash(ctx context.Context, branch, tableName
func (n *NomsStatsDatabase) SetSchemaHash(ctx context.Context, branch, tableName string, h hash.Hash) error {
n.mu.Lock()
defer n.mu.Unlock()
branchIdx := -1
for i, b := range n.branches {
if strings.EqualFold(branch, b) {
n.schemaHashes[i][tableName] = h
tagRef := ref.NewTagRef(branch + "/" + tableName)

if _, ok, err := n.destDb.DbData().Ddb.HasTag(ctx, tagRef.GetPath()); ok {
if err := n.destDb.DbData().Ddb.DeleteTag(ctx, tagRef); err != nil {
return err
}
} else if err != nil {
return err
}
branchIdx = i
break
}
}
if branchIdx < 0 {
branchIdx = len(n.branches)
if err := n.trackBranch(ctx, branch); err != nil {
return err
}
}

branchSpec, err := doltdb.NewCommitSpec(branch)
if err != nil {
return err
}
n.schemaHashes[branchIdx][tableName] = h
tagRef := ref.NewTagRef(branch + "/" + tableName)
if _, ok, err := n.destDb.DbData().Ddb.HasTag(ctx, tagRef.GetPath()); ok {
if err := n.destDb.DbData().Ddb.DeleteTag(ctx, tagRef); err != nil {
return err
}
} else if err != nil {
return err
}

headRef, err := n.destDb.DbData().Rsr.CWBHeadRef()
if err != nil {
return err
}
branchSpec, err := doltdb.NewCommitSpec(branch)
if err != nil {
return err
}

optCmt, err := n.destDb.DbData().Ddb.Resolve(ctx, branchSpec, headRef)
if err != nil {
return err
}
headRef, err := n.destDb.DbData().Rsr.CWBHeadRef()
if err != nil {
return err
}

props := datas.NewTagMeta("stats", "[email protected]", h.String())
return n.destDb.DbData().Ddb.NewTagAtCommit(ctx, tagRef, optCmt.Commit, props)
}
optCmt, err := n.destDb.DbData().Ddb.Resolve(ctx, branchSpec, headRef)
if err != nil {
return err
}
return fmt.Errorf("failed to update schema hash tag")

props := datas.NewTagMeta("stats", "[email protected]", h.String())
return n.destDb.DbData().Ddb.NewTagAtCommit(ctx, tagRef, optCmt.Commit, props)
}
2 changes: 1 addition & 1 deletion go/libraries/doltcore/sqle/statspro/analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ func (p *Provider) RefreshTableStatsWithBranch(ctx *sql.Context, table sql.Table

if oldSchHash, err := statDb.GetSchemaHash(ctx, branch, tableName); oldSchHash.IsEmpty() {
if err := statDb.SetSchemaHash(ctx, branch, tableName, schHash); err != nil {
return err
return fmt.Errorf("set schema hash error: %w", err)
}
} else if oldSchHash != schHash {
ctx.GetLogger().Debugf("statistics refresh: detected table schema change: %s,%s/%s", dbName, table, branch)
Expand Down
27 changes: 14 additions & 13 deletions go/libraries/doltcore/sqle/statspro/configure.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package statspro

import (
"context"
"fmt"
"strings"
"time"

Expand Down Expand Up @@ -67,16 +68,16 @@ func (p *Provider) Configure(ctx context.Context, ctxFactory func(ctx context.Co
// copy closure variables
db := db
eg.Go(func() (err error) {
//defer func() {
// if r := recover(); r != nil {
// if str, ok := r.(fmt.Stringer); ok {
// err = fmt.Errorf("%w: %s", ErrFailedToLoad, str.String())
// } else {
// err = fmt.Errorf("%w: %v", ErrFailedToLoad, r)
// }
// return
// }
//}()
defer func() {
if r := recover(); r != nil {
if str, ok := r.(fmt.Stringer); ok {
err = fmt.Errorf("%w: %s", ErrFailedToLoad, str.String())
} else {
err = fmt.Errorf("%w: %v", ErrFailedToLoad, r)
}
return
}
}()

fs, err := p.pro.FileSystemForDatabase(db.Name())
if err != nil {
Expand Down Expand Up @@ -135,19 +136,19 @@ func (p *Provider) Load(ctx *sql.Context, fs filesys.Filesys, db dsess.SqlDataba
// |statPath| is either file://./stat or mem://stat
statsDb, err := p.sf.Init(ctx, db, p.pro, fs, env.GetCurrentUserHomeDir)
if err != nil {
ctx.GetLogger().Errorf("initialize stats failure: %s; %s\n", err.Error(), helpMsg)
ctx.GetLogger().Errorf("initialize stats failure for %s: %s; %s\n", db.Name(), err.Error(), helpMsg)
return
}

for _, branch := range branches {
if err = statsDb.LoadBranchStats(ctx, branch); err != nil {
// if branch name is invalid, continue loading rest
// TODO: differentiate bad branch name from other errors
ctx.GetLogger().Errorf("load stats init failure: %s; %s\n", err.Error(), helpMsg)
ctx.GetLogger().Errorf("load stats init failure for %s: %s; %s\n", db.Name(), err.Error(), helpMsg)
continue
}
if err := statsDb.Flush(ctx, branch); err != nil {
ctx.GetLogger().Errorf("load stats flush failure: %s; %s\n", err.Error(), helpMsg)
ctx.GetLogger().Errorf("load stats flush failure for %s: %s; %s\n", db.Name(), err.Error(), helpMsg)
continue
}
}
Expand Down
3 changes: 3 additions & 0 deletions go/libraries/doltcore/sqle/statspro/initdbhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ func NewStatsInitDatabaseHook(
return nil
}
statsProv.setStatDb(dbName, statsDb)
} else {
ctx.GetLogger().Debugf("statistics init error: preexisting stats db: %s", dbName)
}
ctx.GetLogger().Debugf("statistics refresh: initialize %s", name)
return statsProv.InitAutoRefresh(ctxFactory, name, bThreads)
Expand All @@ -62,6 +64,7 @@ func NewStatsDropDatabaseHook(statsProv *Provider) sqle.DropDatabaseHook {
if err := db.Close(); err != nil {
ctx.GetLogger().Debugf("failed to close stats database: %s", err)
}
delete(statsProv.statDbs, name)
}
}
}
Loading

0 comments on commit 5ae8af3

Please sign in to comment.