Skip to content

Commit a00f1b8

Browse files
committed
fix(base.SaveDataset): move logbook write operation back down from lib
This fixes a few tests that were failing thanks to one-off save implmentations that no longer received logbook updates with logbook write operations moved up into lib. This was pretty tough to track down Interaction between subsystems on the save path has become hard to predict. We have code that implictly require base.SaveDataset manages updates to history changes. It's test code, but the problem is the implicit-ness, and brittleness of the codebase I think th eway forward on both is to use the event bus, and focus on wiring the bus up properly in all circumstances. If logbook updates from save events happened via a blocking event bus subscription we could avoid this brittleness As-is we need to plumb a *run.State argument down into base.SaveDataset just to pass along to logbook's write.
1 parent 2ab44d0 commit a00f1b8

File tree

11 files changed

+38
-23
lines changed

11 files changed

+38
-23
lines changed

base/dataset_prepare.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func PrepareSaveRef(
9898
}
9999

100100
// we have a valid previous reference & an initID, return!
101-
log.Debugf("PrepareSaveRef found previous initID=%q", ref.InitID)
101+
log.Debugw("PrepareSaveRef found previous initID", "initID", ref.InitID, "path", ref.Path)
102102
return ref, false, nil
103103
}
104104

base/save.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"github.com/qri-io/qri/dsref"
1111
"github.com/qri-io/qri/profile"
1212
"github.com/qri-io/qri/repo"
13+
"github.com/qri-io/qri/transform/run"
1314
)
1415

1516
// SaveSwitches is an alias for the switches that control how saves happen
@@ -19,7 +20,16 @@ type SaveSwitches = dsfs.SaveSwitches
1920
var ErrNameTaken = fmt.Errorf("name already in use")
2021

2122
// SaveDataset saves a version of the dataset for the given initID at the current path
22-
func SaveDataset(ctx context.Context, r repo.Repo, writeDest qfs.Filesystem, initID, prevPath string, changes *dataset.Dataset, sw SaveSwitches) (ds *dataset.Dataset, err error) {
23+
func SaveDataset(
24+
ctx context.Context,
25+
r repo.Repo,
26+
writeDest qfs.Filesystem,
27+
initID string,
28+
prevPath string,
29+
changes *dataset.Dataset,
30+
runState *run.State,
31+
sw SaveSwitches,
32+
) (ds *dataset.Dataset, err error) {
2333
log.Debugf("SaveDataset initID=%q prevPath=%q", initID, prevPath)
2434
var pro *profile.Profile
2535
if pro, err = r.Profile(ctx); err != nil {
@@ -103,6 +113,10 @@ func SaveDataset(ctx context.Context, r repo.Repo, writeDest qfs.Filesystem, ini
103113
return nil, err
104114
}
105115

116+
// Write the save to logbook
117+
if err = r.Logbook().WriteVersionSave(ctx, initID, ds, runState); err != nil {
118+
return nil, err
119+
}
106120
return ds, nil
107121
}
108122

base/test_runner_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func (run *TestRunner) saveDataset(ds *dataset.Dataset, sw SaveSwitches) (dsref.
6262
return dsref.Ref{}, err
6363
}
6464

65-
ds, err := SaveDataset(run.Context, run.Repo, run.Repo.Filesystem().DefaultWriteFS(), ref.InitID, ref.Path, ds, sw)
65+
ds, err := SaveDataset(run.Context, run.Repo, run.Repo.Filesystem().DefaultWriteFS(), ref.InitID, ref.Path, ds, nil, sw)
6666
if err != nil {
6767
return dsref.Ref{}, err
6868
}

cmd/test_runner_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,7 @@ func (run *TestRunner) AddDatasetToRefstore(t *testing.T, refStr string, ds *dat
652652
// No existing commit
653653
emptyHeadRef := ""
654654

655-
if _, err = base.SaveDataset(ctx, r, r.Filesystem().DefaultWriteFS(), initID, emptyHeadRef, ds, base.SaveSwitches{}); err != nil {
655+
if _, err = base.SaveDataset(ctx, r, r.Filesystem().DefaultWriteFS(), initID, emptyHeadRef, ds, nil, base.SaveSwitches{}); err != nil {
656656
t.Fatal(err)
657657
}
658658

lib/datasets.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ func (p *SaveParams) SetNonZeroDefaults() {
737737

738738
// Save adds a history entry, updating a dataset
739739
func (m *DatasetMethods) Save(ctx context.Context, p *SaveParams) (*dataset.Dataset, error) {
740-
log.Debugf("DatasetMethods.Save p=%v", p)
740+
log.Debugw("DatasetMethods.Save", "params", p)
741741
res := &dataset.Dataset{}
742742

743743
if m.inst.http != nil {
@@ -938,7 +938,7 @@ func (m *DatasetMethods) Save(ctx context.Context, p *SaveParams) (*dataset.Data
938938
NewName: p.NewName,
939939
Drop: p.Drop,
940940
}
941-
savedDs, err := base.SaveDataset(ctx, m.inst.repo, writeDest, ref.InitID, ref.Path, ds, switches)
941+
savedDs, err := base.SaveDataset(ctx, m.inst.repo, writeDest, ref.InitID, ref.Path, ds, runState, switches)
942942
if err != nil {
943943
// datasets that are unchanged & have a runState record a record of no-changes
944944
// to logbook
@@ -955,11 +955,6 @@ func (m *DatasetMethods) Save(ctx context.Context, p *SaveParams) (*dataset.Data
955955
return nil, err
956956
}
957957

958-
// Write the save to logbook
959-
if err = m.inst.logbook.WriteVersionSave(ctx, ref.InitID, savedDs, runState); err != nil {
960-
return nil, err
961-
}
962-
963958
success = true
964959

965960
// TODO (b5) - this should be integrated into base.SaveDataset

lib/datasets_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ func TestDatasetRequestsForceSave(t *testing.T) {
165165
Force: true,
166166
})
167167
if err != nil {
168-
t.Errorf("expected empty save with flag to not error. got: %s", err.Error())
168+
t.Errorf("expected empty save with force flag to not error. got: %q", err.Error())
169169
}
170170
}
171171

lib/lib.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -663,10 +663,11 @@ func NewInstanceFromConfigAndNodeAndBus(ctx context.Context, cfg *config.Config,
663663
cancel: cancel,
664664
doneCh: make(chan struct{}),
665665

666-
cfg: cfg,
667-
node: node,
668-
dscache: dc,
669-
logbook: r.Logbook(),
666+
cfg: cfg,
667+
node: node,
668+
dscache: dc,
669+
logbook: r.Logbook(),
670+
transform: transform.NewService(ctx),
670671
}
671672

672673
inst.stats = stats.New(nil)

lib/lib_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ func saveDataset(ctx context.Context, r repo.Repo, ds *dataset.Dataset, sw base.
222222
if err != nil {
223223
return dsref.Ref{}, err
224224
}
225-
res, err := base.SaveDataset(ctx, r, r.Filesystem().DefaultWriteFS(), ref.InitID, ref.Path, ds, sw)
225+
res, err := base.SaveDataset(ctx, r, r.Filesystem().DefaultWriteFS(), ref.InitID, ref.Path, ds, nil, sw)
226226
if err != nil {
227227
return dsref.Ref{}, err
228228
}

lib/transform_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func TestApplyTransform(t *testing.T) {
1818
BodyPath: "testdata/cities_2/body.csv",
1919
})
2020
if err != nil {
21-
t.Error(err)
21+
t.Fatal(err)
2222
}
2323

2424
// Apply a transformation

logbook/logbook.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ func (book *Book) WriteVersionSave(ctx context.Context, initID string, ds *datas
513513
return ErrNoLogbook
514514
}
515515

516-
log.Debugf("WriteVersionSave: %s", initID)
516+
log.Debugw("WriteVersionSave", "initID", initID)
517517
branchLog, err := book.branchLog(ctx, initID)
518518
if err != nil {
519519
return err
@@ -610,9 +610,12 @@ func (book *Book) appendTransformRun(blog *BranchLog, rs *run.State) int {
610610
Ref: rs.ID,
611611
Name: fmt.Sprintf("%d", rs.Number),
612612

613-
Timestamp: rs.StartTime.UnixNano(),
614-
Size: int64(rs.Duration),
615-
Note: string(rs.Status),
613+
Size: int64(rs.Duration),
614+
Note: string(rs.Status),
615+
}
616+
617+
if rs.StartTime != nil {
618+
op.Timestamp = rs.StartTime.UnixNano()
616619
}
617620

618621
blog.Append(op)
@@ -871,10 +874,12 @@ func (book *Book) ResolveRef(ctx context.Context, ref *dsref.Ref) (string, error
871874

872875
var branchLog *BranchLog
873876
if ref.Path == "" {
877+
log.Debugw("finding branch log", "initID", initID)
874878
branchLog, err = book.branchLog(ctx, initID)
875879
if err != nil {
876880
return "", err
877881
}
882+
log.Debugw("found branch log", "initID", initID, "size", branchLog.Size(), "latestSavePath", book.latestSavePath(branchLog.l))
878883
ref.Path = book.latestSavePath(branchLog.l)
879884
}
880885

0 commit comments

Comments
 (0)