| 1 |
package desync |
| 2 |
|
| 3 |
import ( |
| 4 |
"strings" |
| 5 |
"sync" |
| 6 |
) |
| 7 |
|
| 8 |
var _ Store = &FailoverGroup{} |
| 9 |
|
| 10 |
// FailoverGroup wraps multiple stores to provide failover when one or more stores in the group fail. |
| 11 |
// Only one of the stores in the group is considered "active" at a time. If an unexpected error is returned |
| 12 |
// from the active store, the next store in the group becomes the active one and the request retried. |
| 13 |
// When all stores returned a failure, the group will pass up the failure to the caller. The active store |
| 14 |
// rotates through all available stores. All stores in the group are expected to contain the same chunks, |
| 15 |
// there is no failover for missing chunks. Implements the Store interface. |
| 16 |
type FailoverGroup struct { |
| 17 |
stores []Store |
| 18 |
active int |
| 19 |
mu sync.RWMutex |
| 20 |
} |
| 21 |
|
| 22 |
// NewFailoverGroup initializes and returns a store wraps multiple stores to form a group that can fail over |
| 23 |
// between them on failure from one. |
| 24 |
func NewFailoverGroup(stores ...Store) *FailoverGroup { |
| 25 |
return &FailoverGroup{stores: stores} |
| 26 |
} |
| 27 |
|
| 28 |
func (g *FailoverGroup) GetChunk(id ChunkID) (*Chunk, error) { |
| 29 |
var gErr error |
| 30 |
for i := 0; i < len(g.stores); i++ { |
| 31 |
s, active := g.current() |
| 32 |
b, err := s.GetChunk(id) |
| 33 |
if err == nil { // return right away on success |
| 34 |
return b, err |
| 35 |
} |
| 36 |
|
| 37 |
// All stores are meant to hold the same chunks, fail on the first missing chunk |
| 38 |
if _, ok := err.(ChunkMissing); ok { |
| 39 |
return b, err |
| 40 |
} |
| 41 |
|
| 42 |
// Record the error to be returned when all requests fail |
| 43 |
gErr = err |
| 44 |
|
| 45 |
// Fail over to the next store |
| 46 |
g.errorFrom(active) |
| 47 |
} |
| 48 |
return nil, gErr |
| 49 |
} |
| 50 |
|
| 51 |
func (g *FailoverGroup) HasChunk(id ChunkID) (bool, error) { |
| 52 |
var gErr error |
| 53 |
for i := 0; i < len(g.stores); i++ { |
| 54 |
s, active := g.current() |
| 55 |
hc, err := s.HasChunk(id) |
| 56 |
if err == nil { // return right away on success |
| 57 |
return hc, err |
| 58 |
} |
| 59 |
|
| 60 |
// Record the error to be returned when all requests fail |
| 61 |
gErr = err |
| 62 |
|
| 63 |
// Fail over to the next store |
| 64 |
g.errorFrom(active) |
| 65 |
} |
| 66 |
return false, gErr |
| 67 |
} |
| 68 |
|
| 69 |
func (g *FailoverGroup) String() string { |
| 70 |
var str []string |
| 71 |
for _, s := range g.stores { |
| 72 |
str = append(str, s.String()) |
| 73 |
} |
| 74 |
return strings.Join(str, "|") |
| 75 |
} |
| 76 |
|
| 77 |
func (g *FailoverGroup) Close() error { |
| 78 |
var closeErr error |
| 79 |
for _, s := range g.stores { |
| 80 |
if err := s.Close(); err != nil { |
| 81 |
closeErr = err |
| 82 |
} |
| 83 |
} |
| 84 |
return closeErr |
| 85 |
} |
| 86 |
|
| 87 |
// Thread-safe method to return the currently active store. |
| 88 |
func (g *FailoverGroup) current() (Store, int) { |
| 89 |
g.mu.RLock() |
| 90 |
defer g.mu.RUnlock() |
| 91 |
return g.stores[g.active], g.active |
| 92 |
} |
| 93 |
|
| 94 |
// Fail over to the next available store after receiving an error from i (the active). We |
| 95 |
// need i to know which store returned the error as there could be failures from concurrent |
| 96 |
// requests. Another request could have initiated the failover already. So ignore if i is not |
| 97 |
// (no longer) the active store. |
| 98 |
func (g *FailoverGroup) errorFrom(i int) { |
| 99 |
g.mu.Lock() |
| 100 |
defer g.mu.Unlock() |
| 101 |
if i != g.active { |
| 102 |
return |
| 103 |
} |
| 104 |
g.active = (g.active + 1) % len(g.stores) |
| 105 |
} |