Skip to content

Commit

Permalink
Catch and avoid DB errors on witness
Browse files Browse the repository at this point in the history
  • Loading branch information
AlCutter committed Feb 11, 2022
1 parent d7d0db8 commit bbab341
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 3 deletions.
1 change: 1 addition & 0 deletions internal/witness/cmd/feeder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ func (l *ctLog) feedOnce(ctx context.Context, w *wh.Witness) error {
}
if errors.Is(err, wh.ErrSTHTooOld) {
glog.Infof("STH mismatch at log size %d for %s", wSize, l.name)
glog.Infof("%s", wsthRaw)
}
// Parse the STH it returns.
var wsthJSON ct.GetSTHResponse
Expand Down
4 changes: 4 additions & 0 deletions internal/witness/cmd/witness/impl/witness.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ func Main(ctx context.Context, opts ServerOpts) error {
if err != nil {
return fmt.Errorf("failed to connect to DB: %w", err)
}

// Avoid multiple writes colliding and resulting in a "database locked" error.
db.SetMaxOpenConns(1)

// Load log configuration into the map.
logMap, err := buildLogMap(opts.Config)
if err != nil {
Expand Down
8 changes: 5 additions & 3 deletions internal/witness/cmd/witness/internal/witness/witness.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ func (w *Witness) Update(ctx context.Context, logID string, nextRaw []byte, proo
if err != nil {
return nil, fmt.Errorf("couldn't create db tx: %v", err)
}
defer tx.Rollback()

// Get the latest STH (if one exists).
prevRaw, err := w.getLatestSTH(tx.QueryRow, logID)
if err != nil {
Expand Down Expand Up @@ -264,8 +266,8 @@ func (w *Witness) getLatestSTH(queryRow func(query string, args ...interface{})

// setSTH writes the STH to the database for a given log.
func (w *Witness) setSTH(tx *sql.Tx, logID string, sth []byte) error {
tx.Exec(`INSERT INTO sths (logID, sth) VALUES (?, ?)
ON CONFLICT(logID) DO UPDATE SET sth=excluded.sth`,
logID, sth)
if _, err := tx.Exec(`INSERT OR REPLACE INTO sths (logID, sth) VALUES (?, ?)`, logID, sth); err != nil {
return fmt.Errorf("failed to update STH; %v", err)
}
return tx.Commit()
}

0 comments on commit bbab341

Please sign in to comment.