152 lines
4.2 KiB
Go

package outputsnapshot
import (
"context"
"fmt"
"sync"
"time"
)
// MemoryBackend is the in-memory Adapter used by API fixtures. Its coordinator
// and bounded slots have the same ownership/publication rules as Mongo.
type MemoryBackend struct {
mu sync.Mutex
state State
slots [SlotCount]memoryBlob
shards map[string][]byte
}
type memoryBlob struct {
epoch int64
id string
data []byte
}
func (b *MemoryBackend) OutputSnapshotState(context.Context) (State, error) {
b.mu.Lock()
defer b.mu.Unlock()
s := b.state
s.Heads = map[string]Head{}
for k, h := range b.state.Heads {
s.Heads[k] = h
}
return s, nil
}
func (b *MemoryBackend) AcquireOutputSnapshot(_ context.Context, key string, now time.Time) (Lease, bool, error) {
b.mu.Lock()
defer b.mu.Unlock()
h := b.state.Heads[key]
if b.state.Owner != "" && now.Before(b.state.Until) || now.Before(h.NextAttempt) || h.Current != nil && now.Sub(h.Current.AsOf) < RefreshInterval {
return Lease{}, false, nil
}
b.state.Epoch++
b.state.Owner = fmt.Sprintf("memory-%d", b.state.Epoch)
b.state.ActiveKey = key
b.state.ActiveEpoch = b.state.Epoch
b.state.Until = now.Add(LeaseDuration)
l, err := PrepareLease(&b.state, key, now)
if err != nil {
b.state.Owner = ""
return Lease{}, false, err
}
return l, true, nil
}
func (b *MemoryBackend) owns(l Lease, now time.Time) bool {
return b.state.Owner == l.Owner && b.state.Epoch == l.Epoch && now.Before(b.state.Until)
}
func (b *MemoryBackend) RenewOutputSnapshot(_ context.Context, l Lease, now time.Time) (bool, error) {
b.mu.Lock()
defer b.mu.Unlock()
if !b.owns(l, now) {
return false, nil
}
b.state.Until = now.Add(LeaseDuration)
return true, nil
}
func (b *MemoryBackend) PublishOutputSnapshot(_ context.Context, l Lease, g Generation, data []byte, now time.Time) (bool, error) {
b.mu.Lock()
defer b.mu.Unlock()
if !b.owns(l, now) {
return false, nil
}
if !ValidPublication(l, g, data) {
return false, ErrCapacity
}
b.slots[l.Slot] = memoryBlob{l.Epoch, g.ID, append([]byte(nil), data...)}
b.state.Heads[l.Key] = Head{Current: &g, Previous: l.Previous, NextAttempt: g.AsOf.Add(RefreshInterval)}
b.state.Owner = ""
for key := range b.shards {
var slot int
var epoch int64
var index int
if _, err := fmt.Sscanf(key, "%d/%d/%d", &slot, &epoch, &index); err == nil && slot == l.Slot && epoch < l.Epoch {
delete(b.shards, key)
}
}
return true, nil
}
func (b *MemoryBackend) FailOutputSnapshot(_ context.Context, l Lease, now time.Time, failure Failure) error {
b.mu.Lock()
defer b.mu.Unlock()
if b.state.Owner == l.Owner && b.state.Epoch == l.Epoch {
h := b.state.Heads[l.Key]
h.Failed = true
h.ConsecutiveFailures++
h.LastFailure = &failure
h.NextAttempt = now.Add(RetryInterval)
b.state.Heads[l.Key] = h
b.state.Owner = ""
}
return nil
}
func (b *MemoryBackend) OutputSnapshotBlob(_ context.Context, g Generation) ([]byte, error) {
b.mu.Lock()
defer b.mu.Unlock()
v := b.slots[g.Slot]
if v.epoch != g.Epoch || v.id != g.ID {
return nil, ErrExpired
}
return append([]byte(nil), v.data...), nil
}
func shardKey(slot int, epoch int64, index int) string {
return fmt.Sprintf("%d/%d/%d", slot, epoch, index)
}
func (b *MemoryBackend) StageOutputSnapshotShard(_ context.Context, l Lease, index int, data []byte, now time.Time) (bool, error) {
b.mu.Lock()
defer b.mu.Unlock()
if !b.owns(l, now) {
return false, nil
}
if l.Slot < 0 || l.Slot >= SlotCount || index < 0 || index >= MaxShards || len(data) > MaxEncodedBytes {
return false, ErrCapacity
}
if b.shards == nil {
b.shards = map[string][]byte{}
}
key := shardKey(l.Slot, l.Epoch, index)
if _, exists := b.shards[key]; exists {
return false, ErrUnavailable
}
if index == 0 {
for existing := range b.shards {
var slot int
var epoch int64
var index int
if _, err := fmt.Sscanf(existing, "%d/%d/%d", &slot, &epoch, &index); err == nil && slot == l.Slot && epoch < l.Epoch {
delete(b.shards, existing)
}
}
}
b.shards[key] = append([]byte(nil), data...)
return true, nil
}
func (b *MemoryBackend) OutputSnapshotShard(_ context.Context, g Generation, index int) ([]byte, error) {
b.mu.Lock()
defer b.mu.Unlock()
data, ok := b.shards[shardKey(g.Slot, g.Epoch, index)]
if !ok {
return nil, ErrExpired
}
return append([]byte(nil), data...), nil
}