Skip to content

Commit 1c8f630

Browse files
authored
fix: Get a message before redirecting. (#725)
* fix: Get a message before redirecting. The server generally won't know if a redirect is required until it sees the first message. Since this is the common case, do that in tests with redirect instructions. * fix lint * fix lint again * attempt to kick off lint again
1 parent 5e5c385 commit 1c8f630

File tree

2 files changed

+26
-28
lines changed

2 files changed

+26
-28
lines changed

gcs/upload.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -339,12 +339,34 @@ def insert_if_latest_is_unfinalized(latest_blob, live_generation):
339339
@classmethod
340340
def process_bidi_write_object_grpc(cls, db, request_iterator, context):
341341
"""Process a BidiWriteObject streaming RPC, and yield a stream of responses."""
342+
343+
def abort_with_redirect_error(routing_token, handle=None, generation=None):
344+
err = storage_pb2.BidiWriteObjectRedirectedError()
345+
if handle is not None:
346+
err.write_handle.handle = handle
347+
if generation is not None:
348+
err.generation = generation
349+
err.routing_token = routing_token
350+
detail = any_pb2.Any()
351+
detail.Pack(err)
352+
status_proto = status_pb2.Status(
353+
code=grpc.StatusCode.ABORTED.value[0],
354+
message=grpc.StatusCode.ABORTED.value[1],
355+
details=[detail],
356+
)
357+
context.abort_with_status(rpc_status.to_status(status_proto))
358+
342359
# Many tests use a list as the request_iterator
343360
request_iterator = iter(request_iterator)
344361
upload, object_checksums, is_resumable, is_appendable = None, None, False, False
345362
appendable_metadata_in_first_response = False
346363
try:
347364
first_msg = next(request_iterator)
365+
return_redirect_token = testbench.common.get_return_redirect_token(
366+
db, context
367+
)
368+
if return_redirect_token is not None:
369+
abort_with_redirect_error(return_redirect_token)
348370
except StopIteration:
349371
# At least one message is required. This function raises.
350372
testbench.error.invalid("Missing BidiWriteObjectRequest", context)
@@ -402,18 +424,11 @@ def process_bidi_write_object_grpc(cls, db, request_iterator, context):
402424
testbench.common.get_return_write_handle_and_redirect_token(db, context)
403425
)
404426
if return_redirect_token:
405-
err = storage_pb2.BidiWriteObjectRedirectedError()
406-
err.generation = upload.metadata.generation
407-
err.write_handle.handle = bytes(upload.upload_id, "utf-8")
408-
err.routing_token = return_redirect_token
409-
detail = any_pb2.Any()
410-
detail.Pack(err)
411-
status_proto = status_pb2.Status(
412-
code=grpc.StatusCode.ABORTED.value[0],
413-
message=grpc.StatusCode.ABORTED.value[1],
414-
details=[detail],
427+
abort_with_redirect_error(
428+
return_redirect_token,
429+
handle=bytes(upload.upload_id, "utf-8"),
430+
generation=upload.metadata.generation,
415431
)
416-
context.abort_with_status(rpc_status.to_status(status_proto))
417432

418433
# Treat the rest of the first message as a data request, then keep
419434
# pulling from request_iterator

testbench/grpc_server.py

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -895,23 +895,6 @@ def BidiWriteObject(self, request_iterator, context):
895895
test_id = testbench.common.get_retry_test_id_from_context(context)
896896
self.db.dequeue_next_instruction(test_id, "storage.objects.insert")
897897

898-
return_redirect_token = testbench.common.get_return_redirect_token(
899-
self.db, context
900-
)
901-
if return_redirect_token:
902-
detail = any_pb2.Any()
903-
detail.Pack(
904-
storage_pb2.BidiWriteObjectRedirectedError(
905-
routing_token=return_redirect_token
906-
)
907-
)
908-
status_proto = status_pb2.Status(
909-
code=grpc.StatusCode.ABORTED.value[0],
910-
message=grpc.StatusCode.ABORTED.value[1],
911-
details=[detail],
912-
)
913-
context.abort_with_status(rpc_status.to_status(status_proto))
914-
915898
return gcs.upload.Upload.process_bidi_write_object_grpc(
916899
self.db, request_iterator, context
917900
)

0 commit comments

Comments
 (0)