| 1 |
package desync |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"sync" |
| 6 |
) |
| 7 |
|
| 8 |
var _ Store = &DedupQueue{} |
| 9 |
|
| 10 |
// DedupQueue wraps a store and provides deduplication of incoming chunk requests. This is useful when |
| 11 |
// a burst of requests for the same chunk is received and the chunk store serving those is slow. With |
| 12 |
// the DedupQueue wrapper, concurrent requests for the same chunk will result in just one request to the |
| 13 |
// upstream store. Implements the Store interface. |
| 14 |
type DedupQueue struct { |
| 15 |
store Store |
| 16 |
getChunkQueue *queue |
| 17 |
hasChunkQueue *queue |
| 18 |
} |
| 19 |
|
| 20 |
// NewDedupQueue initializes a new instance of the wrapper. |
| 21 |
func NewDedupQueue(store Store) *DedupQueue { |
| 22 |
return &DedupQueue{ |
| 23 |
store: store, |
| 24 |
getChunkQueue: newQueue(), |
| 25 |
hasChunkQueue: newQueue(), |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
func (q *DedupQueue) GetChunk(id ChunkID) (*Chunk, error) { |
| 30 |
req, isInFlight := q.getChunkQueue.loadOrStore(id) |
| 31 |
|
| 32 |
if isInFlight { // The request is already in-flight, wait for it to come back |
| 33 |
data, err := req.wait() |
| 34 |
switch b := data.(type) { |
| 35 |
case nil: |
| 36 |
return nil, err |
| 37 |
case *Chunk: |
| 38 |
// The chunk in the request is shared with all waiters. Chunk |
| 39 |
// materializes its plain data and ID lazily without locking, |
| 40 |
// so hand every caller its own copy. The copies still share |
| 41 |
// the underlying (read-only) chunk data. |
| 42 |
return b.clone(), err |
| 43 |
default: |
| 44 |
return nil, fmt.Errorf("internal error: unexpected type %T", data) |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
// This request is the first one for this chunk, execute as normal |
| 49 |
b, err := q.store.GetChunk(id) |
| 50 |
|
| 51 |
// Signal to any others that wait for us that we're done, they'll use our data |
| 52 |
// and don't need to hit the store themselves |
| 53 |
req.markDone(b, err) |
| 54 |
|
| 55 |
// We're done, drop the request from the queue to avoid keeping all the chunk data |
| 56 |
// in memory after the request is done |
| 57 |
q.getChunkQueue.delete(id) |
| 58 |
|
| 59 |
if b == nil { |
| 60 |
return nil, err |
| 61 |
} |
| 62 |
// Return a copy for the same reason the waiters get one: the chunk stored |
| 63 |
// in the request must stay untouched while waiters are still reading it. |
| 64 |
return b.clone(), err |
| 65 |
} |
| 66 |
|
| 67 |
func (q *DedupQueue) HasChunk(id ChunkID) (bool, error) { |
| 68 |
req, isInFlight := q.hasChunkQueue.loadOrStore(id) |
| 69 |
|
| 70 |
if isInFlight { // The request is already in-flight, wait for it to come back |
| 71 |
data, err := req.wait() |
| 72 |
return data.(bool), err |
| 73 |
} |
| 74 |
|
| 75 |
// This request is the first one for this chunk, execute as normal |
| 76 |
hasChunk, err := q.store.HasChunk(id) |
| 77 |
|
| 78 |
// Signal to any others that wait for us that we're done, they'll use our data |
| 79 |
// and don't need to hit the store themselves |
| 80 |
req.markDone(hasChunk, err) |
| 81 |
|
| 82 |
// We're done, drop the request from the queue to avoid keeping all in memory |
| 83 |
q.hasChunkQueue.delete(id) |
| 84 |
return hasChunk, err |
| 85 |
} |
| 86 |
|
| 87 |
func (q *DedupQueue) String() string { return q.store.String() } |
| 88 |
|
| 89 |
func (q *DedupQueue) Close() error { return q.store.Close() } |
| 90 |
|
| 91 |
// queue manages the in-flight requests |
| 92 |
type queue struct { |
| 93 |
requests map[ChunkID]*request |
| 94 |
mu sync.Mutex |
| 95 |
} |
| 96 |
|
| 97 |
func newQueue() *queue { |
| 98 |
return &queue{requests: make(map[ChunkID]*request)} |
| 99 |
} |
| 100 |
|
| 101 |
// Returns either a new request, or an existing one from the queue. |
| 102 |
func (q *queue) loadOrStore(id ChunkID) (*request, bool) { |
| 103 |
q.mu.Lock() |
| 104 |
req, isInFlight := q.requests[id] |
| 105 |
if !isInFlight { |
| 106 |
req = newRequest() |
| 107 |
q.requests[id] = req |
| 108 |
} |
| 109 |
q.mu.Unlock() |
| 110 |
return req, isInFlight |
| 111 |
} |
| 112 |
|
| 113 |
func (q *queue) delete(id ChunkID) { |
| 114 |
q.mu.Lock() |
| 115 |
delete(q.requests, id) |
| 116 |
q.mu.Unlock() |
| 117 |
} |
| 118 |
|
| 119 |
// queueRequests is used to dedup requests for GetChunk() or HasChunk() with the data |
| 120 |
// being either the chunk itself or a bool in case of HasChunk(). |
| 121 |
type request struct { |
| 122 |
data any |
| 123 |
err error |
| 124 |
done chan struct{} |
| 125 |
} |
| 126 |
|
| 127 |
func newRequest() *request { |
| 128 |
return &request{done: make(chan struct{})} |
| 129 |
} |
| 130 |
|
| 131 |
// Wait for the request to complete. Returns the data as well as the error from the request. |
| 132 |
func (r *request) wait() (any, error) { |
| 133 |
<-r.done |
| 134 |
return r.data, r.err |
| 135 |
} |
| 136 |
|
| 137 |
// Set the result data and marks this request as complete. |
| 138 |
func (r *request) markDone(data any, err error) { |
| 139 |
r.data = data |
| 140 |
r.err = err |
| 141 |
close(r.done) |
| 142 |
} |