| 1 | package session |
| 2 | |
| 3 | type recoveryPublishState struct { |
| 4 | checkpoint recoveryCheckpoint |
| 5 | operations map[string]operationRecord |
| 6 | } |
| 7 | |
| 8 | func (s *Session) recoveryForDurable(durable uint64) (recoveryPublishState, bool) { |
| 9 | if s == nil || s.recovery == nil { |
| 10 | return recoveryPublishState{}, false |
| 11 | } |
| 12 | s.mu.Lock() |
| 13 | defer s.mu.Unlock() |
| 14 | if durable+1 != s.next { |
| 15 | return recoveryPublishState{}, false |
| 16 | } |
| 17 | store, _ := s.binding.handle.(*Store) |
| 18 | if store == nil { |
| 19 | return recoveryPublishState{}, false |
| 20 | } |
| 21 | store.mu.Lock() |
| 22 | tip := store.tip |
| 23 | store.mu.Unlock() |
| 24 | projection := cloneProjection(s.projection) |
| 25 | projection.Messages = nil |
| 26 | checkpoint := recoveryCheckpoint{ |
| 27 | Version: recoveryFormatVersion, SessionID: s.id, StorageGeneration: s.storageGeneration, |
| 28 | StorageRevision: StorageRevision, DurableSequence: durable, LogOffset: tip.LogOffset, |
| 29 | AnchorOffset: tip.AnchorOffset, AnchorFirst: tip.AnchorFirst, |
| 30 | AnchorCommitID: tip.AnchorCommitID, AnchorHash: tip.AnchorHash, |
| 31 | ProjectionVersion: recoveryProjectionVersion, Projection: projection, |
| 32 | RecentMessages: detachMessages(s.recentMessages), CatalogPreview: s.catalogPreview, |
| 33 | } |
| 34 | operations := make(map[string]operationRecord, len(s.commits)) |
| 35 | for _, commit := range s.commits { |
| 36 | if commit.LastSequence() <= durable { |
| 37 | operations[commit.OperationID] = compactOperationRecord(commit) |
| 38 | } |
| 39 | } |
| 40 | return recoveryPublishState{checkpoint: checkpoint, operations: operations}, true |
| 41 | } |
| 42 | |
| 43 | func (s *Session) recoveryPublished(durable uint64) { |
| 44 | if s == nil { |
| 45 | return |
| 46 | } |
| 47 | s.mu.Lock() |
| 48 | defer s.mu.Unlock() |
| 49 | if durable+1 == s.next { |
| 50 | s.durableRecent = detachMessages(s.recentMessages) |
| 51 | } |
| 52 | cut := 0 |
| 53 | for cut < len(s.commits) && s.commits[cut].LastSequence() <= durable { |
| 54 | delete(s.operations, s.commits[cut].OperationID) |
| 55 | cut++ |
| 56 | } |
| 57 | if cut > 0 { |
| 58 | s.commits = append([]Commit(nil), s.commits[cut:]...) |
| 59 | } |
| 60 | if s.externalHistory { |
| 61 | s.projection.Messages = nil |
| 62 | } |
| 63 | } |
| 64 |