internal/bigquery: ignore PreconditionFailed error when updating table It arises from a benign race condition. See the comment in the CL for details. Change-Id: Ica5fce00f21a72b1a3bcf080db528c26f1663393 Reviewed-on: https://go-review.googlesource.com/c/pkgsite-metrics/+/493456 Run-TryBot: Jonathan Amsterdam <jba@google.com> TryBot-Result: Gopher Robot <gobot@golang.org> Reviewed-by: Zvonimir Pavlinovic <zpavlinovic@google.com>
diff --git a/internal/bigquery/bigquery.go b/internal/bigquery/bigquery.go index 95971af..35d42b8 100644 --- a/internal/bigquery/bigquery.go +++ b/internal/bigquery/bigquery.go
@@ -155,10 +155,14 @@ return false, fmt.Errorf("no schema registered for table %q", tableID) } _, err = c.Table(tableID).Update(ctx, bq.TableMetadataToUpdate{Schema: schema}, meta.ETag) - if err != nil && !isAlreadyExistsError(err) { - return false, err + // There is a race condition if multiple threads of control call this function concurrently: + // The table may have changed since Metadata was called above, making the Etag invalid and + // resulting in a PreconditionFailed error. This error is harmless: it just means that someone + // else updated the table before us. Ignore it. + if isAlreadyExistsError(err) || hasCode(err, http.StatusPreconditionFailed) { + return false, nil } - return false, nil + return false, err } // A Row is something that can be uploaded to BigQuery.