Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions google-cloud-bigquery/lib/google/cloud/bigquery/service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -446,9 +446,15 @@ def insert_job config, location: nil
end

def query_job query_job_gapi
execute backoff: true do
retried = false
execute backoff: true, on_retry: -> { retried = true } do
service.insert_job @project, query_job_gapi
end
rescue Google::Cloud::AlreadyExistsError
raise unless retried

job_ref = query_job_gapi.job_reference
get_job job_ref.job_id, location: job_ref.location
end

##
Expand Down Expand Up @@ -612,9 +618,9 @@ def mime_type_for file
nil
end

def execute backoff: nil, &block
def execute backoff: nil, on_retry: nil, &block
if backoff
Backoff.new(retries: retries).execute(&block)
Backoff.new(retries: retries, on_retry: on_retry).execute(&block)
else
yield
end
Expand Down Expand Up @@ -647,10 +653,11 @@ class << self
sleep delay
end

def initialize retries: nil, reasons: nil, backoff: nil
def initialize retries: nil, reasons: nil, backoff: nil, on_retry: nil
@retries = (retries || Backoff.retries).to_i
@reasons = (reasons || Backoff.reasons).to_a
@backoff = backoff || Backoff.backoff
@on_retry = on_retry
end

def execute
Expand All @@ -661,6 +668,7 @@ def execute
raise e unless retry? e.body, current_retries

@backoff.call current_retries
@on_retry&.call
current_retries += 1
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,57 @@
_(job.updated_row_count).must_be :nil?
end

it "recovers a query job when a retried insertion already exists" do
job_gapi = query_job_gapi query, location: nil
job_resp_gapi = query_job_resp_gapi query, job_id: job_gapi.job_reference.job_id

stub = Object.new
stub.define_singleton_method :insert_job do |*|
@insert_attempts ||= 0
@insert_attempts += 1
if @insert_attempts == 1
raise Google::Apis::Error.new "backend error",
status_code: 500,
body: { "error" => { "errors" => [{ "reason" => "backendError" }] } }.to_json
end

raise Google::Apis::ClientError.new "Already Exists",
status_code: 409,
body: { "error" => { "errors" => [{ "reason" => "duplicate" }] } }.to_json
end
expected_project = project
job_ref = job_gapi.job_reference
stub.define_singleton_method :get_job do |received_project, received_job_id, location:|
unless received_project == expected_project && received_job_id == job_ref.job_id && location == job_ref.location
raise "Unexpected get_job arguments"
end

job_resp_gapi
end
bigquery.service.mocked_service = stub

Google::Cloud::Bigquery::Service::Backoff.stub :backoff, -> { ->(_) {} } do
job = bigquery.query_job query

_(job).must_be_kind_of Google::Cloud::Bigquery::QueryJob
_(job.job_id).must_equal job_gapi.job_reference.job_id
end
end

it "raises when the initial query job insertion already exists" do
stub = Object.new
stub.define_singleton_method :insert_job do |*|
raise Google::Apis::ClientError.new "Already Exists",
status_code: 409,
body: { "error" => { "errors" => [{ "reason" => "duplicate" }] } }.to_json
end
bigquery.service.mocked_service = stub

assert_raises Google::Cloud::AlreadyExistsError do
bigquery.query_job query
end
end

it "queries the data with options set" do
mock = Minitest::Mock.new
bigquery.service.mocked_service = mock
Expand Down
Loading