| 1 |
package lmdbsync |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"math" |
| 6 |
"math/rand" |
| 7 |
"time" |
| 8 |
|
| 9 |
"golang.org/x/net/context" |
| 10 |
|
| 11 |
"github.com/bmatsuo/lmdb-go/lmdb" |
| 12 |
) |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
type Handler interface { |
| 18 |
HandleTxnErr(ctx context.Context, env *Env, err error) (context.Context, error) |
| 19 |
} |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
type HandlerChain []Handler |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
func (c HandlerChain) HandleTxnErr(ctx context.Context, env *Env, err error) (context.Context, error) { |
| 28 |
for _, h := range c { |
| 29 |
ctx, err = h.HandleTxnErr(ctx, env, err) |
| 30 |
} |
| 31 |
return ctx, err |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
func (c HandlerChain) Append(h ...Handler) HandlerChain { |
| 38 |
_c := make(HandlerChain, len(c)+len(h)) |
| 39 |
copy(_c, c) |
| 40 |
copy(_c[len(c):], h) |
| 41 |
return _c |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
func MapResizedHandler(maxRetry int, delay DelayFunc) Handler { |
| 56 |
if maxRetry == 0 { |
| 57 |
maxRetry = MapResizedDefaultRetry |
| 58 |
} |
| 59 |
if delay == nil { |
| 60 |
delay = MapResizedDefaultDelay |
| 61 |
} |
| 62 |
return &resizedHandler{ |
| 63 |
MaxRetry: maxRetry, |
| 64 |
Delay: delay, |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
var MapResizedDefaultRetry = 2 |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
type DelayFunc func(attempt int) time.Duration |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
func ExponentialBackoff(base time.Duration, max time.Duration, factor float64) DelayFunc { |
| 80 |
return func(attempt int) time.Duration { |
| 81 |
_max := float64(base) * math.Pow(factor, float64(attempt)) |
| 82 |
_max = math.Min(_max, float64(max)) |
| 83 |
n := rand.Int63n(int64(_max)) |
| 84 |
return time.Duration(n) |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
var MapResizedDefaultDelay = ExponentialBackoff(time.Millisecond, 5*time.Millisecond, 2) |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
type MapFullFunc func(size int64) (int64, bool) |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
func MapFullHandler(fn MapFullFunc) Handler { |
| 110 |
return &mapFullHandler{fn} |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
var ErrTxnRetry = errors.New("lmdbsync: retry failed txn") |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
type TxnRunner interface { |
| 119 |
RunTxn(flags uint, op lmdb.TxnOp) error |
| 120 |
View(op lmdb.TxnOp) error |
| 121 |
Update(op lmdb.TxnOp) error |
| 122 |
UpdateLocked(op lmdb.TxnOp) error |
| 123 |
WithHandler(h Handler) TxnRunner |
| 124 |
} |
| 125 |
|
| 126 |
type handlerRunner struct { |
| 127 |
env *Env |
| 128 |
h Handler |
| 129 |
} |
| 130 |
|
| 131 |
func (r *handlerRunner) WithHandler(h Handler) TxnRunner { |
| 132 |
return &handlerRunner{ |
| 133 |
env: r.env, |
| 134 |
h: HandlerChain{r.h, h}, |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
func (r *handlerRunner) RunTxn(flags uint, op lmdb.TxnOp) error { |
| 139 |
readonly := flags&lmdb.Readonly != 0 |
| 140 |
return r.env.runHandler(readonly, func() error { return r.env.RunTxn(flags, op) }, r.h) |
| 141 |
} |
| 142 |
|
| 143 |
func (r *handlerRunner) View(op lmdb.TxnOp) error { |
| 144 |
return r.env.runHandler(true, func() error { return r.env.View(op) }, r.h) |
| 145 |
} |
| 146 |
|
| 147 |
func (r *handlerRunner) Update(op lmdb.TxnOp) error { |
| 148 |
return r.env.runHandler(false, func() error { return r.env.Update(op) }, r.h) |
| 149 |
} |
| 150 |
|
| 151 |
func (r *handlerRunner) UpdateLocked(op lmdb.TxnOp) error { |
| 152 |
return r.env.runHandler(false, func() error { return r.env.UpdateLocked(op) }, r.h) |
| 153 |
} |
| 154 |
|
| 155 |
type mapFullHandler struct { |
| 156 |
fn MapFullFunc |
| 157 |
} |
| 158 |
|
| 159 |
func (h *mapFullHandler) HandleTxnErr(ctx context.Context, env *Env, err error) (context.Context, error) { |
| 160 |
if !lmdb.IsMapFull(err) { |
| 161 |
return ctx, err |
| 162 |
} |
| 163 |
|
| 164 |
newsize, ok := h.getNewSize(env) |
| 165 |
if !ok { |
| 166 |
return ctx, err |
| 167 |
} |
| 168 |
if env.setMapSize(newsize, 0) != nil { |
| 169 |
return ctx, err |
| 170 |
} |
| 171 |
|
| 172 |
return ctx, ErrTxnRetry |
| 173 |
} |
| 174 |
|
| 175 |
func (h *mapFullHandler) getNewSize(env *Env) (int64, bool) { |
| 176 |
info, err := env.Info() |
| 177 |
if err != nil { |
| 178 |
return 0, false |
| 179 |
} |
| 180 |
newsize, ok := h.fn(info.MapSize) |
| 181 |
if !ok || newsize <= info.MapSize { |
| 182 |
return 0, false |
| 183 |
} |
| 184 |
return newsize, true |
| 185 |
} |
| 186 |
|
| 187 |
type resizedHandlerKey int |
| 188 |
|
| 189 |
type resizeRetryCount struct { |
| 190 |
n int |
| 191 |
} |
| 192 |
|
| 193 |
func (r *resizeRetryCount) Get() int { |
| 194 |
if r == nil { |
| 195 |
return 0 |
| 196 |
} |
| 197 |
return r.n |
| 198 |
} |
| 199 |
|
| 200 |
func (r *resizeRetryCount) Add(n int) *resizeRetryCount { |
| 201 |
if r == nil { |
| 202 |
return &resizeRetryCount{1} |
| 203 |
} |
| 204 |
return &resizeRetryCount{r.n + 1} |
| 205 |
} |
| 206 |
|
| 207 |
func getResizedRetryCount(ctx context.Context) *resizeRetryCount { |
| 208 |
v, _ := ctx.Value(resizedHandlerKey(0)).(*resizeRetryCount) |
| 209 |
return v |
| 210 |
} |
| 211 |
|
| 212 |
func withResizedRetryCount(ctx context.Context, count *resizeRetryCount) context.Context { |
| 213 |
return context.WithValue(ctx, resizedHandlerKey(0), count) |
| 214 |
} |
| 215 |
|
| 216 |
type resizedHandler struct { |
| 217 |
MaxRetry int |
| 218 |
Delay DelayFunc |
| 219 |
} |
| 220 |
|
| 221 |
func (h *resizedHandler) HandleTxnErr(ctx context.Context, env *Env, err error) (context.Context, error) { |
| 222 |
if !lmdb.IsMapResized(err) { |
| 223 |
ctx := context.WithValue(ctx, resizedHandlerKey(0), nil) |
| 224 |
return ctx, err |
| 225 |
} |
| 226 |
|
| 227 |
count := getResizedRetryCount(ctx) |
| 228 |
numRetry := count.Get() |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
maxRetry := h.MaxRetry |
| 233 |
if maxRetry > 0 && numRetry >= maxRetry { |
| 234 |
ctx := withResizedRetryCount(ctx, nil) |
| 235 |
return ctx, err |
| 236 |
} |
| 237 |
|
| 238 |
ctx = withResizedRetryCount(ctx, count.Add(1)) |
| 239 |
|
| 240 |
delay := h.Delay(numRetry) |
| 241 |
err = env.setMapSize(0, delay) |
| 242 |
if err != nil { |
| 243 |
return ctx, err |
| 244 |
} |
| 245 |
return ctx, ErrTxnRetry |
| 246 |
} |