diff --git a/internal/control/report_publish_removed_meanwhile_test.go b/internal/control/report_publish_removed_meanwhile_test.go new file mode 100644 index 00000000..1b523225 --- /dev/null +++ b/internal/control/report_publish_removed_meanwhile_test.go @@ -0,0 +1,150 @@ +package control_test + +import ( + "context" + "net/http" + "strings" + "sync/atomic" + "testing" + "time" + + "github.com/bestagentkits/sisf/internal/control" + "github.com/bestagentkits/sisf/pkg/core" + "github.com/bestagentkits/sisf/pkg/state" + "github.com/bestagentkits/sisf/pkg/state/sqlstore/sqlstoretest" +) + +// removedMeanwhile is a store on which another publisher writes the first +// document under prefix that this one is about to remove or write, between +// its read and its write. A removal then finds nothing, as a versioned +// delete does on PostgreSQL at read committed when the other transaction +// commits first; a write finds the document moved on. +type removedMeanwhile struct { + state.Store + prefix string + // removalsOnly races only a removal, leaving the writes under prefix + // that come before it alone. + removalsOnly bool + armed atomic.Bool +} + +// meanwhile arms a store over s that races the first write under prefix. +func meanwhile(s state.Store, prefix string, removalsOnly bool) *removedMeanwhile { + m := &removedMeanwhile{Store: s, prefix: prefix, removalsOnly: removalsOnly} + m.armed.Store(true) + return m +} + +func (s *removedMeanwhile) Update(ctx context.Context, fn func(ctx context.Context, tx state.Tx) error) error { + return s.Store.Update(ctx, func(ctx context.Context, tx state.Tx) error { + return fn(ctx, removedMeanwhileTx{Tx: tx, store: s}) + }) +} + +type removedMeanwhileTx struct { + state.Tx + store *removedMeanwhile +} + +func (tx removedMeanwhileTx) races(namespace, key string) bool { + return namespace == control.ReportsNamespace && strings.HasPrefix(key, tx.store.prefix) && tx.store.armed.CompareAndSwap(true, false) +} + +func (tx removedMeanwhileTx) DeleteKV(ctx context.Context, namespace, key string, version int64) error { + if tx.races(namespace, key) { + if err := tx.Tx.DeleteKV(ctx, namespace, key, version); err != nil { + return err + } + return core.NewError(core.CodeNotFound, "document "+key+" does not exist") + } + return tx.Tx.DeleteKV(ctx, namespace, key, version) +} + +func (tx removedMeanwhileTx) PutKV(ctx context.Context, e state.KVEntry) (state.KVEntry, error) { + if !tx.store.removalsOnly && tx.races(e.Namespace, e.Key) { + return state.KVEntry{}, core.NewError(core.CodeConflict, "document "+e.Key+" is not at the version read") + } + return tx.Tx.PutKV(ctx, e) +} + +// A report another publisher removed while this one published it does not +// fail the call: the report is gone as surely as if the read had missed it, +// so its issue is still recorded as published. +func TestAReportRemovedMeanwhileIsStillRecordedAsPublished(t *testing.T) { + t.Parallel() + sqlstoretest.Run(t, func(t *testing.T, db *sqlstoretest.Database) { + e, gh, p := reportEnv(t, db) + sendReportText(t, e, "Crash on save", "Saving twice crashes the app.", 1) + store := meanwhile(e.store, "r/", true) + p.Store = store + res, err := p.Publish(context.Background()) + if err != nil { + t.Fatalf("a report removed meanwhile fails the call: %v", err) + } + if store.armed.Load() { + t.Fatal("the report was never removed; the test does not reach the race") + } + if res.Published != 1 || len(gh.created()) != 1 { + t.Fatalf("a report removed meanwhile: %+v, %d issues", res, len(gh.created())) + } + if countKeys(t, e, "r/") != 0 || countKeys(t, e, "p/") != 1 { + t.Fatalf("a report removed meanwhile: %d waiting, %d published", countKeys(t, e, "r/"), countKeys(t, e, "p/")) + } + }) +} + +// The refusal that gives up a report another publisher removed meanwhile +// writes nothing and does not fail the call: the other publisher may have opened the +// report's issue, so the charge this refusal would give back stays. +func TestARefusalOfAReportRemovedMeanwhileWritesNothing(t *testing.T) { + t.Parallel() + sqlstoretest.Run(t, func(t *testing.T, db *sqlstoretest.Database) { + e, gh, p := reportEnv(t, db) + sendReportText(t, e, "Odd report", "Something GitHub will not take.", 1) + gh.fail = []error{rejected(http.StatusUnprocessableEntity), rejected(http.StatusUnprocessableEntity), rejected(http.StatusUnprocessableEntity)} + // The last refusal gives the report up and removes it. + for range 2 { + publish(t, p) + } + store := meanwhile(e.store, "r/", true) + p.Store = store + if _, err := p.Publish(context.Background()); err != nil { + t.Fatalf("a refusal of a report removed meanwhile fails the call: %v", err) + } + if store.armed.Load() { + t.Fatal("the report was never removed; the test does not reach the race") + } + // The removal this test made rolled back with the refusal, so the + // report waits as stored and nothing records it as given up. + if countKeys(t, e, "r/") != 1 || countKeys(t, e, "f/") != 0 { + t.Fatalf("a refusal of a report removed meanwhile: %d waiting, %d given up", countKeys(t, e, "r/"), countKeys(t, e, "f/")) + } + }) +} + +// The note of the last publishing error is shared by every publisher. One +// that clears it after another publisher did so meanwhile does not fail +// its call: the note left is the other publisher's, as recent. +func TestANoteOfThePublishingErrorChangedMeanwhileFailsNothing(t *testing.T) { + t.Parallel() + sqlstoretest.Run(t, func(t *testing.T, db *sqlstoretest.Database) { + e, gh, p := reportEnv(t, db) + sendReportText(t, e, "Search is slow", "It takes a minute.", 1) + // The outage noted, a publication that clears the note after + // another publisher did so meanwhile succeeds. + gh.fail = []error{core.NewError(core.CodeUnavailable, "GitHub is down")} + if _, err := p.Publish(context.Background()); err == nil { + t.Fatal("a failed publication reported no error") + } + db.Clock.Advance(10 * time.Minute) + cleared := meanwhile(e.store, "publish-error", false) + p.Store = cleared + res, err := p.Publish(context.Background()) + if err != nil || res.Published != 1 { + t.Fatalf("a publication whose clearing of the note lost a race: %+v, %v", res, err) + } + if cleared.armed.Load() { + t.Fatal("the note was never cleared; the test does not reach the race") + } + }) +} diff --git a/internal/control/report_publisher.go b/internal/control/report_publisher.go index e5c16d9f..32408ef2 100644 --- a/internal/control/report_publisher.go +++ b/internal/control/report_publisher.go @@ -321,7 +321,7 @@ const reportPublishErrorKey = "publish-error" // issues is cleared only by a report issue that carries its label // (labelled), not by a call that published nothing. func (p ReportPublisher) noteError(ctx context.Context, failure error, labelled bool) error { - return p.Store.Update(ctx, func(ctx context.Context, tx state.Tx) error { + err := p.Store.Update(ctx, func(ctx context.Context, tx state.Tx) error { entry, err := tx.GetKV(ctx, ReportsNamespace, reportPublishErrorKey) switch { case core.HasCode(err, core.CodeNotFound): @@ -344,6 +344,13 @@ func (p ReportPublisher) noteError(ctx context.Context, failure error, labelled _, err = tx.PutKV(ctx, entry) return err }) + // Another publisher cleared or rewrote the note between the read and + // the clearing: the note left is that publisher's, as recent as this + // call's success. + if failure == nil && (core.HasCode(err, core.CodeConflict) || core.HasCode(err, core.CodeNotFound)) { + return nil + } + return err } // publishOne publishes the report stored at entry, read as stored, and @@ -512,15 +519,18 @@ func (p ReportPublisher) markPublished(ctx context.Context, key string, r stored kept := publishedReport{ID: r.ID, Fingerprint: r.Fingerprint, Issue: number, ReceivedAt: r.ReceivedAt, LastSeenAt: r.LastSeenAt, Count: r.Count} current, err := tx.GetKV(ctx, ReportsNamespace, key) + if err == nil { + // Another publisher that found the same issue may remove the + // report between the read and the removal; it is then gone as + // surely as if the read had missed it. + err = tx.DeleteKV(ctx, ReportsNamespace, key, current.Version) + } switch { case err == nil: var now storedReport if json.Unmarshal(current.Value, &now) == nil { kept.LastSeenAt, kept.Count = now.LastSeenAt, now.Count } - if err := tx.DeleteKV(ctx, ReportsNamespace, key, current.Version); err != nil { - return err - } if err := forgetWaiting(ctx, tx, r.Client, key); err != nil { return err } @@ -591,7 +601,10 @@ func (p ReportPublisher) reject(ctx context.Context, key string, code core.Error if !unreadable && stored.Publish.Rejections < maxReportRejections { return putStored(ctx, tx, key, entry.Version, stored) } - if err := tx.DeleteKV(ctx, ReportsNamespace, key, entry.Version); err != nil { + // Removed meanwhile by another publisher, nothing of this write + // stands, the charge it would give back included, since that + // publisher may have opened the report's issue on it. + if err := deleteRead(ctx, tx, key, entry.Version); err != nil { return err } if err := addFailed(ctx, tx, 1); err != nil { diff --git a/internal/control/report_store.go b/internal/control/report_store.go index 09e65af3..ad9367cb 100644 --- a/internal/control/report_store.go +++ b/internal/control/report_store.go @@ -232,10 +232,24 @@ func (s storedReports) putCountIf(ctx context.Context, tx state.Tx, count report return putCount(ctx, tx, count) } +// deleteRead removes the document at key at the version this transaction +// read it. Another writer may remove it between the read and the removal: +// on a store that does not serialize its transactions, as PostgreSQL does +// not at read committed, the removal then finds nothing. That is a lost +// race like any other and is answered as one, with CONFLICT, so the caller +// reads again rather than failing on NOT_FOUND. +func deleteRead(ctx context.Context, tx state.Tx, key string, version int64) error { + err := tx.DeleteKV(ctx, ReportsNamespace, key, version) + if core.HasCode(err, core.CodeNotFound) { + return core.NewError(core.CodeConflict, "a stored report document was removed meanwhile; read it again").WithCause(err) + } + return err +} + // dropReport removes the stored entry e and its fingerprint index, when // the index still names e. func dropReport(ctx context.Context, tx state.Tx, e state.KVEntry) error { - if err := tx.DeleteKV(ctx, ReportsNamespace, e.Key, e.Version); err != nil { + if err := deleteRead(ctx, tx, e.Key, e.Version); err != nil { return err } return dropIndexOf(ctx, tx, e.Key) @@ -249,7 +263,7 @@ func dropIndexOf(ctx context.Context, tx state.Tx, key string) error { index, err := tx.GetKV(ctx, ReportsNamespace, reportIndexPrefix+fingerprint) switch { case err == nil && string(index.Value) == key: - return tx.DeleteKV(ctx, ReportsNamespace, index.Key, index.Version) + return deleteRead(ctx, tx, index.Key, index.Version) case err == nil || core.HasCode(err, core.CodeNotFound): return nil } diff --git a/internal/control/report_store_clients.go b/internal/control/report_store_clients.go index e30185dc..dfe02d74 100644 --- a/internal/control/report_store_clients.go +++ b/internal/control/report_store_clients.go @@ -124,7 +124,7 @@ func (c clientReports) put(ctx context.Context, tx state.Tx) error { if c.entry.Version == 0 { return nil } - return tx.DeleteKV(ctx, ReportsNamespace, c.entry.Key, c.entry.Version) + return deleteRead(ctx, tx, c.entry.Key, c.entry.Version) } raw, err := json.Marshal(c.keys) if err != nil {