package console // The console's reads through the S3 change index (S3 change-index design // §4.6, discussion #336). Armed only by CARLOS_CHANGE_INDEX=read, which // cmd/console turns into a record view handed to SetRecordView; unarmed, // recordView stays nil and every sweep walks the bucket as before. import ( "context" "log" "github.com/carlosframework/platform/internal/provision" "github.com/carlosframework/platform/internal/release" ) // SetRecordView arms the run and seed sweeps' record reads with v. serve // calls it once, before it starts any worker; nil leaves them disarmed. func (s *Server) SetRecordView(v *provision.RecordView) { s.recordView = v } // The viewUse states; zero is none yet. const ( viewUseView int32 = 1 + iota viewUseWalk ) // recordWalkStore is the store a sweep's record walk reads: the armed view's // snapshot over live, refreshed first, when the refresh returned nil and the // snapshot is Complete. Otherwise ok is false and the store is live, so the // sweep walks the bucket as it did before the index (spec §3.4). // // Only record walks take it (provision.ListAllRecords*). The snapshot's // ListDirs of apps// names only apps with record-family state, so it must // never reach an app enumerator, and every write and every non-record read // (runs/, seeds/) stays on live. ctx bounds the refresh, so a stuck index // poll or walk cannot hold the view, or the sweep, for good. func (s *Server) recordWalkStore(ctx context.Context, live release.Store) (release.Store, bool) { v := s.recordView if v == nil || live == nil { return live, false } err := v.Refresh(ctx) sn := v.Snapshot() switch { case err != nil: s.noteViewUse(false, "the record view's refresh failed: "+err.Error()) return live, false case sn == nil || !sn.Complete: s.noteViewUse(false, "the record view's snapshot is incomplete") return live, false } s.noteViewUse(true, "") return sn.Store(live), true } // noteViewUse logs a switch between reading the view and walking the bucket, // once per change rather than on every pass. func (s *Server) noteViewUse(ok bool, why string) { state := viewUseWalk if ok { state = viewUseView } if s.viewUse.Swap(state) == state { return } if ok { log.Printf("console: record view: the sweeps' record walks read the view") return } log.Printf("console: record view: %s; the sweeps' record walks read the bucket until it can vouch again", why) }