| 1 |
package desync |
| 2 |
|
| 3 |
import "fmt" |
| 4 |
|
| 5 |
var _ WriteStore = &WriteDedupQueue{} |
| 6 |
|
| 7 |
// WriteDedupQueue wraps a writable store and provides deduplication of incoming chunk requests and store |
| 8 |
// operation. This is useful when a burst of requests for the same chunk is received and the chunk store |
| 9 |
// serving those is slow or when the underlying filesystem does not support atomic rename operations |
| 10 |
// (Windows). With the DedupQueue wrapper, concurrent requests for the same chunk will result in just |
| 11 |
// one request to the upstream store. Implements the WriteStore interface. |
| 12 |
type WriteDedupQueue struct { |
| 13 |
S WriteStore |
| 14 |
*DedupQueue |
| 15 |
storeChunkQueue *queue |
| 16 |
} |
| 17 |
|
| 18 |
// NewWriteDedupQueue initializes a new instance of the wrapper. |
| 19 |
func NewWriteDedupQueue(store WriteStore) *WriteDedupQueue { |
| 20 |
return &WriteDedupQueue{ |
| 21 |
S: store, |
| 22 |
DedupQueue: NewDedupQueue(store), |
| 23 |
storeChunkQueue: newQueue(), |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
func (q *WriteDedupQueue) GetChunk(id ChunkID) (*Chunk, error) { |
| 28 |
// If the chunk is being stored just wait and return the data |
| 29 |
q.storeChunkQueue.mu.Lock() |
| 30 |
req, isInFlight := q.storeChunkQueue.requests[id] |
| 31 |
q.storeChunkQueue.mu.Unlock() |
| 32 |
|
| 33 |
if isInFlight { |
| 34 |
data, err := req.wait() |
| 35 |
switch b := data.(type) { |
| 36 |
case nil: |
| 37 |
return nil, err |
| 38 |
case *Chunk: |
| 39 |
// The chunk in the request is shared with all waiters. Chunk |
| 40 |
// materializes its plain data and ID lazily without locking, |
| 41 |
// so hand every caller its own copy. The copies still share |
| 42 |
// the underlying (read-only) chunk data. |
| 43 |
return b.clone(), err |
| 44 |
default: |
| 45 |
return nil, fmt.Errorf("internal error: unexpected type %T", data) |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
// If the chunk is not currently being stored get the chunk as usual |
| 50 |
return q.DedupQueue.GetChunk(id) |
| 51 |
} |
| 52 |
|
| 53 |
func (q *WriteDedupQueue) HasChunk(id ChunkID) (bool, error) { |
| 54 |
return q.DedupQueue.HasChunk(id) |
| 55 |
} |
| 56 |
|
| 57 |
func (q *WriteDedupQueue) StoreChunk(chunk *Chunk) error { |
| 58 |
id := chunk.ID() |
| 59 |
req, isInFlight := q.storeChunkQueue.loadOrStore(id) |
| 60 |
|
| 61 |
if isInFlight { // The request is already in-flight, wait for it to come back |
| 62 |
_, err := req.wait() |
| 63 |
return err |
| 64 |
} |
| 65 |
|
| 66 |
// This request is the first one for this chunk, execute as normal |
| 67 |
err := q.S.StoreChunk(chunk) |
| 68 |
|
| 69 |
// Signal to any others that wait for us that we're done, they'll use our data |
| 70 |
// and don't need to hit the store themselves. Publish a copy of the chunk, |
| 71 |
// detached from the one the caller passed in and keeps using, so the copy |
| 72 |
// stays untouched while waiters read from it. |
| 73 |
req.markDone(chunk.clone(), err) |
| 74 |
|
| 75 |
// We're done, drop the request from the queue to avoid keeping all the chunk data |
| 76 |
// in memory after the request is done |
| 77 |
q.storeChunkQueue.delete(id) |
| 78 |
|
| 79 |
return err |
| 80 |
} |