Skip to content

Commit 79d5544

Browse files
authored
Add some debug logs and better error message to c1file open/init functions. (#697)
Also check errors when closing files in tests.
1 parent ec4b0cf commit 79d5544

File tree

2 files changed

+46
-15
lines changed

2 files changed

+46
-15
lines changed

pkg/dotc1z/c1file.go

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,12 @@ func NewC1File(ctx context.Context, dbFilePath string, opts ...C1FOption) (*C1Fi
114114

115115
rawDB, err := sql.Open("sqlite", dbFilePath)
116116
if err != nil {
117-
return nil, err
117+
return nil, fmt.Errorf("new-c1-file: error opening raw db: %w", err)
118118
}
119+
l := ctxzap.Extract(ctx)
120+
l.Debug("new-c1-file: opened raw db",
121+
zap.String("db_file_path", dbFilePath),
122+
)
119123

120124
// Limit to a single connection so idle pool connections don't hold WAL
121125
// read locks that prevent PRAGMA wal_checkpoint(TRUNCATE) from completing
@@ -147,7 +151,7 @@ func NewC1File(ctx context.Context, dbFilePath string, opts ...C1FOption) (*C1Fi
147151

148152
err = c1File.init(ctx)
149153
if err != nil {
150-
return nil, err
154+
return nil, fmt.Errorf("new-c1-file: error initializing c1file: %w", err)
151155
}
152156

153157
return c1File, nil
@@ -225,6 +229,11 @@ func NewC1ZFile(ctx context.Context, outputFilePath string, opts ...C1ZOption) (
225229
if err != nil {
226230
return nil, err
227231
}
232+
l := ctxzap.Extract(ctx)
233+
l.Debug("new-c1z-file: decompressed c1z",
234+
zap.String("db_file_path", dbFilePath),
235+
zap.String("output_file_path", outputFilePath),
236+
)
228237

229238
var c1fopts []C1FOption
230239
for _, pragma := range options.pragmas {
@@ -383,14 +392,18 @@ func (c *C1File) init(ctx context.Context) error {
383392

384393
err = c.InitTables(ctx)
385394
if err != nil {
395+
l.Error("c1file-init: error initializing tables", zap.Error(err))
386396
return err
387397
}
398+
l.Debug("c1file-init: initialized tables",
399+
zap.String("db_file_path", c.dbFilePath),
400+
)
388401

389402
// // Checkpoint the WAL after migrations. Migrations like backfillGrantExpansionColumn
390403
// // can update many rows, filling the WAL. Without a checkpoint, subsequent reads are
391404
// // slow because SQLite must scan the WAL hash table for every page read.
392405
if _, err = c.db.ExecContext(ctx, "PRAGMA wal_checkpoint(TRUNCATE)"); err != nil {
393-
l.Warn("WAL checkpoint after init failed", zap.Error(err))
406+
l.Warn("c1file-init: WAL checkpoint after init failed", zap.Error(err))
394407
}
395408

396409
if c.readOnly {
@@ -415,16 +428,17 @@ func (c *C1File) init(ctx context.Context) error {
415428
}
416429
}
417430
if !hasLockingPragma {
431+
l.Debug("c1file-init: setting locking mode to EXCLUSIVE", zap.String("db_file_path", c.dbFilePath))
418432
_, err = c.db.ExecContext(ctx, "PRAGMA main.locking_mode = EXCLUSIVE")
419433
if err != nil {
420-
return err
434+
return fmt.Errorf("c1file-init: error setting locking mode to EXCLUSIVE: %w", err)
421435
}
422436
}
423437

424438
for _, pragma := range c.pragmas {
425439
_, err := c.db.ExecContext(ctx, fmt.Sprintf("PRAGMA %s = %s", pragma.name, pragma.value))
426440
if err != nil {
427-
return err
441+
return fmt.Errorf("c1file-init: error setting pragma %s = %s: %w", pragma.name, pragma.value, err)
428442
}
429443
}
430444

@@ -440,16 +454,23 @@ func (c *C1File) InitTables(ctx context.Context) error {
440454
return err
441455
}
442456

457+
l := ctxzap.Extract(ctx).With(zap.String("db_file_path", c.dbFilePath))
443458
for _, t := range allTableDescriptors {
444459
query, args := t.Schema()
445460
_, err = c.db.ExecContext(ctx, fmt.Sprintf(query, args...))
446461
if err != nil {
447-
return err
462+
return fmt.Errorf("c1file-init-tables: error initializing table %s: %w", t.Name(), err)
448463
}
464+
l.Debug("c1file-init-tables: initialized table schema, running migrations",
465+
zap.String("table_name", t.Name()),
466+
)
449467
err = t.Migrations(ctx, c.db)
450468
if err != nil {
451-
return err
469+
return fmt.Errorf("c1file-init-tables: error running migration for table %s: %w", t.Name(), err)
452470
}
471+
l.Debug("c1file-init-tables: ran migrations for table",
472+
zap.String("table_name", t.Name()),
473+
)
453474
}
454475

455476
return nil

pkg/dotc1z/grants_test.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,12 @@ func TestGrantExpandableSurvivesCloseReopen(t *testing.T) {
148148
tmpFile, err := os.CreateTemp("", "test-expansion-reopen-*.c1z")
149149
require.NoError(t, err)
150150
tmpPath := tmpFile.Name()
151-
tmpFile.Close()
152-
defer os.Remove(tmpPath)
151+
err = tmpFile.Close()
152+
require.NoError(t, err)
153+
defer func() {
154+
err := os.Remove(tmpPath)
155+
require.NoError(t, err)
156+
}()
153157

154158
// Phase 1: Create store, write expandable grant, close.
155159
func() {
@@ -444,8 +448,12 @@ func TestBackfillMigration_OldSyncGetsExpansionColumn(t *testing.T) {
444448

445449
tmpFile, err := os.CreateTemp("", "test-backfill-*.c1z")
446450
require.NoError(t, err)
447-
defer os.Remove(tmpFile.Name())
448-
tmpFile.Close()
451+
defer func() {
452+
err := os.Remove(tmpFile.Name()) //nolint:gosec // G703 -- tmpFile.Name() is created by os.CreateTemp in this test.
453+
require.NoError(t, err)
454+
}()
455+
err = tmpFile.Close()
456+
require.NoError(t, err)
449457

450458
// Step 1: Create a c1z and write grants the "new" way (expansion column populated).
451459
c1f, err := NewC1ZFile(ctx, tmpFile.Name())
@@ -636,7 +644,8 @@ func setupTestC1Z(ctx context.Context, t *testing.T) (*C1File, string, func()) {
636644
t.Helper()
637645
tmpFile, err := os.CreateTemp("", "test-grants-*.c1z")
638646
require.NoError(t, err)
639-
tmpFile.Close()
647+
err = tmpFile.Close()
648+
require.NoError(t, err)
640649

641650
c1f, err := NewC1ZFile(ctx, tmpFile.Name())
642651
require.NoError(t, err)
@@ -657,9 +666,10 @@ func setupTestC1Z(ctx context.Context, t *testing.T) (*C1File, string, func()) {
657666
require.NoError(t, c1f.PutEntitlements(ctx, ent1, ent2))
658667

659668
cleanup := func() {
660-
_ = c1f.Close(ctx)
661-
// #nosec G703 -- tmpFile.Name() is created by os.CreateTemp in this test.
662-
os.Remove(tmpFile.Name())
669+
err := c1f.Close(ctx)
670+
require.NoError(t, err)
671+
err = os.Remove(tmpFile.Name()) //nolint:gosec // G703 -- tmpFile.Name() is created by os.CreateTemp in this test.
672+
require.NoError(t, err)
663673
}
664674
return c1f, syncID, cleanup
665675
}

0 commit comments

Comments
 (0)