/usr/share/gocode/src/github.com/jacobsa/gcloud/gcs/retry.go is in golang-github-jacobsa-gcloud-dev 0.0~git20150709-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 | // Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package gcs
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"math"
"math/rand"
"net"
"net/url"
"time"
"golang.org/x/net/context"
"google.golang.org/api/googleapi"
)
// A bucket that wraps another, calling its methods in a retry loop with
// randomized exponential backoff.
type retryBucket struct {
maxSleep time.Duration
wrapped Bucket
}
func newRetryBucket(
maxSleep time.Duration,
wrapped Bucket) (b Bucket) {
b = &retryBucket{
maxSleep: maxSleep,
wrapped: wrapped,
}
return
}
////////////////////////////////////////////////////////////////////////
// Helpers
////////////////////////////////////////////////////////////////////////
func shouldRetry(err error) (b bool) {
// HTTP 50x errors.
if typed, ok := err.(*googleapi.Error); ok {
if typed.Code >= 500 && typed.Code < 600 {
b = true
return
}
}
// HTTP 429 errors (GCS uses these for rate limiting).
if typed, ok := err.(*googleapi.Error); ok {
if typed.Code == 429 {
b = true
return
}
}
// Network errors, which tend to show up transiently when doing lots of
// operations in parallel. For example:
//
// dial tcp 74.125.203.95:443: too many open files
//
if _, ok := err.(*net.OpError); ok {
b = true
return
}
// The HTTP package returns ErrUnexpectedEOF in several places. This seems to
// come up when the server terminates the connection in the middle of an
// object read.
if err == io.ErrUnexpectedEOF {
b = true
return
}
// The HTTP library also appears to leak EOF errors from... somewhere in its
// guts as URL errors sometimes.
if urlErr, ok := err.(*url.Error); ok {
if urlErr.Err == io.EOF {
b = true
return
}
}
// Sometimes the HTTP package helpfully encapsulates the real error in a URL
// error.
if urlErr, ok := err.(*url.Error); ok {
b = shouldRetry(urlErr.Err)
return
}
return
}
// Choose an appropriate delay for exponential backoff, given that we have
// already slept the given number of times for this logical request.
func chooseDelay(prevSleepCount uint) (d time.Duration) {
const baseDelay = time.Millisecond
// Choose a a delay in [0, 2^prevSleepCount * baseDelay).
d = (1 << prevSleepCount) * baseDelay
d = time.Duration(float64(d) * rand.Float64())
return
}
// Exponential backoff for a function that might fail.
//
// This is essentially what is described in the "Best practices" section of the
// "Upload Objects" docs:
//
// https://cloud.google.com/storage/docs/json_api/v1/how-tos/upload
//
// with the following exceptions:
//
// * We perform backoff for all operations.
//
// * The random component scales with the delay, so that the first sleep
// cannot be as long as one second. The algorithm used matches the
// description at http://en.wikipedia.org/wiki/Exponential_backoff.
//
// * We retry more types of errors; see shouldRetry above.
//
// State for total sleep time and number of previous sleeps is housed outside
// of this function to allow it to be "resumed" by multiple invocations of
// retryObjectReader.Read.
func expBackoff(
ctx context.Context,
desc string,
maxSleep time.Duration,
f func() error,
prevSleepCount *uint,
prevSleepDuration *time.Duration) (err error) {
for {
// Make an attempt. Stop if successful.
err = f()
if err == nil {
return
}
// Do we want to retry?
if !shouldRetry(err) {
// Special case: don't spam up the logs for EOF, which io.Reader returns
// in the normal course of things.
if err != io.EOF {
log.Printf(
"Not retrying %s after error of type %T (%q): %#v",
desc,
err,
err.Error(),
err)
}
return
}
// Choose a a delay.
d := chooseDelay(*prevSleepCount)
*prevSleepCount++
// Are we out of credit?
if *prevSleepDuration+d > maxSleep {
// Return the most recent error.
return
}
// Sleep, returning early if cancelled.
log.Printf(
"Retrying %s after error of type %T (%q) in %v",
desc,
err,
err,
d)
select {
case <-ctx.Done():
// On cancellation, return the last error we saw.
return
case <-time.After(d):
*prevSleepDuration += d
continue
}
}
}
// Like expBackoff, but assumes that we've never slept before (and won't need
// to sleep again).
func oneShotExpBackoff(
ctx context.Context,
desc string,
maxSleep time.Duration,
f func() error) (err error) {
var prevSleepCount uint
var prevSleepDuration time.Duration
err = expBackoff(
ctx,
desc,
maxSleep,
f,
&prevSleepCount,
&prevSleepDuration)
return
}
////////////////////////////////////////////////////////////////////////
// Read support
////////////////////////////////////////////////////////////////////////
type retryObjectReader struct {
bucket *retryBucket
// The context we should watch when sleeping for retries.
ctx context.Context
// What we are trying to read.
name string
generation int64
byteRange ByteRange
// nil when we start or have seen a permanent error.
wrapped io.ReadCloser
// If we've seen an error that we shouldn't retry for, this will be non-nil
// and should be returned permanently.
permanentErr error
// The number of times we've slept so far, and the total amount of time we've
// spent sleeping.
sleepCount uint
sleepDuration time.Duration
}
// Set up the wrapped reader.
func (rc *retryObjectReader) setUpWrapped() (err error) {
// Call through to create the reader.
req := &ReadObjectRequest{
Name: rc.name,
Generation: rc.generation,
Range: &rc.byteRange,
}
wrapped, err := rc.bucket.wrapped.NewReader(rc.ctx, req)
if err != nil {
return
}
rc.wrapped = wrapped
return
}
// Set up the wrapped reader if necessary, and make one attempt to read through
// it.
//
// Clears the wrapped reader on error.
func (rc *retryObjectReader) readOnce(p []byte) (n int, err error) {
// Set up the wrapped reader if it's not already around.
if rc.wrapped == nil {
err = rc.setUpWrapped()
if err != nil {
return
}
}
// Attempt to read from it.
n, err = rc.wrapped.Read(p)
if err != nil {
rc.wrapped.Close()
rc.wrapped = nil
return
}
return
}
// Invariant: we never return an error from this function unless we've given up
// on retrying. In particular, we won't return a short read because the wrapped
// reader returned a short read and an error.
func (rc *retryObjectReader) Read(p []byte) (n int, err error) {
// Whatever we do, accumulate the bytes that we're returning to the user.
defer func() {
if n < 0 {
panic(fmt.Sprintf("Negative byte count: %d", n))
}
rc.byteRange.Start += uint64(n)
}()
// If we've already decided on a permanent error, return that.
if rc.permanentErr != nil {
err = rc.permanentErr
return
}
// If we let an error escape below, it must be a permanent one.
defer func() {
if err != nil {
rc.permanentErr = err
}
}()
// We will repeatedly make single attempts until we get a successful request.
// Don't forget to accumulate the result each time.
tryOnce := func() (err error) {
var bytesRead int
bytesRead, err = rc.readOnce(p)
n += bytesRead
p = p[bytesRead:]
return
}
err = expBackoff(
rc.ctx,
fmt.Sprintf("Read(%q, %d)", rc.name, rc.generation),
rc.bucket.maxSleep,
tryOnce,
&rc.sleepCount,
&rc.sleepDuration)
return
}
func (rc *retryObjectReader) Close() (err error) {
// If we don't have a wrapped reader, there is nothing useful that we can or
// need to do here.
if rc.wrapped == nil {
return
}
// Call through.
err = rc.wrapped.Close()
return
}
////////////////////////////////////////////////////////////////////////
// Public interface
////////////////////////////////////////////////////////////////////////
func (rb *retryBucket) Name() (name string) {
name = rb.wrapped.Name()
return
}
func (rb *retryBucket) NewReader(
ctx context.Context,
req *ReadObjectRequest) (rc io.ReadCloser, err error) {
// If the user specified the latest generation, we need to figure out what
// that is so that we can create a reader that knows how to keep a stable
// generation despite retrying repeatedly.
var generation int64 = req.Generation
var sleepCount uint
var sleepDuration time.Duration
if generation == 0 {
findGeneration := func() (err error) {
o, err := rb.wrapped.StatObject(
ctx,
&StatObjectRequest{
Name: req.Name,
})
if err != nil {
return
}
generation = o.Generation
return
}
err = expBackoff(
ctx,
fmt.Sprintf("FindLatestGeneration(%q)", req.Name),
rb.maxSleep,
findGeneration,
&sleepCount,
&sleepDuration)
if err != nil {
return
}
}
// Choose an appropriate byte range.
byteRange := ByteRange{0, math.MaxUint64}
if req.Range != nil {
byteRange = *req.Range
}
// Now that we know what generation we're looking for, return an appropriate
// reader that knows how to retry when the connection fails. Make sure to
// inherit the time spent sleeping above.
rc = &retryObjectReader{
bucket: rb,
ctx: ctx,
name: req.Name,
generation: generation,
byteRange: byteRange,
sleepCount: sleepCount,
sleepDuration: sleepDuration,
}
return
}
func (rb *retryBucket) CreateObject(
ctx context.Context,
req *CreateObjectRequest) (o *Object, err error) {
// We can't simply replay the request multiple times, because the first
// attempt might exhaust some of the req.Contents reader, leaving missing
// contents for the second attempt.
//
// So, copy out all contents and create a modified request that serves from
// memory.
contents, err := ioutil.ReadAll(req.Contents)
if err != nil {
err = fmt.Errorf("ioutil.ReadAll: %v", err)
return
}
reqCopy := *req
reqCopy.Contents = bytes.NewReader(contents)
// Call through with that request.
err = oneShotExpBackoff(
ctx,
fmt.Sprintf("CreateObject(%q)", req.Name),
rb.maxSleep,
func() (err error) {
o, err = rb.wrapped.CreateObject(ctx, &reqCopy)
return
})
return
}
func (rb *retryBucket) CopyObject(
ctx context.Context,
req *CopyObjectRequest) (o *Object, err error) {
err = oneShotExpBackoff(
ctx,
fmt.Sprintf("CopyObject(%q, %q)", req.SrcName, req.DstName),
rb.maxSleep,
func() (err error) {
o, err = rb.wrapped.CopyObject(ctx, req)
return
})
return
}
func (rb *retryBucket) ComposeObjects(
ctx context.Context,
req *ComposeObjectsRequest) (o *Object, err error) {
err = oneShotExpBackoff(
ctx,
fmt.Sprintf("ComposeObjects(%q)", req.DstName),
rb.maxSleep,
func() (err error) {
o, err = rb.wrapped.ComposeObjects(ctx, req)
return
})
return
}
func (rb *retryBucket) StatObject(
ctx context.Context,
req *StatObjectRequest) (o *Object, err error) {
err = oneShotExpBackoff(
ctx,
fmt.Sprintf("StatObject(%q)", req.Name),
rb.maxSleep,
func() (err error) {
o, err = rb.wrapped.StatObject(ctx, req)
return
})
return
}
func (rb *retryBucket) ListObjects(
ctx context.Context,
req *ListObjectsRequest) (listing *Listing, err error) {
err = oneShotExpBackoff(
ctx,
fmt.Sprintf("ListObjects(%q)", req.Prefix),
rb.maxSleep,
func() (err error) {
listing, err = rb.wrapped.ListObjects(ctx, req)
return
})
return
}
func (rb *retryBucket) UpdateObject(
ctx context.Context,
req *UpdateObjectRequest) (o *Object, err error) {
err = oneShotExpBackoff(
ctx,
fmt.Sprintf("UpdateObject(%q)", req.Name),
rb.maxSleep,
func() (err error) {
o, err = rb.wrapped.UpdateObject(ctx, req)
return
})
return
}
func (rb *retryBucket) DeleteObject(
ctx context.Context,
req *DeleteObjectRequest) (err error) {
err = oneShotExpBackoff(
ctx,
fmt.Sprintf("DeleteObject(%q)", req.Name),
rb.maxSleep,
func() (err error) {
err = rb.wrapped.DeleteObject(ctx, req)
return
})
return
}
|